Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

Already on GitHub? Sign in to your account

Indexes are always strings, regardless of index type #43

Closed
massung opened this Issue Mar 19, 2012 · 3 comments

Comments

Projects
None yet
3 participants
Contributor

massung commented Mar 19, 2012

{ok,C}=riakc_pb_socket:start_link("localhost",8087).
O1=riakc_obj:new(<<"B">>,<<"K">>,<<1>>).
M=dict:store(<<"index">>,[{"field_int",1000}]).
O2j=riakc_obj:update_metadata(O1,M).
riakc_pb_socket:put(C,O2,[]).
{ok,O3}=riakc_pb_socket:get(<<"B">>,<<"K">>).
M2=riakc_obj:get_metadata(O3).
Indexes=dict:fetch(<<"index">>,M2).
N=proplists:get_value("field_int",Indexes).
io:format("~p",[N]).

As you can see, going in, N is an integer (1000), and coming out it's a string ("1000"). This is due to:

%% Convert {K,V} tuple to protocol buffers
pbify_rpbpair({K,V}) ->
    #rpbpair{key = K, value = any_to_list(V)}.

Simply put, indexes and links need overhauled in the riak-erlang-client so that people aren't mucking with the metadata dictionary and they keep their types (or error if the wrong type).

Fixes the small errors in the example.

{ok,C}=riakc_pb_socket:start_link("localhost",8087).
O1=riakc_obj:new(<<"B">>,<<"K">>,<<1>>).
M=dict:from_list([{<<"index">>,[{"field_int",1000}]}]).
O2=riakc_obj:update_metadata(O1,M).
riakc_pb_socket:put(C,O2,[]).
{ok,O3}=riakc_pb_socket:get(C, <<"B">>,<<"K">>).
M2=riakc_obj:get_metadata(O3).
Indexes=dict:fetch(<<"index">>,M2).
N=proplists:get_value(<<"field_int">>,Indexes).
io:format("~p",[N]).

It comes out as binary <<"1000">>

In riak_pb_kv_codec.erl

decode_content_meta(indexes, PbIndexes, _Pb) ->
    Indexes = [decode_pair(E) || E <- PbIndexes],
    [{?MD_INDEX, Indexes}];

What I understood, by quickly going through specs and code, these secondary indexes are suffixed "special case" which determines the type. The proper solution be then to make a new decode_index_pair/1 to handle these (there already exists encode_index_pair/1 as mentioned in update)? There is also currently already a different decoder for links and the code for this is now located actually in https://github.com/basho/riak_pb

Update: Seems that there is work already done for the encoding side basho/riak_pb#13

If you use PR #81's new riakc_obj:get_secondary_index[es] functions instead of directly accessing the metadata, you now get integers instead of strings for integer indexes.

@beerriot beerriot closed this Jan 23, 2013

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment