Skip to content

Commit

Permalink
Add support for the X-Riak-Deleted header.
Browse files Browse the repository at this point in the history
Fixes riak-erlang-client #55

Make the riak erlang protocol buffers client aware of the
X-Riak-Deleted header so that the header is included with other object
metadata.
  • Loading branch information
kellymclaughlin committed Jul 6, 2012
1 parent 4aa624b commit d6eddc5
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 3 deletions.
1 change: 1 addition & 0 deletions include/riak_pb_kv_codec.hrl
Expand Up @@ -29,6 +29,7 @@
-define(MD_LASTMOD, <<"X-Riak-Last-Modified">>).
-define(MD_USERMETA, <<"X-Riak-Meta">>).
-define(MD_INDEX, <<"index">>).
-define(MD_DELETED, <<"X-Riak-Deleted">>).

%% Content-Type for Erlang term_to_binary format
-define(CTYPE_ERLANG_BINARY, "application/x-erlang-binary").
Expand Down
1 change: 1 addition & 0 deletions src/riak_kv.proto
Expand Up @@ -186,6 +186,7 @@ message RpbContent {
optional uint32 last_mod_usecs = 8;
repeated RpbPair usermeta = 9; // user metadata stored with the object
repeated RpbPair indexes = 10; // user metadata stored with the object
optional bool deleted = 11;
}

// Link metadata
Expand Down
22 changes: 20 additions & 2 deletions src/riak_pb_kv_codec.erl
Expand Up @@ -96,11 +96,25 @@ encode_content_meta(?MD_USERMETA, UserMeta, PbContent) when is_list(UserMeta) ->
PbContent#rpbcontent{usermeta = [encode_pair(E) || E <- UserMeta]};
encode_content_meta(?MD_INDEX, Indexes, PbContent) when is_list(Indexes) ->
PbContent#rpbcontent{indexes = [encode_pair(E) || E <- Indexes]};
encode_content_meta(?MD_DELETED, DeletedVal, PbContent) ->
PbContent#rpbcontent{deleted=header_val_to_bool(DeletedVal)};
encode_content_meta(_Key, _Value, PbContent) ->
%% Ignore unknown metadata - need to add to RpbContent if it needs to make it
%% to/from the client
PbContent.

%% @doc Return a boolean based on a header value.
%% Representations of `true' return `true'; anything
%% else returns `false'.
-spec header_val_to_bool(term()) -> boolean().
header_val_to_bool(<<"true">>) ->
true;
header_val_to_bool("true") ->
true;
header_val_to_bool(true) ->
true;
header_val_to_bool(_) ->
false.

%% @doc Convert a list of rpbcontent pb messages to a list of [{MetaData,Value}] tuples
-spec decode_contents(PBContents::[tuple()]) -> contents().
Expand Down Expand Up @@ -136,7 +150,10 @@ decode_content_meta(usermeta, PbUserMeta, _Pb) ->
[{?MD_USERMETA, UserMeta}];
decode_content_meta(indexes, PbIndexes, _Pb) ->
Indexes = [decode_pair(E) || E <- PbIndexes],
[{?MD_INDEX, Indexes}].
[{?MD_INDEX, Indexes}];
decode_content_meta(deleted, DeletedVal, _Pb) ->
[{?MD_DELETED, DeletedVal}].


%% @doc Convert an rpccontent pb message to an erlang {MetaData,Value} tuple
-spec decode_content(PBContent::tuple()) -> {riakc_obj:metadata(), riakc_obj:value()}.
Expand All @@ -148,7 +165,8 @@ decode_content(PbC) ->
decode_content_meta(links, PbC#rpbcontent.links, PbC) ++
decode_content_meta(last_mod, PbC#rpbcontent.last_mod, PbC) ++
decode_content_meta(usermeta, PbC#rpbcontent.usermeta, PbC) ++
decode_content_meta(indexes, PbC#rpbcontent.indexes, PbC),
decode_content_meta(indexes, PbC#rpbcontent.indexes, PbC) ++
decode_content_meta(deleted, PbC#rpbcontent.deleted, PbC),

{dict:from_list(MD), PbC#rpbcontent.value}.

Expand Down
21 changes: 20 additions & 1 deletion test/encoding_test.erl
Expand Up @@ -18,7 +18,8 @@ pb_test_() ->
{?MD_USERMETA, [{"X-Riak-Meta-MyMetaData1","here it is"},
{"X-Riak-Meta-MoreMd", "have some more"}
]},
{?MD_INDEX, []}
{?MD_INDEX, []},
{?MD_DELETED, true}
]),
Value = <<"test value">>,
{MetaData2, Value2} = riak_pb_kv_codec:decode_content(
Expand All @@ -30,6 +31,24 @@ pb_test_() ->
?assertEqual(true, MdSame),
Value = Value2
end)},
{"deleted header encode decode",
?_test(begin
InputMD = [dict:from_list([{?MD_DELETED, DelVal}]) ||
DelVal <- [true, "true", false, <<"rubbish">>]],
Value = <<"test value">>,
{OutputMD, _} = lists:unzip(
[riak_pb_kv_codec:decode_content(
riak_kv_pb:decode_rpbcontent(
riak_kv_pb:encode_rpbcontent(
riak_pb_kv_codec:encode_content({MD, Value})))) ||
MD <- InputMD]),
MdSame1 = (lists:sort(dict:to_list(lists:nth(1, OutputMD))) =:=
lists:sort(dict:to_list(lists:nth(2, OutputMD)))),
MdSame2 = (lists:sort(dict:to_list(lists:nth(3, OutputMD))) =:=
lists:sort(dict:to_list(lists:nth(4, OutputMD)))),
?assertEqual(true, MdSame1),
?assertEqual(true, MdSame2)
end)},
{"empty content encode decode",
?_test(begin
MetaData = dict:new(),
Expand Down

0 comments on commit d6eddc5

Please sign in to comment.