tokyo_tyrant

目录

tokyo_tyrant extension provides a wrapper for Tokyo Tyrant client libraries. The extension contains the normal key-value API and the table API.

"Tokyo Tyrant is a package of network interface to the DBM called Tokyo Cabinet. Though the DBM has high performance, you might bother in case that multiple processes share the same database, or remote processes access the database. Thus, Tokyo Tyrant is provided for concurrent and remote connections to Tokyo Cabinet. It is composed of the server process managing a database and its access library for client applications." --Tokyo Tyrant documentation

Tokyo Tyrant is written by Mikio Hirabayashi and is licensed under GNU Lesser General Public License.

安装/配置

目录

需求

Before using this extension Tokyo Cabinet and Tokyo Tyrant must be installed.

安装

安装此 PECL 扩展相关的信息可在手册中标题为 PECL 扩展的安装章节中找到。更多信息如新的发行版本、下载、源文件、 维护人员信息及变更日志等,都在此处: » https://pecl.php.net/package/tokyo_tyrant

Configure options

  • --with-tokyo-tyrant[=DIR] DIR is the prefix to the Tokyo Tyrant installation
  • --with-tokyo-cabinet-dir[=DIR] DIR is the prefix to the Tokyo Cabinet installation
  • --disable-tokyo-tyrant-session Disable Tokyo Tyrant session handler support

Enabling the extension

The extension can be enabled by adding extension=tokyo_tyrant.so to the INI-configuration

Running Tokyo Tyrant for the session handler

ttserver -port 2000 -ext /path/to/expire.lua -extpc expire 30.0 '/tmp/sessions.tct#idx=ts:dec'

Note: expire.lua is included in the tokyo_tyrant extension source distribution

Configuring session handler

  • tokyo_tyrant.session_salt="randomlongstring"
  • session.save_handler=tokyo_tyrant
  • session.save_path="tcp://hostname1:2000,tcp://hostname2:2000"

Note: It is important to make sure that tokyo_tyrant.session_salt matches on all servers.

How it works?

The session handler creates a session id like the following: 8b0e27a823fa4a6cf7246945b82c1d51-a5eadbbed1f2075952900628456bfd6830541629-0-5460

The parts from left to right:

  • Session id - Generated session id
  • Checksum - Checksum of session salt, session id, node id and primary key
  • Node id - The id of the node where the session maps to
  • Primary key - The primary key of the row where the session is stored

The checksum contains SHA1 sum of the node id, primary key, session id and the salt which is known only on the server side. This allows quick mapping of session id to node and primary key since there is no need to do an additional search. During session id regeneration only the parts 1 and 2 change but the mapping to the node and primary key stays constant.

In case some of the nodes fail tokyo_tyrant.allow_failover, tokyo_tyrant.fail_threshold and tokyo_tyrant.health_check_divisor INI-settings control the behavior during failover. If failover is allowed the session handler will map the session to a healthy node and creates a new empty session.

运行时配置

这些函数的行为受 php.ini 中的设置影响。

名字默认可修改范围更新日志
tokyo_tyrant.default_timeout2.0PHP_INI_ALL
tokyo_tyrant.session_saltNULLPHP_INI_ALL
tokyo_tyrant.key_prefixNULLPHP_INI_ALL
tokyo_tyrant.allow_failover1PHP_INI_ALL
tokyo_tyrant.fail_threshold5PHP_INI_ALL
tokyo_tyrant.health_check_divisor1000PHP_INI_ALL
tokyo_tyrant.php_expiration0PHP_INI_ALL

这是配置指令的简短说明。

tokyo_tyrant.default_timeout integer
Default timeout when connecting to databases

tokyo_tyrant.session_salt string
The secret that is used to salt session id

tokyo_tyrant.key_prefix string
Prefix all keys with this string. The prefix is transparent to the developer but helps making sure that keys won't collide if multiple applications are using the same database.

tokyo_tyrant.allow_failover integer
Whether to allow session failover in case a server dies.

tokyo_tyrant.fail_threshold integer
How many read/write or connection failures is allowed before server is marked as failed.

tokyo_tyrant.health_check_divisor integer
Defines the divisor for the health check probability. If there are failed servers and the probability matches, the servers are health checked and in case the server seems healthy, it will be added back to the pool.

tokyo_tyrant.php_expiration integer
Whether to use built in session expiration mechanism or delegate the expiration to a lua-script on the server-side.

资源类型

此扩展没有定义资源类型。

预定义常量

TokyoTyrant constants

范例

Basic usage

示例 #1 Putting and getting a key-value pair

<?php
$tt = new TokyoTyrant("localhost");
$tt->put("key", "value");
echo $tt->get("key");
?>

以上例程会输出:

value

简介

The main Tokyo Tyrant class

类摘要

TokyoTyrant

class TokyoTyrant {

/* 常量 */

const integer TokyoTyrant::RDBDEF_PORT = 1978 ;

const integer TokyoTyrant::RDBQC_STREQ = 0 ;

const integer TokyoTyrant::RDBQC_STRINC = 1 ;

const integer TokyoTyrant::RDBQC_STRBW = 2 ;

const integer TokyoTyrant::RDBQC_STREW = 3 ;

const integer TokyoTyrant::RDBQC_STRAND = 4 ;

const integer TokyoTyrant::RDBQC_STROR = 5 ;

const integer TokyoTyrant::RDBQC_STROREQ = 6 ;

const integer TokyoTyrant::RDBQC_STRRX = 7 ;

const integer TokyoTyrant::RDBQC_NUMEQ = 8 ;

const integer TokyoTyrant::RDBQC_NUMGT = 9 ;

const integer TokyoTyrant::RDBQC_NUMGE = 10 ;

const integer TokyoTyrant::RDBQC_NUMLT = 11 ;

const integer TokyoTyrant::RDBQC_NUMLE = 12 ;

const integer TokyoTyrant::RDBQC_NUMBT = 13 ;

const integer TokyoTyrant::RDBQC_NUMOREQ = 14 ;

const integer TokyoTyrant::RDBQC_NEGATE = 16777216 ;

const integer TokyoTyrant::RDBQC_NOIDX = 33554432 ;

const integer TokyoTyrant::RDBQO_STRASC = 0 ;

const integer TokyoTyrant::RDBQO_STRDESC = 1 ;

const integer TokyoTyrant::RDBQO_NUMASC = 2 ;

const integer TokyoTyrant::RDBQO_NUMDESC = 3 ;

const integer TokyoTyrant::RDBIT_LEXICAL = 0 ;

const integer TokyoTyrant::RDBIT_DECIMAL = 1 ;

const integer TokyoTyrant::RDBIT_TOKEN = 2 ;

const integer TokyoTyrant::RDBIT_QGRAM = 3 ;

const integer TokyoTyrant::RDBIT_OPT = 9998 ;

const integer TokyoTyrant::RDBIT_VOID = 9999 ;

const integer TokyoTyrant::RDBIT_KEEP = 16777216 ;

const integer TokyoTyrant::RDBQCFTS_PH = 15 ;

const integer TokyoTyrant::RDBQCFTS_AND = 16 ;

const integer TokyoTyrant::RDBQCFTS_OR = 17 ;

const integer TokyoTyrant::RDBQCFTS_EX = 18 ;

const integer TokyoTyrant::RDBXO_LCKREC = 1 ;

const integer TokyoTyrant::RDBXOLCK_GLB = 2 ;

const integer TokyoTyrant::RDBREC_INT = 1 ;

const integer TokyoTyrant::RDBREC_DBL = 2 ;

const integer TokyoTyrant::RDBMS_UNION = 0 ;

const integer TokyoTyrant::RDBMS_ISECT = 1 ;

const integer TokyoTyrant::RDBMS_DIFF = 2 ;

const integer TokyoTyrant::RDBT_RECON = 1 ;

/* 方法 */

public number add ( string $key , number $increment [, int $type = 0 ] )

public TokyoTyrant connect ( string $host [, int $port = TokyoTyrant::RDBDEF_PORT [, array $options ]] )

public TokyoTyrant connectUri ( string $uri )

public __construct ([ string $host [, int $port = TokyoTyrant::RDBDEF_PORT [, array $options ]]] )

public TokyoTyrant copy ( string $path )

public string ext ( string $name , int $options , string $key , string $value )

public array fwmKeys ( string $prefix , int $max_recs )

public mixed get ( mixed $keys )

public TokyoTyrantIterator getIterator ( void )

public int num ( void )

public TokyoTyrant out ( mixed $keys )

public TokyoTyrant put ( mixed $keys [, string $value = NULL ] )

public TokyoTyrant putCat ( mixed $keys [, string $value ] )

public TokyoTyrant putKeep ( mixed $keys [, string $value ] )

public TokyoTyrant putNr ( mixed $keys [, string $value = NULL ] )

public mixed putShl ( string $key , string $value , int $width )

public mixed restore ( string $log_dir , int $timestamp [, bool $check_consistency = TRUE ] )

public mixed setMaster ( string $host , int $port , int $timestamp [, bool $check_consistency = TRUE ] )

public int size ( string $key )

public array stat ( void )

public mixed sync ( void )

public TokyoTyrant tune ( float $timeout [, int $options = TokyoTyrant::RDBT_RECON ] )

public mixed vanish ( void )

}

