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 "bridge" between Java and C++ and enables
7 // calling c++ ROCKSDB_NAMESPACE::WriteBatchWithIndex methods from Java side.
8 
9 #include "rocksdb/utilities/write_batch_with_index.h"
10 #include "include/org_rocksdb_WBWIRocksIterator.h"
11 #include "include/org_rocksdb_WriteBatchWithIndex.h"
12 #include "rocksdb/comparator.h"
13 #include "rocksjni/portal.h"
14 
15 /*
16  * Class:     org_rocksdb_WriteBatchWithIndex
17  * Method:    newWriteBatchWithIndex
18  * Signature: ()J
19  */
Java_org_rocksdb_WriteBatchWithIndex_newWriteBatchWithIndex__(JNIEnv *,jclass)20 jlong Java_org_rocksdb_WriteBatchWithIndex_newWriteBatchWithIndex__(
21     JNIEnv* /*env*/, jclass /*jcls*/) {
22   auto* wbwi = new ROCKSDB_NAMESPACE::WriteBatchWithIndex();
23   return reinterpret_cast<jlong>(wbwi);
24 }
25 
26 /*
27  * Class:     org_rocksdb_WriteBatchWithIndex
28  * Method:    newWriteBatchWithIndex
29  * Signature: (Z)J
30  */
Java_org_rocksdb_WriteBatchWithIndex_newWriteBatchWithIndex__Z(JNIEnv *,jclass,jboolean joverwrite_key)31 jlong Java_org_rocksdb_WriteBatchWithIndex_newWriteBatchWithIndex__Z(
32     JNIEnv* /*env*/, jclass /*jcls*/, jboolean joverwrite_key) {
33   auto* wbwi = new ROCKSDB_NAMESPACE::WriteBatchWithIndex(
34       ROCKSDB_NAMESPACE::BytewiseComparator(), 0,
35       static_cast<bool>(joverwrite_key));
36   return reinterpret_cast<jlong>(wbwi);
37 }
38 
39 /*
40  * Class:     org_rocksdb_WriteBatchWithIndex
41  * Method:    newWriteBatchWithIndex
42  * Signature: (JBIZ)J
43  */
Java_org_rocksdb_WriteBatchWithIndex_newWriteBatchWithIndex__JBIZ(JNIEnv *,jclass,jlong jfallback_index_comparator_handle,jbyte jcomparator_type,jint jreserved_bytes,jboolean joverwrite_key)44 jlong Java_org_rocksdb_WriteBatchWithIndex_newWriteBatchWithIndex__JBIZ(
45     JNIEnv* /*env*/, jclass /*jcls*/, jlong jfallback_index_comparator_handle,
46     jbyte jcomparator_type, jint jreserved_bytes, jboolean joverwrite_key) {
47   ROCKSDB_NAMESPACE::Comparator* fallback_comparator = nullptr;
48   switch (jcomparator_type) {
49     // JAVA_COMPARATOR
50     case 0x0:
51       fallback_comparator =
52           reinterpret_cast<ROCKSDB_NAMESPACE::ComparatorJniCallback*>(
53               jfallback_index_comparator_handle);
54       break;
55 
56     // JAVA_NATIVE_COMPARATOR_WRAPPER
57     case 0x1:
58       fallback_comparator = reinterpret_cast<ROCKSDB_NAMESPACE::Comparator*>(
59           jfallback_index_comparator_handle);
60       break;
61   }
62   auto* wbwi = new ROCKSDB_NAMESPACE::WriteBatchWithIndex(
63       fallback_comparator, static_cast<size_t>(jreserved_bytes),
64       static_cast<bool>(joverwrite_key));
65   return reinterpret_cast<jlong>(wbwi);
66 }
67 
68 /*
69  * Class:     org_rocksdb_WriteBatchWithIndex
70  * Method:    count0
71  * Signature: (J)I
72  */
Java_org_rocksdb_WriteBatchWithIndex_count0(JNIEnv *,jobject,jlong jwbwi_handle)73 jint Java_org_rocksdb_WriteBatchWithIndex_count0(JNIEnv* /*env*/,
74                                                  jobject /*jobj*/,
75                                                  jlong jwbwi_handle) {
76   auto* wbwi =
77       reinterpret_cast<ROCKSDB_NAMESPACE::WriteBatchWithIndex*>(jwbwi_handle);
78   assert(wbwi != nullptr);
79 
80   return static_cast<jint>(wbwi->GetWriteBatch()->Count());
81 }
82 
83 /*
84  * Class:     org_rocksdb_WriteBatchWithIndex
85  * Method:    put
86  * Signature: (J[BI[BI)V
87  */
Java_org_rocksdb_WriteBatchWithIndex_put__J_3BI_3BI(JNIEnv * env,jobject jobj,jlong jwbwi_handle,jbyteArray jkey,jint jkey_len,jbyteArray jentry_value,jint jentry_value_len)88 void Java_org_rocksdb_WriteBatchWithIndex_put__J_3BI_3BI(
89     JNIEnv* env, jobject jobj, jlong jwbwi_handle, jbyteArray jkey,
90     jint jkey_len, jbyteArray jentry_value, jint jentry_value_len) {
91   auto* wbwi =
92       reinterpret_cast<ROCKSDB_NAMESPACE::WriteBatchWithIndex*>(jwbwi_handle);
93   assert(wbwi != nullptr);
94   auto put = [&wbwi](ROCKSDB_NAMESPACE::Slice key,
95                      ROCKSDB_NAMESPACE::Slice value) {
96     return wbwi->Put(key, value);
97   };
98   std::unique_ptr<ROCKSDB_NAMESPACE::Status> status =
99       ROCKSDB_NAMESPACE::JniUtil::kv_op(put, env, jobj, jkey, jkey_len,
100                                         jentry_value, jentry_value_len);
101   if (status != nullptr && !status->ok()) {
102     ROCKSDB_NAMESPACE::RocksDBExceptionJni::ThrowNew(env, status);
103   }
104 }
105 
106 /*
107  * Class:     org_rocksdb_WriteBatchWithIndex
108  * Method:    put
109  * Signature: (J[BI[BIJ)V
110  */
Java_org_rocksdb_WriteBatchWithIndex_put__J_3BI_3BIJ(JNIEnv * env,jobject jobj,jlong jwbwi_handle,jbyteArray jkey,jint jkey_len,jbyteArray jentry_value,jint jentry_value_len,jlong jcf_handle)111 void Java_org_rocksdb_WriteBatchWithIndex_put__J_3BI_3BIJ(
112     JNIEnv* env, jobject jobj, jlong jwbwi_handle, jbyteArray jkey,
113     jint jkey_len, jbyteArray jentry_value, jint jentry_value_len,
114     jlong jcf_handle) {
115   auto* wbwi =
116       reinterpret_cast<ROCKSDB_NAMESPACE::WriteBatchWithIndex*>(jwbwi_handle);
117   assert(wbwi != nullptr);
118   auto* cf_handle =
119       reinterpret_cast<ROCKSDB_NAMESPACE::ColumnFamilyHandle*>(jcf_handle);
120   assert(cf_handle != nullptr);
121   auto put = [&wbwi, &cf_handle](ROCKSDB_NAMESPACE::Slice key,
122                                  ROCKSDB_NAMESPACE::Slice value) {
123     return wbwi->Put(cf_handle, key, value);
124   };
125   std::unique_ptr<ROCKSDB_NAMESPACE::Status> status =
126       ROCKSDB_NAMESPACE::JniUtil::kv_op(put, env, jobj, jkey, jkey_len,
127                                         jentry_value, jentry_value_len);
128   if (status != nullptr && !status->ok()) {
129     ROCKSDB_NAMESPACE::RocksDBExceptionJni::ThrowNew(env, status);
130   }
131 }
132 
133 /*
134  * Class:     org_rocksdb_WriteBatchWithIndex
135  * Method:    putDirect
136  * Signature: (JLjava/nio/ByteBuffer;IILjava/nio/ByteBuffer;IIJ)V
137  */
Java_org_rocksdb_WriteBatchWithIndex_putDirect(JNIEnv * env,jobject,jlong jwb_handle,jobject jkey,jint jkey_offset,jint jkey_len,jobject jval,jint jval_offset,jint jval_len,jlong jcf_handle)138 void Java_org_rocksdb_WriteBatchWithIndex_putDirect(
139     JNIEnv* env, jobject /*jobj*/, jlong jwb_handle, jobject jkey,
140     jint jkey_offset, jint jkey_len, jobject jval, jint jval_offset,
141     jint jval_len, jlong jcf_handle) {
142   auto* wb = reinterpret_cast<ROCKSDB_NAMESPACE::WriteBatch*>(jwb_handle);
143   assert(wb != nullptr);
144   auto* cf_handle =
145       reinterpret_cast<ROCKSDB_NAMESPACE::ColumnFamilyHandle*>(jcf_handle);
146   auto put = [&wb, &cf_handle](ROCKSDB_NAMESPACE::Slice& key,
147                                ROCKSDB_NAMESPACE::Slice& value) {
148     if (cf_handle == nullptr) {
149       wb->Put(key, value);
150     } else {
151       wb->Put(cf_handle, key, value);
152     }
153   };
154   ROCKSDB_NAMESPACE::JniUtil::kv_op_direct(
155       put, env, jkey, jkey_offset, jkey_len, jval, jval_offset, jval_len);
156 }
157 
158 /*
159  * Class:     org_rocksdb_WriteBatchWithIndex
160  * Method:    merge
161  * Signature: (J[BI[BI)V
162  */
Java_org_rocksdb_WriteBatchWithIndex_merge__J_3BI_3BI(JNIEnv * env,jobject jobj,jlong jwbwi_handle,jbyteArray jkey,jint jkey_len,jbyteArray jentry_value,jint jentry_value_len)163 void Java_org_rocksdb_WriteBatchWithIndex_merge__J_3BI_3BI(
164     JNIEnv* env, jobject jobj, jlong jwbwi_handle, jbyteArray jkey,
165     jint jkey_len, jbyteArray jentry_value, jint jentry_value_len) {
166   auto* wbwi =
167       reinterpret_cast<ROCKSDB_NAMESPACE::WriteBatchWithIndex*>(jwbwi_handle);
168   assert(wbwi != nullptr);
169   auto merge = [&wbwi](ROCKSDB_NAMESPACE::Slice key,
170                        ROCKSDB_NAMESPACE::Slice value) {
171     return wbwi->Merge(key, value);
172   };
173   std::unique_ptr<ROCKSDB_NAMESPACE::Status> status =
174       ROCKSDB_NAMESPACE::JniUtil::kv_op(merge, env, jobj, jkey, jkey_len,
175                                         jentry_value, jentry_value_len);
176   if (status != nullptr && !status->ok()) {
177     ROCKSDB_NAMESPACE::RocksDBExceptionJni::ThrowNew(env, status);
178   }
179 }
180 
181 /*
182  * Class:     org_rocksdb_WriteBatchWithIndex
183  * Method:    merge
184  * Signature: (J[BI[BIJ)V
185  */
Java_org_rocksdb_WriteBatchWithIndex_merge__J_3BI_3BIJ(JNIEnv * env,jobject jobj,jlong jwbwi_handle,jbyteArray jkey,jint jkey_len,jbyteArray jentry_value,jint jentry_value_len,jlong jcf_handle)186 void Java_org_rocksdb_WriteBatchWithIndex_merge__J_3BI_3BIJ(
187     JNIEnv* env, jobject jobj, jlong jwbwi_handle, jbyteArray jkey,
188     jint jkey_len, jbyteArray jentry_value, jint jentry_value_len,
189     jlong jcf_handle) {
190   auto* wbwi =
191       reinterpret_cast<ROCKSDB_NAMESPACE::WriteBatchWithIndex*>(jwbwi_handle);
192   assert(wbwi != nullptr);
193   auto* cf_handle =
194       reinterpret_cast<ROCKSDB_NAMESPACE::ColumnFamilyHandle*>(jcf_handle);
195   assert(cf_handle != nullptr);
196   auto merge = [&wbwi, &cf_handle](ROCKSDB_NAMESPACE::Slice key,
197                                    ROCKSDB_NAMESPACE::Slice value) {
198     return wbwi->Merge(cf_handle, key, value);
199   };
200   std::unique_ptr<ROCKSDB_NAMESPACE::Status> status =
201       ROCKSDB_NAMESPACE::JniUtil::kv_op(merge, env, jobj, jkey, jkey_len,
202                                         jentry_value, jentry_value_len);
203   if (status != nullptr && !status->ok()) {
204     ROCKSDB_NAMESPACE::RocksDBExceptionJni::ThrowNew(env, status);
205   }
206 }
207 
208 /*
209  * Class:     org_rocksdb_WriteBatchWithIndex
210  * Method:    delete
211  * Signature: (J[BI)V
212  */
Java_org_rocksdb_WriteBatchWithIndex_delete__J_3BI(JNIEnv * env,jobject jobj,jlong jwbwi_handle,jbyteArray jkey,jint jkey_len)213 void Java_org_rocksdb_WriteBatchWithIndex_delete__J_3BI(JNIEnv* env,
214                                                         jobject jobj,
215                                                         jlong jwbwi_handle,
216                                                         jbyteArray jkey,
217                                                         jint jkey_len) {
218   auto* wbwi =
219       reinterpret_cast<ROCKSDB_NAMESPACE::WriteBatchWithIndex*>(jwbwi_handle);
220   assert(wbwi != nullptr);
221   auto remove = [&wbwi](ROCKSDB_NAMESPACE::Slice key) {
222     return wbwi->Delete(key);
223   };
224   std::unique_ptr<ROCKSDB_NAMESPACE::Status> status =
225       ROCKSDB_NAMESPACE::JniUtil::k_op(remove, env, jobj, jkey, jkey_len);
226   if (status != nullptr && !status->ok()) {
227     ROCKSDB_NAMESPACE::RocksDBExceptionJni::ThrowNew(env, status);
228   }
229 }
230 
231 /*
232  * Class:     org_rocksdb_WriteBatchWithIndex
233  * Method:    delete
234  * Signature: (J[BIJ)V
235  */
Java_org_rocksdb_WriteBatchWithIndex_delete__J_3BIJ(JNIEnv * env,jobject jobj,jlong jwbwi_handle,jbyteArray jkey,jint jkey_len,jlong jcf_handle)236 void Java_org_rocksdb_WriteBatchWithIndex_delete__J_3BIJ(
237     JNIEnv* env, jobject jobj, jlong jwbwi_handle, jbyteArray jkey,
238     jint jkey_len, jlong jcf_handle) {
239   auto* wbwi =
240       reinterpret_cast<ROCKSDB_NAMESPACE::WriteBatchWithIndex*>(jwbwi_handle);
241   assert(wbwi != nullptr);
242   auto* cf_handle =
243       reinterpret_cast<ROCKSDB_NAMESPACE::ColumnFamilyHandle*>(jcf_handle);
244   assert(cf_handle != nullptr);
245   auto remove = [&wbwi, &cf_handle](ROCKSDB_NAMESPACE::Slice key) {
246     return wbwi->Delete(cf_handle, key);
247   };
248   std::unique_ptr<ROCKSDB_NAMESPACE::Status> status =
249       ROCKSDB_NAMESPACE::JniUtil::k_op(remove, env, jobj, jkey, jkey_len);
250   if (status != nullptr && !status->ok()) {
251     ROCKSDB_NAMESPACE::RocksDBExceptionJni::ThrowNew(env, status);
252   }
253 }
254 
255 /*
256  * Class:     org_rocksdb_WriteBatchWithIndex
257  * Method:    singleDelete
258  * Signature: (J[BI)V
259  */
Java_org_rocksdb_WriteBatchWithIndex_singleDelete__J_3BI(JNIEnv * env,jobject jobj,jlong jwbwi_handle,jbyteArray jkey,jint jkey_len)260 void Java_org_rocksdb_WriteBatchWithIndex_singleDelete__J_3BI(
261     JNIEnv* env, jobject jobj, jlong jwbwi_handle, jbyteArray jkey,
262     jint jkey_len) {
263   auto* wbwi =
264       reinterpret_cast<ROCKSDB_NAMESPACE::WriteBatchWithIndex*>(jwbwi_handle);
265   assert(wbwi != nullptr);
266   auto single_delete = [&wbwi](ROCKSDB_NAMESPACE::Slice key) {
267     return wbwi->SingleDelete(key);
268   };
269   std::unique_ptr<ROCKSDB_NAMESPACE::Status> status =
270       ROCKSDB_NAMESPACE::JniUtil::k_op(single_delete, env, jobj, jkey,
271                                        jkey_len);
272   if (status != nullptr && !status->ok()) {
273     ROCKSDB_NAMESPACE::RocksDBExceptionJni::ThrowNew(env, status);
274   }
275 }
276 
277 /*
278  * Class:     org_rocksdb_WriteBatchWithIndex
279  * Method:    singleDelete
280  * Signature: (J[BIJ)V
281  */
Java_org_rocksdb_WriteBatchWithIndex_singleDelete__J_3BIJ(JNIEnv * env,jobject jobj,jlong jwbwi_handle,jbyteArray jkey,jint jkey_len,jlong jcf_handle)282 void Java_org_rocksdb_WriteBatchWithIndex_singleDelete__J_3BIJ(
283     JNIEnv* env, jobject jobj, jlong jwbwi_handle, jbyteArray jkey,
284     jint jkey_len, jlong jcf_handle) {
285   auto* wbwi =
286       reinterpret_cast<ROCKSDB_NAMESPACE::WriteBatchWithIndex*>(jwbwi_handle);
287   assert(wbwi != nullptr);
288   auto* cf_handle =
289       reinterpret_cast<ROCKSDB_NAMESPACE::ColumnFamilyHandle*>(jcf_handle);
290   assert(cf_handle != nullptr);
291   auto single_delete = [&wbwi, &cf_handle](ROCKSDB_NAMESPACE::Slice key) {
292     return wbwi->SingleDelete(cf_handle, key);
293   };
294   std::unique_ptr<ROCKSDB_NAMESPACE::Status> status =
295       ROCKSDB_NAMESPACE::JniUtil::k_op(single_delete, env, jobj, jkey,
296                                        jkey_len);
297   if (status != nullptr && !status->ok()) {
298     ROCKSDB_NAMESPACE::RocksDBExceptionJni::ThrowNew(env, status);
299   }
300 }
301 
302 /*
303  * Class:     org_rocksdb_WriteBatchWithIndex
304  * Method:    removeDirect
305  * Signature: (JLjava/nio/ByteBuffer;IIJ)V
306  */
Java_org_rocksdb_WriteBatchWithIndex_removeDirect(JNIEnv * env,jobject,jlong jwb_handle,jobject jkey,jint jkey_offset,jint jkey_len,jlong jcf_handle)307 void Java_org_rocksdb_WriteBatchWithIndex_removeDirect(
308     JNIEnv* env, jobject /*jobj*/, jlong jwb_handle, jobject jkey,
309     jint jkey_offset, jint jkey_len, jlong jcf_handle) {
310   auto* wb = reinterpret_cast<ROCKSDB_NAMESPACE::WriteBatch*>(jwb_handle);
311   assert(wb != nullptr);
312   auto* cf_handle =
313       reinterpret_cast<ROCKSDB_NAMESPACE::ColumnFamilyHandle*>(jcf_handle);
314   auto remove = [&wb, &cf_handle](ROCKSDB_NAMESPACE::Slice& key) {
315     if (cf_handle == nullptr) {
316       wb->Delete(key);
317     } else {
318       wb->Delete(cf_handle, key);
319     }
320   };
321   ROCKSDB_NAMESPACE::JniUtil::k_op_direct(remove, env, jkey, jkey_offset,
322                                           jkey_len);
323 }
324 
325 /*
326  * Class:     org_rocksdb_WriteBatchWithIndex
327  * Method:    deleteRange
328  * Signature: (J[BI[BI)V
329  */
Java_org_rocksdb_WriteBatchWithIndex_deleteRange__J_3BI_3BI(JNIEnv * env,jobject jobj,jlong jwbwi_handle,jbyteArray jbegin_key,jint jbegin_key_len,jbyteArray jend_key,jint jend_key_len)330 void Java_org_rocksdb_WriteBatchWithIndex_deleteRange__J_3BI_3BI(
331     JNIEnv* env, jobject jobj, jlong jwbwi_handle, jbyteArray jbegin_key,
332     jint jbegin_key_len, jbyteArray jend_key, jint jend_key_len) {
333   auto* wbwi =
334       reinterpret_cast<ROCKSDB_NAMESPACE::WriteBatchWithIndex*>(jwbwi_handle);
335   assert(wbwi != nullptr);
336   auto deleteRange = [&wbwi](ROCKSDB_NAMESPACE::Slice beginKey,
337                              ROCKSDB_NAMESPACE::Slice endKey) {
338     return wbwi->DeleteRange(beginKey, endKey);
339   };
340   std::unique_ptr<ROCKSDB_NAMESPACE::Status> status =
341       ROCKSDB_NAMESPACE::JniUtil::kv_op(deleteRange, env, jobj, jbegin_key,
342                                         jbegin_key_len, jend_key, jend_key_len);
343   if (status != nullptr && !status->ok()) {
344     ROCKSDB_NAMESPACE::RocksDBExceptionJni::ThrowNew(env, status);
345   }
346 }
347 
348 /*
349  * Class:     org_rocksdb_WriteBatchWithIndex
350  * Method:    deleteRange
351  * Signature: (J[BI[BIJ)V
352  */
Java_org_rocksdb_WriteBatchWithIndex_deleteRange__J_3BI_3BIJ(JNIEnv * env,jobject jobj,jlong jwbwi_handle,jbyteArray jbegin_key,jint jbegin_key_len,jbyteArray jend_key,jint jend_key_len,jlong jcf_handle)353 void Java_org_rocksdb_WriteBatchWithIndex_deleteRange__J_3BI_3BIJ(
354     JNIEnv* env, jobject jobj, jlong jwbwi_handle, jbyteArray jbegin_key,
355     jint jbegin_key_len, jbyteArray jend_key, jint jend_key_len,
356     jlong jcf_handle) {
357   auto* wbwi =
358       reinterpret_cast<ROCKSDB_NAMESPACE::WriteBatchWithIndex*>(jwbwi_handle);
359   assert(wbwi != nullptr);
360   auto* cf_handle =
361       reinterpret_cast<ROCKSDB_NAMESPACE::ColumnFamilyHandle*>(jcf_handle);
362   assert(cf_handle != nullptr);
363   auto deleteRange = [&wbwi, &cf_handle](ROCKSDB_NAMESPACE::Slice beginKey,
364                                          ROCKSDB_NAMESPACE::Slice endKey) {
365     return wbwi->DeleteRange(cf_handle, beginKey, endKey);
366   };
367   std::unique_ptr<ROCKSDB_NAMESPACE::Status> status =
368       ROCKSDB_NAMESPACE::JniUtil::kv_op(deleteRange, env, jobj, jbegin_key,
369                                         jbegin_key_len, jend_key, jend_key_len);
370   if (status != nullptr && !status->ok()) {
371     ROCKSDB_NAMESPACE::RocksDBExceptionJni::ThrowNew(env, status);
372   }
373 }
374 
375 /*
376  * Class:     org_rocksdb_WriteBatchWithIndex
377  * Method:    putLogData
378  * Signature: (J[BI)V
379  */
Java_org_rocksdb_WriteBatchWithIndex_putLogData(JNIEnv * env,jobject jobj,jlong jwbwi_handle,jbyteArray jblob,jint jblob_len)380 void Java_org_rocksdb_WriteBatchWithIndex_putLogData(JNIEnv* env, jobject jobj,
381                                                      jlong jwbwi_handle,
382                                                      jbyteArray jblob,
383                                                      jint jblob_len) {
384   auto* wbwi =
385       reinterpret_cast<ROCKSDB_NAMESPACE::WriteBatchWithIndex*>(jwbwi_handle);
386   assert(wbwi != nullptr);
387   auto putLogData = [&wbwi](ROCKSDB_NAMESPACE::Slice blob) {
388     return wbwi->PutLogData(blob);
389   };
390   std::unique_ptr<ROCKSDB_NAMESPACE::Status> status =
391       ROCKSDB_NAMESPACE::JniUtil::k_op(putLogData, env, jobj, jblob, jblob_len);
392   if (status != nullptr && !status->ok()) {
393     ROCKSDB_NAMESPACE::RocksDBExceptionJni::ThrowNew(env, status);
394   }
395 }
396 
397 /*
398  * Class:     org_rocksdb_WriteBatchWithIndex
399  * Method:    clear
400  * Signature: (J)V
401  */
Java_org_rocksdb_WriteBatchWithIndex_clear0(JNIEnv *,jobject,jlong jwbwi_handle)402 void Java_org_rocksdb_WriteBatchWithIndex_clear0(JNIEnv* /*env*/,
403                                                  jobject /*jobj*/,
404                                                  jlong jwbwi_handle) {
405   auto* wbwi =
406       reinterpret_cast<ROCKSDB_NAMESPACE::WriteBatchWithIndex*>(jwbwi_handle);
407   assert(wbwi != nullptr);
408 
409   wbwi->Clear();
410 }
411 
412 /*
413  * Class:     org_rocksdb_WriteBatchWithIndex
414  * Method:    setSavePoint0
415  * Signature: (J)V
416  */
Java_org_rocksdb_WriteBatchWithIndex_setSavePoint0(JNIEnv *,jobject,jlong jwbwi_handle)417 void Java_org_rocksdb_WriteBatchWithIndex_setSavePoint0(JNIEnv* /*env*/,
418                                                         jobject /*jobj*/,
419                                                         jlong jwbwi_handle) {
420   auto* wbwi =
421       reinterpret_cast<ROCKSDB_NAMESPACE::WriteBatchWithIndex*>(jwbwi_handle);
422   assert(wbwi != nullptr);
423 
424   wbwi->SetSavePoint();
425 }
426 
427 /*
428  * Class:     org_rocksdb_WriteBatchWithIndex
429  * Method:    rollbackToSavePoint0
430  * Signature: (J)V
431  */
Java_org_rocksdb_WriteBatchWithIndex_rollbackToSavePoint0(JNIEnv * env,jobject,jlong jwbwi_handle)432 void Java_org_rocksdb_WriteBatchWithIndex_rollbackToSavePoint0(
433     JNIEnv* env, jobject /*jobj*/, jlong jwbwi_handle) {
434   auto* wbwi =
435       reinterpret_cast<ROCKSDB_NAMESPACE::WriteBatchWithIndex*>(jwbwi_handle);
436   assert(wbwi != nullptr);
437 
438   auto s = wbwi->RollbackToSavePoint();
439 
440   if (s.ok()) {
441     return;
442   }
443 
444   ROCKSDB_NAMESPACE::RocksDBExceptionJni::ThrowNew(env, s);
445 }
446 
447 /*
448  * Class:     org_rocksdb_WriteBatchWithIndex
449  * Method:    popSavePoint
450  * Signature: (J)V
451  */
Java_org_rocksdb_WriteBatchWithIndex_popSavePoint(JNIEnv * env,jobject,jlong jwbwi_handle)452 void Java_org_rocksdb_WriteBatchWithIndex_popSavePoint(JNIEnv* env,
453                                                        jobject /*jobj*/,
454                                                        jlong jwbwi_handle) {
455   auto* wbwi =
456       reinterpret_cast<ROCKSDB_NAMESPACE::WriteBatchWithIndex*>(jwbwi_handle);
457   assert(wbwi != nullptr);
458 
459   auto s = wbwi->PopSavePoint();
460 
461   if (s.ok()) {
462     return;
463   }
464 
465   ROCKSDB_NAMESPACE::RocksDBExceptionJni::ThrowNew(env, s);
466 }
467 
468 /*
469  * Class:     org_rocksdb_WriteBatchWithIndex
470  * Method:    setMaxBytes
471  * Signature: (JJ)V
472  */
Java_org_rocksdb_WriteBatchWithIndex_setMaxBytes(JNIEnv *,jobject,jlong jwbwi_handle,jlong jmax_bytes)473 void Java_org_rocksdb_WriteBatchWithIndex_setMaxBytes(JNIEnv* /*env*/,
474                                                       jobject /*jobj*/,
475                                                       jlong jwbwi_handle,
476                                                       jlong jmax_bytes) {
477   auto* wbwi =
478       reinterpret_cast<ROCKSDB_NAMESPACE::WriteBatchWithIndex*>(jwbwi_handle);
479   assert(wbwi != nullptr);
480 
481   wbwi->SetMaxBytes(static_cast<size_t>(jmax_bytes));
482 }
483 
484 /*
485  * Class:     org_rocksdb_WriteBatchWithIndex
486  * Method:    getWriteBatch
487  * Signature: (J)Lorg/rocksdb/WriteBatch;
488  */
Java_org_rocksdb_WriteBatchWithIndex_getWriteBatch(JNIEnv * env,jobject,jlong jwbwi_handle)489 jobject Java_org_rocksdb_WriteBatchWithIndex_getWriteBatch(JNIEnv* env,
490                                                            jobject /*jobj*/,
491                                                            jlong jwbwi_handle) {
492   auto* wbwi =
493       reinterpret_cast<ROCKSDB_NAMESPACE::WriteBatchWithIndex*>(jwbwi_handle);
494   assert(wbwi != nullptr);
495 
496   auto* wb = wbwi->GetWriteBatch();
497 
498   // TODO(AR) is the `wb` object owned by us?
499   return ROCKSDB_NAMESPACE::WriteBatchJni::construct(env, wb);
500 }
501 
502 /*
503  * Class:     org_rocksdb_WriteBatchWithIndex
504  * Method:    iterator0
505  * Signature: (J)J
506  */
Java_org_rocksdb_WriteBatchWithIndex_iterator0(JNIEnv *,jobject,jlong jwbwi_handle)507 jlong Java_org_rocksdb_WriteBatchWithIndex_iterator0(JNIEnv* /*env*/,
508                                                      jobject /*jobj*/,
509                                                      jlong jwbwi_handle) {
510   auto* wbwi =
511       reinterpret_cast<ROCKSDB_NAMESPACE::WriteBatchWithIndex*>(jwbwi_handle);
512   auto* wbwi_iterator = wbwi->NewIterator();
513   return reinterpret_cast<jlong>(wbwi_iterator);
514 }
515 
516 /*
517  * Class:     org_rocksdb_WriteBatchWithIndex
518  * Method:    iterator1
519  * Signature: (JJ)J
520  */
Java_org_rocksdb_WriteBatchWithIndex_iterator1(JNIEnv *,jobject,jlong jwbwi_handle,jlong jcf_handle)521 jlong Java_org_rocksdb_WriteBatchWithIndex_iterator1(JNIEnv* /*env*/,
522                                                      jobject /*jobj*/,
523                                                      jlong jwbwi_handle,
524                                                      jlong jcf_handle) {
525   auto* wbwi =
526       reinterpret_cast<ROCKSDB_NAMESPACE::WriteBatchWithIndex*>(jwbwi_handle);
527   auto* cf_handle =
528       reinterpret_cast<ROCKSDB_NAMESPACE::ColumnFamilyHandle*>(jcf_handle);
529   auto* wbwi_iterator = wbwi->NewIterator(cf_handle);
530   return reinterpret_cast<jlong>(wbwi_iterator);
531 }
532 
533 /*
534  * Class:     org_rocksdb_WriteBatchWithIndex
535  * Method:    iteratorWithBase
536  * Signature: (JJJ)J
537  */
Java_org_rocksdb_WriteBatchWithIndex_iteratorWithBase(JNIEnv *,jobject,jlong jwbwi_handle,jlong jcf_handle,jlong jbi_handle)538 jlong Java_org_rocksdb_WriteBatchWithIndex_iteratorWithBase(JNIEnv* /*env*/,
539                                                             jobject /*jobj*/,
540                                                             jlong jwbwi_handle,
541                                                             jlong jcf_handle,
542                                                             jlong jbi_handle) {
543   auto* wbwi =
544       reinterpret_cast<ROCKSDB_NAMESPACE::WriteBatchWithIndex*>(jwbwi_handle);
545   auto* cf_handle =
546       reinterpret_cast<ROCKSDB_NAMESPACE::ColumnFamilyHandle*>(jcf_handle);
547   auto* base_iterator =
548       reinterpret_cast<ROCKSDB_NAMESPACE::Iterator*>(jbi_handle);
549   auto* iterator = wbwi->NewIteratorWithBase(cf_handle, base_iterator);
550   return reinterpret_cast<jlong>(iterator);
551 }
552 
553 /*
554  * Class:     org_rocksdb_WriteBatchWithIndex
555  * Method:    getFromBatch
556  * Signature: (JJ[BI)[B
557  */
Java_org_rocksdb_WriteBatchWithIndex_getFromBatch__JJ_3BI(JNIEnv * env,jobject,jlong jwbwi_handle,jlong jdbopt_handle,jbyteArray jkey,jint jkey_len)558 jbyteArray JNICALL Java_org_rocksdb_WriteBatchWithIndex_getFromBatch__JJ_3BI(
559     JNIEnv* env, jobject /*jobj*/, jlong jwbwi_handle, jlong jdbopt_handle,
560     jbyteArray jkey, jint jkey_len) {
561   auto* wbwi =
562       reinterpret_cast<ROCKSDB_NAMESPACE::WriteBatchWithIndex*>(jwbwi_handle);
563   auto* dbopt = reinterpret_cast<ROCKSDB_NAMESPACE::DBOptions*>(jdbopt_handle);
564 
565   auto getter = [&wbwi, &dbopt](const ROCKSDB_NAMESPACE::Slice& key,
566                                 std::string* value) {
567     return wbwi->GetFromBatch(*dbopt, key, value);
568   };
569 
570   return ROCKSDB_NAMESPACE::JniUtil::v_op(getter, env, jkey, jkey_len);
571 }
572 
573 /*
574  * Class:     org_rocksdb_WriteBatchWithIndex
575  * Method:    getFromBatch
576  * Signature: (JJ[BIJ)[B
577  */
Java_org_rocksdb_WriteBatchWithIndex_getFromBatch__JJ_3BIJ(JNIEnv * env,jobject,jlong jwbwi_handle,jlong jdbopt_handle,jbyteArray jkey,jint jkey_len,jlong jcf_handle)578 jbyteArray Java_org_rocksdb_WriteBatchWithIndex_getFromBatch__JJ_3BIJ(
579     JNIEnv* env, jobject /*jobj*/, jlong jwbwi_handle, jlong jdbopt_handle,
580     jbyteArray jkey, jint jkey_len, jlong jcf_handle) {
581   auto* wbwi =
582       reinterpret_cast<ROCKSDB_NAMESPACE::WriteBatchWithIndex*>(jwbwi_handle);
583   auto* dbopt = reinterpret_cast<ROCKSDB_NAMESPACE::DBOptions*>(jdbopt_handle);
584   auto* cf_handle =
585       reinterpret_cast<ROCKSDB_NAMESPACE::ColumnFamilyHandle*>(jcf_handle);
586 
587   auto getter = [&wbwi, &cf_handle, &dbopt](const ROCKSDB_NAMESPACE::Slice& key,
588                                             std::string* value) {
589     return wbwi->GetFromBatch(cf_handle, *dbopt, key, value);
590   };
591 
592   return ROCKSDB_NAMESPACE::JniUtil::v_op(getter, env, jkey, jkey_len);
593 }
594 
595 /*
596  * Class:     org_rocksdb_WriteBatchWithIndex
597  * Method:    getFromBatchAndDB
598  * Signature: (JJJ[BI)[B
599  */
Java_org_rocksdb_WriteBatchWithIndex_getFromBatchAndDB__JJJ_3BI(JNIEnv * env,jobject,jlong jwbwi_handle,jlong jdb_handle,jlong jreadopt_handle,jbyteArray jkey,jint jkey_len)600 jbyteArray Java_org_rocksdb_WriteBatchWithIndex_getFromBatchAndDB__JJJ_3BI(
601     JNIEnv* env, jobject /*jobj*/, jlong jwbwi_handle, jlong jdb_handle,
602     jlong jreadopt_handle, jbyteArray jkey, jint jkey_len) {
603   auto* wbwi =
604       reinterpret_cast<ROCKSDB_NAMESPACE::WriteBatchWithIndex*>(jwbwi_handle);
605   auto* db = reinterpret_cast<ROCKSDB_NAMESPACE::DB*>(jdb_handle);
606   auto* readopt =
607       reinterpret_cast<ROCKSDB_NAMESPACE::ReadOptions*>(jreadopt_handle);
608 
609   auto getter = [&wbwi, &db, &readopt](const ROCKSDB_NAMESPACE::Slice& key,
610                                        std::string* value) {
611     return wbwi->GetFromBatchAndDB(db, *readopt, key, value);
612   };
613 
614   return ROCKSDB_NAMESPACE::JniUtil::v_op(getter, env, jkey, jkey_len);
615 }
616 
617 /*
618  * Class:     org_rocksdb_WriteBatchWithIndex
619  * Method:    getFromBatchAndDB
620  * Signature: (JJJ[BIJ)[B
621  */
Java_org_rocksdb_WriteBatchWithIndex_getFromBatchAndDB__JJJ_3BIJ(JNIEnv * env,jobject,jlong jwbwi_handle,jlong jdb_handle,jlong jreadopt_handle,jbyteArray jkey,jint jkey_len,jlong jcf_handle)622 jbyteArray Java_org_rocksdb_WriteBatchWithIndex_getFromBatchAndDB__JJJ_3BIJ(
623     JNIEnv* env, jobject /*jobj*/, jlong jwbwi_handle, jlong jdb_handle,
624     jlong jreadopt_handle, jbyteArray jkey, jint jkey_len, jlong jcf_handle) {
625   auto* wbwi =
626       reinterpret_cast<ROCKSDB_NAMESPACE::WriteBatchWithIndex*>(jwbwi_handle);
627   auto* db = reinterpret_cast<ROCKSDB_NAMESPACE::DB*>(jdb_handle);
628   auto* readopt =
629       reinterpret_cast<ROCKSDB_NAMESPACE::ReadOptions*>(jreadopt_handle);
630   auto* cf_handle =
631       reinterpret_cast<ROCKSDB_NAMESPACE::ColumnFamilyHandle*>(jcf_handle);
632 
633   auto getter = [&wbwi, &db, &cf_handle, &readopt](
634                     const ROCKSDB_NAMESPACE::Slice& key, std::string* value) {
635     return wbwi->GetFromBatchAndDB(db, *readopt, cf_handle, key, value);
636   };
637 
638   return ROCKSDB_NAMESPACE::JniUtil::v_op(getter, env, jkey, jkey_len);
639 }
640 
641 /*
642  * Class:     org_rocksdb_WriteBatchWithIndex
643  * Method:    disposeInternal
644  * Signature: (J)V
645  */
Java_org_rocksdb_WriteBatchWithIndex_disposeInternal(JNIEnv *,jobject,jlong handle)646 void Java_org_rocksdb_WriteBatchWithIndex_disposeInternal(JNIEnv* /*env*/,
647                                                           jobject /*jobj*/,
648                                                           jlong handle) {
649   auto* wbwi =
650       reinterpret_cast<ROCKSDB_NAMESPACE::WriteBatchWithIndex*>(handle);
651   assert(wbwi != nullptr);
652   delete wbwi;
653 }
654 
655 /* WBWIRocksIterator below */
656 
657 /*
658  * Class:     org_rocksdb_WBWIRocksIterator
659  * Method:    disposeInternal
660  * Signature: (J)V
661  */
Java_org_rocksdb_WBWIRocksIterator_disposeInternal(JNIEnv *,jobject,jlong handle)662 void Java_org_rocksdb_WBWIRocksIterator_disposeInternal(JNIEnv* /*env*/,
663                                                         jobject /*jobj*/,
664                                                         jlong handle) {
665   auto* it = reinterpret_cast<ROCKSDB_NAMESPACE::WBWIIterator*>(handle);
666   assert(it != nullptr);
667   delete it;
668 }
669 
670 /*
671  * Class:     org_rocksdb_WBWIRocksIterator
672  * Method:    isValid0
673  * Signature: (J)Z
674  */
Java_org_rocksdb_WBWIRocksIterator_isValid0(JNIEnv *,jobject,jlong handle)675 jboolean Java_org_rocksdb_WBWIRocksIterator_isValid0(JNIEnv* /*env*/,
676                                                      jobject /*jobj*/,
677                                                      jlong handle) {
678   return reinterpret_cast<ROCKSDB_NAMESPACE::WBWIIterator*>(handle)->Valid();
679 }
680 
681 /*
682  * Class:     org_rocksdb_WBWIRocksIterator
683  * Method:    seekToFirst0
684  * Signature: (J)V
685  */
Java_org_rocksdb_WBWIRocksIterator_seekToFirst0(JNIEnv *,jobject,jlong handle)686 void Java_org_rocksdb_WBWIRocksIterator_seekToFirst0(JNIEnv* /*env*/,
687                                                      jobject /*jobj*/,
688                                                      jlong handle) {
689   reinterpret_cast<ROCKSDB_NAMESPACE::WBWIIterator*>(handle)->SeekToFirst();
690 }
691 
692 /*
693  * Class:     org_rocksdb_WBWIRocksIterator
694  * Method:    seekToLast0
695  * Signature: (J)V
696  */
Java_org_rocksdb_WBWIRocksIterator_seekToLast0(JNIEnv *,jobject,jlong handle)697 void Java_org_rocksdb_WBWIRocksIterator_seekToLast0(JNIEnv* /*env*/,
698                                                     jobject /*jobj*/,
699                                                     jlong handle) {
700   reinterpret_cast<ROCKSDB_NAMESPACE::WBWIIterator*>(handle)->SeekToLast();
701 }
702 
703 /*
704  * Class:     org_rocksdb_WBWIRocksIterator
705  * Method:    next0
706  * Signature: (J)V
707  */
Java_org_rocksdb_WBWIRocksIterator_next0(JNIEnv *,jobject,jlong handle)708 void Java_org_rocksdb_WBWIRocksIterator_next0(JNIEnv* /*env*/, jobject /*jobj*/,
709                                               jlong handle) {
710   reinterpret_cast<ROCKSDB_NAMESPACE::WBWIIterator*>(handle)->Next();
711 }
712 
713 /*
714  * Class:     org_rocksdb_WBWIRocksIterator
715  * Method:    prev0
716  * Signature: (J)V
717  */
Java_org_rocksdb_WBWIRocksIterator_prev0(JNIEnv *,jobject,jlong handle)718 void Java_org_rocksdb_WBWIRocksIterator_prev0(JNIEnv* /*env*/, jobject /*jobj*/,
719                                               jlong handle) {
720   reinterpret_cast<ROCKSDB_NAMESPACE::WBWIIterator*>(handle)->Prev();
721 }
722 
723 /*
724  * Class:     org_rocksdb_WBWIRocksIterator
725  * Method:    seek0
726  * Signature: (J[BI)V
727  */
Java_org_rocksdb_WBWIRocksIterator_seek0(JNIEnv * env,jobject,jlong handle,jbyteArray jtarget,jint jtarget_len)728 void Java_org_rocksdb_WBWIRocksIterator_seek0(JNIEnv* env, jobject /*jobj*/,
729                                               jlong handle, jbyteArray jtarget,
730                                               jint jtarget_len) {
731   auto* it = reinterpret_cast<ROCKSDB_NAMESPACE::WBWIIterator*>(handle);
732   jbyte* target = env->GetByteArrayElements(jtarget, nullptr);
733   if (target == nullptr) {
734     // exception thrown: OutOfMemoryError
735     return;
736   }
737 
738   ROCKSDB_NAMESPACE::Slice target_slice(reinterpret_cast<char*>(target),
739                                         jtarget_len);
740 
741   it->Seek(target_slice);
742 
743   env->ReleaseByteArrayElements(jtarget, target, JNI_ABORT);
744 }
745 
746 /*
747  * Class:     org_rocksdb_WBWIRocksIterator
748  * Method:    seekDirect0
749  * Signature: (JLjava/nio/ByteBuffer;II)V
750  */
Java_org_rocksdb_WBWIRocksIterator_seekDirect0(JNIEnv * env,jobject,jlong handle,jobject jtarget,jint jtarget_off,jint jtarget_len)751 void Java_org_rocksdb_WBWIRocksIterator_seekDirect0(
752     JNIEnv* env, jobject /*jobj*/, jlong handle, jobject jtarget,
753     jint jtarget_off, jint jtarget_len) {
754   auto* it = reinterpret_cast<ROCKSDB_NAMESPACE::WBWIIterator*>(handle);
755   auto seek = [&it](ROCKSDB_NAMESPACE::Slice& target_slice) {
756     it->Seek(target_slice);
757   };
758   ROCKSDB_NAMESPACE::JniUtil::k_op_direct(seek, env, jtarget, jtarget_off,
759                                           jtarget_len);
760 }
761 
762 /*
763  * Class:     org_rocksdb_WBWIRocksIterator
764  * Method:    seekForPrev0
765  * Signature: (J[BI)V
766  */
Java_org_rocksdb_WBWIRocksIterator_seekForPrev0(JNIEnv * env,jobject,jlong handle,jbyteArray jtarget,jint jtarget_len)767 void Java_org_rocksdb_WBWIRocksIterator_seekForPrev0(JNIEnv* env,
768                                                      jobject /*jobj*/,
769                                                      jlong handle,
770                                                      jbyteArray jtarget,
771                                                      jint jtarget_len) {
772   auto* it = reinterpret_cast<ROCKSDB_NAMESPACE::WBWIIterator*>(handle);
773   jbyte* target = env->GetByteArrayElements(jtarget, nullptr);
774   if (target == nullptr) {
775     // exception thrown: OutOfMemoryError
776     return;
777   }
778 
779   ROCKSDB_NAMESPACE::Slice target_slice(reinterpret_cast<char*>(target),
780                                         jtarget_len);
781 
782   it->SeekForPrev(target_slice);
783 
784   env->ReleaseByteArrayElements(jtarget, target, JNI_ABORT);
785 }
786 
787 /*
788  * Class:     org_rocksdb_WBWIRocksIterator
789  * Method:    status0
790  * Signature: (J)V
791  */
Java_org_rocksdb_WBWIRocksIterator_status0(JNIEnv * env,jobject,jlong handle)792 void Java_org_rocksdb_WBWIRocksIterator_status0(JNIEnv* env, jobject /*jobj*/,
793                                                 jlong handle) {
794   auto* it = reinterpret_cast<ROCKSDB_NAMESPACE::WBWIIterator*>(handle);
795   ROCKSDB_NAMESPACE::Status s = it->status();
796 
797   if (s.ok()) {
798     return;
799   }
800 
801   ROCKSDB_NAMESPACE::RocksDBExceptionJni::ThrowNew(env, s);
802 }
803 
804 /*
805  * Class:     org_rocksdb_WBWIRocksIterator
806  * Method:    entry1
807  * Signature: (J)[J
808  */
Java_org_rocksdb_WBWIRocksIterator_entry1(JNIEnv * env,jobject,jlong handle)809 jlongArray Java_org_rocksdb_WBWIRocksIterator_entry1(JNIEnv* env,
810                                                      jobject /*jobj*/,
811                                                      jlong handle) {
812   auto* it = reinterpret_cast<ROCKSDB_NAMESPACE::WBWIIterator*>(handle);
813   const ROCKSDB_NAMESPACE::WriteEntry& we = it->Entry();
814 
815   jlong results[3];
816 
817   // set the type of the write entry
818   results[0] = ROCKSDB_NAMESPACE::WriteTypeJni::toJavaWriteType(we.type);
819 
820   // NOTE: key_slice and value_slice will be freed by
821   // org.rocksdb.DirectSlice#close
822 
823   auto* key_slice = new ROCKSDB_NAMESPACE::Slice(we.key.data(), we.key.size());
824   results[1] = reinterpret_cast<jlong>(key_slice);
825   if (we.type == ROCKSDB_NAMESPACE::kDeleteRecord ||
826       we.type == ROCKSDB_NAMESPACE::kSingleDeleteRecord ||
827       we.type == ROCKSDB_NAMESPACE::kLogDataRecord) {
828     // set native handle of value slice to null if no value available
829     results[2] = 0;
830   } else {
831     auto* value_slice =
832         new ROCKSDB_NAMESPACE::Slice(we.value.data(), we.value.size());
833     results[2] = reinterpret_cast<jlong>(value_slice);
834   }
835 
836   jlongArray jresults = env->NewLongArray(3);
837   if (jresults == nullptr) {
838     // exception thrown: OutOfMemoryError
839     if (results[2] != 0) {
840       auto* value_slice =
841           reinterpret_cast<ROCKSDB_NAMESPACE::Slice*>(results[2]);
842       delete value_slice;
843     }
844     delete key_slice;
845     return nullptr;
846   }
847 
848   env->SetLongArrayRegion(jresults, 0, 3, results);
849   if (env->ExceptionCheck()) {
850     // exception thrown: ArrayIndexOutOfBoundsException
851     env->DeleteLocalRef(jresults);
852     if (results[2] != 0) {
853       auto* value_slice =
854           reinterpret_cast<ROCKSDB_NAMESPACE::Slice*>(results[2]);
855       delete value_slice;
856     }
857     delete key_slice;
858     return nullptr;
859   }
860 
861   return jresults;
862 }
863