com.scalar.db.sql.common.metadata.CachedNamespaceMetadata 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.NamespaceMetadata;
import com.scalar.db.sql.metadata.TableMetadata;
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 CachedNamespaceMetadata extends AbstractNamespaceMetadata {
private final NamespaceMetadata namespaceMetadata;
/** A cache for table metadata. The key is a table name, and the value is a table metadata. */
private final LoadingCache> tableMetadataCache;
/**
* A cache for table names. The key is a namespace name, and the value is a list of table names.
* It is assumed that this cache has only one entry for the namespace.
*/
private final LoadingCache> tableNamesCache;
CachedNamespaceMetadata(NamespaceMetadata namespaceMetadata, long cacheExpirationTimeSecs) {
this.namespaceMetadata = Objects.requireNonNull(namespaceMetadata);
tableMetadataCache = createTableMetadataCache(cacheExpirationTimeSecs);
tableNamesCache = createTableNamesCache(cacheExpirationTimeSecs);
}
private LoadingCache> createTableMetadataCache(
long cacheExpirationTimeSecs) {
CacheBuilder