Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -9,24 +9,25 @@
import com.scalar.db.exception.storage.ExecutionException;
import com.scalar.db.util.ScalarDbUtils;
import java.util.Objects;
import java.util.Optional;
import java.util.concurrent.CompletionException;
import java.util.concurrent.TimeUnit;
import javax.annotation.Nullable;
import javax.annotation.concurrent.ThreadSafe;

/** A class that manages and caches virtual table information */
/** A class that manages and caches virtual table information. */
@ThreadSafe
public class VirtualTableInfoManager {

private final LoadingCache<TableKey, VirtualTableInfo> virtualTableInfoCache;
private final LoadingCache<TableKey, Optional<VirtualTableInfo>> virtualTableInfoCache;

public VirtualTableInfoManager(DistributedStorageAdmin admin, long cacheExpirationTimeSecs) {
Caffeine<Object, Object> builder = Caffeine.newBuilder();
if (cacheExpirationTimeSecs >= 0) {
builder.expireAfterWrite(cacheExpirationTimeSecs, TimeUnit.SECONDS);
}
virtualTableInfoCache =
builder.build(key -> admin.getVirtualTableInfo(key.namespace, key.table).orElse(null));
builder.build(key -> admin.getVirtualTableInfo(key.namespace, key.table));
}

/**
Expand Down Expand Up @@ -60,7 +61,9 @@ public VirtualTableInfo getVirtualTableInfo(String namespace, String table)
throws ExecutionException {
try {
TableKey key = new TableKey(namespace, table);
return virtualTableInfoCache.get(key);
Optional<VirtualTableInfo> virtualTableInfo = virtualTableInfoCache.get(key);
assert virtualTableInfo != null;
return virtualTableInfo.orElse(null);
} catch (CompletionException e) {
throw new ExecutionException(
CoreError.GETTING_VIRTUAL_TABLE_INFO_FAILED.buildMessage(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ public void getVirtualTableInfo_WithCacheExpiration_ShouldExpireCache() throws E
}

@Test
public void getVirtualTableInfo_VirtualTableDoesNotExist_ShouldNotCacheNullResult()
public void getVirtualTableInfo_VirtualTableDoesNotExist_ShouldCacheEmptyResult()
throws Exception {
// Arrange
manager = new VirtualTableInfoManager(admin, -1);
Expand All @@ -168,8 +168,8 @@ public void getVirtualTableInfo_VirtualTableDoesNotExist_ShouldNotCacheNullResul
manager.getVirtualTableInfo("ns", "table");
manager.getVirtualTableInfo("ns", "table");

// Assert - admin should be called multiple times because null is not cached
verify(admin, times(3)).getVirtualTableInfo("ns", "table");
// Assert - admin should be called only once because Optional.empty() is cached
verify(admin, times(1)).getVirtualTableInfo("ns", "table");
}

@Test
Expand Down