<commit_before>%%%-------------------------------------------------------------------
%%%
%%% riak_kv_select: Upgrade and downgrade for the riak_select_v* records.
%%%
%%% Copyright (C) 2016 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.
%%%
%%%-------------------------------------------------------------------

-module(riak_kv_select).

-export([convert/2]).
-export([current_version/0]).
-export([first_version/0]).
-export([is_sql_select_record/1]).

-include("riak_kv_ts.hrl").

%% Return the version as an integer that the select record is currently using.
-spec current_version() -> integer().
current_version() ->
    select_record_version(?SQL_SELECT_RECORD_NAME).

%% Return the first version that was declared for the select record.
-spec first_version() -> integer().
first_version() ->
    1.

%% Convert a select record to a different version.
-spec convert(integer(), Select1::tuple()) -> Select2::tuple().
convert(Version, Select) when is_integer(Version) ->
    CurrentVersion = select_record_version(element(1, Select)),
    if
        Version == CurrentVersion ->
            Select;
        Version > CurrentVersion ->
            VersionSteps = lists:seq(CurrentVersion, Version),
            upgrade_select(VersionSteps, Select);
        Version < CurrentVersion ->
            VersionSteps = lists:seq(CurrentVersion, Version, -1),
            downgrade_select(VersionSteps, Select)
    end.

%% Iterate over the versions and upgrade the record, from 1 to 2, 2 to 3 etc.
upgrade_select([_], Select) ->
    Select;
upgrade_select([1,2 = To|Tail], Select1) ->
    Select2 = setelement(1, Select1, riak_select_v2),
    Select3 = erlang:append_element(Select2, ?GROUP_BY_DEFAULT),
    upgrade_select([To|Tail], Select3).

%% Iterate over the versions backwards to downgrade, from 3 to 2 then 2 to 1 etc.
downgrade_select([_], Select) ->
    Select;
downgrade_select([2,1=To|Tail], Select1) ->
    Select2 = setelement(1, Select1, riak_select_v1),
    %% The group_by field might not be the default value but it is safe to
    %% downgrade because it is not used by remote vnodes.
    Select3 = erlang:delete_element(?SQL_SELECT.group_by, Select2),
    downgrade_select([To|Tail], Select3).

%%
select_record_version(RecordName) ->
    case RecordName of
        riak_select_v1 -> 1;
        riak_select_v2 -> 2<commit_msg>Do not use the record definition to find the index of group by, which this could change in a future definition.<commit_after>%%%-------------------------------------------------------------------
%%%
%%% riak_kv_select: Upgrade and downgrade for the riak_select_v* records.
%%%
%%% Copyright (C) 2016 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.
%%%
%%%-------------------------------------------------------------------

-module(riak_kv_select).

-export([convert/2]).
-export([current_version/0]).
-export([first_version/0]).
-export([is_sql_select_record/1]).

-include("riak_kv_ts.hrl").

%% Return the version as an integer that the select record is currently using.
-spec current_version() -> integer().
current_version() ->
    select_record_version(?SQL_SELECT_RECORD_NAME).

%% Return the first version that was declared for the select record.
-spec first_version() -> integer().
first_version() ->
    1.

%% Convert a select record to a different version.
-spec convert(integer(), Select1::tuple()) -> Select2::tuple().
convert(Version, Select) when is_integer(Version) ->
    CurrentVersion = select_record_version(element(1, Select)),
    if
        Version == CurrentVersion ->
            Select;
        Version > CurrentVersion ->
            VersionSteps = lists:seq(CurrentVersion, Version),
            upgrade_select(VersionSteps, Select);
        Version < CurrentVersion ->
            VersionSteps = lists:seq(CurrentVersion, Version, -1),
            downgrade_select(VersionSteps, Select)
    end.

%% Iterate over the versions and upgrade the record, from 1 to 2, 2 to 3 etc.
upgrade_select([_], Select) ->
    Select;
upgrade_select([1,2 = To|Tail], Select1) ->
    Select2 = setelement(1, Select1, riak_select_v2),
    Select3 = erlang:append_element(Select2, ?GROUP_BY_DEFAULT),
    upgrade_select([To|Tail], Select3).

%% Iterate over the versions backwards to downgrade, from 3 to 2 then 2 to 1 etc.
downgrade_select([_], Select) ->
    Select;
downgrade_select([2,1=To|Tail], Select1) ->
    Select2 = setelement(1, Select1, riak_select_v1),
    %% The group_by field might not be the default value but it is safe to
    %% downgrade because it is not used by remote vnodes.
    V2GroupByIndex = 13,
    Select3 = erlang:delete_element(V2GroupByIndex, Select2),
    downgrade_select([To|Tail], Select3).

%%
select_record_version(RecordName) ->
    case RecordName of
        riak_select_v1 -> 1;
        riak_select_v2 -> 2
{'fraction_non_alphanumeric': 0.15485168426344897, 'fraction_numerical': 0.017094017094017096, 'mean_word_length': 3.161785216178522, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 37, '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': '1321716', 'n_tokens_mistral': 1721, 'n_tokens_neox': 1669, 'n_words': 565}