persistent_cache.cc 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  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++ for
  7. // ROCKSDB_NAMESPACE::PersistentCache.
  8. #include "rocksdb/persistent_cache.h"
  9. #include <jni.h>
  10. #include <string>
  11. #include "include/org_rocksdb_PersistentCache.h"
  12. #include "loggerjnicallback.h"
  13. #include "portal.h"
  14. #include "rocksjni/cplusplus_to_java_convert.h"
  15. /*
  16. * Class: org_rocksdb_PersistentCache
  17. * Method: newPersistentCache
  18. * Signature: (JLjava/lang/String;JJZ)J
  19. */
  20. jlong Java_org_rocksdb_PersistentCache_newPersistentCache(
  21. JNIEnv* env, jclass, jlong jenv_handle, jstring jpath, jlong jsz,
  22. jlong jlogger_handle, jboolean joptimized_for_nvm) {
  23. auto* rocks_env = reinterpret_cast<ROCKSDB_NAMESPACE::Env*>(jenv_handle);
  24. jboolean has_exception = JNI_FALSE;
  25. std::string path =
  26. ROCKSDB_NAMESPACE::JniUtil::copyStdString(env, jpath, &has_exception);
  27. if (has_exception == JNI_TRUE) {
  28. return 0;
  29. }
  30. auto* logger =
  31. reinterpret_cast<std::shared_ptr<ROCKSDB_NAMESPACE::LoggerJniCallback>*>(
  32. jlogger_handle);
  33. auto* cache =
  34. new std::shared_ptr<ROCKSDB_NAMESPACE::PersistentCache>(nullptr);
  35. ROCKSDB_NAMESPACE::Status s = ROCKSDB_NAMESPACE::NewPersistentCache(
  36. rocks_env, path, static_cast<uint64_t>(jsz), *logger,
  37. static_cast<bool>(joptimized_for_nvm), cache);
  38. if (!s.ok()) {
  39. ROCKSDB_NAMESPACE::RocksDBExceptionJni::ThrowNew(env, s);
  40. }
  41. return GET_CPLUSPLUS_POINTER(cache);
  42. }
  43. /*
  44. * Class: org_rocksdb_PersistentCache
  45. * Method: disposeInternal
  46. * Signature: (J)V
  47. */
  48. void Java_org_rocksdb_PersistentCache_disposeInternalJni(JNIEnv*, jclass,
  49. jlong jhandle) {
  50. auto* cache =
  51. reinterpret_cast<std::shared_ptr<ROCKSDB_NAMESPACE::PersistentCache>*>(
  52. jhandle);
  53. delete cache; // delete std::shared_ptr
  54. }