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({
90 # key_md5 => $key_md5,
92 value => $value_sector,
96 return $value_sector->data;
108 my ($obj, $old_key, $new_key) = @_;
110 my $sector = $self->load_sector( $obj->_base_offset, 'refs' )
113 # if ( $sector->staleness != $obj->_staleness ) {
117 my $value_sector = $sector->get_data_for({
122 unless ( $value_sector ) {
123 $value_sector = DBM::Deep::Sector::DBI::Scalar->new({
128 $sector->write_data({
130 value => $value_sector,
134 if ( $value_sector->isa( 'DBM::Deep::Sector::DBI::Reference' ) ) {
135 $sector->write_data({
137 value => $value_sector,
139 $value_sector->increment_refcount;
142 $sector->write_data({
144 value => $value_sector->clone,
151 # exists returns '', not undefined.
154 my ($obj, $key) = @_;
156 my $sector = $self->load_sector( $obj->_base_offset, 'refs' )
159 # if ( $sector->staleness != $obj->_staleness ) {
163 my $data = $sector->get_data_for({
164 # key_md5 => $self->_apply_digest( $key ),
169 # exists() returns 1 or '' for true/false.
170 return $data ? 1 : '';
175 my ($obj, $key) = @_;
177 my $sector = $self->load_sector( $obj->_base_offset, 'refs' )
180 # if ( $sector->staleness != $obj->_staleness ) {
184 return $sector->delete_key({
185 # key_md5 => $self->_apply_digest( $key ),
193 my ($obj, $key, $value) = @_;
195 my $r = Scalar::Util::reftype( $value ) || '';
198 last if $r eq 'HASH';
199 last if $r eq 'ARRAY';
201 DBM::Deep->_throw_error(
202 "Storage of references of type '$r' is not supported."
206 # Load the reference entry
207 # Determine if the row was deleted under us
210 my $sector = $self->load_sector( $obj->_base_offset, 'refs' )
211 or die "Cannot load sector at '@{[$obj->_base_offset]}'\n";;
214 if ( $r eq 'ARRAY' || $r eq 'HASH' ) {
216 if ( $r eq 'ARRAY' ) {
217 $tmpvar = tied @$value;
218 } elsif ( $r eq 'HASH' ) {
219 $tmpvar = tied %$value;
223 my $is_dbm_deep = eval { local $SIG{'__DIE__'}; $tmpvar->isa( 'DBM::Deep' ); };
225 unless ( $is_dbm_deep ) {
226 DBM::Deep->_throw_error( "Cannot store something that is tied." );
229 unless ( $tmpvar->_engine->storage == $self->storage ) {
230 DBM::Deep->_throw_error( "Cannot store values across DBM::Deep files. Please use export() instead." );
233 # Load $tmpvar's sector
235 # First, verify if we're storing the same thing to this spot. If we
236 # are, then this should be a no-op. -EJS, 2008-05-19
238 # See whether or not we are storing ourselves to ourself.
239 # Write the sector as data in this reference (keyed by $key)
240 my $value_sector = $self->load_sector( $tmpvar->_base_offset );
241 $sector->write_data({
243 key_md5 => $self->_apply_digest( $key ),
244 value => $value_sector,
246 $value_sector->increment_refcount;
251 $type = substr( $r, 0, 1 );
252 $class = 'DBM::Deep::Sector::DBI::Reference';
255 if ( tied($value) ) {
256 DBM::Deep->_throw_error( "Cannot store something that is tied." );
259 $class = 'DBM::Deep::Sector::DBI::Scalar';
263 # Create this after loading the reference sector in case something bad
264 # happens. This way, we won't allocate value sector(s) needlessly.
265 my $value_sector = $class->new({
271 $sector->write_data({
273 # key_md5 => $self->_apply_digest( $key ),
274 value => $value_sector,
277 # This code is to make sure we write all the values in the $value to the
278 # disk and to make sure all changes to $value after the assignment are
279 # reflected on disk. This may be counter-intuitive at first, but it is
281 # NOTE - simply tying $value won't perform a STORE on each value. Hence,
282 # the copy to a temp value.
283 if ( $r eq 'ARRAY' ) {
285 tie @$value, 'DBM::Deep', {
286 base_offset => $value_sector->offset,
287 # staleness => $value_sector->staleness,
288 storage => $self->storage,
292 bless $value, 'DBM::Deep::Array' unless Scalar::Util::blessed( $value );
294 elsif ( $r eq 'HASH' ) {
296 tie %$value, 'DBM::Deep', {
297 base_offset => $value_sector->offset,
298 # staleness => $value_sector->staleness,
299 storage => $self->storage,
304 bless $value, 'DBM::Deep::Hash' unless Scalar::Util::blessed( $value );