risingwave_java_binding/
lib.rs1#![feature(type_alias_impl_trait)]
16#![feature(try_blocks)]
17
18mod hummock_iterator;
19use std::ffi::c_void;
20use std::ops::Deref;
21
22use anyhow::anyhow;
23use cfg_or_panic::cfg_or_panic;
24use jni::objects::JByteArray;
25use jni::sys::{JNI_VERSION_1_2, jint};
26use jni::{JNIEnv, JavaVM};
27use prost::Message;
28use risingwave_common::error::AsReport;
29use risingwave_jni_core::jvm_runtime::{jvm_env, register_java_binding_native_methods};
30use risingwave_jni_core::{
31 EnvParam, JAVA_BINDING_ASYNC_RUNTIME, JavaBindingIterator, Pointer, execute_and_catch,
32 gen_class_name, to_guarded_slice,
33};
34
35use crate::hummock_iterator::new_hummock_java_binding_iter;
36
37fn register_hummock_java_binding_native_methods(
38 env: &mut JNIEnv<'_>,
39) -> Result<(), jni::errors::Error> {
40 let binding_class = env
41 .find_class(gen_class_name!(com.risingwave.java.binding.HummockIterator))
42 .inspect_err(|e| tracing::error!(error = ?e.as_report(), "jvm find class error"))?;
43 macro_rules! gen_native_method_array {
44 () => {{
45 risingwave_jni_core::split_extract_plain_native_methods! {{long iteratorNewHummock(byte[] readPlan);}, gen_native_method_array}
46 }};
47 ({$({ $func_name:ident, {$($ret:tt)+}, {$($args:tt)*} })*}) => {
48 [
49 $(
50 risingwave_jni_core::gen_native_method_entry! {
51 Java_com_risingwave_java_binding_HummockIterator_, $func_name, {$($ret)+}, {$($args)*}
52 },
53 )*
54 ]
55 }
56 }
57 env.register_native_methods(binding_class, &gen_native_method_array!())
58 .inspect_err(
59 |e| tracing::error!(error = ?e.as_report(), "jvm register native methods error"),
60 )?;
61
62 tracing::info!("register native methods for jvm successfully");
63 Ok(())
64}
65
66#[unsafe(no_mangle)]
67#[allow(non_snake_case)]
68pub extern "system" fn JNI_OnLoad(jvm: JavaVM, _reserved: *mut c_void) -> jint {
69 let result: Result<(), jni::errors::Error> = try {
70 let mut env = jvm_env(&jvm)?;
71 register_java_binding_native_methods(&mut env)?;
72 register_hummock_java_binding_native_methods(&mut env)?;
73 };
74 let _ =
75 result.inspect_err(|e| eprintln!("unable to register native method: {:?}", e.as_report()));
76
77 JNI_VERSION_1_2
78}
79
80#[cfg_or_panic(not(madsim))]
81#[unsafe(no_mangle)]
82extern "system" fn Java_com_risingwave_java_binding_HummockIterator_iteratorNewHummock<'a>(
83 env: EnvParam<'a>,
84 read_plan: JByteArray<'a>,
85) -> Pointer<'static, JavaBindingIterator<'static>> {
86 execute_and_catch(env, move |env| {
87 let read_plan = Message::decode(to_guarded_slice(&read_plan, env)?.deref())?;
88 let iter = JAVA_BINDING_ASYNC_RUNTIME
89 .block_on(new_hummock_java_binding_iter(read_plan))
90 .map_err(|e| anyhow!(e))?;
91 let iter = JavaBindingIterator::new_hummock_iter(iter);
92 Ok(iter.into())
93 })
94}