trace_writer_jnicallback.cc 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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_NAMESPACE::TraceWriter.
  8. #include "rocksjni/trace_writer_jnicallback.h"
  9. #include "rocksjni/portal.h"
  10. namespace ROCKSDB_NAMESPACE {
  11. TraceWriterJniCallback::TraceWriterJniCallback(JNIEnv* env,
  12. jobject jtrace_writer)
  13. : JniCallback(env, jtrace_writer) {
  14. m_jwrite_proxy_methodid = AbstractTraceWriterJni::getWriteProxyMethodId(env);
  15. if (m_jwrite_proxy_methodid == nullptr) {
  16. // exception thrown: NoSuchMethodException or OutOfMemoryError
  17. return;
  18. }
  19. m_jclose_writer_proxy_methodid =
  20. AbstractTraceWriterJni::getCloseWriterProxyMethodId(env);
  21. if (m_jclose_writer_proxy_methodid == nullptr) {
  22. // exception thrown: NoSuchMethodException or OutOfMemoryError
  23. return;
  24. }
  25. m_jget_file_size_methodid =
  26. AbstractTraceWriterJni::getGetFileSizeMethodId(env);
  27. if (m_jget_file_size_methodid == nullptr) {
  28. // exception thrown: NoSuchMethodException or OutOfMemoryError
  29. return;
  30. }
  31. }
  32. Status TraceWriterJniCallback::Write(const Slice& data) {
  33. jboolean attached_thread = JNI_FALSE;
  34. JNIEnv* env = getJniEnv(&attached_thread);
  35. if (env == nullptr) {
  36. return Status::IOError("Unable to attach JNI Environment");
  37. }
  38. jshort jstatus =
  39. env->CallShortMethod(m_jcallback_obj, m_jwrite_proxy_methodid, &data);
  40. if (env->ExceptionCheck()) {
  41. // exception thrown from CallShortMethod
  42. env->ExceptionDescribe(); // print out exception to stderr
  43. releaseJniEnv(attached_thread);
  44. return Status::IOError(
  45. "Unable to call AbstractTraceWriter#writeProxy(long)");
  46. }
  47. // unpack status code and status sub-code from jstatus
  48. jbyte jcode_value = (jstatus >> 8) & 0xFF;
  49. jbyte jsub_code_value = jstatus & 0xFF;
  50. std::unique_ptr<Status> s =
  51. StatusJni::toCppStatus(jcode_value, jsub_code_value);
  52. releaseJniEnv(attached_thread);
  53. return Status(*s);
  54. }
  55. Status TraceWriterJniCallback::Close() {
  56. jboolean attached_thread = JNI_FALSE;
  57. JNIEnv* env = getJniEnv(&attached_thread);
  58. if (env == nullptr) {
  59. return Status::IOError("Unable to attach JNI Environment");
  60. }
  61. jshort jstatus =
  62. env->CallShortMethod(m_jcallback_obj, m_jclose_writer_proxy_methodid);
  63. if (env->ExceptionCheck()) {
  64. // exception thrown from CallShortMethod
  65. env->ExceptionDescribe(); // print out exception to stderr
  66. releaseJniEnv(attached_thread);
  67. return Status::IOError(
  68. "Unable to call AbstractTraceWriter#closeWriterProxy()");
  69. }
  70. // unpack status code and status sub-code from jstatus
  71. jbyte code_value = (jstatus >> 8) & 0xFF;
  72. jbyte sub_code_value = jstatus & 0xFF;
  73. std::unique_ptr<Status> s =
  74. StatusJni::toCppStatus(code_value, sub_code_value);
  75. releaseJniEnv(attached_thread);
  76. return Status(*s);
  77. }
  78. uint64_t TraceWriterJniCallback::GetFileSize() {
  79. jboolean attached_thread = JNI_FALSE;
  80. JNIEnv* env = getJniEnv(&attached_thread);
  81. if (env == nullptr) {
  82. return 0;
  83. }
  84. jlong jfile_size =
  85. env->CallLongMethod(m_jcallback_obj, m_jget_file_size_methodid);
  86. if (env->ExceptionCheck()) {
  87. // exception thrown from CallLongMethod
  88. env->ExceptionDescribe(); // print out exception to stderr
  89. releaseJniEnv(attached_thread);
  90. return 0;
  91. }
  92. releaseJniEnv(attached_thread);
  93. return static_cast<uint64_t>(jfile_size);
  94. }
  95. } // namespace ROCKSDB_NAMESPACE