[RocksJava] LogLevel support in Options

It's now possible to set a LogLevel in Options and
DBOptions to control LOG verbosity.
This commit is contained in:
fyrz 2014-11-14 23:40:20 +01:00
parent 353303a765
commit 07cd3c42a2
7 changed files with 236 additions and 0 deletions

View File

@ -57,6 +57,7 @@ JAVA_TESTS = org.rocksdb.test.BackupableDBTest\
org.rocksdb.test.DirectComparatorTest\ org.rocksdb.test.DirectComparatorTest\
org.rocksdb.test.FilterTest\ org.rocksdb.test.FilterTest\
org.rocksdb.test.FlushTest\ org.rocksdb.test.FlushTest\
org.rocksdb.test.InfoLogLevelTest\
org.rocksdb.test.KeyMayExistTest\ org.rocksdb.test.KeyMayExistTest\
org.rocksdb.test.MemTableTest\ org.rocksdb.test.MemTableTest\
org.rocksdb.test.MergeTest\ org.rocksdb.test.MergeTest\

View File

@ -83,11 +83,26 @@ public class DBOptions extends RocksObject implements DBOptionsInterface {
@Override @Override
public DBOptions setRateLimiterConfig(RateLimiterConfig config) { public DBOptions setRateLimiterConfig(RateLimiterConfig config) {
assert(isInitialized());
rateLimiterConfig_ = config; rateLimiterConfig_ = config;
setRateLimiter(nativeHandle_, config.newRateLimiterHandle()); setRateLimiter(nativeHandle_, config.newRateLimiterHandle());
return this; return this;
} }
@Override
public DBOptions setInfoLogLevel(InfoLogLevel infoLogLevel) {
assert(isInitialized());
setInfoLogLevel(nativeHandle_, infoLogLevel.getValue());
return this;
}
@Override
public InfoLogLevel infoLogLevel() {
assert(isInitialized());
return InfoLogLevel.getInfoLogLevel(
infoLogLevel(nativeHandle_));
}
@Override @Override
public DBOptions setMaxOpenFiles(int maxOpenFiles) { public DBOptions setMaxOpenFiles(int maxOpenFiles) {
assert(isInitialized()); assert(isInitialized());
@ -487,6 +502,8 @@ public class DBOptions extends RocksObject implements DBOptionsInterface {
private native boolean paranoidChecks(long handle); private native boolean paranoidChecks(long handle);
private native void setRateLimiter(long handle, private native void setRateLimiter(long handle,
long rateLimiterHandle); long rateLimiterHandle);
private native void setInfoLogLevel(long handle, byte logLevel);
private native byte infoLogLevel(long handle);
private native void setMaxOpenFiles(long handle, int maxOpenFiles); private native void setMaxOpenFiles(long handle, int maxOpenFiles);
private native int maxOpenFiles(long handle); private native int maxOpenFiles(long handle);
private native void setMaxTotalWalSize(long handle, private native void setMaxTotalWalSize(long handle,

View File

@ -113,6 +113,20 @@ public interface DBOptionsInterface {
*/ */
Object setRateLimiterConfig(RateLimiterConfig config); Object setRateLimiterConfig(RateLimiterConfig config);
/**
* <p>Sets the RocksDB log level. Default level is INFO</p>
*
* @param infoLogLevel log level to set.
* @return the instance of the current Object.
*/
Object setInfoLogLevel(InfoLogLevel infoLogLevel);
/**
* <p>Returns currently set log level.</p>
* @return {@link org.rocksdb.InfoLogLevel} instance.
*/
InfoLogLevel infoLogLevel();
/** /**
* Number of open files that can be used by the DB. You may need to * Number of open files that can be used by the DB. You may need to
* increase this if your database has a large working set. Value -1 means * increase this if your database has a large working set. Value -1 means

View File

@ -0,0 +1,44 @@
package org.rocksdb;
/**
* RocksDB log levels.
*/
public enum InfoLogLevel {
DEBUG_LEVEL((byte)0),
INFO_LEVEL((byte)1),
WARN_LEVEL((byte)2),
ERROR_LEVEL((byte)3),
FATAL_LEVEL((byte)4),
NUM_INFO_LOG_LEVELS((byte)5);
private final byte value_;
private InfoLogLevel(byte value) {
value_ = value;
}
/**
* Returns the byte value of the enumerations value
*
* @return byte representation
*/
public byte getValue() {
return value_;
}
/**
* Get InfoLogLevel by byte value.
*
* @param value byte representation of InfoLogLevel.
*
* @return {@link org.rocksdb.InfoLogLevel} instance or null.
*/
public static InfoLogLevel getInfoLogLevel(byte value) {
for (InfoLogLevel infoLogLevel : InfoLogLevel.values()) {
if (infoLogLevel.getValue() == value){
return infoLogLevel;
}
}
return null;
}
}

View File

@ -604,6 +604,7 @@ public class Options extends RocksObject
@Override @Override
public Options setMemTableConfig(MemTableConfig config) public Options setMemTableConfig(MemTableConfig config)
throws RocksDBException { throws RocksDBException {
assert(isInitialized());
memTableConfig_ = config; memTableConfig_ = config;
setMemTableFactory(nativeHandle_, config.newMemTableFactoryHandle()); setMemTableFactory(nativeHandle_, config.newMemTableFactoryHandle());
return this; return this;
@ -611,11 +612,26 @@ public class Options extends RocksObject
@Override @Override
public Options setRateLimiterConfig(RateLimiterConfig config) { public Options setRateLimiterConfig(RateLimiterConfig config) {
assert(isInitialized());
rateLimiterConfig_ = config; rateLimiterConfig_ = config;
setRateLimiter(nativeHandle_, config.newRateLimiterHandle()); setRateLimiter(nativeHandle_, config.newRateLimiterHandle());
return this; return this;
} }
@Override
public Options setInfoLogLevel(InfoLogLevel infoLogLevel) {
assert(isInitialized());
setInfoLogLevel(nativeHandle_, infoLogLevel.getValue());
return this;
}
@Override
public InfoLogLevel infoLogLevel() {
assert(isInitialized());
return InfoLogLevel.getInfoLogLevel(
infoLogLevel(nativeHandle_));
}
@Override @Override
public String memTableFactoryName() { public String memTableFactoryName() {
assert(isInitialized()); assert(isInitialized());
@ -1025,6 +1041,8 @@ public class Options extends RocksObject
private native boolean paranoidChecks(long handle); private native boolean paranoidChecks(long handle);
private native void setRateLimiter(long handle, private native void setRateLimiter(long handle,
long rateLimiterHandle); long rateLimiterHandle);
private native void setInfoLogLevel(long handle, byte logLevel);
private native byte infoLogLevel(long handle);
private native void setMaxOpenFiles(long handle, int maxOpenFiles); private native void setMaxOpenFiles(long handle, int maxOpenFiles);
private native int maxOpenFiles(long handle); private native int maxOpenFiles(long handle);
private native void setMaxTotalWalSize(long handle, private native void setMaxTotalWalSize(long handle,

View File

@ -0,0 +1,98 @@
package org.rocksdb.test;
import org.junit.ClassRule;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;
import org.rocksdb.*;
import java.io.IOException;
import static java.nio.file.Files.readAllBytes;
import static java.nio.file.Paths.get;
import static org.assertj.core.api.Assertions.assertThat;
public class InfoLogLevelTest {
@ClassRule
public static final RocksMemoryResource rocksMemoryResource =
new RocksMemoryResource();
@Rule
public TemporaryFolder dbFolder = new TemporaryFolder();
@Test
public void testInfoLogLevel() throws RocksDBException,
IOException {
RocksDB db = null;
try {
db = RocksDB.open(dbFolder.getRoot().getAbsolutePath());
db.put("key".getBytes(), "value".getBytes());
assertThat(getLogContents()).isNotEmpty();
} finally {
if (db != null) {
db.close();
}
}
}
@Test
public void testFatalLogLevel() throws RocksDBException,
IOException {
RocksDB db = null;
Options options = null;
try {
options = new Options().
setCreateIfMissing(true).
setInfoLogLevel(InfoLogLevel.FATAL_LEVEL);
assertThat(options.infoLogLevel()).
isEqualTo(InfoLogLevel.FATAL_LEVEL);
db = RocksDB.open(options,
dbFolder.getRoot().getAbsolutePath());
db.put("key".getBytes(), "value".getBytes());
assertThat(getLogContents()).isEmpty();
} finally {
if (db != null) {
db.close();
}
}
}
@Test
public void testFatalLogLevelWithDBOptions()
throws RocksDBException, IOException {
RocksDB db = null;
Options options = null;
DBOptions dbOptions = null;
try {
dbOptions = new DBOptions().
setInfoLogLevel(InfoLogLevel.FATAL_LEVEL);
options = new Options(dbOptions,
new ColumnFamilyOptions()).
setCreateIfMissing(true);
assertThat(dbOptions.infoLogLevel()).
isEqualTo(InfoLogLevel.FATAL_LEVEL);
assertThat(options.infoLogLevel()).
isEqualTo(InfoLogLevel.FATAL_LEVEL);
db = RocksDB.open(options,
dbFolder.getRoot().getAbsolutePath());
db.put("key".getBytes(), "value".getBytes());
assertThat(getLogContents()).isEmpty();
} finally {
if (db != null) {
db.close();
}
}
}
/**
* Read LOG file contents into String.
*
* @return LOG file contents as String.
* @throws IOException if file is not found.
*/
private String getLogContents() throws IOException {
return new String(readAllBytes(get(
dbFolder.getRoot().getAbsolutePath()+ "/LOG")));
}
}

View File

@ -625,6 +625,28 @@ void Java_org_rocksdb_Options_setRateLimiter(
reinterpret_cast<rocksdb::RateLimiter*>(jrate_limiter_handle)); reinterpret_cast<rocksdb::RateLimiter*>(jrate_limiter_handle));
} }
/*
* Class: org_rocksdb_Options
* Method: setInfoLogLevel
* Signature: (JB)V
*/
void Java_org_rocksdb_Options_setInfoLogLevel(
JNIEnv* env, jobject jobj, jlong jhandle, jbyte jlog_level) {
reinterpret_cast<rocksdb::Options*>(jhandle)->info_log_level =
static_cast<rocksdb::InfoLogLevel>(jlog_level);
}
/*
* Class: org_rocksdb_Options
* Method: infoLogLevel
* Signature: (J)B
*/
jbyte Java_org_rocksdb_Options_infoLogLevel(
JNIEnv* env, jobject jobj, jlong jhandle) {
return static_cast<jbyte>(
reinterpret_cast<rocksdb::Options*>(jhandle)->info_log_level);
}
/* /*
* Class: org_rocksdb_Options * Class: org_rocksdb_Options
* Method: tableCacheNumshardbits * Method: tableCacheNumshardbits
@ -2835,6 +2857,28 @@ void Java_org_rocksdb_DBOptions_setRateLimiter(
reinterpret_cast<rocksdb::RateLimiter*>(jrate_limiter_handle)); reinterpret_cast<rocksdb::RateLimiter*>(jrate_limiter_handle));
} }
/*
* Class: org_rocksdb_DBOptions
* Method: setInfoLogLevel
* Signature: (JB)V
*/
void Java_org_rocksdb_DBOptions_setInfoLogLevel(
JNIEnv* env, jobject jobj, jlong jhandle, jbyte jlog_level) {
reinterpret_cast<rocksdb::DBOptions*>(jhandle)->info_log_level =
static_cast<rocksdb::InfoLogLevel>(jlog_level);
}
/*
* Class: org_rocksdb_DBOptions
* Method: infoLogLevel
* Signature: (J)B
*/
jbyte Java_org_rocksdb_DBOptions_infoLogLevel(
JNIEnv* env, jobject jobj, jlong jhandle) {
return static_cast<jbyte>(
reinterpret_cast<rocksdb::DBOptions*>(jhandle)->info_log_level);
}
/* /*
* Class: org_rocksdb_DBOptions * Class: org_rocksdb_DBOptions
* Method: setMaxTotalWalSize * Method: setMaxTotalWalSize