summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPaul J. Davis <paul.joseph.davis@gmail.com>2019-08-20 16:16:57 -0500
committerPaul J. Davis <paul.joseph.davis@gmail.com>2019-09-25 09:42:45 -0500
commitd7d32a007c4c2502c93ee4dee9823cdf5f794917 (patch)
treec05bae63cd467f13a5980176d15c6639d72be1e3
parentecc9ae442ee14474b4df08fc677f6f0d37965829 (diff)
downloadcouchdb-d7d32a007c4c2502c93ee4dee9823cdf5f794917.tar.gz
Update couch_views to use couch_eval
-rw-r--r--src/couch_views/src/couch_views.app.src3
-rw-r--r--src/couch_views/src/couch_views_indexer.erl65
-rw-r--r--src/couch_views/test/couch_views_indexer_test.erl1
-rw-r--r--src/couch_views/test/couch_views_map_test.erl7
-rw-r--r--src/couch_views/test/couch_views_trace_index_test.erl2
5 files changed, 58 insertions, 20 deletions
diff --git a/src/couch_views/src/couch_views.app.src b/src/couch_views/src/couch_views.app.src
index c80c30b02..0d666affd 100644
--- a/src/couch_views/src/couch_views.app.src
+++ b/src/couch_views/src/couch_views.app.src
@@ -26,6 +26,7 @@
config,
couch_stats,
fabric,
- couch_jobs
+ couch_jobs,
+ couch_eval
]}
]}.
diff --git a/src/couch_views/src/couch_views_indexer.erl b/src/couch_views/src/couch_views_indexer.erl
index 83d1b6aa2..55ce06311 100644
--- a/src/couch_views/src/couch_views_indexer.erl
+++ b/src/couch_views/src/couch_views_indexer.erl
@@ -120,7 +120,7 @@ update(#{} = Db, Mrst0, State0) ->
case State4 of
finished ->
- couch_query_servers:stop_doc_map(Mrst2#mrst.qserver);
+ couch_eval:release_map_context(Mrst2#mrst.qserver);
_ ->
update(Db, Mrst2, State4)
end.
@@ -171,20 +171,42 @@ map_docs(Mrst, Docs) ->
% Run all the non deleted docs through the view engine and
Mrst1 = start_query_server(Mrst),
QServer = Mrst1#mrst.qserver,
- MapFun = fun
- (#{deleted := true} = Change) ->
- Change#{results => []};
- (#{deleted := false} = Change) ->
- #{doc := Doc} = Change,
- couch_stats:increment_counter([couchdb, mrview, map_doc]),
- {ok, RawResults} = couch_query_servers:map_doc_raw(QServer, Doc),
- JsonResults = couch_query_servers:raw_to_ejson(RawResults),
- ListResults = lists:map(fun(ViewResults) ->
- [list_to_tuple(Res) || Res <- ViewResults]
- end, JsonResults),
- Change#{results => ListResults}
- end,
- {Mrst1, lists:map(MapFun, Docs)}.
+
+ {Deleted0, NotDeleted0} = lists:partition(fun(Doc) ->
+ #{deleted := Deleted} = Doc,
+ Deleted
+ end, Docs),
+
+ Deleted1 = lists:map(fun(Doc) ->
+ Doc#{results => []}
+ end, Deleted0),
+
+ DocsToMap = lists:map(fun(Doc) ->
+ #{doc := DocRec} = Doc,
+ DocRec
+ end, NotDeleted0),
+
+ {ok, AllResults} = couch_eval:map_docs(QServer, DocsToMap),
+
+ % The expanded function head here is making an assertion
+ % that the results match the given doc
+ NotDeleted1 = lists:zipwith(fun(#{id := DocId} = Doc, {DocId, Results}) ->
+ Doc#{results => Results}
+ end, NotDeleted0, AllResults),
+
+ % I'm being a bit careful here resorting the docs
+ % in order of the changes feed. Theoretically this is
+ % unnecessary since we're inside a single transaction.
+ % However, I'm concerned if we ever split this up
+ % into multiple transactions that this detail might
+ % be important but forgotten.
+ MappedDocs = lists:sort(fun(A, B) ->
+ #{sequence := ASeq} = A,
+ #{sequence := BSeq} = B,
+ ASeq =< BSeq
+ end, Deleted1 ++ NotDeleted1),
+
+ {Mrst1, MappedDocs}.
write_docs(TxDb, Mrst, Docs, State) ->
@@ -249,12 +271,21 @@ fetch_docs(Db, Changes) ->
start_query_server(#mrst{qserver = nil} = Mrst) ->
#mrst{
+ db_name = DbName,
+ idx_name = DDocId,
language = Language,
+ sig = Sig,
lib = Lib,
views = Views
} = Mrst,
- Defs = [View#mrview.def || View <- Views],
- {ok, QServer} = couch_query_servers:start_doc_map(Language, Defs, Lib),
+ {ok, QServer} = couch_eval:acquire_map_context(
+ DbName,
+ DDocId,
+ Language,
+ Sig,
+ Lib,
+ [View#mrview.def || View <- Views]
+ ),
Mrst#mrst{qserver = QServer};
start_query_server(#mrst{} = Mrst) ->
diff --git a/src/couch_views/test/couch_views_indexer_test.erl b/src/couch_views/test/couch_views_indexer_test.erl
index 02c8ceedb..20ad0dc6b 100644
--- a/src/couch_views/test/couch_views_indexer_test.erl
+++ b/src/couch_views/test/couch_views_indexer_test.erl
@@ -52,6 +52,7 @@ setup() ->
Ctx = test_util:start_couch([
fabric,
couch_jobs,
+ couch_js,
couch_views
]),
Ctx.
diff --git a/src/couch_views/test/couch_views_map_test.erl b/src/couch_views/test/couch_views_map_test.erl
index 0b0ab6894..f8ba18319 100644
--- a/src/couch_views/test/couch_views_map_test.erl
+++ b/src/couch_views/test/couch_views_map_test.erl
@@ -20,7 +20,12 @@
setup() ->
- test_util:start_couch([fabric, couch_jobs, couch_views]).
+ test_util:start_couch([
+ fabric,
+ couch_jobs,
+ couch_js,
+ couch_views
+ ]).
teardown(State) ->
diff --git a/src/couch_views/test/couch_views_trace_index_test.erl b/src/couch_views/test/couch_views_trace_index_test.erl
index b7fe66b49..c4f76d897 100644
--- a/src/couch_views/test/couch_views_trace_index_test.erl
+++ b/src/couch_views/test/couch_views_trace_index_test.erl
@@ -51,7 +51,7 @@ indexer_test_() ->
setup() ->
- test_util:start_couch([fabric]).
+ test_util:start_couch([fabric, couch_js]).
cleanup(Ctx) ->