nickva commented on code in PR #5466:
URL: https://github.com/apache/couchdb/pull/5466#discussion_r2024017205


##########
src/couch_scanner/src/couch_scanner_plugin_conflict_finder.erl:
##########
@@ -0,0 +1,171 @@
+% Licensed under the Apache License, Version 2.0 (the "License"); you may not
+% use this file except in compliance with the License. You may obtain a copy of
+% the License at
+%
+%   http://www.apache.org/licenses/LICENSE-2.0
+%
+% Unless required by applicable law or agreed to in writing, software
+% distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+% WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+% License for the specific language governing permissions and limitations under
+% the License.
+
+-module(couch_scanner_plugin_conflict_finder).
+-behaviour(couch_scanner_plugin).
+
+-export([
+    start/2,
+    resume/2,
+    complete/1,
+    checkpoint/1,
+    db/2,
+    doc_id/3
+]).
+
+-include_lib("couch_scanner/include/couch_scanner_plugin.hrl").
+
+-record(st, {
+    sid,
+    opts = #{},
+    dbname,
+    report = #{},
+    doc_report = true
+}).
+
+-define(CONFLICTS, <<"conflicts">>).
+-define(DELETED_CONFLICTS, <<"deleted_conflicts">>).
+
+-define(OPTS, #{
+    ?CONFLICTS => true,
+    ?DELETED_CONFLICTS => true
+}).
+
+% Behavior callbacks
+
+start(SId, #{}) ->
+    St = init_config(#st{sid = SId}),
+    ?INFO("Starting.", [], #{sid => SId}),
+    {ok, St}.
+
+resume(SId, #{<<"opts">> := OldOpts}) ->
+    St = init_config(#st{sid = SId}),
+    case OldOpts == St#st.opts of
+        true ->
+            ?INFO("Resuming.", [], #{sid => SId}),
+            {ok, St};
+        false ->
+            ?INFO("Resetting. Config changed.", [], #{sid => SId}),
+            reset
+    end.
+
+complete(#st{sid = SId, dbname = DbName, report = Report} = St) ->
+    report_per_db(St, DbName, Report),
+    ?INFO("Completed", [], #{sid => SId}),
+    {ok, #{}}.
+
+checkpoint(#st{sid = SId, opts = Opts}) ->
+    case Opts == opts() of
+        true ->
+            {ok, #{<<"opts">> => Opts}};
+        false ->
+            ?INFO("Resetting. Config changed.", [], #{sid => SId}),
+            reset
+    end.
+
+db(#st{} = St, _DbName) ->
+    {ok, St}.
+
+doc_id(#st{} = St, <<?DESIGN_DOC_PREFIX, _/binary>>, _Db) ->
+    {skip, St};
+doc_id(#st{} = St, DocId, Db) ->
+    {ok, #doc_info{revs = Revs}} = couch_db:get_doc_info(Db, DocId),
+    DbName = mem3:dbname(couch_db:name(Db)),
+    {ok, check(St, DbName, DocId, Revs)}.
+
+% Private
+
+init_config(#st{} = St) ->
+    St#st{
+        opts = opts(),
+        doc_report = cfg_bool("doc_report", St#st.doc_report)
+    }.
+
+check(#st{} = St, _, _, Revs) when length(Revs) =< 1 ->
+    St;
+check(#st{doc_report = true, opts = Opts} = St, DbName, DocId, Revs) ->
+    {DeletedConflicts, Conflicts} =
+        lists:partition(fun(R) -> R#rev_info.deleted end, Revs),
+    ConflictsReport = gen_report(doc, ?CONFLICTS, Opts, Conflicts),
+    DeletedConflictsReport = gen_report(doc, ?DELETED_CONFLICTS, Opts, 
DeletedConflicts),
+    DocReport = maps:merge(ConflictsReport, DeletedConflictsReport),
+    report_per_doc(#st{} = St, DbName, DocId, DocReport),
+    DbReport = maps:from_list([{K, tuple_size(V)} || {K, V} <- 
maps:to_list(DocReport)]),

Review Comment:
   This could be a maps:map perhaps 
https://www.erlang.org/doc/apps/stdlib/maps.html#map/2 ?



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscr...@couchdb.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org

Reply via email to