/usr/share/perl5/Paranoid/BerkeleyDB.pm is in libparanoid-perl 0.34-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 | # Paranoid::BerkeleyDB -- BerkeleyDB concurrent-access Object
#
# (c) 2005, Arthur Corliss <corliss@digitalmages.com>
#
# $Id: BerkeleyDB.pm,v 0.85 2011/12/08 07:30:26 acorliss Exp $
#
# This software is licensed under the same terms as Perl, itself.
# Please see http://dev.perl.org/licenses/ for more information.
#
#####################################################################
#####################################################################
#
# Environment definitions
#
#####################################################################
package Paranoid::BerkeleyDB;
use strict;
use warnings;
use vars qw($VERSION);
use Paranoid;
use Paranoid::Debug qw(:all);
use Paranoid::Filesystem qw(pmkdir);
use Paranoid::Lockfile;
use BerkeleyDB;
use Carp;
($VERSION) = ( q$Revision: 0.85 $ =~ /(\d+(?:\.(\d+))+)/sm );
use constant DEF_MODE => 0700;
use constant BDB_ERR => -1;
#####################################################################
#
# BerkeleyDB code follows
#
#####################################################################
sub _openEnv ($) {
# Purpose: Opens a shared environment
# Returns: True/False
# Usage: $self->_openEnv;
my $self = shift;
my $rv = 0;
pdebug( 'entering', PDLEVEL2 );
pIn();
if ( defined $$self{DbEnv} ) {
# Environment is already defined
pdebug( 'environment already created', PDLEVEL3 );
$rv = 1;
} else {
# suppress BerlekelDB warnings on older version
no strict 'subs';
# use lockfile to avoid race conditions
plock( "$$self{DbDir}/plock", 'write', $$self{DbMode} );
pdebug( "creating environment in $$self{DbDir}", PDLEVEL3 );
$$self{DbEnv} = BerkeleyDB::Env->new(
'-Home' => $$self{DbDir},
'-Flags' => DB_CREATE | DB_INIT_CDB | DB_INIT_MPOOL |
DB_CDB_ALLDB,
'-Mode' => $$self{DbMode},
);
$rv = defined $$self{DbEnv};
pdebug( "failed to open environment: $BerkeleyDB::Error", PDLEVEL1 )
unless $rv;
# release the lock
punlock("$$self{DbDir}/plock");
}
pOut();
pdebug( "leaving w/rv: $rv", PDLEVEL2 );
return $rv;
}
sub _openDb ($$) {
# Purpose: Opens a database
# Returns: True/False
# Usage: $self->_openDb('foo.db');
my $self = shift;
my $dbNm = shift;
my $rv = 0;
my $dbh;
pdebug( "entering w/($dbNm)", PDLEVEL2 );
pIn();
if ( !defined $$self{DbEnv} ) {
# No environment available
pdebug( 'environment not available', PDLEVEL3 );
$rv = 0;
} else {
# suppress BerlekelDB warnings on older version
no strict 'subs';
# use lockfile to avoid race conditions
plock( "$$self{DbDir}/plock", 'write', $$self{DbMode} );
pdebug( "creating database $dbNm in $$self{DbDir}", PDLEVEL3 );
$dbh = BerkeleyDB::Hash->new(
'-Filename' => $dbNm,
'-Env' => $$self{DbEnv},
'-Flags' => DB_CREATE,
'-Mode' => $$self{DbMode},
);
$rv = defined $dbh;
if ($rv) {
$$self{Dbs}{$dbNm} = $dbh;
} else {
pdebug( "failed to open database: $BerkeleyDB::Error", PDLEVEL1 );
}
# release the lock
punlock("$$self{DbDir}/plock");
}
pOut();
pdebug( "leaving w/rv: $rv", PDLEVEL2 );
return $rv;
}
sub _closeDb ($$) {
# Purpose: Closes a database
# Returns: True/False
# Usage: $self->_closeDb('foo.db');
my $self = shift;
my $dbNm = shift;
my $rv = 1;
pdebug( "entering w/($dbNm)", PDLEVEL2 );
pIn();
if ( exists $$self{Dbs}{$dbNm} and defined $$self{Dbs}{$dbNm}) {
# use lockfile to avoid race conditions
plock( "$$self{DbDir}/plock", 'write', $$self{DbMode} );
# Close the database
if ( $$self{Dbs}{$dbNm}->db_close ) {
delete $$self{Dbs}{$dbNm};
$rv = 1;
} else {
pdebug( "failed to close database $dbNm: $BerkeleyDB::Error",
PDLEVEL1 );
}
# release the lock
punlock("$$self{DbDir}/plock");
} else {
# Database is already gone or never was
delete $$self{Dbs}{$dbNm};
$rv = 1;
}
pOut();
pdebug( "leaving w/rv: $rv", PDLEVEL2 );
return $rv;
}
sub _closeEnv ($) {
# Purpose: Closes the environment
# Returns: True/False
# Usage: $self->_closeEnv;
my $self = shift;
my $rv = 0;
pdebug( 'entering', PDLEVEL2 );
pIn();
if ( defined $$self{DbEnv} ) {
# Make sure there are no databases open
if ( scalar keys %{ $$self{Dbs} } ) {
pdebug( 'cannot close an environment with databases open',
PDLEVEL1 );
} else {
$$self{DbEnv} = undef;
$rv = 1;
}
} else {
$rv = 1;
}
pOut();
pdebug( "leaving w/rv: $rv", PDLEVEL2 );
return $rv;
}
sub _closeAll ($) {
# Purpose: Closes all databases and environment
# Returns: True/False
# Usage: $self->_closeAll;
my $self = shift;
my $rv = 1;
my @dbs;
pdebug( 'entering', PDLEVEL2 );
pIn();
# Close all the databases
foreach ( keys %{ $$self{Dbs} } ) {
unless ( $rv = $self->_closeDb($_) ) {
$rv = 0;
last;
}
}
# Close the environment
if ($rv) {
$rv = $self->_closeEnv;
}
pOut();
pdebug( "leaving w/rv: $rv", PDLEVEL2 );
return $rv;
}
sub _reopenAll ($) {
# Purpose: Closes and reopens all databases and environment
# Returns: True/False
# Usage: $self->_reopenAll;
my $self = shift;
my $rv = 0;
my @pkgs = qw(BerkeleyDB::Common BerkeleyDB::CDS::Lock
BerkeleyDB::Env BerkeleyDB::Cursor);
my ( @dbs, $fqm, $code, %crefs );
pdebug( 'entering', PDLEVEL2 );
pIn();
# Get a list of open dbs
@dbs = keys %{ $$self{Dbs} };
{
no strict 'refs';
no warnings qw(redefine prototype);
# Remove DESTROY hooks from BerkeleyDB temporarily
foreach (@pkgs) {
$fqm = $_ . '::DESTROY';
$code = *{$fqm}{CODE};
if ( defined $code ) {
$crefs{$fqm} = $code;
*{$fqm} = sub { return 1 };
}
}
# Close everything
$$self{DbLock} = undef;
foreach (@dbs) { delete $$self{Dbs}{$_} }
$$self{DbEnv} = undef;
# Reinstall DESTROY hooks
foreach ( keys %crefs ) {
*{$_} = $crefs{$_};
}
}
# Reopen environment
if ( $self->_openEnv ) {
# Reopen all dbs
$rv = 1;
foreach (@dbs) {
unless ( $self->_openDb($_) ) {
$rv = 0;
last;
}
}
}
pOut();
pdebug( "leaving w/rv: $rv", PDLEVEL2 );
return $rv;
}
sub _chkPID ($) {
# Purpose: Checks the PID the object was created under and reopens the
# database & environments if needed.
# Returns: True or croaks.
# Usage: $self->_chkPID;
my $self = shift;
# Check PID
if ( $$self{PID} != $$ ) {
if ( $self->_reopenAll ) {
$$self{DbLock} = undef;
$$self{PID} = $$;
} else {
croak 'failed to reopen databases due to forked process';
}
}
return 1;
}
sub new (@) {
# Purpose: Instantiates a new object of this class
# Returns: Object reference if successful, undef otherwise
# Usage: $obj = Paranoid::BerkeleyDB->new(
# DbDir = $dir,
# DbName = $name,
# );
my ( $class, %args ) = @_;
my %init = (
DbDir => undef,
DbName => undef,
Dbs => {},
DbEnv => undef,
DbMode => undef,
DbLock => undef,
PID => $$,
);
my $dbdir = defined $args{DbDir} ? $args{DbDir} : 'undef';
my $dbNm = defined $args{DbName} ? $args{DbName} : 'undef';
my $mode = defined $args{DbMode} ? $args{DbMode} : DEF_MODE;
my ( $self, @dbs, $d );
pdebug( "entering w/DbDir => \"$dbdir\", DbName => \"$dbNm\"", PDLEVEL1 );
pIn();
# Bless the reference
$self = \%init;
bless $self, $class;
# Populate list of dbs
@dbs = ( defined $dbNm and ref($dbNm) eq 'ARRAY' ) ? @$dbNm : ($dbNm);
# Set some defaults
$$self{DbMode} = $mode;
$$self{DbDir} = $dbdir;
# Make sure directory is available and writeable
$self = undef
unless defined $dbdir
and length $dbdir
and -d $dbdir
and -w _;
# Open database environment
if ($self) {
$self = undef unless $self->_openEnv;
}
# Open database(s)
if ($self) {
if ( defined $dbNm ) {
$$self{DbName} = $dbs[0];
foreach (@dbs) {
unless ( defined $_ and $self->_openDb($_) ) {
$self = undef;
last;
}
}
}
}
pOut();
pdebug( 'leaving w/rv: ' . ( defined $self ? $self : 'undef' ),
PDLEVEL1 );
return $self;
}
sub addDb ($$) {
# Purpose: Adds a new named database to the current environment
# Returns: True/false
# Usage: $rv = $db->addDb( 'foo.db' );
my $self = shift;
my $dbNm = shift;
my $dbdir = $$self{DbDir};
my $n = defined $dbNm ? $dbNm : 'undef';
my $mode = $$self{DbMode};
my $rv = 0;
my $db;
pdebug( "entering w/($n)", PDLEVEL1 );
pIn();
croak 'Mandatory first argument must be a valid filename'
unless defined $dbNm and length $dbNm;
$self->_chkPID;
$rv = $self->_openDb($dbNm);
pOut();
pdebug( "leaving w/rv: $rv", PDLEVEL1 );
return $rv;
}
sub getVal ($$;$) {
# Purpose: Returns the associated value for the requested key.
# Returns: String if key exists, undef otherwise
# Usage: $db->getVal( $key );
# Usage: $db->getVal( $key, $dbName );
my $self = shift;
my $key = shift;
my $db = shift;
my $k = defined $key ? $key : 'undef';
my $d = defined $db ? $db : 'undef';
my $dref = $$self{Dbs};
my ( $val, $v );
pdebug( "entering w/($k)($d)", PDLEVEL1 );
pIn();
# Set the default database name if it wasn't passed
unless ( defined $db ) {
$db = $$self{DbName};
pdebug( "setting db to default ($db)", PDLEVEL2 );
}
$self->_chkPID;
# Check the existence of the database
if ( exists $$dref{$db} ) {
unless ( $$dref{$db}->db_get( $key, $val ) == 0 ) {
pdebug( "no such key exists ($key)", PDLEVEL2 );
}
} else {
# Report invalid database
Paranoid::ERROR =
pdebug( "attempted to access a nonexistent database ($db)",
PDLEVEL1 );
}
$v = defined $val ? $val : 'undef';
pOut();
pdebug( "leaving w/rv: $v", PDLEVEL1 );
return $val;
}
sub setVal ($$;$$) {
# Purpose: Associates the key with the passed value or, if the
# value is undefined, deletes any existing key.
# Returns: True/false
# Usage: $db->setVal( $key, $value );
# Usage: $db->setVal( $key, $value, $dbName );
my $self = shift;
my $key = shift;
my $val = shift;
my $db = shift;
my $k = defined $key ? $key : 'undef';
my $v = defined $val ? $val : 'undef';
my $d = defined $db ? $db : 'undef';
my $dref = $$self{Dbs};
my $rv = 0;
my $lock;
pdebug( "entering w/($k)($v)($d)", PDLEVEL1 );
pIn();
# Set the default database name if it wasn't passed
unless ( defined $db ) {
$db = $$self{DbName};
pdebug( "setting db to default ($db)", PDLEVEL2 );
}
$self->_chkPID;
# Check the existence of the database
if ( exists $$dref{$db} ) {
# Use an inherited lock or get a new one
$lock =
defined $$self{DbLock}
? $$self{DbLock}
: $$dref{$db}->cds_lock;
# Requested database exists
#
# Make sure key is defined
if ( defined $key and defined $lock ) {
# Check whether setting a new record or deleting one
if ( defined $val ) {
# Setting a new record
pdebug( "setting key $key to $val", PDLEVEL2 );
$rv = !$$dref{$db}->db_put( $key, $val );
} else {
# Deleting the record
pdebug( "deleting key ($key)", PDLEVEL2 );
$rv = !$$dref{$db}->db_del($key);
}
} else {
# Report use of an undefined key
Paranoid::ERROR =
pdebug( 'attempted to use an undefined key', PDLEVEL1 );
}
# Unlock database
$lock->cds_unlock unless defined $$self{DbLock};
} else {
# Report invalid database
Paranoid::ERROR =
pdebug( "attempted to access a nonexistent database ($db)",
PDLEVEL1 );
}
pOut();
pdebug( "leaving w/rv: $rv", PDLEVEL1 );
return $rv;
}
sub getKeys ($;$$) {
# Purpose: Returns a list of all the keys in the database and
# optionally runs a subroutine over each record.
# Returns: List of keys.
# Usage: @keys = $db->getKeys;
# Usage: @keys = $db->getKeys( $dbName );
# Usage: @keys = $db->getKeys( $dbName, undef );
my $self = shift;
my $db = shift;
my $subRef = shift;
my $d = defined $db ? $db : 'undef';
my $s = defined $subRef ? $subRef : 'undef';
my $dref = $$self{Dbs};
my ( $cursor, $key, $val, @keys, $locked );
pdebug( "entering w/($d)($s)", PDLEVEL1 );
pIn();
# Set the default database name if it wasn't passed
unless ( defined $db ) {
$db = $$self{DbName};
pdebug( "setting db to default ($db)", PDLEVEL2 );
}
$self->_chkPID;
# Make sure database exists
if ( exists $$dref{$db} ) {
# Create/store a global lock if a subref is passed
# (checking to make sure we don't already have one)
if ( defined $$self{DbLock} ) {
$locked = 1;
} else {
$locked = 0;
$$self{DbLock} = $$dref{$db}->cds_lock;
}
# Retrieve all the keys
$key = $val = '';
$cursor =
defined $subRef
? $$dref{$db}->db_cursor(DB_WRITECURSOR)
: $$dref{$db}->db_cursor;
while ( $cursor->c_get( $key, $val, DB_NEXT ) == 0 ) {
if ( defined $key ) {
# The method was passed a subroutine reference, so
# pass the db object ref and values to the sub
&$subRef( $self, $key, $val ) if defined $subRef;
# Save the key;
push @keys, $key;
}
}
$cursor->c_close;
undef $cursor;
# Close a global lock if we opened it
unless ($locked) {
$$self{DbLock}->cds_unlock;
$$self{DbLock} = undef;
}
} else {
# Report invalid database
Paranoid::ERROR =
pdebug( "attempted to access a nonexistent database ($db)",
PDLEVEL1 );
}
pOut();
pdebug( "leaving w/@{[ scalar @keys ]} keys", PDLEVEL1 );
return @keys;
}
sub purgeDb ($;$) {
# Purpose: Empties the database.
# Returns: True/false
# Usage: $rv = $db->purgeDb;
# Usage: $rv = $db->purgeDb( $dbName );
my $self = shift;
my $db = shift;
my $d = defined $db ? $db : 'undef';
my $dref = $$self{Dbs};
my $rv = 0;
my $lock;
pdebug( "entering w/($d)", PDLEVEL1 );
pIn();
# Set the default database name if it wasn't passed
unless ( defined $db ) {
$db = $$self{DbName};
pdebug( "setting db to default ($db)", PDLEVEL2 );
}
$self->_chkPID;
# Make sure database exists
if ( exists $$dref{$db} ) {
# Use an inherited lock or get a new one
$lock =
defined $$self{DbLock}
? $$self{DbLock}
: $$dref{$db}->cds_lock;
# Purge the database
$$dref{$db}->truncate($rv);
# Unlock database
$$dref{$db}->db_sync;
$lock->cds_unlock unless defined $$self{DbLock};
} else {
# Report invalid database
Paranoid::ERROR =
pdebug( "attempted to purge a nonexistent database ($db)",
PDLEVEL1 );
$rv = BDB_ERR;
}
pOut();
pdebug( "leaving w/rv: $rv", PDLEVEL1 );
return $rv;
}
sub listDbs ($) {
# Purpose: Returns a list of all named database
# Returns: List of database names
# Usage: @dbs = $db->listDbs;
my $self = shift;
my $dref = $$self{Dbs};
my @dbs = keys %$dref;
pdebug( 'entering', PDLEVEL1 );
pdebug( "Leaving w/rv: @dbs", PDLEVEL1 );
return @dbs;
}
sub cds_lock ($) {
# Purpose: Places a lock on the database environment
# Returns: True/False
# Usage: $self->cds_lock;
my $self = shift;
my $dref = $$self{Dbs};
my $rv = 0;
my $db;
pdebug( 'entering', PDLEVEL1 );
pIn();
# Set the default database name if it wasn't passed
unless ( defined $db ) {
$db = $$self{DbName};
pdebug( "setting db to default ($db)", PDLEVEL2 );
}
if ( defined $$self{DbLock} ) {
# Already have a lock
$rv = 1;
} else {
# Get a new lock
$rv = defined( $$self{DbLock} = $$dref{$db}->cds_lock );
}
pOut();
pdebug( "leaving w/rv: $rv", PDLEVEL1 );
return $rv;
}
sub cds_unlock ($) {
# Purpose: Unlocks the database environment
# Returns: True/False
# Usage: $self->cds_unlock;
my $self = shift;
my $rv = 0;
pdebug( 'entering', PDLEVEL1 );
pIn();
# Remove the lock
if ( defined $$self{DbLock} ) {
$$self{DbLock}->cds_unlock;
$$self{DbLock} = undef;
}
$rv = 1;
pOut();
pdebug( "leaving w/rv: $rv", PDLEVEL1 );
return $rv;
}
sub DESTROY {
my $self = shift;
my $dref = $$self{Dbs};
my $dbdir = $$self{DbDir};
pdebug( 'entering', PDLEVEL1 );
pIn();
# Release any locks
if ( defined $$self{DbLock} ) {
$$self{DbLock}->cds_unlock;
$$self{DbLock} = undef;
}
# Sync & Close all dbs
$self->_closeAll;
pOut();
pdebug( 'leaving', PDLEVEL1 );
return 1;
}
1;
__END__
=head1 NAME
Paranoid::BerkeleyDB -- BerkeleyDB CDS Object
=head1 VERSION
$Id: BerkeleyDB.pm,v 0.85 2011/12/08 07:30:26 acorliss Exp $
=head1 SYNOPSIS
use Paranoid::BerkeleyDB;
$db = Paranoid::BerkeleyDB->new(DbDir => '/tmp', DbName => 'foo.db',
DbMode => 0640);
$rv = $db->addDb($dbname);
$val = $db->getVal($key);
$val = $db->getVal($key, $dbname);
$rv = $db->setVal($key, $val);
$rv = $db->setVal($key, $val, $dbname);
@keys = $db->getKeys();
@keys = $db->getKeys($dbname);
@keys = $db->getKeys(undef, \&sub);
@keys = $db->getKeys($dbname, \&sub);
$db->purgeDb();
$db->purgeDb($dbname);
@dbs = $db->listDbs();
$db->cds_lock;
$db->cds_unlock;
# Close environment & databases
$db = undef;
=head1 DESCRIPTION
This provides a OO-based wrapper for BerkeleyDB that creates Concurrent Data
Store (CDS) databases. This is a feature of Berkeley DB v3.x and higher that
provides for concurrent use of Berkeley DBs. It provides for multiple reader,
single writer locking, and multiple databases can share the same environment.
This module hides much of the complexity of the API (as provided by the
L<BerkeleyDB(3)> module. Conversely, it also severely limits the options and
flexibility of the module and libraries as well. In short, if you want a
quick and easy way for local processes to have concurrent access to Berkeley
DBs without learning bdb internals, this is your module. If you want full
access to all of the bdb features and tuning/scalability features, you'd
better learn dbd.
One particulary nice feature of this module, however, is that it's fork-safe.
That means you can open a CDS db in a parent process, fork, and continue r/w
operations without fear of corruption or lock contention due to stale
filehandles.
B<lock> and B<unlock> methods are also provided to allow mass changes as an
atomic operation. Since the environment is always created with a single
global write lock (regardless of how many databases exist within the
environment) operations can be made on multiple databases.
=head1 SUBROUTINES/METHODS
=head2 new
$db = Paranoid::BerkeleyDB->new(DbDir => '/tmp', DbName => 'foo.db');
This class method is the object instantiator. Two arguments are required:
B<DbDir> which is the path to the directory where the database files will be
stored, and B<DbName> which is the filename of the database itself. If
B<DbDir> doesn't exist it will be created for you automatically.
B<DbMode> is optional, and if omitted defaults to 0700. This affects the
database directory, files, and lockfile.
This method will create a BerkeleyDB Environment and will support
multiprocess transactions.
Any errors in the operation will be stored in B<Paranoid::ERROR>.
=head2 addDb
$rv = $db->addDb($dbname);
This method adds another database to the current object and environment.
Calling this method does require an exclusive write lock to the database
to prevent race conditions.
Any errors in the operation will be stored in B<Paranoid::ERROR>.
=head2 getVal
$val = $db->getVal($key);
$val = $db->getVal($key, $dbname);
This method retrieves the associated string to the passed key. Called with
one argument the method uses the default database. Otherwise, a second
argument specifying the specific database is required.
Requesting a non-existent key or from a nonexistent database will result in
an undef being returned. In the case of the latter an error message will also
be set in B<Paranoid::ERROR>.
=head2 setVal
$rv = $db->setVal($key, $val);
$rv = $db->setVal($key, $val, $dbname);
This method adds or updates an associative pair. If the passed value is
B<undef> the key is deleted from the database. If no database is explicitly
named it is assumed that the default database is the one to work on.
Requesting a non-existent key or from a nonexistent database will result in
an undef being returned. In the case of the latter an error message will also
be set in B<Paranoid::ERROR>.
=head2 getKeys
@keys = $db->getKeys();
@keys = $db->getKeys($dbname);
@keys = $db->getKeys(undef, \&sub);
@keys = $db->getKeys($dbname, \&sub);
If this method is called without the optional subroutine reference it will
return all the keys in the hash in hash order. If a subroutine reference is
called it will be called as each key/value pair is iterated over with three
arguments:
&$subRef($dbObj, $key, $value);
with $dbObj being a handle to the current database object. You may
use this ref to make changes to the database. Anytime a code
reference is handed to this method it is automatically opened with a write
lock under the assumption that this might be a transformative operation.
=head2 purgeDb
$db->purgeDb();
$db->purgeDb($dbname);
This method purges all associative pairs from the designated database. If no
database name was passed then the default database will be used. This method
returns the number of records purged, or a -1 if an invalid database was
requested.
=head2 listDbs
@dbs = $db->listDbs();
This method returns a list of databases accessible by this object.
=head2 cds_lock
$db->cds_lock;
This method places a global write lock on the shared database environment.
Since environments are created with a global lock (covering all databases in
the environment) no writes or reads can be done by other processes until this
is unlocked.
=head2 cds_unlock
$db->cds_unlock;
This method removes a global write lock on the shared database environment.
=head2 DESTROY
A DESTROY method is provided which should sync and close an open database, as
well as release any locks.
=head1 DEPENDENCIES
=over
=item o
L<Paranoid>
=item o
L<Paranoid::Debug>
=item o
L<Paranoid::Filesystem>
=item o
L<Paranoid::Lockfile>
=item o
L<BerkeleyDB>
=back
=head1 BUGS AND LIMITATIONS
Race conditions, particularly on database creation/opens, are worked around by
the use of external lock files and B<flock> advisory file locks. Lockfiles
are not used during normal operations on the database.
While CDS allows for safe concurrent use of database files, it makes no
allowances for recovery from stale locks. If a process exits badly and fails
to release a write lock (which causes all other process operations to block
indefinitely) you have to intervene manually. The brute force intervention
would mean killing all accessing processes and deleting the environment files
(files in the same directory call __db.*). Those will be recreated by the
next process to access them.
Berkeley DB provides a handy CLI utility called L<db_stat(1)>. It can provide
some statistics on your shared database environment via invocation like so:
db_stat -m -h .
The last argument, of course, is the directory in which the environment was
created. The example above would work fine if your working directory was that
directory.
You can also show all existing locks via:
db_stat -N -Co -h .
=head1 SEE ALSO
L<BerkeleyDB(3)>
=head1 HISTORY
2011/12/06: Added fork-safe operation
=head1 AUTHOR
Arthur Corliss (corliss@digitalmages.com)
=head1 LICENSE AND COPYRIGHT
This software is licensed under the same terms as Perl, itself.
Please see http://dev.perl.org/licenses/ for more information.
(c) 2005, Arthur Corliss (corliss@digitalmages.com)
|