BerkeleyDB-Easy

 view release on metacpan or  search on metacpan

lib/BerkeleyDB/Easy.pm  view on Meta::CPAN


    my $db = BerkeleyDB::Easy::Btree->new(
        -Filename => 'test.db',
        -Flags    => DB_CREATE,
    );

    $db->put('foo', 'bar');

    my $foo = $db->get('foo');

    my $cur = $db->cursor;

    while (my ($key, $val) = $cur->next) {
        $db->del($key);
    }

    $db->sync;

=head1 DESCRIPTION

BerkeleyDB::Easy is a convenience wrapper around BerkeleyDB.pm. It will 

lib/BerkeleyDB/Easy/Common.pm  view on Meta::CPAN


use strict;
use warnings;
no warnings 'uninitialized';

use Exporter     ();
use Scalar::Util ();

# The following are "switchboard" methods for class dispatching.
# The idea is that sometimes classes need to call each other laterally
# and not through the inheritance chain. For example, Handle::cursor()
# creates a new cursor, so it finds the right class via $self->_Cursor.
# That way, if you wanted to extend that class with your own, you
# could just override Common::_Cursor here instead of tracking down
# and overriding all the various call sites.

use constant {
	_Base   => 'BerkeleyDB::Easy',
	_Handle => 'BerkeleyDB::Easy::Handle',
	_Cursor => 'BerkeleyDB::Easy::Cursor',
	_Error  => 'BerkeleyDB::Easy::Error',
	_Common => 'BerkeleyDB::Easy::Common',

lib/BerkeleyDB/Easy/Common.pm  view on Meta::CPAN

	# get declared when we unrolled @_.
	my $decl = do {
		my %r =     map  { $_ => 1             } @{$spec->[RECV]};
		join q(, ), grep { $_ ne A and !$r{$_} } @{$spec->[SEND]};
	};

	# What BerkeleyDB.pm class are we wrapping?
	# Either ::Common (for all handle types) or ::Cursor.
	my $isa = do { no strict 'refs'; ${qq($pack\::ISA)}[0] };

	# Does the function return something we need to keep? (db_cursor)
	#   Yes (R): keep it and get $status from SUPER::status.
	#    No (S): return value is $status.
	my $keep = ( grep { $_ eq R } @{$spec->[SUCC]} ) ? R : S;
	
	# What function are we wrapping?
	my $func = $spec->[FUNC];

	# Does it require a default flag?
	my $flag = $spec->[FLAG];

lib/BerkeleyDB/Easy/Cursor.pm  view on Meta::CPAN

1;

=encoding utf8

=head1 NAME

BerkeleyDB::Easy::Cursor - Cursor to database handle

=head1 METHODS

Most of the functionaly for BerkeleyDB cursors are crammed into a few
underlying functions, with the behavior specified by a flag. For example, to
get the next record, you call C<c_get> and provide the C<DB_NEXT> flag. In
this module, these are split out into individual wrapper methods, and the
required flag is provided for you. You can specify additional flags
and they will be OR'ed together with the default.

=head2 first

Get the first record.

	($key, $val) = $cursor->first();

=head2 last

Get the last record.

	($key, $val) = $cursor->last();

=head2 next

Get the next record.
	
	($key, $val) = $cursor->next();

=head2 prev

Move the previous record.
	
	($key, $val) = $cursor->prev();

=head2 current

Get the record at the current position.
	
	($key, $val) = $cursor->current();

=head2 set

Position the cursor to the specified key.
	
	($key, $val) = $cursor->set($key);

=head2 after

Set the next record to the specified value.
Returns true on success.

	$bool = $cursor->after($val);

=head2 before
	
Set the previous record to the specified value.

	$bool = $cursor->before($val);

=head2 replace

Replace the record at the current position.
	
	$bool = $cursor->replace($val);

=head2 del

Delete the current record.
	
	$bool = $cursor->del();

=head2 close

Close the cursor.
	
	$bool = $cursor->close();

=head1 BUGS

This module is functional but unfinished.

=head1 AUTHOR

Rob Schaber, C<< <robschaber at gmail.com> >>

=head1 LICENSE

lib/BerkeleyDB/Easy/Handle.pm  view on Meta::CPAN


my %subs = (
    db_get    => ['db_get'   ,[     ],[A    ],[S  ],[S],0, ],
    get       => ['db_get'   ,[K,F  ],[K,V,F],[V  ],[ ],0, ],
    db_put    => ['db_put'   ,[     ],[A    ],[S  ],[S],0, ],
    put       => ['db_put'   ,[K,V,F],[K,V,F],[V  ],[ ],0, ],
    db_del    => ['db_del'   ,[     ],[A    ],[S  ],[S],0, ],
    del       => ['db_del'   ,[K,F  ],[K,F  ],[K  ],[ ],0, ],
    db_sync   => ['db_sync'  ,[     ],[A    ],[S  ],[S],0, ],
    sync      => ['db_sync'  ,[F    ],[F    ],[T  ],[ ],0, ],
    db_cursor => ['db_cursor',[     ],[A    ],[R  ],[R],0, ],
    associate => ['associate',[     ],[A    ],[S  ],[S],0, ],
    pget      => ['db_pget'  ,[X    ],[X,K,V],[K,V],[ ],0, ],
);

$subs{exists} = $BerkeleyDB::db_version >= 4.6
    ? ['exists',[K,F],[K,F  ],[T],[N],0]
    : ['db_get',[K,F],[K,V,F],[T],[N],0];

# Install the stubs
while (my ($name, $spec) = each %subs) {
    __PACKAGE__->_install($name, $spec);
}

#
# Constructor for a cursor to this DB handle. It could/should probably live
# at Cursor->new() but it's here for now.
#
sub cursor {
    my ($self, $flags) = @_;
    my $cursor = $self->db_cursor($flags);
    my $class  = $self->_Cursor;
    (my $file  = $class) =~ s(::)(\/)g;
    require $file . q(.pm);
    return bless $cursor, $class;
}

# Method aliases for naming consistency
*delete = \&del;
*cur    = \&cursor;

INFO and __PACKAGE__->_info(q(Handle.pm finished loading));

1;

=encoding utf8

=head1 NAME

BerkeleyDB::Easy::Handle - Generic class for Btree, Hash, Recno, Queue, and

lib/BerkeleyDB/Easy/Handle.pm  view on Meta::CPAN

    $val = $db->put($key, $val);

=head2 exists

    $bool = $db->exists($key);

=head2 sync

    $status = $db->sync();

=head2 cursor

    $cursor = $db->cursor();

=head1 BUGS

This module is functional but unfinished.

=head1 AUTHOR

Rob Schaber, C<< <robschaber at gmail.com> >>

=head1 LICENSE

t/synopsis.t  view on Meta::CPAN

my $db = BerkeleyDB::Easy::Btree->new();

is ref $db, 'BerkeleyDB::Easy::Btree';

my $val = $db->put('foo', 'bar');
is $val, 'bar';

my $foo = $db->get('foo');
is $foo, 'bar';

my $cur = $db->cursor;
is ref $cur, 'BerkeleyDB::Easy::Cursor';

while (my ($key, $val) = $cur->next) {
    my $key = $db->del($key);
    is $key, 'foo';
}

done_testing;



( run in 0.282 second using v1.01-cache-2.11-cpan-4d50c553e7e )