forked from systemed/tilemaker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnode_stores.cpp
More file actions
116 lines (92 loc) · 3.06 KB
/
node_stores.cpp
File metadata and controls
116 lines (92 loc) · 3.06 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
#include <algorithm>
#include <boost/sort/sort.hpp>
#include "node_stores.h"
void BinarySearchNodeStore::reopen()
{
std::lock_guard<std::mutex> lock(mutex);
for (auto i = 0; i < mLatpLons.size(); i++)
mLatpLons[i]->clear();
mLatpLons.clear();
for (auto i = 0; i < NODE_SHARDS; i++) {
mLatpLons.push_back(std::make_unique<map_t>());
}
}
bool BinarySearchNodeStore::contains(size_t shard, NodeID i) const {
auto internalShard = mLatpLons[shardPart(i)];
auto id = idPart(i);
auto iter = std::lower_bound(internalShard->begin(), internalShard->end(), id, [](auto const &e, auto i) {
return e.first < i;
});
return !(iter == internalShard->end() || iter->first != id);
}
LatpLon BinarySearchNodeStore::at(NodeID i) const {
auto shard = mLatpLons[shardPart(i)];
auto id = idPart(i);
auto iter = std::lower_bound(shard->begin(), shard->end(), id, [](auto const &e, auto i) {
return e.first < i;
});
if(iter == shard->end() || iter->first != id)
throw std::out_of_range("Could not find node with id " + std::to_string(i));
return iter->second;
}
size_t BinarySearchNodeStore::size() const {
std::lock_guard<std::mutex> lock(mutex);
uint64_t size = 0;
for (auto i = 0; i < mLatpLons.size(); i++)
size += mLatpLons[i]->size();
return size;
}
void BinarySearchNodeStore::insert(const std::vector<element_t>& elements) {
uint32_t newEntries[NODE_SHARDS] = {};
std::vector<map_t::iterator> iterators;
// Before taking the lock, do a pass to find out how much
// to grow each backing collection
for (auto it = elements.begin(); it != elements.end(); it++) {
newEntries[shardPart(it->first)]++;
}
std::lock_guard<std::mutex> lock(mutex);
for (auto i = 0; i < NODE_SHARDS; i++) {
auto size = mLatpLons[i]->size();
mLatpLons[i]->resize(size + newEntries[i]);
iterators.push_back(mLatpLons[i]->begin() + size);
}
for (auto it = elements.begin(); it != elements.end(); it++) {
uint32_t shard = shardPart(it->first);
uint32_t id = idPart(it->first);
*iterators[shard] = std::make_pair(id, it->second);
iterators[shard]++;
}
}
void BinarySearchNodeStore::finalize(size_t threadNum) {
std::lock_guard<std::mutex> lock(mutex);
for (auto i = 0; i < NODE_SHARDS; i++) {
boost::sort::block_indirect_sort(
mLatpLons[i]->begin(), mLatpLons[i]->end(),
[](auto const &a, auto const &b) { return a.first < b.first; },
threadNum);
}
}
void CompactNodeStore::reopen()
{
std::lock_guard<std::mutex> lock(mutex);
mLatpLons = std::make_unique<map_t>();
}
LatpLon CompactNodeStore::at(NodeID i) const {
if(i >= mLatpLons->size())
throw std::out_of_range("Could not find node with id " + std::to_string(i));
return mLatpLons->at(i);
}
size_t CompactNodeStore::size() const {
std::lock_guard<std::mutex> lock(mutex);
return mLatpLons->size();
}
void CompactNodeStore::insert(const std::vector<element_t>& elements) {
std::lock_guard<std::mutex> lock(mutex);
for(auto const &i: elements)
insert_back(i.first, i.second);
}
// @brief Make the store empty
void CompactNodeStore::clear() {
std::lock_guard<std::mutex> lock(mutex);
mLatpLons->clear();
}