/
btree.cc
501 lines (383 loc) · 12.9 KB
/
btree.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
// -*- C++ -*- Time-stamp: <2011-03-14 19:18:16 ptr>
/*
*
* Copyright (c) 2010-2011
* Petr Ovtchenkov
*
* Licensed under the Academic Free License version 3.0
*
*/
#include <yard/yard.h>
#include "pack.h"
#include <inttypes.h>
#include <mt/uid.h>
#include <fstream>
#include <iostream>
#include <sstream>
#include <iomanip>
#include <functional>
#include <exam/defs.h>
#include <algorithm>
#include <functional>
#include <cassert>
#if !defined(STLPORT) && defined(__GNUC__)
# include <ext/functional>
#endif
#include <iterator>
#include <stdexcept>
namespace yard {
using namespace std;
void write_data( std::fstream& file, BTree::off_type address, const char* data, std::streamsize size )
{
file.seekp( address );
file.write( data, size );
}
const BTree::key_type BTree::upper_key_bound = { {0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff} };
const BTree::key_type BTree::lower_key_bound = { {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0} };
const std::pair<BTree::key_type,BTree::key_type> BTree::keys_range = { BTree::lower_key_bound, BTree::upper_key_bound };
const BTree::key_type BTree::zero_key = { {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0} };
const uint64_t BTree::magic_id = 0x81a5c36900000000LL; // fix: LSB/MSB
const std::streamsize BTree::alignment = 4096;
BTree::BTree()
{
}
BTree::BTree( const char* filename, std::ios_base::openmode mode, std::streamsize block_size )
{
BTree::open( filename, mode, block_size );
}
BTree::~BTree()
{
BTree::close();
}
BTree::off_type BTree::seek_to_end()
{
file_.seekp( 0, ios_base::end );
BTree::off_type address = file_.tellp();
int over = address % alignment;
BTree::off_type delta = over ? alignment - over : 0;
file_.seekp( delta, ios_base::end );
return address + delta;
}
BTree::off_type BTree::append_data( const char* data, std::streamsize size )
{
off_type address = seek_to_end();
write_data( file_, address, data, size );
return address;
}
BTree::off_type BTree::append(const detail::block_type& block)
{
off_type address = seek_to_end();
block.pack(file_);
return address;
}
void BTree::save(off_type offset, const detail::block_type& block)
{
file_.seekp(offset, ios_base::beg);
block.pack(file_);
}
void BTree::load( off_type offset, detail::block_type& block ) const
{
file_.seekg( offset, ios_base::beg );
block.unpack(file_);
}
const detail::block_type& BTree::get_block( off_type offset ) const
{
map<off_type, detail::block_type>::const_iterator it = cache_.find(offset);
if ( it == cache_.end() ) {
detail::block_type& result = cache_[offset];
result.set_block_size( bsz );
load(offset, result);
return result;
}
return it->second;
}
BTree::key_type BTree::min_in_subtree( off_type block_address ) const
{
const detail::block_type& block = get_block( block_address );
assert(block.begin() != block.end());
if ( block.is_leaf() ) {
return block.begin()->first;
}
return min_in_subtree( block.begin()->second.address );
}
BTree::key_type BTree::max_in_subtree( off_type block_address ) const
{
const detail::block_type& block = get_block( block_address );
assert(block.begin() != block.end());
if ( block.is_leaf() ) {
return (--block.end())->first;
}
return max_in_subtree( (--block.end())->second.address );
}
BTree::coordinate_type BTree::lookup(const coordinate_type& start, const key_type& key) const
{
if ( !is_open() || fail() ) {
throw ios_base::failure( "BTree io operation fail" );
}
if ( start.empty() ) {
return lookup(key);
}
coordinate_type result = start;
pair<key_type, key_type>& segment = result.top().second;
while ( (segment.first > key) || (segment.second < key) ) {
result.pop();
assert(!result.empty());
segment = result.top().second;
}
lookup_down(result, key);
return result;
}
/*
BTree::key_type prev_uuid(BTree::key_type key)
{
if (key.u.l[1] == 0)
--key.u.l[0];
--key.u.l[1];
return key;
}
*/
void BTree::lookup_down( coordinate_type& path, const key_type& key ) const
{
auto prev_uuid = []( key_type key ) { if ( key.u.l[1]-- == 0 ) { --key.u.l[0]; } return key; };
for ( ; ; ) {
const detail::block_type& new_block = get_block( path.top().first );
if ( new_block.is_leaf() ) {
return;
}
std::pair<key_type, key_type> segment;
detail::block_type::const_iterator next_block = new_block.route(key);
segment.first = next_block->first;
detail::block_type::const_iterator next_next_block = next_block;
advance(next_next_block, 1);
segment.second = next_next_block == new_block.end() ? upper_key_bound : prev_uuid(next_next_block->first);
path.push(make_pair(next_block->second.address, segment));
}
}
BTree::coordinate_type BTree::lookup( const key_type& key ) const
{
if ( !is_open() || fail() ) {
throw ios_base::failure( "BTree io operation fail" );
}
coordinate_type path;
path.push( make_pair( root_block_off, keys_range ) );
lookup_down( path, key );
return path;
}
BTree::key_type BTree::get_shortest_key(const key_type& first_key, const key_type& second_key)
{
assert(second_key > first_key);
//chose the key with most zeros at the end
uint8_t first[16];
uint8_t second[16];
uuid_to_long_number(first_key, first);
uuid_to_long_number(second_key, second);
uint8_t result[16];
for (int i = 0; i < 16; ++i)
{
result[i] = 0;
}
for (int i = 15; i >= 0; --i)
{
result[i] = second[i];
if (second[i] > first[i])
break;
}
key_type new_key;
long_number_to_uuid(result, new_key);
assert(new_key <= second_key);
assert(new_key > first_key);
return new_key;
}
BTree::coordinate_type BTree::insert(coordinate_type path, const key_type& key, const detail::block_coordinate& coord)
{
const block_desc& desc = path.top();
detail::block_type& block = const_cast<detail::block_type&>( get_block( desc.first ) );
if ( !block.insert(key, coord) ) {
return path; // key already present
}
if ( !block.is_overfilled() ) {
save(path.top().first, block);
return path;
}
detail::block_type new_block;
block.divide(new_block);
pair<xmt::uuid_type, xmt::uuid_type> delimiter;
if (block.is_leaf()) {
delimiter.first = (--block.end())->first;
delimiter.second = new_block.begin()->first;
} else {
delimiter.first = max_in_subtree((--block.end())->second.address);
delimiter.second = min_in_subtree(new_block.begin()->second.address);
}
off_type address_of_block = append(block);
off_type address_of_new_block = append(new_block);
cache_[address_of_block] = block;
cache_[address_of_new_block] = new_block;
cache_.erase(desc.first);
key_type new_key = get_shortest_key(delimiter.first, delimiter.second);
detail::block_coordinate new_coord;
new_coord.address = address_of_new_block;
new_coord.size = bsz;
key_type key_to_update = desc.second.first;
path.pop();
if ( !path.empty() ) {
detail::block_type& parent_block = const_cast<detail::block_type&>( get_block( path.top().first ) );
int count = parent_block.erase(key_to_update);
assert(count == 1);
detail::block_coordinate coord;
coord.address = address_of_block;
coord.size = bsz;
bool result = parent_block.insert(key_to_update, coord);
assert(result);
return insert(path, new_key, new_coord);
}
detail::block_type new_root;
new_root.set_block_size( bsz );
new_root.set_flags(detail::block_type::root_node);
{
// detail::block_coordinate zero_coord;
// zero_coord.address = address_of_block;
// zero_coord.size = bsz;
bool result = new_root.insert(zero_key, /* zero_coord */ detail::block_coordinate{address_of_block, bsz} );
assert(result);
}
bool result = new_root.insert(new_key, new_coord);
assert(result);
cache_[ root_block_off ] = new_root;
save( root_block_off, new_root);
return path;
}
void BTree::open( const char* filename, std::ios_base::openmode mode, std::streamsize block_size )
{
if ( (mode & std::ios_base::trunc) ) {
file_.open(filename, std::ios_base::in | std::ios_base::out | std::ios_base::trunc);
format_ver = 0;
bsz = block_size;
root_block_off = alignment;
uint8_t toc_key = magic;
uint64_t toc_value = magic_id;
file_.write( reinterpret_cast<const char*>(&toc_key), sizeof(toc_key) );
file_.write( reinterpret_cast<const char*>(&toc_value), sizeof(toc_value) );
toc_key = ver;
toc_value = format_ver;
file_.write( reinterpret_cast<const char*>(&toc_key), sizeof(toc_key) );
file_.write( reinterpret_cast<const char*>(&toc_value), sizeof(toc_value) );
toc_key = defbsz;
toc_value = block_size;
file_.write( reinterpret_cast<const char*>(&toc_key), sizeof(toc_key) );
file_.write( reinterpret_cast<const char*>(&toc_value), sizeof(toc_value) );
toc_key = rb_off;
toc_value = root_block_off;
file_.write( reinterpret_cast<const char*>(&toc_key), sizeof(toc_key) );
file_.write( reinterpret_cast<const char*>(&toc_value), sizeof(toc_value) );
toc_key = toc_reserve;
toc_value = cglbuf_beg = (std::max)(static_cast<std::streambuf::off_type>(static_cast<std::streambuf::off_type>(file_.tellp()) + (sizeof(toc_key) + sizeof(toc_value)) * 4), static_cast<std::streambuf::off_type>(128) );
file_.write( reinterpret_cast<const char*>(&toc_key), sizeof(toc_key) );
file_.write( reinterpret_cast<const char*>(&toc_value), sizeof(toc_value) );
toc_key = cgleaf_off;
// toc_value = 0ULL; // equal to prev, i.e. to value of toc_reserve
file_.write( reinterpret_cast<const char*>(&toc_key), sizeof(toc_key) );
file_.write( reinterpret_cast<const char*>(&toc_value), sizeof(toc_value) );
toc_key = cgleaf_off_end;
toc_value = cglbuf_end = root_block_off; // limited by first block
file_.write( reinterpret_cast<const char*>(&toc_key), sizeof(toc_key) );
file_.write( reinterpret_cast<const char*>(&toc_value), sizeof(toc_value) );
// end of toc:
toc_key = 0;
toc_value = 0ULL;
file_.write( reinterpret_cast<const char*>(&toc_key), sizeof(toc_key) );
file_.write( reinterpret_cast<const char*>(&toc_value), sizeof(toc_value) );
detail::block_type root;
root.set_block_size( bsz );
root.set_flags( detail::block_type::root_node | detail::block_type::leaf_node );
unsigned int root_address = append(root);
assert(root_address == root_block_off );
} else {
file_.open(filename, (mode & std::ios_base::out) ? std::ios_base::in | std::ios_base::out : std::ios_base::in );
uint8_t toc_key;
uint64_t toc_value;
file_.read( reinterpret_cast<char*>(&toc_key), sizeof(toc_key) );
file_.read( reinterpret_cast<char*>(&toc_value), sizeof(toc_value) );
if ( file_.fail() || (toc_key != magic) || (toc_value != magic_id) ) {
return; // signal about fail: bad file format
}
off_type reserve = 0;
for ( ; ; ) {
file_.read( reinterpret_cast<char*>(&toc_key), sizeof(toc_key) );
file_.read( reinterpret_cast<char*>(&toc_value), sizeof(toc_value) );
if ( file_.fail() ) {
return; // signal about fail: bad file format
}
switch ( toc_key ) {
case cgleaf_off:
cglbuf_beg = toc_value;
break;
case cgleaf_off_end:
cglbuf_end = toc_value;
break;
case ver:
format_ver = toc_value;
break;
case defbsz:
bsz = toc_value;
break;
case rb_off:
root_block_off = toc_value;
break;
case toc_reserve:
reserve = toc_value;
break;
case 0:
if ( toc_value == 0ULL ) {
return; // ok, end of toc
}
break;
default:
break;
}
}
}
}
void BTree::clear_cache()
{
cache_.clear();
}
void BTree::close()
{
// commits graph leafs not written to file here,
// and may be lost; see/use BTree::flush.
// dump all unwritten
// fill/dump control structs
file_.clear();
file_.close();
}
BTree::off_type BTree::add_value( const char* data, std::streamsize size )
{
return append_data( data, size );
}
std::string BTree::operator []( const key_type& key ) const throw(std::invalid_argument, std::runtime_error, std::bad_alloc, std::ios_base::failure )
{
coordinate_type c = lookup( key );
const detail::block_type& b = get( c );
detail::block_type::const_iterator i = b.find( key );
if ( i == b.end() ) {
throw std::invalid_argument( "bad key" );
}
std::string s;
s.reserve( i->second.size );
file_.seekg( i->second.address );
copy_n( istreambuf_iterator<char>(file_), i->second.size, back_inserter(s) );
if ( file_.fail() ) {
throw ios_base::failure( "BTree io operation fail" );
}
return s;
}
void BTree::insert( const key_type& key, const std::string& value )
{
detail::block_coordinate addr;
addr.address = add_value( value.data(), value.size() );
addr.size = value.size();
coordinate_type c = lookup( key );
insert( c, key, addr );
}
} // namespace yard