You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@couchdb.apache.org by da...@apache.org on 2020/03/25 20:06:49 UTC

[couchdb] 04/06: Implement couch_views:cleanup_indices/2

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

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

commit a07f219272102abb89ea308ea79ce02c412da6c8
Author: Paul J. Davis <pa...@gmail.com>
AuthorDate: Wed Mar 25 14:51:47 2020 -0500

    Implement couch_views:cleanup_indices/2
---
 src/couch_views/src/couch_views.erl     | 17 ++++++++++++++
 src/couch_views/src/couch_views_fdb.erl | 40 ++++++++++++++++++++++++++++++++-
 2 files changed, 56 insertions(+), 1 deletion(-)

diff --git a/src/couch_views/src/couch_views.erl b/src/couch_views/src/couch_views.erl
index 2268052..5f7e767 100644
--- a/src/couch_views/src/couch_views.erl
+++ b/src/couch_views/src/couch_views.erl
@@ -21,6 +21,7 @@
 
     % fabric2_index behavior
     build_indices/2,
+    cleanup_indices/2,
     get_info/2
 ]).
 
@@ -75,6 +76,22 @@ build_indices(#{} = Db, DDocs) when is_list(DDocs) ->
     end, DDocs).
 
 
+cleanup_indices(#{} = Db, DDocs) when is_list(DDocs) ->
+    ActiveSigs = lists:filtermap(fun(DDoc) ->
+        try couch_views_util:ddoc_to_mrst(DbName, DDoc) of
+            {ok, #mrst{sig = Sig}} ->
+                {true, Sig}
+        catch _:_ ->
+            false
+        end
+    end, DDocs),
+    ExistingSigs = couch_views_fdb:list_signatures(Db),
+    StaleSigs = ExistingSigs -- ActiveSigs,
+    lists:foreach(fun(Sig) ->
+        couch_views_fdb:clear_index(Db, Sig)
+    end, StaleSigs).
+
+
 get_info(Db, DDoc) ->
     DbName = fabric2_db:name(Db),
     {ok, Mrst} = couch_views_util:ddoc_to_mrst(DbName, DDoc),
diff --git a/src/couch_views/src/couch_views_fdb.erl b/src/couch_views/src/couch_views_fdb.erl
index a0224b2..64d6f66 100644
--- a/src/couch_views/src/couch_views_fdb.erl
+++ b/src/couch_views/src/couch_views_fdb.erl
@@ -21,7 +21,10 @@
 
     fold_map_idx/6,
 
-    write_doc/4
+    write_doc/4,
+
+    list_signatures/1,
+    clear_index/2
 ]).
 
 -ifdef(TEST).
@@ -151,6 +154,41 @@ write_doc(TxDb, Sig, ViewIds, Doc) ->
     end, lists:zip3(ViewIds, Results, KVSizes)).
 
 
+list_signatures(Db) ->
+    #{
+        db_prefix := DbPrefix
+    } = Db,
+    ViewSeqRange = {?DB_VIEWS, ?VIEW_INFO, ?VIEW_UPDATE_SEQ}
+    RangePrefix = erlfdb_tuple:pack(ViewSeqRange, DbPrefix),
+    fabric2_fdb:fold_range(Db, RangePrefix, fun({Key, _Val}, Acc) ->
+        {Sig} = erlfdb_tuple:unpack(Key, RangePrefix),
+        [Sig | Acc]
+    end, [], []).
+
+
+clear_index(Db, Signature) ->
+    #{
+        tx := Tx,
+        db_prefix := DbPrefix
+    } = Db,
+
+    % Clear index info keys
+    Keys = [
+        {?DB_VIEWS, ?VIEW_INFO, ?VIEW_UPDATE_SEQ, Signature},
+        {?DB_VIEWS, ?VIEW_INFO, ?VIEW_ROW_COUNT, Signature},
+        {?DB_VIEWS, ?VIEW_INFO, ?VIEW_KV_SIZE, Signature}
+    ],
+    lists:foreach(fun(Key) ->
+        FDBKey = erlfdb_tuple:pack(Key, DbPrefix),
+        erlfdb:clear(Tx, FDBKey)
+    end, Keys),
+
+    % Clear index data
+    RangeTuple = {?DB_VIEWS, ?VIEW_DATA, Signature}
+    RangePrefix = erlfdb_tuple:pack(RangeTuple, DbPrefix),
+    erlfdb:clear_range_startswith(Tx, RangePrefix).
+
+
 % For each row in a map view we store the the key/value
 % in FoundationDB:
 %