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

Dev 0.2 #31

Closed
wants to merge 26 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
75b3ec9
Initial support for native protocol v2
rpt May 22, 2014
8cf57ba
Add v2 authentication request/response messages
rpt May 26, 2014
6ca82ad
Support for query parameters (e.g. paging, serial consistency)
rpt Jun 3, 2014
db3e364
Add support for BATCH request
rpt Sep 2, 2014
bf6b019
Some irrelevant changes
rpt Sep 2, 2014
e94594e
Add support for batch request
rpt Sep 10, 2014
1cc263a
Return name and query when erlcql_client:prepare/3 fails
puzza007 Oct 10, 2014
9c0e1bc
Merge pull request #28 from puzza007/gh-26-prepared-query-errors
rpt Oct 10, 2014
ca5a2eb
Don't test with R14 on Travis
rpt Nov 25, 2014
2026878
Add default_timeout config option
puzza007 Nov 25, 2014
82c21f6
Merge pull request #30 from puzza007/default-timeout-config-option
rpt Nov 25, 2014
8a03629
Only return stream id when the response gets to the right caller
rpt Nov 25, 2014
5076fa4
Only return stream when we get a response on it
puzza007 Nov 26, 2014
193192f
vsn 0.2.1
puzza007 Nov 27, 2014
50c57e1
Merge pull request #1 from Regulators/fix-timeout-bug
AeroNotix Nov 27, 2014
abdd552
Add metrics
AeroNotix Nov 27, 2014
ea20ae3
Lager is the default, always
AeroNotix Nov 27, 2014
3259b50
Tweak Makefile
AeroNotix Nov 27, 2014
41e2cf9
Pin to lager 2.0.3
AeroNotix Nov 27, 2014
7b85979
Use quintana
AeroNotix Dec 1, 2014
600b497
Merge pull request #2 from Regulators/gh-alf-logging/metric-additions
puzza007 Dec 2, 2014
7c92684
Fix bad reference to erlcql_folsom
AeroNotix Dec 5, 2014
70c5624
Merge pull request #3 from Regulators/alf-fix-bad-ref-to-erlcql_folsom
aerosol Dec 5, 2014
2b38f6e
Regulate versions
AeroNotix Dec 5, 2014
7702013
Hibernate after sending a response
AeroNotix Dec 5, 2014
e78acf0
Merge pull request #4 from Regulators/alf-hibernate-after-sending-res…
aerosol Dec 5, 2014
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
Prev Previous commit
Next Next commit
Add support for batch request
Right now it only supports prepared queries.
Bump to 0.2.0-pre1.
  • Loading branch information
