<commit_before>%%%% -------------------------------------------------------------------
%%%%
%%%% Copyright (c) 2014 SyncFree Consortium.  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(rubis).

-define(ru, rubis_utils).
-define(default_group, <<"global_index">>).

-define(committed, {ok, []}).

-type key() :: term().
-type reason() :: atom().

-export([process_request/2]).

process_request('Ping', _) ->
    {ok, TxId} = pvc:start_transaction(),
    Commit = pvc:commit_transaction(TxId),
    case Commit of
        ?committed ->
            ok;
        {error, Reason} ->
            {error, Reason}
    end;

process_request('Load', #{num_keys := N, bin_size := Size}) ->
    Val = crypto:strong_rand_bytes(Size),
    {Keys, Updates} = lists:foldl(fun(K, {KeyAcc, UpdateAcc}) ->
        Gen = integer_to_binary(K, 36),
        {[Gen | KeyAcc], [{Gen, Val} | UpdateAcc]}
    end, {[],[]}, lists:seq(1, N)),
    {ok, TxId} = pvc:start_transaction(),
    case pvc:read_keys(Keys, TxId) of
        {error, _}=ReadError ->
            ReadError;
        {ok, _} ->
            ok = pvc:update_keys(Updates, TxId),
            Commit = pvc:commit_transaction(TxId),
            case Commit of
                ?committed ->
                    ok;
                {error, Reason} ->
                    {error, Reason}
            end
    end;

process_request('ReadOnlyTx', #{keys := Keys}) ->
    {ok, TxId} = pvc:start_transaction(),
    case pvc:read_keys(Keys, TxId) of
        {error, _}=ReadError ->
            ReadError;
        {ok, _} ->
            Commit = pvc:commit_transaction(TxId),
            case Commit of
                ?committed ->
                    ok;
                {error, Reason} ->
                    {error, Reason}
            end
    end;

process_request('ReadWriteTx', #{read_keys := Keys, ops := OpList}) ->
    Updates = lists:map(fun(#{key := K, value := V}) -> {K, V} end, OpList),

    {ok, TxId} = pvc:start_transaction(),
    case pvc:read_keys(Keys, TxId) of
        {error, _}=ReadError ->
            ReadError;
        {ok, _} ->
            ok = pvc:update_keys(Updates, TxId),
            Commit = pvc:commit_transaction(TxId),
            case Commit of
                ?committed ->
                    ok;
                {error, Reason} ->
                    {error, Reason}
            end
    end;

%% Used for rubis load
process_request('PutRegion', #{region_name := Name}) ->
    put_region(Name);

%% Used for rubis load
process_request('PutCategory', #{category_name := Name}) ->
    put_category(Name);

%% Benchmark only
process_request('AuthUser', #{username := Username,
                              password := Password}) -><commit_msg>Don't batch reads during microbenchmarks<commit_after>%%%% -------------------------------------------------------------------
%%%%
%%%% Copyright (c) 2014 SyncFree Consortium.  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(rubis).

-define(ru, rubis_utils).
-define(default_group, <<"global_index">>).

-define(committed, {ok, []}).

-type key() :: term().
-type reason() :: atom().

-export([process_request/2]).

%% @doc Force sequential reads for blotter-style microbenchmarks
sequential_read([], _Tx) ->
    ok;

sequential_read([Key | Keys], Tx) ->
    case pvc:read_keys(Key, Tx) of
        {error, _}=ReadError ->
            ReadError;
        {ok, _} ->
            sequential_read(Keys, Tx)
    end.

process_request('Ping', _) ->
    {ok, TxId} = pvc:start_transaction(),
    Commit = pvc:commit_transaction(TxId),
    case Commit of
        ?committed ->
            ok;
        {error, Reason} ->
            {error, Reason}
    end;

process_request('Load', #{num_keys := N, bin_size := Size}) ->
    Val = crypto:strong_rand_bytes(Size),
    {Keys, Updates} = lists:foldl(fun(K, {KeyAcc, UpdateAcc}) ->
        Gen = integer_to_binary(K, 36),
        {[Gen | KeyAcc], [{Gen, Val} | UpdateAcc]}
    end, {[],[]}, lists:seq(1, N)),
    {ok, TxId} = pvc:start_transaction(),
    case pvc:read_keys(Keys, TxId) of
        {error, _}=ReadError ->
            ReadError;
        {ok, _} ->
            ok = pvc:update_keys(Updates, TxId),
            Commit = pvc:commit_transaction(TxId),
            case Commit of
                ?committed ->
                    ok;
                {error, Reason} ->
                    {error, Reason}
            end
    end;

process_request('ReadOnlyTx', #{keys := Keys}) ->
    {ok, TxId} = pvc:start_transaction(),
    case sequential_read(Keys, TxId) of
        {error, _}=ReadError ->
            ReadError;
        ok ->
            Commit = pvc:commit_transaction(TxId),
            case Commit of
                ?committed ->
                    ok;
                {error, Reason} ->
                    {error, Reason}
            end
    end;

process_request('ReadWriteTx', #{read_keys := Keys, ops := OpList}) ->
    Updates = lists:map(fun(#{key := K, value := V}) -> {K, V} end, OpList),

    {ok, TxId} = pvc:start_transaction(),
    case sequential_read(Keys, TxId) of
        {error, _}=ReadError ->
            ReadError;
        ok ->
            ok = pvc:update_keys(Updates, TxId),
            Commit = pvc:commit_transaction(TxId),
            case Commit of
                ?committed ->
                    ok;
                {error, Reason} ->
                    {error, Reason}
            end
    end;

%% Used for rubis load
process_request('PutRegion', #{region_name := Name}) ->
    put_region(Name);

%% Used for rubis load
process_request('PutCategory', #{category_name := Name}) ->
    put_category(Name);

%% Benchmark only
process_request('AuthUser', #{username := Username,
                              password := Password}) ->
{'fraction_non_alphanumeric': 0.18280786087940054, 'fraction_numerical': 0.003393185352749894, 'mean_word_length': 1.9772727272727273, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 57, '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': '1299742', 'n_tokens_mistral': 2091, 'n_tokens_neox': 2024, 'n_words': 554}