Repository navigation
Expand file tree
/
Copy pathcuration_index.cpp
More file actions
74 lines (66 loc) · 2.33 KB
/
Copy pathcuration_index.cpp
File metadata and controls
74 lines (66 loc) · 2.33 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
#include "curation_index.h"
#include "string_utils.h"
Option<std::map<std::string, curation_t*>> CurationIndex::get_curations(uint32_t limit, uint32_t offset) {
std::shared_lock lock(mutex);
std::map<std::string, curation_t*> res;
auto it = curation_definitions.begin();
if(offset > 0) {
if(offset >= curation_definitions.size()) {
return Option<std::map<std::string, curation_t*>>(400, "Invalid offset param.");
}
std::advance(it, offset);
}
auto end = curation_definitions.end();
if(limit > 0 && (offset + limit < curation_definitions.size())) {
end = it;
std::advance(end, limit);
}
while(it != end) {
res[it->first] = &it->second;
++it;
}
return Option<std::map<std::string, curation_t*>>(res);
}
bool CurationIndex::get_curation(const std::string& id, curation_t& ov) {
std::shared_lock lock(mutex);
auto defIt = curation_definitions.find(id);
if(defIt == curation_definitions.end()) return false;
ov = defIt->second;
return true;
}
Option<bool> CurationIndex::add_curation(const curation_t& ov, bool write_to_store) {
std::unique_lock lock(mutex);
// upsert by id (lexicographic order maintained by map key)
curation_definitions[ov.id] = ov;
lock.unlock();
if(write_to_store) {
bool ok = store->insert(get_curation_key(name, ov.id), ov.to_json().dump());
if(!ok) {
return Option<bool>(500, "Error while storing the curation on disk.");
}
}
return Option<bool>(true);
}
Option<bool> CurationIndex::remove_curation(const std::string& id) {
std::unique_lock lock(mutex);
auto defIt = curation_definitions.find(id);
if(defIt == curation_definitions.end()) {
return Option<bool>(404, "Could not find that `id`.");
}
bool removed = store->remove(get_curation_key(name, id));
if(!removed) {
return Option<bool>(500, "Error while deleting the curation from disk.");
}
curation_definitions.erase(defIt);
return Option<bool>(true);
}
nlohmann::json CurationIndex::to_view_json() const {
std::shared_lock lock(mutex);
nlohmann::json obj;
obj["items"] = nlohmann::json::array();
for(const auto& kv: curation_definitions) {
obj["items"].push_back(kv.second.to_json());
}
obj["name"] = name;
return obj;
}