knowrob  2.1.0
A Knowledge Base System for Cognition-enabled Robots
knowrob::mongo::Pipeline Class Reference

#include <Pipeline.h>

Public Member Functions

 Pipeline (bson_t *arrayDocument=nullptr)
 
auto * arrayDocument () const
 
auto isNested () const
 
void setIsNested (bool isNested)
 
bson_t * appendStageBegin ()
 
bson_t * appendStageBegin (std::string_view stageOperator)
 
void appendStageEnd (bson_t *stage)
 
void append (const knowrob::GraphTerm &query, const TripleStore &tripleStore)
 
void append (const knowrob::TriplePattern &query, const TripleStore &tripleStore)
 
void appendBuiltin (const knowrob::GraphBuiltin &builtin)
 
void limit (uint32_t maxDocuments)
 
void unwind (std::string_view field, bool preserveNullAndEmptyArrays=false)
 
void unset (std::string_view field)
 
void project (std::string_view field)
 
void project (const std::vector< std::string_view > &fields)
 
void replaceRoot (std::string_view newRootField)
 
void merge (std::string_view collection)
 
void sortAscending (std::string_view field)
 
void sortDescending (std::string_view field)
 
void setUnion (std::string_view field, const std::vector< std::string_view > &sets)
 
void addToArray (std::string_view key, std::string_view arrayKey, std::string_view elementKey)
 
void matchEmptyArray (std::string_view arrayKey)
 
 Pipeline (bson_t *arrayDocument=nullptr)
 
auto * arrayDocument () const
 
auto isNested () const
 
void setIsNested (bool isNested)
 
bson_t * appendStageBegin ()
 
bson_t * appendStageBegin (std::string_view stageOperator)
 
void appendStageEnd (bson_t *stage)
 
void append (const knowrob::GraphTerm &query, const TripleStore &tripleStore)
 
void append (const knowrob::TriplePattern &query, const TripleStore &tripleStore)
 
void appendBuiltin (const knowrob::GraphBuiltin &builtin)
 
void limit (uint32_t maxDocuments)
 
void unwind (std::string_view field, bool preserveNullAndEmptyArrays=false)
 
void unset (std::string_view field)
 
void project (std::string_view field)
 
void project (const std::vector< std::string_view > &fields)
 
void replaceRoot (std::string_view newRootField)
 
void merge (std::string_view collection)
 
void sortAscending (std::string_view field)
 
void sortDescending (std::string_view field)
 
void setUnion (std::string_view field, const std::vector< std::string_view > &sets)
 
void addToArray (std::string_view key, std::string_view arrayKey, std::string_view elementKey)
 
void matchEmptyArray (std::string_view arrayKey)
 

Static Public Member Functions

static bson_t * loadFromJSON (std::string_view filename, const std::map< std::string, std::string > &parameters)
 
static bson_t * loadFromJSON (std::string_view filename, const std::map< std::string, std::string > &parameters)
 

Protected Member Functions

void appendTerm_recursive (const knowrob::GraphTerm &query, const TripleStore &tripleStore, std::set< std::string_view > &groundedVariables)
 
void matchBinary (const knowrob::GraphBuiltin &builtin, std::string_view predicate)
 
void setAccumulated (const knowrob::GraphBuiltin &builtin, std::string_view predicate)
 
void bindValue (const knowrob::GraphBuiltin &builtin)
 
void appendUnion (const knowrob::GraphUnion &unionTerm, const TripleStore &tripleStore, std::set< std::string_view > &groundedVariables)
 
void appendTerm_recursive (const knowrob::GraphTerm &query, const TripleStore &tripleStore, std::set< std::string_view > &groundedVariables)
 
void matchBinary (const knowrob::GraphBuiltin &builtin, std::string_view predicate)
 
void setAccumulated (const knowrob::GraphBuiltin &builtin, std::string_view predicate)
 
void bindValue (const knowrob::GraphBuiltin &builtin)
 
void appendUnion (const knowrob::GraphUnion &unionTerm, const TripleStore &tripleStore, std::set< std::string_view > &groundedVariables)
 

Protected Attributes

bson_t * arrayDocument_
 
uint32_t numStages_
 
std::list< bson_wrapperstages_
 
std::list< bson_wrapperstageOperators_
 
bool isNested_ = false
 
bson_t * lastStage_
 
bson_t * lastOperator_
 

