You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@couchdb.apache.org by Norman Barker <no...@gmail.com> on 2009/11/07 22:38:05 UTC
calling views in Erlang
Hi,
I have written some code to query a view within Erlang and it seems
too simple :-) Am I really just looping through a view and returning
{ok, State} if a key matches a particular input KeyId and calling
{stop, State} when I want the iteration to stop?
I appreciate there is an index behind the view which makes things
fast, but the looping seems really simple. Verification of this
approach would be very much appreciated as I go forward!
case couch_view:get_map_view(Db, DesignId, ViewName, []) of
{ok, View, Group} ->
FoldlFun = fun({{Key, DocId}, Value}, _, {MaxRecords,
StartPos, Counter, DocIdList}=Acc) ->
case Key == KeyId of
true ->
case Counter < StartPos of
true ->
% not emitting a value yet until we go past StartPos
{ok, {MaxRecords, StartPos, Counter + 1,
DocIdList}};
_ ->
case (Counter - StartPos) < MaxRecords of
true ->
{ok, {MaxRecords, StartPos, Counter +
1, [DocId] ++ DocIdList}};
_ ->
% gone past max records
{stop, Acc}
end
end;
_ ->
{stop, Acc}
end
end,
FoldAccInit = {MaxRecords, StartPos, 0, []},
case couch_view:fold(View, {KeyId, nil}, fwd, FoldlFun,
FoldAccInit) of
{ok, {_, _, _, DocIdList}} ->
io:fwrite("LIST ~p ViewName ~p ~n", [DocIdList, ViewName]);
_ ->
throw({error, {[{code, "NoApplicableCode"},
{reason, "Error parsing view" ++ binary_to_list(ViewName)}]}})
end;
{not_found, Reason} ->
throw({error, {[{code, "NoApplicableCode"}, {reason, Reason}]}})
end;
thanks,
Norman
Re: calling views in Erlang
Posted by Paul Davis <pa...@gmail.com>.
On Sat, Nov 7, 2009 at 10:57 PM, Paul Davis <pa...@gmail.com> wrote:
> Norman,
>
>> I have written some code to query a view within Erlang and it seems
>> too simple :-)
>
> Welcome to CouchDB. Simple is the implementation corollary of Relax. :)
>
>> Am I really just looping through a view and returning
>> {ok, State} if a key matches a particular input KeyId and calling
>> {stop, State} when I want the iteration to stop?
>>
>> I appreciate there is an index behind the view which makes things
>> fast, but the looping seems really simple. Verification of this
>> approach would be very much appreciated as I go forward!
>
> Even in advanced algorithms indexes are basically just sorted lists at
> the core. Couch's view interface makes that pretty plain. So yes, you
> can just iterate over the views as you see fit. The key to why this is
> quick and delicious is that seeking to the start of iteration (when
> you provide a startkey) is that you only do a log(N) seek to the first
> key.
>
> HTH,
> Paul Davis
>
I should mention, that even with no startkey, its still a log(N) seek,
just that its seeking to a special key that sorts before all keys. The
seeking part is important because its assumed that most view
iterations are over small subsets of the index not starting near the
head of the index on average.
Paul Davis
Re: calling views in Erlang
Posted by Paul Davis <pa...@gmail.com>.
Norman,
> I have written some code to query a view within Erlang and it seems
> too simple :-)
Welcome to CouchDB. Simple is the implementation corollary of Relax. :)
> Am I really just looping through a view and returning
> {ok, State} if a key matches a particular input KeyId and calling
> {stop, State} when I want the iteration to stop?
>
> I appreciate there is an index behind the view which makes things
> fast, but the looping seems really simple. Verification of this
> approach would be very much appreciated as I go forward!
Even in advanced algorithms indexes are basically just sorted lists at
the core. Couch's view interface makes that pretty plain. So yes, you
can just iterate over the views as you see fit. The key to why this is
quick and delicious is that seeking to the start of iteration (when
you provide a startkey) is that you only do a log(N) seek to the first
key.
HTH,
Paul Davis