Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
videlalvaro committed Apr 29, 2010
0 parents commit dbaca45
Show file tree
Hide file tree
Showing 4 changed files with 147 additions and 0 deletions.
45 changes: 45 additions & 0 deletions PHPCask.class.php
@@ -0,0 +1,45 @@
<?php

class PHPCask
{
protected $link;

public function __construct($node, $cookie)
{
$this->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);
}

}
?>
35 changes: 35 additions & 0 deletions 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 :)_
58 changes: 58 additions & 0 deletions 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}.
9 changes: 9 additions & 0 deletions test.php
@@ -0,0 +1,9 @@
<?php

require_once('./PHPCask.class.php');

$phpcask = new PHPCask('phpcask@localhost', $argv[1]);
var_dump($phpcask->put("foo", "bar"));
var_dump($phpcask->get("foo"));
var_dump($phpcask->delete("foo"));
var_dump($phpcask->get("foo"));

0 comments on commit dbaca45

Please sign in to comment.