diff options
author | Paul J. Davis <paul.joseph.davis@gmail.com> | 2020-03-25 14:50:45 -0500 |
---|---|---|
committer | Paul J. Davis <paul.joseph.davis@gmail.com> | 2020-04-10 16:30:49 -0500 |
commit | 7bc9148b75b1396b91b6cdccca2c8e87b791e0f0 (patch) | |
tree | e6f2065d066c7616b5d0750717b0d3b98cc36813 | |
parent | 3e1c822794990cab06002c4d8a7a6ac22069d9a4 (diff) | |
download | couchdb-7bc9148b75b1396b91b6cdccca2c8e87b791e0f0.tar.gz |
Extend fabric2_index callbacks for index cleanup
Each registered index type can now get a signal on when to clean up
their indexes.
-rw-r--r-- | src/fabric/src/fabric2_index.erl | 23 |
1 files changed, 23 insertions, 0 deletions
diff --git a/src/fabric/src/fabric2_index.erl b/src/fabric/src/fabric2_index.erl index 9a6607e2e..7f9d51974 100644 --- a/src/fabric/src/fabric2_index.erl +++ b/src/fabric/src/fabric2_index.erl @@ -19,6 +19,7 @@ -export([ register_index/1, db_updated/1, + cleanup/1, start_link/0 ]). @@ -38,6 +39,9 @@ -callback build_indices(Db :: map(), DDocs :: list(#doc{})) -> [{ok, JobId::binary()} | {error, any()}]. +-callback cleanup_indices(Db :: map(), DDocs :: list(#doc{})) -> + [ok | {error, any()}]. + -define(SHARDS, 32). -define(DEFAULT_DELAY_MSEC, 60000). @@ -54,6 +58,25 @@ db_updated(DbName) when is_binary(DbName) -> ets:insert_new(Table, {DbName, now_msec()}). +cleanup(Db) -> + try + fabric2_fdb:transactional(Db, fun(TxDb) -> + DDocs = fabric2_db:get_design_docs(TxDb), + lists:foreach(fun(Mod) -> + Mod:cleanup_indices(TxDb, DDocs) + end, registrations()) + end) + catch + error:database_does_not_exist -> + ok; + Tag:Reason -> + Stack = erlang:get_stacktrace(), + DbName = fabric2_db:name(Db), + LogMsg = "~p failed to cleanup indices for `~s` ~p:~p ~p", + couch_log:error(LogMsg, [?MODULE, DbName, Tag, Reason, Stack]) + end. + + start_link() -> gen_server:start_link({local, ?MODULE}, ?MODULE, [], []). |