Detailed Description

A aggregation pipeline.

Definition at line 24 of file Pipeline.h.

Constructor & Destructor Documentation

◆ Pipeline() [1/2]

Pipeline::Pipeline ( bson_t *  arrayDocument = nullptr)
explicit
Parameters
arrayDocumentan initialized array document.

Definition at line 20 of file Pipeline.cpp.

22  numStages_(0),
23  lastStage_(nullptr),
24  lastOperator_(nullptr) {
25 }
auto * arrayDocument() const
Definition: Pipeline.h:34

◆ Pipeline() [2/2]

knowrob::mongo::Pipeline::Pipeline ( bson_t *  arrayDocument = nullptr)
explicit
Parameters
arrayDocumentan initialized array document.

Member Function Documentation

◆ addToArray() [1/2]

void Pipeline::addToArray ( std::string_view  key,
std::string_view  arrayKey,
std::string_view  elementKey 
)

Add an element to an array.

Parameters
keythe output filed
arrayKeyinput array field
elementKeyfield of an additional element

Definition at line 332 of file Pipeline.cpp.

332  {
333  bson_t concatOperator, concatArray, concatArray1;
334  auto setStage1 = appendStageBegin("$set");
335  BSON_APPEND_DOCUMENT_BEGIN(setStage1, key.data(), &concatOperator);
336  {
337  BSON_APPEND_ARRAY_BEGIN(&concatOperator, "$concatArrays", &concatArray);
338  {
339  BSON_APPEND_UTF8(&concatArray, "0", arrayKey.data());
340  BSON_APPEND_ARRAY_BEGIN(&concatArray, "1", &concatArray1);
341  {
342  BSON_APPEND_UTF8(&concatArray1, "0", elementKey.data());
343  }
344  bson_append_array_end(&concatArray, &concatArray1);
345  }
346  bson_append_array_end(&concatOperator, &concatArray);
347  }
348  bson_append_document_end(setStage1, &concatOperator);
349  appendStageEnd(setStage1);
350 }
void appendStageEnd(bson_t *stage)
Definition: Pipeline.cpp:46
bson_t * appendStageBegin()
Definition: Pipeline.cpp:27

◆ addToArray() [2/2]

void knowrob::mongo::Pipeline::addToArray ( std::string_view  key,
std::string_view  arrayKey,
std::string_view  elementKey 
)

Add an element to an array.

Parameters
keythe output filed
arrayKeyinput array field
elementKeyfield of an additional element

◆ append() [1/4]

void Pipeline::append ( const knowrob::GraphTerm query,
const TripleStore tripleStore 
)

Definition at line 64 of file Pipeline.cpp.

64  {
65  std::set<std::string_view> groundedVariables;
66  appendTerm_recursive(query, tripleStore, groundedVariables);
67 }
void appendTerm_recursive(const knowrob::GraphTerm &query, const TripleStore &tripleStore, std::set< std::string_view > &groundedVariables)
Definition: Pipeline.cpp:69

◆ append() [2/4]

void knowrob::mongo::Pipeline::append ( const knowrob::GraphTerm query,
const TripleStore tripleStore 
)

◆ append() [3/4]

void Pipeline::append ( const knowrob::TriplePattern query,
const TripleStore tripleStore 
)

Definition at line 55 of file Pipeline.cpp.

55  {
56  // append lookup stages to pipeline
57  TripleLookupData lookupData(&query);
58  // indicate that no variables in tripleExpression may have been instantiated
59  // by a previous step to allow for some optimizations.
60  lookupData.mayHasMoreGroundings = false;
61  lookupTriple(*this, tripleStore, lookupData);
62 }
void lookupTriple(Pipeline &pipeline, const TripleStore &tripleStore, const TripleLookupData &lookupData)

◆ append() [4/4]

void knowrob::mongo::Pipeline::append ( const knowrob::TriplePattern query,
const TripleStore tripleStore 
)

◆ appendBuiltin() [1/2]

void Pipeline::appendBuiltin ( const knowrob::GraphBuiltin builtin)

Definition at line 160 of file Pipeline.cpp.

