knowrob  2.1.0
A Knowledge Base System for Cognition-enabled Robots
mongo_kb.cpp File Reference
#include <SWI-cpp.h>
#include <iostream>
#include "knowrob/storage/mongo/MongoInterface.h"
#include "knowrob/reasoner/mongolog/bson_pl.h"
#include "knowrob/storage/mongo/Document.h"
Include dependency graph for mongo_kb.cpp:

Go to the source code of this file.

Namespaces

 knowrob
 

Macros

#define PL_SAFE_ARG_MACROS
 
#define PREDICATE_COLLECTION   MongoInterface::get().connect(PL_A1, (char*)PL_A2)
 
#define PREDICATE_CURSOR   MongoInterface::get().cursor((char*)PL_A1)
 

Functions

 PREDICATE (mng_collections, 2)
 
 PREDICATE (mng_distinct_values_json, 4)
 
 PREDICATE (mng_index_create_core, 3)
 
 PREDICATE (mng_drop_unsafe, 2)
 
 PREDICATE (mng_store, 3)
 
 PREDICATE (mng_remove, 3)
 
 PREDICATE (mng_update, 4)
 
 PREDICATE (mng_bulk_write, 3)
 
 PREDICATE (mng_cursor_create, 3)
 
 PREDICATE (mng_cursor_create, 4)
 
 PREDICATE (mng_cursor_destroy, 1)
 
 PREDICATE (mng_cursor_erase, 1)
 
 PREDICATE (mng_cursor_filter, 2)
 
 PREDICATE (mng_cursor_aggregate, 2)
 
 PREDICATE (mng_cursor_descending, 2)
 
 PREDICATE (mng_cursor_ascending, 2)
 
 PREDICATE (mng_cursor_limit, 2)
 
 PREDICATE (mng_cursor_next_pairs, 2)
 
 PREDICATE (mng_cursor_next_json, 2)
 

Macro Definition Documentation

◆ PL_SAFE_ARG_MACROS

#define PL_SAFE_ARG_MACROS

Definition at line 10 of file mongo_kb.cpp.

◆ PREDICATE_COLLECTION

#define PREDICATE_COLLECTION   MongoInterface::get().connect(PL_A1, (char*)PL_A2)

Definition at line 22 of file mongo_kb.cpp.

◆ PREDICATE_CURSOR

#define PREDICATE_CURSOR   MongoInterface::get().cursor((char*)PL_A1)

Definition at line 23 of file mongo_kb.cpp.

Function Documentation

◆ PREDICATE() [1/19]

PREDICATE ( mng_bulk_write  ,
 
)

Definition at line 144 of file mongo_kb.cpp.

144  {
145  static const PlAtom ATOM_insert("insert");
146  static const PlAtom ATOM_remove("remove");
147  static const PlAtom ATOM_update("update");
148 
149  try {
150  auto bulk = PREDICATE_COLLECTION->createBulkOperation();
151  PlTail pl_list(PL_A3);
152  PlTerm pl_member;
153 
154  while(pl_list.next(pl_member)) {
155  const PlAtom operation_name(pl_member.name());
156  const auto &pl_value1 = pl_member[1];
157  bson_error_t err;
158 
159  // parse the document
160  auto doc1 = Document(bson_new());
161  if(!bsonpl_concat(doc1.bson(),pl_value1,&err)) {
162  throw MongoException("invalid_term", err);
163  }
164 
165  if(operation_name == ATOM_insert) {
166  bulk->pushInsert(doc1.bson());
167  }
168  else if(operation_name == ATOM_remove) {
169  bulk->pushRemoveAll(doc1.bson());
170  }
171  else if(operation_name == ATOM_update) {
172  const auto &pl_value2 = pl_member[2];
173  auto doc2 = Document(bson_new());
174  if(!bsonpl_concat(doc2.bson(), pl_value2, &err)) {
175  throw MongoException("invalid_term", err);
176  }
177  bulk->pushUpdate(doc1.bson(), doc2.bson());
178  }
179  else {
180  bson_set_error(&err,
181  MONGOC_ERROR_COMMAND,
182  MONGOC_ERROR_COMMAND_INVALID_ARG,
183  "unknown bulk operation '%s'", pl_member.name());
184  throw MongoException("bulk_error", err);
185  }
186  }
187 
188  bulk->execute();
189  return TRUE;
190  }
191  catch(const MongoException &exc) { throw MongoPLException(exc); }
192  catch(const std::exception &exc) { throw MongoPLException(exc); }
193 }
bool bsonpl_concat(bson_t *doc, const PlTerm &term, bson_error_t *err)
Definition: bson_pl.cpp:357
#define PREDICATE_COLLECTION
Definition: mongo_kb.cpp:22

◆ PREDICATE() [2/19]

PREDICATE ( mng_collections  ,
 
)

Definition at line 48 of file mongo_kb.cpp.

