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

Better cleanup of view index caches.

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