You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@opendal.apache.org by "tisonkun (via GitHub)" <gi...@apache.org> on 2023/05/22 17:07:01 UTC
[GitHub] [incubator-opendal] tisonkun commented on a diff in pull request #2288: refactor(bindings/java): split different classes
tisonkun commented on code in PR #2288:
URL: https://github.com/apache/incubator-opendal/pull/2288#discussion_r1200793381
##########
bindings/java/src/lib.rs:
##########
@@ -75,263 +68,16 @@ pub unsafe extern "system" fn JNI_OnUnload(_: JavaVM, _: *mut c_void) {
}
}
-#[no_mangle]
-pub extern "system" fn Java_org_apache_opendal_Operator_newOperator(
- mut env: JNIEnv,
- _class: JClass,
- input: JString,
- params: JObject,
-) -> jlong {
- let input: String = env
- .get_string(&input)
- .expect("cannot get java string")
- .into();
-
- let scheme = Scheme::from_str(&input).unwrap();
-
- let map = convert_jmap_to_hashmap(&mut env, ¶ms);
- if let Ok(operator) = Operator::via_map(scheme, map) {
- Box::into_raw(Box::new(operator)) as jlong
- } else {
- env.exception_clear().expect("cannot clear exception");
- env.throw_new(
- "java/lang/IllegalArgumentException",
- "Unsupported operator.",
- )
- .expect("cannot throw exception");
- 0 as jlong
- }
-}
-
-/// # Safety
-///
-/// This function should not be called before the Operator are ready.
-#[no_mangle]
-pub unsafe extern "system" fn Java_org_apache_opendal_Operator_writeAsync(
- mut env: JNIEnv,
- _class: JClass,
- ptr: *mut Operator,
- file: JString,
- content: JString,
-) -> jobject {
- let op = &mut *ptr;
-
- let file: String = env.get_string(&file).unwrap().into();
- let content: String = env.get_string(&content).unwrap().into();
-
- let class = "java/util/concurrent/CompletableFuture";
- let f = env.new_object(class, "()V", &[]).unwrap();
-
- // keep the future alive, so that we can complete it later
- // but this approach will be limited by global ref table size (65535)
- let future = env.new_global_ref(&f).unwrap();
-
- RUNTIME.get_unchecked().spawn(async move {
- let result = op.write(&file, content).await;
-
- let env = ENV.with(|cell| *cell.borrow_mut()).unwrap();
- let mut env = JNIEnv::from_raw(env).unwrap();
-
- match result {
- Ok(()) => env
- .call_method(
- future,
- "complete",
- "(Ljava/lang/Object;)Z",
- &[JValue::Object(&JObject::null())],
- )
- .unwrap(),
- Err(err) => {
- let exception = convert_error_to_exception(&mut env, err).unwrap();
- env.call_method(
- future,
- "completeExceptionally",
- "(Ljava/lang/Throwable;)Z",
- &[JValue::Object(&exception)],
- )
- .unwrap()
- }
+fn or_throw<T>(env: &mut JNIEnv, res: opendal::Result<T>) -> T
Review Comment:
Why do you think it's unreachable? See https://github.com/jni-rs/jni-rs/issues/76
--
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: commits-unsubscribe@opendal.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org