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