-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDatabase.cpp
More file actions
101 lines (90 loc) · 3.17 KB
/
Database.cpp
File metadata and controls
101 lines (90 loc) · 3.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
#include <array>
#include <uuid.h>
#include "Database.hpp"
using namespace CCC;
Database::Database(std::shared_ptr<DataBaseConnection> connection, const std::string &name) {
this->data.connection = connection;
this->data.name = name;
}
Document Database::createDocument(const json &content, std::string uuid) {
cpr::Body body(content.as_string());
if (uuid.empty()) {
std::vector<std::string> uuids;
const auto result = this->data.connection->uuids(1l);
for (const auto ¤t : result.array_range()){
uuids.push_back(current.as_string());
}
uuid = uuids.at(0ul);
}
const auto result = this->data.connection->createDocument(this->data.name, uuid, body);
const auto real_uuid = result["id"s].to_string();
const auto revision = result["rev"s].to_string();
const auto document = Document(uuid, this->data.name, revision, this->data.connection);
return document;
}
Document Database::createDocument(const json &content){
std::array<uuids::uuid, 16> data;
for (unsigned long i = 0; i < 16; ++i){
std::random_device device;
auto seed = std::array<int, std::mt19937::state_size> {};
std::generate(std::begin(seed), std::end(seed), std::ref(device));
std::seed_seq seq(std::begin(seed), std::end(seed));
std::mt19937 generator(seq);
uuids::uuid_random_generator generation{generator};
data[i] = generation();
}
std::string idstring;
for (const auto ¤t : data){
idstring += uuids::to_string(current);
}
return this->createDocument(content, idstring);
}
Database::DeleteResult Database::removeDocument(Document &document) {
const auto id = document.uuid();
const auto result = this->data.connection->deleteDocument(this->data.name, id);
return result;
}
std::vector<Document> Database::documents() {
const auto name = this->data.name;
const auto connection = this->data.connection;
const auto result = this->data.connection->dbAllDocs(name, json());
std::vector<Document> docs;
for (const auto ¤t : result["rows"s].array_range()){
const auto uuid = current["id"s].as_string();
const auto revision = current["value"s]["rev"s].as_string();
const auto temp = Document(uuid, name, revision, connection);
docs.push_back(temp);
}
return docs;
}
std::vector<std::string> Database::findDocument(const Map &map){
std::vector<std::string> result;
const auto name = this->data.name;
DataBaseConnection::FindOptions options;
json selector;
for (const auto ¤t : map){
selector[current.first] = current.second;
}
options.selector = selector;
auto data = this->data.connection->findDocs(name, options);
if (data.second == DataBaseConnection::FindResult::OK){
for (const auto ¤t : data.first.array_range()){
result.push_back(current["_id"].as_string());
}
}
return result;
}
std::string Database::name() {
return this->data.name;
}
bool Database::compact(){
return this->data.connection->compactDB(this->data.name);
}
long Database::maximalNumberOfRevisions() {
const auto name = this->data.name;
return this->data.connection.get()->maximalNumberOfRevisions(name);
}
bool Database::setMaximalNumberOfRevisions(const long count) {
const auto name = this->data.name;
return this->data.connection.get()->setMaximalNumberOfRevisions(name, count);
}