powered by nequal
Home » Net_TokyoTyrant » Timeline » 1026

Changeset 1026 -- 2009-07-21 02:18:42

Author
個々一番
Comment
RDB機能をサポートを追加(リファクタするかもしれません) コメントを少しつけた

Diffs

Net_TokyoTyrant/trunk/tests/tmp_test.php

@@ -1,108 +0,0 @@
-<?php
-ini_set('memory_limit', -1);
-require_once dirname(dirname(__FILE__)) . '/Net/TokyoTyrant.php';
-
-$tt = new Net_TokyoTyrant();
-$key = 'keytest';
-$data = 'the test data';
-$key2 = 'keytest2';
-$data2 = 'the test2 data';
-$count_key = 'count';
-$extname = 'echo';
-$error = null;
-try {
-    $tt->connect('dummy', 1978);
-} catch (Net_TokyoTyrantException $e) {
-    $error = $e->getMessage();
-}
-assert(is_string($error) && strlen($error) > 1);
-
-
-$tt->connect('localhost', 1978, 1000);
-
-assert($tt->vanish() === true);
-
-assert($tt->put($key, $data) === true);
-$getdata = $tt->get($key);
-assert($getdata === $data);
-
-assert($tt->putkeep($key, $data . 'keep') === false);
-$getdata = $tt->get($key);
-assert($getdata === $data);
-$tt->out($key);
-assert($tt->putkeep($key, $data . 'keep') === true);
-$getdata = $tt->get($key);
-assert($getdata === $data . 'keep');
-
-assert($tt->put($key, $data) === true);
-$getdata = $tt->get($key);
-assert($getdata === $data);
-assert($tt->putcat($key, $data) === true);
-$getdata = $tt->get($key);
-assert($getdata === $data . $data);
-
-
-assert($tt->put($key, $data) === true);
-assert($tt->putrtt($key, $data, 2) === true);
-$getdata = $tt->get($key);
-assert($getdata === substr($data, strlen($data) - 2, 2));
-
-
-assert($tt->out($key) === true);
-$getdata = $tt->get($key);
-assert($getdata === false);
-
-assert($tt->put($key, $data));
-assert($tt->put($key2, $data2));
-assert(count($tt->mget(array($key, $key2))) === 2);
-assert(count($tt->fwmkeys('key', 2)) === 2);
-assert($tt->vsize($key) === strlen($data));
-assert($tt->vanish() === true);
-assert($tt->iterinit() === true);
-assert($tt->iternext() === false);
-
-assert($tt->put($key, $data));
-assert($tt->iterinit() === true);
-assert($tt->iternext() === $key);
-assert($tt->iternext() === false);
-
-assert($tt->addint($count_key, 1) === 1);
-assert($tt->addint($count_key, 2) === 3);
-assert($tt->addint($count_key, -2) === 1);
-assert($tt->putint($count_key, 1));
-assert($tt->getint($count_key) === 1);
-assert($tt->addint($count_key, 1) === 2);
-assert($tt->getint($count_key) === 2);
-assert($tt->addint($count_key, -3) === -1);
-
-//$value = 'data';
-//assert($tt->ext($extname, $key, $value) === $value);
-//assert($tt->ext($extname, $key, $value, Net_TokyoTyrant::RDBXOLCKNON) === $value);
-//assert($tt->ext($extname, $key, $value, Net_TokyoTyrant::RDBXOLCKREC) === $value);
-//assert($tt->ext($extname, $key, $value, Net_TokyoTyrant::RDBXOLCKGLB) === $value);
-
-//big size data
-//$big_data = str_repeat('1', 1024 * 128);
-//for ($i = 0; $i < 1000; $i++) {
-//    assert($tt->put('bigdata', $big_data));
-//}
-
-//$tt->setTimeout(60);
-$big_data = str_repeat('1', 1024 * 1024 * 32);
-// limit size fllow code is error.... fummm....
-//$big_data = str_repeat('1', 1024 * 1024 * 33);
-assert($tt->put('bigdata', $big_data));
-
-assert($tt->sync() === true);
-assert(is_array($tt->size()));
-assert(is_array($tt->rnum()));
-
-assert($tt->copy('/tmp/test.net_tokyotyrant.db') === true);
-assert(file_exists('/tmp/test.net_tokyotyrant.db') === true);
-assert(strlen($tt->stat()) > 1);
-
-
-assert($tt->optimize('') === true);
-assert($tt->copy('/tmp/test.net_tokyotyrant.db') === true);
-
-$tt->close();

