Merge pull request #396 from fyrz/RocksJava-LogLevel

[RocksJava] LogLevel support in Options
This commit is contained in:
Yueh-Hsuan Chiang 2014-11-15 17:03:14 -08:00
commit a77e97c536
7 changed files with 237 additions and 0 deletions

View File

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

View File

@ -83,11 +83,26 @@ public class DBOptions extends RocksObject implements DBOptionsInterface {
@Override
public DBOptions setRateLimiterConfig(RateLimiterConfig config) {
assert(isInitialized());
rateLimiterConfig_ = config;
setRateLimiter(nativeHandle_, config.newRateLimiterHandle());
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
public DBOptions setMaxOpenFiles(int maxOpenFiles) {
assert(isInitialized());
@ -487,6 +502,8 @@ public class DBOptions extends RocksObject implements DBOptionsInterface {
private native boolean paranoidChecks(long handle);
private native void setRateLimiter(long handle,
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 int maxOpenFiles(long handle);
private native void setMaxTotalWalSize(long handle,

View File

@ -113,6 +113,20 @@ public interface DBOptionsInterface {
*/
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
* increase this if your database has a large working set. Value -1 means

View File

@ -0,0 +1,47 @@
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.
* @throws java.lang.IllegalArgumentException if an invalid
* value is provided.
*/
public static InfoLogLevel getInfoLogLevel(byte value) {
for (InfoLogLevel infoLogLevel : InfoLogLevel.values()) {
if (infoLogLevel.getValue() == value){
return infoLogLevel;
}
}
throw new IllegalArgumentException(
"Illegal value provided for InfoLogLevel.");
}
}

View File

@ -616,6 +616,20 @@ public class Options extends RocksObject
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
public String memTableFactoryName() {
assert(isInitialized());
@ -1025,6 +1039,8 @@ public class Options extends RocksObject
private native boolean paranoidChecks(long handle);
private native void setRateLimiter(long handle,
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 int maxOpenFiles(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));
}
/*
* 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
* Method: tableCacheNumshardbits
@ -2835,6 +2857,28 @@ void Java_org_rocksdb_DBOptions_setRateLimiter(
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
* Method: setMaxTotalWalSize