Call fabric view reduce callback with meta information

This is a temporary hack to allow passing meta through
fabric_view_reduce:handle_message. The meta rows were not present on
reduce functions in the previous views implementation, but they
provide an excellent place to determine which a view is about to start
streaming, which is essential for the multi query view implementation.

The hack is that we don't do the proper row collection worker dance
like we do in the handle_message callback for #view_row. For now this
sets a process dict flag to indicate meta has been sent so that we
only send it once. The proper fix is to send meta through
fabric_view:maybe_send_row, but that gets ugly in a hurry and can be
addressed separately.

COUCHDB-523
COUCHDB-1993
diff --git a/src/fabric_view.erl b/src/fabric_view.erl
index 31c25d9..09e13f2 100644
--- a/src/fabric_view.erl
+++ b/src/fabric_view.erl
@@ -98,6 +98,7 @@
         % we still need to send the total/offset header
         {ok, State};
     false ->
+        erase(meta_sent),
         {_, Acc} = Callback(complete, AccIn),
         {stop, State#collector{user_acc=Acc}}
     end;
@@ -124,6 +125,7 @@
                 maybe_send_row(NewState#collector{user_acc=Acc, limit=Limit-1})
             end
         catch complete ->
+            erase(meta_sent),
             {_, Acc} = Callback(complete, AccIn),
             {stop, State#collector{user_acc=Acc}}
         end
diff --git a/src/fabric_view_reduce.erl b/src/fabric_view_reduce.erl
index d2ea464..6b40a6a 100644
--- a/src/fabric_view_reduce.erl
+++ b/src/fabric_view_reduce.erl
@@ -74,6 +74,30 @@
         {error, Resp}
     end;
 
+%% HACK: this just sends meta once. Instead we should move the counter logic
+%% from the #view_row handle_message below into this function and and pass the
+%% meta call through maybe_send_row. This will also be more efficient doing it
+%% here as it's one less worker round trip reply.
+%% Prior to switching to couch_mrview, the fabric_view_reduce implementation
+%% did not get a total_and_offset call, whereas now we do. We now use this
+%% message as a clean way to indicate to couch_mrview_http:view_cb that the
+%% reduce response is starting.
+handle_message({meta, Meta}, {_Worker, From}, State) ->
+    gen_server:reply(From, ok),
+    #collector{
+        callback = Callback,
+        user_acc = AccIn
+    } = State,
+
+    {Go, Acc} = case get(meta_sent) of
+        undefined ->
+            put(meta_sent, true),
+            Callback({meta, Meta}, AccIn);
+        _ ->
+            {ok, AccIn}
+    end,
+    {Go, State#collector{user_acc = Acc}};
+
 handle_message(#view_row{key=Key} = Row, {Worker, From}, State) ->
     #collector{counters = Counters0, rows = Rows0} = State,
     case fabric_dict:lookup_element(Worker, Counters0) of