mongodb.cpp
Go to the documentation of this file.
1 #include "mongodb.h"
2 
3 // Simox
4 #include <SimoxUtility/json.h>
5 
9 
11 {
12  namespace bbuilder = bsoncxx::builder;
13  namespace bdoc = bsoncxx::document;
14 
15  namespace detail
16  {
17  std::string
18  escapeName(const std::string& segmentName)
19  {
20  std::string ret = segmentName;
21  //simox::alg::replace_all(ret, Prefix, PrefixEscaped);
22  for (const auto& [s, r] : EscapeTable)
23  {
24  ret = simox::alg::replace_all(ret, s, r);
25  }
26  return ret;
27  }
28 
29  std::string
30  unescapeName(const std::string& escapedName)
31  {
32  std::string ret = escapedName;
33  for (
34  const auto& [s, r] :
35  EscapeTable) // Here we assume that noone uses the replaced char usually in the segment name... TODO
36  {
37  ret = simox::alg::replace_all(ret, r, s);
38  }
39  return ret;
40  }
41 
42  bool
43  insert(mongocxx::collection& coll, const nlohmann::json& value)
44  {
45  std::string v = value.dump();
46  auto q = bsoncxx::from_json(v);
47  auto res = coll.insert_one(q.view());
48 
49  return (bool)res;
50  }
51 
52  std::optional<nlohmann::json>
53  contains(mongocxx::collection& coll, const nlohmann::json& value)
54  {
55  // check mongodb
56  std::string v = value.dump();
57  auto q = bsoncxx::from_json(v);
58  auto res = coll.find_one(q.view());
59  if (res)
60  {
61  return nlohmann::json::parse(bsoncxx::to_json(*res));
62  }
63  return std::nullopt;
64  }
65 
66  bool
67  update(mongocxx::collection& coll,
68  const nlohmann::json& query,
69  const nlohmann::json& update)
70  {
71  // check mongodb
72  auto q = bsoncxx::from_json(query.dump());
73  auto udoc = bsoncxx::from_json(update.dump());
74  auto u = bbuilder::basic::make_document(bbuilder::basic::kvp("$set", udoc));
75 
76  auto res = coll.update_one(q.view(), u.view());
77 
78  return (bool)res;
79  }
80  } // namespace detail
81 
82  std::optional<mongocxx::database>
83  databaseExists(mongocxx::client& client, const std::string& databaseName)
84  {
85  //auto names = client.list_databases();
86  //if (auto it = std::find(names.begin(), names.end(), databaseName); it != names.end())
87  //{
88  // return client[databaseName];
89  //}
90  return std::nullopt;
91  }
92 
93  mongocxx::database
94  ensureDatabaseExists(mongocxx::client& client,
95  const std::string& databaseName,
96  bool createIfNotExistent)
97  {
98  auto db = databaseExists(client, databaseName);
99  if (!db)
100  {
101  if (!createIfNotExistent)
102  {
103  throw armarx::LocalException("Database existence can not be ensured: " +
104  databaseName);
105  }
106  }
107  return client[databaseName];
108  }
109 
110  std::optional<mongocxx::collection>
111  collectionExists(mongocxx::database& db, const std::string& collectionName)
112  {
113  if (db.has_collection(collectionName))
114  {
115  return db[collectionName];
116  }
117  return std::nullopt;
118  }
119 
120  mongocxx::collection
121  ensureCollectionExists(mongocxx::database& db,
122  const std::string& collectionName,
123  bool createIfNotExistent)
124  {
125  auto coll = collectionExists(db, collectionName);
126  if (!coll)
127  {
128  if (!createIfNotExistent)
129  {
130  throw armarx::LocalException("Collection existence can not be ensured: " +
131  collectionName);
132  }
133  }
134  return db[collectionName];
135  }
136 
137  std::string
139  {
140  if (id.hasTimestamp())
141  {
142  return id.timestampStr();
143  }
144 
145  // fallback
146  throw armarx::LocalException("Called toDocumentName() on non-snapshot id: " + id.str());
147  }
148 
149  std::string
151  {
152  ARMARX_CHECK(id.isWellDefined());
153  std::stringstream ss;
154  if (id.hasMemoryName())
155  {
156  ss << detail::escapeName(id.memoryName);
157  }
158  if (id.hasCoreSegmentName())
159  {
160  ss << "." << detail::escapeName(id.coreSegmentName);
161  }
162  if (id.hasProviderSegmentName())
163  {
164  ss << "." << detail::escapeName(id.providerSegmentName);
165  }
166  if (id.hasEntityName())
167  {
168  ss << "." << detail::escapeName(id.entityName);
169  }
170 
171  return ss.str();
172  }
173 
174  std::optional<nlohmann::json>
175  documentExists(mongocxx::collection& collection, const nlohmann::json& json)
176  {
177  return detail::contains(collection, json);
178  }
179 
180  nlohmann::json
181  ensureDocumentExists(mongocxx::collection& collection,
182  const nlohmann::json& json,
183  bool createIfNotExistent)
184  {
185  auto op = documentExists(collection, json);
186  if (!op)
187  {
188  if (!createIfNotExistent)
189  {
190  throw armarx::LocalException("Document existence can not be ensured: " +
191  json.dump());
192  }
193  }
194 
195  if (!op)
196  {
197  detail::insert(collection, json);
198  }
199 
200  auto find = detail::contains(collection, json);
201  if (!find)
202  {
203  throw armarx::LocalException(
204  "Even after creating the docuemnt it wasnt found.. error: " + json.dump());
205  }
206  return *find;
207  }
208 
209  nlohmann::json
210  readDataFromDocument(mongocxx::collection& collection, const nlohmann::json& json)
211  {
212  auto doc = detail::contains(collection, json);
213 
214  ARMARX_CHECK((bool)doc) << " Could not find document matching: " << json.dump()
215  << " in collection " << collection.name();
216 
217  return *doc;
218  }
219 
220  void
221  writeDataToDocument(mongocxx::collection& collection,
222  const nlohmann::json& query,
223  const nlohmann::json& update)
224  {
225  if (documentExists(collection, query))
226  {
227  detail::update(collection, query, update);
228  }
229  else
230  {
231  nlohmann::json full;
232  full.update(query);
233  full.update(update);
234  detail::insert(collection, full);
235  }
236  }
237 
238  std::vector<nlohmann::json>
239  getAllDocuments(mongocxx::collection& collection)
240  {
241  std::vector<nlohmann::json> ret;
242  mongocxx::cursor cursor = collection.find({});
243  for (const auto& doc : cursor)
244  {
245  ret.push_back(
246  nlohmann::json::parse(bsoncxx::to_json(doc, bsoncxx::ExtendedJsonMode::k_relaxed)));
247  }
248  return ret;
249  }
250 
251 } // namespace armarx::armem::server::ltm::util::mongodb
armarx::armem::server::ltm::util::mongodb::ensureDocumentExists
nlohmann::json ensureDocumentExists(mongocxx::collection &collection, const nlohmann::json &json, bool createIfNotExistent)
Definition: mongodb.cpp:181
armarx::armem::server::ltm::util::mongodb::toDocumentID
std::string toDocumentID(const armem::MemoryID &id)
Definition: mongodb.cpp:138
str
std::string str(const T &t)
Definition: UserAssistedSegmenterGuiWidgetController.cpp:42
armarx::aron::ret
ReaderT::InputType T & ret
Definition: rw.h:21
armarx::data::from_json
void from_json(const nlohmann::json &j, PackagePath &pp)
Definition: json_conversions.h:45
armarx::armem::server::ltm::util::mongodb::databaseExists
std::optional< mongocxx::database > databaseExists(mongocxx::client &client, const std::string &databaseName)
Definition: mongodb.cpp:83
armarx::armem::server::ltm::util::mongodb::readDataFromDocument
nlohmann::json readDataFromDocument(mongocxx::collection &collection, const nlohmann::json &json)
Definition: mongodb.cpp:210
armarx::armem::server::ltm::util::mongodb::detail::contains
std::optional< nlohmann::json > contains(mongocxx::collection &coll, const nlohmann::json &value)
Definition: mongodb.cpp:53
detail
Definition: OpenCVUtil.cpp:127
aron_conversions.h
ARMARX_CHECK
#define ARMARX_CHECK(expression)
Shortcut for ARMARX_CHECK_EXPRESSION.
Definition: ExpressionException.h:82
NLohmannJSONConverter.h
cxxopts::value
std::shared_ptr< Value > value()
Definition: cxxopts.hpp:926
armarx::armem::server::ltm::util::mongodb::detail::escapeName
std::string escapeName(const std::string &segmentName)
Definition: mongodb.cpp:18
armarx::armem::MemoryID
A memory ID.
Definition: MemoryID.h:47
error.h
armarx::armem::server::ltm::util::mongodb::getAllDocuments
std::vector< nlohmann::json > getAllDocuments(mongocxx::collection &collection)
Definition: mongodb.cpp:239
armarx::armem::server::ltm::util::mongodb::detail::insert
bool insert(mongocxx::collection &coll, const nlohmann::json &value)
Definition: mongodb.cpp:43
armarx::armem::server::ltm::util::mongodb
Definition: mongodb.cpp:10
armarx::armem::server::ltm::util::mongodb::documentExists
std::optional< nlohmann::json > documentExists(mongocxx::collection &collection, const nlohmann::json &json)
Definition: mongodb.cpp:175
mongodb.h
q
#define q
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::armem::server::ltm::util::mongodb::writeDataToDocument
void writeDataToDocument(mongocxx::collection &collection, const nlohmann::json &query, const nlohmann::json &update)
Definition: mongodb.cpp:221
armarx::data::to_json
void to_json(nlohmann::json &j, const PackagePath &pp)
Definition: json_conversions.h:36
armarx::armem::server::ltm::util::mongodb::ensureCollectionExists
mongocxx::collection ensureCollectionExists(mongocxx::database &db, const std::string &collectionName, bool createIfNotExistent)
Definition: mongodb.cpp:121
armarx::armem::server::ltm::util::mongodb::detail::EscapeTable
const std::map< std::string, std::string > EscapeTable
Definition: mongodb.h:27
armarx::armem::server::ltm::util::mongodb::toCollectionName
std::string toCollectionName(const armem::MemoryID &id)
Definition: mongodb.cpp:150
armarx::ctrlutil::v
double v(double t, double v0, double a0, double j)
Definition: CtrlUtil.h:39
armarx::armem::laser_scans::constants::memoryName
const std::string memoryName
Definition: constants.h:28
armarx::armem::server::ltm::util::mongodb::collectionExists
std::optional< mongocxx::collection > collectionExists(mongocxx::database &db, const std::string &collectionName)
Definition: mongodb.cpp:111
armarx::armem::server::ltm::util::mongodb::ensureDatabaseExists
mongocxx::database ensureDatabaseExists(mongocxx::client &client, const std::string &databaseName, bool createIfNotExistent)
Definition: mongodb.cpp:94
armarx::ctrlutil::s
double s(double t, double s0, double v0, double a0, double j)
Definition: CtrlUtil.h:33
armarx::armem::server::ltm::util::mongodb::detail::unescapeName
std::string unescapeName(const std::string &escapedName)
Definition: mongodb.cpp:30