160  {
161  switch (builtin.builtinType()) {
163  bindValue(builtin);
164  break;
166  setAccumulated(builtin, "$max");
167  break;
169  setAccumulated(builtin, "$min");
170  break;
172  matchBinary(builtin, "$lte");
173  break;
175  matchBinary(builtin, "$lt");
176  break;
178  matchBinary(builtin, "$gt");
179  break;
181  matchBinary(builtin, "$gte");
182  break;
184  matchBinary(builtin, "$eq");
185  break;
187  matchBinary(builtin, "$ne");
188  break;
189  }
190 }
auto builtinType() const
Definition: GraphBuiltin.h:108
void setAccumulated(const knowrob::GraphBuiltin &builtin, std::string_view predicate)
Definition: Pipeline.cpp:210
void matchBinary(const knowrob::GraphBuiltin &builtin, std::string_view predicate)
Definition: Pipeline.cpp:232
void bindValue(const knowrob::GraphBuiltin &builtin)
Definition: Pipeline.cpp:192

◆ appendBuiltin() [2/2]

void knowrob::mongo::Pipeline::appendBuiltin ( const knowrob::GraphBuiltin builtin)

◆ appendStageBegin() [1/4]

bson_t * Pipeline::appendStageBegin ( )

Definition at line 27 of file Pipeline.cpp.

27  {
28  auto arrayKey = std::to_string(numStages_++);
29  bson_wrapper &stage = stages_.emplace_back();
30  BSON_APPEND_DOCUMENT_BEGIN(arrayDocument_, arrayKey.c_str(), &stage.bson);
31  lastStage_ = &stage.bson;
32  return &stage.bson;
33 }
std::list< bson_wrapper > stages_
Definition: Pipeline.h:139

◆ appendStageBegin() [2/4]

bson_t* knowrob::mongo::Pipeline::appendStageBegin ( )

◆ appendStageBegin() [3/4]

bson_t * Pipeline::appendStageBegin ( std::string_view  stageOperator)

Definition at line 35 of file Pipeline.cpp.

35  {
36  auto arrayKey = std::to_string(numStages_++);
37  bson_wrapper &stage = stages_.emplace_back();
38  bson_wrapper &stageOperator = stageOperators_.emplace_back();
39  BSON_APPEND_DOCUMENT_BEGIN(arrayDocument_, arrayKey.c_str(), &stage.bson);
40  BSON_APPEND_DOCUMENT_BEGIN(&stage.bson, stageOperatorString.data(), &stageOperator.bson);
41  lastOperator_ = &stageOperator.bson;
42  lastStage_ = &stage.bson;
43  return &stageOperator.bson;
44 }
std::list< bson_wrapper > stageOperators_
Definition: Pipeline.h:140

◆ appendStageBegin() [4/4]

bson_t* knowrob::mongo::Pipeline::appendStageBegin ( std::string_view  stageOperator)

◆ appendStageEnd() [1/2]

void Pipeline::appendStageEnd ( bson_t *  stage)

Definition at line 46 of file Pipeline.cpp.

46  {
47  if (lastOperator_ == stage) {
48  bson_append_document_end(lastStage_, lastOperator_);
49  bson_append_document_end(arrayDocument_, lastStage_);
50  } else {
51  bson_append_document_end(arrayDocument_, stage);
52  }
53 }

◆ appendStageEnd() [2/2]

void knowrob::mongo::Pipeline::appendStageEnd ( bson_t *  stage)

◆ appendTerm_recursive() [1/2]

void Pipeline::appendTerm_recursive ( const knowrob::GraphTerm query,
const TripleStore tripleStore,
std::set< std::string_view > &  groundedVariables 
)
protected

Definition at line 69 of file Pipeline.cpp.

71  {
72  switch (query.termType()) {
74  auto &expr = ((const GraphPattern &) query).value();
75  TripleLookupData lookupData(expr.get());
76  // indicate that all previous groundings of variables are known
77  lookupData.mayHasMoreGroundings = false;
78  lookupData.knownGroundedVariables = groundedVariables;
79  // remember variables in tripleExpression, they have a grounding in next step
80  if (!expr->isOptional()) {
81  for (auto &var: expr->getVariables()) {
82  groundedVariables.insert(var->name());
83  }
84  }
85  lookupTriple(*this, tripleStore, lookupData);
86  break;
87  }
89  appendBuiltin((const knowrob::GraphBuiltin &) query);
90  break;
92  for (auto &elem: ((const knowrob::GraphSequence &) query).terms()) {
93  appendTerm_recursive(*elem, tripleStore, groundedVariables);
94  }
95  break;
97  appendUnion((const knowrob::GraphUnion &) query, tripleStore, groundedVariables);
98  break;
99  }
100 }
auto termType() const
Definition: GraphTerm.h:43
void appendBuiltin(const knowrob::GraphBuiltin &builtin)
Definition: Pipeline.cpp:160
void appendUnion(const knowrob::GraphUnion &unionTerm, const TripleStore &tripleStore, std::set< std::string_view > &groundedVariables)
Definition: Pipeline.cpp:102
VariableRule & var()
Definition: terms.cpp:91

