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 value to the other
183 # 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 $data = $old_value->data({ export => 1 });
208 $data = $blist->delete_md5( $args );
217 my $e = $self->engine;
218 my $blist_loc = $e->storage->read_at( $self->offset + $self->base_size, $e->byte_size );
219 return unpack( $StP{$e->byte_size}, $blist_loc );
222 sub get_bucket_list {
227 # XXX Add in check here for recycling?
229 my $engine = $self->engine;
231 my $blist_loc = $self->get_blist_loc;
233 # There's no index or blist yet
234 unless ( $blist_loc ) {
235 return unless $args->{create};
237 my $blist = DBM::Deep::Sector::File::BucketList->new({
239 key_md5 => $args->{key_md5},
242 $engine->storage->print_at( $self->offset + $self->base_size,
243 pack( $StP{$engine->byte_size}, $blist->offset ),
249 my $sector = $engine->load_sector( $blist_loc )
250 or DBM::Deep->_throw_error( "Cannot read sector at $blist_loc in get_bucket_list()" );
252 my $last_sector = undef;
253 while ( $sector->isa( 'DBM::Deep::Sector::File::Index' ) ) {
254 $blist_loc = $sector->get_entry( ord( substr( $args->{key_md5}, $i++, 1 ) ) );
255 $last_sector = $sector;
257 $sector = $engine->load_sector( $blist_loc )
258 or DBM::Deep->_throw_error( "Cannot read sector at $blist_loc in get_bucket_list()" );
266 # This means we went through the Index sector(s) and found an empty slot
268 return unless $args->{create};
270 DBM::Deep->_throw_error( "No last_sector when attempting to build a new entry" )
273 my $blist = DBM::Deep::Sector::File::BucketList->new({
275 key_md5 => $args->{key_md5},
278 $last_sector->set_entry( ord( substr( $args->{key_md5}, $i - 1, 1 ) ) => $blist->offset );
283 $sector->find_md5( $args->{key_md5} );
285 # See whether or not we need to reindex the bucketlist
286 # Yes, the double-braces are there for a reason. if() doesn't create a redo-able block,
287 # so we have to create a bare block within the if() for redo-purposes. Patch and idea
288 # submitted by sprout@cpan.org. -RobK, 2008-01-09
289 if ( !$sector->has_md5 && $args->{create} && $sector->{idx} == -1 ) {{
292 my $new_index = DBM::Deep::Sector::File::Index->new({
297 #XXX q.v. the comments for this function.
298 foreach my $entry ( $sector->chopped_up ) {
299 my ($spot, $md5) = @{$entry};
300 my $idx = ord( substr( $md5, $i, 1 ) );
302 # XXX This is inefficient
303 my $blist = $blist_cache{$idx}
304 ||= DBM::Deep::Sector::File::BucketList->new({
308 $new_index->set_entry( $idx => $blist->offset );
310 my $new_spot = $blist->write_at_next_open( $md5 );
311 $engine->reindex_entry( $spot => $new_spot );
314 # Handle the new item separately.
316 my $idx = ord( substr( $args->{key_md5}, $i, 1 ) );
318 # If all the previous blist's items have been thrown into one
319 # blist and the new item belongs in there too, we need
321 if ( keys %blist_cache == 1 and each %blist_cache == $idx ) {
324 my $blist = $blist_cache{$idx}
325 ||= DBM::Deep::Sector::File::BucketList->new({
329 $new_index->set_entry( $idx => $blist->offset );
332 $blist->find_md5( $args->{key_md5} );
335 key_md5 => $args->{key_md5},
336 value => DBM::Deep::Sector::File::Null->new({
342 # my $blist = $blist_cache{$idx}
343 # ||= DBM::Deep::Sector::File::BucketList->new({
347 # $new_index->set_entry( $idx => $blist->offset );
349 # #XXX THIS IS HACKY!
350 # $blist->find_md5( $args->{key_md5} );
351 # $blist->write_md5({
352 # key => $args->{key},
353 # key_md5 => $args->{key_md5},
354 # value => DBM::Deep::Sector::File::Null->new({
361 if ( $last_sector ) {
362 $last_sector->set_entry(
363 ord( substr( $args->{key_md5}, $i - 1, 1 ) ),
367 $engine->storage->print_at( $self->offset + $self->base_size,
368 pack( $StP{$engine->byte_size}, $new_index->offset ),
376 (undef, $sector) = %blist_cache;
377 $last_sector = $new_index;
381 $sector = $blist_cache{ ord( substr( $args->{key_md5}, $i, 1 ) ) };
382 $sector->find_md5( $args->{key_md5} );
388 sub get_class_offset {
391 my $e = $self->engine;
394 $e->storage->read_at(
395 $self->offset + $self->base_size + 1 * $e->byte_size, $e->byte_size,
403 my $class_offset = $self->get_class_offset;
405 return unless $class_offset;
407 return $self->engine->load_sector( $class_offset )->data;
416 unless ( $obj = $self->engine->cache->{ $self->offset } ) {
417 $obj = DBM::Deep->new({
419 base_offset => $self->offset,
420 staleness => $self->staleness,
421 storage => $self->engine->storage,
422 engine => $self->engine,
425 if ( $self->engine->storage->{autobless} ) {
426 my $classname = $self->get_classname;
427 if ( defined $classname ) {
428 bless $obj, $classname;
432 $self->engine->cache->{$self->offset} = $obj;
435 # We're not exporting, so just return.
436 unless ( $args->{export} ) {
440 # We shouldn't export if this is still referred to.
441 if ( $self->get_refcount > 1 ) {
451 # We're not ready to be removed yet.
452 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 ),