forked from extend/goldrush
-
Notifications
You must be signed in to change notification settings - Fork 46
/
glc_code.erl
646 lines (587 loc) · 25.3 KB
/
glc_code.erl
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
%% @doc Code generation functions.
-module(glc_code).
-compile({nowarn_unused_function, {abstract_module,3}}).
-compile({nowarn_unused_function, {abstract_tables,1}}).
-compile({nowarn_unused_function, {abstract_reset,1}}).
-compile({nowarn_unused_function, {abstract_filter,3}}).
-compile({nowarn_unused_function, {abstract_filter_,4}}).
-compile({nowarn_unused_function, {abstract_opfilter,6}}).
-compile({nowarn_unused_function, {abstract_all,4}}).
-compile({nowarn_unused_function, {abstract_any,4}}).
-compile({nowarn_unused_function, {abstract_with,3}}).
-compile({nowarn_unused_function, {abstract_within,3}}).
-compile({nowarn_unused_function, {abstract_getkey,4}}).
-compile({nowarn_unused_function, {abstract_getkey_,4}}).
-compile({nowarn_unused_function, {abstract_getparam,3}}).
-compile({nowarn_unused_function, {param_variable,1}}).
-compile({nowarn_unused_function, {field_variable,1}}).
-compile({nowarn_unused_function, {field_variable_,1}}).
-compile({nowarn_unused_function, {compile_forms,2}}).
-compile({nowarn_unused_function, {load_binary,2}}).
-export([
compile/3
]).
-define(erl, erl_syntax).
-record(module, {
'query' :: term(),
tables :: [{atom(), atom()}],
qtree :: term(),
store :: term()
}).
-type syntaxTree() :: erl_syntax:syntaxTree().
-record(state, {
event = undefined :: syntaxTree(),
fields = [] :: [{atom(), syntaxTree()}],
fieldc = 0 :: non_neg_integer(),
paramvars = [] :: [{term(), syntaxTree()}],
paramstab = undefined :: atom(),
countstab = undefined :: atom(),
statistics = false :: boolean()
}).
-type nextFun() :: fun((#state{}) -> [syntaxTree()]).
compile(Module, ModuleData, Stats) ->
{ok, forms, Forms} = abstract_module(Module, ModuleData, Stats),
{ok, Module, Binary} = compile_forms(Forms, [nowarn_unused_vars]),
{ok, loaded, Module} = load_binary(Module, Binary),
{ok, Module}.
%% abstract code generation functions
%% @private Generate an abstract dispatch module.
-spec abstract_module(atom(), #module{}, #state{}) -> {ok, forms, list()}.
abstract_module(Module, Data, Stats) ->
Forms = [?erl:revert(E) || E <- abstract_module_(Module, Data, Stats)],
case lists:keyfind(errors, 1, erl_syntax_lib:analyze_forms(Forms)) of
false -> {ok, forms, Forms};
{_, []} -> {ok, forms, Forms};
{_, [_|_]}=Errors -> Errors
end.
%% @private Generate an abstract dispatch module.
-spec abstract_module_(atom(), #module{}, #state{}) -> [?erl:syntaxTree()].
abstract_module_(Module, #module{tables=Tables,
qtree=Tree, store=Store}=Data, Stats) ->
{_, ParamsTable} = lists:keyfind(params, 1, Tables),
{_, CountsTable} = lists:keyfind(counters, 1, Tables),
State = #state{ event=?erl:variable("Event"),
paramstab=ParamsTable,
countstab=CountsTable,
statistics=Stats},
AbstractMod = [
%% -module(Module)
?erl:attribute(?erl:atom(module), [?erl:atom(Module)]),
%% -export([
?erl:attribute(
?erl:atom(export),
[?erl:list([
%% get/1
?erl:arity_qualifier(
?erl:atom(get),
?erl:integer(1)),
%% info/1
?erl:arity_qualifier(
?erl:atom(info),
?erl:integer(1)),
%% reset_counters/1
?erl:arity_qualifier(
?erl:atom(reset_counters),
?erl:integer(1)),
%% table/1
?erl:arity_qualifier(
?erl:atom(table),
?erl:integer(1)),
?erl:arity_qualifier(
?erl:atom(runjob),
?erl:integer(2)),
%% handle/1
?erl:arity_qualifier(
?erl:atom(handle),
?erl:integer(1))])]),
%% ]).
%% get(Name) -> Term.
?erl:function(
?erl:atom(get),
abstract_get(Data) ++
[?erl:clause(
[?erl:underscore()], none,
[?erl:abstract({error, undefined})])]),
%% info(Name) -> Term.
?erl:function(
?erl:atom(info),
abstract_info(Data, State) ++
[?erl:clause(
[?erl:underscore()], none,
[abstract_apply(erlang, error, [?erl:atom(badarg)])])]),
%% reset_counters(Name) -> boolean().
?erl:function(
?erl:atom(reset_counters),
abstract_reset(State) ++
[?erl:clause(
[?erl:underscore()], none,
[abstract_apply(erlang, error, [?erl:atom(badarg)])])]),
%% table(Name) -> atom().
?erl:function(
?erl:atom(table),
abstract_tables(Tables) ++
[?erl:clause(
[?erl:underscore()], none,
[abstract_apply(erlang, error, [?erl:atom(badarg)])])]),
%% handle(Event) - entry function
?erl:function(
?erl:atom(handle),
[?erl:clause([?erl:variable("Event")], none,
[abstract_count(input, State),
?erl:application(none,
?erl:atom(handle_), [?erl:variable("Event")])])]),
?erl:function(
?erl:atom(runjob),
[?erl:clause([?erl:variable("Fun"), ?erl:variable("Event")], none,
[abstract_count(job_input, State),
?erl:application(none,
?erl:atom(job_), [?erl:variable("Fun"),
?erl:variable("Event")])])]),
%% input_(Node, App, Pid, Tags, Values) - filter roots
?erl:function(
?erl:atom(handle_),
[?erl:clause([?erl:variable("Event")], none,
abstract_filter(Tree, Data, State))]),
?erl:function(
?erl:atom(job_),
[?erl:clause([?erl:variable("Fun"),
?erl:variable("Meta")], none,
[?erl:application(none,
?erl:atom(job_result), [
?erl:catch_expr(
abstract_apply(glc_run, execute, [
?erl:variable("Fun"),
?erl:list([?erl:variable("Meta"),
?erl:abstract(Store)])
])),
?erl:variable("Meta")])
]
)]),
?erl:function(
?erl:atom(job_result),
abstract_runjob(Data, State)
)
],
%% Transform Term -> Key to Key -> Term
gr_param:transform(ParamsTable),
AbstractMod.
%% @private Return the clauses of the table/1 function.
abstract_tables(Tables) ->
[?erl:clause(
[?erl:abstract(K)], none,
[?erl:abstract(V)])
|| {K, V} <- Tables].
abstract_query_find(K, Store) ->
case lists:keyfind(K, 1, Store) of
{_, Val} ->
{ok, Val};
_ ->
{error, notfound}
end.
%% @private Return the original query as an expression.
abstract_query({with, Query, _}) ->
[?erl:abstract(Query)];
abstract_query([{with, _Query, _}|_] = I) ->
[?erl:abstract([Query || {with, Query, _} <- I])];
%[?erl:abstract(_Query)];
abstract_query({any, [{with, _Q, _A}|_] = I}) ->
Queries = glc_lib:reduce(glc:any([Q || {with, Q, _} <- I])),
[?erl:abstract(Queries)];
abstract_query({all, [{with, _Q, _A}|_] = I}) ->
Queries = glc_lib:reduce(glc:all([Q || {with, Q, _} <- I])),
[?erl:abstract(Queries)];
abstract_query({ok, {other, Other}}) ->
SpcBin = abstract_apply(erlang, 'list_to_binary', [?erl:abstract(Other)]),
[?erl:tuple([?erl:atom(ok), abstract_apply(erlang, 'binary_to_term', [SpcBin])])];
abstract_query(Query) ->
[?erl:abstract(Query)].
%% @private Return the clauses of the get/1 function.
abstract_get(#module{'query'=_Query, store=undefined}) ->
[];
abstract_get(#module{'query'=_Query, store=Store}) ->
[?erl:clause([?erl:abstract(K)], none,
abstract_query(abstract_query_find(K, Store)))
|| {K, _} <- Store].
%% @private
abstract_runjob(#module{'query'=_Query, store=_Store}, State) ->
Time = abstract_apply(erlang, '/', [?erl:variable("Time"),
?erl:abstract(1000000)]),
[?erl:clause([?erl:variable("JobResult"),
?erl:variable("Meta")], none,
[
?erl:case_expr(?erl:variable("JobResult"),
[
?erl:clause(
[?erl:tuple([?erl:variable("Time"), ?erl:variable("Result")])],
none,
[?erl:case_expr(?erl:variable("Result"),
[
?erl:clause(
[?erl:tuple([?erl:atom(error),?erl:variable("Reason")])],
none,
[abstract_count(input, State), abstract_count(job_error, State),
?erl:application(none, ?erl:atom(handle_),
abstract_job(Time, [?erl:tuple([?erl:atom(error),
?erl:variable("Reason")])])),
abstract_count(job_time, State, ?erl:variable("Time")),
?erl:tuple([?erl:variable("Time"),
?erl:tuple([?erl:atom(error),
?erl:variable("Reason")])])]),
?erl:clause(
[?erl:variable("Result")],
none,
[abstract_count(input, State), abstract_count(job_run, State),
?erl:application(none, ?erl:atom(handle_),
abstract_job(Time)),
abstract_count(job_time, State, ?erl:variable("Time")),
?erl:tuple([?erl:variable("Time"),
?erl:variable("Result")])])
])
])
])
]
)].
abstract_job(Time) ->
abstract_job(Time, []).
abstract_job(Time, Error) ->
Pairs = abstract_apply(gre, pairs, [?erl:variable("Meta")]),
Runtime = ?erl:list([?erl:tuple([?erl:atom(runtime), Time])]),
[abstract_apply(gre, make,
[abstract_apply(erlang, '++', [?erl:list(Error),
abstract_apply(erlang, '++', [Pairs, Runtime])]),
?erl:abstract([list])])].
%% @private Return the clauses of the info/1 function.
abstract_info(#module{'query'=Query}, State) ->
[?erl:clause([?erl:abstract(K)], none, V)
|| {K, V} <- [
{'query', abstract_query(Query)},
{input, abstract_getcount(input, State)},
{filter, abstract_getcount(filter, State)},
{output, abstract_getcount(output, State)},
{job_input, abstract_getcount(job_input, State)},
{job_run, abstract_getcount(job_run, State)},
{job_time, abstract_getcount(job_time, State)},
{job_error, abstract_getcount(job_error, State)}
]].
abstract_reset(#state{statistics=false}) ->
Reset = [?erl:abstract(0)],
[?erl:clause([?erl:abstract(K)], none, V)
|| {K, V} <- [
{all, Reset},
{input, Reset},
{filter, Reset},
{output, Reset},
{job_input, Reset},
{job_run, Reset},
{job_time, Reset},
{job_error, Reset}
]];
abstract_reset(#state{statistics=true}) ->
[?erl:clause([?erl:abstract(K)], none, V)
|| {K, V} <- [
{all, abstract_resetcount([input, filter, output,
job_input, job_run,
job_time, job_error])},
{input, abstract_resetcount(input)},
{filter, abstract_resetcount(filter)},
{output, abstract_resetcount(output)},
{job_input, abstract_resetcount(job_input)},
{job_run, abstract_resetcount(job_run)},
{job_time, abstract_resetcount(job_time)},
{job_error, abstract_resetcount(job_error)}
]].
%% @private Return a list of expressions to apply a filter.
-spec abstract_filter(glc_ops:op() | [glc_ops:op()], #module{}, #state{}) -> [syntaxTree()].
abstract_filter({Type, [{with, _Cond, _Fun}|_] = I}, Data, State) when Type =:= all; Type =:= any ->
Cond = glc_lib:reduce(glc:Type([Q || {with, Q, _} <- I])),
abstract_filter_(Cond,
_OnMatch=fun(State2) ->
Funs = [ F || {with, _, F} <- I ],
[abstract_count(output, State)] ++
abstract_with(Funs, Data, State2) end,
_OnNomatch=fun(_State2) -> [abstract_count(filter, State)] end, State);
abstract_filter([{with, _Cond, _Fun}|_] = I, Data, State) ->
OnNomatch = fun(_State2) -> [abstract_count(filter, State, 0)] end,
Funs = lists:foldr(fun({with, Cond, Fun}, Acc) ->
[{Cond, Fun, Data}|Acc]
end, [], I),
abstract_within(Funs, OnNomatch, State);
abstract_filter({with, Cond, Fun}, Data, State) ->
abstract_filter_(Cond,
_OnMatch=fun(State2) ->
[abstract_count(output, State)] ++
abstract_with(Fun, Data, State2) end,
_OnNomatch=fun(_State2) -> [abstract_count(filter, State)] end, State);
abstract_filter(Cond, _Data, State) ->
abstract_filter_(Cond,
_OnMatch=fun(_State2) -> [abstract_count(output, State)] end,
_OnNomatch=fun(_State2) -> [abstract_count(filter, State)] end, State).
%% @private Return a list of expressions to apply a filter.
%% A filter expects two continuation functions which generates the expressions
%% to apply when the filter matches or fails to match. The state passed to the
%% functions will contain all the variable bindings of previously accessed
%% fields and parameters.
-spec abstract_filter_(glc_ops:op(), nextFun(), nextFun(), #state{}) ->
syntaxTree().
abstract_filter_({null, true}, OnMatch, _OnNomatch, State) ->
OnMatch(State);
abstract_filter_({null, false}, _OnMatch, OnNomatch, State) ->
OnNomatch(State);
abstract_filter_({Key, '*'}, OnMatch, OnNomatch, State) ->
abstract_getkey(Key,
_OnMatch=fun(#state{}=State2) -> OnMatch(State2) end,
_OnNomatch=fun(State2) -> OnNomatch(State2) end, State);
abstract_filter_({Key, '!'}, OnMatch, OnNomatch, State) ->
abstract_getkey(Key,
_OnNomatch=fun(State2) -> OnNomatch(State2) end,
_OnMatch=fun(#state{}=State2) -> OnMatch(State2) end,
State);
abstract_filter_({Key, Op, Value}, OnMatch, OnNomatch, State)
when Op =:= '>'; Op =:= '='; Op =:= '!='; Op =:= '<';
Op =:= '>='; Op =:= '=<'; Op =:= '<=' ->
Op2 = case Op of
'=' -> '=:=';
'!=' -> '=/=';
'<=' -> '=<';
Op -> Op
end,
abstract_opfilter(Key, Op2, Value, OnMatch, OnNomatch, State);
abstract_filter_({'any', Conds}, OnMatch, OnNomatch, State) ->
abstract_any(Conds, OnMatch, OnNomatch, State);
abstract_filter_({'all', Conds}, OnMatch, OnNomatch, State) ->
abstract_all(Conds, OnMatch, OnNomatch, State).
%% @private Return a branch based on a built in operator.
-spec abstract_opfilter(atom(), atom(), term(), nextFun(),
nextFun(), #state{}) -> [syntaxTree()].
abstract_opfilter(Key, Opname, Value, OnMatch, OnNomatch, State) ->
abstract_getkey(Key,
_OnMatch=fun(#state{}=State2) ->
[?erl:case_expr(
abstract_apply(erlang, Opname, [
?erl:variable(field_variable(Key)),
?erl:abstract(Value)
]),
[?erl:clause([?erl:atom(true)], none,
OnMatch(State2)),
?erl:clause([?erl:atom(false)], none,
OnNomatch(State2))])] end,
_OnNomatch=fun(State2) -> OnNomatch(State2) end, State).
%% @private Generate an `all' filter.
%% An `all' filter is evaluated by testing all conditions that must hold. If
%% any of the conditions does not hold the evaluation is short circuted at that
%% point. This means that the `OnNomatch' branch is executed once for each
%% condition. The `OnMatch' branch is only executed once.
-spec abstract_all([glc_ops:op()], nextFun(), nextFun(), #state{}) ->
[syntaxTree()].
abstract_all([H|T], OnMatch, OnNomatch, State) ->
abstract_filter_(H,
_OnMatch=fun(State2) -> abstract_all(T, OnMatch, OnNomatch, State2)
end, OnNomatch, State);
abstract_all([], OnMatch, _OnNomatch, State) ->
OnMatch(State).
%% @private
-spec abstract_any([glc_ops:op()], nextFun(), nextFun(), #state{}) ->
[syntaxTree()].
abstract_any([H|T], OnMatch, OnNomatch, State) ->
abstract_filter_(H, OnMatch,
_OnNomatch=fun(State2) -> abstract_any(T, OnMatch, OnNomatch, State2)
end, State);
abstract_any([], _OnMatch, OnNomatch, State) ->
OnNomatch(State).
%% @private
-spec abstract_with(fun((gre:event()) -> term()),
#module{}, #state{}) -> [syntaxTree()].
abstract_with([Fun0|_] = Funs, Data, State)
when is_function(Fun0, 1); is_function(Fun0, 2) ->
abstract_getparam(Funs, fun(#state{event=Event, paramvars=Params}) ->
lists:map(fun(Fun) ->
{_, Fun2} = lists:keyfind(Fun, 1, Params),
abstract_with_({Fun, Fun2}, Event, Data)
end, Funs)
end, State);
abstract_with(Fun, Data, State) when is_function(Fun, 1); is_function(Fun, 2) ->
abstract_getparam(Fun, fun(#state{event=Event, paramvars=Params}) ->
{_, Fun2} = lists:keyfind(Fun, 1, Params),
[abstract_with_({Fun, Fun2}, Event, Data)]
end, State).
abstract_within([{H, Fun, Data}|T], OnNomatch, State) ->
OnMatch = fun(State2) -> [abstract_count(output, State)] ++
abstract_with(Fun, Data, State2)
++ abstract_within(T, OnNomatch, State2)
end,
abstract_filter_(H, OnMatch,
_OnNomatch=fun(State2) ->
[abstract_count(filter, State)] ++
abstract_within(T, OnNomatch, State2)
end, State);
abstract_within([], OnNomatch, State) ->
OnNomatch(State).
abstract_with_({Fun, Fun2}, Event, #module{store=Store}) ->
?erl:application(none, Fun2,
case Fun of
_ when is_function(Fun, 1) ->
[Event];
_ when is_function(Fun, 2) ->
[Event, ?erl:abstract(Store)]
end).
%% @private Bind the value of a field to a variable.
%% If the value of a field has already been bound to a variable the previous
%% binding is reused over re-accessing the value. The `OnMatch' function is
%% expected to access the variable stored in the state record. The `OnNomatch'
%% function must not attempt to access the variable.
-spec abstract_getkey(atom(), nextFun(), nextFun(), #state{}) ->
[syntaxTree()].
abstract_getkey(Key, OnMatch, OnNomatch, #state{fields=Fields}=State) ->
case lists:keyfind(Key, 1, Fields) of
{Key, _Variable} -> OnMatch(State);
false -> abstract_getkey_(Key, OnMatch, OnNomatch, State)
end.
-spec abstract_getkey_(atom(), nextFun(), nextFun(), #state{}) ->
[syntaxTree()].
abstract_getkey_(Key, OnMatch, OnNomatch, #state{
event=Event, fields=Fields}=State) ->
[?erl:case_expr(
abstract_apply(gre, find, [?erl:atom(Key), Event]),
[?erl:clause([
?erl:tuple([
?erl:atom(true),
?erl:variable(field_variable(Key))])], none,
OnMatch(State#state{
fields=[{Key, ?erl:variable(field_variable(Key))}
|Fields]})),
?erl:clause([
?erl:atom(false)], none,
OnNomatch(State))
]
)].
%% @private Bind the value of a parameter to a variable.
%% During code generation the parameter value is used as the identity of the
%% parameter. At runtime a unique integer is used as the identity.
-spec abstract_getparam(term(), nextFun(), #state{}) -> [syntaxTree()].
abstract_getparam([_|_]=Terms, OnBound, #state{paramvars=_Params, fields=_Fields,
paramstab=_ParamsTable}=State)
when is_list(Terms) ->
{Keys, Bound} = lists:foldl(fun(Term, {Acc0, #state{paramvars=Params,
paramstab=ParamsTable,
statistics=_Stats}=State0}) ->
case lists:keyfind(Term, 1, Params) of
{_, _Variable} ->
{Acc0, State0};
false ->
Key = abstract_getparam_key(Term, ParamsTable),
Lookup = abstract_apply(gr_param, lookup_element,
[abstract_apply(table, [?erl:atom(params)]),
?erl:abstract(Key)]),
Expr = ?erl:match_expr(param_variable(Key), Lookup),
State1 = State0#state{paramvars=[{Term, param_variable(Key)}|Params]},
{[Expr|Acc0], State1}
end
end, {[], State}, Terms),
Keys ++ OnBound(Bound);
abstract_getparam(Term, OnBound, #state{paramvars=Params}=State) ->
case lists:keyfind(Term, 1, Params) of
{_, _Variable} -> OnBound(State);
%% parameter not bound to variable in this scope.
false -> abstract_getparam([Term], OnBound, State)
end.
abstract_getparam_key(Term, ParamsTable) ->
case gr_param:lookup(ParamsTable, Term) of
[{_, Key2}] ->
Key2;
[] ->
Key2 = gr_param:info_size(ParamsTable),
gr_param:insert(ParamsTable, {Term, Key2}),
Key2
end.
%% @private Generate a variable name for the value of a field.
-spec field_variable(atom()) -> string().
field_variable(Key) ->
"Field_" ++ field_variable_(atom_to_list(Key)).
%% @private Escape non-alphanumeric values.
-spec field_variable_(string()) -> string().
field_variable_([H|T]) when H >= $0, H =< $9 ->
[H|field_variable_(T)];
field_variable_([H|T]) when H >= $A, H =< $Z ->
[H|field_variable_(T)];
field_variable_([H|T]) when H >= $a, H =< $z ->
[H|field_variable_(T)];
field_variable_([H|T]) ->
"_" ++ integer_to_list(H, 16) ++ "_" ++ field_variable_(T);
field_variable_([]) ->
[].
%% @private Generate a variable name for the value of a parameter.
-spec param_variable(integer()) -> syntaxTree().
param_variable(Key) ->
?erl:variable("Param_" ++ integer_to_list(Key)).
%% @ private Generate a list of field variable names.
%% Walk the query tree and generate a safe variable name string for each field
%% that is accessed by the conditions in the query. Only allow alpha-numeric.
%%-spec field_variables(glc_ops:op()) -> [{atom(), string()}].
%%field_variables(Query) ->
%% lists:usort(field_variables_(Query)).
%%-spec field_variables(glc_ops:op()) -> [{atom(), string()}].
%%field_variables_({Key, '=', _Term}) ->
%% [{Key, field_variable(Key)}].
%% @private Return an expression to increment a counter.
%% @todo Pass state record. Only Generate code if `statistics' is enabled.
-spec abstract_count(atom(), #state{}) -> syntaxTree().
abstract_count(Counter, State) ->
abstract_count(Counter, State, 1).
abstract_count(_Counter, #state{statistics=false}, Value) when is_integer(Value) ->
?erl:abstract(Value);
abstract_count(_Counter, #state{statistics=false}, Value) ->
?erl:abstract(Value);
abstract_count(Counter, #state{statistics=true}, Value) when is_integer(Value) ->
abstract_apply(gr_counter, update_counter,
[abstract_apply(table, [?erl:atom(counters)]),
?erl:abstract(Counter),
?erl:abstract({2,Value})]);
abstract_count(Counter, #state{statistics=true}, Value) ->
abstract_apply(gr_counter, update_counter,
[abstract_apply(table, [?erl:atom(counters)]),
?erl:abstract(Counter),
?erl:tuple([?erl:abstract(2), Value])]).
%% @private Return an expression to get the value of a counter.
%% @todo Pass state record. Only Generate code if `statistics' is enabled.
-spec abstract_getcount(atom(), #state{}) -> [syntaxTree()].
abstract_getcount(Counter, State) when is_atom(Counter) ->
abstract_getcount(?erl:abstract(Counter), State);
abstract_getcount(_Counter, #state{statistics = false}) -> [?erl:abstract(0)];
abstract_getcount(Counter, #state{statistics = true}) ->
[abstract_apply(gr_counter, lookup_element,
[abstract_apply(table, [?erl:atom(counters)]), Counter])].
%% @private Return an expression to reset a counter.
-spec abstract_resetcount(atom() | [filter | input | output |
job_input | job_run | job_time | job_error ])
-> [syntaxTree()].
abstract_resetcount(Counter) ->
[abstract_apply(gr_counter, reset_counters,
[abstract_apply(table, [?erl:atom(counters)]),
?erl:abstract(Counter)])].
%% abstract code util functions
%% @private Compile an abstract module.
-spec compile_forms(term(), [term()]) -> {ok, atom(), binary()}.
compile_forms(Forms, Opts) ->
case compile:forms(Forms, Opts) of
{ok, Module, Binary} ->
{ok, Module, Binary};
{ok, Module, Binary, _Warnings} ->
{ok, Module, Binary};
Error ->
erlang:error({compile_forms, Error})
end.
%% @private Load a module binary.
-spec load_binary(atom(), binary()) -> {ok, loaded, atom()}.
load_binary(Module, Binary) ->
case code:load_binary(Module, "", Binary) of
{module, Module} -> {ok, loaded, Module};
{error, Reason} -> exit({error_loading_module, Module, Reason})
end.
%% @private Apply an exported function.
-spec abstract_apply(atom(), atom(), [syntaxTree()]) -> syntaxTree().
abstract_apply(Module, Function, Arguments) ->
?erl:application(?erl:atom(Module), ?erl:atom(Function), Arguments).
%% @private Apply a module local function.
-spec abstract_apply(atom(), [syntaxTree()]) -> syntaxTree().
abstract_apply(Function, Arguments) ->
?erl:application(?erl:atom(Function), Arguments).