ShadowySpirits commented on code in PR #2112:
URL:
https://github.com/apache/incubator-opendal/pull/2112#discussion_r1177530950
##########
bindings/java/src/lib.rs:
##########
@@ -57,6 +134,52 @@ pub extern "system" fn
Java_org_apache_opendal_Operator_getOperator(
}
}
+/// # Safety
+///
+/// This function should not be called before the Operator are ready.
+#[no_mangle]
+pub unsafe extern "system" fn Java_org_apache_opendal_Operator_asyncWrite(
+ mut env: JNIEnv,
+ _class: JClass,
+ ptr: *mut Operator,
+ file: JString,
+ content: JString,
+ future: JObject,
+) {
+ let op = &mut *ptr;
+
+ let file: String = env.get_string(&file).unwrap().into();
+ let content: String = env.get_string(&content).unwrap().into();
+ // keep the future alive, so that we can complete it later
+ // but this approach will be limited by global ref table size
+ let future = env.new_global_ref(future).unwrap();
+
+ let x = async move {
+ op.write(&file, content).await.unwrap();
+ JENV.with(|cell| {
+ let env_ptr = cell.borrow().unwrap();
+ let mut env = JNIEnv::from_raw(env_ptr).unwrap();
+
+ // build result
+ let boolean_class = env.find_class("java/lang/Boolean").unwrap();
+ let boolean = env
+ .get_static_field(boolean_class, "TRUE", "Ljava/lang/Boolean;")
+ .unwrap();
+
+ // complete the java future
Review Comment:
I am afraid we must call `Future#complete` in Rust code even if we create a
future in Rust. Future presents a task, which should be completed by the
executor. In our scenario, the executor is our rust code.
This is different from the rust asynchronous mechanism. The runtime is
responsible for advancing the state of the future in Rust.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]