Skip to content

Commit

Permalink
feat(binding/java): add rename support (#3238)
Browse files Browse the repository at this point in the history
* test(binding/java): add rename test

* test(binding/java): remove copy capability dependency in rename test
  • Loading branch information
G-XD authored Oct 8, 2023
1 parent 779750a commit 0995d40
Show file tree
Hide file tree
Showing 5 changed files with 385 additions and 0 deletions.
28 changes: 28 additions & 0 deletions bindings/java/src/blocking_operator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -178,3 +178,31 @@ fn intern_copy(

Ok(op.copy(&source_path, &target_path)?)
}

/// # Safety
///
/// This function should not be called before the Operator are ready.
#[no_mangle]
pub unsafe extern "system" fn Java_org_apache_opendal_BlockingOperator_rename(
mut env: JNIEnv,
_: JClass,
op: *mut BlockingOperator,
source_path: JString,
target_path: JString,
) {
intern_rename(&mut env, &mut *op, source_path, target_path).unwrap_or_else(|e| {
e.throw(&mut env);
})
}

fn intern_rename(
env: &mut JNIEnv,
op: &mut BlockingOperator,
source_path: JString,
target_path: JString,
) -> Result<()> {
let source_path = jstring_to_string(env, &source_path)?;
let target_path = jstring_to_string(env, &target_path)?;

Ok(op.rename(&source_path, &target_path)?)
}
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,10 @@ public void copy(String sourcePath, String targetPath) {
copy(nativeHandle, sourcePath, targetPath);
}

public void rename(String sourcePath, String targetPath) {
rename(nativeHandle, sourcePath, targetPath);
}

@Override
protected native void disposeInternal(long handle);

Expand All @@ -92,4 +96,6 @@ public void copy(String sourcePath, String targetPath) {
private static native long createDir(long nativeHandle, String path);

private static native long copy(long nativeHandle, String sourcePath, String targetPath);

private static native long rename(long nativeHandle, String sourcePath, String targetPath);
}
7 changes: 7 additions & 0 deletions bindings/java/src/main/java/org/apache/opendal/Operator.java
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,11 @@ public CompletableFuture<Void> copy(String sourcePath, String targetPath) {
return AsyncRegistry.take(requestId);
}

public CompletableFuture<Void> rename(String sourcePath, String targetPath) {
final long requestId = rename(nativeHandle, sourcePath, targetPath);
return AsyncRegistry.take(requestId);
}

@Override
protected native void disposeInternal(long handle);

Expand Down Expand Up @@ -215,4 +220,6 @@ public CompletableFuture<Void> copy(String sourcePath, String targetPath) {
private static native long createDir(long nativeHandle, String path);

private static native long copy(long nativeHandle, String sourcePath, String targetPath);

private static native long rename(long nativeHandle, String sourcePath, String targetPath);
}
41 changes: 41 additions & 0 deletions bindings/java/src/operator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -372,6 +372,47 @@ async fn do_copy(op: &mut Operator, source_path: String, target_path: String) ->
Ok(op.copy(&source_path, &target_path).await?)
}

/// # Safety
///
/// This function should not be called before the Operator are ready.
#[no_mangle]
pub unsafe extern "system" fn Java_org_apache_opendal_Operator_rename(
mut env: JNIEnv,
_: JClass,
op: *mut Operator,
source_path: JString,
target_path: JString,
) -> jlong {
intern_rename(&mut env, op, source_path, target_path).unwrap_or_else(|e| {
e.throw(&mut env);
0
})
}

fn intern_rename(
env: &mut JNIEnv,
op: *mut Operator,
source_path: JString,
target_path: JString,
) -> Result<jlong> {
let op = unsafe { &mut *op };
let id = request_id(env)?;

let source_path = jstring_to_string(env, &source_path)?;
let target_path = jstring_to_string(env, &target_path)?;

unsafe { get_global_runtime() }.spawn(async move {
let result = do_rename(op, source_path, target_path).await;
complete_future(id, result.map(|_| JValueOwned::Void))
});

Ok(id)
}

async fn do_rename(op: &mut Operator, source_path: String, target_path: String) -> Result<()> {
Ok(op.rename(&source_path, &target_path).await?)
}

/// # Safety
///
/// This function should not be called before the Operator are ready.
Expand Down
Loading

0 comments on commit 0995d40

Please sign in to comment.