[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

@@ -13,6 +13,7 @@ package org.rocksdb;
* become out-of-scope to release the allocated memory in c++.
*/
public class Options {
static final long DEFAULT_CACHE_SIZE = 8 << 20;
/**
* Construct options for opening a RocksDB.
*
@@ -21,6 +22,7 @@ public class Options {
*/
public Options() {
nativeHandle_ = 0;
cacheSize_ = DEFAULT_CACHE_SIZE;
newOptions();
}
@@ -198,6 +200,24 @@ public class Options {
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
* in the c++ side.
@@ -231,4 +251,5 @@ public class Options {
private native int maxBackgroundCompactions(long handle);
long nativeHandle_;
long cacheSize_;
}

View File

@@ -33,7 +33,12 @@ public class RocksDB {
*/
public static RocksDB open(String path) throws RocksDBException {
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;
}
@@ -44,7 +49,7 @@ public class RocksDB {
public static RocksDB open(Options options, String path)
throws RocksDBException {
RocksDB db = new RocksDB();
db.open(options.nativeHandle_, path);
db.open(options.nativeHandle_, options.cacheSize_, path);
return db;
}
@@ -145,9 +150,8 @@ public class RocksDB {
}
// native methods
private native void open0(String path) throws RocksDBException;
private native void open(
long optionsHandle, String path) throws RocksDBException;
long optionsHandle, long cacheSize, String path) throws RocksDBException;
private native void put(
long handle, byte[] key, int keyLen,
byte[] value, int valueLen) throws RocksDBException;

View File

@@ -366,15 +366,25 @@ public class DbBenchmark {
randSeed_ = (long) flags.get(Flag.seed);
databaseDir_ = (String) flags.get(Flag.db);
writesPerSeconds_ = (int) flags.get(Flag.writes_per_second);
cacheSize_ = (long) flags.get(Flag.cache_size);
gen_ = new RandomGenerator(compressionRatio_);
finishLock_ = new Object();
}
private void prepareOptions(Options options) {
options.setCacheSize(cacheSize_);
if (!useExisting_) {
options.setCreateIfMissing(true);
}
}
private void run() throws RocksDBException {
if (!useExisting_) {
destroyDb();
}
open();
Options options = new Options();
prepareOptions(options);
open(options);
printHeader();
@@ -426,7 +436,7 @@ public class DbBenchmark {
}
} else if (benchmark.equals("delete")) {
destroyDb();
open();
open(options);
} else {
known = false;
System.err.println("Unknown benchmark: " + benchmark);
@@ -467,6 +477,7 @@ public class DbBenchmark {
}
writeOpt.dispose();
}
options.dispose();
db_.close();
}
@@ -495,8 +506,8 @@ public class DbBenchmark {
}
}
private void open() throws RocksDBException {
db_ = RocksDB.open(databaseDir_);
private void open(Options options) throws RocksDBException {
db_ = RocksDB.open(options, databaseDir_);
}
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" +
"\tNegative means use default settings.") {
@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 writesPerSeconds_;
final long randSeed_;
final long cacheSize_;
final boolean useExisting_;
final String databaseDir_;
final double compressionRatio_;