Skip to content

Commit

Permalink
feat(bindings/java): implement Operator#delete
Browse files Browse the repository at this point in the history
Signed-off-by: tison <wander4096@gmail.com>
  • Loading branch information
tisonkun committed May 27, 2023
1 parent 780f682 commit 0576a6c
Show file tree
Hide file tree
Showing 3 changed files with 103 additions and 1 deletion.
8 changes: 7 additions & 1 deletion bindings/java/src/main/java/org/apache/opendal/Operator.java
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,6 @@ private long requestId() {
final long requestId = Math.abs(UUID.randomUUID().getLeastSignificantBits());
final CompletableFuture<?> prev = registry.putIfAbsent(requestId, f);
if (prev == null) {

return requestId;
}
}
Expand Down Expand Up @@ -131,6 +130,11 @@ public CompletableFuture<String> read(String path) {
return registry().take(requestId);
}

public CompletableFuture<Void> delete(String path) {
final long requestId = delete(nativeHandle, path);
return registry().take(requestId);
}

@Override
protected native void disposeInternal(long handle);

Expand All @@ -140,5 +144,7 @@ public CompletableFuture<String> read(String path) {

private static native long write(long nativeHandle, String path, String content);

private static native long delete(long nativeHandle, String path);

private static native long stat(long nativeHandle, String path);
}
35 changes: 35 additions & 0 deletions bindings/java/src/operator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,41 @@ async fn do_read<'local>(op: &mut Operator, path: String) -> Result<JObject<'loc
Ok(result.into())
}

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

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

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

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

Ok(id)
}

async fn do_delete<'local>(op: &mut Operator, path: String) -> Result<()> {
Ok(op.delete(&path).await?)
}

fn request_id(env: &mut JNIEnv) -> Result<jlong> {
let registry = env
.call_static_method(
Expand Down
61 changes: 61 additions & 0 deletions bindings/java/src/test/java/org/apache/opendal/OperatorTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

package org.apache.opendal;

import static org.assertj.core.api.Assertions.assertThat;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.CompletionException;
import org.apache.opendal.exception.OpenDALException;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

public class OperatorTest {
private Operator op;

@BeforeEach
public void init() {
Map<String, String> params = new HashMap<>();
params.put("root", "/tmp");
this.op = new Operator("Memory", params);
}

@AfterEach
public void clean() {
this.op.close();
}

@Test
public void testCreateAndDelete() {
op.write("testCreateAndDelete", "Odin").join();
assertThat(op.read("testCreateAndDelete").join()).isEqualTo("Odin");
op.delete("testCreateAndDelete");
op.stat("testCreateAndDelete")
.handle((r, e) -> {
assertThat(r).isNull();
assertThat(e).isInstanceOf(CompletionException.class).hasCauseInstanceOf(OpenDALException.class);
OpenDALException.Code code = ((OpenDALException) e.getCause()).getCode();
assertThat(code).isEqualTo(OpenDALException.Code.NotFound);
return null;
})
.join();
}
}

0 comments on commit 0576a6c

Please sign in to comment.