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

template_db/block_backend_write.h

0 of 619 instrumented lines run by the suite (0.0%) 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__BLOCK_BACKEND_WRITE_H
20#define DE__OSM3S___TEMPLATE_DB__BLOCK_BACKEND_WRITE_H
22#include "block_backend.h"
24#include <type_traits>
27template< typename Index, typename Object, typename File_Blocks, typename Iterator >
28void flush_if_necessary_and_write_obj(
29 uint64* start_ptr, uint8*& insert_ptr, File_Blocks& file_blocks, Iterator& file_it,
30 const Index& idx, const Object& obj, uint32 block_size, const std::string& data_filename)
310{
320 uint32 idx_size = idx.size_of();
330 uint32 obj_size = obj.size_of();
350 if (insert_ptr - (uint8*)start_ptr + obj_size > block_size)
360 {
370 uint bytes_written = insert_ptr - (uint8*)start_ptr;
380 if (bytes_written > 8 + idx_size)
390 {
400 *(uint32*)start_ptr = bytes_written;
410 *(((uint32*)start_ptr)+1) = bytes_written;
420 file_it = file_blocks.insert_block(file_it, start_ptr);
430 }
440 if (idx_size + obj_size + 8 > block_size)
450 {
460 if (obj_size > 64*1024*1024)
470 throw File_Error(0, data_filename, "Block_Backend: an item's size exceeds limit of 64 MiB.");
490 uint buf_scale = (idx_size + obj_size + 7)/block_size + 1;
500 Void64_Pointer< uint64 > large_buf(buf_scale * block_size);
510 *(uint32*)large_buf.ptr = block_size;
520 *(((uint32*)large_buf.ptr)+1) = idx_size + obj_size + 8;
530 memcpy(large_buf.ptr+1, start_ptr+1, idx_size);
540 obj.to_data(((uint8*)large_buf.ptr) + 8 + idx_size);
560 for (uint i = 0; i+1 < buf_scale; ++i)
570 {
580 file_it = file_blocks.insert_block(file_it, large_buf.ptr + i*block_size/8, block_size, idx);
590 }
600 file_it = file_blocks.insert_block(
610 file_it, large_buf.ptr + (buf_scale-1)*block_size/8, idx_size + obj_size + 8 - block_size*(buf_scale-1),
620 idx);
640 insert_ptr = ((uint8*)start_ptr) + 8 + idx_size;
650 return;
660 }
670 else
680 insert_ptr = ((uint8*)start_ptr) + 8 + idx_size;
690 }
710 obj.to_data(insert_ptr);
720 insert_ptr = insert_ptr + obj_size;
730}
76template< typename Index, typename File_Blocks >
77struct File_Handler
79 File_Handler(
80 File_Blocks& file_blocks_, const std::vector< Index >& relevant_idxs,
81 uint32 block_size_, const std::string& data_filename_)
820 : file_blocks(file_blocks_),
830 file_it(file_blocks.write_begin(relevant_idxs.begin(), relevant_idxs.end(), true)),
840 block_size(block_size_), data_filename(data_filename_) {}
86 void insert_block(uint64* ptr)
870 { file_it = file_blocks.insert_block(file_it, ptr); }
88 void replace_block(uint64* ptr)
890 {
900 file_it = file_blocks.replace_block(file_it, ptr);
910 ++file_it;
920 }
93 void erase_block()
940 { file_it = file_blocks.erase_block(file_it); }
96 File_Blocks& file_blocks;
97 typename File_Blocks::Write_Iterator file_it;
98 uint32 block_size;
99 std::string data_filename;
100};
103template< typename Container >
104uint32 total_size_of(const Container& container)
1050{
1060 uint32 total_size = 0;
1070 for (const auto& i : container)
1080 total_size += i.size_of();
1090 return total_size;
1100}
113struct Offset_Size
115 uint32 offset;
116 uint32 size;
117};
120template< typename Cont_Entry >
121struct Idx_Block_To_Write
123 Idx_Block_To_Write(std::vector< Offset_Size >&& existing_, uint32 existing_size)
1240 : existing(existing_), to_insert(nullptr), size(4 + existing_size) {}
125 Idx_Block_To_Write(std::vector< Offset_Size >&& existing_, uint32 existing_size, const Cont_Entry& to_insert_)
1260 : existing(existing_), to_insert(&to_insert_), size(4 + existing_size + total_size_of(to_insert_.second)) {}
127 Idx_Block_To_Write(const Cont_Entry& to_insert_)
1280 : to_insert(&to_insert_), size(4 + to_insert_.first.size_of() + total_size_of(to_insert_.second)) {}
130 std::vector< Offset_Size > existing;
131 Cont_Entry* to_insert;
132 uint32 size;
133};
136template< typename Cont_Entry, typename File_Handler >
137void build_dest_block(
138 const std::vector< Idx_Block_To_Write< Cont_Entry > >& to_write, uint32 from, uint32 until,
139 const uint8* source, uint8* dest, const File_Handler& handler)
1400{
1410 uint32 total_size = 4;
1420 for (uint32 i = from; i < until; ++i)
1430 total_size += to_write[i].size;
1440 *(uint32*)dest = total_size;
1450 dest += 4;
1470 if (total_size > handler.block_size)
1480 throw File_Error(0, handler.data_filename, "build_dest_block: total_size bigger than block_size");
1500 total_size = 4;
1510 for (uint32 i = from; i < until; ++i)
1520 {
1530 total_size += to_write[i].size;
1540 *(uint32*)dest = total_size;
1550 dest += 4;
1560 if (source && !to_write[i].existing.empty())
1570 {
158 // if not empty then the first entry is the index, thus catered for
1590 for (const auto& j : to_write[i].existing)
1600 {
1610 memcpy(dest, source + j.offset, j.size);
1620 dest += j.size;
1630 }
1640 }
1650 else if (to_write[i].to_insert)
1660 {
1670 to_write[i].to_insert->first.to_data(dest);
1680 dest += to_write[i].to_insert->first.size_of();
1690 }
1710 if (to_write[i].to_insert)
1720 {
1730 for (const auto& j : to_write[i].to_insert->second)
1740 {
1750 j.to_data(dest);
1760 dest += j.size_of();
1770 }
1780 }
1790 }
1800}
183// If the elements fit within one block then compile and return that block.
184// If the elements do not fit within one block then compile and flush a block and return the remainder.
185template< typename Cont_Entry, typename File_Handler >
186void force_flush_group(
187 const std::vector< Idx_Block_To_Write< Cont_Entry > >& to_write, uint32 from, uint32 until,
188 const uint8* source, uint8* dest, uint32 total_size,
189 File_Handler& handler)
1900{
1910 if (total_size < handler.block_size - 4)
1920 build_dest_block(to_write, from, until, source, dest, handler);
1930 else
1940 {
1950 while (total_size >= (handler.block_size - 4)*2)
1960 {
1970 uint32 split = from;
1980 uint32 partial_size = 0;
1990 while (split < until && partial_size <= (handler.block_size - 4)*2/3)
2000 partial_size += to_write[split++].size;
2010 if (partial_size > handler.block_size - 4)
2020 partial_size -= to_write[--split].size;
2040 build_dest_block(to_write, from, split, source, dest, handler);
2050 handler.insert_block((uint64*)dest);
2060 from = split;
2070 total_size -= partial_size;
2080 }
2100 uint32 split = from;
2110 uint32 partial_size = 0;
2120 while (split < until && partial_size*2 <= total_size)
2130 partial_size += to_write[split++].size;
2150 bool two_blocks = false;
2160 if (2*partial_size < total_size + to_write[split-1].size)
2170 {
2180 if (partial_size <= handler.block_size - 4)
2190 two_blocks = true;
2200 }
2210 else
2220 {
2230 if (total_size - partial_size + to_write[split-1].size <= handler.block_size - 4)
2240 {
2250 --split;
2260 partial_size -= to_write[split].size;
2270 two_blocks = true;
2280 }
2290 }
2310 if (two_blocks)
2320 {
2330 build_dest_block(to_write, from, split, source, dest, handler);
2340 handler.insert_block((uint64*)dest);
2350 build_dest_block(to_write, split, until, source, dest, handler);
2360 }
2370 else
2380 {
2390 uint32 partial_size_l = partial_size - to_write[split-1].size;
2400 uint32 split_l = split-1;
2410 while (from+1 < split_l && partial_size_l - to_write[split_l-1].size > total_size*2/3)
2420 partial_size_l -= to_write[--split_l].size;
2440 uint32 partial_size_r = total_size - partial_size;
2450 uint32 split_r = split;
2460 while (split_r+1 < until && partial_size_r - to_write[split_r].size > total_size*2/3)
2470 partial_size_r -= to_write[split_r++].size;
2490 build_dest_block(to_write, from, split_l, source, dest, handler);
2500 handler.insert_block((uint64*)dest);
2510 build_dest_block(to_write, split_l, split_r, source, dest, handler);
2520 handler.insert_block((uint64*)dest);
2530 build_dest_block(to_write, split_r, until, source, dest, handler);
2540 }
2550 }
2560}
259// If the elements fit within one block then compile and return that block.
260// If the elements do not fit within one block then compile and flush a block and return the remainder.
261template< typename Cont_Entry, typename File_Handler >
262void flush_segment(
263 const Idx_Block_To_Write< Cont_Entry >& to_write, const uint8* source, uint8* dest, File_Handler& handler)
2640{
2650 uint8* dest_pos = dest + 8;
2670 if (source && !to_write.existing.empty())
2680 {
2690 for (const auto& j : to_write.existing)
2700 {
2710 memcpy(dest_pos, source + j.offset, j.size);
2720 dest_pos += j.size;
2730 }
2740 }
2750 else if (to_write.to_insert)
2760 {
2770 to_write.to_insert->first.to_data(dest_pos);
2780 dest_pos += to_write.to_insert->first.size_of();
2790 }
2810 bool fit_found = false;
2820 bool oversized_found = false;
2830 if (to_write.to_insert)
2840 {
2850 auto idx_size = to_write.to_insert->first.size_of();
2870 auto it = to_write.to_insert->second.begin();
2880 while (it != to_write.to_insert->second.end())
2890 {
2900 auto obj_size = it->size_of();
2910 if (obj_size + idx_size + 8 < handler.block_size)
2920 {
2930 if ((uint32)(dest_pos - dest) + obj_size > handler.block_size)
2940 {
2950 *(uint32*)dest = (uint32)(dest_pos - dest);
2960 *(uint32*)(dest + 4) = (uint32)(dest_pos - dest);
2970 handler.insert_block((uint64*)dest);
2990 dest_pos = dest + idx_size + 8;
3000 }
3020 it->to_data((void*)dest_pos);
3030 dest_pos += obj_size;
3040 fit_found = true;
3050 }
3060 else
3070 oversized_found = true;
3080 ++it;
3090 }
3100 }
3120 if (!to_write.existing.empty() || fit_found)
3130 {
3140 *(uint32*)dest = (uint32)(dest_pos - dest);
3150 *(uint32*)(dest + 4) = (uint32)(dest_pos - dest);
3160 handler.insert_block((uint64*)dest);
3170 }
3190 if (oversized_found)
3200 {
3210 auto idx_size = to_write.to_insert->first.size_of();
3230 for (auto it = to_write.to_insert->second.begin(); it != to_write.to_insert->second.end(); ++it)
3240 {
3250 auto obj_size = it->size_of();
3260 if (obj_size + idx_size + 8 >= handler.block_size)
3270 {
3280 if (obj_size > 64*1024*1024)
3290 throw File_Error(0, handler.data_filename, "Block_Backend: an item's size exceeds limit of 64 MiB.");
3310 uint buf_scale = (idx_size + obj_size + 7)/handler.block_size + 1;
3320 Void64_Pointer< uint64 > large_buf(buf_scale * handler.block_size);
3330 *(uint32*)large_buf.ptr = handler.block_size;
3340 *(((uint32*)large_buf.ptr)+1) = idx_size + obj_size + 8;
3350 memcpy(large_buf.ptr+1, dest+8, idx_size);
3360 it->to_data(((uint8*)large_buf.ptr) + 8 + idx_size);
3380 for (uint i = 0; i+1 < buf_scale; ++i)
3390 handler.file_it = handler.file_blocks.insert_block(
3400 handler.file_it,
3410 large_buf.ptr + i*handler.block_size/8, handler.block_size, to_write.to_insert->first);
3430 handler.file_it = handler.file_blocks.insert_block(
3440 handler.file_it,
3450 large_buf.ptr + (buf_scale-1)*handler.block_size/8, idx_size + obj_size + 8 - handler.block_size*(buf_scale-1),
3460 to_write.to_insert->first);
3470 }
3480 }
3490 }
3500}
353template< typename Object >
354class Object_Set_Predicate
356public:
3570 Object_Set_Predicate(const std::set< Object >& data_) : data(&data_) {}
359 bool match(const Object& obj) const
3600 { return data->find(obj) != data->end(); }
362 typedef Object Base_Object;
364private:
365 const std::set< Object >* data;
366};
369template< typename Index, typename Container, typename File_Blocks >
370void create_from_scratch(
371 File_Handler< Index, File_Blocks >& file_handler,
372 const std::map< Index, Container >& to_insert,
373 const std::vector< Index >& relevant_idxs,
374 std::map< Index, Delta_Count >* obj_count)
3750{
3760 Void_Pointer< uint8 > dest(file_handler.block_size);
3780 std::vector< Idx_Block_To_Write< typename std::remove_reference< decltype(*to_insert.begin()) >::type > > to_write;
3800 auto to_insert_begin = to_insert.lower_bound(*(file_handler.file_it.lower_bound()));
3810 auto to_insert_end = to_insert.end();
3820 if (file_handler.file_it.upper_bound() != relevant_idxs.end())
3830 to_insert_end = to_insert.lower_bound(*(file_handler.file_it.upper_bound()));
3850 uint32 total_size = 0;
3860 uint32 cur_from = 0;
3870 for (auto it = to_insert_begin; it != to_insert_end; ++it)
3880 {
3890 if (!it->second.empty())
3900 {
3910 to_write.push_back({ *it });
3930 if (to_write.back().size >= file_handler.block_size - 4)
3940 {
3950 if (cur_from + 1 < to_write.size())
3960 {
3970 force_flush_group(
3980 to_write, cur_from, to_write.size()-1, nullptr, dest.ptr, total_size, file_handler);
3990 file_handler.insert_block((uint64*)dest.ptr);
4000 }
4020 flush_segment(to_write.back(), nullptr, dest.ptr, file_handler);
4040 to_write.clear();
4050 cur_from = 0;
4060 total_size = 0;
4070 }
4080 else if (to_write.size() - cur_from > 1
4090 && to_write.back().size + to_write[to_write.size()-2].size > file_handler.block_size - 4)
4100 {
4110 force_flush_group(
4120 to_write, cur_from, to_write.size()-1, nullptr, dest.ptr, total_size, file_handler);
4130 file_handler.insert_block((uint64*)dest.ptr);
4150 to_write.erase(to_write.begin(), to_write.end()-1);
4160 cur_from = 0;
4170 total_size = to_write.back().size;
4180 }
4190 else
4200 total_size += to_write.back().size;
4210 }
4230 while (total_size >= (file_handler.block_size - 4)*2)
4240 {
4250 uint32 j = cur_from;
4260 uint32 partial_size = 0;
4270 while (j < to_write.size() && partial_size <= (file_handler.block_size - 4)*2/3)
4280 partial_size += to_write[j++].size;
4290 if (partial_size > file_handler.block_size - 4)
4300 partial_size -= to_write[--j].size;
4320 build_dest_block(to_write, cur_from, j, nullptr, dest.ptr, file_handler);
4330 file_handler.insert_block((uint64*)dest.ptr);
4340 cur_from = j;
4350 total_size -= partial_size;
4360 }
4380 if (file_handler.block_size < cur_from * 8)
4390 {
4400 to_write.erase(to_write.begin(), to_write.begin() + cur_from);
4410 cur_from = 0;
4420 }
4430 }
4450 if (total_size > 0)
4460 {
4470 force_flush_group(
4480 to_write, cur_from, to_write.size(), nullptr, dest.ptr, total_size, file_handler);
4490 file_handler.insert_block((uint64*)dest.ptr);
4500 }
4510 ++file_handler.file_it;
4520}
455template< typename Index, typename Object_Predicate >
456struct Existing_Idx_Info
458 Existing_Idx_Info(const uint8* src_start, const uint8* pos, const Object_Predicate* pred)
4590 : size(0)
4600 {
4610 const uint8* src_end = src_start + *(uint32*)pos;
4620 pos += 4;
4630 if (src_end > pos)
4640 {
4650 uint32 idx_size = Index::size_of((void*)pos);
4660 existing.push_back({ (uint32)(pos - src_start), idx_size });
4670 pos += idx_size;
4680 size += idx_size;
4700 while (pos < src_end)
4710 {
4720 uint32 obj_size = Object_Predicate::Base_Object::size_of((void*)pos);
4730 if (!pred || !pred->match(typename Object_Predicate::Base_Object((void*)pos)))
4740 {
4750 if (existing.empty() || existing.back().offset + existing.back().size < pos - src_start)
4760 existing.push_back({ (uint32)(pos - src_start), obj_size });
4770 else
4780 existing.back().size += obj_size;
4790 ++count.after;
4800 size += obj_size;
4810 }
4820 pos += obj_size;
4830 ++count.before;
4840 }
4850 }
4860 }
488 std::vector< Offset_Size > existing;
489 uint32 size;
490 Delta_Count count;
491};
494template< typename Index, typename Object_Predicate >
495class To_Delete_Matcher
497 typedef typename std::map< Index, Object_Predicate >::const_iterator Iterator;
499public:
500 To_Delete_Matcher(const std::map< Index, Object_Predicate >& to_delete)
5010 : it(to_delete.begin()), end(to_delete.end()), last_seen_equal(false) {}
503 const Object_Predicate* seek(uint8* data)
5040 {
5050 while (it != end && it->first.less(data))
5060 ++it;
5070 last_seen_equal = (it != end && it->first.equal(data));
5080 return last_seen_equal ? &it->second : nullptr;
5090 }
5110 bool was_equal() const { return last_seen_equal; }
5120 Index idx() { return it->first; }
514private:
515 Iterator it;
516 Iterator end;
517 bool last_seen_equal;
518};
521template< typename Index, typename Object_Predicate, typename Container, typename File_Blocks >
522void update_group(
523 File_Handler< Index, File_Blocks >& file_handler,
524 const std::map< Index, Object_Predicate >& to_delete, const std::map< Index, Container >& to_insert,
525 const std::vector< Index >& relevant_idxs,
526 std::map< Index, Delta_Count >* obj_count)
5270{
5280 Void_Pointer< uint8 > source(file_handler.block_size);
5290 Void_Pointer< uint8 > dest(file_handler.block_size);
5310 file_handler.file_blocks.read_block(file_handler.file_it, (uint64*)source.ptr);
5330 std::vector< Idx_Block_To_Write< typename std::remove_reference< decltype(*to_insert.begin()) >::type > > to_write;
5350 auto to_insert_begin = to_insert.lower_bound(*(file_handler.file_it.lower_bound()));
5360 auto to_insert_end = to_insert.end();
5370 if (file_handler.file_it.upper_bound() != relevant_idxs.end())
5380 to_insert_end = to_insert.lower_bound(*(file_handler.file_it.upper_bound()));
5400 uint8* src_pos(source.ptr + 4);
5410 uint8* source_end(source.ptr + *(uint32*)source.ptr);
5430 To_Delete_Matcher< Index, Object_Predicate > del_match(to_delete);
5450 uint32 total_size = 0;
5460 uint32 cur_from = 0;
5470 for (auto it = to_insert_begin; it != to_insert_end; ++it)
5480 {
5490 while (src_pos < source_end && !it->first.leq(src_pos + 4))
5500 {
5510 Existing_Idx_Info< Index, Object_Predicate > info(source.ptr, src_pos, del_match.seek(src_pos + 4));
5520 if (obj_count && del_match.was_equal())
5530 (*obj_count)[del_match.idx()] = info.count;
5540 if (info.count.after > 0)
5550 {
5560 to_write.push_back({ std::move(info.existing), info.size });
5570 total_size += to_write.back().size;
5580 }
5590 src_pos = source.ptr + *(uint32*)src_pos;
5600 }
5620 bool entry_created = false;
5630 if (src_pos < source_end && it->first.equal(src_pos + 4))
5640 {
5650 Existing_Idx_Info< Index, Object_Predicate > info(source.ptr, src_pos, del_match.seek(src_pos + 4));
5660 info.count.after += it->second.size();
5670 if (obj_count)
5680 (*obj_count)[it->first] = info.count;
5690 if (info.count.after > 0)
5700 {
5710 to_write.push_back({ std::move(info.existing), info.size, *it });
5720 entry_created = true;
5730 }
5740 src_pos = source.ptr + *(uint32*)src_pos;
5750 }
5760 else if (!it->second.empty())
5770 {
5780 to_write.push_back({ *it });
5790 entry_created = true;
5800 }
5820 if (entry_created)
5830 {
5840 if (to_write.back().size >= file_handler.block_size - 4)
5850 {
5860 if (cur_from + 1 < to_write.size())
5870 {
5880 force_flush_group(
5890 to_write, cur_from, to_write.size()-1, source.ptr, dest.ptr, total_size, file_handler);
5900 file_handler.insert_block((uint64*)dest.ptr);
5910 }
5930 flush_segment(to_write.back(), source.ptr, dest.ptr, file_handler);
5950 to_write.clear();
5960 cur_from = 0;
5970 total_size = 0;
5980 }
5990 else if (to_write.size() - cur_from > 1
6000 && to_write.back().size + to_write[to_write.size()-2].size > file_handler.block_size - 4)
6010 {
6020 force_flush_group(
6030 to_write, cur_from, to_write.size()-1, source.ptr, dest.ptr, total_size, file_handler);
6040 file_handler.insert_block((uint64*)dest.ptr);
6060 to_write.erase(to_write.begin(), to_write.end()-1);
6070 cur_from = 0;
6080 total_size = to_write.back().size;
6090 }
6100 else
6110 total_size += to_write.back().size;
6120 }
6140 while (total_size >= (file_handler.block_size - 4)*2)
6150 {
6160 uint32 j = cur_from;
6170 uint32 partial_size = 0;
6180 while (j < to_write.size() && partial_size <= (file_handler.block_size - 4)*2/3)
6190 partial_size += to_write[j++].size;
6200 if (partial_size > file_handler.block_size - 4)
6210 partial_size -= to_write[--j].size;
6230 build_dest_block(to_write, cur_from, j, source.ptr, dest.ptr, file_handler);
6240 file_handler.insert_block((uint64*)dest.ptr);
6250 cur_from = j;
6260 total_size -= partial_size;
6270 }
6290 if (file_handler.block_size < cur_from * 8)
6300 {
6310 to_write.erase(to_write.begin(), to_write.begin() + cur_from);
6320 cur_from = 0;
6330 }
6340 }
6350 while (src_pos < source_end)
6360 {
6370 Existing_Idx_Info< Index, Object_Predicate > info(source.ptr, src_pos, del_match.seek(src_pos + 4));
6380 if (obj_count && del_match.was_equal())
6390 (*obj_count)[del_match.idx()] = info.count;
6400 if (info.count.after > 0)
6410 {
6420 to_write.push_back({ std::move(info.existing), info.size });
6430 total_size += to_write.back().size;
6440 }
6450 src_pos = source.ptr + *(uint32*)src_pos;
6460 }
6480 if (total_size > 0)
6490 {
6500 force_flush_group(
6510 to_write, cur_from, to_write.size(), source.ptr, dest.ptr, total_size, file_handler);
6520 file_handler.replace_block((uint64*)dest.ptr);
6530 }
6540 else
6550 file_handler.erase_block();
6560}
659template< typename File_Handler >
660void flush_or_delete_block(
661 uint64* start_ptr, uint bytes_written, File_Handler& file_handler, uint32 idx_size)
6620{
6630 if (bytes_written > 8 + idx_size)
6640 {
6650 *(uint32*)start_ptr = bytes_written;
6660 *(((uint32*)start_ptr)+1) = bytes_written;
6670 file_handler.replace_block(start_ptr);
6680 }
6690 else
6700 file_handler.erase_block();
6710}
674template< typename File_Blocks, typename Iterator >
675bool read_block_or_blocks(
676 File_Blocks& file_blocks, Iterator& file_it, uint32 block_size,
677 Void64_Pointer< uint64 >& source, uint32& buffer_size)
6780{
6790 file_blocks.read_block(file_it, source.ptr);
6810 if (*(((uint32*)source.ptr) + 1) > block_size)
6820 {
6830 uint32 new_buffer_size = (*(((uint32*)source.ptr) + 1)/block_size + 1) * block_size;
6840 if (buffer_size < new_buffer_size)
6850 {
6860 Void64_Pointer< uint64 > new_source_buffer(new_buffer_size);
6870 memcpy(new_source_buffer.ptr, source.ptr, block_size);
6880 source.swap(new_source_buffer);
6900 buffer_size = new_buffer_size;
6910 }
6920 for (uint i_offset = block_size; i_offset < new_buffer_size; i_offset += block_size)
6930 {
6940 ++file_it;
6950 file_blocks.read_block(file_it, source.ptr + i_offset/8, false);
6960 }
6980 return true;
6990 }
7000 return false;
7010}
704template< typename Object_Predicate >
705Delta_Count skip_deleted_objects(
706 uint64* source_start_ptr, uint64* dest_start_ptr,
707 const Object_Predicate& objs_to_delete, uint32 idx_size)
7080{
7090 Delta_Count count;
7110 uint32 src_obj_offset = 8 + idx_size;
7120 uint32 dest_obj_offset = src_obj_offset;
7130 uint32 src_size = *(uint32*)source_start_ptr;
7140 memcpy(((uint8*)dest_start_ptr) + 8, ((uint8*)source_start_ptr) + 8, idx_size);
7160 while (src_obj_offset < src_size)
7170 {
7180 typename Object_Predicate::Base_Object obj(((uint8*)source_start_ptr) + src_obj_offset);
7190 uint32 obj_size = obj.size_of();
7210 if (!objs_to_delete.match(obj))
7220 {
7230 memcpy(
7240 ((uint8*)dest_start_ptr) + dest_obj_offset, ((uint8*)source_start_ptr) + src_obj_offset, obj_size);
7250 dest_obj_offset += obj_size;
7260 ++count.after;
7270 }
7280 src_obj_offset += obj_size;
7290 ++count.before;
7300 }
7320 *(uint32*)dest_start_ptr = dest_obj_offset;
7330 *(((uint32*)dest_start_ptr)+1) = dest_obj_offset;
7340 memcpy(dest_start_ptr + 1, source_start_ptr + 1, idx_size);
7360 return count;
7370}
740template< typename Iterator >
741uint64 append_insertables(
742 uint64* dest_start_ptr, uint32 block_size, Iterator& cur_insert, const Iterator& cur_end)
7430{
7440 uint32 obj_append_offset = *(uint32*)dest_start_ptr;
7450 uint64 counter = 0;
7470 while ((cur_insert != cur_end) && (obj_append_offset + cur_insert->size_of() < block_size))
7480 {
7490 cur_insert->to_data(((uint8*)dest_start_ptr) + obj_append_offset);
7500 obj_append_offset += cur_insert->size_of();
7510 ++cur_insert;
7520 ++counter;
7530 }
7550 *(uint32*)dest_start_ptr = obj_append_offset;
7560 *(((uint32*)dest_start_ptr)+1) = obj_append_offset;
7580 return counter;
7590}
762template< typename Index, typename Del_Container, typename Ins_Container, typename File_Blocks >
763void update_segments(
764 File_Handler< Index, File_Blocks >& file_handler,
765 const std::map< Index, Del_Container >& to_delete, const std::map< Index, Ins_Container >& to_insert,
766 std::map< Index, Delta_Count >* obj_count)
7670{
7680 file_handler.file_it.start_segments_mode();
7700 uint32 buffer_size = file_handler.block_size;
7710 Void64_Pointer< uint64 > source(buffer_size);
7720 Void64_Pointer< uint64 > dest(buffer_size);
7740 Index idx = file_handler.file_it.block().index;
7750 auto delete_it = to_delete.find(idx);
7760 auto insert_it = to_insert.find(idx);
7770 uint32 idx_size = idx.size_of();
7790 Delta_Count count;
7810 decltype(insert_it->second.begin()) cur_insert;
7820 if (insert_it != to_insert.end())
7830 cur_insert = insert_it->second.begin();
7850 while (!file_handler.file_it.is_end() && file_handler.file_it.block().index == idx)
7860 {
7870 auto delta_it = file_handler.file_it;
7880 bool oversized = read_block_or_blocks(
7890 file_handler.file_blocks, file_handler.file_it, file_handler.block_size, source, buffer_size);
7900 if (oversized)
7910 {
7920 ++file_handler.file_it;
7930 typename Del_Container::Base_Object obj(((uint8*)source.ptr) + 8 + idx_size);
7940 if (delete_it != to_delete.end() && delete_it->second.match(obj))
7950 file_handler.file_blocks.erase_blocks(delta_it, file_handler.file_it);
7960 else
7970 ++count.after;
7980 ++count.before;
7990 }
8000 else
8010 {
8020 if (*(uint32*)source.ptr < 8 + idx_size)
8030 { // something has seriously gone wrong - such a block shuld not exist
8040 ++file_handler.file_it;
8050 continue;
8060 }
8080 if (delete_it != to_delete.end())
8090 count += skip_deleted_objects(source.ptr, dest.ptr, delete_it->second, idx_size);
8100 else
8110 memcpy(dest.ptr, source.ptr, *(uint32*)source.ptr);
8130 if (*(uint32*)source.ptr != *(uint32*)dest.ptr
8140 || (insert_it != to_insert.end() && *(uint32*)source.ptr < file_handler.block_size/2))
8150 {
8160 if (insert_it != to_insert.end())
8170 count.after += append_insertables(dest.ptr, file_handler.block_size, cur_insert, insert_it->second.end());
8180 flush_or_delete_block(dest.ptr, *(uint32*)dest.ptr, file_handler, idx_size);
8190 }
8200 else
8210 ++file_handler.file_it;
8220 }
8230 }
8250 uint8* pos = ((uint8*)dest.ptr) + idx_size + 8;
8260 memcpy(dest.ptr+1, source.ptr+1, idx_size);
8270 if (insert_it != to_insert.end())
8280 {
8290 while (cur_insert != insert_it->second.end())
8300 {
8310 flush_if_necessary_and_write_obj(
8320 dest.ptr, pos, file_handler.file_blocks, file_handler.file_it, idx, *cur_insert, file_handler.block_size, file_handler.data_filename);
8330 ++cur_insert;
8340 ++count.after;
8350 }
8360 }
8370 if (pos > ((uint8*)dest.ptr) + idx_size + 8)
8380 {
8390 *(uint32*)dest.ptr = pos - (uint8*)dest.ptr;
8400 *(((uint32*)dest.ptr)+1) = pos - (uint8*)dest.ptr;
8410 file_handler.insert_block(dest.ptr);
8420 }
8440 if (obj_count)
8450 (*obj_count)[idx] += count;
8460 file_handler.file_it.end_segments_mode();
8470}
850template< typename Index, typename Object, typename Iterator >
851template< typename Container >
852void Block_Backend< Index, Object, Iterator >::update(
853 const std::map< Index, std::set< Object > >& to_delete,
854 const std::map< Index, Container >& to_insert,
855 std::map< Index, Delta_Count >* obj_count)
8560{
8570 std::vector< Index > relevant_idxs;
8580 auto iit = to_insert.begin();
8590 for (auto dit = to_delete.begin(); dit != to_delete.end(); ++dit)
8600 {
8610 while (iit != to_insert.end() && iit->first < dit->first)
8620 relevant_idxs.push_back((iit++)->first);
8630 if (iit != to_insert.end() && iit->first == dit->first)
8640 ++iit;
8650 relevant_idxs.push_back(dit->first);
8660 }
8670 while (iit != to_insert.end())
8680 relevant_idxs.push_back((iit++)->first);
8700 File_Handler< Index, File_Blocks_ > handler(file_blocks, relevant_idxs, block_size, data_filename);
8720 std::map< Index, Object_Set_Predicate< Object > > to_delete_;
8730 for (const auto& i : to_delete)
8740 to_delete_.insert(std::make_pair(i.first, Object_Set_Predicate< Object >(i.second)));
8760 while (handler.file_it.lower_bound() != relevant_idxs.end())
8770 {
8780 if (handler.file_it.block_type() == File_Block_Index_Entry< Index >::EMPTY)
8790 create_from_scratch(handler, to_insert, relevant_idxs, obj_count);
8800 else if (handler.file_it.block_type() == File_Block_Index_Entry< Index >::GROUP)
8810 update_group(handler, to_delete_, to_insert, relevant_idxs, obj_count);
8820 else //if (file_it.block_type() == File_Block_Index_Entry< Index >::SEGMENT)
8830 update_segments(handler, to_delete_, to_insert, obj_count);
8840 }
8850}
888#endif