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  MaxHistorySize::setMaxHistorySize(maxSize);
20  truncate();
21  }
22 
23 
24  auto Entity::update(const EntityUpdate& update) -> UpdateResult
25  {
26  UpdateResult result = EntityBase::update(update);
27  result.removedSnapshots = this->truncate();
28  return result;
29  }
30 
31 
32  std::vector<EntitySnapshot> Entity::truncate()
33  {
34  std::vector<EntitySnapshot> removedElements;
35  if (_maxHistorySize >= 0)
36  {
37  while (this->_container.size() > size_t(_maxHistorySize))
38  {
39  removedElements.push_back(std::move(this->_container.begin()->second));
40  this->_container.erase(this->_container.begin());
41  }
42  ARMARX_CHECK_LESS_EQUAL(this->_container.size(), size_t(_maxHistorySize));
43  }
44  return removedElements;
45  }
46 
47 
48  // TODO: add core segment if param is set
49  std::vector<Memory::Base::UpdateResult>
51  {
52  // Group updates by core segment, then update each core segment in a batch to only lock it once.
53  std::map<std::string, std::vector<const EntityUpdate*>> updatesPerCoreSegment;
54  for (const EntityUpdate& update : commit.updates)
55  {
56  updatesPerCoreSegment[update.entityID.coreSegmentName].push_back(&update);
57  }
58 
59  std::vector<Memory::Base::UpdateResult> result;
60  // To throw an exception after the commit if a core segment is missing and the memory should not create new ones
61  std::vector<std::string> missingCoreSegmentNames;
62  for (const auto& [coreSegmentName, updates] : updatesPerCoreSegment)
63  {
64  auto it = this->_container.find(coreSegmentName);
65  if (it != this->_container.end())
66  {
67  CoreSegment& coreSegment = it->second;
68 
69  // Lock the core segment for the whole batch.
70  coreSegment.doLocked([&result, &coreSegment, updates = &updates]()
71  {
72  for (const EntityUpdate* update : *updates)
73  {
74  auto r = coreSegment.update(*update);
75  Base::UpdateResult ret { r };
76  ret.memoryUpdateType = UpdateType::UpdatedExisting;
77  result.push_back(ret);
78  }
79  });
80  }
81  else
82  {
83  // Perform the other updates first, then throw afterwards.
84  missingCoreSegmentNames.push_back(coreSegmentName);
85  }
86  }
87  // Throw an exception if something went wrong.
88  if (not missingCoreSegmentNames.empty())
89  {
90  // Just throw an exception for the first entry. We can extend this exception in the future.
91  throw armem::error::MissingEntry::create<CoreSegment>(missingCoreSegmentNames.front(), *this);
92  }
93  return result;
94  }
95 
96 
97  // TODO: Add core segment if param is set
98  Memory::Base::UpdateResult
100  {
101  this->_checkContainerName(update.entityID.memoryName, this->name());
102 
103  CoreSegment& segment = getCoreSegment(update.entityID.coreSegmentName);
104  Base::UpdateResult result;
105  segment.doLocked([&result, &segment, &update]()
106  {
107  result = segment.update(update);
108  });
109  result.memoryUpdateType = UpdateType::UpdatedExisting;
110  return result;
111  }
112 }
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:32
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::server::wm::CoreSegment::doLocked
auto doLocked(FunctionT &&function) const
Definition: memory_definitions.h:112
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:50
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:24