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).
6 // This file implements the "bridge" between Java and C++ and enables
7 // calling c++ rocksdb::TtlDB methods.
17 #include "include/org_rocksdb_TtlDB.h"
18 #include "rocksdb/utilities/db_ttl.h"
19 #include "rocksjni/portal.h"
22 * Class: org_rocksdb_TtlDB
24 * Signature: (JLjava/lang/String;IZ)J
26 jlong
Java_org_rocksdb_TtlDB_open(JNIEnv
* env
, jclass
/*jcls*/,
27 jlong joptions_handle
, jstring jdb_path
,
28 jint jttl
, jboolean jread_only
) {
29 const char* db_path
= env
->GetStringUTFChars(jdb_path
, nullptr);
30 if (db_path
== nullptr) {
31 // exception thrown: OutOfMemoryError
35 auto* opt
= reinterpret_cast<rocksdb::Options
*>(joptions_handle
);
36 rocksdb::DBWithTTL
* db
= nullptr;
38 rocksdb::DBWithTTL::Open(*opt
, db_path
, &db
, jttl
, jread_only
);
39 env
->ReleaseStringUTFChars(jdb_path
, db_path
);
41 // as TTLDB extends RocksDB on the java side, we can reuse
42 // the RocksDB portal here.
44 return reinterpret_cast<jlong
>(db
);
46 rocksdb::RocksDBExceptionJni::ThrowNew(env
, s
);
52 * Class: org_rocksdb_TtlDB
54 * Signature: (JLjava/lang/String;[[B[J[IZ)[J
56 jlongArray
Java_org_rocksdb_TtlDB_openCF(JNIEnv
* env
, jclass
/*jcls*/,
57 jlong jopt_handle
, jstring jdb_path
,
58 jobjectArray jcolumn_names
,
59 jlongArray jcolumn_options
,
60 jintArray jttls
, jboolean jread_only
) {
61 const char* db_path
= env
->GetStringUTFChars(jdb_path
, nullptr);
62 if (db_path
== nullptr) {
63 // exception thrown: OutOfMemoryError
67 const jsize len_cols
= env
->GetArrayLength(jcolumn_names
);
68 jlong
* jco
= env
->GetLongArrayElements(jcolumn_options
, nullptr);
70 // exception thrown: OutOfMemoryError
71 env
->ReleaseStringUTFChars(jdb_path
, db_path
);
75 std::vector
<rocksdb::ColumnFamilyDescriptor
> column_families
;
76 jboolean has_exception
= JNI_FALSE
;
77 rocksdb::JniUtil::byteStrings
<std::string
>(
79 [](const char* str_data
, const size_t str_len
) {
80 return std::string(str_data
, str_len
);
82 [&jco
, &column_families
](size_t idx
, std::string cf_name
) {
83 rocksdb::ColumnFamilyOptions
* cf_options
=
84 reinterpret_cast<rocksdb::ColumnFamilyOptions
*>(jco
[idx
]);
85 column_families
.push_back(
86 rocksdb::ColumnFamilyDescriptor(cf_name
, *cf_options
));
90 env
->ReleaseLongArrayElements(jcolumn_options
, jco
, JNI_ABORT
);
92 if (has_exception
== JNI_TRUE
) {
94 env
->ReleaseStringUTFChars(jdb_path
, db_path
);
98 std::vector
<int32_t> ttl_values
;
99 jint
* jttlv
= env
->GetIntArrayElements(jttls
, nullptr);
100 if (jttlv
== nullptr) {
101 // exception thrown: OutOfMemoryError
102 env
->ReleaseStringUTFChars(jdb_path
, db_path
);
105 const jsize len_ttls
= env
->GetArrayLength(jttls
);
106 for (jsize i
= 0; i
< len_ttls
; i
++) {
107 ttl_values
.push_back(jttlv
[i
]);
109 env
->ReleaseIntArrayElements(jttls
, jttlv
, JNI_ABORT
);
111 auto* opt
= reinterpret_cast<rocksdb::DBOptions
*>(jopt_handle
);
112 std::vector
<rocksdb::ColumnFamilyHandle
*> handles
;
113 rocksdb::DBWithTTL
* db
= nullptr;
114 rocksdb::Status s
= rocksdb::DBWithTTL::Open(
115 *opt
, db_path
, column_families
, &handles
, &db
, ttl_values
, jread_only
);
117 // we have now finished with db_path
118 env
->ReleaseStringUTFChars(jdb_path
, db_path
);
120 // check if open operation was successful
122 const jsize resultsLen
= 1 + len_cols
; // db handle + column family handles
123 std::unique_ptr
<jlong
[]> results
=
124 std::unique_ptr
<jlong
[]>(new jlong
[resultsLen
]);
125 results
[0] = reinterpret_cast<jlong
>(db
);
126 for (int i
= 1; i
<= len_cols
; i
++) {
127 results
[i
] = reinterpret_cast<jlong
>(handles
[i
- 1]);
130 jlongArray jresults
= env
->NewLongArray(resultsLen
);
131 if (jresults
== nullptr) {
132 // exception thrown: OutOfMemoryError
136 env
->SetLongArrayRegion(jresults
, 0, resultsLen
, results
.get());
137 if (env
->ExceptionCheck()) {
138 // exception thrown: ArrayIndexOutOfBoundsException
139 env
->DeleteLocalRef(jresults
);
145 rocksdb::RocksDBExceptionJni::ThrowNew(env
, s
);
151 * Class: org_rocksdb_TtlDB
152 * Method: createColumnFamilyWithTtl
153 * Signature: (JLorg/rocksdb/ColumnFamilyDescriptor;[BJI)J;
155 jlong
Java_org_rocksdb_TtlDB_createColumnFamilyWithTtl(
156 JNIEnv
* env
, jobject
/*jobj*/, jlong jdb_handle
, jbyteArray jcolumn_name
,
157 jlong jcolumn_options
, jint jttl
) {
158 jbyte
* cfname
= env
->GetByteArrayElements(jcolumn_name
, nullptr);
159 if (cfname
== nullptr) {
160 // exception thrown: OutOfMemoryError
163 const jsize len
= env
->GetArrayLength(jcolumn_name
);
166 reinterpret_cast<rocksdb::ColumnFamilyOptions
*>(jcolumn_options
);
168 auto* db_handle
= reinterpret_cast<rocksdb::DBWithTTL
*>(jdb_handle
);
169 rocksdb::ColumnFamilyHandle
* handle
;
170 rocksdb::Status s
= db_handle
->CreateColumnFamilyWithTtl(
171 *cfOptions
, std::string(reinterpret_cast<char*>(cfname
), len
), &handle
,
174 env
->ReleaseByteArrayElements(jcolumn_name
, cfname
, 0);
177 return reinterpret_cast<jlong
>(handle
);
179 rocksdb::RocksDBExceptionJni::ThrowNew(env
, s
);