-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlmdb.cpp
More file actions
361 lines (322 loc) · 8.05 KB
/
Copy pathlmdb.cpp
File metadata and controls
361 lines (322 loc) · 8.05 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
#include <catch2/catch.hpp>
#include "temp_file_test.h"
#include "lmdb_wrapper.h"
#include "lmdb_string_store.h"
#include "lmdb_path_store.h"
#include "span.hpp"
#include "test_config.h"
#include "util.h"
#include "trace.h"
namespace lmdb_test {
ConfigPath scanner_output_file { "scanner_output_file", "" };
using namespace Catch::Matchers;
struct LMDB_Test : public TempFileTest
{
auto init_env() {
mdb::mdb_env env;
all_files_created.insert("scanner.mdb");
all_files_created.insert("scanner.mdb-lock");
env.set_maxdbs(5);
env.open((tmp_path / "scanner.mdb").string().c_str(), mdb::flags::env::nosubdir);
return env;
}
};
TEST_CASE("lmdb - multiple spans", "[lmdb]") {
LMDB_Test test;
auto env = test.init_env();
auto txn = env.txn_read_write();
using key_t = uint32_t;
struct value_t {
uint32_t dummy = 42;
tcb::span<uint32_t> a;
tcb::span<uint32_t> b;
};
auto dbi = txn.open_db<key_t, value_t>("db", mdb::flags::open_db::create);
std::vector<uint32_t> b = { 5, 6, 7, 8, 9, 10 };
dbi.put(1, { 43, {}, b });
auto val = dbi.get(1);
CHECK(val.dummy == 43);
CHECK(val.a.size() == 0);
std::vector<uint32_t> b1;
for (uint32_t v : val.b)
b1.push_back(v);
CHECK_THAT(b1, UnorderedEquals(b));
txn.commit();
}
TEST_CASE("lmdb - string store", "[lmdb]") {
LMDB_Test test;
auto env = test.init_env();
auto txn = env.txn_read_write();
mdb::string_id_store<uint32_t> string_store { "strings" };
string_store.init(txn, {});
for (auto str : { "D3","std.core","D4","D2","std.core","D4","D1","std.core","D2","D3","D4","std.core","std.core" })
string_store.try_add(str);
string_store.commit_changes(txn);
txn.commit();
}
TEST_CASE("lmdb - path store", "[lmdb]") {
LMDB_Test test;
auto env = test.init_env();
auto txn = env.txn_read_write();
mdb::path_id_store<uint32_t> ps { "paths" };
test.create_dir("a/.b/c/d");
auto try_add = [&](const fs::path& p) {
return ps.try_add(p.string());
};
fs::path cur_path;
auto set_cur_path = [&](std::string_view rel_path) {
cur_path = test.tmp_path;
if (!rel_path.empty()) cur_path /= rel_path;
ps.update_current_path(cur_path.string());
};
set_cur_path("");
auto check = [&](auto file_id, std::string rel_path) {
INFO("rel path is " << rel_path);
CHECK(file_id == try_add(rel_path));
CHECK(file_id == try_add(cur_path / rel_path));
#ifdef _WIN32
std::replace(rel_path.begin(), rel_path.end(), '/', '\\');
CHECK(file_id == try_add(rel_path));
CHECK(file_id == try_add(cur_path / rel_path));
#endif
};
auto id = try_add("");
CHECK(ps.current_path_id == id);
check(id, "");
check(id, ".");
check(id, "a/..");
check(id, "a/./../");
check(id, "a/.b/../..");
check(id, "a/.b/c//../../../");
check(id, "a//../a/.b/c/../../.b/../../.");
auto id_a = try_add("a");
check(id_a, "a");
check(id_a, "./a");
check(id_a, "a//.b////..");
check(id_a, "a/.b/./c/..///..");
check(id_a, "a/.b/..//../a/../a/");
check(id_a, "a/./.b/c/d/../../..");
set_cur_path("a");
CHECK(ps.current_path_id == id_a);
check(id_a, "");
check(id_a, "../a");
check(id_a, "..//a/.b/./../");
auto id_b = try_add(".b");
check(id_b, "./../a/.b//");
set_cur_path("a/.b");
CHECK(ps.current_path_id == id_b);
check(id_b, "");
check(id_b, ".");
check(id_b, "../../a/.b/c/../.");
// todo: change_dir("C:") check(id, "../x"); etc.
}
auto read_scanner_output() {
TRACE();
std::vector<std::string> ret;
std::ifstream fin(scanner_output_file);
std::string line;
while (std::getline(fin, line)) {
if (line.empty() || line.substr(0, 4) == "::::") continue;
ret.push_back(line);
}
return ret;
}
static bool IsPathSeparator(char c) {
#ifdef _WIN32
return c == '/' || c == '\\';
#else
return c == '/';
#endif
}
bool CanonicalizePath(char* path, size_t* len, uint64_t* slash_bits,
std::string* err) {
// WARNING: this function is performance-critical; please benchmark
// any changes you make to it.
//METRIC_RECORD("canonicalize path");
if (*len == 0) {
*err = "empty path";
return false;
}
const int kMaxPathComponents = 60;
char* components[kMaxPathComponents];
int component_count = 0;
char* start = path;
char* dst = start;
const char* src = start;
const char* end = start + *len;
if (IsPathSeparator(*src)) {
#ifdef _WIN32
// network path starts with //
if (*len > 1 && IsPathSeparator(*(src + 1))) {
src += 2;
dst += 2;
} else {
++src;
++dst;
}
#else
++src;
++dst;
#endif
}
while (src < end) {
if (*src == '.') {
if (src + 1 == end || IsPathSeparator(src[1])) {
// '.' component; eliminate.
src += 2;
continue;
} else if (src[1] == '.' && (src + 2 == end || IsPathSeparator(src[2]))) {
// '..' component. Back up if possible.
if (component_count > 0) {
dst = components[component_count - 1];
src += 3;
--component_count;
} else {
*dst++ = *src++;
*dst++ = *src++;
*dst++ = *src++;
}
continue;
}
}
if (IsPathSeparator(*src)) {
src++;
continue;
}
if (component_count == kMaxPathComponents) {
*err = "path has too many components";
return false;
//Fatal("path has too many components : %s", path);
}
components[component_count] = dst;
++component_count;
while (src != end && !IsPathSeparator(*src))
*dst++ = *src++;
*dst++ = *src++; // Copy '/' or final \0 character as well.
}
if (dst == start) {
*dst++ = '.';
*dst++ = '\0';
}
*len = dst - start - 1;
#if 0
#ifdef _WIN32
uint64_t bits = 0;
uint64_t bits_mask = 1;
for (char* c = start; c < start + *len; ++c) {
switch (*c) {
case '\\':
bits |= bits_mask;
*c = '/';
//NINJA_FALLTHROUGH;
case '/':
bits_mask <<= 1;
}
}
*slash_bits = bits;
#else
* slash_bits = 0;
#endif
#endif
return true;
}
auto hash_map_benchmark(std::vector<std::string> & files) {
TRACE();
//files.resize(files.size() / 100);
//std::unordered_map<std::string, int> map;
//std::unordered_map<std::string_view, int> map;
absl::flat_hash_map<std::string_view, int> map;
//map.reserve(10000);
int i = 0;
std::string err;
for (auto& file : files) {
#if 1
std::size_t len = file.size();
uint64_t slash_bits = 0;
if (!CanonicalizePath(file.data(), &len, &slash_bits, &err)) {
fmt::print("failed to canonicalize: {}\n", err);
return map;
}
file.resize(len);
#endif
auto [itr, inserted] = map.try_emplace(file, i);
if (inserted)
i++;
}
std::cout << i << " unique files out of " << files.size() << "\n";
#if 0
TRACE();
int j = 0;
for (auto& file : files) {
#if 0
std::size_t len = file.size();
uint64_t slash_bits = 0;
if (!CanonicalizePath(file.data(), &len, &slash_bits, &err)) {
fmt::print("failed to canonicalize: {}\n", err);
return map;
}
file.resize(len);
#endif
auto [itr, inserted] = map.try_emplace(file, i);
if (!inserted)
j++;
}
std::cout << j << " insert attempts\n";
#endif
return map;
}
void canonicalize_benchmark(const std::unordered_map<std::string_view, int>& map)
{
TRACE();
std::size_t total_size = 0;
for (auto&& [file, id] : map) {
total_size += std::filesystem::canonical(file).native().size();
}
std::cout << "total canonical length = " << total_size << "\n";
}
TEST_CASE("lmdb - path store - benchmark", "[lmdb_path_store_benchmark]") {
auto files = read_scanner_output();
auto file_map = hash_map_benchmark(files);
//canonicalize_benchmark(file_map);
//return;
LMDB_Test test;
auto env = test.init_env();
auto txn = env.txn_read_write();
mdb::path_id_store<uint32_t> ps { "paths" };
ps.update_current_path();
#if 0
for (int i = 0; i < 10; i++) {
timer t;
t.start();
uint32_t total = 0;
std::unordered_map<std::string, uint32_t> map;
for (auto& file : files) {
auto [itr, inserted] = map.try_emplace(file, 0);
if (inserted)
itr->second = ps.try_add(file);
}
t.stop();
}
#endif
#if 0
for (int i = 0; i < 10; i++) {
timer t;
t.start();
for (auto& file : files) {
ps.try_add(file);
}
t.stop();
std::cout << (uint32_t)ps.next_id << "\n";
}
#endif
#if 1
{
TRACE_BLOCK("path_store: try_add");
for (auto& [file, id] : file_map) {
id = ps.try_add(file);
}
std::cout << "buffer size " << ps.normal_paths.size() << "\n";
}
#endif
}
} // namespace lmdb_test