This is an automated email from the ASF dual-hosted git repository.
jshao pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/gravitino.git
The following commit(s) were added to refs/heads/main by this push:
new c5037258a7 [#9143] Use orElseGet() instead of orElse() for lazy
evaluation in Config.java (#9281)
c5037258a7 is described below
commit c5037258a78e806d7216aa1da1eb5d5e41fc7cd5
Author: zhan7236 <[email protected]>
AuthorDate: Wed Dec 3 10:10:41 2025 +0800
[#9143] Use orElseGet() instead of orElse() for lazy evaluation in
Config.java (#9281)
### What changes were proposed in this pull request?
Use `orElseGet()` instead of `orElse()` when determining the
configuration directory in `Config.loadFromFile()`.
### Why are the changes needed?
- `orElse()` eagerly evaluates its argument, meaning the fallback
expression is computed even when the Optional contains a value.
- This causes the lookup and mapping for `GRAVITINO_HOME` to run
unnecessarily whenever `GRAVITINO_CONF_DIR` is set.
- Using `orElseGet()` with a Supplier lambda ensures the fallback
expression is only evaluated when needed.
Fixes #9143
### How was this patch tested?
- Compiled successfully with `./gradlew :common:compileJava`
- All tests pass with `./gradlew :common:test` and `./gradlew
:server:test`
---
common/src/main/java/org/apache/gravitino/Config.java | 9 +++++----
1 file changed, 5 insertions(+), 4 deletions(-)
diff --git a/common/src/main/java/org/apache/gravitino/Config.java
b/common/src/main/java/org/apache/gravitino/Config.java
index 91acfb0018..b713725cba 100644
--- a/common/src/main/java/org/apache/gravitino/Config.java
+++ b/common/src/main/java/org/apache/gravitino/Config.java
@@ -86,10 +86,11 @@ public abstract class Config {
public Config loadFromFile(String name) throws Exception {
String confDir =
Optional.ofNullable(System.getenv("GRAVITINO_CONF_DIR"))
- .orElse(
- Optional.ofNullable(System.getenv("GRAVITINO_HOME"))
- .map(s -> s + File.separator + "conf")
- .orElse(null));
+ .orElseGet(
+ () ->
+ Optional.ofNullable(System.getenv("GRAVITINO_HOME"))
+ .map(s -> s + File.separator + "conf")
+ .orElse(null));
if (confDir == null) {
throw new IllegalArgumentException("GRAVITINO_CONF_DIR or GRAVITINO_HOME
not set");