forked from apache/calcite
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[CALCITE-6728] Introduce new methods to lookup tables and schemas ins…
…ide schemas
- Loading branch information
Showing
1 changed file
with
24 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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<T> 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<String, T>(delegate::get)); | ||
Check failure on line 46 in core/src/main/java/org/apache/calcite/schema/lookup/LoadingCacheLookup.java GitHub Actions / CheckerFramework (JDK 11)
Check failure on line 46 in core/src/main/java/org/apache/calcite/schema/lookup/LoadingCacheLookup.java GitHub Actions / CheckerFramework (JDK 11, oldest Guava)
|
||
this.cacheIgnoreCase = CacheBuilder.newBuilder() | ||
.expireAfterWrite(1, TimeUnit.MINUTES) | ||
.build( | ||
CacheLoader.from(name -> { | ||
Named<T> 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<T> delegate) { | |
return delegate.getNames(pattern); | ||
} | ||
|
||
/** | ||
* CacheLoader which doesn't return null values. | ||
* | ||
* @param <K> Key | ||
* @param <V> Value | ||
*/ | ||
private static class NullSafeCacheLoader<K, V> extends CacheLoader<K, V> { | ||
private final Function<K, @Nullable V> function; | ||
|
||
NullSafeCacheLoader(Function<K, @Nullable V> function) { | ||
this.function = function; | ||
} | ||
|
||
@Override public V load(final K key) { | ||
V result = function.apply(key); | ||
if (result != null) { | ||
return result; | ||
} | ||
throw new EntryNotFoundException(); | ||
} | ||
} | ||
} |