1/** Copyright 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2018 Roland Olbricht et al. 3 * This file is part of Overpass_API. 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. 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. 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/>. 19#ifndef DE__OSM3S___TEMPLATE_DB__RANDOM_FILE_INDEX_H 20#define DE__OSM3S___TEMPLATE_DB__RANDOM_FILE_INDEX_H 35struct Random_File_Index_Entry 37 Random_File_Index_Entry(uint32 pos_, uint32 size_) 3838130031228 q : pos(pos_), size(size_) {} 39// Random_File_Index_Entry(const uint32 index_) 40// : index(index_), pos(i), size(i) {} 47struct Random_File_Index 50 Random_File_Index(const File_Properties& file_prop, 51 Access_Mode access_mode, bool use_shadow, 52 const std::string& db_dir, const std::string& file_name_extension, 53 int compression_method_ = File_Blocks_Index_Base::USE_DEFAULT); 552607228 q bool writeable() const { return (empty_index_file_name != ""); } 560 const std::string& file_name_extension() const { return file_name_extension_; } 582607228 q std::string get_map_file_name() const { return map_file_name; } 597821228 q uint64 get_block_size() const { return block_size_; } 60165736228 q uint32 get_compression_factor() const { return compression_factor; } 61473745195 q uint32 get_compression_method() const { return compression_method; } 63 std::vector< Random_File_Index_Entry >& get_blocks() 67 std::vector< std::pair< uint32, uint32 > >& get_void_blocks() 690 if (!void_blocks_initialized) 74 static const int FILE_FORMAT_VERSION = 1007053000; 78 std::string index_file_name; 79 std::string empty_index_file_name; 80 std::string map_file_name; 81 std::string file_name_extension_; 83 std::vector< Random_File_Index_Entry > blocks; 84 std::vector< std::pair< uint32, uint32 > > void_blocks; 85 bool void_blocks_initialized; 88 uint32 compression_factor; 89 int compression_method; 91 void init_void_blocks(); 98inline std::vector< bool > get_map_index_footprint 99 (const File_Properties& file_prop, std::string db_dir, bool use_shadow); 102/** Implementation Random_File_Index: ---------------------------------------*/ 104static const int VOID_BLOCK_ENTRY_SIZE = 8; 107inline Random_File_Index::Random_File_Index 108 (const File_Properties& file_prop, 109 Access_Mode access_mode, bool use_shadow, 110 const std::string& db_dir, const std::string& file_name_extension, int compression_method_) : 111563228 q npos(std::numeric_limits< uint32 >::max()), 112563228 q index_file_name(db_dir + file_prop.get_file_name_trunk() 113563228 q + file_name_extension + file_prop.get_id_suffix() 114563228 q + file_prop.get_index_suffix() 115563228 q + (use_shadow ? file_prop.get_shadow_suffix() : "")), 116563228 q empty_index_file_name(access_mode == Access_Mode::writeable || access_mode == Access_Mode::truncate 117563228 q ? db_dir + file_prop.get_file_name_trunk() + file_prop.get_id_suffix() + file_prop.get_shadow_suffix() 119563228 q map_file_name(db_dir + file_prop.get_file_name_trunk() 120563228 q + file_name_extension + file_prop.get_id_suffix()), 121563228 q file_name_extension_(file_name_extension), 122563228 q void_blocks_initialized(false), 123563228 q block_size_(file_prop.get_map_block_size()), 124563228 q compression_factor(file_prop.get_map_compression_factor()), 125563228 q compression_method(compression_method_ == File_Blocks_Index_Base::USE_DEFAULT ? 126563228 q file_prop.get_map_compression_method() : compression_method_), 132563228 q if (access_mode == Access_Mode::truncate) 1330 Raw_File(map_file_name, O_WRONLY|O_TRUNC, S_666, "Random_File:20"); 136563228 q Raw_File val_file(map_file_name, O_RDONLY, S_666, "Random_File:8"); 137563228 q file_size = val_file.size("Random_File:9"); 1420 if (e.error_number != ENOENT) 148563228 q if (access_mode == Access_Mode::truncate) 1490 Raw_File(index_file_name, O_WRONLY|O_TRUNC, S_666, "Random_File:19"); 154563228 q access_mode == Access_Mode::writeable || access_mode == Access_Mode::truncate ? O_RDONLY|O_CREAT : O_RDONLY, 158563228 q uint32 index_size = source_file.size("Random_File:10"); 159563228 q Void_Pointer< uint8 > index_buf(index_size); 160563228 q source_file.read(index_buf.ptr, index_size, "Random_File:14"); 162563228 q bool read_old_format = (file_name_extension == ".legacy" || 163563228 q (index_size > 0 && *(int32*)index_buf.ptr < 7512)); 164 // We support this way the old format although it has no version marker. 166563228 q if (!read_old_format && index_size > 0) 168563228 q uint8 block_exp = *(uint8*)(index_buf.ptr + 4); 169563228 q uint8 compression_exp = *(uint8*)(index_buf.ptr + 5); 170563228 q uint16 guessed_compression_method = *(uint16*)(index_buf.ptr + 6); 171563228 q uint32 guessed_compression_factor = 1u<<compression_exp; 173563228 q if (block_exp < 32 && compression_exp < 32 && guessed_compression_method < 3) 175563228 q block_count = file_size / (1ull<<block_exp); 17838130594228 q while (pos < index_size) 18038130031228 q Random_File_Index_Entry entry(*(uint32*)(index_buf.ptr + pos), 18138130031228 q *(uint32*)(index_buf.ptr + pos + 4)); 18338130031228 q blocks.push_back(entry); 18538130031228 q if (entry.size > guessed_compression_factor * 2) // increased buffer size for lz4 1870 read_old_format = true; 19138130031228 q if (entry.pos != npos && entry.pos >= block_count) 1930 read_old_format = true; 204563228 q block_size_ = 1ull<<block_exp; 205563228 q compression_factor = guessed_compression_factor; 206563228 q compression_method = guessed_compression_method; 2100 read_old_format = true; 2150 block_count = file_size/block_size_; 2180 while (pos < index_size) 2200 Random_File_Index_Entry entry(*(uint32*)(index_buf.ptr + pos), 2210 compression_factor); //block size is always 1 in the legacy format 2220 if (entry.pos != npos) 2230 entry.pos *= compression_factor; 2240 blocks.push_back(entry); 2260 if (entry.pos != npos && entry.pos >= block_count) 2270 throw File_Error(0, index_file_name, "Random_File: bad pos in index file"); 2350 if (e.error_number != ENOENT) 239563228 q if (empty_index_file_name != "") 244inline void Random_File_Index::init_void_blocks() 2460 std::vector< bool > is_referred(block_count, false); 2470 for (std::vector< Random_File_Index_Entry >::const_iterator it = blocks.begin(); it != blocks.end(); ++it) 2490 if (it->pos != npos) 2510 for (uint32 i = 0; i < it->size; ++i) 2520 is_referred[it->pos + i] = true; 2560 bool empty_index_file_used = false; 2570 if (empty_index_file_name != "") 2610 Raw_File void_blocks_file(empty_index_file_name, O_RDONLY, S_666, ""); 2620 uint32 void_index_size = void_blocks_file.size("Random_File:11"); 2630 Void_Pointer< uint8 > index_buf(void_index_size); 2640 void_blocks_file.read(index_buf.ptr, void_index_size, "Random_File:15"); 2650 for (uint32 i = 0; i < void_index_size/VOID_BLOCK_ENTRY_SIZE; ++i) 2660 void_blocks.push_back(*(std::pair< uint32, uint32 >*)(index_buf.ptr + 8*i)); 2670 empty_index_file_used = true; 2690 catch (File_Error e) {} 2720 if (!empty_index_file_used) 274 // determine void_blocks 2750 uint32 last_start = 0; 2760 for (uint32 i(0); i < block_count; ++i) 2810 void_blocks.push_back(std::make_pair(i - last_start, last_start)); 2850 if (last_start < block_count) 2860 void_blocks.push_back(std::make_pair(block_count - last_start, last_start)); 2890 std::stable_sort(void_blocks.begin(), void_blocks.end()); 2900 void_blocks_initialized = true; 294inline Random_File_Index::~Random_File_Index() 296563228 q if (empty_index_file_name == "") 299 // Keep space for file version and size information 3000 uint32 index_size = 8 + 8 * blocks.size(); 3030 Void_Pointer< uint8 > index_buf(index_size); 3050 *(uint32*)index_buf.ptr = FILE_FORMAT_VERSION; 3060 *(uint8*)(index_buf.ptr + 4) = shift_log(block_size_); 3070 *(uint8*)(index_buf.ptr + 5) = shift_log(compression_factor); 3080 *(uint16*)(index_buf.ptr + 6) = compression_method; 3100 for (std::vector< Random_File_Index_Entry >::const_iterator 3110 it = blocks.begin(); it != blocks.end(); ++it) 3130 *(uint32*)(index_buf.ptr+pos) = it->pos; 3150 *(uint32*)(index_buf.ptr+pos) = it->size; 3190 Raw_File dest_file(index_file_name, O_RDWR|O_CREAT, S_666, "Random_File:7"); 3210 if (index_size < dest_file.size("Random_File:12")) 3220 dest_file.resize(index_size, "Random_File:13"); 3230 dest_file.write(index_buf.ptr, index_size, "Random_File:17"); 3260 Void_Pointer< uint8 > void_index_buf(void_blocks.size() * 8); 3270 std::pair< uint32, uint32 >* it_ptr = (std::pair< uint32, uint32 >*)(void_index_buf.ptr); 3280 for (std::vector< std::pair< uint32, uint32 > >::const_iterator it(void_blocks.begin()); 3290 it != void_blocks.end(); ++it) 3330 Raw_File void_file(empty_index_file_name, O_RDWR|O_TRUNC, S_666, "Random_File:5"); 3340 void_file.write(void_index_buf.ptr, void_blocks.size() * 8, "Random_File:18"); 3360 catch (File_Error e) {} 340/** Implementation non-members: ---------------------------------------------*/ 343inline std::vector< bool > get_map_index_footprint 344 (const File_Properties& file_prop, std::string db_dir, bool use_shadow = false) 3460 Random_File_Index index(file_prop, Access_Mode::readonly, use_shadow, db_dir, ""); 3480 std::vector< bool > result(index.block_count, true); 3490 for (std::vector< std::pair< uint32, uint32 > >::const_iterator 3500 it = index.get_void_blocks().begin(); it != index.get_void_blocks().end(); ++it) 3520 for (uint32 i = 0; i < it->first; ++i) 3530 result[it->second + i] = false;