Skip to content

Commit

Permalink
refactor(bindings/java): narrow unsafe boundary (#2351)
Browse files Browse the repository at this point in the history
Signed-off-by: tison <wander4096@gmail.com>
  • Loading branch information
tisonkun authored May 28, 2023
1 parent 3e6efbd commit 504df15
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 12 deletions.
7 changes: 7 additions & 0 deletions bindings/java/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,13 @@ unsafe fn get_current_env<'local>() -> JNIEnv<'local> {
JNIEnv::from_raw(env).unwrap()
}

/// # Safety
///
/// This function could be only when the lib is loaded.
unsafe fn get_global_runtime<'local>() -> &'local Runtime {
RUNTIME.get_unchecked()
}

fn jmap_to_hashmap(env: &mut JNIEnv, params: &JObject) -> Result<HashMap<String, String>> {
let map = JMap::from_env(env, params)?;
let mut iter = map.iter(env)?;
Expand Down
19 changes: 7 additions & 12 deletions bindings/java/src/operator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,13 @@ use jni::objects::JValueOwned;
use jni::objects::{JByteArray, JClass};
use jni::sys::jlong;
use jni::JNIEnv;

use opendal::Operator;
use opendal::Scheme;

use crate::get_current_env;
use crate::jmap_to_hashmap;
use crate::Result;
use crate::RUNTIME;
use crate::{get_current_env, get_global_runtime};

#[no_mangle]
pub extern "system" fn Java_org_apache_opendal_Operator_constructor(
Expand Down Expand Up @@ -93,8 +93,7 @@ fn intern_write(
let path = env.get_string(&path)?.to_str()?.to_string();
let content = env.convert_byte_array(content)?;

let runtime = unsafe { RUNTIME.get_unchecked() };
runtime.spawn(async move {
unsafe { get_global_runtime() }.spawn(async move {
let result = do_write(op, path, content).await;
complete_future(id, result.map(|_| JValueOwned::Void))
});
Expand Down Expand Up @@ -135,8 +134,7 @@ fn intern_append(
let path = env.get_string(&path)?.to_str()?.to_string();
let content = env.convert_byte_array(content)?;

let runtime = unsafe { RUNTIME.get_unchecked() };
runtime.spawn(async move {
unsafe { get_global_runtime() }.spawn(async move {
let result = do_append(op, path, content).await;
complete_future(id, result.map(|_| JValueOwned::Void))
});
Expand Down Expand Up @@ -170,8 +168,7 @@ fn intern_stat(env: &mut JNIEnv, op: *mut Operator, path: JString) -> Result<jlo

let path = env.get_string(&path)?.to_str()?.to_string();

let runtime = unsafe { RUNTIME.get_unchecked() };
runtime.spawn(async move {
unsafe { get_global_runtime() }.spawn(async move {
let result = do_stat(op, path).await;
complete_future(id, result.map(JValueOwned::Long))
});
Expand Down Expand Up @@ -206,8 +203,7 @@ fn intern_read(env: &mut JNIEnv, op: *mut Operator, path: JString) -> Result<jlo

let path = env.get_string(&path)?.to_str()?.to_string();

let runtime = unsafe { RUNTIME.get_unchecked() };
runtime.spawn(async move {
unsafe { get_global_runtime() }.spawn(async move {
let result = do_read(op, path).await;
complete_future(id, result.map(JValueOwned::Object))
});
Expand Down Expand Up @@ -246,8 +242,7 @@ fn intern_delete(env: &mut JNIEnv, op: *mut Operator, path: JString) -> Result<j

let path = env.get_string(&path)?.to_str()?.to_string();

let runtime = unsafe { RUNTIME.get_unchecked() };
runtime.spawn(async move {
unsafe { get_global_runtime() }.spawn(async move {
let result = do_delete(op, path).await;
complete_future(id, result.map(|_| JValueOwned::Void))
});
Expand Down

0 comments on commit 504df15

Please sign in to comment.