[JAVA] Add java binding for Options.block_cache.

Summary:
Add java bindings for Options.block_cache and allow DbBenchmark to
set cache_size.

Test Plan:
make rocksdbjava
make jtest
make jdb_Bench

Reviewers: haobo, sdong, ankgup87

Reviewed By: ankgup87

CC: leveldb, dhruba

Differential Revision: https://reviews.facebook.net/D17481
This commit is contained in:
Yueh-Hsuan Chiang 2014-04-14 13:42:36 -07:00
parent 2885ad9b77
commit 31e7e7fe84
6 changed files with 84 additions and 44 deletions

View File

@ -28,12 +28,24 @@ db_bench: java
rm -rf /tmp/rocksdbjni-bench rm -rf /tmp/rocksdbjni-bench
java -Djava.library.path=.:../ -cp "$(ROCKSDB_JAR):.:./*" org.rocksdb.benchmark.DbBenchmark --threads=1 --benchmarks=fillseq,readrandom,readwhilewriting java -Djava.library.path=.:../ -cp "$(ROCKSDB_JAR):.:./*" org.rocksdb.benchmark.DbBenchmark --threads=1 --benchmarks=fillseq,readrandom,readwhilewriting
rm -rf /tmp/rocksdbjni-bench rm -rf /tmp/rocksdbjni-bench
java -Djava.library.path=.:../ -cp "$(ROCKSDB_JAR):.:./*" org.rocksdb.benchmark.DbBenchmark --threads=1 --benchmarks=fillseq,readrandom,readwhilewriting --cache_size=200000000
rm -rf /tmp/rocksdbjni-bench
java -Djava.library.path=.:../ -cp "$(ROCKSDB_JAR):.:./*" org.rocksdb.benchmark.DbBenchmark --threads=2 --benchmarks=fillseq,readrandom,readwhilewriting java -Djava.library.path=.:../ -cp "$(ROCKSDB_JAR):.:./*" org.rocksdb.benchmark.DbBenchmark --threads=2 --benchmarks=fillseq,readrandom,readwhilewriting
rm -rf /tmp/rocksdbjni-bench rm -rf /tmp/rocksdbjni-bench
java -Djava.library.path=.:../ -cp "$(ROCKSDB_JAR):.:./*" org.rocksdb.benchmark.DbBenchmark --threads=2 --benchmarks=fillseq,readrandom,readwhilewriting --cache_size=200000000
rm -rf /tmp/rocksdbjni-bench
java -Djava.library.path=.:../ -cp "$(ROCKSDB_JAR):.:./*" org.rocksdb.benchmark.DbBenchmark --threads=4 --benchmarks=fillseq,readrandom,readwhilewriting java -Djava.library.path=.:../ -cp "$(ROCKSDB_JAR):.:./*" org.rocksdb.benchmark.DbBenchmark --threads=4 --benchmarks=fillseq,readrandom,readwhilewriting
rm -rf /tmp/rocksdbjni-bench rm -rf /tmp/rocksdbjni-bench
java -Djava.library.path=.:../ -cp "$(ROCKSDB_JAR):.:./*" org.rocksdb.benchmark.DbBenchmark --threads=4 --benchmarks=fillseq,readrandom,readwhilewriting --cache_size=200000000
rm -rf /tmp/rocksdbjni-bench
java -Djava.library.path=.:../ -cp "$(ROCKSDB_JAR):.:./*" org.rocksdb.benchmark.DbBenchmark --threads=8 --benchmarks=fillseq,readrandom,readwhilewriting java -Djava.library.path=.:../ -cp "$(ROCKSDB_JAR):.:./*" org.rocksdb.benchmark.DbBenchmark --threads=8 --benchmarks=fillseq,readrandom,readwhilewriting
rm -rf /tmp/rocksdbjni-bench rm -rf /tmp/rocksdbjni-bench
java -Djava.library.path=.:../ -cp "$(ROCKSDB_JAR):.:./*" org.rocksdb.benchmark.DbBenchmark --threads=8 --benchmarks=fillseq,readrandom,readwhilewriting --cache_size=200000000
rm -rf /tmp/rocksdbjni-bench
java -Djava.library.path=.:../ -cp "$(ROCKSDB_JAR):.:./*" org.rocksdb.benchmark.DbBenchmark --threads=16 --benchmarks=fillseq,readrandom,readwhilewriting java -Djava.library.path=.:../ -cp "$(ROCKSDB_JAR):.:./*" org.rocksdb.benchmark.DbBenchmark --threads=16 --benchmarks=fillseq,readrandom,readwhilewriting
rm -rf /tmp/rocksdbjni-bench rm -rf /tmp/rocksdbjni-bench
java -Djava.library.path=.:../ -cp "$(ROCKSDB_JAR):.:./*" org.rocksdb.benchmark.DbBenchmark --threads=16 --benchmarks=fillseq,readrandom,readwhilewriting --cache_size=200000000
rm -rf /tmp/rocksdbjni-bench
java -Djava.library.path=.:../ -cp "$(ROCKSDB_JAR):.:./*" org.rocksdb.benchmark.DbBenchmark --threads=32 --benchmarks=fillseq,readrandom,readwhilewriting java -Djava.library.path=.:../ -cp "$(ROCKSDB_JAR):.:./*" org.rocksdb.benchmark.DbBenchmark --threads=32 --benchmarks=fillseq,readrandom,readwhilewriting
rm -rf /tmp/rocksdbjni-bench
java -Djava.library.path=.:../ -cp "$(ROCKSDB_JAR):.:./*" org.rocksdb.benchmark.DbBenchmark --threads=32 --benchmarks=fillseq,readrandom,readwhilewriting --cache_size=200000000