48  {
49  auto db_handle = MongoInterface::get().connect(PL_A1);
50  bson_error_t err;
51  char **strv;
52  if ((strv = mongoc_database_get_collection_names_with_opts(
53  db_handle->db(), nullptr /* opts */, &err))) {
54  PlTail l(PL_A2);
55  for (int i=0; strv[i]; i++) {
56  l.append( strv[i] );
57  }
58  l.close();
59  bson_strfreev(strv);
60  return TRUE;
61  }
62  else {
63  throw MongoPLException(MongoException("collection_lookup_failed", err));
64  }
65 }

◆ PREDICATE() [3/19]

PREDICATE ( mng_cursor_aggregate  ,
 
)

Definition at line 244 of file mongo_kb.cpp.

244  {
245  try {
246  Document doc_a(termToDocument(PL_A2));
247  PREDICATE_CURSOR->aggregate(doc_a.bson());
248  return TRUE;
249  }
250  catch(const MongoException &exc) { throw MongoPLException(exc); }
251  catch(const std::exception &exc) { throw MongoPLException(exc); }
252 }
#define PREDICATE_CURSOR
Definition: mongo_kb.cpp:23

◆ PREDICATE() [4/19]

PREDICATE ( mng_cursor_ascending  ,
 
)

Definition at line 263 of file mongo_kb.cpp.

263  {
264  try {
265  PREDICATE_CURSOR->ascending((char*)PL_A2);
266  return TRUE;
267  }
268  catch(const MongoException &exc) { throw MongoPLException(exc); }
269  catch(const std::exception &exc) { throw MongoPLException(exc); }
270 }

◆ PREDICATE() [5/19]

PREDICATE ( mng_cursor_create  ,
 
)

Definition at line 195 of file mongo_kb.cpp.

195  {
196  try {
197  PL_A3 = MongoInterface::get().cursor_create(PL_A1,(char*)PL_A2)->id().c_str();
198  return TRUE;
199  }
200  catch(const MongoException &exc) { throw MongoPLException(exc); }
201  catch(const std::exception &exc) { throw MongoPLException(exc); }
202 }

◆ PREDICATE() [6/19]

PREDICATE ( mng_cursor_create  ,
 
)

Definition at line 204 of file mongo_kb.cpp.

204  {
205  try {
206  Document doc_a(termToDocument(PL_A4));
207  auto cursor = MongoInterface::get().cursor_create(PL_A1,(char*)PL_A2);
208  cursor->filter(doc_a.bson());
209  PL_A3 = cursor->id().c_str();
210  return TRUE;
211  }
212  catch(const MongoException &exc) { throw MongoPLException(exc); }
213  catch(const std::exception &exc) { throw MongoPLException(exc); }
214 }

◆ PREDICATE() [7/19]

PREDICATE ( mng_cursor_descending  ,
 
)

Definition at line 254 of file mongo_kb.cpp.

254  {
255  try {
256  PREDICATE_CURSOR->descending((char*)PL_A2);
257  return TRUE;
258  }
259  catch(const MongoException &exc) { throw MongoPLException(exc); }
260  catch(const std::exception &exc) { throw MongoPLException(exc); }
261 }

◆ PREDICATE() [8/19]

PREDICATE ( mng_cursor_destroy  ,
 
)

Definition at line 216 of file mongo_kb.cpp.

216  {
217  try {
218  char* cursor_id = (char*)PL_A1;
219  MongoInterface::get().cursor_destroy(cursor_id);
220  return TRUE;
221  }
222  catch(const MongoException &exc) { throw MongoPLException(exc); }
223  catch(const std::exception &exc) { throw MongoPLException(exc); }
224 }

◆ PREDICATE() [9/19]

PREDICATE ( mng_cursor_erase  ,
 
)

Definition at line 226 of file mongo_kb.cpp.

226  {
227  try {
228  return PREDICATE_CURSOR->erase();
229  }
230  catch(const MongoException &exc) { throw MongoPLException(exc); }
231  catch(const std::exception &exc) { throw MongoPLException(exc); }
232 }

◆ PREDICATE() [10/19]

PREDICATE ( mng_cursor_filter  ,
 
)

Definition at line 234 of file mongo_kb.cpp.

234  {
235  try {
236  Document doc_a(termToDocument(PL_A2));
237  PREDICATE_CURSOR->filter(doc_a.bson());
238  return TRUE;
239  }
240  catch(const MongoException &exc) { throw MongoPLException(exc); }
241  catch(const std::exception &exc) { throw MongoPLException(exc); }
242 }

◆ PREDICATE() [11/19]

PREDICATE ( mng_cursor_limit  ,
 
)

Definition at line 272 of file mongo_kb.cpp.

272  {
273  try {
274  PREDICATE_CURSOR->limit((int)PL_A2);
275  return TRUE;
276  }
277  catch(const MongoException &exc) { throw MongoPLException(exc); }
278  catch(const std::exception &exc) { throw MongoPLException(exc); }
279 }

