Overpass API regression suite coverage (Nova Scotia) / template_db/random_file.h

template_db/random_file.h

45 of 132 instrumented lines run by the suite (34.1%) 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_H
20#define DE__OSM3S___TEMPLATE_DB__RANDOM_FILE_H
22#include "random_file_index.h"
23#include "types.h"
24#include "lz4_wrapper.h"
25#include "zlib_wrapper.h"
27#include <unistd.h>
29#include <cerrno>
30#include <cstdio>
31#include <cstdlib>
32#include <limits>
33#include <map>
34#include <vector>
37template< typename Key, typename Value >
38struct Random_File
40private:
41 Random_File(const Random_File& f) {}
43public:
44 Random_File(Random_File_Index*);
45 ~Random_File();
47 Value get(Key pos);
48 void put(Key pos, const Value& index);
50private:
51 bool changed;
52 uint32 index_size;
53 uint32 compression_factor;
55 Raw_File val_file;
56 Random_File_Index* index;
57 Void_Pointer< uint8 > cache;
58 uint32 cache_pos;
59 uint32 block_size;
61 Void_Pointer< uint8 > buffer;
63 void move_cache_window(uint32 pos);
64 uint32 allocate_block(uint32 data_size);
65};
68/** Implementation Random_File: ---------------------------------------------*/
71template< typename Key, typename Value >
72Random_File< Key, Value >::Random_File(Random_File_Index* index_)
732644168 q : changed(false), index_size(Value::max_size_of()),
742644168 q compression_factor(index_->get_compression_factor()),
752644168 q val_file(index_->get_map_file_name(),
762644168 q index_->writeable() ? O_RDWR|O_CREAT : O_RDONLY,
772644168 q S_666, "Random_File:3"),
782644168 q index(index_),
792644168 q cache(index_->get_block_size() * index_->get_compression_factor()), cache_pos(index->npos),
802644168 q block_size(index_->get_block_size()),
812644168 q buffer(index_->get_block_size() * index_->get_compression_factor() * 2) // increased buffer size for lz4
822644168 q{}
85template< typename Key, typename Value >
86Random_File< Key, Value >::~Random_File()
872644168 q{
882644168 q move_cache_window(index->npos);
89 //delete index;
902644168 q}
93template< typename Key, typename Value >
94Value Random_File< Key, Value >::get(Key pos)
95899582151 q{
96899582151 q move_cache_window(pos.val() / (block_size*compression_factor /index_size));
97899582151 q return Value(cache.ptr + (pos.val() % (block_size*compression_factor/index_size))*index_size);
98899582151 q}
101template< typename Key, typename Value >
102void Random_File< Key, Value >::put(Key pos, const Value& val)
1030{
1040 if (!index->writeable())
1050 throw File_Error(0, index->get_map_file_name(), "Random_File:2");
1070 move_cache_window(pos.val() / (block_size*compression_factor/index_size));
1080 val.to_data(cache.ptr + (pos.val() % (block_size*compression_factor/index_size))*index_size);
1090 changed = true;
1100}
113template< typename Key, typename Value >
114void Random_File< Key, Value >::move_cache_window(uint32 pos)
115902226168 q{
116 // The cache already contains the needed position.
117902226168 q if ((pos == cache_pos) && (cache_pos != index->npos))
11887355473 q return;
12028672168 q if (sigterm_status() && pos != index->npos)
1210 throw File_Error(0, "-", "SIGTERM received");
12328672168 q if (pos != index->npos && pos >= 256*1024*1024/Value::max_size_of())
1240 throw File_Error(0, index->get_map_file_name(), "Random_File: id too large for map file");
12628672168 q if (changed)
1270 {
1280 uint32 data_size = compression_factor;
1290 void* target = cache.ptr;
1310 if (index->get_compression_method() == File_Blocks_Index_Base::ZLIB_COMPRESSION)
1320 {
1330 target = buffer.ptr;
1340 uint32 compressed_size = Zlib_Deflate(1)
1350 .compress(cache.ptr, block_size * compression_factor, target, block_size * index->get_compression_factor());
1360 data_size = (compressed_size - 1) / block_size + 1;
1370 zero_padding((uint8*)target + compressed_size, block_size * data_size - compressed_size);
1380 }
1390 else if (index->get_compression_method() == File_Blocks_Index_Base::LZ4_COMPRESSION)
1400 {
1410 target = buffer.ptr;
1420 uint32 compressed_size = LZ4_Deflate()
1430 .compress(cache.ptr, block_size * compression_factor, target, block_size * index->get_compression_factor() * 2);
1440 data_size = (compressed_size - 1) / block_size + 1;
1450 zero_padding((uint8*)target + compressed_size, block_size * data_size - compressed_size);
1460 }
1480 uint32 disk_pos = allocate_block(data_size);
150 // Save the found position to the index.
1510 if (index->get_blocks().size() <= cache_pos)
1520 index->get_blocks().resize(cache_pos+1, Random_File_Index_Entry(index->npos, 1));
1530 Random_File_Index_Entry entry(disk_pos, data_size);
1540 index->get_blocks()[cache_pos] = entry;
156 // Write the data at the found position.
1570 val_file.seek((int64)disk_pos*block_size, "Random_File:21");
1580 val_file.write((uint8*)target, block_size * data_size, "Random_File:22");
1590 }
16028672168 q changed = false;
16228672168 q if (pos == index->npos)
1632644168 q return;
16526028151 q if ((index->get_blocks().size() <= pos) || (index->get_blocks()[pos].pos == index->npos))
166258236 q {
167 // Reset the whole cache to zero.
16867685839036 q for (uint32 i = 0; i < block_size * compression_factor; ++i)
16967685580836 q *(cache.ptr + i) = 0;
170258236 q }
17123446146 q else
17223446146 q {
17323446146 q val_file.seek((int64)(index->get_blocks()[pos].pos)*block_size, "Random_File:23");
17423446146 q if (index->get_compression_method() == File_Blocks_Index_Base::NO_COMPRESSION)
1750 val_file.read(cache.ptr, block_size * index->get_blocks()[pos].size, "Random_File:24");
17623446146 q else if (index->get_compression_method() == File_Blocks_Index_Base::ZLIB_COMPRESSION)
1770 {
1780 val_file.read(buffer.ptr, block_size * index->get_blocks()[pos].size, "Random_File:25");
1790 Zlib_Inflate().decompress
1800 (buffer.ptr, block_size * index->get_blocks()[pos].size, cache.ptr, block_size * index->get_compression_factor());
1810 }
18223446146 q else if (index->get_compression_method() == File_Blocks_Index_Base::LZ4_COMPRESSION)
18323446146 q {
18423446146 q val_file.read(buffer.ptr, block_size * index->get_blocks()[pos].size, "Random_File:26");
18523446146 q LZ4_Inflate().decompress
18623446146 q (buffer.ptr, block_size * index->get_blocks()[pos].size, cache.ptr, block_size * index->get_compression_factor());
18723446146 q }
18823446146 q }
18926028151 q cache_pos = pos;
19026028151 q}
193// Finds an appropriate block, removes it from the list of available blocks, and returns it
194template< typename Key, typename Value >
195uint32 Random_File< Key, Value >::allocate_block(uint32 data_size)
1960{
1970 uint32 result = this->index->block_count;
1990 if (this->index->get_void_blocks().empty())
2000 this->index->block_count += data_size;
2010 else
2020 {
2030 std::vector< std::pair< uint32, uint32 > >::iterator pos_it
2040 = std::lower_bound(this->index->get_void_blocks().begin(), this->index->get_void_blocks().end(),
2050 std::make_pair(data_size, uint32(0)));
2070 if (pos_it != this->index->get_void_blocks().end() && pos_it->first == data_size)
2080 {
209 // We have a gap of exactly the needed size.
2100 result = pos_it->second;
2110 this->index->get_void_blocks().erase(pos_it);
2120 }
2130 else
2140 {
2150 pos_it = --(this->index->get_void_blocks().end());
2160 uint32 last_size = pos_it->first;
2170 while (pos_it != this->index->get_void_blocks().begin() && last_size > data_size)
2180 {
2190 --pos_it;
2200 if (last_size == pos_it->first)
2210 {
222 // We have a gap size that appears twice (or more often).
223 // This is a good heuristic choice.
2240 result = pos_it->second;
2250 pos_it->first -= data_size;
2260 pos_it->second += data_size;
2270 rearrange_block(this->index->get_void_blocks().begin(), pos_it, *pos_it);
2280 return result;
2290 }
2300 last_size = pos_it->first;
2310 }
2330 pos_it = --(this->index->get_void_blocks().end());
2340 if (pos_it->first >= data_size)
2350 {
236 // If no really matching block exists then we choose the largest one.
2370 result = pos_it->second;
2380 pos_it->first -= data_size;
2390 pos_it->second += data_size;
2400 rearrange_block(this->index->get_void_blocks().begin(), pos_it, *pos_it);
2410 }
2420 else
2430 this->index->block_count += data_size;
2440 }
2450 }
2470 return result;
2480}
251#endif