预定义常量

TokyoTyrant Constants

TokyoTyrant::RDBDEF_PORT
The default port of the Tokyo Tyrant database

TokyoTyrant::RDBQC_STREQ
string is equal to

TokyoTyrant::RDBQC_STRINC
string is included in

TokyoTyrant::RDBQC_STRBW
string begins with

TokyoTyrant::RDBQC_STREW
string ends with

TokyoTyrant::RDBQC_STRAND
string includes all tokens in

TokyoTyrant::RDBQC_STROR
string includes at least one token in

TokyoTyrant::RDBQC_STROREQ
string is equal to at least one token in

TokyoTyrant::RDBQC_STRRX
string matches regular expressions of

TokyoTyrant::RDBQC_NUMEQ
number is equal to

TokyoTyrant::RDBQC_NUMGT
number is greater than

TokyoTyrant::RDBQC_NUMGE
number is greater than or equal to

TokyoTyrant::RDBQC_NUMLT
number is less than

TokyoTyrant::RDBQC_NUMLE
number is less than or equal to

TokyoTyrant::RDBQC_NUMBT
number is between two tokens of

TokyoTyrant::RDBQC_NUMOREQ
number is equal to at least one token in

TokyoTyrant::RDBQC_NEGATE
negation flag

TokyoTyrant::RDBQC_NOIDX
no index flag

TokyoTyrant::RDBQO_STRASC
string ascending

TokyoTyrant::RDBQO_STRDESC
string descending

TokyoTyrant::RDBQO_NUMASC
number ascending

TokyoTyrant::RDBQO_NUMDESC
number descending

TokyoTyrant::RDBIT_LEXICAL
lexical string

TokyoTyrant::RDBIT_DECIMAL
decimal string

TokyoTyrant::RDBIT_TOKEN
token inverted index (Tokyo Tyrant >= 1.1.29)

TokyoTyrant::RDBIT_QGRAM
QGRAM inverted index (Tokyo Tyrant >= 1.1.29)

TokyoTyrant::RDBIT_OPT
optimize

TokyoTyrant::RDBIT_VOID
void

TokyoTyrant::RDBIT_KEEP
keep existing index

TokyoTyrant::RDBQCFTS_PH
full-text search with the phrase of (Tokyo Tyrant >= 1.1.29)

TokyoTyrant::RDBQCFTS_AND
full-text search with all tokens in (Tokyo Tyrant >= 1.1.29)

TokyoTyrant::RDBQCFTS_OR
full-text search with at least one token in (Tokyo Tyrant >= 1.1.29)

TokyoTyrant::RDBQCFTS_EX
full-text search with the compound expression of (Tokyo Tyrant >= 1.1.29)

TokyoTyrant::RDBQCFTS_AND
Metasearch union between records (Tokyo Tyrant >= 1.1.33)

TokyoTyrant::RDBQCFTS_OR
Metasearch intersection between records (Tokyo Tyrant >= 1.1.33)

TokyoTyrant::RDBQCFTS_EX
Metasearch difference between records (Tokyo Tyrant >= 1.1.33)

TokyoTyrant::RDBT_RECON
Whether to reconnect on connection failure. It is recommended to have this parameter on for persistent connections

TokyoTyrant::RDBXOLCK_REC
record locking

TokyoTyrant::RDBXOLCK_GLB
global locking

TokyoTyrant::RDBREC_INT
record type int

TokyoTyrant::RDBREC_DBL
record type float (double)

TokyoTyrant::TTE_SUCCESS
success

TokyoTyrant::TTE_INVALID
invalid operation

TokyoTyrant::TTE_NOHOST
host not found

TokyoTyrant::TTE_REFUSED
connection refused

TokyoTyrant::TTE_SEND
send error

TokyoTyrant::TTE_RECV
recv error

TokyoTyrant::TTE_KEEP
record exist

TokyoTyrant::TTE_NOREC
no record found

TokyoTyrant::TTE_MISC
miscellaneous error

TokyoTyrant::add

Adds to a numeric key

说明

public number TokyoTyrant::add ( string $key , number $increment [, int $type = 0 ] )

Adds to an int or double value. This increments the value by the given amount and returns the new value. If the key does not exist a new key is created with initial value of the increment parameter.

参数

key
The string key

increment
The amount to increment

type
TokyoTyrant::RDBREC_INT or TokyoTyrant::RDBREC_DBL constant. If this parameter is omitted the type is guessed from the increment parameters type.

返回值

Returns the new value on success

范例

示例 #1 TokyoTyrant::add example

<?php
$tt = new TokyoTyrant("localhost", TokyoTyrant::RDBDEF_PORT);
/* Adds integer 3 to key and creates a new key */
$tt->add("test", 3);

/* String value is converted to double */
echo $tt->add("test", "3.5", TokyoTyrant::RDBREC_DBL);
?>

以上例程的输出类似于:

6.5

参见

  • TokyoTyrant::put
  • TokyoTyrant::putcat
  • TokyoTyrant::putkeep

TokyoTyrant::connect

Connect to a database

说明

public TokyoTyrant TokyoTyrant::connect ( string $host [, int $port = TokyoTyrant::RDBDEF_PORT [, array $options ]] )

Connects to a remote database

参数

host
The hostname

port
The port. Default: 1978

options
Connection options: timeout (default: 5.0), reconnect (default: TRUE) and persistent (default: TRUE)

返回值

This method returns the current object and throws TokyoTyrantException on failure.

范例

示例 #1 TokyoTyrant::connect example

<?php
$tt = new TokyoTyrant();
$tt->connect("localhost", TokyoTyrant::RDBDEF_PORT)->put("test", "value");
?>

参见

  • TokyoTyrant::connectUri

TokyoTyrant::connectUri

Connects to a database

说明

public TokyoTyrant TokyoTyrant::connectUri ( string $uri )

Connects to a database using an uri

参数

uri
An URI to the database. For example tcp://localhost:1979/

返回值

This method returns the current object and throws TokyoTyrantException on failure.

范例

示例 #1 TokyoTyrant::connectUri example

<?php
$tt = new TokyoTyrant();
$tt->connectUri("tcp://localhost:1978/")->put("test", "hi");
?>

参见

  • TokyoTyrant::connect

TokyoTyrant::__construct

Construct a new TokyoTyrant object

说明

public TokyoTyrant::__construct ([ string $host [, int $port = TokyoTyrant::RDBDEF_PORT [, array $options ]]] )

Constructs a new TokyoTyrant object and optionally connects to the database

参数

host
The hostname. Default: NULL

port
port number. Default: 1978

options
Connection options: timeout (default: 5.0), reconnect (default: TRUE) and persistent (default: TRUE)

返回值

Throws TokyoTyrantException if connection to database fails

参见

  • TokyoTyrant::connect
  • TokyoTyrant::connectUri

TokyoTyrant::copy

Copies the database

说明

public TokyoTyrant TokyoTyrant::copy ( string $path )

Makes a copy of the current database

参数

path
Path to where to copy the database. The user running the remote database must have a write access to the directory.

返回值

This method returns the current object and throws TokyoTyrantException on failure.

范例

示例 #1 TokyoTyrant::copy example

<?php
$tt = new TokyoTyrant("localhost", 1978);
$tt->copy("/tmp/foobar.tct");
?>

参见

  • TokyoTyrant::restore

TokyoTyrant::ext

Execute a remote script

