Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: add count_by #40

Open
wants to merge 16 commits into
base: master
Choose a base branch
from
17 changes: 15 additions & 2 deletions src/sumo_store_riak.erl
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@
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
]).

%% Utilities
Expand Down Expand Up @@ -473,11 +473,13 @@ sleep_custom(FieldValue, FieldType) ->
%% @private
wakeup(Doc) ->
sumo_utils:doc_transform(fun wakeup_fun/4, Doc).
wakeup_fun(_, _, undefined, _) ->
undefined;
wakeup_fun(_, _, <<"$nil">>, _) ->
undefined;
wakeup_fun(FieldType, _, FieldValue, _)
when FieldType =:= datetime; FieldType =:= date ->
case {FieldType, iso8601:is_datetime(FieldValue)} of
case {FieldType, sumo_utils:is_datetime(FieldValue)} of
{datetime, true} -> iso8601:parse(FieldValue);
{date, true} -> {Date, _} = iso8601:parse(FieldValue), Date;
_ -> FieldValue
Expand Down Expand Up @@ -705,3 +707,14 @@ build_sort(Sorts) ->
binary:list_to_bin([sumo_utils:to_bin(Field), "_register", " ", sumo_utils:to_bin(Dir)])
end || {Field, Dir} <- Sorts],
[{sort, binary:list_to_bin(interpose(", ", Res))}].

-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, [], State) ->
count(DocName, State);
count_by(_DocName, _Conditions, #{conn := _Conn} = _State) ->
0.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should only have 2 spaces for indentation.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's move this function to the API section; below count.

On the other hand, I don't really see the point of having this function, at least I don't see specific logic with the conditions, for example, if condition list if empty, bypass to count(DocName, State), otherwise return 0. This looks to me like a very specific logic. Could you please elaborate more on this, what is the purpose of having this function implemented in this way? What if we have conditions, should we perform the count query based on the conditions?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

currrrently I'm just getting the code to compile and run, I'm not sure how to implement a count_by in the context, maybe I could do a search and get the count ?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

currrrently I'm just getting the code to compile and run, I'm not sure how to implement a count_by in the context, maybe I could do a search and get the count ?

Sorry, I cannot help much here since I haven't worked with Riak a long time ago, but I'd review RiakKV doc and see what would be the best way for implementing this, maybe investigate if there are examples or other cases where this has been implemented already.

Once you have some implementation done, we can review it! Looking forward to that 😉