Repository navigation
Expand file tree
/
Copy pathcqerl_batch.erl
More file actions
68 lines (56 loc) · 2.77 KB
/
Copy pathcqerl_batch.erl
File metadata and controls
68 lines (56 loc) · 2.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
-module(cqerl_batch).
-include("cqerl_protocol.hrl").
-export([start_link/3, init/4]).
-export([system_continue/3, system_terminate/4]).
start_link(Call, Inet, Batch=#cql_query_batch{}) ->
proc_lib:start_link(?MODULE, init, [Call, Inet, Batch, self()]).
init(Call={ClientPid, _}, Inet, Batch=#cql_query_batch{queries=Queries0}, Parent) ->
Queries = lists:map(fun
(Query=#cql_query{statement=Statement}) when is_list(Statement) ->
Query#cql_query{statement=list_to_binary(Statement)};
(Query) -> Query
end, Queries0),
QueryStates = lists:zip(
Queries,
lists:map(fun (Query) -> cqerl_cache:lookup(ClientPid, Inet, Query) end, Queries)
),
Debug = sys:debug_options([]),
proc_lib:init_ack(Parent, {ok, self()}),
loop(Call, Inet, Batch#cql_query_batch{queries=QueryStates}, Debug, Parent).
loop(Call, Inet, Batch=#cql_query_batch{queries=QueryStates}, Debug, Parent) ->
case lists:all(fun ({_, queued}) -> false;
(_) -> true end, QueryStates) of
true ->
terminate(Call, Batch);
false ->
receive
{prepared, CachedQuery=#cqerl_cached_query{key={Inet, Statement}}} ->
NewQueries = lists:map(fun
({Query=#cql_query{statement=Statement1}, queued}) when Statement1 == Statement ->
{Query, CachedQuery};
(Other) -> Other
end, Batch#cql_query_batch.queries),
loop(Call, Inet, Batch#cql_query_batch{queries=NewQueries}, Debug, Parent);
{preparation_failed, Reason} ->
cqerl_client:batch_failed(Call, Batch, Reason),
exit({failed, {Reason, Call, Batch}});
{system, From, Request} ->
sys:handle_system_msg(Request, From, Parent, ?MODULE, Debug, {Call, Inet, Batch})
end
end.
terminate(Call, Batch) ->
Queries = lists:map(fun
({#cql_query{statement=Statement, values=Values}, uncached}) ->
#cqerl_query{statement=Statement, kind=normal,
values=cqerl_protocol:encode_query_values(Values)};
({#cql_query{values=Values},
#cqerl_cached_query{query_ref=Ref, params_metadata=Metadata}}) ->
#cqerl_query{statement=Ref, kind=prepared,
values=cqerl_protocol:encode_query_values(Values, Metadata#cqerl_result_metadata.columns)}
end, Batch#cql_query_batch.queries),
cqerl_client:batch_ready(Call, Batch#cql_query_batch{queries=Queries}),
exit(normal).
system_continue(Parent, Debug, {Call, Inet, Batch}) ->
loop(Call, Inet, Batch, Debug, Parent).
system_terminate(Reason, _Parent, _Debug, _State) ->
exit(Reason).