rpt committed Sep 10, 2014
commit e94594eb0f2dc7b3459d2db5825e3a7c0fbc0f2d
2 changes: 1 addition & 1 deletion src/erlcql.app.src
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{application, erlcql,
[{description, "Cassandra native protocol CQL client"},
{vsn, "0.1.7"},
{vsn, "0.2.0"},
{registered, []},
{applications, [kernel,
stdlib,
Expand Down
31 changes: 27 additions & 4 deletions src/erlcql_client.erl
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,8 @@ prepare(Pid, QueryString, Name) ->
execute(Pid, QueryId, Values, Consistency) ->
async_call(Pid, {execute, QueryId, Values, Consistency}).

-spec batch(pid(), [{atom(), values()}], proplist()) ->
result() | {error, Reason :: term()}.
batch(Pid, Queries, Params) ->
async_call(Pid, {batch, Queries, Params}).

Expand Down Expand Up @@ -258,7 +260,7 @@ startup({_Ref, {prepare, Query, _}}, _From, State) ->
not_ready(prepare, Query, State);
startup({_Ref, {execute, Query, _, _}}, _From, State) ->
not_ready(execute, Query, State);
startup({_Ref, {batch, _, _, _}}, _From, State) ->
startup({_Ref, {batch, _, _}}, _From, State) ->
not_ready(batch, State);
startup({_Ref, options}, _From, State) ->
not_ready(options, State);
Expand Down Expand Up @@ -328,9 +330,15 @@ ready({Ref, {execute, Name, Values, Consistency}}, {From, _},
{reply, {error, invalid_query_name}, ready, State}
end;
ready({Ref, {batch, Queries, Params}}, {From, _},
#state{version = Version} = State) ->
Batch = erlcql_encode:batch(Version, Queries, Params),
send(Batch, {Ref, From}, State);
#state{prepared_ets = PreparedETS,
version = Version} = State) ->
case expand_prepared(PreparedETS, Queries, []) of
{error, Error} ->
{reply, {error, Error}, ready, State};
Queries2 ->
Batch = erlcql_encode:batch(Version, Queries2, Params),
send(Batch, {Ref, From}, State)
end;
ready({Ref, options}, {From, _}, #state{version = Version} = State) ->
Options = erlcql_encode:options(Version),
send(Options, {Ref, From}, State);
Expand Down Expand Up @@ -385,6 +393,21 @@ code_change(_OldVsn, StateName, State, _Extra) ->
terminate(_Reason, _StateName, #state{socket = Socket}) ->
close_socket(Socket).

-spec expand_prepared(ets(), [{atom(), values()}], [{binary(), values()}]) ->
[{binary(), values()}] | {error, invalid_query_name}.
expand_prepared(_, [], Acc) ->
lists:reverse(Acc);
expand_prepared(ETS, [{Name, Values} | Qs], Acc) when is_atom(Name) ->
case ets:lookup(ETS, Name) of
[{Name, _Query, QueryId, undefined}] ->
expand_prepared(ETS, Qs, [{QueryId, Values} | Acc]);
[{Name, _Query, QueryId, Types}] ->
TypedValues = lists:zip(Types, Values),
expand_prepared(ETS, Qs, [{QueryId, TypedValues} | Acc]);
[] ->
{error, invalid_query_name}
end.

-spec maybe_create_prepared_ets(proplist()) -> ets().
maybe_create_prepared_ets(Opts) ->
New = fun() -> ets:new(?PREPARED_ETS_NAME, ?PREPARED_ETS_OPTS) end,
Expand Down
6 changes: 2 additions & 4 deletions src/erlcql_encode.erl
Original file line number Diff line number Diff line change
Expand Up @@ -132,10 +132,8 @@ batch(2, Queries, Params) when is_list(Queries) ->
{batch, [batch_type(BatchType), short(length(Queries)), Queries2,
short(consistency(Consistency))]}.

batch_query({Query, Values}) when is_atom(Query) ->
[1, short_bytes(Query), erlcql_convert:to_binary(Values)];
batch_query({Query, Values}) ->
[1, long_string(Query), erlcql_convert:to_binary(Values)].
batch_query({QueryId, Values}) ->
[1, short_bytes(QueryId), erlcql_convert:to_binary(Values)].

-spec register(version(), [event_type()]) -> {register, iolist()}.
register(_V, Events) ->
Expand Down
26 changes: 25 additions & 1 deletion test/erlcql_prepare_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
%% Suite ----------------------------------------------------------------------

all() ->
[prepare_execute].
[prepare_execute,
prepare_batch].

%% Fixtures -------------------------------------------------------------------

Expand Down Expand Up @@ -68,3 +69,26 @@ prepare_execute(Config) ->

{[Row], _} = execute(Client, select, [hd(Values)]),
Row = Values.

prepare_batch(Config) ->
Keyspace = ?c(keyspace, Config),
Table = gen_table_name(),
Create = [<<"CREATE TABLE ">>, Keyspace, <<".">>, Table,
<<" (x int PRIMARY KEY, y int)">>],
single_query(Create),

Insert = [<<"INSERT INTO ">>, Table,
<<" (x, y) VALUES (?, ?)">>],
Select = [<<"SELECT * FROM ">>, Table],
Prepare = [{insert, Insert},
{select, Select}],
Opts = [{use, Keyspace},
{prepare, Prepare}],
Client = start_client(Opts),

Points = [[1, 1], [2, 3], [3, 5]],
Queries = [{insert, V} || V <- Points],
batch(Client, Queries),

{Rows, _} = execute(Client, select, []),
Points = lists:sort(Rows).
5 changes: 5 additions & 0 deletions test/erlcql_test.erl
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,11 @@ execute(Pid, Name, Values) ->
{ok, Response} = erlcql_client:execute(Pid, Name, Values, ?CONSISTENCY),
Response.

batch(Pid, Queries) ->
Opts = [{batch_type, logged} | ?CONSISTENCY],
{ok, Response} = erlcql_client:batch(Pid, Queries, Opts),
Response.

-spec stop_client(pid()) -> ok.
stop_client(Pid) ->
true = unlink(Pid),
Expand Down
1 change: 1 addition & 0 deletions test/erlcql_test.hrl
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
single_query/1,
'query'/2,
execute/3,
batch/2,
start_client/1,
stop_client/1]).

Expand Down