-
Notifications
You must be signed in to change notification settings - Fork 1
/
mem_db.cpp
59 lines (55 loc) · 1.27 KB
/
mem_db.cpp
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
#include "mem_db.hpp"
#include <array>
#include <fstream>
MemDb &MemDb::instance()
{
static MemDb db;
return db;
}
static auto ToStr(uint32_t v) -> std::string
{
auto dig = std::array{'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};
auto ret = std::string{};
for (auto i = 0U; i < sizeof(v) * 2; ++i)
{
ret = dig[v % 16] + ret;
v /= 16;
}
return ret;
}
std::optional<std::string> MemDb::lookup(uint32_t hash)
{
std::unique_lock<std::mutex> lock(mutex);
for (;;)
{
const auto iter = cache.find(hash);
if (iter != std::end(cache))
return iter->second;
std::ifstream strm(".coddle/" + ToStr(hash) + ".artifact", std::ios::binary);
if (!strm)
{
if (wip.find(hash) != std::end(wip))
{
cv.wait(lock);
continue;
}
wip.insert(hash);
return std::nullopt;
}
std::ostringstream out;
out << strm.rdbuf();
cache.emplace(hash, out.str());
return out.str();
}
}
void MemDb::insert(uint32_t hash, const std::string &serOut)
{
const std::lock_guard<std::mutex> lock(mutex);
cache.emplace(hash, serOut);
{
std::ofstream strm(".coddle/" + ToStr(hash) + ".artifact", std::ios::binary);
strm << serOut;
}
wip.erase(hash);
cv.notify_all();
}