提交 cbc072fc authored 作者: S.Vladykin's avatar S.Vladykin

Better cleanup of view index caches.

上级 bbea3c39
......@@ -10,6 +10,7 @@ import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.Map;
import java.util.Random;
import org.h2.api.ErrorCode;
import org.h2.command.Command;
......@@ -112,7 +113,8 @@ public class Session extends SessionWithState {
private long modificationMetaID = -1;
private SubQueryInfo subQueryInfo;
private int parsingView;
private SmallLRUCache<Object, ViewIndex> viewIndexCache;
private volatile SmallLRUCache<Object, ViewIndex> viewIndexCache;
private HashMap<Object, ViewIndex> subQueryIndexCache;
/**
* Temporary LOBs from result sets. Those are kept for some time. The
......@@ -1315,11 +1317,20 @@ public class Session extends SessionWithState {
}
}
public SmallLRUCache<Object, ViewIndex> getViewIndexCache() {
if (viewIndexCache == null) {
viewIndexCache = SmallLRUCache.newInstance(Constants.VIEW_INDEX_CACHE_SIZE);
public Map<Object, ViewIndex> getViewIndexCache(boolean subQuery) {
if (subQuery) {
// for sub-queries we don't need to use LRU because it should not grow too large
// for a single query and by the end of the statement we will drop the whole cache
if (subQueryIndexCache == null) {
subQueryIndexCache = New.hashMap();
}
return subQueryIndexCache;
}
SmallLRUCache<Object, ViewIndex> cache = viewIndexCache;
if (cache == null) {
viewIndexCache = cache = SmallLRUCache.newInstance(Constants.VIEW_INDEX_CACHE_SIZE);
}
return viewIndexCache;
return cache;
}
/**
......@@ -1499,9 +1510,14 @@ public class Session extends SessionWithState {
*/
public void endStatement() {
startStatement = -1;
subQueryIndexCache = null;
closeTemporaryResults();
}
public void clearViewIndexCache() {
viewIndexCache = null;
}
@Override
public void addTemporaryLob(Value v) {
if (v.getType() != Value.CLOB && v.getType() != Value.BLOB) {
......
......@@ -8,6 +8,7 @@ package org.h2.table;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Map;
import org.h2.api.ErrorCode;
import org.h2.command.Prepared;
import org.h2.command.dml.Query;
......@@ -29,7 +30,6 @@ import org.h2.result.Row;
import org.h2.result.SortOrder;
import org.h2.schema.Schema;
import org.h2.util.New;
import org.h2.util.SmallLRUCache;
import org.h2.util.StatementBuilder;
import org.h2.util.StringUtils;
import org.h2.value.Value;
......@@ -228,14 +228,12 @@ public class TableView extends Table {
PlanItem item = new PlanItem();
item.cost = index.getCost(session, masks, filters, filter, sortOrder);
final CacheKey cacheKey = new CacheKey(masks, this);
SmallLRUCache<Object, ViewIndex> indexCache = session.getViewIndexCache();
Map<Object, ViewIndex> indexCache = session.getViewIndexCache(topQuery != null);
ViewIndex i = indexCache.get(cacheKey);
if (i != null) {
item.setIndex(i);
return item;
if (i == null) {
i = new ViewIndex(this, index, session, masks, filters, filter, sortOrder);
indexCache.put(cacheKey, i);
}
i = new ViewIndex(this, index, session, masks, filters, filter, sortOrder);
indexCache.put(cacheKey, i);
item.setIndex(i);
return item;
}
......@@ -257,6 +255,10 @@ public class TableView extends Table {
return true;
}
public Query getTopQuery() {
return topQuery;
}
@Override
public String getDropSQL() {
return "DROP VIEW IF EXISTS " + getSQL() + " CASCADE";
......@@ -397,6 +399,9 @@ public class TableView extends Table {
database.removeMeta(session, getId());
querySQL = null;
index = null;
for (Session s : database.getSessions(true)) {
s.clearViewIndexCache();
}
invalidate();
}
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论