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

template_db/random_file_index.h

91 of 217 instrumented lines run by the suite (41.9%) 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__RANDOM_FILE_INDEX_H
20#define DE__OSM3S___TEMPLATE_DB__RANDOM_FILE_INDEX_H
22#include "types.h"
24#include <unistd.h>
26#include <algorithm>
27#include <cerrno>
28#include <cstdio>
29#include <cstdlib>
30#include <limits>
31#include <map>
32#include <vector>
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) {}
42 uint32 pos;
43 uint32 size;
44};
47struct Random_File_Index
49public:
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);
54 ~Random_File_Index();
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()
64797645209 q {
65797645209 q return blocks;
66797645209 q }
67 std::vector< std::pair< uint32, uint32 > >& get_void_blocks()
680 {
690 if (!void_blocks_initialized)
700 init_void_blocks();
710 return void_blocks;
720 }
74 static const int FILE_FORMAT_VERSION = 1007053000;
75 const uint32 npos;
77private:
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;
87 uint64 block_size_;
88 uint32 compression_factor;
89 int compression_method;
91 void init_void_blocks();
93public:
94 uint32 block_count;
95};
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()
118563228 q : ""),
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_),
127563228 q block_count(0)
129563228 q uint64 file_size = 0;
130563228 q try
132563228 q if (access_mode == Access_Mode::truncate)
1330 Raw_File(map_file_name, O_WRONLY|O_TRUNC, S_666, "Random_File:20");
134563228 q else
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");
140563228 q catch (File_Error e)
1420 if (e.error_number != ENOENT)
1430 throw;
1440 }
146563228 q try
148563228 q if (access_mode == Access_Mode::truncate)
1490 Raw_File(index_file_name, O_WRONLY|O_TRUNC, S_666, "Random_File:19");
150563228 q else
152563228 q Raw_File source_file(
153563228 q index_file_name,
154563228 q access_mode == Access_Mode::writeable || access_mode == Access_Mode::truncate ? O_RDONLY|O_CREAT : O_RDONLY,
155563228 q S_666, "Random_File:6");
157 // read index file
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);
177563228 q uint32 pos = 8;
17838130594228 q while (pos < index_size)
17938130031228 q {
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
1860 {
1870 read_old_format = true;
1880 break;
1890 }
19138130031228 q if (entry.pos != npos && entry.pos >= block_count)
1920 {
1930 read_old_format = true;
1940 break;
1950 }
19738130031228 q pos += 8;
19838130031228 q }
200563228 q if (read_old_format)
2010 blocks.clear();
202563228 q else
204563228 q block_size_ = 1ull<<block_exp;
205563228 q compression_factor = guessed_compression_factor;
206563228 q compression_method = guessed_compression_method;
2090 else
2100 read_old_format = true;
213563228 q if (read_old_format)
2140 {
2150 block_count = file_size/block_size_;
2170 uint32 pos = 0;
2180 while (pos < index_size)
2190 {
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");
2280 pos += 4;
2290 }
2300 }
233563228 q catch (File_Error e)
2350 if (e.error_number != ENOENT)
2360 throw;
2370 }
239563228 q if (empty_index_file_name != "")
2400 init_void_blocks();
244inline void Random_File_Index::init_void_blocks()
2450{
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)
2480 {
2490 if (it->pos != npos)
2500 {
2510 for (uint32 i = 0; i < it->size; ++i)
2520 is_referred[it->pos + i] = true;
2530 }
2540 }
2560 bool empty_index_file_used = false;
2570 if (empty_index_file_name != "")
2580 {
2590 try
2600 {
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;
2680 }
2690 catch (File_Error e) {}
2700 }
2720 if (!empty_index_file_used)
2730 {
274 // determine void_blocks
2750 uint32 last_start = 0;
2760 for (uint32 i(0); i < block_count; ++i)
2770 {
2780 if (is_referred[i])
2790 {
2800 if (last_start < i)
2810 void_blocks.push_back(std::make_pair(i - last_start, last_start));
2820 last_start = i+1;
2830 }
2840 }
2850 if (last_start < block_count)
2860 void_blocks.push_back(std::make_pair(block_count - last_start, last_start));
2870 }
2890 std::stable_sort(void_blocks.begin(), void_blocks.end());
2900 void_blocks_initialized = true;
2910}
294inline Random_File_Index::~Random_File_Index()
296563228 q if (empty_index_file_name == "")
297563228 q return;
299 // Keep space for file version and size information
3000 uint32 index_size = 8 + 8 * blocks.size();
3010 uint32 pos = 8;
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)
3120 {
3130 *(uint32*)(index_buf.ptr+pos) = it->pos;
3140 pos += 4;
3150 *(uint32*)(index_buf.ptr+pos) = it->size;
3160 pos += 4;
3170 }
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");
325 // Write void blocks
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)
3300 *(it_ptr++) = *it;
3310 try
3320 {
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");
3350 }
3360 catch (File_Error e) {}
3370}
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)
3450{
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)
3510 {
3520 for (uint32 i = 0; i < it->first; ++i)
3530 result[it->second + i] = false;
3540 }
3550 return result;
3560}
358#endif