Overpass API regression suite coverage (Saxony) / template_db/block_backend.h

template_db/block_backend.h

189 of 207 instrumented lines run by the suite (91.3%) at 8edf11b0419c (this file on GitHub).

runnever runcolumns: line, executions, queries (click for the queries that run the line)

1/** Copyright 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2018 Roland Olbricht et al.
2 *
3 * This file is part of Overpass_API.
4 *
5 * Overpass_API is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU Affero General Public License as
7 * published by the Free Software Foundation, either version 3 of the
8 * License, or (at your option) any later version.
9 *
10 * Overpass_API is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU Affero General Public License
16 * along with Overpass_API. If not, see <http://www.gnu.org/licenses/>.
17 */
19#ifndef DE__OSM3S___TEMPLATE_DB__BLOCK_BACKEND_H
20#define DE__OSM3S___TEMPLATE_DB__BLOCK_BACKEND_H
22#include "file_blocks.h"
23#include "ranges.h"
24#include "types.h"
26#include <cstring>
27#include <map>
28#include <set>
31template< typename Object >
32struct Idx_Handle
34 Idx_Handle()
354715744668 q : obj(0), ptr_to_raw(0) {}
36 ~Idx_Handle()
374715744668 q {
384715744668 q delete obj;
394715744668 q }
40 void set_ptr(uint8* ptr)
413282969649668 q {
423282969649668 q delete obj;
433282969649668 q obj = 0;
443282969649668 q ptr_to_raw = ptr;
453282969649668 q }
46 const Object& object() const
477194294494667 q {
487194294494667 q if (!obj)
492156741852667 q obj = new Object(ptr_to_raw);
507194294494667 q return *obj;
517194294494667 q }
52 uint8* get_ptr_to_raw() const
53825605707368 q {
54825605707368 q return ptr_to_raw;
55825605707368 q }
57 Idx_Handle(const Idx_Handle& rhs)
58 : obj(0), ptr_to_raw(rhs.ptr_to_raw) {}
59 Idx_Handle& operator=(const Idx_Handle& rhs)
60 {
61 set_ptr(rhs.ptr_to_raw);
62 }
64private:
65 mutable Object* obj;
66 uint8* ptr_to_raw;
67};
70template< typename Object >
71struct Handle : Idx_Handle< Object >
73 typename Object::Id_Type id() const
74825605707368 q {
75825605707368 q return Object::get_id(this->get_ptr_to_raw());
76825605707368 q }
77};
80//-----------------------------------------------------------------------------
83template< typename Index, typename Object, typename Idx_Assessor, typename File_Handle >
84struct Block_Backend_Basic_Iterator
86 Block_Backend_Basic_Iterator(
87 uint32 block_size, const File_Handle& file_handle, const Idx_Assessor& idx_assessor);
88 Block_Backend_Basic_Iterator(const Block_Backend_Basic_Iterator& rhs);
89 const Block_Backend_Basic_Iterator& operator=(const Block_Backend_Basic_Iterator& rhs);
91 Block_Backend_Basic_Iterator& operator++();
93 const Index& index()
942717357393667 q {
952717357393667 q return idx_cache.object();
962717357393667 q }
97 const Object& object()
984471834386667 q {
994471834386667 q return obj_cache.object();
1004471834386667 q }
101 const Handle< Object >& handle()
102908541416418 q {
103908541416418 q return obj_cache;
104908541416418 q }
106 bool is_end() const
10739891368731 q {
10839891368731 q return obj_offset == 0 || idx_block_offset == 0;
10939891368731 q }
111 bool operator==(const Block_Backend_Basic_Iterator& rhs) const
1122831490916668 q {
1132831490916668 q return obj_offset == rhs.obj_offset && file_handle == rhs.file_handle;
1142831490916668 q }
116private:
117 uint32 block_size;
118 Void64_Pointer< uint64 > buffer;
119 uint32 buffer_size;
120 uint32 idx_block_offset; // Points to the entry that contains the jump offset
121 uint32 obj_offset;
122 Idx_Handle< Index > idx_cache;
123 Handle< Object > obj_cache;
124 File_Handle file_handle;
125 Idx_Assessor idx_assessor;
127 uint32 next_idx_block_offset() const
1283444227564667 q {
1293444227564667 q return *(uint32*)(((uint8*)buffer.ptr) + idx_block_offset);
1303444227564667 q }
131 uint8* idx_ptr() const
132441812111668 q {
133441812111668 q return ((uint8*)buffer.ptr) + idx_block_offset + 4;
134441812111668 q }
135 uint32 total_payload_size() const
136165162936667 q {
137165162936667 q return *(uint32*)buffer.ptr;
138165162936667 q }
140 void increment_idx();
141 void increment_block();
142};
145template< typename Index, typename Object, typename Idx_Assessor, typename File_Handle >
146Block_Backend_Basic_Iterator< Index, Object, Idx_Assessor, File_Handle >::
147 Block_Backend_Basic_Iterator(
148 uint32 block_size_, const File_Handle& file_handle_, const Idx_Assessor& idx_assessor_)
1491098510668 q : block_size(block_size_), buffer(block_size_), buffer_size(block_size_),
1501098510668 q idx_block_offset(0), obj_offset(0), file_handle(file_handle_), idx_assessor(idx_assessor_)
1511098510668 q{
1521098510668 q increment_block();
1541098510668 q if (idx_block_offset == 0)
1551061851668 q obj_offset = 0;
15636659667 q else if (idx_assessor.is_relevant(idx_ptr()))
1576862666 q obj_offset = idx_block_offset + 4 + Index::size_of(idx_ptr());
15829797650 q else
15929797650 q obj_offset = next_idx_block_offset();
1611128307668 q while (obj_offset > 0 && obj_offset >= next_idx_block_offset())
16229797650 q increment_idx();
1641098510668 q idx_cache.set_ptr(idx_ptr());
1651098510668 q obj_cache.set_ptr(((uint8*)buffer.ptr) + obj_offset);
1661098510668 q}
169template< typename Index, typename Object, typename Idx_Assessor, typename File_Handle >
170Block_Backend_Basic_Iterator< Index, Object, Idx_Assessor, File_Handle >::
171 Block_Backend_Basic_Iterator(const Block_Backend_Basic_Iterator& rhs)
1721259362449 q : block_size(rhs.block_size), buffer(rhs.buffer_size), buffer_size(rhs.buffer_size),
1731259362449 q idx_block_offset(rhs.idx_block_offset), obj_offset(rhs.obj_offset),
1741259362449 q file_handle(rhs.file_handle), idx_assessor(rhs.idx_assessor)
1751259362449 q{
1761259362449 q memcpy(buffer.ptr, rhs.buffer.ptr, buffer_size);
1771259362449 q idx_cache.set_ptr(((uint8*)buffer.ptr) + idx_block_offset + 4);
1781259362449 q obj_cache.set_ptr(((uint8*)buffer.ptr) + obj_offset);
1791259362449 q}
182template< typename Index, typename Object, typename Idx_Assessor, typename File_Handle >
183const Block_Backend_Basic_Iterator< Index, Object, Idx_Assessor, File_Handle >&
184 Block_Backend_Basic_Iterator< Index, Object, Idx_Assessor, File_Handle >::
185 operator=(const Block_Backend_Basic_Iterator& rhs)
187 if (buffer_size != rhs.buffer_size)
188 {
189 Void64_Pointer< uint64 > new_buffer(rhs.buffer_size);
190 buffer.swap(new_buffer);
191 }
193 block_size = rhs.block_size;
194 buffer_size = rhs.buffer_size;
195 idx_block_offset = rhs.idx_block_offset;
196 obj_offset = rhs.obj_offset;
197 file_handle = rhs.file_handle;
198 idx_assessor = rhs.idx_assessor;
200 memcpy(buffer.ptr, rhs.buffer.ptr, buffer_size);
201 idx_cache.set_ptr(((uint8*)buffer.ptr) + idx_block_offset + 4);
202 obj_cache.set_ptr(((uint8*)buffer.ptr) + obj_offset);
204 return *this;
208template< typename Index, typename Object, typename Idx_Assessor, typename File_Handle >
209Block_Backend_Basic_Iterator< Index, Object, Idx_Assessor, File_Handle >&
210 Block_Backend_Basic_Iterator< Index, Object, Idx_Assessor, File_Handle >::operator++()
2113140464548667 q{
2123140464548667 q obj_offset += Object::size_of(((uint8*)buffer.ptr) + obj_offset);
2133278224108667 q while (obj_offset > 0 && obj_offset >= next_idx_block_offset())
214137759560667 q increment_idx();
2153140464548667 q obj_cache.set_ptr(((uint8*)buffer.ptr) + obj_offset);
2163140464548667 q return *this;
2173140464548667 q}
220template< typename Index, typename Object, typename Idx_Assessor, typename File_Handle >
221void Block_Backend_Basic_Iterator< Index, Object, Idx_Assessor, File_Handle >::increment_idx()
222137789357667 q{
223137789357667 q do
224165162936667 q {
225165162936667 q idx_block_offset = next_idx_block_offset();
226165162936667 q if (idx_block_offset >= total_payload_size())
227779178667 q increment_block();
228165162936667 q }
229165162936667 q while (idx_block_offset > 0 && !idx_assessor.is_relevant(idx_ptr()));
231137789357667 q if (idx_block_offset == 0)
23235785667 q obj_offset = 0;
233137753572667 q else
234137753572667 q obj_offset = idx_block_offset + 4 + Index::size_of(idx_ptr());
236137789357667 q idx_cache.set_ptr(idx_ptr());
237137789357667 q}
240template< typename Index, typename Object, typename Idx_Assessor, typename File_Handle >
241void Block_Backend_Basic_Iterator< Index, Object, Idx_Assessor, File_Handle >::increment_block()
2421877688668 q{
2431877688668 q if (!file_handle.next(buffer.ptr))
2441097636668 q {
2451097636668 q idx_block_offset = 0;
2461097636668 q return;
2471097636668 q }
248780052667 q idx_block_offset = 4;
250780052667 q if (next_idx_block_offset() > block_size)
2510 {
2520 uint32 new_buffer_size = (next_idx_block_offset()/block_size + 1) * block_size;
2530 if (buffer_size < new_buffer_size)
2540 {
2550 Void64_Pointer< uint64 > new_buffer(new_buffer_size);
2560 memcpy(new_buffer.ptr, buffer.ptr, block_size);
2570 buffer.swap(new_buffer);
2580 buffer_size = new_buffer_size;
2590 }
2600 for (uint i_offset = block_size; i_offset < new_buffer_size; i_offset += block_size)
2610 file_handle.next(buffer.ptr + i_offset/8, false);
2620 }
263780052667 q}
266//-----------------------------------------------------------------------------
269struct Flat_Idx_Assessor
271 bool is_relevant(uint8*)
27210257590664 q {
27310257590664 q return true;
27410257590664 q }
275};
278template< typename File_Blocks, typename File_Iterator >
279struct Flat_File_Handle
281 Flat_File_Handle(File_Blocks& file_blocks_, bool is_end)
282275408668 q : file_blocks(&file_blocks_), file_it(is_end ? file_blocks_.flat_end() : file_blocks_.flat_begin()),
283275408668 q file_end(file_blocks_.flat_end()) {}
285 bool next(uint64* ptr, bool check_idx = true)
286303014668 q {
287303014668 q if (file_it == file_end)
288275408668 q return false;
28927606664 q file_blocks->read_block(file_it, ptr, check_idx);
29027606664 q ++file_it;
29127606664 q return true;
292303014668 q }
294 bool operator==(const Flat_File_Handle& rhs) const
2951118664 q {
2961118664 q return file_it == rhs.file_it;
2971118664 q }
299private:
300 const File_Blocks* file_blocks;
301 File_Iterator file_it;
302 File_Iterator file_end;
303};
306template< typename Index, typename Object, typename Iterator >
307struct Block_Backend_Flat_Iterator
308 : Block_Backend_Basic_Iterator< Index, Object, Flat_Idx_Assessor,
309 Flat_File_Handle< File_Blocks< Index, Iterator >, typename File_Blocks< Index, Iterator >::Flat_Iterator > >
311 typedef File_Blocks< Index, Iterator > File_Blocks_;
312 typedef Flat_File_Handle< File_Blocks_, typename File_Blocks_::Flat_Iterator > File_Handle_;
314 Block_Backend_Flat_Iterator(File_Blocks_& file_blocks, uint32 block_size, bool is_end = false)
315275408668 q : Block_Backend_Basic_Iterator< Index, Object, Flat_Idx_Assessor, File_Handle_ >(
316275408668 q block_size, File_Handle_(file_blocks, is_end), Flat_Idx_Assessor()) {}
318 Block_Backend_Flat_Iterator(const Block_Backend_Flat_Iterator& rhs)
319828922 q : Block_Backend_Basic_Iterator< Index, Object, Flat_Idx_Assessor, File_Handle_ >(rhs) {}
320};
323//-----------------------------------------------------------------------------
326template< typename Index, typename Iterator >
327struct Discrete_Idx_Assessor
329 Discrete_Idx_Assessor(const Iterator& index_it_, const Iterator& index_end_)
330524302668 q : index_it(index_it_), index_end(index_end_) {}
332 bool is_relevant(uint8* ptr)
33311453701474 q {
33411453701474 q Index idx((void*)ptr);
33513054083474 q while (index_it != index_end && *index_it < idx)
3361600382461 q ++index_it;
33711453701474 q return index_it != index_end && *index_it == idx;
33811453701474 q }
340private:
341 Iterator index_it;
342 Iterator index_end;
343};
346template< typename File_Blocks, typename File_Iterator >
347struct Discrete_File_Handle
349 Discrete_File_Handle(File_Blocks& file_blocks_, const File_Iterator& file_it_)
350524302668 q : file_blocks(&file_blocks_), file_it(file_it_), file_end(file_blocks_.discrete_end()) {}
352 bool next(uint64* ptr, bool check_idx = true)
353660296668 q {
354660296668 q if (file_it == file_end)
355523778668 q return false;
356136518474 q file_blocks->read_block(file_it, ptr, check_idx);
357136518474 q ++file_it;
358136518474 q return true;
359660296668 q }
361 bool operator==(const Discrete_File_Handle& rhs) const
362256123514 q {
363256123514 q return file_it == rhs.file_it;
364256123514 q }
366private:
367 const File_Blocks* file_blocks;
368 File_Iterator file_it;
369 File_Iterator file_end;
370};
373template< typename Index, typename Object, typename Iterator >
374struct Block_Backend_Discrete_Iterator
375 : Block_Backend_Basic_Iterator< Index, Object, Discrete_Idx_Assessor< Index, Iterator >,
376 Discrete_File_Handle< File_Blocks< Index, Iterator >,
377 typename File_Blocks< Index, Iterator >::Discrete_Iterator > >
379 typedef File_Blocks< Index, Iterator > File_Blocks_;
380 typedef Discrete_File_Handle< File_Blocks_, typename File_Blocks_::Discrete_Iterator > File_Handle_;
382 Block_Backend_Discrete_Iterator
383 (File_Blocks_& file_blocks, const Iterator& index_it, const Iterator& index_end, uint32 block_size)
384249900514 q : Block_Backend_Basic_Iterator< Index, Object, Discrete_Idx_Assessor< Index, Iterator >, File_Handle_ >(
385249900514 q block_size, File_Handle_(file_blocks, file_blocks.discrete_begin(index_it, index_end)),
386249900514 q Discrete_Idx_Assessor< Index, Iterator >(index_it, index_end)) {}
388 Block_Backend_Discrete_Iterator(File_Blocks_& file_blocks, uint32 block_size)
389274402668 q : Block_Backend_Basic_Iterator< Index, Object, Discrete_Idx_Assessor< Index, Iterator >, File_Handle_ >(
390274402668 q block_size, File_Handle_(file_blocks, file_blocks.discrete_end()),
391274402668 q Discrete_Idx_Assessor< Index, Iterator >(Iterator(), Iterator())) {}
393 Block_Backend_Discrete_Iterator(const Block_Backend_Discrete_Iterator& it)
39433882126 q : Block_Backend_Basic_Iterator< Index, Object, Discrete_Idx_Assessor< Index, Iterator >, File_Handle_ >(it) {}
395};
398//-----------------------------------------------------------------------------
401template< typename Index, typename Iterator >
402struct Range_Idx_Assessor
404 Range_Idx_Assessor(const Iterator& index_it_, const Iterator& index_end_)
405298800668 q : index_it(index_it_), index_end(index_end_) {}
407 bool is_relevant(uint8* ptr)
408143452519641 q {
409143452519641 q Index idx((void*)ptr);
410144652183641 q while (index_it != index_end && !(idx < index_it.upper_bound()))
4111199664637 q ++index_it;
412143452519641 q return index_it != index_end && !(idx < index_it.lower_bound()) && idx < index_it.upper_bound();
413143452519641 q }
415private:
416 Iterator index_it;
417 Iterator index_end;
418};
421template< typename File_Blocks, typename Index >
422struct Range_File_Handle
424 Range_File_Handle(File_Blocks& file_blocks_,
425 const File_Blocks_Range_Iterator< Index, typename Ranges< Index >::Iterator >& file_it_)
426298800668 q : file_blocks(&file_blocks_), file_it(file_it_),
427298800668 q file_end(file_blocks_.template range_end< typename Ranges< Index >::Iterator >()) {}
429 bool next(uint64* ptr, bool check_idx = true)
430914378668 q {
431914378668 q if (file_it == file_end)
432298450668 q return false;
433615928641 q file_blocks->read_block(file_it, ptr, check_idx);
434615928641 q ++file_it;
435615928641 q return true;
436914378668 q }
438 bool operator==(const Range_File_Handle& rhs) const
439195998641 q {
440195998641 q return file_it == rhs.file_it;
441195998641 q }
443private:
444 const File_Blocks* file_blocks;
445 File_Blocks_Range_Iterator< Index, typename Ranges< Index >::Iterator > file_it;
446 File_Blocks_Range_Iterator< Index, typename Ranges< Index >::Iterator > file_end;
447};
450template< typename Index, typename Object, typename Iterator >
451struct Block_Backend_Range_Iterator
452 : Block_Backend_Basic_Iterator< Index, Object, Range_Idx_Assessor< Index, typename Ranges< Index >::Iterator >,
453 Range_File_Handle< File_Blocks< Index, Iterator >, Index > >
455 typedef File_Blocks< Index, Iterator > File_Blocks_;
456 typedef Range_File_Handle< File_Blocks_, Index > File_Handle_;
458 Block_Backend_Range_Iterator(
459 File_Blocks_& file_blocks,
460 const typename Ranges< Index >::Iterator& index_it, const typename Ranges< Index >::Iterator& index_end,
461 uint32 block_size)
46224398646 q : Block_Backend_Basic_Iterator< Index, Object,
46324398646 q Range_Idx_Assessor< Index, typename Ranges< Index >::Iterator >, File_Handle_ >(
46424398646 q block_size, File_Handle_(file_blocks, file_blocks.range_begin(index_it, index_end)),
46524398646 q Range_Idx_Assessor< Index, typename Ranges< Index >::Iterator >(index_it, index_end)) {}
467 Block_Backend_Range_Iterator(File_Blocks_& file_blocks, uint32 block_size)
468274402668 q : Block_Backend_Basic_Iterator< Index, Object,
469274402668 q Range_Idx_Assessor< Index, typename Ranges< Index >::Iterator >, File_Handle_ >(
470274402668 q block_size, File_Handle_(file_blocks, file_blocks.template range_end< typename Ranges< Index >::Iterator >()),
471274402668 q Range_Idx_Assessor< Index, typename Ranges< Index >::Iterator >(
472274402668 q typename Ranges< Index >::Iterator(), typename Ranges< Index >::Iterator())) {}
474 Block_Backend_Range_Iterator(const Block_Backend_Range_Iterator& it)
4751142588414 q : Block_Backend_Basic_Iterator< Index, Object,
4761142588414 q Range_Idx_Assessor< Index, typename Ranges< Index >::Iterator >, File_Handle_ >(it) {}
477};
480//-----------------------------------------------------------------------------
483struct Delta_Count
4850 Delta_Count() : before(0), after(0) {}
487 Delta_Count& operator+=(Delta_Count rhs)
4880 {
4890 before += rhs.before;
4900 after += rhs.after;
4910 return *this;
4920 }
494 uint64 before;
495 uint64 after;
496};
499template< class TIndex, class TObject, class TIterator = typename std::vector< TIndex >::const_iterator >
500struct Block_Backend
502 typedef Block_Backend_Flat_Iterator< TIndex, TObject, TIterator > Flat_Iterator;
503 typedef Block_Backend_Discrete_Iterator< TIndex, TObject, TIterator > Discrete_Iterator;
504 typedef Block_Backend_Range_Iterator< TIndex, TObject, TIterator > Range_Iterator;
506 typedef File_Blocks< TIndex, TIterator > File_Blocks_;
508 Block_Backend(File_Blocks_Index_Base* index_);
509 ~Block_Backend();
5111006664 q Flat_Iterator flat_begin() { return Flat_Iterator(file_blocks, block_size, false); }
512131759859664 q const Flat_Iterator& flat_end() const { return *flat_end_it; }
514 Discrete_Iterator discrete_begin(TIterator begin, TIterator end)
515249900514 q { return Discrete_Iterator(file_blocks, begin, end, block_size); }
516255115156514 q const Discrete_Iterator& discrete_end() const { return *discrete_end_it; }
518 Range_Iterator range_begin(const Ranges< TIndex >& arg)
51924398646 q { return Range_Iterator(file_blocks, arg.begin(), arg.end(), block_size); }
5211994646333642 q const Range_Iterator& range_end() const { return *range_end_it; }
523 template< typename Container >
524 void update(
525 const std::map< TIndex, std::set< TObject > >& to_delete,
526 const std::map< TIndex, Container >& to_insert,
527 std::map< TIndex, Delta_Count >* obj_count = nullptr);
529 uint read_count() const { return file_blocks.read_count(); }
530 void reset_read_count() const { file_blocks.reset_read_count(); }
532private:
533 File_Blocks_ file_blocks;
534 Flat_Iterator* flat_end_it;
535 Discrete_Iterator* discrete_end_it;
536 Range_Iterator* range_end_it;
537 uint32 block_size;
538 std::string data_filename;
539};
542template< class TIndex, class TObject, class TIterator >
543Block_Backend< TIndex, TObject, TIterator >::Block_Backend(File_Blocks_Index_Base* index_)
544274402668 q : file_blocks(index_),
545274402668 q block_size(index_->get_block_size() * index_->get_compression_factor()),
546274402668 q data_filename(index_->get_data_file_name())
547274402668 q{
548274402668 q flat_end_it = new Flat_Iterator(file_blocks, block_size, true);
549274402668 q discrete_end_it = new Discrete_Iterator(file_blocks, block_size);
550274402668 q range_end_it = new Range_Iterator(file_blocks, block_size);
551274402668 q}
554template< class TIndex, class TObject, class TIterator >
555Block_Backend< TIndex, TObject, TIterator >::~Block_Backend()
556274402668 q{
557274402668 q delete flat_end_it;
558274402668 q delete discrete_end_it;
559274402668 q delete range_end_it;
560274402668 q}
563#endif