说明

public string TokyoTyrant::ext ( string $name , int $options , string $key , string $value )

Executes a remote script extension.

参数

name
Name of the function to execute

options
Either TokyoTyrant::RDBXO_LCKREC for record locking and TokyoTyrant::RDBXO_LCKGLB for global locking.

key
The key to pass to the function

value
The value to pass to the function

返回值

Returns the result of the script function

范例

示例 #1 TokyoTyrant::ext example

<?php
$tt = new TokyoTyrant("localhost", 1978);
echo $tt->ext("somefunc", TokyoTyrant::RDBXO_LCKREC, "some_key", "some_value");
?>

TokyoTyrant::fwmKeys

Returns the forward matching keys

说明

public array TokyoTyrant::fwmKeys ( string $prefix , int $max_recs )

Returns the forward matching keys from the database

参数

prefix
Prefix of the keys

max_recs
Maximum records to return

返回值

Returns an array of matching keys. The values are not returned

范例

示例 #1 TokyoTyrant::fwmKeys example

<?php

$tt = new TokyoTyrant("localhost");

/* Create 20 macthing keys */
for ($i = 0; $i < 20; $i++) {
    $tt->put("key_" . $i, "value_" . $i);
}

/* Create 20 non-macthing keys */
for ($i = 0; $i < 20; $i++) {
    $tt->put("something_" . $i, "data_" . $i);
}

/* Get five matching keys */
var_dump($tt->fwmKeys("key_", 5));
?>

以上例程的输出类似于:

array(5) {
  [0]=>
  string(5) "key_5"
  [1]=>
  string(6) "key_14"
  [2]=>
  string(5) "key_6"
  [3]=>
  string(6) "key_15"
  [4]=>
  string(5) "key_7"
}

TokyoTyrant::get

The get purpose

说明

public mixed TokyoTyrant::get ( mixed $keys )

This method is used to return a value or multiple values. This method accepts a string or an array as a value.

参数

keys
A string key or an array of string keys

返回值

Returns a string or an array based on the given parameters. Throws a TokyoTyrantException on error. If string is passed null is returned if the key is not found. In case an array is given as an parameter only existing keys will be returned. It is not an error to pass a key that does not exist.

范例

示例 #1 TokyoTyrant::get example

<?php
$tt = new TokyoTyrant("localhost", 1978);
$tt->put("key1", "value1");
$tt->put("key2", "value2");
var_dump($tt->get(array("key1", "key2")));
var_dump($tt->get("key1"));
?>

以上例程会输出:

array(2) {
  ["key1"]=>
  string(6) "value1"
  ["key2"]=>
  string(6) "value2"
}
string(6) "value1"

参见

  • TokyoTyrant::put

TokyoTyrant::getIterator

Get an iterator

说明

public TokyoTyrantIterator TokyoTyrant::getIterator ( void )

Gets an iterator for iterating all keys / values in the database.

参数

此函数没有参数。

返回值

This method returns TokyoTyrantIterator object and throws TokyoTyrantException on failure.

范例

示例 #1 TokyoTyrant::getIterator example

<?php
$tt = new TokyoTyrant("localhost");
$it = $tt->getIterator();

foreach ($it as $k => $v) {

}
?>

参见

  • TokyoTyrantTable::getQuery
  • TokyoTyrantTable::getIterator

TokyoTyrant::num

Number of records in the database

说明

public int TokyoTyrant::num ( void )

Returns the number of records in the database

参数

此函数没有参数。

返回值

Returns number of records in the database

范例

示例 #1 TokyoTyrant::num example

<?php
/* Connect to a database on default port */
$tt = new TokyoTyrant("localhost");

/* Echo the number of records */
echo $tt->num();
?>

以上例程的输出类似于:

1234

参见

  • TokyoTyrant::stat

TokyoTyrant::out

Removes records

说明

public TokyoTyrant TokyoTyrant::out ( mixed $keys )

Removes a record or multiple records. This method accepts a string for a single key or an array of keys for multiple records.

参数

keys
A string key or an array of string keys

返回值

This method returns the current object and throws TokyoTyrantException on failure.

范例

示例 #1 TokyoTyrant::out example

<?php
/* Connect to a database on default port */
$tt = new TokyoTyrant("localhost");

$tt->put("test1", "value1");
$tt->put("test2", "value2");

$tt->out(array("test1", "test2"));
?>

参见

  • TokyoTyrant::put

TokyoTyrant::put

Puts values

说明

public TokyoTyrant TokyoTyrant::put ( mixed $keys [, string $value = NULL ] )

Puts a key-value pair into the database or multiple key-value pairs. If keys is string then the second parameter value defines the value. The second parameter is mandatory if keys is a string. If the key exists the value will be replaced with new value.

参数

keys
A string key or an array of key-value pairs

value
The value in case a string key is used

返回值

This method returns a reference to the current object and throws TokyoTyrantException on failure.

范例

示例 #1 TokyoTyrant::put example

<?php
/* Connect to a database on default port */
$tt = new TokyoTyrant("localhost");

/* Put single key-value pair */
$tt->put("key", "value");

/* Put key-value pairs, new value overwrites the old */
$tt->put(array("key1" => "value1", "key" => "value2"));

/* Get back one key */
echo $tt->get("key");
?>

以上例程会输出:

value2

参见

  • TokyoTyrant::putKeep
  • TokyoTyrant::putCat

TokyoTyrant::putCat

Concatenates to a record

说明

public TokyoTyrant TokyoTyrant::putCat ( mixed $keys [, string $value ] )

Appends a value into existing key or multiple values if keys is an array. The second parameter is mandatory if keys is a string. If the record does not exist a new record is created.

参数

keys
A string key or an array of key-value pairs

value
The value in case a string key is used

返回值

This method returns a reference to the current object and throws TokyoTyrantException on failure.

范例

示例 #1 TokyoTyrant::putCat example

<?php
/* Connect to a database on default port */
$tt = new TokyoTyrant("localhost");

/* Create a new key */
$tt->put("key", "value");

/* Concatenate single key-value pair */
$tt->putCat("key", " has more data");

/* Echo the key */
echo $tt->get("key");
?>

以上例程会输出:

value has more data

参见

  • TokyoTyrant::put
  • TokyoTyrant::putKeep

TokyoTyrant::putKeep

Puts a record

说明

public TokyoTyrant TokyoTyrant::putKeep ( mixed $keys [, string $value ] )

Puts a key-value pair into the database or multiple key-value pairs. If keys is string then the second parameter value defines the value. The second parameter is mandatory if keys is a string. If the key already exists this method throws an exception indicating that the records exists.

参数

keys
A string key or an array of key-value pairs

value
The string value

返回值

This method returns a reference to the current object and throws TokyoTyrantException on failure.

范例

示例 #1 tokyotyrant::putKeep example

<?php
/* Connect to a database on default port */
$tt = new TokyoTyrant("localhost");

/* Create a new key */
$tt->put("key", "value");

try {
    $tt->putKeep("key", "new value");
} catch (TokyoTyrantException $e) {
    if ($e->getCode() === TokyoTyrant::TTE_KEEP) {
        echo "Existing record! Not modified\n";
    } else {
        echo "Error: " , $e->getMessage() , "\n"; 
    }
}
echo $tt->get("key");
?>

以上例程会输出:

Existing record! Not modified
value

参见

  • TokyoTyrant::put
  • TokyoTyrant::putCat

TokyoTyrant::putNr

Puts value

说明

public TokyoTyrant TokyoTyrant::putNr ( mixed $keys [, string $value = NULL ] )

Puts a key-value pair into the database or multiple key-value pairs. If keys is string then the second parameter value defines the value. The second parameter is mandatory if keys is a string. This method does not wait for the response from the server.

参数

keys
A string key or an array of key-value pairs

value
The value in case a string key is used

返回值

This method returns a reference to the current object and throws TokyoTyrantException on failure.

范例

示例 #1 TokyoTyrant::putNr example

<?php
/* Connect to a database on default port */
$tt = new TokyoTyrant("localhost");

/* Put single key-value pair */
$tt->putNr("key", "value");

/* Put key-value pairs */
$tt->putNr(array("key1" => "value1", "key2" => "value2"));

/* Get back one key */
echo $tt->get("key1");
?>

以上例程会输出:

value1

