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
|
#include "osrm2feature_map.hpp"
#include "../defines.hpp"
#include "../coding/varint.hpp"
#include "../coding/internal/file_data.hpp"
#include "../coding/file_name_utils.hpp"
#include "../base/assert.hpp"
#include "../base/logging.hpp"
#include "../base/math.hpp"
#include "../base/scope_guard.hpp"
#include "../std/fstream.hpp"
#include "../std/sstream.hpp"
#include "../std/unordered_map.hpp"
#include "../3party/succinct/mapper.hpp"
namespace routing
{
OsrmNodeIdT const INVALID_NODE_ID = -1;
OsrmMappingTypes::FtSeg::FtSeg(uint32_t fid, uint32_t ps, uint32_t pe)
: m_fid(fid),
m_pointStart(static_cast<uint16_t>(ps)),
m_pointEnd(static_cast<uint16_t>(pe))
{
CHECK_NOT_EQUAL(ps, pe, ());
CHECK_EQUAL(m_pointStart, ps, ());
CHECK_EQUAL(m_pointEnd, pe, ());
}
OsrmMappingTypes::FtSeg::FtSeg(uint64_t x)
: m_fid(x & 0xFFFFFFFF), m_pointStart(x >> 48), m_pointEnd((x >> 32) & 0xFFFF)
{
}
uint64_t OsrmMappingTypes::FtSeg::Store() const
{
return (uint64_t(m_pointStart) << 48) + (uint64_t(m_pointEnd) << 32) + uint64_t(m_fid);
}
bool OsrmMappingTypes::FtSeg::Merge(FtSeg const & other)
{
if (other.m_fid != m_fid)
return false;
bool const dir = other.m_pointEnd > other.m_pointStart;
if (dir != (m_pointEnd > m_pointStart))
return false;
auto const s1 = min(m_pointStart, m_pointEnd);
auto const e1 = max(m_pointStart, m_pointEnd);
auto const s2 = min(other.m_pointStart, other.m_pointEnd);
auto const e2 = max(other.m_pointStart, other.m_pointEnd);
if (my::IsIntersect(s1, e1, s2, e2))
{
m_pointStart = min(s1, s2);
m_pointEnd = max(e1, e2);
if (!dir)
swap(m_pointStart, m_pointEnd);
return true;
}
else
return false;
}
bool OsrmMappingTypes::FtSeg::IsIntersect(FtSeg const & other) const
{
if (other.m_fid != m_fid)
return false;
auto const s1 = min(m_pointStart, m_pointEnd);
auto const e1 = max(m_pointStart, m_pointEnd);
auto const s2 = min(other.m_pointStart, other.m_pointEnd);
auto const e2 = max(other.m_pointStart, other.m_pointEnd);
return my::IsIntersect(s1, e1, s2, e2);
}
string OsrmMappingTypes::DebugPrint(OsrmMappingTypes::FtSeg const & seg)
{
stringstream ss;
ss << "{ fID = " << seg.m_fid <<
", pStart = " << seg.m_pointStart <<
", pEnd = " << seg.m_pointEnd << " }";
return ss.str();
}
string OsrmMappingTypes::DebugPrint(OsrmMappingTypes::SegOffset const & off)
{
stringstream ss;
ss << "{ " << off.m_nodeId << ", " << off.m_offset << " }";
return ss.str();
}
void OsrmFtSegMapping::Clear()
{
m_offsets.clear();
m_handle.Unmap();
}
void OsrmFtSegMapping::Load(FilesMappingContainer & cont)
{
Clear();
{
ReaderSource<FileReader> src(cont.GetReader(ROUTING_NODEIND_TO_FTSEGIND_FILE_TAG));
uint32_t const count = ReadVarUint<uint32_t>(src);
m_offsets.resize(count);
for (uint32_t i = 0; i < count; ++i)
{
m_offsets[i].m_nodeId = ReadVarUint<OsrmNodeIdT>(src);
m_offsets[i].m_offset = ReadVarUint<uint32_t>(src);
}
}
Map(cont);
m_backwardIndex.Construct(*this, m_offsets.back().m_nodeId, cont);
}
void OsrmFtSegMapping::Map(FilesMappingContainer & cont)
{
m_handle.Assign(cont.Map(ROUTING_FTSEG_FILE_TAG));
ASSERT(m_handle.IsValid(), ());
succinct::mapper::map(m_segments, m_handle.GetData<char>());
}
void OsrmFtSegMapping::Unmap()
{
m_handle.Unmap();
}
bool OsrmFtSegMapping::IsMapped() const
{
return m_handle.IsValid();
}
void OsrmFtSegMapping::DumpSegmentsByFID(uint32_t fID) const
{
#ifdef DEBUG
for (size_t i = 0; i < m_segments.size(); ++i)
{
OsrmMappingTypes::FtSeg s(m_segments[i]);
if (s.m_fid == fID)
LOG(LDEBUG, (s));
}
#endif
}
void OsrmFtSegMapping::DumpSegmentByNode(OsrmNodeIdT nodeId) const
{
#ifdef DEBUG
ForEachFtSeg(nodeId, [] (OsrmMappingTypes::FtSeg const & s)
{
LOG(LDEBUG, (s));
});
#endif
}
void OsrmFtSegMapping::GetOsrmNodes(FtSegSetT & segments, OsrmNodesT & res, volatile bool const & requestCancel) const
{
auto addResFn = [&] (uint64_t seg, size_t idx, bool forward)
{
OsrmNodeIdT const nodeId = GetNodeId(idx);
auto it = res.insert({ seg, { forward ? nodeId : INVALID_NODE_ID,
forward ? INVALID_NODE_ID : nodeId } });
if (it.second)
return false;
else
{
if (forward)
{
ASSERT_EQUAL(it.first->second.first, INVALID_NODE_ID, ());
it.first->second.first = nodeId;
}
else
{
ASSERT_EQUAL(it.first->second.second, INVALID_NODE_ID, ());
it.first->second.second = nodeId;
}
}
return true;
};
for (auto it = segments.begin(); it != segments.end(); ++it)
{
OsrmMappingTypes::FtSeg const & seg = *(*it);
uint32_t nodeId = m_backwardIndex.GetNodeIdByFid(seg.m_fid);
auto range = GetSegmentsRange(nodeId);
for (int i = range.first; i != range.second; ++i)
{
OsrmMappingTypes::FtSeg const s(m_segments[i]);
if (s.m_fid != seg.m_fid)
continue;
if (s.m_pointStart <= s.m_pointEnd)
{
if (seg.m_pointStart >= s.m_pointStart && seg.m_pointEnd <= s.m_pointEnd)
{
if (addResFn(seg.Store(), i, true))
{
break;
}
}
}
else
{
if (seg.m_pointStart >= s.m_pointEnd && seg.m_pointEnd <= s.m_pointStart)
{
if (addResFn(seg.Store(), i, false))
{
break;
}
}
}
}
}
}
void OsrmFtSegMapping::GetSegmentByIndex(size_t idx, OsrmMappingTypes::FtSeg & seg) const
{
ASSERT_LESS(idx, m_segments.size(), ());
OsrmMappingTypes::FtSeg(m_segments[idx]).Swap(seg);
}
pair<size_t, size_t> OsrmFtSegMapping::GetSegmentsRange(OsrmNodeIdT nodeId) const
{
SegOffsetsT::const_iterator it = lower_bound(m_offsets.begin(), m_offsets.end(), OsrmMappingTypes::SegOffset(nodeId, 0),
[] (OsrmMappingTypes::SegOffset const & o, OsrmMappingTypes::SegOffset const & val)
{
return (o.m_nodeId < val.m_nodeId);
});
size_t const index = distance(m_offsets.begin(), it);
size_t const start = (index > 0 ? m_offsets[index - 1].m_offset + nodeId : nodeId);
if (index < m_offsets.size() && m_offsets[index].m_nodeId == nodeId)
return make_pair(start, m_offsets[index].m_offset + nodeId + 1);
else
return make_pair(start, start + 1);
}
OsrmNodeIdT OsrmFtSegMapping::GetNodeId(size_t segInd) const
{
SegOffsetsT::const_iterator it = lower_bound(m_offsets.begin(), m_offsets.end(), OsrmMappingTypes::SegOffset(segInd, 0),
[] (OsrmMappingTypes::SegOffset const & o, OsrmMappingTypes::SegOffset const & val)
{
return (o.m_nodeId + o.m_offset < val.m_nodeId);
});
size_t const index = distance(m_offsets.begin(), it);
uint32_t const prevOffset = index > 0 ? m_offsets[index-1].m_offset : 0;
if ((index < m_offsets.size()) &&
(segInd >= prevOffset + m_offsets[index].m_nodeId) &&
(segInd <= m_offsets[index].m_offset + m_offsets[index].m_nodeId))
{
return m_offsets[index].m_nodeId;
}
return (segInd - prevOffset);
}
OsrmFtSegMappingBuilder::OsrmFtSegMappingBuilder()
: m_lastOffset(0)
{
}
void OsrmFtSegMappingBuilder::Append(OsrmNodeIdT nodeId, FtSegVectorT const & data)
{
size_t const count = data.size();
if (count == 0)
m_buffer.emplace_back(OsrmMappingTypes::FtSeg(OsrmMappingTypes::FtSeg::INVALID_FID, 0, 1).Store());
else
{
for (size_t i = 0; i < count; ++i)
m_buffer.emplace_back(data[i].Store());
}
if (count > 1)
{
m_lastOffset += (data.size() - 1);
uint32_t const off = static_cast<uint32_t>(m_lastOffset);
CHECK_EQUAL(m_lastOffset, off, ());
m_offsets.emplace_back(OsrmMappingTypes::SegOffset(nodeId, off));
}
}
void OsrmFtSegMappingBuilder::Save(FilesContainerW & cont) const
{
{
FileWriter writer = cont.GetWriter(ROUTING_NODEIND_TO_FTSEGIND_FILE_TAG);
uint32_t const count = static_cast<uint32_t>(m_offsets.size());
WriteVarUint(writer, count);
for (uint32_t i = 0; i < count; ++i)
{
WriteVarUint(writer, m_offsets[i].m_nodeId);
WriteVarUint(writer, m_offsets[i].m_offset);
}
// Write padding to make next elias_fano start address multiple of 4.
writer.WritePaddingByEnd(4);
}
string const fName = cont.GetFileName() + "." ROUTING_FTSEG_FILE_TAG;
MY_SCOPE_GUARD(deleteFileGuard, bind(&FileWriter::DeleteFileX, cref(fName)));
succinct::elias_fano_compressed_list compressed(m_buffer);
succinct::mapper::freeze(compressed, fName.c_str());
cont.Write(fName, ROUTING_FTSEG_FILE_TAG);
}
void OsrmFtSegBackwardIndex::Save(string const & nodesFileName, string const & bitsFileName)
{
{
string const nodesFileNameTmp = nodesFileName + EXTENSION_TMP;
succinct::mapper::freeze(m_nodeIds, nodesFileNameTmp.c_str());
my::RenameFileX(nodesFileNameTmp, nodesFileName);
}
{
string const bitsFileNameTmp = bitsFileName + EXTENSION_TMP;
succinct::mapper::freeze(m_rankIndex, bitsFileNameTmp.c_str());
my::RenameFileX(bitsFileNameTmp, bitsFileName);
}
}
bool OsrmFtSegBackwardIndex::Load(string const & nodesFileName, string const & bitsFileName)
{
uint64_t size;
if (!GetPlatform().GetFileSizeByFullPath(nodesFileName, size) || !GetPlatform().GetFileSizeByFullPath(bitsFileName, size))
return false;
m_pMappedNodes.reset(new MmapReader(nodesFileName));
m_pMappedBits.reset(new MmapReader(bitsFileName));
succinct::mapper::map(m_nodeIds, reinterpret_cast<char const *>(m_pMappedNodes->Data()));
succinct::mapper::map(m_rankIndex, reinterpret_cast<char const *>(m_pMappedBits->Data()));
return true;
}
void OsrmFtSegBackwardIndex::Construct(const OsrmFtSegMapping & mapping, const uint32_t maxNodeId, FilesMappingContainer & routingFile)
{
Clear();
// Calculate data file pathes
Platform const & p = GetPlatform();
string const routingName = routingFile.GetName();
string const name(routingName, routingName.rfind(my::GetNativeSeparator()) + 1,
routingName.find(DATA_FILE_EXTENSION) - routingName.rfind(my::GetNativeSeparator()) - 1);
string const offsetsIndexName = p.GetIndexFileName(name, FEATURES_OFFSETS_TABLE_FILE_EXT);
string const bitsFileName = p.GetIndexFileName(name, FTSEG_MAPPING_BACKWARD_INDEX_BITS_EXT);
string const nodesFileName = p.GetIndexFileName(name, FTSEG_MAPPING_BACKWARD_INDEX_NODES_EXT);
m_table = feature::FeaturesOffsetsTable::CreateIfNotExistsAndLoad(offsetsIndexName, FilesContainerR(name + DATA_FILE_EXTENSION));
if (Load(bitsFileName, nodesFileName))
return;
// Generate temporary index to speedup processing
unordered_map<uint64_t, uint32_t> temporaryBackwardIndex;
for (uint32_t i = 0; i < maxNodeId; ++i)
{
auto indexes = mapping.GetSegmentsRange(i);
for (size_t j = indexes.first; j != indexes.second; ++j)
{
OsrmMappingTypes::FtSeg seg;
mapping.GetSegmentByIndex(j, seg);
temporaryBackwardIndex.insert(make_pair(seg.m_fid, i));
}
}
// Create final index
vector<bool> inIndex(m_table->size(), false);
vector<uint32_t> nodeIds;
for (size_t i = 0; i < m_table->size(); ++i)
{
uint64_t fid = m_table->GetFeatureOffset(i);
auto it = temporaryBackwardIndex.find(fid);
if (it != temporaryBackwardIndex.end())
{
inIndex[i] = true;
nodeIds.push_back(it->second);
}
}
// Pack and save index
succinct::elias_fano_compressed_list(nodeIds).swap(m_nodeIds);
succinct::rs_bit_vector(inIndex).swap(m_rankIndex);
LOG(LINFO, ("Writing section to data file", routingName));
Save(bitsFileName, nodesFileName);
}
uint32_t OsrmFtSegBackwardIndex::GetNodeIdByFid(const uint32_t fid) const
{
if (!m_table)
return INVALID_NODE_ID;
size_t const index = m_table->GetFeatureIndexbyOffset(fid);
if (index == m_table->size())
return INVALID_NODE_ID;
size_t node_index = m_rankIndex.rank(index);
ASSERT_LESS(node_index, m_nodeIds.size(), ());
return m_nodeIds[node_index];
}
void OsrmFtSegBackwardIndex::Clear()
{
ClearContainer(m_nodeIds);
ClearContainer(m_rankIndex);
m_table.reset();
m_pMappedBits.reset();
m_pMappedNodes.reset();
}
}
|