Net_TokyoTyrant/trunk/tests/nomal_test.php

@@ -0,0 +1,114 @@
+<?php
+ini_set('memory_limit', -1);
+require_once dirname(dirname(__FILE__)) . '/Net/TokyoTyrant.php';
+require_once dirname(dirname(__FILE__)) . '/Net/TokyoTyrant/Table.php';
+
+$tt = new Net_TokyoTyrant();
+$key = 'keytest';
+$data = 'the test data';
+$key2 = 'keytest2';
+$data2 = 'the test2 data';
+$count_key = 'count';
+$extname = 'echo';
+$error = null;
+try {
+    $tt->connect('dummy', 1978);
+} catch (Net_TokyoTyrantException $e) {
+    $error = $e->getMessage();
+}
+assert(is_string($error) && strlen($error) > 1);
+
+
+$tt->connect('localhost', 1978, 1000);
+
+assert($tt->vanish() === true);
+
+assert($tt->put($key, $data) === true);
+$getdata = $tt->get($key);
+assert($getdata === $data);
+
+assert($tt->putkeep($key, $data . 'keep') === false);
+$getdata = $tt->get($key);
+assert($getdata === $data);
+$tt->out($key);
+assert($tt->putkeep($key, $data . 'keep') === true);
+$getdata = $tt->get($key);
+assert($getdata === $data . 'keep');
+
+assert($tt->put($key, $data) === true);
+$getdata = $tt->get($key);
+assert($getdata === $data);
+assert($tt->putcat($key, $data) === true);
+$getdata = $tt->get($key);
+assert($getdata === $data . $data);
+
+
+assert($tt->put($key, $data) === true);
+assert($tt->putrtt($key, $data, 2) === true);
+$getdata = $tt->get($key);
+assert($getdata === substr($data, strlen($data) - 2, 2));
+
+
+assert($tt->out($key) === true);
+$getdata = $tt->get($key);
+assert($getdata === false);
+
+assert($tt->put($key, $data));
+assert($tt->put($key2, $data2));
+assert(count($tt->mget(array($key, $key2))) === 2);
+assert(count($tt->fwmkeys('key', 2)) === 2);
+assert($tt->vsize($key) === strlen($data));
+assert($tt->vanish() === true);
+assert($tt->iterinit() === true);
+assert($tt->iternext() === false);
+
+assert($tt->put($key, $data));
+assert($tt->iterinit() === true);
+assert($tt->iternext() === $key);
+assert($tt->iternext() === false);
+
+assert($tt->addint($count_key, 1) === 1);
+assert($tt->addint($count_key, 2) === 3);
+assert($tt->addint($count_key, -2) === 1);
+assert($tt->putint($count_key, 1));
+assert($tt->getint($count_key) === 1);
+assert($tt->addint($count_key, 1) === 2);
+assert($tt->getint($count_key) === 2);
+assert($tt->addint($count_key, -3) === -1);
+
+//$value = 'data';
+//assert($tt->ext($extname, $key, $value) === $value);
+//assert($tt->ext($extname, $key, $value, Net_TokyoTyrant::RDBXOLCKNON) === $value);
+//assert($tt->ext($extname, $key, $value, Net_TokyoTyrant::RDBXOLCKREC) === $value);
+//assert($tt->ext($extname, $key, $value, Net_TokyoTyrant::RDBXOLCKGLB) === $value);
+
+//big size data
+//$big_data = str_repeat('1', 1024 * 128);
+//for ($i = 0; $i < 1000; $i++) {
+//    assert($tt->put('bigdata', $big_data));
+//}
+
+//$tt->setTimeout(60);
+$big_data = str_repeat('1', 1024 * 1024 * 32);
+// limit size fllow code is error.... fummm....
+//$big_data = str_repeat('1', 1024 * 1024 * 33);
+assert($tt->put('bigdata', $big_data));
+
+assert($tt->sync() === true);
+assert(is_array($tt->size()));
+assert(is_array($tt->rnum()));
+
+assert($tt->copy('/tmp/test.net_tokyotyrant.db') === true);
+assert(file_exists('/tmp/test.net_tokyotyrant.db') === true);
+assert(strlen($tt->stat()) > 1);
+
+
+assert($tt->optimize('') === true);
+assert($tt->copy('/tmp/test.net_tokyotyrant.db') === true);
+assert($tt->vanish() === true);
+
+$tt->close();
+$test_data = array();
+$test_data[] = array('name' => 'Alex Low', 'vehicle' => 'silvana', 'age' => 29);
+$test_data[] = array('name' => 'Claus Valca', 'vehicle' => 'vanship', 'age' => 15);
+

