Remove unnessecary java.util.List expense in JNI
This commit is contained in:
parent
76e8beeeb9
commit
e84137c8ae
@ -1081,21 +1081,19 @@ jbyte Java_org_rocksdb_Options_compressionType(
|
||||
* vector.
|
||||
*/
|
||||
std::vector<rocksdb::CompressionType> rocksdb_compression_vector_helper(
|
||||
JNIEnv* env, jobject jcompressionLevels) {
|
||||
JNIEnv* env, jbyteArray jcompressionLevels) {
|
||||
std::vector<rocksdb::CompressionType> 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<rocksdb::CompressionType>(
|
||||
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<rocksdb::CompressionType>(jcl));
|
||||
}
|
||||
env->ReleaseByteArrayElements(jcompressionLevels, jcompressionLevel,
|
||||
JNI_ABORT);
|
||||
|
||||
return compressionLevels;
|
||||
}
|
||||
|
||||
@ -1103,34 +1101,29 @@ std::vector<rocksdb::CompressionType> 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<rocksdb::CompressionType> 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<rocksdb::CompressionType>::size_type i = 0;
|
||||
i != compressionLevels.size(); i++) {
|
||||
jclass jByteClazz = env->FindClass("java/lang/Byte");
|
||||
jmethodID midByte = env->GetMethodID(jByteClazz, "<init>", "(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<jsize>(compressionLevels.size()));
|
||||
env->SetByteArrayRegion(jcompressionLevels, 0,
|
||||
static_cast<jsize>(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<rocksdb::Options*>(jhandle);
|
||||
std::vector<rocksdb::CompressionType> 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<rocksdb::Options*>(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<rocksdb::ColumnFamilyOptions*>(jhandle);
|
||||
std::vector<rocksdb::CompressionType> 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<rocksdb::ColumnFamilyOptions*>(jhandle);
|
||||
return rocksdb_compression_list_helper(env,
|
||||
|
@ -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<std::string> column_family_names;
|
||||
auto opt = reinterpret_cast<rocksdb::Options*>(jopt_handle);
|
||||
auto* opt = reinterpret_cast<rocksdb::Options*>(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<jsize>(column_family_names.size()), jcls_ba, NULL);
|
||||
if (s.ok()) {
|
||||
for (std::vector<std::string>::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<jsize>(column_family_names[i].size()),
|
||||
reinterpret_cast<const jbyte*>(column_family_names[i].data()));
|
||||
env->CallBooleanMethod(jvalue_list,
|
||||
rocksdb::ListJni::getListAddMethodId(env), jcf_value);
|
||||
env->SetObjectArrayElement(jresults, static_cast<jsize>(i), jcf_value);
|
||||
env->DeleteLocalRef(jcf_value);
|
||||
}
|
||||
}
|
||||
return jvalue_list;
|
||||
return jresults;
|
||||
}
|
||||
|
||||
//////////////////////////////////////////////////////////////////////////////
|
||||
|
@ -218,10 +218,10 @@ public class ColumnFamilyOptions extends RocksObject
|
||||
@Override
|
||||
public ColumnFamilyOptions setCompressionPerLevel(
|
||||
final List<CompressionType> compressionLevels) {
|
||||
final List<Byte> 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<CompressionType> compressionPerLevel() {
|
||||
final List<Byte> byteCompressionTypes =
|
||||
final byte[] byteCompressionTypes =
|
||||
compressionPerLevel(nativeHandle_);
|
||||
final List<CompressionType> 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<Byte> compressionLevels);
|
||||
private native List<Byte> compressionPerLevel(long handle);
|
||||
byte[] compressionLevels);
|
||||
private native byte[] compressionPerLevel(long handle);
|
||||
private native void useFixedLengthPrefixExtractor(
|
||||
long handle, int prefixLength);
|
||||
private native void useCappedPrefixExtractor(
|
||||
|
@ -679,10 +679,10 @@ public class Options extends RocksObject
|
||||
|
||||
@Override
|
||||
public Options setCompressionPerLevel(final List<CompressionType> compressionLevels) {
|
||||
final List<Byte> 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<CompressionType> compressionPerLevel() {
|
||||
final List<Byte> byteCompressionTypes =
|
||||
final byte[] byteCompressionTypes =
|
||||
compressionPerLevel(nativeHandle_);
|
||||
final List<CompressionType> 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<Byte> compressionLevels);
|
||||
private native List<Byte> compressionPerLevel(long handle);
|
||||
byte[] compressionLevels);
|
||||
private native byte[] compressionPerLevel(long handle);
|
||||
private native void useFixedLengthPrefixExtractor(
|
||||
long handle, int prefixLength);
|
||||
private native void useCappedPrefixExtractor(
|
||||
|
@ -377,7 +377,7 @@ public class RocksDB extends RocksObject {
|
||||
*/
|
||||
public static List<byte[]> 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<byte[]> listColumnFamilies(
|
||||
protected native static byte[][] listColumnFamilies(
|
||||
long optionsHandle, String path) throws RocksDBException;
|
||||
protected native void put(
|
||||
long handle, byte[] key, int keyLen,
|
||||
|
Loading…
x
Reference in New Issue
Block a user