◆ appendTerm_recursive() [2/2]

void knowrob::mongo::Pipeline::appendTerm_recursive ( const knowrob::GraphTerm query,
const TripleStore tripleStore,
std::set< std::string_view > &  groundedVariables 
)
protected

◆ appendUnion() [1/2]

void Pipeline::appendUnion ( const knowrob::GraphUnion unionTerm,
const TripleStore tripleStore,
std::set< std::string_view > &  groundedVariables 
)
protected

Definition at line 102 of file Pipeline.cpp.

104  {
105  // First run a $lookup operation for each branch of the union.
106  for (uint32_t i = 0; i < unionTerm.terms().size(); i++) {
107  auto branchVars = groundedVariables;
108 
109  // construct a lookup
110  bson_t letDoc;
111  auto lookupStage = appendStageBegin("$lookup");
112  BSON_APPEND_UTF8(lookupStage, "from", tripleStore.oneCollection->name().data());
113  BSON_APPEND_UTF8(lookupStage, "as", ("next" + std::to_string(i)).data());
114  BSON_APPEND_DOCUMENT_BEGIN(lookupStage, "let", &letDoc);
115  BSON_APPEND_UTF8(&letDoc, "v_VARS", isNested_ ? "$$v_VARS" : "$v_VARS");
116  bson_append_document_end(lookupStage, &letDoc);
117 
118  // construct inner pipelines, one for each branch of the union
119  bson_t pipelineArray;
120  BSON_APPEND_ARRAY_BEGIN(lookupStage, "pipeline", &pipelineArray);
121  Pipeline nestedPipeline(&pipelineArray);
122  nestedPipeline.setIsNested(true);
123  nestedPipeline.appendTerm_recursive(*unionTerm.terms()[i], tripleStore, branchVars);
124  bson_append_array_end(lookupStage, &pipelineArray);
125 
126  appendStageEnd(lookupStage);
127  }
128 
129  // concatenate individual results
130  bson_t concatDoc, concatArray;
131  auto setConcatStage = appendStageBegin("$set");
132  BSON_APPEND_DOCUMENT_BEGIN(setConcatStage, "next", &concatDoc);
133  BSON_APPEND_ARRAY_BEGIN(&concatDoc, "$concatArrays", &concatArray);
134  for (uint32_t i = 0; i < unionTerm.terms().size(); i++) {
135  BSON_APPEND_UTF8(&concatArray, std::to_string(i).c_str(), ("$next" + std::to_string(i)).data());
136  }
137  bson_append_array_end(&concatDoc, &concatArray);
138  bson_append_document_end(setConcatStage, &concatDoc);
139  appendStageEnd(setConcatStage);
140  // delete individual results
141  for (uint32_t i = 0; i < unionTerm.terms().size(); i++) {
142  unset("next" + std::to_string(i));
143  }
144  // unwind the concatenated array
145  unwind("$next");
146  // project the bindings of one of the branches into v_VARS field
147  bson_t setDoc, mergeArray;
148  auto setMergedStage = appendStageBegin("$set");
149  BSON_APPEND_DOCUMENT_BEGIN(setMergedStage, "v_VARS", &setDoc);
150  BSON_APPEND_ARRAY_BEGIN(&setDoc, "$mergeObjects", &mergeArray);
151  BSON_APPEND_UTF8(&mergeArray, "0", "$next.v_VARS");
152  BSON_APPEND_UTF8(&mergeArray, "1", "$v_VARS");
153  bson_append_array_end(&setDoc, &mergeArray);
154  bson_append_document_end(setMergedStage, &setDoc);
155  appendStageEnd(setMergedStage);
156  // and finally unset the next field
157  unset("next");
158 }
const auto & terms() const
void unwind(std::string_view field, bool preserveNullAndEmptyArrays=false)
Definition: Pipeline.cpp:255
void unset(std::string_view field)
Definition: Pipeline.cpp:267
std::shared_ptr< knowrob::mongo::Collection > oneCollection
Definition: TripleStore.h:28