Net_TokyoTyrant/trunk/tests/table_test.php

@@ -0,0 +1,73 @@
+<?php
+ini_set('memory_limit', -1);
+ini_set('display_errors', 'On');
+error_reporting(E_ALL);
+require_once dirname(dirname(__FILE__)) . '/Net/TokyoTyrant/Table.php';
+
+$test_data = array();
+$test_data[] = array('name' => 'Alex Low', 'vehicle' => 'silvana', 'age' => 29);
+$test_data[] = array('name' => 'Claus Valca', 'vehicle' => 'vanship', 'age' => 15);
+$test_data[] = array('name' => 'Lavie Head', 'vehicle' => 'vanship', 'age' => 15);
+
+$ttt = new Net_TokyoTyrant_Table();
+$ttt->connect('localhost', 11978, 1000);
+assert($ttt->vanish() === true);
+assert($ttt->setindex('name',  Net_TokyoTyrant_Table::ITLEXICAL));
+
+assert(is_string($uid1 = $ttt->genuid()));
+assert($ttt->put($uid1 , $test_data[0]));
+assert(count(array_diff($ttt->get($uid1),$test_data[0])) === 0);
+
+assert($ttt->out($uid1));
+assert($ttt->get($uid1) === false);
+assert($ttt->put($uid1 , $test_data[0]));
+
+
+assert($ttt->putkeep($uid1 , $test_data[0]) == false);
+assert(is_string($uid2 = $ttt->genuid()));
+assert($ttt->putkeep($uid2 , $test_data[0]));
+assert(count(array_diff($ttt->get($uid2),$test_data[0])) === 0);
+
+assert($ttt->putcat($uid2 , $test_data[0]));
+assert(count(array_diff($ttt->get($uid2),$test_data[0])) === 0);
+
+assert(is_array($ttt->mget(array($uid1, $uid2))));
+
+//clear
+assert($ttt->vanish() === true);
+assert($ttt->setindex('name',  Net_TokyoTyrant_Table::ITLEXICAL));
+assert(is_string($uid1 = $ttt->genuid()));
+assert($ttt->put($uid1 , $test_data[0]));
+
+assert(is_string($uid2 = $ttt->genuid()));
+assert($ttt->put($uid2 , $test_data[1]));
+assert(is_string($uid3 = 'aaaaa'));
+assert($ttt->put($uid3 , $test_data[1]));
+
+//Query
+$ttq = $ttt->getQuery();
+$ttq->addcond("vehicle", Net_TokyoTyrant_Query::QCSTRINC, "silvana");
+$ttq->setorder("age", Net_TokyoTyrant_Query::QCSTRINC);
+$ttq->setlimit(10);
+
+assert($ttq->searchcount() === 1);
+$result = $ttq->search();
+assert($result[0] === $uid1);
+
+$result = $ttq->searchget();
+assert(count(array_diff($result[0]['value'], $test_data[0])) === 0);
+
+$ttq = $ttt->getQuery();
+$ttq->setorder("age", Net_TokyoTyrant_Query::QONUMASC);
+$ttq->setlimit(1);
+$result = $ttq->searchget();
+assert((int)$result[0]['value']['age'] === $test_data['1']['age']);
+assert($ttq->searchcount() === 1);
+
+
+$ttq = $ttt->getQuery();
+$ttq->addcond("vehicle", Net_TokyoTyrant_Query::QCSTRINC, "vanship");
+$ttq->setorder("age", Net_TokyoTyrant_Query::QCSTRINC);
+assert($ttq->searchcount() === 2);
+
+$ttt->close();

