From 637744ce155dd80e2d5faa923e9074f2fac2f2b3 Mon Sep 17 00:00:00 2001 From: Ankit Gupta Date: Mon, 7 Jul 2014 20:50:21 -0700 Subject: [PATCH 1/6] Package .so file with JNI jar --- java/Makefile | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/java/Makefile b/java/Makefile index 7a9c64732..287fe1021 100644 --- a/java/Makefile +++ b/java/Makefile @@ -3,6 +3,11 @@ NATIVE_JAVA_CLASSES = org.rocksdb.RocksDB org.rocksdb.Options org.rocksdb.WriteB NATIVE_INCLUDE = ./include ROCKSDB_JAR = rocksdbjni.jar +ROCKSDBJNILIB = ./librocksdbjni.so +ifeq ($(PLATFORM), OS_MACOSX) + ROCKSDBJNILIB = ./librocksdbjni.jnilib +endif + clean: -find . -name "*.class" -exec rm {} \; -find . -name "hs*.log" -exec rm {} \; @@ -11,7 +16,7 @@ clean: java: javac org/rocksdb/util/*.java org/rocksdb/*.java @cp ../HISTORY.md ./HISTORY-CPP.md - jar -cf $(ROCKSDB_JAR) org/rocksdb/*.class org/rocksdb/util/*.class HISTORY*.md + jar -cf $(ROCKSDB_JAR) org/rocksdb/*.class org/rocksdb/util/*.class HISTORY*.md $(ROCKSDBJNILIB) @rm -f ./HISTORY-CPP.md javah -d $(NATIVE_INCLUDE) -jni $(NATIVE_JAVA_CLASSES) From 4216ca36aeb358f5558e5c90faa0470a8ae8968d Mon Sep 17 00:00:00 2001 From: Ankit Gupta Date: Mon, 7 Jul 2014 21:45:02 -0700 Subject: [PATCH 2/6] Class IDs and method IDs should not be cached --- java/rocksjni/portal.h | 54 +++++++++++++++++++++--------------------- 1 file changed, 27 insertions(+), 27 deletions(-) diff --git a/java/rocksjni/portal.h b/java/rocksjni/portal.h index b4f874330..da6608393 100644 --- a/java/rocksjni/portal.h +++ b/java/rocksjni/portal.h @@ -22,7 +22,7 @@ class RocksDBJni { public: // Get the java class id of org.rocksdb.RocksDB. static jclass getJClass(JNIEnv* env) { - static jclass jclazz = env->FindClass("org/rocksdb/RocksDB"); + jclass jclazz = env->FindClass("org/rocksdb/RocksDB"); assert(jclazz != nullptr); return jclazz; } @@ -30,7 +30,7 @@ class RocksDBJni { // Get the field id of the member variable of org.rocksdb.RocksDB // that stores the pointer to rocksdb::DB. static jfieldID getHandleFieldID(JNIEnv* env) { - static jfieldID fid = env->GetFieldID( + jfieldID fid = env->GetFieldID( getJClass(env), "nativeHandle_", "J"); assert(fid != nullptr); return fid; @@ -55,7 +55,7 @@ class RocksDBExceptionJni { public: // Get the jclass of org.rocksdb.RocksDBException static jclass getJClass(JNIEnv* env) { - static jclass jclazz = env->FindClass("org/rocksdb/RocksDBException"); + jclass jclazz = env->FindClass("org/rocksdb/RocksDBException"); assert(jclazz != nullptr); return jclazz; } @@ -71,7 +71,7 @@ class RocksDBExceptionJni { } jstring msg = env->NewStringUTF(s.ToString().c_str()); // get the constructor id of org.rocksdb.RocksDBException - static jmethodID mid = env->GetMethodID( + jmethodID mid = env->GetMethodID( getJClass(env), "", "(Ljava/lang/String;)V"); assert(mid != nullptr); @@ -83,7 +83,7 @@ class OptionsJni { public: // Get the java class id of org.rocksdb.Options. static jclass getJClass(JNIEnv* env) { - static jclass jclazz = env->FindClass("org/rocksdb/Options"); + jclass jclazz = env->FindClass("org/rocksdb/Options"); assert(jclazz != nullptr); return jclazz; } @@ -91,7 +91,7 @@ class OptionsJni { // Get the field id of the member variable of org.rocksdb.Options // that stores the pointer to rocksdb::Options static jfieldID getHandleFieldID(JNIEnv* env) { - static jfieldID fid = env->GetFieldID( + jfieldID fid = env->GetFieldID( getJClass(env), "nativeHandle_", "J"); assert(fid != nullptr); return fid; @@ -115,7 +115,7 @@ class WriteOptionsJni { public: // Get the java class id of org.rocksdb.WriteOptions. static jclass getJClass(JNIEnv* env) { - static jclass jclazz = env->FindClass("org/rocksdb/WriteOptions"); + jclass jclazz = env->FindClass("org/rocksdb/WriteOptions"); assert(jclazz != nullptr); return jclazz; } @@ -123,7 +123,7 @@ class WriteOptionsJni { // Get the field id of the member variable of org.rocksdb.WriteOptions // that stores the pointer to rocksdb::WriteOptions static jfieldID getHandleFieldID(JNIEnv* env) { - static jfieldID fid = env->GetFieldID( + jfieldID fid = env->GetFieldID( getJClass(env), "nativeHandle_", "J"); assert(fid != nullptr); return fid; @@ -148,7 +148,7 @@ class ReadOptionsJni { public: // Get the java class id of org.rocksdb.ReadOptions. static jclass getJClass(JNIEnv* env) { - static jclass jclazz = env->FindClass("org/rocksdb/ReadOptions"); + jclass jclazz = env->FindClass("org/rocksdb/ReadOptions"); assert(jclazz != nullptr); return jclazz; } @@ -156,7 +156,7 @@ class ReadOptionsJni { // Get the field id of the member variable of org.rocksdb.ReadOptions // that stores the pointer to rocksdb::ReadOptions static jfieldID getHandleFieldID(JNIEnv* env) { - static jfieldID fid = env->GetFieldID( + jfieldID fid = env->GetFieldID( getJClass(env), "nativeHandle_", "J"); assert(fid != nullptr); return fid; @@ -181,13 +181,13 @@ class ReadOptionsJni { class WriteBatchJni { public: static jclass getJClass(JNIEnv* env) { - static jclass jclazz = env->FindClass("org/rocksdb/WriteBatch"); + jclass jclazz = env->FindClass("org/rocksdb/WriteBatch"); assert(jclazz != nullptr); return jclazz; } static jfieldID getHandleFieldID(JNIEnv* env) { - static jfieldID fid = env->GetFieldID( + jfieldID fid = env->GetFieldID( getJClass(env), "nativeHandle_", "J"); assert(fid != nullptr); return fid; @@ -211,7 +211,7 @@ class WriteBatchJni { class HistogramDataJni { public: static jmethodID getConstructorMethodId(JNIEnv* env, jclass jclazz) { - static jmethodID mid = env->GetMethodID( + jmethodID mid = env->GetMethodID( jclazz, "", "(DDDDD)V"); assert(mid != nullptr); return mid; @@ -222,7 +222,7 @@ class BackupableDBOptionsJni { public: // Get the java class id of org.rocksdb.BackupableDBOptions. static jclass getJClass(JNIEnv* env) { - static jclass jclazz = env->FindClass("org/rocksdb/BackupableDBOptions"); + jclass jclazz = env->FindClass("org/rocksdb/BackupableDBOptions"); assert(jclazz != nullptr); return jclazz; } @@ -230,7 +230,7 @@ class BackupableDBOptionsJni { // Get the field id of the member variable of org.rocksdb.BackupableDBOptions // that stores the pointer to rocksdb::BackupableDBOptions static jfieldID getHandleFieldID(JNIEnv* env) { - static jfieldID fid = env->GetFieldID( + jfieldID fid = env->GetFieldID( getJClass(env), "nativeHandle_", "J"); assert(fid != nullptr); return fid; @@ -255,7 +255,7 @@ class IteratorJni { public: // Get the java class id of org.rocksdb.Iteartor. static jclass getJClass(JNIEnv* env) { - static jclass jclazz = env->FindClass("org/rocksdb/RocksIterator"); + jclass jclazz = env->FindClass("org/rocksdb/RocksIterator"); assert(jclazz != nullptr); return jclazz; } @@ -263,7 +263,7 @@ class IteratorJni { // Get the field id of the member variable of org.rocksdb.Iterator // that stores the pointer to rocksdb::Iterator. static jfieldID getHandleFieldID(JNIEnv* env) { - static jfieldID fid = env->GetFieldID( + jfieldID fid = env->GetFieldID( getJClass(env), "nativeHandle_", "J"); assert(fid != nullptr); return fid; @@ -288,7 +288,7 @@ class FilterJni { public: // Get the java class id of org.rocksdb.FilterPolicy. static jclass getJClass(JNIEnv* env) { - static jclass jclazz = env->FindClass("org/rocksdb/Filter"); + jclass jclazz = env->FindClass("org/rocksdb/Filter"); assert(jclazz != nullptr); return jclazz; } @@ -296,7 +296,7 @@ class FilterJni { // Get the field id of the member variable of org.rocksdb.Filter // that stores the pointer to rocksdb::FilterPolicy. static jfieldID getHandleFieldID(JNIEnv* env) { - static jfieldID fid = env->GetFieldID( + jfieldID fid = env->GetFieldID( getJClass(env), "nativeHandle_", "J"); assert(fid != nullptr); return fid; @@ -321,28 +321,28 @@ class ListJni { public: // Get the java class id of java.util.List. static jclass getListClass(JNIEnv* env) { - static jclass jclazz = env->FindClass("java/util/List"); + jclass jclazz = env->FindClass("java/util/List"); assert(jclazz != nullptr); return jclazz; } // Get the java class id of java.util.ArrayList. static jclass getArrayListClass(JNIEnv* env) { - static jclass jclazz = env->FindClass("java/util/ArrayList"); + jclass jclazz = env->FindClass("java/util/ArrayList"); assert(jclazz != nullptr); return jclazz; } // Get the java class id of java.util.Iterator. static jclass getIteratorClass(JNIEnv* env) { - static jclass jclazz = env->FindClass("java/util/Iterator"); + jclass jclazz = env->FindClass("java/util/Iterator"); assert(jclazz != nullptr); return jclazz; } // Get the java method id of java.util.List.iterator(). static jmethodID getIteratorMethod(JNIEnv* env) { - static jmethodID mid = env->GetMethodID( + jmethodID mid = env->GetMethodID( getListClass(env), "iterator", "()Ljava/util/Iterator;"); assert(mid != nullptr); return mid; @@ -350,7 +350,7 @@ class ListJni { // Get the java method id of java.util.Iterator.hasNext(). static jmethodID getHasNextMethod(JNIEnv* env) { - static jmethodID mid = env->GetMethodID( + jmethodID mid = env->GetMethodID( getIteratorClass(env), "hasNext", "()Z"); assert(mid != nullptr); return mid; @@ -358,7 +358,7 @@ class ListJni { // Get the java method id of java.util.Iterator.next(). static jmethodID getNextMethod(JNIEnv* env) { - static jmethodID mid = env->GetMethodID( + jmethodID mid = env->GetMethodID( getIteratorClass(env), "next", "()Ljava/lang/Object;"); assert(mid != nullptr); return mid; @@ -366,7 +366,7 @@ class ListJni { // Get the java method id of arrayList constructor. static jmethodID getArrayListConstructorMethodId(JNIEnv* env, jclass jclazz) { - static jmethodID mid = env->GetMethodID( + jmethodID mid = env->GetMethodID( jclazz, "", "(I)V"); assert(mid != nullptr); return mid; @@ -374,7 +374,7 @@ class ListJni { // Get the java method id of java.util.List.add(). static jmethodID getListAddMethodId(JNIEnv* env) { - static jmethodID mid = env->GetMethodID( + jmethodID mid = env->GetMethodID( getListClass(env), "add", "(Ljava/lang/Object;)Z"); assert(mid != nullptr); return mid; From 05bd545000613d59908d93e7ddd117af3dd0b85a Mon Sep 17 00:00:00 2001 From: Ankit Gupta Date: Wed, 9 Jul 2014 09:02:05 -0700 Subject: [PATCH 3/6] Caching methodId and fieldId is fine --- java/org/rocksdb/RocksDB.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/java/org/rocksdb/RocksDB.java b/java/org/rocksdb/RocksDB.java index a55266e25..1117b220e 100644 --- a/java/org/rocksdb/RocksDB.java +++ b/java/org/rocksdb/RocksDB.java @@ -21,7 +21,7 @@ import org.rocksdb.util.Environment; public class RocksDB extends RocksObject { public static final int NOT_FOUND = -1; private static final String[] compressionLibs_ = { - "snappy", "z", "bzip2", "lz4", "lz4hc"}; + "snappy", "z", "bzip2", "lz4", "lz4hc", "gflags"}; /** * Loads the necessary library files. From 21e522673fa39244fed8afcbb5015cea14a57479 Mon Sep 17 00:00:00 2001 From: Ankit Gupta Date: Wed, 9 Jul 2014 09:06:20 -0700 Subject: [PATCH 4/6] Revert wrong commit --- java/org/rocksdb/RocksDB.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/java/org/rocksdb/RocksDB.java b/java/org/rocksdb/RocksDB.java index 1117b220e..a55266e25 100644 --- a/java/org/rocksdb/RocksDB.java +++ b/java/org/rocksdb/RocksDB.java @@ -21,7 +21,7 @@ import org.rocksdb.util.Environment; public class RocksDB extends RocksObject { public static final int NOT_FOUND = -1; private static final String[] compressionLibs_ = { - "snappy", "z", "bzip2", "lz4", "lz4hc", "gflags"}; + "snappy", "z", "bzip2", "lz4", "lz4hc"}; /** * Loads the necessary library files. From b6caaea9d3228cb5f2efffb184069e38179b6ba4 Mon Sep 17 00:00:00 2001 From: Ankit Gupta Date: Wed, 9 Jul 2014 09:06:40 -0700 Subject: [PATCH 5/6] Caching methodId and fieldId is fine --- java/rocksjni/portal.h | 28 ++++++++++++++-------------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/java/rocksjni/portal.h b/java/rocksjni/portal.h index da6608393..b46ac6554 100644 --- a/java/rocksjni/portal.h +++ b/java/rocksjni/portal.h @@ -30,7 +30,7 @@ class RocksDBJni { // Get the field id of the member variable of org.rocksdb.RocksDB // that stores the pointer to rocksdb::DB. static jfieldID getHandleFieldID(JNIEnv* env) { - jfieldID fid = env->GetFieldID( + static jfieldID fid = env->GetFieldID( getJClass(env), "nativeHandle_", "J"); assert(fid != nullptr); return fid; @@ -71,7 +71,7 @@ class RocksDBExceptionJni { } jstring msg = env->NewStringUTF(s.ToString().c_str()); // get the constructor id of org.rocksdb.RocksDBException - jmethodID mid = env->GetMethodID( + static jmethodID mid = env->GetMethodID( getJClass(env), "", "(Ljava/lang/String;)V"); assert(mid != nullptr); @@ -91,7 +91,7 @@ class OptionsJni { // Get the field id of the member variable of org.rocksdb.Options // that stores the pointer to rocksdb::Options static jfieldID getHandleFieldID(JNIEnv* env) { - jfieldID fid = env->GetFieldID( + static jfieldID fid = env->GetFieldID( getJClass(env), "nativeHandle_", "J"); assert(fid != nullptr); return fid; @@ -123,7 +123,7 @@ class WriteOptionsJni { // Get the field id of the member variable of org.rocksdb.WriteOptions // that stores the pointer to rocksdb::WriteOptions static jfieldID getHandleFieldID(JNIEnv* env) { - jfieldID fid = env->GetFieldID( + static jfieldID fid = env->GetFieldID( getJClass(env), "nativeHandle_", "J"); assert(fid != nullptr); return fid; @@ -156,7 +156,7 @@ class ReadOptionsJni { // Get the field id of the member variable of org.rocksdb.ReadOptions // that stores the pointer to rocksdb::ReadOptions static jfieldID getHandleFieldID(JNIEnv* env) { - jfieldID fid = env->GetFieldID( + static jfieldID fid = env->GetFieldID( getJClass(env), "nativeHandle_", "J"); assert(fid != nullptr); return fid; @@ -187,7 +187,7 @@ class WriteBatchJni { } static jfieldID getHandleFieldID(JNIEnv* env) { - jfieldID fid = env->GetFieldID( + static jfieldID fid = env->GetFieldID( getJClass(env), "nativeHandle_", "J"); assert(fid != nullptr); return fid; @@ -230,7 +230,7 @@ class BackupableDBOptionsJni { // Get the field id of the member variable of org.rocksdb.BackupableDBOptions // that stores the pointer to rocksdb::BackupableDBOptions static jfieldID getHandleFieldID(JNIEnv* env) { - jfieldID fid = env->GetFieldID( + static jfieldID fid = env->GetFieldID( getJClass(env), "nativeHandle_", "J"); assert(fid != nullptr); return fid; @@ -263,7 +263,7 @@ class IteratorJni { // Get the field id of the member variable of org.rocksdb.Iterator // that stores the pointer to rocksdb::Iterator. static jfieldID getHandleFieldID(JNIEnv* env) { - jfieldID fid = env->GetFieldID( + static jfieldID fid = env->GetFieldID( getJClass(env), "nativeHandle_", "J"); assert(fid != nullptr); return fid; @@ -296,7 +296,7 @@ class FilterJni { // Get the field id of the member variable of org.rocksdb.Filter // that stores the pointer to rocksdb::FilterPolicy. static jfieldID getHandleFieldID(JNIEnv* env) { - jfieldID fid = env->GetFieldID( + static jfieldID fid = env->GetFieldID( getJClass(env), "nativeHandle_", "J"); assert(fid != nullptr); return fid; @@ -342,7 +342,7 @@ class ListJni { // Get the java method id of java.util.List.iterator(). static jmethodID getIteratorMethod(JNIEnv* env) { - jmethodID mid = env->GetMethodID( + static jmethodID mid = env->GetMethodID( getListClass(env), "iterator", "()Ljava/util/Iterator;"); assert(mid != nullptr); return mid; @@ -350,7 +350,7 @@ class ListJni { // Get the java method id of java.util.Iterator.hasNext(). static jmethodID getHasNextMethod(JNIEnv* env) { - jmethodID mid = env->GetMethodID( + static jmethodID mid = env->GetMethodID( getIteratorClass(env), "hasNext", "()Z"); assert(mid != nullptr); return mid; @@ -358,7 +358,7 @@ class ListJni { // Get the java method id of java.util.Iterator.next(). static jmethodID getNextMethod(JNIEnv* env) { - jmethodID mid = env->GetMethodID( + static jmethodID mid = env->GetMethodID( getIteratorClass(env), "next", "()Ljava/lang/Object;"); assert(mid != nullptr); return mid; @@ -366,7 +366,7 @@ class ListJni { // Get the java method id of arrayList constructor. static jmethodID getArrayListConstructorMethodId(JNIEnv* env, jclass jclazz) { - jmethodID mid = env->GetMethodID( + static jmethodID mid = env->GetMethodID( jclazz, "", "(I)V"); assert(mid != nullptr); return mid; @@ -374,7 +374,7 @@ class ListJni { // Get the java method id of java.util.List.add(). static jmethodID getListAddMethodId(JNIEnv* env) { - jmethodID mid = env->GetMethodID( + static jmethodID mid = env->GetMethodID( getListClass(env), "add", "(Ljava/lang/Object;)Z"); assert(mid != nullptr); return mid; From 0a4d930264d0ea9252438480c629d94731312339 Mon Sep 17 00:00:00 2001 From: Ankit Gupta Date: Wed, 9 Jul 2014 09:09:08 -0700 Subject: [PATCH 6/6] Caching methodId and fieldId is fine: v2 --- java/rocksjni/portal.h | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/java/rocksjni/portal.h b/java/rocksjni/portal.h index b46ac6554..20b446863 100644 --- a/java/rocksjni/portal.h +++ b/java/rocksjni/portal.h @@ -211,8 +211,7 @@ class WriteBatchJni { class HistogramDataJni { public: static jmethodID getConstructorMethodId(JNIEnv* env, jclass jclazz) { - jmethodID mid = env->GetMethodID( - jclazz, "", "(DDDDD)V"); + static jmethodID mid = env->GetMethodID(jclazz, "", "(DDDDD)V"); assert(mid != nullptr); return mid; }