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 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__FILE_BLOCKS_INDEX_H
20#define DE__OSM3S___TEMPLATE_DB__FILE_BLOCKS_INDEX_H
22#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_Entry
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_)
460 : index(index_), pos(pos_), size(size_) {}
48 Index index;
49 uint32 pos;
50 uint32 size;
51};
54struct File_Blocks_Index_Structure_Params;
57struct File_Blocks_Index_File
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);
630 const uint8* header() const { return buf.ptr; }
640 const uint8* begin() const { return buf.ptr ? buf.ptr + 8 : 0; }
650 const uint8* end() const { return buf.ptr + size_; }
660 uint32 size() const { return size_; }
68 void clear_buf()
690 {
700 buf.resize(0);
710 size_ = 0;
720 }
74 std::string file_name;
76 template< typename Index >
77 static void inc(const uint8*& ptr)
780 {
790 ptr += 12;
800 ptr += Index::size_of((void*)ptr);
810 }
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_Mmap
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()
1017678590 q {
1027678590 q if (ptr)
1037678590 q munmap(ptr, size_);
1047678590 q }
10562393590 q const uint8* header() const { return (uint8*)ptr; }
10654715590 q const uint8* begin() const { return ptr ? ((uint8*)ptr) + 8 : 0; }
107810393590 q const uint8* end() const { return ((uint8*)ptr) + size_; }
10854715590 q uint32 size() const { return size_; }
110 std::string file_name;
112private:
113 void* ptr;
114 uint32 size_;
115};
118struct File_Blocks_Index_Structure_Params
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_Iterator
140public:
141432554590 q File_Blocks_Index_Iterator(const uint8* ptr_, const uint8* end_) : ptr(ptr_), end(end_), idx(0) {}
14212250663590 q ~File_Blocks_Index_Iterator() { delete idx; }
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++()
157550550590 q {
158 //std::cout<<"DEBUG ++ "<<(void*)ptr<<' '<<(void*)end<<'\n';
159550550590 q delete idx;
160550550590 q idx = 0;
161550550590 q ptr += 12;
162550550590 q ptr += Index::size_of((void*)ptr);
163550550590 q }
164 bool operator==(File_Blocks_Index_Iterator rhs) const
165830082590 q { return ptr == rhs.ptr; }
1660 bool operator!=(File_Blocks_Index_Iterator rhs) const { return ptr != rhs.ptr; }
1686670210584 q bool is_end() const { return ptr == end; }
16951981580 q void set_end() { ptr = end; }
171 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 file
17510903324584 q void* idx_ptr() const { return (void*)(ptr + 12); }
176 Index index() const
177462525590 q {
178462525590 q if (!idx)
179462525590 q idx = new Index((void*)(ptr + 12));
180462525590 q return *idx;
181462525590 q }
182462525590 q uint32 pos() const { return *(uint32*)ptr; }
183925050590 q uint32 size() const { return *(uint32*)(ptr + 4); }
185private:
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_Base
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_; }
202570233590 q virtual std::string get_data_file_name() const { return data_file_name; }
203161562590 q virtual uint64 get_block_size() const { return params.block_size_; }
204161562590 q virtual uint32 get_compression_factor() const { return params.compression_factor; }
20553854590 q virtual uint32 get_compression_method() const { return params.compression_method; }
2060 virtual uint32 get_block_count() const { return params.block_count; }
2077013 q virtual int32 get_file_format_version() const { return params.file_format_version; }
209 void increase_block_count(uint32 delta) { params.block_count += delta; }
2100 virtual bool empty() const { return params.empty_; }
211 File_Blocks_Index_Iterator< Index > begin()
21254715590 q { posix_madvise((void*)idx_file.header(), idx_file.size(), POSIX_MADV_WILLNEED);
21354715590 q return File_Blocks_Index_Iterator< Index >(idx_file.begin(), idx_file.end()); }
214 File_Blocks_Index_Iterator< Index > end()
215377839590 q { return File_Blocks_Index_Iterator< Index >(idx_file.end(), idx_file.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_Base
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_; }
2440 virtual std::string get_data_file_name() const { return data_file_name; }
2450 virtual uint64 get_block_size() const { return params.block_size_; }
2460 virtual uint32 get_compression_factor() const { return params.compression_factor; }
2470 virtual uint32 get_compression_method() const { return params.compression_method; }
2480 virtual uint32 get_block_count() const { return params.block_count; }
2490 virtual int32 get_file_format_version() const { return params.file_format_version; }
2510 void increase_block_count(uint32 delta) { params.block_count += delta; }
2520 virtual bool empty() const { return params.empty_; }
253 File_Blocks_Index_Iterator< Index > begin()
2540 {
2550 if (!idx_file_buf_valid)
2560 {
2570 idx_file.rebuild_index_buf(params, block_list);
2580 idx_file_buf_valid = true;
2590 }
2600 return File_Blocks_Index_Iterator< Index >(idx_file.begin(), idx_file.end());
2610 }
262 File_Blocks_Index_Iterator< Index > end()
2630 {
2640 if (!idx_file_buf_valid)
2650 {
2660 idx_file.rebuild_index_buf(params, block_list);
2670 idx_file_buf_valid = true;
2680 }
2690 return File_Blocks_Index_Iterator< Index >(idx_file.end(), idx_file.end());
2700 }
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)
2780 { return block_list.insert(block_it, entry); }
279 typename std::list< File_Block_Index_Entry< Index > >::iterator erase(
280 typename std::list< File_Block_Index_Entry< Index > >::iterator& block_it)
2810 { return block_list.erase(block_it); }
283 std::vector< std::pair< uint32, uint32 > >& get_void_blocks()
2840 {
2850 if (!void_blocks_initialized)
2860 init_void_blocks();
2870 return void_blocks;
2880 }
289 void drop_block_array()
2900 {
2910 idx_file_buf_valid = false;
2920 }
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)
3197678590 q try
3207678590 q {
3217678590 q Raw_File val_file(data_file_name, O_RDONLY, S_666, "File_Blocks_Index::File_Blocks_Index::1");
3227678590 q return val_file.size("File_Blocks_Index::File_Blocks_Index::2");
3237678590 q }
3247678590 q catch (File_Error e)
3257678590 q {
3260 if (e.error_number != ENOENT)
3270 throw;
3280 }
3290 return 0;
333template< typename Index >
334const uint8* binary_seek(const uint8* begin, const uint8* end, const Index& target)
33518424519 q{
33618424519 q auto min = begin;
33718424519 q auto max = end;
338185477519 q while (max - min > 2*(Index::const_size() + 12))
339167053519 q {
340167053519 q auto middle = min + ((max - min)/(Index::const_size() + 12)/2*(Index::const_size() + 12));
341167053519 q if (target.less((void*)(middle+12)))
34287080513 q max = middle;
34379973519 q else
34479973519 q min = middle;
345167053519 q }
34618424519 q return min;
34718424519 q}
350template< typename Index >
351void File_Blocks_Index_Iterator< Index >::seek(const Index& target)
3526582185584 q{
3536582185584 q delete idx;
3546582185584 q idx = 0;
3566582185584 q if (ptr == end || target.less((void*)(ptr+12)))
3576372080584 q return;
358210105581 q if (Index::const_size() && ((end - ptr) > 32*(Index::const_size() + 12))
359210105581 q && !target.less((void*)(ptr + 32*(Index::const_size() + 12) + 12)))
36018424519 q {
36118424519 q decltype(ptr) next = binary_seek(ptr, end, target);
36218424519 q if (next != end)
36318424519 q {
36451131519 q while (ptr < next && target.equal((void*)(next+12)))
36532707270 q next -= (Index::const_size() + 12);
36618424519 q }
36718424519 q ptr = next;
36818424519 q }
3691092014581 q while (!target.equal((void*)(ptr+12)))
3701011642578 q {
3711011642578 q decltype(ptr) next = ptr+12;
3721011642578 q next += Index::size_of((void*)next);
3731011642578 q bool is_segment = false;
3741087810578 q while (next != end && Index::equal((void*)(ptr+12), (void*)(next+12)))
37576168470 q {
37676168470 q is_segment = true;
37776168470 q next += 12;
37876168470 q next += Index::size_of((void*)next);
37976168470 q }
380 //std::cout<<"DEBUG seek "<<(void*)ptr<<' '<<(void*)next<<' '<<(void*)end<<' '<<is_segment<<'\n';
3811011642578 q if (is_segment)
38223186470 q ptr = next;
3831011642578 q if (next == end || target.less((void*)(next+12)))
384129733577 q return;
385881909571 q ptr = next;
386881909571 q }
387210105581 q}
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)
3930 : file_name(db_dir + file_prop.get_file_name_trunk()
3940 + file_name_extension + file_prop.get_data_suffix()
3950 + file_prop.get_index_suffix()
3960 + (use_shadow ? file_prop.get_shadow_suffix() : "")),
3970 buf(0), size_(0)
3980{
3990 try
4000 {
4010 if (access_mode == Access_Mode::truncate)
4020 {
4030 Raw_File(file_name, O_WRONLY|O_TRUNC, S_666, "File_Blocks_Index_File::File_Blocks_Index_File::1");
4040 buf.resize(0);
4050 }
4060 else
4070 {
4080 Raw_File source_file(file_name, O_RDONLY, S_666, "File_Blocks_Index_File::File_Blocks_Index_File::2");
410 // read index file
4110 size_ = source_file.size("File_Blocks_Index_File::File_Blocks_Index_File::3");
4120 buf.resize(size_);
4130 source_file.read(buf.ptr, size_, "File_Blocks_Index_File::File_Blocks_Index_File::4");
4140 }
4150 }
4160 catch (File_Error e)
4170 {
4180 if (e.error_number != ENOENT)
4190 throw;
4200 buf.resize(0);
4210 }
4220}
425inline 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)
4287678590 q : file_name(db_dir + file_prop.get_file_name_trunk()
4297678590 q + file_name_extension + file_prop.get_data_suffix()
4307678590 q + file_prop.get_index_suffix()
4317678590 q + (use_shadow ? file_prop.get_shadow_suffix() : "")),
4327678590 q ptr(0), size_(0)
4347678590 q try
4357678590 q {
4367678590 q Raw_File source_file(file_name, O_RDONLY, S_666, "File_Blocks_Index_Mmap::File_Blocks_Index_Mmap::1");
438 // read index file
4397678590 q size_ = source_file.size("File_Blocks_Index_Mmap::File_Blocks_Index_Mmap::2");
4407678590 q if (size_)
4417678590 q ptr = mmap(0, size_, PROT_READ, MAP_PRIVATE, source_file.fd(), 0);
4427678590 q if (ptr == (void*)(-1))
4430 throw File_Error(errno, file_name, "File_Blocks_Index_Mmap::File_Blocks_Index_Mmap::3");
4447678590 q }
4457678590 q catch (File_Error e)
4467678590 q {
4470 if (e.error_number != ENOENT)
4480 throw;
4490 }
453template< 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)
4577678590 q : empty_(false),
4587678590 q block_size_(file_prop.get_block_size()), // can be overwritten by index file
4597678590 q compression_factor(file_prop.get_compression_factor()), // can be overwritten by index file
4607678590 q compression_method(compression_method_ == File_Blocks_Index_Base::USE_DEFAULT ?
4617678590 q file_prop.get_compression_method() : compression_method_), // can be overwritten by index file
4627678590 q file_format_version(0), block_count(0)
4647678590 q const uint8* header = idx_file.header();
4657678590 q if (header)
4667678590 q {
4677678590 q if (file_name_extension != ".legacy")
4687678590 q {
4697678590 q file_format_version = *(int32*)header;
4707678590 q if (file_format_version < 7512 || file_format_version > FILE_FORMAT_VERSION)
4710 throw File_Error(
4720 0, idx_file.file_name,
4730 "File_Blocks_Index: Unsupported index file format version "
4740 + std::to_string(file_format_version) + " outside range [7512, " + std::to_string(FILE_FORMAT_VERSION) + "]");
4757678590 q block_size_ = 1ull<<*(uint8*)(header + 4);
4767678590 q if (!block_size_)
4770 throw File_Error(0, idx_file.file_name, "File_Blocks_Index: Illegal block size");
4787678590 q compression_factor = 1u<<*(uint8*)(header + 5);
4797678590 q if (!compression_factor || compression_factor > block_size_)
4800 throw File_Error(0, idx_file.file_name, "File_Blocks_Index: Illegal compression factor");
4817678590 q compression_method = *(uint16*)(header + 6);
4827678590 q }
4837678590 q if (file_size % block_size_)
4840 throw File_Error(0, idx_file.file_name, "File_Blocks_Index: Data file size does not match block size");
4857678590 q block_count = file_size / block_size_;
4867678590 q }
4877678590 q empty_ = (file_size == 0);
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)
4957678590 q : idx_file(file_prop, db_dir, use_shadow, file_name_extension),
4967678590 q data_file_name(db_dir + file_prop.get_file_name_trunk()
4977678590 q + file_name_extension + file_prop.get_data_suffix()),
4987678590 q params(file_prop, file_name_extension, USE_DEFAULT, idx_file, file_size_of(data_file_name)),
4997678590 q file_name_extension_(file_name_extension) {}
502template< class Index >
503Writeable_File_Blocks_Index< Index >::Writeable_File_Blocks_Index
504 (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),
5080 empty_index_file_name(db_dir + file_prop.get_file_name_trunk()
5090 + file_name_extension + file_prop.get_data_suffix()
5100 + file_prop.get_shadow_suffix()),
5110 data_file_name(db_dir + file_prop.get_file_name_trunk()
5120 + file_name_extension + file_prop.get_data_suffix()),
5130 params(file_prop, file_name_extension, compression_method_, idx_file, file_size_of(data_file_name)),
5140 file_name_extension_(file_name_extension), void_blocks_initialized(false)
5150{
5160 init_blocks();
5170 init_void_blocks();
5180}
521template< class Index >
522void Writeable_File_Blocks_Index< Index >::init_blocks()
5230{
5240 if (idx_file.header())
5250 {
526// clock_t start = clock();
5280 const uint8* ptr = idx_file.begin();
5290 while (ptr < idx_file.end())
5300 {
5310 Index index((void*)(ptr + 12));
5320 File_Block_Index_Entry< Index >
5330 entry(index, *(uint32*)(ptr), *(uint32*)(ptr + 4));
5340 if (entry.pos >= params.block_count)
5350 throw File_Error(0, idx_file.file_name, "File_Blocks_Index: bad pos in index file");
5360 if (entry.pos + entry.size > params.block_count)
5370 throw File_Error(0, idx_file.file_name, "File_Blocks_Index: bad size in index file");
5380 File_Blocks_Index_File::inc< Index >(ptr);
5400 block_list.push_back(entry);
5410 }
543// clock_t end = clock();
544// std::cout<<std::dec<<params.block_size_<<'\t'<<(end - start)<<'\t'<<data_file_name<<'\n';
5450 }
5460}
549inline std::vector< std::pair< uint32, uint32 > > compute_void_blocks(const std::vector< bool >& is_referred)
5500{
5510 std::vector< std::pair< uint32, uint32 > > void_blocks;
552 // determine void_blocks
5530 uint32 last_start = 0;
5540 for (uint32 i = 0; i < is_referred.size(); ++i)
5550 {
5560 if (is_referred[i])
5570 {
5580 if (last_start < i)
5590 void_blocks.push_back(std::make_pair(i - last_start, last_start));
5600 last_start = i+1;
5610 }
5620 }
5630 if (last_start < is_referred.size())
5640 void_blocks.push_back(std::make_pair(is_referred.size() - last_start, last_start));
5660 return void_blocks;
5670}
570template< typename Iterator >
571std::vector< std::pair< uint32, uint32 > > compute_void_blocks(Iterator begin, Iterator end, uint32 block_count)
5720{
5730 std::vector< bool > is_referred(block_count, false);
5740 for (auto it = begin; it != end; ++it)
5750 {
5760 for (uint32 i = 0; i < it.size(); ++i)
5770 is_referred[it.pos() + i] = true;
5780 }
5800 return compute_void_blocks(is_referred);
5810}
584template< typename List >
585std::vector< std::pair< uint32, uint32 > > compute_void_blocks(const List& block_list, uint32 block_count)
5860{
5870 std::vector< bool > is_referred(block_count, false);
5880 for (auto it = block_list.begin(); it != block_list.end(); ++it)
5890 {
5900 for (uint32 i = 0; i < it->size; ++i)
5910 is_referred[it->pos + i] = true;
5920 }
5940 return compute_void_blocks(is_referred);
5950}
598template< class Index >
599void Writeable_File_Blocks_Index< Index >::init_void_blocks()
6000{
6010 bool empty_index_file_used = false;
6020 if (empty_index_file_name != "")
6030 {
6040 try
6050 {
6060 Raw_File void_blocks_file(empty_index_file_name, O_RDONLY, S_666, "");
6070 uint32 void_index_size = void_blocks_file.size("File_Blocks_Index::File_Blocks_Index::6");
6080 Void_Pointer< uint8 > index_buf(void_index_size);
6090 void_blocks_file.read(index_buf.ptr, void_index_size,
6100 "File_Blocks_Index::File_Blocks_Index::7");
6110 for (uint32 i = 0; i < void_index_size/8; ++i)
6120 void_blocks.push_back(*(std::pair< uint32, uint32 >*)(index_buf.ptr + 8*i));
6130 empty_index_file_used = true;
6140 }
6150 catch (File_Error e) {}
6160 }
6180 if (!empty_index_file_used)
6190 void_blocks = compute_void_blocks(block_list, params.block_count);
6210 std::stable_sort(void_blocks.begin(), void_blocks.end());
6220 void_blocks_initialized = true;
6230}
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)
6300{
631 // Keep space for file version and size information
6320 size_ = 8;
6330 uint32 pos = 8;
6350 for (typename std::list< File_Block_Index_Entry< Index > >::const_iterator
6360 it(block_list.begin()); it != block_list.end(); ++it)
6370 size_ += 12 + it->index.size_of();
6390 buf.resize(size_);
6410 *(uint32*)buf.ptr = File_Blocks_Index_Structure_Params::FILE_FORMAT_VERSION;
6420 *(uint8*)(buf.ptr + 4) = shift_log(params.block_size_);
6430 *(uint8*)(buf.ptr + 5) = shift_log(params.compression_factor);
6440 *(uint16*)(buf.ptr + 6) = params.compression_method;
6460 for (typename std::list< File_Block_Index_Entry< Index > >::const_iterator
6470 it(block_list.begin()); it != block_list.end(); ++it)
6480 {
6490 *(uint32*)(buf.ptr+pos) = it->pos;
6500 pos += 4;
6510 *(uint32*)(buf.ptr+pos) = it->size;
6520 pos += 4;
6530 *(uint32*)(buf.ptr+pos) = 0;
6540 pos += 4;
6550 it->index.to_data(buf.ptr+pos);
6560 pos += it->index.size_of();
6570 }
6580}
661template< class Index >
662Writeable_File_Blocks_Index< Index >::~Writeable_File_Blocks_Index()
6630{
664 // Keep space for file version and size information
6650 if (!idx_file_buf_valid)
6660 idx_file.rebuild_index_buf(params, block_list);
6680 Raw_File dest_file(idx_file.file_name, O_RDWR|O_CREAT, S_666,
6690 "File_Blocks_Index::~File_Blocks_Index::1");
6710 if (idx_file.size() < dest_file.size("File_Blocks_Index::~File_Blocks_Index::2"))
6720 dest_file.resize(idx_file.size(), "File_Blocks_Index::~File_Blocks_Index::3");
6730 dest_file.write((void*)idx_file.header(), idx_file.size(), "File_Blocks_Index::~File_Blocks_Index::4");
675 // Write void blocks
6760 Void_Pointer< uint8 > void_index_buf(void_blocks.size() * 8);
6770 std::pair< uint32, uint32 >* it_ptr = (std::pair< uint32, uint32 >*)(void_index_buf.ptr);
6780 for (std::vector< std::pair< uint32, uint32 > >::const_iterator it(void_blocks.begin());
6790 it != void_blocks.end(); ++it)
6800 *(it_ptr++) = *it;
6820 try
6830 {
6840 Raw_File void_file(empty_index_file_name, O_RDWR|O_TRUNC, S_666,
6850 "File_Blocks_Index::~File_Blocks_Index::5");
6860 void_file.write(void_index_buf.ptr, void_blocks.size() * 8,
6870 "File_Blocks_Index::~File_Blocks_Index::6");
6880 }
6890 catch (File_Error e) {}
6900}
693/** 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)
6980{
6990 Readonly_File_Blocks_Index< Index > index(file_prop, false, db_dir, "");
7000 if (index.get_file_format_version() < min_version)
7010 return {};
7030 auto void_blocks = compute_void_blocks(index.begin(), index.end(), index.get_block_count());
7050 std::vector< bool > result(index.get_block_count(), true);
7060 for (typename std::vector< std::pair< uint32, uint32 > >::const_iterator
7070 it = void_blocks.begin(); it != void_blocks.end(); ++it)
7080 {
7090 for (uint32 i = 0; i < it->first; ++i)
7100 result[it->second + i] = false;
7110 }
7120 return result;
7130}
715#endif