WorkingMemorySnapshotListSegment.cpp
Go to the documentation of this file.
1/*
2* This file is part of ArmarX.
3*
4* ArmarX is free software; you can redistribute it and/or modify
5* it under the terms of the GNU General Public License version 2 as
6* published by the Free Software Foundation.
7*
8* ArmarX is distributed in the hope that it will be useful, but
9* WITHOUT ANY WARRANTY; without even the implied warranty of
10* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11* GNU General Public License for more details.
12*
13* You should have received a copy of the GNU General Public License
14* along with this program. If not, see <http://www.gnu.org/licenses/>.
15*
16* @package MemoryX::LongtermMemory
17* @author Alexey Kozlov ( kozlov at kit dot edu)
18* @date 2013
19* @copyright http://www.gnu.org/licenses/gpl-2.0.txt
20* GNU General Public License
21*/
22
24
25#include <Ice/ObjectAdapter.h>
26
28
32
33//#include <MemoryX/updater/ObjectLocalization/MemoryXUpdaterObjectFactories.h>
34//#include <ArmarXCore/observers/ObserverObjectFactories.h>
35
36namespace memoryx
37{
38 const std::string SNAPSHOT_NAME_FIELD = "name";
39
41 const DatabaseInterfacePrx& databasePrx,
42 const CollectionInterfacePrx& collection,
44 databasePrx(databasePrx), snapshotListCollection(collection), ic(ic)
45 {
46 dbSerializer = new MongoSerializer(ic, true);
47 snapshotListCollection->ensureIndex(SNAPSHOT_NAME_FIELD, true);
48 }
49
53
54 WorkingMemorySnapshotInterfacePrx
56 const std::string& name,
57 const AbstractWorkingMemoryInterfacePrx& workingMemory,
58 const Ice::Current& c)
59 {
60 removeSnapshot(name);
61 WorkingMemorySnapshotPtr snapshot = new WorkingMemorySnapshot(name, ic, databasePrx);
62
63 // store all WM segments
64 snapshot->saveWorkingMemory(workingMemory);
65
66 const DBStorableData dbSnapshot = dbSerializer->serialize(snapshot);
67 snapshotListCollection->saveWithUserKey(dbSnapshot, SNAPSHOT_NAME_FIELD);
68 return createSnapshotProxy(snapshot, c);
69 }
70
71 WorkingMemorySnapshotInterfacePrx
73 const std::string& name,
74 const AbstractWorkingMemoryInterfacePrx& workingMemory,
75 const Ice::StringSeq& entityIdList,
76 const Ice::Current& c)
77 {
78 removeSnapshot(name);
79 WorkingMemorySnapshotPtr snapshot = new WorkingMemorySnapshot(name, ic, databasePrx);
80
81 // store all WM segments
82 snapshot->saveWorkingMemorySubset(workingMemory, entityIdList);
83
84 const DBStorableData dbSnapshot = dbSerializer->serialize(snapshot);
85 snapshotListCollection->saveWithUserKey(dbSnapshot, SNAPSHOT_NAME_FIELD);
86 return createSnapshotProxy(snapshot, c);
87 }
88
89 WorkingMemorySnapshotInterfacePrx
90 WorkingMemorySnapshotListSegment::openSnapshot(const std::string& name, const Ice::Current& c)
91 {
92 WorkingMemorySnapshotPtr snapshot = findSnapshot(name);
93
94 if (snapshot)
95 {
96 return createSnapshotProxy(snapshot, c);
97 }
98 else
99 throw SnapshotNotFoundException(
100 "Snapshot not found, check that corresponding record exists in " +
101 snapshotListCollection->getNS() + " collection",
102 name);
103 }
104
105 void
107 const WorkingMemorySnapshotInterfacePrx& snapshot,
108 const Ice::Current&)
109 {
110 SnapshotMap::iterator itSnap = openedSnapshots.find(snapshot->ice_getIdentity());
111
112 if (itSnap != openedSnapshots.end())
113 {
114 openedSnapshots.erase(itSnap);
115 }
116 }
117
118 void
120 const std::string& name,
121 const AbstractWorkingMemoryInterfacePrx& workingMemory,
122 const Ice::Current& c)
123 {
124 try
125 {
126 WorkingMemorySnapshotPtr snapshot = findSnapshot(name);
127
128 if (snapshot)
129 {
130 snapshot->restoreWorkingMemory(workingMemory);
131 }
132 else
133 {
134 throw SnapshotNotFoundException("SnapshotNotFound", name);
135 }
136 }
137 catch (...)
138 {
140 }
141 }
142
143 bool
144 WorkingMemorySnapshotListSegment::removeSnapshot(const std::string& name, const Ice::Current&)
145 {
146 WorkingMemorySnapshotPtr snapshot = findSnapshot(name);
147
148 if (snapshot)
149 {
150 snapshot->clear();
151 snapshotListCollection->removeByFieldValue(SNAPSHOT_NAME_FIELD, name);
152 return true;
153 }
154 else
155 {
156 return false;
157 }
158 }
159
160 NameList
162 {
163 return snapshotListCollection->findAllFieldValues(SNAPSHOT_NAME_FIELD);
164 }
165
166 WorkingMemorySnapshotInterfacePrx
167 WorkingMemorySnapshotListSegment::createSnapshotProxy(const WorkingMemorySnapshotPtr& snapshot,
168 const Ice::Current& c)
169 {
170 Ice::Identity snapId = snapshot->getIceId();
171 openedSnapshots[snapId] = snapshot;
172 Ice::ObjectPrx node = c.adapter->add(snapshot, snapId);
173 return WorkingMemorySnapshotInterfacePrx::uncheckedCast(node);
174 }
175
177 WorkingMemorySnapshotListSegment::findSnapshot(const std::string& name)
178 {
179 DBStorableData dbSnapshot =
180 snapshotListCollection->findOneByFieldValue(SNAPSHOT_NAME_FIELD, name);
181
182 if (!dbSnapshot.JSON.empty())
183 {
184 WorkingMemorySnapshotPtr snapshot = new WorkingMemorySnapshot(name, ic, databasePrx);
185 dbSerializer->deserialize(dbSnapshot, snapshot);
186 return snapshot;
187 }
188 else
189 {
191 }
192 }
193
194 Ice::Identity
196 {
197 Ice::Identity id;
198 id.name = (parentMemory ? parentMemory->getMemoryName() : "") + "_" + segmentName;
199 return id;
200 }
201
202 Ice::Int
203 WorkingMemorySnapshotListSegment::size(const ::Ice::Current&) const
204 {
205 return snapshotListCollection->count();
206 }
207
208 void
210 {
211 const NameList snapshotNames = getSnapshotNames();
212
213 for (NameList::const_iterator it = snapshotNames.begin(); it != snapshotNames.end(); ++it)
214 {
215 removeSnapshot(*it);
216 }
217 }
218
219 void
220 WorkingMemorySnapshotListSegment::print(const ::Ice::Current&) const
221 {
222 // TODO implement
223 }
224} // namespace memoryx
constexpr T c
bool removeSnapshot(const ::std::string &name, const ::Ice::Current &=Ice::emptyCurrent) override
NameList getSnapshotNames(const ::Ice::Current &=Ice::emptyCurrent) const override
void clear(const ::Ice::Current &=Ice::emptyCurrent) override
void print(const ::Ice::Current &=Ice::emptyCurrent) const override
void loadSnapshot(const ::std::string &name, const AbstractWorkingMemoryInterfacePrx &workingMemory, const ::Ice::Current &=Ice::emptyCurrent) override
WorkingMemorySnapshotInterfacePrx createSnapshot(const ::std::string &name, const AbstractWorkingMemoryInterfacePrx &workingMemory, const ::Ice::Current &=Ice::emptyCurrent) override
void closeSnapshot(const WorkingMemorySnapshotInterfacePrx &snapshot, const ::Ice::Current &=Ice::emptyCurrent) override
WorkingMemorySnapshotListSegment(const DatabaseInterfacePrx &databasePrx, const CollectionInterfacePrx &collection, Ice::CommunicatorPtr ic)
Ice::Identity getIceId(const Ice::Current &) const override
WorkingMemorySnapshotInterfacePrx openSnapshot(const ::std::string &name, const ::Ice::Current &=Ice::emptyCurrent) override
Ice::Int size(const ::Ice::Current &=Ice::emptyCurrent) const override
WorkingMemorySnapshotInterfacePrx createSubsetSnapshot(const ::std::string &name, const AbstractWorkingMemoryInterfacePrx &workingMemory, const Ice::StringSeq &entityIdList, const ::Ice::Current &c=Ice::emptyCurrent) override
The WorkingMemorySnapshot class handles snapshot IO methods.
::IceInternal::Handle<::Ice::Communicator > CommunicatorPtr
Definition IceManager.h:49
void handleExceptions()
VirtualRobot headers.
IceUtil::Handle< WorkingMemorySnapshot > WorkingMemorySnapshotPtr
const std::string SNAPSHOT_NAME_FIELD