Skip to content

Commit

Permalink
initial import
Browse files Browse the repository at this point in the history
  • Loading branch information
cstar committed Nov 6, 2009
0 parents commit e45cdf6
Show file tree
Hide file tree
Showing 16 changed files with 722 additions and 0 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
.hg/
.hgignore
*.beam

22 changes: 22 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
Copyright (c) 2009
adroll.com
Valentino Volonghi

Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
35 changes: 35 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
LIBDIR=`erl -eval 'io:format("~s~n", [code:lib_dir()])' -s init stop -noshell`

all:
mkdir -p ebin/
(cd src;$(MAKE))
# test compile fails if eunit not present
#(cd test;$(MAKE))

clean: clean_tests
(cd src;$(MAKE) clean)
rm -rf erl_crash.dump *.beam

clean_tests:
(cd test;$(MAKE) clean)
rm -rf erl_crash.dump *.beam

test: clean
mkdir -p ebin/
(cd src;$(MAKE))
(cd test;$(MAKE))
(cd test;$(MAKE) test)

testrun: all
mkdir -p ebin/
(cd test;$(MAKE) test)

install: all
# original "mkdir -p {LIBDIR}/erldis-0.0.1/{ebin,include}"
# actually makes a directory called {ebin,include}
mkdir -p ${LIBDIR}/erldis-0.0.1/ebin
mkdir -p ${LIBDIR}/erldis-0.0.1/include
for i in ebin/*.beam; do install $$i $(LIBDIR)/erldis-0.0.1/$$i ; done
for i in include/*.hrl; do install $$i $(LIBDIR)/erldis-0.0.1/$$i ; done
# also install .app file
install ebin/erldis.app $(LIBDIR)/erldis-0.0.1/ebin/erldis.app
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
erldis : erlang client library for redis
----------------------------------------

This is a fork from over at [bitbucket](http://bitbucket.org/adroll/erldis).

Code written by [Valentino Volonghi](http://bitbucket.org/dialtone/)
10 changes: 10 additions & 0 deletions ebin/erldis.app
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{application, erldis, [
{description, "Erlang Redis application"},
{vsn, "0.0.1"},
{registered, [erldis_sup]},
{mod, {erldis_app, []}},
% TODO: include eunit?
{applications, [kernel, stdlib]},
{modules, [erldis_client, erldis, erldis_proto, erldis_app, erldis_sup]},
{env, [{host, "localhost"}, {port, 6379}, {timeout, 500}]}
]}.
1 change: 1 addition & 0 deletions include/erldis.hrl
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
-record(redis, {socket,buffer=[],reply_caller,calls=0,remaining=0,pstate=empty,results=[]}).
9 changes: 9 additions & 0 deletions src/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
include ../support/include.mk

all: $(EBIN_FILES)

debug:
$(MAKE) DEBUG=-DDEBUG

clean:
rm -rf $(EBIN_FILES) erl_crash.dump
116 changes: 116 additions & 0 deletions src/erldis.erl
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
-module(erldis).

-compile(export_all).
-define(EOL, "\r\n").

%% helpers
flatten({error, Message}) ->
{error, Message};
flatten(List) when is_list(List)->
lists:flatten(List).

%% exposed API
connect(Host) ->
erldis_client:connect(Host).
connect(Host, Port) ->
erldis_client:connect(Host, Port).
connect(Host, Port, Options) ->
erldis_client:connect(Host, Port, Options).

quit(Client) ->
erldis_client:asend(Client, "QUIT"),
erldis_client:disconnect(Client).

%% Commands operating on string values
internal_set_like(Client, Command, Key, Value) ->
erldis_client:send(Client, Command, [[Key, length(Value)],
[Value]]).

get_all_results(Client) -> erldis_client:get_all_results(Client).

auth(Client, Password) -> erldis_client:ssend(Client, auth, [Password]).

set(Client, Key, Value) -> internal_set_like(Client, set, Key, Value).
get(Client, Key) -> erldis_client:ssend(Client, get, [Key]).
getset(Client, Key, Value) -> internal_set_like(Client, getset, Key, Value).
mget(Client, Keys) -> erldis_client:ssend(Client, mget, Keys).
setnx(Client, Key, Value) -> internal_set_like(Client, setnx, Key, Value).
incr(Client, Key) -> erldis_client:ssend(Client, incr, [Key]).
incrby(Client, Key, By) -> erldis_client:ssend(Client, incrby, [Key, By]).
decr(Client, Key) -> erldis_client:ssend(Client, decr, [Key]).
decrby(Client, Key, By) -> erldis_client:ssend(Client, decrby, [Key, By]).



%% Commands operating on every value
exists(Client, Key) -> erldis_client:ssend(Client, exists, [Key]).
del(Client, Key) -> erldis_client:ssend(Client, del, [Key]).
type(Client, Key) -> erldis_client:ssend(Client, type, [Key]).
keys(Client, Pattern) -> erldis_client:ssend(Client, keys, [Pattern]).
randomkey(Client, Key) -> erldis_client:ssend(Client, randomkey, [Key]).
rename(Client, OldKey, NewKey) -> erldis_client:ssend(Client, rename, [OldKey, NewKey]).
renamenx(Client, OldKey, NewKey) -> erldis_client:ssend(Client, renamenx, [OldKey, NewKey]).
dbsize(Client) -> erldis_client:ssend(Client, dbsize).
expire(Client, Key, Seconds) -> erldis_client:ssend(Client, expire, [Key, Seconds]).
ttl(Client, Key) -> erldis_client:ssend(Client, ttl, [Key]).



%% Commands operating on lists
rpush(Client, Key, Value) -> internal_set_like(Client, rpush, Key, Value).
lpush(Client, Key, Value) -> internal_set_like(Client, lpush, Key, Value).
llen(Client, Key) -> erldis_client:ssend(Client, llen, [Key]).
lrange(Client, Key, Start, End) -> erldis_client:ssend(Client, lrange, [Key, Start, End]).
ltrim(Client, Key, Start, End) -> erldis_client:ssend(Client, ltrim, [Key, Start, End]).
lindex(Client, Key, Index) -> erldis_client:ssend(Client, lindex, [Key, Index]).
lset(Client, Key, Index, Value) ->
erldis_client:send(Client, lset, [[Key, Index, length(Value)],
[Value]]).
lrem(Client, Key, Number, Value) ->
erldis_client:send(Client, lrem, [[Key, Number, length(Value)],
[Value]]).
lpop(Client, Key) -> erldis_client:ssend(Client, lpop, [Key]).
rpop(Client, Key) -> erldis_client:ssend(Client, rpop, [Key]).



%% Commands operating on sets
sadd(Client, Key, Value) -> internal_set_like(Client, sadd, Key, Value).
srem(Client, Key, Value) -> internal_set_like(Client, srem, Key, Value).
smove(Client, SrcKey, DstKey, Member) -> erldis_client:send(Client, smove, [[SrcKey, DstKey, length(Member)],
[Member]]).
scard(Client, Key) -> erldis_client:ssend(Client, scard, [Key]).
sismember(Client, Key, Value) -> internal_set_like(Client, sismember, Key, Value).
sintersect(Client, Keys) -> erldis_client:ssend(Client, sinter, Keys).
sinter(Client, Keys) -> sintersect(Client, Keys).
sinterstore(Client, DstKey, Keys) -> erldis_client:ssend(Client, sinterstore, [DstKey|Keys]).
sunion(Client, Keys) -> erldis_client:ssend(Client, sunion, Keys).
sunionstore(Client, DstKey, Keys) -> erldis_client:ssend(Client, sunionstore, [DstKey|Keys]).
sdiff(Client, Keys) -> erldis_client:ssend(Client, sdiff, Keys).
sdiffstore(Client, DstKey, Keys) -> erldis_client:ssend(Client, sdiffstore, [DstKey|Keys]).
smembers(Client, Key) -> erldis_client:ssend(Client, smembers, [Key]).


%% Multiple DB commands
select(Client, Index) -> erldis_client:ssend(Client, select, [Index]).
move(Client, Key, DBIndex) -> erldis_client:ssend(Client, move, [Key, DBIndex]).
flushdb(Client) -> erldis_client:ssend(Client, flushdb).
flushall(Client) -> erldis_client:ssend(Client, flushall).


%% Commands operating on both lists and sets
sort(Client, Key) -> erldis_client:ssend(Client, sort, [Key]).
sort(Client, Key, Extra) -> erldis_client:ssend(Client, sort, [Key, Extra]).


%% Persistence control commands
save(Client) -> erldis_client:ssend(Client, save).
bgsave(Client) -> erldis_client:ssend(Client, bgsave).
lastsave(Client) -> erldis_client:ssend(Client, lastsave).
shutdown(Client) -> erldis_client:asend(Client, shutdown).


%% Remote server control commands
info(Client) -> erldis_client:ssend(Client, info).
slaveof(Client, Host, Port) -> erldis_client:ssend(Client, slaveof, [Host, Port]).
slaveof(Client) -> erldis_client:ssend(Client, slaveof, ["no one"]).
18 changes: 18 additions & 0 deletions src/erldis_app.erl
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
% japerk: do an erlang application with supervisor for client so that there
% is only one connection. Otherwise was getting issues with gen_tcp hanging
% on connect, even with a Timeout given.
-module(erldis_app).

-behaviour(application).

-export([start/2, stop/1]).

start(_Type, _Args) -> erldis_sup:start_link().

stop(Client) when is_pid(Client) ->
erldis:quit(Client);
stop(_State) ->
case erldis_sup:client() of
undefined -> ok;
Client -> erldis:quit(Client)
end.
Loading

0 comments on commit e45cdf6

Please sign in to comment.