◆ PREDICATE() [12/19]

PREDICATE ( mng_cursor_next_json  ,
 
)

Definition at line 296 of file mongo_kb.cpp.

296  {
297  try {
298  const bson_t *doc;
299  if(PREDICATE_CURSOR->next(&doc)) {
300  char* str = bson_as_canonical_extended_json(doc, nullptr);
301  PL_A2 = str;
302  bson_free(str);
303  return TRUE;
304  }
305  else {
306  return FALSE;
307  }
308  }
309  catch(const MongoException &exc) { throw MongoPLException(exc); }
310  catch(const std::exception &exc) { throw MongoPLException(exc); }
311 }

◆ PREDICATE() [13/19]

PREDICATE ( mng_cursor_next_pairs  ,
 
)

Definition at line 281 of file mongo_kb.cpp.

281  {
282  try {
283  const bson_t *doc;
284  if(PREDICATE_CURSOR->next(&doc)) {
285  PL_A2 = bson_to_term(doc);
286  return TRUE;
287  }
288  else {
289  return FALSE;
290  }
291  }
292  catch(const MongoException &exc) { throw MongoPLException(exc); }
293  catch(const std::exception &exc) { throw MongoPLException(exc); }
294 }
PlTerm bson_to_term(const bson_t *bson)
Definition: bson_pl.cpp:210

◆ PREDICATE() [14/19]

PREDICATE ( mng_distinct_values_json  ,
 
)

Definition at line 67 of file mongo_kb.cpp.

67  {
68  auto db_handle = MongoInterface::get().connect(PL_A1);
69  char* coll_name = (char*)PL_A2;
70  char* key = (char*)PL_A3;
71  bson_error_t err;
72  bson_t reply;
73  //
74  bson_t *command = BCON_NEW("distinct", BCON_UTF8(coll_name), "key", BCON_UTF8(key));
75  bool success = mongoc_database_command_simple(
76  db_handle->db(), command, nullptr, &reply, &err);
77  if(success) {
78  char* str = bson_as_canonical_extended_json(&reply, nullptr);
79  PL_A4 = str;
80  bson_free(str);
81  }
82  bson_destroy(command);
83  return success;
84 }

◆ PREDICATE() [15/19]

PREDICATE ( mng_drop_unsafe  ,
 
)

Definition at line 106 of file mongo_kb.cpp.

106  {
107  try {
108  PREDICATE_COLLECTION->drop();
109  return TRUE;
110  }
111  catch(const MongoException &exc) { throw MongoPLException(exc); }
112  catch(const std::exception &exc) { throw MongoPLException(exc); }
113 }

◆ PREDICATE() [16/19]

PREDICATE ( mng_index_create_core  ,
 
)

Definition at line 86 of file mongo_kb.cpp.

86  {
87  static const PlAtom ATOM_minus("-");
88 
89  std::vector<mongo::IndexKey> indexes;
90  PlTail pl_list(PL_A3);
91  PlTerm pl_member;
92  while (pl_list.next(pl_member)) {
93  const PlAtom mode_atom(pl_member.name());
94  const PlTerm &pl_value = pl_member[1];
95  if (mode_atom == ATOM_minus) {
96  indexes.emplace_back((char *) pl_value, mongo::IndexType::DESCENDING);
97  } else {
98  indexes.emplace_back((char *) pl_value, mongo::IndexType::ASCENDING);
99  }
100  }
101 
102  return MongoInterface::get().connect(PL_A1)->create_index((char*)PL_A2,indexes);
103 }

◆ PREDICATE() [17/19]

PREDICATE ( mng_remove  ,
 
)

Definition at line 124 of file mongo_kb.cpp.

124  {
125  try {
126  PREDICATE_COLLECTION->removeAll(Document(termToDocument(PL_A3)));
127  return TRUE;
128  }
129  catch(const MongoException &exc) { throw MongoPLException(exc); }
130  catch(const std::exception &exc) { throw MongoPLException(exc); }
131 }

◆ PREDICATE() [18/19]

PREDICATE ( mng_store  ,
 
)

Definition at line 115 of file mongo_kb.cpp.

115  {
116  try {
117  PREDICATE_COLLECTION->storeOne(Document(termToDocument(PL_A3)));
118  return TRUE;
119  }
120  catch(const MongoException &exc) { throw MongoPLException(exc); }
121  catch(const std::exception &exc) { throw MongoPLException(exc); }
122 }

◆ PREDICATE() [19/19]

PREDICATE ( mng_update  ,
 
)

Definition at line 133 of file mongo_kb.cpp.

133  {
134  try {
135  PREDICATE_COLLECTION->update(
136  Document(termToDocument(PL_A3)),
137  Document(termToDocument(PL_A4)));
138  return TRUE;
139  }
140  catch(const MongoException &exc) { throw MongoPLException(exc); }
141  catch(const std::exception &exc) { throw MongoPLException(exc); }
142 }