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 modify6 * it under the terms of the GNU Affero General Public License as7 * published by the Free Software Foundation, either version 3 of the8 * 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 of12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the13 * GNU General Public License for more details.14 *15 * You should have received a copy of the GNU Affero General Public License16 * along with Overpass_API. If not, see <http://www.gnu.org/licenses/>.17 */19#ifndef DE__OSM3S___TEMPLATE_DB__BLOCK_BACKEND_H20#define DE__OSM3S___TEMPLATE_DB__BLOCK_BACKEND_H22#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_Handle33{34 Idx_Handle()36 ~Idx_Handle()40 void set_ptr(uint8* ptr)46 const Object& object() const52 uint8* get_ptr_to_raw() const57 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 >72{73 typename Object::Id_Type id() const77};80//-----------------------------------------------------------------------------83template< typename Index, typename Object, typename Idx_Assessor, typename File_Handle >84struct Block_Backend_Basic_Iterator85{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()97 const Object& object()101 const Handle< Object >& handle()106 bool is_end() const111 bool operator==(const Block_Backend_Basic_Iterator& rhs) const116private: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 offset121 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() const131 uint8* idx_ptr() const135 uint32 total_payload_size() const140 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_)150216277590 q idx_block_offset(0), obj_offset(0), file_handle(file_handle_), idx_assessor(idx_assessor_)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),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)186{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;205}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++()220template< typename Index, typename Object, typename Idx_Assessor, typename File_Handle >221void Block_Backend_Basic_Iterator< Index, Object, Idx_Assessor, File_Handle >::increment_idx()240template< typename Index, typename Object, typename Idx_Assessor, typename File_Handle >241void Block_Backend_Basic_Iterator< Index, Object, Idx_Assessor, File_Handle >::increment_block()266//-----------------------------------------------------------------------------269struct Flat_Idx_Assessor270{271 bool is_relevant(uint8*)275};278template< typename File_Blocks, typename File_Iterator >279struct Flat_File_Handle280{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()),285 bool next(uint64* ptr, bool check_idx = true)294 bool operator==(const Flat_File_Handle& rhs) const299private: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_Iterator308 : Block_Backend_Basic_Iterator< Index, Object, Flat_Idx_Assessor,309 Flat_File_Handle< File_Blocks< Index, Iterator >, typename File_Blocks< Index, Iterator >::Flat_Iterator > >310{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)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_Assessor328{329 Discrete_Idx_Assessor(const Iterator& index_it_, const Iterator& index_end_)332 bool is_relevant(uint8* ptr)340private:341 Iterator index_it;342 Iterator index_end;343};346template< typename File_Blocks, typename File_Iterator >347struct Discrete_File_Handle348{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)361 bool operator==(const Discrete_File_Handle& rhs) const366private: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_Iterator375 : 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 > >378{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_Iterator383 (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)),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_ >(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_Assessor403{404 Range_Idx_Assessor(const Iterator& index_it_, const Iterator& index_end_)407 bool is_relevant(uint8* ptr)41270559646569 q return index_it != index_end && !(idx < index_it.lower_bound()) && idx < index_it.upper_bound();415private:416 Iterator index_it;417 Iterator index_end;418};421template< typename File_Blocks, typename Index >422struct Range_File_Handle423{424 Range_File_Handle(File_Blocks& file_blocks_,425 const File_Blocks_Range_Iterator< Index, typename Ranges< Index >::Iterator >& file_it_)429 bool next(uint64* ptr, bool check_idx = true)438 bool operator==(const Range_File_Handle& rhs) const443private: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_Iterator452 : Block_Backend_Basic_Iterator< Index, Object, Range_Idx_Assessor< Index, typename Ranges< Index >::Iterator >,453 Range_File_Handle< File_Blocks< Index, Iterator >, Index > >454{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)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)47053854590 q block_size, File_Handle_(file_blocks, file_blocks.template range_end< typename Ranges< Index >::Iterator >()),474 Block_Backend_Range_Iterator(const Block_Backend_Range_Iterator& it)4764567156364 q Range_Idx_Assessor< Index, typename Ranges< Index >::Iterator >, File_Handle_ >(it) {}477};480//-----------------------------------------------------------------------------483struct Delta_Count484{487 Delta_Count& operator+=(Delta_Count rhs)494 uint64 before;495 uint64 after;496};499template< class TIndex, class TObject, class TIterator = typename std::vector< TIndex >::const_iterator >500struct Block_Backend501{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();514 Discrete_Iterator discrete_begin(TIterator begin, TIterator end)518 Range_Iterator range_begin(const Ranges< TIndex >& arg)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_)554template< class TIndex, class TObject, class TIterator >555Block_Backend< TIndex, TObject, TIterator >::~Block_Backend()563#endif