From b3633533914c315d109456737970d4f6163b50f5 Mon Sep 17 00:00:00 2001 From: Louis-Philippe Gauthier Date: Mon, 6 Apr 2015 18:48:42 -0400 Subject: [PATCH 1/5] Use iolists --- src/marina_request.erl | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/src/marina_request.erl b/src/marina_request.erl index baebf66..abf2f43 100644 --- a/src/marina_request.erl +++ b/src/marina_request.erl @@ -30,7 +30,7 @@ prepare(Stream, FrameFlags, Query) -> stream = Stream, opcode = ?OP_PREPARE, flags = frame_flags(FrameFlags), - body = [<<(marina_types:encode_long_string(Query))/binary>>] + body = [marina_types:encode_long_string(Query)] }). -spec query(stream(), [frame_flag()], query(), [value()], consistency(), [flag()]) -> iolist(). @@ -41,9 +41,9 @@ query(Stream, FrameFlags, Query, Values, ConsistencyLevel, Flags) -> stream = Stream, opcode = ?OP_QUERY, flags = frame_flags(FrameFlags), - body = [<<(marina_types:encode_long_string(Query))/binary, - (marina_types:encode_short(ConsistencyLevel))/binary, Flags2, - Values2/binary>>] + body = [marina_types:encode_long_string(Query), + marina_types:encode_short(ConsistencyLevel), Flags2, + Values2] }). -spec startup([frame_flag()]) -> iolist(). @@ -77,8 +77,7 @@ flags_and_values(Flags, Values) -> Flags2 = flags([{values, true} | Flags]), ValuesCount = length(Values), EncodedValues = [marina_types:encode_bytes(Value) || Value <- Values], - Values2 = <<(marina_types:encode_short(ValuesCount))/binary, - (iolist_to_binary(EncodedValues))/binary>>, + Values2 = [marina_types:encode_short(ValuesCount), EncodedValues], {Flags2, Values2}. frame_flags([]) -> From d2dbdacae244b8de63b0fde650e9222fb862f75e Mon Sep 17 00:00:00 2001 From: Louis-Philippe Gauthier Date: Mon, 6 Apr 2015 20:34:13 -0400 Subject: [PATCH 2/5] Use main erlang-lz4 repository --- rebar.config | 2 +- src/marina_frame.erl | 2 +- src/marina_utils.erl | 15 +++++++++++++++ 3 files changed, 17 insertions(+), 2 deletions(-) diff --git a/rebar.config b/rebar.config index ed58884..fd788f3 100644 --- a/rebar.config +++ b/rebar.config @@ -3,7 +3,7 @@ {deps, [ {edown, ".*", {git, "https://github.com/esl/edown.git", "HEAD"}}, - {lz4, ".*", {git, "https://github.com/lpgauth/erlang-lz4.git", {branch, "marina"}}}, + {lz4, ".*", {git, "https://github.com/szktty/erlang-lz4.git", "0572b0ea2a"}}, {timing, ".*", {git, "https://github.com/lpgauth/timing.git", {branch, "master"}}} ]}. diff --git a/src/marina_frame.erl b/src/marina_frame.erl index 7d9c3d6..f20edf4 100644 --- a/src/marina_frame.erl +++ b/src/marina_frame.erl @@ -70,5 +70,5 @@ decode(Rest, Acc) -> encode_body(0, Body) -> Body; encode_body(1, Body) -> - {ok, Body2} = lz4:pack(iolist_to_binary(Body)), + {ok, Body2} = marina_utils:pack(Body), Body2. diff --git a/src/marina_utils.erl b/src/marina_utils.erl index 89e5656..32130c1 100644 --- a/src/marina_utils.erl +++ b/src/marina_utils.erl @@ -5,7 +5,9 @@ childs_specs/0, child_name/1, info_msg/2, + pack/1, timeout/2, + unpack/1, warning_msg/2 ]). @@ -21,6 +23,16 @@ child_name(N) -> info_msg(Format, Data) -> error_logger:info_msg(Format, Data). +pack(Iolist) when is_list(Iolist) -> + pack(iolist_to_binary(Iolist)); +pack(Binary) -> + case lz4:compress(Binary, []) of + {ok, Compressed} -> + {ok, <<(size(Binary)):32/unsigned-integer, Compressed/binary>>}; + Error -> + Error + end. + timeout(Timestamp, Timeout) -> Diff = timer:now_diff(os:timestamp(), Timestamp) div 1000, case Timeout - Diff of @@ -28,6 +40,9 @@ timeout(Timestamp, Timeout) -> X -> X end. +unpack(<>) -> + lz4:uncompress(Binary, Size). + warning_msg(Format, Data) -> error_logger:warning_msg(Format, Data). From 790e39464b58a6181ccc17f5e4444e72cb4fc295 Mon Sep 17 00:00:00 2001 From: Louis-Philippe Gauthier Date: Mon, 6 Apr 2015 20:37:16 -0400 Subject: [PATCH 3/5] Should use marina_utils --- src/marina_frame.erl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/marina_frame.erl b/src/marina_frame.erl index f20edf4..87709f5 100644 --- a/src/marina_frame.erl +++ b/src/marina_frame.erl @@ -50,7 +50,7 @@ pending_size(_) -> decode_body(0, Body) -> Body; decode_body(1, Body) -> - {ok, Body2} = lz4:unpack(Body), + {ok, Body2} = marina_utils:unpack(Body), Body2. decode(<<1:1, ?PROTO_VERSION:7/unsigned-integer, Flags:8/unsigned-integer, From 8efaf74e12274b01966a1ee04bf38a8f728b4229 Mon Sep 17 00:00:00 2001 From: Louis-Philippe Gauthier Date: Mon, 6 Apr 2015 20:58:35 -0400 Subject: [PATCH 4/5] Add missing specs --- src/marina.erl | 14 +++++++------- src/marina_sup.erl | 2 +- src/marina_utils.erl | 18 ++++++++++++++++-- 3 files changed, 24 insertions(+), 10 deletions(-) diff --git a/src/marina.erl b/src/marina.erl index 4f892cd..da9f985 100644 --- a/src/marina.erl +++ b/src/marina.erl @@ -20,43 +20,43 @@ %% public -spec async_execute(statement_id(), consistency(), [flag()], pid()) -> - {ok, erlang:ref()} | {error, backlog_full}. + {ok, reference()} | {error, backlog_full}. async_execute(StatementId, ConsistencyLevel, Flags, Pid) -> async_execute(StatementId, [], ConsistencyLevel, Flags, Pid). -spec async_execute(statement_id(), [value()], consistency(), [flag()], pid()) -> - {ok, erlang:ref()} | {error, backlog_full}. + {ok, reference()} | {error, backlog_full}. async_execute(StatementId, Values, ConsistencyLevel, Flags, Pid) -> async_call({execute, StatementId, Values, ConsistencyLevel, Flags}, Pid). -spec async_prepare(query(), pid()) -> - {ok, erlang:ref()} | {error, backlog_full}. + {ok, reference()} | {error, backlog_full}. async_prepare(Query, Pid) -> async_call({prepare, Query}, Pid). -spec async_query(query(), consistency(), [flag()], pid()) -> - {ok, erlang:ref()} | {error, backlog_full}. + {ok, reference()} | {error, backlog_full}. async_query(Query, ConsistencyLevel, Flags, Pid) -> async_query(Query, [], ConsistencyLevel, Flags, Pid). -spec async_query(query(), [value()], consistency(), [flag()], pid()) -> - {ok, erlang:ref()} | {error, backlog_full}. + {ok, reference()} | {error, backlog_full}. async_query(Query, Values, ConsistencyLevel, Flags, Pid) -> async_call({query, Query, Values, ConsistencyLevel, Flags}, Pid). -spec async_reusable_query(query(), consistency(), [flag()], pid(), timeout()) -> - {ok, erlang:ref()} | {error, term()}. + {ok, reference()} | {error, term()}. async_reusable_query(Query, ConsistencyLevel, Flags, Pid, Timeout) -> async_reusable_query(Query, [], ConsistencyLevel, Flags, Pid, Timeout). -spec async_reusable_query(query(), [value()], consistency(), [flag()], pid(), timeout()) -> - {ok, erlang:ref()} | {error, term()}. + {ok, reference()} | {error, term()}. async_reusable_query(Query, Values, ConsistencyLevel, Flags, Pid, Timeout) -> case marina_cache:get(Query) of diff --git a/src/marina_sup.erl b/src/marina_sup.erl index 5415c50..0663a3f 100644 --- a/src/marina_sup.erl +++ b/src/marina_sup.erl @@ -23,5 +23,5 @@ init([]) -> marina_queue:init(), {ok, {{one_for_one, 5, 10}, - marina_utils:childs_specs() + marina_utils:child_specs() }}. diff --git a/src/marina_utils.erl b/src/marina_utils.erl index 32130c1..0d683e9 100644 --- a/src/marina_utils.erl +++ b/src/marina_utils.erl @@ -2,7 +2,7 @@ -include("marina.hrl"). -export([ - childs_specs/0, + child_specs/0, child_name/1, info_msg/2, pack/1, @@ -13,16 +13,24 @@ ]). %% public -childs_specs() -> +-spec child_specs() -> [supervisor:child_spec()]. + +child_specs() -> PoolSize = application:get_env(?APP, pool_size, ?DEFAULT_POOL_SIZE), [child_spec(N) || N <- lists:seq(1, PoolSize)]. +-spec child_name(integer()) -> atom(). + child_name(N) -> list_to_atom(?SERVER_BASE_NAME ++ integer_to_list(N)). +-spec info_msg(string(), [term()]) -> ok. + info_msg(Format, Data) -> error_logger:info_msg(Format, Data). +-spec pack(binary() | iolist()) -> {ok, binary()} | {error, term()}. + pack(Iolist) when is_list(Iolist) -> pack(iolist_to_binary(Iolist)); pack(Binary) -> @@ -33,6 +41,8 @@ pack(Binary) -> Error end. +-spec timeout(erlang:timestamp(), non_neg_integer()) -> non_neg_integer(). + timeout(Timestamp, Timeout) -> Diff = timer:now_diff(os:timestamp(), Timestamp) div 1000, case Timeout - Diff of @@ -40,9 +50,13 @@ timeout(Timestamp, Timeout) -> X -> X end. +-spec unpack(binary()) -> {ok, binary()} | {error, term()}. + unpack(<>) -> lz4:uncompress(Binary, Size). +-spec warning_msg(string(), [term()]) -> ok. + warning_msg(Format, Data) -> error_logger:warning_msg(Format, Data). From 9572b9a0b5d2d1c5b1c67e6590cbd49f1abd6fae Mon Sep 17 00:00:00 2001 From: Louis-Philippe Gauthier Date: Mon, 6 Apr 2015 21:00:56 -0400 Subject: [PATCH 5/5] Bump version to 0.2.2 --- src/marina.app.src | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/marina.app.src b/src/marina.app.src index a3597ca..ee3f44e 100644 --- a/src/marina.app.src +++ b/src/marina.app.src @@ -1,6 +1,6 @@ {application, marina, [ {description, "cassandra client"}, - {vsn, "0.2.1"}, + {vsn, "0.2.2"}, {registered, []}, {applications, [ kernel,