Skip to content

Commit

Permalink
[CALCITE-6728] Introduce new methods to lookup tables and schemas ins…
Browse files Browse the repository at this point in the history
…ide schemas
  • Loading branch information
kramerul committed Dec 19, 2024
1 parent 45f68c0 commit 6e4d10b
Showing 1 changed file with 13 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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) {
Expand All @@ -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

View workflow job for this annotation

GitHub Actions / CheckerFramework (JDK 11)

[Task :core:compileJava] [argument.type.incompatible] incompatible argument for parameter v of put. map.put(name, delegate.get(name)); ^ found : T[ extends @initialized @nullable Object super @initialized @nullable Void]

Check failure on line 65 in core/src/main/java/org/apache/calcite/schema/lookup/CachedLookup.java

View workflow job for this annotation

GitHub Actions / CheckerFramework (JDK 11, oldest Guava)

[Task :core:compileJava] [argument.type.incompatible] incompatible argument for parameter v of put. map.put(name, delegate.get(name)); ^ found : T[ extends @initialized @nullable Object super @initialized @nullable Void]
}
}
return map;
}

public void enable(boolean enabled) {
if (!enabled) {
cachedDelegate.set(null);
cachedDelegate.reset();
}
this.enabled = enabled;
}
Expand Down

0 comments on commit 6e4d10b

Please sign in to comment.