Skip to content
New issue

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

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

move tscoverage en/decoders from riak_kv over here #190

Merged
merged 2 commits into from
Apr 19, 2016
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 54 additions & 1 deletion src/riak_pb_ts_codec.erl
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,9 @@
encode_cells/1,
encode_cells_non_strict/1,
decode_cells/1,
encode_field_type/1]).
encode_field_type/1,
encode_cover_list/1,
decode_cover_list/1]).


-type tsrow() :: #tsrow{}.
Expand Down Expand Up @@ -243,6 +245,57 @@ decode_cells([#tscell{varchar_value = undefined,
%% TODO: represent null cells by something other than an empty list. emptyTsCell atom maybe?
decode_cells(T, [[] | Acc]).



%% Copied and modified from riak_kv_pb_coverage:convert_list. Would
%% be nice to collapse them back together, probably with a closure,
%% but time and effort.
-type ts_range() :: {FieldName::binary(),
{{StartVal::integer(), StartIncl::boolean()},
{EndVal::integer(), EndIncl::boolean()}}}.

-spec encode_cover_list([{{IP::string(), Port::non_neg_integer()},
Context::binary(),
ts_range(),
SQLText::binary()}]) -> [#tscoverageentry{}].
encode_cover_list(Entries) ->
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We generally try to make sure exported functions have specs. Internal ones are less important.

[#tscoverageentry{ip = IP, port = Port,
cover_context = Context,
range = encode_ts_range({Range, SQLText})}
|| {{IP, Port}, Context, Range, SQLText} <- Entries].

-spec decode_cover_list([#tscoverageentry{}]) ->
[{{IP::string(), Port::non_neg_integer()},
CoverContext::binary(), ts_range(), Text::binary()}].
decode_cover_list(Entries) ->
[begin
{RangeStruct, Text} = decode_ts_range(Range),
{{IP, Port}, CoverContext, RangeStruct, Text}
end || #tscoverageentry{ip = IP, port = Port,
cover_context = CoverContext,
range = Range} <- Entries].

-spec encode_ts_range({ts_range(), binary()}) -> #tsrange{}.
encode_ts_range({{FieldName, {{StartVal, StartIncl}, {EndVal, EndIncl}}}, Text}) ->
#tsrange{field_name = FieldName,
lower_bound = StartVal,
lower_bound_inclusive = StartIncl,
upper_bound = EndVal,
upper_bound_inclusive = EndIncl,
desc = Text
}.

-spec decode_ts_range(#tsrange{}) -> {ts_range(), binary()}.
decode_ts_range(#tsrange{field_name = FieldName,
lower_bound = StartVal,
lower_bound_inclusive = StartIncl,
upper_bound = EndVal,
upper_bound_inclusive = EndIncl,
desc = Text}) ->
{{FieldName, {{StartVal, StartIncl}, {EndVal, EndIncl}}}, Text}.



-ifdef(TEST).
-include_lib("eunit/include/eunit.hrl").

Expand Down