Permalink
Browse files

Pass options into start_link; support QCSTROREQ

Use a more traditional approach to passing in options (directly instead
of only via get_env). Replace put_env (which doesn't exist) with
set_env. Fix various warnings. Support QCSTROREQ operator. Make all
principe_table functions take the socket as a first argument for
consistency.
  • Loading branch information...
1 parent d4eafd6 commit 9678cff9b34ebc832e6872932c1adc95b4620c3f Evan Miller committed Jan 30, 2012
View
12 src/medici.erl
@@ -57,13 +57,17 @@ start() ->
%% options until you restart your Erlang VM.
%% @end
start(StartupOptions) when is_list(StartupOptions) ->
- {ok, AppEnvOptions} = application:get_env(medici, options),
- CombinedOptions = [StartupOptions | AppEnvOptions],
+ AppEnvOptions = case application:get_env(medici, options) of
+ {ok, Val} -> Val;
+ undefined -> []
+ end,
+ CombinedOptions = StartupOptions ++ AppEnvOptions,
%% Merge into a single set of options, favoring those passed in
%% to start/1 over the app env.
MediciOptions = [{K, proplists:get_value(K, CombinedOptions)} ||
- K <- proplists:get_keys(CombinedOptions)],
- application:put_env(medici, {options, MediciOptions}),
+ K <- proplists:get_keys(CombinedOptions)],
+ application:load(medici),
+ ok = application:set_env(medici, options, MediciOptions),
application:start(medici).
stop() ->
View
8 src/medici_conn.erl
@@ -24,7 +24,7 @@
-behaviour(gen_server).
%% API
--export([start_link/0]).
+-export([start_link/0, start_link/1]).
%% gen_server callbacks
-export([init/1, handle_call/3, handle_cast/2, handle_info/2,
@@ -37,8 +37,10 @@
%% @spec start_link() -> {ok,Pid} | ignore | {error,Error}
%% @private Starts the connection handler
start_link() ->
- {ok, MediciOpts} = application:get_env(medici, options),
- gen_server:start_link(?MODULE, MediciOpts, []).
+ start_link([]).
+
+start_link(StartArgs) ->
+ gen_server:start_link(?MODULE, StartArgs, []).
%%====================================================================
%% gen_server callbacks
View
7 src/medici_conn_sup.erl
@@ -42,20 +42,19 @@ start_link(StartArgs) ->
%% to find out about restart strategy, maximum restart frequency and child
%% specifications.
%%--------------------------------------------------------------------
-init(StartArgs) ->
- {ok, MediciOpts} = application:get_env(options),
+init(MediciOpts) ->
ClientCount = proplists:get_value(num_connections, MediciOpts, ?NUM_CLIENTS),
case proplists:get_bool(native, MediciOpts) of
false ->
ChildList = [{list_to_atom("medici_connection_"++integer_to_list(ChildNum)),
- {medici_conn, start_link, StartArgs},
+ {medici_conn, start_link, [MediciOpts]},
permanent,
2000,
worker,
[medici_conn]} || ChildNum <- lists:seq(1, ClientCount)];
true ->
ChildList = [{list_to_atom("medici_connection_"++integer_to_list(ChildNum)),
- {medici_native_conn, start_link, StartArgs},
+ {medici_native_conn, start_link, [MediciOpts]},
permanent,
2000,
worker,
View
6 src/medici_controller.erl
@@ -10,7 +10,7 @@
-behaviour(gen_server).
%% API
--export([start_link/0]).
+-export([start_link/0, start_link/1]).
%% gen_server callbacks
-export([init/1, handle_call/3, handle_cast/2, handle_info/2,
@@ -28,7 +28,9 @@
%% Description: Starts the server
%%--------------------------------------------------------------------
start_link() ->
- {ok, MediciOpts} = application:get_env(options),
+ start_link([]).
+
+start_link(MediciOpts) ->
MyName = proplists:get_value(controller_name, MediciOpts, ?CONTROLLER_NAME),
gen_server:start_link({local, MyName}, ?MODULE, MediciOpts, []).
View
6 src/medici_native_conn.erl
@@ -26,7 +26,7 @@
-behaviour(gen_server).
%% API
--export([start_link/0]).
+-export([start_link/0, start_link/1]).
%% gen_server callbacks
-export([init/1, handle_call/3, handle_cast/2, handle_info/2,
@@ -39,7 +39,9 @@
%% @spec start_link() -> {ok,Pid} | ignore | {error,Error}
%% @private Starts the connection handler
start_link() ->
- {ok, MediciOpts} = application:get_env(options),
+ start_link([]).
+
+start_link(MediciOpts) ->
gen_server:start_link(?MODULE, MediciOpts, []).
%%====================================================================
View
6 src/medici_native_controller.erl
@@ -10,7 +10,7 @@
-behaviour(gen_server).
%% API
--export([start_link/0]).
+-export([start_link/0, start_link/1]).
%% gen_server callbacks
-export([init/1, handle_call/3, handle_cast/2, handle_info/2,
@@ -28,7 +28,9 @@
%% Description: Starts the server
%%--------------------------------------------------------------------
start_link() ->
- {ok, MediciOpts} = application:get_env(options),
+ start_link([]).
+
+start_link(MediciOpts) ->
MyName = proplists:get_value(controller, MediciOpts, ?CONTROLLER_NAME),
gen_server:start_link({local, MyName}, ?MODULE, MediciOpts, []).
View
21 src/medici_port_srv.erl
@@ -22,7 +22,7 @@
-behaviour(gen_server).
%% API
--export([start_link/0]).
+-export([start_link/0, start_link/1]).
%% gen_server callbacks
-export([init/1, handle_call/3, handle_cast/2, handle_info/2,
@@ -43,12 +43,10 @@
%%
%% @private Start the Tyrant port server
start_link() ->
- case application:get_env(options) of
- {ok, MediciOpts} ->
- ServerOpts = proplists:get_value(run_server, MediciOpts, []);
- _ ->
- ServerOpts = []
- end,
+ start_link([]).
+
+start_link(MediciOpts) ->
+ ServerOpts = proplists:get_value(run_server, MediciOpts, []),
case proplists:get_value(server_name, ServerOpts) of
undefined ->
gen_server:start_link({local, ?PORT_SRV_NAME}, ?MODULE, [], []);
@@ -59,15 +57,10 @@ start_link() ->
%%====================================================================
%% gen_server callbacks
%%====================================================================
-init(_Args) ->
+init(MediciOpts) ->
{ok, LogMatch} = re:compile(?LOG_REGEXP),
{ok, PidMatch} = re:compile(?PID_REGEXP),
- case application:get_env(options) of
- {ok, MediciOpts} ->
- ServerOpts = proplists:get_value(run_server, MediciOpts, []);
- _ ->
- ServerOpts = []
- end,
+ ServerOpts = proplists:get_value(run_server, MediciOpts, []),
process_flag(trap_exit, true),
start_server(ServerOpts, #state{log_match=LogMatch,
pid_match=PidMatch}).
View
18 src/medici_sup.erl
@@ -24,10 +24,11 @@
%% Description: Starts the supervisor
%%--------------------------------------------------------------------
start_link() ->
- supervisor:start_link(?MODULE, []).
+ start_link([]).
-start_link(StartArgs) ->
- supervisor:start_link(?MODULE, StartArgs).
+start_link(_StartArgs) ->
+ {ok, MediciOpts} = application:get_env(medici, options),
+ supervisor:start_link(?MODULE, MediciOpts).
%%====================================================================
%% Supervisor callbacks
@@ -41,26 +42,25 @@ start_link(StartArgs) ->
%% to find out about restart strategy, maximum restart frequency and child
%% specifications.
%%--------------------------------------------------------------------
-init(StartArgs) ->
- {ok, MediciOpts} = application:get_env(options),
+init(MediciOpts) ->
case proplists:get_bool(native, MediciOpts) of
false ->
MediciController = {controller,
- {medici_controller, start_link, StartArgs},
+ {medici_controller, start_link, [MediciOpts]},
permanent,
2000,
worker,
[medici_controller]};
true ->
MediciController = {controller,
- {medici_native_controller, start_link, StartArgs},
+ {medici_native_controller, start_link, [MediciOpts]},
permanent,
2000,
worker,
[medici_native_controller]}
end,
MediciConnSupervisor = {connection_supervisor,
- {medici_conn_sup, start_link, StartArgs},
+ {medici_conn_sup, start_link, [MediciOpts]},
permanent,
infinity,
supervisor,
@@ -71,7 +71,7 @@ init(StartArgs) ->
MediciConnSupervisor]}};
_ ->
MediciPortSupervisor = {port_supervisor,
- {medici_port_sup, start_link, StartArgs},
+ {medici_port_sup, start_link, [MediciOpts]},
permanent,
infinity,
supervisor,
View
44 src/principe_table.erl
@@ -23,8 +23,8 @@
-export([connect/0, connect/1, put/3, putkeep/3, putcat/3, update/3, out/2,
get/2, mget/2, vsiz/2, iterinit/1, iternext/1, fwmkeys/3, sync/1, optimize/2,
vanish/1, rnum/1, size/1, stat/1, copy/2, restore/3, addint/3, adddouble/3,
- adddouble/4, setmst/3, setindex/3, query_limit/3, query_limit/2, query_condition/4,
- query_order/3, search/2, genuid/1, searchcount/2, searchout/2]).
+ adddouble/4, setmst/3, setindex/3, query_limit/3, query_limit/4, query_add_condition/5,
+ query_order/4, search/2, genuid/1, searchcount/2, searchout/2]).
-include("principe.hrl").
@@ -94,7 +94,7 @@ columnize_values([], Current, Stack) ->
FinalStack = lists:reverse([list_to_binary(lists:reverse(Current)) | Stack]),
return_column_vals(FinalStack, []);
columnize_values([0 | T], [], Stack) ->
- columnize_values(T, [], Stack);
+ columnize_values(T, [], [<<"">>|Stack]);
columnize_values([0 | T], Current, Stack) ->
columnize_values(T, [], [list_to_binary(lists:reverse(Current)) | Stack]);
columnize_values([H | T], Current, Stack) ->
@@ -367,10 +367,11 @@ genuid(Socket) ->
Error
end.
-%% @spec query_condition(Query::proplist(),
-%% ColName::iolist(),
-%% Op::query_opcode(),
-%% ExprList::query_expr()) -> proplist()
+%% @spec query_add_condition(Socket::port(),
+%% Query::proplist(),
+%% ColName::iolist(),
+%% Op::query_opcode(),
+%% ExprList::query_expr()) -> proplist()
%%
%% @doc
%% Add a condition for a query. ExprList should be a list of one or more
@@ -380,7 +381,7 @@ genuid(Socket) ->
%% the last atom is no_index an existing index on the remote database server will
%% be bypassed.
%% @end
-query_condition(Query, ColName, Op, ExprList) when is_list(ExprList) ->
+query_add_condition(_Sock, Query, ColName, Op, ExprList) when is_list(ExprList) ->
[{{add_cond, ColName, Op, ExprList},
["addcond",
?NULL,
@@ -391,12 +392,13 @@ query_condition(Query, ColName, Op, ExprList) when is_list(ExprList) ->
convert_query_exprlist(ExprList)]
} | Query].
-%% @spec query_limit(Query::proplist(),
+%% @spec query_limit(Socket::port(),
+%% Query::proplist(),
%% Max::integer(),
%% Skip::integer()) -> proplist()
%%
%% @doc Set a limit on the number of returned values for Query, skip the first Skip records.
-query_limit(Query, Max, Skip) when is_integer(Max), Max > 0, is_integer(Skip), Skip >= 0 ->
+query_limit(_Sock, Query, Max, Skip) when is_integer(Max), Max > 0, is_integer(Skip), Skip >= 0 ->
LimitKey = {set_limit, Max, Skip},
LimitValue = ["setlimit",
?NULL,
@@ -410,21 +412,23 @@ query_limit(Query, Max, Skip) when is_integer(Max), Max > 0, is_integer(Skip), S
[{LimitKey, LimitValue} | proplists:delete(ExistingKey, Query)]
end.
-%% @spec query_limit(Query::proplist(),
+%% @spec query_limit(Socket::port(),
+%% Query::proplist(),
%% Max::integer()) -> proplist()
%%
%% @doc Set a limit on the number of returned values for Query.
%%
%% XXX: should the missing skip be 0 or -1 (protocol ref and perl versions seem to disagree)
-query_limit(Query, Max) ->
- query_limit(Query, Max, 0).
+query_limit(_Sock, Query, Max) ->
+ query_limit(_Sock, Query, Max, 0).
-%% @spec query_order(Query::proplist(),
+%% @spec query_order(Socket::port(),
+%% Query::proplist(),
%% ColName::index_col(),
%% Type::order_type()) -> proplist()
%%
%% @doc Set the order for returned values in Query.
-query_order(Query, primary, Type) when is_atom(Type) ->
+query_order(_Sock, Query, primary, Type) when is_atom(Type) ->
OrderKey = {set_order, primary, Type},
OrderValue = ["setorder",
?NULL,
@@ -437,7 +441,7 @@ query_order(Query, primary, Type) when is_atom(Type) ->
{value, ExistingKey} ->
[{OrderKey, OrderValue} | proplists:delete(ExistingKey, Query)]
end;
-query_order(Query, ColName, Type) when is_atom(Type) ->
+query_order(_Sock, Query, ColName, Type) when is_atom(Type) ->
OrderKey = {set_order, ColName, Type},
OrderValue = ["setorder",
?NULL,
@@ -456,14 +460,14 @@ query_order(Query, ColName, Type) when is_atom(Type) ->
%%
%% @doc Run a prepared query against the table and return matching keys.
search(Socket, Query) ->
- ?TRaw(<<"search">>, [V || {_K, V}=Prop <- Query, is_tuple(Prop), size(Prop)==2]).
+ ?TRaw(<<"search">>, [V || {_K, V}=Prop <- Query, is_tuple(Prop), erlang:size(Prop)==2]).
%% @spec searchcount(Socket::port,
%% Query::proplist()) -> [integer()] | error()
%%
%% @doc Run a prepared query against the table and get the count of matching keys.
searchcount(Socket, Query) ->
- case ?TRaw(<<"search">>, [V || {_K, V}=Prop <- Query, is_tuple(Prop), size(Prop)==2] ++ ["count"]) of
+ case ?TRaw(<<"search">>, [V || {_K, V}=Prop <- Query, is_tuple(Prop), erlang:size(Prop)==2] ++ ["count"]) of
{error, Reason} ->
{error, Reason};
[] ->
@@ -477,7 +481,7 @@ searchcount(Socket, Query) ->
%%
%% @doc Run a prepared query against the table and remove the matching records.
searchout(Socket, Query) ->
- ?TSimple(<<"search">>, [V || {_K, V}=Prop <- Query, is_tuple(Prop), size(Prop)==2] ++ ["out"]).
+ ?TSimple(<<"search">>, [V || {_K, V}=Prop <- Query, is_tuple(Prop), erlang:size(Prop)==2] ++ ["out"]).
%% %% Run a prepared query against the table and get the matching records. Due
%% %% to protocol restraints, the returned result cannot include columns whose
@@ -518,6 +522,8 @@ add_condition_op_val(Op) when is_atom(Op) ->
?QCSTRAND;
str_or ->
?QCSTROR;
+ str_in_list ->
+ ?QCSTROREQ;
str_regex ->
?QCSTRRX;
num_eq ->

0 comments on commit 9678cff

Please sign in to comment.