MemoryManager.cpp
Go to the documentation of this file.
1 // Header
2 #include "MemoryManager.h"
3 
4 // Simox
5 #include <SimoxUtility/json.h>
6 
7 // ArmarX
10 
14 
15 #include "operations.h"
16 
18 {
19  namespace bsoncxxbuilder = bsoncxx::builder::stream;
20  namespace bsoncxxdoc = bsoncxx::document;
21 
22  PoolClientPtr Memory::checkConnection() const
23  {
24  // Check connection:
26  {
27  ARMARX_WARNING << deactivateSpam("LTM_ConnectionError_" + cache.name())
28  << "The connection to mongocxx for ltm '" << cache.name() << "' is not valid. Settings are: " << dbsettings.toString()
29  << "\nTo start it, run e.g.: \n"
30  << "armarx memory start"
31  << "\n\n";
32  return nullptr;
33  }
34 
36  auto client = pool.acquire();
37 
38  return client;
39  }
40 
42  {
43  TIMING_START(LTM_Reload);
44  ARMARX_DEBUG << "(Re)Establishing connection to: " << dbsettings.toString();
45 
46  auto client = checkConnection();
47  if (!client)
48  {
49  return;
50  }
51 
52  std::lock_guard l(ltm_mutex);
53  auto databases = client->list_databases();
54  std::stringstream ss;
55  ss << "Found Memory-Collection in MongoDB for '" + cache.name() + "': \n";
56  for (const auto& doc : databases)
57  {
58  auto el = doc["name"];
59  ss << "\t - " << el.get_utf8().value << "\n";
60  }
61  ARMARX_DEBUG << ss.str();
62 
63  armem::wm::Memory temp(lut.id());
64  mongocxx::database db = client->database(dbsettings.database);
65  util::load(db, temp);
66 
67  lut.append(temp);
68 
69  TIMING_END_STREAM(LTM_Reload, ARMARX_DEBUG);
70  }
71 
73  {
74  TIMING_START(LTM_Convert);
75 
76  auto client = checkConnection();
77  if (!client)
78  {
79  return;
80  }
81 
82  std::lock_guard l(ltm_mutex);
83  mongocxx::database db = client->database(dbsettings.database);
84 
85  util::convert(db, m);
86 
87  TIMING_END_STREAM(LTM_Convert, ARMARX_DEBUG);
88  }
89 
91  {
92  TIMING_START(LTM_Encode);
93 
94  auto client = checkConnection();
95  if (!client)
96  {
97  return;
98  }
99 
100  std::lock_guard l(ltm_mutex);
101  mongocxx::database db = client->database(dbsettings.database);
102  util::store(db, cache);
103 
104  // what to do with clear text data after encoding?
105  // TODO!
106 
107  // Finaly clear cache and put reference to lut
108  moveCacheToLUTAndClearCache();
109 
110  TIMING_END_STREAM(LTM_Encode, ARMARX_DEBUG);
111  }
112 }
armarx::armem::server::ltm::mongodb::Memory::convert
void convert(armem::wm::Memory &) override
Definition: MemoryManager.cpp:72
TIMING_START
#define TIMING_START(name)
Definition: TimeUtil.h:280
operations.h
armarx::armem::server::ltm::mongodb::util::convert
void convert(const mongocxx::database &db, armem::wm::Memory &m)
Definition: operations.cpp:166
armarx::armem::server::ltm::mongodb::Memory::reload
void reload() override
Definition: MemoryManager.cpp:41
armarx::armem::server::ltm::mongodb::ConnectionManager::MongoDBSettings::database
std::string database
Definition: ConnectionManager.h:39
armarx::armem::server::ltm::mongodb::ConnectionManager::Connect
static mongocxx::pool & Connect(const MongoDBSettings &settings)
Definition: ConnectionManager.cpp:104
aron_conversions.h
Dict.h
TIMING_END_STREAM
#define TIMING_END_STREAM(name, os)
Definition: TimeUtil.h:300
armarx::armem::server::ltm::mongodb::ConnectionManager::MongoDBSettings::toString
std::string toString() const
Definition: ConnectionManager.cpp:89
NLohmannJSONConverter.h
deactivateSpam
SpamFilterDataPtr deactivateSpam(SpamFilterDataPtr const &spamFilter, float deactivationDurationSec, const std::string &identifier, bool deactivate)
Definition: Logging.cpp:72
armarx::armem::server::ltm::mongodb
Definition: ConnectionManager.cpp:7
armarx::armem::server::ltm::mongodb::util::store
void store(const mongocxx::database &db, const armem::wm::Memory &m)
Definition: operations.cpp:237
ARMARX_DEBUG
#define ARMARX_DEBUG
Definition: Logging.h:177
armarx::armem::server::ltm::mongodb::Memory::dbsettings
ConnectionManager::MongoDBSettings dbsettings
Definition: MemoryManager.h:35
armarx::armem::wm::Memory
Client-side working memory.
Definition: memory_definitions.h:133
armarx::armem::server::ltm::mongodb::Memory::encodeAndStore
void encodeAndStore() override
Definition: MemoryManager.cpp:90
armarx::armem::server::ltm::mongodb::PoolClientPtr
mongocxx::pool::entry PoolClientPtr
Definition: ConnectionManager.h:23
armarx::armem::server::ltm::mongodb::ConnectionManager::ConnectionIsValid
static bool ConnectionIsValid(const MongoDBSettings &settings, bool forceNewConnection=false)
Definition: ConnectionManager.cpp:124
TimeUtil.h
Logging.h
ARMARX_WARNING
#define ARMARX_WARNING
Definition: Logging.h:186
armarx::armem::server::ltm::mongodb::util::load
void load(const mongocxx::database &db, armem::wm::Memory &m)
Definition: operations.cpp:40
MemoryManager.h