Overpass API regression suite coverage (Nova Scotia) / 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()
3510305978590 q : obj(0), ptr_to_raw(0) {}
36 ~Idx_Handle()
3710305978590 q {
3810305978590 q delete obj;
3910305978590 q }
40 void set_ptr(uint8* ptr)
411568958019590 q {
421568958019590 q delete obj;
431568958019590 q obj = 0;
441568958019590 q ptr_to_raw = ptr;
451568958019590 q }
46 const Object& object() const
473509587566588 q {
483509587566588 q if (!obj)
491021412139588 q obj = new Object(ptr_to_raw);
503509587566588 q return *obj;
513509587566588 q }
52 uint8* get_ptr_to_raw() const
53441853508337 q {
54441853508337 q return ptr_to_raw;
55441853508337 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
74441853508337 q {
75441853508337 q return Object::get_id(this->get_ptr_to_raw());
76441853508337 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()
941403116820588 q {
951403116820588 q return idx_cache.object();
961403116820588 q }
97 const Object& object()
982104687203588 q {
992104687203588 q return obj_cache.object();
1002104687203588 q }
101 const Handle< Object >& handle()
102499051010377 q {
103499051010377 q return obj_cache;
104499051010377 q }
106 bool is_end() const
1077044030526 q {
1087044030526 q return obj_offset == 0 || idx_block_offset == 0;
1097044030526 q }
111 bool operator==(const Block_Backend_Basic_Iterator& rhs) const
1121482335372590 q {
1131482335372590 q return obj_offset == rhs.obj_offset && file_handle == rhs.file_handle;
1141482335372590 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
1281650619799590 q {
1291650619799590 q return *(uint32*)(((uint8*)buffer.ptr) + idx_block_offset);
1301650619799590 q }
131 uint8* idx_ptr() const
132194764186590 q {
133194764186590 q return ((uint8*)buffer.ptr) + idx_block_offset + 4;
134194764186590 q }
135 uint32 total_payload_size() const
13691488811590 q {
13791488811590 q return *(uint32*)buffer.ptr;
13891488811590 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_)
149216277590 q : block_size(block_size_), buffer(block_size_), buffer_size(block_size_),
150216277590 q idx_block_offset(0), obj_offset(0), file_handle(file_handle_), idx_assessor(idx_assessor_)
151216277590 q{
152216277590 q increment_block();
154216277590 q if (idx_block_offset == 0)
155196973590 q obj_offset = 0;
15619304590 q else if (idx_assessor.is_relevant(idx_ptr()))
1573752587 q obj_offset = idx_block_offset + 4 + Index::size_of(idx_ptr());
15815552574 q else
15915552574 q obj_offset = next_idx_block_offset();
161231829590 q while (obj_offset > 0 && obj_offset >= next_idx_block_offset())
16215552574 q increment_idx();
164216277590 q idx_cache.set_ptr(idx_ptr());
165216277590 q obj_cache.set_ptr(((uint8*)buffer.ptr) + obj_offset);
166216277590 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)
1724936712389 q : block_size(rhs.block_size), buffer(rhs.buffer_size), buffer_size(rhs.buffer_size),
1734936712389 q idx_block_offset(rhs.idx_block_offset), obj_offset(rhs.obj_offset),
1744936712389 q file_handle(rhs.file_handle), idx_assessor(rhs.idx_assessor)
1754936712389 q{
1764936712389 q memcpy(buffer.ptr, rhs.buffer.ptr, buffer_size);
1774936712389 q idx_cache.set_ptr(((uint8*)buffer.ptr) + idx_block_offset + 4);
1784936712389 q obj_cache.set_ptr(((uint8*)buffer.ptr) + obj_offset);
1794936712389 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++()
2111507115586588 q{
2121507115586588 q obj_offset += Object::size_of(((uint8*)buffer.ptr) + obj_offset);
2131558636489588 q while (obj_offset > 0 && obj_offset >= next_idx_block_offset())
21451520903588 q increment_idx();
2151507115586588 q obj_cache.set_ptr(((uint8*)buffer.ptr) + obj_offset);
2161507115586588 q return *this;
2171507115586588 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()
22251536455590 q{
22351536455590 q do
22491488811590 q {
22591488811590 q idx_block_offset = next_idx_block_offset();
22691488811590 q if (idx_block_offset >= total_payload_size())
227461655590 q increment_block();
22891488811590 q }
22991488811590 q while (idx_block_offset > 0 && !idx_assessor.is_relevant(idx_ptr()));
23151536455590 q if (idx_block_offset == 0)
23218434590 q obj_offset = 0;
23351518021588 q else
23451518021588 q obj_offset = idx_block_offset + 4 + Index::size_of(idx_ptr());
23651536455590 q idx_cache.set_ptr(idx_ptr());
23751536455590 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()
242677932590 q{
243677932590 q if (!file_handle.next(buffer.ptr))
244215407590 q {
245215407590 q idx_block_offset = 0;
246215407590 q return;
247215407590 q }
248462525590 q idx_block_offset = 4;
250462525590 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 }
263462525590 q}
266//-----------------------------------------------------------------------------
269struct Flat_Idx_Assessor
271 bool is_relevant(uint8*)
2724804706585 q {
2734804706585 q return true;
2744804706585 q }
275};
278template< typename File_Blocks, typename File_Iterator >
279struct Flat_File_Handle
281 Flat_File_Handle(File_Blocks& file_blocks_, bool is_end)
28254807590 q : file_blocks(&file_blocks_), file_it(is_end ? file_blocks_.flat_end() : file_blocks_.flat_begin()),
28354807590 q file_end(file_blocks_.flat_end()) {}
285 bool next(uint64* ptr, bool check_idx = true)
28670900590 q {
28770900590 q if (file_it == file_end)
28854807590 q return false;
28916093585 q file_blocks->read_block(file_it, ptr, check_idx);
29016093585 q ++file_it;
29116093585 q return true;
29270900590 q }
294 bool operator==(const Flat_File_Handle& rhs) const
2953499585 q {
2963499585 q return file_it == rhs.file_it;
2973499585 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)
31554807590 q : Block_Backend_Basic_Iterator< Index, Object, Flat_Idx_Assessor, File_Handle_ >(
31654807590 q block_size, File_Handle_(file_blocks, is_end), Flat_Idx_Assessor()) {}
318 Block_Backend_Flat_Iterator(const Block_Backend_Flat_Iterator& rhs)
31933033412 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_)
33088842590 q : index_it(index_it_), index_end(index_end_) {}
332 bool is_relevant(uint8* ptr)
33316125329410 q {
33416125329410 q Index idx((void*)ptr);
33520811733410 q while (index_it != index_end && *index_it < idx)
3364686404404 q ++index_it;
33716125329410 q return index_it != index_end && *index_it == idx;
33816125329410 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_)
35088842590 q : file_blocks(&file_blocks_), file_it(file_it_), file_end(file_blocks_.discrete_end()) {}
352 bool next(uint64* ptr, bool check_idx = true)
353176073590 q {
354176073590 q if (file_it == file_end)
35588331590 q return false;
35687742410 q file_blocks->read_block(file_it, ptr, check_idx);
35787742410 q ++file_it;
35887742410 q return true;
359176073590 q }
361 bool operator==(const Discrete_File_Handle& rhs) const
36240630441 q {
36340630441 q return file_it == rhs.file_it;
36440630441 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)
38434988442 q : Block_Backend_Basic_Iterator< Index, Object, Discrete_Idx_Assessor< Index, Iterator >, File_Handle_ >(
38534988442 q block_size, File_Handle_(file_blocks, file_blocks.discrete_begin(index_it, index_end)),
38634988442 q Discrete_Idx_Assessor< Index, Iterator >(index_it, index_end)) {}
388 Block_Backend_Discrete_Iterator(File_Blocks_& file_blocks, uint32 block_size)
38953854590 q : Block_Backend_Basic_Iterator< Index, Object, Discrete_Idx_Assessor< Index, Iterator >, File_Handle_ >(
39053854590 q block_size, File_Handle_(file_blocks, file_blocks.discrete_end()),
39153854590 q Discrete_Idx_Assessor< Index, Iterator >(Iterator(), Iterator())) {}
393 Block_Backend_Discrete_Iterator(const Block_Backend_Discrete_Iterator& it)
39439222110 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_)
40572628590 q : index_it(index_it_), index_end(index_end_) {}
407 bool is_relevant(uint8* ptr)
40870559646569 q {
40970559646569 q Index idx((void*)ptr);
41072474393569 q while (index_it != index_end && !(idx < index_it.upper_bound()))
4111914747568 q ++index_it;
41270559646569 q return index_it != index_end && !(idx < index_it.lower_bound()) && idx < index_it.upper_bound();
41370559646569 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_)
42672628590 q : file_blocks(&file_blocks_), file_it(file_it_),
42772628590 q file_end(file_blocks_.template range_end< typename Ranges< Index >::Iterator >()) {}
429 bool next(uint64* ptr, bool check_idx = true)
430430959590 q {
431430959590 q if (file_it == file_end)
43272269590 q return false;
433358690569 q file_blocks->read_block(file_it, ptr, check_idx);
434358690569 q ++file_it;
435358690569 q return true;
436430959590 q }
438 bool operator==(const Range_File_Handle& rhs) const
439108021567 q {
440108021567 q return file_it == rhs.file_it;
441108021567 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)
46218774574 q : Block_Backend_Basic_Iterator< Index, Object,
46318774574 q Range_Idx_Assessor< Index, typename Ranges< Index >::Iterator >, File_Handle_ >(
46418774574 q block_size, File_Handle_(file_blocks, file_blocks.range_begin(index_it, index_end)),
46518774574 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)
46853854590 q : Block_Backend_Basic_Iterator< Index, Object,
46953854590 q Range_Idx_Assessor< Index, typename Ranges< Index >::Iterator >, File_Handle_ >(
47053854590 q block_size, File_Handle_(file_blocks, file_blocks.template range_end< typename Ranges< Index >::Iterator >()),
47153854590 q Range_Idx_Assessor< Index, typename Ranges< Index >::Iterator >(
47253854590 q typename Ranges< Index >::Iterator(), typename Ranges< Index >::Iterator())) {}
474 Block_Backend_Range_Iterator(const Block_Backend_Range_Iterator& it)
4754567156364 q : Block_Backend_Basic_Iterator< Index, Object,
4764567156364 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();
511953585 q Flat_Iterator flat_begin() { return Flat_Iterator(file_blocks, block_size, false); }
51270513679585 q const Flat_Iterator& flat_end() const { return *flat_end_it; }
514 Discrete_Iterator discrete_begin(TIterator begin, TIterator end)
51534988442 q { return Discrete_Iterator(file_blocks, begin, end, block_size); }
516177026579442 q const Discrete_Iterator& discrete_end() const { return *discrete_end_it; }
518 Range_Iterator range_begin(const Ranges< TIndex >& arg)
51918774574 q { return Range_Iterator(file_blocks, arg.begin(), arg.end(), block_size); }
521965976621569 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_)
54453854590 q : file_blocks(index_),
54553854590 q block_size(index_->get_block_size() * index_->get_compression_factor()),
54653854590 q data_filename(index_->get_data_file_name())
54753854590 q{
54853854590 q flat_end_it = new Flat_Iterator(file_blocks, block_size, true);
54953854590 q discrete_end_it = new Discrete_Iterator(file_blocks, block_size);
55053854590 q range_end_it = new Range_Iterator(file_blocks, block_size);
55153854590 q}
554template< class TIndex, class TObject, class TIterator >
555Block_Backend< TIndex, TObject, TIterator >::~Block_Backend()
55653854590 q{
55753854590 q delete flat_end_it;
55853854590 q delete discrete_end_it;
55953854590 q delete range_end_it;
56053854590 q}
563#endif