-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdb.cpp
More file actions
124 lines (96 loc) · 3.41 KB
/
Copy pathdb.cpp
File metadata and controls
124 lines (96 loc) · 3.41 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
#include "db/db.hpp"
#include <algorithm>
#include <functional>
#include <boost/filesystem.hpp>
#include <boost/regex.hpp>
namespace fs = boost::filesystem;
namespace henhouse::db
{
namespace
{
const int MAX_DIR_LENGTH = 8;
const int MAX_DIR_SPLIT_LENGTH = MAX_DIR_LENGTH * 4;
const offset_type NO_OFFSET = 0;
fs::path get_key_dir(const fs::path& root, const stde::string_view& key)
{
REQUIRE(!key.empty());
fs::path p = root;
for(std::size_t i = 0; i < key.size() && i < MAX_DIR_SPLIT_LENGTH; i += MAX_DIR_LENGTH)
{
const auto d = key.substr(i, MAX_DIR_LENGTH);
p.append(d.begin(), d.end());
}
if(key.size() > MAX_DIR_SPLIT_LENGTH)
{
const auto d = key.substr(MAX_DIR_SPLIT_LENGTH, key.size() - MAX_DIR_SPLIT_LENGTH);
p.append(d.begin(), d.end());
}
return p;
}
}
void sanatize_key(std::string& res, const stde::string_view& key)
{
res.assign(key.data(), key.size());
std::replace_if(std::begin(res), std::end(res),
[](char c)
{
return !((c >= '0' && c <= '9') ||
(c >= 'A' && c <= 'Z') ||
(c >= 'a' && c <= 'z'));
}, '_');
}
summary_result timeline_db::summary(const stde::string_view& key) const
{
const auto& tl = get_tl(key);
return tl.summary();
}
get_result timeline_db::get(const stde::string_view& key, time_type t) const
{
const auto& tl = get_tl(key);
return tl.get(t, NO_OFFSET);
}
bool timeline_db::put(const stde::string_view& key, time_type t, count_type count)
{
auto& tl = get_tl(key);
return tl.put(t, count);
}
diff_result timeline_db::diff(const stde::string_view& key, time_type a, time_type b, const offset_type index_offset) const
{
const auto& tl = get_tl(key);
return tl.diff(a, b, index_offset);
}
std::size_t timeline_db::key_index_size(const stde::string_view& key) const
{
const auto& tl = get_tl(key);
return tl.index.size();
}
std::size_t timeline_db::key_data_size(const stde::string_view& key) const
{
const auto& tl = get_tl(key);
return tl.data.size();
}
timeline& timeline_db::get_tl(const stde::string_view& key)
{
REQUIRE_FALSE(key.empty());
const auto h = std::hash<stde::string_view>{}(key);
const auto t = _tls.find(h);
if(t != std::end(_tls)) return t->second;
const auto key_dir = get_key_dir(_root, key);
if(!fs::exists(key_dir)) fs::create_directories(key_dir);
_tls.set(h, from_directory(key_dir.string(), _new_tl_resolution));
auto p = _tls.find(h);
return p->second;
}
const timeline& timeline_db::get_tl(const stde::string_view& key) const
{
REQUIRE_FALSE(key.empty());
const auto h = std::hash<stde::string_view>{}(key);
const auto t = _tls.find(h);
if(t != std::end(_tls)) return t->second;
const auto key_dir = get_key_dir(_root, key);
if(!fs::exists(key_dir)) fs::create_directories(key_dir);
_tls.set(h, from_directory(key_dir.string(), _new_tl_resolution));
auto p = _tls.find(h);
return p->second;
}
}