From e84137c8ae84a71ddd83fae981760046dbd7ae44 Mon Sep 17 00:00:00 2001 From: Adam Retter Date: Wed, 3 Feb 2016 17:47:38 +0000 Subject: [PATCH] Remove unnessecary java.util.List expense in JNI --- java/rocksjni/options.cc | 61 ++++++++----------- java/rocksjni/rocksjni.cc | 24 +++----- .../java/org/rocksdb/ColumnFamilyOptions.java | 14 ++--- java/src/main/java/org/rocksdb/Options.java | 14 ++--- java/src/main/java/org/rocksdb/RocksDB.java | 4 +- 5 files changed, 53 insertions(+), 64 deletions(-) diff --git a/java/rocksjni/options.cc b/java/rocksjni/options.cc index b8d7684ec..018d8bbe4 100644 --- a/java/rocksjni/options.cc +++ b/java/rocksjni/options.cc @@ -1081,21 +1081,19 @@ jbyte Java_org_rocksdb_Options_compressionType( * vector. */ std::vector rocksdb_compression_vector_helper( - JNIEnv* env, jobject jcompressionLevels) { + JNIEnv* env, jbyteArray jcompressionLevels) { std::vector compressionLevels; - // iterate over compressionLevels - jobject iteratorObj = env->CallObjectMethod( - jcompressionLevels, rocksdb::ListJni::getIteratorMethod(env)); - while (env->CallBooleanMethod( - iteratorObj, rocksdb::ListJni::getHasNextMethod(env)) == JNI_TRUE) { - // get compression - jobject jcompression_obj = env->CallObjectMethod(iteratorObj, - rocksdb::ListJni::getNextMethod(env)); - jbyte jcompression = env->CallByteMethod(jcompression_obj, - rocksdb::ByteJni::getByteValueMethod(env)); - compressionLevels.push_back(static_cast( - jcompression)); + + jsize len = env->GetArrayLength(jcompressionLevels); + jbyte* jcompressionLevel = env->GetByteArrayElements(jcompressionLevels, NULL); + for(int i = 0; i < len; i++) { + jbyte jcl; + jcl = jcompressionLevel[i]; + compressionLevels.push_back(static_cast(jcl)); } + env->ReleaseByteArrayElements(jcompressionLevels, jcompressionLevel, + JNI_ABORT); + return compressionLevels; } @@ -1103,34 +1101,29 @@ std::vector rocksdb_compression_vector_helper( * Helper method to convert a CompressionType vector to a Java * List. */ -jobject rocksdb_compression_list_helper(JNIEnv* env, +jbyteArray rocksdb_compression_list_helper(JNIEnv* env, std::vector compressionLevels) { - jclass jListClazz = env->FindClass("java/util/ArrayList"); - jmethodID midList = rocksdb::ListJni::getArrayListConstructorMethodId( - env, jListClazz); - jobject jcompressionLevels = env->NewObject(jListClazz, - midList, compressionLevels.size()); - // insert in java list + jbyte jbuf[compressionLevels.size()]; for (std::vector::size_type i = 0; i != compressionLevels.size(); i++) { - jclass jByteClazz = env->FindClass("java/lang/Byte"); - jmethodID midByte = env->GetMethodID(jByteClazz, "", "(B)V"); - jobject obj = env->NewObject(jByteClazz, midByte, - compressionLevels[i]); - env->CallBooleanMethod(jcompressionLevels, - rocksdb::ListJni::getListAddMethodId(env), obj); + jbuf[i] = compressionLevels[i]; } + // insert in java array + jbyteArray jcompressionLevels = env->NewByteArray( + static_cast(compressionLevels.size())); + env->SetByteArrayRegion(jcompressionLevels, 0, + static_cast(compressionLevels.size()), jbuf); return jcompressionLevels; } /* * Class: org_rocksdb_Options * Method: setCompressionPerLevel - * Signature: (JLjava/util/List;)V + * Signature: (J[B)V */ void Java_org_rocksdb_Options_setCompressionPerLevel( JNIEnv* env, jobject jobj, jlong jhandle, - jobject jcompressionLevels) { + jbyteArray jcompressionLevels) { auto* options = reinterpret_cast(jhandle); std::vector compressionLevels = rocksdb_compression_vector_helper(env, jcompressionLevels); @@ -1140,9 +1133,9 @@ void Java_org_rocksdb_Options_setCompressionPerLevel( /* * Class: org_rocksdb_Options * Method: compressionPerLevel - * Signature: (J)Ljava/util/List; + * Signature: (J)[B */ -jobject Java_org_rocksdb_Options_compressionPerLevel( +jbyteArray Java_org_rocksdb_Options_compressionPerLevel( JNIEnv* env, jobject jobj, jlong jhandle) { auto* options = reinterpret_cast(jhandle); return rocksdb_compression_list_helper(env, @@ -2285,11 +2278,11 @@ jbyte Java_org_rocksdb_ColumnFamilyOptions_compressionType( /* * Class: org_rocksdb_ColumnFamilyOptions * Method: setCompressionPerLevel - * Signature: (JLjava/util/List;)V + * Signature: (J[B)V */ void Java_org_rocksdb_ColumnFamilyOptions_setCompressionPerLevel( JNIEnv* env, jobject jobj, jlong jhandle, - jobject jcompressionLevels) { + jbyteArray jcompressionLevels) { auto* options = reinterpret_cast(jhandle); std::vector compressionLevels = rocksdb_compression_vector_helper(env, jcompressionLevels); @@ -2299,9 +2292,9 @@ void Java_org_rocksdb_ColumnFamilyOptions_setCompressionPerLevel( /* * Class: org_rocksdb_ColumnFamilyOptions * Method: compressionPerLevel - * Signature: (J)Ljava/util/List; + * Signature: (J)[B */ -jobject Java_org_rocksdb_ColumnFamilyOptions_compressionPerLevel( +jbyteArray Java_org_rocksdb_ColumnFamilyOptions_compressionPerLevel( JNIEnv* env, jobject jobj, jlong jhandle) { auto* options = reinterpret_cast(jhandle); return rocksdb_compression_list_helper(env, diff --git a/java/rocksjni/rocksjni.cc b/java/rocksjni/rocksjni.cc index 5004a27fc..7ef416618 100644 --- a/java/rocksjni/rocksjni.cc +++ b/java/rocksjni/rocksjni.cc @@ -171,25 +171,21 @@ jlongArray Java_org_rocksdb_RocksDB_open__JLjava_lang_String_2_3_3B_3J( /* * Class: org_rocksdb_RocksDB * Method: listColumnFamilies - * Signature: (JLjava/lang/String;)Ljava/util/List; + * Signature: (JLjava/lang/String;)[[B */ -jobject Java_org_rocksdb_RocksDB_listColumnFamilies( +jobjectArray Java_org_rocksdb_RocksDB_listColumnFamilies( JNIEnv* env, jclass jclazz, jlong jopt_handle, jstring jdb_path) { std::vector column_family_names; - auto opt = reinterpret_cast(jopt_handle); + auto* opt = reinterpret_cast(jopt_handle); const char* db_path = env->GetStringUTFChars(jdb_path, 0); - jobject jvalue_list = nullptr; - rocksdb::Status s = rocksdb::DB::ListColumnFamilies(*opt, db_path, &column_family_names); env->ReleaseStringUTFChars(jdb_path, db_path); - if (s.ok()) { - // Don't reuse class pointer - jclass jListClazz = env->FindClass("java/util/ArrayList"); - jmethodID mid = rocksdb::ListJni::getArrayListConstructorMethodId(env, - jListClazz); - jvalue_list = env->NewObject(jListClazz, mid, column_family_names.size()); + jclass jcls_ba = env->FindClass("[B"); + jobjectArray jresults = env->NewObjectArray( + static_cast(column_family_names.size()), jcls_ba, NULL); + if (s.ok()) { for (std::vector::size_type i = 0; i < column_family_names.size(); i++) { jbyteArray jcf_value = @@ -197,11 +193,11 @@ jobject Java_org_rocksdb_RocksDB_listColumnFamilies( env->SetByteArrayRegion( jcf_value, 0, static_cast(column_family_names[i].size()), reinterpret_cast(column_family_names[i].data())); - env->CallBooleanMethod(jvalue_list, - rocksdb::ListJni::getListAddMethodId(env), jcf_value); + env->SetObjectArrayElement(jresults, static_cast(i), jcf_value); + env->DeleteLocalRef(jcf_value); } } - return jvalue_list; + return jresults; } ////////////////////////////////////////////////////////////////////////////// diff --git a/java/src/main/java/org/rocksdb/ColumnFamilyOptions.java b/java/src/main/java/org/rocksdb/ColumnFamilyOptions.java index 91457defa..500935cf4 100644 --- a/java/src/main/java/org/rocksdb/ColumnFamilyOptions.java +++ b/java/src/main/java/org/rocksdb/ColumnFamilyOptions.java @@ -218,10 +218,10 @@ public class ColumnFamilyOptions extends RocksObject @Override public ColumnFamilyOptions setCompressionPerLevel( final List compressionLevels) { - final List byteCompressionTypes = new ArrayList<>( - compressionLevels.size()); - for (final CompressionType compressionLevel : compressionLevels) { - byteCompressionTypes.add(compressionLevel.getValue()); + final byte[] byteCompressionTypes = new byte[ + compressionLevels.size()]; + for (int i = 0; i < compressionLevels.size(); i++) { + byteCompressionTypes[i] = compressionLevels.get(i).getValue(); } setCompressionPerLevel(nativeHandle_, byteCompressionTypes); return this; @@ -229,7 +229,7 @@ public class ColumnFamilyOptions extends RocksObject @Override public List compressionPerLevel() { - final List byteCompressionTypes = + final byte[] byteCompressionTypes = compressionPerLevel(nativeHandle_); final List compressionLevels = new ArrayList<>(); for (final Byte byteCompressionType : byteCompressionTypes) { @@ -695,8 +695,8 @@ public class ColumnFamilyOptions extends RocksObject private native void setCompressionType(long handle, byte compressionType); private native byte compressionType(long handle); private native void setCompressionPerLevel(long handle, - List compressionLevels); - private native List compressionPerLevel(long handle); + byte[] compressionLevels); + private native byte[] compressionPerLevel(long handle); private native void useFixedLengthPrefixExtractor( long handle, int prefixLength); private native void useCappedPrefixExtractor( diff --git a/java/src/main/java/org/rocksdb/Options.java b/java/src/main/java/org/rocksdb/Options.java index 2c6f2e81f..1bc45ed1a 100644 --- a/java/src/main/java/org/rocksdb/Options.java +++ b/java/src/main/java/org/rocksdb/Options.java @@ -679,10 +679,10 @@ public class Options extends RocksObject @Override public Options setCompressionPerLevel(final List compressionLevels) { - final List byteCompressionTypes = new ArrayList<>( - compressionLevels.size()); - for (final CompressionType compressionLevel : compressionLevels) { - byteCompressionTypes.add(compressionLevel.getValue()); + final byte[] byteCompressionTypes = new byte[ + compressionLevels.size()]; + for (int i = 0; i < compressionLevels.size(); i++) { + byteCompressionTypes[i] = compressionLevels.get(i).getValue(); } setCompressionPerLevel(nativeHandle_, byteCompressionTypes); return this; @@ -690,7 +690,7 @@ public class Options extends RocksObject @Override public List compressionPerLevel() { - final List byteCompressionTypes = + final byte[] byteCompressionTypes = compressionPerLevel(nativeHandle_); final List compressionLevels = new ArrayList<>(); for (final Byte byteCompressionType : byteCompressionTypes) { @@ -1206,8 +1206,8 @@ public class Options extends RocksObject private native void setCompressionType(long handle, byte compressionType); private native byte compressionType(long handle); private native void setCompressionPerLevel(long handle, - List compressionLevels); - private native List compressionPerLevel(long handle); + byte[] compressionLevels); + private native byte[] compressionPerLevel(long handle); private native void useFixedLengthPrefixExtractor( long handle, int prefixLength); private native void useCappedPrefixExtractor( diff --git a/java/src/main/java/org/rocksdb/RocksDB.java b/java/src/main/java/org/rocksdb/RocksDB.java index fc98ac2b0..143649347 100644 --- a/java/src/main/java/org/rocksdb/RocksDB.java +++ b/java/src/main/java/org/rocksdb/RocksDB.java @@ -377,7 +377,7 @@ public class RocksDB extends RocksObject { */ public static List listColumnFamilies(final Options options, final String path) throws RocksDBException { - return RocksDB.listColumnFamilies(options.nativeHandle_, path); + return Arrays.asList(RocksDB.listColumnFamilies(options.nativeHandle_, path)); } private void storeOptionsInstance(DBOptionsInterface options) { @@ -1734,7 +1734,7 @@ public class RocksDB extends RocksObject { final long[] columnFamilyOptions ) throws RocksDBException; - protected native static List listColumnFamilies( + protected native static byte[][] listColumnFamilies( long optionsHandle, String path) throws RocksDBException; protected native void put( long handle, byte[] key, int keyLen,