PersistentEntitySegment.h
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::Core
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
23#pragma once
24
25#include <mutex>
26
28#include <MemoryX/interface/components/CommonStorageInterface.h>
29#include <MemoryX/interface/core/MemoryInterface.h>
30
31namespace memoryx
32{
33 class MongoSerializer;
34 using MongoSerializerPtr = IceUtil::Handle<MongoSerializer>;
35
36 /**
37 \page MemoryX-HowTos-create-new-persisten-segment How to create a new persistent memory segment in MemoryX
38
39 \li Create Ice interface for new segment in MemoryX/interface/memorytypes/MemorySegments.ice which inherits from memoryx::PersistentEntitySegmentBase
40 \code
41 ["cpp:virtual"]
42 class PersistentDataSegmentBase extends PersistentEntitySegmentBase
43 {
44 // add some special methods here
45 };
46 \endcode
47
48 \li Create C++ header file
49 \code
50 #ifndef ARMARX_MEMORYX_NEW_DATA_SEGMENT_H
51 #define ARMARX_MEMORYX_NEW_DATA_SEGMENT_H
52
53 #include <ArmarXCore/core/system/ImportExportComponent.h>
54
55 #include "../entity/profiler/ProfilerTransition.h"
56 #include <MemoryX/core/memory/PersistentEntitySegment.h>
57 #include <MemoryX/interface/memorytypes/MemorySegments.h>
58
59 namespace memoryx
60 {
61 class ARMARXCOMPONENT_IMPORT_EXPORT PersistentDataSegment :
62 virtual public PersistentEntitySegment,
63 virtual public PersistentDataSegmentBase
64 {
65 public:
66 PersistentDataSegment(CollectionInterfacePrx entityCollection, Ice::CommunicatorPtr communicator, bool useMongoIds = true);
67
68 // implementation of Ice interface methods
69 ["cpp:const"]
70 hasDataEntity(string name);
71 };
72
73 using PersistentProfilerDataSegmentPtr = IceInternal::Handle<PersistentProfilerDataSegment>;
74 }
75
76 #endif
77 \endcode
78
79 \li Create C++ implementation file
80 \code
81 #include "PersistentDataSegment.h"
82
83 using namespace memoryx;
84
85 PersistentDataSegment::PersistentDataSegment(CollectionInterfacePrx entityCollection, Ice::CommunicatorPtr communicator, bool useMongoIds) :
86 PersistentEntitySegment(entityCollection, communicator, useMongoIds),
87 PersistentDataSegmentBase()
88 {
89 }
90
91 // implementation of further methods
92 \endcode
93
94 \li Add header and implementation files to the appropriate CMake target.
95
96 \li Add/register new segment in memoryx::PriorKnowledge or memoryx::LongtermMemory and add accessor functions to the recpective memory interface.
97 \code
98 \endcode
99
100 */
101
102 /**
103 * @brief The PersistentEntitySegment class is the base class for all memory segments containing memoryx::Entity instances to be stored permanently in MongoDB.
104 */
106 public virtual PersistentEntitySegmentBase,
107 public virtual SegmentUtilImplementations
108 {
109 public:
110 PersistentEntitySegment(CollectionInterfacePrx entityCollection,
112 bool useMongoIds = true);
113 ~PersistentEntitySegment() override;
114
115 NameList getReadCollectionsNS(const ::Ice::Current& = Ice::emptyCurrent) const override;
116 void clearReadCollections(const ::Ice::Current& = Ice::emptyCurrent) override;
117 void addReadCollection(const CollectionInterfacePrx& coll,
118 const ::Ice::Current& = Ice::emptyCurrent) override;
119 std::string getWriteCollectionNS(const ::Ice::Current& = Ice::emptyCurrent) const override;
120 void setWriteCollection(const CollectionInterfacePrx& coll,
121 const ::Ice::Current& = Ice::emptyCurrent) override;
122 void setSingleRWCollection(const CollectionInterfacePrx& coll,
123 const ::Ice::Current& = Ice::emptyCurrent) override;
124
125 std::string getObjectTypeId(const ::Ice::Current& = Ice::emptyCurrent) const override;
126
127 /**
128 * @brief addEntity add new \p entity and return the newly generated entity ID
129 * @param entity
130 * @return
131 */
132 std::string addEntity(const EntityBasePtr& entity,
133 const ::Ice::Current& c = Ice::emptyCurrent) override;
134 virtual std::string addEntityThreadUnsafe(const EntityBasePtr& entity);
135 std::string upsertEntity(const std::string& entityId,
136 const EntityBasePtr& entity,
137 const ::Ice::Current& = Ice::emptyCurrent) override;
138 virtual std::string upsertEntityThreadUnsafe(const std::string& entityId,
139 const EntityBasePtr& entity);
140 std::string upsertEntityByName(const std::string& entityName,
141 const EntityBasePtr& entity,
142 const ::Ice::Current& = Ice::emptyCurrent) override;
143 virtual std::string upsertEntityByNameThreadUnsafe(const std::string& entityName,
144 const EntityBasePtr& entity);
145 /**
146 * @brief addEntityList adds all entities contained in \entityList to this segment and returns a list of created entity IDs
147 * @return list of generated entity IDs
148 */
149 EntityIdList addEntityList(const EntityBaseList& entityList,
150 const Ice::Current& = Ice::emptyCurrent) override;
151 virtual EntityIdList addEntityListThreadUnsafe(const EntityBaseList& entityList);
152
153 EntityIdList upsertEntityList(const EntityBaseList& entityList,
154 const Ice::Current& = Ice::emptyCurrent) override;
155 virtual EntityIdList upsertEntityListThreadUnsafe(const EntityBaseList& entityList);
156
157 void updateEntity(const ::std::string& entityId,
158 const EntityBasePtr& update,
159 const ::Ice::Current& = Ice::emptyCurrent) override;
160 virtual void updateEntityThreadUnsafe(const ::std::string& entityId,
161 const EntityBasePtr& update);
162
163 /**
164 * @brief removeEntity removes an entity with the ID \p entityId
165 * @param entityId
166 */
167 void removeEntity(const ::std::string& entityId,
168 const ::Ice::Current& = Ice::emptyCurrent) override;
169 virtual void removeEntityThreadUnsafe(const ::std::string& entityId);
170 /**
171 * @brief removeAllEntities collects all entities managed by this memory segment and removes them from the segment
172 * @param c
173 */
174 void removeAllEntities(const ::Ice::Current& c = Ice::emptyCurrent) override;
175
176 bool hasEntityById(const std::string& entityId,
177 const Ice::Current& = Ice::emptyCurrent) const override;
178 bool hasEntityByIdThreadUnsafe(const std::string& entityId) const;
179 bool hasEntityByName(const std::string& entityName,
180 const Ice::Current& = Ice::emptyCurrent) const override;
181 bool hasEntityByNameThreadUnsafe(const std::string& entityName) const;
182
183 EntityBasePtr getEntityById(const ::std::string& entityId,
184 const ::Ice::Current& = Ice::emptyCurrent) const override;
185 virtual EntityBasePtr getEntityByIdThreadUnsafe(const ::std::string& entityId) const;
186 EntityBasePtr getEntityByName(const ::std::string& name,
187 const ::Ice::Current& = Ice::emptyCurrent) const override;
188 virtual EntityBasePtr getEntityByNameThreadUnsafe(const ::std::string& name) const;
189 EntityBaseList
190 getEntitiesByAttrValue(const ::std::string& attrName,
191 const ::std::string& attrValue,
192 const ::Ice::Current& = Ice::emptyCurrent) const override;
193 EntityBaseList
194 getEntitiesByAttrValueList(const ::std::string& attrName,
195 const NameList& attrValueList,
196 const ::Ice::Current& = Ice::emptyCurrent) const override;
197 EntityIdList getAllEntityIds(const ::Ice::Current& = Ice::emptyCurrent) const override;
198 virtual EntityIdList getAllEntityIdsThreadUnsafe() const;
199 /**
200 * @brief getAllEntities returns a list of all entities managed by this memory segment
201 * @return
202 */
203 EntityBaseList getAllEntities(const ::Ice::Current& = Ice::emptyCurrent) const override;
204 IdEntityMap getIdEntityMap(const ::Ice::Current& = Ice::emptyCurrent) const override;
205
206 /**
207 * @brief size counts the number of memoryx::Entity instances contained available reachable throuhg memoryx::PersistentEntitySegment::readCollections.
208 * @return number of entities contained in this memory segment
209 */
210 Ice::Int size(const ::Ice::Current& = Ice::emptyCurrent) const override;
211 void print(const ::Ice::Current& = Ice::emptyCurrent) const override;
212 /**
213 * @brief clear removes all elements from the current memoryx::PersistentEntitySegment::writeCollection
214 */
215 void clear(const ::Ice::Current& = Ice::emptyCurrent) override;
216
217 ::Ice::Identity getIceId(const ::Ice::Current& = Ice::emptyCurrent) const override;
218 std::string getSegmentName(const ::Ice::Current& = Ice::emptyCurrent) const override;
219
220 // EntityMemorySegmentInterface interface
221 public:
222 EntityRefBasePtr getEntityRefById(const std::string& id,
223 const Ice::Current&) const override;
224 EntityRefBasePtr getEntityRefByName(const std::string& name,
225 const Ice::Current& c) const override;
226
227 void setEntityAttribute(const std::string& entityId,
228 const EntityAttributeBasePtr& attribute,
229 const Ice::Current&) override;
230 void setEntityAttributes(const std::string& entityId,
231 const EntityAttributeList& attributeMap,
232 const Ice::Current&) override;
233
234 // AbstractMemorySegment interface
235 public:
236 void setParentMemory(const MemoryInterfacePtr& memory, const Ice::Current&) override;
237
238 // PersistentEntitySegmentBase interface
239 public:
240 /**
241 * @brief retrieves Entity Refs that match the query. The query
242 * should be a mongo query like: { "attrs.endNode.value.value.entityId": "54b2a43b43b5d3199e3cb5b3" }
243 * @param query
244 * @param c
245 * @return
246 */
247 EntityRefList findRefsByQuery(const std::string& query, const Ice::Current& c) override;
248
249 protected:
250 // set segment name
251 void setSegmentName(const std::string& segmentName,
252 const ::Ice::Current& = Ice::emptyCurrent) override;
253
254 MemoryInterfacePtr parentMemory;
255
256 CollectionPrxList readCollections;
257 // mutable boost::mutex readCollectionsMutex;
258
259 CollectionInterfacePrx writeCollection;
261 mutable std::recursive_mutex dbSerializerMutex;
262
264
265 EntityBasePtr deserializeEntity(const DBStorableData& dbEntity) const;
266 std::string segmentName;
267 };
268
270
271} // namespace memoryx
constexpr T c
void setSegmentName(const std::string &segmentName, const ::Ice::Current &=Ice::emptyCurrent) override
void setParentMemory(const MemoryInterfacePtr &memory, const Ice::Current &) override
PersistentEntitySegment(CollectionInterfacePrx entityCollection, Ice::CommunicatorPtr ic, bool useMongoIds=true)
virtual EntityBasePtr getEntityByIdThreadUnsafe(const ::std::string &entityId) const
EntityRefList findRefsByQuery(const std::string &query, const Ice::Current &c) override
retrieves Entity Refs that match the query.
virtual EntityIdList getAllEntityIdsThreadUnsafe() const
void clear(const ::Ice::Current &=Ice::emptyCurrent) override
clear removes all elements from the current memoryx::PersistentEntitySegment::writeCollection
void addReadCollection(const CollectionInterfacePrx &coll, const ::Ice::Current &=Ice::emptyCurrent) override
virtual std::string upsertEntityByNameThreadUnsafe(const std::string &entityName, const EntityBasePtr &entity)
std::string addEntity(const EntityBasePtr &entity, const ::Ice::Current &c=Ice::emptyCurrent) override
addEntity add new entity and return the newly generated entity ID
virtual std::string addEntityThreadUnsafe(const EntityBasePtr &entity)
virtual std::string upsertEntityThreadUnsafe(const std::string &entityId, const EntityBasePtr &entity)
bool hasEntityById(const std::string &entityId, const Ice::Current &=Ice::emptyCurrent) const override
void print(const ::Ice::Current &=Ice::emptyCurrent) const override
virtual EntityIdList upsertEntityListThreadUnsafe(const EntityBaseList &entityList)
std::string getSegmentName(const ::Ice::Current &=Ice::emptyCurrent) const override
EntityBasePtr getEntityById(const ::std::string &entityId, const ::Ice::Current &=Ice::emptyCurrent) const override
void setEntityAttribute(const std::string &entityId, const EntityAttributeBasePtr &attribute, const Ice::Current &) override
std::string getObjectTypeId(const ::Ice::Current &=Ice::emptyCurrent) const override
std::string upsertEntityByName(const std::string &entityName, const EntityBasePtr &entity, const ::Ice::Current &=Ice::emptyCurrent) override
NameList getReadCollectionsNS(const ::Ice::Current &=Ice::emptyCurrent) const override
EntityIdList upsertEntityList(const EntityBaseList &entityList, const Ice::Current &=Ice::emptyCurrent) override
bool hasEntityByIdThreadUnsafe(const std::string &entityId) const
void removeAllEntities(const ::Ice::Current &c=Ice::emptyCurrent) override
removeAllEntities collects all entities managed by this memory segment and removes them from the segm...
void setSingleRWCollection(const CollectionInterfacePrx &coll, const ::Ice::Current &=Ice::emptyCurrent) override
EntityIdList getAllEntityIds(const ::Ice::Current &=Ice::emptyCurrent) const override
void clearReadCollections(const ::Ice::Current &=Ice::emptyCurrent) override
virtual EntityBasePtr getEntityByNameThreadUnsafe(const ::std::string &name) const
std::string upsertEntity(const std::string &entityId, const EntityBasePtr &entity, const ::Ice::Current &=Ice::emptyCurrent) override
virtual EntityIdList addEntityListThreadUnsafe(const EntityBaseList &entityList)
virtual void updateEntityThreadUnsafe(const ::std::string &entityId, const EntityBasePtr &update)
virtual void removeEntityThreadUnsafe(const ::std::string &entityId)
bool hasEntityByName(const std::string &entityName, const Ice::Current &=Ice::emptyCurrent) const override
EntityBaseList getEntitiesByAttrValue(const ::std::string &attrName, const ::std::string &attrValue, const ::Ice::Current &=Ice::emptyCurrent) const override
::Ice::Identity getIceId(const ::Ice::Current &=Ice::emptyCurrent) const override
std::string getWriteCollectionNS(const ::Ice::Current &=Ice::emptyCurrent) const override
EntityBasePtr deserializeEntity(const DBStorableData &dbEntity) const
EntityBaseList getAllEntities(const ::Ice::Current &=Ice::emptyCurrent) const override
getAllEntities returns a list of all entities managed by this memory segment
bool hasEntityByNameThreadUnsafe(const std::string &entityName) const
void removeEntity(const ::std::string &entityId, const ::Ice::Current &=Ice::emptyCurrent) override
removeEntity removes an entity with the ID entityId
void setWriteCollection(const CollectionInterfacePrx &coll, const ::Ice::Current &=Ice::emptyCurrent) override
EntityIdList addEntityList(const EntityBaseList &entityList, const Ice::Current &=Ice::emptyCurrent) override
addEntityList adds all entities contained in \entityList to this segment and returns a list of create...
EntityRefBasePtr getEntityRefByName(const std::string &name, const Ice::Current &c) const override
void setEntityAttributes(const std::string &entityId, const EntityAttributeList &attributeMap, const Ice::Current &) override
EntityBaseList getEntitiesByAttrValueList(const ::std::string &attrName, const NameList &attrValueList, const ::Ice::Current &=Ice::emptyCurrent) const override
IdEntityMap getIdEntityMap(const ::Ice::Current &=Ice::emptyCurrent) const override
Ice::Int size(const ::Ice::Current &=Ice::emptyCurrent) const override
size counts the number of memoryx::Entity instances contained available reachable throuhg memoryx::Pe...
EntityRefBasePtr getEntityRefById(const std::string &id, const Ice::Current &) const override
void updateEntity(const ::std::string &entityId, const EntityBasePtr &update, const ::Ice::Current &=Ice::emptyCurrent) override
EntityBasePtr getEntityByName(const ::std::string &name, const ::Ice::Current &=Ice::emptyCurrent) const override
::IceInternal::Handle<::Ice::Communicator > CommunicatorPtr
Definition IceManager.h:49
VirtualRobot headers.
IceUtil::Handle< PersistentEntitySegment > PersistentEntitySegmentPtr
IceUtil::Handle< MongoSerializer > MongoSerializerPtr
Definition EntityRef.h:41