[RocksJava] TTL-Support

This commit is contained in:
fyrz 2014-11-17 19:22:44 +01:00
parent 1190ebe5aa
commit ca47da9e63
4 changed files with 171 additions and 1 deletions

View File

@ -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\

View File

@ -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<ColumnFamilyDescriptor>& column_families,
// std::vector<ColumnFamilyHandle*>* handles,
// DBWithTTL** dbptr, std::vector<int32_t> ttls,
// bool read_only = false);
public static TtlDB open(DBOptions options, String db_path,
List<ColumnFamilyDescriptor> columnFamilyDescriptors,
List<ColumnFamilyHandle> columnFamilyHandles,
List<Integer> 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;
}

View File

@ -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();
}
}
}
}

42
java/rocksjni/ttl.cc Normal file
View File

@ -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 <stdio.h>
#include <stdlib.h>
#include <jni.h>
#include <string>
#include <vector>
#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<rocksdb::Options*>(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);
}