jni_multiget_helpers.h 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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. // Copyright (c) 2011 The LevelDB Authors. All rights reserved.
  6. // Use of this source code is governed by a BSD-style license that can be
  7. // found in the LICENSE file. See the AUTHORS file for names of contributors.
  8. #pragma once
  9. #include <jni.h>
  10. #include <functional>
  11. #include "rocksdb/convenience.h"
  12. #include "rocksdb/db.h"
  13. namespace ROCKSDB_NAMESPACE {
  14. /**
  15. * @brief Encapsulate keys and key conversions from Java/JNI objects for
  16. * MultiGet
  17. *
  18. */
  19. class MultiGetJNIKeys {
  20. private:
  21. std::vector<ROCKSDB_NAMESPACE::Slice> slices_;
  22. std::vector<std::unique_ptr<jbyte[]>> key_bufs;
  23. public:
  24. /**
  25. * @brief Construct helper multiget keys object from array of java keys
  26. *
  27. * @param env JNI environment
  28. * @param jkeys Array of `byte[]`, each of which contains a key
  29. * @param jkey_offs array of offsets into keys, at which each key starts
  30. * @param jkey_lens array of key lengths
  31. * @return true if the keys were copied successfully from the parameters
  32. * @return false if a Java exception was raised (memory problem, or array
  33. * indexing problem)
  34. */
  35. bool fromByteArrays(JNIEnv* env, jobjectArray jkeys, jintArray jkey_offs,
  36. jintArray jkey_lens);
  37. /**
  38. * @brief Construct helper multiget keys object from array of java keys
  39. *
  40. * @param env env JNI environment
  41. * @param jkeys jkeys Array of byte[], each of which is a key
  42. * @return true if the keys were copied successfully from the parameters
  43. * @return false if a Java exception was raised (memory problem, or array
  44. * indexing problem)
  45. */
  46. bool fromByteArrays(JNIEnv* env, jobjectArray jkeys);
  47. /**
  48. * @brief Construct helper multiget keys object from array of java ByteBuffers
  49. *
  50. * @param env JNI environment
  51. * @param jkeys Array of `java.nio.ByteBuffer`, each of which contains a key
  52. * @param jkey_offs array of offsets into buffers, at which each key starts
  53. * @param jkey_lens array of key lengths
  54. * @return `true` if the keys were copied successfully from the parameters
  55. * @return `false` if a Java exception was raised (memory problem, or array
  56. * indexing problem)
  57. */
  58. bool fromByteBuffers(JNIEnv* env, jobjectArray jkeys, jintArray jkey_offs,
  59. jintArray jkey_lens);
  60. /**
  61. * @brief Used when the keys need to be passed to a RocksDB function which
  62. * takes keys as an array of slice pointers
  63. *
  64. * @return ROCKSDB_NAMESPACE::Slice* an array of slices, the n-th slice
  65. * contains the n-th key created by `fromByteArrays()` or `fromByteBuffers()`
  66. */
  67. ROCKSDB_NAMESPACE::Slice* data();
  68. /**
  69. * @brief Used when the keys need to be passed to a RocksDB function which
  70. * takes keys as a vector of slices
  71. *
  72. * @return std::vector<ROCKSDB_NAMESPACE::Slice>& a vector of slices, the n-th
  73. * slice contains the n-th key created by `fromByteArrays()` or
  74. * `fromByteBuffers()`
  75. */
  76. inline std::vector<ROCKSDB_NAMESPACE::Slice>& slices() { return slices_; }
  77. /**
  78. * @brief
  79. *
  80. * @return std::vector<ROCKSDB_NAMESPACE::Slice>::size_type the number of keys
  81. * in this object
  82. */
  83. std::vector<ROCKSDB_NAMESPACE::Slice>::size_type size();
  84. };
  85. /**
  86. * @brief Class with static helpers for returning java objects from RocksDB data
  87. * returned by MultiGet
  88. *
  89. */
  90. class MultiGetJNIValues {
  91. public:
  92. /**
  93. * @brief create an array of `byte[]` containing the result values from
  94. * `MultiGet`
  95. *
  96. * @tparam TValue a `std::string` or a `PinnableSlice` containing the result
  97. * for a single key
  98. * @return jobjectArray an array of `byte[]`, one per value in the input
  99. * vector
  100. */
  101. template <class TValue>
  102. static jobjectArray byteArrays(JNIEnv*, std::vector<TValue>&,
  103. std::vector<ROCKSDB_NAMESPACE::Status>&);
  104. /**
  105. * @brief fill a supplied array of `byte[]` with the result values from
  106. * `MultiGet`
  107. *
  108. * @tparam TValue a `std::string` or a `PinnableSlice` containing the result
  109. * for a single key
  110. * @param jvalues the array of `byte[]` to instantiate
  111. * @param jvalue_sizes the offsets at which to place the results in `jvalues`
  112. * @param jstatuses the status for every individual key/value get
  113. */
  114. template <class TValue>
  115. static void fillByteBuffersAndStatusObjects(
  116. JNIEnv*, std::vector<TValue>&, std::vector<ROCKSDB_NAMESPACE::Status>&,
  117. jobjectArray jvalues, jintArray jvalue_sizes, jobjectArray jstatuses);
  118. };
  119. /**
  120. * @brief class with static helper for arrays of column family handles
  121. *
  122. */
  123. class ColumnFamilyJNIHelpers {
  124. public:
  125. /**
  126. * @brief create a native array of cf handles from java handles
  127. *
  128. * @param env
  129. * @param jcolumn_family_handles
  130. * @return unique ptr to vector of handles on success, reset() unique ptr on
  131. * failure (and a JNI exception will be generated)
  132. */
  133. static std::unique_ptr<std::vector<ROCKSDB_NAMESPACE::ColumnFamilyHandle*>>
  134. handlesFromJLongArray(JNIEnv* env, jlongArray jcolumn_family_handles);
  135. /**
  136. * @brief create a column family handle from a raw pointer, or raise an
  137. * appropriate JNI exception
  138. *
  139. * @param env
  140. * @param jcolumn_family_handle the raw pointer to convert
  141. * @return ROCKSDB_NAMESPACE::ColumnFamilyHandle* or raises a java exception
  142. */
  143. static ROCKSDB_NAMESPACE::ColumnFamilyHandle* handleFromJLong(
  144. JNIEnv* env, jlong jcolumn_family_handle);
  145. };
  146. }; // namespace ROCKSDB_NAMESPACE