sophia-perl/Sophia.pm

216 lines
4.3 KiB
Perl
Raw Permalink Normal View History

2015-03-20 17:56:04 +03:00
package Database::Sophia;
use utf8;
use strict;
use vars qw($AUTOLOAD $VERSION $ABSTRACT @ISA @EXPORT);
BEGIN {
2015-03-20 21:18:57 +03:00
$VERSION = 1.2;
2015-03-20 17:56:04 +03:00
$ABSTRACT = "Sophia is a modern embeddable key-value database designed for a high load environment (XS for Sophia)";
2015-03-20 21:18:57 +03:00
@ISA = qw(DynaLoader);
2015-03-20 17:56:04 +03:00
};
bootstrap Database::Sophia $VERSION;
use DynaLoader ();
use Exporter ();
2015-03-20 21:18:57 +03:00
package Database::Sophia::Txn;
sub get
{
my ($self, $db, $key) = @_;
$db->get($key, $self);
}
sub delete
{
my ($self, $db, $key) = @_;
$db->delete($key, $self);
}
sub set
{
my ($self, $db, $key, $value) = @_;
$db->set($key, $value, $self);
}
package Database::Sophia::Snapshot;
*get = *Database::Sophia::Txn::get;
sub cursor
{
my ($self, $db, $key, $order) = @_;
$db->cursor($key, $order, $self);
}
2015-03-20 17:56:04 +03:00
1;
__END__
=head1 NAME
2015-03-20 23:36:29 +03:00
Database::Sophia - XS for Sophia 1.2, a modern embeddable key-value database designed for a high load environment.
2015-03-20 17:56:04 +03:00
=head1 SYNOPSIS
use Database::Sophia;
2015-03-20 23:36:29 +03:00
my $env = Database::Sophia::env();
my $ctl = $env->ctl;
2015-03-20 17:56:04 +03:00
2015-03-20 23:36:29 +03:00
$ctl->set('sophia.path', './storage');
$ctl->set('db', 'test');
2015-03-20 17:56:04 +03:00
2015-03-20 23:36:29 +03:00
$env->open;
2015-03-20 17:56:04 +03:00
2015-03-20 23:36:29 +03:00
my $db = $ctl->get('db.test');
2015-03-20 17:56:04 +03:00
2015-03-20 23:36:29 +03:00
if ($db->set('login', 'lastmac') != 0) {
die $ctl->get('sophia.error');
2015-03-20 17:56:04 +03:00
}
2015-03-20 23:36:29 +03:00
warn $db->get('login');
2015-03-20 17:56:04 +03:00
2015-03-20 23:36:29 +03:00
$db->delete('login');
my $transaction = $env->begin;
$transaction->set($db, 'login', 'otherguy');
$transaction->commit;
2015-03-20 17:56:04 +03:00
=head1 DESCRIPTION
It has unique architecture that was created as a result of research and rethinking of primary algorithmic constraints, associated with a getting popular Log-file based data structures, such as LSM-tree.
See http://sphia.org/
=head1 METHODS
2015-03-20 23:36:29 +03:00
=head2 Database::Sophia
2015-03-20 17:56:04 +03:00
2015-03-20 23:36:29 +03:00
=head3 $env = Database::Sophia::env()
2015-03-20 17:56:04 +03:00
2015-03-20 23:36:29 +03:00
return the environment handle
2015-03-20 17:56:04 +03:00
2015-03-20 23:36:29 +03:00
=head3 $ctl = $env->ctl()
2015-03-20 17:56:04 +03:00
2015-03-20 23:36:29 +03:00
get configuration object (Database::Sophia::Ctl)
2015-03-20 17:56:04 +03:00
2015-03-20 23:36:29 +03:00
=head3 $rc = $env->open()
2015-03-20 17:56:04 +03:00
2015-03-20 23:36:29 +03:00
opens the database
2015-03-20 17:56:04 +03:00
2015-03-20 23:36:29 +03:00
=head3 $txn = $env->begin()
2015-03-20 17:56:04 +03:00
2015-03-20 23:36:29 +03:00
starts a new transaction and returns its object (Database::Sophia::Txn)
2015-03-20 17:56:04 +03:00
2015-03-20 23:36:29 +03:00
=head2 Database::Sophia::Ctl
2015-03-20 17:56:04 +03:00
2015-03-20 23:36:29 +03:00
=head3 $rc = $ctl->set('key', 'value')
2015-03-20 17:56:04 +03:00
2015-03-20 23:36:29 +03:00
set a configuration parameter or call system procedure
2015-03-20 17:56:04 +03:00
2015-03-20 23:36:29 +03:00
=head3 $value = $ctl->get('key')
2015-03-20 17:56:04 +03:00
2015-03-20 23:36:29 +03:00
get the value of a configuration parameter, or database object by 'db.<NAME>' key,
or snapshot object by 'snapshot.<NAME>' key
2015-03-20 17:56:04 +03:00
2015-03-20 23:36:29 +03:00
=head3 $cursor = $ctl->cursor
2015-03-20 17:56:04 +03:00
2015-03-20 23:36:29 +03:00
get an iterator over all configuration values
2015-03-20 17:56:04 +03:00
2015-03-20 23:36:29 +03:00
=head2 Database::Sophia::DB
2015-03-20 17:56:04 +03:00
2015-03-20 23:36:29 +03:00
=head3 $ctl = $db->ctl()
2015-03-20 17:56:04 +03:00
2015-03-20 23:36:29 +03:00
get a configuration object for this database, equivalent to part of the global
Ctl with prefix 'db.<name>'
2015-03-20 17:56:04 +03:00
2015-03-20 23:36:29 +03:00
=head3 $value = $db->get('key')
2015-03-20 17:56:04 +03:00
2015-03-20 23:36:29 +03:00
read a key as a single-statement transaction
2015-03-20 17:56:04 +03:00
2015-03-20 23:36:29 +03:00
=head3 $db->set('key', 'value')
2015-03-20 17:56:04 +03:00
2015-03-20 23:36:29 +03:00
set a key as a single-statement transaction
2015-03-20 17:56:04 +03:00
2015-03-20 23:36:29 +03:00
=head3 $db->delete('key')
2015-03-20 17:56:04 +03:00
2015-03-20 23:36:29 +03:00
delete a key as a single-statement transaction
2015-03-20 17:56:04 +03:00
2015-03-20 23:36:29 +03:00
=head3 $cursor = $db->cursor($starting_key, $order)
2015-03-20 17:56:04 +03:00
2015-03-20 23:36:29 +03:00
get an iterator over database keys, starting with $starting_key in the direction $order,
which may be one of '<', '<=', '>=', '>'
2015-03-20 17:56:04 +03:00
2015-03-20 23:36:29 +03:00
=head2 Database::Sophia::Txn
2015-03-20 17:56:04 +03:00
2015-03-20 23:36:29 +03:00
=head3 $txn->get($db, 'key')
2015-03-20 17:56:04 +03:00
2015-03-20 23:36:29 +03:00
get a key from database $db as a part of transaction $txn.
2015-03-20 17:56:04 +03:00
2015-03-20 23:36:29 +03:00
=head3 $txn->set($db, 'key', 'value')
2015-03-20 17:56:04 +03:00
2015-03-20 23:36:29 +03:00
set a key in database $db as a part of transaction $txn.
2015-03-20 17:56:04 +03:00
2015-03-20 23:36:29 +03:00
=head3 $txn->delete($db, 'key')
2015-03-20 17:56:04 +03:00
2015-03-20 23:36:29 +03:00
delete a key in database $db as a part of transaction $txn.
2015-03-20 17:56:04 +03:00
2015-03-20 23:36:29 +03:00
=head3 $txn->commit
2015-03-20 17:56:04 +03:00
2015-03-20 23:36:29 +03:00
apply a transaction; if you want to rollback it instead of commiting,
just destroy all references to the $txn object
2015-03-20 17:56:04 +03:00
2015-03-20 23:36:29 +03:00
=head2 Database::Sophia::Snapshot
2015-03-20 17:56:04 +03:00
2015-03-20 23:36:29 +03:00
=head3 $snapshot->get($db, 'key')
2015-03-20 17:56:04 +03:00
2015-03-20 23:36:29 +03:00
get a key from the snapshot
2015-03-20 17:56:04 +03:00
2015-03-20 23:36:29 +03:00
=head3 $snapshot->drop
2015-03-20 17:56:04 +03:00
2015-03-20 23:36:29 +03:00
delete (drop) the snapshot
2015-03-20 17:56:04 +03:00
2015-03-20 23:36:29 +03:00
=head3 $snapshot->cursor($db, $starting_key, $order)
2015-03-20 17:56:04 +03:00
2015-03-20 23:36:29 +03:00
get an iterator over database keys in the snapshot, starting with $starting_key in the direction $order,
which may be one of '<', '<=', '>=', '>'
2015-03-20 17:56:04 +03:00
2015-03-20 23:36:29 +03:00
=head2 Database::Sophia::Cursor
2015-03-20 17:56:04 +03:00
2015-03-20 23:36:29 +03:00
=head3 $key = $cursor->cur_key
2015-03-20 17:56:04 +03:00
2015-03-20 23:36:29 +03:00
return the current key.
2015-03-20 17:56:04 +03:00
2015-03-20 23:36:29 +03:00
=head3 $value = $cursor->cur_value
2015-03-20 17:56:04 +03:00
2015-03-20 23:36:29 +03:00
return the current value.
2015-03-20 17:56:04 +03:00
2015-03-20 23:36:29 +03:00
=head3 $next_key = $cursor->next_key
2015-03-20 17:56:04 +03:00
2015-03-20 23:36:29 +03:00
go to the next key and return it; if there's no more keys, return undef
2015-03-20 17:56:04 +03:00
=head1 DESTROY
2015-03-20 23:36:29 +03:00
like usually in Perl, you just need to remove all references to any object to destroy it
2015-03-20 17:56:04 +03:00
=head1 AUTHOR
2015-03-20 23:36:29 +03:00
Vitaliy Filippov <vitalif@mail.ru>
2015-03-20 17:56:04 +03:00
=head1 COPYRIGHT AND LICENSE
2015-03-20 23:36:29 +03:00
This software is copyright (c) 2015 by Vitaliy Filippov.
2015-03-20 17:56:04 +03:00
This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.
See libsophia license and COPYRIGHT
http://sphia.org/
=cut