From ae115eff8f3210b17d098731335bcdb767632d4d Mon Sep 17 00:00:00 2001 From: Alan Paxton Date: Tue, 22 Nov 2022 15:48:59 -0800 Subject: [PATCH] improve copying of Env in Options (#10666) Summary: Closes https://github.com/facebook/rocksdb/issues/9909 - Constructing an Options from a DBOptions should use the Env from the DBOptions - DBOptions should be constructed with the default Env as the env_, rather than null. Why ever not ? Pull Request resolved: https://github.com/facebook/rocksdb/pull/10666 Reviewed By: riversand963 Differential Revision: D40515418 Pulled By: ajkr fbshipit-source-id: 4122ba3f537660720262694c21ab4bfb13b6f8de --- java/src/main/java/org/rocksdb/DBOptions.java | 1 + java/src/main/java/org/rocksdb/Options.java | 2 +- .../java/org/rocksdb/MixedOptionsTest.java | 30 +++++++++++++++++++ 3 files changed, 32 insertions(+), 1 deletion(-) 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); + } + } + } }