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
13 additions
and
18 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 |
---|---|---|
|
@@ -16,12 +16,12 @@ | |
*/ | ||
package org.apache.calcite.schema.lookup; | ||
|
||
import org.apache.calcite.util.LazyReference; | ||
import org.apache.calcite.util.NameMap; | ||
|
||
import org.checkerframework.checker.nullness.qual.Nullable; | ||
|
||
import java.util.Set; | ||
import java.util.concurrent.atomic.AtomicReference; | ||
|
||
/** | ||
* This class can be used to make a snapshot of a lookups. | ||
|
@@ -31,7 +31,7 @@ | |
public class CachedLookup<T> implements Lookup<T> { | ||
|
||
private final Lookup<T> delegate; | ||
private AtomicReference<Lookup<T>> cachedDelegate = new AtomicReference<>(); | ||
private LazyReference<Lookup<T>> cachedDelegate = new LazyReference<>(); | ||
private boolean enabled = true; | ||
|
||
public CachedLookup(Lookup<T> delegate) { | ||
|
@@ -54,28 +54,23 @@ private Lookup<T> delegate() { | |
if (!enabled) { | ||
return delegate; | ||
} | ||
while (true) { | ||
Lookup<T> cached = cachedDelegate.get(); | ||
if (cached != null) { | ||
return cached; | ||
} | ||
NameMap<T> map = new NameMap<>(); | ||
for (String name : delegate.getNames(LikePattern.any())) { | ||
T entry = delegate.get(name); | ||
if (entry != null) { | ||
map.put(name, delegate.get(name)); | ||
} | ||
} | ||
cached = new NameMapLookup<>(map); | ||
if (cachedDelegate.compareAndSet(null, cached)) { | ||
return cached; | ||
return cachedDelegate.getOrCompute(() -> new NameMapLookup<>(loadNameMap())); | ||
} | ||
|
||
private NameMap<T> loadNameMap() { | ||
NameMap<T> map = new NameMap<>(); | ||
for (String name : delegate.getNames(LikePattern.any())) { | ||
T entry = delegate.get(name); | ||
if (entry != null) { | ||
map.put(name, delegate.get(name)); | ||
Check failure on line 65 in core/src/main/java/org/apache/calcite/schema/lookup/CachedLookup.java GitHub Actions / CheckerFramework (JDK 11)
Check failure on line 65 in core/src/main/java/org/apache/calcite/schema/lookup/CachedLookup.java GitHub Actions / CheckerFramework (JDK 11, oldest Guava)
|
||
} | ||
} | ||
return map; | ||
} | ||
|
||
public void enable(boolean enabled) { | ||
if (!enabled) { | ||
cachedDelegate.set(null); | ||
cachedDelegate.reset(); | ||
} | ||
this.enabled = enabled; | ||
} | ||
|