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
23 Memory::checkConnection() const
24 {
25 // Check connection:
27 {
28 ARMARX_WARNING << deactivateSpam("LTM_ConnectionError_" + cache.name())
29 << "The connection to mongocxx for ltm '" << cache.name()
30 << "' is not valid. Settings are: " << dbsettings.toString()
31 << "\nTo start it, run e.g.: \n"
32 << "armarx memory start"
33 << "\n\n";
34 return nullptr;
35 }
36
38 auto client = pool.acquire();
39
40 return client;
41 }
42
43 void
45 {
46 TIMING_START(LTM_Reload);
47 ARMARX_DEBUG << "(Re)Establishing connection to: " << dbsettings.toString();
48
49 auto client = checkConnection();
50 if (!client)
51 {
52 return;
53 }
54
55 std::lock_guard l(ltm_mutex);
56 auto databases = client->list_databases();
57 std::stringstream ss;
58 ss << "Found Memory-Collection in MongoDB for '" + cache.name() + "': \n";
59 for (const auto& doc : databases)
60 {
61 auto el = doc["name"];
62 ss << "\t - " << el.get_utf8().value << "\n";
63 }
64 ARMARX_DEBUG << ss.str();
65
66 armem::wm::Memory temp(lut.id());
67 mongocxx::database db = client->database(dbsettings.database);
68 util::load(db, temp);
69
70 lut.append(temp);
71
73 }
74
75 void
77 {
78 TIMING_START(LTM_Convert);
79
80 auto client = checkConnection();
81 if (!client)
82 {
83 return;
84 }
85
86 std::lock_guard l(ltm_mutex);
87 mongocxx::database db = client->database(dbsettings.database);
88
89 util::convert(db, m);
90
91 TIMING_END_STREAM(LTM_Convert, ARMARX_DEBUG);
92 }
93
94 void
96 {
97 TIMING_START(LTM_Encode);
98
99 auto client = checkConnection();
100 if (!client)
101 {
102 return;
103 }
104
105 std::lock_guard l(ltm_mutex);
106 mongocxx::database db = client->database(dbsettings.database);
107 util::store(db, cache);
108
109 // what to do with clear text data after encoding?
110 // TODO!
111
112 // Finaly clear cache and put reference to lut
113 moveCacheToLUTAndClearCache();
114
115 TIMING_END_STREAM(LTM_Encode, ARMARX_DEBUG);
116 }
117} // namespace armarx::armem::server::ltm::mongodb
SpamFilterDataPtr deactivateSpam(SpamFilterDataPtr const &spamFilter, float deactivationDurationSec, const std::string &identifier, bool deactivate)
Definition Logging.cpp:75
static mongocxx::pool & Connect(const MongoDBSettings &settings)
static bool ConnectionIsValid(const MongoDBSettings &settings, bool forceNewConnection=false)
ConnectionManager::MongoDBSettings dbsettings
void convert(armem::wm::Memory &) override
Client-side working memory.
#define ARMARX_DEBUG
The logging level for output that is only interesting while debugging.
Definition Logging.h:184
#define ARMARX_WARNING
The logging level for unexpected behaviour, but not a serious problem.
Definition Logging.h:193
#define TIMING_START(name)
Helper macro to do timing tests.
Definition TimeUtil.h:289
#define TIMING_END_STREAM(name, os)
Prints duration.
Definition TimeUtil.h:310
This file is part of ArmarX.
void store(const mongocxx::database &db, const armem::wm::Memory &m)
void load(const mongocxx::database &db, armem::wm::Memory &m)
void convert(const mongocxx::database &db, armem::wm::Memory &m)