Revision history for DBM::Deep.
+0.99_01 ??? ?? ??;??:?? 2006 Pacific
+ - Removed error()/clear_error()
+
0.98 Feb 28 11:00:00 2006 Pacific
- Added in patch by David Cantrell to allow use of DATA filehandle
- Fixed bug where attempting to export() a structure that used autobless would die
use Digest::MD5 ();
use Scalar::Util ();
+use DBM::Deep::Engine;
+
use vars qw( $VERSION );
$VERSION = q(0.99_01);
# These are the defaults to be optionally overridden below
my $self = bless {
- type => TYPE_HASH,
+ type => TYPE_HASH,
base_offset => length(SIG_FILE),
+ engine => 'DBM::Deep::Engine',
}, $class;
foreach my $param ( keys %$self ) {
return $self->{base_offset};
}
-sub error {
- ##
- # Get last error string, or undef if no error
- ##
- return $_[0]
- ? ( $_[0]->_get_self->{root}->{error} or undef )
- : $@;
-}
-
##
# Utility methods
##
sub _throw_error {
- ##
- # Store error string in self
- ##
- my $error_text = $_[1];
-
- if ( Scalar::Util::blessed $_[0] ) {
- my $self = $_[0]->_get_self;
- $self->_root->{error} = $error_text;
-
- unless ($self->_root->{debug}) {
- die "DBM::Deep: $error_text\n";
- }
-
- warn "DBM::Deep: $error_text\n";
- return;
- }
- else {
- die "DBM::Deep: $error_text\n";
- }
-}
-
-sub clear_error {
- ##
- # Clear error state
- ##
- my $self = $_[0]->_get_self;
-
- undef $self->_root->{error};
+ die "DBM::Deep: $_[1]\n";
}
sub _precalc_sizes {
##
# Make sure file is open
##
- if (!defined($self->_fh) && !$self->_open()) {
- return;
- }
+# if (!defined($self->_fh) && !$self->_open()) {
+# return;
+# }
unless ( _is_writable( $self->_fh ) ) {
$self->_throw_error( 'Cannot write to a readonly filehandle' );
##
# Make sure file is open
##
- if (!defined($self->_fh)) { $self->_open(); }
+# if (!defined($self->_fh)) { $self->_open(); }
my $md5 = $DIGEST_FUNC->($key);
##
# Make sure file is open
##
- if (!defined($self->_fh)) { $self->_open(); }
+# if (!defined($self->_fh)) { $self->_open(); }
##
# Request exclusive lock for writing
##
# Make sure file is open
##
- if (!defined($self->_fh)) { $self->_open(); }
+# if (!defined($self->_fh)) { $self->_open(); }
##
# Request shared lock for reading
##
# Make sure file is open
##
- if (!defined($self->_fh)) { $self->_open(); }
+# if (!defined($self->_fh)) { $self->_open(); }
##
# Request exclusive lock for writing
q.v. adjusting the interal parameters.
-=item * error() / clear_error()
-
-Error handling methods. These are deprecated and will be removed in 1.00.
-.
=back
=head2 HASHES
=head1 ERROR HANDLING
Most DBM::Deep methods return a true value for success, and call die() on
-failure. You can wrap calls in an eval block to catch the die. Also, the
-actual error message is stored in an internal scalar, which can be fetched by
-calling the C<error()> method.
+failure. You can wrap calls in an eval block to catch the die.
my $db = DBM::Deep->new( "foo.db" ); # create hash
eval { $db->push("foo"); }; # ILLEGAL -- push is array-only call
print $@; # prints error message
- print $db->error(); # prints error message
-
-You can then call C<clear_error()> to clear the current error state.
-
- $db->clear_error();
-
-If you set the C<debug> option to true when creating your DBM::Deep object,
-all errors are considered NON-FATAL, and dumped to STDERR. This should only
-be used for debugging purposes and not production work. DBM::Deep expects errors
-to be thrown, not propagated back up the stack.
-
-B<NOTE>: error() and clear_error() are considered deprecated and I<will> be removed
-in 1.00. Please don't use them. Instead, wrap all your functions with in eval-blocks.
=head1 LARGEFILE SUPPORT
##
# Make sure file is open
##
- if (!defined($self->_fh)) { $self->_open(); }
+# if (!defined($self->_fh)) { $self->_open(); }
##
# Request shared lock for reading
##
# Make sure file is open
##
- if (!defined($self->_fh)) { $self->_open(); }
+# if (!defined($self->_fh)) { $self->_open(); }
##
# Request shared lock for reading
##
unlink "t/test.db";
my $db = eval { DBM::Deep->new( "t/test.db" ) };
-if ( DBM::Deep::error( $db ) || !$db ) {
- diag "ERROR: " . (DBM::Deep::error($db) || $@ || "UNKNOWN\n");
+if ( $@ ) {
+ diag "ERROR: $@";
Test::More->builder->BAIL_OUT( "Opening a new file fails" );
}
unlink "t/test.db";
my $db = DBM::Deep->new( "t/test.db" );
-if ($db->error()) {
- die "ERROR: " . $db->error();
-}
##
# put/get key
##
undef $db;
$db = DBM::Deep->new( "t/test.db" );
-if ($db->error()) {
- die "ERROR: " . $db->error();
-}
is( $db->get("key1"), "value222222222222222222222222", "The value we set is still there after closure" );
##
file => "t/test.db",
type => DBM::Deep->TYPE_HASH
);
-if ($db->error()) {
- die "ERROR: " . $db->error();
-}
##
# put/get many keys
file => "t/test.db",
type => DBM::Deep->TYPE_ARRAY
);
-if ($db->error()) {
- die "ERROR: " . $db->error();
-}
TODO: {
local $TODO = "How is this test ever supposed to pass?";
file => "t/test.db",
type => DBM::Deep->TYPE_ARRAY
);
-if ($db->error()) {
- die "ERROR: " . $db->error();
-}
##
# put/get many keys
use_ok( 'DBM::Deep' );
##
-# make sure you can clear the error state
-##
-##
# test a corrupted file
##
open FH, '>t/test.db';
file => "t/test.db",
locking => 1,
);
-if ($db->error()) {
- die "ERROR: " . $db->error();
-}
##
# basic put/get
my $db = DBM::Deep->new(
file => "t/test.db",
);
-if ($db->error()) {
- die "ERROR: " . $db->error();
-}
##
# basic deep hash
file => "t/test.db",
type => DBM::Deep->TYPE_ARRAY,
);
-if ($db->error()) {
- die "ERROR: " . $db->error();
-}
$db->[0] = [];
my $temp_db = $db->[0];
my $db = DBM::Deep->new(
file => "t/test.db"
);
-if ($db->error()) {
- die "ERROR: " . $db->error();
-}
##
# large keys
file => "t/test.db",
autoflush => 1,
);
-if ($db->error()) {
- die "ERROR: " . $db->error();
-}
##
# create some unused space
my $result = $db->optimize();
my $after = (stat($db->_fh()))[7];
-if ($db->error()) {
- die "ERROR: " . $db->error();
-}
-
ok( $result, "optimize succeeded" );
ok( $after < $before, "file size has shrunk" ); # make sure file shrunk
autoflush => 1,
locking => 1
);
- if ($db->error()) {
- die "ERROR: " . $db->error();
- }
# optimize and exit
$db->optimize();
autoflush => 1,
locking => 1
);
- if ($db->error()) {
- die "ERROR: " . $db->error();
- }
# sleep for 1 second to make sure optimize() is running in the other fork
sleep(1);
my $db = DBM::Deep->new(
file => "t/test.db",
);
-if ($db->error()) {
- die "ERROR: " . $db->error();
-}
$db->{key1} = "value1";
file => "t/test.db",
autoflush => 1
);
-if ($db->error()) {
- die "ERROR: " . $db->error();
-}
$db->{key1} = "value1";
$db->{key2} = "value2";
my $before = (stat($db->_fh()))[7];
file => "t/test.db",
autoflush => 1
);
-if ($db->error()) {
- die "ERROR: " . $db->error();
-}
$db->{key1} = "value1";
$db->{key2} = "value2";
my $after = (stat($db->_fh()))[7];
my $db = DBM::Deep->new(
file => "t/test.db",
);
-if ($db->error()) {
- die "ERROR: " . $db->error();
-}
ok( !$db->set_filter( 'floober', sub {} ), "floober isn't a value filter key" );
my $db = new DBM::Deep(
file => "t/test.db"
);
-if ($db->error()) {
- die "ERROR: " . $db->error();
-}
##
# Set digest handler
unlink "t/test.db";
my $db = DBM::Deep->new( "t/test.db" );
-if ($db->error()) {
- die "ERROR: " . $db->error();
-}
##
# put/get simple keys
unlink "t/test.db";
my $db = DBM::Deep->new( "t/test.db" );
-if ($db->error()) {
- die "ERROR: " . $db->error();
-}
##
# Create structure in memory
unlink "t/test.db";
my $db = DBM::Deep->new( "t/test.db" );
-if ($db->error()) {
- die "ERROR: " . $db->error();
-}
##
# Create structure in DB
unlink "t/test.db";
my $db = DBM::Deep->new( "t/test.db" );
-if ($db->error()) {
- die "ERROR: " . $db->error();
-}
unlink "t/test2.db";
my $db2 = DBM::Deep->new( "t/test2.db" );
-if ($db2->error()) {
- die "ERROR: " . $db2->error();
-}
##
# Create structure in $db
my %hash;
my $db = tie %hash, 'DBM::Deep', 't/test.db';
- if ($db->error()) {
- print "ERROR: " . $db->error();
- ok(0);
- exit(0);
- }
- else { ok(1, "Tied an hash with an array for params" ); }
+ ok(1, "Tied an hash with an array for params" );
}
{
file => 't/test.db',
};
- if ($db->error()) {
- print "ERROR: " . $db->error();
- ok(0);
- exit(0);
- }
- else { ok(1, "Tied a hash with a hashref for params" ); }
+ ok(1, "Tied a hash with a hashref for params" );
}
{
my @array;
my $db = tie @array, 'DBM::Deep', 't/test.db';
- if ($db->error()) {
- print "ERROR: " . $db->error();
- ok(0);
- exit(0);
- }
- else { ok(1, "Tied an array with an array for params" ); }
+ ok(1, "Tied an array with an array for params" );
is( $db->{type}, DBM::Deep->TYPE_ARRAY, "TIE_ARRAY sets the correct type" );
}
file => 't/test.db',
};
- if ($db->error()) {
- print "ERROR: " . $db->error();
- ok(0);
- exit(0);
- }
- else { ok(1, "Tied an array with a hashref for params" ); }
+ ok(1, "Tied an array with a hashref for params" );
is( $db->{type}, DBM::Deep->TYPE_ARRAY, "TIE_ARRAY sets the correct type" );
}
unlink "t/test.db";
my $db = DBM::Deep->new( "t/test.db" );
-if ($db->error()) {
- die "ERROR: " . $db->error();
-}
##
# Create structure in $db
unlink "t/test.db";
my $db = DBM::Deep->new( "t/test.db" );
-if ($db->error()) {
- die "ERROR: " . $db->error();
-}
$db->{key1} = "value1";
is( $db->{key1}, "value1", "Value set correctly" );
file => "t/test.db",
autobless => 1,
);
- if ($db->error()) {
- die "ERROR: " . $db->error();
- }
my $obj = bless {
a => 1,
file => 't/test.db',
autobless => 1,
);
- if ($db->error()) {
- die "ERROR: " . $db->error();
- }
my $obj = $db->{blessed};
isa_ok( $obj, 'Foo' );
my $db = DBM::Deep->new(
file => 't/test.db',
);
- if ($db->error()) {
- die "ERROR: " . $db->error();
- }
my $obj = $db->{blessed};
isa_ok( $obj, 'DBM::Deep' );
file => "t/test2.db",
autobless => 1,
);
- if ($db->error()) {
- die "ERROR: " . $db->error();
- }
my $obj = bless {
a => 1,
b => [ 1 .. 3 ],
file => "t/test2.db",
autobless => 1,
);
- if ($db->error()) {
- die "ERROR: " . $db->error();
- }
my $blessed = $db->{blessed};
isa_ok( $blessed, 'Foo' );
file => "t/test3.db",
autobless => 1,
);
- if ($db->error()) {
- die "ERROR: " . $db->error();
- }
my $obj = bless {}, 'Foo';
unlink "t/test.db";
{
my $db = DBM::Deep->new( "t/test.db" );
- if ($db->error()) {
- die "ERROR: " . $db->error();
- }
my $x = 25;
my $y = 30;
{
my $db = DBM::Deep->new( "t/test.db" );
- if ($db->error()) {
- die "ERROR: " . $db->error();
- }
my $x = 25;
my $y = 30;