View File

@ -56,13 +56,11 @@ public class RocksDBSample {
assert(db == null); assert(db == null);
assert(false); assert(false);
} }
// be sure to release the c++ pointer // be sure to release the c++ pointer
options.dispose();
db.close(); db.close();
try { try {
db = RocksDB.open(db_path); db = RocksDB.open(options, db_path);
db.put("hello".getBytes(), "world".getBytes()); db.put("hello".getBytes(), "world".getBytes());
byte[] value = db.get("hello".getBytes()); byte[] value = db.get("hello".getBytes());
System.out.format("Get('hello') = %s\n", System.out.format("Get('hello') = %s\n",
@ -127,5 +125,7 @@ public class RocksDBSample {
if (db != null) { if (db != null) {
db.close(); db.close();
} }
// be sure to dispose c++ pointer
options.dispose();
} }
} }

View File

@ -13,6 +13,7 @@ package org.rocksdb;
* become out-of-scope to release the allocated memory in c++. * become out-of-scope to release the allocated memory in c++.
*/ */
public class Options { public class Options {
static final long DEFAULT_CACHE_SIZE = 8 << 20;
/** /**
* Construct options for opening a RocksDB. * Construct options for opening a RocksDB.
* *
@ -21,6 +22,7 @@ public class Options {
*/ */
public Options() { public Options() {
nativeHandle_ = 0; nativeHandle_ = 0;
cacheSize_ = DEFAULT_CACHE_SIZE;
newOptions(); newOptions();
} }
@ -198,6 +200,24 @@ public class Options {
return maxBackgroundCompactions(nativeHandle_); return maxBackgroundCompactions(nativeHandle_);
} }
/**
* Set the amount of cache in bytes that will be used by RocksDB.
* If cacheSize is non-positive, then cache will not be used.
*
* DEFAULT: 8M
*/
public Options setCacheSize(long cacheSize) {
cacheSize_ = cacheSize;
return this;
}
/**
* @return the amount of cache in bytes that will be used by RocksDB.
*/
public long cacheSize() {
return cacheSize_;
}
/** /**
* Release the memory allocated for the current instance * Release the memory allocated for the current instance
* in the c++ side. * in the c++ side.
@ -231,4 +251,5 @@ public class Options {
private native int maxBackgroundCompactions(long handle); private native int maxBackgroundCompactions(long handle);
long nativeHandle_; long nativeHandle_;
long cacheSize_;
} }

View File

@ -33,7 +33,12 @@ public class RocksDB {
*/ */
public static RocksDB open(String path) throws RocksDBException { public static RocksDB open(String path) throws RocksDBException {
RocksDB db = new RocksDB(); RocksDB db = new RocksDB();
db.open0(path);
// This allows to use the rocksjni default Options instead of
// the c++ one.
Options options = new Options();
db.open(options.nativeHandle_, options.cacheSize_, path);
options.dispose();
return db; return db;
} }
@ -44,7 +49,7 @@ public class RocksDB {
public static RocksDB open(Options options, String path) public static RocksDB open(Options options, String path)
throws RocksDBException { throws RocksDBException {
RocksDB db = new RocksDB(); RocksDB db = new RocksDB();
db.open(options.nativeHandle_, path); db.open(options.nativeHandle_, options.cacheSize_, path);
return db; return db;
} }
@ -145,9 +150,8 @@ public class RocksDB {
} }
// native methods // native methods
private native void open0(String path) throws RocksDBException;
private native void open( private native void open(
long optionsHandle, String path) throws RocksDBException; long optionsHandle, long cacheSize, String path) throws RocksDBException;
private native void put( private native void put(
long handle, byte[] key, int keyLen, long handle, byte[] key, int keyLen,
byte[] value, int valueLen) throws RocksDBException; byte[] value, int valueLen) throws RocksDBException;

View File

@ -366,15 +366,25 @@ public class DbBenchmark {
randSeed_ = (long) flags.get(Flag.seed); randSeed_ = (long) flags.get(Flag.seed);
databaseDir_ = (String) flags.get(Flag.db); databaseDir_ = (String) flags.get(Flag.db);
writesPerSeconds_ = (int) flags.get(Flag.writes_per_second); writesPerSeconds_ = (int) flags.get(Flag.writes_per_second);
cacheSize_ = (long) flags.get(Flag.cache_size);
gen_ = new RandomGenerator(compressionRatio_); gen_ = new RandomGenerator(compressionRatio_);
finishLock_ = new Object(); finishLock_ = new Object();
} }
private void prepareOptions(Options options) {
options.setCacheSize(cacheSize_);
if (!useExisting_) {
options.setCreateIfMissing(true);
}
}
private void run() throws RocksDBException { private void run() throws RocksDBException {
if (!useExisting_) { if (!useExisting_) {
destroyDb(); destroyDb();
} }
open(); Options options = new Options();
prepareOptions(options);
open(options);
printHeader(); printHeader();
@ -426,7 +436,7 @@ public class DbBenchmark {
} }
} else if (benchmark.equals("delete")) { } else if (benchmark.equals("delete")) {
destroyDb(); destroyDb();
open(); open(options);
} else { } else {
known = false; known = false;
System.err.println("Unknown benchmark: " + benchmark); System.err.println("Unknown benchmark: " + benchmark);
@ -467,6 +477,7 @@ public class DbBenchmark {
} }
writeOpt.dispose(); writeOpt.dispose();
} }
options.dispose();
db_.close(); db_.close();
} }
@ -495,8 +506,8 @@ public class DbBenchmark {
} }
} }
private void open() throws RocksDBException { private void open(Options options) throws RocksDBException {
db_ = RocksDB.open(databaseDir_); db_ = RocksDB.open(options, databaseDir_);
} }
private void start() { private void start() {
@ -703,11 +714,11 @@ public class DbBenchmark {
} }
}, },
cache_size(-1, cache_size(-1L,
"Number of bytes to use as a cache of uncompressed data.\n" + "Number of bytes to use as a cache of uncompressed data.\n" +
"\tNegative means use default settings.") { "\tNegative means use default settings.") {
@Override public Object parseValue(String value) { @Override public Object parseValue(String value) {
return Integer.parseInt(value); return Long.parseLong(value);
} }
}, },
@ -794,6 +805,7 @@ public class DbBenchmark {
final int threadNum_; final int threadNum_;
final int writesPerSeconds_; final int writesPerSeconds_;
final long randSeed_; final long randSeed_;
final long cacheSize_;
final boolean useExisting_; final boolean useExisting_;
final String databaseDir_; final String databaseDir_;
final double compressionRatio_; final double compressionRatio_;

View File

@ -14,47 +14,38 @@
#include "include/org_rocksdb_RocksDB.h" #include "include/org_rocksdb_RocksDB.h"
#include "rocksjni/portal.h" #include "rocksjni/portal.h"
#include "rocksdb/db.h" #include "rocksdb/db.h"
#include "rocksdb/cache.h"
////////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////
// rocksdb::DB::Open // rocksdb::DB::Open
void rocksdb_open_helper(
JNIEnv* env, jobject java_db, jstring jdb_path, const rocksdb::Options& opt) {
rocksdb::DB* db;
const char* db_path = env->GetStringUTFChars(jdb_path, 0);
rocksdb::Status s = rocksdb::DB::Open(opt, db_path, &db);
env->ReleaseStringUTFChars(jdb_path, db_path);
if (s.ok()) {
rocksdb::RocksDBJni::setHandle(env, java_db, db);
return;
}
rocksdb::RocksDBExceptionJni::ThrowNew(env, s);
}
/*
* Class: org_rocksdb_RocksDB
* Method: open0
* Signature: (Ljava/lang/String;)V
*/
void Java_org_rocksdb_RocksDB_open0(
JNIEnv* env, jobject jdb, jstring jdb_path) {
rocksdb::Options options;
options.create_if_missing = true;
rocksdb_open_helper(env, jdb, jdb_path, options);
}
/* /*
* Class: org_rocksdb_RocksDB * Class: org_rocksdb_RocksDB
* Method: open * Method: open
* Signature: (JLjava/lang/String;)V * Signature: (JLjava/lang/String;)V
*/ */
void Java_org_rocksdb_RocksDB_open( void Java_org_rocksdb_RocksDB_open(
JNIEnv* env, jobject jdb, jlong jopt_handle, jstring jdb_path) { JNIEnv* env, jobject jdb, jlong jopt_handle,
auto options = reinterpret_cast<rocksdb::Options*>(jopt_handle); jlong jcache_size, jstring jdb_path) {
rocksdb_open_helper(env, jdb, jdb_path, *options); auto opt = reinterpret_cast<rocksdb::Options*>(jopt_handle);
if (jcache_size > 0) {
opt->no_block_cache = false;
opt->block_cache = rocksdb::NewLRUCache(jcache_size);
} else {
opt->no_block_cache = true;
opt->block_cache = nullptr;
}
rocksdb::DB* db = nullptr;
const char* db_path = env->GetStringUTFChars(jdb_path, 0);
rocksdb::Status s = rocksdb::DB::Open(*opt, db_path, &db);
env->ReleaseStringUTFChars(jdb_path, db_path);
if (s.ok()) {
rocksdb::RocksDBJni::setHandle(env, jdb, db);
return;
}
rocksdb::RocksDBExceptionJni::ThrowNew(env, s);
} }
////////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////