diff options
author | Paul J. Davis <paul.joseph.davis@gmail.com> | 2020-03-25 14:51:47 -0500 |
---|---|---|
committer | Paul J. Davis <paul.joseph.davis@gmail.com> | 2020-04-10 16:30:49 -0500 |
commit | e0d0391ff8a639101814fae8e74f73b2403561fd (patch) | |
tree | 10e695f23d1b2822cfe447e95762281627c7a293 | |
parent | 7bc9148b75b1396b91b6cdccca2c8e87b791e0f0 (diff) | |
download | couchdb-e0d0391ff8a639101814fae8e74f73b2403561fd.tar.gz |
Implement couch_views:cleanup_indices/2
-rw-r--r-- | src/couch_views/src/couch_views.erl | 18 | ||||
-rw-r--r-- | src/couch_views/src/couch_views_fdb.erl | 40 |
2 files changed, 57 insertions, 1 deletions
diff --git a/src/couch_views/src/couch_views.erl b/src/couch_views/src/couch_views.erl index 2acba00a6..cc183643b 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 ]). @@ -76,6 +77,23 @@ build_indices(#{} = Db, DDocs) when is_list(DDocs) -> end, DDocs). +cleanup_indices(#{} = Db, DDocs) when is_list(DDocs) -> + DbName = fabric2_db:name(Db), + 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 3b008d44b..2181e5373 100644 --- a/src/couch_views/src/couch_views_fdb.erl +++ b/src/couch_views/src/couch_views_fdb.erl @@ -27,7 +27,10 @@ fold_map_idx/6, - write_doc/4 + write_doc/4, + + list_signatures/1, + clear_index/2 ]). -ifdef(TEST). @@ -211,6 +214,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: % |