DBM-Deep

 view release on metacpan or  search on metacpan

utils/lib/DBM/Deep/10002.pm  view on Meta::CPAN

    if (!ref($_[0])) { return; } # Perl calls import() on use -- ignore

    my $self = shift->_get_self;
    my ($struct) = @_;

    # struct is not a reference, so just import based on our type
    if (!ref($struct)) {
        $struct = $self->_repr( @_ );
    }

    #XXX This isn't the best solution. Better would be to use Data::Walker,
    #XXX but that's a lot more thinking than I want to do right now.
    eval {
        local $SIG{'__DIE__'};
        $self->_import( Clone::clone( $struct ) );
    }; if ( my $e = $@ ) {
        die $e;
    }

    return 1;
}

#XXX Need to keep track of who has a fh to this file in order to
#XXX close them all prior to optimize on Win32/cygwin
sub optimize {
    ##
    # Rebuild entire database into new file, then move
    # it back on top of original.
    ##
    my $self = shift->_get_self;

#XXX Need to create a new test for this
#    if ($self->_storage->{links} > 1) {
#        $self->_throw_error("Cannot optimize: reference count is greater than 1");
#    }

    #XXX Do we have to lock the tempfile?

    my $db_temp = DBM::Deep::10002->new(
        file => $self->_storage->{file} . '.tmp',
        type => $self->_type,

        # Bring over all the parameters that we need to bring over
        num_txns => $self->_engine->num_txns,
        byte_size => $self->_engine->byte_size,
        max_buckets => $self->_engine->max_buckets,
    );

    $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->_storage->{file} . '.tmp' );
    chmod( $perms, $self->_storage->{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->_storage->close;
    }

    if (!rename $self->_storage->{file} . '.tmp', $self->_storage->{file}) {
        unlink $self->_storage->{file} . '.tmp';
        $self->unlock();
        $self->_throw_error("Optimize failed: Cannot copy temp file over original: $!");
    }

    $self->unlock();
    $self->_storage->close;

    $self->_storage->open;
    $self->lock();
    $self->_engine->setup_fh( $self );
    $self->unlock();

    return 1;
}

sub clone {
    ##
    # Make copy of object and return
    ##
    my $self = shift->_get_self;

    return DBM::Deep::10002->new(
        type        => $self->_type,
        base_offset => $self->_base_offset,
        staleness   => $self->_staleness,
        storage     => $self->_storage,
        engine      => $self->_engine,
    );
}

#XXX Migrate this to the engine, where it really belongs and go through some
# API - stop poking in the innards of someone else..
{
    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 = shift->_get_self;



( run in 0.570 second using v1.01-cache-2.11-cpan-39bf76dae61 )