1 package DBM::Deep::Engine::DBI;
6 use warnings FATAL => 'all';
8 use base 'DBM::Deep::Engine';
10 use DBM::Deep::Sector::DBI ();
11 use DBM::Deep::Storage::DBI ();
13 sub sector_type { 'DBM::Deep::Sector::DBI' }
14 sub iterator_class { 'DBM::Deep::Iterator::DBI' }
20 $args->{storage} = DBM::Deep::Storage::DBI->new( $args )
21 unless exists $args->{storage};
27 # Grab the parameters we want to use
28 foreach my $param ( keys %$self ) {
29 next unless exists $args->{$param};
30 $self->{$param} = $args->{$param};
40 # Default the id to 1. This means that we will be creating a row if there
41 # isn't one. The assumption is that the row_id=1 cannot never be deleted. I
42 # don't know if this is a good assumption.
43 $obj->{base_offset} ||= 1;
45 my ($rows) = $self->storage->read_from(
46 refs => $obj->_base_offset,
50 # We don't have a row yet.
52 $self->storage->write_to(
53 refs => $obj->_base_offset,
54 ref_type => $obj->_type,
58 my $sector = DBM::Deep::Sector::DBI::Reference->new({
60 offset => $obj->_base_offset,
68 my $sector = $self->load_sector( $obj->_base_offset, 'refs' )
71 # if ( $sector->staleness != $obj->_staleness ) {
75 # my $key_md5 = $self->_apply_digest( $key );
77 my $value_sector = $sector->get_data_for({
79 # key_md5 => $key_md5,
83 unless ( $value_sector ) {
84 $value_sector = DBM::Deep::Sector::DBI::Scalar->new({
91 # key_md5 => $key_md5,
93 value => $value_sector,
97 return $value_sector->data;
109 my ($obj, $old_key, $new_key) = @_;
111 my $sector = $self->load_sector( $obj->_base_offset, 'refs' )
114 # if ( $sector->staleness != $obj->_staleness ) {
118 my $value_sector = $sector->get_data_for({
123 unless ( $value_sector ) {
124 $value_sector = DBM::Deep::Sector::DBI::Scalar->new({
129 $sector->write_data({
131 value => $value_sector,
135 if ( $value_sector->isa( 'DBM::Deep::Sector::DBI::Reference' ) ) {
136 $sector->write_data({
138 value => $value_sector,
140 $value_sector->increment_refcount;
143 $sector->write_data({
145 value => $value_sector->clone,
152 # exists returns '', not undefined.
155 my ($obj, $key) = @_;
157 my $sector = $self->load_sector( $obj->_base_offset, 'refs' )
160 # if ( $sector->staleness != $obj->_staleness ) {
164 my $data = $sector->get_data_for({
165 # key_md5 => $self->_apply_digest( $key ),
170 # exists() returns 1 or '' for true/false.
171 return $data ? 1 : '';
176 my ($obj, $key) = @_;
178 my $sector = $self->load_sector( $obj->_base_offset, 'refs' )
181 # if ( $sector->staleness != $obj->_staleness ) {
185 return $sector->delete_key({
186 # key_md5 => $self->_apply_digest( $key ),
194 my ($obj, $key, $value) = @_;
196 my $r = Scalar::Util::reftype( $value ) || '';
199 last if $r eq 'HASH';
200 last if $r eq 'ARRAY';
202 DBM::Deep->_throw_error(
203 "Storage of references of type '$r' is not supported."
207 # Load the reference entry
208 # Determine if the row was deleted under us
211 my $sector = $self->load_sector( $obj->_base_offset, 'refs' )
212 or die "Cannot load sector at '@{[$obj->_base_offset]}'\n";;
215 if ( $r eq 'ARRAY' || $r eq 'HASH' ) {
217 if ( $r eq 'ARRAY' ) {
218 $tmpvar = tied @$value;
219 } elsif ( $r eq 'HASH' ) {
220 $tmpvar = tied %$value;
224 my $is_dbm_deep = eval { local $SIG{'__DIE__'}; $tmpvar->isa( 'DBM::Deep' ); };
226 unless ( $is_dbm_deep ) {
227 DBM::Deep->_throw_error( "Cannot store something that is tied." );
230 unless ( $tmpvar->_engine->storage == $self->storage ) {
231 DBM::Deep->_throw_error( "Cannot store values across DBM::Deep files. Please use export() instead." );
234 # Load $tmpvar's sector
236 # First, verify if we're storing the same thing to this spot. If we
237 # are, then this should be a no-op. -EJS, 2008-05-19
239 # See whether or not we are storing ourselves to ourself.
240 # Write the sector as data in this reference (keyed by $key)
241 my $value_sector = $self->load_sector( $tmpvar->_base_offset, 'refs' );
242 $sector->write_data({
244 # key_md5 => $self->_apply_digest( $key ),
245 value => $value_sector,
247 $value_sector->increment_refcount;
252 $type = substr( $r, 0, 1 );
253 $class = 'DBM::Deep::Sector::DBI::Reference';
256 if ( tied($value) ) {
257 DBM::Deep->_throw_error( "Cannot store something that is tied." );
260 $class = 'DBM::Deep::Sector::DBI::Scalar';
264 # Create this after loading the reference sector in case something bad
265 # happens. This way, we won't allocate value sector(s) needlessly.
266 my $value_sector = $class->new({
272 $sector->write_data({
274 # key_md5 => $self->_apply_digest( $key ),
275 value => $value_sector,
278 # This code is to make sure we write all the values in the $value to the
279 # disk and to make sure all changes to $value after the assignment are
280 # reflected on disk. This may be counter-intuitive at first, but it is
282 # NOTE - simply tying $value won't perform a STORE on each value. Hence,
283 # the copy to a temp value.
284 if ( $r eq 'ARRAY' ) {
286 tie @$value, 'DBM::Deep', {
287 base_offset => $value_sector->offset,
288 # staleness => $value_sector->staleness,
289 storage => $self->storage,
293 bless $value, 'DBM::Deep::Array' unless Scalar::Util::blessed( $value );
295 elsif ( $r eq 'HASH' ) {
297 tie %$value, 'DBM::Deep', {
298 base_offset => $value_sector->offset,
299 # staleness => $value_sector->staleness,
300 storage => $self->storage,
305 bless $value, 'DBM::Deep::Hash' unless Scalar::Util::blessed( $value );