Package org.h2.mvstore.tx
Class TransactionStore
java.lang.Object
org.h2.mvstore.tx.TransactionStore
A store that supports concurrent MVCC read-committed transactions.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic class
A change in a map.static interface
This listener can be registered with the transaction to be notified of every compensating change during transaction rollback.private static final class
-
Field Summary
FieldsModifier and TypeFieldDescription(package private) final AtomicReference
<BitSet> This is intended to be the source of ultimate truth about transaction being committed.private final DataType
<?> private boolean
private static final int
private static final long
private static final int
Hard limit on the number of concurrently opened transactionsprivate int
Soft limit on the number of concurrently opened transactions.(package private) final AtomicReference
<VersionedBitSet> This BitSet is used as vacancy indicator for transaction slots in transactions[].The persisted map of prepared transactions.private static final TransactionStore.RollbackListener
(package private) final MVStore
The store.(package private) final int
Default blocked transaction timeoutprivate final AtomicReferenceArray
<Transaction> Array holding all open transaction objects.private static final String
private static final char
static final String
The prefix for undo log entries.private static final char
private final MVMap.Builder
<Long, Record<?, ?>> Undo logs. -
Constructor Summary
ConstructorsConstructorDescriptionTransactionStore
(MVStore store) Create a new transaction store.TransactionStore
(MVStore store, DataType<?> dataType) TransactionStore
(MVStore store, MetaType<?> metaDataType, DataType<?> dataType, int timeoutMillis) Create a new transaction store. -
Method Summary
Modifier and TypeMethodDescription(package private) long
addUndoLogRecord
(int transactionId, long logId, Record<?, ?> record) Add an undo log entry.begin()
Begin a new transaction.begin
(TransactionStore.RollbackListener listener, int timeoutMillis, int ownerId, IsolationLevel isolationLevel) Begin a new transaction.(package private) static long
calculateUndoLogsTotalSize
(RootReference<Long, Record<?, ?>>[] undoLogRootReferences) Calculate the size for undo log entries.void
close()
Close the transaction store.(package private) RootReference<Long,
Record<?, ?>>[] Get the root references (snapshots) for undo-log maps.(package private) void
commit
(Transaction t, boolean recovery) Commit a transaction.(package private) MVMap.Builder
<Long, Record<?, ?>> void
Commit all transactions that are in the committed state, and rollback all open transactions.(package private) void
endTransaction
(Transaction t, boolean hasChanges) End this transaction.private void
flipCommittingTransactionsBit
(int transactionId, boolean flag) (package private) Iterator
<TransactionStore.Change> getChanges
(Transaction t, long maxLogId, long toLogId) Get the changes of the given transaction, starting from the latest log id back to the given log id.(package private) static long
getLogId
(long operationId) Get the log id for the given operation id.(package private) <K,
V> MVMap <K, VersionedValue<V>> getMap
(int mapId) (package private) int
Get the list of unclosed transactions that have pending writes.(package private) static long
getOperationId
(int transactionId, long logId) Combine the transaction id and the log id to an operation id.(package private) Transaction
getTransaction
(int transactionId) Get Transaction object for a transaction id.(package private) static int
getTransactionId
(long operationId) Get the transaction id for the given operation id.private static String
getUndoLogName
(int transactionId) Generate a string used to name undo log map for a specific transaction.boolean
Check whether a given map exists.void
init()
Initialize the store without any RollbackListener.void
init
(TransactionStore.RollbackListener listener) Initialize the store.private boolean
private void
markUndoLogAsCommitted
(int transactionId) (package private) <K,
V> MVMap <K, VersionedValue<V>> openMap
(int mapId) Open the map with the given id.<K,
V> MVMap <K, V> Open the map with the given name.openTypeRegistry
(MVStore store, MetaType<?> metaDataType) (package private) <K,
V> MVMap <K, VersionedValue<V>> openVersionedMap
(String name, DataType<K> keyType, DataType<V> valueType) private Transaction
registerTransaction
(int txId, int status, String name, long logId, int timeoutMillis, int ownerId, IsolationLevel isolationLevel, TransactionStore.RollbackListener listener) (package private) void
removeMap
(TransactionMap<?, ?> map) Remove the given map.(package private) void
removeUndoLogRecord
(int transactionId) Remove an undo log entry.(package private) void
rollbackTo
(Transaction t, long maxLogId, long toLogId) Rollback to an old savepoint.void
setMaxTransactionId
(int max) Set the maximum transaction id, after which ids are re-used.(package private) void
Store a transaction.
-
Field Details
-
store
The store. -
timeoutMillis
final int timeoutMillisDefault blocked transaction timeout -
preparedTransactions
The persisted map of prepared transactions. Key: transactionId, value: [ status, name ]. -
typeRegistry
-
undoLogs
Undo logs.If the first entry for a transaction doesn't have a logId of 0, then the transaction is partially committed (which means rollback is not possible). Log entries are written before the data is changed (write-ahead).
Key: opId, value: [ mapId, key, oldValue ].
-
undoLogBuilder
-
dataType
-
openTransactions
This BitSet is used as vacancy indicator for transaction slots in transactions[]. It provides easy way to find first unoccupied slot, and also allows for copy-on-write non-blocking updates. -
committingTransactions
This is intended to be the source of ultimate truth about transaction being committed. Once bit is set, corresponding transaction is logically committed, although it might be plenty of "uncommitted" entries in various maps and undo record are still around. Nevertheless, all of those should be considered by other transactions as committed. -
init
private boolean init -
maxTransactionId
private int maxTransactionIdSoft limit on the number of concurrently opened transactions. Not really needed but used by some test. -
transactions
Array holding all open transaction objects. Position in array is "transaction id". VolatileReferenceArray would do the job here, but there is no such thing in Java yet -
TYPE_REGISTRY_NAME
- See Also:
-
UNDO_LOG_NAME_PREFIX
The prefix for undo log entries.- See Also:
-
UNDO_LOG_COMMITTED
private static final char UNDO_LOG_COMMITTED- See Also:
-
UNDO_LOG_OPEN
private static final char UNDO_LOG_OPEN- See Also:
-
MAX_OPEN_TRANSACTIONS
private static final int MAX_OPEN_TRANSACTIONSHard limit on the number of concurrently opened transactions- See Also:
-
LOG_ID_BITS
private static final int LOG_ID_BITS- See Also:
-
LOG_ID_MASK
private static final long LOG_ID_MASK- See Also:
-
ROLLBACK_LISTENER_NONE
-
-
Constructor Details
-
TransactionStore
Create a new transaction store.- Parameters:
store
- the store
-
TransactionStore
-
TransactionStore
public TransactionStore(MVStore store, MetaType<?> metaDataType, DataType<?> dataType, int timeoutMillis) Create a new transaction store.- Parameters:
store
- the storemetaDataType
- the data type for type registry map valuesdataType
- default data type for map keys and valuestimeoutMillis
- lock acquisition timeout in milliseconds, 0 means no wait
-
-
Method Details
-
getUndoLogName
Generate a string used to name undo log map for a specific transaction. This name will contain transaction id.- Parameters:
transactionId
- of the corresponding transaction- Returns:
- undo log name
-
createUndoLogBuilder
MVMap.Builder<Long,Record<?, createUndoLogBuilder()?>> -
openTypeRegistry
-
init
public void init()Initialize the store without any RollbackListener.- See Also:
-
init
Initialize the store. This is needed before a transaction can be opened. If the transaction store is corrupt, this method can throw an exception, in which case the store can only be used for reading.- Parameters:
listener
- to notify about transaction rollback
-
markUndoLogAsCommitted
private void markUndoLogAsCommitted(int transactionId) -
endLeftoverTransactions
public void endLeftoverTransactions()Commit all transactions that are in the committed state, and rollback all open transactions. -
getMaxTransactionId
int getMaxTransactionId() -
setMaxTransactionId
public void setMaxTransactionId(int max) Set the maximum transaction id, after which ids are re-used. If the old transaction is still in use when re-using an old id, the new transaction fails.- Parameters:
max
- the maximum id
-
hasMap
Check whether a given map exists.- Parameters:
name
- the map name- Returns:
- true if it exists
-
getOperationId
static long getOperationId(int transactionId, long logId) Combine the transaction id and the log id to an operation id.- Parameters:
transactionId
- the transaction idlogId
- the log id- Returns:
- the operation id
-
getTransactionId
static int getTransactionId(long operationId) Get the transaction id for the given operation id.- Parameters:
operationId
- the operation id- Returns:
- the transaction id
-
getLogId
static long getLogId(long operationId) Get the log id for the given operation id.- Parameters:
operationId
- the operation id- Returns:
- the log id
-
getOpenTransactions
Get the list of unclosed transactions that have pending writes.- Returns:
- the list of transactions (sorted by id)
-
close
public void close()Close the transaction store. -
begin
Begin a new transaction.- Returns:
- the transaction
-
begin
public Transaction begin(TransactionStore.RollbackListener listener, int timeoutMillis, int ownerId, IsolationLevel isolationLevel) Begin a new transaction.- Parameters:
listener
- to be notified in case of a rollbacktimeoutMillis
- to wait for a blocking transactionownerId
- of the owner (Session?) to be reported by getBlockerIdisolationLevel
- of new transaction- Returns:
- the transaction
-
registerTransaction
private Transaction registerTransaction(int txId, int status, String name, long logId, int timeoutMillis, int ownerId, IsolationLevel isolationLevel, TransactionStore.RollbackListener listener) -
storeTransaction
Store a transaction.- Parameters:
t
- the transaction
-
addUndoLogRecord
Add an undo log entry.- Parameters:
transactionId
- id of the transactionlogId
- sequential number of the log record within transactionrecord
- Record(mapId, key, previousValue) to add- Returns:
- key for the added record
-
removeUndoLogRecord
void removeUndoLogRecord(int transactionId) Remove an undo log entry.- Parameters:
transactionId
- id of the transaction
-
removeMap
Remove the given map.- Parameters:
map
- the map
-
commit
Commit a transaction.- Parameters:
t
- transaction to commitrecovery
- if called during initial transaction recovery procedure therefore undo log is stored under "committed" name already
-
flipCommittingTransactionsBit
private void flipCommittingTransactionsBit(int transactionId, boolean flag) -
openVersionedMap
<K,V> MVMap<K,VersionedValue<V>> openVersionedMap(String name, DataType<K> keyType, DataType<V> valueType) -
openMap
Open the map with the given name.- Type Parameters:
K
- the key typeV
- the value type- Parameters:
name
- the map namekeyType
- the key typevalueType
- the value type- Returns:
- the map
-
openMap
Open the map with the given id.- Type Parameters:
K
- key typeV
- value type- Parameters:
mapId
- the id- Returns:
- the map
-
getMap
-
endTransaction
End this transaction. Change status to CLOSED and vacate transaction slot. Will try to commit MVStore if autocommitDelay is 0 or if database is idle and amount of unsaved changes is sizable.- Parameters:
t
- the transactionhasChanges
- true if transaction has done any updates (even if they are fully rolled back), false if it just performed a data access
-
collectUndoLogRootReferences
RootReference<Long,Record<?, collectUndoLogRootReferences()?>>[] Get the root references (snapshots) for undo-log maps. Those snapshots can potentially be used to optimize TransactionMap.size().- Returns:
- the array of root references or null if snapshotting is not possible
-
calculateUndoLogsTotalSize
Calculate the size for undo log entries.- Parameters:
undoLogRootReferences
- the root references- Returns:
- the number of key-value pairs
-
isUndoEmpty
private boolean isUndoEmpty() -
getTransaction
Get Transaction object for a transaction id.- Parameters:
transactionId
- id for an open transaction- Returns:
- Transaction object.
-
rollbackTo
Rollback to an old savepoint.- Parameters:
t
- the transactionmaxLogId
- the last log idtoLogId
- the log id to roll back to
-
getChanges
Get the changes of the given transaction, starting from the latest log id back to the given log id.- Parameters:
t
- the transactionmaxLogId
- the maximum log idtoLogId
- the minimum log id- Returns:
- the changes
-