Commits

Benoit Chesneau  committed 53eba2b

add couchbeam_view:first/1 and couchbeam_view:first/2

  • Participants
  • Parent commits 7b14a90

Comments (0)

Files changed (2)

File src/couchbeam_view.erl

 -export([init/1, terminate/2, code_change/3, handle_call/3, handle_cast/2,
          handle_info/2]).
 -export([fetch_view/1, fetch_view/2, parse_view/1, parse_view/2,
-         count/1, count/2]).
+         count/1, count/2, first/1, first/2]).
 -export([close_view/1]).
                   
                   
     count(ViewPid, false).
 count(ViewPid, Refresh) ->
     gen_server:call(ViewPid, {count, Refresh}, infinity).
+    
+first(ViewPid) ->
+    first(ViewPid, false).
+first(ViewPid, Refresh) ->
+    case parse_view(ViewPid, Refresh) of
+        {_, _, _, []} -> {[]};
+        {_, _, _, [FirstRow|_]} -> FirstRow
+    end.
                      
 close_view(ViewPid) ->
     try
         Id = proplists:get_value(<<"id">>, Row1),
         Key = proplists:get_value(<<"key">>, Row1),
         case proplists:get_value(<<"value">>, Row1) of
-        [] -> Id;
-        {Value} -> {Id, Key, {Value}};
-        Value when is_list(Value) -> {Id, Key, Value};
-        Value when is_integer(Value) -> {Id, Key, Value};
-        Value when is_binary(Value) -> {Id, Key, Value};
-        _ -> Id
+            [] -> Id;
+            {Value} -> {Id, Key, {Value}};
+            Value when is_list(Value) -> {Id, Key, Value};
+            Value when is_integer(Value) -> {Id, Key, Value};
+            Value when is_binary(Value) -> {Id, Key, Value};
+            _ -> Id
         end
     end || Row <- Rows],
     {TotalRows, Offset, Meta, Rows1};

File t/005-view.t

 %%! -pa ./ebin
 
 main(_) ->
-    etap:plan(11),
+    etap:plan(12),
     start_app(),
     case (catch test()) of
         ok ->
     etap:is(couchbeam_view:count(VRresults4), 2, "view, startkey, endkey : nb rows ok"),
     VResults5 = couchbeam_db:query_view(Db, {"couchbeam",  "test"}, [{"keys", [<<"test">>, <<"test3">>]}]),
     etap:is(couchbeam_view:count(VResults5), 2, "view, keys : nb rows ok"),
+    {Id, _, _} = couchbeam_view:first(VResults5),
+    etap:is(Id, <<"test">>, "first key ok"),
     ok.