Skip to content

Commit

Permalink
[#314] Adding count_by function
Browse files Browse the repository at this point in the history
  • Loading branch information
ferigis committed Jul 12, 2017
1 parent 1a15318 commit 3c0d477
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 2 deletions.
13 changes: 13 additions & 0 deletions src/adapter_test_helpers/sumo_basic_test_helper.erl
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
delete/1,
check_proper_dates/1,
count/1,
count_by/1,
persist_using_changeset/1
]).

Expand Down Expand Up @@ -194,6 +195,18 @@ count(Config) ->
6 = sumo:count(Name),
ok.

-spec count_by(config()) -> ok.
count_by(Config) ->
{_, Name} = lists:keyfind(name, 1, Config),

6 = sumo:count_by(Name, [{age, '>', 2}]),
4 = sumo:count_by(Name, [{age, '>', 2}, {age, '=<', 5}]),
2 = sumo:count_by(Name, [{age, '>', 5}]),
0 = sumo:count_by(Name, [{age, '>', 7}]),
8 = sumo:count_by(Name, []),

ok.

-spec persist_using_changeset(config()) -> ok.
persist_using_changeset(Config) ->
{_, Name} = lists:keyfind(name, 1, Config),
Expand Down
2 changes: 1 addition & 1 deletion src/adapter_test_helpers/sumo_find_test_helper.erl
Original file line number Diff line number Diff line change
Expand Up @@ -64,4 +64,4 @@ init_store(Name) ->
sumo:persist(Name, Module:new(<<"D">>, <<"B">>, 4)),
sumo:persist(Name, Module:new(<<"E">>, <<"A">>, 2)),
sumo:persist(Name, Module:new(<<"F">>, <<"E">>, 1)),
ok.
ok.
18 changes: 17 additions & 1 deletion src/adapters/sumo_store_mnesia.erl
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@
delete_all/2,
find_all/2, find_all/5,
find_by/3, find_by/5, find_by/6,
count/2
count/2,
count_by/3
]).

%%%=============================================================================
Expand Down Expand Up @@ -245,6 +246,21 @@ count(DocName, State) ->
_:Reason -> {error, Reason, State}
end.

-spec count_by(DocName, Conditions, State) -> Response when
DocName :: sumo:schema_name(),
Conditions :: sumo:conditions(),
State :: state(),
Response :: sumo_store:result(non_neg_integer(), state()).
count_by(DocName, Conditions, State) ->
MatchSpec = build_match_spec(DocName, Conditions),
Transaction = fun() ->
length(mnesia:select(DocName, MatchSpec))
end,
case mnesia:transaction(Transaction) of
{aborted, Reason} -> {error, Reason, State};
{atomic, Count} -> {ok, Count, State}
end.

%%%=============================================================================
%%% Internal functions
%%%=============================================================================
Expand Down
9 changes: 9 additions & 0 deletions src/sumo.erl
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
delete_by/2,
delete_all/1,
count/1,
count_by/2,
call/2, call/3
]).

Expand Down Expand Up @@ -270,6 +271,14 @@ count(DocName) ->
Error -> exit(Error)
end.

%% @doc Counts the total number of docs matching the provided Conditions.
-spec count_by(schema_name(), conditions()) -> non_neg_integer().
count_by(DocName, Conditions) ->
case sumo_store:count_by(sumo_config:get_store(DocName), DocName, Conditions) of
{ok, Total} -> Total;
Error -> exit(Error)
end.

%% @doc Calls the given custom function of a store.
-spec call(schema_name(), atom()) -> term().
call(DocName, Function) ->
Expand Down
22 changes: 22 additions & 0 deletions src/sumo_store.erl
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
find_by/5,
find_by/6,
count/2,
count_by/3,
call/4
]).

Expand Down Expand Up @@ -130,6 +131,11 @@
Schema :: sumo:schema_name(),
Res :: result(non_neg_integer(), State).

-callback count_by(Schema, Conditions, State) -> Res when
Schema :: sumo:schema_name(),
Conditions :: sumo:conditions(),
Res :: result(non_neg_integer(), State).

%%%=============================================================================
%%% API
%%%=============================================================================
Expand Down Expand Up @@ -257,6 +263,15 @@ find_by(Name, DocName, Conditions, SortFields, Limit, Offset) ->
count(Name, DocName) ->
wpool:call(Name, {count, DocName}).

%% @doc Counts the total number of docs that match the given conditions.
-spec count_by(Name, DocName, Conditions) -> Res when
Name :: atom(),
DocName :: sumo:schema_name(),
Conditions :: sumo:conditions(),
Res :: {ok, non_neg_integer()} | {error, term()}.
count_by(Name, DocName, Conditions) ->
wpool:call(Name, {count_by, DocName, Conditions}).

%% @doc Calls a custom function in the given store name.
-spec call(Name, DocName, Function, Args) -> Res when
Name :: atom(),
Expand Down Expand Up @@ -372,6 +387,13 @@ handle_call(
{OkOrError, Reply, NewState} = Handler:count(DocName, HState),
{reply, {OkOrError, Reply}, State#state{handler_state = NewState}};

handle_call(
{count_by, DocName, Conditions}, _From,
#state{handler = Handler, handler_state = HState} = State
) ->
{OkOrError, Reply, NewState} = Handler:count_by(DocName, Conditions, HState),
{reply, {OkOrError, Reply}, State#state{handler_state = NewState}};

handle_call(
{call, DocName, Function, Args}, _From,
#state{handler = Handler, handler_state = HState} = State
Expand Down
1 change: 1 addition & 0 deletions test/sumo_basic_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
delete/1,
check_proper_dates/1,
count/1,
count_by/1,
persist_using_changeset/1
]}
]).
Expand Down

0 comments on commit 3c0d477

Please sign in to comment.