r592@rob-kinyons-computer-2 (orig r10555): rkinyon | 2008-01-15 14:19:42 -0500
[dbsrgits/DBM-Deep.git] / lib / DBM / Deep.pm
CommitLineData
ffed8b01 1package DBM::Deep;
2
2120a181 3use 5.006_000;
460b1067 4
ffed8b01 5use strict;
460b1067 6use warnings;
8b957036 7
45f047f8 8our $VERSION = q(1.0008);
86867f3a 9
2120a181 10use Fcntl qw( :flock );
12b96196 11
ffed8b01 12use Digest::MD5 ();
a8fdabda 13use FileHandle::Fmode ();
ffed8b01 14use Scalar::Util ();
ffed8b01 15
2120a181 16use DBM::Deep::Engine;
460b1067 17use DBM::Deep::File;
95967a5e 18
c57b19c6 19use overload
20 '""' => sub { overload::StrVal( $_[0] ) },
21 fallback => 1;
22
ffed8b01 23##
24# Setup constants for users to pass to new()
25##
2120a181 26sub TYPE_HASH () { DBM::Deep::Engine->SIG_HASH }
27sub TYPE_ARRAY () { DBM::Deep::Engine->SIG_ARRAY }
ffed8b01 28
2120a181 29# This is used in all the children of this class in their TIE<type> methods.
0ca7ea98 30sub _get_args {
31 my $proto = shift;
32
33 my $args;
34 if (scalar(@_) > 1) {
35 if ( @_ % 2 ) {
36 $proto->_throw_error( "Odd number of parameters to " . (caller(1))[2] );
37 }
38 $args = {@_};
39 }
d0b74c17 40 elsif ( ref $_[0] ) {
4d35d856 41 unless ( eval { local $SIG{'__DIE__'}; %{$_[0]} || 1 } ) {
0ca7ea98 42 $proto->_throw_error( "Not a hashref in args to " . (caller(1))[2] );
43 }
44 $args = $_[0];
45 }
d0b74c17 46 else {
0ca7ea98 47 $args = { file => shift };
48 }
49
50 return $args;
51}
52
ffed8b01 53sub new {
d0b74c17 54 ##
55 # Class constructor method for Perl OO interface.
56 # Calls tie() and returns blessed reference to tied hash or array,
57 # providing a hybrid OO/tie interface.
58 ##
59 my $class = shift;
60 my $args = $class->_get_args( @_ );
61
62 ##
63 # Check if we want a tied hash or array.
64 ##
65 my $self;
66 if (defined($args->{type}) && $args->{type} eq TYPE_ARRAY) {
6fe26b29 67 $class = 'DBM::Deep::Array';
68 require DBM::Deep::Array;
d0b74c17 69 tie @$self, $class, %$args;
70 }
71 else {
6fe26b29 72 $class = 'DBM::Deep::Hash';
73 require DBM::Deep::Hash;
d0b74c17 74 tie %$self, $class, %$args;
75 }
ffed8b01 76
d0b74c17 77 return bless $self, $class;
ffed8b01 78}
79
96041a25 80# This initializer is called from the various TIE* methods. new() calls tie(),
81# which allows for a single point of entry.
0795f290 82sub _init {
0795f290 83 my $class = shift;
994ccd8e 84 my ($args) = @_;
0795f290 85
83371fe3 86 $args->{storage} = DBM::Deep::File->new( $args )
87 unless exists $args->{storage};
460b1067 88
89 # locking implicitly enables autoflush
90 if ($args->{locking}) { $args->{autoflush} = 1; }
91
0795f290 92 # These are the defaults to be optionally overridden below
93 my $self = bless {
95967a5e 94 type => TYPE_HASH,
e06824f8 95 base_offset => undef,
2120a181 96 staleness => undef,
359a01ac 97
83371fe3 98 storage => undef,
2120a181 99 engine => undef,
0795f290 100 }, $class;
2120a181 101
102 $args->{engine} = DBM::Deep::Engine->new( { %{$args}, obj => $self } )
103 unless exists $args->{engine};
8db25060 104
fde3db1a 105 # Grab the parameters we want to use
0795f290 106 foreach my $param ( keys %$self ) {
107 next unless exists $args->{$param};
3e9498a1 108 $self->{$param} = $args->{$param};
ffed8b01 109 }
d0b74c17 110
2120a181 111 eval {
112 local $SIG{'__DIE__'};
0795f290 113
2120a181 114 $self->lock;
115 $self->_engine->setup_fh( $self );
116 $self->_storage->set_inode;
117 $self->unlock;
118 }; if ( $@ ) {
119 my $e = $@;
120 eval { local $SIG{'__DIE__'}; $self->unlock; };
121 die $e;
122 }
359a01ac 123
0795f290 124 return $self;
ffed8b01 125}
126
ffed8b01 127sub TIEHASH {
6fe26b29 128 shift;
129 require DBM::Deep::Hash;
130 return DBM::Deep::Hash->TIEHASH( @_ );
ffed8b01 131}
132
133sub TIEARRAY {
6fe26b29 134 shift;
135 require DBM::Deep::Array;
136 return DBM::Deep::Array->TIEARRAY( @_ );
ffed8b01 137}
138
ffed8b01 139sub lock {
994ccd8e 140 my $self = shift->_get_self;
83371fe3 141 return $self->_storage->lock( $self, @_ );
ffed8b01 142}
143
144sub unlock {
994ccd8e 145 my $self = shift->_get_self;
83371fe3 146 return $self->_storage->unlock( $self, @_ );
ffed8b01 147}
148
906c8e01 149sub _copy_value {
150 my $self = shift->_get_self;
151 my ($spot, $value) = @_;
152
153 if ( !ref $value ) {
154 ${$spot} = $value;
155 }
156 elsif ( eval { local $SIG{__DIE__}; $value->isa( 'DBM::Deep' ) } ) {
f9c33187 157 ${$spot} = $value->_repr;
906c8e01 158 $value->_copy_node( ${$spot} );
159 }
160 else {
161 my $r = Scalar::Util::reftype( $value );
162 my $c = Scalar::Util::blessed( $value );
163 if ( $r eq 'ARRAY' ) {
164 ${$spot} = [ @{$value} ];
165 }
166 else {
167 ${$spot} = { %{$value} };
168 }
95bbd935 169 ${$spot} = bless ${$spot}, $c
906c8e01 170 if defined $c;
171 }
172
173 return 1;
174}
175
2120a181 176#sub _copy_node {
177# die "Must be implemented in a child class\n";
178#}
179#
180#sub _repr {
181# die "Must be implemented in a child class\n";
182#}
ffed8b01 183
184sub export {
d0b74c17 185 ##
186 # Recursively export into standard Perl hashes and arrays.
187 ##
994ccd8e 188 my $self = shift->_get_self;
d0b74c17 189
f9c33187 190 my $temp = $self->_repr;
d0b74c17 191
192 $self->lock();
193 $self->_copy_node( $temp );
194 $self->unlock();
195
2120a181 196 my $classname = $self->_engine->get_classname( $self );
197 if ( defined $classname ) {
198 bless $temp, $classname;
68f943b3 199 }
200
d0b74c17 201 return $temp;
ffed8b01 202}
203
e00d0eb3 204sub _check_legality {
205 my $self = shift;
206 my ($val) = @_;
207
208 my $r = Scalar::Util::reftype( $val );
209
210 return $r if !defined $r || '' eq $r;
211 return $r if 'HASH' eq $r;
212 return $r if 'ARRAY' eq $r;
213
214 DBM::Deep->_throw_error(
215 "Storage of references of type '$r' is not supported."
216 );
217}
218
ffed8b01 219sub import {
e00d0eb3 220 # Perl calls import() on use -- ignore
221 return if !ref $_[0];
d0b74c17 222
994ccd8e 223 my $self = shift->_get_self;
224 my ($struct) = @_;
d0b74c17 225
e00d0eb3 226 my $type = $self->_check_legality( $struct );
227 if ( !$type ) {
228 DBM::Deep->_throw_error( "Cannot import a scalar" );
d0b74c17 229 }
230
e00d0eb3 231 if ( substr( $type, 0, 1 ) ne $self->_type ) {
232 DBM::Deep->_throw_error(
233 "Cannot import " . ('HASH' eq $type ? 'a hash' : 'an array')
234 . " into " . ('HASH' eq $type ? 'an array' : 'a hash')
235 );
7a960a12 236 }
237
e00d0eb3 238 my %seen;
239 my $recurse;
240 $recurse = sub {
241 my ($db, $val) = @_;
242
243 my $obj = 'HASH' eq Scalar::Util::reftype( $db ) ? tied(%$db) : tied(@$db);
244 $obj ||= $db;
245
246 my $r = $self->_check_legality( $val );
247 if ( 'HASH' eq $r ) {
248 while ( my ($k, $v) = each %$val ) {
249 my $r = $self->_check_legality( $v );
250 if ( $r ) {
251 my $temp = 'HASH' eq $r ? {} : [];
252 if ( my $c = Scalar::Util::blessed( $v ) ) {
253 bless $temp, $c;
254 }
255 $obj->put( $k, $temp );
256 $recurse->( $temp, $v );
257 }
258 else {
259 $obj->put( $k, $v );
260 }
261 }
262 }
263 elsif ( 'ARRAY' eq $r ) {
264 foreach my $k ( 0 .. $#$val ) {
265 my $v = $val->[$k];
266 my $r = $self->_check_legality( $v );
267 if ( $r ) {
268 my $temp = 'HASH' eq $r ? {} : [];
269 if ( my $c = Scalar::Util::blessed( $v ) ) {
270 bless $temp, $c;
271 }
272 $obj->put( $k, $temp );
273 $recurse->( $temp, $v );
274 }
275 else {
276 $obj->put( $k, $v );
277 }
278 }
279 }
280 };
281 $recurse->( $self, $struct );
282
7a960a12 283 return 1;
ffed8b01 284}
285
13ff93d5 286#XXX Need to keep track of who has a fh to this file in order to
287#XXX close them all prior to optimize on Win32/cygwin
ffed8b01 288sub optimize {
d0b74c17 289 ##
290 # Rebuild entire database into new file, then move
291 # it back on top of original.
292 ##
994ccd8e 293 my $self = shift->_get_self;
cc4bef86 294
295#XXX Need to create a new test for this
83371fe3 296# if ($self->_storage->{links} > 1) {
1400a48e 297# $self->_throw_error("Cannot optimize: reference count is greater than 1");
d0b74c17 298# }
299
7a960a12 300 #XXX Do we have to lock the tempfile?
301
e00d0eb3 302 #XXX Should we use tempfile() here instead of a hard-coded name?
d0b74c17 303 my $db_temp = DBM::Deep->new(
83371fe3 304 file => $self->_storage->{file} . '.tmp',
2120a181 305 type => $self->_type,
306
307 # Bring over all the parameters that we need to bring over
888453b9 308 ( map { $_ => $self->_engine->$_ } qw(
309 byte_size max_buckets data_sector_size num_txns
310 )),
d0b74c17 311 );
d0b74c17 312
313 $self->lock();
c57b19c6 314 $self->_engine->clear_cache;
d0b74c17 315 $self->_copy_node( $db_temp );
316 undef $db_temp;
317
318 ##
319 # Attempt to copy user, group and permissions over to new file
320 ##
321 my @stats = stat($self->_fh);
322 my $perms = $stats[2] & 07777;
323 my $uid = $stats[4];
324 my $gid = $stats[5];
83371fe3 325 chown( $uid, $gid, $self->_storage->{file} . '.tmp' );
326 chmod( $perms, $self->_storage->{file} . '.tmp' );
d0b74c17 327
ffed8b01 328 # q.v. perlport for more information on this variable
90f93b43 329 if ( $^O eq 'MSWin32' || $^O eq 'cygwin' ) {
d0b74c17 330 ##
331 # Potential race condition when optmizing on Win32 with locking.
332 # The Windows filesystem requires that the filehandle be closed
333 # before it is overwritten with rename(). This could be redone
334 # with a soft copy.
335 ##
336 $self->unlock();
83371fe3 337 $self->_storage->close;
d0b74c17 338 }
339
83371fe3 340 if (!rename $self->_storage->{file} . '.tmp', $self->_storage->{file}) {
341 unlink $self->_storage->{file} . '.tmp';
d0b74c17 342 $self->unlock();
1400a48e 343 $self->_throw_error("Optimize failed: Cannot copy temp file over original: $!");
d0b74c17 344 }
345
346 $self->unlock();
83371fe3 347 $self->_storage->close;
2120a181 348
83371fe3 349 $self->_storage->open;
2120a181 350 $self->lock();
72e315ac 351 $self->_engine->setup_fh( $self );
2120a181 352 $self->unlock();
70b55428 353
d0b74c17 354 return 1;
ffed8b01 355}
356
357sub clone {
d0b74c17 358 ##
359 # Make copy of object and return
360 ##
994ccd8e 361 my $self = shift->_get_self;
d0b74c17 362
363 return DBM::Deep->new(
c3aafc14 364 type => $self->_type,
d0b74c17 365 base_offset => $self->_base_offset,
2120a181 366 staleness => $self->_staleness,
83371fe3 367 storage => $self->_storage,
2120a181 368 engine => $self->_engine,
d0b74c17 369 );
ffed8b01 370}
371
2120a181 372#XXX Migrate this to the engine, where it really belongs and go through some
373# API - stop poking in the innards of someone else..
ffed8b01 374{
375 my %is_legal_filter = map {
376 $_ => ~~1,
377 } qw(
378 store_key store_value
379 fetch_key fetch_value
380 );
381
382 sub set_filter {
994ccd8e 383 my $self = shift->_get_self;
384 my $type = lc shift;
385 my $func = shift;
d0b74c17 386
ffed8b01 387 if ( $is_legal_filter{$type} ) {
83371fe3 388 $self->_storage->{"filter_$type"} = $func;
ffed8b01 389 return 1;
390 }
391
392 return;
393 }
888453b9 394
395 sub filter_store_key { $_[0]->set_filter( store_key => $_[1] ); }
396 sub filter_store_value { $_[0]->set_filter( store_value => $_[1] ); }
397 sub filter_fetch_key { $_[0]->set_filter( fetch_key => $_[1] ); }
398 sub filter_fetch_value { $_[0]->set_filter( fetch_value => $_[1] ); }
ffed8b01 399}
400
fee0243f 401sub begin_work {
402 my $self = shift->_get_self;
2120a181 403 return $self->_engine->begin_work( $self, @_ );
fee0243f 404}
405
406sub rollback {
407 my $self = shift->_get_self;
2120a181 408 return $self->_engine->rollback( $self, @_ );
fee0243f 409}
410
359a01ac 411sub commit {
412 my $self = shift->_get_self;
2120a181 413 return $self->_engine->commit( $self, @_ );
359a01ac 414}
fee0243f 415
ffed8b01 416##
417# Accessor methods
418##
419
72e315ac 420sub _engine {
421 my $self = $_[0]->_get_self;
422 return $self->{engine};
423}
424
83371fe3 425sub _storage {
2ac02042 426 my $self = $_[0]->_get_self;
83371fe3 427 return $self->{storage};
ffed8b01 428}
429
4d35d856 430sub _type {
2ac02042 431 my $self = $_[0]->_get_self;
d0b74c17 432 return $self->{type};
ffed8b01 433}
434
4d35d856 435sub _base_offset {
2ac02042 436 my $self = $_[0]->_get_self;
d0b74c17 437 return $self->{base_offset};
ffed8b01 438}
439
2120a181 440sub _staleness {
441 my $self = $_[0]->_get_self;
442 return $self->{staleness};
443}
444
994ccd8e 445sub _fh {
994ccd8e 446 my $self = $_[0]->_get_self;
83371fe3 447 return $self->_storage->{fh};
994ccd8e 448}
449
ffed8b01 450##
451# Utility methods
452##
453
261d1296 454sub _throw_error {
807f63a7 455 my $n = 0;
456 while( 1 ) {
457 my @caller = caller( ++$n );
458 next if $caller[0] =~ m/^DBM::Deep/;
459
460 die "DBM::Deep: $_[1] at $0 line $caller[2]\n";
807f63a7 461 }
ffed8b01 462}
463
ffed8b01 464sub STORE {
d0b74c17 465 ##
466 # Store single hash key/value or array element in database.
467 ##
468 my $self = shift->_get_self;
2120a181 469 my ($key, $value) = @_;
81d3d316 470
a8fdabda 471 if ( !FileHandle::Fmode::is_W( $self->_fh ) ) {
acd4faf2 472 $self->_throw_error( 'Cannot write to a readonly filehandle' );
473 }
d0b74c17 474
475 ##
476 # Request exclusive lock for writing
477 ##
478 $self->lock( LOCK_EX );
479
0cb639bd 480 # User may be storing a complex value, in which case we do not want it run
481 # through the filtering system.
83371fe3 482 if ( !ref($value) && $self->_storage->{filter_store_value} ) {
483 $value = $self->_storage->{filter_store_value}->( $value );
d0b74c17 484 }
485
2120a181 486 $self->_engine->write_value( $self, $key, $value);
d0b74c17 487
488 $self->unlock();
489
86867f3a 490 return 1;
ffed8b01 491}
492
493sub FETCH {
d0b74c17 494 ##
495 # Fetch single value or element given plain key or array index
496 ##
cb79ec85 497 my $self = shift->_get_self;
2120a181 498 my ($key) = @_;
ffed8b01 499
d0b74c17 500 ##
501 # Request shared lock for reading
502 ##
503 $self->lock( LOCK_SH );
504
2120a181 505 my $result = $self->_engine->read_value( $self, $key);
d0b74c17 506
507 $self->unlock();
508
a86430bd 509 # Filters only apply to scalar values, so the ref check is making
510 # sure the fetched bucket is a scalar, not a child hash or array.
83371fe3 511 return ($result && !ref($result) && $self->_storage->{filter_fetch_value})
512 ? $self->_storage->{filter_fetch_value}->($result)
cb79ec85 513 : $result;
ffed8b01 514}
515
516sub DELETE {
d0b74c17 517 ##
518 # Delete single key/value pair or element given plain key or array index
519 ##
a97c8f67 520 my $self = shift->_get_self;
2120a181 521 my ($key) = @_;
d0b74c17 522
a8fdabda 523 if ( !FileHandle::Fmode::is_W( $self->_fh ) ) {
a86430bd 524 $self->_throw_error( 'Cannot write to a readonly filehandle' );
525 }
d0b74c17 526
527 ##
528 # Request exclusive lock for writing
529 ##
530 $self->lock( LOCK_EX );
531
d0b74c17 532 ##
533 # Delete bucket
534 ##
2120a181 535 my $value = $self->_engine->delete_key( $self, $key);
a86430bd 536
83371fe3 537 if (defined $value && !ref($value) && $self->_storage->{filter_fetch_value}) {
538 $value = $self->_storage->{filter_fetch_value}->($value);
3b6a5056 539 }
540
d0b74c17 541 $self->unlock();
542
543 return $value;
ffed8b01 544}
545
546sub EXISTS {
d0b74c17 547 ##
548 # Check if a single key or element exists given plain key or array index
549 ##
a97c8f67 550 my $self = shift->_get_self;
551 my ($key) = @_;
d0b74c17 552
d0b74c17 553 ##
554 # Request shared lock for reading
555 ##
556 $self->lock( LOCK_SH );
557
2120a181 558 my $result = $self->_engine->key_exists( $self, $key );
d0b74c17 559
560 $self->unlock();
561
562 return $result;
ffed8b01 563}
564
565sub CLEAR {
d0b74c17 566 ##
567 # Clear all keys from hash, or all elements from array.
568 ##
a97c8f67 569 my $self = shift->_get_self;
ffed8b01 570
a8fdabda 571 if ( !FileHandle::Fmode::is_W( $self->_fh ) ) {
a86430bd 572 $self->_throw_error( 'Cannot write to a readonly filehandle' );
573 }
574
d0b74c17 575 ##
576 # Request exclusive lock for writing
577 ##
578 $self->lock( LOCK_EX );
579
2120a181 580 #XXX Rewrite this dreck to do it in the engine as a tight loop vs.
581 # iterating over keys - such a WASTE - is this required for transactional
582 # clearning?! Surely that can be detected in the engine ...
f9a320bb 583 if ( $self->_type eq TYPE_HASH ) {
584 my $key = $self->first_key;
585 while ( $key ) {
83c43bb5 586 # Retrieve the key before deleting because we depend on next_key
f9a320bb 587 my $next_key = $self->next_key( $key );
2120a181 588 $self->_engine->delete_key( $self, $key, $key );
f9a320bb 589 $key = $next_key;
590 }
591 }
592 else {
593 my $size = $self->FETCHSIZE;
c3aafc14 594 for my $key ( 0 .. $size - 1 ) {
2120a181 595 $self->_engine->delete_key( $self, $key, $key );
f9a320bb 596 }
597 $self->STORESIZE( 0 );
598 }
d0b74c17 599
600 $self->unlock();
601
602 return 1;
ffed8b01 603}
604
ffed8b01 605##
606# Public method aliases
607##
7f441181 608sub put { (shift)->STORE( @_ ) }
609sub store { (shift)->STORE( @_ ) }
610sub get { (shift)->FETCH( @_ ) }
611sub fetch { (shift)->FETCH( @_ ) }
baa27ab6 612sub delete { (shift)->DELETE( @_ ) }
613sub exists { (shift)->EXISTS( @_ ) }
614sub clear { (shift)->CLEAR( @_ ) }
ffed8b01 615
888453b9 616sub _dump_file {shift->_get_self->_engine->_dump_file;}
617
ffed8b01 6181;
ffed8b01 619__END__