From 1e2c85e5153ba0185bb6faa7a8ffca1e9aa7aebf Mon Sep 17 00:00:00 2001 From: Ulrich Kramer Date: Thu, 19 Dec 2024 08:56:54 +0100 Subject: [PATCH] [CALCITE-6728] Introduce new methods to lookup tables and schemas inside schemas --- .../schema/lookup/LoadingCacheLookup.java | 40 +++++++++++-------- 1 file changed, 24 insertions(+), 16 deletions(-) diff --git a/core/src/main/java/org/apache/calcite/schema/lookup/LoadingCacheLookup.java b/core/src/main/java/org/apache/calcite/schema/lookup/LoadingCacheLookup.java index 7c2e7cce40d..6100f46118a 100644 --- a/core/src/main/java/org/apache/calcite/schema/lookup/LoadingCacheLookup.java +++ b/core/src/main/java/org/apache/calcite/schema/lookup/LoadingCacheLookup.java @@ -26,6 +26,7 @@ import java.util.Set; import java.util.concurrent.ExecutionException; import java.util.concurrent.TimeUnit; +import java.util.function.Function; /** * This class can be used to cache lookups. @@ -42,24 +43,10 @@ public LoadingCacheLookup(Lookup delegate) { this.delegate = delegate; this.cache = CacheBuilder.newBuilder() .expireAfterWrite(1, TimeUnit.MINUTES) - .build( - CacheLoader.from(name -> { - T result = delegate.get(name); - if (result != null) { - return result; - } - throw new EntryNotFoundException(); - })); + .build(new NullSafeCacheLoader(delegate::get)); this.cacheIgnoreCase = CacheBuilder.newBuilder() .expireAfterWrite(1, TimeUnit.MINUTES) - .build( - CacheLoader.from(name -> { - Named result = delegate.getIgnoreCase(name); - if (result != null) { - return result; - } - throw new EntryNotFoundException(); - })); + .build(new NullSafeCacheLoader<>(delegate::getIgnoreCase)); } @Override public @Nullable T get(String name) { @@ -92,4 +79,25 @@ public LoadingCacheLookup(Lookup delegate) { return delegate.getNames(pattern); } + /** + * CacheLoader which doesn't return null values. + * + * @param Key + * @param Value + */ + private static class NullSafeCacheLoader extends CacheLoader { + private final Function function; + + NullSafeCacheLoader(Function function) { + this.function = function; + } + + @Override public V load(final K key) { + V result = function.apply(key); + if (result != null) { + return result; + } + throw new EntryNotFoundException(); + } + } }