/* * Copyright 2004-2019 H2 Group. Multiple-Licensed under the MPL 2.0, * and the EPL 1.0 (http://h2database.com/html/license.html). * Initial Developer: H2 Group */ package org.h2.mvstore.tx; import org.h2.mvstore.Cursor; import org.h2.mvstore.DataUtils; import org.h2.mvstore.MVMap; import org.h2.mvstore.Page; import org.h2.mvstore.RootReference; import org.h2.mvstore.type.DataType; import org.h2.value.VersionedValue; import java.util.AbstractMap; import java.util.AbstractSet; import java.util.BitSet; import java.util.Iterator; import java.util.Map; import java.util.NoSuchElementException; import java.util.Set; /** * A map that supports transactions. * *
* Methods of this class may be changed at any time without notice. If * you use this class directly make sure that your application or library * requires exactly the same version of MVStore or H2 jar as the version that * you use during its development and build. *
* * @param
* Key: key the key of the data.
* Value: { transactionId, oldVersion, value }
*/
public final MVMap
* If the row is locked, this method will retry until the row could be
* updated or until a lock timeout.
*
* @param key the key
* @throws IllegalStateException if a lock timeout occurs
* @throws ClassCastException if type of the specified key is not compatible with this map
*/
@Override
public V remove(Object key) {
return set(key, (V)null);
}
/**
* Update the value for the given key.
*
* If the row is locked, this method will retry until the row could be
* updated or until a lock timeout.
*
* @param key the key
* @param value the new value (not null)
* @return the old value
* @throws IllegalStateException if a lock timeout occurs
*/
@Override
public V put(K key, V value) {
DataUtils.checkArgument(value != null, "The value may not be null");
return set(key, value);
}
/**
* Put the value for the given key if entry for this key does not exist.
* It is atomic equivalent of the following expression:
* contains(key) ? get(k) : put(key, value);
*
* @param key the key
* @param value the new value (not null)
* @return the old value
*/
// Do not add @Override, code should be compatible with Java 7
public V putIfAbsent(K key, V value) {
DataUtils.checkArgument(value != null, "The value may not be null");
TxDecisionMaker decisionMaker = new TxDecisionMaker.PutIfAbsentDecisionMaker(map.getId(), key, value,
transaction);
return set(key, decisionMaker);
}
/**
* Appends entry to underlying map. This method may be used concurrently,
* but latest appended values are not guaranteed to be visible.
* @param key should be higher in map's order than any existing key
* @param value to be appended
*/
public void append(K key, V value) {
map.append(key, VersionedValueUncommitted.getInstance(transaction.log(map.getId(), key, null), value, null));
}
/**
* Lock row for the given key.
*
* If the row is locked, this method will retry until the row could be
* updated or until a lock timeout.
*
* @param key the key
* @return the locked value
* @throws IllegalStateException if a lock timeout occurs
*/
public V lock(K key) {
TxDecisionMaker decisionMaker = new TxDecisionMaker.LockDecisionMaker(map.getId(), key, transaction);
return set(key, decisionMaker);
}
/**
* Update the value for the given key, without adding an undo log entry.
*
* @param key the key
* @param value the value
* @return the old value
*/
public V putCommitted(K key, V value) {
DataUtils.checkArgument(value != null, "The value may not be null");
VersionedValue newValue = VersionedValueCommitted.getInstance(value);
VersionedValue oldValue = map.put(key, newValue);
@SuppressWarnings("unchecked")
V result = (V) (oldValue == null ? null : oldValue.getCurrentValue());
return result;
}
private V set(Object key, V value) {
TxDecisionMaker decisionMaker = new TxDecisionMaker.PutDecisionMaker(map.getId(), key, value, transaction);
return set(key, decisionMaker);
}
private V set(Object key, TxDecisionMaker decisionMaker) {
TransactionStore store = transaction.store;
Transaction blockingTransaction;
long sequenceNumWhenStarted;
VersionedValue result;
do {
sequenceNumWhenStarted = store.openTransactions.get().getVersion();
assert transaction.getBlockerId() == 0;
// although second parameter (value) is not really used,
// since TxDecisionMaker has it embedded,
// MVRTreeMap has weird traversal logic based on it,
// and any non-null value will do
@SuppressWarnings("unchecked")
K k = (K) key;
result = map.operate(k, VersionedValue.DUMMY, decisionMaker);
MVMap.Decision decision = decisionMaker.getDecision();
assert decision != null;
assert decision != MVMap.Decision.REPEAT;
blockingTransaction = decisionMaker.getBlockingTransaction();
if (decision != MVMap.Decision.ABORT || blockingTransaction == null) {
@SuppressWarnings("unchecked")
V res = result == null ? null : (V) result.getCurrentValue();
return res;
}
decisionMaker.reset();
} while (blockingTransaction.sequenceNum > sequenceNumWhenStarted
|| transaction.waitFor(blockingTransaction, map, key));
throw DataUtils.newIllegalStateException(DataUtils.ERROR_TRANSACTION_LOCKED,
"Map entry <{0}> with key <{1}> and value {2} is locked by tx {3} and can not be updated by tx {4}"
+ " within allocated time interval {5} ms.",
map.getName(), key, result, blockingTransaction.transactionId, transaction.transactionId,
transaction.timeoutMillis);
}
/**
* Try to remove the value for the given key.
*
* This will fail if the row is locked by another transaction (that
* means, if another open transaction changed the row).
*
* @param key the key
* @return whether the entry could be removed
*/
public boolean tryRemove(K key) {
return trySet(key, null);
}
/**
* Try to update the value for the given key.
*
* This will fail if the row is locked by another transaction (that
* means, if another open transaction changed the row).
*
* @param key the key
* @param value the new value
* @return whether the entry could be updated
*/
public boolean tryPut(K key, V value) {
DataUtils.checkArgument(value != null, "The value may not be null");
return trySet(key, value);
}
/**
* Try to set or remove the value. When updating only unchanged entries,
* then the value is only changed if it was not changed after opening
* the map.
*
* @param key the key
* @param value the new value (null to remove the value)
* @return true if the value was set, false if there was a concurrent
* update
*/
public boolean trySet(K key, V value) {
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);
return true;
} catch (IllegalStateException e) {
return false;
}
}
/**
* Get the effective value for the given key.
*
* @param key the key
* @return the value or null
* @throws ClassCastException if type of the specified key is not compatible with this map
*/
@Override
@SuppressWarnings("unchecked")
public V get(Object key) {
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.getCurrentValue();
}
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.getCurrentValue();
} else {
return (V) data.getCommittedValue();
}
}
/**
* Whether the map contains the key.
*
* @param key the key
* @return true if the map contains an entry for this key
* @throws ClassCastException if type of the specified key is not compatible with this map
*/
@Override
public boolean containsKey(Object key) {
return get(key) != null;
}
/**
* Whether the entry for this key was added or removed from this
* session.
*
* @param key the key
* @return true if yes
*/
public boolean isSameTransaction(K key) {
VersionedValue data = map.get(key);
if (data == null) {
// doesn't exist or deleted by a committed transaction
return false;
}
int tx = TransactionStore.getTransactionId(data.getOperationId());
return tx == transaction.transactionId;
}
/**
* Check whether this map is closed.
*
* @return true if closed
*/
public boolean isClosed() {
return map.isClosed();
}
/**
* Clear the map.
*/
@Override
public void clear() {
// TODO truncate transactionally?
map.clear();
}
@Override
public Set