-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdisk_storage.cpp
More file actions
189 lines (150 loc) · 5.63 KB
/
disk_storage.cpp
File metadata and controls
189 lines (150 loc) · 5.63 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
#include "disk_storage.hpp"
namespace numerator {
static const std::string kEmptyString = "";
static const size_t kRestoreBatchSize = 10000;
static const int kBloomFilterBits = 10;
static const uint32_t kBinaryDumpMagicNumber = 0xdeadface;
void
DiskStorage::init(const std::string &path, int cache)
{
cache_size = cache;
options.create_if_missing = true;
if (cache_size > 0) {
// cache option is in megabytes
options.block_cache = leveldb::NewLRUCache(1024 * 1024 * cache);
}
options.filter_policy = leveldb::NewBloomFilterPolicy(kBloomFilterBits);
leveldb::Status status = leveldb::DB::Open(options, path, &db);
THROW_EXC_IF_FAILED(status.ok(), "LevelDB initialization failed: %s", status.ToString().c_str());
}
void
DiskStorage::write(const KVPairs &kv_pairs)
{
leveldb::WriteBatch batch;
leveldb::Status status;
BOOST_FOREACH(const KVPairs::value_type &p, kv_pairs) {
leveldb::Slice key(reinterpret_cast<const char*>(&p.key), sizeof(p.key));
batch.Put(key, p.value);
}
status = db->Write(leveldb::WriteOptions(), &batch);
THROW_EXC_IF_FAILED(status.ok(), "LevelDB's write method failed: %s", status.ToString().c_str());
}
NumID
DiskStorage::load_in_memory(MemoryStorage &storage)
{
NumID current_id;
NumID max_id = 0;
size_t loaded = 0;
leveldb::ReadOptions read_options;
read_options.fill_cache = false;
boost::shared_ptr<leveldb::Iterator> it(db->NewIterator(read_options));
for (it->SeekToFirst(); it->Valid(); it->Next()) {
THROW_EXC_IF_FAILED(it->status().ok(), "LevelDB iteration failed: %s", it->status().ToString().c_str());
memcpy(¤t_id, it->key().data(), it->key().size());
storage.insert(it->value().ToString(), current_id);
loaded++;
if (current_id > max_id) {
max_id = current_id;
}
}
LOG(INFO) << "loaded " << loaded << " entries";
return max_id;
}
void
DiskStorage::lookup(const Keys &keys, Values &values, Failures &failures)
{
leveldb::Status status;
Values::value_type value;
values.clear();
failures.clear();
FailureIdx failure_idx = 0;
leveldb::ReadOptions read_options;
if (cache_size < 0) {
read_options.fill_cache = false;
}
BOOST_FOREACH(const Keys::value_type &k, keys) {
leveldb::Slice key(reinterpret_cast<const char*>(&k), sizeof(k));
status = db->Get(read_options, key, &value);
if (status.ok()) {
values.push_back(value);
} else if (status.IsNotFound()) {
failures.push_back(failure_idx);
values.push_back(kEmptyString);
} else {
THROW_EXC("LevelDB's Get method failed: %s", status.ToString().c_str());
}
failure_idx++;
}
}
void
DiskStorage::dump(std::ostream &stream, bool binary_dump)
{
NumID current_id;
leveldb::ReadOptions read_options;
read_options.fill_cache = false;
boost::shared_ptr<leveldb::Iterator> it(db->NewIterator(read_options));
if (binary_dump) {
stream.write(reinterpret_cast<const char*>(&kBinaryDumpMagicNumber), sizeof(kBinaryDumpMagicNumber));
}
for (it->SeekToFirst(); it->Valid(); it->Next()) {
THROW_EXC_IF_FAILED(it->status().ok(), "LevelDB iteration failed: %s", it->status().ToString().c_str());
memcpy(¤t_id, it->key().data(), it->key().size());
if (binary_dump) {
uint32_t value_size = it->value().ToString().size();
stream.write(reinterpret_cast<const char*>(¤t_id), sizeof(current_id));
stream.write(reinterpret_cast<const char*>(&value_size), sizeof(value_size));
stream.write(it->value().ToString().c_str(), value_size);
} else {
stream << current_id << "\t" << it->value().ToString() << std::endl;
}
}
}
void
DiskStorage::restore(std::ifstream &stream, bool binary_dump)
{
THROW_EXC_IF_FAILED(db != NULL, "Database wasn't initialized");
KVPairs kv_pairs;
NumID key;
kv_pairs.reserve(kRestoreBatchSize);
if (!binary_dump) {
std::string line;
Values items;
while (std::getline(stream, line)) {
boost::split(items, line, boost::is_any_of("\t"));
try {
key = boost::lexical_cast<NumID>(items[0]);
} catch (std::exception &exc) {
THROW_EXC("%s at \"%s\"", exc.what(), line.c_str());
}
kv_pairs.push_back(KVPair(key, items[1]));
if (kv_pairs.size() >= kRestoreBatchSize) {
write(kv_pairs);
kv_pairs.clear();
}
}
} else {
uint32_t magic_number = 0;
uint32_t value_size = 0;
stream.read(reinterpret_cast<char*>(&magic_number), sizeof(magic_number));
THROW_EXC_IF_FAILED(magic_number == kBinaryDumpMagicNumber, "Invalid binary dump magic number: %u (expected %u)",
magic_number, kBinaryDumpMagicNumber);
std::vector<char> buffer;
std::string value;
while (!stream.eof()) {
stream.read(reinterpret_cast<char*>(&key), sizeof(key));
stream.read(reinterpret_cast<char*>(&value_size), sizeof(value_size));
buffer.reserve(value_size);
stream.read(&buffer[0], value_size);
value.assign(&buffer[0], value_size);
kv_pairs.push_back(KVPair(key, value));
if (kv_pairs.size() >= kRestoreBatchSize) {
write(kv_pairs);
kv_pairs.clear();
}
}
}
if (kv_pairs.size()) {
write(kv_pairs);
}
}
} // namespace