Connectors

Perl connector

Install, connect, query and handle errors with the Raft DB Perl connector (core modules only).

The Perl connector speaks the Raft DB binary protocol (specification) and offers the same operations as every other connector. Download the package from the Download page.

No TLS yet. User names, passwords and tokens travel in clear text. Use only on trusted networks.

Requirements#

Perl 5 and core modules only (IO::Socket::INET, IO::Select, JSON::PP; JSON::PP is core since 5.14). One file: lib/RaftDB/Client.pm.

Install#

Download the tarball from the Download page and install it:

Shell
cpanm raftdb-perl-connector-<version>.tar.gz

or unpack it and run perl Makefile.PL && make && make install. Then use RaftDB::Client;.

Connect#

Perl
# options: user, password, token, timeout (seconds, default 30; applies to connect and every read)
my $db = RaftDB::Client->connect('127.0.0.1:7480', user => 'admin', password => 'pw');   # or token => '...' 

Query#

Perl
$db->put("u$_", { id => $_, name => "user $_", age => 20 + $_ % 50, score => $_ / 4 }) for 1 .. 100;

my $row = $db->get('u7');
print "u7: $row->{name}, age $row->{age}\n";
print "adults 60+: ", $db->count({ age => { '$gte' => 60 } }), "\n";
print "sql count : ", $db->count_sql('SELECT COUNT(*) FROM documents WHERE age >= 60'), "\n";

A page and an iterator (from examples.pl)#

Perl
my $page = $db->query({}, limit => 10);
print "first page: ", scalar @{ $page->{rows} }, " rows, next=$page->{next}\n";

my $it = $db->scan('SELECT id, name FROM documents WHERE age = 20 ORDER BY id');
while (defined(my $r = $it->())) { print "  scan: $r->{id} $r->{name}\n" }

Errors#

Server errors die with an RaftDB::Error object (->code, ->message; it stringifies as Raft DB error N: text). I/O failures, timeouts and calls on a closed client die with an ordinary string. After an I/O error, a timeout or code 7 the connection is closed.

Perl
eval { $db->count('{"age":'); 1 } or do {
    my $e = $@;
    if (ref $e && $e->isa('RaftDB::Error')) { print "server error ", $e->code, ": ", $e->message, "\n" } else { die $e }
};

Transactions and prepared statements#

The connector has no transaction call and no prepared-statement call: the protocol has none (every operation is one request and one reply, see the specification), and none of the connector's README, tests or examples uses one. Use the MySQL or MariaDB front-end with a standard driver for those: see Standard MySQL and MariaDB drivers.

Notes#

  • Perl does not distinguish 1.0 from 1: a hash value 1.0 is sent as the integer 1. The server is strict about integer versus double, so to store a whole-valued double pass the document as JSON text ('{"score":1.0}').
  • The optional UDP transport (SPEC section 10) is not implemented; pipelining is not used.
  • JSON::PP is slow on very large documents (a 1 MB string takes a second or two, as the README says).
  • scan returns an iterator (code ref): call it for each row, it returns undef at the end.

Conformance status#

24/24 conformance scenarios (28 checks) on Windows (Perl 5.38, msys) and on Linux (test server A). The scenarios are listed in the specification page. The package's README.txt has the install steps and a short example.

See also#