couchdb-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Norman Barker <norman.bar...@gmail.com>
Subject calling views in Erlang
Date Sat, 07 Nov 2009 21:38:05 GMT
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

Mime
View raw message