Net_TokyoTyrant/trunk/Net/TokyoTyrant/Query.php

@@ -0,0 +1,130 @@
+<?php
+require_once(dirname(dirname(__FILE__)) . '/TokyoTyrant/Table.php');
+
+class Net_TokyoTyrant_Query
+{
+
+    /* query condition: string is equal to*/
+    const QCSTREQ = 0;
+    /* query condition: string is included in*/
+    const QCSTRINC = 1;
+    /* query condition: string begins with*/
+    const QCSTRBW = 2;
+    /* query condition: string ends with*/
+    const QCSTREW = 3;
+    /* query condition: string includes all tokens in*/
+    const QCSTRAND = 4;
+    /* query condition: string includes at least one token in*/
+    const QCSTROR = 5;
+    /* query condition: string is equal to at least one token in*/
+    const QCSTROREQ = 6;
+    /* query condition: string matches regular expressions of*/
+    const QCSTRRX = 7;
+    /* query condition: number is equal to*/
+    const QCNUMEQ = 8;
+    /* query condition: number is greater than*/
+    const QCNUMGT = 9;
+    /* query condition: number is greater than or equal to*/
+    const QCNUMGE = 10;
+    /* query condition: number is less than*/
+    const QCNUMLT = 11;
+    /* query condition: number is less than or equal to*/
+    const QCNUMLE = 12;
+    /* query condition: number is between two tokens of*/
+    const QCNUMBT = 13;
+    /* query condition: number is equal to at least one token in*/
+    const QCNUMOREQ = 14;
+    /* query condition: full-text search with the phrase of*/
+    const QCFTSPH = 15;
+    /* query condition: full-text search with all tokens in*/
+    const QCFTSAND = 16;
+    /* query condition: full-text search with at least one token in*/
+    const QCFTSOR = 17;
+    /* query condition: full-text search with the compound expression of*/
+    const QCFTSEX = 18;
+    /* query condition: negation flag*/
+    const QCNEGATE = 0x1000000; //1 << 24 umm..
+    /* query condition: no index flag*/
+    const QCNOIDX =  0x2000000; //1 << 24 umm..
+    /* order type: string ascending*/
+    const QOSTRASC = 0;
+    /* order type: string descending*/
+    const QOSTRDESC = 1;
+    /* order type: number ascending*/
+    const QONUMASC = 2;
+    /* order type: number descending*/
+    const QONUMDESC = 3;
+
+    private
+        $tttable = null,
+        $params = array();
+    public function __construct(Net_TokyoTyrant_Table $tokyotyrant_table)
+    {
+        $this->tttable = $tokyotyrant_table;
+    }
+
+    public function addcond($name , $op, $expr)
+    {
+        $this->params[$name] = sprintf("addcond\0%s\0%s\0%s", $name, $op, $expr);
+    }
+
+    public function setorder($name, $type)
+    {
+        $this->params['setorder' . $name] = sprintf("setorder\0%s\0%s", $name, $type);
+    }
+
+    public function setlimit($max = -1, $skp = -1)
+    {
+        $this->params['setlimit'] = sprintf("setlimit\0%s\0%s", $max, $skp);
+    }
+
+    public function search()
+    {
+        $params = array_values($this->params);
+        $values = $this->tttable->misc('search', $params, 1);
+        return $values;
+    }
+
+    public function searchget($names = null)
+    {
+        $params = array_values($this->params);
+        if (! is_null($names)) {
+            $params[] = sprintf("get\0%s", implode("\0", $names));
+        } else {
+            $params[] = 'get';
+        }
+
+        try {
+            $values = $this->tttable->misc('search', $params, 1);
+        } catch(Net_TokyoTyrantProtocolException $e) {
+            return false;
+        }
+
+        foreach ($values as $value) {
+            $col = explode("\0", $value);
+            $col_count = count($col);
+            $data = array();
+            for ($i = 2; $i < $col_count ; $i+=2) {
+                $data[$col[$i]] = $col[$i + 1];
+            }
+            $item = array();
+            $item['key'] = $col[1];
+            $item['value']= $data;
+            $result[] = $item;
+        }
+        return $result;
+    }
+
+    public function searchcount()
+    {
+        $params = array_values($this->params);
+        $params[] = 'count';
+        try {
+            $count = $this->tttable->misc('search', $params, 1);
+        } catch(Net_TokyoTyrantProtocolException $e) {
+            return 0;
+        }
+
+        return $count !== false ? (int)$count[0] : 0;
+    }
+}

