<commit_before>%%%-------------------------------------------------------------------
%%% @author <NAME>
%%% @copyright (C) 2015 ACK CYFRONET AGH
%%% This software is released under the MIT license
%%% cited in 'LICENSE.txt'.
%%% @end
%%%-------------------------------------------------------------------
%%% @doc
%%% Translations between protobuff and internal protocol
%%% @end
%%%-------------------------------------------------------------------
-module(translator).
-author("<NAME>").

-include("global_definitions.hrl").
-include("proto/oneclient/common_messages.hrl").
-include("proto/oneclient/stream_messages.hrl").
-include("proto/oneclient/handshake_messages.hrl").
-include("proto/oneclient/event_messages.hrl").
-include("proto/oneclient/diagnostic_messages.hrl").
-include_lib("ctool/include/logging.hrl").
-include_lib("clproto/include/messages.hrl").

%% API
-export([translate_from_protobuf/1, translate_to_protobuf/1]).

%%%===================================================================
%%% API
%%%===================================================================

%%--------------------------------------------------------------------
%% @doc
%% traslate protobuf record to internal record
%% @end
%%--------------------------------------------------------------------
-spec translate_from_protobuf(tuple() | undefined) -> tuple() | undefined.
translate_from_protobuf(#'FileBlock'{offset = Off, size = S}) ->
    #file_block{offset = Off, size = S};
translate_from_protobuf(#'Status'{code = Code, description = Desc}) ->
    #status{code = Code, description = Desc};
translate_from_protobuf(#'Event'{event = {_, Record}}) ->
    translate_from_protobuf(Record);
translate_from_protobuf(#'ReadEvent'{} = Record) ->
    #read_event{
        counter = Record#'ReadEvent'.counter,
        file_id = Record#'ReadEvent'.file_id,
        size = Record#'ReadEvent'.size,
        blocks = [translate_to_protobuf(B) || B <- Record#'ReadEvent'.blocks]
    };
translate_from_protobuf(#'WriteEvent'{} = Record) ->
    #write_event{
        counter = Record#'WriteEvent'.counter,
        file_id = Record#'WriteEvent'.file_id,
        size = Record#'WriteEvent'.size,
        blocks = [translate_to_protobuf(B) || B <- Record#'WriteEvent'.blocks]
    };
translate_from_protobuf(#'HandshakeRequest'{token = Token, session_id = SessionId}) ->
    #handshake_request{token = translate_from_protobuf(Token), session_id = SessionId};
translate_from_protobuf(#'MessageStream'{stream_id = StmId, sequence_number = SeqNum}) ->
    #message_stream{stream_id = StmId, sequence_number = SeqNum};
translate_from_protobuf(#'EndOfMessageStream'{}) ->
    #end_of_message_stream{};
translate_from_protobuf(#'Token'{value = Val}) ->
    #token{value = Val};
translate_from_protobuf(#'Ping'{}) ->
    #ping{};
translate_from_protobuf(#'GetProtocolVersion'{}) ->
    #get_protocol_version{};
translate_from_protobuf(#'Data'{data = Data}) ->
    #data{data = Data};
translate_from_protobuf(undefined) ->
    undefined.

%%--------------------------------------------------------------------
%% @doc
%% translate internal record to protobuf record
%% @end
%%--------------------------------------------------------------------
-spec translate_to_protobuf(tuple() | undefined) -> tuple() | undefined.
translate_to_protobuf(#file_block{offset = Off, size = S}) ->
    #'FileBlock'{offset = Off, size = S};
translate_to_protobuf(#status{code = Code, description = Desc}) ->
    {status, #'Status'{code = Code, description = Desc}};
translate_to_protobuf(#event_subscription_cancellation{id = Id}) ->
    {event_subscription, #'EventSubscription'{
        event_subscription = {event_subscription_cancellation,
            #'EventSubscriptionCancellation'{
                id = Id
            }
        }
    }};
translate_to_protobuf(#read_event_subscription{} = Sub) ->
    {event_subscription, #'EventSubscription'{
        event_subscription = {read_event_subscription,
            #'ReadEventSubscription'{
                id = Sub#read_event_subscription.id,
                counter_threshold = Sub#read_event_subscription.producer_counter_threshold,
                time_threshold = Sub#read_event_subscription.producer_time_threshold,
                size_threshold = Sub#read_event_subscription.producer_size_threshold
            }
        }
    }};
translate_to_protobuf(#write_event_subscription{} = Sub) ->
    {event_subscription, #'EventSubscription'{
        event_subscription = {write_event_subscription,
            #'WriteEventSubscription'{
                id = Sub#write_event_subscription.id,<commit_msg>VFS-1072 Fix file block translation.<commit_after>%%%-------------------------------------------------------------------
%%% @author <NAME>
%%% @copyright (C) 2015 ACK CYFRONET AGH
%%% This software is released under the MIT license
%%% cited in 'LICENSE.txt'.
%%% @end
%%%-------------------------------------------------------------------
%%% @doc
%%% Translations between protobuff and internal protocol
%%% @end
%%%-------------------------------------------------------------------
-module(translator).
-author("<NAME>").

-include("global_definitions.hrl").
-include("proto/oneclient/common_messages.hrl").
-include("proto/oneclient/stream_messages.hrl").
-include("proto/oneclient/handshake_messages.hrl").
-include("proto/oneclient/event_messages.hrl").
-include("proto/oneclient/diagnostic_messages.hrl").
-include_lib("ctool/include/logging.hrl").
-include_lib("clproto/include/messages.hrl").

%% API
-export([translate_from_protobuf/1, translate_to_protobuf/1]).

%%%===================================================================
%%% API
%%%===================================================================

%%--------------------------------------------------------------------
%% @doc
%% traslate protobuf record to internal record
%% @end
%%--------------------------------------------------------------------
-spec translate_from_protobuf(tuple() | undefined) -> tuple() | undefined.
translate_from_protobuf(#'Status'{code = Code, description = Desc}) ->
    #status{code = Code, description = Desc};
translate_from_protobuf(#'Event'{event = {_, Record}}) ->
    translate_from_protobuf(Record);
translate_from_protobuf(#'ReadEvent'{} = Record) ->
    #read_event{
        counter = Record#'ReadEvent'.counter,
        file_id = Record#'ReadEvent'.file_id,
        size = Record#'ReadEvent'.size,
        blocks = [translate_from_protobuf(B) || B <- Record#'ReadEvent'.blocks]
    };
translate_from_protobuf(#'WriteEvent'{} = Record) ->
    #write_event{
        counter = Record#'WriteEvent'.counter,
        file_id = Record#'WriteEvent'.file_id,
        size = Record#'WriteEvent'.size,
        file_size = Record#'WriteEvent'.file_size,
        blocks = [translate_from_protobuf(B) || B <- Record#'WriteEvent'.blocks]
    };
translate_from_protobuf(#'FileBlock'{offset = Off, size = S}) ->
    #file_block{offset = Off, size = S};
translate_from_protobuf(#'HandshakeRequest'{token = Token, session_id = SessionId}) ->
    #handshake_request{token = translate_from_protobuf(Token), session_id = SessionId};
translate_from_protobuf(#'MessageStream'{stream_id = StmId, sequence_number = SeqNum}) ->
    #message_stream{stream_id = StmId, sequence_number = SeqNum};
translate_from_protobuf(#'EndOfMessageStream'{}) ->
    #end_of_message_stream{};
translate_from_protobuf(#'Token'{value = Val}) ->
    #token{value = Val};
translate_from_protobuf(#'Ping'{}) ->
    #ping{};
translate_from_protobuf(#'GetProtocolVersion'{}) ->
    #get_protocol_version{};
translate_from_protobuf(#'Data'{data = Data}) ->
    #data{data = Data};
translate_from_protobuf(undefined) ->
    undefined.

%%--------------------------------------------------------------------
%% @doc
%% translate internal record to protobuf record
%% @end
%%--------------------------------------------------------------------
-spec translate_to_protobuf(tuple() | undefined) -> tuple() | undefined.
translate_to_protobuf(#status{code = Code, description = Desc}) ->
    {status, #'Status'{code = Code, description = Desc}};
translate_to_protobuf(#event_subscription_cancellation{id = Id}) ->
    {event_subscription, #'EventSubscription'{
        event_subscription = {event_subscription_cancellation,
            #'EventSubscriptionCancellation'{
                id = Id
            }
        }
    }};
translate_to_protobuf(#read_event_subscription{} = Sub) ->
    {event_subscription, #'EventSubscription'{
        event_subscription = {read_event_subscription,
            #'ReadEventSubscription'{
                id = Sub#read_event_subscription.id,
                counter_threshold = Sub#read_event_subscription.producer_counter_threshold,
                time_threshold = Sub#read_event_subscription.producer_time_threshold,
                size_threshold = Sub#read_event_subscription.producer_size_threshold
            }
        }
    }};
translate_to_protobuf(#write_event_subscription{} = Sub) ->
    {event_subscription, #'EventSubscription'{
        event_subscription = {write_event_subscription,
            #'WriteEventSubscription'{
                id = Sub#write_event_subscription.id,
{'fraction_non_alphanumeric': 0.2590139009556907, 'fraction_numerical': 0.0017376194613379669, 'mean_word_length': 2.7695456406058123, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 46, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '5362342', 'n_tokens_mistral': 2512, 'n_tokens_neox': 2456, 'n_words': 389}