Skip to content

Commit

Permalink
New version 0.2
Browse files Browse the repository at this point in the history
  • Loading branch information
lpgauth committed May 3, 2011
1 parent 9a8e18f commit e48c2ad
Show file tree
Hide file tree
Showing 6 changed files with 159 additions and 0 deletions.
10 changes: 10 additions & 0 deletions README
@@ -0,0 +1,10 @@
-include("cassandra_thrift/include/cassandra_types.hrl").

ColumnPath = #columnPath{column_family="emails", column="username"},

case cassanderl_sup:call(get, ["example@example.com", ColumnPath, 1]) of
{ok, {ok, R1}} ->
R1#columnOrSuperColumn.column#column.value;
{exception, notFoundException} ->
undefined
end.
7 changes: 7 additions & 0 deletions rebar.config
@@ -0,0 +1,7 @@
{erl_opts, [debug_info]}.
{deps, [
{cassandra_thrift, "19.4.0",
{git, "https://github.com/lpgauth/cassandra-thrift-erlang.git", "19.4.0"}},
{thrift, "0.6.0",
{git, "https://github.com/lpgauth/thrift-erlang.git", "master"}}
]}.
13 changes: 13 additions & 0 deletions src/cassanderl.app.src
@@ -0,0 +1,13 @@
{application, cassanderl, [
{description, "Cassandra client"},
{vsn, "0.2"},
{registered, []},
{applications, [kernel, stdlib]},
{mod, {cassanderl_app, []}},
{env, [
{hostname, "127.0.0.1"},
{port, 9160},
{keyspace, "Keyspace1"},
{worker_pool_size, 10}
]}
]}.
61 changes: 61 additions & 0 deletions src/cassanderl.erl
@@ -0,0 +1,61 @@
-module(cassanderl).

-behaviour(gen_server).

%% API
-export([start_link/1]).

%% gen_server callbacks
-export([init/1, handle_call/3, handle_cast/2, handle_info/2,
terminate/2, code_change/3]).

-record(state, {conn}).
-define(SERVER, ?MODULE).

%%====================================================================
%% API
%%====================================================================

start_link(Name) ->
gen_server:start_link({local, Name}, ?MODULE, [], []).

%%====================================================================
%% gen_server callbacks
%%====================================================================

init([]) ->
%% Open Connection
{ok, Hostname} = application:get_env(cassanderl, hostname),
{ok, Port} = application:get_env(cassanderl, port),
{ok, Conn} = thrift_client_util:new(Hostname, Port, cassandra_thrift, [{framed, true}]), ok,

%% Set KeySpace
{ok, KeySpace} = application:get_env(keyspace),
{Conn2, {ok, ok}} = thrift_client:call(Conn, set_keyspace, [KeySpace]),
{ok, #state{conn = Conn2}}.

handle_call({call, Function, Args}, _From, #state{conn=Conn}=State) ->
try thrift_client:call(Conn, Function, Args) of
{NewConn, Response} ->
NewState = State#state{conn=NewConn},
{reply, {ok, Response}, NewState}
catch
{NewConn, {exception, {Exception}}} ->
NewState = State#state{conn=NewConn},
{reply, {exception, Exception}, NewState}
end;
handle_call(_Request, _From, State) ->
{reply, ok, State}.

handle_cast(_Msg, State) ->
{noreply, State}.

handle_info(_Info, State) ->
{noreply, State}.

terminate(_Reason, #state{conn=Conn}) ->
thrift_client:close(Conn),
ok.

code_change(_OldVsn, State, _Extra) ->
{ok, State}.
16 changes: 16 additions & 0 deletions src/cassanderl_app.erl
@@ -0,0 +1,16 @@
-module(cassanderl_app).

-behaviour(application).

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

%% ===================================================================
%% Application callbacks
%% ===================================================================

start(_StartType, _StartArgs) ->
cassanderl_sup:start_link().

stop(_State) ->
ok.
52 changes: 52 additions & 0 deletions src/cassanderl_sup.erl
@@ -0,0 +1,52 @@

-module(cassanderl_sup).

-behaviour(supervisor).

%% API
-export([start_link/0, pick_worker/0, call/2]).

%% Supervisor callbacks
-export([init/1]).

%% ===================================================================
%% API functions
%% ===================================================================

start_link() ->
supervisor:start_link({local, ?MODULE}, ?MODULE, []).

%% ===================================================================
%% Supervisor callbacks
%% ===================================================================

init([]) ->
{ok, Size} = application:get_env(cassanderl, worker_pool_size),
put(worker_pool_size, Size),
Workers = [ worker_spec(I) || I <- lists:seq(1, Size) ],
{ok, {{one_for_one, 10, 1}, Workers}}.

%% ===================================================================
%% Internal API
%% ===================================================================

worker_spec(N) ->
Name = list_to_atom("cassanderl_" ++ integer_to_list(N)),
{Name,
{cassanderl, start_link, [Name]},
permanent, 1000, worker,
[cassanderl]
}.

pick_worker() ->
random:seed(erlang:now()),
RandomN = random:uniform(get(worker_pool_size)),
list_to_existing_atom("cassanderl_" ++ integer_to_list(RandomN)).

call(Function, Args) ->
Worker = pick_worker(),
gen_server:call(Worker, {call, Function, Args}).




0 comments on commit e48c2ad

Please sign in to comment.