1 package DBM::Deep::Sector::File::Reference;
6 use warnings FATAL => 'all';
8 use base qw( DBM::Deep::Sector::File::Data );
12 # Please refer to the pack() documentation for further information
14 1 => 'C', # Unsigned char value (no order needed as it's just one byte)
15 2 => 'n', # Unsigned short in "network" (big-endian) order
16 4 => 'N', # Unsigned long in "network" (big-endian) order
17 8 => 'Q', # Usigned quad (no order specified, presumably machine-dependent)
23 my $e = $self->engine;
25 unless ( $self->offset ) {
26 my $classname = Scalar::Util::blessed( delete $self->{data} );
27 my $leftover = $self->size - $self->base_size - 3 * $e->byte_size;
30 if ( defined $classname ) {
31 my $class_sector = DBM::Deep::Sector::File::Scalar->new({
35 $class_offset = $class_sector->offset;
38 $self->{offset} = $e->_request_data_sector( $self->size );
39 $e->storage->print_at( $self->offset, $self->type ); # Sector type
40 # Skip staleness counter
41 $e->storage->print_at( $self->offset + $self->base_size,
42 pack( $StP{$e->byte_size}, 0 ), # Index/BList loc
43 pack( $StP{$e->byte_size}, $class_offset ), # Classname loc
44 pack( $StP{$e->byte_size}, 1 ), # Initial refcount
45 chr(0) x $leftover, # Zero-fill the rest
49 $self->{type} = $e->storage->read_at( $self->offset, 1 );
52 $self->{staleness} = unpack(
54 $e->storage->read_at( $self->offset + $e->SIG_SIZE, $STALE_SIZE ),
60 sub staleness { $_[0]{staleness} }
62 sub get_data_location_for {
66 # Assume that the head is not allowed unless otherwise specified.
67 $args->{allow_head} = 0 unless exists $args->{allow_head};
69 # Assume we don't create a new blist location unless otherwise specified.
70 $args->{create} = 0 unless exists $args->{create};
72 my $blist = $self->get_bucket_list({
73 key_md5 => $args->{key_md5},
75 create => $args->{create},
77 return unless $blist && $blist->{found};
79 # At this point, $blist knows where the md5 is. What it -doesn't- know yet
80 # is whether or not this transaction has this key. That's part of the next
82 my $location = $blist->get_data_location_for({
83 allow_head => $args->{allow_head},
93 my $location = $self->get_data_location_for( $args )
96 return $self->engine->load_sector( $location );
103 my $blist = $self->get_bucket_list({
104 key_md5 => $args->{key_md5},
107 }) or DBM::Deep->_throw_error( "How did write_data fail (no blist)?!" );
109 # Handle any transactional bookkeeping.
110 if ( $self->engine->trans_id ) {
111 if ( ! $blist->has_md5 ) {
112 $blist->mark_deleted({
118 my @trans_ids = $self->engine->get_running_txn_ids;
119 if ( $blist->has_md5 ) {
121 my $old_value = $blist->get_data_for;
122 foreach my $other_trans_id ( @trans_ids ) {
123 next if $blist->get_data_location_for({
124 trans_id => $other_trans_id,
128 trans_id => $other_trans_id,
130 key_md5 => $args->{key_md5},
131 value => $old_value->clone,
138 foreach my $other_trans_id ( @trans_ids ) {
139 #XXX This doesn't seem to possible to ever happen . . .
140 next if $blist->get_data_location_for({ trans_id => $other_trans_id, allow_head => 0 });
141 $blist->mark_deleted({
142 trans_id => $other_trans_id,
149 #XXX Is this safe to do transactionally?
150 # Free the place we're about to write to.
151 if ( $blist->get_data_location_for({ allow_head => 0 }) ) {
152 $blist->get_data_for({ allow_head => 0 })->free;
157 key_md5 => $args->{key_md5},
158 value => $args->{value},
166 # This can return nothing if we are deleting an entry in a hashref that was
167 # auto-vivified as part of the delete process. For example:
169 # delete $x->{foo}{bar};
170 my $blist = $self->get_bucket_list({
171 key_md5 => $args->{key_md5},
174 # Save the location so that we can free the data
175 my $location = $blist->get_data_location_for({
178 my $old_value = $location && $self->engine->load_sector( $location );
180 my @trans_ids = $self->engine->get_running_txn_ids;
182 # If we're the HEAD and there are running txns, then we need to clone this
183 # value to the other transactions to preserve Isolation.
184 if ( $self->engine->trans_id == 0 ) {
186 foreach my $other_trans_id ( @trans_ids ) {
187 next if $blist->get_data_location_for({ trans_id => $other_trans_id, allow_head => 0 });
189 trans_id => $other_trans_id,
191 key_md5 => $args->{key_md5},
192 value => $old_value->clone,
200 $blist->mark_deleted( $args );
203 #XXX Is this export => 1 actually doing anything?
204 $data = $old_value->data({ export => 1 });
209 $data = $blist->delete_md5( $args );
218 my $e = $self->engine;
219 my $blist_loc = $e->storage->read_at( $self->offset + $self->base_size, $e->byte_size );
220 return unpack( $StP{$e->byte_size}, $blist_loc );
223 sub get_bucket_list {
228 # XXX Add in check here for recycling?
230 my $engine = $self->engine;
232 my $blist_loc = $self->get_blist_loc;
234 # There's no index or blist yet
235 unless ( $blist_loc ) {
236 return unless $args->{create};
238 my $blist = DBM::Deep::Sector::File::BucketList->new({
240 key_md5 => $args->{key_md5},
243 $engine->storage->print_at( $self->offset + $self->base_size,
244 pack( $StP{$engine->byte_size}, $blist->offset ),
250 my $sector = $engine->load_sector( $blist_loc )
251 or DBM::Deep->_throw_error( "Cannot read sector at $blist_loc in get_bucket_list()" );
253 my $last_sector = undef;
254 while ( $sector->isa( 'DBM::Deep::Sector::File::Index' ) ) {
255 $blist_loc = $sector->get_entry( ord( substr( $args->{key_md5}, $i++, 1 ) ) );
256 $last_sector = $sector;
258 $sector = $engine->load_sector( $blist_loc )
259 or DBM::Deep->_throw_error( "Cannot read sector at $blist_loc in get_bucket_list()" );
267 # This means we went through the Index sector(s) and found an empty slot
269 return unless $args->{create};
271 DBM::Deep->_throw_error( "No last_sector when attempting to build a new entry" )
274 my $blist = DBM::Deep::Sector::File::BucketList->new({
276 key_md5 => $args->{key_md5},
279 $last_sector->set_entry( ord( substr( $args->{key_md5}, $i - 1, 1 ) ) => $blist->offset );
284 $sector->find_md5( $args->{key_md5} );
286 # See whether or not we need to reindex the bucketlist
287 # Yes, the double-braces are there for a reason. if() doesn't create a redo-able block,
288 # so we have to create a bare block within the if() for redo-purposes. Patch and idea
289 # submitted by sprout@cpan.org. -RobK, 2008-01-09
290 if ( !$sector->has_md5 && $args->{create} && $sector->{idx} == -1 ) {{
293 my $new_index = DBM::Deep::Sector::File::Index->new({
298 #XXX q.v. the comments for this function.
299 foreach my $entry ( $sector->chopped_up ) {
300 my ($spot, $md5) = @{$entry};
301 my $idx = ord( substr( $md5, $i, 1 ) );
303 # XXX This is inefficient
304 my $blist = $blist_cache{$idx}
305 ||= DBM::Deep::Sector::File::BucketList->new({
309 $new_index->set_entry( $idx => $blist->offset );
311 my $new_spot = $blist->write_at_next_open( $md5 );
312 $engine->reindex_entry( $spot => $new_spot );
315 # Handle the new item separately.
317 my $idx = ord( substr( $args->{key_md5}, $i, 1 ) );
319 # If all the previous blist's items have been thrown into one
320 # blist and the new item belongs in there too, we need
322 if ( keys %blist_cache == 1 and each %blist_cache == $idx ) {
325 my $blist = $blist_cache{$idx}
326 ||= DBM::Deep::Sector::File::BucketList->new({
330 $new_index->set_entry( $idx => $blist->offset );
333 $blist->find_md5( $args->{key_md5} );
336 key_md5 => $args->{key_md5},
337 value => DBM::Deep::Sector::File::Null->new({
343 # my $blist = $blist_cache{$idx}
344 # ||= DBM::Deep::Sector::File::BucketList->new({
348 # $new_index->set_entry( $idx => $blist->offset );
350 # #XXX THIS IS HACKY!
351 # $blist->find_md5( $args->{key_md5} );
352 # $blist->write_md5({
353 # key => $args->{key},
354 # key_md5 => $args->{key_md5},
355 # value => DBM::Deep::Sector::File::Null->new({
362 if ( $last_sector ) {
363 $last_sector->set_entry(
364 ord( substr( $args->{key_md5}, $i - 1, 1 ) ),
368 $engine->storage->print_at( $self->offset + $self->base_size,
369 pack( $StP{$engine->byte_size}, $new_index->offset ),
377 (undef, $sector) = %blist_cache;
378 $last_sector = $new_index;
382 $sector = $blist_cache{ ord( substr( $args->{key_md5}, $i, 1 ) ) };
383 $sector->find_md5( $args->{key_md5} );
389 sub get_class_offset {
392 my $e = $self->engine;
395 $e->storage->read_at(
396 $self->offset + $self->base_size + 1 * $e->byte_size, $e->byte_size,
404 my $class_offset = $self->get_class_offset;
406 return unless $class_offset;
408 return $self->engine->load_sector( $class_offset )->data;
411 # Look to hoist this method into a ::Reference trait
418 unless ( $obj = $self->engine->cache->{ $self->offset } ) {
419 $obj = DBM::Deep->new({
421 base_offset => $self->offset,
422 staleness => $self->staleness,
423 storage => $self->engine->storage,
424 engine => $self->engine,
427 if ( $self->engine->storage->{autobless} ) {
428 my $classname = $self->get_classname;
429 if ( defined $classname ) {
430 bless $obj, $classname;
434 $self->engine->cache->{$self->offset} = $obj;
437 # We're not exporting, so just return.
438 unless ( $args->{export} ) {
442 # We shouldn't export if this is still referred to.
443 if ( $self->get_refcount > 1 ) {
453 # We're not ready to be removed yet.
454 return if $self->decrement_refcount > 0;
456 # Rebless the object into DBM::Deep::Null.
457 eval { %{ $self->engine->cache->{ $self->offset } } = (); };
458 eval { @{ $self->engine->cache->{ $self->offset } } = (); };
459 bless $self->engine->cache->{ $self->offset }, 'DBM::Deep::Null';
460 delete $self->engine->cache->{ $self->offset };
462 my $blist_loc = $self->get_blist_loc;
463 $self->engine->load_sector( $blist_loc )->free if $blist_loc;
465 my $class_loc = $self->get_class_offset;
466 $self->engine->load_sector( $class_loc )->free if $class_loc;
468 $self->SUPER::free();
471 sub increment_refcount {
474 my $refcount = $self->get_refcount;
478 $self->write_refcount( $refcount );
483 sub decrement_refcount {
486 my $refcount = $self->get_refcount;
490 $self->write_refcount( $refcount );
498 my $e = $self->engine;
501 $e->storage->read_at(
502 $self->offset + $self->base_size + 2 * $e->byte_size, $e->byte_size,
511 my $e = $self->engine;
512 $e->storage->print_at(
513 $self->offset + $self->base_size + 2 * $e->byte_size,
514 pack( $StP{$e->byte_size}, $num ),