From dbaca4588df194c02ec7e88fd3b42f5d437c92d9 Mon Sep 17 00:00:00 2001 From: Alvaro Videla Date: Thu, 29 Apr 2010 17:32:38 +0800 Subject: [PATCH] initial commit --- PHPCask.class.php | 45 ++++++++++++++++++++++++++++++++++++ README.md | 35 ++++++++++++++++++++++++++++ php_cask.erl | 58 +++++++++++++++++++++++++++++++++++++++++++++++ test.php | 9 ++++++++ 4 files changed, 147 insertions(+) create mode 100644 PHPCask.class.php create mode 100644 README.md create mode 100644 php_cask.erl create mode 100644 test.php diff --git a/PHPCask.class.php b/PHPCask.class.php new file mode 100644 index 0000000..f398737 --- /dev/null +++ b/PHPCask.class.php @@ -0,0 +1,45 @@ +link = peb_connect($node, $cookie); + if(!$this->link) + { + throw new Exception(sprintf("Can't connect to node: %s with cookie: %s", $node, $cookie)); + } + } + + public function put($key, $value) + { + $x = peb_encode("[~b, ~b]", array(array($key, $value))); + $result = peb_rpc("php_cask", "put", $x, $this->link); + return peb_decode($result); + } + + public function get($key) + { + $x = peb_encode("[~b]", array(array($key))); + $result = peb_rpc("php_cask", "get", $x, $this->link); + return peb_decode($result); + } + + public function delete($key) + { + $x = peb_encode("[~b]", array(array($key))); + $result = peb_rpc("php_cask", "delete", $x, $this->link); + return peb_decode($result); + } + + public function list_keys() + { + $x = peb_encode("[]", array(array())); + $result = peb_rpc("php_cask", "list_keys", $x, $this->link); + return peb_decode($result); + } + +} +?> \ No newline at end of file diff --git a/README.md b/README.md new file mode 100644 index 0000000..cb006fc --- /dev/null +++ b/README.md @@ -0,0 +1,35 @@ +# PHPCask # + +PHPCask is client for the Bitcask K/V Store recently released by [Basho](http://blog.basho.com/2010/04/27/hello,-bitcask/) + +On the PHP side it requires the [PHP Erlang Bridge Extension](http://code.google.com/p/mypeb/) + +Note that this client is *very* simple. It's aim is to showcase what can be done with the PHP Erlang Bridge Extension and with Bitcask from PHP. + +## Running The Tests ## + +Open the Terminal and type: + + > cd path/to/bitcask/ebin + > erlc path/to/phpcask/php_cask.erl + > erl -sname phpcask + +Inside the erlang CLI: + + > php_cask:start(). + +Open another Terminal window and type: + + > cd path/to/phpcask/ + > php test.php erlang_cookie + +You can read the test.php file to see how does it works. + +*erlang_cookie* must be the value of the Erlang Cookie used by the node running Bitcask. + +# TODO # + +* Improve error handling +* Implement the complete Bitcask API +* Add more test +* Fork this project and improve it _that's you :)_ \ No newline at end of file diff --git a/php_cask.erl b/php_cask.erl new file mode 100644 index 0000000..269dfd2 --- /dev/null +++ b/php_cask.erl @@ -0,0 +1,58 @@ +-module(php_cask). + +-behaviour(gen_server). + +-export([start/0, start_link/0, stop/0]). + +-export([init/1, handle_call/3, handle_cast/2, handle_info/2, + terminate/2, code_change/3]). + +-export([get/1, put/2, delete/1, list_keys/0]). + +start() -> gen_server:start({local, ?MODULE}, ?MODULE, [], []). +start_link() -> gen_server:start_link({local, ?MODULE}, ?MODULE, [], []). +stop() -> gen_server:cast(?MODULE, stop). + +-record(state, {ref}). + +get(Key) -> gen_server:call(?MODULE, {get, Key}). + +put(Key, Value) -> + gen_server:call(?MODULE, {put, Key, Value}). + +delete(Key) -> + gen_server:call(?MODULE, {delete, Key}). + +list_keys() -> + gen_server:call(?MODULE, {list_keys}). + +init([]) -> + Bitcask = bitcask:open("./data", [read_write]), + {ok, #state{ref=Bitcask}}. + +handle_call({get, Key}, _From, #state{ref=Bitcask}=State) -> + Reply = + case bitcask:get(Bitcask, Key) of + {ok, Value} -> binary_to_term(Value); + _ -> not_found + end, + {reply, Reply, State}; + +handle_call({put, Key, Value}, _From, #state{ref=Bitcask}=State) -> + Reply = bitcask:put(Bitcask, Key, term_to_binary(Value)), + {reply, Reply, State}; + +handle_call({delete, Key}, _From, #state{ref=Bitcask}=State) -> + Reply = bitcask:delete(Bitcask, Key), + {reply, Reply, State}; + +handle_call({list_keys}, _From, #state{ref=Bitcask}=State) -> + Reply = bitcask:list_keys(Bitcask), + {reply, Reply, State}. + +handle_cast(stop, State) -> {stop, normal, State}. +handle_info(_Info, State) -> + error_logger:info_msg("handle_info ~p ~p.~n", [_Info, State]), + {noreply, State}. +terminate(_Reason, _State) -> ok. +code_change(_OldVsn, State, _Extra) -> {ok, State}. \ No newline at end of file diff --git a/test.php b/test.php new file mode 100644 index 0000000..ca16151 --- /dev/null +++ b/test.php @@ -0,0 +1,9 @@ +put("foo", "bar")); +var_dump($phpcask->get("foo")); +var_dump($phpcask->delete("foo")); +var_dump($phpcask->get("foo")); \ No newline at end of file