From ca47da9e6338f76b7fc1e1345d798cd2199487f2 Mon Sep 17 00:00:00 2001 From: fyrz Date: Mon, 17 Nov 2014 19:22:44 +0100 Subject: [PATCH] [RocksJava] TTL-Support --- java/Makefile | 4 +- java/org/rocksdb/TtlDB.java | 75 ++++++++++++++++++++++++++++ java/org/rocksdb/test/TtlDBTest.java | 51 +++++++++++++++++++ java/rocksjni/ttl.cc | 42 ++++++++++++++++ 4 files changed, 171 insertions(+), 1 deletion(-) create mode 100644 java/org/rocksdb/TtlDB.java create mode 100644 java/org/rocksdb/test/TtlDBTest.java create mode 100644 java/rocksjni/ttl.cc diff --git a/java/Makefile b/java/Makefile index c8f443f7b..32717ddd8 100644 --- a/java/Makefile +++ b/java/Makefile @@ -29,13 +29,14 @@ NATIVE_JAVA_CLASSES = org.rocksdb.AbstractComparator\ org.rocksdb.SkipListMemTableConfig\ org.rocksdb.Slice\ org.rocksdb.Statistics\ + org.rocksdb.TtlDB\ org.rocksdb.VectorMemTableConfig\ org.rocksdb.StringAppendOperator\ org.rocksdb.WriteBatch\ org.rocksdb.WriteBatch.Handler\ org.rocksdb.test.WriteBatchInternal\ org.rocksdb.test.WriteBatchTest\ - org.rocksdb.WriteOptions\ + org.rocksdb.WriteOptions\ org.rocksdb.WriteBatchWithIndex\ org.rocksdb.WBWIRocksIterator @@ -79,6 +80,7 @@ JAVA_TESTS = org.rocksdb.test.BackupableDBOptionsTest\ org.rocksdb.test.SizeUnitTest\ org.rocksdb.test.SliceTest\ org.rocksdb.test.SnapshotTest\ + org.rocksdb.test.TtlDBTest\ org.rocksdb.test.StatisticsCollectorTest\ org.rocksdb.test.WriteBatchHandlerTest\ org.rocksdb.test.WriteBatchTest\ diff --git a/java/org/rocksdb/TtlDB.java b/java/org/rocksdb/TtlDB.java new file mode 100644 index 000000000..dd6c8cfbc --- /dev/null +++ b/java/org/rocksdb/TtlDB.java @@ -0,0 +1,75 @@ +// Copyright (c) 2014, Facebook, Inc. All rights reserved. +// This source code is licensed under the BSD-style license found in the +// LICENSE file in the root directory of this source tree. An additional grant +// of patent rights can be found in the PATENTS file in the same directory. + +package org.rocksdb; + +import java.util.List; + + +public class TtlDB extends RocksDB { + + //static Status Open(const Options& options, const std::string& dbname, + // DBWithTTL** dbptr, int32_t ttl = 0, + // bool read_only = false); + public static TtlDB open(Options options, String db_path, int ttl, + boolean readOnly) throws RocksDBException { + TtlDB ttldb = new TtlDB(); + ttldb.open(options.nativeHandle_, db_path, ttl, readOnly); + + // Prevent the RocksDB object from attempting to delete + // the underly C++ DB object. + //ttldb.disOwnNativeHandle(); + return ttldb; + } + + //static Status Open(const DBOptions& db_options, const std::string& dbname, + // const std::vector& column_families, + // std::vector* handles, + // DBWithTTL** dbptr, std::vector ttls, + // bool read_only = false); + public static TtlDB open(DBOptions options, String db_path, + List columnFamilyDescriptors, + List columnFamilyHandles, + List ttlValues, boolean readOnly){ + + + return null; + } + + public ColumnFamilyHandle createColumnFamilyWithTtl( + ColumnFamilyDescriptor columnFamilyDescriptor, int ttl) { + return null; + } + + /** + * Close the TtlDB instance and release resource. + * + * Internally, TtlDB owns the {@code rocksdb::DB} pointer to its associated + * {@link org.rocksdb.RocksDB}. The release of that RocksDB pointer is handled in the destructor + * of the c++ {@code rocksdb::TtlDB} and should be transparent to Java developers. + */ + @Override public synchronized void close() { + if (isInitialized()) { + super.close(); + } + } + + /** + * A protected construction that will be used in the static factory + * method {@link #open(DBOptions, String, java.util.List, java.util.List)} and + * {@link #open(DBOptions, String, java.util.List, java.util.List, java.util.List, boolean)}. + */ + protected TtlDB() { + super(); + } + + @Override protected void finalize() throws Throwable { + close(); + super.finalize(); + } + + private native void open(long optionsHandle, String db_path, int ttl, + boolean readOnly) throws RocksDBException; +} diff --git a/java/org/rocksdb/test/TtlDBTest.java b/java/org/rocksdb/test/TtlDBTest.java new file mode 100644 index 000000000..1637558a5 --- /dev/null +++ b/java/org/rocksdb/test/TtlDBTest.java @@ -0,0 +1,51 @@ +// Copyright (c) 2014, Facebook, Inc. All rights reserved. +// This source code is licensed under the BSD-style license found in the +// LICENSE file in the root directory of this source tree. An additional grant +// of patent rights can be found in the PATENTS file in the same directory. + +package org.rocksdb.test; + +import org.junit.ClassRule; +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.TemporaryFolder; +import org.rocksdb.Options; +import org.rocksdb.RocksDBException; +import org.rocksdb.TtlDB; + +import static org.assertj.core.api.Assertions.assertThat; + +public class TtlDBTest { + + @ClassRule + public static final RocksMemoryResource rocksMemoryResource = + new RocksMemoryResource(); + + @Rule + public TemporaryFolder dbFolder = new TemporaryFolder(); + + @Test + public void ttlDBOpen() throws RocksDBException, InterruptedException { + Options options = null; + TtlDB ttlDB = null; + try { + options = new Options().setCreateIfMissing(true); + ttlDB = TtlDB.open(options, dbFolder.getRoot().getAbsolutePath(), + 1, false); + ttlDB.put("key".getBytes(), "value".getBytes()); + assertThat(ttlDB.get("key".getBytes())). + isEqualTo("value".getBytes()); + Thread.sleep(1250); + ttlDB.compactRange(); + + assertThat(ttlDB.get("key".getBytes())).isNull(); + } finally { + if (ttlDB != null) { + ttlDB.close(); + } + if (options != null) { + options.dispose(); + } + } + } +} diff --git a/java/rocksjni/ttl.cc b/java/rocksjni/ttl.cc new file mode 100644 index 000000000..c8e7c44a5 --- /dev/null +++ b/java/rocksjni/ttl.cc @@ -0,0 +1,42 @@ +// Copyright (c) 2014, Facebook, Inc. All rights reserved. +// This source code is licensed under the BSD-style license found in the +// LICENSE file in the root directory of this source tree. An additional grant +// of patent rights can be found in the PATENTS file in the same directory. +// +// This file implements the "bridge" between Java and C++ and enables +// calling c++ rocksdb::TtlDB methods. +// from Java side. + +#include +#include +#include +#include +#include + +#include "include/org_rocksdb_TtlDB.h" +#include "rocksjni/portal.h" +#include "rocksdb/utilities/db_ttl.h" + +/* + * Class: org_rocksdb_TtlDB + * Method: open + * Signature: (JJ)V + */ +void Java_org_rocksdb_TtlDB_open(JNIEnv* env, jobject jttldb, + jlong joptions_handle, jstring jdb_path, jint jttl, + jboolean jread_only) { + auto opt = reinterpret_cast(joptions_handle); + rocksdb::DBWithTTL* db = nullptr; + const char* db_path = env->GetStringUTFChars(jdb_path, 0); + rocksdb::Status s = rocksdb::DBWithTTL::Open(*opt, db_path, &db, + jttl, jread_only); + env->ReleaseStringUTFChars(jdb_path, db_path); + + // as TTLDB extends RocksDB on the java side, we can reuse + // the RocksDB portal here. + if (s.ok()) { + rocksdb::RocksDBJni::setHandle(env, jttldb, db); + return; + } + rocksdb::RocksDBExceptionJni::ThrowNew(env, s); +}