[erlang-questions] qlc cursor offset
Alain O'Dea
alain.odea@REDACTED
Tue Dec 28 17:18:55 CET 2010
I code-shopped Paul Mineiro's solution when I needed to do efficient
pagination with Mnesia like this for scrumjet:
% Author: Paul Mineiro
% SOURCE: http://erlanganswers.com/web/mcedemo/mnesia/OrderedBy.html
-module (scrumjet_mnesia).
-export ([ limit/3 ]).
limit (Tab, Offset, Number) ->
seek (Offset,
Number,
mnesia:select (Tab,
[ { mnesia:table_info (Tab, wild_pattern), [],
[ '$_' ] } ],
10,
read)).
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.
The source of the scrumjet_mnesia can be downloaded conveniently from:
https://github.com/AlainODea/scrumjet/blob/master/src/scrumjet_mnesia.erl
Paul Mineiro's original solution is here:
http://erlanganswers.com/web/mcedemo/mnesia/OrderedBy.html
On Tue, Dec 28, 2010 at 12:05 PM, Andre Nathan <andre@REDACTED>wrote:
> Hello
>
> Is there a way to move the qlc cursor to a given offset without fetching
> all the intermediate results? The idea is to implement pagination and
> allow a user to jump to a specific page without the need to retrieve all
> the data from previous pages.
>
> My mnesia table is something like this:
>
> -record(quota, {
> user,
> domain_id,
> count_cur,
> count_max
> }).
>
> and I'd like to fetch the quotas for all users of a given domain_id, a
> few each time.
>
> Thanks in advance
> Andre
>
>
> ________________________________________________________________
> erlang-questions (at) erlang.org mailing list.
> See http://www.erlang.org/faq.html
> To unsubscribe; mailto:erlang-questions-unsubscribe@REDACTED
>
>
More information about the erlang-questions
mailing list