提交 69b907a3 authored 作者: noelgrandin@gmail.com's avatar noelgrandin@gmail.com

use a ConcurrentHashMap as a Set to avoid taking the lock where possible

上级 1ecb2f8c
...@@ -9,8 +9,8 @@ import java.util.ArrayDeque; ...@@ -9,8 +9,8 @@ import java.util.ArrayDeque;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collections; import java.util.Collections;
import java.util.Comparator; import java.util.Comparator;
import java.util.HashSet;
import java.util.Set; import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import org.h2.api.DatabaseEventListener; import org.h2.api.DatabaseEventListener;
import org.h2.api.ErrorCode; import org.h2.api.ErrorCode;
import org.h2.command.ddl.Analyze; import org.h2.command.ddl.Analyze;
...@@ -50,7 +50,8 @@ public class MVTable extends TableBase { ...@@ -50,7 +50,8 @@ public class MVTable extends TableBase {
private final ArrayList<Index> indexes = New.arrayList(); private final ArrayList<Index> indexes = New.arrayList();
private long lastModificationId; private long lastModificationId;
private volatile Session lockExclusiveSession; private volatile Session lockExclusiveSession;
private final HashSet<Session> lockSharedSessions = New.hashSet(); /** using a ConcurrentHashMap as a Set */
private final ConcurrentHashMap<Session, Session> lockSharedSessions = new ConcurrentHashMap<Session, Session>();
/** /**
* The queue of sessions waiting to lock the table. It is a FIFO queue to * The queue of sessions waiting to lock the table. It is a FIFO queue to
...@@ -116,6 +117,9 @@ public class MVTable extends TableBase { ...@@ -116,6 +117,9 @@ public class MVTable extends TableBase {
if (lockExclusiveSession == session) { if (lockExclusiveSession == session) {
return true; return true;
} }
if (!exclusive && lockSharedSessions.contains(session)) {
return true;
}
synchronized (getLockSyncObject()) { synchronized (getLockSyncObject()) {
if (!exclusive && lockSharedSessions.contains(session)) { if (!exclusive && lockSharedSessions.contains(session)) {
return true; return true;
...@@ -234,7 +238,7 @@ public class MVTable extends TableBase { ...@@ -234,7 +238,7 @@ public class MVTable extends TableBase {
if (!lockSharedSessions.contains(session)) { if (!lockSharedSessions.contains(session)) {
traceLock(session, exclusive, "ok"); traceLock(session, exclusive, "ok");
session.addLock(this); session.addLock(this);
lockSharedSessions.add(session); lockSharedSessions.put(session, session);
} }
return true; return true;
} }
...@@ -293,7 +297,7 @@ public class MVTable extends TableBase { ...@@ -293,7 +297,7 @@ public class MVTable extends TableBase {
} }
visited.add(session); visited.add(session);
ArrayList<Session> error = null; ArrayList<Session> error = null;
for (Session s : lockSharedSessions) { for (Session s : lockSharedSessions.keySet()) {
if (s == session) { if (s == session) {
// it doesn't matter if we have locked the object already // it doesn't matter if we have locked the object already
continue; continue;
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论