Skip to content
Snippets Groups Projects
Commit b52535b7 authored by Rainer Kartmann's avatar Rainer Kartmann
Browse files

Add max history size in ProviderSegment

parent b0119c42
No related branches found
No related tags found
1 merge request!89Implement core data structure of ArMem Memories
......@@ -34,21 +34,21 @@ namespace armarx::armem
it->second->update(update);
}
void Entity::setHistorySize(long maxSize)
void Entity::setMaxHistorySize(long maxSize)
{
this->historySize = maxSize;
this->maxHistorySize = maxSize;
truncateHistoryToSize();
}
void Entity::truncateHistoryToSize()
{
if (historySize >= 0)
if (maxHistorySize >= 0)
{
while (history.size() > size_t(historySize))
while (history.size() > size_t(maxHistorySize))
{
history.erase(history.begin());
}
ARMARX_CHECK_LESS_EQUAL(historySize, history.size());
ARMARX_CHECK_LESS_EQUAL(history.size(), maxHistorySize);
}
}
......
......@@ -33,7 +33,7 @@ namespace armarx::armem
*
* The current history is truncated if necessary.
*/
void setHistorySize(long maxSize);
void setMaxHistorySize(long maxSize);
private:
......@@ -52,7 +52,7 @@ namespace armarx::armem
*
* If negative, the size of `history` is not limited.
*/
long historySize = -1;
long maxHistorySize = -1;
};
......
......@@ -42,6 +42,7 @@ namespace armarx::armem
void update(const InternalEntityUpdate& update, int index);
};
using EntityDataPtr = std::unique_ptr<EntityData>;
}
......@@ -35,6 +35,7 @@ namespace armarx::armem
std::map<std::string, CoreSegmentPtr> coreSegments;
};
using MemoryPtr = std::unique_ptr<Memory>;
......
......@@ -4,6 +4,12 @@
namespace armarx::armem
{
Entity* ProviderSegment::getEntity(const std::string& name)
{
auto it = this->entities.find(name);
return it == entities.end() ? nullptr : it->second.get();
}
void ProviderSegment::update(const InternalEntityUpdate& update)
{
checkStorageName(update.entityID.providerSegmentName, this->name, "provider segment");
......@@ -14,9 +20,19 @@ namespace armarx::armem
// Add entity entry.
it = entities.emplace(update.entityID.entityName,
std::make_unique<Entity>(update.entityID.entityName)).first;
it->second->setMaxHistorySize(maxHistorySize);
}
// Update entity.
it->second->update(update);
}
void ProviderSegment::setMaxHistorySize(long maxSize)
{
this->maxHistorySize = maxSize;
for (auto& [name, entity] : entities)
{
entity->setMaxHistorySize(maxSize);
}
}
}
......@@ -25,6 +25,7 @@ namespace armarx::armem
{
return entities.count(name) > 0;
}
Entity* getEntity(const std::string& name);
/**
* @brief Updates an entity's history.
......@@ -34,10 +35,24 @@ namespace armarx::armem
virtual void update(const InternalEntityUpdate& update) override;
/**
* @brief Sets the maximum history size of entities in this segment.
* This affects all current entities as well as new ones.
* @see Entity::setMaxHistorySize()
*/
void setMaxHistorySize(long maxSize);
public:
std::map<std::string, EntityPtr> entities;
/**
* @brief Maximum size of entity histories.
* @see Entity::maxHstorySize
*/
long maxHistorySize = -1;
};
using ProviderSegmentPtr = std::unique_ptr<ProviderSegment>;
......
......@@ -103,8 +103,28 @@ BOOST_AUTO_TEST_CASE(test_segment_setup)
entity->update(update);
BOOST_CHECK_EQUAL(entity->history.size(), 3);
}
BOOST_AUTO_TEST_CASE(test_history_size_in_entity)
{
armem::EntityPtr entity = std::make_unique<armem::Entity>("entity");
armem::InternalEntityUpdate update;
update.entityID.entityName = entity->name;
// With unlimited history.
update.timeCreated = armem::Time::milliSeconds(1000);
entity->update(update);
update.timeCreated = armem::Time::milliSeconds(2000);
entity->update(update);
update.timeCreated = armem::Time::milliSeconds(3000);
entity->update(update);
BOOST_CHECK_EQUAL(entity->history.size(), 3);
// Now with maximum history size.
entity->setHistorySize(2);
entity->setMaxHistorySize(2);
BOOST_CHECK_EQUAL(entity->history.size(), 2);
BOOST_CHECK_EQUAL(entity->history.count(armem::Time::milliSeconds(1000)), 0);
BOOST_CHECK_EQUAL(entity->history.count(armem::Time::milliSeconds(2000)), 1);
......@@ -112,14 +132,14 @@ BOOST_AUTO_TEST_CASE(test_segment_setup)
update.timeCreated = armem::Time::milliSeconds(4000);
coreSegment->update(update);
entity->update(update);
BOOST_CHECK_EQUAL(entity->history.size(), 2);
BOOST_CHECK_EQUAL(entity->history.count(armem::Time::milliSeconds(2000)), 0);
BOOST_CHECK_EQUAL(entity->history.count(armem::Time::milliSeconds(3000)), 1);
BOOST_CHECK_EQUAL(entity->history.count(armem::Time::milliSeconds(4000)), 1);
// Disable maximum history size.
entity->setHistorySize(-1);
entity->setMaxHistorySize(-1);
update.timeCreated = armem::Time::milliSeconds(5000);
entity->update(update);
......@@ -127,5 +147,76 @@ BOOST_AUTO_TEST_CASE(test_segment_setup)
BOOST_CHECK_EQUAL(entity->history.count(armem::Time::milliSeconds(3000)), 1);
BOOST_CHECK_EQUAL(entity->history.count(armem::Time::milliSeconds(4000)), 1);
BOOST_CHECK_EQUAL(entity->history.count(armem::Time::milliSeconds(5000)), 1);
}
BOOST_AUTO_TEST_CASE(test_history_size_in_provider_segment)
{
armem::ProviderSegment providerSegment("SomeRGBImageProvider");
armem::InternalEntityUpdate update;
update.entityID.providerSegmentName = providerSegment.name;
std::vector<std::string> entityNames = { "A", "B" };
// Fill entities and histories with unlimited size.
for (const auto& name : entityNames)
{
update.entityID.entityName = name;
update.timeCreated = armem::Time::milliSeconds(1000);
providerSegment.update(update);
update.timeCreated = armem::Time::milliSeconds(2000);
providerSegment.update(update);
update.timeCreated = armem::Time::milliSeconds(3000);
providerSegment.update(update);
}
update.entityID.entityName = entityNames.back();
update.timeCreated = armem::Time::milliSeconds(4000);
providerSegment.update(update);
BOOST_CHECK_EQUAL(providerSegment.getEntity("A")->history.size(), 3);
BOOST_CHECK_EQUAL(providerSegment.getEntity("B")->history.size(), 4);
// Employ maximum history size.
providerSegment.setMaxHistorySize(3);
BOOST_CHECK_EQUAL(providerSegment.getEntity("A")->history.size(), 3);
BOOST_CHECK_EQUAL(providerSegment.getEntity("B")->history.size(), 3);
providerSegment.setMaxHistorySize(2);
BOOST_CHECK_EQUAL(providerSegment.getEntity("A")->history.size(), 2);
BOOST_CHECK_EQUAL(providerSegment.getEntity("B")->history.size(), 2);
providerSegment.setMaxHistorySize(3);
BOOST_CHECK_EQUAL(providerSegment.getEntity("A")->history.size(), 2);
BOOST_CHECK_EQUAL(providerSegment.getEntity("B")->history.size(), 2);
// Add new entity.
providerSegment.setMaxHistorySize(2);
update.entityID.entityName = "C";
update.timeCreated = armem::Time::milliSeconds(1000);
providerSegment.update(update);
update.timeCreated = armem::Time::milliSeconds(2000);
providerSegment.update(update);
update.timeCreated = armem::Time::milliSeconds(3000);
providerSegment.update(update);
// Check correctly inherited history size.
BOOST_CHECK_EQUAL(providerSegment.getEntity("C")->maxHistorySize, 2);
// Check actual history size.
BOOST_CHECK_EQUAL(providerSegment.getEntity("C")->history.size(), 2);
// Remove maximum.
providerSegment.setMaxHistorySize(-1);
entityNames.push_back("C");
for (const auto& name : entityNames)
{
update.entityID.entityName = name;
update.timeCreated = armem::Time::milliSeconds(5000);
providerSegment.update(update);
BOOST_CHECK_EQUAL(providerSegment.getEntity(name)->history.size(), 3);
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment