Skip to content

Commit

Permalink
refactor(binding/java): read should return bytes (#3153)
Browse files Browse the repository at this point in the history
Signed-off-by: tison <[email protected]>
  • Loading branch information
tisonkun authored Sep 21, 2023
1 parent 9ea8b93 commit 332748b
Show file tree
Hide file tree
Showing 9 changed files with 31 additions and 23 deletions.
15 changes: 8 additions & 7 deletions bindings/java/src/blocking_operator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ use jni::objects::JByteArray;
use jni::objects::JClass;
use jni::objects::JObject;
use jni::objects::JString;
use jni::sys::jlong;
use jni::sys::jstring;
use jni::sys::{jbyteArray, jlong};
use jni::JNIEnv;

use opendal::layers::BlockingLayer;
use opendal::BlockingOperator;
use opendal::Operator;
Expand Down Expand Up @@ -78,17 +78,18 @@ pub unsafe extern "system" fn Java_org_apache_opendal_BlockingOperator_read(
_: JClass,
op: *mut BlockingOperator,
path: JString,
) -> jstring {
) -> jbyteArray {
intern_read(&mut env, &mut *op, path).unwrap_or_else(|e| {
e.throw(&mut env);
JObject::null().into_raw()
JByteArray::default().into_raw()
})
}

fn intern_read(env: &mut JNIEnv, op: &mut BlockingOperator, path: JString) -> Result<jstring> {
fn intern_read(env: &mut JNIEnv, op: &mut BlockingOperator, path: JString) -> Result<jbyteArray> {
let path = env.get_string(&path)?;
let content = String::from_utf8(op.read(path.to_str()?)?)?;
Ok(env.new_string(content)?.into_raw())
let content = op.read(path.to_str()?)?;
let result = env.byte_array_from_slice(content.as_slice())?;
Ok(result.into_raw())
}

/// # Safety
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public void write(String path, byte[] content) {
write(nativeHandle, path, content);
}

public String read(String path) {
public byte[] read(String path) {
return read(nativeHandle, path);
}

Expand All @@ -68,7 +68,7 @@ public Metadata stat(String path) {

private static native void write(long nativeHandle, String path, byte[] content);

private static native String read(long nativeHandle, String path);
private static native byte[] read(long nativeHandle, String path);

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ public CompletableFuture<Metadata> stat(String path) {
return f.thenApply(Metadata::new);
}

public CompletableFuture<String> read(String path) {
public CompletableFuture<byte[]> read(String path) {
final long requestId = read(nativeHandle, path);
return AsyncRegistry.take(requestId);
}
Expand Down
3 changes: 1 addition & 2 deletions bindings/java/src/operator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -222,10 +222,9 @@ fn intern_read(env: &mut JNIEnv, op: *mut Operator, path: JString) -> Result<jlo

async fn do_read<'local>(op: &mut Operator, path: String) -> Result<JObject<'local>> {
let content = op.read(&path).await?;
let content = String::from_utf8(content)?;

let env = unsafe { get_current_env() };
let result = env.new_string(content)?;
let result = env.byte_array_from_slice(content.as_slice())?;
Ok(result.into())
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,15 @@

package org.apache.opendal;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertTrue;
import io.cucumber.java.en.Given;
import io.cucumber.java.en.Then;
import io.cucumber.java.en.When;
import java.nio.charset.StandardCharsets;
import java.time.Duration;
import java.util.HashMap;
import java.util.Map;
Expand Down Expand Up @@ -67,8 +69,8 @@ public void the_async_file_test_content_length_must_be_13(String path, int lengt

@Then("The async file {string} must have content {string}")
public void the_async_file_test_must_have_content_hello_world(String path, String content) {
String readContent = op.read(path).join();
assertEquals(content, readContent);
byte[] readContent = op.read(path).join();
assertThat(readContent).isEqualTo(content.getBytes(StandardCharsets.UTF_8));
}

@Then("The presign operation should success or raise exception Unsupported")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Map;
import org.junit.jupiter.api.AfterEach;
Expand Down Expand Up @@ -53,7 +54,7 @@ public void testStatNotExistFile() {
@Test
public void testCreateAndDelete() {
op.write("testCreateAndDelete", "Odin");
assertThat(op.read("testCreateAndDelete")).isEqualTo("Odin");
assertThat(op.read("testCreateAndDelete")).isEqualTo("Odin".getBytes(StandardCharsets.UTF_8));
op.delete("testCreateAndDelete");
assertThatExceptionOfType(OpenDALException.class)
.isThrownBy(() -> op.stat("testCreateAndDelete"))
Expand Down
10 changes: 6 additions & 4 deletions bindings/java/src/test/java/org/apache/opendal/OperatorTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import java.nio.charset.StandardCharsets;
import java.nio.file.Path;
import java.util.Arrays;
import java.util.HashMap;
Expand All @@ -42,7 +43,7 @@ public void testCreateAndDelete() {
@Cleanup Operator op = new Operator("Memory", params);

op.write("testCreateAndDelete", "Odin").join();
assertThat(op.read("testCreateAndDelete").join()).isEqualTo("Odin");
assertThat(op.read("testCreateAndDelete").join()).isEqualTo("Odin".getBytes(StandardCharsets.UTF_8));
op.delete("testCreateAndDelete").join();
assertThatThrownBy(() -> op.stat("testCreateAndDelete").join())
.is(OpenDALExceptionCondition.ofAsync(OpenDALException.Code.NotFound));
Expand All @@ -59,17 +60,18 @@ public void testAppendManyTimes() {
for (int i = 0; i < trunks.length; i++) {
op.append("testAppendManyTimes", trunks[i]).join();
String expected = Arrays.stream(trunks).limit(i + 1).collect(Collectors.joining());
assertThat(op.read("testAppendManyTimes").join()).isEqualTo(expected);
assertThat(op.read("testAppendManyTimes").join()).isEqualTo(expected.getBytes(StandardCharsets.UTF_8));
}

// write overwrite existing content
op.write("testAppendManyTimes", "new attempt").join();
assertThat(op.read("testAppendManyTimes").join()).isEqualTo("new attempt");
assertThat(op.read("testAppendManyTimes").join()).isEqualTo("new attempt".getBytes(StandardCharsets.UTF_8));

for (int i = 0; i < trunks.length; i++) {
op.append("testAppendManyTimes", trunks[i]).join();
String expected = Arrays.stream(trunks).limit(i + 1).collect(Collectors.joining());
assertThat(op.read("testAppendManyTimes").join()).isEqualTo("new attempt" + expected);
assertThat(op.read("testAppendManyTimes").join())
.isEqualTo(("new attempt" + expected).getBytes(StandardCharsets.UTF_8));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Map;
import lombok.Cleanup;
Expand All @@ -48,7 +49,7 @@ public void testAccessRedisService() {
@Cleanup final Operator op = new Operator("Redis", params);

op.write("testAccessRedisService", "Odin").join();
assertThat(op.read("testAccessRedisService").join()).isEqualTo("Odin");
assertThat(op.read("testAccessRedisService").join()).isEqualTo("Odin".getBytes(StandardCharsets.UTF_8));
op.delete("testAccessRedisService").join();
assertThatThrownBy(() -> op.stat("testAccessRedisService").join())
.is(OpenDALExceptionCondition.ofAsync(OpenDALException.Code.NotFound));
Expand All @@ -64,7 +65,7 @@ public void testAccessRedisServiceBlocking() {
@Cleanup final BlockingOperator op = new BlockingOperator("Redis", params);

op.write("testAccessRedisServiceBlocking", "Odin");
assertThat(op.read("testAccessRedisServiceBlocking")).isEqualTo("Odin");
assertThat(op.read("testAccessRedisServiceBlocking")).isEqualTo("Odin".getBytes(StandardCharsets.UTF_8));
op.delete("testAccessRedisServiceBlocking");
assertThatExceptionOfType(OpenDALException.class)
.isThrownBy(() -> op.stat("testAccessRedisServiceBlocking"))
Expand Down
6 changes: 4 additions & 2 deletions bindings/java/src/test/java/org/apache/opendal/StepsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,14 @@

package org.apache.opendal;

import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertTrue;
import io.cucumber.java.en.Given;
import io.cucumber.java.en.Then;
import io.cucumber.java.en.When;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Map;
import lombok.Cleanup;
Expand Down Expand Up @@ -64,7 +66,7 @@ public void the_blocking_file_test_content_length_must_be_13(String path, int le

@Then("The blocking file {string} must have content {string}")
public void the_blocking_file_test_must_have_content_hello_world(String path, String content) {
String readContent = op.read(path);
assertEquals(content, readContent);
byte[] readContent = op.read(path);
assertThat(readContent).isEqualTo(content.getBytes(StandardCharsets.UTF_8));
}
}

0 comments on commit 332748b

Please sign in to comment.