diff options
author | Garren Smith <garren.smith@gmail.com> | 2019-11-26 16:27:50 +0200 |
---|---|---|
committer | garren smith <garren.smith@gmail.com> | 2019-11-26 17:04:24 +0200 |
commit | 0cea6a48869948155a477a654240078e9dd467c7 (patch) | |
tree | daa77d1de331344ef2d297094429d819bb5c4a5d | |
parent | 3c2b92c032f234666454925c911b01e1b781af2e (diff) | |
download | couchdb-0cea6a48869948155a477a654240078e9dd467c7.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. |