<commit_before>%% -------------------------------------------------------------------
%%
%% riak_core: Core Active Preference Lists
%%
%% Copyright (c) 2007-2010 Basho Technologies, Inc.  All Rights Reserved.
%%
%% This file is provided to you 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.
%%
%% -------------------------------------------------------------------
%% Get active preference list - preference list with secondary nodes
%% substituted.
%% -------------------------------------------------------------------
-module(riak_core_apl).
-export([get_apl/2, get_apl/4]).

-ifdef(TEST).
-include_lib("eunit/include/eunit.hrl").
-endif.

-type index() :: non_neg_integer().
-type n_val() :: non_neg_integer().
-type ring() :: term().
-type preflist() :: [{index(), node()}].

%% Get the active preflist taking account of which nodes are up
-spec get_apl(binary(), n_val()) -> preflist().
get_apl(DocIdx, N) ->
    {ok, Ring} = riak_core_ring_manager:get_my_ring(),
    get_apl(DocIdx, N, Ring, [node()|nodes()]).

%% Get the active preflist taking account of which nodes are up
%% for a given ring/upnodes list
-spec get_apl(binary(), n_val(), ring(), [node()]) -> preflist().
get_apl(DocIdx, N, Ring, UpNodes) ->
    UpNodes1 = ordsets:from_list(UpNodes),
    Preflist = riak_core_ring:preflist(DocIdx, Ring),
    {Primaries, Fallbacks} = lists:split(N, Preflist),
    {Up, Pangs} = check_up(Primaries, UpNodes1, [], []),
    lists:reverse(Up) ++ find_fallbacks(Pangs, Fallbacks, UpNodes1, []).

%% Split a preference list into up and down lists
-spec check_up(preflist(), [node()], preflist(), preflist()) -> {preflist(), preflist()}.
check_up([], _UpNodes, Up, Pangs) ->
    {Up, Pangs};
check_up([{Partition,Node}|Rest], UpNodes, Up, Pangs) ->
    case is_up(Node, UpNodes) of
        true ->
            check_up(Rest, UpNodes, [{Partition, Node} | Up], Pangs);
        false ->
            check_up(Rest, UpNodes, Up, [{Partition, Node} | Pangs])
    end.

%% Find fallbacks for downed nodes in the preference list
-spec find_fallbacks(preflist(), preflist(), [node()], preflist()) -> preflist().<commit_msg>Added riak_core_apl:active_owners call to get preflist of all active primary nodes.<commit_after>%% -------------------------------------------------------------------
%%
%% riak_core: Core Active Preference Lists
%%
%% Copyright (c) 2007-2010 Basho Technologies, Inc.  All Rights Reserved.
%%
%% This file is provided to you 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.
%%
%% -------------------------------------------------------------------
%% Get active preference list - preference list with secondary nodes
%% substituted.
%% -------------------------------------------------------------------
-module(riak_core_apl).
-export([active_owners/0, active_owners/2,
         get_apl/2, get_apl/4]).

-ifdef(TEST).
-include_lib("eunit/include/eunit.hrl").
-endif.

-type index() :: non_neg_integer().
-type n_val() :: non_neg_integer().
-type ring() :: term().
-type preflist() :: [{index(), node()}].

%% Return preflist of all active primary nodes (with no
%% substituion of fallbacks).  Used to simulate a
%% preflist with N=ring_size
-spec active_owners() -> preflist().
active_owners() ->
    {ok, Ring} = riak_core_ring_manager:get_my_ring(),
    active_owners(Ring, [node()|nodes()]).

-spec active_owners(ring(), [node()]) -> preflist().
active_owners(Ring, UpNodes) ->
    UpNodes1 = ordsets:from_list(UpNodes),
    Primaries = riak_core_ring:all_owners(Ring),
    {Up, _Pangs} = check_up(Primaries, UpNodes1, [], []),
    lists:reverse(Up).

%% Get the active preflist taking account of which nodes are up
-spec get_apl(binary(), n_val()) -> preflist().
get_apl(DocIdx, N) ->
    {ok, Ring} = riak_core_ring_manager:get_my_ring(),
    get_apl(DocIdx, N, Ring, [node()|nodes()]).

%% Get the active preflist taking account of which nodes are up
%% for a given ring/upnodes list
-spec get_apl(binary(), n_val(), ring(), [node()]) -> preflist().
get_apl(DocIdx, N, Ring, UpNodes) ->
    UpNodes1 = ordsets:from_list(UpNodes),
    Preflist = riak_core_ring:preflist(DocIdx, Ring),
    {Primaries, Fallbacks} = lists:split(N, Preflist),
    {Up, Pangs} = check_up(Primaries, UpNodes1, [], []),
    lists:reverse(Up) ++ find_fallbacks(Pangs, Fallbacks, UpNodes1, []).

%% Split a preference list into up and down lists
-spec check_up(preflist(), [node()], preflist(), preflist()) -> {preflist(), preflist()}.
check_up([], _UpNodes, Up, Pangs) ->
    {Up, Pangs};
check_up([{Partition,Node}|Rest], UpNodes, Up, Pangs) ->
    case is_up(Node, UpNodes) of
        true ->
            check_up(Rest, UpNodes, [{Partition, Node} | Up], Pangs);
        false ->
            check_up(Rest, UpNodes, Up, [{Partition, Node} | Pangs])
    end.

%% Find fallbacks for downed nodes in the preference list
-spec find_fallbacks(preflist(), preflist(), [node()], preflist()) -> preflist().
{'fraction_non_alphanumeric': 0.21290650406504066, 'fraction_numerical': 0.006436314363143632, 'mean_word_length': 3.080856945404285, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 27, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4874596', 'n_tokens_mistral': 1827, 'n_tokens_neox': 1796, 'n_words': 562}