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

#include <Cursor.h>

Inheritance diagram for knowrob::mongo::Cursor:

Public Member Functions

 Cursor (const std::shared_ptr< Collection > &collection)
 
 Cursor (const Cursor &)=delete
 
 ~Cursor ()
 
const auto & id ()
 
void limit (unsigned int limit)
 
void ascending (const char *key)
 
void descending (const char *key)
 
void filter (const bson_t *query_doc)
 
void aggregate (const bson_t *query_doc)
 
bool next (const bson_t **doc, bool ignore_empty=false)
 
bool erase ()
 
const auto * query () const
 
 Cursor (const std::shared_ptr< Collection > &collection)
 
 Cursor (const Cursor &)=delete
 
 ~Cursor ()
 
const auto & id ()
 
void limit (unsigned int limit)
 
void ascending (const char *key)
 
void descending (const char *key)
 
void filter (const bson_t *query_doc)
 
void aggregate (const bson_t *query_doc)
 
bool next (const bson_t **doc, bool ignore_empty=false)
 
bool erase ()
 
const auto * query () const
 

Detailed Description

An abstraction for MongoDB answer cursors.

Definition at line 18 of file Cursor.h.

Constructor & Destructor Documentation

◆ Cursor() [1/4]

Cursor::Cursor ( const std::shared_ptr< Collection > &  collection)
explicit

Definition at line 15 of file Cursor.cpp.

16  : collection_(collection),
17  cursor_(nullptr),
18  isAggregateQuery_(false),
19  limit_(0) {
20  query_ = bson_new();
21  opts_ = bson_new();
22  collection_->appendSession(opts_);
23  // use pointer as id
24  std::stringstream ss;
25  ss << static_cast<const void *>(this);
26  id_ = ss.str();
27 }

◆ Cursor() [2/4]

knowrob::mongo::Cursor::Cursor ( const Cursor )
delete

◆ ~Cursor() [1/2]

Cursor::~Cursor ( )

Definition at line 29 of file Cursor.cpp.

29  {
30  if (cursor_ != nullptr) {
31  mongoc_cursor_destroy(cursor_);
32  cursor_ = nullptr;
33  }
34  if (query_ != nullptr) {
35  bson_destroy(query_);
36  query_ = nullptr;
37  }
38  if (opts_ != nullptr) {
39  bson_destroy(opts_);
40  opts_ = nullptr;
41  }
42 }

◆ Cursor() [3/4]

knowrob::mongo::Cursor::Cursor ( const std::shared_ptr< Collection > &  collection)
explicit

◆ Cursor() [4/4]

knowrob::mongo::Cursor::Cursor ( const Cursor )
delete

◆ ~Cursor() [2/2]

knowrob::mongo::Cursor::~Cursor ( )

Member Function Documentation

◆ aggregate() [1/2]

void Cursor::aggregate ( const bson_t *  query_doc)

Run an aggregation pipeline to obtain results for this Cursor.

Parameters
query_doc

Definition at line 62 of file Cursor.cpp.

62  {
63  isAggregateQuery_ = true;
64  bson_concat(query_, query_doc);
65 }

◆ aggregate() [2/2]

void knowrob::mongo::Cursor::aggregate ( const bson_t *  query_doc)

Run an aggregation pipeline to obtain results for this Cursor.

Parameters
query_doc

◆ ascending() [1/2]

void Cursor::ascending ( const char *  key)

Sort results in ascending order.

Parameters
keya field name in result documents.

Definition at line 48 of file Cursor.cpp.

48  {
49  static bson_t *doc = BCON_NEW("sort", "{", key, BCON_INT32(1), "}");
50  bson_concat(opts_, doc);
51 }

◆ ascending() [2/2]

void knowrob::mongo::Cursor::ascending ( const char *  key)

Sort results in ascending order.

Parameters
keya field name in result documents.

◆ descending() [1/2]

void Cursor::descending ( const char *  key)

Sort results in descending order.

Parameters
keya field name in result documents.

Definition at line 53 of file Cursor.cpp.

53  {
54  static bson_t *doc = BCON_NEW("sort", "{", key, BCON_INT32(-1), "}");
55  bson_concat(opts_, doc);
56 }

◆ descending() [2/2]

