diff options
author | Nick Vatamaniuc <vatamane@apache.org> | 2020-03-08 00:38:48 -0500 |
---|---|---|
committer | Nick Vatamaniuc <nickva@users.noreply.github.com> | 2020-03-09 15:10:34 -0400 |
commit | 132cf7a9ba797a1cf228c39ccf6448a683a934d0 (patch) | |
tree | 60db1291cb438b8cc58c2d1fb1cdf4dbf8dcfc76 | |
parent | 02ca72ba34eb1768a631f12e34022464cf70278f (diff) | |
download | couchdb-132cf7a9ba797a1cf228c39ccf6448a683a934d0.tar.gz |
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.
-rw-r--r-- | src/couch_views/src/couch_views.erl | 21 | ||||
-rw-r--r-- | src/couch_views/src/couch_views_sup.erl | 8 | ||||
-rw-r--r-- | 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 322415b91..58cfb2467 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 7a72a1f33..2a40f0a79 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 cd5b2b0bf..02a12e788 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}, |