Converted as many max-buckets foreach loops to _find_in_buckets as possible ... 3...
[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' }
14sub SIG_SCALAR () { 'S' }
15sub SIG_NULL () { 'N' }
16sub SIG_DATA () { 'D' }
17sub SIG_INDEX () { 'I' }
18sub SIG_BLIST () { 'B' }
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};
28 $self->{bucket_size} = $self->{hash_size} + $self->{long_size};
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
54 # key/value. Upgrading this is possible (see above) but probably not necessary.
55 # If you need more than 4 GB for a single key or value, this module is really
56 # 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 ##
91 # Maximum number of buckets per list before another level of indexing is done.
92 # Increase this value for slightly greater speed, but larger database files.
93 # DO NOT decrease this value below 16, due to risk of recursive reindex overrun.
94 ##
612969fb 95 max_buckets => 16,
96 }, $class;
97
251dfd0e 98 $self->precalc_sizes;
612969fb 99
100 return $self;
1bf65be7 101}
102
70b55428 103sub setup_fh {
104 my $self = shift;
105 my ($obj) = @_;
106
107 $self->open( $obj ) if !defined $obj->_fh;
108
673464d9 109 #XXX We have to make sure we don't mess up when autoflush isn't turned on
70b55428 110 unless ( $obj->_root->{inode} ) {
111 my @stats = stat($obj->_fh);
112 $obj->_root->{inode} = $stats[1];
113 $obj->_root->{end} = $stats[7];
114 }
115
116 return 1;
117}
118
a20d9a3f 119sub open {
20f7b20c 120 ##
121 # Open a fh to the database, create if nonexistent.
122 # Make sure file signature matches DBM::Deep spec.
123 ##
a20d9a3f 124 my $self = shift;
70b55428 125 my ($obj) = @_;
a20d9a3f 126
3d1b8be9 127 if (defined($obj->_fh)) { $self->close_fh( $obj ); }
20f7b20c 128
673464d9 129 # Theoretically, adding O_BINARY should remove the need for the binmode
130 # Of course, testing it is going to be ... interesting.
131 my $flags = O_RDWR | O_CREAT | O_BINARY;
a20d9a3f 132
673464d9 133 my $fh;
134 sysopen( $fh, $obj->_root->{file}, $flags )
135 or $obj->_throw_error("Cannot sysopen file: " . $obj->_root->{file} . ": $!");
136 $obj->_root->{fh} = $fh;
a20d9a3f 137
138 #XXX Can we remove this by using the right sysopen() flags?
139 # Maybe ... q.v. above
140 binmode $fh; # for win32
141
cd59cad8 142 if ($obj->_root->{autoflush}) {
a20d9a3f 143 my $old = select $fh;
144 $|=1;
145 select $old;
146 }
20f7b20c 147
cd59cad8 148 seek($fh, 0 + $obj->_root->{file_offset}, SEEK_SET);
a20d9a3f 149
150 my $signature;
8db25060 151 my $bytes_read = read( $fh, $signature, length(SIG_FILE));
20f7b20c 152
a20d9a3f 153 ##
154 # File is empty -- write signature and master index
155 ##
156 if (!$bytes_read) {
cd59cad8 157 seek($fh, 0 + $obj->_root->{file_offset}, SEEK_SET);
8db25060 158 print( $fh SIG_FILE);
d0b74c17 159
ec1bce6b 160 $self->create_tag($obj, $obj->_base_offset, $obj->_type, chr(0) x $self->{index_size});
a20d9a3f 161
a20d9a3f 162 # Flush the filehandle
163 my $old_fh = select $fh;
164 my $old_af = $|; $| = 1; $| = $old_af;
165 select $old_fh;
166
a20d9a3f 167 return 1;
168 }
20f7b20c 169
a20d9a3f 170 ##
171 # Check signature was valid
172 ##
8db25060 173 unless ($signature eq SIG_FILE) {
3d1b8be9 174 $self->close_fh( $obj );
e5fc7e69 175 $obj->_throw_error("Signature not found -- file is not a Deep DB");
a20d9a3f 176 }
177
a20d9a3f 178 ##
179 # Get our type from master index signature
180 ##
e5fc7e69 181 my $tag = $self->load_tag($obj, $obj->_base_offset)
182 or $obj->_throw_error("Corrupted file, no master index record");
d0b74c17 183
e5fc7e69 184 unless ($obj->{type} eq $tag->{signature}) {
185 $obj->_throw_error("File type mismatch");
a20d9a3f 186 }
20f7b20c 187
d0b74c17 188#XXX We probably also want to store the hash algorithm name and not assume anything
189#XXX The cool thing would be to allow a different hashing algorithm at every level
190
a20d9a3f 191 return 1;
192}
193
3d1b8be9 194sub close_fh {
cd59cad8 195 my $self = shift;
a21f2d90 196 my ($obj) = @_;
cd59cad8 197
198 if ( my $fh = $obj->_root->{fh} ) {
199 close $fh;
200 }
201 $obj->_root->{fh} = undef;
202
203 return 1;
204}
205
d4b1166e 206sub create_tag {
20f7b20c 207 ##
208 # Given offset, signature and content, create tag and write to disk
209 ##
d4b1166e 210 my $self = shift;
20f7b20c 211 my ($obj, $offset, $sig, $content) = @_;
212 my $size = length($content);
213
d4b1166e 214 my $fh = $obj->_fh;
215
20f7b20c 216 seek($fh, $offset + $obj->_root->{file_offset}, SEEK_SET);
251dfd0e 217 print( $fh $sig . pack($self->{data_pack}, $size) . $content );
20f7b20c 218
219 if ($offset == $obj->_root->{end}) {
8db25060 220 $obj->_root->{end} += SIG_SIZE + $self->{data_size} + $size;
20f7b20c 221 }
222
223 return {
224 signature => $sig,
225 size => $size,
8db25060 226 offset => $offset + SIG_SIZE + $self->{data_size},
20f7b20c 227 content => $content
228 };
d4b1166e 229}
230
231sub load_tag {
20f7b20c 232 ##
233 # Given offset, load single tag and return signature, size and data
234 ##
d4b1166e 235 my $self = shift;
20f7b20c 236 my ($obj, $offset) = @_;
237
d4b1166e 238 my $fh = $obj->_fh;
239
20f7b20c 240 seek($fh, $offset + $obj->_root->{file_offset}, SEEK_SET);
e5fc7e69 241
75be6413 242 #XXX I'm not sure this check will work if autoflush isn't enabled ...
e5fc7e69 243 return if eof $fh;
20f7b20c 244
d4b1166e 245 my $b;
8db25060 246 read( $fh, $b, SIG_SIZE + $self->{data_size} );
251dfd0e 247 my ($sig, $size) = unpack( "A $self->{data_pack}", $b );
20f7b20c 248
249 my $buffer;
250 read( $fh, $buffer, $size);
251
252 return {
253 signature => $sig,
254 size => $size,
8db25060 255 offset => $offset + SIG_SIZE + $self->{data_size},
20f7b20c 256 content => $buffer
257 };
d4b1166e 258}
259
20f7b20c 260sub add_bucket {
261 ##
262 # Adds one key/value pair to bucket list, given offset, MD5 digest of key,
263 # plain (undigested) key and value.
264 ##
d4b1166e 265 my $self = shift;
20f7b20c 266 my ($obj, $tag, $md5, $plain_key, $value) = @_;
75be6413 267
20f7b20c 268 my $location = 0;
269 my $result = 2;
270
271 my $root = $obj->_root;
272
273 my $is_dbm_deep = eval { local $SIG{'__DIE__'}; $value->isa( 'DBM::Deep' ) };
274 my $internal_ref = $is_dbm_deep && ($value->_root eq $root);
275
276 my $fh = $obj->_fh;
277
386bab6c 278 my ($subloc, $offset) = $self->_find_in_buckets( $tag, $md5 );
75be6413 279
386bab6c 280 # Updating a known md5
281 if ( $subloc ) {
282 $result = 1;
20f7b20c 283
386bab6c 284 seek($fh, $subloc + SIG_SIZE + $root->{file_offset}, SEEK_SET);
285 my $size;
286 read( $fh, $size, $self->{data_size});
287 $size = unpack($self->{data_pack}, $size);
20f7b20c 288
386bab6c 289 ##
290 # If value is a hash, array, or raw value with equal or less size, we can
291 # reuse the same content area of the database. Otherwise, we have to create
292 # a new content area at the EOF.
293 ##
294 my $actual_length;
295 if ( $internal_ref ) {
296 $actual_length = $self->{long_size};
297 }
298 else {
299 my $r = Scalar::Util::reftype( $value ) || '';
300 if ( $r eq 'HASH' || $r eq 'ARRAY' ) {
301 $actual_length = $self->{index_size};
302
303 # if autobless is enabled, must also take into consideration
304 # the class name, as it is stored along with key/value.
305 if ( $root->{autobless} ) {
306 my $value_class = Scalar::Util::blessed($value);
307 if ( defined $value_class && !$value->isa('DBM::Deep') ) {
308 $actual_length += length($value_class);
8db25060 309 }
75be6413 310 }
20f7b20c 311 }
386bab6c 312 else { $actual_length = length($value); }
75be6413 313 }
20f7b20c 314
386bab6c 315 if ($actual_length <= $size) {
316 $location = $subloc;
20f7b20c 317 }
75be6413 318 else {
319 $location = $root->{end};
386bab6c 320 seek(
321 $fh,
322 $tag->{offset} + $offset + $self->{hash_size} + $root->{file_offset},
323 SEEK_SET,
324 );
325 print( $fh pack($self->{long_pack}, $location) );
75be6413 326 }
75be6413 327 }
386bab6c 328 # Adding a new md5
329 elsif ( defined $offset ) {
330 $result = 2;
331 $location = $root->{end};
332
333 seek( $fh, $tag->{offset} + $offset + $root->{file_offset}, SEEK_SET );
334 print( $fh $md5 . pack($self->{long_pack}, $location) );
335 }
336 # If bucket didn't fit into list, split into a new index level
337 else {
338 $self->split_index( $obj, $md5, $tag );
339
340 $location = $root->{end};
341 }
20f7b20c 342
343 ##
344 # Seek to content area and store signature, value and plaintext key
345 ##
346 if ($location) {
20f7b20c 347 seek($fh, $location + $root->{file_offset}, SEEK_SET);
348
349 ##
8db25060 350 # Write signature based on content type, set content length and write
351 # actual value.
20f7b20c 352 ##
353 my $r = Scalar::Util::reftype($value) || '';
8db25060 354 my $content_length;
355 if ( $internal_ref ) {
356 print( $fh SIG_INTERNAL );
357 print( $fh pack($self->{data_pack}, $self->{long_size}) );
358 print( $fh pack($self->{long_pack}, $value->_base_offset) );
359 $content_length = $self->{long_size};
20f7b20c 360 }
361 else {
8db25060 362 if ($r eq 'HASH') {
363 print( $fh SIG_HASH );
364 print( $fh pack($self->{data_pack}, $self->{index_size}) . chr(0) x $self->{index_size} );
365 $content_length = $self->{index_size};
366 }
367 elsif ($r eq 'ARRAY') {
368 print( $fh SIG_ARRAY );
369 print( $fh pack($self->{data_pack}, $self->{index_size}) . chr(0) x $self->{index_size} );
370 $content_length = $self->{index_size};
371 }
372 elsif (!defined($value)) {
373 print( $fh SIG_NULL );
374 print( $fh pack($self->{data_pack}, 0) );
375 $content_length = 0;
376 }
377 else {
378 print( $fh SIG_DATA );
379 print( $fh pack($self->{data_pack}, length($value)) . $value );
380 $content_length = length($value);
381 }
20f7b20c 382 }
383
384 ##
385 # Plain key is stored AFTER value, as keys are typically fetched less often.
386 ##
251dfd0e 387 print( $fh pack($self->{data_pack}, length($plain_key)) . $plain_key );
20f7b20c 388
389 ##
390 # If value is blessed, preserve class name
391 ##
392 if ( $root->{autobless} ) {
393 my $value_class = Scalar::Util::blessed($value);
8db25060 394 if ( defined $value_class && !$value->isa( 'DBM::Deep' ) ) {
20f7b20c 395 ##
396 # Blessed ref -- will restore later
397 ##
398 print( $fh chr(1) );
251dfd0e 399 print( $fh pack($self->{data_pack}, length($value_class)) . $value_class );
20f7b20c 400 $content_length += 1;
251dfd0e 401 $content_length += $self->{data_size} + length($value_class);
20f7b20c 402 }
403 else {
404 print( $fh chr(0) );
405 $content_length += 1;
406 }
407 }
408
409 ##
410 # If this is a new content area, advance EOF counter
411 ##
412 if ($location == $root->{end}) {
8db25060 413 $root->{end} += SIG_SIZE;
251dfd0e 414 $root->{end} += $self->{data_size} + $content_length;
415 $root->{end} += $self->{data_size} + length($plain_key);
20f7b20c 416 }
417
418 ##
419 # If content is a hash or array, create new child DBM::Deep object and
420 # pass each key or element to it.
421 ##
8db25060 422 if ( ! $internal_ref ) {
423 if ($r eq 'HASH') {
424 my $branch = DBM::Deep->new(
425 type => DBM::Deep->TYPE_HASH,
426 base_offset => $location,
427 root => $root,
428 );
429 foreach my $key (keys %{$value}) {
430 $branch->STORE( $key, $value->{$key} );
431 }
20f7b20c 432 }
8db25060 433 elsif ($r eq 'ARRAY') {
434 my $branch = DBM::Deep->new(
435 type => DBM::Deep->TYPE_ARRAY,
436 base_offset => $location,
437 root => $root,
438 );
439 my $index = 0;
440 foreach my $element (@{$value}) {
441 $branch->STORE( $index, $element );
442 $index++;
443 }
20f7b20c 444 }
445 }
446
447 return $result;
448 }
d4b1166e 449
e5fc7e69 450 $obj->_throw_error("Fatal error: indexing failed -- possibly due to corruption in file");
d4b1166e 451}
452
75be6413 453sub split_index {
454 my $self = shift;
455 my ($obj, $md5, $tag) = @_;
456
457 my $fh = $obj->_fh;
458 my $root = $obj->_root;
459 my $keys = $tag->{content};
460
461 seek($fh, $tag->{ref_loc} + $root->{file_offset}, SEEK_SET);
462 print( $fh pack($self->{long_pack}, $root->{end}) );
463
464 my $index_tag = $self->create_tag(
465 $obj,
466 $root->{end},
8db25060 467 SIG_INDEX,
75be6413 468 chr(0) x $self->{index_size},
469 );
470
471 my @offsets = ();
472
473 $keys .= $md5 . pack($self->{long_pack}, 0);
474
475 BUCKET:
476 for (my $i = 0; $i <= $self->{max_buckets}; $i++) {
9cec1360 477 my ($key, $old_subloc) = $self->_get_key_subloc( $keys, $i );
75be6413 478
479 next BUCKET unless $key;
480
75be6413 481 my $num = ord(substr($key, $tag->{ch} + 1, 1));
482
483 if ($offsets[$num]) {
8db25060 484 my $offset = $offsets[$num] + SIG_SIZE + $self->{data_size};
75be6413 485 seek($fh, $offset + $root->{file_offset}, SEEK_SET);
486 my $subkeys;
487 read( $fh, $subkeys, $self->{bucket_list_size});
488
489 for (my $k=0; $k<$self->{max_buckets}; $k++) {
9cec1360 490 my ($temp, $subloc) = $self->_get_key_subloc( $subkeys, $k );
75be6413 491
492 if (!$subloc) {
493 seek($fh, $offset + ($k * $self->{bucket_size}) + $root->{file_offset}, SEEK_SET);
494 print( $fh $key . pack($self->{long_pack}, $old_subloc || $root->{end}) );
495 last;
496 }
497 } # k loop
498 }
499 else {
500 $offsets[$num] = $root->{end};
501 seek($fh, $index_tag->{offset} + ($num * $self->{long_size}) + $root->{file_offset}, SEEK_SET);
502 print( $fh pack($self->{long_pack}, $root->{end}) );
503
8db25060 504 my $blist_tag = $self->create_tag($obj, $root->{end}, SIG_BLIST, chr(0) x $self->{bucket_list_size});
75be6413 505
506 seek($fh, $blist_tag->{offset} + $root->{file_offset}, SEEK_SET);
507 print( $fh $key . pack($self->{long_pack}, $old_subloc || $root->{end}) );
508 }
509 } # i loop
510
511 return;
512}
513
8db25060 514sub read_from_loc {
515 my $self = shift;
516 my ($obj, $subloc) = @_;
517
518 my $fh = $obj->_fh;
519
520 ##
521 # Found match -- seek to offset and read signature
522 ##
523 my $signature;
524 seek($fh, $subloc + $obj->_root->{file_offset}, SEEK_SET);
525 read( $fh, $signature, SIG_SIZE);
526
527 ##
528 # If value is a hash or array, return new DBM::Deep object with correct offset
529 ##
530 if (($signature eq SIG_HASH) || ($signature eq SIG_ARRAY)) {
531 my $obj = DBM::Deep->new(
532 type => $signature,
533 base_offset => $subloc,
534 root => $obj->_root,
535 );
536
537 if ($obj->_root->{autobless}) {
538 ##
539 # Skip over value and plain key to see if object needs
540 # to be re-blessed
541 ##
542 seek($fh, $self->{data_size} + $self->{index_size}, SEEK_CUR);
543
544 my $size;
545 read( $fh, $size, $self->{data_size}); $size = unpack($self->{data_pack}, $size);
546 if ($size) { seek($fh, $size, SEEK_CUR); }
547
548 my $bless_bit;
549 read( $fh, $bless_bit, 1);
550 if (ord($bless_bit)) {
551 ##
552 # Yes, object needs to be re-blessed
553 ##
554 my $class_name;
555 read( $fh, $size, $self->{data_size}); $size = unpack($self->{data_pack}, $size);
556 if ($size) { read( $fh, $class_name, $size); }
557 if ($class_name) { $obj = bless( $obj, $class_name ); }
558 }
559 }
560
561 return $obj;
562 }
563 elsif ( $signature eq SIG_INTERNAL ) {
564 my $size;
565 read( $fh, $size, $self->{data_size});
566 $size = unpack($self->{data_pack}, $size);
567
568 if ( $size ) {
569 my $new_loc;
570 read( $fh, $new_loc, $size );
571 $new_loc = unpack( $self->{long_pack}, $new_loc );
572
573 return $self->read_from_loc( $obj, $new_loc );
574 }
575 else {
576 return;
577 }
578 }
579 ##
580 # Otherwise return actual value
581 ##
582 elsif ($signature eq SIG_DATA) {
583 my $size;
584 read( $fh, $size, $self->{data_size});
585 $size = unpack($self->{data_pack}, $size);
586
587 my $value = '';
588 if ($size) { read( $fh, $value, $size); }
589 return $value;
590 }
591
592 ##
593 # Key exists, but content is null
594 ##
595 return;
596}
597
9020ee8c 598sub get_bucket_value {
beac1dff 599 ##
600 # Fetch single value given tag and MD5 digested key.
601 ##
602 my $self = shift;
603 my ($obj, $tag, $md5) = @_;
9020ee8c 604
386bab6c 605 my ($subloc, $offset) = $self->_find_in_buckets( $tag, $md5 );
606 if ( $subloc ) {
8db25060 607 return $self->read_from_loc( $obj, $subloc );
386bab6c 608 }
beac1dff 609 return;
9020ee8c 610}
ab0e4957 611
612sub delete_bucket {
beac1dff 613 ##
614 # Delete single key/value pair given tag and MD5 digested key.
615 ##
616 my $self = shift;
617 my ($obj, $tag, $md5) = @_;
ab0e4957 618
386bab6c 619 my ($subloc, $offset) = $self->_find_in_buckets( $tag, $md5 );
620 if ( $subloc ) {
621 my $fh = $obj->_fh;
622 seek($fh, $tag->{offset} + $offset + $obj->_root->{file_offset}, SEEK_SET);
623 print( $fh substr($tag->{content}, $offset + $self->{bucket_size} ) );
251dfd0e 624 print( $fh chr(0) x $self->{bucket_size} );
d0b74c17 625
ab0e4957 626 return 1;
386bab6c 627 }
beac1dff 628 return;
ab0e4957 629}
630
912d50b1 631sub bucket_exists {
beac1dff 632 ##
633 # Check existence of single key given tag and MD5 digested key.
634 ##
635 my $self = shift;
636 my ($obj, $tag, $md5) = @_;
912d50b1 637
386bab6c 638 my ($subloc, $offset) = $self->_find_in_buckets( $tag, $md5 );
639 if ( $subloc ) {
912d50b1 640 return 1;
386bab6c 641 }
beac1dff 642 return;
912d50b1 643}
644
6736c116 645sub find_bucket_list {
beac1dff 646 ##
647 # Locate offset for bucket list, given digested key
648 ##
649 my $self = shift;
d0b74c17 650 my ($obj, $md5, $args) = @_;
651 $args = {} unless $args;
652
beac1dff 653 ##
654 # Locate offset for bucket list using digest index system
655 ##
e5fc7e69 656 my $tag = $self->load_tag($obj, $obj->_base_offset)
657 or $self->_throw_error( "INTERNAL ERROR - Cannot find tag" );
d0b74c17 658
e5fc7e69 659 my $ch = 0;
8db25060 660 while ($tag->{signature} ne SIG_BLIST) {
d0b74c17 661 my $num = ord substr($md5, $ch, 1);
662
663 my $ref_loc = $tag->{offset} + ($num * $self->{long_size});
664 $tag = $self->index_lookup( $obj, $tag, $num );
665
666 if (!$tag) {
667 if ( $args->{create} ) {
668 my $fh = $obj->_fh;
669 seek($fh, $ref_loc + $obj->_root->{file_offset}, SEEK_SET);
670 print( $fh pack($self->{long_pack}, $obj->_root->{end}) );
671
672 $tag = $self->create_tag(
673 $obj, $obj->_root->{end},
8db25060 674 SIG_BLIST,
d0b74c17 675 chr(0) x $self->{bucket_list_size},
676 );
677
678 $tag->{ref_loc} = $ref_loc;
679 $tag->{ch} = $ch;
680
681 last;
682 }
683 else {
684 return;
685 }
686 }
687
688 $tag->{ch} = $ch;
689 $tag->{ref_loc} = $ref_loc;
690
beac1dff 691 $ch++;
692 }
d0b74c17 693
beac1dff 694 return $tag;
6736c116 695}
696
d0b74c17 697sub index_lookup {
698 ##
699 # Given index tag, lookup single entry in index and return .
700 ##
701 my $self = shift;
702 my ($obj, $tag, $index) = @_;
703
704 my $location = unpack(
705 $self->{long_pack},
706 substr(
707 $tag->{content},
708 $index * $self->{long_size},
709 $self->{long_size},
710 ),
711 );
712
713 if (!$location) { return; }
714
715 return $self->load_tag( $obj, $location );
716}
717
6736c116 718sub traverse_index {
beac1dff 719 ##
720 # Scan index and recursively step into deeper levels, looking for next key.
721 ##
6736c116 722 my $self = shift;
723 my ($obj, $offset, $ch, $force_return_next) = @_;
d0b74c17 724
beac1dff 725 my $tag = $self->load_tag($obj, $offset );
6736c116 726
727 my $fh = $obj->_fh;
d0b74c17 728
8db25060 729 if ($tag->{signature} ne SIG_BLIST) {
beac1dff 730 my $content = $tag->{content};
e5fc7e69 731 my $start = $obj->{return_next} ? 0 : ord(substr($obj->{prev_md5}, $ch, 1));
d0b74c17 732
beac1dff 733 for (my $index = $start; $index < 256; $index++) {
e5fc7e69 734 my $subloc = unpack(
735 $self->{long_pack},
736 substr($content, $index * $self->{long_size}, $self->{long_size}),
737 );
738
beac1dff 739 if ($subloc) {
e5fc7e69 740 my $result = $self->traverse_index(
741 $obj, $subloc, $ch + 1, $force_return_next,
742 );
743
beac1dff 744 if (defined($result)) { return $result; }
745 }
746 } # index loop
d0b74c17 747
beac1dff 748 $obj->{return_next} = 1;
749 } # tag is an index
d0b74c17 750
e5fc7e69 751 else {
beac1dff 752 my $keys = $tag->{content};
753 if ($force_return_next) { $obj->{return_next} = 1; }
d0b74c17 754
beac1dff 755 ##
756 # Iterate through buckets, looking for a key match
757 ##
8db25060 758 for (my $i = 0; $i < $self->{max_buckets}; $i++) {
9cec1360 759 my ($key, $subloc) = $self->_get_key_subloc( $keys, $i );
d0b74c17 760
8db25060 761 # End of bucket list -- return to outer loop
beac1dff 762 if (!$subloc) {
beac1dff 763 $obj->{return_next} = 1;
764 last;
765 }
8db25060 766 # Located previous key -- return next one found
beac1dff 767 elsif ($key eq $obj->{prev_md5}) {
beac1dff 768 $obj->{return_next} = 1;
769 next;
770 }
8db25060 771 # Seek to bucket location and skip over signature
beac1dff 772 elsif ($obj->{return_next}) {
8db25060 773 seek($fh, $subloc + $obj->_root->{file_offset}, SEEK_SET);
d0b74c17 774
beac1dff 775 # Skip over value to get to plain key
8db25060 776 my $sig;
777 read( $fh, $sig, SIG_SIZE );
778
beac1dff 779 my $size;
e5fc7e69 780 read( $fh, $size, $self->{data_size});
781 $size = unpack($self->{data_pack}, $size);
beac1dff 782 if ($size) { seek($fh, $size, SEEK_CUR); }
d0b74c17 783
beac1dff 784 # Read in plain key and return as scalar
beac1dff 785 my $plain_key;
e5fc7e69 786 read( $fh, $size, $self->{data_size});
787 $size = unpack($self->{data_pack}, $size);
beac1dff 788 if ($size) { read( $fh, $plain_key, $size); }
d0b74c17 789
beac1dff 790 return $plain_key;
791 }
8db25060 792 }
d0b74c17 793
beac1dff 794 $obj->{return_next} = 1;
795 } # tag is a bucket list
d0b74c17 796
beac1dff 797 return;
6736c116 798}
799
800sub get_next_key {
beac1dff 801 ##
802 # Locate next key, given digested previous one
803 ##
6736c116 804 my $self = shift;
805 my ($obj) = @_;
d0b74c17 806
beac1dff 807 $obj->{prev_md5} = $_[1] ? $_[1] : undef;
808 $obj->{return_next} = 0;
d0b74c17 809
beac1dff 810 ##
811 # If the previous key was not specifed, start at the top and
812 # return the first one found.
813 ##
814 if (!$obj->{prev_md5}) {
815 $obj->{prev_md5} = chr(0) x $self->{hash_size};
816 $obj->{return_next} = 1;
817 }
d0b74c17 818
beac1dff 819 return $self->traverse_index( $obj, $obj->_base_offset, 0 );
6736c116 820}
821
75be6413 822# Utilities
823
9cec1360 824sub _get_key_subloc {
75be6413 825 my $self = shift;
826 my ($keys, $idx) = @_;
827
9cec1360 828 my ($key, $subloc) = unpack(
829 "a$self->{hash_size} $self->{long_pack}",
75be6413 830 substr(
831 $keys,
9cec1360 832 ($idx * $self->{bucket_size}),
833 $self->{bucket_size},
75be6413 834 ),
835 );
836
9cec1360 837 return ($key, $subloc);
75be6413 838}
839
d608b06e 840sub _find_in_buckets {
841 my $self = shift;
842 my ($tag, $md5) = @_;
843
844 BUCKET:
845 for ( my $i = 0; $i < $self->{max_buckets}; $i++ ) {
846 my ($key, $subloc) = $self->_get_key_subloc( $tag->{content}, $i );
847
848 return ($subloc, $i * $self->{bucket_size}) unless $subloc;
849
850 next BUCKET if $key ne $md5;
851
852 return ($subloc, $i * $self->{bucket_size});
853 }
854
855 return;
856}
857
a20d9a3f 8581;
859__END__