◆ appendUnion() [2/2]

void knowrob::mongo::Pipeline::appendUnion ( const knowrob::GraphUnion unionTerm,
const TripleStore tripleStore,
std::set< std::string_view > &  groundedVariables 
)
protected

◆ arrayDocument() [1/2]

auto* knowrob::mongo::Pipeline::arrayDocument ( ) const
inline
Returns
the pipeline array.

Definition at line 34 of file Pipeline.h.

34 { return arrayDocument_; }

◆ arrayDocument() [2/2]

auto* knowrob::mongo::Pipeline::arrayDocument ( ) const
inline
Returns
the pipeline array.

Definition at line 34 of file Pipeline.h.

34 { return arrayDocument_; }

◆ bindValue() [1/2]

void Pipeline::bindValue ( const knowrob::GraphBuiltin builtin)
protected

Definition at line 192 of file Pipeline.cpp.

192  {
193  // e.g. `{ $set: { "begin": "$next.begin" } }`
194  if (!builtin.bindVar()) {
195  KB_ERROR("No variable to bind in $min/$max operation");
196  return;
197  }
198  if (builtin.arguments().size() != 1) {
199  KB_ERROR("Bind operation requires one argument");
200  return;
201  }
202  static const std::string varPrefix = "v_VARS.";
203  static const std::string varSuffix = ".val";
204  auto setStage = appendStageBegin("$set");
205  auto varKey = varPrefix + std::string(builtin.bindVar()->name()) + varSuffix;
206  MongoTerm::appendWithVars(setStage, varKey.c_str(), builtin.arguments()[0]);
207  appendStageEnd(setStage);
208 }
#define KB_ERROR
Definition: Logger.h:28
auto & arguments() const
Definition: Function.h:47
auto bindVar() const
Definition: GraphBuiltin.h:113
static void appendWithVars(bson_t *doc, const char *key, const TermPtr &term, const char *queryOperator=nullptr, bool matchNullValue=false)
Definition: MongoTerm.cpp:101
TermRule & string()
Definition: terms.cpp:63

◆ bindValue() [2/2]

void knowrob::mongo::Pipeline::bindValue ( const knowrob::GraphBuiltin builtin)
protected

◆ isNested() [1/2]

auto knowrob::mongo::Pipeline::isNested ( ) const
inline

Definition at line 36 of file Pipeline.h.

36 { return isNested_; }

◆ isNested() [2/2]

auto knowrob::mongo::Pipeline::isNested ( ) const
inline

Definition at line 36 of file Pipeline.h.

36 { return isNested_; }

◆ limit() [1/2]

void Pipeline::limit ( uint32_t  maxDocuments)

Append a $limit stage.

Parameters
maxDocumentslimit of resulting documents.

Definition at line 249 of file Pipeline.cpp.

249  {
250  auto unwindStage = appendStageBegin();
251  BSON_APPEND_INT32(unwindStage, "$limit", maxDocuments);
252  appendStageEnd(unwindStage);
253 }

◆ limit() [2/2]

void knowrob::mongo::Pipeline::limit ( uint32_t  maxDocuments)

Append a $limit stage.

Parameters
maxDocumentslimit of resulting documents.

◆ loadFromJSON() [1/2]

bson_t * Pipeline::loadFromJSON ( std::string_view  filename,
const std::map< std::string, std::string > &  parameters 
)
static

Load a pipeline from a JSON file.

Parameters
filenamethe file name
parametersa map of parameters
Returns
a pipeline

Definition at line 368 of file Pipeline.cpp.

368  {
369  auto resolved = URI::resolve(filename);
370  // Load JSON file
371  boost::property_tree::ptree pt;
372  boost::property_tree::read_json(resolved, pt);
373 
374  // Convert JSON to string
375  std::stringstream ss;
376  boost::property_tree::write_json(ss, pt);
377 
378  // Replace placeholders with actual values
379  std::string pipeline = ss.str();
380  for (const auto &param: parameters) {
381  replaceAll(pipeline, "${" + param.first + "}", param.second);
382  }
383 
384  // Convert JSON to BSON
385  bson_error_t error;
386  bson_t *bson = bson_new_from_json((const uint8_t *) pipeline.c_str(), pipeline.size(), &error);
387 
388  if (!bson) {
389  KB_ERROR("Error loading pipeline: {}", error.message);
390  return nullptr;
391  }
392 
393  return bson;
394 }
void replaceAll(std::string &str, const std::string &from, const std::string &to)
Definition: Pipeline.cpp:360
static std::string resolve(const std::string_view &uriString)
Definition: URI.cpp:79

