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

add boss_db:paginate for mnesia #190

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
add paginate to mnesia driver
  • Loading branch information
chan committed Jul 21, 2014
commit ce6f961718667ba205e2403b3f408b938a64f17a
45 changes: 45 additions & 0 deletions src/db_adapters/boss_db_adapter_mnesia.erl
Original file line number Diff line number Diff line change
@@ -5,6 +5,7 @@
-export([transaction/2]).
-export([table_exists/2, get_migrations_table/1, migration_done/3]).

-define(DEFAULT_PAGE_SIZE, 10).
%-define(TRILLION, (1000 * 1000 * 1000 * 1000)).

start(_) ->
@@ -22,6 +23,7 @@ init(_Options) ->
terminate(_) ->
ok.


% -----
find(_, Id) when is_list(Id) ->
Type = infer_type_from_id(Id),
@@ -264,6 +266,8 @@ infer_type_from_id(Id) when is_list(Id) ->
list_to_atom(hd(string:tokens(Id, "-"))).

%-----
build_query(Type, Conditions) ->
build_query(Type, Conditions, none, none, none, none).
build_query(Type, Conditions, _Max, _Skip, _Sort, _SortOrder) -> % a Query is a {Pattern, Filter} combo
Fldnames = mnesia:table_info(Type, attributes),
BlankPattern = [ {Fld, '_'} || Fld <- Fldnames],
@@ -283,3 +287,44 @@ build_conditions1([First|Rest], Pattern, Filter) ->
build_conditions1(Rest, Pattern, [First|Filter]).


limit (Tab, Offset, Number, MatchSpec) ->
Fun = fun() ->
seek (Offset,
Number,
mnesia:select (Tab,
MatchSpec,
Number,
read)
)
end,
mnesia:transaction(Fun).


seek (_Offset, _Number, '$end_of_table') ->
[];
seek (Offset, Number, X) when Offset =< 0 ->
read (Number, X, []);
seek (Offset, Number, { Results, Cont }) ->
NumResults = length (Results),
case Offset > NumResults of
true ->
seek (Offset - NumResults, Number, mnesia:select (Cont));
false ->
{ _, DontDrop } = lists:split (Offset, Results),
Keep = lists:sublist (DontDrop, Number),
read (Number - length (Keep), mnesia:select (Cont), [ Keep ])
end.

read (Number, _, Acc) when Number =< 0 ->
lists:foldl (fun erlang:'++'/2, [], Acc);
read (_Number, '$end_of_table', Acc) ->
lists:foldl (fun erlang:'++'/2, [], Acc);
read (Number, { Results, Cont }, Acc) ->
NumResults = length (Results),
case Number > NumResults of
true ->
read (Number - NumResults, mnesia:select (Cont), [ Results | Acc ]);
false ->
{ Keep, _ } = lists:split (Number, Results),
lists:foldl (fun erlang:'++'/2, Keep, Acc)
end.