write_batch_with_index.cc 30 KB

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