◆ loadFromJSON() [2/2]

static bson_t* knowrob::mongo::Pipeline::loadFromJSON ( std::string_view  filename,
const std::map< std::string, std::string > &  parameters 
)
static

Load a pipeline from a JSON file.

Parameters
filenamethe file name
parametersa map of parameters
Returns
a pipeline

◆ matchBinary() [1/2]

void Pipeline::matchBinary ( const knowrob::GraphBuiltin builtin,
std::string_view  predicate 
)
protected

Definition at line 232 of file Pipeline.cpp.

232  {
233  // e.g.: `{ $match: { $expr: { $lte: ["$v_scope.begin", "$v_scope.end"] } } }`
234  if (builtin.arguments().size() != 2) {
235  KB_ERROR("Binary operation requires two arguments");
236  return;
237  }
238  bson_t exprDoc, ltDoc;
239  auto matchStage = appendStageBegin("$match");
240  BSON_APPEND_DOCUMENT_BEGIN(matchStage, "$expr", &exprDoc);
241  BSON_APPEND_ARRAY_BEGIN(&exprDoc, predicate.data(), &ltDoc);
242  MongoTerm::appendWithVars(&ltDoc, "0", builtin.arguments()[0]);
243  MongoTerm::appendWithVars(&ltDoc, "1", builtin.arguments()[1]);
244  bson_append_array_end(&exprDoc, &ltDoc);
245  bson_append_document_end(matchStage, &exprDoc);
246  appendStageEnd(matchStage);
247 }
PredicateRule & predicate()
Definition: formula.cpp:221

◆ matchBinary() [2/2]

void knowrob::mongo::Pipeline::matchBinary ( const knowrob::GraphBuiltin builtin,
std::string_view  predicate 
)
protected

◆ matchEmptyArray() [1/2]

void Pipeline::matchEmptyArray ( std::string_view  arrayKey)

Match an empty array.

Parameters
arrayKeythe array field

Definition at line 352 of file Pipeline.cpp.

352  {
353  bson_t emptyArray;
354  auto matchStage = appendStageBegin("$match");
355  BSON_APPEND_ARRAY_BEGIN(matchStage, arrayKey.data(), &emptyArray);
356  bson_append_array_end(matchStage, &emptyArray);
357  appendStageEnd(matchStage);
358 }

◆ matchEmptyArray() [2/2]

void knowrob::mongo::Pipeline::matchEmptyArray ( std::string_view  arrayKey)

Match an empty array.

Parameters
arrayKeythe array field

◆ merge() [1/2]

void Pipeline::merge ( std::string_view  collection)

Append a $merge stage.

Parameters
collectionthe output collection

Definition at line 291 of file Pipeline.cpp.

291  {
292  auto unwindStage = appendStageBegin("$merge");
293  BSON_APPEND_UTF8(unwindStage, "into", collection.data());
294  BSON_APPEND_UTF8(unwindStage, "on", "_id");
295  BSON_APPEND_UTF8(unwindStage, "whenMatched", "merge");
296  appendStageEnd(unwindStage);
297 }

◆ merge() [2/2]

void knowrob::mongo::Pipeline::merge ( std::string_view  collection)

Append a $merge stage.

Parameters
collectionthe output collection

◆ project() [1/4]

void Pipeline::project ( const std::vector< std::string_view > &  fields)

Append a $project stage.

Parameters
fieldsa list of document fields to include in output documents

Definition at line 305 of file Pipeline.cpp.

305  {
306  auto projectStage = appendStageBegin("$project");
307  for (auto field: fields) {
308  BSON_APPEND_INT32(projectStage, field.data(), 1);
309  }
310  appendStageEnd(projectStage);
311 }

◆ project() [2/4]

void knowrob::mongo::Pipeline::project ( const std::vector< std::string_view > &  fields)

Append a $project stage.

