提交 75a32ce8 authored 作者: Andrei Tokar's avatar Andrei Tokar

drop TransactionMap.readLogId

上级 52dec4ac
...@@ -380,7 +380,7 @@ public class MVPrimaryIndex extends BaseIndex { ...@@ -380,7 +380,7 @@ public class MVPrimaryIndex extends BaseIndex {
return dataMap; return dataMap;
} }
Transaction t = session.getTransaction(); Transaction t = session.getTransaction();
return dataMap.getInstance(t, Long.MAX_VALUE); return dataMap.getInstance(t);
} }
/** /**
......
...@@ -463,7 +463,7 @@ public final class MVSecondaryIndex extends BaseIndex implements MVIndex { ...@@ -463,7 +463,7 @@ public final class MVSecondaryIndex extends BaseIndex implements MVIndex {
return dataMap; return dataMap;
} }
Transaction t = session.getTransaction(); Transaction t = session.getTransaction();
return dataMap.getInstance(t, Long.MAX_VALUE); return dataMap.getInstance(t);
} }
/** /**
......
...@@ -328,7 +328,7 @@ public class MVSpatialIndex extends BaseIndex implements SpatialIndex, MVIndex { ...@@ -328,7 +328,7 @@ public class MVSpatialIndex extends BaseIndex implements SpatialIndex, MVIndex {
return dataMap; return dataMap;
} }
Transaction t = session.getTransaction(); Transaction t = session.getTransaction();
return dataMap.getInstance(t, Long.MAX_VALUE); return dataMap.getInstance(t);
} }
/** /**
......
...@@ -24,14 +24,6 @@ import java.util.Map; ...@@ -24,14 +24,6 @@ import java.util.Map;
*/ */
public class TransactionMap<K, V> { public class TransactionMap<K, V> {
/**
* If a record was read that was updated by this transaction, and the
* update occurred before this log id, the older version is read. This
* is so that changes are not immediately visible, to support statement
* processing (for example "update test set id = id + 1").
*/
long readLogId = Long.MAX_VALUE;
/** /**
* The map used for writing (the latest version). * The map used for writing (the latest version).
* <p> * <p>
...@@ -50,29 +42,14 @@ public class TransactionMap<K, V> { ...@@ -50,29 +42,14 @@ public class TransactionMap<K, V> {
this.map = map; this.map = map;
} }
/**
* Set the savepoint. Afterwards, reads are based on the specified
* savepoint.
*
* @param savepoint the savepoint
*/
public void setSavepoint(long savepoint) {
this.readLogId = savepoint;
}
/** /**
* Get a clone of this map for the given transaction. * Get a clone of this map for the given transaction.
* *
* @param transaction the transaction * @param transaction the transaction
* @param savepoint the savepoint
* @return the map * @return the map
*/ */
public TransactionMap<K, V> getInstance(Transaction transaction, public TransactionMap<K, V> getInstance(Transaction transaction) {
long savepoint) { return new TransactionMap<>(transaction, map);
TransactionMap<K, V> m =
new TransactionMap<>(transaction, map);
m.setSavepoint(savepoint);
return m;
} }
/** /**
...@@ -114,11 +91,11 @@ public class TransactionMap<K, V> { ...@@ -114,11 +91,11 @@ public class TransactionMap<K, V> {
// the undo log is larger than the map - // the undo log is larger than the map -
// count the entries of the map // count the entries of the map
size = 0; size = 0;
Cursor<K, VersionedValue> cursor = map.cursor(null); Cursor<K, VersionedValue> cursor = new Cursor<>(mapRootPage, null);
while (cursor.hasNext()) { while (cursor.hasNext()) {
K key = cursor.next(); K key = cursor.next();
VersionedValue data = cursor.getValue(); VersionedValue data = cursor.getValue();
data = getValue(mapRootPage, undoRootPage, key, readLogId, data, committingTransactions); data = getValue(data, committingTransactions);
if (data != null && data.value != null) { if (data != null && data.value != null) {
size++; size++;
} }
...@@ -141,7 +118,7 @@ public class TransactionMap<K, V> { ...@@ -141,7 +118,7 @@ public class TransactionMap<K, V> {
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
K key = (K) op[1]; K key = (K) op[1];
VersionedValue data = map.get(mapRootPage, key); VersionedValue data = map.get(mapRootPage, key);
data = getValue(mapRootPage, undoRootPage, key, readLogId, data, committingTransactions); data = getValue(data, committingTransactions);
if (data == null || data.value == null) { if (data == null || data.value == null) {
Integer old = temp.put(key, 1); Integer old = temp.put(key, 1);
// count each key only once (there might be // count each key only once (there might be
...@@ -283,7 +260,7 @@ public class TransactionMap<K, V> { ...@@ -283,7 +260,7 @@ public class TransactionMap<K, V> {
* @return whether the entry could be removed * @return whether the entry could be removed
*/ */
public boolean tryRemove(K key) { public boolean tryRemove(K key) {
return trySet(key, null, false); return trySet(key, null);
} }
/** /**
...@@ -298,7 +275,7 @@ public class TransactionMap<K, V> { ...@@ -298,7 +275,7 @@ public class TransactionMap<K, V> {
*/ */
public boolean tryPut(K key, V value) { public boolean tryPut(K key, V value) {
DataUtils.checkArgument(value != null, "The value may not be null"); DataUtils.checkArgument(value != null, "The value may not be null");
return trySet(key, value, false); return trySet(key, value);
} }
/** /**
...@@ -308,50 +285,13 @@ public class TransactionMap<K, V> { ...@@ -308,50 +285,13 @@ public class TransactionMap<K, V> {
* *
* @param key the key * @param key the key
* @param value the new value (null to remove the value) * @param value the new value (null to remove the value)
* @param onlyIfUnchanged only set the value if it was not changed (by
* this or another transaction) since the map was opened
* @return true if the value was set, false if there was a concurrent * @return true if the value was set, false if there was a concurrent
* update * update
*/ */
public boolean trySet(K key, V value, boolean onlyIfUnchanged) { public boolean trySet(K key, V value) {
VersionedValue current;
if (onlyIfUnchanged) {
TransactionStore store = transaction.store;
BitSet committingTransactions;
MVMap.RootReference mapRootReference;
MVMap.RootReference undoLogRootReference;
do {
committingTransactions = store.committingTransactions.get();
mapRootReference = map.getRoot();
undoLogRootReference = store.undoLog.getRoot();
} while(committingTransactions != store.committingTransactions.get() ||
mapRootReference != map.getRoot());
Page mapRootPage = mapRootReference.root;
current = map.get(mapRootPage, key);
VersionedValue old = getValue(mapRootPage, undoLogRootReference.root, key, readLogId, current,
committingTransactions);
if (!map.areValuesEqual(old, current)) {
assert current != null;
long tx = TransactionStore.getTransactionId(current.getOperationId());
if (tx == transaction.transactionId) {
if (value == null) {
// ignore removing an entry
// if it was added or changed
// in the same statement
return true;
} else if (current.value == null) {
// add an entry that was removed
// in the same statement
} else {
return false;
}
} else {
return false;
}
}
}
try { try {
// TODO: effective transaction.timeoutMillis should be set to 0 here and restored before return
// TODO: eliminate exception usage as part of normal control flaw
set(key, value); set(key, value);
return true; return true;
} catch (IllegalStateException e) { } catch (IllegalStateException e) {
...@@ -365,8 +305,25 @@ public class TransactionMap<K, V> { ...@@ -365,8 +305,25 @@ public class TransactionMap<K, V> {
* @param key the key * @param key the key
* @return the value or null * @return the value or null
*/ */
@SuppressWarnings("unchecked")
public V get(K key) { public V get(K key) {
return get(key, readLogId); VersionedValue data = map.get(key);
if (data == null) {
// doesn't exist or deleted by a committed transaction
return null;
}
long id = data.getOperationId();
if (id == 0) {
// it is committed
return (V)data.value;
}
int tx = TransactionStore.getTransactionId(id);
if (tx == transaction.transactionId || transaction.store.committingTransactions.get().get(tx)) {
// added by this transaction or another transaction which is committed by now
return (V)data.value;
} else {
return (V) data.getCommittedValue();
}
} }
/** /**
...@@ -379,19 +336,6 @@ public class TransactionMap<K, V> { ...@@ -379,19 +336,6 @@ public class TransactionMap<K, V> {
return get(key) != null; return get(key) != null;
} }
/**
* Get the effective value for the given key.
*
* @param key the key
* @param maxLogId the maximum log id
* @return the value or null
*/
@SuppressWarnings("unchecked")
public V get(K key, long maxLogId) {
VersionedValue data = getValue(key, maxLogId);
return data == null ? null : (V) data.value;
}
/** /**
* Whether the entry for this key was added or removed from this * Whether the entry for this key was added or removed from this
* session. * session.
...@@ -409,76 +353,30 @@ public class TransactionMap<K, V> { ...@@ -409,76 +353,30 @@ public class TransactionMap<K, V> {
return tx == transaction.transactionId; return tx == transaction.transactionId;
} }
private VersionedValue getValue(K key, long maxLog) {
TransactionStore store = transaction.store;
BitSet committingTransactions;
MVMap.RootReference mapRootReference;
MVMap.RootReference undoLogRootReference;
do {
committingTransactions = store.committingTransactions.get();
mapRootReference = map.getRoot();
undoLogRootReference = store.undoLog.getRoot();
} while(committingTransactions != store.committingTransactions.get() ||
mapRootReference != map.getRoot());
Page undoRootPage = undoLogRootReference.root;
Page mapRootPage = mapRootReference.root;
VersionedValue data = map.get(mapRootPage, key);
return getValue(mapRootPage, undoRootPage, key, maxLog, data, committingTransactions);
}
/** /**
* Get the versioned value from the raw versioned value (possibly uncommitted), * Get the versioned value from the raw versioned value (possibly uncommitted),
* as visible by the current transaction. * as visible by the current transaction.
* *
* @param root Page of the map to get value from at the time when snapshot was taken
* @param undoRoot Page of the undoLog map at the time when snapshot was taken
* @param key the key
* @param maxLog the maximum log id of the entry
* @param data the value stored in the main map * @param data the value stored in the main map
* @param committingTransactions set of transactions being committed * @param committingTransactions set of transactions being committed
* at the time when snapshot was taken * at the time when snapshot was taken
* @return the value * @return the value
*/ */
VersionedValue getValue(Page root, Page undoRoot, K key, long maxLog, VersionedValue getValue(VersionedValue data, BitSet committingTransactions) {
VersionedValue data, BitSet committingTransactions) {
// TODO: This method is overly complicated and has a bunch of extra parameters
// TODO: to support maxLog feature, which is not really used by H2
long id; long id;
int tx; int tx;
while (true) { // If value doesn't exist or it was deleted by a committed transaction,
// If value doesn't exist or it was deleted by a committed transaction, // or if value is a committed one, just return it.
// or if value is a committed one, just return it. if (data != null &&
if (data != null && (id = data.getOperationId()) != 0 &&
(id = data.getOperationId()) != 0) { ((tx = TransactionStore.getTransactionId(id)) != transaction.transactionId &&
if ((tx = TransactionStore.getTransactionId(id)) == transaction.transactionId) { !committingTransactions.get(tx))) {
// current value comes from our transaction // current value comes from another uncommitted transaction
if (TransactionStore.getLogId(id) >= maxLog) { // take committed value instead
Object d[] = transaction.store.undoLog.get(undoRoot, id); Object committedValue = data.getCommittedValue();
if (d == null) { data = committedValue == null ? null : VersionedValue.getInstance(committedValue);
if (transaction.store.store.isReadOnly()) {
// uncommitted transaction for a read-only store
return null;
}
// this entry should be committed or rolled back
// in the meantime (the transaction might still be open)
// or it might be changed again in a different
// transaction (possibly one with the same id)
data = map.get(root, key);
} else {
data = (VersionedValue) d[2];
}
continue;
}
} else if (!committingTransactions.get(tx)) {
// current value comes from another uncommitted transaction
// take committed value instead
Object committedValue = data.getCommittedValue();
data = committedValue == null ? null : VersionedValue.getInstance(committedValue);
}
}
return data;
} }
return data;
} }
/** /**
...@@ -717,8 +615,6 @@ public class TransactionMap<K, V> { ...@@ -717,8 +615,6 @@ public class TransactionMap<K, V> {
private final TransactionMap<K,?> transactionMap; private final TransactionMap<K,?> transactionMap;
private final BitSet committingTransactions; private final BitSet committingTransactions;
private final Cursor<K,VersionedValue> cursor; private final Cursor<K,VersionedValue> cursor;
private final Page root;
private final Page undoRoot;
private final boolean includeAllUncommitted; private final boolean includeAllUncommitted;
private X current; private X current;
...@@ -728,16 +624,11 @@ public class TransactionMap<K, V> { ...@@ -728,16 +624,11 @@ public class TransactionMap<K, V> {
MVMap<K, VersionedValue> map = transactionMap.map; MVMap<K, VersionedValue> map = transactionMap.map;
BitSet committingTransactions; BitSet committingTransactions;
MVMap.RootReference mapRootReference; MVMap.RootReference mapRootReference;
Page undoRoot;
do { do {
committingTransactions = store.committingTransactions.get(); committingTransactions = store.committingTransactions.get();
undoRoot = store.undoLog.getRootPage();
mapRootReference = map.getRoot(); mapRootReference = map.getRoot();
} while (committingTransactions != store.committingTransactions.get() } while (committingTransactions != store.committingTransactions.get());
|| undoRoot != store.undoLog.getRootPage());
this.root = mapRootReference.root;
this.undoRoot = undoRoot;
this.cursor = new Cursor<>(mapRootReference.root, from, to); this.cursor = new Cursor<>(mapRootReference.root, from, to);
this.includeAllUncommitted = includeAllUncommitted; this.includeAllUncommitted = includeAllUncommitted;
this.committingTransactions = committingTransactions; this.committingTransactions = committingTransactions;
...@@ -750,8 +641,7 @@ public class TransactionMap<K, V> { ...@@ -750,8 +641,7 @@ public class TransactionMap<K, V> {
K key = cursor.next(); K key = cursor.next();
VersionedValue data = cursor.getValue(); VersionedValue data = cursor.getValue();
if (!includeAllUncommitted) { if (!includeAllUncommitted) {
data = transactionMap.getValue(root, undoRoot, key, transactionMap.readLogId, data = transactionMap.getValue(data, committingTransactions);
data, committingTransactions);
} }
if (data != null && (data.value != null || if (data != null && (data.value != null ||
includeAllUncommitted && transactionMap.transaction.transactionId != includeAllUncommitted && transactionMap.transaction.transactionId !=
......
...@@ -55,7 +55,6 @@ public class TestTransactionStore extends TestBase { ...@@ -55,7 +55,6 @@ public class TestTransactionStore extends TestBase {
testStopWhileCommitting(); testStopWhileCommitting();
testGetModifiedMaps(); testGetModifiedMaps();
testKeyIterator(); testKeyIterator();
testMultiStatement();
testTwoPhaseCommit(); testTwoPhaseCommit();
testSavepoint(); testSavepoint();
testConcurrentTransactionsReadCommitted(); testConcurrentTransactionsReadCommitted();
...@@ -537,96 +536,6 @@ public class TestTransactionStore extends TestBase { ...@@ -537,96 +536,6 @@ public class TestTransactionStore extends TestBase {
s.close(); s.close();
} }
/**
* Tests behavior when used for a sequence of SQL statements. Each statement
* uses a savepoint. Within a statement, changes by the statement itself are
* not seen; the change is only seen when the statement finished.
* <p>
* Update statements that change the key of multiple rows may use delete/add
* pairs to do so (they don't need to first delete all entries and then
* re-add them). Trying to add multiple values for the same key is not
* allowed (an update statement that would result in a duplicate key).
*/
private void testMultiStatement() {
MVStore s = MVStore.open(null);
TransactionStore ts = new TransactionStore(s);
ts.init();
Transaction tx;
TransactionMap<String, String> m;
long startUpdate;
tx = ts.begin();
// start of statement
// create table test
startUpdate = tx.setSavepoint();
m = tx.openMap("test");
m.setSavepoint(startUpdate);
// start of statement
// insert into test(id, name) values(1, 'Hello'), (2, 'World')
startUpdate = tx.setSavepoint();
m.setSavepoint(startUpdate);
assertTrue(m.trySet("1", "Hello", true));
assertTrue(m.trySet("2", "World", true));
// not seen yet (within the same statement)
assertNull(m.get("1"));
assertNull(m.get("2"));
// start of statement
startUpdate = tx.setSavepoint();
// now we see the newest version
m.setSavepoint(startUpdate);
assertEquals("Hello", m.get("1"));
assertEquals("World", m.get("2"));
// update test set primaryKey = primaryKey + 1
// (this is usually a tricky case)
assertEquals("Hello", m.get("1"));
assertTrue(m.trySet("1", null, true));
assertTrue(m.trySet("2", "Hello", true));
assertEquals("World", m.get("2"));
// already updated by this statement, so it has no effect
// but still returns true because it was changed by this transaction
assertTrue(m.trySet("2", null, true));
assertTrue(m.trySet("3", "World", true));
// not seen within this statement
assertEquals("Hello", m.get("1"));
assertEquals("World", m.get("2"));
assertNull(m.get("3"));
// start of statement
startUpdate = tx.setSavepoint();
m.setSavepoint(startUpdate);
// select * from test
assertNull(m.get("1"));
assertEquals("Hello", m.get("2"));
assertEquals("World", m.get("3"));
// start of statement
startUpdate = tx.setSavepoint();
m.setSavepoint(startUpdate);
// update test set id = 1
// should fail: duplicate key
assertTrue(m.trySet("2", null, true));
assertTrue(m.trySet("1", "Hello", true));
assertTrue(m.trySet("3", null, true));
assertFalse(m.trySet("1", "World", true));
tx.rollbackToSavepoint(startUpdate);
startUpdate = tx.setSavepoint();
m.setSavepoint(startUpdate);
assertNull(m.get("1"));
assertEquals("Hello", m.get("2"));
assertEquals("World", m.get("3"));
tx.commit();
ts.close();
s.close();
}
private void testTwoPhaseCommit() { private void testTwoPhaseCommit() {
String fileName = getBaseDir() + "/testTwoPhaseCommit.h3"; String fileName = getBaseDir() + "/testTwoPhaseCommit.h3";
FileUtils.delete(fileName); FileUtils.delete(fileName);
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论