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

Session local view index cache.

上级 2e613734
...@@ -19,6 +19,7 @@ import org.h2.command.Prepared; ...@@ -19,6 +19,7 @@ import org.h2.command.Prepared;
import org.h2.command.dml.SetTypes; import org.h2.command.dml.SetTypes;
import org.h2.constraint.Constraint; import org.h2.constraint.Constraint;
import org.h2.index.Index; import org.h2.index.Index;
import org.h2.index.ViewIndex;
import org.h2.jdbc.JdbcConnection; import org.h2.jdbc.JdbcConnection;
import org.h2.message.DbException; import org.h2.message.DbException;
import org.h2.message.Trace; import org.h2.message.Trace;
...@@ -111,6 +112,7 @@ public class Session extends SessionWithState { ...@@ -111,6 +112,7 @@ 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;
/** /**
* Temporary LOBs from result sets. Those are kept for some time. The * Temporary LOBs from result sets. Those are kept for some time. The
...@@ -1313,6 +1315,13 @@ public class Session extends SessionWithState { ...@@ -1313,6 +1315,13 @@ public class Session extends SessionWithState {
} }
} }
public SmallLRUCache<Object, ViewIndex> getViewIndexCache() {
if (viewIndexCache == null) {
viewIndexCache = SmallLRUCache.newInstance(Constants.VIEW_INDEX_CACHE_SIZE);
}
return viewIndexCache;
}
/** /**
* Remember the result set and close it as soon as the transaction is * Remember the result set and close it as soon as the transaction is
* committed (if it needs to be closed). This is done to delete temporary * committed (if it needs to be closed). This is done to delete temporary
...@@ -1549,5 +1558,4 @@ public class Session extends SessionWithState { ...@@ -1549,5 +1558,4 @@ public class Session extends SessionWithState {
} }
} }
} }
...@@ -32,7 +32,6 @@ import org.h2.util.New; ...@@ -32,7 +32,6 @@ import org.h2.util.New;
import org.h2.util.SmallLRUCache; 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.util.SynchronizedVerifier;
import org.h2.value.Value; import org.h2.value.Value;
/** /**
...@@ -51,8 +50,6 @@ public class TableView extends Table { ...@@ -51,8 +50,6 @@ public class TableView extends Table {
private ViewIndex index; private ViewIndex index;
private boolean recursive; private boolean recursive;
private DbException createException; private DbException createException;
private final SmallLRUCache<CacheKey, ViewIndex> indexCache =
SmallLRUCache.newInstance(Constants.VIEW_INDEX_CACHE_SIZE);
private long lastModificationCheck; private long lastModificationCheck;
private long maxDataModificationId; private long maxDataModificationId;
private User owner; private User owner;
...@@ -97,8 +94,6 @@ public class TableView extends Table { ...@@ -97,8 +94,6 @@ public class TableView extends Table {
this.columnNames = columnNames; this.columnNames = columnNames;
this.recursive = recursive; this.recursive = recursive;
index = new ViewIndex(this, querySQL, params, recursive); index = new ViewIndex(this, querySQL, params, recursive);
SynchronizedVerifier.check(indexCache);
indexCache.clear();
initColumnsAndTables(session); initColumnsAndTables(session);
} }
...@@ -136,8 +131,6 @@ public class TableView extends Table { ...@@ -136,8 +131,6 @@ public class TableView extends Table {
if (views != null) { if (views != null) {
views = New.arrayList(views); views = New.arrayList(views);
} }
SynchronizedVerifier.check(indexCache);
indexCache.clear();
initColumnsAndTables(session); initColumnsAndTables(session);
if (views != null) { if (views != null) {
for (TableView v : views) { for (TableView v : views) {
...@@ -234,30 +227,16 @@ public class TableView extends Table { ...@@ -234,30 +227,16 @@ public class TableView extends Table {
TableFilter[] filters, int filter, SortOrder sortOrder) { TableFilter[] filters, int filter, SortOrder sortOrder) {
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, session); final CacheKey cacheKey = new CacheKey(masks, this);
SmallLRUCache<Object, ViewIndex> indexCache = session.getViewIndexCache();
synchronized (this) { ViewIndex i = indexCache.get(cacheKey);
SynchronizedVerifier.check(indexCache); if (i != null) {
ViewIndex i2 = indexCache.get(cacheKey); item.setIndex(i);
if (i2 != null) {
item.setIndex(i2);
return item; return item;
} }
} i = new ViewIndex(this, index, session, masks, filters, filter, sortOrder);
// We cannot hold the lock during the ViewIndex creation or we risk ABBA indexCache.put(cacheKey, i);
// deadlocks if the view creation calls back into H2 via something like item.setIndex(i);
// a FunctionTable.
ViewIndex i2 = new ViewIndex(this, index, session, masks, filters, filter, sortOrder);
synchronized (this) {
// have to check again in case another session has beat us to it
ViewIndex i3 = indexCache.get(cacheKey);
if (i3 != null) {
item.setIndex(i3);
return item;
}
indexCache.put(cacheKey, i2);
item.setIndex(i2);
}
return item; return item;
} }
...@@ -596,11 +575,11 @@ public class TableView extends Table { ...@@ -596,11 +575,11 @@ public class TableView extends Table {
private static final class CacheKey { private static final class CacheKey {
private final int[] masks; private final int[] masks;
private final Session session; private final TableView view;
public CacheKey(int[] masks, Session session) { public CacheKey(int[] masks, TableView view) {
this.masks = masks; this.masks = masks;
this.session = session; this.view = view;
} }
@Override @Override
...@@ -608,7 +587,7 @@ public class TableView extends Table { ...@@ -608,7 +587,7 @@ public class TableView extends Table {
final int prime = 31; final int prime = 31;
int result = 1; int result = 1;
result = prime * result + Arrays.hashCode(masks); result = prime * result + Arrays.hashCode(masks);
result = prime * result + session.hashCode(); result = prime * result + view.hashCode();
return result; return result;
} }
...@@ -624,7 +603,7 @@ public class TableView extends Table { ...@@ -624,7 +603,7 @@ public class TableView extends Table {
return false; return false;
} }
CacheKey other = (CacheKey) obj; CacheKey other = (CacheKey) obj;
if (session != other.session) { if (view != other.view) {
return false; return false;
} }
if (!Arrays.equals(masks, other.masks)) { if (!Arrays.equals(masks, other.masks)) {
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论