This is an automated email from the ASF dual-hosted git repository.

vatamane pushed a commit to branch prototype/fdb-layer
in repository https://gitbox.apache.org/repos/asf/couchdb.git

commit 132cf7a9ba797a1cf228c39ccf6448a683a934d0
Author: Nick Vatamaniuc <[email protected]>
AuthorDate: Sun Mar 8 00:38:48 2020 -0500

    Enable index auto-updating for couch_views
    
     * Register with the fabric2_index module
    
     * Provide a build_indices/2 callback
    
     * In the callback attempt to parse an `#mrst{}` indexing context and then
       trigger an indexing job. We don't wait for the job to finish but instead
       rely on `couch_job`'s max worker limit to keep concurrency in check.
---
 src/couch_views/src/couch_views.erl               | 21 ++++++++++++++++++++-
 src/couch_views/src/couch_views_sup.erl           |  8 ++++++++
 src/couch_views/test/couch_views_indexer_test.erl | 18 +++++++++++++++++-
 3 files changed, 45 insertions(+), 2 deletions(-)

diff --git a/src/couch_views/src/couch_views.erl 
b/src/couch_views/src/couch_views.erl
index 322415b..58cfb24 100644
--- a/src/couch_views/src/couch_views.erl
+++ b/src/couch_views/src/couch_views.erl
@@ -12,8 +12,15 @@
 
 -module(couch_views).
 
+
+-behavior(fabric2_index).
+
+
 -export([
-    query/6
+    query/6,
+
+    % fabric2_index behavior
+    build_indices/2
 ]).
 
 
@@ -55,6 +62,18 @@ query(Db, DDoc, ViewName, Callback, Acc0, Args0) ->
     end.
 
 
+build_indices(#{} = Db, DDocs) when is_list(DDocs) ->
+    DbName = fabric2_db:name(Db),
+    lists:filtermap(fun(DDoc) ->
+        try couch_views_util:ddoc_to_mrst(DbName, DDoc) of
+            {ok, #mrst{} = Mrst} ->
+                {true, couch_views_jobs:build_view_async(Db, Mrst)}
+        catch _:_ ->
+            false
+        end
+    end, DDocs).
+
+
 read_view(Db, Mrst, ViewName, Callback, Acc0, Args) ->
     fabric2_fdb:transactional(Db, fun(TxDb) ->
         try
diff --git a/src/couch_views/src/couch_views_sup.erl 
b/src/couch_views/src/couch_views_sup.erl
index 7a72a1f..2a40f0a 100644
--- a/src/couch_views/src/couch_views_sup.erl
+++ b/src/couch_views/src/couch_views_sup.erl
@@ -28,6 +28,7 @@
 
 
 start_link() ->
+    ok = register_views_index(),
     Arg = case fabric2_node_types:is_type(view_indexing) of
         true -> normal;
         false -> builds_disabled
@@ -50,6 +51,13 @@ init(builds_disabled) ->
     {ok, {flags(), []}}.
 
 
+register_views_index() ->
+    case fabric2_node_types:is_type(api_frontend) of
+        true -> fabric2_index:register_index(couch_views);
+        false -> ok
+    end.
+
+
 flags() ->
     #{
         strategy => one_for_one,
diff --git a/src/couch_views/test/couch_views_indexer_test.erl 
b/src/couch_views/test/couch_views_indexer_test.erl
index cd5b2b0..02a12e7 100644
--- a/src/couch_views/test/couch_views_indexer_test.erl
+++ b/src/couch_views/test/couch_views_indexer_test.erl
@@ -41,7 +41,8 @@ indexer_test_() ->
                     ?TDEF_FE(multipe_identical_keys_from_same_doc),
                     ?TDEF_FE(fewer_multipe_identical_keys_from_same_doc),
                     ?TDEF_FE(handle_size_key_limits),
-                    ?TDEF_FE(handle_size_value_limits)
+                    ?TDEF_FE(handle_size_value_limits),
+                    ?TDEF_FE(index_autoupdater_callback)
                 ]
             }
         }
@@ -536,6 +537,21 @@ handle_size_value_limits(Db) ->
     ], Out1).
 
 
+index_autoupdater_callback(Db) ->
+    DDoc = create_ddoc(),
+    Doc1 = doc(0),
+    {ok, _} = fabric2_db:update_doc(Db, DDoc, []),
+    {ok, _} = fabric2_db:update_doc(Db, Doc1, []),
+
+    DbSeq = fabric2_db:get_update_seq(Db),
+
+    Result = couch_views:build_indices(Db, [DDoc]),
+    ?assertMatch([{ok, <<_/binary>>}], Result),
+    [{ok, JobId}] = Result,
+
+    ?assertEqual(ok, couch_views_jobs:wait_for_job(JobId, DbSeq)).
+
+
 row(Id, Key, Value) ->
     {row, [
         {id, Id},

Reply via email to