参见

  • TokyoTyrant::putNr
  • TokyoTyrant::putKeep
  • TokyoTyrant::putCat

TokyoTyrant::putShl

Concatenates to a record

说明

public mixed TokyoTyrant::putShl ( string $key , string $value , int $width )

Concatenate to a record and shift to left.

参数

key
A string key

value
The value to concatenate

width
The width of the record

返回值

This method returns a reference to the current object and throws TokyoTyrantException on failure.

范例

示例 #1 TokyoTyrant::putShl example

<?php
/* Connect to a database on default port */
$tt = new TokyoTyrant("localhost");

/* Create a new key */
$tt->put("key", "just a long piece of data");

/* Concatenate and shift to left */
$tt->putShl("key", " and string", 15);

/* Echo the key */
echo $tt->get("key");
?>

以上例程会输出:

data and string

参见

  • TokyoTyrant::put
  • TokyoTyrant::putKeep
  • TokyoTyrant::putCat

TokyoTyrant::restore

Restore the database

说明

public mixed TokyoTyrant::restore ( string $log_dir , int $timestamp [, bool $check_consistency = TRUE ] )

Restore the database from the update log.

Warning

This method is not supported on 32bit platforms.

参数

log_dir
Directory where the log is

timestamp
Beginning timestamp with microseconds

check_consistency
Whether to check consistency: Default: TRUE

返回值

This method returns the current object and throws TokyoTyrantException on failure.

TokyoTyrant::setMaster

Set the replication master

说明

public mixed TokyoTyrant::setMaster ( string $host , int $port , int $timestamp [, bool $check_consistency = TRUE ] )

Sets the replication master of the database

Warning

This method is not supported on 32bit platforms.

参数

host
Hostname of the replication master. If NULL the replication is disabled.

port
Port of the replication master

timestamp
Beginning timestamp with microseconds

check_consistency
Whether to check consistency.

返回值

This method returns the current object and throws TokyoTyrantException on failure.

范例

示例 #1 TokyoTyrant::setMaster example

<?php
/* Connect to a database */
$tt = new TokyoTyrant("tokyotyrant.example.com");

/* Disable the replication */
$tt->setMaster(NULL, 0, 0);
?>

参见

  • Classname::Method

TokyoTyrant::size

Returns the size of the value

说明

public int TokyoTyrant::size ( string $key )

Returns the size of a value by key

参数

key
The key of which size to fetch

返回值

Returns the size of the key or throw TokyoTyrantException on error

范例

示例 #1 TokyoTyrant::size example

<?php
$tt = new TokyoTyrant("localhost");

$tt->put("test_key", "12345");

echo $tt->size("test_key");
?>

以上例程会输出:

5

TokyoTyrant::stat

Get statistics

说明

public array TokyoTyrant::stat ( void )

Returns statistics of the remote database

参数

此函数没有参数。

返回值

Returns an array of key value pairs describing the statistics

范例

示例 #1 TokyoTyrant::stat example

<?php
$tt = new TokyoTyrant("localhost");

var_dump($tt->stat());
?>

以上例程的输出类似于:

array(19) {
  ["version"]=>
  string(6) "1.1.28"
  ["libver"]=>
  string(3) "311"
  ["protver"]=>
  string(4) "0.91"
  ["os"]=>
  string(5) "Linux"
  ["time"]=>
  string(17) "1247358357.665630"
  ["pid"]=>
  string(5) "14348"
  ["sid"]=>
  string(8) "59025947"
  ["type"]=>
  string(9) "on-memory"
  ["path"]=>
  string(1) "*"
  ["rnum"]=>
  string(1) "4"
  ["size"]=>
  string(6) "262856"
  ["bigend"]=>
  string(1) "0"
  ["fd"]=>
  string(1) "5"
  ["loadavg"]=>
  string(8) "0.000000"
  ["memsize"]=>
  string(8) "77328384"
  ["memrss"]=>
  string(7) "1183744"
  ["ru_real"]=>
  string(13) "162776.042152"
  ["ru_user"]=>
  string(8) "0.476029"
  ["ru_sys"]=>
  string(8) "8.652540"
}

参见

  • Classname::Method

TokyoTyrant::sync

Synchronize the database

说明

public mixed TokyoTyrant::sync ( void )

Synchronizes the database on to the physical device

参数

此函数没有参数。

返回值

This method returns the current object and throws TokyoTyrantException on failure.

TokyoTyrant::tune

Tunes connection values

说明

public TokyoTyrant TokyoTyrant::tune ( float $timeout [, int $options = TokyoTyrant::RDBT_RECON ] )

Tunes database connection options.

参数

timeout
The objects timeout value (default: 5.0)

options
Bitmask of options to tune. This can be either 0 or TokyoTyrant::RDBT_RECON. It is recommended not to change the second parameter.

返回值

This method returns a reference to the current object and throws TokyoTyrantException on failure.

TokyoTyrant::vanish

Empties the database

说明

public mixed TokyoTyrant::vanish ( void )

Empties a remote database

参数

此函数没有参数。

返回值

This method returns the current object and throws TokyoTyrantException on failure.

范例

示例 #1 TokyoTyrant::vanish example

<?php
$tt = new TokyoTyrant("localhost");
$tt->vanish();
?>

参见

  • TokyoTyrant::out

简介

Provides an API to the table databases. A table database can be create using the following command: ttserver -port 1979 /tmp/tt_table.tct. In Tokyo Tyrant the table API is a schemaless database which can store arbitrary amount of key-value pairs under a single primary key.

类摘要

TokyoTyrantTable

class TokyoTyrantTable extends TokyoTyrant {

/* 方法 */

public void add ( string $key , mixed $increment [, string $type ] )

public int genUid ( void )

public array get ( mixed $keys )

public TokyoTyrantIterator getIterator ( void )

public TokyoTyrantQuery getQuery ( void )

public void out ( mixed $keys )

public int put ( string $key , array $columns )

public void putCat ( string $key , array $columns )

public void putKeep ( string $key , array $columns )

public void putNr ( mixed $keys [, string $value ] )

public void putShl ( string $key , string $value , int $width )

public mixed setIndex ( string $column , int $type )

/* 继承的方法 */

public number TokyoTyrant::add ( string $key , number $increment [, int $type = 0 ] )

public TokyoTyrant TokyoTyrant::connect ( string $host [, int $port = TokyoTyrant::RDBDEF_PORT [, array $options ]] )

public TokyoTyrant TokyoTyrant::connectUri ( string $uri )

public TokyoTyrant::__construct ([ string $host [, int $port = TokyoTyrant::RDBDEF_PORT [, array $options ]]] )

public TokyoTyrant TokyoTyrant::copy ( string $path )

public string TokyoTyrant::ext ( string $name , int $options , string $key , string $value )

public array TokyoTyrant::fwmKeys ( string $prefix , int $max_recs )

public mixed TokyoTyrant::get ( mixed $keys )

public TokyoTyrantIterator TokyoTyrant::getIterator ( void )

public int TokyoTyrant::num ( void )

public TokyoTyrant TokyoTyrant::out ( mixed $keys )

public TokyoTyrant TokyoTyrant::put ( mixed $keys [, string $value = NULL ] )

public TokyoTyrant TokyoTyrant::putCat ( mixed $keys [, string $value ] )

public TokyoTyrant TokyoTyrant::putKeep ( mixed $keys [, string $value ] )

public TokyoTyrant TokyoTyrant::putNr ( mixed $keys [, string $value = NULL ] )

public mixed TokyoTyrant::putShl ( string $key , string $value , int $width )

public mixed TokyoTyrant::restore ( string $log_dir , int $timestamp [, bool $check_consistency = TRUE ] )

public mixed TokyoTyrant::setMaster ( string $host , int $port , int $timestamp [, bool $check_consistency = TRUE ] )

public int TokyoTyrant::size ( string $key )

public array TokyoTyrant::stat ( void )

public mixed TokyoTyrant::sync ( void )

public TokyoTyrant TokyoTyrant::tune ( float $timeout [, int $options = TokyoTyrant::RDBT_RECON ] )

public mixed TokyoTyrant::vanish ( void )

}

TokyoTyrantTable::add

Adds a record

说明

public void TokyoTyrantTable::add ( string $key , mixed $increment [, string $type ] )

This method is not supported with table databases.

参数

key
The string key

increment
The amount to increment

