-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdatabase.cpp
More file actions
745 lines (600 loc) · 21 KB
/
database.cpp
File metadata and controls
745 lines (600 loc) · 21 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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
#include <iostream>
#include <fstream>
#include <filesystem>
#include <string>
#include <vector>
#include <map>
#include <set>
#include <chrono>
#include <thread>
#include <random>
#include <nlohmann/json.hpp>
#include "hnswlib.h"
#include "sha256.h"
#include "main.h"
#include "database.h"
#include "storage.h"
// *** LOADING ***
void loadCollection(std::string collectionPath) {
// Firstly load metadata-file (.metadata extension). If it does not exist, we cannot
// load the collection ==> just return and cout message
std::mutex coutLock;
if (!std::filesystem::exists(collectionPath + "/collection.metadata")) {
std::cout << "Attention: Something went wrong! Collection may be corrupted" << std::endl;
std::cout << "Collection cannot be loaded: " << collectionPath << " Aborted." << std::endl;
return;
}
// Load and Parse metadata
std::ifstream fs(collectionPath + "/collection.metadata", std::fstream::in);
nlohmann::json metadata;
metadata << fs;
fs.close();
// Create collection and set metadata
collection_t* col = new collection_t(metadata["name"]);
collections[metadata["name"]] = col;
// Set indexes
if (metadata.contains("indexes") && metadata["indexes"].is_array()) {
for (const auto& indexMeta : metadata["indexes"]) {
// Parse JSON Object to Index
const std::string indexName = indexMeta["name"].get<std::string>();
const nlohmann::json indexObject = nlohmann::json(indexMeta);
std::shared_ptr<DbIndex::Iindex_t> index = DbIndex::loadIndexFromJSON(indexObject);
// Set Index in collection
if (index != nullptr)
col->indexes[indexName] = index;
else
std::cout << "Cannot load index: " << indexName << std::endl;
}
}
// Iterate through the collectionPath-Folder and load all storage files (.knndb extension)
std::vector<std::thread> workers;
for (auto& file : std::filesystem::directory_iterator(collectionPath)) {
if (file.path().filename().u8string().find(".knndb") == std::string::npos)
continue; // No storage file knndb file
// Load .KNNDB File in another Thread
workers.push_back(std::thread([file, col, &coutLock]() {
try {
group_storage_t* storage = new group_storage_t(file.path().u8string());
col->storage.push_back(storage);
}
catch (std::exception ex) {
// File is corrupted
// ==> happens when new created documents are inserted and the program exits when it never
// wrote down or when it is writing down and the program exits. Then the other file
// should yet exist and we can savely remove this file (new created docs are unlucky/maybe later we fix that (: )
std::filesystem::remove(file);
}
}));
}
// Finish all Worker
for (size_t i = 0; i < workers.size(); ++i)
{
if (workers[i].joinable())
workers[i].join();
}
// Build indexes in the background
std::thread(&collection_t::BuildIndexes, col).detach();
}
void loadDatabase(std::string dataPath) {
// Iterate through the dataPath-Folder and check all folders for collection-folders
std::cout << "[INFO] Loading Collections..." << std::endl;
for (const auto& file : std::filesystem::directory_iterator(dataPath)) {
const std::string filePath = file.path().u8string();
const std::string fileName = file.path().filename().u8string();
if (fileName.find("col_") != std::string::npos && std::filesystem::is_directory(filePath)) {
// Found saved collection
std::cout << " - " << fileName;
loadCollection(filePath);
std::cout << " ==> SUCCESS" << std::endl;
}
}
std::cout << "[INFO] Loaded all Collections. Indexes are building in the background" << std::endl;
}
void saveDatabase(std::string dataPath) {
// Save Collections
for (const auto& col : collections) {
std::unique_lock<std::mutex> lockGuard(col.second->saveLock, std::defer_lock);
for (const auto storage : col.second->storage)
storage->save();
lockGuard.lock();
// Save Metadata to JSON-Object
nlohmann::json dbMetadata;
dbMetadata["name"] = col.first;
dbMetadata["indexes"] = DbIndex::saveIndexesToString(col.second->indexes);
// Write JSON down
std::ofstream metadataFile (dataPath + "/col_" + col.first + "/collection.metadata", std::fstream::trunc);
metadataFile << dbMetadata.dump();
metadataFile.close();
lockGuard.unlock();
}
}
// *** collection_t ***
collection_t::collection_t(std::string name)
{
this->storage = {};
this->name = name;
}
std::vector<size_t> collection_t::insertDocuments(const std::vector<nlohmann::json> documents)
{
if (!documents.size())
return {}; // Emtpy
std::random_device rndDev;
std::mt19937 rng(rndDev());
// Find one random storage container
group_storage_t* storage = nullptr;
size_t storageSize = this->storage.size();
for (size_t i = 0; i < storageSize; ++i) {
storage = this->storage[std::rand() % storageSize];
if(storage->countDocuments() < MAX_ELEMENTS_IN_STORAGE)
break; // is not full
}
// Check if Full, all containers are full or less than 10 containers exist
if (storage == nullptr || storage->countDocuments() >= MAX_ELEMENTS_IN_STORAGE || this->storage.size() < 10) {
// Create new Storage File
const std::string storagePath = DATA_PATH + "/col_" + this->name + "/storageNew" + std::to_string(rng()) + ".knndb";
storage = new group_storage_t(storagePath);
this->storage.push_back(storage);
}
// Enter all Documents
std::vector<size_t> entereredIds(documents.size());
auto enteredIdIT = entereredIds.begin();
for (auto doc : documents) {
if (!doc.contains("id")) {
// Create unused ID
bool hasUnusedOne = false;
while (!hasUnusedOne) {
size_t value = static_cast<size_t>(rng());
doc["id"] = value;
// Check if it is used
hasUnusedOne = true;
for (const auto& item : this->storage) {
if (item->savedHere(value)) {
hasUnusedOne = false;
break;
}
}
}
}
storage->insertDocument(doc);
*enteredIdIT = doc["id"];
++enteredIdIT;
}
return entereredIds;
}
std::set<std::tuple<std::string, DbIndex::IndexType, std::shared_ptr<DbIndex::Iindex_t>>> collection_t::getIndexedKeys()
{
std::set<std::tuple<std::string, DbIndex::IndexType, std::shared_ptr<DbIndex::Iindex_t>>> list;
// Get all Keys from all Indexes
for (const auto& index : this->indexes) {
const auto type = index.second->getType();
for(const auto& keyName : index.second->getIncludedKeys())
list.insert(std::tuple<std::string, DbIndex::IndexType, std::shared_ptr<DbIndex::Iindex_t>>(keyName, type, index.second));
}
return list;
}
void collection_t::BuildIndexes()
{
if (!this->indexes.size())
return; // No Indexes exists
// Mark Waiting Stage: If try_lock (or own_lock) returns false, someone else is already waiting
// so we can just return
std::unique_lock<std::mutex> waitLock(this->indexBuilderWaiting, std::try_to_lock);
if (!waitLock.owns_lock())
return;
// Mark Working Stage: Waiting until we can start and then mark that we
// are not waiting anymore
std::unique_lock<std::mutex> workLock(this->indexBuilderWorking);
waitLock.unlock();
// Create the Indexes completly new (get Savestring and reload them) in the background and reset them directly
std::map<std::string, std::shared_ptr<DbIndex::Iindex_t>> backgroundIndexes = {};
for (const auto& [name, index] : this->indexes) {
std::map<std::string, std::shared_ptr<DbIndex::Iindex_t>> m = { { name, index } };
auto metadata = DbIndex::saveIndexesToString(m)[0];
backgroundIndexes[name] = DbIndex::loadIndexFromJSON(metadata);
backgroundIndexes[name]->reset();
backgroundIndexes[name]->inBuilding = true;
}
auto workerFunc = [&backgroundIndexes](const nlohmann::json& item) {
// Add Item to all Indexes
for (const auto& [name, index] : backgroundIndexes)
index->addItem(item);
};
// Iterate through storages and process all items (in Threads)
std::vector<std::thread> storageWorker = {};
for (const auto& storage : this->storage)
storageWorker.push_back(std::thread(&group_storage_t::doFuncOnAllDocuments, storage, workerFunc));
// Wait for all to complete
for (auto& worker : storageWorker)
worker.join();
// Finish all Indexes and unlock
for (const auto& [name, index] : backgroundIndexes) {
index->finish();
index->inBuilding = false;
}
// Push background Index to Live
for (auto& [name, index] : backgroundIndexes)
this->indexes[name] = index;
}
size_t collection_t::countDocuments()
{
size_t total = 0;
for (const auto& storage : this->storage)
total += storage->countDocuments();
return total;
}
// *** Indexes ***
// *** KeyValue Index ***
DbIndex::KeyValueIndex_t::KeyValueIndex_t(std::string keyName, bool isHashedIndex)
{
this->perfomedOnKey = keyName;
this->isHashedIndex = isHashedIndex;
this->data = {};
this->createdAt = 0;
}
void DbIndex::KeyValueIndex_t::reset()
{
std::unique_lock<std::mutex> lockGuard(this->useLock);
this->data.clear();
}
void DbIndex::KeyValueIndex_t::finish()
{
// Set time
this->createdAt = std::chrono::duration_cast<std::chrono::seconds>(
std::chrono::system_clock::now().time_since_epoch() // Since 1970
).count();
}
void DbIndex::KeyValueIndex_t::addItem(const nlohmann::json& item)
{
if (!item.contains(this->perfomedOnKey))
return;
// document contains the key
auto dataKey = item[this->perfomedOnKey];
if (this->isHashedIndex)
dataKey = sha256(dataKey.dump());
std::unique_lock<std::mutex> lockGuard(this->useLock);
this->data[dataKey].push_back(item["id"].get<size_t>());
}
std::vector<std::vector<size_t>> DbIndex::KeyValueIndex_t::perform(std::vector<std::string>& values)
{
while (this->inBuilding)
std::this_thread::sleep_for(std::chrono::nanoseconds(25));
std::unique_lock<std::mutex> lockGuard(this->useLock);
std::vector<std::vector<size_t>> result = {};
if (!this->data.size() || !values.size()) // Nothing available: Not build or No data
return result;
// Find for every Value the result and push it to result
result.reserve(values.size());
for (const std::string& value : values) {
if (this->isHashedIndex) {
result.push_back(this->data[sha256(value)]);
}
else {
auto obj = nlohmann::json::parse(value);
result.push_back(this->data[obj]);
}
}
return result;
}
nlohmann::json DbIndex::KeyValueIndex_t::saveMetadata()
{
nlohmann::json metadata;
metadata["type"] = DbIndex::IndexType::KeyValueIndex;
metadata["isHashedIndex"] = this->isHashedIndex;
metadata["keyName"] = this->perfomedOnKey;
return metadata;
}
std::set<std::string> DbIndex::KeyValueIndex_t::getIncludedKeys()
{
return { this->perfomedOnKey };
}
// *** Multiple KeyValue Index ***
DbIndex::MultipleKeyValueIndex_t::MultipleKeyValueIndex_t(std::vector<std::string> keyNames, bool isFullHashedIndex, std::vector<bool> isHashedIndex)
{
this->indexes = {};
this->keyNames = keyNames;
this->isFullHashedIndex = isFullHashedIndex;
this->isHashedIndex = isHashedIndex;
// Enter all Sub-Indexes
for (int i = 0; i < keyNames.size(); ++i) {
std::shared_ptr<DbIndex::KeyValueIndex_t> subIndex(
new DbIndex::KeyValueIndex_t(
keyNames[i],
(isFullHashedIndex || (isHashedIndex.size() == keyNames.size() && isHashedIndex[i]))
)
);
this->indexes.push_back(subIndex);
}
this->createdAt = 0;
}
void DbIndex::MultipleKeyValueIndex_t::reset()
{
for (const auto& subIndex : this->indexes)
subIndex->reset();
}
void DbIndex::MultipleKeyValueIndex_t::finish()
{
// Set time
this->createdAt = std::chrono::duration_cast<std::chrono::seconds>(
std::chrono::system_clock::now().time_since_epoch() // Since 1970
).count();
}
void DbIndex::MultipleKeyValueIndex_t::addItem(const nlohmann::json& item)
{
for (const auto& subIndex : this->indexes)
subIndex->addItem(item);
}
std::vector<size_t> DbIndex::MultipleKeyValueIndex_t::perform(std::map<std::string, std::vector<std::string>>& query) {
while (this->inBuilding)
std::this_thread::sleep_for(std::chrono::nanoseconds(25));
std::unique_lock<std::mutex> lockGuard(this->useLock);
if (!this->indexes.size() || !query.size()) // Nothing available: Not build or No data
return std::vector<size_t>();
// Get all Findings from the Sub Indexes and calculate all ids
std::map<size_t, size_t> idScores = {}; //1. doc id 2. score
size_t maxScore = 0;
for (const auto& index : this->indexes) {
if (query.count(index->perfomedOnKey)) {
// Query-key is indexes here
for (const auto& subResult : index->perform(query[index->perfomedOnKey])) {
// Perform Index
for (const auto& docID : subResult) {
// Add 1 to every result id
if (idScores.count(docID))
idScores[docID] += 1;
else
idScores[docID] = 1;
}
}
++maxScore;
}
}
// Get all ids, which got the maxScore
std::set<size_t> result = {};
for (const auto& pair : idScores) {
if (pair.second == maxScore) // maxScore is achieved
result.insert(pair.first);
}
return std::vector<size_t>(result.begin(), result.end());
}
nlohmann::json DbIndex::MultipleKeyValueIndex_t::saveMetadata()
{
nlohmann::json metadata;
metadata["type"] = DbIndex::IndexType::MultipleKeyValueIndex;
metadata["keyNames"] = this->keyNames;
metadata["isFullHashedIndex"] = this->isFullHashedIndex;
metadata["isHashedIndex"] = this->isHashedIndex;
return metadata;
}
std::set<std::string> DbIndex::MultipleKeyValueIndex_t::getIncludedKeys()
{
std::set<std::string> keys = {};
for (const auto& subIndex : this->indexes)
keys.insert(subIndex->perfomedOnKey);
return keys;
}
// *** Knn Index ***
DbIndex::KnnIndex_t::KnnIndex_t(std::string keyName, size_t space)
{
this->perfomedOnKey = keyName;
this->space = hnswlib::L2Space(space);
this->index = std::shared_ptr<hnswlib::HierarchicalNSW<float>>(new hnswlib::HierarchicalNSW<float>(&this->space, 0));
this->spaceValue = space;
this->createdAt = 0;
this->elementCount = 0;
}
std::vector<std::vector<size_t>> DbIndex::KnnIndex_t::perform(std::vector<std::vector<float>>& query, size_t limit)
{
std::vector<std::vector<size_t>> results;
results.reserve(query.size());
std::vector<size_t> queryR (limit);
for (const auto& value : query) {
// Calculate K-Nearest-Neighbours
auto gd = this->index->searchKnn(value.data(), limit);
int gdIndex = gd.size() - 1;
// Reverse and remove Distance at the same time
while (!gd.empty()) {
const auto [rDis, rID] = gd.top();
queryR[gdIndex] = rID;
gd.pop();
--gdIndex;
}
results.push_back(queryR);
}
return results;
}
void DbIndex::KnnIndex_t::reset()
{
std::unique_lock<std::mutex> lockGuard(this->useLock);
// Default 3 elements
std::shared_ptr<hnswlib::HierarchicalNSW<float>>(new hnswlib::HierarchicalNSW<float>(&this->space, 3));
}
void DbIndex::KnnIndex_t::finish()
{
// Set time
this->createdAt = std::chrono::duration_cast<std::chrono::seconds>(
std::chrono::system_clock::now().time_since_epoch() // Since 1970
).count();
}
void DbIndex::KnnIndex_t::addItem(const nlohmann::json& item)
{
const auto document = item;
if (!document.contains(this->perfomedOnKey))
return;
if (!document[this->perfomedOnKey].is_array())
return;
// Got the key and it is an array
std::vector<float> data = std::vector<float>(this->spaceValue);
auto it = data.begin();
// Build data-vector with only floats
for (const auto& item : document[this->perfomedOnKey].items()) {
const auto value = item.value();
if (value.is_number_float() || value.is_number() || value.is_number_integer())
*it = value.get<float>(); // Is integral value
else if (value.is_string())
*it = std::stof(value.get<std::string>()); // Was string
else
*it = 0; // Something weird is that
++it;
if (it == data.end())
break; // There are too much, just abort
}
// Add Padding
while (it != data.end()) {
*it = 0;
++it;
}
std::unique_lock<std::mutex> lockGuard(this->useLock);
// Add one more to index
this->elementCount += 1;
static_cast<hnswlib::HierarchicalNSW<float>*>(this->index.get())->resizeIndex(this->elementCount);
// Add Datapoint when it is locked
hnswlib::labeltype _id = document["id"].get<size_t>();
this->index->addPoint(data.data(), _id);
}
void DbIndex::KnnIndex_t::perform(const std::vector<float>& query, std::priority_queue<std::pair<float, hnswlib::labeltype>>* result, size_t limit) {
while (this->inBuilding)
std::this_thread::sleep_for(std::chrono::nanoseconds(25));
std::unique_lock<std::mutex> lockGuard(this->useLock);
*result = this->index->searchKnn(query.data(), limit);
}
nlohmann::json DbIndex::KnnIndex_t::saveMetadata()
{
nlohmann::json metadata;
metadata["type"] = DbIndex::IndexType::KnnIndex;
metadata["keyName"] = this->perfomedOnKey;
metadata["space"] = this->spaceValue;
return metadata;
}
std::set<std::string> DbIndex::KnnIndex_t::getIncludedKeys()
{
return { this->perfomedOnKey };
}
// *** Range Index ***
DbIndex::RangeIndex_t::RangeIndex_t(std::string keyName)
{
this->perfomedOnKey = keyName;
this->data = {};
this->createdAt = 0;
}
void DbIndex::RangeIndex_t::reset()
{
std::unique_lock<std::mutex> lockGuard(this->useLock);
this->data.clear();
}
void DbIndex::RangeIndex_t::finish()
{
this->createdAt = std::chrono::duration_cast<std::chrono::seconds>(
std::chrono::system_clock::now().time_since_epoch() // Since 1970
).count();
}
void DbIndex::RangeIndex_t::addItem(const nlohmann::json& item)
{
if(!item.contains(this->perfomedOnKey))
return;
// Parse Value and insert it
try {
auto value = item[this->perfomedOnKey];
std::unique_lock<std::mutex> lockGuard(this->useLock);
if (value.is_number())
this->data.insert(std::make_pair(value.get<float>(), item["id"].get<size_t>()));
else if (value.is_string())
this->data.insert(std::make_pair(std::stof(value.get<std::string>()), item["id"].get<size_t>()));
}
catch (int code) {}
}
void DbIndex::RangeIndex_t::perform(float lowerBound, float higherBound, std::vector<size_t>& results)
{
while (this->inBuilding)
std::this_thread::sleep_for(std::chrono::nanoseconds(25));
std::unique_lock<std::mutex> lockGuard(this->useLock);
auto itLower = this->data.lower_bound(lowerBound);
auto itHigher = this->data.upper_bound(higherBound);
for (auto it = itLower; it != itHigher; ++it)
results.push_back(it->second);
}
nlohmann::json DbIndex::RangeIndex_t::saveMetadata()
{
nlohmann::json metadata;
metadata["type"] = DbIndex::IndexType::RangeIndex;
metadata["keyName"] = this->perfomedOnKey;
return metadata;
}
// *** Metadata Saver/Loader
std::shared_ptr<DbIndex::Iindex_t> DbIndex::loadIndexFromJSON(const nlohmann::json& metadata)
{
std::shared_ptr<DbIndex::Iindex_t> index = nullptr;
const std::string indexName = metadata["name"].get<std::string>();
const DbIndex::IndexType indexType = metadata["type"].get<DbIndex::IndexType>();
switch (indexType) {
case DbIndex::IndexType::KeyValueIndex:
index = std::shared_ptr<DbIndex::Iindex_t>(
new DbIndex::KeyValueIndex_t(metadata["keyName"].get<std::string>(), metadata["isHashedIndex"].get<bool>())
);
break;
case DbIndex::IndexType::MultipleKeyValueIndex:
index = std::shared_ptr<DbIndex::Iindex_t>(
new DbIndex::MultipleKeyValueIndex_t(metadata["keyNames"], metadata["isFullHashedIndex"].get<bool>(), metadata["isHashedIndex"])
);
break;
case DbIndex::IndexType::KnnIndex:
index = std::shared_ptr<DbIndex::Iindex_t>(
new DbIndex::KnnIndex_t(metadata["keyName"].get<std::string>(), metadata["space"].get<size_t>())
);
break;
case DbIndex::IndexType::RangeIndex:
index = std::shared_ptr<DbIndex::Iindex_t>(
new DbIndex::RangeIndex_t(metadata["keyName"].get<std::string>())
);
break;
}
return index;
}
std::vector<nlohmann::json> DbIndex::saveIndexesToString(std::map<std::string, std::shared_ptr<DbIndex::Iindex_t>>& indexes)
{
std::vector<nlohmann::json> savedIndexes = {};
for (const auto& pair : indexes) {
nlohmann::json metadata = pair.second->saveMetadata();
metadata["name"] = pair.first;
savedIndexes.push_back(metadata);
}
return savedIndexes;
}
namespace CollectionFunctions {
void performTTLCheck(const collection_t* col) {
// Iterate through every storage and document to check wheter the &ttl field exists
auto nowTimeSeconds = std::chrono::duration_cast<std::chrono::seconds>(
std::chrono::system_clock::now().time_since_epoch() // Since 1970
).count();
for (const auto& storage : col->storage) {
std::vector<size_t> ttlExpired = {}; // document ids
// Check all documents
storage->doFuncOnAllDocuments([&nowTimeSeconds, &ttlExpired](const nlohmann::json& document) {
if (!document.contains("&ttl"))
return; // Field does not exist
// Calc diff
auto docTTLSeconds = document["&ttl"].get<long long>();
long long diff = nowTimeSeconds - docTTLSeconds;
// If nowTime is greater than docTime ==> diff > 0 ==> Document's TTL is expired
// If nowTime is less than docTime ==> diff < 0 ==> Document's TTL is NOT expired
if (diff > 0) // Expired
ttlExpired.push_back(document["id"].get<size_t>());
});
// Remove expired ones
for (const size_t& id : ttlExpired)
storage->removeDocument(id);
if (ttlExpired.size()) // Save if something happend
storage->save();
}
}
void runCircle() {
while (!INTERRUPT) {
// Do TTL Check
for (const auto& item : collections)
performTTLCheck(item.second);
std::this_thread::sleep_for(std::chrono::minutes(5));
}
}
void StartManagerThread() {
managerThread = std::thread(runCircle);
}
}