]> git.proxmox.com Git - ceph.git/blob - ceph/src/rocksdb/java/rocksjni/wal_filter_jnicallback.cc
import 14.2.4 nautilus point release
[ceph.git] / ceph / src / rocksdb / java / rocksjni / wal_filter_jnicallback.cc
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).
5 //
6 // This file implements the callback "bridge" between Java and C++ for
7 // rocksdb::WalFilter.
8
9 #include "rocksjni/wal_filter_jnicallback.h"
10 #include "rocksjni/portal.h"
11
12 namespace rocksdb {
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
21 return;
22 }
23 jstring jname = (jstring)env->CallObjectMethod(m_jcallback_obj, jname_mid);
24 if(env->ExceptionCheck()) {
25 // exception thrown
26 return;
27 }
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) {
32 // exception thrown
33 return;
34 }
35
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
40 return;
41 }
42
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
47 return;
48 }
49 }
50
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);
56 if (env == nullptr) {
57 return;
58 }
59
60 jobject jcf_lognumber_map =
61 rocksdb::HashMapJni::fromCppMap(env, &cf_lognumber_map);
62 if (jcf_lognumber_map == nullptr) {
63 // exception occurred
64 env->ExceptionDescribe(); // print out exception to stderr
65 releaseJniEnv(attached_thread);
66 return;
67 }
68
69 jobject jcf_name_id_map =
70 rocksdb::HashMapJni::fromCppMap(env, &cf_name_id_map);
71 if (jcf_name_id_map == nullptr) {
72 // exception occurred
73 env->ExceptionDescribe(); // print out exception to stderr
74 env->DeleteLocalRef(jcf_lognumber_map);
75 releaseJniEnv(attached_thread);
76 return;
77 }
78
79 env->CallVoidMethod(m_jcallback_obj,
80 m_column_family_log_number_map_mid,
81 jcf_lognumber_map,
82 jcf_name_id_map);
83
84 env->DeleteLocalRef(jcf_lognumber_map);
85 env->DeleteLocalRef(jcf_name_id_map);
86
87 if(env->ExceptionCheck()) {
88 // exception thrown from CallVoidMethod
89 env->ExceptionDescribe(); // print out exception to stderr
90 }
91
92 releaseJniEnv(attached_thread);
93 }
94
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;
102 }
103
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;
110 }
111
112 jshort jlog_record_found_result = env->CallShortMethod(m_jcallback_obj,
113 m_log_record_found_proxy_mid,
114 static_cast<jlong>(log_number),
115 jlog_file_name,
116 reinterpret_cast<jlong>(&batch),
117 reinterpret_cast<jlong>(new_batch));
118
119 env->DeleteLocalRef(jlog_file_name);
120
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;
126 }
127
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;
131
132 releaseJniEnv(attached_thread);
133
134 *batch_changed = jbatch_changed_value == JNI_TRUE;
135
136 return WalProcessingOptionJni::toCppWalProcessingOption(
137 jwal_processing_option_value);
138 }
139
140 const char* WalFilterJniCallback::Name() const {
141 return m_name.get();
142 }
143
144 } // namespace rocksdb