type
TokyoTyrant::RDB_RECINT or TokyoTyrant::RDB_RECDBL constant. If this parameter is omitted the type is guessed from the increment parameters type.

返回值

This method throws an TokyoTyrantException if used through this class.

参见

  • TokyoTyrant::add

TokyoTyrantTable::genUid

Generate unique id

说明

public int TokyoTyrantTable::genUid ( void )

Generates an unique id inside the table database. In table databases rows are referenced using a numeric primary key.

参数

此函数没有参数。

返回值

Returns an unique id or throws TokyoTyrantException on error

范例

示例 #1 TokyoTyrantTable::genUid example

<?php
$tt = new TokyoTyrantTable("localhost", 1122);

echo $tt->genUid();
?>

以上例程的输出类似于:

4

参见

  • TokyoTyrantTable::put

TokyoTyrantTable::get

Get a row

说明

public array TokyoTyrantTable::get ( mixed $keys )

Gets a row from table database. keys is a single integer for the primary key of the row or an array of integers for multiple rows.

参数

keys
The primary key, can be a string or an integer

返回值

Returns the row as an array

范例

示例 #1 TokyoTyrantTable::get example

<?php
/* Connect to a table database */
$tt = new TokyoTyrantTable("localhost", 1979);

/* Passing null to put generates a new uid */
$index = $tt->put(null, array("column1" => "some data", "column2" => "more data"));

/* Get the row back */
var_dump($tt->get($index));
?>

以上例程会输出:

array(2) {
  ["column1"]=>
  string(9) "some data"
  ["column2"]=>
  string(9) "more data"
}

注释

Note:

Starting from version 0.3.0 this method accepts array as parameter. Please note that multi-get on tables is not binary-safe.

参见

  • TokyoTyrantTable::put

TokyoTyrantTable::getIterator

Get an iterator

说明

public TokyoTyrantIterator TokyoTyrantTable::getIterator ( void )

Gets an iterator for iterating all keys / values in the database.

参数

此函数没有参数。

返回值

This method returns TokyoTyrantIterator object and throws TokyoTyrantException on failure.

范例

示例 #1 TokyoTyrantTable::getIterator example

<?php
$tt = new TokyoTyrantTable("localhost");
$it = $tt->getIterator();

foreach ($it as $k => $v) {
  var_dump($k, $v);
}
?>

参见

  • TokyoTyrantTable::getQuery
  • TokyoTyrant::getIterator

TokyoTyrantTable::getQuery

Get a query object

说明

public TokyoTyrantQuery TokyoTyrantTable::getQuery ( void )

Get a query object to execute searches on the database

参数

此函数没有参数。

返回值

Returns TokyoTyrantQuery on success and throws TokyoTyrantException on error

范例

示例 #1 TokyoTyrantTable::getQuery example

<?php
/* Connect to a table database */
$table = new TokyoTyrantTable("localhost", 1979);

/* Put a few rows */
$table->put(null, array("column1" => "some data", "column2" => "more data"));
$table->put(null, array("something" => "value", "data" => "good data"));

/* Get query object */
$query = $table->getQuery();

/* Add condition to query */
$query->addCond('data', TokyoTyrant::RDBQC_STREQ, 'good data');

/* Get matching rows */
var_dump($query->search());
?>

以上例程的输出类似于:

array(1) {
  [11]=>
  array(2) {
    ["something"]=>
    string(5) "value"
    ["data"]=>
    string(9) "good data"
  }
}

参见

  • TokyoTyrantQuery::search
  • TokyoTyrantQuery::out

TokyoTyrantTable::out

Remove records

说明

public void TokyoTyrantTable::out ( mixed $keys )

Removes records from a table database.

参数

keys
A single integer key or an array of integers

返回值

This method returns the current object and throws TokyoTyrantException on failure.

范例

示例 #1 TokyoTyrantTable::out example

<?php
/* Connect to a table database */
$tt = new TokyoTyrantTable("localhost", 1979);

/* Passing null to put generates a new uid */
$index = $tt->put(null, array("column1" => "some data", "column2" => "more data"));

/* Delete the row */
$tt->out($index);
?>

参见

  • TokyoTyrantTable::put

TokyoTyrantTable::put

Store a row

说明

public int TokyoTyrantTable::put ( string $key , array $columns )

Puts a new row into the database. This method parameters are key which is the primary key of the row, passing NULL will generate a new unique id. value is an array containing the row contents which is usually key value pairs.

参数

key
The primary key of the row

columns
The row contents

返回值

Returns the primary key on success and throws TokyoTyrantException on error

范例

示例 #1 TokyoTyrantTable::put example

<?php
/* Connect to a table database */
$tt = new TokyoTyrantTable("localhost", 1979);

/* Passing null to put generates a new uid */
$index = $tt->put(null, array("column1" => "some data", "column2" => "more data"));

/* Get the row back */
var_dump($tt->get($index));

/* Modify an existing row */
$tt->put($index, array("column1" => "other data", "column2" => "better data"));

/* Get the row back */
var_dump($tt->get($index));
?>

以上例程会输出:

array(2) {
  ["column1"]=>
  string(9) "some data"
  ["column2"]=>
  string(9) "more data"
}
array(2) {
  ["column1"]=>
  string(10) "other data"
  ["column2"]=>
  string(11) "better data"
}

参见

  • TokyoTyrantTable::get

TokyoTyrantTable::putCat

Concatenates to a row

说明

public void TokyoTyrantTable::putCat ( string $key , array $columns )

This method can be used to add new columns to existing records. Existing keys will be left unmodified but any new columns will be appended to the row. Passing null as key will generate a new row.

参数

key
The primary key of the row or NULL

columns
Array of row contents

返回值

Returns the primary key and throws TokyoTyrantException on error.

范例

示例 #1 TokyoTyrantTable::putCat example

<?php
/* Connect to a table database */
$tt = new TokyoTyrantTable("localhost", 1979);

/* Passing null to put generates a new uid */
$index = $tt->put(null, array("column1" => "some data", "column2" => "more data"));

/* Get the row back */
var_dump($tt->get($index));

/* Modify an existing row */
$tt->putcat($index, array("column1" => "something new", "new_column" => "other data"));

/* Get the row back */
var_dump($tt->get($index));
?>

以上例程会输出:

array(2) {
  ["column1"]=>
  string(9) "some data"
  ["column2"]=>
  string(9) "more data"
}
array(3) {
  ["column1"]=>
  string(9) "some data"
  ["column2"]=>
  string(9) "more data"
  ["new_column"]=>
  string(10) "other data"
}

参见

  • TokyoTyrantTable::put

TokyoTyrantTable::putKeep

Put a new record

说明

public void TokyoTyrantTable::putKeep ( string $key , array $columns )

Puts a new record into the database. If the key already exists this method throws an exception indicating that the records exists.

参数

key
The primary key of the row or NULL

columns
Array of the row contents

返回值

Returns the primary key and throws TokyoTyrantException on error.

范例

示例 #1 TokyoTyrantTable::putKeep example

<?php
/* Connect to a table database */
$tt = new TokyoTyrantTable("localhost", 1979);

/* Passing null to put generates a new uid */
$index = $tt->put(null, array("column1" => "some data", "column2" => "more data"));

/* Get the row back */
var_dump($tt->get($index));

try {
    $tt->putKeep($index, array("column1" => "something new", "new_column" => "other data"));
} catch (TokyoTyrantException $e) {
    if ($e->getCode() === TokyoTyrant::TTE_KEEP) {
        echo "Existing record! Not modified\n";
    } else {
        echo "Error: " , $e->getMessage() , "\n"; 
    }
}

/* Get the row back */
var_dump($tt->get($index));
?>

以上例程的输出类似于:

array(2) {
  ["column1"]=>
  string(9) "some data"
  ["column2"]=>
  string(9) "more data"
}
Existing record! Not modified
array(2) {
  ["column1"]=>
  string(9) "some data"
  ["column2"]=>
  string(9) "more data"
}

参见

  • TokyoTyrantTable::put

TokyoTyrantTable::putNr

Puts value

说明

public void TokyoTyrantTable::putNr ( mixed $keys [, string $value ] )

This method is not supported on table databases. Calling this method through TokyoTyrantTable is considered an error and an TokyoTyrantException will be thrown.

参数

keys
A string key or an array of key-value pairs

