diff --git a/java/src/main/java/org/rocksdb/DBOptions.java b/java/src/main/java/org/rocksdb/DBOptions.java index 5432222620..9eb5ca8738 100644 --- a/java/src/main/java/org/rocksdb/DBOptions.java +++ b/java/src/main/java/org/rocksdb/DBOptions.java @@ -31,6 +31,7 @@ public class DBOptions extends RocksObject public DBOptions() { super(newDBOptions()); numShardBits_ = DEFAULT_NUM_SHARD_BITS; + env_ = Env.getDefault(); } /** diff --git a/java/src/main/java/org/rocksdb/Options.java b/java/src/main/java/org/rocksdb/Options.java index 1f1e5507a5..54f88262bd 100644 --- a/java/src/main/java/org/rocksdb/Options.java +++ b/java/src/main/java/org/rocksdb/Options.java @@ -66,7 +66,7 @@ public class Options extends RocksObject final ColumnFamilyOptions columnFamilyOptions) { super(newOptions(dbOptions.nativeHandle_, columnFamilyOptions.nativeHandle_)); - env_ = Env.getDefault(); + env_ = dbOptions.getEnv() != null ? dbOptions.getEnv() : Env.getDefault(); } /** diff --git a/java/src/test/java/org/rocksdb/MixedOptionsTest.java b/java/src/test/java/org/rocksdb/MixedOptionsTest.java index 10c92d49dd..4e17d04ef3 100644 --- a/java/src/test/java/org/rocksdb/MixedOptionsTest.java +++ b/java/src/test/java/org/rocksdb/MixedOptionsTest.java @@ -52,4 +52,34 @@ public class MixedOptionsTest { } } } + + @Test + public void mixedOptionsEnvTest() { + try (final ColumnFamilyOptions cfOptions = new ColumnFamilyOptions(); + final DBOptions dbOptions = new DBOptions()) { + assertThat(dbOptions.getEnv()).isNotNull(); + assertThat(dbOptions.getEnv()).isSameAs(Env.getDefault()); + final Env memEnv = new RocksMemEnv(Env.getDefault()); + + try (final Options options = new Options(dbOptions, cfOptions)) { + assertThat(options.getEnv()).isSameAs(Env.getDefault()); + } + + dbOptions.setEnv(memEnv); + memEnv.setBackgroundThreads(4, Priority.LOW); + Env.getDefault().setBackgroundThreads(2, Priority.HIGH); + assertThat(dbOptions.getEnv().getBackgroundThreads(Priority.LOW)).isEqualTo(4); + assertThat(dbOptions.getEnv().getBackgroundThreads(Priority.HIGH)).isEqualTo(2); + assertThat(Env.getDefault().getBackgroundThreads(Priority.LOW)).isEqualTo(4); + assertThat(Env.getDefault().getBackgroundThreads(Priority.HIGH)).isEqualTo(2); + + try (final Options options = new Options(dbOptions, cfOptions)) { + assertThat(options.getEnv().getBackgroundThreads(Priority.LOW)).isEqualTo(4); + assertThat(options.getEnv().getBackgroundThreads(Priority.HIGH)).isEqualTo(2); + + assertThat(options.getEnv()).isNotSameAs(Env.getDefault()); + assertThat(options.getEnv()).isSameAs(memEnv); + } + } + } }