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