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
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
|
#pragma once
#include "generator/osm_decl.hpp"
#include "coding/file_name_utils.hpp"
#include "coding/file_reader.hpp"
#include "coding/file_writer.hpp"
#include "coding/mmap_reader.hpp"
#include "base/logging.hpp"
#include "std/algorithm.hpp"
#include "std/deque.hpp"
#include "std/exception.hpp"
#include "std/limits.hpp"
#include "std/utility.hpp"
#include "std/vector.hpp"
#include "std/fstream.hpp"
#include "defines.hpp"
/// Classes for reading and writing any data in file with map of offsets for
/// fast searching in memory by some key.
namespace cache
{
enum class EMode { Write = true, Read = false };
namespace detail
{
template <class TFile, class TValue>
class IndexFile
{
using TKey = uint64_t;
static_assert(is_integral<TKey>::value, "TKey is not integral type");
using TElement = pair<TKey, TValue>;
using TContainer = vector<TElement>;
TContainer m_elements;
TFile m_file;
uint64_t m_fileSize = 0;
static size_t constexpr kFlushCount = 1024;
struct ElementComparator
{
bool operator()(TElement const & r1, TElement const & r2) const
{
return ((r1.first == r2.first) ? r1.second < r2.second : r1.first < r2.first);
}
bool operator()(TElement const & r1, TKey r2) const { return (r1.first < r2); }
bool operator()(TKey r1, TElement const & r2) const { return (r1 < r2.first); }
};
static size_t CheckedCast(uint64_t v)
{
ASSERT_LESS(v, numeric_limits<size_t>::max(), ("Value too long for memory address : ", v));
return static_cast<size_t>(v);
}
public:
IndexFile(string const & name) : m_file(name.c_str()) {}
string GetFileName() const { return m_file.GetName(); }
void WriteAll()
{
if (m_elements.empty())
return;
m_file.Write(&m_elements[0], m_elements.size() * sizeof(TElement));
m_elements.clear();
}
void ReadAll()
{
m_elements.clear();
m_fileSize = m_file.Size();
if (m_fileSize == 0)
return;
LOG_SHORT(LINFO, ("Offsets reading is started for file ", GetFileName()));
CHECK_EQUAL(0, m_fileSize % sizeof(TElement), ("Damaged file."));
try
{
m_elements.resize(CheckedCast(m_fileSize / sizeof(TElement)));
}
catch (exception const &) // bad_alloc
{
LOG(LCRITICAL, ("Insufficient memory for required offset map"));
}
m_file.Read(0, &m_elements[0], CheckedCast(m_fileSize));
sort(m_elements.begin(), m_elements.end(), ElementComparator());
LOG_SHORT(LINFO, ("Offsets reading is finished"));
}
void Add(TKey k, TValue const & v)
{
if (m_elements.size() > kFlushCount)
WriteAll();
m_elements.push_back(make_pair(k, v));
}
bool GetValueByKey(TKey key, TValue & value) const
{
auto it = lower_bound(m_elements.begin(), m_elements.end(), key, ElementComparator());
if ((it != m_elements.end()) && ((*it).first == key))
{
value = (*it).second;
return true;
}
return false;
}
template <class ToDo>
void ForEachByKey(TKey k, ToDo && toDo) const
{
auto range = equal_range(m_elements.begin(), m_elements.end(), k, ElementComparator());
for (; range.first != range.second; ++range.first)
{
if (toDo((*range.first).second))
return;
}
}
};
} // namespace detail
template <EMode TMode>
class OSMElementCache
{
public:
using TKey = uint64_t;
using TStorage = typename conditional<TMode == EMode::Write, FileWriter, FileReader>::type;
using TOffsetFile = typename conditional<TMode == EMode::Write, FileWriter, FileReader>::type;
protected:
using TBuffer = vector<uint8_t>;
TStorage m_storage;
detail::IndexFile<TOffsetFile, uint64_t> m_offsets;
string m_name;
TBuffer m_data;
bool m_preload = false;
public:
OSMElementCache(string const & name, bool preload = false)
: m_storage(name)
, m_offsets(name + OFFSET_EXT)
, m_name(name)
, m_preload(preload)
{
InitStorage<TMode>();
}
template <EMode T>
typename enable_if<T == EMode::Write, void>::type InitStorage() {}
template <EMode T>
typename enable_if<T == EMode::Read, void>::type InitStorage()
{
if (!m_preload)
return;
size_t sz = m_storage.Size();
m_data.resize(sz);
m_storage.Read(0, m_data.data(), sz);
}
template <class TValue, EMode T = TMode>
typename enable_if<T == EMode::Write, void>::type Write(TKey id, TValue const & value)
{
m_offsets.Add(id, m_storage.Pos());
m_data.clear();
MemWriter<TBuffer> w(m_data);
value.Write(w);
// write buffer
ASSERT_LESS(m_data.size(), numeric_limits<uint32_t>::max(), ());
uint32_t sz = static_cast<uint32_t>(m_data.size());
m_storage.Write(&sz, sizeof(sz));
m_storage.Write(m_data.data(), sz * sizeof(TBuffer::value_type));
}
template <class TValue, EMode T = TMode>
typename enable_if<T == EMode::Read, bool>::type Read(TKey id, TValue & value)
{
uint64_t pos = 0;
if (!m_offsets.GetValueByKey(id, pos))
{
LOG_SHORT(LWARNING, ("Can't find offset in file", m_offsets.GetFileName(), "by id", id));
return false;
}
uint32_t valueSize = m_preload ? *(reinterpret_cast<uint32_t *>(m_data.data() + pos)) : 0;
size_t offset = pos + sizeof(uint32_t);
if (!m_preload)
{
// in case not-in-memory work we read buffer
m_storage.Read(pos, &valueSize, sizeof(valueSize));
m_data.resize(valueSize);
m_storage.Read(pos + sizeof(valueSize), m_data.data(), valueSize);
offset = 0;
}
MemReader reader(m_data.data() + offset, valueSize);
value.Read(reader);
return true;
}
inline void SaveOffsets() { m_offsets.WriteAll(); }
inline void LoadOffsets() { m_offsets.ReadAll(); }
};
/// Used to store all world nodes inside temporary index file.
/// To find node by id, just calculate offset inside index file:
/// offset_in_file = sizeof(LatLon) * node_ID
class PointStorage
{
size_t m_processedPoint = 0;
public:
struct LatLon
{
int32_t lat;
int32_t lon;
};
static_assert(sizeof(LatLon) == 8, "Invalid structure size");
struct LatLonPos
{
uint64_t pos;
int32_t lat;
int32_t lon;
};
static_assert(sizeof(LatLonPos) == 16, "Invalid structure size");
inline size_t GetProcessedPoint() const { return m_processedPoint; }
inline void IncProcessedPoint() { ++m_processedPoint; }
};
template <EMode TMode>
class RawFilePointStorage : public PointStorage
{
#ifdef OMIM_OS_WINDOWS
using TFileReader = FileReader;
#else
using TFileReader = MmapReader;
#endif
typename conditional<TMode == EMode::Write, FileWriter, TFileReader>::type m_file;
constexpr static double const kValueOrder = 1E+7;
public:
RawFilePointStorage(string const & name) : m_file(name) {}
template <EMode T = TMode>
typename enable_if<T == EMode::Write, void>::type AddPoint(uint64_t id, double lat, double lng)
{
int64_t const lat64 = lat * kValueOrder;
int64_t const lng64 = lng * kValueOrder;
LatLon ll;
ll.lat = static_cast<int32_t>(lat64);
ll.lon = static_cast<int32_t>(lng64);
CHECK_EQUAL(static_cast<int64_t>(ll.lat), lat64, ("Latitude is out of 32bit boundary!"));
CHECK_EQUAL(static_cast<int64_t>(ll.lon), lng64, ("Longtitude is out of 32bit boundary!"));
m_file.Seek(id * sizeof(ll));
m_file.Write(&ll, sizeof(ll));
IncProcessedPoint();
}
template <EMode T = TMode>
typename enable_if<T == EMode::Read, bool>::type GetPoint(uint64_t id, double & lat,
double & lng) const
{
LatLon ll;
m_file.Read(id * sizeof(ll), &ll, sizeof(ll));
// assume that valid coordinate is not (0, 0)
if (ll.lat != 0.0 || ll.lon != 0.0)
{
lat = static_cast<double>(ll.lat) / kValueOrder;
lng = static_cast<double>(ll.lon) / kValueOrder;
return true;
}
LOG(LERROR, ("Node with id = ", id, " not found!"));
return false;
}
};
template <EMode TMode>
class RawMemPointStorage : public PointStorage
{
typename conditional<TMode == EMode::Write, FileWriter, FileReader>::type m_file;
constexpr static double const kValueOrder = 1E+7;
vector<LatLon> m_data;
public:
RawMemPointStorage(string const & name) : m_file(name), m_data((size_t)0xFFFFFFFF)
{
InitStorage<TMode>();
}
~RawMemPointStorage() { DoneStorage<TMode>(); }
template <EMode T>
typename enable_if<T == EMode::Write, void>::type InitStorage() {}
template <EMode T>
typename enable_if<T == EMode::Read, void>::type InitStorage()
{
m_file.Read(0, m_data.data(), m_data.size() * sizeof(LatLon));
}
template <EMode T>
typename enable_if<T == EMode::Write, void>::type DoneStorage()
{
m_file.Write(m_data.data(), m_data.size() * sizeof(LatLon));
}
template <EMode T>
typename enable_if<T == EMode::Read, void>::type DoneStorage() {}
template <EMode T = TMode>
typename enable_if<T == EMode::Write, void>::type AddPoint(uint64_t id, double lat, double lng)
{
int64_t const lat64 = lat * kValueOrder;
int64_t const lng64 = lng * kValueOrder;
LatLon & ll = m_data[id];
ll.lat = static_cast<int32_t>(lat64);
ll.lon = static_cast<int32_t>(lng64);
CHECK_EQUAL(static_cast<int64_t>(ll.lat), lat64, ("Latitude is out of 32bit boundary!"));
CHECK_EQUAL(static_cast<int64_t>(ll.lon), lng64, ("Longtitude is out of 32bit boundary!"));
IncProcessedPoint();
}
template <EMode T = TMode>
typename enable_if<T == EMode::Read, bool>::type GetPoint(uint64_t id, double & lat,
double & lng) const
{
LatLon const & ll = m_data[id];
// assume that valid coordinate is not (0, 0)
if (ll.lat != 0.0 || ll.lon != 0.0)
{
lat = static_cast<double>(ll.lat) / kValueOrder;
lng = static_cast<double>(ll.lon) / kValueOrder;
return true;
}
LOG(LERROR, ("Node with id = ", id, " not found!"));
return false;
}
};
template <EMode TMode>
class MapFilePointStorage : public PointStorage
{
typename conditional<TMode == EMode::Write, FileWriter, FileReader>::type m_file;
unordered_map<uint64_t, pair<int32_t, int32_t>> m_map;
constexpr static double const kValueOrder = 1E+7;
public:
MapFilePointStorage(string const & name) : m_file(name + ".short") { InitStorage<TMode>(); }
template <EMode T>
typename enable_if<T == EMode::Write, void>::type InitStorage() {}
template <EMode T>
typename enable_if<T == EMode::Read, void>::type InitStorage()
{
LOG(LINFO, ("Nodes reading is started"));
uint64_t const count = m_file.Size();
uint64_t pos = 0;
while (pos < count)
{
LatLonPos ll;
m_file.Read(pos, &ll, sizeof(ll));
m_map.emplace(make_pair(ll.pos, make_pair(ll.lat, ll.lon)));
pos += sizeof(ll);
}
LOG(LINFO, ("Nodes reading is finished"));
}
void AddPoint(uint64_t id, double lat, double lng)
{
int64_t const lat64 = lat * kValueOrder;
int64_t const lng64 = lng * kValueOrder;
LatLonPos ll;
ll.pos = id;
ll.lat = static_cast<int32_t>(lat64);
ll.lon = static_cast<int32_t>(lng64);
CHECK_EQUAL(static_cast<int64_t>(ll.lat), lat64, ("Latitude is out of 32bit boundary!"));
CHECK_EQUAL(static_cast<int64_t>(ll.lon), lng64, ("Longtitude is out of 32bit boundary!"));
m_file.Write(&ll, sizeof(ll));
IncProcessedPoint();
}
bool GetPoint(uint64_t id, double & lat, double & lng) const
{
auto i = m_map.find(id);
if (i == m_map.end())
return false;
lat = static_cast<double>(i->second.first) / kValueOrder;
lng = static_cast<double>(i->second.second) / kValueOrder;
return true;
}
};
} // namespace cache
|