diff --git a/java/Makefile b/java/Makefile index b87fea16d..f0ab4c12e 100644 --- a/java/Makefile +++ b/java/Makefile @@ -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\ diff --git a/java/org/rocksdb/DBOptions.java b/java/org/rocksdb/DBOptions.java index 6ab276755..e19ee9a0a 100644 --- a/java/org/rocksdb/DBOptions.java +++ b/java/org/rocksdb/DBOptions.java @@ -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, diff --git a/java/org/rocksdb/DBOptionsInterface.java b/java/org/rocksdb/DBOptionsInterface.java index d3df483cb..19ffe375d 100644 --- a/java/org/rocksdb/DBOptionsInterface.java +++ b/java/org/rocksdb/DBOptionsInterface.java @@ -113,6 +113,20 @@ public interface DBOptionsInterface { */ Object setRateLimiterConfig(RateLimiterConfig config); + /** + *

Sets the RocksDB log level. Default level is INFO

+ * + * @param infoLogLevel log level to set. + * @return the instance of the current Object. + */ + Object setInfoLogLevel(InfoLogLevel infoLogLevel); + + /** + *

Returns currently set log level.

+ * @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 diff --git a/java/org/rocksdb/InfoLogLevel.java b/java/org/rocksdb/InfoLogLevel.java new file mode 100644 index 000000000..0a4a0e6ea --- /dev/null +++ b/java/org/rocksdb/InfoLogLevel.java @@ -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; + } +} diff --git a/java/org/rocksdb/Options.java b/java/org/rocksdb/Options.java index c5ea7216e..0d2a79698 100644 --- a/java/org/rocksdb/Options.java +++ b/java/org/rocksdb/Options.java @@ -604,6 +604,7 @@ public class Options extends RocksObject @Override public Options setMemTableConfig(MemTableConfig config) throws RocksDBException { + assert(isInitialized()); memTableConfig_ = config; setMemTableFactory(nativeHandle_, config.newMemTableFactoryHandle()); return this; @@ -611,11 +612,26 @@ public class Options extends RocksObject @Override public Options setRateLimiterConfig(RateLimiterConfig config) { + assert(isInitialized()); rateLimiterConfig_ = config; setRateLimiter(nativeHandle_, config.newRateLimiterHandle()); 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 +1041,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, diff --git a/java/org/rocksdb/test/InfoLogLevelTest.java b/java/org/rocksdb/test/InfoLogLevelTest.java new file mode 100644 index 000000000..c2da83979 --- /dev/null +++ b/java/org/rocksdb/test/InfoLogLevelTest.java @@ -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"))); + } +} diff --git a/java/rocksjni/options.cc b/java/rocksjni/options.cc index d725cc305..50bab7a1b 100644 --- a/java/rocksjni/options.cc +++ b/java/rocksjni/options.cc @@ -625,6 +625,28 @@ void Java_org_rocksdb_Options_setRateLimiter( reinterpret_cast(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(jhandle)->info_log_level = + static_cast(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( + reinterpret_cast(jhandle)->info_log_level); +} + /* * Class: org_rocksdb_Options * Method: tableCacheNumshardbits @@ -2835,6 +2857,28 @@ void Java_org_rocksdb_DBOptions_setRateLimiter( reinterpret_cast(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(jhandle)->info_log_level = + static_cast(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( + reinterpret_cast(jhandle)->info_log_level); +} + /* * Class: org_rocksdb_DBOptions * Method: setMaxTotalWalSize