void knowrob::mongo::Cursor::descending ( const char *  key)

Sort results in descending order.

Parameters
keya field name in result documents.

◆ erase() [1/2]

bool Cursor::erase ( )

Erase all documents that are result documents of this Cursor.

Returns
true on success.

Definition at line 98 of file Cursor.cpp.

98  {
99  bson_error_t err;
100  bool success = mongoc_collection_delete_many(
101  collection_->coll(), query_, opts_, nullptr /* reply */, &err);
102  if (!success) {
103  throw MongoException("erase_error", err);
104  }
105  return true;
106 }

◆ erase() [2/2]

bool knowrob::mongo::Cursor::erase ( )

Erase all documents that are result documents of this Cursor.

Returns
true on success.

◆ filter() [1/2]

void Cursor::filter ( const bson_t *  query_doc)

Filter results of the getAnswerCursor by the pattern provided.

Parameters
query_doca query document.

Definition at line 58 of file Cursor.cpp.

58  {
59  bson_concat(query_, query_doc);
60 }

◆ filter() [2/2]

void knowrob::mongo::Cursor::filter ( const bson_t *  query_doc)

Filter results of the getAnswerCursor by the pattern provided.

Parameters
query_doca query document.

◆ id() [1/2]

const auto& knowrob::mongo::Cursor::id ( )
inline
Returns
the unique id of this Cursor.

Definition at line 29 of file Cursor.h.

29 { return id_; };

◆ id() [2/2]

const auto& knowrob::mongo::Cursor::id ( )
inline
Returns
the unique id of this Cursor.

Definition at line 29 of file Cursor.h.

29 { return id_; };

◆ limit() [1/2]

void Cursor::limit ( unsigned int  limit)

Limit results produced by this Cursor.

Parameters
limitthe maximum number of results.

Definition at line 44 of file Cursor.cpp.

44  {
45  limit_ = limit;
46 }
void limit(unsigned int limit)
Definition: Cursor.cpp:44

◆ limit() [2/2]

void knowrob::mongo::Cursor::limit ( unsigned int  limit)

Limit results produced by this Cursor.

Parameters
limitthe maximum number of results.

◆ next() [1/2]

bool Cursor::next ( const bson_t **  doc,
bool  ignore_empty = false 
)

Start the query if it has not been started yet and generate the next result document.

Parameters
docpointer to result document.
ignore_empty
Returns
true on success

Definition at line 67 of file Cursor.cpp.

67  {
68  if (cursor_ == nullptr) {
69  if (isAggregateQuery_) {
70  cursor_ = mongoc_collection_aggregate(
71  collection_->coll(), MONGOC_QUERY_NONE, query_, opts_, nullptr /* read_prefs */ );
72  } else {
73  cursor_ = mongoc_collection_find_with_opts(
74  collection_->coll(), query_, opts_, nullptr /* read_prefs */ );
75  }
76  if (limit_ > 0) {
77  mongoc_cursor_set_limit(cursor_, limit_);
78  }
79  }
80  // make sure cursor has no error
81  bson_error_t err1;
82  if (mongoc_cursor_error(cursor_, &err1)) {
83  throw MongoException("cursor_error", err1);
84  }
85  // get next document
86  if (!mongoc_cursor_next(cursor_, doc)) {
87  // make sure cursor has no error after next has been called
88  bson_error_t err2;
89  if (mongoc_cursor_error(cursor_, &err2)) {
90  throw MongoException("cursor_error", err2);
91  }
92  return ignore_empty;
93  } else {
94  return true;
95  }
96 }

◆ next() [2/2]

bool knowrob::mongo::Cursor::next ( const bson_t **  doc,
bool  ignore_empty = false 
)

Start the query if it has not been started yet and generate the next result document.

Parameters
docpointer to result document.
ignore_empty
Returns
true on success

◆ query() [1/2]

const auto* knowrob::mongo::Cursor::query ( ) const
inline
Returns
the bson_t object associated to this Cursor.

Definition at line 78 of file Cursor.h.

78 { return query_; }

◆ query() [2/2]

const auto* knowrob::mongo::Cursor::query ( ) const
inline
Returns
the bson_t object associated to this Cursor.

Definition at line 78 of file Cursor.h.

78 { return query_; }

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