From: rkinyon Date: Wed, 1 Mar 2006 18:27:26 +0000 (+0000) Subject: Removed global variable X-Git-Tag: 0-99_01~104 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=612969fb497f2c520aa405dff43755334df2b64a;p=dbsrgits%2FDBM-Deep.git Removed global variable --- diff --git a/lib/DBM/Deep.pm b/lib/DBM/Deep.pm index 4eab81d..aec6531 100644 --- a/lib/DBM/Deep.pm +++ b/lib/DBM/Deep.pm @@ -122,7 +122,7 @@ sub _init { my $self = bless { type => TYPE_HASH, base_offset => length(SIG_FILE), - engine => 'DBM::Deep::Engine', + engine => DBM::Deep::Engine->new, }, $class; foreach my $param ( keys %$self ) { @@ -492,7 +492,7 @@ sub STORE { ? $self->_root->{filter_store_value}->($_[2]) : $_[2]; - my $md5 = $DBM::Deep::Engine::DIGEST_FUNC->($key); + my $md5 = $self->{engine}{digest}->($key); unless ( _is_writable( $self->_fh ) ) { $self->_throw_error( 'Cannot write to a readonly filehandle' ); @@ -557,7 +557,7 @@ sub FETCH { my $self = shift->_get_self; my $key = shift; - my $md5 = $DBM::Deep::Engine::DIGEST_FUNC->($key); + my $md5 = $self->{engine}{digest}->($key); ## # Request shared lock for reading @@ -592,7 +592,7 @@ sub DELETE { my $self = $_[0]->_get_self; my $key = $_[1]; - my $md5 = $DBM::Deep::Engine::DIGEST_FUNC->($key); + my $md5 = $self->{engine}{digest}->($key); ## # Request exclusive lock for writing @@ -632,7 +632,7 @@ sub EXISTS { my $self = $_[0]->_get_self; my $key = $_[1]; - my $md5 = $DBM::Deep::Engine::DIGEST_FUNC->($key); + my $md5 = $self->{engine}{digest}->($key); ## # Request shared lock for reading diff --git a/lib/DBM/Deep/Array.pm b/lib/DBM/Deep/Array.pm index 0087ccc..7cb0b5b 100644 --- a/lib/DBM/Deep/Array.pm +++ b/lib/DBM/Deep/Array.pm @@ -43,7 +43,7 @@ sub FETCH { } } - $key = pack($DBM::Deep::Engine::LONG_PACK, $key); + $key = pack($self->{engine}{long_pack}, $key); } my $rv = $self->SUPER::FETCH( $key ); @@ -73,7 +73,7 @@ sub STORE { } } - $key = pack($DBM::Deep::Engine::LONG_PACK, $key); + $key = pack($self->{engine}{long_pack}, $key); } my $rv = $self->SUPER::STORE( $key, $value ); @@ -105,7 +105,7 @@ sub EXISTS { } } - $key = pack($DBM::Deep::Engine::LONG_PACK, $key); + $key = pack($self->{engine}{long_pack}, $key); } my $rv = $self->SUPER::EXISTS( $key ); @@ -133,7 +133,7 @@ sub DELETE { } } - $key = pack($DBM::Deep::Engine::LONG_PACK, $key); + $key = pack($self->{engine}{long_pack}, $key); } my $rv = $self->SUPER::DELETE( $key ); @@ -165,7 +165,7 @@ sub FETCHSIZE { $self->unlock; if ($packed_size) { - return int(unpack($DBM::Deep::Engine::LONG_PACK, $packed_size)); + return int(unpack($self->{engine}{long_pack}, $packed_size)); } return 0; @@ -183,7 +183,7 @@ sub STORESIZE { my $SAVE_FILTER = $self->_root->{filter_store_value}; $self->_root->{filter_store_value} = undef; - my $result = $self->STORE('length', pack($DBM::Deep::Engine::LONG_PACK, $new_length)); + my $result = $self->STORE('length', pack($self->{engine}{long_pack}, $new_length)); $self->_root->{filter_store_value} = $SAVE_FILTER; diff --git a/lib/DBM/Deep/Engine.pm b/lib/DBM/Deep/Engine.pm index a8678c2..cd04b51 100644 --- a/lib/DBM/Deep/Engine.pm +++ b/lib/DBM/Deep/Engine.pm @@ -23,11 +23,11 @@ our ($LONG_SIZE, $LONG_PACK, $DATA_LENGTH_SIZE, $DATA_LENGTH_PACK); # DO NOT decrease this value below 16, due to risk of recursive reindex overrun. ## our $MAX_BUCKETS = 16; -our ($DIGEST_FUNC, $HASH_SIZE); +our ($HASH_SIZE); our ($INDEX_SIZE, $BUCKET_SIZE, $BUCKET_LIST_SIZE); set_digest(); -sub _precalc_sizes { +sub precalc_sizes { ## # Precalculate index, bucket and bucket list sizes ## @@ -52,19 +52,43 @@ sub set_pack { $DATA_LENGTH_SIZE = $data_s ? $data_s : 4; $DATA_LENGTH_PACK = $data_p ? $data_p : 'N'; - _precalc_sizes(); + precalc_sizes(); } sub set_digest { + my $self = shift; ## # Set key digest function (default is MD5) ## my ($digest_func, $hash_size) = @_; - $DIGEST_FUNC = $digest_func ? $digest_func : \&Digest::MD5::md5; + $self->{digest} = $digest_func ? $digest_func : \&Digest::MD5::md5; + $HASH_SIZE = $hash_size ? $hash_size : 16; - _precalc_sizes(); + precalc_sizes(); +} + +sub new { + my $class = shift; + my ($args) = @_; + + my $self = bless { + long_size => 4, + long_pack => 'N', + data_size => 4, + data_pack => 'N', + digest => \&Digest::MD5::md5, + hash_size => 16, + max_buckets => 16, + }, $class; + + #XXX Where does 256 come from? + $self->{index_size} = 256 * $self->{long_size}; + $self->{bucket_size} = $self->{hash_size} + $self->{long_size}; + $self->{bucket_list_size} = $self->{max_buckets} * $self->{bucket_size}; + + return $self; } sub setup_fh { diff --git a/lib/DBM/Deep/Hash.pm b/lib/DBM/Deep/Hash.pm index 12b9005..8ab84f2 100644 --- a/lib/DBM/Deep/Hash.pm +++ b/lib/DBM/Deep/Hash.pm @@ -87,7 +87,7 @@ sub NEXTKEY { ? $self->_root->{filter_store_key}->($_[1]) : $_[1]; - my $prev_md5 = $DBM::Deep::Engine::DIGEST_FUNC->($prev_key); + my $prev_md5 = $self->{engine}{digest}->($prev_key); ## # Request shared lock for reading diff --git a/t/15_digest.t b/t/15_digest.t index 15f9c67..5bc2333 100644 --- a/t/15_digest.t +++ b/t/15_digest.t @@ -2,8 +2,7 @@ # DBM::Deep Test ## use strict; -use Test; -BEGIN { plan tests => 13 } +use Test::More tests => 13; use DBM::Deep; @@ -20,7 +19,7 @@ my $db = new DBM::Deep( ## # Set digest handler ## -DBM::Deep::Engine::set_digest( \&my_digest, 8 ); +$db->_get_self->{engine}->set_digest( \&my_digest, 8 ); ## # put/get key