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

#include <ChangeStream.h>

Collaboration diagram for knowrob::mongo::ChangeStream:

Public Member Functions

 ChangeStream (const std::shared_ptr< Collection > &collection, const bson_t *query, ChangeStreamCallback callback)
 
 ChangeStream (const ChangeStream &)=delete
 
 ~ChangeStream ()
 
bool next ()
 
 ChangeStream (const std::shared_ptr< Collection > &collection, const bson_t *query, ChangeStreamCallback callback)
 
 ChangeStream (const ChangeStream &)=delete
 
 ~ChangeStream ()
 
bool next ()
 

Protected Attributes

std::shared_ptr< Collectioncollection_
 
ChangeStreamCallback callback_
 
mongoc_change_stream_t * stream_
 
bson_wrapper_ptr next_ptr_
 

Detailed Description

A stream of query results that invokes a callback whenever a new result is found.

Definition at line 27 of file ChangeStream.h.

Constructor & Destructor Documentation

◆ ChangeStream() [1/4]

ChangeStream::ChangeStream ( const std::shared_ptr< Collection > &  collection,
const bson_t *  query,
ChangeStreamCallback  callback 
)

Definition at line 11 of file ChangeStream.cpp.

15  : collection_(collection),
16  callback_(std::move(callback)),
17  stream_(nullptr),
18  next_ptr_() {
19  // connect and append session ID to options
20  bson_t *opts = BCON_NEW(
21  "maxAwaitTimeMS", BCON_INT32(1), // the watcher should be non-blocking
22  "fullDocument", BCON_UTF8("updateLookup") // always fetch full document
23  );
24  collection_->appendSession(opts);
25  bson_destroy(opts);
26  // create the stream object
27  stream_ = mongoc_collection_watch(collection_->coll(), query, opts);
28 }
mongoc_change_stream_t * stream_
Definition: ChangeStream.h:43
std::shared_ptr< Collection > collection_
Definition: ChangeStream.h:41
ChangeStreamCallback callback_
Definition: ChangeStream.h:42
bson_wrapper_ptr next_ptr_
Definition: ChangeStream.h:44

◆ ChangeStream() [2/4]

knowrob::mongo::ChangeStream::ChangeStream ( const ChangeStream )
delete

◆ ~ChangeStream() [1/2]

ChangeStream::~ChangeStream ( )

Definition at line 30 of file ChangeStream.cpp.

30  {
31  if (stream_ != nullptr) {
32  mongoc_change_stream_destroy(stream_);
33  stream_ = nullptr;
34  }
35 }

◆ ChangeStream() [3/4]

knowrob::mongo::ChangeStream::ChangeStream ( const std::shared_ptr< Collection > &  collection,
const bson_t *  query,
ChangeStreamCallback  callback 
)

◆ ChangeStream() [4/4]

knowrob::mongo::ChangeStream::ChangeStream ( const ChangeStream )
delete

◆ ~ChangeStream() [2/2]

knowrob::mongo::ChangeStream::~ChangeStream ( )

Member Function Documentation

◆ next() [1/2]

bool ChangeStream::next ( )

Definition at line 37 of file ChangeStream.cpp.

37  {
38  if (stream_ == nullptr) {
39  // stream had an error before
40  return false;
41  }
42 
43  // try retrieving next document
44  const bson_t *doc;
45  if (mongoc_change_stream_next(stream_, &doc)) {
46  next_ptr_.bson = doc;
48  return true;
49  }
50 
51  // check if stream has an error
52  const bson_t *err_doc;
53  bson_error_t error;
54  if (mongoc_change_stream_error_document(stream_, &error, &err_doc)) {
55  mongoc_change_stream_destroy(stream_);
56  stream_ = nullptr;
57  throw MongoException("watch_error", error);
58  }
59 
60  return false;
61 }

◆ next() [2/2]

bool knowrob::mongo::ChangeStream::next ( )

Member Data Documentation

◆ callback_

ChangeStreamCallback knowrob::mongo::ChangeStream::callback_
protected

Definition at line 42 of file ChangeStream.h.

◆ collection_

std::shared_ptr< Collection > knowrob::mongo::ChangeStream::collection_
protected

Definition at line 41 of file ChangeStream.h.

◆ next_ptr_

bson_wrapper_ptr knowrob::mongo::ChangeStream::next_ptr_
protected

Definition at line 44 of file ChangeStream.h.

◆ stream_

mongoc_change_stream_t * knowrob::mongo::ChangeStream::stream_
protected

Definition at line 43 of file ChangeStream.h.


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