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__BLOCK_BACKEND_WRITE_H 20#define DE__OSM3S___TEMPLATE_DB__BLOCK_BACKEND_WRITE_H 22#include "block_backend.h" 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) 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) 370 uint bytes_written = insert_ptr - (uint8*)start_ptr; 380 if (bytes_written > 8 + idx_size) 400 *(uint32*)start_ptr = bytes_written; 410 *(((uint32*)start_ptr)+1) = bytes_written; 420 file_it = file_blocks.insert_block(file_it, start_ptr); 440 if (idx_size + obj_size + 8 > block_size) 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) 580 file_it = file_blocks.insert_block(file_it, large_buf.ptr + i*block_size/8, block_size, idx); 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), 640 insert_ptr = ((uint8*)start_ptr) + 8 + idx_size; 680 insert_ptr = ((uint8*)start_ptr) + 8 + idx_size; 710 obj.to_data(insert_ptr); 720 insert_ptr = insert_ptr + obj_size; 76template< typename Index, typename File_Blocks > 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) 900 file_it = file_blocks.replace_block(file_it, ptr); 940 { file_it = file_blocks.erase_block(file_it); } 96 File_Blocks& file_blocks; 97 typename File_Blocks::Write_Iterator file_it; 99 std::string data_filename; 103template< typename Container > 104uint32 total_size_of(const Container& container) 1060 uint32 total_size = 0; 1070 for (const auto& i : container) 1080 total_size += i.size_of(); 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; 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) 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; 1470 if (total_size > handler.block_size) 1480 throw File_Error(0, handler.data_filename, "build_dest_block: total_size bigger than block_size"); 1510 for (uint32 i = from; i < until; ++i) 1530 total_size += to_write[i].size; 1540 *(uint32*)dest = total_size; 1560 if (source && !to_write[i].existing.empty()) 158 // if not empty then the first entry is the index, thus catered for 1590 for (const auto& j : to_write[i].existing) 1610 memcpy(dest, source + j.offset, j.size); 1650 else if (to_write[i].to_insert) 1670 to_write[i].to_insert->first.to_data(dest); 1680 dest += to_write[i].to_insert->first.size_of(); 1710 if (to_write[i].to_insert) 1730 for (const auto& j : to_write[i].to_insert->second) 1760 dest += j.size_of(); 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) 1910 if (total_size < handler.block_size - 4) 1920 build_dest_block(to_write, from, until, source, dest, handler); 1950 while (total_size >= (handler.block_size - 4)*2) 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); 2070 total_size -= partial_size; 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) 2180 if (partial_size <= handler.block_size - 4) 2230 if (total_size - partial_size + to_write[split-1].size <= handler.block_size - 4) 2260 partial_size -= to_write[split].size; 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); 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); 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 > 263 const Idx_Block_To_Write< Cont_Entry >& to_write, const uint8* source, uint8* dest, File_Handler& handler) 2650 uint8* dest_pos = dest + 8; 2670 if (source && !to_write.existing.empty()) 2690 for (const auto& j : to_write.existing) 2710 memcpy(dest_pos, source + j.offset, j.size); 2750 else if (to_write.to_insert) 2770 to_write.to_insert->first.to_data(dest_pos); 2780 dest_pos += to_write.to_insert->first.size_of(); 2810 bool fit_found = false; 2820 bool oversized_found = false; 2830 if (to_write.to_insert) 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()) 2900 auto obj_size = it->size_of(); 2910 if (obj_size + idx_size + 8 < handler.block_size) 2930 if ((uint32)(dest_pos - dest) + obj_size > handler.block_size) 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; 3020 it->to_data((void*)dest_pos); 3030 dest_pos += obj_size; 3070 oversized_found = true; 3120 if (!to_write.existing.empty() || fit_found) 3140 *(uint32*)dest = (uint32)(dest_pos - dest); 3150 *(uint32*)(dest + 4) = (uint32)(dest_pos - dest); 3160 handler.insert_block((uint64*)dest); 3190 if (oversized_found) 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) 3250 auto obj_size = it->size_of(); 3260 if (obj_size + idx_size + 8 >= handler.block_size) 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( 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( 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); 353template< typename Object > 354class Object_Set_Predicate 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; 365 const std::set< Object >* data; 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) 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) 3890 if (!it->second.empty()) 3910 to_write.push_back({ *it }); 3930 if (to_write.back().size >= file_handler.block_size - 4) 3950 if (cur_from + 1 < to_write.size()) 3980 to_write, cur_from, to_write.size()-1, nullptr, dest.ptr, total_size, file_handler); 3990 file_handler.insert_block((uint64*)dest.ptr); 4020 flush_segment(to_write.back(), nullptr, dest.ptr, file_handler); 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) 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); 4170 total_size = to_write.back().size; 4200 total_size += to_write.back().size; 4230 while (total_size >= (file_handler.block_size - 4)*2) 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); 4350 total_size -= partial_size; 4380 if (file_handler.block_size < cur_from * 8) 4400 to_write.erase(to_write.begin(), to_write.begin() + cur_from); 4480 to_write, cur_from, to_write.size(), nullptr, dest.ptr, total_size, file_handler); 4490 file_handler.insert_block((uint64*)dest.ptr); 4510 ++file_handler.file_it; 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) 4610 const uint8* src_end = src_start + *(uint32*)pos; 4650 uint32 idx_size = Index::size_of((void*)pos); 4660 existing.push_back({ (uint32)(pos - src_start), idx_size }); 4700 while (pos < src_end) 4720 uint32 obj_size = Object_Predicate::Base_Object::size_of((void*)pos); 4730 if (!pred || !pred->match(typename Object_Predicate::Base_Object((void*)pos))) 4750 if (existing.empty() || existing.back().offset + existing.back().size < pos - src_start) 4760 existing.push_back({ (uint32)(pos - src_start), obj_size }); 4780 existing.back().size += obj_size; 488 std::vector< Offset_Size > existing; 494template< typename Index, typename Object_Predicate > 495class To_Delete_Matcher 497 typedef typename std::map< Index, Object_Predicate >::const_iterator Iterator; 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) 5050 while (it != end && it->first.less(data)) 5070 last_seen_equal = (it != end && it->first.equal(data)); 5080 return last_seen_equal ? &it->second : nullptr; 5110 bool was_equal() const { return last_seen_equal; } 5120 Index idx() { return it->first; } 517 bool last_seen_equal; 521template< typename Index, typename Object_Predicate, typename Container, typename File_Blocks > 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) 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) 5490 while (src_pos < source_end && !it->first.leq(src_pos + 4)) 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) 5560 to_write.push_back({ std::move(info.existing), info.size }); 5570 total_size += to_write.back().size; 5590 src_pos = source.ptr + *(uint32*)src_pos; 5620 bool entry_created = false; 5630 if (src_pos < source_end && it->first.equal(src_pos + 4)) 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(); 5680 (*obj_count)[it->first] = info.count; 5690 if (info.count.after > 0) 5710 to_write.push_back({ std::move(info.existing), info.size, *it }); 5720 entry_created = true; 5740 src_pos = source.ptr + *(uint32*)src_pos; 5760 else if (!it->second.empty()) 5780 to_write.push_back({ *it }); 5790 entry_created = true; 5840 if (to_write.back().size >= file_handler.block_size - 4) 5860 if (cur_from + 1 < to_write.size()) 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); 5930 flush_segment(to_write.back(), source.ptr, dest.ptr, file_handler); 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) 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); 6080 total_size = to_write.back().size; 6110 total_size += to_write.back().size; 6140 while (total_size >= (file_handler.block_size - 4)*2) 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); 6260 total_size -= partial_size; 6290 if (file_handler.block_size < cur_from * 8) 6310 to_write.erase(to_write.begin(), to_write.begin() + cur_from); 6350 while (src_pos < source_end) 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) 6420 to_write.push_back({ std::move(info.existing), info.size }); 6430 total_size += to_write.back().size; 6450 src_pos = source.ptr + *(uint32*)src_pos; 6510 to_write, cur_from, to_write.size(), source.ptr, dest.ptr, total_size, file_handler); 6520 file_handler.replace_block((uint64*)dest.ptr); 6550 file_handler.erase_block(); 659template< typename File_Handler > 660void flush_or_delete_block( 661 uint64* start_ptr, uint bytes_written, File_Handler& file_handler, uint32 idx_size) 6630 if (bytes_written > 8 + idx_size) 6650 *(uint32*)start_ptr = bytes_written; 6660 *(((uint32*)start_ptr)+1) = bytes_written; 6670 file_handler.replace_block(start_ptr); 6700 file_handler.erase_block(); 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) 6790 file_blocks.read_block(file_it, source.ptr); 6810 if (*(((uint32*)source.ptr) + 1) > block_size) 6830 uint32 new_buffer_size = (*(((uint32*)source.ptr) + 1)/block_size + 1) * block_size; 6840 if (buffer_size < new_buffer_size) 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; 6920 for (uint i_offset = block_size; i_offset < new_buffer_size; i_offset += block_size) 6950 file_blocks.read_block(file_it, source.ptr + i_offset/8, false); 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) 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) 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)) 7240 ((uint8*)dest_start_ptr) + dest_obj_offset, ((uint8*)source_start_ptr) + src_obj_offset, obj_size); 7250 dest_obj_offset += obj_size; 7280 src_obj_offset += obj_size; 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); 740template< typename Iterator > 741uint64 append_insertables( 742 uint64* dest_start_ptr, uint32 block_size, Iterator& cur_insert, const Iterator& cur_end) 7440 uint32 obj_append_offset = *(uint32*)dest_start_ptr; 7470 while ((cur_insert != cur_end) && (obj_append_offset + cur_insert->size_of() < block_size)) 7490 cur_insert->to_data(((uint8*)dest_start_ptr) + obj_append_offset); 7500 obj_append_offset += cur_insert->size_of(); 7550 *(uint32*)dest_start_ptr = obj_append_offset; 7560 *(((uint32*)dest_start_ptr)+1) = obj_append_offset; 762template< typename Index, typename Del_Container, typename Ins_Container, typename File_Blocks > 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) 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(); 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) 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); 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); 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; 8080 if (delete_it != to_delete.end()) 8090 count += skip_deleted_objects(source.ptr, dest.ptr, delete_it->second, idx_size); 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)) 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); 8210 ++file_handler.file_it; 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()) 8290 while (cur_insert != insert_it->second.end()) 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); 8370 if (pos > ((uint8*)dest.ptr) + idx_size + 8) 8390 *(uint32*)dest.ptr = pos - (uint8*)dest.ptr; 8400 *(((uint32*)dest.ptr)+1) = pos - (uint8*)dest.ptr; 8410 file_handler.insert_block(dest.ptr); 8450 (*obj_count)[idx] += count; 8460 file_handler.file_it.end_segments_mode(); 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) 8570 std::vector< Index > relevant_idxs; 8580 auto iit = to_insert.begin(); 8590 for (auto dit = to_delete.begin(); dit != to_delete.end(); ++dit) 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) 8650 relevant_idxs.push_back(dit->first); 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()) 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);