1 package DBM::Deep::Sector::File::BucketList;
6 use warnings FATAL => 'all';
8 use base qw( DBM::Deep::Sector::File );
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 $engine = $self->engine;
25 unless ( $self->offset ) {
26 my $leftover = $self->size - $self->base_size;
28 $self->{offset} = $engine->_request_blist_sector( $self->size );
29 $engine->storage->print_at( $self->offset, $engine->SIG_BLIST ); # Sector type
30 # Skip staleness counter
31 $engine->storage->print_at( $self->offset + $self->base_size,
32 chr(0) x $leftover, # Zero-fill the data
36 if ( $self->{key_md5} ) {
45 $self->engine->storage->print_at( $self->offset + $self->base_size,
46 chr(0) x ($self->size - $self->base_size), # Zero-fill the data
52 unless ( $self->{size} ) {
53 my $e = $self->engine;
54 # Base + numbuckets * bucketsize
55 $self->{size} = $self->base_size + $e->max_buckets * $self->bucket_size;
60 sub free_meth { '_add_free_blist_sector' }
65 my $e = $self->engine;
66 foreach my $bucket ( $self->chopped_up ) {
67 my $rest = $bucket->[-1];
69 # Delete the keysector
70 my $l = unpack( $StP{$e->byte_size}, substr( $rest, $e->hash_size, $e->byte_size ) );
71 my $s = $e->load_sector( $l ); $s->free if $s;
73 # Delete the HEAD sector
74 $l = unpack( $StP{$e->byte_size},
76 $e->hash_size + $e->byte_size,
80 $s = $e->load_sector( $l ); $s->free if $s;
82 foreach my $txn ( 0 .. $e->num_txns - 2 ) {
83 my $l = unpack( $StP{$e->byte_size},
85 $e->hash_size + 2 * $e->byte_size + $txn * ($e->byte_size + $STALE_SIZE),
89 my $s = $e->load_sector( $l ); $s->free if $s;
98 unless ( $self->{bucket_size} ) {
99 my $e = $self->engine;
100 # Key + head (location) + transactions (location + staleness-counter)
101 my $location_size = $e->byte_size + $e->byte_size + ($e->num_txns - 1) * ($e->byte_size + $STALE_SIZE);
102 $self->{bucket_size} = $e->hash_size + $location_size;
104 return $self->{bucket_size};
107 # XXX This is such a poor hack. I need to rethink this code.
111 my $e = $self->engine;
114 foreach my $idx ( 0 .. $e->max_buckets - 1 ) {
115 my $spot = $self->offset + $self->base_size + $idx * $self->bucket_size;
116 my $md5 = $e->storage->read_at( $spot, $e->hash_size );
118 #XXX If we're chopping, why would we ever have the blank_md5?
119 last if $md5 eq $e->blank_md5;
121 my $rest = $e->storage->read_at( undef, $self->bucket_size - $e->hash_size );
122 push @buckets, [ $spot, $md5 . $rest ];
128 sub write_at_next_open {
132 #XXX This is such a hack!
133 $self->{_next_open} = 0 unless exists $self->{_next_open};
135 my $spot = $self->offset + $self->base_size + $self->{_next_open}++ * $self->bucket_size;
136 $self->engine->storage->print_at( $spot, $entry );
143 unless ( exists $self->{found} ) {
146 return $self->{found};
152 $self->{found} = undef;
156 $self->{key_md5} = shift;
159 # If we don't have an MD5, then what are we supposed to do?
160 unless ( exists $self->{key_md5} ) {
161 DBM::Deep->_throw_error( "Cannot find_md5 without a key_md5 set" );
164 my $e = $self->engine;
165 foreach my $idx ( 0 .. $e->max_buckets - 1 ) {
166 my $potential = $e->storage->read_at(
167 $self->offset + $self->base_size + $idx * $self->bucket_size, $e->hash_size,
170 if ( $potential eq $e->blank_md5 ) {
175 if ( $potential eq $self->{key_md5} ) {
189 DBM::Deep->_throw_error( "write_md5: no key" ) unless exists $args->{key};
190 DBM::Deep->_throw_error( "write_md5: no key_md5" ) unless exists $args->{key_md5};
191 DBM::Deep->_throw_error( "write_md5: no value" ) unless exists $args->{value};
193 my $engine = $self->engine;
195 $args->{trans_id} = $engine->trans_id unless exists $args->{trans_id};
197 my $spot = $self->offset + $self->base_size + $self->{idx} * $self->bucket_size;
198 $engine->add_entry( $args->{trans_id}, $spot );
200 unless ($self->{found}) {
201 my $key_sector = DBM::Deep::Sector::File::Scalar->new({
203 data => $args->{key},
206 $engine->storage->print_at( $spot,
208 pack( $StP{$engine->byte_size}, $key_sector->offset ),
214 + $engine->byte_size;
216 if ( $args->{trans_id} ) {
217 $loc += $engine->byte_size + ($args->{trans_id} - 1) * ( $engine->byte_size + $STALE_SIZE );
219 $engine->storage->print_at( $loc,
220 pack( $StP{$engine->byte_size}, $args->{value}->offset ),
221 pack( $StP{$STALE_SIZE}, $engine->get_txn_staleness_counter( $args->{trans_id} ) ),
225 $engine->storage->print_at( $loc,
226 pack( $StP{$engine->byte_size}, $args->{value}->offset ),
236 my $engine = $self->engine;
238 $args->{trans_id} = $engine->trans_id unless exists $args->{trans_id};
240 my $spot = $self->offset + $self->base_size + $self->{idx} * $self->bucket_size;
241 $engine->add_entry( $args->{trans_id}, $spot );
245 + $engine->byte_size;
247 if ( $args->{trans_id} ) {
248 $loc += $engine->byte_size + ($args->{trans_id} - 1) * ( $engine->byte_size + $STALE_SIZE );
250 $engine->storage->print_at( $loc,
251 pack( $StP{$engine->byte_size}, 1 ), # 1 is the marker for deleted
252 pack( $StP{$STALE_SIZE}, $engine->get_txn_staleness_counter( $args->{trans_id} ) ),
256 $engine->storage->print_at( $loc,
257 pack( $StP{$engine->byte_size}, 1 ), # 1 is the marker for deleted
266 my $engine = $self->engine;
267 return undef unless $self->{found};
269 # Save the location so that we can free the data
270 my $location = $self->get_data_location_for({
273 my $key_sector = $self->get_key_for;
275 my $spot = $self->offset + $self->base_size + $self->{idx} * $self->bucket_size;
276 $engine->storage->print_at( $spot,
277 $engine->storage->read_at(
278 $spot + $self->bucket_size,
279 $self->bucket_size * ( $engine->max_buckets - $self->{idx} - 1 ),
281 chr(0) x $self->bucket_size,
286 my $data_sector = $self->engine->load_sector( $location );
287 my $data = $data_sector->data({ export => 1 });
293 sub get_data_location_for {
298 $args->{allow_head} = 0 unless exists $args->{allow_head};
299 $args->{trans_id} = $self->engine->trans_id unless exists $args->{trans_id};
300 $args->{idx} = $self->{idx} unless exists $args->{idx};
302 my $e = $self->engine;
304 my $spot = $self->offset + $self->base_size
305 + $args->{idx} * $self->bucket_size
309 if ( $args->{trans_id} ) {
310 $spot += $e->byte_size + ($args->{trans_id} - 1) * ( $e->byte_size + $STALE_SIZE );
313 my $buffer = $e->storage->read_at(
315 $e->byte_size + $STALE_SIZE,
317 my ($loc, $staleness) = unpack( $StP{$e->byte_size} . ' ' . $StP{$STALE_SIZE}, $buffer );
319 # XXX Merge the two if-clauses below
320 if ( $args->{trans_id} ) {
321 # We have found an entry that is old, so get rid of it
322 if ( $staleness != (my $s = $e->get_txn_staleness_counter( $args->{trans_id} ) ) ) {
323 $e->storage->print_at(
325 pack( $StP{$e->byte_size} . ' ' . $StP{$STALE_SIZE}, (0) x 2 ),
331 # If we're in a transaction and we never wrote to this location, try the
333 if ( $args->{trans_id} && !$loc && $args->{allow_head} ) {
334 return $self->get_data_location_for({
341 return $loc <= 1 ? 0 : $loc;
349 return unless $self->{found};
350 my $location = $self->get_data_location_for({
351 allow_head => $args->{allow_head},
353 return $self->engine->load_sector( $location );
359 $idx = $self->{idx} unless defined $idx;
361 if ( $idx >= $self->engine->max_buckets ) {
362 DBM::Deep->_throw_error( "get_key_for(): Attempting to retrieve $idx" );
365 my $location = $self->engine->storage->read_at(
366 $self->offset + $self->base_size + $idx * $self->bucket_size + $self->engine->hash_size,
367 $self->engine->byte_size,
369 $location = unpack( $StP{$self->engine->byte_size}, $location );
370 DBM::Deep->_throw_error( "get_key_for: No location?" ) unless $location;
372 return $self->engine->load_sector( $location );