Added version to the file header
[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
29b01632 307sub _length_needed {
308 my $self = shift;
f37c15ab 309 my ($obj, $value, $key) = @_;
29b01632 310
311 my $is_dbm_deep = eval {
312 local $SIG{'__DIE__'};
313 $value->isa( 'DBM::Deep' );
314 };
315
f37c15ab 316 my $len = SIG_SIZE + $self->{data_size}
317 + $self->{data_size} + length( $key );
29b01632 318
f37c15ab 319 if ( $is_dbm_deep && $value->_root eq $obj->_root ) {
320 return $len + $self->{long_size};
29b01632 321 }
322
323 my $r = Scalar::Util::reftype( $value ) || '';
9a187d8c 324 if ( $obj->_root->{autobless} ) {
325 # This is for the bit saying whether or not this thing is blessed.
326 $len += 1;
327 }
328
29b01632 329 unless ( $r eq 'HASH' || $r eq 'ARRAY' ) {
f37c15ab 330 if ( defined $value ) {
331 $len += length( $value );
332 }
333 return $len;
29b01632 334 }
335
f37c15ab 336 $len += $self->{index_size};
29b01632 337
338 # if autobless is enabled, must also take into consideration
f37c15ab 339 # the class name as it is stored after the key.
29b01632 340 if ( $obj->_root->{autobless} ) {
341 my $value_class = Scalar::Util::blessed($value);
f37c15ab 342 if ( defined $value_class && !$is_dbm_deep ) {
343 $len += $self->{data_size} + length($value_class);
29b01632 344 }
345 }
346
f37c15ab 347 return $len;
29b01632 348}
349
20f7b20c 350sub add_bucket {
351 ##
352 # Adds one key/value pair to bucket list, given offset, MD5 digest of key,
353 # plain (undigested) key and value.
354 ##
d4b1166e 355 my $self = shift;
20f7b20c 356 my ($obj, $tag, $md5, $plain_key, $value) = @_;
75be6413 357
eea0d863 358 # This verifies that only supported values will be stored.
359 {
360 my $r = Scalar::Util::reftype( $value );
361 last if !defined $r;
362
363 last if $r eq 'HASH';
364 last if $r eq 'ARRAY';
365
366 $obj->_throw_error(
367 "Storage of variables of type '$r' is not supported."
368 );
369 }
370
20f7b20c 371 my $location = 0;
372 my $result = 2;
373
374 my $root = $obj->_root;
f37c15ab 375 my $fh = $obj->_fh;
20f7b20c 376
f37c15ab 377 my $actual_length = $self->_length_needed( $obj, $value, $plain_key );
20f7b20c 378
9a187d8c 379 my ($subloc, $offset, $size) = $self->_find_in_buckets( $tag, $md5 );
75be6413 380
f9c33187 381# $self->_release_space( $obj, $size, $subloc );
386bab6c 382 # Updating a known md5
f9c33187 383#XXX This needs updating to use _release_space
386bab6c 384 if ( $subloc ) {
385 $result = 1;
20f7b20c 386
386bab6c 387 if ($actual_length <= $size) {
388 $location = $subloc;
20f7b20c 389 }
75be6413 390 else {
f37c15ab 391 $location = $self->_request_space( $obj, $actual_length );
386bab6c 392 seek(
393 $fh,
9a187d8c 394 $tag->{offset} + $offset
395 + $self->{hash_size} + $root->{file_offset},
386bab6c 396 SEEK_SET,
397 );
9a187d8c 398 print( $fh pack($self->{long_pack}, $location ) );
399 print( $fh pack($self->{long_pack}, $actual_length ) );
75be6413 400 }
75be6413 401 }
386bab6c 402 # Adding a new md5
403 elsif ( defined $offset ) {
f37c15ab 404 $location = $self->_request_space( $obj, $actual_length );
386bab6c 405
406 seek( $fh, $tag->{offset} + $offset + $root->{file_offset}, SEEK_SET );
9a187d8c 407 print( $fh $md5 . pack($self->{long_pack}, $location ) );
408 print( $fh pack($self->{long_pack}, $actual_length ) );
386bab6c 409 }
410 # If bucket didn't fit into list, split into a new index level
f9c33187 411 # split_index() will do the _request_space() call
386bab6c 412 else {
f9c33187 413 $location = $self->split_index( $obj, $md5, $tag );
386bab6c 414 }
20f7b20c 415
d5d7c51d 416 $self->write_value( $obj, $location, $plain_key, $value );
417
418 return $result;
419}
420
9d4fa373 421sub _get_tied {
422 my $item = shift;
423 my $r = Scalar::Util::reftype( $item ) || return;
424 if ( $r eq 'HASH' ) {
425 return tied(%$item);
426 }
427 elsif ( $r eq 'ARRAY' ) {
428 return tied(@$item);
429 }
430 else {
431 return;
432 };
433}
434
435sub _get_dbm_object {
436 my $item = shift;
437
438 my $obj = eval {
439 local $SIG{__DIE__};
440 if ($item->isa( 'DBM::Deep' )) {
441 return $item;
442 }
443 return;
444 };
445 return $obj if $obj;
446
447 my $r = Scalar::Util::reftype( $item ) || '';
448 if ( $r eq 'HASH' ) {
449 my $obj = eval {
450 local $SIG{__DIE__};
451 my $obj = tied(%$item);
452 if ($obj->isa( 'DBM::Deep' )) {
453 return $obj;
454 }
455 return;
456 };
457 return $obj if $obj;
458 }
459 elsif ( $r eq 'ARRAY' ) {
460 my $obj = eval {
461 local $SIG{__DIE__};
462 my $obj = tied(@$item);
463 if ($obj->isa( 'DBM::Deep' )) {
464 return $obj;
465 }
466 return;
467 };
468 return $obj if $obj;
469 }
470
471 return;
472}
473
d5d7c51d 474sub write_value {
475 my $self = shift;
476 my ($obj, $location, $key, $value) = @_;
477
478 my $fh = $obj->_fh;
479 my $root = $obj->_root;
480
9d4fa373 481 my $dbm_deep_obj = _get_dbm_object( $value );
482 if ( $dbm_deep_obj && $dbm_deep_obj->_root ne $obj->_root ) {
483 $obj->_throw_error( "Cannot cross-reference. Use export() instead" );
484 }
d5d7c51d 485
486 seek($fh, $location + $root->{file_offset}, SEEK_SET);
487
20f7b20c 488 ##
d5d7c51d 489 # Write signature based on content type, set content length and write
490 # actual value.
20f7b20c 491 ##
9d4fa373 492 my $r = Scalar::Util::reftype( $value ) || '';
493 if ( $dbm_deep_obj ) {
494 $self->write_tag( $obj, undef, SIG_INTERNAL,pack($self->{long_pack}, $dbm_deep_obj->_base_offset) );
f37c15ab 495 }
496 elsif ($r eq 'HASH') {
9d4fa373 497 if ( !$dbm_deep_obj && tied %{$value} ) {
019ab3a1 498 $obj->_throw_error( "Cannot store something that is tied" );
499 }
9e4f83a0 500 $self->write_tag( $obj, undef, SIG_HASH, chr(0)x$self->{index_size} );
f37c15ab 501 }
502 elsif ($r eq 'ARRAY') {
9d4fa373 503 if ( !$dbm_deep_obj && tied @{$value} ) {
019ab3a1 504 $obj->_throw_error( "Cannot store something that is tied" );
505 }
9e4f83a0 506 $self->write_tag( $obj, undef, SIG_ARRAY, chr(0)x$self->{index_size} );
f37c15ab 507 }
508 elsif (!defined($value)) {
9e4f83a0 509 $self->write_tag( $obj, undef, SIG_NULL, '' );
d5d7c51d 510 }
511 else {
9e4f83a0 512 $self->write_tag( $obj, undef, SIG_DATA, $value );
d5d7c51d 513 }
20f7b20c 514
d5d7c51d 515 ##
516 # Plain key is stored AFTER value, as keys are typically fetched less often.
517 ##
518 print( $fh pack($self->{data_pack}, length($key)) . $key );
20f7b20c 519
9a187d8c 520 # Internal references don't care about autobless
9d4fa373 521 return 1 if $dbm_deep_obj;
9a187d8c 522
d5d7c51d 523 ##
524 # If value is blessed, preserve class name
525 ##
526 if ( $root->{autobless} ) {
527 my $value_class = Scalar::Util::blessed($value);
9d4fa373 528 if ( defined $value_class && !$dbm_deep_obj ) {
d5d7c51d 529 print( $fh chr(1) );
530 print( $fh pack($self->{data_pack}, length($value_class)) . $value_class );
20f7b20c 531 }
d5d7c51d 532 else {
533 print( $fh chr(0) );
20f7b20c 534 }
d5d7c51d 535 }
20f7b20c 536
d5d7c51d 537 ##
d5d7c51d 538 # If content is a hash or array, create new child DBM::Deep object and
539 # pass each key or element to it.
540 ##
9d4fa373 541 if ($r eq 'HASH') {
542 my %x = %$value;
543 tie %$value, 'DBM::Deep', {
544 base_offset => $location,
545 root => $root,
546 };
547 %$value = %x;
548 }
549 elsif ($r eq 'ARRAY') {
550 my @x = @$value;
551 tie @$value, 'DBM::Deep', {
552 base_offset => $location,
553 root => $root,
554 };
555 @$value = @x;
20f7b20c 556 }
d4b1166e 557
d5d7c51d 558 return 1;
d4b1166e 559}
560
75be6413 561sub split_index {
562 my $self = shift;
563 my ($obj, $md5, $tag) = @_;
564
565 my $fh = $obj->_fh;
566 my $root = $obj->_root;
16d1ad9b 567
568 my $loc = $self->_request_space(
569 $obj, $self->tag_size( $self->{index_size} ),
570 );
571
7b1e1aa1 572 seek($fh, $tag->{ref_loc} + $root->{file_offset}, SEEK_SET);
16d1ad9b 573 print( $fh pack($self->{long_pack}, $loc) );
75be6413 574
9e4f83a0 575 my $index_tag = $self->write_tag(
16d1ad9b 576 $obj, $loc, SIG_INDEX,
f37c15ab 577 chr(0)x$self->{index_size},
75be6413 578 );
579
f9c33187 580 my $newtag_loc = $self->_request_space(
581 $obj, $self->tag_size( $self->{bucket_list_size} ),
582 );
75be6413 583
7b1e1aa1 584 my $keys = $tag->{content}
f9c33187 585 . $md5 . pack($self->{long_pack}, $newtag_loc)
586 . pack($self->{long_pack}, 0);
75be6413 587
f9c33187 588 my @newloc = ();
75be6413 589 BUCKET:
590 for (my $i = 0; $i <= $self->{max_buckets}; $i++) {
9a187d8c 591 my ($key, $old_subloc, $size) = $self->_get_key_subloc( $keys, $i );
75be6413 592
f9c33187 593 die "[INTERNAL ERROR]: No key in split_index()\n" unless $key;
594 die "[INTERNAL ERROR]: No subloc in split_index()\n" unless $old_subloc;
75be6413 595
75be6413 596 my $num = ord(substr($key, $tag->{ch} + 1, 1));
597
f9c33187 598 if ($newloc[$num]) {
599 seek($fh, $newloc[$num] + $root->{file_offset}, SEEK_SET);
75be6413 600 my $subkeys;
601 read( $fh, $subkeys, $self->{bucket_list_size});
602
f9c33187 603 # This is looking for the first empty spot
7b1e1aa1 604 my ($subloc, $offset, $size) = $self->_find_in_buckets(
f9c33187 605 { content => $subkeys }, '',
7b1e1aa1 606 );
75be6413 607
f9c33187 608 seek($fh, $newloc[$num] + $offset + $root->{file_offset}, SEEK_SET);
609 print( $fh $key . pack($self->{long_pack}, $old_subloc) );
7b1e1aa1 610
611 next;
75be6413 612 }
75be6413 613
7b1e1aa1 614 seek($fh, $index_tag->{offset} + ($num * $self->{long_size}) + $root->{file_offset}, SEEK_SET);
2603d86e 615
7b1e1aa1 616 my $loc = $self->_request_space(
617 $obj, $self->tag_size( $self->{bucket_list_size} ),
618 );
2603d86e 619
7b1e1aa1 620 print( $fh pack($self->{long_pack}, $loc) );
75be6413 621
7b1e1aa1 622 my $blist_tag = $self->write_tag(
623 $obj, $loc, SIG_BLIST,
624 chr(0)x$self->{bucket_list_size},
625 );
626
627 seek($fh, $blist_tag->{offset} + $root->{file_offset}, SEEK_SET);
f9c33187 628 print( $fh $key . pack($self->{long_pack}, $old_subloc) );
7b1e1aa1 629
f9c33187 630 $newloc[$num] = $blist_tag->{offset};
7b1e1aa1 631 }
632
633 $self->_release_space(
f9c33187 634 $obj, $self->tag_size( $self->{bucket_list_size} ),
7b1e1aa1 635 $tag->{offset} - SIG_SIZE - $self->{data_size},
636 );
75be6413 637
f9c33187 638 return $newtag_loc;
75be6413 639}
640
8db25060 641sub read_from_loc {
642 my $self = shift;
643 my ($obj, $subloc) = @_;
644
645 my $fh = $obj->_fh;
646
647 ##
648 # Found match -- seek to offset and read signature
649 ##
650 my $signature;
651 seek($fh, $subloc + $obj->_root->{file_offset}, SEEK_SET);
652 read( $fh, $signature, SIG_SIZE);
653
654 ##
655 # If value is a hash or array, return new DBM::Deep object with correct offset
656 ##
657 if (($signature eq SIG_HASH) || ($signature eq SIG_ARRAY)) {
685e40f1 658 my $new_obj = DBM::Deep->new({
8db25060 659 type => $signature,
660 base_offset => $subloc,
661 root => $obj->_root,
685e40f1 662 });
8db25060 663
685e40f1 664 if ($new_obj->_root->{autobless}) {
8db25060 665 ##
666 # Skip over value and plain key to see if object needs
667 # to be re-blessed
668 ##
669 seek($fh, $self->{data_size} + $self->{index_size}, SEEK_CUR);
670
671 my $size;
c6ea6b6c 672 read( $fh, $size, $self->{data_size});
673 $size = unpack($self->{data_pack}, $size);
8db25060 674 if ($size) { seek($fh, $size, SEEK_CUR); }
675
676 my $bless_bit;
677 read( $fh, $bless_bit, 1);
678 if (ord($bless_bit)) {
679 ##
680 # Yes, object needs to be re-blessed
681 ##
682 my $class_name;
c6ea6b6c 683 read( $fh, $size, $self->{data_size});
684 $size = unpack($self->{data_pack}, $size);
8db25060 685 if ($size) { read( $fh, $class_name, $size); }
685e40f1 686 if ($class_name) { $new_obj = bless( $new_obj, $class_name ); }
8db25060 687 }
688 }
689
685e40f1 690 return $new_obj;
8db25060 691 }
692 elsif ( $signature eq SIG_INTERNAL ) {
693 my $size;
694 read( $fh, $size, $self->{data_size});
695 $size = unpack($self->{data_pack}, $size);
696
697 if ( $size ) {
698 my $new_loc;
699 read( $fh, $new_loc, $size );
700 $new_loc = unpack( $self->{long_pack}, $new_loc );
701
702 return $self->read_from_loc( $obj, $new_loc );
703 }
704 else {
705 return;
706 }
707 }
708 ##
709 # Otherwise return actual value
710 ##
711 elsif ($signature eq SIG_DATA) {
712 my $size;
713 read( $fh, $size, $self->{data_size});
714 $size = unpack($self->{data_pack}, $size);
715
716 my $value = '';
717 if ($size) { read( $fh, $value, $size); }
718 return $value;
719 }
720
721 ##
722 # Key exists, but content is null
723 ##
724 return;
725}
726
9020ee8c 727sub get_bucket_value {
beac1dff 728 ##
729 # Fetch single value given tag and MD5 digested key.
730 ##
731 my $self = shift;
732 my ($obj, $tag, $md5) = @_;
9020ee8c 733
9a187d8c 734 my ($subloc, $offset, $size) = $self->_find_in_buckets( $tag, $md5 );
386bab6c 735 if ( $subloc ) {
8db25060 736 return $self->read_from_loc( $obj, $subloc );
386bab6c 737 }
beac1dff 738 return;
9020ee8c 739}
ab0e4957 740
741sub delete_bucket {
beac1dff 742 ##
743 # Delete single key/value pair given tag and MD5 digested key.
744 ##
745 my $self = shift;
746 my ($obj, $tag, $md5) = @_;
ab0e4957 747
9a187d8c 748 my ($subloc, $offset, $size) = $self->_find_in_buckets( $tag, $md5 );
f9c33187 749#XXX This needs _release_space()
386bab6c 750 if ( $subloc ) {
751 my $fh = $obj->_fh;
752 seek($fh, $tag->{offset} + $offset + $obj->_root->{file_offset}, SEEK_SET);
753 print( $fh substr($tag->{content}, $offset + $self->{bucket_size} ) );
251dfd0e 754 print( $fh chr(0) x $self->{bucket_size} );
d0b74c17 755
ab0e4957 756 return 1;
386bab6c 757 }
beac1dff 758 return;
ab0e4957 759}
760
912d50b1 761sub bucket_exists {
beac1dff 762 ##
763 # Check existence of single key given tag and MD5 digested key.
764 ##
765 my $self = shift;
766 my ($obj, $tag, $md5) = @_;
912d50b1 767
9a187d8c 768 my ($subloc, $offset, $size) = $self->_find_in_buckets( $tag, $md5 );
d5d7c51d 769 return $subloc && 1;
912d50b1 770}
771
6736c116 772sub find_bucket_list {
beac1dff 773 ##
774 # Locate offset for bucket list, given digested key
775 ##
776 my $self = shift;
d0b74c17 777 my ($obj, $md5, $args) = @_;
778 $args = {} unless $args;
779
beac1dff 780 ##
781 # Locate offset for bucket list using digest index system
782 ##
e5fc7e69 783 my $tag = $self->load_tag($obj, $obj->_base_offset)
d5d7c51d 784 or $obj->_throw_error( "INTERNAL ERROR - Cannot find tag" );
d0b74c17 785
e5fc7e69 786 my $ch = 0;
8db25060 787 while ($tag->{signature} ne SIG_BLIST) {
d0b74c17 788 my $num = ord substr($md5, $ch, 1);
789
790 my $ref_loc = $tag->{offset} + ($num * $self->{long_size});
791 $tag = $self->index_lookup( $obj, $tag, $num );
792
793 if (!$tag) {
29b01632 794 return if !$args->{create};
d0b74c17 795
16d1ad9b 796 my $loc = $self->_request_space(
797 $obj, $self->tag_size( $self->{bucket_list_size} ),
798 );
799
7b1e1aa1 800 my $fh = $obj->_fh;
801 seek($fh, $ref_loc + $obj->_root->{file_offset}, SEEK_SET);
16d1ad9b 802 print( $fh pack($self->{long_pack}, $loc) );
d0b74c17 803
9e4f83a0 804 $tag = $self->write_tag(
16d1ad9b 805 $obj, $loc, SIG_BLIST,
f37c15ab 806 chr(0)x$self->{bucket_list_size},
d5d7c51d 807 );
808
809 $tag->{ref_loc} = $ref_loc;
810 $tag->{ch} = $ch;
811
812 last;
d0b74c17 813 }
814
16d1ad9b 815 $tag->{ch} = $ch++;
d0b74c17 816 $tag->{ref_loc} = $ref_loc;
beac1dff 817 }
d0b74c17 818
beac1dff 819 return $tag;
6736c116 820}
821
d0b74c17 822sub index_lookup {
823 ##
824 # Given index tag, lookup single entry in index and return .
825 ##
826 my $self = shift;
827 my ($obj, $tag, $index) = @_;
828
829 my $location = unpack(
830 $self->{long_pack},
831 substr(
832 $tag->{content},
833 $index * $self->{long_size},
834 $self->{long_size},
835 ),
836 );
837
838 if (!$location) { return; }
839
840 return $self->load_tag( $obj, $location );
841}
842
6736c116 843sub traverse_index {
beac1dff 844 ##
845 # Scan index and recursively step into deeper levels, looking for next key.
846 ##
6736c116 847 my $self = shift;
848 my ($obj, $offset, $ch, $force_return_next) = @_;
d0b74c17 849
beac1dff 850 my $tag = $self->load_tag($obj, $offset );
6736c116 851
852 my $fh = $obj->_fh;
d0b74c17 853
8db25060 854 if ($tag->{signature} ne SIG_BLIST) {
beac1dff 855 my $content = $tag->{content};
e5fc7e69 856 my $start = $obj->{return_next} ? 0 : ord(substr($obj->{prev_md5}, $ch, 1));
d0b74c17 857
d5d7c51d 858 for (my $idx = $start; $idx < (2**8); $idx++) {
e5fc7e69 859 my $subloc = unpack(
860 $self->{long_pack},
e06824f8 861 substr(
862 $content,
863 $idx * $self->{long_size},
864 $self->{long_size},
865 ),
e5fc7e69 866 );
867
beac1dff 868 if ($subloc) {
e5fc7e69 869 my $result = $self->traverse_index(
870 $obj, $subloc, $ch + 1, $force_return_next,
871 );
872
beac1dff 873 if (defined($result)) { return $result; }
874 }
875 } # index loop
d0b74c17 876
beac1dff 877 $obj->{return_next} = 1;
878 } # tag is an index
d0b74c17 879
e5fc7e69 880 else {
beac1dff 881 my $keys = $tag->{content};
882 if ($force_return_next) { $obj->{return_next} = 1; }
d0b74c17 883
beac1dff 884 ##
885 # Iterate through buckets, looking for a key match
886 ##
8db25060 887 for (my $i = 0; $i < $self->{max_buckets}; $i++) {
9cec1360 888 my ($key, $subloc) = $self->_get_key_subloc( $keys, $i );
d0b74c17 889
8db25060 890 # End of bucket list -- return to outer loop
beac1dff 891 if (!$subloc) {
beac1dff 892 $obj->{return_next} = 1;
893 last;
894 }
8db25060 895 # Located previous key -- return next one found
beac1dff 896 elsif ($key eq $obj->{prev_md5}) {
beac1dff 897 $obj->{return_next} = 1;
898 next;
899 }
8db25060 900 # Seek to bucket location and skip over signature
beac1dff 901 elsif ($obj->{return_next}) {
8db25060 902 seek($fh, $subloc + $obj->_root->{file_offset}, SEEK_SET);
d0b74c17 903
beac1dff 904 # Skip over value to get to plain key
8db25060 905 my $sig;
906 read( $fh, $sig, SIG_SIZE );
907
beac1dff 908 my $size;
e5fc7e69 909 read( $fh, $size, $self->{data_size});
910 $size = unpack($self->{data_pack}, $size);
beac1dff 911 if ($size) { seek($fh, $size, SEEK_CUR); }
d0b74c17 912
beac1dff 913 # Read in plain key and return as scalar
beac1dff 914 my $plain_key;
e5fc7e69 915 read( $fh, $size, $self->{data_size});
916 $size = unpack($self->{data_pack}, $size);
beac1dff 917 if ($size) { read( $fh, $plain_key, $size); }
d0b74c17 918
beac1dff 919 return $plain_key;
920 }
8db25060 921 }
d0b74c17 922
beac1dff 923 $obj->{return_next} = 1;
924 } # tag is a bucket list
d0b74c17 925
beac1dff 926 return;
6736c116 927}
928
929sub get_next_key {
beac1dff 930 ##
931 # Locate next key, given digested previous one
932 ##
6736c116 933 my $self = shift;
934 my ($obj) = @_;
d0b74c17 935
beac1dff 936 $obj->{prev_md5} = $_[1] ? $_[1] : undef;
937 $obj->{return_next} = 0;
d0b74c17 938
beac1dff 939 ##
940 # If the previous key was not specifed, start at the top and
941 # return the first one found.
942 ##
943 if (!$obj->{prev_md5}) {
944 $obj->{prev_md5} = chr(0) x $self->{hash_size};
945 $obj->{return_next} = 1;
946 }
d0b74c17 947
beac1dff 948 return $self->traverse_index( $obj, $obj->_base_offset, 0 );
6736c116 949}
950
75be6413 951# Utilities
952
9cec1360 953sub _get_key_subloc {
75be6413 954 my $self = shift;
955 my ($keys, $idx) = @_;
956
6ed2f3df 957 my ($key, $subloc, $size) = unpack(
958 "a$self->{hash_size} $self->{long_pack} $self->{long_pack}",
75be6413 959 substr(
960 $keys,
9cec1360 961 ($idx * $self->{bucket_size}),
962 $self->{bucket_size},
75be6413 963 ),
964 );
965
6ed2f3df 966 return ($key, $subloc, $size);
75be6413 967}
968
d608b06e 969sub _find_in_buckets {
970 my $self = shift;
971 my ($tag, $md5) = @_;
972
973 BUCKET:
974 for ( my $i = 0; $i < $self->{max_buckets}; $i++ ) {
9a187d8c 975 my ($key, $subloc, $size) = $self->_get_key_subloc(
976 $tag->{content}, $i,
977 );
d608b06e 978
9a187d8c 979 return ($subloc, $i * $self->{bucket_size}, $size) unless $subloc;
d608b06e 980
981 next BUCKET if $key ne $md5;
982
9a187d8c 983 return ($subloc, $i * $self->{bucket_size}, $size);
d608b06e 984 }
985
986 return;
987}
988
7b1e1aa1 989#sub _print_at {
990# my $self = shift;
991# my ($obj, $spot, $data) = @_;
992#
993# my $fh = $obj->_fh;
994# seek( $fh, $spot, SEEK_SET );
995# print( $fh $data );
996#
997# return;
998#}
999
994ccd8e 1000sub _request_space {
1001 my $self = shift;
1002 my ($obj, $size) = @_;
1003
1004 my $loc = $obj->_root->{end};
c9ec091a 1005 $obj->_root->{end} += $size;
994ccd8e 1006
1007 return $loc;
1008}
1009
1010sub _release_space {
1011 my $self = shift;
1012 my ($obj, $size, $loc) = @_;
1013
7b1e1aa1 1014 my $next_loc = 0;
1015
1016 my $fh = $obj->_fh;
1017 seek( $fh, $loc + $obj->_root->{file_offset}, SEEK_SET );
1018 print( $fh SIG_FREE
1019 . pack($self->{long_pack}, $size )
1020 . pack($self->{long_pack}, $next_loc )
1021 );
1022
994ccd8e 1023 return;
1024}
1025
a20d9a3f 10261;
1027__END__
d5d7c51d 1028
1029# This will be added in later, after more refactoring is done. This is an early
1030# attempt at refactoring on the physical level instead of the virtual level.
1031sub _read_at {
1032 my $self = shift;
1033 my ($obj, $spot, $amount, $unpack) = @_;
1034
1035 my $fh = $obj->_fh;
1036 seek( $fh, $spot + $obj->_root->{file_offset}, SEEK_SET );
1037
1038 my $buffer;
1039 my $bytes_read = read( $fh, $buffer, $amount );
1040
1041 if ( $unpack ) {
1042 $buffer = unpack( $unpack, $buffer );
1043 }
1044
1045 if ( wantarray ) {
1046 return ($buffer, $bytes_read);
1047 }
1048 else {
1049 return $buffer;
1050 }
1051}