From: Rob Kinyon Date: Mon, 11 Jan 2010 02:17:28 +0000 (-0500) Subject: Refactored to _descend to fix the recursion bug X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=c2472eded1c8461656b9df2151a763a8379d9b25;p=dbsrgits%2FDBM-Deep.git Refactored to _descend to fix the recursion bug --- diff --git a/lib/DBM/Deep/Engine.pm b/lib/DBM/Deep/Engine.pm index 0419be7..5d9b034 100644 --- a/lib/DBM/Deep/Engine.pm +++ b/lib/DBM/Deep/Engine.pm @@ -381,5 +381,40 @@ sub storage { $_[0]{storage} } sub sector_type { die "sector_type must be implemented in a child class" } +# This code is to make sure we write all the values in the $value to the +# disk and to make sure all changes to $value after the assignment are +# reflected on disk. This may be counter-intuitive at first, but it is +# correct dwimmery. +# NOTE - simply tying $value won't perform a STORE on each value. Hence, +# the copy to a temp value. +sub _descend { + my $self = shift; + my ($value, $value_sector) = @_; + my $r = Scalar::Util::reftype( $value ) || ''; + + if ( $r eq 'ARRAY' ) { + my @temp = @$value; + tie @$value, 'DBM::Deep', { + base_offset => $value_sector->offset, + staleness => $value_sector->staleness, + storage => $self->storage, + engine => $self, + }; + @$value = @temp; + bless $value, 'DBM::Deep::Array' unless Scalar::Util::blessed( $value ); + } + elsif ( $r eq 'HASH' ) { + my %temp = %$value; + tie %$value, 'DBM::Deep', { + base_offset => $value_sector->offset, + staleness => $value_sector->staleness, + storage => $self->storage, + engine => $self, + }; + %$value = %temp; + bless $value, 'DBM::Deep::Hash' unless Scalar::Util::blessed( $value ); + } +} + 1; __END__ diff --git a/lib/DBM/Deep/Engine/DBI.pm b/lib/DBM/Deep/Engine/DBI.pm index 4e6d719..845771b 100644 --- a/lib/DBM/Deep/Engine/DBI.pm +++ b/lib/DBM/Deep/Engine/DBI.pm @@ -278,35 +278,7 @@ sub write_value { value => $value_sector, }); - # This code is to make sure we write all the values in the $value to the - # disk and to make sure all changes to $value after the assignment are - # reflected on disk. This may be counter-intuitive at first, but it is - # correct dwimmery. - # NOTE - simply tying $value won't perform a STORE on each value. Hence, - # the copy to a temp value. - if ( $r eq 'ARRAY' ) { - my @temp = @$value; - tie @$value, 'DBM::Deep', { - base_offset => $value_sector->offset, - staleness => $value_sector->staleness, - storage => $self->storage, - engine => $self, - }; - @$value = @temp; - bless $value, 'DBM::Deep::Array' unless Scalar::Util::blessed( $value ); - } - elsif ( $r eq 'HASH' ) { - my %temp = %$value; - tie %$value, 'DBM::Deep', { - base_offset => $value_sector->offset, - staleness => $value_sector->staleness, - storage => $self->storage, - engine => $self, - }; - - %$value = %temp; - bless $value, 'DBM::Deep::Hash' unless Scalar::Util::blessed( $value ); - } + $self->_descend( $value, $value_sector ); return 1; } diff --git a/lib/DBM/Deep/Engine/File.pm b/lib/DBM/Deep/Engine/File.pm index 47c233a..f2e4398 100644 --- a/lib/DBM/Deep/Engine/File.pm +++ b/lib/DBM/Deep/Engine/File.pm @@ -363,35 +363,7 @@ sub write_value { value => $value_sector, }); - # This code is to make sure we write all the values in the $value to the - # disk and to make sure all changes to $value after the assignment are - # reflected on disk. This may be counter-intuitive at first, but it is - # correct dwimmery. - # NOTE - simply tying $value won't perform a STORE on each value. Hence, - # the copy to a temp value. - if ( $r eq 'ARRAY' ) { - my @temp = @$value; - tie @$value, 'DBM::Deep', { - base_offset => $value_sector->offset, - staleness => $value_sector->staleness, - storage => $self->storage, - engine => $self, - }; - @$value = @temp; - bless $value, 'DBM::Deep::Array' unless Scalar::Util::blessed( $value ); - } - elsif ( $r eq 'HASH' ) { - my %temp = %$value; - tie %$value, 'DBM::Deep', { - base_offset => $value_sector->offset, - staleness => $value_sector->staleness, - storage => $self->storage, - engine => $self, - }; - - %$value = %temp; - bless $value, 'DBM::Deep::Hash' unless Scalar::Util::blessed( $value ); - } + $self->_descend( $value, $value_sector ); return 1; }