com.scalar.db.sql.common.metadata.CachedMetadata Maven / Gradle / Ivy
package com.scalar.db.sql.common.metadata;
import com.google.common.cache.CacheBuilder;
import com.google.common.cache.CacheLoader;
import com.google.common.cache.LoadingCache;
import com.google.common.collect.ImmutableList;
import com.google.common.util.concurrent.UncheckedExecutionException;
import com.scalar.db.sql.metadata.Metadata;
import com.scalar.db.sql.metadata.NamespaceMetadata;
import com.scalar.db.sql.metadata.UserMetadata;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.concurrent.TimeUnit;
import javax.annotation.Nonnull;
import javax.annotation.concurrent.ThreadSafe;
@ThreadSafe
public class CachedMetadata implements Metadata {
private static final String NAMESPACE_NAMES_CACHE_KEY = "key";
/**
* A cache for namespace metadata. The key is a namespace name, and the value is a namespace
* metadata.
*/
private final LoadingCache> namespaceMetadataCache;
/**
* A cache for namespace names. The key is a fixed string {@code NAMESPACE_NAMES_CACHE_KEY}, and
* the value is a list of namespace names. It is assumed that this cache has only one entry for
* the fixed string.
*/
private final LoadingCache> namespaceNamesCache;
private final Metadata metadata;
private CachedMetadata(Metadata metadata, long cacheExpirationTimeSecs) {
this.metadata = metadata;
namespaceMetadataCache =
createNamespaceMetadataCache(Objects.requireNonNull(metadata), cacheExpirationTimeSecs);
namespaceNamesCache = createNamespaceNamesCache(metadata, cacheExpirationTimeSecs);
}
private LoadingCache> createNamespaceMetadataCache(
Metadata metadata, long cacheExpirationTimeSecs) {
CacheBuilder