Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 1 addition & 20 deletions bindings/java/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -132,25 +132,6 @@
<artifactId>maven-surefire-plugin</artifactId>
<version>3.0.0</version>
</plugin>
<plugin>
<groupId>com.coderplus.maven.plugins</groupId>
<artifactId>copy-rename-maven-plugin</artifactId>
<version>1.0</version>
<executions>
<execution>
<id>copy-file</id>
<phase>generate-sources</phase>
<goals>
<goal>copy</goal>
</goals>
<configuration>
<sourceFile>../tests/features/binding.feature</sourceFile>
<destinationFile>target/test-classes/features/binding.feature</destinationFile>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>

</project>
</project>
40 changes: 35 additions & 5 deletions bindings/java/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@ use std::ffi::c_void;
use std::str::FromStr;
use std::sync::Arc;

use jni::objects::JClass;
use jni::objects::JMap;
use jni::objects::JObject;
use jni::objects::JString;
use jni::objects::{JClass, JThrowable, JValue};
use jni::sys::{jboolean, jint};
use jni::sys::{jlong, JNI_VERSION_1_8};
use jni::{JNIEnv, JavaVM};
Expand Down Expand Up @@ -120,7 +120,7 @@ pub extern "system" fn Java_org_apache_opendal_Operator_getOperator(
///
/// This function should not be called before the Operator are ready.
#[no_mangle]
pub unsafe extern "system" fn Java_org_apache_opendal_Operator_asyncWrite(
pub unsafe extern "system" fn Java_org_apache_opendal_Operator_writeAsync(
mut env: JNIEnv,
_class: JClass,
ptr: *mut Operator,
Expand Down Expand Up @@ -221,6 +221,31 @@ pub unsafe extern "system" fn Java_org_apache_opendal_Operator_read<'local>(
output
}

fn convert_error_into_java_exception<'local>(
env: &mut JNIEnv<'local>,
error: opendal::Error,
) -> Result<JThrowable<'local>, jni::errors::Error> {
let error_code_class = env.find_class("org/apache/opendal/exception/OpenDALErrorCode")?;
let error_code_string = env.new_string(error.kind().into_static())?;
let error_code = env.call_static_method(
error_code_class,
"parse",
"(Ljava/lang/String;)Lorg/apache/opendal/exception/OpenDALErrorCode;",
&[JValue::Object(error_code_string.as_ref())],
)?;

let exception_class = env.find_class("org/apache/opendal/exception/OpenDALException")?;
let exception = env.new_object(
exception_class,
"(Lorg/apache/opendal/exception/OpenDALErrorCode;Ljava/lang/String;)V",
&[
JValue::Object(error_code.l()?.as_ref()),
JValue::Object(env.new_string(error.to_string())?.as_ref()),
],
)?;
Ok(JThrowable::from(exception))
}

/// # Safety
///
/// This function should not be called before the Operator are ready.
Expand All @@ -236,8 +261,13 @@ pub unsafe extern "system" fn Java_org_apache_opendal_Operator_stat(
.get_string(&file)
.expect("Couldn't get java string!")
.into();
let metadata = op.stat(&file).unwrap();
Box::into_raw(Box::new(metadata)) as jlong
let result = op.stat(&file);
if let Err(error) = result {
let exception = convert_error_into_java_exception(&mut env, error).unwrap();
env.throw(exception).unwrap();
return 0 as jlong;
}
Box::into_raw(Box::new(result.unwrap())) as jlong
}

