<commit_before>%% -------------------------------------------------------------------
%%
%% riakc_pb: protocol buffer utility functions
%%
%% 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.
%%
%% -------------------------------------------------------------------

%% @doc protocol buffer utilities

-module(riakc_pb).
-ifdef(TEST).
-include_lib("eunit/include/eunit.hrl").
-endif.
-include("riakclient_pb.hrl").
-include("riakc_obj.hrl").
-compile([export_all]).

%% Names of PB fields in bucket properties
-define(PB_PROPS,   <<"props">>).
-define(PB_KEYS,    <<"keys">>).
-define(PB_LINKFUN, <<"linkfun">>).
-define(PB_MOD,     <<"mod">>).
-define(PB_FUN,     <<"fun">>).
-define(PB_CHASH,   <<"chash_keyfun">>).
-define(PB_JSFUN,    <<"jsfun">>).
-define(PB_JSANON,   <<"jsanon">>).
-define(PB_JSBUCKET, <<"bucket">>).
-define(PB_JSKEY,    <<"key">>).
-define(PB_ALLOW_MULT, <<"allow_mult">>).

%% Create an iolist of msg code and protocol buffer message
encode(Msg) when is_atom(Msg) ->
    [msg_code(Msg)];
encode(Msg) when is_tuple(Msg) ->
    MsgType = element(1, Msg),
    [msg_code(MsgType) | riakclient_pb:iolist(MsgType, Msg)].
 
%% Decode a protocol buffer message given its type - if no bytes
%% return the atom for the message code
decode(MsgCode, <<>>) ->
    msg_type(MsgCode);
decode(MsgCode, MsgData) ->
    riakclient_pb:decode(msg_type(MsgCode), MsgData).

msg_type(0) -> rpberrorresp;
msg_type(1) -> rpbhelloreq;
msg_type(2) -> rpbhelloresp;
msg_type(3) -> rpbpingreq;
msg_type(4) -> rpbpingresp;
msg_type(5) -> rpbgetreq;
msg_type(6) -> rpbgetresp;
msg_type(7) -> rpbputreq;
msg_type(8) -> rpbputresp;
msg_type(9) -> rpbdelreq;
msg_type(10) -> rpbdelresp;
msg_type(11) -> rpbgetbucketpropsreq;
msg_type(12) -> rpbgetbucketpropsresp;
msg_type(13) -> rpbsetbucketpropsreq;
msg_type(14) -> rpbsetbucketpropsresp;
msg_type(15) -> rpblistbucketsreq;
msg_type(16) -> rpblistbucketsresp;
msg_type(17) -> rpblistkeysreq;
msg_type(18) -> rpblistkeysresp;
msg_type(19) -> rpbmapredreq;
msg_type(20) -> rpbmapredresp;
msg_type(_) -> undefined.
    
msg_code(rpberrorresp) -> 0;
msg_code(rpbhelloreq)  -> 1;
msg_code(rpbhelloresp) -> 2;
msg_code(rpbpingreq)   -> 3;
msg_code(rpbpingresp)  -> 4;
msg_code(rpbgetreq)    -> 5;
msg_code(rpbgetresp)   -> 6;
msg_code(rpbputreq)    -> 7;
msg_code(rpbputresp)   -> 8;
msg_code(rpbdelreq)    -> 9;
msg_code(rpbdelresp)   -> 10;
msg_code(rpbgetbucketpropsreq)   -> 11;
msg_code(rpbgetbucketpropsresp)  -> 12;
msg_code(rpbsetbucketpropsreq)   -> 13;
msg_code(rpbsetbucketpropsresp)  -> 14;
msg_code(rpblistbucketsreq)      -> 15;
msg_code(rpblistbucketsresp)     -> 16;
msg_code(rpblistkeysreq)         -> 17;
msg_code(rpblistkeysresp)        -> 18;
msg_code(rpbmapredreq)           -> 19;
msg_code(rpbmapredresp)          -> 20.
    

%% ===================================================================
%% Encoding/Decoding
%% ===================================================================
    
%% Convert options to protocol buffers
pbify_rpboptions(undefined) ->
    undefined;
