1 package DBM::Deep::Engine;
5 use Fcntl qw( :DEFAULT :flock :seek );
9 # Open a fh to the database, create if nonexistent.
10 # Make sure file signature matches DBM::Deep spec.
15 if (defined($obj->_fh)) { $self->close( $obj ); }
18 local $SIG{'__DIE__'};
19 # Theoretically, adding O_BINARY should remove the need for the binmode
20 # Of course, testing it is going to be ... interesting.
21 my $flags = O_RDWR | O_CREAT | O_BINARY;
24 sysopen( $fh, $obj->_root->{file}, $flags )
26 $obj->_root->{fh} = $fh;
27 }; if ($@ ) { $obj->_throw_error( "Received error: $@\n" ); }
28 if (! defined($obj->_fh)) {
29 return $obj->_throw_error("Cannot sysopen file: " . $obj->_root->{file} . ": $!");
34 #XXX Can we remove this by using the right sysopen() flags?
35 # Maybe ... q.v. above
36 binmode $fh; # for win32
38 if ($obj->_root->{autoflush}) {
44 seek($fh, 0 + $obj->_root->{file_offset}, SEEK_SET);
47 my $bytes_read = read( $fh, $signature, length(DBM::Deep->SIG_FILE));
50 # File is empty -- write signature and master index
53 seek($fh, 0 + $obj->_root->{file_offset}, SEEK_SET);
54 print( $fh DBM::Deep->SIG_FILE);
55 $self->create_tag($obj, $obj->_base_offset, $obj->_type, chr(0) x $DBM::Deep::INDEX_SIZE);
57 my $plain_key = "[base]";
58 print( $fh pack($DBM::Deep::DATA_LENGTH_PACK, length($plain_key)) . $plain_key );
60 # Flush the filehandle
61 my $old_fh = select $fh;
62 my $old_af = $|; $| = 1; $| = $old_af;
65 my @stats = stat($fh);
66 $obj->_root->{inode} = $stats[1];
67 $obj->_root->{end} = $stats[7];
73 # Check signature was valid
75 unless ($signature eq DBM::Deep->SIG_FILE) {
77 return $obj->_throw_error("Signature not found -- file is not a Deep DB");
80 my @stats = stat($fh);
81 $obj->_root->{inode} = $stats[1];
82 $obj->_root->{end} = $stats[7];
85 # Get our type from master index signature
87 my $tag = $self->load_tag($obj, $obj->_base_offset);
89 #XXX We probably also want to store the hash algorithm name and not assume anything
90 #XXX The cool thing would be to allow a different hashing algorithm at every level
93 return $obj->_throw_error("Corrupted file, no master index record");
95 if ($obj->{type} ne $tag->{signature}) {
96 return $obj->_throw_error("File type mismatch");
106 if ( my $fh = $obj->_root->{fh} ) {
109 $obj->_root->{fh} = undef;
116 # Given offset, signature and content, create tag and write to disk
119 my ($obj, $offset, $sig, $content) = @_;
120 my $size = length($content);
124 seek($fh, $offset + $obj->_root->{file_offset}, SEEK_SET);
125 print( $fh $sig . pack($DBM::Deep::DATA_LENGTH_PACK, $size) . $content );
127 if ($offset == $obj->_root->{end}) {
128 $obj->_root->{end} += DBM::Deep->SIG_SIZE + $DBM::Deep::DATA_LENGTH_SIZE + $size;
134 offset => $offset + DBM::Deep->SIG_SIZE + $DBM::Deep::DATA_LENGTH_SIZE,
141 # Given offset, load single tag and return signature, size and data
144 my ($obj, $offset) = @_;
148 seek($fh, $offset + $obj->_root->{file_offset}, SEEK_SET);
149 if (eof $fh) { return undef; }
152 read( $fh, $b, DBM::Deep->SIG_SIZE + $DBM::Deep::DATA_LENGTH_SIZE );
153 my ($sig, $size) = unpack( "A $DBM::Deep::DATA_LENGTH_PACK", $b );
156 read( $fh, $buffer, $size);
161 offset => $offset + DBM::Deep->SIG_SIZE + $DBM::Deep::DATA_LENGTH_SIZE,
168 # Given index tag, lookup single entry in index and return .
171 my ($obj, $tag, $index) = @_;
173 my $location = unpack($DBM::Deep::LONG_PACK, substr($tag->{content}, $index * $DBM::Deep::LONG_SIZE, $DBM::Deep::LONG_SIZE) );
174 if (!$location) { return; }
176 return $self->load_tag( $obj, $location );
181 # Adds one key/value pair to bucket list, given offset, MD5 digest of key,
182 # plain (undigested) key and value.
185 my ($obj, $tag, $md5, $plain_key, $value) = @_;
186 my $keys = $tag->{content};
190 my $root = $obj->_root;
192 my $is_dbm_deep = eval { local $SIG{'__DIE__'}; $value->isa( 'DBM::Deep' ) };
193 my $internal_ref = $is_dbm_deep && ($value->_root eq $root);
198 # Iterate through buckets, seeing if this is a new entry or a replace.
200 for (my $i=0; $i<$DBM::Deep::MAX_BUCKETS; $i++) {
201 my $subloc = unpack($DBM::Deep::LONG_PACK, substr($keys, ($i * $DBM::Deep::BUCKET_SIZE) + $DBM::Deep::HASH_SIZE, $DBM::Deep::LONG_SIZE));
204 # Found empty bucket (end of list). Populate and exit loop.
208 $location = $internal_ref
209 ? $value->_base_offset
212 seek($fh, $tag->{offset} + ($i * $DBM::Deep::BUCKET_SIZE) + $root->{file_offset}, SEEK_SET);
213 print( $fh $md5 . pack($DBM::Deep::LONG_PACK, $location) );
217 my $key = substr($keys, $i * $DBM::Deep::BUCKET_SIZE, $DBM::Deep::HASH_SIZE);
220 # Found existing bucket with same key. Replace with new value.
225 $location = $value->_base_offset;
226 seek($fh, $tag->{offset} + ($i * $DBM::Deep::BUCKET_SIZE) + $root->{file_offset}, SEEK_SET);
227 print( $fh $md5 . pack($DBM::Deep::LONG_PACK, $location) );
231 seek($fh, $subloc + DBM::Deep->SIG_SIZE + $root->{file_offset}, SEEK_SET);
233 read( $fh, $size, $DBM::Deep::DATA_LENGTH_SIZE); $size = unpack($DBM::Deep::DATA_LENGTH_PACK, $size);
236 # If value is a hash, array, or raw value with equal or less size, we can
237 # reuse the same content area of the database. Otherwise, we have to create
238 # a new content area at the EOF.
241 my $r = Scalar::Util::reftype( $value ) || '';
242 if ( $r eq 'HASH' || $r eq 'ARRAY' ) {
243 $actual_length = $DBM::Deep::INDEX_SIZE;
245 # if autobless is enabled, must also take into consideration
246 # the class name, as it is stored along with key/value.
247 if ( $root->{autobless} ) {
248 my $value_class = Scalar::Util::blessed($value);
249 if ( defined $value_class && !$value->isa('DBM::Deep') ) {
250 $actual_length += length($value_class);
254 else { $actual_length = length($value); }
256 if ($actual_length <= $size) {
260 $location = $root->{end};
261 seek($fh, $tag->{offset} + ($i * $DBM::Deep::BUCKET_SIZE) + $DBM::Deep::HASH_SIZE + $root->{file_offset}, SEEK_SET);
262 print( $fh pack($DBM::Deep::LONG_PACK, $location) );
270 # If this is an internal reference, return now.
271 # No need to write value or plain key
278 # If bucket didn't fit into list, split into a new index level
281 seek($fh, $tag->{ref_loc} + $root->{file_offset}, SEEK_SET);
282 print( $fh pack($DBM::Deep::LONG_PACK, $root->{end}) );
284 my $index_tag = $self->create_tag($obj, $root->{end}, DBM::Deep->SIG_INDEX, chr(0) x $DBM::Deep::INDEX_SIZE);
287 $keys .= $md5 . pack($DBM::Deep::LONG_PACK, 0);
289 for (my $i=0; $i<=$DBM::Deep::MAX_BUCKETS; $i++) {
290 my $key = substr($keys, $i * $DBM::Deep::BUCKET_SIZE, $DBM::Deep::HASH_SIZE);
292 my $old_subloc = unpack($DBM::Deep::LONG_PACK, substr($keys, ($i * $DBM::Deep::BUCKET_SIZE) +
293 $DBM::Deep::HASH_SIZE, $DBM::Deep::LONG_SIZE));
294 my $num = ord(substr($key, $tag->{ch} + 1, 1));
296 if ($offsets[$num]) {
297 my $offset = $offsets[$num] + DBM::Deep->SIG_SIZE + $DBM::Deep::DATA_LENGTH_SIZE;
298 seek($fh, $offset + $root->{file_offset}, SEEK_SET);
300 read( $fh, $subkeys, $DBM::Deep::BUCKET_LIST_SIZE);
302 for (my $k=0; $k<$DBM::Deep::MAX_BUCKETS; $k++) {
303 my $subloc = unpack($DBM::Deep::LONG_PACK, substr($subkeys, ($k * $DBM::Deep::BUCKET_SIZE) +
304 $DBM::Deep::HASH_SIZE, $DBM::Deep::LONG_SIZE));
306 seek($fh, $offset + ($k * $DBM::Deep::BUCKET_SIZE) + $root->{file_offset}, SEEK_SET);
307 print( $fh $key . pack($DBM::Deep::LONG_PACK, $old_subloc || $root->{end}) );
313 $offsets[$num] = $root->{end};
314 seek($fh, $index_tag->{offset} + ($num * $DBM::Deep::LONG_SIZE) + $root->{file_offset}, SEEK_SET);
315 print( $fh pack($DBM::Deep::LONG_PACK, $root->{end}) );
317 my $blist_tag = $self->create_tag($obj, $root->{end}, DBM::Deep->SIG_BLIST, chr(0) x $DBM::Deep::BUCKET_LIST_SIZE);
319 seek($fh, $blist_tag->{offset} + $root->{file_offset}, SEEK_SET);
320 print( $fh $key . pack($DBM::Deep::LONG_PACK, $old_subloc || $root->{end}) );
325 $location ||= $root->{end};
326 } # re-index bucket list
329 # Seek to content area and store signature, value and plaintext key
333 seek($fh, $location + $root->{file_offset}, SEEK_SET);
336 # Write signature based on content type, set content length and write actual value.
338 my $r = Scalar::Util::reftype($value) || '';
340 print( $fh DBM::Deep->TYPE_HASH );
341 print( $fh pack($DBM::Deep::DATA_LENGTH_PACK, $DBM::Deep::INDEX_SIZE) . chr(0) x $DBM::Deep::INDEX_SIZE );
342 $content_length = $DBM::Deep::INDEX_SIZE;
344 elsif ($r eq 'ARRAY') {
345 print( $fh DBM::Deep->TYPE_ARRAY );
346 print( $fh pack($DBM::Deep::DATA_LENGTH_PACK, $DBM::Deep::INDEX_SIZE) . chr(0) x $DBM::Deep::INDEX_SIZE );
347 $content_length = $DBM::Deep::INDEX_SIZE;
349 elsif (!defined($value)) {
350 print( $fh DBM::Deep->SIG_NULL );
351 print( $fh pack($DBM::Deep::DATA_LENGTH_PACK, 0) );
355 print( $fh DBM::Deep->SIG_DATA );
356 print( $fh pack($DBM::Deep::DATA_LENGTH_PACK, length($value)) . $value );
357 $content_length = length($value);
361 # Plain key is stored AFTER value, as keys are typically fetched less often.
363 print( $fh pack($DBM::Deep::DATA_LENGTH_PACK, length($plain_key)) . $plain_key );
366 # If value is blessed, preserve class name
368 if ( $root->{autobless} ) {
369 my $value_class = Scalar::Util::blessed($value);
370 if ( defined $value_class && $value_class ne 'DBM::Deep' ) {
372 # Blessed ref -- will restore later
375 print( $fh pack($DBM::Deep::DATA_LENGTH_PACK, length($value_class)) . $value_class );
376 $content_length += 1;
377 $content_length += $DBM::Deep::DATA_LENGTH_SIZE + length($value_class);
381 $content_length += 1;
386 # If this is a new content area, advance EOF counter
388 if ($location == $root->{end}) {
389 $root->{end} += DBM::Deep->SIG_SIZE;
390 $root->{end} += $DBM::Deep::DATA_LENGTH_SIZE + $content_length;
391 $root->{end} += $DBM::Deep::DATA_LENGTH_SIZE + length($plain_key);
395 # If content is a hash or array, create new child DBM::Deep object and
396 # pass each key or element to it.
399 my $branch = DBM::Deep->new(
400 type => DBM::Deep->TYPE_HASH,
401 base_offset => $location,
404 foreach my $key (keys %{$value}) {
405 $branch->STORE( $key, $value->{$key} );
408 elsif ($r eq 'ARRAY') {
409 my $branch = DBM::Deep->new(
410 type => DBM::Deep->TYPE_ARRAY,
411 base_offset => $location,
415 foreach my $element (@{$value}) {
416 $branch->STORE( $index, $element );
424 return $obj->_throw_error("Fatal error: indexing failed -- possibly due to corruption in file");
427 sub get_bucket_value {
429 # Fetch single value given tag and MD5 digested key.
432 my ($obj, $tag, $md5) = @_;
433 my $keys = $tag->{content};
438 # Iterate through buckets, looking for a key match
441 for (my $i=0; $i<$DBM::Deep::MAX_BUCKETS; $i++) {
442 my $key = substr($keys, $i * $DBM::Deep::BUCKET_SIZE, $DBM::Deep::HASH_SIZE);
443 my $subloc = unpack($DBM::Deep::LONG_PACK, substr($keys, ($i * $DBM::Deep::BUCKET_SIZE) + $DBM::Deep::HASH_SIZE, $DBM::Deep::LONG_SIZE));
447 # Hit end of list, no match
452 if ( $md5 ne $key ) {
457 # Found match -- seek to offset and read signature
460 seek($fh, $subloc + $obj->_root->{file_offset}, SEEK_SET);
461 read( $fh, $signature, DBM::Deep->SIG_SIZE);
464 # If value is a hash or array, return new DBM::Deep object with correct offset
466 if (($signature eq DBM::Deep->TYPE_HASH) || ($signature eq DBM::Deep->TYPE_ARRAY)) {
467 my $obj = DBM::Deep->new(
469 base_offset => $subloc,
473 if ($obj->_root->{autobless}) {
475 # Skip over value and plain key to see if object needs
478 seek($fh, $DBM::Deep::DATA_LENGTH_SIZE + $DBM::Deep::INDEX_SIZE, SEEK_CUR);
481 read( $fh, $size, $DBM::Deep::DATA_LENGTH_SIZE); $size = unpack($DBM::Deep::DATA_LENGTH_PACK, $size);
482 if ($size) { seek($fh, $size, SEEK_CUR); }
485 read( $fh, $bless_bit, 1);
486 if (ord($bless_bit)) {
488 # Yes, object needs to be re-blessed
491 read( $fh, $size, $DBM::Deep::DATA_LENGTH_SIZE); $size = unpack($DBM::Deep::DATA_LENGTH_PACK, $size);
492 if ($size) { read( $fh, $class_name, $size); }
493 if ($class_name) { $obj = bless( $obj, $class_name ); }
501 # Otherwise return actual value
503 elsif ($signature eq DBM::Deep->SIG_DATA) {
506 read( $fh, $size, $DBM::Deep::DATA_LENGTH_SIZE); $size = unpack($DBM::Deep::DATA_LENGTH_PACK, $size);
507 if ($size) { read( $fh, $value, $size); }
512 # Key exists, but content is null