1 // Copyright (c) 2011-present, Facebook, Inc. All rights reserved.
2 // This source code is licensed under both the GPLv2 (found in the
3 // COPYING file in the root directory) and Apache 2.0 License
4 // (found in the LICENSE.Apache file in the root directory).
6 // This file implements the callback "bridge" between Java and C++ for
9 #include "rocksjni/wal_filter_jnicallback.h"
10 #include "rocksjni/portal.h"
13 WalFilterJniCallback::WalFilterJniCallback(
14 JNIEnv
* env
, jobject jwal_filter
)
15 : JniCallback(env
, jwal_filter
) {
16 // Note: The name of a WalFilter will not change during it's lifetime,
17 // so we cache it in a global var
18 jmethodID jname_mid
= AbstractWalFilterJni::getNameMethodId(env
);
19 if(jname_mid
== nullptr) {
20 // exception thrown: NoSuchMethodException or OutOfMemoryError
23 jstring jname
= (jstring
)env
->CallObjectMethod(m_jcallback_obj
, jname_mid
);
24 if(env
->ExceptionCheck()) {
28 jboolean has_exception
= JNI_FALSE
;
29 m_name
= JniUtil::copyString(env
, jname
,
30 &has_exception
); // also releases jname
31 if (has_exception
== JNI_TRUE
) {
36 m_column_family_log_number_map_mid
=
37 AbstractWalFilterJni::getColumnFamilyLogNumberMapMethodId(env
);
38 if(m_column_family_log_number_map_mid
== nullptr) {
39 // exception thrown: NoSuchMethodException or OutOfMemoryError
43 m_log_record_found_proxy_mid
=
44 AbstractWalFilterJni::getLogRecordFoundProxyMethodId(env
);
45 if(m_log_record_found_proxy_mid
== nullptr) {
46 // exception thrown: NoSuchMethodException or OutOfMemoryError
51 void WalFilterJniCallback::ColumnFamilyLogNumberMap(
52 const std::map
<uint32_t, uint64_t>& cf_lognumber_map
,
53 const std::map
<std::string
, uint32_t>& cf_name_id_map
) {
54 jboolean attached_thread
= JNI_FALSE
;
55 JNIEnv
* env
= getJniEnv(&attached_thread
);
60 jobject jcf_lognumber_map
=
61 rocksdb::HashMapJni::fromCppMap(env
, &cf_lognumber_map
);
62 if (jcf_lognumber_map
== nullptr) {
64 env
->ExceptionDescribe(); // print out exception to stderr
65 releaseJniEnv(attached_thread
);
69 jobject jcf_name_id_map
=
70 rocksdb::HashMapJni::fromCppMap(env
, &cf_name_id_map
);
71 if (jcf_name_id_map
== nullptr) {
73 env
->ExceptionDescribe(); // print out exception to stderr
74 env
->DeleteLocalRef(jcf_lognumber_map
);
75 releaseJniEnv(attached_thread
);
79 env
->CallVoidMethod(m_jcallback_obj
,
80 m_column_family_log_number_map_mid
,
84 env
->DeleteLocalRef(jcf_lognumber_map
);
85 env
->DeleteLocalRef(jcf_name_id_map
);
87 if(env
->ExceptionCheck()) {
88 // exception thrown from CallVoidMethod
89 env
->ExceptionDescribe(); // print out exception to stderr
92 releaseJniEnv(attached_thread
);
95 WalFilter::WalProcessingOption
WalFilterJniCallback::LogRecordFound(
96 unsigned long long log_number
, const std::string
& log_file_name
,
97 const WriteBatch
& batch
, WriteBatch
* new_batch
, bool* batch_changed
) {
98 jboolean attached_thread
= JNI_FALSE
;
99 JNIEnv
* env
= getJniEnv(&attached_thread
);
100 if (env
== nullptr) {
101 return WalFilter::WalProcessingOption::kCorruptedRecord
;
104 jstring jlog_file_name
= JniUtil::toJavaString(env
, &log_file_name
);
105 if (jlog_file_name
== nullptr) {
106 // exception occcurred
107 env
->ExceptionDescribe(); // print out exception to stderr
108 releaseJniEnv(attached_thread
);
109 return WalFilter::WalProcessingOption::kCorruptedRecord
;
112 jshort jlog_record_found_result
= env
->CallShortMethod(m_jcallback_obj
,
113 m_log_record_found_proxy_mid
,
114 static_cast<jlong
>(log_number
),
116 reinterpret_cast<jlong
>(&batch
),
117 reinterpret_cast<jlong
>(new_batch
));
119 env
->DeleteLocalRef(jlog_file_name
);
121 if (env
->ExceptionCheck()) {
122 // exception thrown from CallShortMethod
123 env
->ExceptionDescribe(); // print out exception to stderr
124 releaseJniEnv(attached_thread
);
125 return WalFilter::WalProcessingOption::kCorruptedRecord
;
128 // unpack WalProcessingOption and batch_changed from jlog_record_found_result
129 jbyte jwal_processing_option_value
= (jlog_record_found_result
>> 8) & 0xFF;
130 jbyte jbatch_changed_value
= jlog_record_found_result
& 0xFF;
132 releaseJniEnv(attached_thread
);
134 *batch_changed
= jbatch_changed_value
== JNI_TRUE
;
136 return WalProcessingOptionJni::toCppWalProcessingOption(
137 jwal_processing_option_value
);
140 const char* WalFilterJniCallback::Name() const {
144 } // namespace rocksdb