Net_TokyoTyrant/trunk/Net/TokyoTyrant/Table.php

@@ -0,0 +1,146 @@
+<?php
+require_once(dirname(dirname(__FILE__)) . '/TokyoTyrant.php');
+require_once(dirname(dirname(__FILE__)) . '/TokyoTyrant/Query.php');
+
+class Net_TokyoTyrant_Table extends Net_TokyoTyrant
+{
+    /* public const */
+    const ITLEXICAL = 0;
+    const ITDECIMAL = 1;
+    const ITTOKEN   = 2;
+    const ITQGRAM   = 3;
+    const ITOPT     = 9999;
+    const ITVOID    = 9999;
+    const ITKEEP    = 0x1000000; //1 << 24 umm..
+
+    public function put($key, $values)
+    {
+        $params = array();
+        $params[] = $key;
+        foreach ($values as $name => $value) {
+            $params[] = $name;
+            $params[] = $value;
+        }
+
+        try {
+            $this->misc('put', $params, 0);
+        } catch(Net_TokyoTyrantProtocolException $e) {
+            return false;
+        }
+        return true;
+    }
+
+    public function putkeep($key, $values)
+    {
+        $params = array();
+        $params[] = $key;
+        foreach ($values as $name => $value) {
+            $params[] = $name;
+            $params[] = $value;
+        }
+        try {
+            $this->misc('putkeep', $params, 0);
+        } catch(Net_TokyoTyrantProtocolException $e) {
+            return false;
+        }
+        return true;
+    }
+
+    public function putcat($key, $values)
+    {
+        $params = array();
+
+        foreach ($values as $key => $value) {
+            $params[] = $key;
+            $params[] = $value;
+        }
+
+        try {
+            $this->misc('putcat', $params, 0);
+        } catch(Net_TokyoTyrantProtocolException $e) {
+            return false;
+        }
+
+        return true;
+    }
+
+    public function out($key)
+    {
+        $params = array();
+        $params[] = $key;
+
+        try {
+            $this->misc('out', $params, 0);
+        } catch(Net_TokyoTyrantProtocolException $e) {
+            return false;
+        }
+
+        return true;
+    }
+
+    public function get($key)
+    {
+        $params = array();
+        $params[] = $key;
+        try {
+            $values = $this->misc('get', $params, 0);
+        } catch(Net_TokyoTyrantProtocolException $e) {
+            return false;
+        }
+        $values_count = count($values);
+        $result = array();
+        for ($i = 0;$i < $values_count; $i+=2) {
+            $result[$values[$i]] = $values[$i + 1];
+        }
+        return $result;
+    }
+
+
+    public function mget($keys)
+    {
+        $values = parent::mget($keys);
+
+        $result = array();
+        foreach ($values as $value) {
+            $col = explode("\0", $value);
+            $col_count = count($col);
+            $data = array();
+            for ($i = 0; $i < $col_count ; $i+=2) {
+                $data[$col[$i]] = $col[$i + 1];
+            }
+            $result[] = $data;
+        }
+        return $result;
+    }
+
+
+    public function setindex($name, $type)
+    {
+        $params = array();
+        $params[] = $name;
+        $params[] = $type;
+
+        try {
+            $this->misc('setindex', $params, 0);
+        } catch(Net_TokyoTyrantProtocolException $e) {
+            return false;
+        }
+        return true;
+    }
+
+    public function genuid()
+    {
+        try {
+            $result = $this->misc('genuid', array(), 0);
+        } catch(Net_TokyoTyrantProtocolException $e) {
+            return false;
+        }
+        return $result[0];
+
+    }
+
+    public function getQuery()
+    {
+        return new Net_TokyoTyrant_Query($this);
+    }
+}

