trace_writer_jnicallback.cc 3.5 KB

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