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

#include <BulkOperation.h>

Public Member Functions

 BulkOperation (mongoc_bulk_operation_t *handle)
 
 BulkOperation (const BulkOperation &)=delete
 
 ~BulkOperation ()
 
void pushInsert (const bson_t *document)
 
void pushRemoveAll (const bson_t *document)
 
void pushRemoveOne (const bson_t *document)
 
void pushUpdate (bson_t *query, bson_t *update)
 
void execute ()
 
bool empty () const
 
 BulkOperation (mongoc_bulk_operation_t *handle)
 
 BulkOperation (const BulkOperation &)=delete
 
 ~BulkOperation ()
 
void pushInsert (const bson_t *document)
 
void pushRemoveAll (const bson_t *document)
 
void pushRemoveOne (const bson_t *document)
 
void pushUpdate (bson_t *query, bson_t *update)
 
void execute ()
 
bool empty () const
 

Protected Member Functions

void validateBulkHandle ()
 
void validateBulkHandle ()
 

Protected Attributes

mongoc_bulk_operation_t * handle_
 
bool empty_ = true
 

Detailed Description

Provides an abstraction for submitting multiple write operations as a single batch.

Definition at line 16 of file BulkOperation.h.

Constructor & Destructor Documentation

◆ BulkOperation() [1/4]

BulkOperation::BulkOperation ( mongoc_bulk_operation_t *  handle)
explicit

Default constructor. Note that the pointer is owned by this object afterwards, and it will take care of freeing its memory.

Parameters
handlea handle to the mongoc_bulk_operation_t pointer.

Definition at line 11 of file BulkOperation.cpp.

12  : handle_(handle) {
13 }
mongoc_bulk_operation_t * handle_
Definition: BulkOperation.h:67

◆ BulkOperation() [2/4]

knowrob::mongo::BulkOperation::BulkOperation ( const BulkOperation )
delete

◆ ~BulkOperation() [1/2]

BulkOperation::~BulkOperation ( )

Definition at line 15 of file BulkOperation.cpp.

15  {
16  if (handle_) {
17  mongoc_bulk_operation_destroy(handle_);
18  handle_ = nullptr;
19  }
20 }

◆ BulkOperation() [3/4]

knowrob::mongo::BulkOperation::BulkOperation ( mongoc_bulk_operation_t *  handle)
explicit

Default constructor. Note that the pointer is owned by this object afterwards, and it will take care of freeing its memory.

Parameters
handlea handle to the mongoc_bulk_operation_t pointer.

◆ BulkOperation() [4/4]

knowrob::mongo::BulkOperation::BulkOperation ( const BulkOperation )
delete

◆ ~BulkOperation() [2/2]

knowrob::mongo::BulkOperation::~BulkOperation ( )

Member Function Documentation

◆ empty() [1/2]

bool knowrob::mongo::BulkOperation::empty ( ) const
inline
Returns
true if this bulk operation is empty.

Definition at line 64 of file BulkOperation.h.

64 { return empty_; }

◆ empty() [2/2]

bool knowrob::mongo::BulkOperation::empty ( ) const
inline
Returns
true if this bulk operation is empty.

Definition at line 64 of file BulkOperation.h.

64 { return empty_; }

◆ execute() [1/2]

void BulkOperation::execute ( )

Execute this bulk operation. Note that a bulk operation can only be executed once.

Definition at line 91 of file BulkOperation.cpp.

91  {
93 
94  bson_error_t bulk_err;
95  bson_t bulk_reply;
96  // perform the bulk write
97  bool success = mongoc_bulk_operation_execute(handle_, &bulk_reply, &bulk_err);
98  // cleanup
99  bson_destroy(&bulk_reply);
100  mongoc_bulk_operation_destroy(handle_);
101  handle_ = nullptr;
102  // throw exception on error
103  if (!success) {
104  throw MongoException("bulk_operation", bulk_err);
105  }
106 }

◆ execute() [2/2]