pbify_rpboptions(Options) ->
   pbify_rpboptions(Options, #rpboptions{}).

pbify_rpboptions([], RpbOpts) ->
    RpbOpts;
pbify_rpboptions([return_body|Rest], RpbOpts) ->
    pbify_rpboptions(Rest, RpbOpts#rpboptions{return_body = true}).

%% Convert a list of {MetaData,Value} pairs to protocol buffers
pbify_rpbcontents([], Acc) ->
    lists:reverse(Acc);
pbify_rpbcontents([Content | Rest], Acc) ->
    pbify_rpbcontents(Rest, [pbify_rpbcontent(Content) | Acc]).

%% Convert a metadata/value pair into an #rpbcontent{} record    
pbify_rpbcontent({Metadata, Value}) ->
    dict:fold(fun pbify_rpbcontent_entry/3, {#rpbcontent{value = Value}, []}, Metadata).

%% Convert the metadata dictionary entries to protocol buffers
pbify_rpbcontent_entry(?MD_CTYPE, ContentType, PbContent) when is_list(ContentType) -> 
    PbContent#rpbcontent{content_type = ContentType};
pbify_rpbcontent_entry(?MD_CHARSET, Charset, PbContent) when is_list(Charset) ->
    PbContent#rpbcontent{charset = Charset};
pbify_rpbcontent_entry(?MD_ENCODING, Encoding, PbContent) when is_list(Encoding) ->
    PbContent#rpbcontent{content_encoding = Encoding};
pbify_rpbcontent_entry(?MD_VTAG, Vtag, PbContent) when is_list(Vtag) ->
    PbContent#rpbcontent{vtag = Vtag};
pbify_rpbcontent_entry(?MD_LINKS, Links, PbContent) when is_list(Links) ->
    PbContent#rpbcontent{links = [pbify_rpblink(E) || E <- Links]};
pbify_rpbcontent_entry(?MD_LASTMOD, {MS,S,US}, PbContent) -> 
    PbContent#rpbcontent{last_mod = 1000000*MS+S, last_mod_usecs = US};
pbify_rpbcontent_entry(?MD_USERMETA, UserMeta, PbContent) when is_list(UserMeta) ->
    PbContent#rpbcontent{usermeta = [pbify_rpbpair(E) || E <- UserMeta]};
pbify_rpbcontent_entry(Key, Value, PbContent) ->
    PbContent.
<commit_msg>Cleaned up warnings about unused variables.<commit_after>%% -------------------------------------------------------------------
%%
%% riakc_pb: protocol buffer utility functions
%%
%% 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.
%%
%% -------------------------------------------------------------------

%% @doc protocol buffer utilities

-module(riakc_pb).
-ifdef(TEST).
-include_lib("eunit/include/eunit.hrl").
-endif.
-include("riakclient_pb.hrl").
-include("riakc_obj.hrl").
-compile([export_all]).

%% Names of PB fields in bucket properties
-define(PB_PROPS,   <<"props">>).
-define(PB_KEYS,    <<"keys">>).
-define(PB_LINKFUN, <<"linkfun">>).
-define(PB_MOD,     <<"mod">>).
-define(PB_FUN,     <<"fun">>).
-define(PB_CHASH,   <<"chash_keyfun">>).
-define(PB_JSFUN,    <<"jsfun">>).
-define(PB_JSANON,   <<"jsanon">>).
-define(PB_JSBUCKET, <<"bucket">>).
-define(PB_JSKEY,    <<"key">>).
-define(PB_ALLOW_MULT, <<"allow_mult">>).

%% Create an iolist of msg code and protocol buffer message
encode(Msg) when is_atom(Msg) ->
    [msg_code(Msg)];
encode(Msg) when is_tuple(Msg) ->
    MsgType = element(1, Msg),
    [msg_code(MsgType) | riakclient_pb:iolist(MsgType, Msg)].
 
%% Decode a protocol buffer message given its type - if no bytes
%% return the atom for the message code
decode(MsgCode, <<>>) ->
    msg_type(MsgCode);
decode(MsgCode, MsgData) ->
    riakclient_pb:decode(msg_type(MsgCode), MsgData).

msg_type(0) -> rpberrorresp;
msg_type(1) -> rpbhelloreq;
msg_type(2) -> rpbhelloresp;
msg_type(3) -> rpbpingreq;
msg_type(4) -> rpbpingresp;
msg_type(5) -> rpbgetreq;
msg_type(6) -> rpbgetresp;
msg_type(7) -> rpbputreq;
msg_type(8) -> rpbputresp;
msg_type(9) -> rpbdelreq;
msg_type(10) -> rpbdelresp;
msg_type(11) -> rpbgetbucketpropsreq;
msg_type(12) -> rpbgetbucketpropsresp;
msg_type(13) -> rpbsetbucketpropsreq;
msg_type(14) -> rpbsetbucketpropsresp;
msg_type(15) -> rpblistbucketsreq;
msg_type(16) -> rpblistbucketsresp;
msg_type(17) -> rpblistkeysreq;
msg_type(18) -> rpblistkeysresp;
msg_type(19) -> rpbmapredreq;
msg_type(20) -> rpbmapredresp;
msg_type(_) -> undefined.
    
msg_code(rpberrorresp) -> 0;
msg_code(rpbhelloreq)  -> 1;
msg_code(rpbhelloresp) -> 2;
msg_code(rpbpingreq)   -> 3;
msg_code(rpbpingresp)  -> 4;
msg_code(rpbgetreq)    -> 5;
msg_code(rpbgetresp)   -> 6;
msg_code(rpbputreq)    -> 7;
msg_code(rpbputresp)   -> 8;
msg_code(rpbdelreq)    -> 9;
msg_code(rpbdelresp)   -> 10;
msg_code(rpbgetbucketpropsreq)   -> 11;
msg_code(rpbgetbucketpropsresp)  -> 12;
msg_code(rpbsetbucketpropsreq)   -> 13;
msg_code(rpbsetbucketpropsresp)  -> 14;
msg_code(rpblistbucketsreq)      -> 15;
msg_code(rpblistbucketsresp)     -> 16;
msg_code(rpblistkeysreq)         -> 17;
msg_code(rpblistkeysresp)        -> 18;
msg_code(rpbmapredreq)           -> 19;
msg_code(rpbmapredresp)          -> 20.
    

%% ===================================================================
%% Encoding/Decoding
%% ===================================================================
    
%% Convert options to protocol buffers
pbify_rpboptions(undefined) ->
    undefined;
pbify_rpboptions(Options) ->
   pbify_rpboptions(Options, #rpboptions{}).

pbify_rpboptions([], RpbOpts) ->
    RpbOpts;
pbify_rpboptions([return_body|Rest], RpbOpts) ->
    pbify_rpboptions(Rest, RpbOpts#rpboptions{return_body = true}).

%% Convert a list of {MetaData,Value} pairs to protocol buffers
pbify_rpbcontents([], Acc) ->
    lists:reverse(Acc);
pbify_rpbcontents([Content | Rest], Acc) ->
    pbify_rpbcontents(Rest, [pbify_rpbcontent(Content) | Acc]).

%% Convert a metadata/value pair into an #rpbcontent{} record    
pbify_rpbcontent({Metadata, Value}) ->
    dict:fold(fun pbify_rpbcontent_entry/3, {#rpbcontent{value = Value}, []}, Metadata).

%% Convert the metadata dictionary entries to protocol buffers
pbify_rpbcontent_entry(?MD_CTYPE, ContentType, PbContent) when is_list(ContentType) -> 
    PbContent#rpbcontent{content_type = ContentType};
pbify_rpbcontent_entry(?MD_CHARSET, Charset, PbContent) when is_list(Charset) ->
    PbContent#rpbcontent{charset = Charset};
pbify_rpbcontent_entry(?MD_ENCODING, Encoding, PbContent) when is_list(Encoding) ->
    PbContent#rpbcontent{content_encoding = Encoding};
pbify_rpbcontent_entry(?MD_VTAG, Vtag, PbContent) when is_list(Vtag) ->
    PbContent#rpbcontent{vtag = Vtag};
pbify_rpbcontent_entry(?MD_LINKS, Links, PbContent) when is_list(Links) ->
    PbContent#rpbcontent{links = [pbify_rpblink(E) || E <- Links]};
pbify_rpbcontent_entry(?MD_LASTMOD, {MS,S,US}, PbContent) -> 
    PbContent#rpbcontent{last_mod = 1000000*MS+S, last_mod_usecs = US};
pbify_rpbcontent_entry(?MD_USERMETA, UserMeta, PbContent) when is_list(UserMeta) ->
    PbContent#rpbcontent{usermeta = [pbify_rpbpair(E) || E <- UserMeta]};
pbify_rpbcontent_entry(_Key, _Value, PbContent) ->
    %% Ignore unknown metadata - need to add to RpbContent if it needs to make it
    %% to/from the client
    PbContent.

{'fraction_non_alphanumeric': 0.18839122486288848, 'fraction_numerical': 0.015539305301645339, 'mean_word_length': 4.4677661169415295, 'pattern_counts': {'":': 0, '<': 55, '<?xml version=': 0, '>': 175, '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': '3257560', 'n_tokens_mistral': 4079, 'n_tokens_neox': 3848, 'n_words': 798}