Commit | Line | Data |
a20d9a3f |
1 | package DBM::Deep::Engine; |
2 | |
b48ae6ec |
3 | use 5.006_000; |
460b1067 |
4 | |
a20d9a3f |
5 | use strict; |
f72b2dfb |
6 | use warnings; |
a20d9a3f |
7 | |
9f83dc31 |
8 | our $VERSION = q(1.0004); |
86867f3a |
9 | |
359a01ac |
10 | use Scalar::Util (); |
a20d9a3f |
11 | |
21838116 |
12 | # File-wide notes: |
9a63e1f2 |
13 | # * Every method in here assumes that the storage has been appropriately |
c3aafc14 |
14 | # safeguarded. This can be anything from flock() to some sort of manual |
15 | # mutex. But, it's the caller's responsability to make sure that this has |
16 | # been done. |
21838116 |
17 | |
8db25060 |
18 | # Setup file and tag signatures. These should never change. |
8db25060 |
19 | sub SIG_FILE () { 'DPDB' } |
460b1067 |
20 | sub SIG_HEADER () { 'h' } |
8db25060 |
21 | sub SIG_HASH () { 'H' } |
22 | sub SIG_ARRAY () { 'A' } |
8db25060 |
23 | sub SIG_NULL () { 'N' } |
24 | sub SIG_DATA () { 'D' } |
25 | sub SIG_INDEX () { 'I' } |
26 | sub SIG_BLIST () { 'B' } |
7b1e1aa1 |
27 | sub SIG_FREE () { 'F' } |
8db25060 |
28 | sub SIG_SIZE () { 1 } |
f72b2dfb |
29 | |
30 | my $STALE_SIZE = 2; |
8db25060 |
31 | |
9a63e1f2 |
32 | # Please refer to the pack() documentation for further information |
33 | my %StP = ( |
f72b2dfb |
34 | 1 => 'C', # Unsigned char value (no order needed as it's just one byte) |
9a63e1f2 |
35 | 2 => 'n', # Unsigned short in "network" (big-endian) order |
36 | 4 => 'N', # Unsigned long in "network" (big-endian) order |
37 | 8 => 'Q', # Usigned quad (no order specified, presumably machine-dependent) |
38 | ); |
83371fe3 |
39 | |
c3aafc14 |
40 | ################################################################################ |
c3aafc14 |
41 | |
612969fb |
42 | sub new { |
43 | my $class = shift; |
44 | my ($args) = @_; |
45 | |
46 | my $self = bless { |
9a63e1f2 |
47 | byte_size => 4, |
48 | |
49 | digest => undef, |
50 | hash_size => 16, # In bytes |
51 | hash_chars => 256, # Number of chars the algorithm uses per byte |
612969fb |
52 | max_buckets => 16, |
f72b2dfb |
53 | num_txns => 1, # The HEAD |
9a63e1f2 |
54 | trans_id => 0, # Default to the HEAD |
460b1067 |
55 | |
f72b2dfb |
56 | data_sector_size => 64, # Size in bytes of each data sector |
57 | |
9a63e1f2 |
58 | entries => {}, # This is the list of entries for transactions |
83371fe3 |
59 | storage => undef, |
612969fb |
60 | }, $class; |
61 | |
f72b2dfb |
62 | # Never allow byte_size to be set directly. |
63 | delete $args->{byte_size}; |
e0098e7f |
64 | if ( defined $args->{pack_size} ) { |
65 | if ( lc $args->{pack_size} eq 'small' ) { |
9a63e1f2 |
66 | $args->{byte_size} = 2; |
e0098e7f |
67 | } |
68 | elsif ( lc $args->{pack_size} eq 'medium' ) { |
9a63e1f2 |
69 | $args->{byte_size} = 4; |
e0098e7f |
70 | } |
71 | elsif ( lc $args->{pack_size} eq 'large' ) { |
9a63e1f2 |
72 | $args->{byte_size} = 8; |
e0098e7f |
73 | } |
74 | else { |
9a63e1f2 |
75 | DBM::Deep->_throw_error( "Unknown pack_size value: '$args->{pack_size}'" ); |
e0098e7f |
76 | } |
77 | } |
78 | |
fde3db1a |
79 | # Grab the parameters we want to use |
80 | foreach my $param ( keys %$self ) { |
81 | next unless exists $args->{$param}; |
3e9498a1 |
82 | $self->{$param} = $args->{$param}; |
fde3db1a |
83 | } |
84 | |
f72b2dfb |
85 | my %validations = ( |
86 | max_buckets => { floor => 16, ceil => 256 }, |
87 | num_txns => { floor => 1, ceil => 255 }, |
88 | data_sector_size => { floor => 32, ceil => 256 }, |
89 | ); |
90 | |
91 | while ( my ($attr, $c) = each %validations ) { |
92 | if ( !defined $self->{$attr} |
93 | || !length $self->{$attr} |
94 | || $self->{$attr} =~ /\D/ |
95 | || $self->{$attr} < $c->{floor} |
96 | ) { |
97 | $self->{$attr} = '(undef)' if !defined $self->{$attr}; |
98 | warn "Floor of $attr is $c->{floor}. Setting it to $c->{floor} from '$self->{$attr}'\n"; |
99 | $self->{$attr} = $c->{floor}; |
100 | } |
101 | elsif ( $self->{$attr} > $c->{ceil} ) { |
102 | warn "Ceiling of $attr is $c->{ceil}. Setting it to $c->{ceil} from '$self->{$attr}'\n"; |
103 | $self->{$attr} = $c->{ceil}; |
104 | } |
e0098e7f |
105 | } |
106 | |
9a63e1f2 |
107 | if ( !$self->{digest} ) { |
108 | require Digest::MD5; |
109 | $self->{digest} = \&Digest::MD5::md5; |
110 | } |
111 | |
260a80b4 |
112 | return $self; |
113 | } |
114 | |
9a63e1f2 |
115 | ################################################################################ |
460b1067 |
116 | |
9a63e1f2 |
117 | sub read_value { |
c3aafc14 |
118 | my $self = shift; |
9a63e1f2 |
119 | my ($obj, $key) = @_; |
120 | |
121 | # This will be a Reference sector |
122 | my $sector = $self->_load_sector( $obj->_base_offset ) |
123 | or return; |
124 | |
125 | if ( $sector->staleness != $obj->_staleness ) { |
126 | return; |
127 | } |
128 | |
129 | my $key_md5 = $self->_apply_digest( $key ); |
130 | |
131 | my $value_sector = $sector->get_data_for({ |
132 | key_md5 => $key_md5, |
133 | allow_head => 1, |
134 | }); |
135 | |
136 | unless ( $value_sector ) { |
137 | $value_sector = DBM::Deep::Engine::Sector::Null->new({ |
138 | engine => $self, |
139 | data => undef, |
140 | }); |
141 | |
142 | $sector->write_data({ |
143 | key_md5 => $key_md5, |
144 | key => $key, |
145 | value => $value_sector, |
146 | }); |
147 | } |
148 | |
149 | return $value_sector->data; |
c3aafc14 |
150 | } |
151 | |
9a63e1f2 |
152 | sub get_classname { |
260a80b4 |
153 | my $self = shift; |
9a63e1f2 |
154 | my ($obj) = @_; |
260a80b4 |
155 | |
9a63e1f2 |
156 | # This will be a Reference sector |
157 | my $sector = $self->_load_sector( $obj->_base_offset ) |
158 | or DBM::Deep->_throw_error( "How did get_classname fail (no sector for '$obj')?!" ); |
612969fb |
159 | |
9a63e1f2 |
160 | if ( $sector->staleness != $obj->_staleness ) { |
161 | return; |
162 | } |
ea2f6d67 |
163 | |
9a63e1f2 |
164 | return $sector->get_classname; |
1bf65be7 |
165 | } |
166 | |
e137c258 |
167 | sub make_reference { |
168 | my $self = shift; |
169 | my ($obj, $old_key, $new_key) = @_; |
170 | |
171 | # This will be a Reference sector |
172 | my $sector = $self->_load_sector( $obj->_base_offset ) |
173 | or DBM::Deep->_throw_error( "How did get_classname fail (no sector for '$obj')?!" ); |
174 | |
175 | if ( $sector->staleness != $obj->_staleness ) { |
176 | return; |
177 | } |
178 | |
179 | my $old_md5 = $self->_apply_digest( $old_key ); |
180 | |
181 | my $value_sector = $sector->get_data_for({ |
182 | key_md5 => $old_md5, |
183 | allow_head => 1, |
184 | }); |
185 | |
186 | unless ( $value_sector ) { |
187 | $value_sector = DBM::Deep::Engine::Sector::Null->new({ |
188 | engine => $self, |
189 | data => undef, |
190 | }); |
191 | |
192 | $sector->write_data({ |
193 | key_md5 => $old_md5, |
194 | key => $old_key, |
195 | value => $value_sector, |
196 | }); |
197 | } |
198 | |
199 | if ( $value_sector->isa( 'DBM::Deep::Engine::Sector::Reference' ) ) { |
200 | $sector->write_data({ |
201 | key => $new_key, |
202 | key_md5 => $self->_apply_digest( $new_key ), |
203 | value => $value_sector, |
204 | }); |
205 | $value_sector->increment_refcount; |
206 | } |
207 | else { |
208 | $sector->write_data({ |
209 | key => $new_key, |
210 | key_md5 => $self->_apply_digest( $new_key ), |
211 | value => $value_sector->clone, |
212 | }); |
213 | } |
214 | } |
215 | |
9a63e1f2 |
216 | sub key_exists { |
0d0f3d5d |
217 | my $self = shift; |
9a63e1f2 |
218 | my ($obj, $key) = @_; |
0d0f3d5d |
219 | |
9a63e1f2 |
220 | # This will be a Reference sector |
221 | my $sector = $self->_load_sector( $obj->_base_offset ) |
222 | or return ''; |
0d0f3d5d |
223 | |
9a63e1f2 |
224 | if ( $sector->staleness != $obj->_staleness ) { |
225 | return ''; |
226 | } |
0d0f3d5d |
227 | |
9a63e1f2 |
228 | my $data = $sector->get_data_for({ |
229 | key_md5 => $self->_apply_digest( $key ), |
230 | allow_head => 1, |
231 | }); |
20b7f047 |
232 | |
9a63e1f2 |
233 | # exists() returns 1 or '' for true/false. |
234 | return $data ? 1 : ''; |
0d0f3d5d |
235 | } |
236 | |
9a63e1f2 |
237 | sub delete_key { |
e064ccd1 |
238 | my $self = shift; |
9a63e1f2 |
239 | my ($obj, $key) = @_; |
e064ccd1 |
240 | |
9a63e1f2 |
241 | my $sector = $self->_load_sector( $obj->_base_offset ) |
242 | or return; |
460b1067 |
243 | |
9a63e1f2 |
244 | if ( $sector->staleness != $obj->_staleness ) { |
245 | return; |
246 | } |
247 | |
248 | return $sector->delete_key({ |
249 | key_md5 => $self->_apply_digest( $key ), |
250 | allow_head => 0, |
251 | }); |
252 | } |
253 | |
254 | sub write_value { |
255 | my $self = shift; |
256 | my ($obj, $key, $value) = @_; |
257 | |
258 | my $r = Scalar::Util::reftype( $value ) || ''; |
259 | { |
260 | last if $r eq ''; |
261 | last if $r eq 'HASH'; |
262 | last if $r eq 'ARRAY'; |
e064ccd1 |
263 | |
9a63e1f2 |
264 | DBM::Deep->_throw_error( |
265 | "Storage of references of type '$r' is not supported." |
266 | ); |
460b1067 |
267 | } |
260a80b4 |
268 | |
e137c258 |
269 | # This will be a Reference sector |
270 | my $sector = $self->_load_sector( $obj->_base_offset ) |
271 | or DBM::Deep->_throw_error( "Cannot write to a deleted spot in DBM::Deep." ); |
272 | |
273 | if ( $sector->staleness != $obj->_staleness ) { |
274 | DBM::Deep->_throw_error( "Cannot write to a deleted spot in DBM::Deep.n" ); |
275 | } |
276 | |
9a63e1f2 |
277 | my ($class, $type); |
278 | if ( !defined $value ) { |
279 | $class = 'DBM::Deep::Engine::Sector::Null'; |
280 | } |
281 | elsif ( $r eq 'ARRAY' || $r eq 'HASH' ) { |
e137c258 |
282 | my $is_dbm_deep = eval { local $SIG{'__DIE__'}; $value->isa( 'DBM::Deep' ); }; |
283 | if ( $is_dbm_deep ) { |
284 | if ( $value->_engine->storage == $self->storage ) { |
285 | my $value_sector = $self->_load_sector( $value->_base_offset ); |
286 | $sector->write_data({ |
287 | key => $key, |
288 | key_md5 => $self->_apply_digest( $key ), |
289 | value => $value_sector, |
290 | }); |
291 | $value_sector->increment_refcount; |
292 | return 1; |
293 | } |
294 | |
295 | DBM::Deep->_throw_error( "Cannot store values across DBM::Deep files. Please use export() instead." ); |
296 | } |
9a63e1f2 |
297 | if ( $r eq 'ARRAY' && tied(@$value) ) { |
298 | DBM::Deep->_throw_error( "Cannot store something that is tied." ); |
299 | } |
300 | if ( $r eq 'HASH' && tied(%$value) ) { |
301 | DBM::Deep->_throw_error( "Cannot store something that is tied." ); |
302 | } |
303 | $class = 'DBM::Deep::Engine::Sector::Reference'; |
304 | $type = substr( $r, 0, 1 ); |
305 | } |
306 | else { |
e137c258 |
307 | if ( tied($value) ) { |
308 | DBM::Deep->_throw_error( "Cannot store something that is tied." ); |
309 | } |
9a63e1f2 |
310 | $class = 'DBM::Deep::Engine::Sector::Scalar'; |
460b1067 |
311 | } |
9b2370e0 |
312 | |
9a63e1f2 |
313 | # Create this after loading the reference sector in case something bad happens. |
314 | # This way, we won't allocate value sector(s) needlessly. |
315 | my $value_sector = $class->new({ |
316 | engine => $self, |
317 | data => $value, |
318 | type => $type, |
319 | }); |
320 | |
321 | $sector->write_data({ |
322 | key => $key, |
323 | key_md5 => $self->_apply_digest( $key ), |
324 | value => $value_sector, |
325 | }); |
326 | |
327 | # This code is to make sure we write all the values in the $value to the disk |
328 | # and to make sure all changes to $value after the assignment are reflected |
329 | # on disk. This may be counter-intuitive at first, but it is correct dwimmery. |
330 | # NOTE - simply tying $value won't perform a STORE on each value. Hence, the |
331 | # copy to a temp value. |
332 | if ( $r eq 'ARRAY' ) { |
333 | my @temp = @$value; |
334 | tie @$value, 'DBM::Deep', { |
335 | base_offset => $value_sector->offset, |
336 | staleness => $value_sector->staleness, |
337 | storage => $self->storage, |
338 | engine => $self, |
339 | }; |
340 | @$value = @temp; |
341 | bless $value, 'DBM::Deep::Array' unless Scalar::Util::blessed( $value ); |
e064ccd1 |
342 | } |
9a63e1f2 |
343 | elsif ( $r eq 'HASH' ) { |
344 | my %temp = %$value; |
345 | tie %$value, 'DBM::Deep', { |
346 | base_offset => $value_sector->offset, |
347 | staleness => $value_sector->staleness, |
348 | storage => $self->storage, |
349 | engine => $self, |
350 | }; |
e064ccd1 |
351 | |
9a63e1f2 |
352 | %$value = %temp; |
353 | bless $value, 'DBM::Deep::Hash' unless Scalar::Util::blessed( $value ); |
354 | } |
460b1067 |
355 | |
9a63e1f2 |
356 | return 1; |
e064ccd1 |
357 | } |
358 | |
9a63e1f2 |
359 | # XXX Add staleness here |
360 | sub get_next_key { |
460b1067 |
361 | my $self = shift; |
9a63e1f2 |
362 | my ($obj, $prev_key) = @_; |
70b55428 |
363 | |
9a63e1f2 |
364 | # XXX Need to add logic about resetting the iterator if any key in the reference has changed |
365 | unless ( $prev_key ) { |
366 | $obj->{iterator} = DBM::Deep::Iterator->new({ |
367 | base_offset => $obj->_base_offset, |
368 | engine => $self, |
369 | }); |
370 | } |
118ba343 |
371 | |
9a63e1f2 |
372 | return $obj->{iterator}->get_next_key( $obj ); |
373 | } |
118ba343 |
374 | |
9a63e1f2 |
375 | ################################################################################ |
260a80b4 |
376 | |
9a63e1f2 |
377 | sub setup_fh { |
378 | my $self = shift; |
379 | my ($obj) = @_; |
359a01ac |
380 | |
9a63e1f2 |
381 | # We're opening the file. |
382 | unless ( $obj->_base_offset ) { |
383 | my $bytes_read = $self->_read_file_header; |
118ba343 |
384 | |
9a63e1f2 |
385 | # Creating a new file |
386 | unless ( $bytes_read ) { |
387 | $self->_write_file_header; |
118ba343 |
388 | |
9a63e1f2 |
389 | # 1) Create Array/Hash entry |
390 | my $initial_reference = DBM::Deep::Engine::Sector::Reference->new({ |
391 | engine => $self, |
392 | type => $obj->_type, |
393 | }); |
394 | $obj->{base_offset} = $initial_reference->offset; |
395 | $obj->{staleness} = $initial_reference->staleness; |
118ba343 |
396 | |
9a63e1f2 |
397 | $self->storage->flush; |
118ba343 |
398 | } |
9a63e1f2 |
399 | # Reading from an existing file |
118ba343 |
400 | else { |
401 | $obj->{base_offset} = $bytes_read; |
9a63e1f2 |
402 | my $initial_reference = DBM::Deep::Engine::Sector::Reference->new({ |
403 | engine => $self, |
404 | offset => $obj->_base_offset, |
405 | }); |
406 | unless ( $initial_reference ) { |
407 | DBM::Deep->_throw_error("Corrupted file, no master index record"); |
359a01ac |
408 | } |
118ba343 |
409 | |
9a63e1f2 |
410 | unless ($obj->_type eq $initial_reference->type) { |
411 | DBM::Deep->_throw_error("File type mismatch"); |
118ba343 |
412 | } |
9a63e1f2 |
413 | |
414 | $obj->{staleness} = $initial_reference->staleness; |
118ba343 |
415 | } |
118ba343 |
416 | } |
9a63e1f2 |
417 | |
418 | return 1; |
419 | } |
420 | |
421 | sub begin_work { |
422 | my $self = shift; |
423 | my ($obj) = @_; |
424 | |
425 | if ( $self->trans_id ) { |
426 | DBM::Deep->_throw_error( "Cannot begin_work within an active transaction" ); |
427 | } |
428 | |
429 | my @slots = $self->read_txn_slots; |
f72b2dfb |
430 | my $found; |
431 | for my $i ( 0 .. $#slots ) { |
9a63e1f2 |
432 | next if $slots[$i]; |
f72b2dfb |
433 | |
9a63e1f2 |
434 | $slots[$i] = 1; |
f72b2dfb |
435 | $self->set_trans_id( $i + 1 ); |
436 | $found = 1; |
9a63e1f2 |
437 | last; |
438 | } |
f72b2dfb |
439 | unless ( $found ) { |
440 | DBM::Deep->_throw_error( "Cannot allocate transaction ID" ); |
441 | } |
9a63e1f2 |
442 | $self->write_txn_slots( @slots ); |
443 | |
444 | if ( !$self->trans_id ) { |
445 | DBM::Deep->_throw_error( "Cannot begin_work - no available transactions" ); |
446 | } |
447 | |
448 | return; |
449 | } |
450 | |
451 | sub rollback { |
452 | my $self = shift; |
453 | my ($obj) = @_; |
454 | |
455 | if ( !$self->trans_id ) { |
456 | DBM::Deep->_throw_error( "Cannot rollback without an active transaction" ); |
457 | } |
458 | |
459 | # Each entry is the file location for a bucket that has a modification for |
460 | # this transaction. The entries need to be expunged. |
461 | foreach my $entry (@{ $self->get_entries } ) { |
462 | # Remove the entry here |
463 | my $read_loc = $entry |
464 | + $self->hash_size |
465 | + $self->byte_size |
f72b2dfb |
466 | + $self->byte_size |
467 | + ($self->trans_id - 1) * ( $self->byte_size + $STALE_SIZE ); |
9a63e1f2 |
468 | |
469 | my $data_loc = $self->storage->read_at( $read_loc, $self->byte_size ); |
470 | $data_loc = unpack( $StP{$self->byte_size}, $data_loc ); |
471 | $self->storage->print_at( $read_loc, pack( $StP{$self->byte_size}, 0 ) ); |
472 | |
473 | if ( $data_loc > 1 ) { |
474 | $self->_load_sector( $data_loc )->free; |
475 | } |
260a80b4 |
476 | } |
e06824f8 |
477 | |
9a63e1f2 |
478 | $self->clear_entries; |
70b55428 |
479 | |
9a63e1f2 |
480 | my @slots = $self->read_txn_slots; |
f72b2dfb |
481 | $slots[$self->trans_id-1] = 0; |
9a63e1f2 |
482 | $self->write_txn_slots( @slots ); |
483 | $self->inc_txn_staleness_counter( $self->trans_id ); |
484 | $self->set_trans_id( 0 ); |
6fde4ed2 |
485 | |
70b55428 |
486 | return 1; |
487 | } |
488 | |
9a63e1f2 |
489 | sub commit { |
16d1ad9b |
490 | my $self = shift; |
9a63e1f2 |
491 | my ($obj) = @_; |
492 | |
493 | if ( !$self->trans_id ) { |
494 | DBM::Deep->_throw_error( "Cannot commit without an active transaction" ); |
495 | } |
496 | |
497 | foreach my $entry (@{ $self->get_entries } ) { |
498 | # Overwrite the entry in head with the entry in trans_id |
499 | my $base = $entry |
500 | + $self->hash_size |
501 | + $self->byte_size; |
502 | |
503 | my $head_loc = $self->storage->read_at( $base, $self->byte_size ); |
504 | $head_loc = unpack( $StP{$self->byte_size}, $head_loc ); |
f72b2dfb |
505 | |
506 | my $spot = $base + $self->byte_size + ($self->trans_id - 1) * ( $self->byte_size + $STALE_SIZE ); |
9a63e1f2 |
507 | my $trans_loc = $self->storage->read_at( |
f72b2dfb |
508 | $spot, $self->byte_size, |
9a63e1f2 |
509 | ); |
510 | |
511 | $self->storage->print_at( $base, $trans_loc ); |
512 | $self->storage->print_at( |
f72b2dfb |
513 | $spot, |
514 | pack( $StP{$self->byte_size} . ' ' . $StP{$STALE_SIZE}, (0) x 2 ), |
9a63e1f2 |
515 | ); |
516 | |
517 | if ( $head_loc > 1 ) { |
518 | $self->_load_sector( $head_loc )->free; |
519 | } |
520 | } |
521 | |
522 | $self->clear_entries; |
523 | |
524 | my @slots = $self->read_txn_slots; |
f72b2dfb |
525 | $slots[$self->trans_id-1] = 0; |
9a63e1f2 |
526 | $self->write_txn_slots( @slots ); |
527 | $self->inc_txn_staleness_counter( $self->trans_id ); |
528 | $self->set_trans_id( 0 ); |
529 | |
530 | return 1; |
16d1ad9b |
531 | } |
532 | |
9a63e1f2 |
533 | sub read_txn_slots { |
d4b1166e |
534 | my $self = shift; |
f72b2dfb |
535 | my $bl = $self->txn_bitfield_len; |
536 | my $num_bits = $bl * 8; |
537 | return split '', unpack( 'b'.$num_bits, |
9a63e1f2 |
538 | $self->storage->read_at( |
f72b2dfb |
539 | $self->trans_loc, $bl, |
9a63e1f2 |
540 | ) |
541 | ); |
542 | } |
20f7b20c |
543 | |
9a63e1f2 |
544 | sub write_txn_slots { |
545 | my $self = shift; |
f72b2dfb |
546 | my $num_bits = $self->txn_bitfield_len * 8; |
9a63e1f2 |
547 | $self->storage->print_at( $self->trans_loc, |
f72b2dfb |
548 | pack( 'b'.$num_bits, join('', @_) ), |
7dcefff3 |
549 | ); |
9a63e1f2 |
550 | } |
551 | |
552 | sub get_running_txn_ids { |
553 | my $self = shift; |
554 | my @transactions = $self->read_txn_slots; |
f72b2dfb |
555 | my @trans_ids = map { $_+1} grep { $transactions[$_] } 0 .. $#transactions; |
9a63e1f2 |
556 | } |
557 | |
558 | sub get_txn_staleness_counter { |
559 | my $self = shift; |
560 | my ($trans_id) = @_; |
20f7b20c |
561 | |
9a63e1f2 |
562 | # Hardcode staleness of 0 for the HEAD |
563 | return 0 unless $trans_id; |
f37c15ab |
564 | |
f72b2dfb |
565 | return unpack( $StP{$STALE_SIZE}, |
9a63e1f2 |
566 | $self->storage->read_at( |
90fb1a24 |
567 | $self->trans_loc + $self->txn_bitfield_len + $STALE_SIZE * ($trans_id - 1), |
568 | $STALE_SIZE, |
9a63e1f2 |
569 | ) |
570 | ); |
d4b1166e |
571 | } |
572 | |
9a63e1f2 |
573 | sub inc_txn_staleness_counter { |
d4b1166e |
574 | my $self = shift; |
9a63e1f2 |
575 | my ($trans_id) = @_; |
20f7b20c |
576 | |
9a63e1f2 |
577 | # Hardcode staleness of 0 for the HEAD |
a24e7f93 |
578 | return 0 unless $trans_id; |
20f7b20c |
579 | |
9a63e1f2 |
580 | $self->storage->print_at( |
90fb1a24 |
581 | $self->trans_loc + $self->txn_bitfield_len + $STALE_SIZE * ($trans_id - 1), |
f72b2dfb |
582 | pack( $StP{$STALE_SIZE}, $self->get_txn_staleness_counter( $trans_id ) + 1 ), |
86867f3a |
583 | ); |
9a63e1f2 |
584 | } |
20f7b20c |
585 | |
9a63e1f2 |
586 | sub get_entries { |
587 | my $self = shift; |
588 | return [ keys %{ $self->{entries}{$self->trans_id} ||= {} } ]; |
d4b1166e |
589 | } |
590 | |
9a63e1f2 |
591 | sub add_entry { |
ea2f6d67 |
592 | my $self = shift; |
9a63e1f2 |
593 | my ($trans_id, $loc) = @_; |
ea2f6d67 |
594 | |
9a63e1f2 |
595 | $self->{entries}{$trans_id} ||= {}; |
596 | $self->{entries}{$trans_id}{$loc} = undef; |
597 | } |
ea2f6d67 |
598 | |
9a63e1f2 |
599 | # If the buckets are being relocated because of a reindexing, the entries |
600 | # mechanism needs to be made aware of it. |
601 | sub reindex_entry { |
602 | my $self = shift; |
603 | my ($old_loc, $new_loc) = @_; |
604 | |
605 | TRANS: |
606 | while ( my ($trans_id, $locs) = each %{ $self->{entries} } ) { |
607 | foreach my $orig_loc ( keys %{ $locs } ) { |
608 | if ( $orig_loc == $old_loc ) { |
609 | delete $locs->{orig_loc}; |
610 | $locs->{$new_loc} = undef; |
611 | next TRANS; |
612 | } |
613 | } |
ea2f6d67 |
614 | } |
ea2f6d67 |
615 | } |
616 | |
9a63e1f2 |
617 | sub clear_entries { |
d4b1166e |
618 | my $self = shift; |
9a63e1f2 |
619 | delete $self->{entries}{$self->trans_id}; |
620 | } |
eea0d863 |
621 | |
9a63e1f2 |
622 | ################################################################################ |
eea0d863 |
623 | |
9a63e1f2 |
624 | { |
625 | my $header_fixed = length( SIG_FILE ) + 1 + 4 + 4; |
da1f1300 |
626 | my $this_file_version = 3; |
9a63e1f2 |
627 | |
628 | sub _write_file_header { |
629 | my $self = shift; |
630 | |
f72b2dfb |
631 | my $nt = $self->num_txns; |
632 | my $bl = $self->txn_bitfield_len; |
633 | |
634 | my $header_var = 1 + 1 + 1 + 1 + $bl + $STALE_SIZE * ($nt - 1) + 3 * $self->byte_size; |
9a63e1f2 |
635 | |
636 | my $loc = $self->storage->request_space( $header_fixed + $header_var ); |
637 | |
638 | $self->storage->print_at( $loc, |
639 | SIG_FILE, |
640 | SIG_HEADER, |
f72b2dfb |
641 | pack('N', $this_file_version), # At this point, we're at 9 bytes |
642 | pack('N', $header_var), # header size |
9a63e1f2 |
643 | # --- Above is $header_fixed. Below is $header_var |
644 | pack('C', $self->byte_size), |
f72b2dfb |
645 | |
646 | # These shenanigans are to allow a 256 within a C |
647 | pack('C', $self->max_buckets - 1), |
648 | pack('C', $self->data_sector_size - 1), |
649 | |
650 | pack('C', $nt), |
651 | pack('C' . $bl, 0 ), # Transaction activeness bitfield |
652 | pack($StP{$STALE_SIZE}.($nt-1), 0 x ($nt-1) ), # Transaction staleness counters |
9a63e1f2 |
653 | pack($StP{$self->byte_size}, 0), # Start of free chain (blist size) |
654 | pack($StP{$self->byte_size}, 0), # Start of free chain (data size) |
655 | pack($StP{$self->byte_size}, 0), # Start of free chain (index size) |
eea0d863 |
656 | ); |
eea0d863 |
657 | |
f72b2dfb |
658 | #XXX Set these less fragilely |
659 | $self->set_trans_loc( $header_fixed + 4 ); |
660 | $self->set_chains_loc( $header_fixed + 4 + $bl + $STALE_SIZE * ($nt-1) ); |
20f7b20c |
661 | |
9a63e1f2 |
662 | return; |
c9b6d0d8 |
663 | } |
75be6413 |
664 | |
9a63e1f2 |
665 | sub _read_file_header { |
666 | my $self = shift; |
ea2f6d67 |
667 | |
9a63e1f2 |
668 | my $buffer = $self->storage->read_at( 0, $header_fixed ); |
669 | return unless length($buffer); |
019404df |
670 | |
f72b2dfb |
671 | my ($file_signature, $sig_header, $file_version, $size) = unpack( |
9a63e1f2 |
672 | 'A4 A N N', $buffer |
673 | ); |
ea2f6d67 |
674 | |
9a63e1f2 |
675 | unless ( $file_signature eq SIG_FILE ) { |
676 | $self->storage->close; |
677 | DBM::Deep->_throw_error( "Signature not found -- file is not a Deep DB" ); |
678 | } |
ea2f6d67 |
679 | |
9a63e1f2 |
680 | unless ( $sig_header eq SIG_HEADER ) { |
681 | $self->storage->close; |
f72b2dfb |
682 | DBM::Deep->_throw_error( "Pre-1.00 file version found" ); |
683 | } |
684 | |
685 | unless ( $file_version == $this_file_version ) { |
686 | $self->storage->close; |
687 | DBM::Deep->_throw_error( |
688 | "Wrong file version found - " . $file_version . |
689 | " - expected " . $this_file_version |
690 | ); |
75be6413 |
691 | } |
504185fb |
692 | |
9a63e1f2 |
693 | my $buffer2 = $self->storage->read_at( undef, $size ); |
f72b2dfb |
694 | my @values = unpack( 'C C C C', $buffer2 ); |
386bab6c |
695 | |
f72b2dfb |
696 | if ( @values != 4 || grep { !defined } @values ) { |
9a63e1f2 |
697 | $self->storage->close; |
698 | DBM::Deep->_throw_error("Corrupted file - bad header"); |
ea2f6d67 |
699 | } |
700 | |
9a63e1f2 |
701 | #XXX Add warnings if values weren't set right |
f72b2dfb |
702 | @{$self}{qw(byte_size max_buckets data_sector_size num_txns)} = @values; |
ea2f6d67 |
703 | |
f72b2dfb |
704 | # These shenangians are to allow a 256 within a C |
705 | $self->{max_buckets} += 1; |
706 | $self->{data_sector_size} += 1; |
707 | |
708 | my $bl = $self->txn_bitfield_len; |
709 | |
710 | my $header_var = scalar(@values) + $bl + $STALE_SIZE * ($self->num_txns - 1) + 3 * $self->byte_size; |
9a63e1f2 |
711 | unless ( $size == $header_var ) { |
712 | $self->storage->close; |
713 | DBM::Deep->_throw_error( "Unexpected size found ($size <-> $header_var)." ); |
c9b6d0d8 |
714 | } |
20f7b20c |
715 | |
f72b2dfb |
716 | $self->set_trans_loc( $header_fixed + scalar(@values) ); |
717 | $self->set_chains_loc( $header_fixed + scalar(@values) + $bl + $STALE_SIZE * ($self->num_txns - 1) ); |
718 | |
9a63e1f2 |
719 | return length($buffer) + length($buffer2); |
720 | } |
d5d7c51d |
721 | } |
722 | |
9a63e1f2 |
723 | sub _load_sector { |
d5d7c51d |
724 | my $self = shift; |
9a63e1f2 |
725 | my ($offset) = @_; |
d5d7c51d |
726 | |
9a63e1f2 |
727 | # Add a catch for offset of 0 or 1 |
728 | return if $offset <= 1; |
d5d7c51d |
729 | |
9a63e1f2 |
730 | my $type = $self->storage->read_at( $offset, 1 ); |
731 | return if $type eq chr(0); |
d5d7c51d |
732 | |
9a63e1f2 |
733 | if ( $type eq $self->SIG_ARRAY || $type eq $self->SIG_HASH ) { |
734 | return DBM::Deep::Engine::Sector::Reference->new({ |
735 | engine => $self, |
736 | type => $type, |
737 | offset => $offset, |
738 | }); |
f37c15ab |
739 | } |
9a63e1f2 |
740 | # XXX Don't we need key_md5 here? |
741 | elsif ( $type eq $self->SIG_BLIST ) { |
742 | return DBM::Deep::Engine::Sector::BucketList->new({ |
743 | engine => $self, |
744 | type => $type, |
745 | offset => $offset, |
746 | }); |
d5d7c51d |
747 | } |
9a63e1f2 |
748 | elsif ( $type eq $self->SIG_INDEX ) { |
749 | return DBM::Deep::Engine::Sector::Index->new({ |
750 | engine => $self, |
751 | type => $type, |
752 | offset => $offset, |
753 | }); |
d5d7c51d |
754 | } |
9a63e1f2 |
755 | elsif ( $type eq $self->SIG_NULL ) { |
756 | return DBM::Deep::Engine::Sector::Null->new({ |
757 | engine => $self, |
758 | type => $type, |
759 | offset => $offset, |
760 | }); |
d5d7c51d |
761 | } |
9a63e1f2 |
762 | elsif ( $type eq $self->SIG_DATA ) { |
763 | return DBM::Deep::Engine::Sector::Scalar->new({ |
764 | engine => $self, |
765 | type => $type, |
766 | offset => $offset, |
767 | }); |
9d4fa373 |
768 | } |
9a63e1f2 |
769 | # This was deleted from under us, so just return and let the caller figure it out. |
770 | elsif ( $type eq $self->SIG_FREE ) { |
771 | return; |
20f7b20c |
772 | } |
d4b1166e |
773 | |
9a63e1f2 |
774 | DBM::Deep->_throw_error( "'$offset': Don't know what to do with type '$type'" ); |
d4b1166e |
775 | } |
776 | |
9a63e1f2 |
777 | sub _apply_digest { |
75be6413 |
778 | my $self = shift; |
9a63e1f2 |
779 | return $self->{digest}->(@_); |
780 | } |
16d1ad9b |
781 | |
9a63e1f2 |
782 | sub _add_free_blist_sector { shift->_add_free_sector( 0, @_ ) } |
783 | sub _add_free_data_sector { shift->_add_free_sector( 1, @_ ) } |
784 | sub _add_free_index_sector { shift->_add_free_sector( 2, @_ ) } |
75be6413 |
785 | |
9a63e1f2 |
786 | sub _add_free_sector { |
787 | my $self = shift; |
788 | my ($multiple, $offset, $size) = @_; |
75be6413 |
789 | |
9a63e1f2 |
790 | my $chains_offset = $multiple * $self->byte_size; |
75be6413 |
791 | |
9a63e1f2 |
792 | my $storage = $self->storage; |
75be6413 |
793 | |
9a63e1f2 |
794 | # Increment staleness. |
795 | # XXX Can this increment+modulo be done by "&= 0x1" ? |
f72b2dfb |
796 | my $staleness = unpack( $StP{$STALE_SIZE}, $storage->read_at( $offset + SIG_SIZE, $STALE_SIZE ) ); |
797 | $staleness = ($staleness + 1 ) % ( 2 ** ( 8 * $STALE_SIZE ) ); |
798 | $storage->print_at( $offset + SIG_SIZE, pack( $StP{$STALE_SIZE}, $staleness ) ); |
75be6413 |
799 | |
9a63e1f2 |
800 | my $old_head = $storage->read_at( $self->chains_loc + $chains_offset, $self->byte_size ); |
75be6413 |
801 | |
9a63e1f2 |
802 | $storage->print_at( $self->chains_loc + $chains_offset, |
803 | pack( $StP{$self->byte_size}, $offset ), |
804 | ); |
75be6413 |
805 | |
9a63e1f2 |
806 | # Record the old head in the new sector after the signature and staleness counter |
f72b2dfb |
807 | $storage->print_at( $offset + SIG_SIZE + $STALE_SIZE, $old_head ); |
9a63e1f2 |
808 | } |
75be6413 |
809 | |
9a63e1f2 |
810 | sub _request_blist_sector { shift->_request_sector( 0, @_ ) } |
811 | sub _request_data_sector { shift->_request_sector( 1, @_ ) } |
812 | sub _request_index_sector { shift->_request_sector( 2, @_ ) } |
7b1e1aa1 |
813 | |
9a63e1f2 |
814 | sub _request_sector { |
815 | my $self = shift; |
816 | my ($multiple, $size) = @_; |
75be6413 |
817 | |
9a63e1f2 |
818 | my $chains_offset = $multiple * $self->byte_size; |
2603d86e |
819 | |
9a63e1f2 |
820 | my $old_head = $self->storage->read_at( $self->chains_loc + $chains_offset, $self->byte_size ); |
821 | my $loc = unpack( $StP{$self->byte_size}, $old_head ); |
75be6413 |
822 | |
9a63e1f2 |
823 | # We don't have any free sectors of the right size, so allocate a new one. |
824 | unless ( $loc ) { |
825 | my $offset = $self->storage->request_space( $size ); |
7b1e1aa1 |
826 | |
9a63e1f2 |
827 | # Zero out the new sector. This also guarantees correct increases |
828 | # in the filesize. |
829 | $self->storage->print_at( $offset, chr(0) x $size ); |
7b1e1aa1 |
830 | |
9a63e1f2 |
831 | return $offset; |
7b1e1aa1 |
832 | } |
833 | |
9a63e1f2 |
834 | # Read the new head after the signature and the staleness counter |
f72b2dfb |
835 | my $new_head = $self->storage->read_at( $loc + SIG_SIZE + $STALE_SIZE, $self->byte_size ); |
9a63e1f2 |
836 | $self->storage->print_at( $self->chains_loc + $chains_offset, $new_head ); |
837 | $self->storage->print_at( |
f72b2dfb |
838 | $loc + SIG_SIZE + $STALE_SIZE, |
9a63e1f2 |
839 | pack( $StP{$self->byte_size}, 0 ), |
7b1e1aa1 |
840 | ); |
75be6413 |
841 | |
9a63e1f2 |
842 | return $loc; |
75be6413 |
843 | } |
844 | |
9a63e1f2 |
845 | ################################################################################ |
8db25060 |
846 | |
9a63e1f2 |
847 | sub storage { $_[0]{storage} } |
848 | sub byte_size { $_[0]{byte_size} } |
849 | sub hash_size { $_[0]{hash_size} } |
850 | sub hash_chars { $_[0]{hash_chars} } |
851 | sub num_txns { $_[0]{num_txns} } |
852 | sub max_buckets { $_[0]{max_buckets} } |
853 | sub blank_md5 { chr(0) x $_[0]->hash_size } |
f72b2dfb |
854 | sub data_sector_size { $_[0]{data_sector_size} } |
855 | |
856 | # This is a calculated value |
857 | sub txn_bitfield_len { |
858 | my $self = shift; |
859 | unless ( exists $self->{txn_bitfield_len} ) { |
860 | my $temp = ($self->num_txns) / 8; |
861 | if ( $temp > int( $temp ) ) { |
862 | $temp = int( $temp ) + 1; |
863 | } |
864 | $self->{txn_bitfield_len} = $temp; |
865 | } |
866 | return $self->{txn_bitfield_len}; |
867 | } |
8db25060 |
868 | |
9a63e1f2 |
869 | sub trans_id { $_[0]{trans_id} } |
870 | sub set_trans_id { $_[0]{trans_id} = $_[1] } |
8db25060 |
871 | |
9a63e1f2 |
872 | sub trans_loc { $_[0]{trans_loc} } |
873 | sub set_trans_loc { $_[0]{trans_loc} = $_[1] } |
874 | |
875 | sub chains_loc { $_[0]{chains_loc} } |
876 | sub set_chains_loc { $_[0]{chains_loc} = $_[1] } |
877 | |
9f83dc31 |
878 | sub _dump_file { |
879 | my $self = shift; |
880 | |
881 | # Read the header |
882 | my $spot = $self->_read_file_header(); |
883 | |
884 | my %types = ( |
885 | 0 => 'B', |
886 | 1 => 'D', |
887 | 2 => 'I', |
888 | ); |
889 | |
890 | my %sizes = ( |
891 | 'D' => $self->data_sector_size, |
990141bb |
892 | 'B' => DBM::Deep::Engine::Sector::BucketList->new({engine=>$self,offset=>1})->size, |
893 | 'I' => DBM::Deep::Engine::Sector::Index->new({engine=>$self,offset=>1})->size, |
9f83dc31 |
894 | ); |
895 | |
90fb1a24 |
896 | my $return = ""; |
9f83dc31 |
897 | # Read the free sector chains |
898 | my %sectors; |
899 | foreach my $multiple ( 0 .. 2 ) { |
90fb1a24 |
900 | $return .= "Chains($types{$multiple}):"; |
901 | my $old_loc = $self->chains_loc + $multiple * $self->byte_size; |
9f83dc31 |
902 | while ( 1 ) { |
903 | my $loc = unpack( |
904 | $StP{$self->byte_size}, |
905 | $self->storage->read_at( $old_loc, $self->byte_size ), |
906 | ); |
907 | |
908 | # We're now out of free sectors of this kind. |
909 | unless ( $loc ) { |
910 | last; |
911 | } |
912 | |
913 | $sectors{ $types{$multiple} }{ $loc } = undef; |
914 | $old_loc = $loc + SIG_SIZE + $STALE_SIZE; |
90fb1a24 |
915 | $return .= " $loc"; |
9f83dc31 |
916 | } |
90fb1a24 |
917 | $return .= $/; |
9f83dc31 |
918 | } |
919 | |
9f83dc31 |
920 | SECTOR: |
921 | while ( $spot < $self->storage->{end} ) { |
922 | # Read each sector in order. |
923 | my $sector = $self->_load_sector( $spot ); |
924 | if ( !$sector ) { |
925 | # Find it in the free-sectors that were found already |
926 | foreach my $type ( keys %sectors ) { |
927 | if ( exists $sectors{$type}{$spot} ) { |
928 | my $size = $sizes{$type}; |
929 | $return .= sprintf "%08d: %s %04d\n", $spot, 'F' . $type, $size; |
930 | $spot += $size; |
931 | next SECTOR; |
932 | } |
933 | } |
934 | |
90fb1a24 |
935 | die "********\n$return\nDidn't find free sector for $spot in chains\n********\n"; |
9f83dc31 |
936 | } |
937 | else { |
938 | $return .= sprintf "%08d: %s %04d", $spot, $sector->type, $sector->size; |
939 | if ( $sector->type eq 'D' ) { |
940 | $return .= ' ' . $sector->data; |
941 | } |
942 | elsif ( $sector->type eq 'A' || $sector->type eq 'H' ) { |
943 | $return .= ' REF: ' . $sector->get_refcount; |
944 | } |
945 | elsif ( $sector->type eq 'B' ) { |
946 | foreach my $bucket ( $sector->chopped_up ) { |
947 | $return .= "\n "; |
a24e7f93 |
948 | $return .= sprintf "%08d", unpack($StP{$self->byte_size}, |
9f83dc31 |
949 | substr( $bucket->[-1], $self->hash_size, $self->byte_size), |
950 | ); |
951 | foreach my $txn ( 0 .. $self->num_txns - 1 ) { |
952 | my $l = unpack( $StP{$self->byte_size}, |
953 | substr( $bucket->[-1], |
954 | $self->hash_size + $self->byte_size + $txn * ($self->byte_size + $STALE_SIZE), |
955 | $self->byte_size, |
956 | ), |
957 | ); |
a24e7f93 |
958 | $return .= sprintf " %08d", $l; |
9f83dc31 |
959 | } |
960 | } |
961 | } |
962 | $return .= $/; |
963 | |
964 | $spot += $sector->size; |
965 | } |
966 | } |
967 | |
968 | return $return; |
969 | } |
970 | |
9a63e1f2 |
971 | ################################################################################ |
972 | |
973 | package DBM::Deep::Iterator; |
974 | |
975 | sub new { |
976 | my $class = shift; |
977 | my ($args) = @_; |
978 | |
979 | my $self = bless { |
980 | breadcrumbs => [], |
981 | engine => $args->{engine}, |
982 | base_offset => $args->{base_offset}, |
983 | }, $class; |
984 | |
985 | Scalar::Util::weaken( $self->{engine} ); |
986 | |
987 | return $self; |
988 | } |
989 | |
990 | sub reset { $_[0]{breadcrumbs} = [] } |
991 | |
992 | sub get_sector_iterator { |
993 | my $self = shift; |
994 | my ($loc) = @_; |
995 | |
996 | my $sector = $self->{engine}->_load_sector( $loc ) |
997 | or return; |
998 | |
999 | if ( $sector->isa( 'DBM::Deep::Engine::Sector::Index' ) ) { |
1000 | return DBM::Deep::Iterator::Index->new({ |
1001 | iterator => $self, |
1002 | sector => $sector, |
685e40f1 |
1003 | }); |
9a63e1f2 |
1004 | } |
1005 | elsif ( $sector->isa( 'DBM::Deep::Engine::Sector::BucketList' ) ) { |
1006 | return DBM::Deep::Iterator::BucketList->new({ |
1007 | iterator => $self, |
1008 | sector => $sector, |
1009 | }); |
1010 | } |
8db25060 |
1011 | |
9a63e1f2 |
1012 | DBM::Deep->_throw_error( "get_sector_iterator(): Why did $loc make a $sector?" ); |
1013 | } |
1014 | |
1015 | sub get_next_key { |
1016 | my $self = shift; |
1017 | my ($obj) = @_; |
1018 | |
1019 | my $crumbs = $self->{breadcrumbs}; |
1020 | my $e = $self->{engine}; |
1021 | |
1022 | unless ( @$crumbs ) { |
1023 | # This will be a Reference sector |
1024 | my $sector = $e->_load_sector( $self->{base_offset} ) |
1025 | # If no sector is found, thist must have been deleted from under us. |
1026 | or return; |
1027 | |
1028 | if ( $sector->staleness != $obj->_staleness ) { |
1029 | return; |
8db25060 |
1030 | } |
1031 | |
9a63e1f2 |
1032 | my $loc = $sector->get_blist_loc |
1033 | or return; |
1034 | |
1035 | push @$crumbs, $self->get_sector_iterator( $loc ); |
8db25060 |
1036 | } |
8db25060 |
1037 | |
9a63e1f2 |
1038 | FIND_NEXT_KEY: { |
1039 | # We're at the end. |
1040 | unless ( @$crumbs ) { |
1041 | $self->reset; |
8db25060 |
1042 | return; |
1043 | } |
8db25060 |
1044 | |
9a63e1f2 |
1045 | my $iterator = $crumbs->[-1]; |
1046 | |
1047 | # This level is done. |
1048 | if ( $iterator->at_end ) { |
1049 | pop @$crumbs; |
1050 | redo FIND_NEXT_KEY; |
1051 | } |
1052 | |
1053 | if ( $iterator->isa( 'DBM::Deep::Iterator::Index' ) ) { |
1054 | # If we don't have any more, it will be caught at the |
1055 | # prior check. |
1056 | if ( my $next = $iterator->get_next_iterator ) { |
1057 | push @$crumbs, $next; |
1058 | } |
1059 | redo FIND_NEXT_KEY; |
1060 | } |
1061 | |
1062 | unless ( $iterator->isa( 'DBM::Deep::Iterator::BucketList' ) ) { |
1063 | DBM::Deep->_throw_error( |
1064 | "Should have a bucketlist iterator here - instead have $iterator" |
1065 | ); |
1066 | } |
1067 | |
1068 | # At this point, we have a BucketList iterator |
1069 | my $key = $iterator->get_next_key; |
1070 | if ( defined $key ) { |
1071 | return $key; |
1072 | } |
1073 | #XXX else { $iterator->set_to_end() } ? |
1074 | |
1075 | # We hit the end of the bucketlist iterator, so redo |
1076 | redo FIND_NEXT_KEY; |
8db25060 |
1077 | } |
1078 | |
9a63e1f2 |
1079 | DBM::Deep->_throw_error( "get_next_key(): How did we get here?" ); |
8db25060 |
1080 | } |
1081 | |
9a63e1f2 |
1082 | package DBM::Deep::Iterator::Index; |
1083 | |
1084 | sub new { |
1085 | my $self = bless $_[1] => $_[0]; |
1086 | $self->{curr_index} = 0; |
1087 | return $self; |
1088 | } |
1089 | |
1090 | sub at_end { |
beac1dff |
1091 | my $self = shift; |
9a63e1f2 |
1092 | return $self->{curr_index} >= $self->{iterator}{engine}->hash_chars; |
1093 | } |
9020ee8c |
1094 | |
9a63e1f2 |
1095 | sub get_next_iterator { |
1096 | my $self = shift; |
94e8af14 |
1097 | |
9a63e1f2 |
1098 | my $loc; |
1099 | while ( !$loc ) { |
1100 | return if $self->at_end; |
1101 | $loc = $self->{sector}->get_entry( $self->{curr_index}++ ); |
386bab6c |
1102 | } |
94e8af14 |
1103 | |
9a63e1f2 |
1104 | return $self->{iterator}->get_sector_iterator( $loc ); |
1105 | } |
1106 | |
1107 | package DBM::Deep::Iterator::BucketList; |
1108 | |
1109 | sub new { |
1110 | my $self = bless $_[1] => $_[0]; |
1111 | $self->{curr_index} = 0; |
1112 | return $self; |
1113 | } |
1114 | |
1115 | sub at_end { |
1116 | my $self = shift; |
1117 | return $self->{curr_index} >= $self->{iterator}{engine}->max_buckets; |
1118 | } |
1119 | |
1120 | sub get_next_key { |
1121 | my $self = shift; |
1122 | |
1123 | return if $self->at_end; |
1124 | |
1125 | my $idx = $self->{curr_index}++; |
1126 | |
1127 | my $data_loc = $self->{sector}->get_data_location_for({ |
1128 | allow_head => 1, |
1129 | idx => $idx, |
1130 | }) or return; |
1131 | |
1132 | #XXX Do we want to add corruption checks here? |
1133 | return $self->{sector}->get_key_for( $idx )->data; |
1134 | } |
1135 | |
1136 | package DBM::Deep::Engine::Sector; |
1137 | |
1138 | sub new { |
1139 | my $self = bless $_[1], $_[0]; |
1140 | Scalar::Util::weaken( $self->{engine} ); |
1141 | $self->_init; |
1142 | return $self; |
1143 | } |
1144 | |
1145 | #sub _init {} |
1146 | #sub clone { DBM::Deep->_throw_error( "Must be implemented in the child class" ); } |
1147 | |
1148 | sub engine { $_[0]{engine} } |
1149 | sub offset { $_[0]{offset} } |
1150 | sub type { $_[0]{type} } |
1151 | |
1152 | sub base_size { |
1153 | my $self = shift; |
f72b2dfb |
1154 | return $self->engine->SIG_SIZE + $STALE_SIZE; |
9a63e1f2 |
1155 | } |
1156 | |
1157 | sub free { |
1158 | my $self = shift; |
1159 | |
1160 | my $e = $self->engine; |
1161 | |
1162 | $e->storage->print_at( $self->offset, $e->SIG_FREE ); |
1163 | # Skip staleness counter |
1164 | $e->storage->print_at( $self->offset + $self->base_size, |
1165 | chr(0) x ($self->size - $self->base_size), |
1166 | ); |
1167 | |
1168 | my $free_meth = $self->free_meth; |
1169 | $e->$free_meth( $self->offset, $self->size ); |
1170 | |
beac1dff |
1171 | return; |
9020ee8c |
1172 | } |
ab0e4957 |
1173 | |
9a63e1f2 |
1174 | package DBM::Deep::Engine::Sector::Data; |
1175 | |
1176 | our @ISA = qw( DBM::Deep::Engine::Sector ); |
1177 | |
1178 | # This is in bytes |
f72b2dfb |
1179 | sub size { $_[0]{engine}->data_sector_size } |
9a63e1f2 |
1180 | sub free_meth { return '_add_free_data_sector' } |
1181 | |
1182 | sub clone { |
beac1dff |
1183 | my $self = shift; |
9a63e1f2 |
1184 | return ref($self)->new({ |
1185 | engine => $self->engine, |
9a63e1f2 |
1186 | type => $self->type, |
f72b2dfb |
1187 | data => $self->data, |
9a63e1f2 |
1188 | }); |
1189 | } |
1190 | |
1191 | package DBM::Deep::Engine::Sector::Scalar; |
1192 | |
1193 | our @ISA = qw( DBM::Deep::Engine::Sector::Data ); |
ab0e4957 |
1194 | |
9a63e1f2 |
1195 | sub free { |
1196 | my $self = shift; |
633df1fd |
1197 | |
9a63e1f2 |
1198 | my $chain_loc = $self->chain_loc; |
633df1fd |
1199 | |
9a63e1f2 |
1200 | $self->SUPER::free(); |
633df1fd |
1201 | |
9a63e1f2 |
1202 | if ( $chain_loc ) { |
1203 | $self->engine->_load_sector( $chain_loc )->free; |
633df1fd |
1204 | } |
1205 | |
9a63e1f2 |
1206 | return; |
1207 | } |
1208 | |
1209 | sub type { $_[0]{engine}->SIG_DATA } |
1210 | sub _init { |
1211 | my $self = shift; |
1212 | |
1213 | my $engine = $self->engine; |
1214 | |
1215 | unless ( $self->offset ) { |
f72b2dfb |
1216 | my $data_section = $self->size - $self->base_size - $engine->byte_size - 1; |
7a960a12 |
1217 | |
9a63e1f2 |
1218 | $self->{offset} = $engine->_request_data_sector( $self->size ); |
7a960a12 |
1219 | |
9a63e1f2 |
1220 | my $data = delete $self->{data}; |
1221 | my $dlen = length $data; |
1222 | my $continue = 1; |
1223 | my $curr_offset = $self->offset; |
1224 | while ( $continue ) { |
633df1fd |
1225 | |
9a63e1f2 |
1226 | my $next_offset = 0; |
1227 | |
1228 | my ($leftover, $this_len, $chunk); |
1229 | if ( $dlen > $data_section ) { |
1230 | $leftover = 0; |
1231 | $this_len = $data_section; |
1232 | $chunk = substr( $data, 0, $this_len ); |
1233 | |
1234 | $dlen -= $data_section; |
1235 | $next_offset = $engine->_request_data_sector( $self->size ); |
1236 | $data = substr( $data, $this_len ); |
1237 | } |
1238 | else { |
1239 | $leftover = $data_section - $dlen; |
1240 | $this_len = $dlen; |
1241 | $chunk = $data; |
ea2f6d67 |
1242 | |
9a63e1f2 |
1243 | $continue = 0; |
ea2f6d67 |
1244 | } |
9a63e1f2 |
1245 | |
1246 | $engine->storage->print_at( $curr_offset, $self->type ); # Sector type |
1247 | # Skip staleness |
1248 | $engine->storage->print_at( $curr_offset + $self->base_size, |
1249 | pack( $StP{$engine->byte_size}, $next_offset ), # Chain loc |
1250 | pack( $StP{1}, $this_len ), # Data length |
1251 | $chunk, # Data to be stored in this sector |
1252 | chr(0) x $leftover, # Zero-fill the rest |
1253 | ); |
1254 | |
1255 | $curr_offset = $next_offset; |
633df1fd |
1256 | } |
1257 | |
9a63e1f2 |
1258 | return; |
386bab6c |
1259 | } |
9a63e1f2 |
1260 | } |
7a960a12 |
1261 | |
9a63e1f2 |
1262 | sub data_length { |
1263 | my $self = shift; |
7a960a12 |
1264 | |
9a63e1f2 |
1265 | my $buffer = $self->engine->storage->read_at( |
1266 | $self->offset + $self->base_size + $self->engine->byte_size, 1 |
1267 | ); |
633df1fd |
1268 | |
9a63e1f2 |
1269 | return unpack( $StP{1}, $buffer ); |
ab0e4957 |
1270 | } |
1271 | |
9a63e1f2 |
1272 | sub chain_loc { |
beac1dff |
1273 | my $self = shift; |
9a63e1f2 |
1274 | return unpack( |
1275 | $StP{$self->engine->byte_size}, |
1276 | $self->engine->storage->read_at( |
1277 | $self->offset + $self->base_size, |
1278 | $self->engine->byte_size, |
1279 | ), |
1280 | ); |
1281 | } |
912d50b1 |
1282 | |
9a63e1f2 |
1283 | sub data { |
1284 | my $self = shift; |
1285 | |
1286 | my $data; |
1287 | while ( 1 ) { |
1288 | my $chain_loc = $self->chain_loc; |
1289 | |
1290 | $data .= $self->engine->storage->read_at( |
1291 | $self->offset + $self->base_size + $self->engine->byte_size + 1, $self->data_length, |
1292 | ); |
1293 | |
1294 | last unless $chain_loc; |
1295 | |
1296 | $self = $self->engine->_load_sector( $chain_loc ); |
ea2f6d67 |
1297 | } |
9a63e1f2 |
1298 | |
1299 | return $data; |
912d50b1 |
1300 | } |
1301 | |
9a63e1f2 |
1302 | package DBM::Deep::Engine::Sector::Null; |
1303 | |
1304 | our @ISA = qw( DBM::Deep::Engine::Sector::Data ); |
1305 | |
1306 | sub type { $_[0]{engine}->SIG_NULL } |
1307 | sub data_length { 0 } |
1308 | sub data { return } |
1309 | |
1310 | sub _init { |
beac1dff |
1311 | my $self = shift; |
d0b74c17 |
1312 | |
9a63e1f2 |
1313 | my $engine = $self->engine; |
d0b74c17 |
1314 | |
9a63e1f2 |
1315 | unless ( $self->offset ) { |
1316 | my $leftover = $self->size - $self->base_size - 1 * $engine->byte_size - 1; |
d0b74c17 |
1317 | |
9a63e1f2 |
1318 | $self->{offset} = $engine->_request_data_sector( $self->size ); |
1319 | $engine->storage->print_at( $self->offset, $self->type ); # Sector type |
1320 | # Skip staleness counter |
1321 | $engine->storage->print_at( $self->offset + $self->base_size, |
1322 | pack( $StP{$engine->byte_size}, 0 ), # Chain loc |
1323 | pack( $StP{1}, $self->data_length ), # Data length |
1324 | chr(0) x $leftover, # Zero-fill the rest |
1325 | ); |
d0b74c17 |
1326 | |
9a63e1f2 |
1327 | return; |
1328 | } |
1329 | } |
d0b74c17 |
1330 | |
9a63e1f2 |
1331 | package DBM::Deep::Engine::Sector::Reference; |
16d1ad9b |
1332 | |
9a63e1f2 |
1333 | our @ISA = qw( DBM::Deep::Engine::Sector::Data ); |
d0b74c17 |
1334 | |
9a63e1f2 |
1335 | sub _init { |
1336 | my $self = shift; |
d5d7c51d |
1337 | |
9a63e1f2 |
1338 | my $e = $self->engine; |
d5d7c51d |
1339 | |
9a63e1f2 |
1340 | unless ( $self->offset ) { |
1341 | my $classname = Scalar::Util::blessed( delete $self->{data} ); |
e137c258 |
1342 | my $leftover = $self->size - $self->base_size - 3 * $e->byte_size; |
9a63e1f2 |
1343 | |
1344 | my $class_offset = 0; |
1345 | if ( defined $classname ) { |
1346 | my $class_sector = DBM::Deep::Engine::Sector::Scalar->new({ |
1347 | engine => $e, |
1348 | data => $classname, |
1349 | }); |
1350 | $class_offset = $class_sector->offset; |
d0b74c17 |
1351 | } |
1352 | |
9a63e1f2 |
1353 | $self->{offset} = $e->_request_data_sector( $self->size ); |
1354 | $e->storage->print_at( $self->offset, $self->type ); # Sector type |
1355 | # Skip staleness counter |
1356 | $e->storage->print_at( $self->offset + $self->base_size, |
1357 | pack( $StP{$e->byte_size}, 0 ), # Index/BList loc |
1358 | pack( $StP{$e->byte_size}, $class_offset ), # Classname loc |
e137c258 |
1359 | pack( $StP{$e->byte_size}, 1 ), # Initial refcount |
9a63e1f2 |
1360 | chr(0) x $leftover, # Zero-fill the rest |
1361 | ); |
1362 | } |
1363 | else { |
1364 | $self->{type} = $e->storage->read_at( $self->offset, 1 ); |
beac1dff |
1365 | } |
d0b74c17 |
1366 | |
9a63e1f2 |
1367 | $self->{staleness} = unpack( |
f72b2dfb |
1368 | $StP{$STALE_SIZE}, |
1369 | $e->storage->read_at( $self->offset + $e->SIG_SIZE, $STALE_SIZE ), |
9a63e1f2 |
1370 | ); |
1371 | |
1372 | return; |
6736c116 |
1373 | } |
1374 | |
9a63e1f2 |
1375 | sub free { |
d0b74c17 |
1376 | my $self = shift; |
d0b74c17 |
1377 | |
e137c258 |
1378 | # We're not ready to be removed yet. |
1379 | if ( $self->decrement_refcount > 0 ) { |
1380 | return; |
1381 | } |
1382 | |
9a63e1f2 |
1383 | my $blist_loc = $self->get_blist_loc; |
1384 | $self->engine->_load_sector( $blist_loc )->free if $blist_loc; |
d0b74c17 |
1385 | |
9a63e1f2 |
1386 | my $class_loc = $self->get_class_offset; |
1387 | $self->engine->_load_sector( $class_loc )->free if $class_loc; |
d0b74c17 |
1388 | |
9a63e1f2 |
1389 | $self->SUPER::free(); |
d0b74c17 |
1390 | } |
1391 | |
9a63e1f2 |
1392 | sub staleness { $_[0]{staleness} } |
1393 | |
1394 | sub get_data_for { |
6736c116 |
1395 | my $self = shift; |
9a63e1f2 |
1396 | my ($args) = @_; |
d0b74c17 |
1397 | |
9a63e1f2 |
1398 | # Assume that the head is not allowed unless otherwise specified. |
1399 | $args->{allow_head} = 0 unless exists $args->{allow_head}; |
6736c116 |
1400 | |
9a63e1f2 |
1401 | # Assume we don't create a new blist location unless otherwise specified. |
1402 | $args->{create} = 0 unless exists $args->{create}; |
d0b74c17 |
1403 | |
9a63e1f2 |
1404 | my $blist = $self->get_bucket_list({ |
1405 | key_md5 => $args->{key_md5}, |
1406 | key => $args->{key}, |
1407 | create => $args->{create}, |
1408 | }); |
1409 | return unless $blist && $blist->{found}; |
e5fc7e69 |
1410 | |
9a63e1f2 |
1411 | # At this point, $blist knows where the md5 is. What it -doesn't- know yet |
1412 | # is whether or not this transaction has this key. That's part of the next |
1413 | # function call. |
1414 | my $location = $blist->get_data_location_for({ |
1415 | allow_head => $args->{allow_head}, |
1416 | }) or return; |
e5fc7e69 |
1417 | |
9a63e1f2 |
1418 | return $self->engine->_load_sector( $location ); |
1419 | } |
d0b74c17 |
1420 | |
9a63e1f2 |
1421 | sub write_data { |
1422 | my $self = shift; |
1423 | my ($args) = @_; |
1424 | |
1425 | my $blist = $self->get_bucket_list({ |
1426 | key_md5 => $args->{key_md5}, |
1427 | key => $args->{key}, |
1428 | create => 1, |
1429 | }) or DBM::Deep->_throw_error( "How did write_data fail (no blist)?!" ); |
1430 | |
1431 | # Handle any transactional bookkeeping. |
1432 | if ( $self->engine->trans_id ) { |
1433 | if ( ! $blist->has_md5 ) { |
1434 | $blist->mark_deleted({ |
1435 | trans_id => 0, |
1436 | }); |
1437 | } |
42717e46 |
1438 | } |
e5fc7e69 |
1439 | else { |
9a63e1f2 |
1440 | my @trans_ids = $self->engine->get_running_txn_ids; |
1441 | if ( $blist->has_md5 ) { |
1442 | if ( @trans_ids ) { |
1443 | my $old_value = $blist->get_data_for; |
1444 | foreach my $other_trans_id ( @trans_ids ) { |
1445 | next if $blist->get_data_location_for({ |
1446 | trans_id => $other_trans_id, |
1447 | allow_head => 0, |
1448 | }); |
1449 | $blist->write_md5({ |
1450 | trans_id => $other_trans_id, |
1451 | key => $args->{key}, |
1452 | key_md5 => $args->{key_md5}, |
1453 | value => $old_value->clone, |
1454 | }); |
1455 | } |
beac1dff |
1456 | } |
9a63e1f2 |
1457 | } |
1458 | else { |
1459 | if ( @trans_ids ) { |
1460 | foreach my $other_trans_id ( @trans_ids ) { |
1461 | #XXX This doesn't seem to possible to ever happen . . . |
1462 | next if $blist->get_data_location_for({ trans_id => $other_trans_id, allow_head => 0 }); |
1463 | $blist->mark_deleted({ |
1464 | trans_id => $other_trans_id, |
1465 | }); |
ea2f6d67 |
1466 | } |
9a63e1f2 |
1467 | } |
1468 | } |
1469 | } |
ea2f6d67 |
1470 | |
9a63e1f2 |
1471 | #XXX Is this safe to do transactionally? |
1472 | # Free the place we're about to write to. |
1473 | if ( $blist->get_data_location_for({ allow_head => 0 }) ) { |
1474 | $blist->get_data_for({ allow_head => 0 })->free; |
1475 | } |
8db25060 |
1476 | |
9a63e1f2 |
1477 | $blist->write_md5({ |
1478 | key => $args->{key}, |
1479 | key_md5 => $args->{key_md5}, |
1480 | value => $args->{value}, |
1481 | }); |
1482 | } |
d0b74c17 |
1483 | |
9a63e1f2 |
1484 | sub delete_key { |
1485 | my $self = shift; |
1486 | my ($args) = @_; |
86867f3a |
1487 | |
9a63e1f2 |
1488 | # XXX What should happen if this fails? |
1489 | my $blist = $self->get_bucket_list({ |
1490 | key_md5 => $args->{key_md5}, |
1491 | }) or DBM::Deep->_throw_error( "How did delete_key fail (no blist)?!" ); |
1492 | |
1493 | # Save the location so that we can free the data |
1494 | my $location = $blist->get_data_location_for({ |
1495 | allow_head => 0, |
1496 | }); |
1497 | my $old_value = $location && $self->engine->_load_sector( $location ); |
1498 | |
1499 | my @trans_ids = $self->engine->get_running_txn_ids; |
1500 | |
90fb1a24 |
1501 | # If we're the HEAD and there are running txns, then we need to clone this value to the other |
1502 | # transactions to preserve Isolation. |
9a63e1f2 |
1503 | if ( $self->engine->trans_id == 0 ) { |
1504 | if ( @trans_ids ) { |
1505 | foreach my $other_trans_id ( @trans_ids ) { |
1506 | next if $blist->get_data_location_for({ trans_id => $other_trans_id, allow_head => 0 }); |
1507 | $blist->write_md5({ |
1508 | trans_id => $other_trans_id, |
1509 | key => $args->{key}, |
1510 | key_md5 => $args->{key_md5}, |
1511 | value => $old_value->clone, |
1512 | }); |
beac1dff |
1513 | } |
8db25060 |
1514 | } |
9a63e1f2 |
1515 | } |
d0b74c17 |
1516 | |
9a63e1f2 |
1517 | my $data; |
1518 | if ( @trans_ids ) { |
1519 | $blist->mark_deleted( $args ); |
1520 | |
1521 | if ( $old_value ) { |
1522 | $data = $old_value->data; |
1523 | $old_value->free; |
1524 | } |
1525 | } |
1526 | else { |
1527 | $data = $blist->delete_md5( $args ); |
42717e46 |
1528 | } |
d0b74c17 |
1529 | |
9a63e1f2 |
1530 | return $data; |
6736c116 |
1531 | } |
1532 | |
9a63e1f2 |
1533 | sub get_blist_loc { |
1534 | my $self = shift; |
75be6413 |
1535 | |
9a63e1f2 |
1536 | my $e = $self->engine; |
1537 | my $blist_loc = $e->storage->read_at( $self->offset + $self->base_size, $e->byte_size ); |
1538 | return unpack( $StP{$e->byte_size}, $blist_loc ); |
1539 | } |
1540 | |
1541 | sub get_bucket_list { |
75be6413 |
1542 | my $self = shift; |
9a63e1f2 |
1543 | my ($args) = @_; |
1544 | $args ||= {}; |
1545 | |
1546 | # XXX Add in check here for recycling? |
1547 | |
1548 | my $engine = $self->engine; |
1549 | |
1550 | my $blist_loc = $self->get_blist_loc; |
1551 | |
1552 | # There's no index or blist yet |
1553 | unless ( $blist_loc ) { |
1554 | return unless $args->{create}; |
1555 | |
1556 | my $blist = DBM::Deep::Engine::Sector::BucketList->new({ |
1557 | engine => $engine, |
1558 | key_md5 => $args->{key_md5}, |
1559 | }); |
1560 | |
1561 | $engine->storage->print_at( $self->offset + $self->base_size, |
1562 | pack( $StP{$engine->byte_size}, $blist->offset ), |
1563 | ); |
1564 | |
1565 | return $blist; |
1566 | } |
1567 | |
1568 | my $sector = $engine->_load_sector( $blist_loc ) |
1569 | or DBM::Deep->_throw_error( "Cannot read sector at $blist_loc in get_bucket_list()" ); |
1570 | my $i = 0; |
1571 | my $last_sector = undef; |
1572 | while ( $sector->isa( 'DBM::Deep::Engine::Sector::Index' ) ) { |
1573 | $blist_loc = $sector->get_entry( ord( substr( $args->{key_md5}, $i++, 1 ) ) ); |
1574 | $last_sector = $sector; |
1575 | if ( $blist_loc ) { |
1576 | $sector = $engine->_load_sector( $blist_loc ) |
1577 | or DBM::Deep->_throw_error( "Cannot read sector at $blist_loc in get_bucket_list()" ); |
1578 | } |
1579 | else { |
1580 | $sector = undef; |
1581 | last; |
1582 | } |
1583 | } |
1584 | |
1585 | # This means we went through the Index sector(s) and found an empty slot |
1586 | unless ( $sector ) { |
1587 | return unless $args->{create}; |
1588 | |
1589 | DBM::Deep->_throw_error( "No last_sector when attempting to build a new entry" ) |
1590 | unless $last_sector; |
1591 | |
1592 | my $blist = DBM::Deep::Engine::Sector::BucketList->new({ |
1593 | engine => $engine, |
1594 | key_md5 => $args->{key_md5}, |
1595 | }); |
1596 | |
1597 | $last_sector->set_entry( ord( substr( $args->{key_md5}, $i - 1, 1 ) ) => $blist->offset ); |
1598 | |
1599 | return $blist; |
1600 | } |
75be6413 |
1601 | |
9a63e1f2 |
1602 | $sector->find_md5( $args->{key_md5} ); |
1603 | |
1604 | # See whether or not we need to reindex the bucketlist |
1605 | if ( !$sector->has_md5 && $args->{create} && $sector->{idx} == -1 ) { |
1606 | my $new_index = DBM::Deep::Engine::Sector::Index->new({ |
1607 | engine => $engine, |
1608 | }); |
1609 | |
1610 | my %blist_cache; |
1611 | #XXX q.v. the comments for this function. |
1612 | foreach my $entry ( $sector->chopped_up ) { |
1613 | my ($spot, $md5) = @{$entry}; |
1614 | my $idx = ord( substr( $md5, $i, 1 ) ); |
1615 | |
1616 | # XXX This is inefficient |
1617 | my $blist = $blist_cache{$idx} |
1618 | ||= DBM::Deep::Engine::Sector::BucketList->new({ |
1619 | engine => $engine, |
1620 | }); |
1621 | |
1622 | $new_index->set_entry( $idx => $blist->offset ); |
1623 | |
1624 | my $new_spot = $blist->write_at_next_open( $md5 ); |
1625 | $engine->reindex_entry( $spot => $new_spot ); |
1626 | } |
1627 | |
1628 | # Handle the new item separately. |
1629 | { |
1630 | my $idx = ord( substr( $args->{key_md5}, $i, 1 ) ); |
1631 | my $blist = $blist_cache{$idx} |
1632 | ||= DBM::Deep::Engine::Sector::BucketList->new({ |
1633 | engine => $engine, |
1634 | }); |
1635 | |
1636 | $new_index->set_entry( $idx => $blist->offset ); |
1637 | |
1638 | #XXX THIS IS HACKY! |
1639 | $blist->find_md5( $args->{key_md5} ); |
1640 | $blist->write_md5({ |
1641 | key => $args->{key}, |
1642 | key_md5 => $args->{key_md5}, |
1643 | value => DBM::Deep::Engine::Sector::Null->new({ |
1644 | engine => $engine, |
1645 | data => undef, |
1646 | }), |
1647 | }); |
1648 | } |
1649 | |
1650 | if ( $last_sector ) { |
1651 | $last_sector->set_entry( |
1652 | ord( substr( $args->{key_md5}, $i - 1, 1 ) ), |
1653 | $new_index->offset, |
1654 | ); |
1655 | } else { |
1656 | $engine->storage->print_at( $self->offset + $self->base_size, |
1657 | pack( $StP{$engine->byte_size}, $new_index->offset ), |
1658 | ); |
1659 | } |
1660 | |
90fb1a24 |
1661 | $sector->clear; |
9a63e1f2 |
1662 | $sector->free; |
1663 | |
1664 | $sector = $blist_cache{ ord( substr( $args->{key_md5}, $i, 1 ) ) }; |
1665 | $sector->find_md5( $args->{key_md5} ); |
1666 | } |
1667 | |
1668 | return $sector; |
1669 | } |
1670 | |
1671 | sub get_class_offset { |
1672 | my $self = shift; |
1673 | |
1674 | my $e = $self->engine; |
ea2f6d67 |
1675 | return unpack( |
9a63e1f2 |
1676 | $StP{$e->byte_size}, |
1677 | $e->storage->read_at( |
1678 | $self->offset + $self->base_size + 1 * $e->byte_size, $e->byte_size, |
75be6413 |
1679 | ), |
1680 | ); |
75be6413 |
1681 | } |
1682 | |
9a63e1f2 |
1683 | sub get_classname { |
d608b06e |
1684 | my $self = shift; |
21838116 |
1685 | |
9a63e1f2 |
1686 | my $class_offset = $self->get_class_offset; |
1687 | |
1688 | return unless $class_offset; |
1689 | |
1690 | return $self->engine->_load_sector( $class_offset )->data; |
1691 | } |
1692 | |
1693 | #XXX Add singleton handling here |
1694 | sub data { |
1695 | my $self = shift; |
1696 | |
1697 | my $new_obj = DBM::Deep->new({ |
1698 | type => $self->type, |
1699 | base_offset => $self->offset, |
1700 | staleness => $self->staleness, |
1701 | storage => $self->engine->storage, |
1702 | engine => $self->engine, |
1703 | }); |
1704 | |
1705 | if ( $self->engine->storage->{autobless} ) { |
1706 | my $classname = $self->get_classname; |
1707 | if ( defined $classname ) { |
1708 | bless $new_obj, $classname; |
1709 | } |
1710 | } |
1711 | |
1712 | return $new_obj; |
1713 | } |
1714 | |
e137c258 |
1715 | sub increment_refcount { |
1716 | my $self = shift; |
1717 | |
1718 | my $e = $self->engine; |
1719 | my $refcount = unpack( |
1720 | $StP{$e->byte_size}, |
1721 | $e->storage->read_at( |
1722 | $self->offset + $self->base_size + 2 * $e->byte_size, $e->byte_size, |
1723 | ), |
1724 | ); |
1725 | |
1726 | $refcount++; |
1727 | |
1728 | $e->storage->print_at( |
1729 | $self->offset + $self->base_size + 2 * $e->byte_size, |
1730 | pack( $StP{$e->byte_size}, $refcount ), |
1731 | ); |
1732 | |
1733 | return $refcount; |
1734 | } |
1735 | |
1736 | sub decrement_refcount { |
1737 | my $self = shift; |
1738 | |
1739 | my $e = $self->engine; |
1740 | my $refcount = unpack( |
1741 | $StP{$e->byte_size}, |
1742 | $e->storage->read_at( |
1743 | $self->offset + $self->base_size + 2 * $e->byte_size, $e->byte_size, |
1744 | ), |
1745 | ); |
1746 | |
1747 | $refcount--; |
1748 | |
1749 | $e->storage->print_at( |
1750 | $self->offset + $self->base_size + 2 * $e->byte_size, |
1751 | pack( $StP{$e->byte_size}, $refcount ), |
1752 | ); |
1753 | |
1754 | return $refcount; |
1755 | } |
1756 | |
1757 | sub get_refcount { |
1758 | my $self = shift; |
1759 | |
1760 | my $e = $self->engine; |
1761 | return unpack( |
1762 | $StP{$e->byte_size}, |
1763 | $e->storage->read_at( |
1764 | $self->offset + $self->base_size + 2 * $e->byte_size, $e->byte_size, |
1765 | ), |
1766 | ); |
1767 | } |
1768 | |
9a63e1f2 |
1769 | package DBM::Deep::Engine::Sector::BucketList; |
1770 | |
1771 | our @ISA = qw( DBM::Deep::Engine::Sector ); |
1772 | |
1773 | sub _init { |
1774 | my $self = shift; |
1775 | |
1776 | my $engine = $self->engine; |
1777 | |
1778 | unless ( $self->offset ) { |
1779 | my $leftover = $self->size - $self->base_size; |
1780 | |
1781 | $self->{offset} = $engine->_request_blist_sector( $self->size ); |
1782 | $engine->storage->print_at( $self->offset, $engine->SIG_BLIST ); # Sector type |
1783 | # Skip staleness counter |
1784 | $engine->storage->print_at( $self->offset + $self->base_size, |
1785 | chr(0) x $leftover, # Zero-fill the data |
9a187d8c |
1786 | ); |
9a63e1f2 |
1787 | } |
d608b06e |
1788 | |
9a63e1f2 |
1789 | if ( $self->{key_md5} ) { |
1790 | $self->find_md5; |
d608b06e |
1791 | } |
1792 | |
9a63e1f2 |
1793 | return $self; |
d608b06e |
1794 | } |
1795 | |
90fb1a24 |
1796 | sub clear { |
1797 | my $self = shift; |
1798 | $self->engine->storage->print_at( $self->offset + $self->base_size, |
1799 | chr(0) x ($self->size - $self->base_size), # Zero-fill the data |
1800 | ); |
1801 | } |
1802 | |
9a63e1f2 |
1803 | sub size { |
994ccd8e |
1804 | my $self = shift; |
9a63e1f2 |
1805 | unless ( $self->{size} ) { |
1806 | my $e = $self->engine; |
1807 | # Base + numbuckets * bucketsize |
1808 | $self->{size} = $self->base_size + $e->max_buckets * $self->bucket_size; |
1809 | } |
1810 | return $self->{size}; |
1811 | } |
994ccd8e |
1812 | |
9a63e1f2 |
1813 | sub free_meth { return '_add_free_blist_sector' } |
7b1e1aa1 |
1814 | |
9f83dc31 |
1815 | sub free { |
1816 | my $self = shift; |
1817 | |
1818 | my $e = $self->engine; |
1819 | foreach my $bucket ( $self->chopped_up ) { |
1820 | my $rest = $bucket->[-1]; |
1821 | |
1822 | # Delete the keysector |
1823 | my $l = unpack( $StP{$e->byte_size}, substr( $rest, $e->hash_size, $e->byte_size ) ); |
1824 | my $s = $e->_load_sector( $l ); $s->free if $s; |
1825 | |
1826 | foreach my $txn ( 0 .. $e->num_txns - 1 ) { |
1827 | my $l = unpack( $StP{$e->byte_size}, |
1828 | substr( $rest, |
1829 | $e->hash_size + $e->byte_size + $txn * ($e->byte_size + $STALE_SIZE), |
1830 | $e->byte_size, |
1831 | ), |
1832 | ); |
1833 | my $s = $e->_load_sector( $l ); $s->free if $s; |
1834 | } |
1835 | } |
1836 | |
1837 | $self->SUPER::free(); |
1838 | } |
1839 | |
9a63e1f2 |
1840 | sub bucket_size { |
1841 | my $self = shift; |
1842 | unless ( $self->{bucket_size} ) { |
1843 | my $e = $self->engine; |
1844 | # Key + head (location) + transactions (location + staleness-counter) |
f72b2dfb |
1845 | my $location_size = $e->byte_size + $e->byte_size + ($e->num_txns - 1) * ($e->byte_size + $STALE_SIZE); |
9a63e1f2 |
1846 | $self->{bucket_size} = $e->hash_size + $location_size; |
1847 | } |
1848 | return $self->{bucket_size}; |
1849 | } |
7b1e1aa1 |
1850 | |
9a63e1f2 |
1851 | # XXX This is such a poor hack. I need to rethink this code. |
1852 | sub chopped_up { |
1853 | my $self = shift; |
1854 | |
1855 | my $e = $self->engine; |
1856 | |
1857 | my @buckets; |
1858 | foreach my $idx ( 0 .. $e->max_buckets - 1 ) { |
1859 | my $spot = $self->offset + $self->base_size + $idx * $self->bucket_size; |
1860 | my $md5 = $e->storage->read_at( $spot, $e->hash_size ); |
1861 | |
1862 | #XXX If we're chopping, why would we ever have the blank_md5? |
1863 | last if $md5 eq $e->blank_md5; |
1864 | |
1865 | my $rest = $e->storage->read_at( undef, $self->bucket_size - $e->hash_size ); |
1866 | push @buckets, [ $spot, $md5 . $rest ]; |
1867 | } |
1868 | |
1869 | return @buckets; |
994ccd8e |
1870 | } |
1871 | |
9a63e1f2 |
1872 | sub write_at_next_open { |
1873 | my $self = shift; |
1874 | my ($entry) = @_; |
1875 | |
1876 | #XXX This is such a hack! |
1877 | $self->{_next_open} = 0 unless exists $self->{_next_open}; |
1878 | |
1879 | my $spot = $self->offset + $self->base_size + $self->{_next_open}++ * $self->bucket_size; |
1880 | $self->engine->storage->print_at( $spot, $entry ); |
1881 | |
1882 | return $spot; |
e96daec8 |
1883 | } |
1884 | |
9a63e1f2 |
1885 | sub has_md5 { |
1886 | my $self = shift; |
1887 | unless ( exists $self->{found} ) { |
1888 | $self->find_md5; |
1889 | } |
1890 | return $self->{found}; |
1891 | } |
86867f3a |
1892 | |
9a63e1f2 |
1893 | sub find_md5 { |
1894 | my $self = shift; |
1895 | |
1896 | $self->{found} = undef; |
1897 | $self->{idx} = -1; |
1898 | |
1899 | if ( @_ ) { |
1900 | $self->{key_md5} = shift; |
1901 | } |
1902 | |
1903 | # If we don't have an MD5, then what are we supposed to do? |
1904 | unless ( exists $self->{key_md5} ) { |
1905 | DBM::Deep->_throw_error( "Cannot find_md5 without a key_md5 set" ); |
1906 | } |
1907 | |
1908 | my $e = $self->engine; |
1909 | foreach my $idx ( 0 .. $e->max_buckets - 1 ) { |
1910 | my $potential = $e->storage->read_at( |
1911 | $self->offset + $self->base_size + $idx * $self->bucket_size, $e->hash_size, |
1912 | ); |
1913 | |
1914 | if ( $potential eq $e->blank_md5 ) { |
1915 | $self->{idx} = $idx; |
86867f3a |
1916 | return; |
9a63e1f2 |
1917 | } |
1918 | |
1919 | if ( $potential eq $self->{key_md5} ) { |
1920 | $self->{found} = 1; |
1921 | $self->{idx} = $idx; |
86867f3a |
1922 | return; |
9a63e1f2 |
1923 | } |
86867f3a |
1924 | } |
1925 | |
1926 | return; |
1927 | } |
1928 | |
9a63e1f2 |
1929 | sub write_md5 { |
86867f3a |
1930 | my $self = shift; |
9a63e1f2 |
1931 | my ($args) = @_; |
1932 | |
1933 | DBM::Deep->_throw_error( "write_md5: no key" ) unless exists $args->{key}; |
1934 | DBM::Deep->_throw_error( "write_md5: no key_md5" ) unless exists $args->{key_md5}; |
1935 | DBM::Deep->_throw_error( "write_md5: no value" ) unless exists $args->{value}; |
86867f3a |
1936 | |
9a63e1f2 |
1937 | my $engine = $self->engine; |
86867f3a |
1938 | |
9a63e1f2 |
1939 | $args->{trans_id} = $engine->trans_id unless exists $args->{trans_id}; |
86867f3a |
1940 | |
9a63e1f2 |
1941 | my $spot = $self->offset + $self->base_size + $self->{idx} * $self->bucket_size; |
1942 | $engine->add_entry( $args->{trans_id}, $spot ); |
1943 | |
1944 | unless ($self->{found}) { |
1945 | my $key_sector = DBM::Deep::Engine::Sector::Scalar->new({ |
1946 | engine => $engine, |
1947 | data => $args->{key}, |
1948 | }); |
1949 | |
1950 | $engine->storage->print_at( $spot, |
1951 | $args->{key_md5}, |
1952 | pack( $StP{$engine->byte_size}, $key_sector->offset ), |
1953 | ); |
86867f3a |
1954 | } |
1955 | |
9a63e1f2 |
1956 | my $loc = $spot |
1957 | + $engine->hash_size |
f72b2dfb |
1958 | + $engine->byte_size; |
9a63e1f2 |
1959 | |
f72b2dfb |
1960 | if ( $args->{trans_id} ) { |
1961 | $loc += $engine->byte_size + ($args->{trans_id} - 1) * ( $engine->byte_size + $STALE_SIZE ); |
1962 | |
1963 | $engine->storage->print_at( $loc, |
1964 | pack( $StP{$engine->byte_size}, $args->{value}->offset ), |
1965 | pack( $StP{$STALE_SIZE}, $engine->get_txn_staleness_counter( $args->{trans_id} ) ), |
1966 | ); |
1967 | } |
1968 | else { |
1969 | $engine->storage->print_at( $loc, |
1970 | pack( $StP{$engine->byte_size}, $args->{value}->offset ), |
1971 | ); |
1972 | } |
9a63e1f2 |
1973 | } |
1974 | |
1975 | sub mark_deleted { |
1976 | my $self = shift; |
1977 | my ($args) = @_; |
1978 | $args ||= {}; |
1979 | |
1980 | my $engine = $self->engine; |
1981 | |
1982 | $args->{trans_id} = $engine->trans_id unless exists $args->{trans_id}; |
1983 | |
1984 | my $spot = $self->offset + $self->base_size + $self->{idx} * $self->bucket_size; |
1985 | $engine->add_entry( $args->{trans_id}, $spot ); |
1986 | |
1987 | my $loc = $spot |
1988 | + $engine->hash_size |
f72b2dfb |
1989 | + $engine->byte_size; |
1990 | |
1991 | if ( $args->{trans_id} ) { |
1992 | $loc += $engine->byte_size + ($args->{trans_id} - 1) * ( $engine->byte_size + $STALE_SIZE ); |
1993 | |
1994 | $engine->storage->print_at( $loc, |
1995 | pack( $StP{$engine->byte_size}, 1 ), # 1 is the marker for deleted |
1996 | pack( $StP{$STALE_SIZE}, $engine->get_txn_staleness_counter( $args->{trans_id} ) ), |
1997 | ); |
1998 | } |
1999 | else { |
2000 | $engine->storage->print_at( $loc, |
2001 | pack( $StP{$engine->byte_size}, 1 ), # 1 is the marker for deleted |
2002 | ); |
2003 | } |
9a63e1f2 |
2004 | |
9a63e1f2 |
2005 | } |
2006 | |
2007 | sub delete_md5 { |
2008 | my $self = shift; |
2009 | my ($args) = @_; |
2010 | |
2011 | my $engine = $self->engine; |
2012 | return undef unless $self->{found}; |
2013 | |
2014 | # Save the location so that we can free the data |
2015 | my $location = $self->get_data_location_for({ |
2016 | allow_head => 0, |
2017 | }); |
2018 | my $key_sector = $self->get_key_for; |
2019 | |
2020 | my $spot = $self->offset + $self->base_size + $self->{idx} * $self->bucket_size; |
2021 | $engine->storage->print_at( $spot, |
2022 | $engine->storage->read_at( |
2023 | $spot + $self->bucket_size, |
2024 | $self->bucket_size * ( $engine->max_buckets - $self->{idx} - 1 ), |
2025 | ), |
2026 | chr(0) x $self->bucket_size, |
2027 | ); |
2028 | |
2029 | $key_sector->free; |
2030 | |
2031 | my $data_sector = $self->engine->_load_sector( $location ); |
2032 | my $data = $data_sector->data; |
2033 | $data_sector->free; |
2034 | |
2035 | return $data; |
2036 | } |
2037 | |
2038 | sub get_data_location_for { |
2039 | my $self = shift; |
2040 | my ($args) = @_; |
2041 | $args ||= {}; |
2042 | |
2043 | $args->{allow_head} = 0 unless exists $args->{allow_head}; |
2044 | $args->{trans_id} = $self->engine->trans_id unless exists $args->{trans_id}; |
2045 | $args->{idx} = $self->{idx} unless exists $args->{idx}; |
2046 | |
2047 | my $e = $self->engine; |
2048 | |
2049 | my $spot = $self->offset + $self->base_size |
2050 | + $args->{idx} * $self->bucket_size |
2051 | + $e->hash_size |
f72b2dfb |
2052 | + $e->byte_size; |
2053 | |
2054 | if ( $args->{trans_id} ) { |
2055 | $spot += $e->byte_size + ($args->{trans_id} - 1) * ( $e->byte_size + $STALE_SIZE ); |
2056 | } |
9a63e1f2 |
2057 | |
2058 | my $buffer = $e->storage->read_at( |
2059 | $spot, |
f72b2dfb |
2060 | $e->byte_size + $STALE_SIZE, |
9a63e1f2 |
2061 | ); |
f72b2dfb |
2062 | my ($loc, $staleness) = unpack( $StP{$e->byte_size} . ' ' . $StP{$STALE_SIZE}, $buffer ); |
2063 | |
2064 | if ( $args->{trans_id} ) { |
2065 | # We have found an entry that is old, so get rid of it |
2066 | if ( $staleness != (my $s = $e->get_txn_staleness_counter( $args->{trans_id} ) ) ) { |
2067 | $e->storage->print_at( |
2068 | $spot, |
2069 | pack( $StP{$e->byte_size} . ' ' . $StP{$STALE_SIZE}, (0) x 2 ), |
2070 | ); |
2071 | $loc = 0; |
2072 | } |
86867f3a |
2073 | } |
2074 | |
9a63e1f2 |
2075 | # If we're in a transaction and we never wrote to this location, try the |
2076 | # HEAD instead. |
2077 | if ( $args->{trans_id} && !$loc && $args->{allow_head} ) { |
2078 | return $self->get_data_location_for({ |
2079 | trans_id => 0, |
2080 | allow_head => 1, |
2081 | idx => $args->{idx}, |
2082 | }); |
2083 | } |
2084 | return $loc <= 1 ? 0 : $loc; |
2085 | } |
2086 | |
2087 | sub get_data_for { |
2088 | my $self = shift; |
2089 | my ($args) = @_; |
2090 | $args ||= {}; |
2091 | |
2092 | return unless $self->{found}; |
2093 | my $location = $self->get_data_location_for({ |
2094 | allow_head => $args->{allow_head}, |
2095 | }); |
2096 | return $self->engine->_load_sector( $location ); |
2097 | } |
2098 | |
2099 | sub get_key_for { |
2100 | my $self = shift; |
2101 | my ($idx) = @_; |
2102 | $idx = $self->{idx} unless defined $idx; |
2103 | |
2104 | if ( $idx >= $self->engine->max_buckets ) { |
2105 | DBM::Deep->_throw_error( "get_key_for(): Attempting to retrieve $idx" ); |
86867f3a |
2106 | } |
2107 | |
9a63e1f2 |
2108 | my $location = $self->engine->storage->read_at( |
2109 | $self->offset + $self->base_size + $idx * $self->bucket_size + $self->engine->hash_size, |
2110 | $self->engine->byte_size, |
2111 | ); |
2112 | $location = unpack( $StP{$self->engine->byte_size}, $location ); |
2113 | DBM::Deep->_throw_error( "get_key_for: No location?" ) unless $location; |
2114 | |
2115 | return $self->engine->_load_sector( $location ); |
2116 | } |
86867f3a |
2117 | |
9a63e1f2 |
2118 | package DBM::Deep::Engine::Sector::Index; |
2119 | |
2120 | our @ISA = qw( DBM::Deep::Engine::Sector ); |
2121 | |
2122 | sub _init { |
2123 | my $self = shift; |
2124 | |
2125 | my $engine = $self->engine; |
2126 | |
2127 | unless ( $self->offset ) { |
2128 | my $leftover = $self->size - $self->base_size; |
2129 | |
2130 | $self->{offset} = $engine->_request_index_sector( $self->size ); |
2131 | $engine->storage->print_at( $self->offset, $engine->SIG_INDEX ); # Sector type |
2132 | # Skip staleness counter |
2133 | $engine->storage->print_at( $self->offset + $self->base_size, |
2134 | chr(0) x $leftover, # Zero-fill the rest |
2135 | ); |
2136 | } |
2137 | |
2138 | return $self; |
2139 | } |
2140 | |
f72b2dfb |
2141 | #XXX Change here |
9a63e1f2 |
2142 | sub size { |
2143 | my $self = shift; |
2144 | unless ( $self->{size} ) { |
2145 | my $e = $self->engine; |
2146 | $self->{size} = $self->base_size + $e->byte_size * $e->hash_chars; |
2147 | } |
2148 | return $self->{size}; |
2149 | } |
2150 | |
2151 | sub free_meth { return '_add_free_index_sector' } |
2152 | |
2153 | sub free { |
2154 | my $self = shift; |
2155 | my $e = $self->engine; |
2156 | |
2157 | for my $i ( 0 .. $e->hash_chars - 1 ) { |
2158 | my $l = $self->get_entry( $i ) or next; |
2159 | $e->_load_sector( $l )->free; |
86867f3a |
2160 | } |
2161 | |
9a63e1f2 |
2162 | $self->SUPER::free(); |
2163 | } |
2164 | |
2165 | sub _loc_for { |
2166 | my $self = shift; |
2167 | my ($idx) = @_; |
2168 | return $self->offset + $self->base_size + $idx * $self->engine->byte_size; |
2169 | } |
2170 | |
2171 | sub get_entry { |
2172 | my $self = shift; |
2173 | my ($idx) = @_; |
2174 | |
2175 | my $e = $self->engine; |
2176 | |
2177 | DBM::Deep->_throw_error( "get_entry: Out of range ($idx)" ) |
2178 | if $idx < 0 || $idx >= $e->hash_chars; |
2179 | |
2180 | return unpack( |
2181 | $StP{$e->byte_size}, |
2182 | $e->storage->read_at( $self->_loc_for( $idx ), $e->byte_size ), |
2183 | ); |
2184 | } |
2185 | |
2186 | sub set_entry { |
2187 | my $self = shift; |
2188 | my ($idx, $loc) = @_; |
2189 | |
2190 | my $e = $self->engine; |
2191 | |
2192 | DBM::Deep->_throw_error( "set_entry: Out of range ($idx)" ) |
2193 | if $idx < 0 || $idx >= $e->hash_chars; |
2194 | |
2195 | $self->engine->storage->print_at( |
2196 | $self->_loc_for( $idx ), |
2197 | pack( $StP{$e->byte_size}, $loc ), |
2198 | ); |
86867f3a |
2199 | } |
2200 | |
a20d9a3f |
2201 | 1; |
2202 | __END__ |