forked from WallarooLabs/wally
-
Notifications
You must be signed in to change notification settings - Fork 0
/
machida.pony
1355 lines (1112 loc) · 42.8 KB
/
machida.pony
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
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
Copyright 2017 The Wallaroo Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
implied. See the License for the specific language governing
permissions and limitations under the License.
*/
use "collections"
use "buffered"
use "pony-kafka"
use "net"
use "wallaroo"
use "wallaroo/core/common"
use "wallaroo/core/aggregations"
use "wallaroo/core/partitioning"
use "wallaroo/core/sink"
use "wallaroo/core/sink/connector_sink"
use "wallaroo/core/sink/kafka_sink"
use "wallaroo/core/sink/tcp_sink"
use "wallaroo/core/source"
use "wallaroo/core/source/connector_source"
use "wallaroo/core/source/kafka_source"
use "wallaroo/core/source/gen_source"
use "wallaroo/core/source/tcp_source"
use "wallaroo/core/topology"
use "wallaroo/core/state"
use "wallaroo/core/windows"
// these are included because of wallaroo issue #814
use "serialise"
use "wallaroo_labs/mort"
use "wallaroo_labs/time"
use @set_command_line_args[I32](module: ModuleP, args: Pointer[U8] val)
use @set_user_serialization_fns[None](module: Pointer[U8] tag)
use @user_serialization_get_size[USize](o: Pointer[U8] tag)
use @user_serialization[None](o: Pointer[U8] tag, bs: Pointer[U8] tag)
use @user_deserialization[Pointer[U8] val](bs: Pointer[U8] tag)
use @load_module[ModuleP](module_name: CString)
use @application_setup[Pointer[U8] val](module: ModuleP, args: Pointer[U8] val)
use @list_item_count[USize](list: Pointer[U8] val)
use @instantiate_python_class[Pointer[U8] val](klass: Pointer[U8] val)
use @get_application_setup_item[Pointer[U8] val](list: Pointer[U8] val,
idx: USize)
use @get_application_setup_action[Pointer[U8] val](item: Pointer[U8] val)
use @get_list_item[Pointer[U8] val](list: Pointer[U8] val, idx: USize)
use @get_stage_command[Pointer[U8] val](item: Pointer[U8] val)
use @get_name[Pointer[U8] val](o: Pointer[U8] val)
use @computation_compute[Pointer[U8] val](c: Pointer[U8] val,
d: Pointer[U8] val, method: Pointer[U8] tag)
use @stateful_computation_compute[Pointer[U8] val](c: Pointer[U8] val,
d: Pointer[U8] val, s: Pointer[U8] val, m: Pointer[U8] tag)
use @initial_state[Pointer[U8] val](computation: Pointer[U8] val)
use @initial_accumulator[Pointer[U8] val](aggregation: Pointer[U8] val)
use @aggregation_update[None](aggregation: Pointer[U8] val,
data: Pointer[U8] val, acc: Pointer[U8] val)
use @aggregation_combine[Pointer[U8] val](aggregation: Pointer[U8] val,
acc1: Pointer[U8] val, acc2: Pointer[U8] val)
use @aggregation_output[Pointer[U8] val](aggregation: Pointer[U8] val,
key: Pointer[U8] tag, acc: Pointer[U8] val)
use @source_decoder_header_length[USize](source_decoder: Pointer[U8] val)
use @source_decoder_payload_length[USize](source_decoder: Pointer[U8] val,
data: Pointer[U8] tag, size: USize)
use @source_decoder_decode[Pointer[U8] val](source_decoder: Pointer[U8] val,
data: Pointer[U8] tag, size: USize)
use @source_generator_initial_value[Pointer[U8] val](
source_generator: Pointer[U8] val)
use @source_generator_apply[Pointer[U8] val](source_generator: Pointer[U8] val,
data: Pointer[U8] tag)
use @sink_encoder_encode[Pointer[U8] val](sink_encoder: Pointer[U8] val,
data: Pointer[U8] val)
use @extract_key[Pointer[U8] val](key_extractor: Pointer[U8] val,
data: Pointer[U8] val)
use @key_hash[USize](key: Pointer[U8] val)
use @key_eq[I32](key: Pointer[U8] val, other: Pointer[U8] val)
use @py_bool_check[I32](b: Pointer[U8] box)
use @is_py_none[I32](o: Pointer[U8] box)
use @py_incref[None](o: Pointer[U8] box)
use @py_decref[None](o: Pointer[U8] box)
use @py_list_check[I32](b: Pointer[U8] box)
use @py_tuple_check[I32](p: Pointer[U8] box)
use @py_bytes_check[I32](b: Pointer[U8] tag)
use @py_bytes_or_unicode_size[USize](str: Pointer[U8] tag)
use @py_bytes_or_unicode_as_char[Pointer[U8]](str: Pointer[U8] tag)
use @Py_Initialize[None]()
use @PyErr_Clear[None]()
use @PyErr_Occurred[Pointer[U8]]()
use @PyErr_print[None]()
use @PyTuple_GetItem[Pointer[U8] val](t: Pointer[U8] val, idx: USize)
use @PyBytes_AsStringAndSize[I32](str: Pointer[U8] tag, out: Pointer[Pointer[U8]], size: Pointer[ISize] box)
use @PyBytes_Size[USize](str: Pointer[U8] box)
use @PyUnicode_GetLength[USize](str: Pointer[U8] box)
use @PyUnicode_AsUTF8[Pointer[U8]](str: Pointer[U8] box)
use @PyUnicode_AsUTF8AndSize[Pointer[U8]](str: Pointer[U8] tag, size: Pointer[ISize] box)
use @PyUnicode_FromStringAndSize[Pointer[U8]](str: Pointer[U8] tag, size: USize)
use @PyList_New[Pointer[U8] val](size: USize)
use @PyList_Size[USize](l: Pointer[U8] box)
use @PyList_GetItem[Pointer[U8] val](l: Pointer[U8] box, i: USize)
use @PyList_SetItem[I32](l: Pointer[U8] box, i: USize, item: Pointer[U8] box)
use @PyList_AsTuple[Pointer[U8] val](list: Pointer[U8] tag)
use @PyLong_AsLong[I64](i: Pointer[U8] box)
use @PyObject_HasAttrString[I32](o: Pointer[U8] box, attr: Pointer[U8] tag)
use @PyObject_IsTrue[I32](o: Pointer[U8] box)
type CString is Pointer[U8] tag
type ModuleP is Pointer[U8] val
class val PyData
var _data: Pointer[U8] val
new val create(data: Pointer[U8] val) =>
_data = data
fun obj(): Pointer[U8] val =>
_data
fun _serialise_space(): USize =>
Machida.user_serialization_get_size(_data)
fun _serialise(bytes: Pointer[U8] tag) =>
Machida.user_serialization(_data, bytes)
fun ref _deserialise(bytes: Pointer[U8] tag) =>
_data = recover Machida.user_deserialization(bytes) end
fun _final() =>
Machida.dec_ref(_data)
class PyState is State
var _state: Pointer[U8] val
new create(state: Pointer[U8] val) =>
_state = state
fun obj(): Pointer[U8] val =>
_state
fun _serialise_space(): USize =>
Machida.user_serialization_get_size(_state)
fun _serialise(bytes: Pointer[U8] tag) =>
Machida.user_serialization(_state, bytes)
fun ref _deserialise(bytes: Pointer[U8] tag) =>
_state = recover Machida.user_deserialization(bytes) end
fun _final() =>
Machida.dec_ref(_state)
class val PyKeyExtractor
var _key_extractor: Pointer[U8] val
new val create(key_extractor: Pointer[U8] val) =>
_key_extractor = key_extractor
fun apply(data: PyData val): String =>
recover
let ps = Machida.extract_key(_key_extractor, data.obj())
Machida.print_errors()
if ps.is_null() then
@printf[I32]("Error in key extractor function".cstring())
Fail()
end
let ret = Machida.py_bytes_or_unicode_to_pony_string(ps)
Machida.dec_ref(ps)
ret
end
fun _serialise_space(): USize =>
Machida.user_serialization_get_size(_key_extractor)
fun _serialise(bytes: Pointer[U8] tag) =>
Machida.user_serialization(_key_extractor, bytes)
fun ref _deserialise(bytes: Pointer[U8] tag) =>
_key_extractor = recover Machida.user_deserialization(bytes) end
fun _final() =>
Machida.dec_ref(_key_extractor)
class PySourceHandler is SourceHandler[(PyData val | None)]
var _source_decoder: Pointer[U8] val
new create(source_decoder: Pointer[U8] val) =>
_source_decoder = source_decoder
fun decode(data: Array[U8] val): (PyData val | None) =>
let r = Machida.source_decoder_decode(_source_decoder, data.cpointer(),
data.size())
if not Machida.is_py_none(r) then
PyData(r)
else
None
end
fun _serialise_space(): USize =>
Machida.user_serialization_get_size(_source_decoder)
fun _serialise(bytes: Pointer[U8] tag) =>
Machida.user_serialization(_source_decoder, bytes)
fun ref _deserialise(bytes: Pointer[U8] tag) =>
_source_decoder = recover Machida.user_deserialization(bytes) end
fun _final() =>
Machida.dec_ref(_source_decoder)
class PyFramedSourceHandler is FramedSourceHandler[(PyData val | None)]
var _source_decoder: Pointer[U8] val
let _header_length: USize
new create(source_decoder: Pointer[U8] val) ? =>
_source_decoder = source_decoder
let hl = Machida.framed_source_decoder_header_length(_source_decoder)
if (Machida.err_occurred()) or (hl == 0) then
@printf[U32]("ERROR: _header_length %d is invalid\n".cstring(), hl)
error
else
_header_length = hl
end
fun header_length(): USize =>
_header_length
fun payload_length(data: Array[U8] iso): USize =>
Machida.framed_source_decoder_payload_length(_source_decoder, data.cpointer(),
data.size())
fun decode(data: Array[U8] val): (PyData val | None) =>
let r = Machida.source_decoder_decode(_source_decoder, data.cpointer(),
data.size())
if not Machida.is_py_none(r) then
PyData(r)
else
None
end
fun _serialise_space(): USize =>
Machida.user_serialization_get_size(_source_decoder)
fun _serialise(bytes: Pointer[U8] tag) =>
Machida.user_serialization(_source_decoder, bytes)
fun ref _deserialise(bytes: Pointer[U8] tag) =>
_source_decoder = recover Machida.user_deserialization(bytes) end
fun _final() =>
Machida.dec_ref(_source_decoder)
class val PyGenSourceHandlerBuilder
var _source_generator: Pointer[U8] val
new create(source_generator: Pointer[U8] val) =>
_source_generator = source_generator
fun apply(): PyGenSourceHandler =>
PyGenSourceHandler(_source_generator)
class PyGenSourceHandler is GenSourceGenerator[PyData val]
var _source_generator: Pointer[U8] val
new create(source_generator: Pointer[U8] val) =>
_source_generator = source_generator
fun initial_value(): (PyData val | None) =>
let r = Machida.source_generator_initial_value(_source_generator)
if not Machida.is_py_none(r) then
PyData(r)
else
None
end
fun apply(data: PyData val): (PyData val | None) =>
let r = Machida.source_generator_apply(_source_generator, data.obj())
if not Machida.is_py_none(r) then
PyData(r)
else
None
end
fun _serialise_space(): USize =>
Machida.user_serialization_get_size(_source_generator)
fun _serialise(bytes: Pointer[U8] tag) =>
Machida.user_serialization(_source_generator, bytes)
fun ref _deserialise(bytes: Pointer[U8] tag) =>
_source_generator = recover Machida.user_deserialization(bytes) end
fun _final() =>
Machida.dec_ref(_source_generator)
class val PyComputation is StatelessComputation[PyData val, PyData val]
var _computation: Pointer[U8] val
let _name: String
let _is_multi: Bool
new val create(computation: Pointer[U8] val) =>
_computation = computation
_name = Machida.get_name(_computation)
_is_multi = Machida.implements_compute_multi(_computation)
fun apply(input: PyData val): (PyData val | Array[PyData val] val | None) =>
let r: Pointer[U8] val =
Machida.computation_compute(_computation, input.obj(), _is_multi)
if not Machida.is_py_none(r) then
Machida.process_computation_results(r, _is_multi)
else
None
end
fun name(): String =>
_name
fun _serialise_space(): USize =>
Machida.user_serialization_get_size(_computation)
fun _serialise(bytes: Pointer[U8] tag) =>
Machida.user_serialization(_computation, bytes)
fun ref _deserialise(bytes: Pointer[U8] tag) =>
_computation = recover Machida.user_deserialization(bytes) end
fun _final() =>
Machida.dec_ref(_computation)
class PyStateComputation is StateComputation[PyData val, PyData val, PyState]
var _computation: Pointer[U8] val
let _name: String
let _is_multi: Bool
new create(computation: Pointer[U8] val) =>
_computation = computation
_name = Machida.get_name(_computation)
_is_multi = Machida.implements_compute_multi(_computation)
fun apply(input: PyData val, state: PyState):
(PyData val | Array[PyData val] val | None)
=>
let data =
Machida.stateful_computation_compute(_computation, input.obj(),
state.obj(), _is_multi)
let d = recover if Machida.is_py_none(data) then
Machida.dec_ref(data)
None
else
Machida.process_computation_results(data, _is_multi)
end
end
d
fun name(): String =>
_name
fun initial_state(): PyState =>
Machida.initial_state(_computation)
fun _serialise_space(): USize =>
Machida.user_serialization_get_size(_computation)
fun _serialise(bytes: Pointer[U8] tag) =>
Machida.user_serialization(_computation, bytes)
fun ref _deserialise(bytes: Pointer[U8] tag) =>
_computation = recover Machida.user_deserialization(bytes) end
fun _final() =>
Machida.dec_ref(_computation)
class val PyAggregation is
Aggregation[PyData val, (PyData val | None), PyState]
var _aggregation: Pointer[U8] val
let _name: String
new val create(aggregation: Pointer[U8] val) =>
_aggregation = aggregation
_name = Machida.get_name(_aggregation)
fun initial_accumulator(): PyState =>
Machida.initial_accumulator(_aggregation)
fun update(data: PyData val, acc: PyState) =>
Machida.aggregation_update(_aggregation, data.obj(), acc.obj())
fun combine(acc1: PyState, acc2: PyState): PyState =>
Machida.aggregation_combine(_aggregation, acc1.obj(), acc2.obj())
fun output(key: Key, window_end_ts: U64, acc: PyState): (PyData val | None)
=>
let data =
Machida.aggregation_output(_aggregation, key.cstring(), acc.obj())
recover if Machida.is_py_none(data) then
Machida.dec_ref(data)
None
else
PyData(data)
end
end
fun name(): String =>
_name
fun _serialise_space(): USize =>
Machida.user_serialization_get_size(_aggregation)
fun _serialise(bytes: Pointer[U8] tag) =>
Machida.user_serialization(_aggregation, bytes)
fun ref _deserialise(bytes: Pointer[U8] tag) =>
_aggregation = recover Machida.user_deserialization(bytes) end
fun _final() =>
Machida.dec_ref(_aggregation)
class PyTCPEncoder is TCPSinkEncoder[PyData val]
var _sink_encoder: Pointer[U8] val
new create(sink_encoder: Pointer[U8] val) =>
_sink_encoder = sink_encoder
fun apply(data: PyData val, wb: Writer): Array[ByteSeq] val =>
let byte_buffer = Machida.sink_encoder_encode(_sink_encoder, data.obj())
if not Machida.is_py_none(byte_buffer) then
let byte_string = @py_bytes_or_unicode_as_char(byte_buffer)
if not byte_string.is_null() then
let arr = recover val
// create a temporary Array[U8] wrapper for the C array, then clone it
Machida.py_bytes_or_unicode_to_pony_array(byte_buffer)
end
Machida.dec_ref(byte_buffer)
wb.write(arr)
else
Machida.print_errors()
Fail()
end
end
wb.done()
fun _serialise_space(): USize =>
Machida.user_serialization_get_size(_sink_encoder)
fun _serialise(bytes: Pointer[U8] tag) =>
Machida.user_serialization(_sink_encoder, bytes)
fun ref _deserialise(bytes: Pointer[U8] tag) =>
_sink_encoder = recover Machida.user_deserialization(bytes) end
fun _final() =>
Machida.dec_ref(_sink_encoder)
class PyKafkaEncoder is KafkaSinkEncoder[PyData val]
var _sink_encoder: Pointer[U8] val
new create(sink_encoder: Pointer[U8] val) =>
_sink_encoder = sink_encoder
fun apply(data: PyData val, wb: Writer):
(Array[ByteSeq] val, (Array[ByteSeq] val | None), (None | KafkaPartitionId))
=>
let out_and_key_and_part_id = Machida.sink_encoder_encode(_sink_encoder, data.obj())
// `out_and_key_and_part_id` is a tuple of `(out, key, part_id)`, where `out` is a
// string and key is `None` or a string and `part_id` is `None` or a KafkaPartitionId.
let out_p = @PyTuple_GetItem(out_and_key_and_part_id, 0)
let key_p = @PyTuple_GetItem(out_and_key_and_part_id, 1)
let part_id_p = @PyTuple_GetItem(out_and_key_and_part_id, 2)
let out = wb.>write(recover val
// create a temporary Array[U8] wrapper for the C array, then clone it
Machida.py_bytes_or_unicode_to_pony_array(out_p)
end).done()
let key = if Machida.is_py_none(key_p) then
None
else
wb.>write(recover
Machida.py_bytes_or_unicode_to_pony_array(key_p)
end).done()
end
let part_id = if Machida.is_py_none(part_id_p) then
None
else
@PyLong_AsLong(part_id_p).i32()
end
Machida.dec_ref(out_and_key_and_part_id)
(consume out, consume key, part_id)
fun _serialise_space(): USize =>
Machida.user_serialization_get_size(_sink_encoder)
fun _serialise(bytes: Pointer[U8] tag) =>
Machida.user_serialization(_sink_encoder, bytes)
fun ref _deserialise(bytes: Pointer[U8] tag) =>
_sink_encoder = recover Machida.user_deserialization(bytes) end
fun _final() =>
Machida.dec_ref(_sink_encoder)
class PyConnectorEncoder is ConnectorSinkEncoder[PyData val]
var _sink_encoder: Pointer[U8] val
new create(sink_encoder: Pointer[U8] val) =>
_sink_encoder = sink_encoder
fun apply(data: PyData val, wb: Writer): Array[ByteSeq] val =>
let byte_buffer = Machida.sink_encoder_encode(_sink_encoder, data.obj())
if not Machida.is_py_none(byte_buffer) then
let byte_string = @py_bytes_or_unicode_as_char(byte_buffer)
if not byte_string.is_null() then
let arr = recover val
// create a temporary Array[U8] wrapper for the C array, then clone it
Array[U8].from_cpointer(@py_bytes_or_unicode_as_char(byte_buffer),
@PyBytes_Size(byte_buffer)).clone()
end
Machida.dec_ref(byte_buffer)
wb.write(arr)
else
Machida.print_errors()
Fail()
end
end
wb.done()
fun _serialise_space(): USize =>
Machida.user_serialization_get_size(_sink_encoder)
fun _serialise(bytes: Pointer[U8] tag) =>
Machida.user_serialization(_sink_encoder, bytes)
fun ref _deserialise(bytes: Pointer[U8] tag) =>
_sink_encoder = recover Machida.user_deserialization(bytes) end
fun _final() =>
Machida.dec_ref(_sink_encoder)
primitive Machida
fun print_errors(): Bool =>
if err_occurred() then
@PyErr_Print[None]()
true
else
false
end
fun err_occurred(): Bool =>
let er = @PyErr_Occurred[Pointer[U8]]()
if not er.is_null() then
true
else
false
end
fun start_python() =>
@Py_Initialize()
fun load_module(module_name: String): ModuleP ? =>
let r = @load_module(module_name.cstring())
if print_errors() then
error
end
r
fun application_setup(module: ModuleP, args: Array[String] val):
Pointer[U8] val ?
=>
let pyargs = Machida.pony_array_string_to_py_list_string(args)
let r = @application_setup(module, pyargs)
if print_errors() then
error
end
r
fun apply_application_setup(application_setup_data: Pointer[U8] val,
env: Env): (String, Pipeline[PyData val] val) ?
=>
recover val
let pipeline_tree = PyPipelineTree(application_setup_data, env)
let pipeline = pipeline_tree.build()?
(pipeline_tree.app_name, pipeline)
end
fun framed_source_decoder_header_length(source_decoder: Pointer[U8] val): USize =>
@PyErr_Clear[None]()
let r = @source_decoder_header_length(source_decoder)
print_errors()
if (r >= 1) and (r <= 8) then
r
else
@printf[U32]("ERROR: header_length() method returned invalid size\n".cstring())
4
end
fun framed_source_decoder_payload_length(source_decoder: Pointer[U8] val,
data: Pointer[U8] tag, size: USize): USize
=>
@PyErr_Clear[None]()
let r = @source_decoder_payload_length(source_decoder, data, size)
if err_occurred() then
print_errors()
4
else
r
end
fun source_decoder_decode(source_decoder: Pointer[U8] val,
data: Pointer[U8] tag, size: USize): Pointer[U8] val
=>
let r = @source_decoder_decode(source_decoder, data, size)
print_errors()
if r.is_null() then Fail() end
r
fun source_generator_initial_value(source_decoder: Pointer[U8] val):
Pointer[U8] val
=>
let r = @source_generator_initial_value(source_decoder)
print_errors()
if r.is_null() then Fail() end
r
fun source_generator_apply(source_decoder: Pointer[U8] val,
data: Pointer[U8] val): Pointer[U8] val
=>
let r = @source_generator_apply(source_decoder, data)
print_errors()
if r.is_null() then Fail() end
r
fun sink_encoder_encode(sink_encoder: Pointer[U8] val, data: Pointer[U8] val):
Pointer[U8] val
=>
let r = @sink_encoder_encode(sink_encoder, data)
print_errors()
if r.is_null() then Fail() end
r
fun computation_compute(computation: Pointer[U8] val, data: Pointer[U8] val,
multi: Bool): Pointer[U8] val
=>
let method = if multi then "compute_multi" else "compute" end
let r = @computation_compute(computation, data, method.cstring())
print_errors()
if r.is_null() then Fail() end
r
fun stateful_computation_compute(computation: Pointer[U8] val,
data: Pointer[U8] val, state: Pointer[U8] val, multi: Bool):
Pointer[U8] val
=>
let method = if multi then "compute_multi" else "compute" end
let r =
@stateful_computation_compute(computation, data, state, method.cstring())
print_errors()
if r.is_null() then Fail() end
r
fun initial_state(computation: Pointer[U8] val): PyState =>
PyState(@initial_state(computation))
fun initial_accumulator(aggregation: Pointer[U8] val): PyState =>
PyState(@initial_accumulator(aggregation))
fun aggregation_update(aggregation: Pointer[U8] val, data: Pointer[U8] val,
acc: Pointer[U8] val)
=>
@aggregation_update(aggregation, data, acc)
fun aggregation_combine(aggregation: Pointer[U8] val, acc1: Pointer[U8] val,
acc2: Pointer[U8] val): PyState
=>
PyState(@aggregation_combine(aggregation, acc1, acc2))
fun aggregation_output(aggregation: Pointer[U8] val, key: Pointer[U8] tag,
acc: Pointer[U8] val): Pointer[U8] val
=>
@aggregation_output(aggregation, key, acc)
fun key_hash(key: Pointer[U8] val): USize =>
let r = @key_hash(key)
print_errors()
r.usize()
fun key_eq(key: Pointer[U8] val, other: Pointer[U8] val): Bool =>
let r = not (@key_eq(key, other) == 0)
print_errors()
r
fun extract_key(key_extractor: Pointer[U8] val,
data: Pointer[U8] val): Pointer[U8] val
=>
let r = @extract_key(key_extractor, data)
print_errors()
if r.is_null() then Fail() end
r
fun py_list_to_pony_array_string(py_array: Pointer[U8] val):
Array[String] val
=>
let size = @PyList_Size(py_array)
let arr = recover iso Array[String](size) end
for i in Range(0, size) do
arr.push(Machida.py_bytes_or_unicode_to_pony_string(@PyList_GetItem(py_array, i)))
end
consume arr
fun py_list_to_filtered_pony_array_pydata(py_array: Pointer[U8] val):
(Array[PyData val] val | None)
=>
let size = @PyList_Size(py_array)
let arr = recover iso Array[PyData val](size) end
for i in Range(0, size) do
let obj = @PyList_GetItem(py_array, i)
if not Machida.is_py_none(obj) then
Machida.inc_ref(obj)
arr.push(PyData(obj))
end
end
if arr.size() == 0 then
None
else
arr.compact()
consume arr
end
fun pony_array_string_to_py_list_string(args: Array[String] val):
Pointer[U8] val
=>
let l = @PyList_New(args.size())
for (i, v) in args.pairs() do
@PyList_SetItem(l, i, @PyUnicode_FromStringAndSize(v.cstring(), v.size()))
end
l
fun get_name(o: Pointer[U8] val): String =>
let ps = @get_name(o)
recover
if not ps.is_null() then
let ret = Machida.py_bytes_or_unicode_to_pony_string(ps)
dec_ref(ps)
ret
else
"undefined-name"
end
end
fun py_bytes_or_unicode_to_pony_string(p: Pointer[U8] box): String =>
recover
var ps: Pointer[U8] = Pointer[U8]
var ps_size: ISize = 0
var err: I32 = 0
if @py_bytes_check(p) == 0 then
ps = @PyUnicode_AsUTF8AndSize(p, addressof ps_size)
else
err = @PyBytes_AsStringAndSize(p, addressof ps, addressof ps_size)
end
if (err != 0) or ps.is_null() then
@printf[U32]("unexpected non-string value\n".cstring())
print_errors()
Fail()
String
elseif ps_size < 0 then
String
else
String.copy_cpointer(ps, ps_size.usize())
end
end
fun py_bytes_or_unicode_to_pony_array(p: Pointer[U8] box): Array[U8] iso^ =>
recover
var ps: Pointer[U8] = Pointer[U8]
var ps_size: ISize = 0
var err: I32 = 0
if @py_bytes_check(p) == 0 then
ps = @PyUnicode_AsUTF8AndSize(p, addressof ps_size)
else
err = @PyBytes_AsStringAndSize(p, addressof ps, addressof ps_size)
end
if (err != 0) or ps.is_null() then
@printf[U32]("unexpected non-string value\n".cstring())
print_errors()
Fail()
Array[U8]
elseif ps_size < 0 then
Array[U8]
else
Array[U8].from_cpointer(ps, ps_size.usize()).clone()
end
end
fun set_command_line_args(m: ModuleP, args: Array[String val] val) =>
let l = @PyList_New(args.size())
for (i, v) in args.pairs() do
@PyList_SetItem(l, i, @PyUnicode_FromStringAndSize(v.cstring(), v.size()))
end
let t = @PyList_AsTuple(l)
dec_ref(l)
let result = @set_command_line_args(m, t)
fun set_user_serialization_fns(m: Pointer[U8] val) =>
@set_user_serialization_fns(m)
fun user_serialization_get_size(o: Pointer[U8] tag): USize =>
let r = @user_serialization_get_size(o)
if (print_errors()) then
FatalUserError("Serialization failed")
end
r
fun user_serialization(o: Pointer[U8] tag, bs: Pointer[U8] tag) =>
@user_serialization(o, bs)
if (print_errors()) then
FatalUserError("Serialization failed")
end
fun user_deserialization(bs: Pointer[U8] tag): Pointer[U8] val =>
let r = @user_deserialization(bs)
if (print_errors()) then
FatalUserError("Deserialization failed")
end
r
fun bool_check(b: Pointer[U8] val): Bool =>
not (@py_bool_check(b) == 0)
fun is_py_none(o: Pointer[U8] box): Bool =>
not (@is_py_none(o) == 0)
fun inc_ref(o: Pointer[U8] box) =>
@py_incref(o)
fun dec_ref(o: Pointer[U8] box) =>
@py_decref(o)
fun process_computation_results(data: Pointer[U8] val, multi: Bool):
(PyData val | Array[PyData val] val | None)
=>
if not multi then
PyData(data)
else
if @py_list_check(data) == 1 then
let out = Machida.py_list_to_filtered_pony_array_pydata(data)
Machida.dec_ref(data)
out
else
@printf[U32]("compute_multi must return a list\n".cstring())
Fail()
recover val Array[PyData val] end
end
end
fun implements_compute_multi(o: Pointer[U8] box): Bool =>
implements_method(o, "compute_multi")
fun implements_method(o: Pointer[U8] box, method: String): Bool =>
@PyObject_HasAttrString(o, method.cstring()) == 1
primitive _SourceConfig
fun from_tuple(source_config_tuple: Pointer[U8] val, env: Env):
SourceConfig ?
=>
let type_name = recover val
Machida.py_bytes_or_unicode_to_pony_string(@PyTuple_GetItem(source_config_tuple, 0))
end
let source_name = recover val
Machida.py_bytes_or_unicode_to_pony_string(@PyTuple_GetItem(source_config_tuple, 1))
end
match type_name
| "gen" =>
let gen = recover val
let g = @PyTuple_GetItem(source_config_tuple, 2)
Machida.inc_ref(g)
PyGenSourceHandlerBuilder(g)
end
GenSourceConfig[PyData val](gen)
| "tcp" =>
let host = recover val
Machida.py_bytes_or_unicode_to_pony_string(@PyTuple_GetItem(source_config_tuple, 2))
end
let port = recover val
Machida.py_bytes_or_unicode_to_pony_string(@PyTuple_GetItem(source_config_tuple, 3))
end
let decoder = recover val
let d = @PyTuple_GetItem(source_config_tuple, 4)
Machida.inc_ref(d)
PyFramedSourceHandler(d)?
end
let valid: Bool = recover val
let v = @PyObject_IsTrue(@PyTuple_GetItem(source_config_tuple, 5))
if v == 1 then
true
else
false
end
end
let parallelism = recover val
USize.from[I64](@PyLong_AsLong(@PyTuple_GetItem(source_config_tuple, 6)))
end
let max_size: USize = recover val
USize.from[I64](@PyLong_AsLong(@PyTuple_GetItem(source_config_tuple, 7)))
end
let max_received_count: USize = recover val
USize.from[I64](@PyLong_AsLong(@PyTuple_GetItem(source_config_tuple, 8)))
end
TCPSourceConfig[(PyData val | None)](source_name, decoder, host, port,
valid, parallelism, max_size, max_received_count)
| "kafka-internal" =>
let ksclip = KafkaSourceConfigCLIParser(env.out, source_name)
let ksco = ksclip.parse_options(env.args)?
let decoder = recover val
let d = @PyTuple_GetItem(source_config_tuple, 2)
Machida.inc_ref(d)
PySourceHandler(d)
end
KafkaSourceConfig[(PyData val | None)](source_name, consume ksco,
(env.root as TCPConnectionAuth), decoder)
| "kafka" =>
let ksco = _kafka_config_options(source_config_tuple)
let decoder = recover val
let d = @PyTuple_GetItem(source_config_tuple, 5)
Machida.inc_ref(d)
PySourceHandler(d)
end
KafkaSourceConfig[(PyData val | None)](source_name, consume ksco,
(env.root as TCPConnectionAuth), decoder)
| "source_connector" =>
let host = recover val
Machida.py_bytes_or_unicode_to_pony_string(@PyTuple_GetItem(source_config_tuple, 2))
end
let port = recover val
Machida.py_bytes_or_unicode_to_pony_string(@PyTuple_GetItem(source_config_tuple, 3))
end
let decoder = recover val
let d = @PyTuple_GetItem(source_config_tuple, 5)
Machida.inc_ref(d)
PyFramedSourceHandler(d)?
end