Commit | Line | Data |
a20d9a3f |
1 | package DBM::Deep::Engine; |
2 | |
3 | use strict; |
4 | |
5 | use Fcntl qw( :DEFAULT :flock :seek ); |
6 | |
8db25060 |
7 | ## |
8 | # Setup file and tag signatures. These should never change. |
9 | ## |
10 | sub SIG_FILE () { 'DPDB' } |
11 | sub SIG_INTERNAL () { 'i' } |
12 | sub SIG_HASH () { 'H' } |
13 | sub SIG_ARRAY () { 'A' } |
14 | sub SIG_SCALAR () { 'S' } |
15 | sub SIG_NULL () { 'N' } |
16 | sub SIG_DATA () { 'D' } |
17 | sub SIG_INDEX () { 'I' } |
18 | sub SIG_BLIST () { 'B' } |
19 | sub SIG_SIZE () { 1 } |
20 | |
612969fb |
21 | sub precalc_sizes { |
beac1dff |
22 | ## |
23 | # Precalculate index, bucket and bucket list sizes |
24 | ## |
251dfd0e |
25 | my $self = shift; |
1bf65be7 |
26 | |
251dfd0e |
27 | $self->{index_size} = (2**8) * $self->{long_size}; |
28 | $self->{bucket_size} = $self->{hash_size} + $self->{long_size}; |
29 | $self->{bucket_list_size} = $self->{max_buckets} * $self->{bucket_size}; |
1bf65be7 |
30 | |
251dfd0e |
31 | return 1; |
1bf65be7 |
32 | } |
33 | |
34 | sub set_pack { |
beac1dff |
35 | ## |
36 | # Set pack/unpack modes (see file header for more) |
37 | ## |
251dfd0e |
38 | my $self = shift; |
1bf65be7 |
39 | my ($long_s, $long_p, $data_s, $data_p) = @_; |
40 | |
81d16922 |
41 | ## |
75be6413 |
42 | # Set to 4 and 'N' for 32-bit offset tags (default). Theoretical limit of 4 |
43 | # GB per file. |
beac1dff |
44 | # (Perl must be compiled with largefile support for files > 2 GB) |
81d16922 |
45 | # |
46 | # Set to 8 and 'Q' for 64-bit offsets. Theoretical limit of 16 XB per file. |
beac1dff |
47 | # (Perl must be compiled with largefile and 64-bit long support) |
81d16922 |
48 | ## |
251dfd0e |
49 | $self->{long_size} = $long_s ? $long_s : 4; |
50 | $self->{long_pack} = $long_p ? $long_p : 'N'; |
1bf65be7 |
51 | |
81d16922 |
52 | ## |
75be6413 |
53 | # Set to 4 and 'N' for 32-bit data length prefixes. Limit of 4 GB for each |
54 | # key/value. Upgrading this is possible (see above) but probably not necessary. |
55 | # If you need more than 4 GB for a single key or value, this module is really |
56 | # not for you :-) |
81d16922 |
57 | ## |
251dfd0e |
58 | $self->{data_size} = $data_s ? $data_s : 4; |
59 | $self->{data_pack} = $data_p ? $data_p : 'N'; |
1bf65be7 |
60 | |
beac1dff |
61 | return $self->precalc_sizes(); |
1bf65be7 |
62 | } |
63 | |
64 | sub set_digest { |
beac1dff |
65 | ## |
66 | # Set key digest function (default is MD5) |
67 | ## |
251dfd0e |
68 | my $self = shift; |
1bf65be7 |
69 | my ($digest_func, $hash_size) = @_; |
70 | |
d0b74c17 |
71 | $self->{digest} = $digest_func ? $digest_func : \&Digest::MD5::md5; |
251dfd0e |
72 | $self->{hash_size} = $hash_size ? $hash_size : 16; |
612969fb |
73 | |
beac1dff |
74 | return $self->precalc_sizes(); |
612969fb |
75 | } |
76 | |
77 | sub new { |
78 | my $class = shift; |
79 | my ($args) = @_; |
80 | |
81 | my $self = bless { |
82 | long_size => 4, |
83 | long_pack => 'N', |
84 | data_size => 4, |
85 | data_pack => 'N', |
251dfd0e |
86 | |
612969fb |
87 | digest => \&Digest::MD5::md5, |
88 | hash_size => 16, |
251dfd0e |
89 | |
81d16922 |
90 | ## |
91 | # Maximum number of buckets per list before another level of indexing is done. |
92 | # Increase this value for slightly greater speed, but larger database files. |
93 | # DO NOT decrease this value below 16, due to risk of recursive reindex overrun. |
94 | ## |
612969fb |
95 | max_buckets => 16, |
96 | }, $class; |
97 | |
251dfd0e |
98 | $self->precalc_sizes; |
612969fb |
99 | |
100 | return $self; |
1bf65be7 |
101 | } |
102 | |
70b55428 |
103 | sub setup_fh { |
104 | my $self = shift; |
105 | my ($obj) = @_; |
106 | |
107 | $self->open( $obj ) if !defined $obj->_fh; |
108 | |
673464d9 |
109 | #XXX We have to make sure we don't mess up when autoflush isn't turned on |
70b55428 |
110 | unless ( $obj->_root->{inode} ) { |
111 | my @stats = stat($obj->_fh); |
112 | $obj->_root->{inode} = $stats[1]; |
113 | $obj->_root->{end} = $stats[7]; |
114 | } |
115 | |
116 | return 1; |
117 | } |
118 | |
a20d9a3f |
119 | sub open { |
20f7b20c |
120 | ## |
121 | # Open a fh to the database, create if nonexistent. |
122 | # Make sure file signature matches DBM::Deep spec. |
123 | ## |
a20d9a3f |
124 | my $self = shift; |
70b55428 |
125 | my ($obj) = @_; |
a20d9a3f |
126 | |
3d1b8be9 |
127 | if (defined($obj->_fh)) { $self->close_fh( $obj ); } |
20f7b20c |
128 | |
673464d9 |
129 | # Theoretically, adding O_BINARY should remove the need for the binmode |
130 | # Of course, testing it is going to be ... interesting. |
131 | my $flags = O_RDWR | O_CREAT | O_BINARY; |
a20d9a3f |
132 | |
673464d9 |
133 | my $fh; |
134 | sysopen( $fh, $obj->_root->{file}, $flags ) |
135 | or $obj->_throw_error("Cannot sysopen file: " . $obj->_root->{file} . ": $!"); |
136 | $obj->_root->{fh} = $fh; |
a20d9a3f |
137 | |
138 | #XXX Can we remove this by using the right sysopen() flags? |
139 | # Maybe ... q.v. above |
140 | binmode $fh; # for win32 |
141 | |
cd59cad8 |
142 | if ($obj->_root->{autoflush}) { |
a20d9a3f |
143 | my $old = select $fh; |
144 | $|=1; |
145 | select $old; |
146 | } |
20f7b20c |
147 | |
cd59cad8 |
148 | seek($fh, 0 + $obj->_root->{file_offset}, SEEK_SET); |
a20d9a3f |
149 | |
150 | my $signature; |
8db25060 |
151 | my $bytes_read = read( $fh, $signature, length(SIG_FILE)); |
20f7b20c |
152 | |
a20d9a3f |
153 | ## |
154 | # File is empty -- write signature and master index |
155 | ## |
156 | if (!$bytes_read) { |
cd59cad8 |
157 | seek($fh, 0 + $obj->_root->{file_offset}, SEEK_SET); |
8db25060 |
158 | print( $fh SIG_FILE); |
d0b74c17 |
159 | |
ec1bce6b |
160 | $self->create_tag($obj, $obj->_base_offset, $obj->_type, chr(0) x $self->{index_size}); |
a20d9a3f |
161 | |
e5fc7e69 |
162 | # Why is this being printed here? I'm not seeing where anything actually points to |
163 | # this spot. |
164 | #XXX $obj->_root->{end} isn't updated from these 10 bytes that are being written |
a20d9a3f |
165 | my $plain_key = "[base]"; |
251dfd0e |
166 | print( $fh pack($self->{data_pack}, length($plain_key)) . $plain_key ); |
a20d9a3f |
167 | |
168 | # Flush the filehandle |
169 | my $old_fh = select $fh; |
170 | my $old_af = $|; $| = 1; $| = $old_af; |
171 | select $old_fh; |
172 | |
a20d9a3f |
173 | return 1; |
174 | } |
20f7b20c |
175 | |
a20d9a3f |
176 | ## |
177 | # Check signature was valid |
178 | ## |
8db25060 |
179 | unless ($signature eq SIG_FILE) { |
3d1b8be9 |
180 | $self->close_fh( $obj ); |
e5fc7e69 |
181 | $obj->_throw_error("Signature not found -- file is not a Deep DB"); |
a20d9a3f |
182 | } |
183 | |
a20d9a3f |
184 | ## |
185 | # Get our type from master index signature |
186 | ## |
e5fc7e69 |
187 | my $tag = $self->load_tag($obj, $obj->_base_offset) |
188 | or $obj->_throw_error("Corrupted file, no master index record"); |
d0b74c17 |
189 | |
e5fc7e69 |
190 | unless ($obj->{type} eq $tag->{signature}) { |
191 | $obj->_throw_error("File type mismatch"); |
a20d9a3f |
192 | } |
20f7b20c |
193 | |
d0b74c17 |
194 | #XXX We probably also want to store the hash algorithm name and not assume anything |
195 | #XXX The cool thing would be to allow a different hashing algorithm at every level |
196 | |
a20d9a3f |
197 | return 1; |
198 | } |
199 | |
3d1b8be9 |
200 | sub close_fh { |
cd59cad8 |
201 | my $self = shift; |
a21f2d90 |
202 | my ($obj) = @_; |
cd59cad8 |
203 | |
204 | if ( my $fh = $obj->_root->{fh} ) { |
205 | close $fh; |
206 | } |
207 | $obj->_root->{fh} = undef; |
208 | |
209 | return 1; |
210 | } |
211 | |
d4b1166e |
212 | sub create_tag { |
20f7b20c |
213 | ## |
214 | # Given offset, signature and content, create tag and write to disk |
215 | ## |
d4b1166e |
216 | my $self = shift; |
20f7b20c |
217 | my ($obj, $offset, $sig, $content) = @_; |
218 | my $size = length($content); |
219 | |
d4b1166e |
220 | my $fh = $obj->_fh; |
221 | |
20f7b20c |
222 | seek($fh, $offset + $obj->_root->{file_offset}, SEEK_SET); |
251dfd0e |
223 | print( $fh $sig . pack($self->{data_pack}, $size) . $content ); |
20f7b20c |
224 | |
225 | if ($offset == $obj->_root->{end}) { |
8db25060 |
226 | $obj->_root->{end} += SIG_SIZE + $self->{data_size} + $size; |
20f7b20c |
227 | } |
228 | |
229 | return { |
230 | signature => $sig, |
231 | size => $size, |
8db25060 |
232 | offset => $offset + SIG_SIZE + $self->{data_size}, |
20f7b20c |
233 | content => $content |
234 | }; |
d4b1166e |
235 | } |
236 | |
237 | sub load_tag { |
20f7b20c |
238 | ## |
239 | # Given offset, load single tag and return signature, size and data |
240 | ## |
d4b1166e |
241 | my $self = shift; |
20f7b20c |
242 | my ($obj, $offset) = @_; |
243 | |
d4b1166e |
244 | my $fh = $obj->_fh; |
245 | |
20f7b20c |
246 | seek($fh, $offset + $obj->_root->{file_offset}, SEEK_SET); |
e5fc7e69 |
247 | |
75be6413 |
248 | #XXX I'm not sure this check will work if autoflush isn't enabled ... |
e5fc7e69 |
249 | return if eof $fh; |
20f7b20c |
250 | |
d4b1166e |
251 | my $b; |
8db25060 |
252 | read( $fh, $b, SIG_SIZE + $self->{data_size} ); |
251dfd0e |
253 | my ($sig, $size) = unpack( "A $self->{data_pack}", $b ); |
20f7b20c |
254 | |
255 | my $buffer; |
256 | read( $fh, $buffer, $size); |
257 | |
258 | return { |
259 | signature => $sig, |
260 | size => $size, |
8db25060 |
261 | offset => $offset + SIG_SIZE + $self->{data_size}, |
20f7b20c |
262 | content => $buffer |
263 | }; |
d4b1166e |
264 | } |
265 | |
20f7b20c |
266 | sub add_bucket { |
267 | ## |
268 | # Adds one key/value pair to bucket list, given offset, MD5 digest of key, |
269 | # plain (undigested) key and value. |
270 | ## |
d4b1166e |
271 | my $self = shift; |
20f7b20c |
272 | my ($obj, $tag, $md5, $plain_key, $value) = @_; |
75be6413 |
273 | |
20f7b20c |
274 | my $keys = $tag->{content}; |
275 | my $location = 0; |
276 | my $result = 2; |
277 | |
278 | my $root = $obj->_root; |
279 | |
280 | my $is_dbm_deep = eval { local $SIG{'__DIE__'}; $value->isa( 'DBM::Deep' ) }; |
281 | my $internal_ref = $is_dbm_deep && ($value->_root eq $root); |
282 | |
283 | my $fh = $obj->_fh; |
284 | |
285 | ## |
286 | # Iterate through buckets, seeing if this is a new entry or a replace. |
287 | ## |
75be6413 |
288 | BUCKET: |
d0b74c17 |
289 | for (my $i = 0; $i < $self->{max_buckets}; $i++) { |
9cec1360 |
290 | my ($key, $subloc) = $self->_get_key_subloc( $keys, $i ); |
75be6413 |
291 | |
20f7b20c |
292 | if (!$subloc) { |
293 | ## |
294 | # Found empty bucket (end of list). Populate and exit loop. |
295 | ## |
296 | $result = 2; |
297 | |
8db25060 |
298 | $location = $root->{end}; |
20f7b20c |
299 | |
75be6413 |
300 | seek( |
301 | $fh, |
302 | $tag->{offset} + ($i * $self->{bucket_size}) + $root->{file_offset}, |
303 | SEEK_SET, |
304 | ); |
305 | |
251dfd0e |
306 | print( $fh $md5 . pack($self->{long_pack}, $location) ); |
20f7b20c |
307 | last; |
308 | } |
309 | |
75be6413 |
310 | if ( $md5 ne $key ) { |
311 | next BUCKET; |
312 | } |
20f7b20c |
313 | |
75be6413 |
314 | ## |
315 | # Found existing bucket with same key. Replace with new value. |
316 | ## |
317 | $result = 1; |
20f7b20c |
318 | |
8db25060 |
319 | seek($fh, $subloc + SIG_SIZE + $root->{file_offset}, SEEK_SET); |
75be6413 |
320 | my $size; |
321 | read( $fh, $size, $self->{data_size}); |
322 | $size = unpack($self->{data_pack}, $size); |
20f7b20c |
323 | |
75be6413 |
324 | ## |
325 | # If value is a hash, array, or raw value with equal or less size, we can |
326 | # reuse the same content area of the database. Otherwise, we have to create |
327 | # a new content area at the EOF. |
328 | ## |
329 | my $actual_length; |
8db25060 |
330 | if ( $internal_ref ) { |
331 | $actual_length = $self->{long_size}; |
332 | } |
333 | else { |
334 | my $r = Scalar::Util::reftype( $value ) || ''; |
335 | if ( $r eq 'HASH' || $r eq 'ARRAY' ) { |
336 | $actual_length = $self->{index_size}; |
337 | |
338 | # if autobless is enabled, must also take into consideration |
339 | # the class name, as it is stored along with key/value. |
340 | if ( $root->{autobless} ) { |
341 | my $value_class = Scalar::Util::blessed($value); |
342 | if ( defined $value_class && !$value->isa('DBM::Deep') ) { |
343 | $actual_length += length($value_class); |
344 | } |
75be6413 |
345 | } |
20f7b20c |
346 | } |
8db25060 |
347 | else { $actual_length = length($value); } |
75be6413 |
348 | } |
20f7b20c |
349 | |
75be6413 |
350 | if ($actual_length <= $size) { |
351 | $location = $subloc; |
20f7b20c |
352 | } |
75be6413 |
353 | else { |
354 | $location = $root->{end}; |
8db25060 |
355 | seek( |
356 | $fh, |
357 | $tag->{offset} + ($i * $self->{bucket_size}) + $self->{hash_size} + $root->{file_offset}, |
358 | SEEK_SET, |
359 | ); |
75be6413 |
360 | print( $fh pack($self->{long_pack}, $location) ); |
361 | } |
362 | |
363 | last; |
20f7b20c |
364 | } |
365 | |
366 | ## |
20f7b20c |
367 | # If bucket didn't fit into list, split into a new index level |
368 | ## |
369 | if (!$location) { |
75be6413 |
370 | $self->split_index( $obj, $md5, $tag ); |
20f7b20c |
371 | |
75be6413 |
372 | $location = $root->{end}; |
373 | } |
20f7b20c |
374 | |
375 | ## |
376 | # Seek to content area and store signature, value and plaintext key |
377 | ## |
378 | if ($location) { |
20f7b20c |
379 | seek($fh, $location + $root->{file_offset}, SEEK_SET); |
380 | |
381 | ## |
8db25060 |
382 | # Write signature based on content type, set content length and write |
383 | # actual value. |
20f7b20c |
384 | ## |
385 | my $r = Scalar::Util::reftype($value) || ''; |
8db25060 |
386 | my $content_length; |
387 | if ( $internal_ref ) { |
388 | print( $fh SIG_INTERNAL ); |
389 | print( $fh pack($self->{data_pack}, $self->{long_size}) ); |
390 | print( $fh pack($self->{long_pack}, $value->_base_offset) ); |
391 | $content_length = $self->{long_size}; |
20f7b20c |
392 | } |
393 | else { |
8db25060 |
394 | if ($r eq 'HASH') { |
395 | print( $fh SIG_HASH ); |
396 | print( $fh pack($self->{data_pack}, $self->{index_size}) . chr(0) x $self->{index_size} ); |
397 | $content_length = $self->{index_size}; |
398 | } |
399 | elsif ($r eq 'ARRAY') { |
400 | print( $fh SIG_ARRAY ); |
401 | print( $fh pack($self->{data_pack}, $self->{index_size}) . chr(0) x $self->{index_size} ); |
402 | $content_length = $self->{index_size}; |
403 | } |
404 | elsif (!defined($value)) { |
405 | print( $fh SIG_NULL ); |
406 | print( $fh pack($self->{data_pack}, 0) ); |
407 | $content_length = 0; |
408 | } |
409 | else { |
410 | print( $fh SIG_DATA ); |
411 | print( $fh pack($self->{data_pack}, length($value)) . $value ); |
412 | $content_length = length($value); |
413 | } |
20f7b20c |
414 | } |
415 | |
416 | ## |
417 | # Plain key is stored AFTER value, as keys are typically fetched less often. |
418 | ## |
251dfd0e |
419 | print( $fh pack($self->{data_pack}, length($plain_key)) . $plain_key ); |
20f7b20c |
420 | |
421 | ## |
422 | # If value is blessed, preserve class name |
423 | ## |
424 | if ( $root->{autobless} ) { |
425 | my $value_class = Scalar::Util::blessed($value); |
8db25060 |
426 | if ( defined $value_class && !$value->isa( 'DBM::Deep' ) ) { |
20f7b20c |
427 | ## |
428 | # Blessed ref -- will restore later |
429 | ## |
430 | print( $fh chr(1) ); |
251dfd0e |
431 | print( $fh pack($self->{data_pack}, length($value_class)) . $value_class ); |
20f7b20c |
432 | $content_length += 1; |
251dfd0e |
433 | $content_length += $self->{data_size} + length($value_class); |
20f7b20c |
434 | } |
435 | else { |
436 | print( $fh chr(0) ); |
437 | $content_length += 1; |
438 | } |
439 | } |
440 | |
441 | ## |
442 | # If this is a new content area, advance EOF counter |
443 | ## |
444 | if ($location == $root->{end}) { |
8db25060 |
445 | $root->{end} += SIG_SIZE; |
251dfd0e |
446 | $root->{end} += $self->{data_size} + $content_length; |
447 | $root->{end} += $self->{data_size} + length($plain_key); |
20f7b20c |
448 | } |
449 | |
450 | ## |
451 | # If content is a hash or array, create new child DBM::Deep object and |
452 | # pass each key or element to it. |
453 | ## |
8db25060 |
454 | if ( ! $internal_ref ) { |
455 | if ($r eq 'HASH') { |
456 | my $branch = DBM::Deep->new( |
457 | type => DBM::Deep->TYPE_HASH, |
458 | base_offset => $location, |
459 | root => $root, |
460 | ); |
461 | foreach my $key (keys %{$value}) { |
462 | $branch->STORE( $key, $value->{$key} ); |
463 | } |
20f7b20c |
464 | } |
8db25060 |
465 | elsif ($r eq 'ARRAY') { |
466 | my $branch = DBM::Deep->new( |
467 | type => DBM::Deep->TYPE_ARRAY, |
468 | base_offset => $location, |
469 | root => $root, |
470 | ); |
471 | my $index = 0; |
472 | foreach my $element (@{$value}) { |
473 | $branch->STORE( $index, $element ); |
474 | $index++; |
475 | } |
20f7b20c |
476 | } |
477 | } |
478 | |
479 | return $result; |
480 | } |
d4b1166e |
481 | |
e5fc7e69 |
482 | $obj->_throw_error("Fatal error: indexing failed -- possibly due to corruption in file"); |
d4b1166e |
483 | } |
484 | |
75be6413 |
485 | sub split_index { |
486 | my $self = shift; |
487 | my ($obj, $md5, $tag) = @_; |
488 | |
489 | my $fh = $obj->_fh; |
490 | my $root = $obj->_root; |
491 | my $keys = $tag->{content}; |
492 | |
493 | seek($fh, $tag->{ref_loc} + $root->{file_offset}, SEEK_SET); |
494 | print( $fh pack($self->{long_pack}, $root->{end}) ); |
495 | |
496 | my $index_tag = $self->create_tag( |
497 | $obj, |
498 | $root->{end}, |
8db25060 |
499 | SIG_INDEX, |
75be6413 |
500 | chr(0) x $self->{index_size}, |
501 | ); |
502 | |
503 | my @offsets = (); |
504 | |
505 | $keys .= $md5 . pack($self->{long_pack}, 0); |
506 | |
507 | BUCKET: |
508 | for (my $i = 0; $i <= $self->{max_buckets}; $i++) { |
9cec1360 |
509 | my ($key, $old_subloc) = $self->_get_key_subloc( $keys, $i ); |
75be6413 |
510 | |
511 | next BUCKET unless $key; |
512 | |
75be6413 |
513 | my $num = ord(substr($key, $tag->{ch} + 1, 1)); |
514 | |
515 | if ($offsets[$num]) { |
8db25060 |
516 | my $offset = $offsets[$num] + SIG_SIZE + $self->{data_size}; |
75be6413 |
517 | seek($fh, $offset + $root->{file_offset}, SEEK_SET); |
518 | my $subkeys; |
519 | read( $fh, $subkeys, $self->{bucket_list_size}); |
520 | |
521 | for (my $k=0; $k<$self->{max_buckets}; $k++) { |
9cec1360 |
522 | my ($temp, $subloc) = $self->_get_key_subloc( $subkeys, $k ); |
75be6413 |
523 | |
524 | if (!$subloc) { |
525 | seek($fh, $offset + ($k * $self->{bucket_size}) + $root->{file_offset}, SEEK_SET); |
526 | print( $fh $key . pack($self->{long_pack}, $old_subloc || $root->{end}) ); |
527 | last; |
528 | } |
529 | } # k loop |
530 | } |
531 | else { |
532 | $offsets[$num] = $root->{end}; |
533 | seek($fh, $index_tag->{offset} + ($num * $self->{long_size}) + $root->{file_offset}, SEEK_SET); |
534 | print( $fh pack($self->{long_pack}, $root->{end}) ); |
535 | |
8db25060 |
536 | my $blist_tag = $self->create_tag($obj, $root->{end}, SIG_BLIST, chr(0) x $self->{bucket_list_size}); |
75be6413 |
537 | |
538 | seek($fh, $blist_tag->{offset} + $root->{file_offset}, SEEK_SET); |
539 | print( $fh $key . pack($self->{long_pack}, $old_subloc || $root->{end}) ); |
540 | } |
541 | } # i loop |
542 | |
543 | return; |
544 | } |
545 | |
8db25060 |
546 | sub read_from_loc { |
547 | my $self = shift; |
548 | my ($obj, $subloc) = @_; |
549 | |
550 | my $fh = $obj->_fh; |
551 | |
552 | ## |
553 | # Found match -- seek to offset and read signature |
554 | ## |
555 | my $signature; |
556 | seek($fh, $subloc + $obj->_root->{file_offset}, SEEK_SET); |
557 | read( $fh, $signature, SIG_SIZE); |
558 | |
559 | ## |
560 | # If value is a hash or array, return new DBM::Deep object with correct offset |
561 | ## |
562 | if (($signature eq SIG_HASH) || ($signature eq SIG_ARRAY)) { |
563 | my $obj = DBM::Deep->new( |
564 | type => $signature, |
565 | base_offset => $subloc, |
566 | root => $obj->_root, |
567 | ); |
568 | |
569 | if ($obj->_root->{autobless}) { |
570 | ## |
571 | # Skip over value and plain key to see if object needs |
572 | # to be re-blessed |
573 | ## |
574 | seek($fh, $self->{data_size} + $self->{index_size}, SEEK_CUR); |
575 | |
576 | my $size; |
577 | read( $fh, $size, $self->{data_size}); $size = unpack($self->{data_pack}, $size); |
578 | if ($size) { seek($fh, $size, SEEK_CUR); } |
579 | |
580 | my $bless_bit; |
581 | read( $fh, $bless_bit, 1); |
582 | if (ord($bless_bit)) { |
583 | ## |
584 | # Yes, object needs to be re-blessed |
585 | ## |
586 | my $class_name; |
587 | read( $fh, $size, $self->{data_size}); $size = unpack($self->{data_pack}, $size); |
588 | if ($size) { read( $fh, $class_name, $size); } |
589 | if ($class_name) { $obj = bless( $obj, $class_name ); } |
590 | } |
591 | } |
592 | |
593 | return $obj; |
594 | } |
595 | elsif ( $signature eq SIG_INTERNAL ) { |
596 | my $size; |
597 | read( $fh, $size, $self->{data_size}); |
598 | $size = unpack($self->{data_pack}, $size); |
599 | |
600 | if ( $size ) { |
601 | my $new_loc; |
602 | read( $fh, $new_loc, $size ); |
603 | $new_loc = unpack( $self->{long_pack}, $new_loc ); |
604 | |
605 | return $self->read_from_loc( $obj, $new_loc ); |
606 | } |
607 | else { |
608 | return; |
609 | } |
610 | } |
611 | ## |
612 | # Otherwise return actual value |
613 | ## |
614 | elsif ($signature eq SIG_DATA) { |
615 | my $size; |
616 | read( $fh, $size, $self->{data_size}); |
617 | $size = unpack($self->{data_pack}, $size); |
618 | |
619 | my $value = ''; |
620 | if ($size) { read( $fh, $value, $size); } |
621 | return $value; |
622 | } |
623 | |
624 | ## |
625 | # Key exists, but content is null |
626 | ## |
627 | return; |
628 | } |
629 | |
9020ee8c |
630 | sub get_bucket_value { |
beac1dff |
631 | ## |
632 | # Fetch single value given tag and MD5 digested key. |
633 | ## |
634 | my $self = shift; |
635 | my ($obj, $tag, $md5) = @_; |
636 | my $keys = $tag->{content}; |
9020ee8c |
637 | |
638 | my $fh = $obj->_fh; |
639 | |
beac1dff |
640 | ## |
641 | # Iterate through buckets, looking for a key match |
642 | ## |
9020ee8c |
643 | BUCKET: |
75be6413 |
644 | for (my $i = 0; $i < $self->{max_buckets}; $i++) { |
9cec1360 |
645 | my ($key, $subloc) = $self->_get_key_subloc( $keys, $i ); |
9020ee8c |
646 | |
beac1dff |
647 | if (!$subloc) { |
648 | ## |
649 | # Hit end of list, no match |
650 | ## |
651 | return; |
652 | } |
9020ee8c |
653 | |
654 | if ( $md5 ne $key ) { |
655 | next BUCKET; |
656 | } |
657 | |
8db25060 |
658 | return $self->read_from_loc( $obj, $subloc ); |
beac1dff |
659 | } # i loop |
9020ee8c |
660 | |
beac1dff |
661 | return; |
9020ee8c |
662 | } |
ab0e4957 |
663 | |
664 | sub delete_bucket { |
beac1dff |
665 | ## |
666 | # Delete single key/value pair given tag and MD5 digested key. |
667 | ## |
668 | my $self = shift; |
669 | my ($obj, $tag, $md5) = @_; |
670 | my $keys = $tag->{content}; |
ab0e4957 |
671 | |
672 | my $fh = $obj->_fh; |
d0b74c17 |
673 | |
beac1dff |
674 | ## |
675 | # Iterate through buckets, looking for a key match |
676 | ## |
ab0e4957 |
677 | BUCKET: |
beac1dff |
678 | for (my $i=0; $i<$self->{max_buckets}; $i++) { |
9cec1360 |
679 | my ($key, $subloc) = $self->_get_key_subloc( $keys, $i ); |
ab0e4957 |
680 | |
beac1dff |
681 | if (!$subloc) { |
682 | ## |
683 | # Hit end of list, no match |
684 | ## |
685 | return; |
686 | } |
ab0e4957 |
687 | |
688 | if ( $md5 ne $key ) { |
689 | next BUCKET; |
690 | } |
691 | |
692 | ## |
693 | # Matched key -- delete bucket and return |
694 | ## |
251dfd0e |
695 | seek($fh, $tag->{offset} + ($i * $self->{bucket_size}) + $obj->_root->{file_offset}, SEEK_SET); |
696 | print( $fh substr($keys, ($i+1) * $self->{bucket_size} ) ); |
697 | print( $fh chr(0) x $self->{bucket_size} ); |
d0b74c17 |
698 | |
ab0e4957 |
699 | return 1; |
beac1dff |
700 | } # i loop |
ab0e4957 |
701 | |
beac1dff |
702 | return; |
ab0e4957 |
703 | } |
704 | |
912d50b1 |
705 | sub bucket_exists { |
beac1dff |
706 | ## |
707 | # Check existence of single key given tag and MD5 digested key. |
708 | ## |
709 | my $self = shift; |
710 | my ($obj, $tag, $md5) = @_; |
711 | my $keys = $tag->{content}; |
d0b74c17 |
712 | |
beac1dff |
713 | ## |
714 | # Iterate through buckets, looking for a key match |
715 | ## |
912d50b1 |
716 | BUCKET: |
beac1dff |
717 | for (my $i=0; $i<$self->{max_buckets}; $i++) { |
9cec1360 |
718 | my ($key, $subloc) = $self->_get_key_subloc( $keys, $i ); |
912d50b1 |
719 | |
beac1dff |
720 | if (!$subloc) { |
721 | ## |
722 | # Hit end of list, no match |
723 | ## |
724 | return; |
725 | } |
912d50b1 |
726 | |
727 | if ( $md5 ne $key ) { |
728 | next BUCKET; |
729 | } |
730 | |
731 | ## |
732 | # Matched key -- return true |
733 | ## |
734 | return 1; |
beac1dff |
735 | } # i loop |
912d50b1 |
736 | |
beac1dff |
737 | return; |
912d50b1 |
738 | } |
739 | |
6736c116 |
740 | sub find_bucket_list { |
beac1dff |
741 | ## |
742 | # Locate offset for bucket list, given digested key |
743 | ## |
744 | my $self = shift; |
d0b74c17 |
745 | my ($obj, $md5, $args) = @_; |
746 | $args = {} unless $args; |
747 | |
beac1dff |
748 | ## |
749 | # Locate offset for bucket list using digest index system |
750 | ## |
e5fc7e69 |
751 | my $tag = $self->load_tag($obj, $obj->_base_offset) |
752 | or $self->_throw_error( "INTERNAL ERROR - Cannot find tag" ); |
d0b74c17 |
753 | |
e5fc7e69 |
754 | my $ch = 0; |
8db25060 |
755 | while ($tag->{signature} ne SIG_BLIST) { |
d0b74c17 |
756 | my $num = ord substr($md5, $ch, 1); |
757 | |
758 | my $ref_loc = $tag->{offset} + ($num * $self->{long_size}); |
759 | $tag = $self->index_lookup( $obj, $tag, $num ); |
760 | |
761 | if (!$tag) { |
762 | if ( $args->{create} ) { |
763 | my $fh = $obj->_fh; |
764 | seek($fh, $ref_loc + $obj->_root->{file_offset}, SEEK_SET); |
765 | print( $fh pack($self->{long_pack}, $obj->_root->{end}) ); |
766 | |
767 | $tag = $self->create_tag( |
768 | $obj, $obj->_root->{end}, |
8db25060 |
769 | SIG_BLIST, |
d0b74c17 |
770 | chr(0) x $self->{bucket_list_size}, |
771 | ); |
772 | |
773 | $tag->{ref_loc} = $ref_loc; |
774 | $tag->{ch} = $ch; |
775 | |
776 | last; |
777 | } |
778 | else { |
779 | return; |
780 | } |
781 | } |
782 | |
783 | $tag->{ch} = $ch; |
784 | $tag->{ref_loc} = $ref_loc; |
785 | |
beac1dff |
786 | $ch++; |
787 | } |
d0b74c17 |
788 | |
beac1dff |
789 | return $tag; |
6736c116 |
790 | } |
791 | |
d0b74c17 |
792 | sub index_lookup { |
793 | ## |
794 | # Given index tag, lookup single entry in index and return . |
795 | ## |
796 | my $self = shift; |
797 | my ($obj, $tag, $index) = @_; |
798 | |
799 | my $location = unpack( |
800 | $self->{long_pack}, |
801 | substr( |
802 | $tag->{content}, |
803 | $index * $self->{long_size}, |
804 | $self->{long_size}, |
805 | ), |
806 | ); |
807 | |
808 | if (!$location) { return; } |
809 | |
810 | return $self->load_tag( $obj, $location ); |
811 | } |
812 | |
6736c116 |
813 | sub traverse_index { |
beac1dff |
814 | ## |
815 | # Scan index and recursively step into deeper levels, looking for next key. |
816 | ## |
6736c116 |
817 | my $self = shift; |
818 | my ($obj, $offset, $ch, $force_return_next) = @_; |
d0b74c17 |
819 | |
beac1dff |
820 | my $tag = $self->load_tag($obj, $offset ); |
6736c116 |
821 | |
822 | my $fh = $obj->_fh; |
d0b74c17 |
823 | |
8db25060 |
824 | if ($tag->{signature} ne SIG_BLIST) { |
beac1dff |
825 | my $content = $tag->{content}; |
e5fc7e69 |
826 | my $start = $obj->{return_next} ? 0 : ord(substr($obj->{prev_md5}, $ch, 1)); |
d0b74c17 |
827 | |
beac1dff |
828 | for (my $index = $start; $index < 256; $index++) { |
e5fc7e69 |
829 | my $subloc = unpack( |
830 | $self->{long_pack}, |
831 | substr($content, $index * $self->{long_size}, $self->{long_size}), |
832 | ); |
833 | |
beac1dff |
834 | if ($subloc) { |
e5fc7e69 |
835 | my $result = $self->traverse_index( |
836 | $obj, $subloc, $ch + 1, $force_return_next, |
837 | ); |
838 | |
beac1dff |
839 | if (defined($result)) { return $result; } |
840 | } |
841 | } # index loop |
d0b74c17 |
842 | |
beac1dff |
843 | $obj->{return_next} = 1; |
844 | } # tag is an index |
d0b74c17 |
845 | |
e5fc7e69 |
846 | else { |
beac1dff |
847 | my $keys = $tag->{content}; |
848 | if ($force_return_next) { $obj->{return_next} = 1; } |
d0b74c17 |
849 | |
beac1dff |
850 | ## |
851 | # Iterate through buckets, looking for a key match |
852 | ## |
8db25060 |
853 | for (my $i = 0; $i < $self->{max_buckets}; $i++) { |
9cec1360 |
854 | my ($key, $subloc) = $self->_get_key_subloc( $keys, $i ); |
d0b74c17 |
855 | |
8db25060 |
856 | # End of bucket list -- return to outer loop |
beac1dff |
857 | if (!$subloc) { |
beac1dff |
858 | $obj->{return_next} = 1; |
859 | last; |
860 | } |
8db25060 |
861 | # Located previous key -- return next one found |
beac1dff |
862 | elsif ($key eq $obj->{prev_md5}) { |
beac1dff |
863 | $obj->{return_next} = 1; |
864 | next; |
865 | } |
8db25060 |
866 | # Seek to bucket location and skip over signature |
beac1dff |
867 | elsif ($obj->{return_next}) { |
8db25060 |
868 | seek($fh, $subloc + $obj->_root->{file_offset}, SEEK_SET); |
d0b74c17 |
869 | |
beac1dff |
870 | # Skip over value to get to plain key |
8db25060 |
871 | my $sig; |
872 | read( $fh, $sig, SIG_SIZE ); |
873 | |
beac1dff |
874 | my $size; |
e5fc7e69 |
875 | read( $fh, $size, $self->{data_size}); |
876 | $size = unpack($self->{data_pack}, $size); |
beac1dff |
877 | if ($size) { seek($fh, $size, SEEK_CUR); } |
d0b74c17 |
878 | |
beac1dff |
879 | # Read in plain key and return as scalar |
beac1dff |
880 | my $plain_key; |
e5fc7e69 |
881 | read( $fh, $size, $self->{data_size}); |
882 | $size = unpack($self->{data_pack}, $size); |
beac1dff |
883 | if ($size) { read( $fh, $plain_key, $size); } |
d0b74c17 |
884 | |
beac1dff |
885 | return $plain_key; |
886 | } |
8db25060 |
887 | } |
d0b74c17 |
888 | |
beac1dff |
889 | $obj->{return_next} = 1; |
890 | } # tag is a bucket list |
d0b74c17 |
891 | |
beac1dff |
892 | return; |
6736c116 |
893 | } |
894 | |
895 | sub get_next_key { |
beac1dff |
896 | ## |
897 | # Locate next key, given digested previous one |
898 | ## |
6736c116 |
899 | my $self = shift; |
900 | my ($obj) = @_; |
d0b74c17 |
901 | |
beac1dff |
902 | $obj->{prev_md5} = $_[1] ? $_[1] : undef; |
903 | $obj->{return_next} = 0; |
d0b74c17 |
904 | |
beac1dff |
905 | ## |
906 | # If the previous key was not specifed, start at the top and |
907 | # return the first one found. |
908 | ## |
909 | if (!$obj->{prev_md5}) { |
910 | $obj->{prev_md5} = chr(0) x $self->{hash_size}; |
911 | $obj->{return_next} = 1; |
912 | } |
d0b74c17 |
913 | |
beac1dff |
914 | return $self->traverse_index( $obj, $obj->_base_offset, 0 ); |
6736c116 |
915 | } |
916 | |
75be6413 |
917 | # Utilities |
918 | |
9cec1360 |
919 | sub _get_key_subloc { |
75be6413 |
920 | my $self = shift; |
921 | my ($keys, $idx) = @_; |
922 | |
9cec1360 |
923 | my ($key, $subloc) = unpack( |
924 | "a$self->{hash_size} $self->{long_pack}", |
75be6413 |
925 | substr( |
926 | $keys, |
9cec1360 |
927 | ($idx * $self->{bucket_size}), |
928 | $self->{bucket_size}, |
75be6413 |
929 | ), |
930 | ); |
931 | |
9cec1360 |
932 | return ($key, $subloc); |
75be6413 |
933 | } |
934 | |
a20d9a3f |
935 | 1; |
936 | __END__ |