📜 ⬆️ ⬇️

PHP module for Tarantool

image Tarantool is a development of Mail.ru. Represents a high-performance key / value no-Sql solution. The lack of ports for popular programming languages ​​inhibits the popularity of this database. Tried to fill this gap.
The branch is experimental. Sources
Further examples.
A little bit about the database itself

Tarantoo l is a distributed key / value repository.
It should be noted that Tarantool is a data warehouse, and not a caching system like memcache. All data is in memory and, if necessary, stored in files (snap).

This storage, in contrast to the traditional memcached & redis storage, has additional features:

Tarantool handles data that is combined into namespaces. Namespace is an analogue of a table in MySQL. The system has organized digital numbering of neimspaces (0, 1, 2, 3 ...).
An index is superimposed on each namespace. Indices can be superimposed on a numeric (int32 or int64) or character field. As mentioned above: indexes can be of two types: HASH & TREE.
In the same way as with namespaces, digital indexing is defined in the system.

All indices are written in the config. Config less man perceive. The following config will be used in the examples below:
namespace [ 0 ] . enabled = 1
namespace [ 0 ] . index [ 0 ] . type = "HASH"
namespace [ 0 ] . index [ 0 ] . unique = 1
namespace [ 0 ] . index [ 0 ] . key_field [ 0 ] . fieldno = 0
namespace [ 0 ] . index [ 0 ] . key_field [ 0 ] . type = "NUM"
')
namespace [ 0 ] . index [ 1 ] . type = "TREE"
namespace [ 0 ] . index [ 1 ] . unique = 0
namespace [ 0 ] . index [ 1 ] . key_field [ 0 ] . fieldno = 1
namespace [ 0 ] . index [ 1 ] . key_field [ 0 ] . type = "STR"

namespace [ 0 ] . index [ 2 ] . type = "TREE"
namespace [ 0 ] . index [ 2 ] . unique = 0
namespace [ 0 ] . index [ 2 ] . key_field [ 0 ] . fieldno = 1
namespace [ 0 ] . index [ 2 ] . key_field [ 0 ] . type = "STR"
namespace [ 0 ] . index [ 2 ] . key_field [ 1 ] . fieldno = 2
namespace [ 0 ] . index [ 2 ] . key_field [ 1 ] . type = "STR"

namespace [ 0 ] . index [ 3 ] . type = "HASH"
namespace [ 0 ] . index [ 3 ] . unique = 0
namespace [ 0 ] . index [ 3 ] . key_field [ 0 ] . fieldno = 3
namespace [ 0 ] . index [ 3 ] . key_field [ 0 ] . type = "NUM"

Explanation:
one namespace is used, namespace = 0
The first index = 0 is always primary. The index type is HASH, superimposed on the numeric field = 0.
The second index = 1, type TREE, is superimposed on the field = 1, type string.
The third index = 2, composite, superimposed on the fields 1 and 2
The fourth index = 3 - superimposed on the numeric field 3

Installation


1) :
** git clone git@github.com:akalend/tarantool.git
master https://github.com/mailru/tarantool.git

2) cd connector/php

3) phpize

4) ./configure

5) sudo make install

6) php.ini extension :
extension=tarantool.so

Constructor

// constructor (host, port);
$ tnt = new Tarantool ( $ host , $ port ) ;

The constructor sets the connection parameters, But the connection itself occurs when the data is first accessed.
Parameters $ host, $ port are optional. But the default is $ host = 127.0.0.1, $ port = 33013. Also in Tarantool there is a secondary port 33014, configured only for reading.

Insert

Tarantool - operates on chunks of data called tuples.
In mathematics, a tuple is a sequence of a finite number of elements
In some programming languages ​​(for example, Python or Lisp), a tuple is a special type of data structure.
© Wikipedia
In PHP, a tuple is represented as an array.
$ tuple = array ( 1 , 'x' , 'abd' , 1023 ) ;
$ res = $ tnt -> insert ( 0 , $ tuple ) ;
var_dump ( $ res ) ;

The zero element of the array is inserted into the zero data field.
The number of elements in the array may be greater than the number of index fields. But in this case, sampling by these fields will be impossible. If the item already exists, it will be replaced with a new one.
Result true / false - the result of the operation INSERT

Sample

