-
Notifications
You must be signed in to change notification settings - Fork 23
Expand file tree
/
Copy pathcommand_storage.cpp
More file actions
408 lines (348 loc) · 10.5 KB
/
command_storage.cpp
File metadata and controls
408 lines (348 loc) · 10.5 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
// Copyright (C) 2017-2018 Egor Pugin <[email protected]>
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
#include "command_storage.h"
#include "file_storage.h"
#include "sw_context.h"
#include <sw/manager/storage.h>
#include <boost/thread/lock_types.hpp>
#include <primitives/emitter.h>
#include <primitives/date_time.h>
#include <primitives/debug.h>
#include <primitives/exceptions.h>
#include <primitives/lock.h>
#include <primitives/symbol.h>
#include <primitives/log.h>
DECLARE_STATIC_LOGGER(logger, "db_file");
#define COMMAND_DB_FORMAT_VERSION 4
namespace sw
{
static path getCurrentModuleName()
{
return primitives::getModuleNameForSymbol(primitives::getCurrentModuleSymbol());
}
static String getCurrentModuleNameHash()
{
return shorten_hash(blake2b_512(getCurrentModuleName().u8string()), 12);
}
static path getDir(const SwBuilderContext &swctx, bool local)
{
if (local)
return path(SW_BINARY_DIR) / "db" / "0.3.1";
return swctx.getLocalStorage().storage_dir_tmp / "db" / "0.3.1";
}
static path getCommandsDbFilename(const SwBuilderContext &swctx, bool local)
{
return getDir(swctx, local) / std::to_string(COMMAND_DB_FORMAT_VERSION) / "commands.bin";
}
static path getCommandsLogFileName(const SwBuilderContext &swctx, bool local)
{
auto cfg = shorten_hash(blake2b_512(getCurrentModuleNameHash()), 12);
return getDir(swctx, local) / std::to_string(COMMAND_DB_FORMAT_VERSION) / ("cmd_log_" + cfg + ".bin");
}
template <class T>
static void write_int(std::vector<uint8_t> &vec, T val)
{
auto sz = vec.size();
vec.resize(sz + sizeof(val));
memcpy(&vec[sz], &val, sizeof(val));
}
static void write_str(std::vector<uint8_t> &vec, const String &val)
{
auto sz = val.size() + 1;
//write_int(vec, sz);
auto vsz = vec.size();
vec.resize(vsz + sz);
memcpy(&vec[vsz], &val[0], sz);
}
Files CommandRecord::getImplicitInputs(detail::Storage &s) const
{
Files files;
for (auto &h : implicit_inputs)
{
boost::upgrade_lock lk(s.m_file_storage_by_hash);
auto i = s.file_storage_by_hash.find(h);
if (i == s.file_storage_by_hash.end())
throw SW_RUNTIME_ERROR("no such file");
auto p = i->second;
lk.unlock();
if (!p.empty())
files.insert(p);
}
return files;
}
void CommandRecord::setImplicitInputs(const Files &files, detail::Storage &s)
{
for (auto &f : files)
{
auto str = normalize_path(f);
auto h = std::hash<String>()(str);
implicit_inputs.insert(h);
boost::upgrade_lock lk(s.m_file_storage_by_hash);
auto i = s.file_storage_by_hash.find(h);
if (i == s.file_storage_by_hash.end())
{
boost::upgrade_to_unique_lock lk2(lk);
s.file_storage_by_hash[h] = str;
}
}
}
FileDb::FileDb(const SwBuilderContext &swctx)
: swctx(swctx)
{
}
void FileDb::write(std::vector<uint8_t> &v, const CommandRecord &f, const detail::Storage &s)
{
v.clear();
if (f.hash == 0)
return;
//if (!std::is_trivially_copyable_v<decltype(f.mtime)>)
//throw SW_RUNTIME_ERROR("x");
write_int(v, f.hash);
#ifndef __APPLE__
write_int(v, file_time_type2time_t(f.mtime));
#else
write_int(v, *(__int128_t*)&f.mtime);
#endif
auto n = f.implicit_inputs.size();
write_int(v, n);
for (auto &h : f.implicit_inputs)
{
boost::upgrade_lock lk(s.m_file_storage_by_hash);
auto i = s.file_storage_by_hash.find(h);
if (i == s.file_storage_by_hash.end())
throw SW_RUNTIME_ERROR("no such file");
auto p = i->second;
lk.unlock();
write_int(v, std::hash<String>()(normalize_path(p)));
}
}
static String getFilesSuffix()
{
return ".files";
}
static void load(const path &fn, Files &files, std::unordered_map<size_t, path> &files2, ConcurrentCommandStorage &commands)
{
// files
if (fs::exists(path(fn) += getFilesSuffix()))
{
primitives::BinaryStream b;
b.load(path(fn) += getFilesSuffix());
while (!b.eof())
{
size_t sz; // record size
b.read(sz);
if (!b.has(sz))
{
fs::resize_file(path(fn) += getFilesSuffix(), b.index() - sizeof(sz));
break; // record is in bad shape
}
if (sz == 0)
continue;
String s;
b.read(s);
files.insert(s);
files2[std::hash<String>()(s)] = s;
}
}
// commands
if (fs::exists(fn))
{
primitives::BinaryStream b;
b.load(fn);
while (!b.eof())
{
size_t sz; // record size
b.read(sz);
if (!b.has(sz))
{
fs::resize_file(path(fn) += getFilesSuffix(), b.index() - sizeof(sz));
break; // record is in bad shape
}
if (sz == 0)
continue;
size_t h;
b.read(h);
auto r = commands.insert(h);
r.first->hash = h;
//if (!std::is_trivially_copyable_v<decltype(r.first->mtime)>)
//throw SW_RUNTIME_ERROR("x");
#ifndef __APPLE__
time_t m;
b.read(m);
r.first->mtime = time_t2file_time_type(m);
#else
__int128_t m;
b.read(m);
r.first->mtime = *(fs::file_time_type*)&m;
#endif
size_t n;
b.read(n);
r.first->implicit_inputs.reserve(n);
while (n--)
{
b.read(h);
auto &f = files2[h];
if (!f.empty())
{
//r.first->implicit_inputs.insert(files2[h]);
r.first->implicit_inputs.insert(h);
}
}
}
}
}
void FileDb::load(Files &files, std::unordered_map<size_t, path> &files2, ConcurrentCommandStorage &commands, bool local) const
{
sw::load(getCommandsDbFilename(swctx, local), files, files2, commands);
sw::load(getCommandsLogFileName(swctx, local), files, files2, commands);
}
void FileDb::save(const Files &files, const detail::Storage &s, ConcurrentCommandStorage &commands, bool local) const
{
std::vector<uint8_t> v;
// files
{
primitives::BinaryStream b(10'000'000); // reserve amount
for (auto &f : files)
{
auto s = normalize_path(f);
auto sz = s.size() + 1;
b.write(sz);
b.write(s);
}
if (!b.empty())
{
auto p = getCommandsDbFilename(swctx, local) += getFilesSuffix();
fs::create_directories(p.parent_path());
b.save(p);
}
}
// commands
{
primitives::BinaryStream b(10'000'000); // reserve amount
for (const auto &[k, r] : commands)
{
write(v, r, s);
auto sz = v.size();
b.write(sz);
b.write(v.data(), v.size());
}
if (!b.empty())
{
auto p = getCommandsDbFilename(swctx, local);
fs::create_directories(p.parent_path());
b.save(p);
}
}
error_code ec;
fs::remove(getCommandsLogFileName(swctx, local), ec);
fs::remove(getCommandsLogFileName(swctx, local) += getFilesSuffix(), ec);
}
detail::FileHolder::FileHolder(const path &fn)
: f(fn, "ab"), fn(fn)
{
// goes first
// but maybe remove?
//if (setvbuf(f.getHandle(), NULL, _IONBF, 0) != 0)
//throw RUNTIME_EXCEPTION("Cannot disable log buffering");
// Opening a file in append mode doesn't set the file pointer to the file's
// end on Windows. Do that explicitly.
fseek(f.getHandle(), 0, SEEK_END);
}
detail::FileHolder::~FileHolder()
{
f.close();
error_code ec; // remove ec? but multiple processes may be writing into this log? or not?
fs::remove(fn, ec);
}
CommandStorage::CommandStorage(const SwBuilderContext &swctx)
: swctx(swctx), fdb(swctx)
{
load();
}
CommandStorage::~CommandStorage()
{
try
{
closeLogs();
save();
}
catch (std::exception &e)
{
LOG_ERROR(logger, "Error during command db save: " << e.what());
}
}
void CommandStorage::async_command_log(const CommandRecord &r, bool local)
{
static std::vector<uint8_t> v;
swctx.getFileStorageExecutor().push([this, local, &r]
{
auto &s = getInternalStorage(local);
{
// write record to vector v
fdb.write(v, r, s);
auto &l = s.getCommandLog(swctx, local);
auto sz = v.size();
fwrite(&sz, sizeof(sz), 1, l.f.getHandle());
fwrite(&v[0], sz, 1, l.f.getHandle());
fflush(l.f.getHandle());
}
{
auto &l = s.getFileLog(swctx, local);
for (auto &f : r.getImplicitInputs(s))
{
auto r = s.file_storage.insert(f);
if (!r.second)
continue;
auto s = normalize_path(f);
auto sz = s.size() + 1;
fwrite(&sz, sizeof(sz), 1, l.f.getHandle());
fwrite(&s[0], sz, 1, l.f.getHandle());
fflush(l.f.getHandle());
}
}
});
}
void detail::Storage::closeLogs()
{
commands.reset();
files.reset();
}
void CommandStorage::closeLogs()
{
global.closeLogs();
local.closeLogs();
}
detail::FileHolder &detail::Storage::getCommandLog(const SwBuilderContext &swctx, bool local)
{
if (!commands)
commands = std::make_unique<FileHolder>(getCommandsLogFileName(swctx, local));
return *commands;
}
detail::FileHolder &detail::Storage::getFileLog(const SwBuilderContext &swctx, bool local)
{
if (!files)
files = std::make_unique<FileHolder>(getCommandsLogFileName(swctx, local) += getFilesSuffix());
return *files;
}
void CommandStorage::load()
{
fdb.load(local.file_storage, local.file_storage_by_hash, local.storage, true);
fdb.load(global.file_storage, global.file_storage_by_hash, global.storage, false);
}
void CommandStorage::save()
{
fdb.save(local.file_storage, local, local.storage, true);
fdb.save(global.file_storage, global, global.storage, false);
}
ConcurrentCommandStorage &CommandStorage::getStorage(bool local)
{
return getInternalStorage(local).storage;
}
detail::Storage &CommandStorage::getInternalStorage(bool local_)
{
return local_ ? local : global;
}
}