From: rkinyon Date: Tue, 28 Feb 2006 19:52:48 +0000 (+0000) Subject: Moved _create_tag, _load_tag, and _index_lookup into the engine X-Git-Tag: 0-99_01~114 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=d4b1166ed4ddfa602789898a5ba50b01aa35f00a;p=dbsrgits%2FDBM-Deep.git Moved _create_tag, _load_tag, and _index_lookup into the engine --- diff --git a/lib/DBM/Deep.pm b/lib/DBM/Deep.pm index baca8cc..c1f8fa8 100644 --- a/lib/DBM/Deep.pm +++ b/lib/DBM/Deep.pm @@ -203,70 +203,6 @@ sub TIEARRAY { #sub DESTROY { #} -sub _create_tag { - ## - # Given offset, signature and content, create tag and write to disk - ## - my ($self, $offset, $sig, $content) = @_; - my $size = length($content); - - my $fh = $self->_fh; - - seek($fh, $offset + $self->_root->{file_offset}, SEEK_SET); - print( $fh $sig . pack($DATA_LENGTH_PACK, $size) . $content ); - - if ($offset == $self->_root->{end}) { - $self->_root->{end} += SIG_SIZE + $DATA_LENGTH_SIZE + $size; - } - - return { - signature => $sig, - size => $size, - offset => $offset + SIG_SIZE + $DATA_LENGTH_SIZE, - content => $content - }; -} - -sub _load_tag { - ## - # Given offset, load single tag and return signature, size and data - ## - my $self = shift; - my $offset = shift; - - my $fh = $self->_fh; - - seek($fh, $offset + $self->_root->{file_offset}, SEEK_SET); - if (eof $fh) { return undef; } - - my $b; - read( $fh, $b, SIG_SIZE + $DATA_LENGTH_SIZE ); - my ($sig, $size) = unpack( "A $DATA_LENGTH_PACK", $b ); - - my $buffer; - read( $fh, $buffer, $size); - - return { - signature => $sig, - size => $size, - offset => $offset + SIG_SIZE + $DATA_LENGTH_SIZE, - content => $buffer - }; -} - -sub _index_lookup { - ## - # Given index tag, lookup single entry in index and return . - ## - my $self = shift; - my ($tag, $index) = @_; - - my $location = unpack($LONG_PACK, substr($tag->{content}, $index * $LONG_SIZE, $LONG_SIZE) ); - if (!$location) { return; } - - return $self->_load_tag( $location ); -} - sub _add_bucket { ## # Adds one key/value pair to bucket list, given offset, MD5 digest of key, @@ -372,7 +308,7 @@ sub _add_bucket { seek($fh, $tag->{ref_loc} + $root->{file_offset}, SEEK_SET); print( $fh pack($LONG_PACK, $root->{end}) ); - my $index_tag = $self->_create_tag($root->{end}, SIG_INDEX, chr(0) x $INDEX_SIZE); + my $index_tag = $self->{engine}->create_tag($self, $root->{end}, SIG_INDEX, chr(0) x $INDEX_SIZE); my @offsets = (); $keys .= $md5 . pack($LONG_PACK, 0); @@ -403,7 +339,7 @@ sub _add_bucket { seek($fh, $index_tag->{offset} + ($num * $LONG_SIZE) + $root->{file_offset}, SEEK_SET); print( $fh pack($LONG_PACK, $root->{end}) ); - my $blist_tag = $self->_create_tag($root->{end}, SIG_BLIST, chr(0) x $BUCKET_LIST_SIZE); + my $blist_tag = $self->{engine}->create_tag($self, $root->{end}, SIG_BLIST, chr(0) x $BUCKET_LIST_SIZE); seek($fh, $blist_tag->{offset} + $root->{file_offset}, SEEK_SET); print( $fh $key . pack($LONG_PACK, $old_subloc || $root->{end}) ); @@ -695,11 +631,11 @@ sub _find_bucket_list { # Locate offset for bucket list using digest index system ## my $ch = 0; - my $tag = $self->_load_tag($self->_base_offset); + my $tag = $self->{engine}->load_tag($self, $self->_base_offset); if (!$tag) { return; } while ($tag->{signature} ne SIG_BLIST) { - $tag = $self->_index_lookup($tag, ord(substr($md5, $ch, 1))); + $tag = $self->{engine}->index_lookup($self, $tag, ord(substr($md5, $ch, 1))); if (!$tag) { return; } $ch++; } @@ -714,7 +650,7 @@ sub _traverse_index { my ($self, $offset, $ch, $force_return_next) = @_; $force_return_next = undef unless $force_return_next; - my $tag = $self->_load_tag( $offset ); + my $tag = $self->{engine}->load_tag($self, $offset ); my $fh = $self->_fh; @@ -1198,9 +1134,9 @@ sub STORE { ## # Locate offset for bucket list using digest index system ## - my $tag = $self->_load_tag($self->_base_offset); + my $tag = $self->{engine}->load_tag($self, $self->_base_offset); if (!$tag) { - $tag = $self->_create_tag($self->_base_offset, SIG_INDEX, chr(0) x $INDEX_SIZE); + $tag = $self->{engine}->create_tag($self, $self->_base_offset, SIG_INDEX, chr(0) x $INDEX_SIZE); } my $ch = 0; @@ -1208,13 +1144,13 @@ sub STORE { my $num = ord(substr($md5, $ch, 1)); my $ref_loc = $tag->{offset} + ($num * $LONG_SIZE); - my $new_tag = $self->_index_lookup($tag, $num); + my $new_tag = $self->{engine}->index_lookup($self, $tag, $num); if (!$new_tag) { seek($fh, $ref_loc + $self->_root->{file_offset}, SEEK_SET); print( $fh pack($LONG_PACK, $self->_root->{end}) ); - $tag = $self->_create_tag($self->_root->{end}, SIG_BLIST, chr(0) x $BUCKET_LIST_SIZE); + $tag = $self->{engine}->create_tag($self, $self->_root->{end}, SIG_BLIST, chr(0) x $BUCKET_LIST_SIZE); $tag->{ref_loc} = $ref_loc; $tag->{ch} = $ch; @@ -1368,7 +1304,7 @@ sub CLEAR { return; } - $self->_create_tag($self->_base_offset, $self->_type, chr(0) x $INDEX_SIZE); + $self->{engine}->create_tag($self, $self->_base_offset, $self->_type, chr(0) x $INDEX_SIZE); $self->unlock(); diff --git a/lib/DBM/Deep/Engine.pm b/lib/DBM/Deep/Engine.pm index 1dd7dd6..6ddca8a 100644 --- a/lib/DBM/Deep/Engine.pm +++ b/lib/DBM/Deep/Engine.pm @@ -52,7 +52,7 @@ sub open { if (!$bytes_read) { seek($fh, 0 + $obj->_root->{file_offset}, SEEK_SET); print( $fh DBM::Deep->SIG_FILE); - $obj->_create_tag($obj->_base_offset, $obj->_type, chr(0) x $DBM::Deep::INDEX_SIZE); + $self->create_tag($obj, $obj->_base_offset, $obj->_type, chr(0) x $DBM::Deep::INDEX_SIZE); my $plain_key = "[base]"; print( $fh pack($DBM::Deep::DATA_LENGTH_PACK, length($plain_key)) . $plain_key ); @@ -84,7 +84,7 @@ sub open { ## # Get our type from master index signature ## - my $tag = $obj->_load_tag($obj->_base_offset); + my $tag = $self->load_tag($obj, $obj->_base_offset); #XXX We probably also want to store the hash algorithm name and not assume anything #XXX The cool thing would be to allow a different hashing algorithm at every level @@ -111,5 +111,70 @@ sub close { return 1; } +sub create_tag { + ## + # Given offset, signature and content, create tag and write to disk + ## + my $self = shift; + my ($obj, $offset, $sig, $content) = @_; + my $size = length($content); + + my $fh = $obj->_fh; + + seek($fh, $offset + $obj->_root->{file_offset}, SEEK_SET); + print( $fh $sig . pack($DBM::Deep::DATA_LENGTH_PACK, $size) . $content ); + + if ($offset == $obj->_root->{end}) { + $obj->_root->{end} += DBM::Deep->SIG_SIZE + $DBM::Deep::DATA_LENGTH_SIZE + $size; + } + + return { + signature => $sig, + size => $size, + offset => $offset + DBM::Deep->SIG_SIZE + $DBM::Deep::DATA_LENGTH_SIZE, + content => $content + }; +} + +sub load_tag { + ## + # Given offset, load single tag and return signature, size and data + ## + my $self = shift; + my ($obj, $offset) = @_; + + my $fh = $obj->_fh; + + seek($fh, $offset + $obj->_root->{file_offset}, SEEK_SET); + if (eof $fh) { return undef; } + + my $b; + read( $fh, $b, DBM::Deep->SIG_SIZE + $DBM::Deep::DATA_LENGTH_SIZE ); + my ($sig, $size) = unpack( "A $DBM::Deep::DATA_LENGTH_PACK", $b ); + + my $buffer; + read( $fh, $buffer, $size); + + return { + signature => $sig, + size => $size, + offset => $offset + DBM::Deep->SIG_SIZE + $DBM::Deep::DATA_LENGTH_SIZE, + content => $buffer + }; +} + +sub index_lookup { + ## + # Given index tag, lookup single entry in index and return . + ## + my $self = shift; + my ($obj, $tag, $index) = @_; + + my $location = unpack($DBM::Deep::LONG_PACK, substr($tag->{content}, $index * $DBM::Deep::LONG_SIZE, $DBM::Deep::LONG_SIZE) ); + if (!$location) { return; } + + return $self->load_tag( $obj, $location ); +} + 1; __END__