sampling, similar to RDBMS interfaces, is carried out in two stages: 1) select statement SELECT and 2) iteration on selection of rows, in our case tuples / tuple
Below is an example:
/ **
* Tarantool :: select
*
* @param number namespace
* @param number index No
* @param mixed key
* @param optional limit default all
* @param optional offset default 0
*
* @return count of tuples
* /
$ count = $ tnt -> select ( 0 , 0 , 1 ) ; // namespace, index, key
var_dump ( $ count ) ;
$ res = $ tnt -> getTuple ( ) ;
var_dump ( $ res ) ;

The given example selects data from namespace = 0, index = 0, key = 1. The result of the Select method is the number of selected tuples. The selection is made on the primary key. In this case, one tuple will be selected by the getTuple () method

If we have to select several data elements, then we need to organize a cycle:
echo "some tuple select by fielNo [1] = 'x' \ n " ;
$ count = $ tnt -> select ( 0 , 1 , 'x' ) ; // namespace, index, key

while ( ( $ res = $ tnt -> getTuple ( ) ) ! = false ) {
var_dump ( $ res ) ;
}

If you need to select by two criteria, for example, an analogue of the SQL statement: SELECT * FROM t WHERE f1 = 'msk' AND f2 = 'realty', then you need to use a composite index. In our example, this is index = 2. We look at the config.
echo "some tuple select by index = 2 (fielNo [1] = 'spb' and fielNo [2] = 'realry') \ n " ;
$ count = $ tnt -> select ( 0 , 2 , array ( 'spb' , 'realty' ) ) ; // namespace, index, key
while ( ( $ res = $ tnt -> getTuple ( ) ) ! = false ) {
var_dump ( $ res ) ;
}

Sample sample by number field (index = 3)
echo "some tuple select by index = 3 (fielNo [3] = 1025) \ n " ;
$ res = $ tnt -> select ( 0 , 3 , 1025 ) ; // namespace, index, key
var_dump ( $ res ) ;

while ( ( $ res = $ tnt -> getTuple ( ) ) ! = false ) {
var_dump ( $ res ) ;
}

If it is necessary to organize sampling in parts, then LIMIT / OFFSET parameters (analogue in MySQL) are provided. Below is an example of sampling 5 tuples starting from the 10th in the recordset.
echo "some tuple select by index = 3 (fielNo [3] = 1025) NEXT 5 (offset = 10, limit = 5) \ n " ;
$ tnt -> select ( 0 , 3 , 1025 , 5 , 10 ) ; // namespace, index, key, limit, offset
while ( ( $ res = $ tnt -> getTuple ( ) ) ! = false ) {
var_dump ( $ res ) ;
}


Data change

Currently, only two data modification methods are implemented: inc - data augmentation, update - data replacement. Both methods work only on the primary index, that is, only the primary index is used as the key.

Consider the simpler inc () method. This method increases (by default by 1) a NUM type field. Errors will occur on other types of fields.
/ **
* Tarantool :: inc
*
* @param namespace
* @param primary key
* @param field No for increment
* @param optional incremental data default +1
*
* @return bool result
* /
// increment
$ tnt -> inc ( 0 , 1 , 3 ) ;

$ tnt -> select ( 0 , 0 , 1 ) ;
$ tuple = $ tnt -> getTuple ( ) ;
var_dump ( $ tuple ) ;
If we want to decrease by 1, then we use:
$ tnt -> inc ( 0 , 1 , 3 , - 1 ) ;


The UPDATE method differs from INSERT in that UPDATE only changes the data fields we need, without touching the rest. The INSERT method implements, if the primary index data already exists, all fields of the tuple. Closer to body practice:
/ **
* Tarantool :: update
*
* @param namespace
* @param primary key
* @param array of new data:
* array (fieldNo => newValue, ...)
*
* @return bool result
* /
$ res = $ tnt -> update ( 0 , 1 , array ( 2 => 'y' ) ) ;
echo "update tuple res = $ res \ n " ;
The second parameter is the selection key, the third parameter is the associative array of data that must be replaced. The keys of the array are the numeric indices corresponding to the numbers of the fields that need to be replaced with the data that correspond to the given keys.
Restrictions

while only int32 indexes are implemented. int64 is not implemented.
UPDATE / AND UPDATE / XOR operations are not implemented

I hope that this module will be useful not only for my project. On the bugs I have noticed, please unsubscribe in a personal, or on the project page.

Source: https://habr.com/ru/post/122054/


All Articles