void knowrob::mongo::BulkOperation::execute ( )

Execute this bulk operation. Note that a bulk operation can only be executed once.

◆ pushInsert() [1/2]

void BulkOperation::pushInsert ( const bson_t *  document)

Add an insertion operation to this batch.

Parameters
documenta document.

Definition at line 33 of file BulkOperation.cpp.

33  {
35 
36  bson_error_t err;
37  if (!mongoc_bulk_operation_insert_with_opts(
38  handle_,
39  document,
40  nullptr,
41  &err)) {
42  throw MongoException("bulk_operation", err);
43  }
44  empty_ = false;
45 }

◆ pushInsert() [2/2]

void knowrob::mongo::BulkOperation::pushInsert ( const bson_t *  document)

Add an insertion operation to this batch.

Parameters
documenta document.

◆ pushRemoveAll() [1/2]

void BulkOperation::pushRemoveAll ( const bson_t *  document)

Add a removal operation to this batch.

Parameters
documenta document pattern.

Definition at line 61 of file BulkOperation.cpp.

61  {
63 
64  bson_error_t err;
65  if (!mongoc_bulk_operation_remove_many_with_opts(
66  handle_,
67  document,
68  nullptr,
69  &err)) {
70  throw MongoException("bulk_operation", err);
71  }
72  empty_ = false;
73 }

◆ pushRemoveAll() [2/2]

void knowrob::mongo::BulkOperation::pushRemoveAll ( const bson_t *  document)

Add a removal operation to this batch.

Parameters
documenta document pattern.

◆ pushRemoveOne() [1/2]

void BulkOperation::pushRemoveOne ( const bson_t *  document)

Add a removal operation to this batch.

Parameters
documenta document pattern.

Definition at line 47 of file BulkOperation.cpp.

47  {
49 
50  bson_error_t err;
51  if (!mongoc_bulk_operation_remove_one_with_opts(
52  handle_,
53  document,
54  nullptr,
55  &err)) {
56  throw MongoException("bulk_operation", err);
57  }
58  empty_ = false;
59 }

◆ pushRemoveOne() [2/2]

void knowrob::mongo::BulkOperation::pushRemoveOne ( const bson_t *  document)

Add a removal operation to this batch.

Parameters
documenta document pattern.

◆ pushUpdate() [1/2]

void BulkOperation::pushUpdate ( bson_t *  query,
bson_t *  update 
)

Add an update operation to this batch.

Parameters
querya document pattern.
updatea update document.

Definition at line 75 of file BulkOperation.cpp.

75  {
77 
78  bson_error_t err;
79  if (!mongoc_bulk_operation_update_many_with_opts(
80  handle_,
81  query,
82  update,
83  nullptr,
84  &err)) {
85  throw MongoException("bulk_operation", err);
86  }
87  empty_ = false;
88 }

◆ pushUpdate() [2/2]

void knowrob::mongo::BulkOperation::pushUpdate ( bson_t *  query,
bson_t *  update 
)

Add an update operation to this batch.

Parameters
querya document pattern.
updatea update document.

◆ validateBulkHandle() [1/2]

void BulkOperation::validateBulkHandle ( )
protected

Definition at line 22 of file BulkOperation.cpp.

22  {
23  if (!handle_) {
24  bson_error_t err;
25  bson_set_error(&err,
26  MONGOC_ERROR_COMMAND,
27  MONGOC_ERROR_COMMAND_INVALID_ARG,
28  "bulk operation can only be executed once");
29  throw MongoException("bulk_operation", err);
30  }
31 }

◆ validateBulkHandle() [2/2]

void knowrob::mongo::BulkOperation::validateBulkHandle ( )
protected

Member Data Documentation

◆ empty_

bool knowrob::mongo::BulkOperation::empty_ = true
protected

Definition at line 68 of file BulkOperation.h.

◆ handle_

mongoc_bulk_operation_t * knowrob::mongo::BulkOperation::handle_
protected

Definition at line 67 of file BulkOperation.h.


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