value
The value in case a string key is used

返回值

This method is not supported on table databases. Calling this method through TokyoTyrantTable is considered an error and an TokyoTyrantException will be thrown.

参见

  • TokyoTyrant::putNr
  • TokyoTyrant::putKeep
  • TokyoTyrant::putCat

TokyoTyrantTable::putShl

Concatenates to a record

说明

public void TokyoTyrantTable::putShl ( string $key , string $value , int $width )

This method is not supported on table databases. Calling this method through TokyoTyrantTable is considered an error and an TokyoTyrantException will be thrown.

参数

key
A string key

value
The value to concatenate

width
The width of the record

返回值

This method is not supported on table databases.

参见

  • TokyoTyrant::put
  • TokyoTyrant::putKeep
  • TokyoTyrant::putCat
  • TokyoTyrantTable::put
  • TokyoTyrantTable::putKeep
  • TokyoTyrantTable::putCat

TokyoTyrantTable::setIndex

Sets index

说明

public mixed TokyoTyrantTable::setIndex ( string $column , int $type )

Sets an index on a specified column. The index type is one of the TokyoTyrant::RDBIT_* constants. Passing TokyoTyrant::RDBIT_VOID removes the index.

参数

column
The name of the column

type
The index type

返回值

This method returns the current object and throws TokyoTyrantException on failure.

简介

This class is used to query the table databases

类摘要

TokyoTyrantQuery

class TokyoTyrantQuery implements Iterator {

/* 方法 */

public mixed addCond ( string $name , int $op , string $expr )

public __construct ( TokyoTyrantTable $table )

public int count ( void )

public array current ( void )

public string hint ( void )

public string key ( void )

public array metaSearch ( array $queries , int $type )

public array next ( void )

public TokyoTyrantQuery out ( void )

public bool rewind ( void )

public array search ( void )

public mixed setLimit ([ int $max [, int $skip ]] )

public mixed setOrder ( string $name , int $type )

public bool valid ( void )

}

TokyoTyrantQuery::addCond

Adds a condition to the query

说明

public mixed TokyoTyrantQuery::addCond ( string $name , int $op , string $expr )

Adds a condition to the query. Condition can be something like: get all keys which value matches expr.

参数

name
Name of the column in the condition

op
The operator. One of the TokyoTyrant::RDBQC_* constants

expr
The expression

返回值

This method returns the current object and throws TokyoTyrantException on failure.

范例

示例 #1 TokyoTyrantQuery::addCond example

<?php
/* Connect to a table database */
$tt = new TokyoTyrantTable("localhost", 1979);

/* Add rows */
$tt->put(null, array("column1" => "some data", "column2" => "something here"));
$tt->put(null, array("column1" => "more data", "column2" => "best data this far"));
$tt->put(null, array("column1" => "again data", "column3" => "not here"));
$tt->put(null, array("column45" => "random data", "column2" => "something along the lines"));
$tt->put(null, array("column21" => "test data", "column2" => "generating.."));
$tt->put(null, array("column1" => "foobar data", "column2" => "value here"));

/* Get a new query object */
$query = $tt->getQuery();

/* Add a search condition */
$query->addCond("column2", TokyoTyrant::RDBQC_STROR, "something");

/* Dump the search results */
var_dump($query->search());
?>

以上例程会输出:

array(2) {
  [1]=>
  array(2) {
    ["column1"]=>
    string(9) "some data"
    ["column2"]=>
    string(14) "something here"
  }
  [4]=>
  array(2) {
    ["column45"]=>
    string(11) "random data"
    ["column2"]=>
    string(25) "something along the lines"
  }
}

参见

  • Classname::Method

TokyoTyrantQuery::__construct

Construct a new query

说明

public TokyoTyrantQuery::__construct ( TokyoTyrantTable $table )

Construct a new query object

参数

table
TokyoTyrantTable object with active database connection

返回值

Returns a new TokyoTyrantQuery object and throws TokyoTyrantException on error

范例

示例 #1 TokyoTyrantQuery::__construct example

<?php
$tt = new TokyoTyrantTable("localhost", 1979);

$query = new TokyoTyrantQuery($tt);

/* Work with $query */
?>

参见

  • TokyoTyrantTable::getQuery

TokyoTyrantQuery::count

Counts records

说明

public int TokyoTyrantQuery::count ( void )

Returns a count of how many records a query returns.

参数

此函数没有参数。

返回值

Returns a count of matching rows and throws TokyoTyrantException on error

范例

示例 #1 TokyoTyrantQuery::count example

<?php
/* Connect to a table database */
$tt = new TokyoTyrantTable("localhost", 1979);

/* Add rows */
$tt->put(null, array("column1" => "some data", "column2" => "something here"));
$tt->put(null, array("column1" => "more data", "column2" => "best data this far"));
$tt->put(null, array("column1" => "again data", "column3" => "not here"));
$tt->put(null, array("column45" => "random data", "column2" => "something along the lines"));
$tt->put(null, array("column21" => "test data", "column2" => "generating.."));
$tt->put(null, array("column1" => "foobar data", "column2" => "value here"));

/* Get a new query object */
$query = $tt->getQuery();

/* Add a search condition */
$query->addCond("column2", TokyoTyrant::RDBQC_STROR, "something");

/* Count the results */
var_dump($query->count());
?>

以上例程会输出:

int(2)

参见

  • TokyoTyrantQuery::out
  • TokyoTyrantQuery::search
  • TokyoTyrantQuery::metaSearch

TokyoTyrantQuery::current

Returns the current element

说明

public array TokyoTyrantQuery::current ( void )

Returns the current element. Part of Iterator interface

参数

此函数没有参数。

返回值

Returns the current row

范例

示例 #1 TokyoTyrantQuery iterator example

<?php
/* Connect to a table database */
$tt = new TokyoTyrantTable("localhost", 1979);

/* Add rows */
$tt->put(null, array("column1" => "some data", "column2" => "something here"));
$tt->put(null, array("column1" => "more data", "column2" => "best data this far"));
$tt->put(null, array("column1" => "again data", "column3" => "foobar here"));
$tt->put(null, array("column45" => "random data", "column2" => "something along the lines"));
$tt->put(null, array("column21" => "test data", "column2" => "generating.."));
$tt->put(null, array("column1" => "foobar data", "column2" => "value here"));

/* Get a new query object */
$query = $tt->getQuery();

/* Add a search condition */
$query->addCond("column2", TokyoTyrant::RDBQC_STROR, "something");

/* Iterate the results */
foreach ($query as $key => $value) {
    echo "pk: $key, columns: ", count($value) ,"\n";
}
?>

以上例程的输出类似于:

pk: 1, columns: 2
pk: 4, columns: 2

参见

  • TokyoTyrantQuery::addCond

TokyoTyrantQuery::hint

Get the hint string of the query

说明

public string TokyoTyrantQuery::hint ( void )

Get the hint string of the query. The hint string contains information about an executed query and it could be compared to for example MySQL EXPLAIN statement.

参数

此函数没有参数。

返回值

This method always returns a string

范例

示例 #1 TokyoTyrantQuery::hint example

<?php
$tt = new TokyoTyrantTable("localhost", 1979);
$tt->vanish();

for ($i = 0; $i < 11; $i++) {
     $tt->put(null, array('a_col' => 'a' . $i, 'b_col' => 'b' . $i));
}

$query = $tt->getQuery();
$query->addCond('a_col', TokyoTyrant::RDBQC_STRBW, 'a');

$query->search();
var_dump($query->hint());
?>

以上例程的输出类似于:

string(72) "
scanning the whole table
result set size: 11
leaving the natural order
"

参见

  • TokyoTyrantQuery::addCond

TokyoTyrantQuery::key

Returns the current key

说明

public string TokyoTyrantQuery::key ( void )

Returns the current key. Part of the Iterator interface

参数

此函数没有参数。

返回值

Returns the current key and throws TokyoTyrantException on error

范例

示例 #1 TokyoTyrantQuery iterator example

<?php
/* Connect to a table database */
$tt = new TokyoTyrantTable("localhost", 1979);

/* Add rows */
$tt->put(null, array("column1" => "some data", "column2" => "something here"));
$tt->put(null, array("column1" => "more data", "column2" => "best data this far"));
$tt->put(null, array("column1" => "again data", "column3" => "foobar here"));
$tt->put(null, array("column45" => "random data", "column2" => "something along the lines"));
$tt->put(null, array("column21" => "test data", "column2" => "generating.."));
$tt->put(null, array("column1" => "foobar data", "column2" => "value here"));

