DBM-Deep
view release on metacpan or search on metacpan
utils/lib/DBM/Deep/09830.pm view on Meta::CPAN
my $self = $_[0]->_get_self;
my $struct = $_[1];
#XXX This use of ref() seems to be ok
if (!ref($struct)) {
##
# struct is not a reference, so just import based on our type
##
shift @_;
if ($self->_type eq TYPE_HASH) { $struct = {@_}; }
elsif ($self->_type eq TYPE_ARRAY) { $struct = [@_]; }
}
my $r = Scalar::Util::reftype($struct) || '';
if ($r eq "HASH" && $self->_type eq TYPE_HASH) {
foreach my $key (keys %$struct) { $self->put($key, $struct->{$key}); }
}
elsif ($r eq "ARRAY" && $self->_type eq TYPE_ARRAY) {
$self->push( @$struct );
}
else {
return $self->_throw_error("Cannot import: type mismatch");
}
return 1;
}
sub optimize {
##
# Rebuild entire database into new file, then move
# it back on top of original.
##
my $self = $_[0]->_get_self;
#XXX Need to create a new test for this
# if ($self->_root->{links} > 1) {
# return $self->_throw_error("Cannot optimize: reference count is greater than 1");
# }
my $db_temp = DBM::Deep::09830->new(
file => $self->_root->{file} . '.tmp',
type => $self->_type
);
if (!$db_temp) {
return $self->_throw_error("Cannot optimize: failed to open temp file: $!");
}
$self->lock();
$self->_copy_node( $db_temp );
undef $db_temp;
##
# Attempt to copy user, group and permissions over to new file
##
my @stats = stat($self->_fh);
my $perms = $stats[2] & 07777;
my $uid = $stats[4];
my $gid = $stats[5];
chown( $uid, $gid, $self->_root->{file} . '.tmp' );
chmod( $perms, $self->_root->{file} . '.tmp' );
# q.v. perlport for more information on this variable
if ( $^O eq 'MSWin32' || $^O eq 'cygwin' ) {
##
# Potential race condition when optmizing on Win32 with locking.
# The Windows filesystem requires that the filehandle be closed
# before it is overwritten with rename(). This could be redone
# with a soft copy.
##
$self->unlock();
$self->_close();
}
if (!rename $self->_root->{file} . '.tmp', $self->_root->{file}) {
unlink $self->_root->{file} . '.tmp';
$self->unlock();
return $self->_throw_error("Optimize failed: Cannot copy temp file over original: $!");
}
$self->unlock();
$self->_close();
$self->_open();
return 1;
}
sub clone {
##
# Make copy of object and return
##
my $self = $_[0]->_get_self;
return DBM::Deep::09830->new(
type => $self->_type,
base_offset => $self->_base_offset,
root => $self->_root
);
}
{
my %is_legal_filter = map {
$_ => ~~1,
} qw(
store_key store_value
fetch_key fetch_value
);
sub set_filter {
##
# Setup filter function for storing or fetching the key or value
##
my $self = $_[0]->_get_self;
my $type = lc $_[1];
my $func = $_[2] ? $_[2] : undef;
if ( $is_legal_filter{$type} ) {
$self->_root->{"filter_$type"} = $func;
return 1;
}
( run in 0.527 second using v1.01-cache-2.11-cpan-39bf76dae61 )