diff --git a/source/RobotAPI/components/armem/server/RobotStateMemory/RobotStateMemory.cpp b/source/RobotAPI/components/armem/server/RobotStateMemory/RobotStateMemory.cpp index 2867abe0aac643a20cd6ce2c16b8dc3140b36188..c269bd56e747159919ce7358739e6964f20d6355 100644 --- a/source/RobotAPI/components/armem/server/RobotStateMemory/RobotStateMemory.cpp +++ b/source/RobotAPI/components/armem/server/RobotStateMemory/RobotStateMemory.cpp @@ -69,6 +69,7 @@ namespace armarx::armem::server::robot_state const std::string prefix = "mem."; workingMemory.name() = "RobotState"; + longtermMemory.name() = "RobotState"; defs->optional(workingMemory.name(), prefix + "MemoryName", "Name of this memory server."); const std::string robotUnitPrefix{sensorValuePrefix}; diff --git a/source/RobotAPI/libraries/armem/core/base/detail/MemoryContainerBase.h b/source/RobotAPI/libraries/armem/core/base/detail/MemoryContainerBase.h index 1a6a216653d243da9613ec967363dc5d44b5c503..518e3f877f38ea364657c0a5448d48bc6cf36fbf 100644 --- a/source/RobotAPI/libraries/armem/core/base/detail/MemoryContainerBase.h +++ b/source/RobotAPI/libraries/armem/core/base/detail/MemoryContainerBase.h @@ -96,7 +96,7 @@ namespace armarx::armem::base::detail { DerivedT d; this->_copySelfEmpty(d); - return d; + return std::move(d); } diff --git a/source/RobotAPI/libraries/armem/core/longtermmemory/Memory.cpp b/source/RobotAPI/libraries/armem/core/longtermmemory/Memory.cpp index 570a861580ae9d66c9082c89ac0905a946dcb944..95ad5af75460d3f106b81f5fcc9d4c11ebb2038d 100644 --- a/source/RobotAPI/libraries/armem/core/longtermmemory/Memory.cpp +++ b/source/RobotAPI/libraries/armem/core/longtermmemory/Memory.cpp @@ -10,6 +10,54 @@ namespace armarx::armem::ltm { + Memory::Memory(const Memory& other) : + Base(other), + dbsettings(other.dbsettings), + alwaysTransferSettings(other.alwaysTransferSettings), + periodicTransferSettings(other.periodicTransferSettings), + onFullTransferSettings(other.onFullTransferSettings), + mongoDBMutex() + { + // Do not copy _mutex. + } + + + Memory::Memory(Memory&& other) : + Base(other), + dbsettings(other.dbsettings), + alwaysTransferSettings(other.alwaysTransferSettings), + periodicTransferSettings(other.periodicTransferSettings), + onFullTransferSettings(other.onFullTransferSettings) + { + // Do not move _mutex. + } + + + Memory& Memory::operator=(const Memory& other) + { + Base::operator=(other); + dbsettings = other.dbsettings; + alwaysTransferSettings = other.alwaysTransferSettings; + periodicTransferSettings = other.periodicTransferSettings; + onFullTransferSettings = other.onFullTransferSettings; + + // Don't copy _mutex. + return *this; + } + + + Memory& Memory::operator=(Memory&& other) + { + Base::operator=(other); + dbsettings = std::move(other.dbsettings); + alwaysTransferSettings = std::move(other.alwaysTransferSettings); + periodicTransferSettings = std::move(other.periodicTransferSettings); + onFullTransferSettings = std::move(other.onFullTransferSettings); + + // Don't move _mutex. + return *this; + } + bool Memory::checkConnection() const { // Check connection: @@ -67,22 +115,32 @@ namespace armarx::armem::ltm return; } + std::scoped_lock l(mongoDBMutex); ARMARX_INFO << "(Re)Establishing connection to: " << dbsettings.toString(); _container.clear(); ARMARX_TRACE; mongocxx::client& client = MongoDBConnectionManager::EstablishConnection(dbsettings); ARMARX_TRACE; - if (not(bool) client) + if (!client) { ARMARX_ERROR << "A client has died. Could not reload."; return; } + + auto databases = client.list_databases(); + for (const auto& doc : databases) + { + nlohmann::json json = nlohmann::json::parse(bsoncxx::to_json(doc)); + ARMARX_INFO << "Found the database: " << json.at("name"); + } + mongocxx::database db = client[dbsettings.database]; + ARMARX_INFO << "Getting collection for id: " << id().str(); mongocxx::collection coll = db[id().str()]; mongocxx::cursor cursor = coll.find({}); - for (auto doc : cursor) + for (const auto& doc : cursor) { nlohmann::json json = nlohmann::json::parse(bsoncxx::to_json(doc)); ARMARX_INFO << "Memory: Found foreign key: " << json.at("foreign_key"); @@ -115,6 +173,7 @@ namespace armarx::armem::ltm return; } + std::scoped_lock l(mongoDBMutex); ARMARX_INFO << "Merge memory with name '" << m.name() << "' into the LTM with name '" << name() << "'"; TIMING_START(LTM_Append); diff --git a/source/RobotAPI/libraries/armem/core/longtermmemory/Memory.h b/source/RobotAPI/libraries/armem/core/longtermmemory/Memory.h index a11404eca5dba8c7eb965e6fb4da8042ae234327..43b67c82ca91a09e875f1c3f85949a45e4f2c83f 100644 --- a/source/RobotAPI/libraries/armem/core/longtermmemory/Memory.h +++ b/source/RobotAPI/libraries/armem/core/longtermmemory/Memory.h @@ -55,6 +55,12 @@ namespace armarx::armem::ltm Mode mode; }; + + Memory(const Memory& other); + Memory(Memory&& other); + Memory& operator=(const Memory& other); + Memory& operator=(Memory&& other); + // PropertyDefinitions related to LTM void defineProperties(armarx::PropertyDefinitionsPtr defs, const std::string& prefix = ""); @@ -88,6 +94,9 @@ namespace armarx::armem::ltm PeriodicTransferSettings periodicTransferSettings; OnFullTransferSettings onFullTransferSettings; + private: + mutable std::mutex mongoDBMutex; + }; } diff --git a/source/RobotAPI/libraries/armem/server/MemoryToIceAdapter.cpp b/source/RobotAPI/libraries/armem/server/MemoryToIceAdapter.cpp index b8634f385021c9c22ba9dea22d8592a19271767e..0d6685c451645176371d6483e0795d6a558c8945 100644 --- a/source/RobotAPI/libraries/armem/server/MemoryToIceAdapter.cpp +++ b/source/RobotAPI/libraries/armem/server/MemoryToIceAdapter.cpp @@ -177,7 +177,7 @@ namespace armarx::armem::server { if (longtermMemory->alwaysTransferSettings.enabled) { - wm::Memory tmp("tmp"); + wm::Memory tmp(longtermMemory->name()); tmp.update(update); longtermMemory->append(tmp); }