/* Get a new query object */
$query = $tt->getQuery();

/* Add a search condition */
$query->addCond("column2", TokyoTyrant::RDBQC_STROR, "something");

/* Iterate the results */
foreach ($query as $key => $value) {
    echo "pk: $key, columns: ", count($value) ,"\n";
}
?>

以上例程的输出类似于:

pk: 1, columns: 2
pk: 4, columns: 2

参见

  • TokyoTyrantQuery::addCond

TokyoTyrantQuery::metaSearch

Retrieve records with multiple queries

说明

public array TokyoTyrantQuery::metaSearch ( array $queries , int $type )

Executes multiple queries on a database and returns matching records. The current object is always the left most object in the search.

参数

queries
Array of TokyoTyrantQuery objects

type
One of the TokyoTyrant::RDBMS_* constants

返回值

Returns the matching rows and throws TokyoTyrantException on error

范例

示例 #1 TokyoTyrantQuery::metaSearch example

<?php
/* Connect to a table database */
$tt = new TokyoTyrantTable("localhost", 1979);

/* Add test data */
$tt->put('cherry',     array('color' => 'red'));
$tt->put('strawberry', array('color' => 'red'));
$tt->put('apple',      array('color' => 'green'));
$tt->put('lemon',      array('color' => 'yellow'));

/* First query */
$query = $tt->getQuery();
$query->addCond('color', TokyoTyrant::RDBQC_STREQ, 'red')->setOrder('color', TokyoTyrant::RDBQO_STRASC);

/* Second query */
$query1 = $tt->getQuery();
$query1->addCond('color', TokyoTyrant::RDBQC_STREQ, 'yellow');

/* Get union between the queries */
var_dump($query->metaSearch(array($query1), TokyoTyrant::RDBMS_UNION));
?>

以上例程会输出:

array(3) {
  ["cherry"]=>
  array(1) {
    ["color"]=>
    string(3) "red"
  }
  ["strawberry"]=>
  array(1) {
    ["color"]=>
    string(3) "red"
  }
  ["lemon"]=>
  array(1) {
    ["color"]=>
    string(6) "yellow"
  }
}

参见

  • TokyoTyrantQuery::search

TokyoTyrantQuery::next

Moves the iterator to next entry

说明

public array TokyoTyrantQuery::next ( void )

Returns the next result in the resultset. Part of the Iterator interface.

参数

此函数没有参数。

返回值

Returns the next row and throws TokyoTyrantException on error.

范例

示例 #1 TokyoTyrantQuery iterator example

<?php
/* Connect to a table database */
$tt = new TokyoTyrantTable("localhost", 1979);

/* Add rows */
$tt->put(null, array("column1" => "some data", "column2" => "something here"));
$tt->put(null, array("column1" => "more data", "column2" => "best data this far"));
$tt->put(null, array("column1" => "again data", "column3" => "foobar here"));
$tt->put(null, array("column45" => "random data", "column2" => "something along the lines"));
$tt->put(null, array("column21" => "test data", "column2" => "generating.."));
$tt->put(null, array("column1" => "foobar data", "column2" => "value here"));

/* Get a new query object */
$query = $tt->getQuery();

/* Add a search condition */
$query->addCond("column2", TokyoTyrant::RDBQC_STROR, "something");

/* Iterate the results */
foreach ($query as $key => $value) {
    echo "pk: $key, columns: ", count($value) ,"\n";
}
?>

以上例程的输出类似于:

pk: 1, columns: 2
pk: 4, columns: 2

参见

  • TokyoTyrantQuery::addCond

TokyoTyrantQuery::out

Removes records based on query

说明

public TokyoTyrantQuery TokyoTyrantQuery::out ( void )

Removes all records that match the query. Works exactly like search but removes the records instead of returning them.

参数

此函数没有参数。

返回值

This method returns the current object and throws TokyoTyrantException on failure.

范例

示例 #1 TokyoTyrantQuery::out example

<?php
/* Connect to a table database */
$tt = new TokyoTyrantTable("localhost", 1979);

/* Add rows */
$tt->put(null, array("column1" => "some data", "column2" => "something here"));
$tt->put(null, array("column1" => "more data", "column2" => "best data this far"));
$tt->put(null, array("column1" => "again data", "column3" => "foobar here"));
$tt->put(null, array("column45" => "random data", "column2" => "something along the lines"));
$tt->put(null, array("column21" => "test data", "column2" => "generating.."));
$tt->put(null, array("column1" => "foobar data", "column2" => "value here"));

/* Get a new query object */
$query = $tt->getQuery();

/* Add a search condition */
$query->addCond("column2", TokyoTyrant::RDBQC_STROR, "something");

/* Remove matching records */
$query->out();
?>

参见

  • TokyoTyrantQuery::addCond

TokyoTyrantQuery::rewind

Rewinds the iterator

说明

public bool TokyoTyrantQuery::rewind ( void )

Rewind the resultset and executes the query if it has not been executed. Part of the Iterator interface.

参数

此函数没有参数。

返回值

Returns TRUE

范例

示例 #1 TokyoTyrantQuery iterator example

<?php
/* Connect to a table database */
$tt = new TokyoTyrantTable("localhost", 1979);

/* Add rows */
$tt->put(null, array("column1" => "some data", "column2" => "something here"));
$tt->put(null, array("column1" => "more data", "column2" => "best data this far"));
$tt->put(null, array("column1" => "again data", "column3" => "foobar here"));
$tt->put(null, array("column45" => "random data", "column2" => "something along the lines"));
$tt->put(null, array("column21" => "test data", "column2" => "generating.."));
$tt->put(null, array("column1" => "foobar data", "column2" => "value here"));

/* Get a new query object */
$query = $tt->getQuery();

/* Add a search condition */
$query->addCond("column2", TokyoTyrant::RDBQC_STROR, "something");

/* Iterate the results */
foreach ($query as $key => $value) {
    echo "pk: $key, columns: ", count($value) ,"\n";
}
?>

以上例程的输出类似于:

pk: 1, columns: 2
pk: 4, columns: 2

参见

  • TokyoTyrantQuery::addCond

TokyoTyrantQuery::search

Searches records

说明

public array TokyoTyrantQuery::search ( void )

Executes a search on the table database. Returns an array of arrays containing the matching records. In the returned array the first level is the primary key of the data and the second level is the row data.

参数

此函数没有参数。

返回值

Returns the matching rows and throws TokyoTyrantException on error

范例

示例 #1 TokyoTyrantQuery::search example

<?php
/* Connect to a table database */
$tt = new TokyoTyrantTable("localhost", 1979);

/* Add rows */
$tt->put(null, array("column1" => "some data", "column2" => "something here"));
$tt->put(null, array("column1" => "more data", "column2" => "best data this far"));
$tt->put(null, array("column1" => "again data", "column3" => "not here"));
$tt->put(null, array("column45" => "random data", "column2" => "something along the lines"));
$tt->put(null, array("column21" => "test data", "column2" => "generating.."));
$tt->put(null, array("column1" => "foobar data", "column2" => "value here"));

/* Get a new query object */
$query = $tt->getQuery();

/* Add a search condition */
$query->addCond("column2", TokyoTyrant::RDBQC_STROR, "something");

/* Dump the search results */
var_dump($query->search());
?>

以上例程会输出:

array(2) {
  [1]=>
  array(2) {
    ["column1"]=>
    string(9) "some data"
    ["column2"]=>
    string(14) "something here"
  }
  [4]=>
  array(2) {
    ["column45"]=>
    string(11) "random data"
    ["column2"]=>
    string(25) "something along the lines"
  }
}

参见

  • TokyoTyrantQuery::out
  • TokyoTyrantQuery::metaSearch

TokyoTyrantQuery::setLimit

Limit results

说明

public mixed TokyoTyrantQuery::setLimit ([ int $max [, int $skip ]] )

Set the maximum amount of records to return on a query.

参数

max
Maximum amount of records. Default: -1

skip
How many records to skip from the start. Default: -1

返回值

This method returns the current object and throws TokyoTyrantException on failure.

TokyoTyrantQuery::setOrder

Orders results

说明