Parameters
fieldsa list of document fields to include in output documents

◆ project() [3/4]

void Pipeline::project ( std::string_view  field)

Append a $project stage.

Parameters
fielda document field to include in output documents

Definition at line 299 of file Pipeline.cpp.

299  {
300  auto projectStage = appendStageBegin("$project");
301  BSON_APPEND_INT32(projectStage, field.data(), 1);
302  appendStageEnd(projectStage);
303 }

◆ project() [4/4]

void knowrob::mongo::Pipeline::project ( std::string_view  field)

Append a $project stage.

Parameters
fielda document field to include in output documents

◆ replaceRoot() [1/2]

void Pipeline::replaceRoot ( std::string_view  newRootField)

Append a $replaceRoot stage.

Parameters
newRootFielda document field

Definition at line 273 of file Pipeline.cpp.

273  {
274  auto unwindStage = appendStageBegin("$replaceRoot");
275  BSON_APPEND_UTF8(unwindStage, "newRoot", newRootField.data());
276  appendStageEnd(unwindStage);
277 }

◆ replaceRoot() [2/2]

void knowrob::mongo::Pipeline::replaceRoot ( std::string_view  newRootField)

Append a $replaceRoot stage.

Parameters
newRootFielda document field

◆ setAccumulated() [1/2]

void Pipeline::setAccumulated ( const knowrob::GraphBuiltin builtin,
std::string_view  predicate 
)
protected

Definition at line 210 of file Pipeline.cpp.

210  {
211  // e.g. `{ $set: "begin", { $max: ["$begin", "$next.begin"] } }`
212  // NOTE: `$min [null,2]` -> 2 and `$max [null,2]` -> 2
213  if (!builtin.bindVar()) {
214  KB_ERROR("No variable to bind in $min/$max operation");
215  return;
216  }
217  static const std::string varPrefix = "v_VARS.";
218  static const std::string varSuffix = ".val";
219  bson_t accumulatedDoc, inputArray;
220  auto setStage = appendStageBegin("$set");
221  auto varKey = varPrefix + std::string(builtin.bindVar()->name()) + varSuffix;
222  BSON_APPEND_DOCUMENT_BEGIN(setStage, varKey.c_str(), &accumulatedDoc);
223  BSON_APPEND_ARRAY_BEGIN(&accumulatedDoc, predicate.data(), &inputArray);
224  for (uint32_t i = 0; i < builtin.arguments().size(); i++) {
225  MongoTerm::appendWithVars(&inputArray, std::to_string(i).c_str(), builtin.arguments()[i]);
226  }
227  bson_append_array_end(&accumulatedDoc, &inputArray);
228  bson_append_document_end(setStage, &accumulatedDoc);
229  appendStageEnd(setStage);
230 }

◆ setAccumulated() [2/2]

void knowrob::mongo::Pipeline::setAccumulated ( const knowrob::GraphBuiltin builtin,
std::string_view  predicate 
)
protected

◆ setIsNested() [1/2]

void knowrob::mongo::Pipeline::setIsNested ( bool  isNested)
inline

Definition at line 38 of file Pipeline.h.

38 { isNested_ = isNested; }
auto isNested() const
Definition: Pipeline.h:36

◆ setIsNested() [2/2]

void knowrob::mongo::Pipeline::setIsNested ( bool  isNested)
inline

Definition at line 38 of file Pipeline.h.

38 { isNested_ = isNested; }

◆ setUnion() [1/2]

void Pipeline::setUnion ( std::string_view  field,
const std::vector< std::string_view > &  sets 
)

Append a ($set o $setUnion) stage.

Parameters
fielda field to store the union of array
setslist of array values

Definition at line 313 of file Pipeline.cpp.

313  {
314  bson_t unionOperator, unionArray;
315  uint32_t numElements = 0;
316  auto setStage = appendStageBegin("$set");
317  BSON_APPEND_DOCUMENT_BEGIN(setStage, field.data(), &unionOperator);
318  {
319  BSON_APPEND_ARRAY_BEGIN(&unionOperator, "$setUnion", &unionArray);
320  {
321  for (auto setString: sets) {
322  auto arrayKey = std::to_string(numElements++);
323  BSON_APPEND_UTF8(&unionArray, arrayKey.c_str(), setString.data());
324  }
325  }
326  bson_append_array_end(&unionOperator, &unionArray);
327  }
328  bson_append_document_end(setStage, &unionOperator);
329  appendStageEnd(setStage);
330 }

