Speed up clear()
[dbsrgits/DBM-Deep.git] / lib / DBM / Deep / Engine / Sector / Reference.pm
CommitLineData
f0276afb 1package DBM::Deep::Engine::Sector::Reference;
2
3use 5.006_000;
4
5use strict;
6use warnings FATAL => 'all';
7
8use base qw( DBM::Deep::Engine::Sector::Data );
9
5ae752e2 10my $STALE_SIZE = 2;
11
12# Please refer to the pack() documentation for further information
13my %StP = (
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)
18);
19
f0276afb 20sub _init {
21 my $self = shift;
22
23 my $e = $self->engine;
24
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;
28
29 my $class_offset = 0;
30 if ( defined $classname ) {
31 my $class_sector = DBM::Deep::Engine::Sector::Scalar->new({
32 engine => $e,
33 data => $classname,
34 });
35 $class_offset = $class_sector->offset;
36 }
37
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
46 );
47 }
48 else {
49 $self->{type} = $e->storage->read_at( $self->offset, 1 );
50 }
51
52 $self->{staleness} = unpack(
53 $StP{$STALE_SIZE},
54 $e->storage->read_at( $self->offset + $e->SIG_SIZE, $STALE_SIZE ),
55 );
56
57 return;
58}
59
60sub staleness { $_[0]{staleness} }
61
62sub get_data_location_for {
63 my $self = shift;
64 my ($args) = @_;
65
66 # Assume that the head is not allowed unless otherwise specified.
67 $args->{allow_head} = 0 unless exists $args->{allow_head};
68
69 # Assume we don't create a new blist location unless otherwise specified.
70 $args->{create} = 0 unless exists $args->{create};
71
72 my $blist = $self->get_bucket_list({
73 key_md5 => $args->{key_md5},
74 key => $args->{key},
75 create => $args->{create},
76 });
77 return unless $blist && $blist->{found};
78
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
81 # function call.
82 my $location = $blist->get_data_location_for({
83 allow_head => $args->{allow_head},
84 }) or return;
85
86 return $location;
87}
88
89sub get_data_for {
90 my $self = shift;
91 my ($args) = @_;
92
93 my $location = $self->get_data_location_for( $args )
94 or return;
95
96 return $self->engine->_load_sector( $location );
97}
98
99sub write_data {
100 my $self = shift;
101 my ($args) = @_;
102
103 my $blist = $self->get_bucket_list({
104 key_md5 => $args->{key_md5},
105 key => $args->{key},
106 create => 1,
107 }) or DBM::Deep->_throw_error( "How did write_data fail (no blist)?!" );
108
109 # Handle any transactional bookkeeping.
110 if ( $self->engine->trans_id ) {
111 if ( ! $blist->has_md5 ) {
112 $blist->mark_deleted({
113 trans_id => 0,
114 });
115 }
116 }
117 else {
118 my @trans_ids = $self->engine->get_running_txn_ids;
119 if ( $blist->has_md5 ) {
120 if ( @trans_ids ) {
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,
125 allow_head => 0,
126 });
127 $blist->write_md5({
128 trans_id => $other_trans_id,
129 key => $args->{key},
130 key_md5 => $args->{key_md5},
131 value => $old_value->clone,
132 });
133 }
134 }
135 }
136 else {
137 if ( @trans_ids ) {
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,
143 });
144 }
145 }
146 }
147 }
148
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;
153 }
154
155 $blist->write_md5({
156 key => $args->{key},
157 key_md5 => $args->{key_md5},
158 value => $args->{value},
159 });
160}
161
162sub delete_key {
163 my $self = shift;
164 my ($args) = @_;
165
c803879b 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:
168 # my $x = {};
169 # delete $x->{foo}{bar};
f0276afb 170 my $blist = $self->get_bucket_list({
171 key_md5 => $args->{key_md5},
c803879b 172 }) or return;
f0276afb 173
174 # Save the location so that we can free the data
175 my $location = $blist->get_data_location_for({
176 allow_head => 0,
177 });
178 my $old_value = $location && $self->engine->_load_sector( $location );
179
180 my @trans_ids = $self->engine->get_running_txn_ids;
181
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 ) {
185 if ( @trans_ids ) {
186 foreach my $other_trans_id ( @trans_ids ) {
187 next if $blist->get_data_location_for({ trans_id => $other_trans_id, allow_head => 0 });
188 $blist->write_md5({
189 trans_id => $other_trans_id,
190 key => $args->{key},
191 key_md5 => $args->{key_md5},
192 value => $old_value->clone,
193 });
194 }
195 }
196 }
197
198 my $data;
199 if ( @trans_ids ) {
200 $blist->mark_deleted( $args );
201
202 if ( $old_value ) {
203 $data = $old_value->data({ export => 1 });
204 $old_value->free;
205 }
206 }
207 else {
208 $data = $blist->delete_md5( $args );
209 }
210
211 return $data;
212}
213
d8f1fa98 214sub clear {
215 my $self = shift;
216
217 my $blist_loc = $self->get_blist_loc or return;
218
219 my $engine = $self->engine;
220
221 if($engine->get_running_txn_ids) {
222 # ~~~ Temporary; the code below this block needs to be modified to
223 # take transactions into account.
224 $self->data->_clear;
225 return;
226 }
227
228 my $sector = $engine->_load_sector( $blist_loc )
229 or DBM::Deep->_throw_error(
230 "Cannot read sector at $blist_loc in clear()"
231 );
232
233 # Set blist offset to 0
234 $engine->storage->print_at( $self->offset + $self->base_size,
235 pack( $StP{$engine->byte_size}, 0 ),
236 );
237
238 # Free the blist
239 $sector->free;
240
241 return;
242}
243
f0276afb 244sub get_blist_loc {
245 my $self = shift;
246
247 my $e = $self->engine;
248 my $blist_loc = $e->storage->read_at( $self->offset + $self->base_size, $e->byte_size );
249 return unpack( $StP{$e->byte_size}, $blist_loc );
250}
251
252sub get_bucket_list {
253 my $self = shift;
254 my ($args) = @_;
255 $args ||= {};
256
257 # XXX Add in check here for recycling?
258
259 my $engine = $self->engine;
260
261 my $blist_loc = $self->get_blist_loc;
262
263 # There's no index or blist yet
264 unless ( $blist_loc ) {
265 return unless $args->{create};
266
267 my $blist = DBM::Deep::Engine::Sector::BucketList->new({
268 engine => $engine,
269 key_md5 => $args->{key_md5},
270 });
271
272 $engine->storage->print_at( $self->offset + $self->base_size,
273 pack( $StP{$engine->byte_size}, $blist->offset ),
274 );
275
276 return $blist;
277 }
278
279 my $sector = $engine->_load_sector( $blist_loc )
280 or DBM::Deep->_throw_error( "Cannot read sector at $blist_loc in get_bucket_list()" );
281 my $i = 0;
282 my $last_sector = undef;
283 while ( $sector->isa( 'DBM::Deep::Engine::Sector::Index' ) ) {
284 $blist_loc = $sector->get_entry( ord( substr( $args->{key_md5}, $i++, 1 ) ) );
285 $last_sector = $sector;
286 if ( $blist_loc ) {
287 $sector = $engine->_load_sector( $blist_loc )
288 or DBM::Deep->_throw_error( "Cannot read sector at $blist_loc in get_bucket_list()" );
289 }
290 else {
291 $sector = undef;
292 last;
293 }
294 }
295
296 # This means we went through the Index sector(s) and found an empty slot
297 unless ( $sector ) {
298 return unless $args->{create};
299
300 DBM::Deep->_throw_error( "No last_sector when attempting to build a new entry" )
301 unless $last_sector;
302
303 my $blist = DBM::Deep::Engine::Sector::BucketList->new({
304 engine => $engine,
305 key_md5 => $args->{key_md5},
306 });
307
308 $last_sector->set_entry( ord( substr( $args->{key_md5}, $i - 1, 1 ) ) => $blist->offset );
309
310 return $blist;
311 }
312
313 $sector->find_md5( $args->{key_md5} );
314
315 # See whether or not we need to reindex the bucketlist
39baa1fd 316 # Yes, the double-braces are there for a reason. if() doesn't create a
317 # redo-able block, so we have to create a bare block within the if() for
318 # redo-purposes.
319 # Patch and idea submitted by sprout@cpan.org. -RobK, 2008-01-09
f0276afb 320 if ( !$sector->has_md5 && $args->{create} && $sector->{idx} == -1 ) {{
321 my $redo;
322
323 my $new_index = DBM::Deep::Engine::Sector::Index->new({
324 engine => $engine,
325 });
326
327 my %blist_cache;
328 #XXX q.v. the comments for this function.
329 foreach my $entry ( $sector->chopped_up ) {
330 my ($spot, $md5) = @{$entry};
331 my $idx = ord( substr( $md5, $i, 1 ) );
332
333 # XXX This is inefficient
334 my $blist = $blist_cache{$idx}
335 ||= DBM::Deep::Engine::Sector::BucketList->new({
336 engine => $engine,
337 });
338
339 $new_index->set_entry( $idx => $blist->offset );
340
341 my $new_spot = $blist->write_at_next_open( $md5 );
342 $engine->reindex_entry( $spot => $new_spot );
343 }
344
345 # Handle the new item separately.
346 {
347 my $idx = ord( substr( $args->{key_md5}, $i, 1 ) );
348
349 # If all the previous blist's items have been thrown into one
350 # blist and the new item belongs in there too, we need
351 # another index.
352 if ( keys %blist_cache == 1 and each %blist_cache == $idx ) {
353 ++$i, ++$redo;
354 } else {
355 my $blist = $blist_cache{$idx}
356 ||= DBM::Deep::Engine::Sector::BucketList->new({
357 engine => $engine,
358 });
359
360 $new_index->set_entry( $idx => $blist->offset );
361
362 #XXX THIS IS HACKY!
363 $blist->find_md5( $args->{key_md5} );
364 $blist->write_md5({
365 key => $args->{key},
366 key_md5 => $args->{key_md5},
367 value => DBM::Deep::Engine::Sector::Null->new({
368 engine => $engine,
369 data => undef,
370 }),
371 });
372 }
373# my $blist = $blist_cache{$idx}
374# ||= DBM::Deep::Engine::Sector::BucketList->new({
375# engine => $engine,
376# });
377#
378# $new_index->set_entry( $idx => $blist->offset );
379#
380# #XXX THIS IS HACKY!
381# $blist->find_md5( $args->{key_md5} );
382# $blist->write_md5({
383# key => $args->{key},
384# key_md5 => $args->{key_md5},
385# value => DBM::Deep::Engine::Sector::Null->new({
386# engine => $engine,
387# data => undef,
388# }),
389# });
390 }
391
392 if ( $last_sector ) {
393 $last_sector->set_entry(
394 ord( substr( $args->{key_md5}, $i - 1, 1 ) ),
395 $new_index->offset,
396 );
397 } else {
398 $engine->storage->print_at( $self->offset + $self->base_size,
399 pack( $StP{$engine->byte_size}, $new_index->offset ),
400 );
401 }
402
403 $sector->clear;
404 $sector->free;
405
406 if ( $redo ) {
407 (undef, $sector) = %blist_cache;
408 $last_sector = $new_index;
409 redo;
410 }
411
412 $sector = $blist_cache{ ord( substr( $args->{key_md5}, $i, 1 ) ) };
413 $sector->find_md5( $args->{key_md5} );
414 }}
415
416 return $sector;
417}
418
419sub get_class_offset {
420 my $self = shift;
421
422 my $e = $self->engine;
423 return unpack(
424 $StP{$e->byte_size},
425 $e->storage->read_at(
426 $self->offset + $self->base_size + 1 * $e->byte_size, $e->byte_size,
427 ),
428 );
429}
430
431sub get_classname {
432 my $self = shift;
433
434 my $class_offset = $self->get_class_offset;
435
436 return unless $class_offset;
437
438 return $self->engine->_load_sector( $class_offset )->data;
439}
440
441sub data {
442 my $self = shift;
443 my ($args) = @_;
444 $args ||= {};
445
446 my $obj;
447 unless ( $obj = $self->engine->cache->{ $self->offset } ) {
448 $obj = DBM::Deep->new({
449 type => $self->type,
450 base_offset => $self->offset,
451 staleness => $self->staleness,
452 storage => $self->engine->storage,
453 engine => $self->engine,
454 });
455
456 if ( $self->engine->storage->{autobless} ) {
457 my $classname = $self->get_classname;
458 if ( defined $classname ) {
459 bless $obj, $classname;
460 }
461 }
462
463 $self->engine->cache->{$self->offset} = $obj;
464 }
465
466 # We're not exporting, so just return.
467 unless ( $args->{export} ) {
468 return $obj;
469 }
470
471 # We shouldn't export if this is still referred to.
472 if ( $self->get_refcount > 1 ) {
473 return $obj;
474 }
475
476 return $obj->export;
477}
478
479sub free {
480 my $self = shift;
481
482 # We're not ready to be removed yet.
483 if ( $self->decrement_refcount > 0 ) {
484 return;
485 }
486
487 # Rebless the object into DBM::Deep::Null.
488 eval { %{ $self->engine->cache->{ $self->offset } } = (); };
489 eval { @{ $self->engine->cache->{ $self->offset } } = (); };
490 bless $self->engine->cache->{ $self->offset }, 'DBM::Deep::Null';
491 delete $self->engine->cache->{ $self->offset };
492
493 my $blist_loc = $self->get_blist_loc;
494 $self->engine->_load_sector( $blist_loc )->free if $blist_loc;
495
496 my $class_loc = $self->get_class_offset;
497 $self->engine->_load_sector( $class_loc )->free if $class_loc;
498
499 $self->SUPER::free();
500}
501
502sub increment_refcount {
503 my $self = shift;
504
505 my $refcount = $self->get_refcount;
506
507 $refcount++;
508
509 $self->write_refcount( $refcount );
510
511 return $refcount;
512}
513
514sub decrement_refcount {
515 my $self = shift;
516
517 my $refcount = $self->get_refcount;
518
519 $refcount--;
520
521 $self->write_refcount( $refcount );
522
523 return $refcount;
524}
525
526sub get_refcount {
527 my $self = shift;
528
529 my $e = $self->engine;
530 return unpack(
531 $StP{$e->byte_size},
532 $e->storage->read_at(
533 $self->offset + $self->base_size + 2 * $e->byte_size, $e->byte_size,
534 ),
535 );
536}
537
538sub write_refcount {
539 my $self = shift;
540 my ($num) = @_;
541
542 my $e = $self->engine;
543 $e->storage->print_at(
544 $self->offset + $self->base_size + 2 * $e->byte_size,
545 pack( $StP{$e->byte_size}, $num ),
546 );
547}
548
5491;
550__END__