diff --git a/java/rocksjni/comparator.cc b/java/rocksjni/comparator.cc index 8dcda6aa6f..420897939d 100644 --- a/java/rocksjni/comparator.cc +++ b/java/rocksjni/comparator.cc @@ -18,7 +18,7 @@ #include "rocksjni/comparatorjnicallback.h" #include "rocksjni/portal.h" -// +// void Java_org_rocksdb_ComparatorOptions_newComparatorOptions( JNIEnv* env, jobject jobj, jstring jpath, jboolean jshare_table_files, @@ -37,10 +37,9 @@ void Java_org_rocksdb_ComparatorOptions_newComparatorOptions( rocksdb::BackupableDBOptionsJni::setHandle(env, jobj, bopt); } +// -// - -// /* * Class: org_rocksdb_AbstractComparator @@ -51,10 +50,9 @@ void Java_org_rocksdb_AbstractComparator_disposeInternal( JNIEnv* env, jobject jobj, jlong handle) { delete reinterpret_cast(handle); } +// -// - -// /* * Class: org_rocksdb_Comparator @@ -63,14 +61,15 @@ void Java_org_rocksdb_AbstractComparator_disposeInternal( */ void Java_org_rocksdb_Comparator_createNewComparator0( JNIEnv* env, jobject jobj, jlong copt_handle) { - const rocksdb::ComparatorJniCallbackOptions* copt = reinterpret_cast(copt_handle); - const rocksdb::ComparatorJniCallback* c = new rocksdb::ComparatorJniCallback(env, jobj, copt); + const rocksdb::ComparatorJniCallbackOptions* copt = + reinterpret_cast(copt_handle); + const rocksdb::ComparatorJniCallback* c = + new rocksdb::ComparatorJniCallback(env, jobj, copt); rocksdb::AbstractComparatorJni::setHandle(env, jobj, c); } +// -// - -// /* * Class: org_rocksdb_DirectComparator @@ -79,10 +78,10 @@ void Java_org_rocksdb_Comparator_createNewComparator0( */ void Java_org_rocksdb_DirectComparator_createNewDirectComparator0( JNIEnv* env, jobject jobj, jlong copt_handle) { - const rocksdb::ComparatorJniCallbackOptions* copt = reinterpret_cast(copt_handle); - const rocksdb::DirectComparatorJniCallback* c = new rocksdb::DirectComparatorJniCallback(env, jobj, copt); + const rocksdb::ComparatorJniCallbackOptions* copt = + reinterpret_cast(copt_handle); + const rocksdb::DirectComparatorJniCallback* c = + new rocksdb::DirectComparatorJniCallback(env, jobj, copt); rocksdb::AbstractComparatorJni::setHandle(env, jobj, c); } - -// - +// diff --git a/java/rocksjni/comparatorjnicallback.cc b/java/rocksjni/comparatorjnicallback.cc index 22bba334c7..1f271fe42c 100644 --- a/java/rocksjni/comparatorjnicallback.cc +++ b/java/rocksjni/comparatorjnicallback.cc @@ -7,14 +7,15 @@ // rocksdb::Comparator. #include "rocksjni/comparatorjnicallback.h" -#include "portal.h" +#include "rocksjni/portal.h" namespace rocksdb { BaseComparatorJniCallback::BaseComparatorJniCallback( - JNIEnv* env, jobject jComparator, const ComparatorJniCallbackOptions* copt) { + JNIEnv* env, jobject jComparator, + const ComparatorJniCallbackOptions* copt) { - //mutex is used for synchronisation when we are re-using - //the global java slice objects + // mutex is used for synchronisation when we are re-using + // the global java slice objects mutex_ = new port::Mutex(copt->use_adaptive_mutex); // Note: Comparator methods may be accessed by multiple threads, @@ -30,11 +31,13 @@ BaseComparatorJniCallback::BaseComparatorJniCallback( // so we cache it in a global var jmethodID jNameMethodId = AbstractComparatorJni::getNameMethodId(env); jstring jsName = (jstring)env->CallObjectMethod(m_jComparator, jNameMethodId); - m_name = JniUtil::copyString(env, jsName); //also releases jsName + m_name = JniUtil::copyString(env, jsName); // also releases jsName m_jCompareMethodId = AbstractComparatorJni::getCompareMethodId(env); - m_jFindShortestSeparatorMethodId = AbstractComparatorJni::getFindShortestSeparatorMethodId(env); - m_jFindShortSuccessorMethodId = AbstractComparatorJni::getFindShortSuccessorMethodId(env); + m_jFindShortestSeparatorMethodId = + AbstractComparatorJni::getFindShortestSeparatorMethodId(env); + m_jFindShortSuccessorMethodId = + AbstractComparatorJni::getFindShortSuccessorMethodId(env); } /** @@ -42,24 +45,25 @@ BaseComparatorJniCallback::BaseComparatorJniCallback( */ JNIEnv* BaseComparatorJniCallback::getJniEnv() const { JNIEnv *env; - jint rs = m_jvm->AttachCurrentThread((void **)&env, NULL); + jint rs = m_jvm->AttachCurrentThread(reinterpret_cast(&env), NULL); assert(rs == JNI_OK); return env; -}; +} const char* BaseComparatorJniCallback::Name() const { return m_name.c_str(); } int BaseComparatorJniCallback::Compare(const Slice& a, const Slice& b) const { - JNIEnv* m_env = getJniEnv(); mutex_->Lock(); AbstractSliceJni::setHandle(m_env, m_jSliceA, &a); AbstractSliceJni::setHandle(m_env, m_jSliceB, &b); - jint result = m_env->CallIntMethod(m_jComparator, m_jCompareMethodId, m_jSliceA, m_jSliceB); + jint result = + m_env->CallIntMethod(m_jComparator, m_jCompareMethodId, m_jSliceA, + m_jSliceB); mutex_->Unlock(); @@ -68,8 +72,8 @@ int BaseComparatorJniCallback::Compare(const Slice& a, const Slice& b) const { return result; } -void BaseComparatorJniCallback::FindShortestSeparator(std::string* start, const Slice& limit) const { - +void BaseComparatorJniCallback::FindShortestSeparator( + std::string* start, const Slice& limit) const { if (start == nullptr) { return; } @@ -82,22 +86,24 @@ void BaseComparatorJniCallback::FindShortestSeparator(std::string* start, const mutex_->Lock(); AbstractSliceJni::setHandle(m_env, m_jSliceLimit, &limit); - jstring jsResultStart = (jstring)m_env->CallObjectMethod(m_jComparator, m_jFindShortestSeparatorMethodId, jsStart, m_jSliceLimit); + jstring jsResultStart = + (jstring)m_env->CallObjectMethod(m_jComparator, + m_jFindShortestSeparatorMethodId, jsStart, m_jSliceLimit); mutex_->Unlock(); m_env->DeleteLocalRef(jsStart); - if(jsResultStart != nullptr) { - //update start with result - *start = JniUtil::copyString(m_env, jsResultStart); //also releases jsResultStart + if (jsResultStart != nullptr) { + // update start with result + *start = + JniUtil::copyString(m_env, jsResultStart); // also releases jsResultStart } m_jvm->DetachCurrentThread(); } void BaseComparatorJniCallback::FindShortSuccessor(std::string* key) const { - if (key == nullptr) { return; } @@ -107,13 +113,16 @@ void BaseComparatorJniCallback::FindShortSuccessor(std::string* key) const { const char* keyUtf = key->c_str(); jstring jsKey = m_env->NewStringUTF(keyUtf); - jstring jsResultKey = (jstring)m_env->CallObjectMethod(m_jComparator, m_jFindShortSuccessorMethodId, jsKey); + jstring jsResultKey = + (jstring)m_env->CallObjectMethod(m_jComparator, + m_jFindShortSuccessorMethodId, jsKey); m_env->DeleteLocalRef(jsKey); - if(jsResultKey != nullptr) { - //update key with result - *key = JniUtil::copyString(m_env, jsResultKey); //also releases jsResultKey + if (jsResultKey != nullptr) { + // update key with result + *key = + JniUtil::copyString(m_env, jsResultKey); // also releases jsResultKey } m_jvm->DetachCurrentThread(); @@ -121,7 +130,7 @@ void BaseComparatorJniCallback::FindShortSuccessor(std::string* key) const { BaseComparatorJniCallback::~BaseComparatorJniCallback() { JNIEnv* m_env = getJniEnv(); - + m_env->DeleteGlobalRef(m_jComparator); m_env->DeleteGlobalRef(m_jSliceA); m_env->DeleteGlobalRef(m_jSliceB); @@ -129,21 +138,23 @@ BaseComparatorJniCallback::~BaseComparatorJniCallback() { // Note: do not need to explicitly detach, as this function is effectively // called from the Java class's disposeInternal method, and so already - // has an attached thread, getJniEnv above is just a no-op Attach to get the env - //jvm->DetachCurrentThread(); + // has an attached thread, getJniEnv above is just a no-op Attach to get + // the env jvm->DetachCurrentThread(); } ComparatorJniCallback::ComparatorJniCallback( - JNIEnv* env, jobject jComparator, const ComparatorJniCallbackOptions* copt) : BaseComparatorJniCallback(env, jComparator, copt) { - + JNIEnv* env, jobject jComparator, + const ComparatorJniCallbackOptions* copt) : + BaseComparatorJniCallback(env, jComparator, copt) { m_jSliceA = env->NewGlobalRef(SliceJni::construct0(env)); m_jSliceB = env->NewGlobalRef(SliceJni::construct0(env)); m_jSliceLimit = env->NewGlobalRef(SliceJni::construct0(env)); } DirectComparatorJniCallback::DirectComparatorJniCallback( - JNIEnv* env, jobject jComparator, const ComparatorJniCallbackOptions* copt) : BaseComparatorJniCallback(env, jComparator, copt) { - + JNIEnv* env, jobject jComparator, + const ComparatorJniCallbackOptions* copt) : + BaseComparatorJniCallback(env, jComparator, copt) { m_jSliceA = env->NewGlobalRef(DirectSliceJni::construct0(env)); m_jSliceB = env->NewGlobalRef(DirectSliceJni::construct0(env)); m_jSliceLimit = env->NewGlobalRef(DirectSliceJni::construct0(env)); diff --git a/java/rocksjni/comparatorjnicallback.h b/java/rocksjni/comparatorjnicallback.h index 8ca0ac64f2..cda32fce10 100644 --- a/java/rocksjni/comparatorjnicallback.h +++ b/java/rocksjni/comparatorjnicallback.h @@ -10,6 +10,7 @@ #define JAVA_ROCKSJNI_COMPARATORJNICALLBACK_H_ #include +#include #include "rocksdb/comparator.h" #include "rocksdb/slice.h" #include "port/port.h" @@ -43,15 +44,18 @@ struct ComparatorJniCallbackOptions { * introduce locking in regions of those methods via mutex_. */ class BaseComparatorJniCallback : public Comparator { - public: - BaseComparatorJniCallback(JNIEnv* env, jobject jComparator, const ComparatorJniCallbackOptions* copt); + public: + BaseComparatorJniCallback( + JNIEnv* env, jobject jComparator, + const ComparatorJniCallbackOptions* copt); virtual ~BaseComparatorJniCallback(); virtual const char* Name() const; virtual int Compare(const Slice& a, const Slice& b) const; - virtual void FindShortestSeparator(std::string* start, const Slice& limit) const; + virtual void FindShortestSeparator( + std::string* start, const Slice& limit) const; virtual void FindShortSuccessor(std::string* key) const; - private: + private: port::Mutex* mutex_; JavaVM* m_jvm; jobject m_jComparator; @@ -61,20 +65,24 @@ class BaseComparatorJniCallback : public Comparator { jmethodID m_jFindShortSuccessorMethodId; JNIEnv* getJniEnv() const; - protected: + protected: jobject m_jSliceA; jobject m_jSliceB; jobject m_jSliceLimit; }; class ComparatorJniCallback : public BaseComparatorJniCallback { - public: - ComparatorJniCallback(JNIEnv* env, jobject jComparator, const ComparatorJniCallbackOptions* copt); + public: + ComparatorJniCallback( + JNIEnv* env, jobject jComparator, + const ComparatorJniCallbackOptions* copt); }; class DirectComparatorJniCallback : public BaseComparatorJniCallback { - public: - DirectComparatorJniCallback(JNIEnv* env, jobject jComparator, const ComparatorJniCallbackOptions* copt); + public: + DirectComparatorJniCallback( + JNIEnv* env, jobject jComparator, + const ComparatorJniCallbackOptions* copt); }; } // namespace rocksdb diff --git a/java/rocksjni/options.cc b/java/rocksjni/options.cc index 4367fc708d..ceb4ce0311 100644 --- a/java/rocksjni/options.cc +++ b/java/rocksjni/options.cc @@ -16,6 +16,7 @@ #include "include/org_rocksdb_ReadOptions.h" #include "include/org_rocksdb_ComparatorOptions.h" +#include "rocksjni/comparatorjnicallback.h" #include "rocksjni/portal.h" #include "rocksdb/db.h" #include "rocksdb/options.h" @@ -25,7 +26,6 @@ #include "rocksdb/slice_transform.h" #include "rocksdb/rate_limiter.h" #include "rocksdb/comparator.h" -#include "comparatorjnicallback.h" /* * Class: org_rocksdb_Options @@ -1794,7 +1794,8 @@ void Java_org_rocksdb_ComparatorOptions_newComparatorOptions( */ jboolean Java_org_rocksdb_ComparatorOptions_useAdaptiveMutex( JNIEnv * env, jobject jobj, jlong jhandle) { - return reinterpret_cast(jhandle)->use_adaptive_mutex; + return reinterpret_cast(jhandle) + ->use_adaptive_mutex; } /* @@ -1804,8 +1805,8 @@ jboolean Java_org_rocksdb_ComparatorOptions_useAdaptiveMutex( */ void Java_org_rocksdb_ComparatorOptions_setUseAdaptiveMutex( JNIEnv * env, jobject jobj, jlong jhandle, jboolean juse_adaptive_mutex) { - reinterpret_cast(jhandle)->use_adaptive_mutex = - static_cast(juse_adaptive_mutex); + reinterpret_cast(jhandle) + ->use_adaptive_mutex = static_cast(juse_adaptive_mutex); } /* diff --git a/java/rocksjni/portal.h b/java/rocksjni/portal.h index bd40d4290f..32452ae0b4 100644 --- a/java/rocksjni/portal.h +++ b/java/rocksjni/portal.h @@ -12,6 +12,8 @@ #include #include +#include + #include "rocksdb/db.h" #include "rocksdb/filter_policy.h" #include "rocksdb/status.h" @@ -364,7 +366,7 @@ class ColumnFamilyHandleJni { }; class ComparatorOptionsJni { - public: + public: // Get the java class id of org.rocksdb.ComparatorOptions. static jclass getJClass(JNIEnv* env) { jclass jclazz = env->FindClass("org/rocksdb/ComparatorOptions"); @@ -383,7 +385,8 @@ class ComparatorOptionsJni { // Pass the ComparatorJniCallbackOptions pointer to the java side. static void setHandle( - JNIEnv* env, jobject jobj, const rocksdb::ComparatorJniCallbackOptions* op) { + JNIEnv* env, jobject jobj, + const rocksdb::ComparatorJniCallbackOptions* op) { env->SetLongField( jobj, getHandleFieldID(env), reinterpret_cast(op)); @@ -418,37 +421,41 @@ class AbstractComparatorJni { // Get the java method `compare` of org.rocksdb.Comparator. static jmethodID getCompareMethodId(JNIEnv* env) { - static jmethodID mid = env->GetMethodID( - getJClass(env), "compare", "(Lorg/rocksdb/AbstractSlice;Lorg/rocksdb/AbstractSlice;)I"); + static jmethodID mid = env->GetMethodID(getJClass(env), + "compare", + "(Lorg/rocksdb/AbstractSlice;Lorg/rocksdb/AbstractSlice;)I"); assert(mid != nullptr); return mid; } // Get the java method `findShortestSeparator` of org.rocksdb.Comparator. static jmethodID getFindShortestSeparatorMethodId(JNIEnv* env) { - static jmethodID mid = env->GetMethodID( - getJClass(env), "findShortestSeparator", "(Ljava/lang/String;Lorg/rocksdb/AbstractSlice;)Ljava/lang/String;"); + static jmethodID mid = env->GetMethodID(getJClass(env), + "findShortestSeparator", + "(Ljava/lang/String;Lorg/rocksdb/AbstractSlice;)Ljava/lang/String;"); assert(mid != nullptr); return mid; } // Get the java method `findShortSuccessor` of org.rocksdb.Comparator. static jmethodID getFindShortSuccessorMethodId(JNIEnv* env) { - static jmethodID mid = env->GetMethodID( - getJClass(env), "findShortSuccessor", "(Ljava/lang/String;)Ljava/lang/String;"); + static jmethodID mid = env->GetMethodID(getJClass(env), + "findShortSuccessor", + "(Ljava/lang/String;)Ljava/lang/String;"); assert(mid != nullptr); return mid; } // Get the pointer to ComparatorJniCallback. - static rocksdb::BaseComparatorJniCallback* getHandle(JNIEnv* env, jobject jobj) { + static rocksdb::BaseComparatorJniCallback* getHandle( + JNIEnv* env, jobject jobj) { return reinterpret_cast( env->GetLongField(jobj, getHandleFieldID(env))); } // Pass the ComparatorJniCallback pointer to the java side. static void setHandle( - JNIEnv* env, jobject jobj, const rocksdb::BaseComparatorJniCallback* op) { + JNIEnv* env, jobject jobj, const rocksdb::BaseComparatorJniCallback* op) { env->SetLongField( jobj, getHandleFieldID(env), reinterpret_cast(op)); @@ -585,8 +592,7 @@ class ListJni { }; class JniUtil { - public: - + public: /** * Copies a jstring to a std::string * and releases the original jstring diff --git a/java/rocksjni/slice.cc b/java/rocksjni/slice.cc index e54b9a745a..0d8b92c9cf 100644 --- a/java/rocksjni/slice.cc +++ b/java/rocksjni/slice.cc @@ -17,7 +17,7 @@ #include "rocksdb/slice.h" #include "rocksjni/portal.h" -// /* * Class: org_rocksdb_AbstractSlice @@ -73,7 +73,8 @@ jstring Java_org_rocksdb_AbstractSlice_toString0( jint Java_org_rocksdb_AbstractSlice_compare0( JNIEnv* env, jobject jobj, jlong handle, jlong otherHandle) { const rocksdb::Slice* slice = reinterpret_cast(handle); - const rocksdb::Slice* otherSlice = reinterpret_cast(otherHandle); + const rocksdb::Slice* otherSlice = + reinterpret_cast(otherHandle); return slice->compare(*otherSlice); } @@ -85,7 +86,8 @@ jint Java_org_rocksdb_AbstractSlice_compare0( jboolean Java_org_rocksdb_AbstractSlice_startsWith0( JNIEnv* env, jobject jobj, jlong handle, jlong otherHandle) { const rocksdb::Slice* slice = reinterpret_cast(handle); - const rocksdb::Slice* otherSlice = reinterpret_cast(otherHandle); + const rocksdb::Slice* otherSlice = + reinterpret_cast(otherHandle); return slice->starts_with(*otherSlice); } @@ -99,9 +101,9 @@ void Java_org_rocksdb_AbstractSlice_disposeInternal( delete reinterpret_cast(handle); } -// +// -// /* * Class: org_rocksdb_Slice @@ -113,13 +115,11 @@ void Java_org_rocksdb_Slice_createNewSlice0( const jsize dataSize = env->GetArrayLength(data); const int len = dataSize - offset; - //jbyte ptrData[len]; jbyte* ptrData = new jbyte[len]; env->GetByteArrayRegion(data, offset, len, ptrData); const rocksdb::Slice* slice = new rocksdb::Slice((const char*)ptrData, len); rocksdb::AbstractSliceJni::setHandle(env, jobj, slice); - } /* @@ -135,14 +135,15 @@ void Java_org_rocksdb_Slice_createNewSlice1( jboolean isCopy; jbyte* ptrData = env->GetByteArrayElements(data, &isCopy); const char* buf = new char[len]; - memcpy((void*)buf, ptrData, len); + memcpy(const_cast(buf), ptrData, len); - const rocksdb::Slice* slice = new rocksdb::Slice(buf, env->GetArrayLength(data)); + const rocksdb::Slice* slice = + new rocksdb::Slice(buf, env->GetArrayLength(data)); rocksdb::AbstractSliceJni::setHandle(env, jobj, slice); env->ReleaseByteArrayElements(data, ptrData, JNI_ABORT); - //NOTE: buf will be deleted in the org.rocksdb.Slice#dispose method + // NOTE: buf will be deleted in the org.rocksdb.Slice#dispose method } /* @@ -155,7 +156,8 @@ jbyteArray Java_org_rocksdb_Slice_data0( const rocksdb::Slice* slice = reinterpret_cast(handle); const int len = slice->size(); const jbyteArray data = env->NewByteArray(len); - env->SetByteArrayRegion(data, 0, len, (jbyte*)slice->data()); + env->SetByteArrayRegion(data, 0, len, + reinterpret_cast(const_cast(slice->data()))); return data; } @@ -170,9 +172,9 @@ void Java_org_rocksdb_Slice_disposeInternalBuf( delete [] slice->data_; } -// +// -// /* * Class: org_rocksdb_DirectSlice @@ -181,7 +183,8 @@ void Java_org_rocksdb_Slice_disposeInternalBuf( */ void Java_org_rocksdb_DirectSlice_createNewDirectSlice0( JNIEnv* env, jobject jobj, jobject data, jint length) { - const char* ptrData = (char*)env->GetDirectBufferAddress(data); + const char* ptrData = + reinterpret_cast(env->GetDirectBufferAddress(data)); const rocksdb::Slice* slice = new rocksdb::Slice(ptrData, length); rocksdb::AbstractSliceJni::setHandle(env, jobj, slice); } @@ -193,7 +196,8 @@ void Java_org_rocksdb_DirectSlice_createNewDirectSlice0( */ void Java_org_rocksdb_DirectSlice_createNewDirectSlice1( JNIEnv* env, jobject jobj, jobject data) { - const char* ptrData = (char*)env->GetDirectBufferAddress(data); + const char* ptrData = + reinterpret_cast(env->GetDirectBufferAddress(data)); const rocksdb::Slice* slice = new rocksdb::Slice(ptrData); rocksdb::AbstractSliceJni::setHandle(env, jobj, slice); } @@ -206,7 +210,8 @@ void Java_org_rocksdb_DirectSlice_createNewDirectSlice1( jobject Java_org_rocksdb_DirectSlice_data0( JNIEnv* env, jobject jobj, jlong handle) { const rocksdb::Slice* slice = reinterpret_cast(handle); - return env->NewDirectByteBuffer((void*)slice->data(), slice->size()); + return env->NewDirectByteBuffer(const_cast(slice->data()), + slice->size()); } /* @@ -228,6 +233,7 @@ jbyte Java_org_rocksdb_DirectSlice_get0( void Java_org_rocksdb_DirectSlice_clear0( JNIEnv* env, jobject jobj, jlong handle) { rocksdb::Slice* slice = reinterpret_cast(handle); + delete [] slice->data_; slice->clear(); } @@ -242,4 +248,4 @@ void Java_org_rocksdb_DirectSlice_removePrefix0( slice->remove_prefix(length); } -// +//