◆ setUnion() [2/2]

void knowrob::mongo::Pipeline::setUnion ( std::string_view  field,
const std::vector< std::string_view > &  sets 
)

Append a ($set o $setUnion) stage.

Parameters
fielda field to store the union of array
setslist of array values

◆ sortAscending() [1/2]

void Pipeline::sortAscending ( std::string_view  field)

Append a $sort stage with ascending sort order.

Parameters
fielda document field

Definition at line 279 of file Pipeline.cpp.

279  {
280  auto sortStage = appendStageBegin("$sort");
281  BSON_APPEND_INT32(sortStage, field.data(), 1);
282  appendStageEnd(sortStage);
283 }

◆ sortAscending() [2/2]

void knowrob::mongo::Pipeline::sortAscending ( std::string_view  field)

Append a $sort stage with ascending sort order.

Parameters
fielda document field

◆ sortDescending() [1/2]

void Pipeline::sortDescending ( std::string_view  field)

Append a $sort stage with descending sort order.

Parameters
fielda document field

Definition at line 285 of file Pipeline.cpp.

285  {
286  auto sortStage = appendStageBegin("$sort");
287  BSON_APPEND_INT32(sortStage, field.data(), -1);
288  appendStageEnd(sortStage);
289 }

◆ sortDescending() [2/2]

void knowrob::mongo::Pipeline::sortDescending ( std::string_view  field)

Append a $sort stage with descending sort order.

Parameters
fielda document field

◆ unset() [1/2]

void Pipeline::unset ( std::string_view  field)

Append a $unset stage.

Parameters
fielda document field

Definition at line 267 of file Pipeline.cpp.

267  {
268  auto unwindStage = appendStageBegin();
269  BSON_APPEND_UTF8(unwindStage, "$unset", field.data());
270  appendStageEnd(unwindStage);
271 }

◆ unset() [2/2]

void knowrob::mongo::Pipeline::unset ( std::string_view  field)

Append a $unset stage.

Parameters
fielda document field

◆ unwind() [1/2]

void Pipeline::unwind ( std::string_view  field,
bool  preserveNullAndEmptyArrays = false 
)

Append a $unwind stage.

Parameters
fieldan array value
preserveNullAndEmptyArraysif true, if the field is null or empty, $unwind outputs the document.

Definition at line 255 of file Pipeline.cpp.

255  {
256  if (preserveNullAndEmptyArrays) {
257  auto unwindStage = appendStageBegin("$unwind");
258  BSON_APPEND_BOOL(unwindStage, "preserveNullAndEmptyArrays", 1);
259  appendStageEnd(unwindStage);
260  } else {
261  auto unwindStage = appendStageBegin();
262  BSON_APPEND_UTF8(unwindStage, "$unwind", field.data());
263  appendStageEnd(unwindStage);
264  }
265 }

◆ unwind() [2/2]

void knowrob::mongo::Pipeline::unwind ( std::string_view  field,
bool  preserveNullAndEmptyArrays = false 
)

Append a $unwind stage.

Parameters
fieldan array value
preserveNullAndEmptyArraysif true, if the field is null or empty, $unwind outputs the document.

Member Data Documentation

◆ arrayDocument_

bson_t * knowrob::mongo::Pipeline::arrayDocument_
protected

Definition at line 137 of file Pipeline.h.

◆ isNested_

bool knowrob::mongo::Pipeline::isNested_ = false
protected

Definition at line 141 of file Pipeline.h.

◆ lastOperator_

bson_t * knowrob::mongo::Pipeline::lastOperator_
protected

Definition at line 144 of file Pipeline.h.

◆ lastStage_

bson_t * knowrob::mongo::Pipeline::lastStage_
protected

Definition at line 143 of file Pipeline.h.

◆ numStages_

uint32_t knowrob::mongo::Pipeline::numStages_
protected

Definition at line 138 of file Pipeline.h.

◆ stageOperators_

std::list< bson_wrapper > knowrob::mongo::Pipeline::stageOperators_
protected

Definition at line 140 of file Pipeline.h.

◆ stages_

std::list< bson_wrapper > knowrob::mongo::Pipeline::stages_
protected

Definition at line 139 of file Pipeline.h.


The documentation for this class was generated from the following files: