powered by nequal
Home » Net_TokyoTyrant » Timeline » 1025

Changeset 1025 -- 2009-07-19 16:29:48

Comment
[Add Tag:Release] Net_TokyoTyrant

Diffs

Net_TokyoTyrant/tags/0.2.1-beta/sample/sample_1.php

@@ -0,0 +1,11 @@
+<?php
+require_once '../Net/TokyoTyrant.php';
+$tt = new Net_TokyoTyrant();
+$tt->connect('localhost', 1978);
+$tt->put('oyomesan', 'nounai');
+var_dump($tt->get('oyomesan'));
+$tt->put('kanozyo', 'pc no naka');
+$tt->put('kareshi', 'otoko ha chotto');
+var_dump($tt->mget(array('oyomesan', 'kanozyo')));
+var_dump($tt->fwmkeys('ka', 100)); //ka
+

Net_TokyoTyrant/tags/0.2.1-beta/tests/tmp_test.php

@@ -0,0 +1,108 @@
+<?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/tags/0.2.1-beta/tests/benchmark/benchmark.php

@@ -0,0 +1,31 @@
+<?php
+require_once dirname(dirname(__FILE__)) . '/Net/TokyoTyrant.php';
+require_once 'Benchmark/Timer.php';
+$timer = new Benchmark_Timer();
+$timer->start();
+
+$tt = new Net_TokyoTyrant();
+$timer->setMarker('create');
+$tt->connect('localhost', 1978);
+$timer->setMarker('connect');
+assert($tt->vanish() === true);
+$timer->setMarker('vanish');
+
+$data = 'aaaaaaa';
+for($i = 0;$i <= 10000 ;$i++){
+   $key = (string) rand(1,100);
+   $tt->put($key, $data);
+}
+
+$timer->setMarker('put');
+
+for($i = 0;$i <= 10000 ;$i++){
+   $key = (string) rand(1,100);
+  $data = $tt->get($key);
+}
+
+$timer->setMarker('get');
+
+$tt->close();
+$timer->setMarker('close');
+$timer->display();

Net_TokyoTyrant/tags/0.2.1-beta/tests/benchmark/memcache_benchmark.php

@@ -0,0 +1,28 @@
+<?php
+require_once 'Benchmark/Timer.php';
+$timer = new Benchmark_Timer();
+$timer->start();
+
+$memcache = new memcache();
+$timer->setMarker('create');
+$memcache->addServer('localhost');
+$timer->setMarker('connect');
+
+$data = 'aaaaaaa';
+for($i = 0;$i <= 10000 ;$i++){
+   $key = (string) rand(1,100);
+   $memcache->set($key, $data);
+}
+
+$timer->setMarker('put');
+
+for($i = 0;$i <= 10000 ;$i++){
+   $key = (string) rand(1,100);
+   $data = $memcache->get($key);
+}
+
+$timer->setMarker('get');
+
+$memcache->close();
+$timer->setMarker('close');
+$timer->display();

Net_TokyoTyrant/tags/0.2.1-beta/tests/benchmark/mysql_benchmark.php

@@ -0,0 +1,31 @@
+<?php
+require_once 'Benchmark/Timer.php';
+$timer = new Benchmark_Timer();
+$timer->start();
+
+$timer->setMarker('create');
+$dbh = new PDO('mysql:host=localhost;dbname=benchmark', 'root');
+$timer->setMarker('connect');
+
+$stmt = $dbh->prepare("INSERT INTO bench (name, value) VALUES (:name, :value)");
+$value = 'aaaaaa';
+for($i = 0;$i <= 10000 ;$i++){
+    $name = rand(1, 1000);
+    $stmt->bindParam(':name', $name);
+    $stmt->bindParam(':value', $value);
+    $stmt->execute();
+}
+
+$timer->setMarker('put');
+$stmt = $dbh->prepare("SELECT * FROM bench where name = ?");
+for($i = 0;$i <= 10000 ;$i++){
+    $name = (string) rand(1, 1000);
+    $stmt->execute(array($name));
+    $row = $stmt->fetch();
+}
+
+$timer->setMarker('get');
+
+$dbh = null;
+$timer->setMarker('close');
+$timer->display();

Net_TokyoTyrant/tags/0.2.1-beta/tests/benchmark/mysql_benchmark.sql

@@ -0,0 +1,4 @@
+CREATE TABLE bench(
+  name VARCHAR(255) primary key,
+  value VARCHAR(255)
+);

Net_TokyoTyrant/tags/0.2.1-beta/Net/TokyoTyrant.php

@@ -0,0 +1,464 @@
+<?php
+class Net_TokyoTyrantException extends Exception {};
+class Net_TokyoTyrantNetworkException extends Net_TokyoTyrantException {};
+class Net_TokyoTyrantProtocolException extends Net_TokyoTyrantException {};
+
+// License: MIT
+// author: Keita Arai <cocoiti@gmail.com>
+
+class Net_TokyoTyrant
+{
+    private
+      $connect = false;
+    private
+      $socket;
+    private
+      $errorNo, $errorMessage;
+    private
+      $socket_timeout;
+
+    const RDBXOLCKNON = 0;
+    const RDBXOLCKREC = 1;
+    const RDBXOLCKGLB = 2;
+
+    public function connect($server, $port, $timeout = 10)
+    {
+        $this->close();
+        $this->socket = @fsockopen($server,$port, $this->errorNo, $errorMessage, $timeout);
+        if (! $this->socket) {
+            throw new Net_TokyoTyrantNetworkException(sprintf('%s, %s', $this->errorNo, $errorMessage));
+        }
+        $this->connect = true;
+    }
+
+    public function setTimeout($timeout)
+    {
+        $this->socket_timeout = $timeout;
+        stream_set_timeout($this->socket, $timeout);
+    }
+
+    public function getTimeout()
+    {
+        return $this->socket_timeout;
+    }
+
+    public function close()
+    {
+        if ($this->connect) {
+            fclose($this->socket);
+        }
+    }
+
+
+    private function _read($length)
+    {
+        if ($this->connect === false) {
+            throw new Net_TokyoTyrantException('not connected');
+        }
+
+        if (@feof($this->socket))
+        {
+            throw new Net_TokyoTyrantNetworkException('socket read eof error');
+        }
+
+        $result = $this->_fullread($this->socket, $length);
+        if ($result === false) {
+            throw new Net_TokyoTyrantNetworkException('socket read error');
+        }
+        return $result;
+    }
+
+
+    private function _write($data)
+    {
+        $result = $this->_fullwrite($this->socket, $data);
+        if ($result === false) {
+            throw new Net_TokyoTyrantNetworkException('socket read error');
+        }
+    }
+
+    private function _fullread ($sd, $len) {
+        $ret = '';
+        $read = 0;
+
+        while ($read < $len && ($buf = fread($sd, $len - $read))) {
+            $read += strlen($buf);
+            $ret .= $buf;
+        }
+
+        return $ret;
+    }
+
+    private function _fullwrite ($sd, $buf) {
+        $total = 0;
+        $len = strlen($buf);
+
+        while ($total < $len && ($written = fwrite($sd, $buf))) {
+            $total += $written;
+            $buf = substr($buf, $written);
+        }
+
+        return $total;
+    }
+
+    private function _doRequest($cmd, $values = array())
+    {
+        $this->_write($cmd . $this->_makeBin($values));
+    }
+
+    private function _makeBin($values){
+        $int = '';
+        $str = '';
+
+        foreach ($values as  $value) {
+            if (is_array($value)) {
+                $str .= $this->_makeBin($value);
+                continue;
+            }
+
+            if (! is_int($value)) {
+                $int .= pack('N', strlen($value));
+                $str .= $value;
+                continue;
+            }
+
+            $int .= pack('N', $value);
+        }
+        return $int . $str;
+    }
+
+
+    protected function _getResponse()
+    {
+        $res = fread($this->socket, 1);
+        $res = unpack('c', $res);
+        if ($res[1] !== 0) {
+            throw new Net_TokyoTyrantProtocolException('Error Response');
+        }
+        return true;
+    }
+
+
+    protected function _getInt1()
+    {
+        $result = '';
+        $res = $this->_read(1);
+        $res = unpack('c', $res);
+        return $res[1];
+    }
+
+    protected function _getInt4()
+    {
+        $result = '';
+        $res = $this->_read(4);
+        $res = unpack('N', $res);
+        return $res[1];
+    }
+
+    protected function _getInt8()
+    {
+        $result = '';
+        $res = $this->_read(8);
+        $res = unpack('N*', $res);
+        return array($res[1], $res[2]);
+    }
+
+    protected function _getValue()
+    {
+        $result = '';
+        $size = $this->_getInt4();
+        return $this->_read($size);
+    }
+
+
+    protected function _getKeyValue()
+    {
+        $result = array();
+        $ksize = $this->_getInt4();
+        $vsize = $this->_getInt4();
+        $result[] = $this->_read($ksize);
+        $result[] = $this->_read($vsize);
+        return $result;
+    }
+
+    protected function _getData()
+    {
+        $result = '';
+        $size = $this->_getInt4();
+        if ($size === 0) {
+            return '';
+        }
+        return $this->_read((int) $size);
+    }
+
+    protected function _getDataList()
+    {
+        $result = array();
+
+        $listCount = $this->_getInt4();
+        for($i = 0;$i < $listCount; $i++) {
+            $result[] = $this->_getValue();
+        }
+        return $result;
+    }
+
+
+    protected function _getKeyValueList()
+    {
+        $result = array();
+
+        $listCount = $this->_getInt4();
+        for($i = 0;$i < $listCount; $i++) {
+            list($key, $value)  = $this->_getKeyValue();
+            $result[$key] = $value;
+        }
+        return $result;
+    }
+
+    public function put($key, $value)
+    {
+        $cmd = pack('c*', 0xC8,0x10);
+        $this->_doRequest($cmd, array((string) $key,(string) $value));
+        try {
+            $this->_getResponse();
+        } catch (Net_TokyoTyrantProtocolException $e) {
+            return false;
+        }
+        return true;
+    }
+
+    public function putkeep($key, $value)
+    {
+        $cmd = pack('c*', 0xC8,0x11);
+        $this->_doRequest($cmd, array((string) $key,(string) $value));
+        try {
+            $this->_getResponse();
+        } catch (Net_TokyoTyrantProtocolException $e) {
+            return false;
+        }
+        return true;
+    }
+
+    public function putcat($key, $value)
+    {
+        $cmd = pack('c*', 0xC8,0x12);
+        $this->_doRequest($cmd, array((string) $key,(string) $value));
+        try {
+            $this->_getResponse();
+        } catch (Net_TokyoTyrantProtocolException $e) {
+            return false;
+        }
+        return true;
+    }
+
+    public function putrtt($key, $value, $width)
+    {
+        $cmd = pack('c*', 0xC8,0x13);
+        $this->_doRequest($cmd, array((string) $key, (string) $value, $width));
+        try {
+            $this->_getResponse();
+        } catch (Net_TokyoTyrantProtocolException $e) {
+            return false;
+        }
+        return true;
+    }
+
+    public function putnr($key, $value)
+    {
+        $cmd = pack('c*', 0xC8,0x18);
+        $this->_doRequest($cmd, array((string) $key, (string) $value, (int) $width));
+        try {
+            $this->_getResponse();
+        } catch (Net_TokyoTyrantProtocolException $e) {
+            return ;
+        }
+        return ;
+    }
+
+    public function out($key)
+    {
+        $cmd = pack('c*', 0xC8,0x20);
+        $this->_doRequest($cmd, array((string) $key));
+        try {
+            $this->_getResponse();
+        } catch (Net_TokyoTyrantProtocolException $e) {
+            return false;
+        }
+        return true;
+    }
+
+    public function get($key)
+    {
+        $cmd = pack('c*', 0xC8,0x30);
+        $this->_doRequest($cmd, array((string) $key));
+        try {
+            $this->_getResponse();
+        } catch (Net_TokyoTyrantProtocolException $e) {
+            return false;
+        }
+        return $this->_getData();
+    }
+
+    public function mget($keys)
+    {
+        $cmd = pack('c*', 0xC8,0x31);
+        $values = array();
+        $values[] = count($keys);
+        foreach($keys as $key) {
+          $values[] = array((string) $key);
+        }
+
+        $this->_doRequest($cmd, $values);
+        try {
+            $this->_getResponse();
+        } catch (Net_TokyoTyrantProtocolException $e) {
+            return false;
+        }
+        return $this->_getKeyValueList();
+    }
+
+    public function fwmkeys($prefix, $max)
+    {
+        $cmd = pack('c*', 0xC8,0x58);
+        $this->_doRequest($cmd, array((string) $prefix, (int) $max));
+        $this->_getResponse();
+        return $this->_getDataList();
+    }
+
+    public function addint($key, $num)
+    {
+        $cmd = pack('c*', 0xC8,0x60);
+        $this->_doRequest($cmd, array((string) $key, (int) $num));
+        $this->_getResponse();
+        return $this->_getInt4();
+    }
+
+    public function putint($key, $num)
+    {
+        //This Code is non support
+        $value = pack('V', $num);
+        return $this->put($key, $value);
+    }
+
+    public function getint($key)
+    {
+        return $this->addint($key, 0);
+    }
+
+    public function adddouble($key, $integ, $fract)
+    {
+        $cmd = pack('c*', 0xC8,0x61);
+        $this->_doRequest($cmd, array((string) $key, (int) $intteg, (int) $fract));
+        $this->_getResponse();
+        return array($this->_getInt8(), $this->_getInt8());
+    }
+
+
+    public function ext($extname, $key, $value, $option = 0)
+    {
+        $cmd = pack('c*', 0xC8,0x68);
+        $this->_doRequest($cmd, array((string) $extname, (int) $option, (string) $key, (string) $value));
+        $this->_getResponse();
+        return $this->_getData();
+    }
+
+    public function vsize($key)
+    {
+        $cmd = pack('c*', 0xC8,0x38);
+        $this->_doRequest($cmd, array((string) $key));
+        $this->_getResponse();
+        return $this->_getInt4();
+    }
+
+    public function iterinit()
+    {
+        $cmd = pack('c*', 0xC8,0x50);
+        $this->_doRequest($cmd);
+        $this->_getResponse();
+        return true;
+    }
+
+    public function iternext()
+    {
+        $cmd = pack('c*', 0xC8,0x51);
+        $this->_doRequest($cmd);
+        try {
+            $this->_getResponse();
+        } catch (Net_TokyoTyrantProtocolException $e) {
+            return false;
+        }
+        return $this->_getValue();
+    }
+
+    public function sync()
+    {
+        $cmd = pack('c*', 0xC8,0x70);
+        $this->_doRequest($cmd);
+        $this->_getResponse();
+        return true;
+    }
+
+    public function optimize($param)
+    {
+        $cmd = pack('c*', 0xC8,0x71);
+        $this->_doRequest($cmd, array((string) $param));
+        $this->_getResponse();
+        return true;
+    }
+
+    public function vanish()
+    {
+        $cmd = pack('c*', 0xC8,0x72);
+        $this->_doRequest($cmd);
+        $this->_getResponse();
+        return true;
+    }
+
+    public function copy($path)
+    {
+        $cmd = pack('c*', 0xC8,0x73);
+        $this->_doRequest($cmd, array((string) $path));
+        $this->_getResponse();
+        return true;
+    }
+
+//    public function restore($path)
+//    {
+//        $cmd = pack('c*', 0xC8,0x74);
+//        $this->_doRequest($cmd, array((string) $path));
+//        $this->_getResponse();
+//        return true;
+//    }
+
+    public function setmst($host, $port)
+    {
+        $cmd = pack('c*', 0xC8,0x78);
+        $this->_doRequest($cmd, array((string) $host, (int) $port));
+        $this->_getResponse();
+        return true;
+    }
+
+    public function rnum()
+    {
+        $cmd = pack('c*', 0xC8,0x80);
+        $this->_doRequest($cmd);
+        $this->_getResponse();
+        return $this->_getInt8();
+    }
+
+    public function size()
+    {
+        $cmd = pack('c*', 0xC8,0x81);
+        $this->_doRequest($cmd);
+        $this->_getResponse();
+        return $this->_getInt8();
+    }
+
+    public function stat()
+    {
+        $cmd = pack('c*', 0xC8,0x88);
+        $this->_doRequest($cmd);
+        $this->_getResponse();
+        return $this->_getValue();
+    }
+}