-
Notifications
You must be signed in to change notification settings - Fork 88
/
jni_bridge.rs
1398 lines (1285 loc) · 49.7 KB
/
jni_bridge.rs
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 2022 The Blaze 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.
pub use datafusion;
pub use jni::{
self,
errors::Result as JniResult,
objects::{JClass, JMethodID, JObject, JStaticMethodID, JValue},
signature::{Primitive, ReturnType},
sys::jvalue,
JNIEnv, JavaVM,
};
use once_cell::sync::OnceCell;
pub use paste::paste;
thread_local! {
pub static THREAD_JNIENV: once_cell::unsync::Lazy<JNIEnv<'static>> =
once_cell::unsync::Lazy::new(|| {
let jvm = &JavaClasses::get().jvm;
let env = jvm
.attach_current_thread_permanently()
.expect("JVM cannot attach current thread");
env.call_static_method_unchecked(
JavaClasses::get().cJniBridge.class,
JavaClasses::get().cJniBridge.method_setContextClassLoader,
JavaClasses::get().cJniBridge.method_setContextClassLoader_ret.clone(),
&[jni::sys::jvalue::from(jni::objects::JValue::from(JavaClasses::get().classloader))]
)
.expect("JVM cannot set ContextClassLoader to current thread");
env
});
}
#[derive(Debug)]
pub struct LocalRef<'a>(pub JObject<'a>);
impl<'a> LocalRef<'a> {
pub fn as_obj(&self) -> JObject<'a> {
self.0
}
}
impl Drop for LocalRef<'_> {
fn drop(&mut self) {
if !self.0.is_null() {
THREAD_JNIENV.with(|env| {
env.delete_local_ref(self.0)
.expect("error deleting local ref")
})
}
}
}
#[macro_export]
macro_rules! jvalues {
($($args:expr,)* $(,)?) => {{
&[$($crate::jni_bridge::JValue::from($args)),*] as &[$crate::jni_bridge::JValue]
}}
}
#[macro_export]
macro_rules! jvalues_sys {
($($args:expr,)* $(,)?) => {{
&[$($crate::jni_bridge::jvalue::from($crate::jni_bridge::JValue::from($args))),*]
as &[$crate::jni_bridge::jvalue]
}}
}
#[macro_export]
macro_rules! jni_map_error_with_env {
($env:expr, $result:expr) => {{
match $result {
Ok(result) => $crate::jni_bridge::datafusion::error::Result::Ok(result),
Err($crate::jni_bridge::jni::errors::Error::JavaException) => {
let ex = $env.exception_occurred().unwrap();
$env.exception_describe().unwrap();
$env.exception_clear().unwrap();
let message_obj = $env
.call_method_unchecked(
ex,
$crate::jni_bridge::JavaClasses::get()
.cJavaThrowable
.method_getMessage,
$crate::jni_bridge::JavaClasses::get()
.cJavaThrowable
.method_getMessage_ret
.clone(),
&[],
)
.unwrap()
.l()
.unwrap();
if !message_obj.is_null() {
let message = $env
.get_string(message_obj.into())
.map(|s| String::from(s))
.unwrap();
Err(
$crate::jni_bridge::datafusion::error::DataFusionError::External(
format!(
"Java exception thrown at {}:{}: {}",
file!(),
line!(),
message
)
.into(),
),
)
} else {
Err(
$crate::jni_bridge::datafusion::error::DataFusionError::External(
format!(
"Java exception thrown at {}:{}: (no message)",
file!(),
line!()
)
.into(),
),
)
}
}
Err(err) => Err(
$crate::jni_bridge::datafusion::error::DataFusionError::External(
format!(
"Unknown JNI error occurred at {}:{}: {:?}",
file!(),
line!(),
err
)
.into(),
),
),
}
}};
}
#[macro_export]
macro_rules! jni_map_error {
($result:expr) => {{
$crate::jni_bridge::THREAD_JNIENV.with(|env| $crate::jni_map_error_with_env!(env, $result))
}};
}
#[macro_export]
macro_rules! jni_new_direct_byte_buffer {
($value:expr) => {{
$crate::jni_bridge::THREAD_JNIENV.with(|env| unsafe {
$crate::jni_map_error_with_env!(
env,
env.new_direct_byte_buffer(
unsafe { $value.get_unchecked(0) as *const u8 as *mut u8 },
$value.len()
)
)
.map(|s| $crate::jni_bridge::LocalRef(s.into()))
})
}};
}
#[macro_export]
macro_rules! jni_new_string {
($value:expr) => {{
$crate::jni_bridge::THREAD_JNIENV
.with(|env| $crate::jni_map_error_with_env!(env, env.new_string($value)))
.map(|s| $crate::jni_bridge::LocalRef(s.into()))
}};
}
#[macro_export]
macro_rules! jni_new_object {
($clsname:ident ($($args:expr),* $(,)?)) => {{
$crate::jni_bridge::THREAD_JNIENV.with(|env| {
log::trace!(
"jni_new_object!({}, {:?})",
stringify!($clsname),
$crate::jvalues!($($args,)*));
$crate::jni_map_error_with_env!(
env,
env.new_object_unchecked(
$crate::jni_bridge::paste! {$crate::jni_bridge::JavaClasses::get().[<c $clsname>].class},
$crate::jni_bridge::paste! {$crate::jni_bridge::JavaClasses::get().[<c $clsname>].ctor},
$crate::jvalues!($($args,)*))
)
.map(|s| $crate::jni_bridge::LocalRef(s.into()))
})
}}
}
#[macro_export]
macro_rules! jni_get_string {
($value:expr) => {{
$crate::jni_bridge::THREAD_JNIENV.with(|env| {
$crate::jni_map_error_with_env!(env, env.get_string($value)).map(|s| String::from(s))
})
}};
}
#[macro_export]
macro_rules! jni_get_object_class {
($value:expr) => {{
$crate::jni_bridge::THREAD_JNIENV.with(|env| {
$crate::jni_map_error_with_env!(env, env.get_object_class($value))
.map(|s| $crate::jni_bridge::LocalRef(s.into()))
})
}};
}
#[macro_export]
macro_rules! jni_call {
($clsname:ident($obj:expr).$method:ident($($args:expr),* $(,)?) -> JObject) => {{
$crate::jni_bridge::THREAD_JNIENV.with(|env| {
jni_call!(env, $clsname($obj).$method($($args,)*))
.and_then(|result| $crate::jni_map_error_with_env!(env, JObject::try_from(result)))
.map(|s| $crate::jni_bridge::LocalRef(s.into()))
})
}};
($clsname:ident($obj:expr).$method:ident($($args:expr),* $(,)?) -> bool) => {{
jni_call!($clsname($obj).$method($($args),*) -> jni::sys::jboolean)
.map(|v| v == jni::sys::JNI_TRUE)
}};
($clsname:ident($obj:expr).$method:ident($($args:expr),* $(,)?) -> $ret:ty) => {{
$crate::jni_bridge::THREAD_JNIENV.with(|env| {
jni_call!(env, $clsname($obj).$method($($args,)*))
.and_then(|result| $crate::jni_map_error_with_env!(env, <$ret>::try_from(result)))
})
}};
($env:expr, $clsname:ident($obj:expr).$method:ident($($args:expr),* $(,)?)) => {{
log::trace!("jni_call!: {}({:?}).{}({:?})",
stringify!($clsname),
$obj,
stringify!($method),
$crate::jvalues!($($args,)*));
$crate::jni_map_error_with_env!(
$env,
$env.call_method_unchecked(
$obj,
$crate::jni_bridge::paste! {$crate::jni_bridge::JavaClasses::get().[<c $clsname>].[<method_ $method>]},
$crate::jni_bridge::paste! {$crate::jni_bridge::JavaClasses::get().[<c $clsname>].[<method_ $method _ret>]}.clone(),
$crate::jvalues_sys!($($args,)*)
)
)
}}
}
#[macro_export]
macro_rules! jni_call_static {
($clsname:ident.$method:ident($($args:expr),* $(,)?) -> JObject) => {{
$crate::jni_bridge::THREAD_JNIENV.with(|env| {
jni_call_static!(env, $clsname.$method($($args,)*))
.and_then(|result| $crate::jni_map_error_with_env!(env, $crate::jni_bridge::JObject::try_from(result)))
.map(|s| $crate::jni_bridge::LocalRef(s.into()))
})
}};
($clsname:ident.$method:ident($($args:expr),* $(,)?) -> bool) => {{
jni_call_static!($clsname.$method($($args),*) -> jni::sys::jboolean)
.map(|r| r == jni::sys::JNI_TRUE)
}};
($clsname:ident.$method:ident($($args:expr),* $(,)?) -> $ret:ty) => {{
$crate::jni_bridge::THREAD_JNIENV.with(|env| {
jni_call_static!(env, $clsname.$method($($args,)*))
.and_then(|result| $crate::jni_map_error_with_env!(env, <$ret>::try_from(result)))
})
}};
($env:expr, $clsname:ident.$method:ident($($args:expr),* $(,)?)) => {{
log::trace!("jni_call_static!: {}.{}({:?})",
stringify!($clsname),
stringify!($method),
$crate::jvalues!($($args,)*));
$crate::jni_map_error_with_env!(
$env,
$env.call_static_method_unchecked(
$crate::jni_bridge::paste! {$crate::jni_bridge::JavaClasses::get().[<c $clsname>].class},
$crate::jni_bridge::paste! {$crate::jni_bridge::JavaClasses::get().[<c $clsname>].[<method_ $method>]},
$crate::jni_bridge::paste! {$crate::jni_bridge::JavaClasses::get().[<c $clsname>].[<method_ $method _ret>]}.clone(),
$crate::jvalues_sys!($($args,)*)
)
)
}}
}
#[macro_export]
macro_rules! jni_convert_byte_array {
($value:expr) => {{
$crate::jni_bridge::THREAD_JNIENV
.with(|env| $crate::jni_map_error_with_env!(env, env.convert_byte_array(*$value)))
}};
}
#[macro_export]
macro_rules! jni_new_global_ref {
($obj:expr) => {{
$crate::jni_bridge::THREAD_JNIENV
.with(|env| $crate::jni_map_error_with_env!(env, env.new_global_ref($obj)))
}};
}
#[macro_export]
macro_rules! jni_new_local_ref {
($obj:expr) => {{
$crate::jni_bridge::THREAD_JNIENV
.with(|env| $crate::jni_map_error_with_env!(env, env.new_local_ref($value)))
}};
}
#[macro_export]
macro_rules! jni_exception_check {
() => {{
$crate::jni_bridge::THREAD_JNIENV
.with(|env| $crate::jni_map_error_with_env!(env, env.exception_check()))
}};
}
#[macro_export]
macro_rules! jni_exception_occurred {
() => {{
$crate::jni_bridge::THREAD_JNIENV
.with(|env| $crate::jni_map_error_with_env!(env, env.exception_occurred()))
.map(|s| $crate::jni_bridge::LocalRef(s.into()))
}};
}
#[macro_export]
macro_rules! jni_exception_describe {
() => {{
$crate::jni_bridge::THREAD_JNIENV
.with(|env| $crate::jni_map_error_with_env!(env, env.exception_describe()))
}};
}
#[macro_export]
macro_rules! jni_exception_clear {
() => {{
$crate::jni_bridge::THREAD_JNIENV
.with(|env| $crate::jni_map_error_with_env!(env, env.exception_clear()))
}};
}
#[macro_export]
macro_rules! jni_throw {
($value:expr) => {{
$crate::jni_bridge::THREAD_JNIENV
.with(|env| $crate::jni_map_error_with_env!(env, env.throw($value)))
}};
}
#[macro_export]
macro_rules! jni_fatal_error {
($($arg:tt)*) => {{
$crate::jni_bridge::THREAD_JNIENV.with(|env| env.fatal_error(format!($($arg)*)))
}};
}
#[allow(non_snake_case)]
pub struct JavaClasses<'a> {
pub jvm: JavaVM,
pub classloader: JObject<'a>,
pub cJniBridge: JniBridge<'a>,
pub cJniUtil: JniUtil<'a>,
pub cClass: JavaClass<'a>,
pub cJavaThrowable: JavaThrowable<'a>,
pub cJavaRuntimeException: JavaRuntimeException<'a>,
pub cJavaChannels: JavaChannels<'a>,
pub cJavaReadableByteChannel: JavaReadableByteChannel<'a>,
pub cJavaBoolean: JavaBoolean<'a>,
pub cJavaAutoCloseable: JavaAutoCloseable<'a>,
pub cJavaLong: JavaLong<'a>,
pub cJavaList: JavaList<'a>,
pub cJavaMap: JavaMap<'a>,
pub cJavaFile: JavaFile<'a>,
pub cJavaURI: JavaURI<'a>,
pub cJavaBuffer: JavaBuffer<'a>,
pub cScalaIterator: ScalaIterator<'a>,
pub cScalaTuple2: ScalaTuple2<'a>,
pub cScalaFunction0: ScalaFunction0<'a>,
pub cScalaFunction1: ScalaFunction1<'a>,
pub cScalaFunction2: ScalaFunction2<'a>,
pub cHadoopFileSystem: HadoopFileSystem<'a>,
pub cHadoopPath: HadoopPath<'a>,
pub cHadoopFSDataInputStream: HadoopFSDataInputStream<'a>,
pub cSparkFileSegment: SparkFileSegment<'a>,
pub cSparkSQLMetric: SparkSQLMetric<'a>,
pub cSparkMetricNode: SparkMetricNode<'a>,
pub cSparkUDFWrapperContext: SparkUDFWrapperContext<'a>,
pub cSparkUDTFWrapperContext: SparkUDTFWrapperContext<'a>,
pub cBlazeConf: BlazeConf<'a>,
pub cBlazeRssPartitionWriterBase: BlazeRssPartitionWriterBase<'a>,
pub cBlazeCallNativeWrapper: BlazeCallNativeWrapper<'a>,
pub cBlazeOnHeapSpillManager: BlazeOnHeapSpillManager<'a>,
pub cBlazeNativeParquetSinkUtils: BlazeNativeParquetSinkUtils<'a>,
}
#[allow(clippy::non_send_fields_in_send_ty)]
unsafe impl<'a> Send for JavaClasses<'a> {}
unsafe impl<'a> Sync for JavaClasses<'a> {}
static JNI_JAVA_CLASSES: OnceCell<JavaClasses> = OnceCell::new();
impl JavaClasses<'static> {
pub fn init(env: &JNIEnv) {
JNI_JAVA_CLASSES.get_or_init(|| {
log::info!("Initializing JavaClasses...");
let env = unsafe { std::mem::transmute::<_, &'static JNIEnv>(env) };
let jni_bridge = JniBridge::new(env).unwrap();
let classloader = env
.call_static_method_unchecked(
jni_bridge.class,
jni_bridge.method_getContextClassLoader,
jni_bridge.method_getContextClassLoader_ret.clone(),
&[],
)
.unwrap()
.l()
.unwrap();
let java_classes = JavaClasses {
jvm: env.get_java_vm().unwrap(),
classloader: get_global_ref_jobject(env, classloader).unwrap(),
cJniBridge: jni_bridge,
cJniUtil: JniUtil::new(env).unwrap(),
cClass: JavaClass::new(env).unwrap(),
cJavaThrowable: JavaThrowable::new(env).unwrap(),
cJavaRuntimeException: JavaRuntimeException::new(env).unwrap(),
cJavaChannels: JavaChannels::new(env).unwrap(),
cJavaReadableByteChannel: JavaReadableByteChannel::new(env).unwrap(),
cJavaBoolean: JavaBoolean::new(env).unwrap(),
cJavaLong: JavaLong::new(env).unwrap(),
cJavaAutoCloseable: JavaAutoCloseable::new(env).unwrap(),
cJavaList: JavaList::new(env).unwrap(),
cJavaMap: JavaMap::new(env).unwrap(),
cJavaFile: JavaFile::new(env).unwrap(),
cJavaURI: JavaURI::new(env).unwrap(),
cJavaBuffer: JavaBuffer::new(env).unwrap(),
cScalaIterator: ScalaIterator::new(env).unwrap(),
cScalaTuple2: ScalaTuple2::new(env).unwrap(),
cScalaFunction0: ScalaFunction0::new(env).unwrap(),
cScalaFunction1: ScalaFunction1::new(env).unwrap(),
cScalaFunction2: ScalaFunction2::new(env).unwrap(),
cHadoopFileSystem: HadoopFileSystem::new(env).unwrap(),
cHadoopPath: HadoopPath::new(env).unwrap(),
cHadoopFSDataInputStream: HadoopFSDataInputStream::new(env).unwrap(),
cSparkFileSegment: SparkFileSegment::new(env).unwrap(),
cSparkSQLMetric: SparkSQLMetric::new(env).unwrap(),
cSparkMetricNode: SparkMetricNode::new(env).unwrap(),
cSparkUDFWrapperContext: SparkUDFWrapperContext::new(env).unwrap(),
cSparkUDTFWrapperContext: SparkUDTFWrapperContext::new(env).unwrap(),
cBlazeConf: BlazeConf::new(env).unwrap(),
cBlazeRssPartitionWriterBase: BlazeRssPartitionWriterBase::new(env).unwrap(),
cBlazeCallNativeWrapper: BlazeCallNativeWrapper::new(env).unwrap(),
cBlazeOnHeapSpillManager: BlazeOnHeapSpillManager::new(env).unwrap(),
cBlazeNativeParquetSinkUtils: BlazeNativeParquetSinkUtils::new(env).unwrap(),
};
log::info!("Initializing JavaClasses finished");
java_classes
});
}
pub fn inited() -> bool {
JNI_JAVA_CLASSES.get().is_some()
}
pub fn get() -> &'static JavaClasses<'static> {
unsafe {
// safety: JNI_JAVA_CLASSES must be initialized frist
JNI_JAVA_CLASSES.get_unchecked()
}
}
}
#[allow(non_snake_case)]
pub struct JniBridge<'a> {
pub class: JClass<'a>,
pub method_getContextClassLoader: JStaticMethodID,
pub method_getContextClassLoader_ret: ReturnType,
pub method_setContextClassLoader: JStaticMethodID,
pub method_setContextClassLoader_ret: ReturnType,
pub method_getResource: JStaticMethodID,
pub method_getResource_ret: ReturnType,
pub method_setTaskContext: JStaticMethodID,
pub method_setTaskContext_ret: ReturnType,
pub method_getTaskContext: JStaticMethodID,
pub method_getTaskContext_ret: ReturnType,
pub method_getTaskOnHeapSpillManager: JStaticMethodID,
pub method_getTaskOnHeapSpillManager_ret: ReturnType,
pub method_isTaskRunning: JStaticMethodID,
pub method_isTaskRunning_ret: ReturnType,
pub method_isDriverSide: JStaticMethodID,
pub method_isDriverSide_ret: ReturnType,
pub method_getDirectMemoryUsed: JStaticMethodID,
pub method_getDirectMemoryUsed_ret: ReturnType,
pub method_getDirectWriteSpillToDiskFile: JStaticMethodID,
pub method_getDirectWriteSpillToDiskFile_ret: ReturnType,
}
impl<'a> JniBridge<'a> {
pub const SIG_TYPE: &'static str = "org/apache/spark/sql/blaze/JniBridge";
pub fn new(env: &JNIEnv<'a>) -> JniResult<JniBridge<'a>> {
let class = get_global_jclass(env, Self::SIG_TYPE)?;
Ok(JniBridge {
class,
method_getContextClassLoader: env.get_static_method_id(
class,
"getContextClassLoader",
"()Ljava/lang/ClassLoader;",
)?,
method_getContextClassLoader_ret: ReturnType::Object,
method_setContextClassLoader: env.get_static_method_id(
class,
"setContextClassLoader",
"(Ljava/lang/ClassLoader;)V",
)?,
method_setContextClassLoader_ret: ReturnType::Primitive(Primitive::Void),
method_getResource: env.get_static_method_id(
class,
"getResource",
"(Ljava/lang/String;)Ljava/lang/Object;",
)?,
method_getResource_ret: ReturnType::Object,
method_getTaskContext: env.get_static_method_id(
class,
"getTaskContext",
"()Lorg/apache/spark/TaskContext;",
)?,
method_getTaskContext_ret: ReturnType::Object,
method_setTaskContext: env.get_static_method_id(
class,
"setTaskContext",
"(Lorg/apache/spark/TaskContext;)V",
)?,
method_setTaskContext_ret: ReturnType::Primitive(Primitive::Void),
method_getTaskOnHeapSpillManager: env.get_static_method_id(
class,
"getTaskOnHeapSpillManager",
"()Lorg/apache/spark/sql/blaze/memory/OnHeapSpillManager;",
)?,
method_getTaskOnHeapSpillManager_ret: ReturnType::Object,
method_isTaskRunning: env.get_static_method_id(class, "isTaskRunning", "()Z")?,
method_isTaskRunning_ret: ReturnType::Primitive(Primitive::Boolean),
method_isDriverSide: env.get_static_method_id(class, "isDriverSide", "()Z")?,
method_isDriverSide_ret: ReturnType::Primitive(Primitive::Boolean),
method_getDirectMemoryUsed: env.get_static_method_id(
class,
"getDirectMemoryUsed",
"()J",
)?,
method_getDirectMemoryUsed_ret: ReturnType::Primitive(Primitive::Long),
method_getDirectWriteSpillToDiskFile: env
.get_static_method_id(
class,
"getDirectWriteSpillToDiskFile",
"()Ljava/lang/String;",
)
.unwrap(),
method_getDirectWriteSpillToDiskFile_ret: ReturnType::Object,
})
}
}
#[allow(non_snake_case)]
pub struct JniUtil<'a> {
pub class: JClass<'a>,
pub method_readFullyFromFSDataInputStream: JStaticMethodID,
pub method_readFullyFromFSDataInputStream_ret: ReturnType,
pub method_writeFullyToFSDataOutputStream: JStaticMethodID,
pub method_writeFullyToFSDataOutputStream_ret: ReturnType,
}
impl<'a> JniUtil<'a> {
pub const SIG_TYPE: &'static str = "org/apache/spark/sql/blaze/JniUtil";
pub fn new(env: &JNIEnv<'a>) -> JniResult<JniUtil<'a>> {
let class = get_global_jclass(env, Self::SIG_TYPE)?;
Ok(JniUtil {
class,
method_readFullyFromFSDataInputStream: env.get_static_method_id(
class,
"readFullyFromFSDataInputStream",
"(Lorg/apache/hadoop/fs/FSDataInputStream;JLjava/nio/ByteBuffer;)V",
)?,
method_readFullyFromFSDataInputStream_ret: ReturnType::Primitive(Primitive::Void),
method_writeFullyToFSDataOutputStream: env.get_static_method_id(
class,
"writeFullyToFSDataOutputStream",
"(Lorg/apache/hadoop/fs/FSDataOutputStream;Ljava/nio/ByteBuffer;)V",
)?,
method_writeFullyToFSDataOutputStream_ret: ReturnType::Primitive(Primitive::Void),
})
}
}
#[allow(non_snake_case)]
pub struct JavaClass<'a> {
pub class: JClass<'a>,
pub method_getName: JMethodID,
pub method_getName_ret: ReturnType,
}
impl<'a> JavaClass<'a> {
pub const SIG_TYPE: &'static str = "java/lang/Class";
pub fn new(env: &JNIEnv<'a>) -> JniResult<JavaClass<'a>> {
let class = get_global_jclass(env, Self::SIG_TYPE)?;
Ok(JavaClass {
class,
method_getName: env.get_method_id(class, "getName", "()Ljava/lang/String;")?,
method_getName_ret: ReturnType::Object,
})
}
}
#[allow(non_snake_case)]
pub struct JavaThrowable<'a> {
pub class: JClass<'a>,
pub method_getMessage: JMethodID,
pub method_getMessage_ret: ReturnType,
}
impl<'a> JavaThrowable<'a> {
pub const SIG_TYPE: &'static str = "java/lang/Throwable";
pub fn new(env: &JNIEnv<'a>) -> JniResult<JavaThrowable<'a>> {
let class = get_global_jclass(env, Self::SIG_TYPE)?;
Ok(JavaThrowable {
class,
method_getMessage: env.get_method_id(class, "getMessage", "()Ljava/lang/String;")?,
method_getMessage_ret: ReturnType::Object,
})
}
}
#[allow(non_snake_case)]
pub struct JavaRuntimeException<'a> {
pub class: JClass<'a>,
pub ctor: JMethodID,
}
impl<'a> JavaRuntimeException<'a> {
pub const SIG_TYPE: &'static str = "java/lang/RuntimeException";
pub fn new(env: &JNIEnv<'a>) -> JniResult<JavaRuntimeException<'a>> {
let class = get_global_jclass(env, Self::SIG_TYPE)?;
Ok(JavaRuntimeException {
class,
ctor: env.get_method_id(
class,
"<init>",
"(Ljava/lang/String;Ljava/lang/Throwable;)V",
)?,
})
}
}
#[allow(non_snake_case)]
pub struct JavaChannels<'a> {
pub class: JClass<'a>,
pub method_newChannel: JStaticMethodID,
pub method_newChannel_ret: ReturnType,
}
impl<'a> JavaChannels<'a> {
pub const SIG_TYPE: &'static str = "java/nio/channels/Channels";
pub fn new(env: &JNIEnv<'a>) -> JniResult<JavaChannels<'a>> {
let class = get_global_jclass(env, Self::SIG_TYPE)?;
Ok(JavaChannels {
class,
method_newChannel: env.get_static_method_id(
class,
"newChannel",
"(Ljava/io/InputStream;)Ljava/nio/channels/ReadableByteChannel;",
)?,
method_newChannel_ret: ReturnType::Object,
})
}
}
#[allow(non_snake_case)]
pub struct JavaReadableByteChannel<'a> {
pub class: JClass<'a>,
pub method_read: JMethodID,
pub method_read_ret: ReturnType,
pub method_close: JMethodID,
pub method_close_ret: ReturnType,
}
impl<'a> JavaReadableByteChannel<'a> {
pub const SIG_TYPE: &'static str = "java/nio/channels/ReadableByteChannel";
pub fn new(env: &JNIEnv<'a>) -> JniResult<JavaReadableByteChannel<'a>> {
let class = get_global_jclass(env, Self::SIG_TYPE)?;
Ok(JavaReadableByteChannel {
class,
method_read: env.get_method_id(class, "read", "(Ljava/nio/ByteBuffer;)I")?,
method_read_ret: ReturnType::Primitive(Primitive::Int),
method_close: env.get_method_id(class, "close", "()V")?,
method_close_ret: ReturnType::Primitive(Primitive::Void),
})
}
}
#[allow(non_snake_case)]
pub struct JavaBoolean<'a> {
pub class: JClass<'a>,
pub ctor: JMethodID,
}
impl<'a> JavaBoolean<'a> {
pub const SIG_TYPE: &'static str = "java/lang/Boolean";
pub fn new(env: &JNIEnv<'a>) -> JniResult<JavaBoolean<'a>> {
let class = get_global_jclass(env, Self::SIG_TYPE)?;
Ok(JavaBoolean {
class,
ctor: env.get_method_id(class, "<init>", "(Z)V")?,
})
}
}
#[allow(non_snake_case)]
pub struct JavaLong<'a> {
pub class: JClass<'a>,
pub ctor: JMethodID,
pub method_longValue: JMethodID,
pub method_longValue_ret: ReturnType,
}
impl<'a> JavaLong<'a> {
pub const SIG_TYPE: &'static str = "java/lang/Long";
pub fn new(env: &JNIEnv<'a>) -> JniResult<JavaLong<'a>> {
let class = get_global_jclass(env, Self::SIG_TYPE)?;
Ok(JavaLong {
class,
ctor: env.get_method_id(class, "<init>", "(J)V")?,
method_longValue: env.get_method_id(class, "longValue", "()J")?,
method_longValue_ret: ReturnType::Primitive(Primitive::Long),
})
}
}
#[allow(non_snake_case)]
pub struct JavaAutoCloseable<'a> {
pub class: JClass<'a>,
pub method_close: JMethodID,
pub method_close_ret: ReturnType,
}
impl<'a> JavaAutoCloseable<'a> {
pub const SIG_TYPE: &'static str = "java/lang/AutoCloseable";
pub fn new(env: &JNIEnv<'a>) -> JniResult<JavaAutoCloseable<'a>> {
let class = get_global_jclass(env, Self::SIG_TYPE)?;
Ok(JavaAutoCloseable {
class,
method_close: env.get_method_id(class, "close", "()V")?,
method_close_ret: ReturnType::Primitive(Primitive::Void),
})
}
}
#[allow(non_snake_case)]
pub struct JavaList<'a> {
pub class: JClass<'a>,
pub method_size: JMethodID,
pub method_size_ret: ReturnType,
pub method_get: JMethodID,
pub method_get_ret: ReturnType,
}
impl<'a> JavaList<'a> {
pub const SIG_TYPE: &'static str = "java/util/List";
pub fn new(env: &JNIEnv<'a>) -> JniResult<JavaList<'a>> {
let class = get_global_jclass(env, Self::SIG_TYPE)?;
Ok(JavaList {
class,
method_size: env.get_method_id(class, "size", "()I")?,
method_size_ret: ReturnType::Primitive(Primitive::Int),
method_get: env.get_method_id(class, "get", "(I)Ljava/lang/Object;")?,
method_get_ret: ReturnType::Object,
})
}
}
#[allow(non_snake_case)]
pub struct JavaMap<'a> {
pub class: JClass<'a>,
pub method_get: JMethodID,
pub method_get_ret: ReturnType,
pub method_put: JMethodID,
pub method_put_ret: ReturnType,
}
impl<'a> JavaMap<'a> {
pub const SIG_TYPE: &'static str = "java/util/Map";
pub fn new(env: &JNIEnv<'a>) -> JniResult<JavaMap<'a>> {
let class = get_global_jclass(env, Self::SIG_TYPE)?;
Ok(JavaMap {
class,
method_get: env
.get_method_id(class, "get", "(Ljava/lang/Object;)Ljava/lang/Object;")
.unwrap(),
method_get_ret: ReturnType::Object,
method_put: env
.get_method_id(
class,
"put",
"(Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;",
)
.unwrap(),
method_put_ret: ReturnType::Primitive(Primitive::Void),
})
}
}
#[allow(non_snake_case)]
pub struct JavaFile<'a> {
pub class: JClass<'a>,
pub method_getPath: JMethodID,
pub method_getPath_ret: ReturnType,
}
impl<'a> JavaFile<'a> {
pub const SIG_TYPE: &'static str = "java/io/File";
pub fn new(env: &JNIEnv<'a>) -> JniResult<JavaFile<'a>> {
let class = get_global_jclass(env, Self::SIG_TYPE)?;
Ok(JavaFile {
class,
method_getPath: env.get_method_id(class, "getPath", "()Ljava/lang/String;")?,
method_getPath_ret: ReturnType::Object,
})
}
}
#[allow(non_snake_case)]
pub struct JavaURI<'a> {
pub class: JClass<'a>,
pub ctor: JMethodID,
}
impl<'a> JavaURI<'a> {
pub const SIG_TYPE: &'static str = "java/net/URI";
pub fn new(env: &JNIEnv<'a>) -> JniResult<JavaURI<'a>> {
let class = get_global_jclass(env, Self::SIG_TYPE)?;
Ok(JavaURI {
class,
ctor: env.get_method_id(class, "<init>", "(Ljava/lang/String;)V")?,
})
}
}
#[allow(non_snake_case)]
pub struct JavaBuffer<'a> {
pub class: JClass<'a>,
pub method_hasRemaining: JMethodID,
pub method_hasRemaining_ret: ReturnType,
pub method_position: JMethodID,
pub method_position_ret: ReturnType,
}
impl<'a> JavaBuffer<'a> {
pub const SIG_TYPE: &'static str = "java/nio/Buffer";
pub fn new(env: &JNIEnv<'a>) -> JniResult<JavaBuffer<'a>> {
let class = get_global_jclass(env, Self::SIG_TYPE)?;
Ok(JavaBuffer {
class,
method_hasRemaining: env.get_method_id(class, "hasRemaining", "()Z")?,
method_hasRemaining_ret: ReturnType::Primitive(Primitive::Boolean),
method_position: env.get_method_id(class, "position", "()I")?,
method_position_ret: ReturnType::Primitive(Primitive::Int),
})
}
}
#[allow(non_snake_case)]
pub struct ScalaIterator<'a> {
pub class: JClass<'a>,
pub method_hasNext: JMethodID,
pub method_hasNext_ret: ReturnType,
pub method_next: JMethodID,
pub method_next_ret: ReturnType,
}
impl<'a> ScalaIterator<'a> {
pub const SIG_TYPE: &'static str = "scala/collection/Iterator";
pub fn new(env: &JNIEnv<'a>) -> JniResult<ScalaIterator<'a>> {
let class = get_global_jclass(env, Self::SIG_TYPE)?;
Ok(ScalaIterator {
class,
method_hasNext: env.get_method_id(class, "hasNext", "()Z")?,
method_hasNext_ret: ReturnType::Primitive(Primitive::Boolean),
method_next: env.get_method_id(class, "next", "()Ljava/lang/Object;")?,
method_next_ret: ReturnType::Object,
})
}
}
#[allow(non_snake_case)]
pub struct ScalaTuple2<'a> {
pub class: JClass<'a>,
pub method__1: JMethodID,
pub method__1_ret: ReturnType,
pub method__2: JMethodID,
pub method__2_ret: ReturnType,
}
impl<'a> ScalaTuple2<'a> {
pub const SIG_TYPE: &'static str = "scala/Tuple2";
pub fn new(env: &JNIEnv<'a>) -> JniResult<ScalaTuple2<'a>> {
let class = get_global_jclass(env, Self::SIG_TYPE)?;
Ok(ScalaTuple2 {
class,
method__1: env.get_method_id(class, "_1", "()Ljava/lang/Object;")?,
method__1_ret: ReturnType::Object,
method__2: env.get_method_id(class, "_2", "()Ljava/lang/Object;")?,
method__2_ret: ReturnType::Object,
})
}
}
#[allow(non_snake_case)]
pub struct ScalaFunction0<'a> {
pub class: JClass<'a>,
pub method_apply: JMethodID,
pub method_apply_ret: ReturnType,
}
impl<'a> ScalaFunction0<'a> {
pub const SIG_TYPE: &'static str = "scala/Function0";
pub fn new(env: &JNIEnv<'a>) -> JniResult<ScalaFunction0<'a>> {
let class = get_global_jclass(env, Self::SIG_TYPE)?;
Ok(ScalaFunction0 {
class,
method_apply: env.get_method_id(class, "apply", "()Ljava/lang/Object;")?,
method_apply_ret: ReturnType::Object,
})
}
}
#[allow(non_snake_case)]
pub struct ScalaFunction1<'a> {
pub class: JClass<'a>,
pub method_apply: JMethodID,
pub method_apply_ret: ReturnType,
}
impl<'a> ScalaFunction1<'a> {
pub const SIG_TYPE: &'static str = "scala/Function1";
pub fn new(env: &JNIEnv<'a>) -> JniResult<ScalaFunction1<'a>> {
let class = get_global_jclass(env, Self::SIG_TYPE)?;
Ok(ScalaFunction1 {
class,
method_apply: env.get_method_id(
class,
"apply",
"(Ljava/lang/Object;)Ljava/lang/Object;",
)?,
method_apply_ret: ReturnType::Object,
})
}
}
#[allow(non_snake_case)]
pub struct ScalaFunction2<'a> {
pub class: JClass<'a>,
pub method_apply: JMethodID,
pub method_apply_ret: ReturnType,
}
impl<'a> ScalaFunction2<'a> {
pub const SIG_TYPE: &'static str = "scala/Function2";
pub fn new(env: &JNIEnv<'a>) -> JniResult<ScalaFunction2<'a>> {
let class = get_global_jclass(env, Self::SIG_TYPE)?;
Ok(ScalaFunction2 {
class,
method_apply: env.get_method_id(
class,
"apply",
"(Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;",
)?,
method_apply_ret: ReturnType::Object,
})
}
}
#[allow(non_snake_case)]
pub struct HadoopFileSystem<'a> {
pub class: JClass<'a>,
pub method_mkdirs: JMethodID,
pub method_mkdirs_ret: ReturnType,