Overpass API regression suite coverage (Nova Scotia) / template_db/file_blocks_index.h
template_db/file_blocks_index.h
139 of 380 instrumented lines run by the suite (36.6%) 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__FILE_BLOCKS_INDEX_H20#define DE__OSM3S___TEMPLATE_DB__FILE_BLOCKS_INDEX_H22#include "types.h"24#include <sys/mman.h>25#include <unistd.h>27#include <algorithm>28#include <cerrno>29#include <cstdlib>30#include <list>31#include <string>32#include <vector>34/** Declarations: -----------------------------------------------------------*/37template< class Index >38struct File_Block_Index_Entry39{40 static const int EMPTY = 1;41 static const int GROUP = 2;42 static const int SEGMENT = 3;43 static const int LAST_SEGMENT = 4;45 File_Block_Index_Entry(const Index& index_, uint32 pos_, uint32 size_)48 Index index;49 uint32 pos;50 uint32 size;51};54struct File_Blocks_Index_Structure_Params;57struct File_Blocks_Index_File58{59public:60 File_Blocks_Index_File(61 const File_Properties& file_prop, const std::string& db_dir,62 Access_Mode access_mode, bool use_shadow, const std::string& file_name_extension);68 void clear_buf()74 std::string file_name;76 template< typename Index >77 static void inc(const uint8*& ptr)83 template< class Index >84 void rebuild_index_buf(85 const File_Blocks_Index_Structure_Params& params,86 const std::list< File_Block_Index_Entry< Index > >& block_list);88private:89 Void_Pointer< uint8 > buf;90 uint32 size_;91};94struct File_Blocks_Index_Mmap95{96public:97 File_Blocks_Index_Mmap(98 const File_Properties& file_prop, const std::string& db_dir,99 bool use_shadow, const std::string& file_name_extension);100 ~File_Blocks_Index_Mmap()110 std::string file_name;112private:113 void* ptr;114 uint32 size_;115};118struct File_Blocks_Index_Structure_Params119{120 template< typename Idx_File >121 File_Blocks_Index_Structure_Params(122 const File_Properties& file_prop, const std::string& file_name_extension, int compression_method_,123 const Idx_File& idx_file, uint64 file_size);125 bool empty_;126 uint64 block_size_;127 uint32 compression_factor;128 int compression_method;129 int32 file_format_version;131 uint32 block_count;133 static const int FILE_FORMAT_VERSION = 7600;134};137template< typename Index >138struct File_Blocks_Index_Iterator139{140public:141432554590 q File_Blocks_Index_Iterator(const uint8* ptr_, const uint8* end_) : ptr(ptr_), end(end_), idx(0) {}14311818109590 q File_Blocks_Index_Iterator(const File_Blocks_Index_Iterator& rhs) : ptr(rhs.ptr), end(rhs.end), idx(0) {}144 File_Blocks_Index_Iterator& operator=(const File_Blocks_Index_Iterator& rhs)145 {146 if (ptr != rhs.ptr)147 {148 delete idx;149 idx = 0;150 ptr = rhs.ptr;151 end = rhs.end;152 }153 return *this;154 }156 void operator++()158 //std::cout<<"DEBUG ++ "<<(void*)ptr<<' '<<(void*)end<<'\n';164 bool operator==(File_Blocks_Index_Iterator rhs) const171 void seek(const Index& idx);172 // asserts: is_end() || idx == index() || (this+1).is_end() || idx < (this+1).index()173 // NB: index() <= idx is not guaranteed, because idx < index() indicates that idx does not exist in the file176 Index index() const185private:186 const uint8* ptr;187 const uint8* end;188 mutable Index* idx;189};192template< typename Index >193struct Readonly_File_Blocks_Index : public File_Blocks_Index_Base194{195public:196 Readonly_File_Blocks_Index(197 const File_Properties& file_prop, bool use_shadow,198 const std::string& db_dir, const std::string& file_name_extension);199 bool writeable() const { return false; }200 const std::string& file_name_extension() const { return file_name_extension_; }209 void increase_block_count(uint32 delta) { params.block_count += delta; }211 File_Blocks_Index_Iterator< Index > begin()214 File_Blocks_Index_Iterator< Index > end()217private:218 File_Blocks_Index_Mmap idx_file;219 std::string data_file_name;220 File_Blocks_Index_Structure_Params params;221 std::string file_name_extension_;222};225template< class Index >226Void_Pointer< uint8 > make_index_buf(227 const File_Blocks_Index_Structure_Params& params,228 const std::list< File_Block_Index_Entry< Index > >& block_list,229 uint32& index_size);232template< class Index >233struct Writeable_File_Blocks_Index : public File_Blocks_Index_Base234{235public:236 Writeable_File_Blocks_Index(237 const File_Properties& file_prop, Access_Mode access_mode, bool use_shadow,238 const std::string& db_dir, const std::string& file_name_extension,239 int compression_method_ = USE_DEFAULT);240 virtual ~Writeable_File_Blocks_Index();241 bool writeable() const { return true; }242 const std::string& file_name_extension() const { return file_name_extension_; }253 File_Blocks_Index_Iterator< Index > begin()262 File_Blocks_Index_Iterator< Index > end()2720 const typename std::list< File_Block_Index_Entry< Index > >::iterator wr_begin() { return block_list.begin(); }2730 const typename std::list< File_Block_Index_Entry< Index > >::iterator wr_end() { return block_list.end(); }275 typename std::list< File_Block_Index_Entry< Index > >::iterator insert(276 typename std::list< File_Block_Index_Entry< Index > >::iterator& block_it,277 const File_Block_Index_Entry< Index >& entry)279 typename std::list< File_Block_Index_Entry< Index > >::iterator erase(280 typename std::list< File_Block_Index_Entry< Index > >::iterator& block_it)283 std::vector< std::pair< uint32, uint32 > >& get_void_blocks()289 void drop_block_array()294private:295 File_Blocks_Index_File idx_file;296 bool idx_file_buf_valid;297 std::string empty_index_file_name;298 std::string data_file_name;299 File_Blocks_Index_Structure_Params params;300 std::string file_name_extension_;302 std::list< File_Block_Index_Entry< Index > > block_list;303 std::vector< std::pair< uint32, uint32 > > void_blocks;304 bool void_blocks_initialized;306 void init_blocks();307 void init_void_blocks();308};311template< class Index >312std::vector< bool > get_data_index_footprint(313 const File_Properties& file_prop, std::string db_dir, int32 min_version = 0);315/** Implementation File_Blocks_Index: ---------------------------------------*/317inline uint64 file_size_of(const std::string& data_file_name)3217678590 q Raw_File val_file(data_file_name, O_RDONLY, S_666, "File_Blocks_Index::File_Blocks_Index::1");333template< typename Index >334const uint8* binary_seek(const uint8* begin, const uint8* end, const Index& target)340167053519 q auto middle = min + ((max - min)/(Index::const_size() + 12)/2*(Index::const_size() + 12));350template< typename Index >351void File_Blocks_Index_Iterator< Index >::seek(const Index& target)380 //std::cout<<"DEBUG seek "<<(void*)ptr<<' '<<(void*)next<<' '<<(void*)end<<' '<<is_segment<<'\n';390inline File_Blocks_Index_File::File_Blocks_Index_File(391 const File_Properties& file_prop, const std::string& db_dir,392 Access_Mode access_mode, bool use_shadow, const std::string& file_name_extension)4030 Raw_File(file_name, O_WRONLY|O_TRUNC, S_666, "File_Blocks_Index_File::File_Blocks_Index_File::1");4080 Raw_File source_file(file_name, O_RDONLY, S_666, "File_Blocks_Index_File::File_Blocks_Index_File::2");410 // read index file425inline File_Blocks_Index_Mmap::File_Blocks_Index_Mmap(426 const File_Properties& file_prop, const std::string& db_dir,427 bool use_shadow, const std::string& file_name_extension)4367678590 q Raw_File source_file(file_name, O_RDONLY, S_666, "File_Blocks_Index_Mmap::File_Blocks_Index_Mmap::1");438 // read index file453template< typename Idx_File >454File_Blocks_Index_Structure_Params::File_Blocks_Index_Structure_Params(455 const File_Properties& file_prop, const std::string& file_name_extension, int compression_method_,456 const Idx_File& idx_file, uint64 file_size)4597678590 q compression_factor(file_prop.get_compression_factor()), // can be overwritten by index file4617678590 q file_prop.get_compression_method() : compression_method_), // can be overwritten by index file4740 + std::to_string(file_format_version) + " outside range [7512, " + std::to_string(FILE_FORMAT_VERSION) + "]");4840 throw File_Error(0, idx_file.file_name, "File_Blocks_Index: Data file size does not match block size");491template< class Index >492Readonly_File_Blocks_Index< Index >::Readonly_File_Blocks_Index(493 const File_Properties& file_prop, bool use_shadow,494 const std::string& db_dir, const std::string& file_name_extension)4987678590 q params(file_prop, file_name_extension, USE_DEFAULT, idx_file, file_size_of(data_file_name)),502template< class Index >503Writeable_File_Blocks_Index< Index >::Writeable_File_Blocks_Index504 (const File_Properties& file_prop, Access_Mode access_mode, bool use_shadow,505 const std::string& db_dir, const std::string& file_name_extension,506 int compression_method_) :5070 idx_file(file_prop, db_dir, access_mode, use_shadow, file_name_extension), idx_file_buf_valid(true),5130 params(file_prop, file_name_extension, compression_method_, idx_file, file_size_of(data_file_name)),521template< class Index >522void Writeable_File_Blocks_Index< Index >::init_blocks()526// clock_t start = clock();543// clock_t end = clock();544// std::cout<<std::dec<<params.block_size_<<'\t'<<(end - start)<<'\t'<<data_file_name<<'\n';549inline std::vector< std::pair< uint32, uint32 > > compute_void_blocks(const std::vector< bool >& is_referred)552 // determine void_blocks570template< typename Iterator >571std::vector< std::pair< uint32, uint32 > > compute_void_blocks(Iterator begin, Iterator end, uint32 block_count)584template< typename List >585std::vector< std::pair< uint32, uint32 > > compute_void_blocks(const List& block_list, uint32 block_count)598template< class Index >599void Writeable_File_Blocks_Index< Index >::init_void_blocks()626template< class Index >627void File_Blocks_Index_File::rebuild_index_buf(628 const File_Blocks_Index_Structure_Params& params,629 const std::list< File_Block_Index_Entry< Index > >& block_list)631 // Keep space for file version and size information661template< class Index >662Writeable_File_Blocks_Index< Index >::~Writeable_File_Blocks_Index()664 // Keep space for file version and size information6730 dest_file.write((void*)idx_file.header(), idx_file.size(), "File_Blocks_Index::~File_Blocks_Index::4");675 // Write void blocks693/** Implementation non-members: ---------------------------------------------*/695template< class Index >696std::vector< bool > get_data_index_footprint(697 const File_Properties& file_prop, std::string db_dir, int32 min_version)715#endif