Added missing files to the MANIFEST
[dbsrgits/DBM-Deep.git] / lib / DBM / Deep / Engine.pm
CommitLineData
a20d9a3f 1package DBM::Deep::Engine;
2
3use strict;
4
5use Fcntl qw( :DEFAULT :flock :seek );
6
8db25060 7##
8# Setup file and tag signatures. These should never change.
9##
10sub SIG_FILE () { 'DPDB' }
11sub SIG_INTERNAL () { 'i' }
12sub SIG_HASH () { 'H' }
13sub SIG_ARRAY () { 'A' }
8db25060 14sub SIG_NULL () { 'N' }
15sub SIG_DATA () { 'D' }
16sub SIG_INDEX () { 'I' }
17sub SIG_BLIST () { 'B' }
7b1e1aa1 18sub SIG_FREE () { 'F' }
8db25060 19sub SIG_SIZE () { 1 }
20
612969fb 21sub precalc_sizes {
beac1dff 22 ##
23 # Precalculate index, bucket and bucket list sizes
24 ##
251dfd0e 25 my $self = shift;
1bf65be7 26
251dfd0e 27 $self->{index_size} = (2**8) * $self->{long_size};
6ed2f3df 28 $self->{bucket_size} = $self->{hash_size} + $self->{long_size} * 2;
251dfd0e 29 $self->{bucket_list_size} = $self->{max_buckets} * $self->{bucket_size};
1bf65be7 30
251dfd0e 31 return 1;
1bf65be7 32}
33
34sub set_pack {
beac1dff 35 ##
36 # Set pack/unpack modes (see file header for more)
37 ##
251dfd0e 38 my $self = shift;
1bf65be7 39 my ($long_s, $long_p, $data_s, $data_p) = @_;
40
81d16922 41 ##
75be6413 42 # Set to 4 and 'N' for 32-bit offset tags (default). Theoretical limit of 4
43 # GB per file.
beac1dff 44 # (Perl must be compiled with largefile support for files > 2 GB)
81d16922 45 #
46 # Set to 8 and 'Q' for 64-bit offsets. Theoretical limit of 16 XB per file.
beac1dff 47 # (Perl must be compiled with largefile and 64-bit long support)
81d16922 48 ##
251dfd0e 49 $self->{long_size} = $long_s ? $long_s : 4;
50 $self->{long_pack} = $long_p ? $long_p : 'N';
1bf65be7 51
81d16922 52 ##
75be6413 53 # Set to 4 and 'N' for 32-bit data length prefixes. Limit of 4 GB for each
d5d7c51d 54 # key/value. Upgrading this is possible (see above) but probably not
55 # necessary. If you need more than 4 GB for a single key or value, this
56 # module is really not for you :-)
81d16922 57 ##
251dfd0e 58 $self->{data_size} = $data_s ? $data_s : 4;
59 $self->{data_pack} = $data_p ? $data_p : 'N';
1bf65be7 60
beac1dff 61 return $self->precalc_sizes();
1bf65be7 62}
63
64sub set_digest {
beac1dff 65 ##
66 # Set key digest function (default is MD5)
67 ##
251dfd0e 68 my $self = shift;
1bf65be7 69 my ($digest_func, $hash_size) = @_;
70
d0b74c17 71 $self->{digest} = $digest_func ? $digest_func : \&Digest::MD5::md5;
251dfd0e 72 $self->{hash_size} = $hash_size ? $hash_size : 16;
612969fb 73
beac1dff 74 return $self->precalc_sizes();
612969fb 75}
76
77sub new {
78 my $class = shift;
79 my ($args) = @_;
80
81 my $self = bless {
82 long_size => 4,
83 long_pack => 'N',
84 data_size => 4,
85 data_pack => 'N',
251dfd0e 86
612969fb 87 digest => \&Digest::MD5::md5,
88 hash_size => 16,
251dfd0e 89
81d16922 90 ##
d5d7c51d 91 # Maximum number of buckets per list before another level of indexing is
92 # done.
93 # Increase this value for slightly greater speed, but larger database
94 # files. DO NOT decrease this value below 16, due to risk of recursive
95 # reindex overrun.
81d16922 96 ##
612969fb 97 max_buckets => 16,
98 }, $class;
99
251dfd0e 100 $self->precalc_sizes;
612969fb 101
102 return $self;
1bf65be7 103}
104
0d0f3d5d 105sub write_file_signature {
106 my $self = shift;
107 my ($obj) = @_;
108
109 my $fh = $obj->_fh;
110
42f79e07 111 my $loc = $self->_request_space(
112 $obj, length( SIG_FILE ) + $self->{data_size},
113 );
0d0f3d5d 114 seek($fh, $loc + $obj->_root->{file_offset}, SEEK_SET);
42f79e07 115 print( $fh SIG_FILE, pack($self->{data_pack}, 0) );
0d0f3d5d 116
117 return;
118}
119
e064ccd1 120sub read_file_signature {
121 my $self = shift;
122 my ($obj) = @_;
123
124 my $fh = $obj->_fh;
125
126 seek($fh, 0 + $obj->_root->{file_offset}, SEEK_SET);
42f79e07 127 my $buffer;
128 my $bytes_read = read(
129 $fh, $buffer, length(SIG_FILE) + $self->{data_size},
130 );
e064ccd1 131
132 if ( $bytes_read ) {
42f79e07 133 my ($signature, $version) = unpack( "A4 $self->{data_pack}", $buffer );
e064ccd1 134 unless ($signature eq SIG_FILE) {
135 $self->close_fh( $obj );
136 $obj->_throw_error("Signature not found -- file is not a Deep DB");
137 }
138 }
139
140 return $bytes_read;
141}
142
70b55428 143sub setup_fh {
144 my $self = shift;
145 my ($obj) = @_;
146
147 $self->open( $obj ) if !defined $obj->_fh;
148
6fde4ed2 149 my $fh = $obj->_fh;
150 flock $fh, LOCK_EX;
118ba343 151
6fde4ed2 152 unless ( $obj->{base_offset} ) {
e064ccd1 153 my $bytes_read = $self->read_file_signature( $obj );
118ba343 154
155 ##
156 # File is empty -- write signature and master index
157 ##
158 if (!$bytes_read) {
0d0f3d5d 159 $self->write_file_signature( $obj );
118ba343 160
c9ec091a 161 $obj->{base_offset} = $self->_request_space(
16d1ad9b 162 $obj, $self->tag_size( $self->{index_size} ),
c9ec091a 163 );
118ba343 164
9e4f83a0 165 $self->write_tag(
c9ec091a 166 $obj, $obj->_base_offset, $obj->_type,
f37c15ab 167 chr(0)x$self->{index_size},
118ba343 168 );
169
170 # Flush the filehandle
171 my $old_fh = select $fh;
172 my $old_af = $|; $| = 1; $| = $old_af;
173 select $old_fh;
174 }
175 else {
176 $obj->{base_offset} = $bytes_read;
177
178 ##
118ba343 179 # Get our type from master index signature
180 ##
181 my $tag = $self->load_tag($obj, $obj->_base_offset)
182 or $obj->_throw_error("Corrupted file, no master index record");
183
184 unless ($obj->{type} eq $tag->{signature}) {
185 $obj->_throw_error("File type mismatch");
186 }
187 }
118ba343 188 }
e06824f8 189
673464d9 190 #XXX We have to make sure we don't mess up when autoflush isn't turned on
70b55428 191 unless ( $obj->_root->{inode} ) {
192 my @stats = stat($obj->_fh);
193 $obj->_root->{inode} = $stats[1];
194 $obj->_root->{end} = $stats[7];
195 }
196
6fde4ed2 197 flock $fh, LOCK_UN;
198
70b55428 199 return 1;
200}
201
a20d9a3f 202sub open {
20f7b20c 203 ##
204 # Open a fh to the database, create if nonexistent.
205 # Make sure file signature matches DBM::Deep spec.
206 ##
a20d9a3f 207 my $self = shift;
70b55428 208 my ($obj) = @_;
a20d9a3f 209
673464d9 210 # Theoretically, adding O_BINARY should remove the need for the binmode
211 # Of course, testing it is going to be ... interesting.
212 my $flags = O_RDWR | O_CREAT | O_BINARY;
a20d9a3f 213
673464d9 214 my $fh;
d5d7c51d 215 my $filename = $obj->_root->{file};
216 sysopen( $fh, $filename, $flags )
217 or $obj->_throw_error("Cannot sysopen file '$filename': $!");
673464d9 218 $obj->_root->{fh} = $fh;
a20d9a3f 219
42f79e07 220 # Even though we use O_BINARY, better be safe than sorry.
221 binmode $fh;
a20d9a3f 222
cd59cad8 223 if ($obj->_root->{autoflush}) {
a20d9a3f 224 my $old = select $fh;
225 $|=1;
226 select $old;
227 }
20f7b20c 228
a20d9a3f 229 return 1;
230}
231
3d1b8be9 232sub close_fh {
cd59cad8 233 my $self = shift;
a21f2d90 234 my ($obj) = @_;
cd59cad8 235
236 if ( my $fh = $obj->_root->{fh} ) {
237 close $fh;
238 }
239 $obj->_root->{fh} = undef;
240
241 return 1;
242}
243
16d1ad9b 244sub tag_size {
245 my $self = shift;
246 my ($size) = @_;
247 return SIG_SIZE + $self->{data_size} + $size;
248}
249
9e4f83a0 250sub write_tag {
20f7b20c 251 ##
252 # Given offset, signature and content, create tag and write to disk
253 ##
d4b1166e 254 my $self = shift;
20f7b20c 255 my ($obj, $offset, $sig, $content) = @_;
f37c15ab 256 my $size = length( $content );
20f7b20c 257
d4b1166e 258 my $fh = $obj->_fh;
259
f37c15ab 260 if ( defined $offset ) {
261 seek($fh, $offset + $obj->_root->{file_offset}, SEEK_SET);
262 }
263
251dfd0e 264 print( $fh $sig . pack($self->{data_pack}, $size) . $content );
20f7b20c 265
f37c15ab 266 return unless defined $offset;
267
20f7b20c 268 return {
269 signature => $sig,
270 size => $size,
8db25060 271 offset => $offset + SIG_SIZE + $self->{data_size},
20f7b20c 272 content => $content
273 };
d4b1166e 274}
275
276sub load_tag {
20f7b20c 277 ##
278 # Given offset, load single tag and return signature, size and data
279 ##
d4b1166e 280 my $self = shift;
20f7b20c 281 my ($obj, $offset) = @_;
282
e06824f8 283# print join(':',map{$_||''}caller(1)), $/;
284
d4b1166e 285 my $fh = $obj->_fh;
286
20f7b20c 287 seek($fh, $offset + $obj->_root->{file_offset}, SEEK_SET);
e5fc7e69 288
75be6413 289 #XXX I'm not sure this check will work if autoflush isn't enabled ...
e5fc7e69 290 return if eof $fh;
20f7b20c 291
d4b1166e 292 my $b;
8db25060 293 read( $fh, $b, SIG_SIZE + $self->{data_size} );
251dfd0e 294 my ($sig, $size) = unpack( "A $self->{data_pack}", $b );
20f7b20c 295
296 my $buffer;
297 read( $fh, $buffer, $size);
298
299 return {
300 signature => $sig,
301 size => $size,
8db25060 302 offset => $offset + SIG_SIZE + $self->{data_size},
20f7b20c 303 content => $buffer
304 };
d4b1166e 305}
306
56ec4340 307sub _get_dbm_object {
308 my $item = shift;
309
310 my $obj = eval {
311 local $SIG{__DIE__};
312 if ($item->isa( 'DBM::Deep' )) {
313 return $item;
314 }
315 return;
316 };
317 return $obj if $obj;
318
319 my $r = Scalar::Util::reftype( $item ) || '';
320 if ( $r eq 'HASH' ) {
321 my $obj = eval {
322 local $SIG{__DIE__};
323 my $obj = tied(%$item);
324 if ($obj->isa( 'DBM::Deep' )) {
325 return $obj;
326 }
327 return;
328 };
329 return $obj if $obj;
330 }
331 elsif ( $r eq 'ARRAY' ) {
332 my $obj = eval {
333 local $SIG{__DIE__};
334 my $obj = tied(@$item);
335 if ($obj->isa( 'DBM::Deep' )) {
336 return $obj;
337 }
338 return;
339 };
340 return $obj if $obj;
341 }
342
343 return;
344}
345
29b01632 346sub _length_needed {
347 my $self = shift;
f37c15ab 348 my ($obj, $value, $key) = @_;
29b01632 349
350 my $is_dbm_deep = eval {
351 local $SIG{'__DIE__'};
352 $value->isa( 'DBM::Deep' );
353 };
354
f37c15ab 355 my $len = SIG_SIZE + $self->{data_size}
356 + $self->{data_size} + length( $key );
29b01632 357
f37c15ab 358 if ( $is_dbm_deep && $value->_root eq $obj->_root ) {
359 return $len + $self->{long_size};
29b01632 360 }
361
362 my $r = Scalar::Util::reftype( $value ) || '';
9a187d8c 363 if ( $obj->_root->{autobless} ) {
364 # This is for the bit saying whether or not this thing is blessed.
365 $len += 1;
366 }
367
29b01632 368 unless ( $r eq 'HASH' || $r eq 'ARRAY' ) {
f37c15ab 369 if ( defined $value ) {
370 $len += length( $value );
371 }
372 return $len;
29b01632 373 }
374
f37c15ab 375 $len += $self->{index_size};
29b01632 376
377 # if autobless is enabled, must also take into consideration
f37c15ab 378 # the class name as it is stored after the key.
29b01632 379 if ( $obj->_root->{autobless} ) {
56ec4340 380 my $c = Scalar::Util::blessed($value);
381 if ( defined $c && !$is_dbm_deep ) {
382 $len += $self->{data_size} + length($c);
29b01632 383 }
384 }
385
f37c15ab 386 return $len;
29b01632 387}
388
20f7b20c 389sub add_bucket {
390 ##
391 # Adds one key/value pair to bucket list, given offset, MD5 digest of key,
392 # plain (undigested) key and value.
393 ##
d4b1166e 394 my $self = shift;
20f7b20c 395 my ($obj, $tag, $md5, $plain_key, $value) = @_;
75be6413 396
eea0d863 397 # This verifies that only supported values will be stored.
398 {
399 my $r = Scalar::Util::reftype( $value );
400 last if !defined $r;
401
402 last if $r eq 'HASH';
403 last if $r eq 'ARRAY';
404
405 $obj->_throw_error(
406 "Storage of variables of type '$r' is not supported."
407 );
408 }
409
20f7b20c 410 my $location = 0;
411 my $result = 2;
412
413 my $root = $obj->_root;
f37c15ab 414 my $fh = $obj->_fh;
20f7b20c 415
f37c15ab 416 my $actual_length = $self->_length_needed( $obj, $value, $plain_key );
20f7b20c 417
9a187d8c 418 my ($subloc, $offset, $size) = $self->_find_in_buckets( $tag, $md5 );
75be6413 419
f9c33187 420# $self->_release_space( $obj, $size, $subloc );
386bab6c 421 # Updating a known md5
f9c33187 422#XXX This needs updating to use _release_space
386bab6c 423 if ( $subloc ) {
424 $result = 1;
20f7b20c 425
386bab6c 426 if ($actual_length <= $size) {
427 $location = $subloc;
20f7b20c 428 }
75be6413 429 else {
f37c15ab 430 $location = $self->_request_space( $obj, $actual_length );
386bab6c 431 seek(
432 $fh,
9a187d8c 433 $tag->{offset} + $offset
434 + $self->{hash_size} + $root->{file_offset},
386bab6c 435 SEEK_SET,
436 );
9a187d8c 437 print( $fh pack($self->{long_pack}, $location ) );
438 print( $fh pack($self->{long_pack}, $actual_length ) );
75be6413 439 }
75be6413 440 }
386bab6c 441 # Adding a new md5
442 elsif ( defined $offset ) {
f37c15ab 443 $location = $self->_request_space( $obj, $actual_length );
386bab6c 444
445 seek( $fh, $tag->{offset} + $offset + $root->{file_offset}, SEEK_SET );
9a187d8c 446 print( $fh $md5 . pack($self->{long_pack}, $location ) );
447 print( $fh pack($self->{long_pack}, $actual_length ) );
386bab6c 448 }
449 # If bucket didn't fit into list, split into a new index level
f9c33187 450 # split_index() will do the _request_space() call
386bab6c 451 else {
f9c33187 452 $location = $self->split_index( $obj, $md5, $tag );
386bab6c 453 }
20f7b20c 454
d5d7c51d 455 $self->write_value( $obj, $location, $plain_key, $value );
456
457 return $result;
458}
459
460sub write_value {
461 my $self = shift;
462 my ($obj, $location, $key, $value) = @_;
463
464 my $fh = $obj->_fh;
465 my $root = $obj->_root;
466
9d4fa373 467 my $dbm_deep_obj = _get_dbm_object( $value );
468 if ( $dbm_deep_obj && $dbm_deep_obj->_root ne $obj->_root ) {
469 $obj->_throw_error( "Cannot cross-reference. Use export() instead" );
470 }
d5d7c51d 471
472 seek($fh, $location + $root->{file_offset}, SEEK_SET);
473
20f7b20c 474 ##
d5d7c51d 475 # Write signature based on content type, set content length and write
476 # actual value.
20f7b20c 477 ##
9d4fa373 478 my $r = Scalar::Util::reftype( $value ) || '';
479 if ( $dbm_deep_obj ) {
480 $self->write_tag( $obj, undef, SIG_INTERNAL,pack($self->{long_pack}, $dbm_deep_obj->_base_offset) );
f37c15ab 481 }
482 elsif ($r eq 'HASH') {
9d4fa373 483 if ( !$dbm_deep_obj && tied %{$value} ) {
019ab3a1 484 $obj->_throw_error( "Cannot store something that is tied" );
485 }
9e4f83a0 486 $self->write_tag( $obj, undef, SIG_HASH, chr(0)x$self->{index_size} );
f37c15ab 487 }
488 elsif ($r eq 'ARRAY') {
9d4fa373 489 if ( !$dbm_deep_obj && tied @{$value} ) {
019ab3a1 490 $obj->_throw_error( "Cannot store something that is tied" );
491 }
9e4f83a0 492 $self->write_tag( $obj, undef, SIG_ARRAY, chr(0)x$self->{index_size} );
f37c15ab 493 }
494 elsif (!defined($value)) {
9e4f83a0 495 $self->write_tag( $obj, undef, SIG_NULL, '' );
d5d7c51d 496 }
497 else {
9e4f83a0 498 $self->write_tag( $obj, undef, SIG_DATA, $value );
d5d7c51d 499 }
20f7b20c 500
d5d7c51d 501 ##
502 # Plain key is stored AFTER value, as keys are typically fetched less often.
503 ##
504 print( $fh pack($self->{data_pack}, length($key)) . $key );
20f7b20c 505
9a187d8c 506 # Internal references don't care about autobless
9d4fa373 507 return 1 if $dbm_deep_obj;
9a187d8c 508
d5d7c51d 509 ##
510 # If value is blessed, preserve class name
511 ##
512 if ( $root->{autobless} ) {
56ec4340 513 my $c = Scalar::Util::blessed($value);
514 if ( defined $c && !$dbm_deep_obj ) {
d5d7c51d 515 print( $fh chr(1) );
56ec4340 516 print( $fh pack($self->{data_pack}, length($c)) . $c );
20f7b20c 517 }
d5d7c51d 518 else {
519 print( $fh chr(0) );
20f7b20c 520 }
d5d7c51d 521 }
20f7b20c 522
d5d7c51d 523 ##
56ec4340 524 # Tie the passed in reference so that changes to it are reflected in the
525 # datafile. The use of $location as the base_offset will act as the
526 # the linkage between parent and child.
527 #
528 # The overall assignment is a hack around the fact that just tying doesn't
529 # store the values. This may not be the wrong thing to do.
d5d7c51d 530 ##
9d4fa373 531 if ($r eq 'HASH') {
532 my %x = %$value;
533 tie %$value, 'DBM::Deep', {
534 base_offset => $location,
535 root => $root,
536 };
537 %$value = %x;
538 }
539 elsif ($r eq 'ARRAY') {
540 my @x = @$value;
541 tie @$value, 'DBM::Deep', {
542 base_offset => $location,
543 root => $root,
544 };
545 @$value = @x;
20f7b20c 546 }
d4b1166e 547
d5d7c51d 548 return 1;
d4b1166e 549}
550
75be6413 551sub split_index {
552 my $self = shift;
553 my ($obj, $md5, $tag) = @_;
554
555 my $fh = $obj->_fh;
556 my $root = $obj->_root;
16d1ad9b 557
558 my $loc = $self->_request_space(
559 $obj, $self->tag_size( $self->{index_size} ),
560 );
561
7b1e1aa1 562 seek($fh, $tag->{ref_loc} + $root->{file_offset}, SEEK_SET);
16d1ad9b 563 print( $fh pack($self->{long_pack}, $loc) );
75be6413 564
9e4f83a0 565 my $index_tag = $self->write_tag(
16d1ad9b 566 $obj, $loc, SIG_INDEX,
f37c15ab 567 chr(0)x$self->{index_size},
75be6413 568 );
569
f9c33187 570 my $newtag_loc = $self->_request_space(
571 $obj, $self->tag_size( $self->{bucket_list_size} ),
572 );
75be6413 573
7b1e1aa1 574 my $keys = $tag->{content}
f9c33187 575 . $md5 . pack($self->{long_pack}, $newtag_loc)
576 . pack($self->{long_pack}, 0);
75be6413 577
f9c33187 578 my @newloc = ();
75be6413 579 BUCKET:
580 for (my $i = 0; $i <= $self->{max_buckets}; $i++) {
9a187d8c 581 my ($key, $old_subloc, $size) = $self->_get_key_subloc( $keys, $i );
75be6413 582
f9c33187 583 die "[INTERNAL ERROR]: No key in split_index()\n" unless $key;
584 die "[INTERNAL ERROR]: No subloc in split_index()\n" unless $old_subloc;
75be6413 585
75be6413 586 my $num = ord(substr($key, $tag->{ch} + 1, 1));
587
f9c33187 588 if ($newloc[$num]) {
589 seek($fh, $newloc[$num] + $root->{file_offset}, SEEK_SET);
75be6413 590 my $subkeys;
591 read( $fh, $subkeys, $self->{bucket_list_size});
592
f9c33187 593 # This is looking for the first empty spot
7b1e1aa1 594 my ($subloc, $offset, $size) = $self->_find_in_buckets(
f9c33187 595 { content => $subkeys }, '',
7b1e1aa1 596 );
75be6413 597
f9c33187 598 seek($fh, $newloc[$num] + $offset + $root->{file_offset}, SEEK_SET);
599 print( $fh $key . pack($self->{long_pack}, $old_subloc) );
7b1e1aa1 600
601 next;
75be6413 602 }
75be6413 603
7b1e1aa1 604 seek($fh, $index_tag->{offset} + ($num * $self->{long_size}) + $root->{file_offset}, SEEK_SET);
2603d86e 605
7b1e1aa1 606 my $loc = $self->_request_space(
607 $obj, $self->tag_size( $self->{bucket_list_size} ),
608 );
2603d86e 609
7b1e1aa1 610 print( $fh pack($self->{long_pack}, $loc) );
75be6413 611
7b1e1aa1 612 my $blist_tag = $self->write_tag(
613 $obj, $loc, SIG_BLIST,
614 chr(0)x$self->{bucket_list_size},
615 );
616
617 seek($fh, $blist_tag->{offset} + $root->{file_offset}, SEEK_SET);
f9c33187 618 print( $fh $key . pack($self->{long_pack}, $old_subloc) );
7b1e1aa1 619
f9c33187 620 $newloc[$num] = $blist_tag->{offset};
7b1e1aa1 621 }
622
623 $self->_release_space(
f9c33187 624 $obj, $self->tag_size( $self->{bucket_list_size} ),
7b1e1aa1 625 $tag->{offset} - SIG_SIZE - $self->{data_size},
626 );
75be6413 627
f9c33187 628 return $newtag_loc;
75be6413 629}
630
8db25060 631sub read_from_loc {
632 my $self = shift;
633 my ($obj, $subloc) = @_;
634
635 my $fh = $obj->_fh;
636
637 ##
638 # Found match -- seek to offset and read signature
639 ##
640 my $signature;
641 seek($fh, $subloc + $obj->_root->{file_offset}, SEEK_SET);
642 read( $fh, $signature, SIG_SIZE);
643
644 ##
645 # If value is a hash or array, return new DBM::Deep object with correct offset
646 ##
647 if (($signature eq SIG_HASH) || ($signature eq SIG_ARRAY)) {
685e40f1 648 my $new_obj = DBM::Deep->new({
8db25060 649 type => $signature,
650 base_offset => $subloc,
651 root => $obj->_root,
685e40f1 652 });
8db25060 653
685e40f1 654 if ($new_obj->_root->{autobless}) {
8db25060 655 ##
656 # Skip over value and plain key to see if object needs
657 # to be re-blessed
658 ##
659 seek($fh, $self->{data_size} + $self->{index_size}, SEEK_CUR);
660
661 my $size;
c6ea6b6c 662 read( $fh, $size, $self->{data_size});
663 $size = unpack($self->{data_pack}, $size);
8db25060 664 if ($size) { seek($fh, $size, SEEK_CUR); }
665
666 my $bless_bit;
667 read( $fh, $bless_bit, 1);
668 if (ord($bless_bit)) {
669 ##
670 # Yes, object needs to be re-blessed
671 ##
672 my $class_name;
c6ea6b6c 673 read( $fh, $size, $self->{data_size});
674 $size = unpack($self->{data_pack}, $size);
8db25060 675 if ($size) { read( $fh, $class_name, $size); }
685e40f1 676 if ($class_name) { $new_obj = bless( $new_obj, $class_name ); }
8db25060 677 }
678 }
679
685e40f1 680 return $new_obj;
8db25060 681 }
682 elsif ( $signature eq SIG_INTERNAL ) {
683 my $size;
684 read( $fh, $size, $self->{data_size});
685 $size = unpack($self->{data_pack}, $size);
686
687 if ( $size ) {
688 my $new_loc;
689 read( $fh, $new_loc, $size );
690 $new_loc = unpack( $self->{long_pack}, $new_loc );
691
692 return $self->read_from_loc( $obj, $new_loc );
693 }
694 else {
695 return;
696 }
697 }
698 ##
699 # Otherwise return actual value
700 ##
701 elsif ($signature eq SIG_DATA) {
702 my $size;
703 read( $fh, $size, $self->{data_size});
704 $size = unpack($self->{data_pack}, $size);
705
706 my $value = '';
707 if ($size) { read( $fh, $value, $size); }
708 return $value;
709 }
710
711 ##
712 # Key exists, but content is null
713 ##
714 return;
715}
716
9020ee8c 717sub get_bucket_value {
beac1dff 718 ##
719 # Fetch single value given tag and MD5 digested key.
720 ##
721 my $self = shift;
722 my ($obj, $tag, $md5) = @_;
9020ee8c 723
9a187d8c 724 my ($subloc, $offset, $size) = $self->_find_in_buckets( $tag, $md5 );
386bab6c 725 if ( $subloc ) {
8db25060 726 return $self->read_from_loc( $obj, $subloc );
386bab6c 727 }
beac1dff 728 return;
9020ee8c 729}
ab0e4957 730
731sub delete_bucket {
beac1dff 732 ##
733 # Delete single key/value pair given tag and MD5 digested key.
734 ##
735 my $self = shift;
736 my ($obj, $tag, $md5) = @_;
ab0e4957 737
9a187d8c 738 my ($subloc, $offset, $size) = $self->_find_in_buckets( $tag, $md5 );
f9c33187 739#XXX This needs _release_space()
386bab6c 740 if ( $subloc ) {
741 my $fh = $obj->_fh;
742 seek($fh, $tag->{offset} + $offset + $obj->_root->{file_offset}, SEEK_SET);
743 print( $fh substr($tag->{content}, $offset + $self->{bucket_size} ) );
251dfd0e 744 print( $fh chr(0) x $self->{bucket_size} );
d0b74c17 745
ab0e4957 746 return 1;
386bab6c 747 }
beac1dff 748 return;
ab0e4957 749}
750
912d50b1 751sub bucket_exists {
beac1dff 752 ##
753 # Check existence of single key given tag and MD5 digested key.
754 ##
755 my $self = shift;
756 my ($obj, $tag, $md5) = @_;
912d50b1 757
9a187d8c 758 my ($subloc, $offset, $size) = $self->_find_in_buckets( $tag, $md5 );
d5d7c51d 759 return $subloc && 1;
912d50b1 760}
761
6736c116 762sub find_bucket_list {
beac1dff 763 ##
764 # Locate offset for bucket list, given digested key
765 ##
766 my $self = shift;
d0b74c17 767 my ($obj, $md5, $args) = @_;
768 $args = {} unless $args;
769
beac1dff 770 ##
771 # Locate offset for bucket list using digest index system
772 ##
e5fc7e69 773 my $tag = $self->load_tag($obj, $obj->_base_offset)
d5d7c51d 774 or $obj->_throw_error( "INTERNAL ERROR - Cannot find tag" );
d0b74c17 775
e5fc7e69 776 my $ch = 0;
8db25060 777 while ($tag->{signature} ne SIG_BLIST) {
d0b74c17 778 my $num = ord substr($md5, $ch, 1);
779
780 my $ref_loc = $tag->{offset} + ($num * $self->{long_size});
781 $tag = $self->index_lookup( $obj, $tag, $num );
782
783 if (!$tag) {
29b01632 784 return if !$args->{create};
d0b74c17 785
16d1ad9b 786 my $loc = $self->_request_space(
787 $obj, $self->tag_size( $self->{bucket_list_size} ),
788 );
789
7b1e1aa1 790 my $fh = $obj->_fh;
791 seek($fh, $ref_loc + $obj->_root->{file_offset}, SEEK_SET);
16d1ad9b 792 print( $fh pack($self->{long_pack}, $loc) );
d0b74c17 793
9e4f83a0 794 $tag = $self->write_tag(
16d1ad9b 795 $obj, $loc, SIG_BLIST,
f37c15ab 796 chr(0)x$self->{bucket_list_size},
d5d7c51d 797 );
798
799 $tag->{ref_loc} = $ref_loc;
800 $tag->{ch} = $ch;
801
802 last;
d0b74c17 803 }
804
16d1ad9b 805 $tag->{ch} = $ch++;
d0b74c17 806 $tag->{ref_loc} = $ref_loc;
beac1dff 807 }
d0b74c17 808
beac1dff 809 return $tag;
6736c116 810}
811
d0b74c17 812sub index_lookup {
813 ##
814 # Given index tag, lookup single entry in index and return .
815 ##
816 my $self = shift;
817 my ($obj, $tag, $index) = @_;
818
819 my $location = unpack(
820 $self->{long_pack},
821 substr(
822 $tag->{content},
823 $index * $self->{long_size},
824 $self->{long_size},
825 ),
826 );
827
828 if (!$location) { return; }
829
830 return $self->load_tag( $obj, $location );
831}
832
6736c116 833sub traverse_index {
beac1dff 834 ##
835 # Scan index and recursively step into deeper levels, looking for next key.
836 ##
6736c116 837 my $self = shift;
838 my ($obj, $offset, $ch, $force_return_next) = @_;
d0b74c17 839
beac1dff 840 my $tag = $self->load_tag($obj, $offset );
6736c116 841
842 my $fh = $obj->_fh;
d0b74c17 843
8db25060 844 if ($tag->{signature} ne SIG_BLIST) {
beac1dff 845 my $content = $tag->{content};
e5fc7e69 846 my $start = $obj->{return_next} ? 0 : ord(substr($obj->{prev_md5}, $ch, 1));
d0b74c17 847
d5d7c51d 848 for (my $idx = $start; $idx < (2**8); $idx++) {
e5fc7e69 849 my $subloc = unpack(
850 $self->{long_pack},
e06824f8 851 substr(
852 $content,
853 $idx * $self->{long_size},
854 $self->{long_size},
855 ),
e5fc7e69 856 );
857
beac1dff 858 if ($subloc) {
e5fc7e69 859 my $result = $self->traverse_index(
860 $obj, $subloc, $ch + 1, $force_return_next,
861 );
862
beac1dff 863 if (defined($result)) { return $result; }
864 }
865 } # index loop
d0b74c17 866
beac1dff 867 $obj->{return_next} = 1;
868 } # tag is an index
d0b74c17 869
e5fc7e69 870 else {
beac1dff 871 my $keys = $tag->{content};
872 if ($force_return_next) { $obj->{return_next} = 1; }
d0b74c17 873
beac1dff 874 ##
875 # Iterate through buckets, looking for a key match
876 ##
8db25060 877 for (my $i = 0; $i < $self->{max_buckets}; $i++) {
9cec1360 878 my ($key, $subloc) = $self->_get_key_subloc( $keys, $i );
d0b74c17 879
8db25060 880 # End of bucket list -- return to outer loop
beac1dff 881 if (!$subloc) {
beac1dff 882 $obj->{return_next} = 1;
883 last;
884 }
8db25060 885 # Located previous key -- return next one found
beac1dff 886 elsif ($key eq $obj->{prev_md5}) {
beac1dff 887 $obj->{return_next} = 1;
888 next;
889 }
8db25060 890 # Seek to bucket location and skip over signature
beac1dff 891 elsif ($obj->{return_next}) {
8db25060 892 seek($fh, $subloc + $obj->_root->{file_offset}, SEEK_SET);
d0b74c17 893
beac1dff 894 # Skip over value to get to plain key
8db25060 895 my $sig;
896 read( $fh, $sig, SIG_SIZE );
897
beac1dff 898 my $size;
e5fc7e69 899 read( $fh, $size, $self->{data_size});
900 $size = unpack($self->{data_pack}, $size);
beac1dff 901 if ($size) { seek($fh, $size, SEEK_CUR); }
d0b74c17 902
beac1dff 903 # Read in plain key and return as scalar
beac1dff 904 my $plain_key;
e5fc7e69 905 read( $fh, $size, $self->{data_size});
906 $size = unpack($self->{data_pack}, $size);
beac1dff 907 if ($size) { read( $fh, $plain_key, $size); }
d0b74c17 908
beac1dff 909 return $plain_key;
910 }
8db25060 911 }
d0b74c17 912
beac1dff 913 $obj->{return_next} = 1;
914 } # tag is a bucket list
d0b74c17 915
beac1dff 916 return;
6736c116 917}
918
919sub get_next_key {
beac1dff 920 ##
921 # Locate next key, given digested previous one
922 ##
6736c116 923 my $self = shift;
924 my ($obj) = @_;
d0b74c17 925
beac1dff 926 $obj->{prev_md5} = $_[1] ? $_[1] : undef;
927 $obj->{return_next} = 0;
d0b74c17 928
beac1dff 929 ##
930 # If the previous key was not specifed, start at the top and
931 # return the first one found.
932 ##
933 if (!$obj->{prev_md5}) {
934 $obj->{prev_md5} = chr(0) x $self->{hash_size};
935 $obj->{return_next} = 1;
936 }
d0b74c17 937
beac1dff 938 return $self->traverse_index( $obj, $obj->_base_offset, 0 );
6736c116 939}
940
75be6413 941# Utilities
942
9cec1360 943sub _get_key_subloc {
75be6413 944 my $self = shift;
945 my ($keys, $idx) = @_;
946
6ed2f3df 947 my ($key, $subloc, $size) = unpack(
948 "a$self->{hash_size} $self->{long_pack} $self->{long_pack}",
75be6413 949 substr(
950 $keys,
9cec1360 951 ($idx * $self->{bucket_size}),
952 $self->{bucket_size},
75be6413 953 ),
954 );
955
6ed2f3df 956 return ($key, $subloc, $size);
75be6413 957}
958
d608b06e 959sub _find_in_buckets {
960 my $self = shift;
961 my ($tag, $md5) = @_;
962
963 BUCKET:
964 for ( my $i = 0; $i < $self->{max_buckets}; $i++ ) {
9a187d8c 965 my ($key, $subloc, $size) = $self->_get_key_subloc(
966 $tag->{content}, $i,
967 );
d608b06e 968
9a187d8c 969 return ($subloc, $i * $self->{bucket_size}, $size) unless $subloc;
d608b06e 970
971 next BUCKET if $key ne $md5;
972
9a187d8c 973 return ($subloc, $i * $self->{bucket_size}, $size);
d608b06e 974 }
975
976 return;
977}
978
7b1e1aa1 979#sub _print_at {
980# my $self = shift;
981# my ($obj, $spot, $data) = @_;
982#
983# my $fh = $obj->_fh;
984# seek( $fh, $spot, SEEK_SET );
985# print( $fh $data );
986#
987# return;
988#}
989
994ccd8e 990sub _request_space {
991 my $self = shift;
992 my ($obj, $size) = @_;
993
994 my $loc = $obj->_root->{end};
c9ec091a 995 $obj->_root->{end} += $size;
994ccd8e 996
997 return $loc;
998}
999
1000sub _release_space {
1001 my $self = shift;
1002 my ($obj, $size, $loc) = @_;
1003
7b1e1aa1 1004 my $next_loc = 0;
1005
1006 my $fh = $obj->_fh;
1007 seek( $fh, $loc + $obj->_root->{file_offset}, SEEK_SET );
1008 print( $fh SIG_FREE
1009 . pack($self->{long_pack}, $size )
1010 . pack($self->{long_pack}, $next_loc )
1011 );
1012
994ccd8e 1013 return;
1014}
1015
a20d9a3f 10161;
1017__END__
d5d7c51d 1018
1019# This will be added in later, after more refactoring is done. This is an early
1020# attempt at refactoring on the physical level instead of the virtual level.
1021sub _read_at {
1022 my $self = shift;
1023 my ($obj, $spot, $amount, $unpack) = @_;
1024
1025 my $fh = $obj->_fh;
1026 seek( $fh, $spot + $obj->_root->{file_offset}, SEEK_SET );
1027
1028 my $buffer;
1029 my $bytes_read = read( $fh, $buffer, $amount );
1030
1031 if ( $unpack ) {
1032 $buffer = unpack( $unpack, $buffer );
1033 }
1034
1035 if ( wantarray ) {
1036 return ($buffer, $bytes_read);
1037 }
1038 else {
1039 return $buffer;
1040 }
1041}