Minor refactor

Convert OrcReaderJniWrapper to singleton model to allow client handle library loading exception
This commit is contained in:
Yurui Zhou committed 2019-06-06 15:12:01 +08:00
1 parent e4c063041b
commit 7a80fbd0d6
6 files changed
+34 -42

No files matched your search

+5 -5
View File
@@ -133,7 +133,7 @@ extern "C" {
#endif
JNIEXPORT jlong JNICALL Java_org_apache_arrow_adapter_orc_OrcReaderJniWrapper_open(
JNIEnv* env, jclass this_cls, jstring file_path) {
JNIEnv* env, jobject this_obj, jstring file_path) {
std::shared_ptr<arrow::io::ReadableFile> in_file;
std::string path = JStringToCString(env, file_path);
@@ -164,19 +164,19 @@ JNIEXPORT jlong JNICALL Java_org_apache_arrow_adapter_orc_OrcReaderJniWrapper_op
}
JNIEXPORT void JNICALL Java_org_apache_arrow_adapter_orc_OrcReaderJniWrapper_close(
JNIEnv* env, jclass this_cls, jlong id) {
JNIEnv* env, jobject this_obj, jlong id) {
orc_reader_holder_.Erase(id);
}
JNIEXPORT jboolean JNICALL Java_org_apache_arrow_adapter_orc_OrcReaderJniWrapper_seek(
JNIEnv* env, jclass this_cls, jlong id, jint row_number) {
JNIEnv* env, jobject this_obj, jlong id, jint row_number) {
auto reader = orc_reader_holder_.Lookup(id);
return reader->Seek(row_number).ok();
}
JNIEXPORT jint JNICALL
Java_org_apache_arrow_adapter_orc_OrcReaderJniWrapper_getNumberOfStripes(JNIEnv* env,
jclass this_cls,
jobject this_obj,
jlong id) {
auto reader = orc_reader_holder_.Lookup(id);
return reader->NumberOfStripes();
@@ -184,7 +184,7 @@ Java_org_apache_arrow_adapter_orc_OrcReaderJniWrapper_getNumberOfStripes(JNIEnv*
JNIEXPORT jlong JNICALL
Java_org_apache_arrow_adapter_orc_OrcReaderJniWrapper_nextStripeReader(JNIEnv* env,
jclass this_cls,
jobject this_obj,
jlong id,
jlong batch_size) {
auto reader = GetNativeReader(id);
@@ -17,12 +17,11 @@
package org.apache.arrow.adapter.orc;
import java.io.IOException;
/**
* Wrapper for orc memory allocated by native code.
*/
class OrcMemoryJniWrapper implements AutoCloseable {
private final long id;
private final long memoryAddress;
@@ -31,14 +30,6 @@ class OrcMemoryJniWrapper implements AutoCloseable {
private final long capacity;
static {
try {
OrcJniUtils.loadOrcAdapterLibraryFromJar();
} catch (IOException e) {
throw new ExceptionInInitializerError(e);
}
}
/**
* Construct a new instance.
* @param id unique id of the underlying memory.
@@ -29,6 +29,7 @@ import org.apache.arrow.vector.ipc.ArrowReader;
* ArrowReader.
*/
public class OrcReader implements AutoCloseable {
private final OrcReaderJniWrapper jniWrapper;
private BufferAllocator allocator;
private final long id;
@@ -41,7 +42,8 @@ public class OrcReader implements AutoCloseable {
*/
public OrcReader(String filePath, BufferAllocator allocator) throws IOException {
this.allocator = allocator;
id = OrcReaderJniWrapper.open(filePath);
this.jniWrapper = OrcReaderJniWrapper.getInstance();
this.id = jniWrapper.open(filePath);
}
/**
@@ -51,7 +53,7 @@ public class OrcReader implements AutoCloseable {
* @return true if seek operation is succeeded
*/
public boolean seek(int rowNumber) {
return OrcReaderJniWrapper.seek(id, rowNumber);
return jniWrapper.seek(id, rowNumber);
}
/**
@@ -61,7 +63,7 @@ public class OrcReader implements AutoCloseable {
* @return ArrowReader that iterate over current stripes
*/
public ArrowReader nextStripeReader(long batchSize) {
long stripeReaderId = OrcReaderJniWrapper.nextStripeReader(id, batchSize);
long stripeReaderId = jniWrapper.nextStripeReader(id, batchSize);
if (stripeReaderId < 0) {
return null;
}
@@ -75,11 +77,11 @@ public class OrcReader implements AutoCloseable {
* @return number of stripes
*/
public int getNumberOfStripes() {
return OrcReaderJniWrapper.getNumberOfStripes(id);
return jniWrapper.getNumberOfStripes(id);
}
@Override
public void close() throws Exception {
OrcReaderJniWrapper.close(id);
jniWrapper.close(id);
}
}
@@ -23,12 +23,20 @@ import java.io.IOException;
* JNI wrapper for Orc reader.
*/
class OrcReaderJniWrapper {
static {
try {
OrcJniUtils.loadOrcAdapterLibraryFromJar();
} catch (IOException e) {
throw new ExceptionInInitializerError(e);
private static volatile OrcReaderJniWrapper INSTANCE;
static OrcReaderJniWrapper getInstance() throws IOException {
if (INSTANCE == null) {
synchronized (OrcReaderJniWrapper.class) {
if (INSTANCE == null) {
OrcJniUtils.loadOrcAdapterLibraryFromJar();
INSTANCE = new OrcReaderJniWrapper();
}
}
}
return INSTANCE;
}
/**
@@ -37,13 +45,13 @@ class OrcReaderJniWrapper {
* @return id of the orc reader instance if file opened successfully,
* otherwise return error code * -1.
*/
static native long open(String fileName);
native long open(String fileName);
/**
* Close the underlying reader and release related resources.
* Release resources associated with designated reader instance.
* @param readerId id of the reader instance.
*/
static native void close(long readerId);
native void close(long readerId);
/**
* Seek to designated row. Invoke nextStripeReader() after seek
@@ -52,14 +60,14 @@ class OrcReaderJniWrapper {
* @param rowNumber the rows number to seek
* @return true if seek operation is succeeded
*/
static native boolean seek(long readerId, int rowNumber);
native boolean seek(long readerId, int rowNumber);
/**
* The number of stripes in the file.
* @param readerId id of the reader instance
* @return number of stripes
*/
static native int getNumberOfStripes(long readerId);
native int getNumberOfStripes(long readerId);
/**
* Get a stripe level ArrowReader with specified batchSize in each record batch.
@@ -67,5 +75,5 @@ class OrcReaderJniWrapper {
* @param batchSize the number of rows loaded on each iteration
* @return id of the stripe reader instance.
*/
static native long nextStripeReader(long readerId, long batchSize);
native long nextStripeReader(long readerId, long batchSize);
}
@@ -45,7 +45,8 @@ public class OrcStripeReader extends ArrowReader {
/**
* Construct a new instance.
* @param id id of the stripe reader instance
* @param id id of the stripe reader instance, obtained by
* calling nextStripeReader from OrcReaderJniWrapper
* @param allocator memory allocator for accounting.
*/
OrcStripeReader(long id, BufferAllocator allocator) {
@@ -17,21 +17,11 @@
package org.apache.arrow.adapter.orc;
import java.io.IOException;
/**
* JNI wrapper for orc stripe reader.
*/
class OrcStripeReaderJniWrapper {
static {
try {
OrcJniUtils.loadOrcAdapterLibraryFromJar();
} catch (IOException e) {
throw new ExceptionInInitializerError(e);
}
}
/**
* Get the schema of current stripe.
* @param readerId id of the stripe reader instance.