DR::Tnt - driver/connector for tarantool
use DR::Tnt; # exports 'tarantool' my $tnt = tarantool host => '1.2.3.4', port => 567, user => 'my_tnt_user', password => '#1@#$JHJH', hashify_tuples => 1, driver => 'sync', # default lua_dir => '/path/to/my/luas', reconnect_interval => 0.5 ; my $tuple = $tnt->get(space => 'index', [ 'key' ]); my $tuples = $tnt->select(myspace => 'myindex', [ 'key' ], $limit, $offset); my $updated = $tnt->update('myspace', [ 'key' ], [ [ '=', 1, 'name' ]]); my $inserted = $tnt->insert(myspace => [ 1, 2, 3, 4 ]); my $replaced = $tnt->replace(myspace => [ 1, 3, 4, 5 ]); my $tuples = $tnt->call_lua('box.space.myspace:select', [ 'key' ]); my $hashified_tuples = $tnt->call_lua([ 'box.space.myspace:select' => 'myspace' ], ['key' ]); my $removed = $tnt->delete(myspace => [ 'key' ]); my $tuples = $tnt->eval_lua('return 123'); my $hashify_tuples = $tnt->eval_lua(['return 123' => 'myspace' ]);
This module provides a synchronous and asynchronous driver for Tarantool.
The driver supports three work flow types:
The primary type, provides an asynchronous, callback-based API. Requires a running AnyEvent machine.
Synchronous driver (based on IO::Socket::INET/IO::Socket::UNIX).
Coro's driver, uses DR::Tnt::Client::AE.
The module does require and makes instance of selected driver.
Loads selected driver and returns connector.
You can choose one driver:
DR::Tnt::Client::Sync will be loaded and created.
DR::Tnt::Client::AE will be loaded and created.
DR::Tnt::Client::Coro will be loaded and created.
Connection point for tarantool instance. If host contains unix/, port have to contain valid unix path to opened socket.
unix/
Auth arguments.
Directory that contains some lua files. After connecting, the driver sends $tnt-eval_lua> for each file in the directory. So You can use the mechanizm to store some values to box.session.storage.
box.session.storage
If the option is set to TRUE, then the driver will extract tuples to hash by box.space._space schema.
TRUE
box.space._space
Internal to reconnect after disconnect or fatal errors. Undefined value disables the mechanizm.
The option is actual for coro and sync drivers (DR::Tnt::Client::Coro and DR::Tnt::Client::Sync).
coro
sync
Default value is TRUE. If TRUE, driver will unpack all strings as utf8-decoded strings.
utf8
Contains array of last error. If there was no errors, the attrubute contains undef.
undef
The array can contain two or three elements:
String error identifier. Example: ER_SOCKET or ER_REQUEST.
ER_SOCKET
ER_REQUEST
Error message. Example: 'Connection timeout'
Connection timeout
Tarantool code. Optional parameter. Example 0x806D. The code is present only for tarantool errors (like lua error, etc).
0x806D
All connectors have the same API. AnyEvent's connector has the last argument - callback for results.
If raise_error is false, coro and sync drivers will return undef and store last_error. Any successfuly call clears last_error attribute.
raise_error
false
last_error
# auth by $tnt->user and $tnt->password if ($tnt->auth) { } if ($tnt->auth($user, $password) { }
Auth user in tarantool. Note: The driver uses <$tnt-user>> and <$tnt-password>> attributes after reconnects.
<$tnt-
my $list = $tnt->select($space, $index, $key); my $list = $tnt->select($space, $index, $key, $limit, $offset, $iterator);
Select tuples from space. You can use space/index's names or numbers.
Default values for $iterator is 'EQ', for $limit is 2**32, for $offset is 0.
$iterator
'EQ'
$limit
2**32
$offset
0
my $tuple = $tnt->get($space, $index, $key);
The same as select, but forces $limit to 1, $offset to 0, $iterator to 'EQ' and returns the first tuple of result list.
select
1
my $updated = $tnt->update($space, $key, [[ '=', 3, 'name' ]]);
Update tuple in database.
my $inserted = $tnt->insert($space, [ $name, $value ]);
my $replaced = $tnt->replace($space, [ $name, $value ]);
my $deleted = $tnt->delete($space, $key);
my $tuples = $tnt->call_lua('my.lua.name', $arg1, $arg2); my $hashified_tuples = $tnt->call_lua(['box.space.name:select' => 'name'], 123);
If proc_name is ARRAYREF, result tuples will be hashified as tuples of selected space.
proc_name
ARRAYREF
my $tuples = $tnt->eval_lua('return {{1}}'); my $hashified_tuples = $tnt->eval_lua(['return {{1}}' => 'name');
if ($tnt->ping) { # connection established. }
To install DR::Tnt, copy and paste the appropriate command in to your terminal.
cpanm
cpanm DR::Tnt
CPAN shell
perl -MCPAN -e shell install DR::Tnt
For more information on module installation, please visit the detailed CPAN module installation guide.