Net_TokyoTyrant/trunk/Net/TokyoTyrant.php

@@ -1,13 +1,30 @@
<?php
+/**
+ * Net_TokyoTyrant
+ * @author cocoitiban <cocoiti@gmail.com>
+ * License: MIT License
+ * @package Net_TokyoTyrant
+*/
+
+
+/* base Excetion */
class Net_TokyoTyrantException extends Exception {};
+/* network error */
class Net_TokyoTyrantNetworkException extends Net_TokyoTyrantException {};
+/* tokyotyrant error */
class Net_TokyoTyrantProtocolException extends Net_TokyoTyrantException {};
-// License: MIT
-// author: Keita Arai <cocoiti@gmail.com>
-
+/**
+ * TokyoTyrant Base Class
+ *
+ * @category Net
+ * @package Net_TokyoTyrant
+ * @author Keita Arai <cocoiti@gmail.com>
+ *@license   http://www.opensource.org/licenses/mit-license.html  MIT License
+ */
class Net_TokyoTyrant
{
+    /* @access private */
private
$connect = false;
private
@@ -17,10 +34,17 @@
private
$socket_timeout;
+    /* @access public */
const RDBXOLCKNON = 0;
const RDBXOLCKREC = 1;
const RDBXOLCKGLB = 2;
-
+
+    /**
+     * server connect
+     * @param string $server servername
+     * @param string $server port number
+     * @param string $server timeout (connection only)
+     */
public function connect($server, $port, $timeout = 10)
{
$this->close();
@@ -31,17 +55,28 @@
$this->connect = true;
}
+    /**
+     * setting socket timeout
+     * @param integer $timeout timeout
+     */
public function setTimeout($timeout)
{
$this->socket_timeout = $timeout;
stream_set_timeout($this->socket, $timeout);
}
+    /**
+     * get timeout
+     * @return integer timeout
+     */
public function getTimeout()
{
return $this->socket_timeout;
}
+    /**
+     * close session
+     */
public function close()
{
if ($this->connect) {
@@ -49,7 +84,12 @@
}
}
-
+    /**
+     * read buffer
+     * @access private
+     * @param $length readlength
+     * @result string buffer data
+     */
private function _read($length)
{
if ($this->connect === false) {
@@ -68,7 +108,10 @@
return $result;
}
-
+    /**
+     * send data
+     * @param $data data
+     */
private function _write($data)
{
$result = $this->_fullwrite($this->socket, $data);
@@ -106,6 +149,11 @@
$this->_write($cmd . $this->_makeBin($values));
}
+    /**
+     * make tokyotyrant data
+     * @param array $values send data
+     * @return string tokyotyrant data
+     */
private function _makeBin($values){
$int = '';
$str = '';
@@ -127,11 +175,18 @@
return $int . $str;
}
-
+    /**
+     * get data
+     * @return
+     */
protected function _getResponse()
{
$res = fread($this->socket, 1);
$res = unpack('c', $res);
+        if ($res[1] === -1) {
+            throw new Net_TokyoTyrantProtocolException('Error send');
+        }
+
if ($res[1] !== 0) {
throw new Net_TokyoTyrantProtocolException('Error Response');
}
@@ -461,4 +516,27 @@
$this->_getResponse();
return $this->_getValue();
}
+
+    public function misc($name, $args, $opts = 0)
+    {
+        $cmd = pack('c*', 0xC8, 0x90);
+        $data = $cmd . pack('N*', strlen($name), $opts, count($args)) . $name;
+
+        foreach ($args as $arg) {
+            $data .= pack('N', strlen($arg)) . $arg;
+        }
+        $this->_write($data);
+        try {
+            $this->_getResponse();
+        } catch (Net_TokyoTyrantProtocolException $e) {
+            $result_count = $this->_getInt4();
+            throw $e;
+        }
+        $result_count = $this->_getInt4();
+        $result = array();
+        for ($i = 0 ; $i < $result_count; $i++) {
+            $result[] = $this->_getValue();
+        }
+        return $result;
+    }
}