diff options
author | Garren Smith <garren.smith@gmail.com> | 2019-11-26 16:27:50 +0200 |
---|---|---|
committer | Paul J. Davis <paul.joseph.davis@gmail.com> | 2020-03-02 12:26:22 -0600 |
commit | f943ac10e223d765aada4b17ae90403cbc5b0a4f (patch) | |
tree | c173552edb98a58e336ac4d45481ea53b161b312 | |
parent | 5aeb21ce1d86c8e0e5c0c4b7bd8c188816a5de75 (diff) | |
download | couchdb-f943ac10e223d765aada4b17ae90403cbc5b0a4f.tar.gz |
Optimize view read latency when the view ready
If the view is already built, read the view status and view
in the same transaction. This will improve view read latency.
-rw-r--r-- | src/couch_views/src/couch_views.erl | 46 |
1 files changed, 26 insertions, 20 deletions
diff --git a/src/couch_views/src/couch_views.erl b/src/couch_views/src/couch_views.erl index 7c7588c67..322415b91 100644 --- a/src/couch_views/src/couch_views.erl +++ b/src/couch_views/src/couch_views.erl @@ -44,36 +44,42 @@ query(Db, DDoc, ViewName, Callback, Acc0, Args0) -> false -> ok end, - ok = maybe_update_view(Db, Mrst, Args3), - try - couch_views_reader:read(Db, Mrst, ViewName, Callback, Acc0, Args3) - after - UpdateAfter = Args3#mrargs.update == lazy, - if UpdateAfter == false -> ok; true -> - couch_views_jobs:build_view_async(Db, Mrst) - end + fabric2_fdb:transactional(Db, fun(TxDb) -> + ok = maybe_update_view(TxDb, Mrst, Args3), + read_view(TxDb, Mrst, ViewName, Callback, Acc0, Args3) + end) + catch throw:{build_view, WaitSeq} -> + couch_views_jobs:build_view(Db, Mrst, WaitSeq), + read_view(Db, Mrst, ViewName, Callback, Acc0, Args3) end. +read_view(Db, Mrst, ViewName, Callback, Acc0, Args) -> + fabric2_fdb:transactional(Db, fun(TxDb) -> + try + couch_views_reader:read(TxDb, Mrst, ViewName, Callback, Acc0, Args) + after + UpdateAfter = Args#mrargs.update == lazy, + if UpdateAfter == false -> ok; true -> + couch_views_jobs:build_view_async(TxDb, Mrst) + end + end + end). + + maybe_update_view(_Db, _Mrst, #mrargs{update = false}) -> ok; maybe_update_view(_Db, _Mrst, #mrargs{update = lazy}) -> ok; -maybe_update_view(Db, Mrst, _Args) -> - WaitSeq = fabric2_fdb:transactional(Db, fun(TxDb) -> - DbSeq = fabric2_db:get_update_seq(TxDb), - ViewSeq = couch_views_fdb:get_update_seq(TxDb, Mrst), - case DbSeq == ViewSeq of - true -> ready; - false -> DbSeq - end - end), - - if WaitSeq == ready -> ok; true -> - couch_views_jobs:build_view(Db, Mrst, WaitSeq) +maybe_update_view(TxDb, Mrst, _Args) -> + DbSeq = fabric2_db:get_update_seq(TxDb), + ViewSeq = couch_views_fdb:get_update_seq(TxDb, Mrst), + case DbSeq == ViewSeq of + true -> ok; + false -> throw({build_view, DbSeq}) end. |