persistent_cache.cc 1.9 KB

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