memory_definitions.cpp
Go to the documentation of this file.
1 #include "memory_definitions.h"
2 
3 #include "error.h"
4 
7 
9 
10 #include <map>
11 #include <vector>
12 
13 
15 {
16 
17  void Entity::setMaxHistorySize(long maxSize)
18  {
19  std::string containerID = (this->id()).str();
20  MaxHistorySize::setMaxHistorySize(maxSize, containerID);
21  truncate();
22  }
23 
24 
25  auto Entity::update(const EntityUpdate& update) -> UpdateResult
26  {
27  UpdateResult result = EntityBase::update(update);
28  result.removedSnapshots = this->truncate();
29  return result;
30  }
31 
32 
33  std::vector<EntitySnapshot> Entity::truncate()
34  {
35  std::vector<EntitySnapshot> removedElements;
36  if (_maxHistorySize >= 0)
37  {
38  while (this->_container.size() > size_t(_maxHistorySize))
39  {
40  removedElements.push_back(std::move(this->_container.begin()->second));
41  this->_container.erase(this->_container.begin());
42  }
43  ARMARX_CHECK_LESS_EQUAL(this->_container.size(), size_t(_maxHistorySize));
44  }
45  return removedElements;
46  }
47 
48 
49  // TODO: add core segment if param is set
50  std::vector<Memory::Base::UpdateResult>
52  {
53  // Group updates by core segment, then update each core segment in a batch to only lock it once.
54  std::map<std::string, std::vector<const EntityUpdate*>> updatesPerCoreSegment;
55  for (const EntityUpdate& update : commit.updates)
56  {
57  updatesPerCoreSegment[update.entityID.coreSegmentName].push_back(&update);
58  }
59 
60  std::vector<Memory::Base::UpdateResult> result;
61  // To throw an exception after the commit if a core segment is missing and the memory should not create new ones
62  std::vector<std::string> missingCoreSegmentNames;
63  for (const auto& [coreSegmentName, updates] : updatesPerCoreSegment)
64  {
65  auto it = this->_container.find(coreSegmentName);
66  if (it != this->_container.end())
67  {
68  CoreSegment& coreSegment = it->second;
69 
70  // Lock the core segment for the whole batch.
71  coreSegment.doLocked([&result, &coreSegment, updates = &updates]()
72  {
73  for (const EntityUpdate* update : *updates)
74  {
75  auto r = coreSegment.update(*update);
76  Base::UpdateResult ret { r };
77  ret.memoryUpdateType = UpdateType::UpdatedExisting;
78  result.push_back(ret);
79  }
80  });
81  }
82  else
83  {
84  // Perform the other updates first, then throw afterwards.
85  missingCoreSegmentNames.push_back(coreSegmentName);
86  }
87  }
88  // Throw an exception if something went wrong.
89  if (not missingCoreSegmentNames.empty())
90  {
91  // Just throw an exception for the first entry. We can extend this exception in the future.
92  throw armem::error::MissingEntry::create<CoreSegment>(missingCoreSegmentNames.front(), *this);
93  }
94  return result;
95  }
96 
97 
98  // TODO: Add core segment if param is set
99  Memory::Base::UpdateResult
101  {
102  this->_checkContainerName(update.entityID.memoryName, this->name());
103 
104  CoreSegment& segment = getCoreSegment(update.entityID.coreSegmentName);
105  Base::UpdateResult result;
106  segment.doLocked([&result, &segment, &update]()
107  {
108  result = segment.update(update);
109  });
110  result.memoryUpdateType = UpdateType::UpdatedExisting;
111  return result;
112  }
113 }
str
std::string str(const T &t)
Definition: UserAssistedSegmenterGuiWidgetController.cpp:42
armarx::aron::ret
ReaderT::InputType T & ret
Definition: rw.h:21
armarx::armem::Commit
A bundle of updates to be sent to the memory.
Definition: Commit.h:89
armarx::armem::server::wm::detail::MaxHistorySize::_maxHistorySize
long _maxHistorySize
Maximum size of entity histories.
Definition: MaxHistorySize.h:29
armarx::armem::base::MemoryBase< CoreSegment, Memory >::getCoreSegment
CoreSegmentT & getCoreSegment(const std::string &name)
Definition: MemoryBase.h:132
armarx::armem::server::wm::Entity::truncate
std::vector< EntitySnapshotT > truncate()
If maximum size is set, ensure history's is not higher.
Definition: memory_definitions.cpp:33
Dict.h
armarx::armem::UpdateType::UpdatedExisting
@ UpdatedExisting
armarx::armem::server::wm
Definition: forward_declarations.h:62
armarx::armem::Commit::updates
std::vector< EntityUpdate > updates
The entity updates.
Definition: Commit.h:97
memory_definitions.h
error.h
armarx::armem::base::CoreSegmentBase::update
UpdateResult update(const EntityUpdate &update)
Updates an entity's history.
Definition: CoreSegmentBase.h:284
armarx::armem::EntityUpdate
An update of an entity for a specific point in time.
Definition: Commit.h:27
armarx::armem::base::detail::MemoryItem::id
MemoryID & id()
Definition: MemoryItem.h:27
armarx::armem::server::wm::CoreSegment::doLocked
auto doLocked(FunctionT &&function) const
Definition: memory_definitions.h:120
armarx::armem::server::ltm::util::mongodb::detail::update
bool update(mongocxx::collection &coll, const nlohmann::json &query, const nlohmann::json &update)
Definition: mongodb.cpp:67
ARMARX_CHECK_LESS_EQUAL
#define ARMARX_CHECK_LESS_EQUAL(lhs, rhs)
This macro evaluates whether lhs is less or equal (<=) rhs and if it turns out to be false it will th...
Definition: ExpressionException.h:109
armarx::armem::MemoryID::memoryName
std::string memoryName
Definition: MemoryID.h:50
ExpressionException.h
armarx::armem::server::wm::CoreSegment
base::CoreSegmentBase
Definition: memory_definitions.h:86
armarx::armem::server::wm::Entity::setMaxHistorySize
void setMaxHistorySize(long maxSize)
Sets the maximum history size.
Definition: memory_definitions.cpp:17
armarx::armem::base::MemoryBase< CoreSegment, Memory >::update
std::vector< UpdateResult > update(const Commit &commit, const bool addMissingCoreSegmentDuringUpdate=false, const bool checkMemoryName=true)
Store all updates in commit.
Definition: MemoryBase.h:297
armarx::armem::base::detail::MemoryContainerBase< std::map< Time, EntitySnapshot >, Entity >::_container
ContainerT _container
Definition: MemoryContainerBase.h:150
armarx::armem::server::wm::Memory::updateLocking
std::vector< Base::UpdateResult > updateLocking(const Commit &commit)
Perform the commit, locking the core segments.
Definition: memory_definitions.cpp:51
armarx::armem::EntityUpdate::entityID
MemoryID entityID
The entity's ID.
Definition: Commit.h:30
armarx::armem::base::detail::MemoryContainerBase< std::map< std::string, CoreSegment >, Memory >::_checkContainerName
void _checkContainerName(const std::string &gottenName, const std::string &actualName, bool emptyOk=true) const
Definition: MemoryContainerBase.h:124
container_maps.h
armarx::armem::server::wm::Entity::update
UpdateResult update(const EntityUpdate &update)
Definition: memory_definitions.cpp:25