Class TransactionMap<K,V>
- java.lang.Object
-
- java.util.AbstractMap<K,V>
-
- org.h2.mvstore.tx.TransactionMap<K,V>
-
- Type Parameters:
K
- the key typeV
- the value type
- All Implemented Interfaces:
java.util.Map<K,V>
public final class TransactionMap<K,V> extends java.util.AbstractMap<K,V>
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.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description private static class
TransactionMap.CommittedIterator<K,V,X>
The iterator for read committed isolation level.private static class
TransactionMap.RepeatableIterator<K,V,X>
The iterator for repeatable read and serializable isolation levels.static class
TransactionMap.TMIterator<K,V,X>
private static class
TransactionMap.UncommittedIterator<K,V,X>
The iterator for read uncommitted isolation level.private static class
TransactionMap.ValidationIterator<K,V,X>
-
Field Summary
Fields Modifier and Type Field Description private boolean
hasChanges
Indicates whether underlying map was modified from within related transactionprivate TxDecisionMaker<K,V>
ifAbsentDecisionMaker
private TxDecisionMaker<K,V>
lockDecisionMaker
MVMap<K,VersionedValue<V>>
map
The map used for writing (the latest version).private Snapshot<K,VersionedValue<V>>
snapshot
Snapshot of this map as of beginning of transaction or first usage within transaction or beginning of the statement, depending on isolation levelprivate Snapshot<K,VersionedValue<V>>
statementSnapshot
Snapshot of this map as of beginning of beginning of the statementprivate Transaction
transaction
The transaction which is used for this map.private TxDecisionMaker<K,V>
txDecisionMaker
-
Constructor Summary
Constructors Constructor Description TransactionMap(Transaction transaction, MVMap<K,VersionedValue<V>> map)
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description private long
adjustSize(RootReference<java.lang.Long,Record<?,?>>[] undoLogRootReferences, RootReference<K,VersionedValue<V>> mapRootReference, java.util.BitSet committingTransactions, long size, long undoLogsTotalSize)
void
append(K key, V value)
Appends entry to underlying map.java.util.Map.Entry<K,V>
ceilingEntry(K key)
Get the entry with smallest key that is larger than or equal to this key, or null if no such key exists.K
ceilingKey(K key)
Get the smallest key that is larger than or equal to this key, or null if no such key exists.private <X> TransactionMap.TMIterator<K,V,X>
chooseIterator(K from, K to, boolean reverse, boolean forEntries)
void
clear()
Clear the map.boolean
containsKey(java.lang.Object key)
Whether the map contains the key.(package private) Snapshot<K,VersionedValue<V>>
createSnapshot()
Create a new snapshot for this map.TransactionMap.TMIterator<K,V,java.util.Map.Entry<K,V>>
entryIterator(K from, K to)
Iterate over entries.java.util.Set<java.util.Map.Entry<K,V>>
entrySet()
java.util.Map.Entry<K,V>
firstEntry()
Get the first entry.K
firstKey()
Get the first key.java.util.Map.Entry<K,V>
floorEntry(K key)
Get the entry with largest key that is smaller than or equal to this key, or null if no such key exists.K
floorKey(K key)
Get the largest key that is smaller than or equal to this key, or null if no such key exists.V
get(java.lang.Object key)
Get the effective value for the given key.V
getFromSnapshot(K key)
Get the value for the given key, or null if value does not exist in accordance with transactional rules.private V
getFromSnapshot(RootReference<K,VersionedValue<V>> rootRef, java.util.BitSet committingTransactions, K key)
V
getImmediate(K key)
Get the value for the given key, or null if not found.TransactionMap<K,V>
getInstance(Transaction transaction)
Get a clone of this map for the given transaction.DataType<K>
getKeyType()
(package private) Snapshot<K,VersionedValue<V>>
getSnapshot()
(package private) Snapshot<K,VersionedValue<V>>
getStatementSnapshot()
Transaction
getTransaction()
java.util.Map.Entry<K,V>
higherEntry(K key)
Get the entry with smallest key that is larger than the given key, or null if no such key exists.K
higherKey(K key)
Get the smallest key that is larger than the given key, or null if no such key exists.private java.util.Map.Entry<K,V>
higherLowerEntry(K key, boolean lower)
private K
higherLowerKey(K key, boolean lower)
boolean
isClosed()
Check whether this map is closed.boolean
isDeletedByCurrentTransaction(K key)
Check if the row was deleted by this transaction.private boolean
isIrrelevant(long operationId, VersionedValue<?> currentValue, java.util.BitSet committingTransactions)
boolean
isSameTransaction(K key)
Whether the entry for this key was added or removed from this session.java.util.Iterator<K>
keyIterator(K from)
Iterate over keys.TransactionMap.TMIterator<K,V,K>
keyIterator(K from, boolean reverse)
Iterate over keys in the specified order.TransactionMap.TMIterator<K,V,K>
keyIterator(K from, K to)
Iterate over keys.TransactionMap.TMIterator<K,V,K>
keyIteratorUncommitted(K from, K to)
Iterate over keys, including keys from uncommitted entries.java.util.Map.Entry<K,V>
lastEntry()
Get the last entry.K
lastKey()
Get the last key.V
lock(K key)
Lock row for the given key.java.util.Map.Entry<K,V>
lowerEntry(K key)
Get the entry with largest key that is smaller than the given key, or null if no such key exists.K
lowerKey(K key)
Get the largest key that is smaller than the given key, or null if no such key exists.(package private) void
promoteSnapshot()
V
put(K key, V value)
Update the value for the given key.V
putCommitted(K key, V value)
Update the value for the given key, without adding an undo log entry.V
putIfAbsent(K key, V value)
Put the value for the given key if entry for this key does not exist.V
remove(java.lang.Object key)
Remove an entry.private V
set(java.lang.Object key, TxDecisionMaker<K,V> decisionMaker)
private V
set(K key, V value)
(package private) void
setStatementSnapshot(Snapshot<K,VersionedValue<V>> snapshot)
int
size()
Get the number of entries, as a integer.long
sizeAsLong()
Get the size of the map as seen by this transaction.long
sizeAsLongMax()
Get the size of the raw map.private long
sizeAsLongRepeatableReadWithChanges()
boolean
tryPut(K key, V value)
Try to update the value for the given key.boolean
tryRemove(K key)
Try to remove the value for the given key.boolean
trySet(K key, V value)
Try to set or remove the value.(package private) <R> R
useSnapshot(java.util.function.BiFunction<RootReference<K,VersionedValue<V>>,java.util.BitSet,R> snapshotConsumer)
Gets a coherent picture of committing transactions and root reference, passes it to the specified function, and returns its result.-
Methods inherited from class java.util.AbstractMap
clone, containsValue, equals, hashCode, isEmpty, keySet, putAll, toString, values
-
-
-
-
Field Detail
-
map
public final MVMap<K,VersionedValue<V>> map
The map used for writing (the latest version).Key: key the key of the data. Value: { transactionId, oldVersion, value }
-
transaction
private final Transaction transaction
The transaction which is used for this map.
-
snapshot
private Snapshot<K,VersionedValue<V>> snapshot
Snapshot of this map as of beginning of transaction or first usage within transaction or beginning of the statement, depending on isolation level
-
statementSnapshot
private Snapshot<K,VersionedValue<V>> statementSnapshot
Snapshot of this map as of beginning of beginning of the statement
-
hasChanges
private boolean hasChanges
Indicates whether underlying map was modified from within related transaction
-
txDecisionMaker
private final TxDecisionMaker<K,V> txDecisionMaker
-
ifAbsentDecisionMaker
private final TxDecisionMaker<K,V> ifAbsentDecisionMaker
-
lockDecisionMaker
private final TxDecisionMaker<K,V> lockDecisionMaker
-
-
Constructor Detail
-
TransactionMap
TransactionMap(Transaction transaction, MVMap<K,VersionedValue<V>> map)
-
-
Method Detail
-
getInstance
public TransactionMap<K,V> getInstance(Transaction transaction)
Get a clone of this map for the given transaction.- Parameters:
transaction
- the transaction- Returns:
- the map
-
size
public int size()
Get the number of entries, as a integer.Integer.MAX_VALUE
is returned if there are more than this entries.- Specified by:
size
in interfacejava.util.Map<K,V>
- Overrides:
size
in classjava.util.AbstractMap<K,V>
- Returns:
- the number of entries, as an integer
- See Also:
sizeAsLong()
-
sizeAsLongMax
public long sizeAsLongMax()
Get the size of the raw map. This includes uncommitted entries, and transiently removed entries, so it is the maximum number of entries.- Returns:
- the maximum size
-
sizeAsLong
public long sizeAsLong()
Get the size of the map as seen by this transaction.- Returns:
- the size
-
adjustSize
private long adjustSize(RootReference<java.lang.Long,Record<?,?>>[] undoLogRootReferences, RootReference<K,VersionedValue<V>> mapRootReference, java.util.BitSet committingTransactions, long size, long undoLogsTotalSize)
-
isIrrelevant
private boolean isIrrelevant(long operationId, VersionedValue<?> currentValue, java.util.BitSet committingTransactions)
-
sizeAsLongRepeatableReadWithChanges
private long sizeAsLongRepeatableReadWithChanges()
-
remove
public V remove(java.lang.Object key)
Remove an entry.If the row is locked, this method will retry until the row could be updated or until a lock timeout.
- Specified by:
remove
in interfacejava.util.Map<K,V>
- Overrides:
remove
in classjava.util.AbstractMap<K,V>
- Parameters:
key
- the key- Throws:
MVStoreException
- if a lock timeout occursjava.lang.ClassCastException
- if type of the specified key is not compatible with this map
-
put
public V put(K key, V value)
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.
- Specified by:
put
in interfacejava.util.Map<K,V>
- Overrides:
put
in classjava.util.AbstractMap<K,V>
- Parameters:
key
- the keyvalue
- the new value (not null)- Returns:
- the old value
- Throws:
MVStoreException
- if a lock timeout occurs
-
putIfAbsent
public V putIfAbsent(K key, V 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);- Parameters:
key
- the keyvalue
- the new value (not null)- Returns:
- the old value
-
append
public void append(K key, V value)
Appends entry to underlying map. This method may be used concurrently, but latest appended values are not guaranteed to be visible.- Parameters:
key
- should be higher in map's order than any existing keyvalue
- to be appended
-
lock
public V lock(K key)
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.
- Parameters:
key
- the key- Returns:
- the locked value
- Throws:
MVStoreException
- if a lock timeout occurs
-
putCommitted
public V putCommitted(K key, V value)
Update the value for the given key, without adding an undo log entry.- Parameters:
key
- the keyvalue
- the value- Returns:
- the old value
-
set
private V set(java.lang.Object key, TxDecisionMaker<K,V> decisionMaker)
-
tryRemove
public boolean tryRemove(K key)
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).
- Parameters:
key
- the key- Returns:
- whether the entry could be removed
-
tryPut
public boolean tryPut(K key, V value)
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).
- Parameters:
key
- the keyvalue
- the new value- Returns:
- whether the entry could be updated
-
trySet
public boolean trySet(K key, V 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.- Parameters:
key
- the keyvalue
- the new value (null to remove the value)- Returns:
- true if the value was set, false if there was a concurrent update
-
get
public V get(java.lang.Object key)
Get the effective value for the given key.
-
getFromSnapshot
public V getFromSnapshot(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- Parameters:
key
- the key- Returns:
- the value, or null if not found
-
getFromSnapshot
private V getFromSnapshot(RootReference<K,VersionedValue<V>> rootRef, java.util.BitSet committingTransactions, K key)
-
getImmediate
public V getImmediate(K key)
Get the value for the given key, or null if not found. Operation is performed on a snapshot of the map taken during this call.- Parameters:
key
- the key- Returns:
- the value, or null if not found
-
getSnapshot
Snapshot<K,VersionedValue<V>> getSnapshot()
-
getStatementSnapshot
Snapshot<K,VersionedValue<V>> getStatementSnapshot()
-
setStatementSnapshot
void setStatementSnapshot(Snapshot<K,VersionedValue<V>> snapshot)
-
promoteSnapshot
void promoteSnapshot()
-
createSnapshot
Snapshot<K,VersionedValue<V>> createSnapshot()
Create a new snapshot for this map.- Returns:
- the snapshot
-
useSnapshot
<R> R useSnapshot(java.util.function.BiFunction<RootReference<K,VersionedValue<V>>,java.util.BitSet,R> snapshotConsumer)
Gets a coherent picture of committing transactions and root reference, passes it to the specified function, and returns its result.- Type Parameters:
R
- type of the result- Parameters:
snapshotConsumer
- function to invoke on a snapshot- Returns:
- function's result
-
containsKey
public boolean containsKey(java.lang.Object key)
Whether the map contains the key.- Specified by:
containsKey
in interfacejava.util.Map<K,V>
- Overrides:
containsKey
in classjava.util.AbstractMap<K,V>
- Parameters:
key
- the key- Returns:
- true if the map contains an entry for this key
- Throws:
java.lang.ClassCastException
- if type of the specified key is not compatible with this map
-
isDeletedByCurrentTransaction
public boolean isDeletedByCurrentTransaction(K key)
Check if the row was deleted by this transaction.- Parameters:
key
- the key- Returns:
true
if it was
-
isSameTransaction
public boolean isSameTransaction(K key)
Whether the entry for this key was added or removed from this session.- Parameters:
key
- the key- Returns:
- true if yes
-
isClosed
public boolean isClosed()
Check whether this map is closed.- Returns:
- true if closed
-
clear
public void clear()
Clear the map.
-
firstEntry
public java.util.Map.Entry<K,V> firstEntry()
Get the first entry.- Returns:
- the first entry, or null if empty
-
firstKey
public K firstKey()
Get the first key.- Returns:
- the first key, or null if empty
-
lastEntry
public java.util.Map.Entry<K,V> lastEntry()
Get the last entry.- Returns:
- the last entry, or null if empty
-
lastKey
public K lastKey()
Get the last key.- Returns:
- the last key, or null if empty
-
higherEntry
public java.util.Map.Entry<K,V> higherEntry(K key)
Get the entry with smallest key that is larger than the given key, or null if no such key exists.- Parameters:
key
- the key (may not be null)- Returns:
- the result
-
higherKey
public K higherKey(K key)
Get the smallest key that is larger than the given key, or null if no such key exists.- Parameters:
key
- the key (may not be null)- Returns:
- the result
-
ceilingEntry
public java.util.Map.Entry<K,V> ceilingEntry(K key)
Get the entry with smallest key that is larger than or equal to this key, or null if no such key exists.- Parameters:
key
- the key (may not be null)- Returns:
- the result
-
ceilingKey
public K ceilingKey(K key)
Get the smallest key that is larger than or equal to this key, or null if no such key exists.- Parameters:
key
- the key (may not be null)- Returns:
- the result
-
floorEntry
public java.util.Map.Entry<K,V> floorEntry(K key)
Get the entry with largest key that is smaller than or equal to this key, or null if no such key exists.- Parameters:
key
- the key (may not be null)- Returns:
- the result
-
floorKey
public K floorKey(K key)
Get the largest key that is smaller than or equal to this key, or null if no such key exists.- Parameters:
key
- the key (may not be null)- Returns:
- the result
-
lowerEntry
public java.util.Map.Entry<K,V> lowerEntry(K key)
Get the entry with largest key that is smaller than the given key, or null if no such key exists.- Parameters:
key
- the key (may not be null)- Returns:
- the result
-
lowerKey
public K lowerKey(K key)
Get the largest key that is smaller than the given key, or null if no such key exists.- Parameters:
key
- the key (may not be null)- Returns:
- the result
-
keyIterator
public java.util.Iterator<K> keyIterator(K from)
Iterate over keys.- Parameters:
from
- the first key to return- Returns:
- the iterator
-
keyIterator
public TransactionMap.TMIterator<K,V,K> keyIterator(K from, boolean reverse)
Iterate over keys in the specified order.- Parameters:
from
- the first key to returnreverse
- if true, iterate in reverse (descending) order- Returns:
- the iterator
-
keyIterator
public TransactionMap.TMIterator<K,V,K> keyIterator(K from, K to)
Iterate over keys.- Parameters:
from
- the first key to returnto
- the last key to return or null if there is no limit- Returns:
- the iterator
-
keyIteratorUncommitted
public TransactionMap.TMIterator<K,V,K> keyIteratorUncommitted(K from, K to)
Iterate over keys, including keys from uncommitted entries.- Parameters:
from
- the first key to returnto
- the last key to return or null if there is no limit- Returns:
- the iterator
-
entryIterator
public TransactionMap.TMIterator<K,V,java.util.Map.Entry<K,V>> entryIterator(K from, K to)
Iterate over entries.- Parameters:
from
- the first key to returnto
- the last key to return- Returns:
- the iterator
-
chooseIterator
private <X> TransactionMap.TMIterator<K,V,X> chooseIterator(K from, K to, boolean reverse, boolean forEntries)
-
getTransaction
public Transaction getTransaction()
-
-