-
Notifications
You must be signed in to change notification settings - Fork 2
/
db.hpp
238 lines (194 loc) · 9.01 KB
/
db.hpp
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
#pragma once
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wold-style-cast"
#define RAPIDJSON_HAS_STDSTRING 1
#include <rapidjson/writer.h>
#include <rapidjson/stringbuffer.h>
#include "rapidjson/document.h"
#pragma GCC diagnostic pop
#include "rocksdb/db.h"
#include <rocksdb/table.h>
#include <rocksdb/filter_policy.h>
#include <rocksdb/cache.h>
#include <rocksdb/write_batch.h>
#include <osmium/osm/types.hpp>
#include <osmium/visitor.hpp>
#include <chrono>
const std::string make_lookup(const int64_t osm_id, const int version){
return std::to_string(osm_id) + "!" + std::to_string(version);
}
class TagStore {
rocksdb::DB* m_db;
rocksdb::ColumnFamilyHandle* m_cf_ways;
rocksdb::ColumnFamilyHandle* m_cf_nodes;
rocksdb::ColumnFamilyHandle* m_cf_relations;
rocksdb::WriteOptions m_write_options;
rocksdb::WriteBatch m_buffer_batch;
void flush_family(const std::string type, rocksdb::ColumnFamilyHandle* cf) {
const auto start = std::chrono::steady_clock::now();
std::cerr << "Flushing " << type << std::endl;
m_db->Flush(rocksdb::FlushOptions{}, cf);
const auto end = std::chrono::steady_clock::now();
const auto diff = end - start;
std::cerr << "Flushed " << type << " in " << std::chrono::duration <double, std::milli> (diff).count() << " ms" << std::endl;
}
void compact_family(const std::string type, rocksdb::ColumnFamilyHandle* cf) {
const auto start = std::chrono::steady_clock::now();
std::cerr << "Compacting " << type << std::endl;
m_db->CompactRange(rocksdb::CompactRangeOptions{}, cf, nullptr, nullptr);
const auto end = std::chrono::steady_clock::now();
const auto diff = end - start;
std::cerr << "Compacted " << type << " in " << std::chrono::duration <double, std::milli> (diff).count() << " ms" << std::endl;
}
void report_count_stats() {
uint64_t node_keys{0};
m_db->GetIntProperty(m_cf_nodes, "rocksdb.estimate-num-keys", &node_keys);
std::cerr << "Stored ~" << node_keys << "/" << stored_nodes_count << " nodes" << std::endl;
uint64_t way_keys{0};
m_db->GetIntProperty(m_cf_ways, "rocksdb.estimate-num-keys", &way_keys);
std::cerr << "Stored ~" << way_keys << "/" << stored_ways_count << " ways" << std::endl;
uint64_t relation_keys{0};
m_db->GetIntProperty(m_cf_relations, "rocksdb.estimate-num-keys", &relation_keys);
std::cerr << "Stored ~" << relation_keys << "/" << stored_relations_count << " relations" << std::endl;
}
public:
unsigned long empty_objects_count{0};
unsigned long stored_tags_count{0};
unsigned long stored_nodes_count{0};
unsigned long stored_ways_count{0};
unsigned long stored_relations_count{0};
unsigned long stored_objects_count() {
return stored_nodes_count + stored_ways_count + stored_relations_count;
}
TagStore(const std::string index_dir, const bool create) {
rocksdb::Options db_options;
db_options.allow_mmap_writes = false;
db_options.max_background_flushes = 4;
db_options.PrepareForBulkLoad();
db_options.target_file_size_base = 512 * 1024 * 1024;
m_write_options = rocksdb::WriteOptions();
m_write_options.disableWAL = true;
m_write_options.sync = false;
rocksdb::BlockBasedTableOptions table_options;
table_options.filter_policy = std::shared_ptr<const rocksdb::FilterPolicy>(rocksdb::NewBloomFilterPolicy(10));
db_options.table_factory.reset(NewBlockBasedTableFactory(table_options));
rocksdb::Status s;
if(create) {
// always clear out the previous tag index first
rocksdb::DestroyDB(index_dir, db_options);
db_options.create_if_missing = true;
s = rocksdb::DB::Open(db_options, index_dir, &m_db);
s = m_db->CreateColumnFamily(rocksdb::ColumnFamilyOptions(), "nodes", &m_cf_nodes);
assert(s.ok());
s = m_db->CreateColumnFamily(rocksdb::ColumnFamilyOptions(), "ways", &m_cf_ways);
assert(s.ok());
s = m_db->CreateColumnFamily(rocksdb::ColumnFamilyOptions(), "relations", &m_cf_relations);
assert(s.ok());
} else {
db_options.error_if_exists = false;
db_options.create_if_missing = false;
std::cout << "Open without create";
// open DB with two column families
std::vector<rocksdb::ColumnFamilyDescriptor> column_families;
// have to open default column family
column_families.push_back(rocksdb::ColumnFamilyDescriptor(
rocksdb::kDefaultColumnFamilyName, rocksdb::ColumnFamilyOptions()));
// open the new one, too
column_families.push_back(rocksdb::ColumnFamilyDescriptor( "nodes", rocksdb::ColumnFamilyOptions()));
column_families.push_back(rocksdb::ColumnFamilyDescriptor( "ways", rocksdb::ColumnFamilyOptions()));
column_families.push_back(rocksdb::ColumnFamilyDescriptor( "relations", rocksdb::ColumnFamilyOptions()));
std::vector<rocksdb::ColumnFamilyHandle*> handles;
s = rocksdb::DB::Open(db_options, index_dir, column_families, &handles, &m_db);
assert(s.ok());
m_cf_nodes = handles[1];
m_cf_ways = handles[2];
m_cf_relations = handles[3];
}
}
rocksdb::Status get_tags(const int64_t osm_id, const int osm_type, const int version, std::string* json_value) {
const auto lookup = make_lookup(osm_id, version);
if(osm_type== 1) {
return m_db->Get(rocksdb::ReadOptions(), m_cf_nodes, lookup, json_value);
} else if (osm_type == 2) {
return m_db->Get(rocksdb::ReadOptions(), m_cf_ways, lookup, json_value);
} else {
return m_db->Get(rocksdb::ReadOptions(), m_cf_relations, lookup, json_value);
}
}
void store_tags(const osmium::Way& way) {
if(store_tags(way, m_cf_ways)) {
stored_ways_count++;
}
}
void store_tags(const osmium::Node& node) {
if(store_tags(node, m_cf_nodes)) {
stored_nodes_count++;
}
}
void store_tags(const osmium::Relation& relation) {
if(store_tags(relation, m_cf_relations)) {
stored_relations_count++;
}
}
bool store_tags(const osmium::OSMObject& object, rocksdb::ColumnFamilyHandle* cf) {
const auto lookup = make_lookup(object.id(), object.version());
if (object.tags().empty()) {
empty_objects_count++;
return false;
}
rapidjson::Document doc;
doc.SetObject();
rapidjson::Document::AllocatorType& a = doc.GetAllocator();
doc.AddMember("@timestamp", object.timestamp().to_iso(), a); //ISO is helpful for debugging, but we should leave it
if (object.deleted()){
doc.AddMember("@deleted", object.deleted(), a);
}
doc.AddMember("@visible", object.visible(), a);
doc.AddMember("@user", std::string{object.user()}, a);
doc.AddMember("@uid", object.uid(), a);
doc.AddMember("@changeset", object.changeset(), a);
doc.AddMember("@version", object.version(), a);
//Ignore trying to store geometries, but if we could scale that, it'd be awesome.
const osmium::TagList& tags = object.tags();
rapidjson::Value object_tags(rapidjson::kObjectType);
for (const osmium::Tag& tag : tags) {
rapidjson::Value key(rapidjson::StringRef(tag.key()));
rapidjson::Value value(rapidjson::StringRef(tag.value()));
object_tags.AddMember(key, value, a);
stored_tags_count++;
}
doc.AddMember("@tags", object_tags, a);
rapidjson::StringBuffer buffer;
rapidjson::Writer<rapidjson::StringBuffer> writer(buffer);
doc.Accept(writer);
rocksdb::Status stat = m_buffer_batch.Put(cf, lookup, buffer.GetString());
if (m_buffer_batch.Count() > 500) {
m_db->Write(m_write_options, &m_buffer_batch);
m_buffer_batch.Clear();
}
if (stored_nodes_count != 0 && (stored_nodes_count % 2000000) == 0) {
flush_family("nodes", m_cf_nodes);
report_count_stats();
}
if (stored_ways_count != 0 && (stored_ways_count % 1000000) == 0) {
flush_family("ways", m_cf_ways);
report_count_stats();
}
if (stored_relations_count != 0 && (stored_relations_count % 1000000) == 0) {
flush_family("relations", m_cf_relations);
report_count_stats();
}
return true;
}
void flush() {
m_db->Write(m_write_options, &m_buffer_batch);
m_buffer_batch.Clear();
flush_family("nodes", m_cf_nodes);
flush_family("ways", m_cf_ways);
flush_family("relations", m_cf_relations);
compact_family("nodes", m_cf_nodes);
compact_family("ways", m_cf_ways);
compact_family("relations", m_cf_relations);
report_count_stats();
}
};