#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,
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);
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}) );
# 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++;
}
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;
##
# 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;
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;
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();
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 );
##
# 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
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__