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

#include <Database.h>

Public Member Functions

 Database (mongoc_client_pool_t *pool, std::string_view db_name)
 
 ~Database ()
 
mongoc_database_t * db ()
 
bool create_index (const char *coll_name, const std::vector< IndexKey > &indexes)
 
 Database (mongoc_client_pool_t *pool, std::string_view db_name)
 
 ~Database ()
 
mongoc_database_t * db ()
 
bool create_index (const char *coll_name, const std::vector< IndexKey > &indexes)
 

Detailed Description

A named database in a Mongo DB instance.

Definition at line 19 of file Database.h.

Constructor & Destructor Documentation

◆ Database() [1/2]

Database::Database ( mongoc_client_pool_t *  pool,
std::string_view  db_name 
)

Definition at line 11 of file Database.cpp.

12  : pool_(pool) {
13  client_ = mongoc_client_pool_pop(pool_);
14  db_ = mongoc_client_get_database(client_, db_name.data());
15 }

◆ ~Database() [1/2]

Database::~Database ( )

Definition at line 17 of file Database.cpp.

17  {
18  mongoc_database_destroy(db_);
19  mongoc_client_pool_push(pool_, client_);
20 }

◆ Database() [2/2]

knowrob::mongo::Database::Database ( mongoc_client_pool_t *  pool,
std::string_view  db_name 
)

◆ ~Database() [2/2]

knowrob::mongo::Database::~Database ( )

Member Function Documentation

◆ create_index() [1/2]

bool Database::create_index ( const char *  coll_name,
const std::vector< IndexKey > &  indexes 
)

Definition at line 22 of file Database.cpp.

22  {
23  bson_t reply;
24  bson_error_t err;
25  bson_t keys;
26  //
27  bson_init(&keys);
28  for (auto &is : indexes) {
29  BSON_APPEND_INT32(&keys, is.value.c_str(), (int)is.type);
30  }
31  char *index_name = mongoc_collection_keys_to_index_string(&keys);
32  //
33  bson_t *cmd = BCON_NEW ("createIndexes", BCON_UTF8(coll_name),
34  "indexes", "[", "{",
35  "key", BCON_DOCUMENT(&keys),
36  "name", BCON_UTF8(index_name),
37  "}", "]"
38  );
39  bool success = mongoc_database_write_command_with_opts(
40  db_, cmd, nullptr /* opts */, &reply, &err);
41  bson_free(index_name);
42  bson_destroy(&reply);
43  bson_destroy(cmd);
44  if (!success) {
45  throw MongoException("create_index_failed", err);
46  }
47  return success;
48 }

◆ create_index() [2/2]

bool knowrob::mongo::Database::create_index ( const char *  coll_name,
const std::vector< IndexKey > &  indexes 
)

◆ db() [1/2]

mongoc_database_t* knowrob::mongo::Database::db ( )
inline

Definition at line 24 of file Database.h.

24 { return db_; }

◆ db() [2/2]

mongoc_database_t* knowrob::mongo::Database::db ( )
inline

Definition at line 24 of file Database.h.

24 { return db_; }

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