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
Showing
6 changed files
with
159 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,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. |
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,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"}} | ||
]}. |
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,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} | ||
]} | ||
]}. |
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,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}. |
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,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. |
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,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}). | ||
|
||
|
||
|
||
|