public mixed TokyoTyrantQuery::setOrder ( string $name , int $type )

Sets the order of a query

参数

name
The column name to apply the ordering on.

type
The type can be one of the following constants:

  • TokyoTyrant::RDBQO_STRASC - String ascending
  • TokyoTyrant::RDBQO_STRDESC - String descending
  • TokyoTyrant::RDBQO_NUMASC - Numberic ascending
  • TokyoTyrant::RDBQO_NUMDESC - String descending

返回值

This method returns the current object.

TokyoTyrantQuery::valid

Checks the validity of current item

说明

public bool TokyoTyrantQuery::valid ( void )

Checks if the current item is valid. Part of the Iterator interface

参数

此函数没有参数。

返回值

Returns TRUE if the current item is valid and FALSE if not.

范例

示例 #1 TokyoTyrantQuery iterator example

<?php
/* Connect to a table database */
$tt = new TokyoTyrantTable("localhost", 1979);

/* Add rows */
$tt->put(null, array("column1" => "some data", "column2" => "something here"));
$tt->put(null, array("column1" => "more data", "column2" => "best data this far"));
$tt->put(null, array("column1" => "again data", "column3" => "foobar here"));
$tt->put(null, array("column45" => "random data", "column2" => "something along the lines"));
$tt->put(null, array("column21" => "test data", "column2" => "generating.."));
$tt->put(null, array("column1" => "foobar data", "column2" => "value here"));

/* Get a new query object */
$query = $tt->getQuery();

/* Add a search condition */
$query->addCond("column2", TokyoTyrant::RDBQC_STROR, "something");

/* Iterate the results */
foreach ($query as $key => $value) {
    echo "pk: $key, columns: ", count($value) ,"\n";
}
?>

以上例程的输出类似于:

pk: 1, columns: 2
pk: 4, columns: 2

参见

  • TokyoTyrantQuery::addCond

简介

Provides an iterator for TokyoTyrant and TokyoTyrantTable objects. The iterator iterates over all keys and values in the database. TokyoTyrantIterator was added in version 0.2.0.

类摘要

TokyoTyrantIterator

class TokyoTyrantIterator implements Iterator {

/* Methods */

public __construct ( mixed $object )

public mixed current ( void )

public mixed key ( void )

public mixed next ( void )

public void rewind ( void )

public bool valid ( void )

/* Inherited methods */

public number TokyoTyrant::add ( string $key , number $increment [, int $type = 0 ] )

public TokyoTyrant TokyoTyrant::connect ( string $host [, int $port = TokyoTyrant::RDBDEF_PORT [, array $options ]] )

public TokyoTyrant TokyoTyrant::connectUri ( string $uri )

public TokyoTyrant::__construct ([ string $host [, int $port = TokyoTyrant::RDBDEF_PORT [, array $options ]]] )

public TokyoTyrant TokyoTyrant::copy ( string $path )

public string TokyoTyrant::ext ( string $name , int $options , string $key , string $value )

public array TokyoTyrant::fwmKeys ( string $prefix , int $max_recs )

public mixed TokyoTyrant::get ( mixed $keys )

public TokyoTyrantIterator TokyoTyrant::getIterator ( void )

public int TokyoTyrant::num ( void )

public TokyoTyrant TokyoTyrant::out ( mixed $keys )

public TokyoTyrant TokyoTyrant::put ( mixed $keys [, string $value = NULL ] )

public TokyoTyrant TokyoTyrant::putCat ( mixed $keys [, string $value ] )

public TokyoTyrant TokyoTyrant::putKeep ( mixed $keys [, string $value ] )

public TokyoTyrant TokyoTyrant::putNr ( mixed $keys [, string $value = NULL ] )

public mixed TokyoTyrant::putShl ( string $key , string $value , int $width )

public mixed TokyoTyrant::restore ( string $log_dir , int $timestamp [, bool $check_consistency = TRUE ] )

public mixed TokyoTyrant::setMaster ( string $host , int $port , int $timestamp [, bool $check_consistency = TRUE ] )

public int TokyoTyrant::size ( string $key )

public array TokyoTyrant::stat ( void )

public mixed TokyoTyrant::sync ( void )

public TokyoTyrant TokyoTyrant::tune ( float $timeout [, int $options = TokyoTyrant::RDBT_RECON ] )

public mixed TokyoTyrant::vanish ( void )

}

TokyoTyrantIterator::__construct

Construct an iterator

说明

public TokyoTyrantIterator::__construct ( mixed $object )

Construct a new TokyoTyrantIterator object. One connection can have multiple iterators but it is not quaranteed that all items are traversed in that case. object parameter can be either an of instance TokyoTyrant or TokyoTyrantTable.

参数

此函数没有参数。

返回值

Throws an exception if iterator initialization fails.

范例

示例 #1 TokyoTyrantIterator::__construct example

<?php
/* Connect to a table database */
$tt = new TokyoTyrantTable("localhost", 1979);

/* Passing null to put generates a new uid */
$index = $tt->put(null, array("column1" => "some data", "column2" => "more data"));

/* Construct an iterator */
$it = new TokyoTyrantIterator($tt);

foreach ($it as $value) {
    var_dump($value);
}
?>

以上例程会输出:

array(2) {
  ["column1"]=>
  string(9) "some data"
  ["column2"]=>
  string(9) "more data"
}

参见

  • TokyoTyrantIterator::key
  • TokyoTyrantIterator::current
  • TokyoTyrantIterator::next
  • TokyoTyrantIterator::rewind
  • TokyoTyrantIterator::key

TokyoTyrantIterator::current

Get the current value

说明

public mixed TokyoTyrantIterator::current ( void )

Returns the current value during iteration.

参数

此函数没有参数。

返回值

Returns the current value on success and false on failure.

参见

  • TokyoTyrantIterator::valid
  • TokyoTyrantIterator::key
  • TokyoTyrantIterator::next
  • TokyoTyrantIterator::rewind

TokyoTyrantIterator::key

Returns the current key

说明

public mixed TokyoTyrantIterator::key ( void )

Returns the current key.

参数

此函数没有参数。

返回值

Returns the current key on success and false on failure.

参见

  • TokyoTyrantIterator::valid
  • TokyoTyrantIterator::current
  • TokyoTyrantIterator::next
  • TokyoTyrantIterator::rewind

TokyoTyrantIterator::next

Move to next key

说明

public mixed TokyoTyrantIterator::next ( void )

Move to next key during iteration and return it's value.

参数

此函数没有参数。

返回值

Returns the next value on success and false on failure.

参见

  • TokyoTyrantIterator::valid
  • TokyoTyrantIterator::key
  • TokyoTyrantIterator::current
  • TokyoTyrantIterator::rewind

TokyoTyrantIterator::rewind

Rewinds the iterator

说明

public void TokyoTyrantIterator::rewind ( void )

Rewinds the iterator for new iteration. Called automatically at the beginning of foreach.

参数

此函数没有参数。

返回值

Throws TokyoTyrantException if iterator initialization fails.

参见

  • TokyoTyrantIterator::valid
  • TokyoTyrantIterator::current
  • TokyoTyrantIterator::next
  • TokyoTyrantIterator::rewind

TokyoTyrantIterator::valid

Rewinds the iterator

说明

public bool TokyoTyrantIterator::valid ( void )

Checks whether the internal pointer points to valid element.

参数

此函数没有参数。

返回值

Returns TRUE if the current item is valid and FALSE if not.

参见

  • TokyoTyrantIterator::key
  • TokyoTyrantIterator::current
  • TokyoTyrantIterator::next
  • TokyoTyrantIterator::rewind

简介

TokyoTyrantException

类摘要

tokyotyrantexception

class tokyotyrantexception extends Exception {

/* 属性 */

protected int $code ;

/* 继承的属性 */

protected string $message ;

protected int $code ;

protected string $file ;

protected int $line ;

/* 继承的方法 */

final public string Exception::getMessage ( void )

final public Throwable Exception::getPrevious ( void )

final public int Exception::getCode ( void )

final public string Exception::getFile ( void )

final public int Exception::getLine ( void )

final public array Exception::getTrace ( void )

final public string Exception::getTraceAsString ( void )

public string Exception::__toString ( void )

final private void Exception::__clone ( void )

}

属性

code
The exception code. This can be compared to TokyoTyrant::TTE_* constants