knowrob  2.1.0
A Knowledge Base System for Cognition-enabled Robots
bson_pl.h File Reference
#include <mongoc.h>
#include <SWI-cpp.h>
Include dependency graph for bson_pl.h:

Go to the source code of this file.

Macros

#define PL_SAFE_ARG_MACROS
 

Functions

PlTerm bson_to_term (const bson_t *bson)
 
bool bsonpl_append (bson_t *doc, const char *key, const PlTerm &term, bson_error_t *err)
 
bool bsonpl_concat (bson_t *doc, const PlTerm &term, bson_error_t *err)
 

Macro Definition Documentation

◆ PL_SAFE_ARG_MACROS

#define PL_SAFE_ARG_MACROS

Definition at line 6 of file bson_pl.h.

Function Documentation

◆ bson_to_term()

PlTerm bson_to_term ( const bson_t *  bson)

Definition at line 210 of file bson_pl.cpp.

211 {
212  static bson_visitor_t visitor = get_bson_visitor();
213  bson_iter_t iter;
214  PlTerm term;
215  PlTail out_list(term);
216  if (bson_iter_init(&iter, doc)) {
217  if(bson_iter_visit_all(&iter, &visitor, &out_list)) {
218  bson_error_t err;
219  bson_set_error(&err,
220  MONGOC_ERROR_BSON,
221  MONGOC_ERROR_BSON_INVALID,
222  "BSON iteration prematurely stopped.");
223  throw knowrob::mongo::MongoException("bson", err);
224  }
225  }
226  out_list.close();
227  return term;
228 }
TermRule & term()
Definition: terms.cpp:136

◆ bsonpl_append()

bool bsonpl_append ( bson_t *  doc,
const char *  key,
const PlTerm &  term,
bson_error_t *  err 
)

Definition at line 311 of file bson_pl.cpp.

311  { // NOLINT(misc-no-recursion)
312  if(PL_is_list((term_t)term)) {
313  // append a document if term is a list
314  bson_t nested_doc;
315  BSON_APPEND_DOCUMENT_BEGIN(doc, key, &nested_doc);
316  if(bsonpl_concat(&nested_doc, term, err)) {
317  bson_append_document_end(doc, &nested_doc);
318  return true;
319  }
320  else {
321  return false;
322  }
323  }
324  else if(std::string_view(term.name()) == "bson_t") {
325  // the term contains a sub-pipeline encoded as bson_t* object which can be concatenated
326  // with the pipeline under construction
327  if(term.arity()!=1) {
328  bson_set_error(err,
329  MONGOC_ERROR_BSON,
330  MONGOC_ERROR_BSON_INVALID,
331  "invalid bson_t term -- arity should be 1: %s", (char*)term
332  );
333  return false;
334  }
335  const PlTerm &bson_value = term[1];
336  auto *sub_bson = (bson_t*)bson_value.operator void *();
337  if(sub_bson) {
338  bool success = bson_concat(doc, sub_bson);
339  bson_destroy(sub_bson);
340  return success;
341  }
342  else {
343  bson_set_error(err,
344  MONGOC_ERROR_BSON,
345  MONGOC_ERROR_BSON_INVALID,
346  "invalid bson_t term -- void pointer is null: %s", (char*)term
347  );
348  return false;
349  }
350  }
351  else {
352  // append typed value otherwise
353  return bsonpl_append_typed(doc,key,term,err);
354  }
355 }
bool bsonpl_concat(bson_t *doc, const PlTerm &term, bson_error_t *err)
Definition: bson_pl.cpp:357

◆ bsonpl_concat()

bool bsonpl_concat ( bson_t *  doc,
const PlTerm &  term,
bson_error_t *  err 
)

Definition at line 357 of file bson_pl.cpp.

357  { // NOLINT(misc-no-recursion)
358  // the term given to bson_from_term must be a list
359  PlTail list(term);
360  PlTerm member;
361  if(!list.next(member)) {
362  // the term is a list []
363  return true;
364  }
365  else if(PL_is_list((term_t)member)) {
366  // the term is a list [[..]|_]
367  do {
368  if(!bsonpl_concat(doc,member,err)) {
369  return false;
370  }
371  } while(list.next(member));
372  return true;
373  }
374  else {
375  // the term is a list [key,value]
376  const char *key = (char*)member;
377  list.next(member);
378  return bsonpl_append(doc,key,member,err);
379  }
380 }
bool bsonpl_append(bson_t *doc, const char *key, const PlTerm &term, bson_error_t *err)
Definition: bson_pl.cpp:311