kocolosk commented on a change in pull request #1370: [5/5] Clustered Purge Implementation URL: https://github.com/apache/couchdb/pull/1370#discussion_r194847016
########## File path: src/couch/src/couch_db.erl ########## @@ -369,8 +379,145 @@ get_full_doc_info(Db, Id) -> get_full_doc_infos(Db, Ids) -> couch_db_engine:open_docs(Db, Ids). -purge_docs(#db{main_pid=Pid}, IdsRevs) -> - gen_server:call(Pid, {purge_docs, IdsRevs}). +purge_docs(Db, IdRevs) -> + purge_docs(Db, IdRevs, []). + +-spec purge_docs(#db{}, [{UUId, Id, [Rev]}], [PurgeOption]) -> + {ok, [Reply]} when + UUId :: binary(), + Id :: binary(), + Rev :: {non_neg_integer(), binary()}, + PurgeOption :: interactive_edit | replicated_changes, + Reply :: {ok, []} | {ok, [Rev]}. +purge_docs(#db{main_pid = Pid} = Db, UUIDsIdsRevs, Options) -> + % Check here if any UUIDs already exist when + % we're not replicating purge infos + IsRepl = lists:member(replicated_changes, Options), + if IsRepl -> ok; true -> + UUIDs = [UUID || {UUID, _, _} <- UUIDsIdsRevs], + lists:foreach(fun(Resp) -> + if Resp == not_found -> ok; true -> + Fmt = "Duplicate purge info UIUD: ~s", + Reason = io_lib:format(Fmt, [element(2, Resp)]), + throw({badreq, Reason}) + end + end, get_purge_infos(Db, UUIDs)) + end, + increment_stat(Db, [couchdb, database_purges]), + gen_server:call(Pid, {purge_docs, UUIDsIdsRevs, Options}). + +-spec get_purge_infos(#db{}, [UUId]) -> [PurgeInfo] when + UUId :: binary(), + PurgeInfo :: {PurgeSeq, UUId, Id, [Rev]} | not_found, + PurgeSeq :: non_neg_integer(), + Id :: binary(), + Rev :: {non_neg_integer(), binary()}. +get_purge_infos(Db, UUIDs) -> + couch_db_engine:load_purge_infos(Db, UUIDs). + + +get_minimum_purge_seq(#db{} = Db) -> + PurgeSeq = couch_db_engine:get_purge_seq(Db), + OldestPurgeSeq = couch_db_engine:get_oldest_purge_seq(Db), + PurgeInfosLimit = couch_db_engine:get_purge_infos_limit(Db), + + FoldFun = fun(#doc{id = DocId, body = {Props}}, SeqAcc) -> + case DocId of + <<"_local/purge-", _/binary>> -> + ClientSeq = couch_util:get_value(<<"purge_seq">>, Props), + case ClientSeq of + CS when is_integer(CS), CS >= PurgeSeq - PurgeInfosLimit -> + {ok, SeqAcc}; Review comment: I found this a bit tricky to follow. This first clause is just an optimization, right? If we failed the second condition on the guard the worst that could happen is that we do an unnecessary `purge_client_exists` check. ---------------------------------------------------------------- This is an automated message from the Apache Git Service. To respond to the message, please log on GitHub and use the URL above to go to the specific comment. For queries about this service, please contact Infrastructure at: us...@infra.apache.org With regards, Apache Git Services