Sean Cribbs  committed 57d4a53

Allow streaming of keys over the raw interface.

Streaming is done using chunked-encoding where each chunk is a JSON
object containing a number of keys. To invoke streaming, set the "keys"
query parameter to "stream". The first chunk is the bucket props unless you
set the "props" parameter to "false", in which case it will be an
empty JSON object.

  • Participants
  • Parent commits 864491e
  • Branches default

Comments (0)

Files changed (2)

File apps/riak/src/raw_http.hrl

 -define(Q_PROPS, "props").
 -define(Q_KEYS,  "keys").
 -define(Q_FALSE, "false").
+-define(Q_STREAM, "stream").
 -define(Q_VTAG,  "vtag").

File apps/riak/src/raw_http_resource.erl

 %%      Includes the bucket props unless the "props=false" query param
 %%      is specified.
 %%      Includes the keys of the documents in the bucket unless the
-%%      "keys=false" query param is specified.
+%%      "keys=false" query param is specified. If "keys=stream" query param
+%%      is specified, keys will be streamed back to the client in JSON chunks
+%%      like so: {"keys":[Key1, Key2,...]}.
 %%      A Link header will also be added to the response by this function
 %%      if the keys are included in the JSON object.  The Link header
 %%      will include links to all keys in the bucket, with the property
     {KeyPart, KeyRD} =
         case wrq:get_qs_value(?Q_KEYS, RD) of
             ?Q_FALSE -> {[], RD};
+            ?Q_STREAM -> {stream, RD};
             _ ->
                 {ok, KeyList} = C:list_keys(B),
                 {[{?Q_KEYS, KeyList}],
                    RD, KeyList)}
-    {mochijson2:encode({struct, SchemaPart++KeyPart}), KeyRD, Ctx}.
+    case KeyPart of
+        stream -> {{stream, {mochijson2:encode({struct, SchemaPart}),
+                             fun() ->
+                                     {ok, ReqId} = C:stream_list_keys(B),
+                                     stream_keys(ReqId)
+                             end}},
+                   KeyRD,
+                   Ctx};
+       _ ->
+            {mochijson2:encode({struct, SchemaPart++KeyPart}), KeyRD, Ctx}
+    end.
+stream_keys(ReqId) ->
+    receive
+        {ReqId, {keys, Keys}} ->
+            {mochijson2:encode({struct, [{<<"keys">>, Keys}]}), fun() -> stream_keys(ReqId) end};                                                                     
+        {ReqId, done} -> {mochijson2:encode({struct, [{<<"keys">>, []}]}), done}
+    end.
 %% @spec accept_bucket_body(reqdata(), context()) -> {true, reqdata(), context()}
 %% @doc Modify the bucket properties according to the body of the