You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@couchdb.apache.org by GitBox <gi...@apache.org> on 2022/07/06 14:15:59 UTC

[GitHub] [couchdb] iilyak commented on a diff in pull request #4033: Implement view_report function

iilyak commented on code in PR #4033:
URL: https://github.com/apache/couchdb/pull/4033#discussion_r914894858


##########
src/couch_mrview/src/couch_mrview_debug.erl:
##########
@@ -48,3 +86,159 @@ view_signature(DbName, DDocName) ->
     {ok, DDoc} = couch_db:open_doc_int(Db, <<"_design/", DDocName/binary>>, []),
     {ok, IdxState} = couch_mrview_util:ddoc_to_mrst(DDocName, DDoc),
     couch_util:to_hex(IdxState#mrst.sig).
+
+first_call(Pid) ->
+    IC =
+        case process_info(Pid, initial_call) of
+            {initial_call, IC0} -> IC0;
+            undefined -> undefined
+        end,
+    Dict =
+        case process_info(Pid, dictionary) of
+            {dictionary, Dict0} -> Dict0;
+            undefined -> []
+        end,
+    MaybeCall = proplists:get_value('$initial_call', Dict, IC),
+    proplists:get_value(initial_call, Dict, MaybeCall).
+
+find_by_first_call(Module, Function) ->
+    FilterPids = fun(Pid) ->
+        case first_call(Pid) of
+            {Module, Function, _} -> true;
+            _ -> false
+        end
+    end,
+    lists:filter(FilterPids, processes()).
+
+get_indexer_pid() ->
+    IndexerPids = find_by_first_call(couch_index, init),
+    hd(IndexerPids).
+
+index_state(Pid) when is_pid(Pid) ->
+    {ok, IdxState} = couch_index:get_state(Pid, 0),
+    case IdxState of
+        #mrst{} ->
+            {ok, Info} = couch_index:get_info(Pid),
+            Sig = IdxState#mrst.sig,
+            DbName = IdxState#mrst.db_name,
+            {pending_updates, PendingUpdates} = lists:keyfind(pending_updates, 1, Info),
+            State = #{
+                signature => Sig,
+                db_name => DbName,
+                idx_name => IdxState#mrst.idx_name,
+                update_seq => IdxState#mrst.update_seq,
+                purge_seq => IdxState#mrst.purge_seq,
+                view_file_path => couch_mrview_util:index_file(DbName, Sig),
+                pending_updates => PendingUpdates
+            },
+            {ok, State};
+        _ ->
+            {error, not_mrview_index}
+    end.
+
+view_state(PidOrIdxState) ->
+    view_state(PidOrIdxState, undefined).
+
+view_state(Pid, ViewName) when is_pid(Pid) ->
+    {ok, IdxState} = couch_index:get_state(Pid, 0),
+    view_state(IdxState, ViewName);
+view_state(IdxState, ViewName) ->
+    case IdxState of
+        #mrst{} ->
+            MrViews = lists:foldl(
+                fun(MrView, Acc) ->
+                    {Name, ReduceFuns} =
+                        case MrView#mrview.reduce_funs of
+                            [] ->
+                                {hd(MrView#mrview.map_names), []};
+                            _ ->
+                                % reduce_funs contains tuples of {Name, ReduceFuns}
+                                hd(MrView#mrview.reduce_funs)
+                        end,
+                    View = #{
+                        Name => #{
+                            id_num => MrView#mrview.id_num,
+                            update_seq => MrView#mrview.update_seq,
+                            purge_seq => MrView#mrview.purge_seq,
+                            reduce_funs => ReduceFuns,
+                            def => MrView#mrview.def,
+                            btree_size => couch_btree:size(MrView#mrview.btree),
+                            options => MrView#mrview.options
+                        }
+                    },
+                    maps:merge(View, Acc)

Review Comment:
   Looks like you are just adding a new value. In such case `maps:put/3` would be better.



-- 
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-unsubscribe@couchdb.apache.org

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