/// # Safety
Expand Down Expand Up @@ -270,7 +300,7 @@ pub unsafe extern "system" fn Java_org_apache_opendal_Metadata_getContentLength(
///
/// This function should not be called before the Stat are ready.
#[no_mangle]
pub unsafe extern "system" fn Java_org_apache_opendal_Metadata_freeStat(
pub unsafe extern "system" fn Java_org_apache_opendal_Metadata_freeMetadata(
mut _env: JNIEnv,
_class: JClass,
ptr: *mut opendal::Metadata,
Expand Down
28 changes: 12 additions & 16 deletions bindings/java/src/main/java/org/apache/opendal/Metadata.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,7 @@

package org.apache.opendal;

public class Metadata {

long ptr;

private native void freeStat(long statPtr);

private native boolean isFile(long statPtr);

private native long getContentLength(long statPtr);


public class Metadata extends OpenDALObject {
public Metadata(long ptr) {
this.ptr = ptr;
}
Expand All @@ -38,12 +28,18 @@ public boolean isFile() {
return isFile(this.ptr);
}

@Override
protected void finalize() {
freeStat(this.ptr);
}

public long getContentLength() {
return getContentLength(this.ptr);
}

@Override
public void close() {
freeMetadata(this.ptr);
}

private native void freeMetadata(long statPtr);

private native boolean isFile(long statPtr);

private native long getContentLength(long statPtr);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
* 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 io.questdb.jar.jni.JarJniLoader;

public abstract class OpenDALObject implements AutoCloseable {
private static final String ORG_APACHE_OPENDAL_RUST_LIBS = "/org/apache/opendal/rust/libs";

private static final String OPENDAL_JAVA = "opendal_java";

static {
JarJniLoader.loadLib(
Operator.class,
ORG_APACHE_OPENDAL_RUST_LIBS,
OPENDAL_JAVA);
}

long ptr;
}
52 changes: 18 additions & 34 deletions bindings/java/src/main/java/org/apache/opendal/Operator.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,50 +20,21 @@

package org.apache.opendal;

import io.questdb.jar.jni.JarJniLoader;
import java.util.Map;
import java.util.concurrent.CompletableFuture;

public class Operator {

long ptr;

public class Operator extends OpenDALObject {
public Operator(String schema, Map<String, String> params) {
this.ptr = getOperator(schema, params);
}

public static final String ORG_APACHE_OPENDAL_RUST_LIBS = "/org/apache/opendal/rust/libs";

public static final String OPENDAL_JAVA = "opendal_java";

static {
JarJniLoader.loadLib(
Operator.class,
ORG_APACHE_OPENDAL_RUST_LIBS,
OPENDAL_JAVA);
}

private native long getOperator(String type, Map<String, String> params);

protected native void freeOperator(long ptr);

private native void write(long ptr, String fileName, String content);

private native void asyncWrite(long ptr, String fileName, String content, CompletableFuture<Boolean> future);

private native String read(long ptr, String fileName);

private native void delete(long ptr, String fileName);

private native long stat(long ptr, String file);

public void write(String fileName, String content) {
write(this.ptr, fileName, content);
}

public CompletableFuture<Boolean> asyncWrite(String fileName, String content) {
public CompletableFuture<Boolean> writeAsync(String fileName, String content) {
CompletableFuture<Boolean> future = new CompletableFuture<>();
asyncWrite(this.ptr, fileName, content, future);
writeAsync(this.ptr, fileName, content, future);
return future;
}

Expand All @@ -81,8 +52,21 @@ public Metadata stat(String fileName) {
}

@Override
protected void finalize() throws Throwable {
super.finalize();
public void close() {
this.freeOperator(ptr);
}

private native long getOperator(String type, Map<String, String> params);

protected native void freeOperator(long ptr);

private native void write(long ptr, String fileName, String content);

private native void writeAsync(long ptr, String fileName, String content, CompletableFuture<Boolean> future);

private native String read(long ptr, String fileName);

private native void delete(long ptr, String fileName);

private native long stat(long ptr, String file);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/*
* 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.exception;

public enum OpenDALErrorCode {
Comment thread
ShadowySpirits marked this conversation as resolved.
UNEXPECTED,
UNSUPPORTED,
CONFIG_INVALID,
NOT_FOUND,
PERMISSION_DENIED,
IS_A_DIRECTORY,
NOT_A_DIRECTORY,
ALREADY_EXISTS,
RATE_LIMITED,
IS_SAME_FILE,
CONDITION_NOT_MATCH,
CONTENT_TRUNCATED,
CONTENT_INCOMPLETE;

public static OpenDALErrorCode parse(String errorCode) {
switch (errorCode) {
case "Unsupported":
return UNSUPPORTED;
case "ConfigInvalid":
return CONFIG_INVALID;
case "NotFound":
return NOT_FOUND;
case "PermissionDenied":
return PERMISSION_DENIED;
case "IsADirectory":
return IS_A_DIRECTORY;
case "NotADirectory":
return NOT_A_DIRECTORY;
case "AlreadyExists":
return ALREADY_EXISTS;
case "RateLimited":
return RATE_LIMITED;
case "IsSameFile":
return IS_SAME_FILE;
case "ConditionNotMatch":
return CONDITION_NOT_MATCH;
case "ContentTruncated":
return CONTENT_TRUNCATED;
case "ContentIncomplete":
return CONTENT_INCOMPLETE;
default:
return UNEXPECTED;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
* 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.exception;

public class OpenDALException extends RuntimeException {
private final OpenDALErrorCode errorCode;

public OpenDALException(OpenDALErrorCode errorCode, String message) {
super(message);
this.errorCode = errorCode;
}

public OpenDALErrorCode getErrorCode() {
return errorCode;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,9 @@
import java.util.Map;
import java.util.concurrent.CompletableFuture;

import static org.junit.jupiter.api.Assertions.*;
import static org.junit.jupiter.api.Assertions.assertTrue;

public class AsyncStepsTest {

Operator operator;

@Given("A new OpenDAL Async Operator")
Expand All @@ -41,7 +40,7 @@ public void a_new_open_dal_async_operator() {

@When("Async write path {string} with content {string}")
public void async_write_path_test_with_content_hello_world(String fileName, String content) {
CompletableFuture<Boolean> future = operator.asyncWrite(fileName, content);
CompletableFuture<Boolean> future = operator.writeAsync(fileName, content);
Boolean result = future.join();
assertTrue(result);
}
Expand Down
Loading