Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(bindings/java): add Stat support #1894

Merged
merged 1 commit into from
Apr 11, 2023
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
114 changes: 87 additions & 27 deletions bindings/java/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ use jni::objects::JClass;
use jni::objects::JMap;
use jni::objects::JObject;
use jni::objects::JString;
use jni::sys::{jboolean, jlong};
use jni::JNIEnv;
use opendal::BlockingOperator;
use opendal::Operator;
Expand All @@ -33,7 +34,7 @@ pub extern "system" fn Java_org_apache_opendal_Operator_getOperator(
_class: JClass,
input: JString,
params: JObject,
) -> *const i32 {
) -> jlong {
let input: String = env
.get_string(&input)
.expect("Couldn't get java string!")
Expand All @@ -43,42 +44,18 @@ pub extern "system" fn Java_org_apache_opendal_Operator_getOperator(

let map = convert_map(&mut env, &params);
if let Ok(operator) = build_operator(scheme, map) {
Box::into_raw(Box::new(operator)) as *const i32
Box::into_raw(Box::new(operator)) as jlong
} else {
env.exception_clear().expect("Couldn't clear exception");
env.throw_new(
"java/lang/IllegalArgumentException",
"Unsupported operator.",
)
.expect("Couldn't throw exception");
std::ptr::null()
0 as jlong
}
}

fn convert_map(env: &mut JNIEnv, params: &JObject) -> HashMap<String, String> {
let mut result: HashMap<String, String> = HashMap::new();
let _ = JMap::from_env(env, params)
.unwrap()
.iter(env)
.and_then(|mut iter| {
while let Some(e) = iter.next(env)? {
let key = JString::from(e.0);
let value = JString::from(e.1);
let key: String = env
.get_string(&key)
.expect("Couldn't get java string!")
.into();
let value: String = env
.get_string(&value)
.expect("Couldn't get java string!")
.into();
result.insert(key, value);
}
Ok(())
});
result
}

/// # Safety
///
/// This function should not be called before the Operator are ready.
Expand All @@ -88,6 +65,7 @@ pub unsafe extern "system" fn Java_org_apache_opendal_Operator_freeOperator(
_class: JClass,
ptr: *mut Operator,
) {
// Take ownership of the pointer by wrapping it with a Box
let _ = Box::from_raw(ptr);
}

Expand Down Expand Up @@ -137,6 +115,64 @@ pub unsafe extern "system" fn Java_org_apache_opendal_Operator_read<'local>(
output
}

/// # Safety
///
/// This function should not be called before the Operator are ready.
#[no_mangle]
pub unsafe extern "system" fn Java_org_apache_opendal_Operator_stat(
mut env: JNIEnv,
_class: JClass,
ptr: *mut BlockingOperator,
file: JString,
) -> jlong {
let op = &mut *ptr;
let file: String = env
.get_string(&file)
.expect("Couldn't get java string!")
.into();
let metadata = op.stat(&file).unwrap();
Box::into_raw(Box::new(metadata)) as jlong
}

/// # Safety
///
/// This function should not be called before the Stat are ready.
#[no_mangle]
pub unsafe extern "system" fn Java_org_apache_opendal_Metadata_isFile(
mut _env: JNIEnv,
_class: JClass,
ptr: *mut opendal::Metadata,
) -> jboolean {
let metadata = &mut *ptr;
metadata.is_file() as jboolean
}

/// # Safety
///
/// This function should not be called before the Stat are ready.
#[no_mangle]
pub unsafe extern "system" fn Java_org_apache_opendal_Metadata_getContentLength(
mut _env: JNIEnv,
_class: JClass,
ptr: *mut opendal::Metadata,
) -> jlong {
let metadata = &mut *ptr;
metadata.content_length() as jlong
}

/// # Safety
///
/// This function should not be called before the Stat are ready.
#[no_mangle]
pub unsafe extern "system" fn Java_org_apache_opendal_Metadata_freeStat(
kidylee marked this conversation as resolved.
Show resolved Hide resolved
mut _env: JNIEnv,
_class: JClass,
ptr: *mut opendal::Metadata,
) {
// Take ownership of the pointer by wrapping it with a Box
let _ = Box::from_raw(ptr);
}

/// # Safety
///
/// This function should not be called before the Operator are ready.
Expand Down Expand Up @@ -188,3 +224,27 @@ fn build_operator(

Ok(op)
}

fn convert_map(env: &mut JNIEnv, params: &JObject) -> HashMap<String, String> {
Xuanwo marked this conversation as resolved.
Show resolved Hide resolved
let mut result: HashMap<String, String> = HashMap::new();
let _ = JMap::from_env(env, params)
.unwrap()
.iter(env)
.and_then(|mut iter| {
while let Some(e) = iter.next(env)? {
let key = JString::from(e.0);
let value = JString::from(e.1);
let key: String = env
.get_string(&key)
.expect("Couldn't get java string!")
.into();
let value: String = env
.get_string(&value)
.expect("Couldn't get java string!")
.into();
result.insert(key, value);
}
Ok(())
});
result
}
49 changes: 49 additions & 0 deletions bindings/java/src/main/java/org/apache/opendal/Metadata.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
* 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;

public class Metadata {

long ptr;

private native void freeStat(long statPtr);

private native boolean isFile(long statPtr);

private native long getContentLength(long statPtr);


public Metadata(long ptr) {
this.ptr = ptr;
}

public boolean isFile() {
return isFile(this.ptr);
}

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

public long getContentLength() {
return getContentLength(this.ptr);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -46,14 +46,16 @@ public Operator(String schema, Map<String, String> params) {

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

private native void freeOperator(long ptr);
protected native void freeOperator(long ptr);

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

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);
Expand All @@ -67,6 +69,11 @@ public void delete(String s) {
delete(this.ptr, s);
}

public Metadata stat(String fileName) {
long statPtr = stat(this.ptr, fileName);
return new Metadata(statPtr);
}

@Override
protected void finalize() throws Throwable {
super.finalize();
Expand Down
13 changes: 8 additions & 5 deletions bindings/java/src/test/java/org/apache/opendal/StepsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
import java.util.HashMap;
import java.util.Map;

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

public class StepsTest {

Expand All @@ -46,21 +46,24 @@ public void blocking_write_path_test_with_content_hello_world(String fileName, S


@Then("The blocking file {string} should exist")
public void the_blocking_file_test_should_exist(String content) {

public void the_blocking_file_test_should_exist(String fileName) {
Metadata metadata = this.operator.stat(fileName);
assertNotNull(metadata);
}


@Then("The blocking file {string} entry mode must be file")
public void the_blocking_file_test_entry_mode_must_be_file(String fileName) {
Metadata metadata = this.operator.stat(fileName);
assertTrue(metadata.isFile());

}

@Then("The blocking file {string} content length must be {int}")
public void the_blocking_file_test_content_length_must_be_13(String fileName, int length) {
String content = this.operator.read(fileName);
Metadata metadata = this.operator.stat(fileName);

assertEquals(content.length(), length);
assertEquals(metadata.getContentLength(), length);
}

@Then("The blocking file {string} must have content {string}")
Expand Down