knowrob  2.1.0
A Knowledge Base System for Cognition-enabled Robots
Collection.h
Go to the documentation of this file.
1 /*
2  * This file is part of KnowRob, please consult
3  * https://github.com/knowrob/knowrob for license details.
4  */
5 
6 #ifndef KNOWROB_MONGO_COLLECTION_H
7 #define KNOWROB_MONGO_COLLECTION_H
8 
9 #include <mongoc.h>
10 #include <string>
11 #include <vector>
12 #include "BulkOperation.h"
13 #include "Document.h"
14 #include "Connection.h"
15 #include "Index.h"
16 
17 namespace knowrob::mongo {
23  class Collection {
24  public:
26  const std::shared_ptr<Connection> &connection,
27  std::string_view databaseName,
28  std::string_view collectionName);
29 
30  Collection(const Collection &collection);
31 
33 
38  void appendSession(bson_t *opts);
39 
43  mongoc_client_session_t *session();
44 
48  auto pool() { return connection_->pool_; }
49 
53  auto connection() { return connection_; }
54 
58  auto client() const { return client_; }
59 
63  auto coll() { return coll_; }
64 
68  const auto &name() const { return name_; }
69 
73  const auto &dbName() const { return dbName_; }
74 
78  const auto &dbURI() const { return connection_->uri_string_; }
79 
83  void drop();
84 
89  std::vector<std::string> distinctValues(std::string_view key);
90 
95  void removeOne(const Document &document);
96 
101  void removeOne(const bson_oid_t &oid);
102 
107  void removeAll(const Document &document);
108 
113  void storeOne(const Document &document);
114 
121  void update(const Document &query, const Document &update, bool upsert = false);
122 
128  void evalAggregation(const bson_t *pipeline);
129 
133  std::shared_ptr<BulkOperation> createBulkOperation();
134 
139  void createAscendingIndex(const std::vector<const char *> &keys);
140 
145  void createIndex(const std::vector<IndexKey> &keys);
146 
151 
155  bool empty();
156 
157  private:
158  std::shared_ptr<Connection> connection_;
159  mongoc_client_t *client_;
160  mongoc_client_session_t *session_;
161  mongoc_collection_t *coll_;
162  mongoc_database_t *db_;
163  const std::string name_;
164  const std::string dbName_;
165 
166  void remove(const Document &document, mongoc_remove_flags_t flag);
167 
168  void createIndex_internal(const bson_t &keys);
169  };
170 }
171 
172 #endif //KNOWROB_MONGO_COLLECTION_H
const auto & dbName() const
Definition: Collection.h:73
void createAscendingIndex(const std::vector< const char * > &keys)
void createIndex(const std::vector< IndexKey > &keys)
void removeOne(const bson_oid_t &oid)
void update(const Document &query, const Document &update, bool upsert=false)
void removeAll(const Document &document)
void update(const Document &query, const Document &update, bool upsert=false)
Definition: Collection.cpp:156
std::shared_ptr< BulkOperation > createBulkOperation()
std::vector< std::string > distinctValues(std::string_view key)
const auto & dbURI() const
Definition: Collection.h:78
void storeOne(const Document &document)
Collection(const Collection &collection)
void evalAggregation(const bson_t *pipeline)
void removeOne(const Document &document)
void appendSession(bson_t *opts)
Collection(const std::shared_ptr< Connection > &connection, std::string_view databaseName, std::string_view collectionName)
mongoc_client_session_t * session()
const auto & name() const
Definition: Collection.h:68
TermRule & string()
Definition: terms.cpp:63