/* * Copyright 2004-2023 H2 Group. Multiple-Licensed under the MPL 2.0, * and the EPL 1.0 (https://h2database.com/html/license.html). * Initial Developer: H2 Group */ package org.h2.mvstore.tx; 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; import java.util.concurrent.atomic.AtomicReference; import java.util.function.BiFunction; import org.h2.engine.IsolationLevel; import org.h2.mvstore.Cursor; import org.h2.mvstore.DataUtils; import org.h2.mvstore.MVMap; import org.h2.mvstore.MVStoreException; import org.h2.mvstore.RootReference; import org.h2.mvstore.type.DataType; import org.h2.value.VersionedValue; /** * 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 MVStoreException if a lock timeout occurs
* @throws ClassCastException if type of the specified key is not compatible with this map
*/
@SuppressWarnings("unchecked")
@Override
public V remove(Object key) {
return set((K)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 MVStoreException 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
*/
@Override
public V putIfAbsent(K key, V value) {
DataUtils.checkArgument(value != null, "The value may not be null");
ifAbsentDecisionMaker.initialize(key, value);
V result = set(key, ifAbsentDecisionMaker, -1);
if (ifAbsentDecisionMaker.getDecision() == MVMap.Decision.ABORT) {
result = ifAbsentDecisionMaker.getLastValue();
}
return result;
}
/**
* 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(new Record<>(map.getId(), key, null)), value, null));
hasChanges = true;
}
/**
* 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 MVStoreException if a lock timeout occurs
*/
public V lock(K key) {
return lock(key, -1);
}
/**
* 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
* @param timeoutMillis
* timeout in milliseconds, {@code -1} for default, {@code -2} to
* skip locking if row is already locked by another transaction
* @return the locked value
* @throws MVStoreException if a lock timeout occurs
*/
public V lock(K key, int timeoutMillis) {
lockDecisionMaker.initialize(key, null);
return set(key, lockDecisionMaker, timeoutMillis);
}
/**
* 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
*/
@SuppressWarnings("UnusedReturnValue")
public V putCommitted(K key, V value) {
DataUtils.checkArgument(value != null, "The value may not be null");
VersionedValue
* 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 (MVStoreException 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
*/
@SuppressWarnings("unchecked")
@Override
public V get(Object key) {
return getImmediate((K)key);
}
/**
* Get the value for the given key, or null if value does not exist in accordance with transactional rules.
* Value is taken from a snapshot, appropriate for an isolation level of the related transaction
*
* @param key the key
* @return the value, or null if not found
*/
public V getFromSnapshot(K key) {
switch (transaction.isolationLevel) {
case READ_UNCOMMITTED: {
Snapshot