Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit dbaca45
Showing
4 changed files
with
147 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
|
||
} | ||
?> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 :)_ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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}. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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")); |