Top Description Inners Fields Constructors Methods
org.h2.mvstore.tx

public Class TransactionStore

extends Object
Class Inheritance
Imports
java.util.ArrayList, .BitSet, .Iterator, .List, .Map, .Objects, java.util.concurrent.atomic.AtomicReference, .AtomicReferenceArray, org.h2.engine.IsolationLevel, org.h2.mvstore.Cursor, .DataUtils, .MVMap, .MVStore, .RootReference, org.h2.mvstore.rtree.MVRTreeMap, .SpatialDataType, org.h2.mvstore.type.DataType, .LongDataType, .MetaType, .ObjectDataType, .StringDataType, org.h2.util.StringUtils, org.h2.value.VersionedValue

A store that supports concurrent MVCC read-committed transactions.

Nested and Inner Type Summary

Modifier and TypeClass and Description
public static class
TransactionStore.Change

A change in a map.

public static interface
TransactionStore.RollbackListener

This listener can be registered with the transaction to be notified of every compensating change during transaction rollback.

private static class

Field Summary

Modifier and TypeField and Description
pack-priv final AtomicReference<BitSet>
committingTransactions

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
MAX_OPEN_TRANSACTIONS

Hard limit on the number of concurrently opened transactions

private int
maxTransactionId

Soft limit on the number of concurrently opened transactions.

pack-priv final AtomicReference<VersionedBitSet>
openTransactions

This BitSet is used as vacancy indicator for transaction slots in transactions[].

private final MVMap<Integer, Object[]>
preparedTransactions

The persisted map of prepared transactions.

private static final TransactionStore.RollbackListener
pack-priv final MVStore
store

The store.

pack-priv final int
timeoutMillis

Default blocked transaction timeout

private final AtomicReferenceArray<Transaction>
transactions

Array holding all open transaction objects.

private static final String
private final MVMap<String, DataType<?>>
private static final char
public static final String
UNDO_LOG_NAME_PREFIX

The prefix for undo log entries.

private static final char
private final MVMap.Builder<Long, Record<?, ?>>
pack-priv final MVMap<Long, Record<?, ?>>[]
undoLogs

Undo logs.

Constructor Summary

AccessConstructor and Description
public
TransactionStore(MVStore
the store
store
)

Create a new transaction store.

public
TransactionStore(MVStore store, DataType<?> dataType)

public
TransactionStore(MVStore
the store
store
,
MetaType<?>
the data type for type registry map values
metaDataType
,
DataType<?>
default data type for map keys and values
dataType
,
int
lock acquisition timeout in milliseconds, 0 means no wait
timeoutMillis
)

Create a new transaction store.

Method Summary

Modifier and TypeMethod and Description
pack-priv long

Returns:

key for the added record
addUndoLogRecord
(int
id of the transaction
transactionId
,
long
sequential number of the log record within transaction
logId
,
Record<?, ?>
Record(mapId, key, previousValue) to add
record
)

Add an undo log entry.

public Transaction

Returns:

the transaction
begin
()

Begin a new transaction.

public Transaction

Returns:

the transaction
begin
(TransactionStore.RollbackListener
to be notified in case of a rollback
listener
,
int
to wait for a blocking transaction
timeoutMillis
,
int
of the owner (Session?) to be reported by getBlockerId
ownerId
,
IsolationLevel
of new transaction
isolationLevel
)

Begin a new transaction.

pack-priv static long

Returns:

the number of key-value pairs
calculateUndoLogsTotalSize
(RootReference<Long, Record<?, ?>>[]
the root references
undoLogRootReferences
)

Calculate the size for undo log entries.

public synchronized void
close()

Close the transaction store.

pack-priv RootReference<Long, Record<?, ?>>[]

Returns:

the array of root references or null if snapshotting is not possible
collectUndoLogRootReferences
()

Get the root references (snapshots) for undo-log maps.

pack-priv void
commit(Transaction
transaction to commit
t
,
boolean
if called during initial transaction recovery procedure therefore undo log is stored under "committed" name already
recovery
)

Commit a transaction.

pack-priv MVMap.Builder<Long, Record<?, ?>>
public void
endLeftoverTransactions()

Commit all transactions that are in the committed state, and rollback all open transactions.

pack-priv void
endTransaction(Transaction
the transaction
t
,
boolean
true if transaction has done any updates (even if they are fully rolled back), false if it just performed a data access
hasChanges
)

End this transaction.

private void
flipCommittingTransactionsBit(int transactionId, boolean flag)

pack-priv Iterator<TransactionStore.Change>

Returns:

the changes
getChanges
(final Transaction
the transaction
t
,
final long
the maximum log id
maxLogId
,
final long
the minimum log id
toLogId
)

Get the changes of the given transaction, starting from the latest log id back to the given log id.

pack-priv static long

Returns:

the log id
getLogId
(long
the operation id
operationId
)

Get the log id for the given operation id.

pack-priv <K, V> MVMap<K, VersionedValue<V>>
getMap(int mapId)

pack-priv int
public List<Transaction>

Returns:

the list of transactions (sorted by id)
getOpenTransactions
()

Get the list of unclosed transactions that have pending writes.

pack-priv static long

Returns:

the operation id
getOperationId
(int
the transaction id
transactionId
,
long
the log id
logId
)

Combine the transaction id and the log id to an operation id.

pack-priv Transaction

Returns:

Transaction object.
getTransaction
(int
id for an open transaction
transactionId
)

Get Transaction object for a transaction id.

pack-priv static int

Returns:

the transaction id
getTransactionId
(long
the operation id
operationId
)

Get the transaction id for the given operation id.

private static String

Returns:

undo log name
getUndoLogName
(int
of the corresponding transaction
transactionId
)

Generate a string used to name undo log map for a specific transaction.

public boolean

Returns:

true if it exists
hasMap
(String
the map name
name
)

Check whether a given map exists.

public void
init()

Initialize the store without any RollbackListener.

public void
init(TransactionStore.RollbackListener
to notify about transaction rollback
listener
)

Initialize the store.

private boolean
private void
markUndoLogAsCommitted(int transactionId)

public <
the key type
K
,
the value type
V
>
MVMap<K, V>

Returns:

the map
openMap
(String
the map name
name
,
DataType<K>
the key type
keyType
,
DataType<V>
the value type
valueType
)

Open the map with the given name.

pack-priv <
key type
K
,
value type
V
>
MVMap<K, VersionedValue<V>>

Returns:

the map
openMap
(int
the id
mapId
)

Open the map with the given id.

private static MVMap<String, DataType<?>>
openTypeRegistry(MVStore store, MetaType<?> metaDataType)

pack-priv <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)

pack-priv void
removeMap(TransactionMap<?, ?>
the map
map
)

Remove the given map.

pack-priv void
removeUndoLogRecord(int
id of the transaction
transactionId
)

Remove an undo log entry.

pack-priv void
rollbackTo(Transaction
the transaction
t
,
long
the last log id
maxLogId
,
long
the log id to roll back to
toLogId
)

Rollback to an old savepoint.

public void
setMaxTransactionId(int
the maximum id
max
)

Set the maximum transaction id, after which ids are re-used.

pack-priv void
storeTransaction(Transaction
the transaction
t
)

Store a transaction.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

committingTransactionsback to summary
pack-priv final AtomicReference<BitSet> 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.

dataTypeback to summary
private final DataType<?> dataType
initback to summary
private boolean init
LOG_ID_BITSback to summary
private static final int LOG_ID_BITS
LOG_ID_MASKback to summary
private static final long LOG_ID_MASK
MAX_OPEN_TRANSACTIONSback to summary
private static final int MAX_OPEN_TRANSACTIONS

Hard limit on the number of concurrently opened transactions

maxTransactionIdback to summary
private int maxTransactionId

Soft limit on the number of concurrently opened transactions. Not really needed but used by some test.

openTransactionsback to summary
pack-priv final AtomicReference<VersionedBitSet> 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.

preparedTransactionsback to summary
private final MVMap<Integer, Object[]> preparedTransactions

The persisted map of prepared transactions. Key: transactionId, value: [ status, name ].

ROLLBACK_LISTENER_NONEback to summary
private static final TransactionStore.RollbackListener ROLLBACK_LISTENER_NONE
storeback to summary
pack-priv final MVStore store

The store.

timeoutMillisback to summary
pack-priv final int timeoutMillis

Default blocked transaction timeout

transactionsback to summary
private final AtomicReferenceArray<Transaction> 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_NAMEback to summary
private static final String TYPE_REGISTRY_NAME
typeRegistryback to summary
private final MVMap<String, DataType<?>> typeRegistry
UNDO_LOG_COMMITTEDback to summary
private static final char UNDO_LOG_COMMITTED
UNDO_LOG_NAME_PREFIXback to summary
public static final String UNDO_LOG_NAME_PREFIX

The prefix for undo log entries.

UNDO_LOG_OPENback to summary
private static final char UNDO_LOG_OPEN
undoLogBuilderback to summary
private final MVMap.Builder<Long, Record<?, ?>> undoLogBuilder
undoLogsback to summary
pack-priv final MVMap<Long, Record<?, ?>>[] 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 ].

Annotations
@SuppressWarnings:unchecked

Constructor Detail

TransactionStoreback to summary
public TransactionStore(MVStore store)

Create a new transaction store.

Parameters
store:MVStore

the store

TransactionStoreback to summary
public TransactionStore(MVStore store, DataType<?> dataType)
TransactionStoreback to summary
public TransactionStore(MVStore store, MetaType<?> metaDataType, DataType<?> dataType, int timeoutMillis)

Create a new transaction store.

Parameters
store:MVStore

the store

metaDataType:MetaType<?>

the data type for type registry map values

dataType:DataType<?>

default data type for map keys and values

timeoutMillis:int

lock acquisition timeout in milliseconds, 0 means no wait

Method Detail

addUndoLogRecordback to summary
pack-priv long addUndoLogRecord(int transactionId, long logId, Record<?, ?> record)

Add an undo log entry.

Parameters
transactionId:int

id of the transaction

logId:long

sequential number of the log record within transaction

record:Record<?, ?>

Record(mapId, key, previousValue) to add

Returns:long

key for the added record

beginback to summary
public Transaction begin()

Begin a new transaction.

Returns:Transaction

the transaction

beginback to summary
public Transaction begin(TransactionStore.RollbackListener listener, int timeoutMillis, int ownerId, IsolationLevel isolationLevel)

Begin a new transaction.

Parameters
listener:TransactionStore.RollbackListener

to be notified in case of a rollback

timeoutMillis:int

to wait for a blocking transaction

ownerId:int

of the owner (Session?) to be reported by getBlockerId

isolationLevel:IsolationLevel

of new transaction

Returns:Transaction

the transaction

calculateUndoLogsTotalSizeback to summary
pack-priv static long calculateUndoLogsTotalSize(RootReference<Long, Record<?, ?>>[] undoLogRootReferences)

Calculate the size for undo log entries.

Parameters
undoLogRootReferences:RootReference<Long, Record<?, ?>>[]

the root references

Returns:long

the number of key-value pairs

closeback to summary
public synchronized void close()

Close the transaction store.

collectUndoLogRootReferencesback to summary
pack-priv RootReference<Long, Record<?, ?>>[] collectUndoLogRootReferences()

Get the root references (snapshots) for undo-log maps. Those snapshots can potentially be used to optimize TransactionMap.size().

Returns:RootReference<Long, Record<?, ?>>[]

the array of root references or null if snapshotting is not possible

commitback to summary
pack-priv void commit(Transaction t, boolean recovery)

Commit a transaction.

Parameters
t:Transaction

transaction to commit

recovery:boolean

if called during initial transaction recovery procedure therefore undo log is stored under "committed" name already

createUndoLogBuilderback to summary
pack-priv MVMap.Builder<Long, Record<?, ?>> createUndoLogBuilder()
Annotations
@SuppressWarnings:unchecked, rawtypes
endLeftoverTransactionsback to summary
public void endLeftoverTransactions()

Commit all transactions that are in the committed state, and rollback all open transactions.

endTransactionback to summary
pack-priv void endTransaction(Transaction t, boolean hasChanges)

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:Transaction

the transaction

hasChanges:boolean

true if transaction has done any updates (even if they are fully rolled back), false if it just performed a data access

flipCommittingTransactionsBitback to summary
private void flipCommittingTransactionsBit(int transactionId, boolean flag)
getChangesback to summary
pack-priv Iterator<TransactionStore.Change> getChanges(final Transaction t, final long maxLogId, final long toLogId)

Get the changes of the given transaction, starting from the latest log id back to the given log id.

Parameters
t:Transaction

the transaction

maxLogId:long

the maximum log id

toLogId:long

the minimum log id

Returns:Iterator<TransactionStore.Change>

the changes

getLogIdback to summary
pack-priv static long getLogId(long operationId)

Get the log id for the given operation id.

Parameters
operationId:long

the operation id

Returns:long

the log id

getMapback to summary
pack-priv <K, V> MVMap<K, VersionedValue<V>> getMap(int mapId)
getMaxTransactionIdback to summary
pack-priv int getMaxTransactionId()
getOpenTransactionsback to summary
public List<Transaction> getOpenTransactions()

Get the list of unclosed transactions that have pending writes.

Returns:List<Transaction>

the list of transactions (sorted by id)

getOperationIdback to summary
pack-priv static long getOperationId(int transactionId, long logId)

Combine the transaction id and the log id to an operation id.

Parameters
transactionId:int

the transaction id

logId:long

the log id

Returns:long

the operation id

getTransactionback to summary
pack-priv Transaction getTransaction(int transactionId)

Get Transaction object for a transaction id.

Parameters
transactionId:int

id for an open transaction

Returns:Transaction

Transaction object.

getTransactionIdback to summary
pack-priv static int getTransactionId(long operationId)

Get the transaction id for the given operation id.

Parameters
operationId:long

the operation id

Returns:int

the transaction id

getUndoLogNameback to summary
private static String getUndoLogName(int transactionId)

Generate a string used to name undo log map for a specific transaction. This name will contain transaction id.

Parameters
transactionId:int

of the corresponding transaction

Returns:String

undo log name

hasMapback to summary
public boolean hasMap(String name)

Check whether a given map exists.

Parameters
name:String

the map name

Returns:boolean

true if it exists

initback to summary
public void init()

Initialize the store without any RollbackListener.

See Also
init(RollbackListener)
initback to summary
public void init(TransactionStore.RollbackListener listener)

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:TransactionStore.RollbackListener

to notify about transaction rollback

isUndoEmptyback to summary
private boolean isUndoEmpty()
markUndoLogAsCommittedback to summary
private void markUndoLogAsCommitted(int transactionId)
openMapback to summary
public <K, V> MVMap<K, V> openMap(String name, DataType<K> keyType, DataType<V> valueType)

Open the map with the given name.

Parameters
<K>
the key type
<V>
the value type
name:String

the map name

keyType:DataType<K>

the key type

valueType:DataType<V>

the value type

Returns:MVMap<K, V>

the map

openMapback to summary
pack-priv <K, V> MVMap<K, VersionedValue<V>> openMap(int mapId)

Open the map with the given id.

Parameters
<K>
key type
<V>
value type
mapId:int

the id

Returns:MVMap<K, VersionedValue<V>>

the map

openTypeRegistryback to summary
private static MVMap<String, DataType<?>> openTypeRegistry(MVStore store, MetaType<?> metaDataType)
openVersionedMapback to summary
pack-priv <K, V> MVMap<K, VersionedValue<V>> openVersionedMap(String name, DataType<K> keyType, DataType<V> valueType)
registerTransactionback to summary
private Transaction registerTransaction(int txId, int status, String name, long logId, int timeoutMillis, int ownerId, IsolationLevel isolationLevel, TransactionStore.RollbackListener listener)
removeMapback to summary
pack-priv void removeMap(TransactionMap<?, ?> map)

Remove the given map.

Parameters
map:TransactionMap<?, ?>

the map

removeUndoLogRecordback to summary
pack-priv void removeUndoLogRecord(int transactionId)

Remove an undo log entry.

Parameters
transactionId:int

id of the transaction

rollbackToback to summary
pack-priv void rollbackTo(Transaction t, long maxLogId, long toLogId)

Rollback to an old savepoint.

Parameters
t:Transaction

the transaction

maxLogId:long

the last log id

toLogId:long

the log id to roll back to

setMaxTransactionIdback to summary
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:int

the maximum id

storeTransactionback to summary
pack-priv void storeTransaction(Transaction t)

Store a transaction.

Parameters
t:Transaction

the transaction

org.h2.mvstore.tx back to summary

public Class TransactionStore.Change

extends Object
Class Inheritance

A change in a map.

Field Summary

Modifier and TypeField and Description
public final Object
key

The key.

public final String
mapName

The name of the map where the change occurred.

public final Object
value

The value.

Constructor Summary

AccessConstructor and Description
public
Change(String mapName, Object key, Object value)

Method Summary

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

keyback to summary
public final Object key

The key.

mapNameback to summary
public final String mapName

The name of the map where the change occurred.

valueback to summary
public final Object value

The value.

Constructor Detail

Changeback to summary
public Change(String mapName, Object key, Object value)
org.h2.mvstore.tx back to summary

public Interface TransactionStore.RollbackListener

Known Direct Implementers
org.h2.engine.SessionLocal

This listener can be registered with the transaction to be notified of every compensating change during transaction rollback. Normally this is not required, if no external resources were modified, because state of all transactional maps will be restored automatically. Only state of external resources, possibly modified by triggers need to be restored.

Method Summary

Modifier and TypeMethod and Description
public void
onRollback(MVMap<Object, VersionedValue<Object>>
modified
map
,
Object
of the modified entry
key
,
VersionedValue<Object>
value in the map (null if delete is rolled back)
existingValue
,
VersionedValue<Object>
value to be restored (null if add is rolled back)
restoredValue
)

Notified of a single map change (add/update/remove)

Method Detail

onRollbackback to summary
public void onRollback(MVMap<Object, VersionedValue<Object>> map, Object key, VersionedValue<Object> existingValue, VersionedValue<Object> restoredValue)

Notified of a single map change (add/update/remove)

Parameters
map:MVMap<Object, VersionedValue<Object>>

modified

key:Object

of the modified entry

existingValue:VersionedValue<Object>

value in the map (null if delete is rolled back)

restoredValue:VersionedValue<Object>

value to be restored (null if add is rolled back)

org.h2.mvstore.tx back to summary

private final Class TransactionStore.TxMapBuilder<K, V>

extends MVMap.Builder<K, V>
Class Inheritance

Nested and Inner Type Summary

Modifier and TypeClass and Description
private static class

Field Summary

Modifier and TypeField and Description
private final DataType<T>
private final MVMap<String, DataType<?>>

Constructor Summary

AccessConstructor and Description
pack-priv
TxMapBuilder(MVMap<String, DataType<?>> typeRegistry, DataType<?> defaultDataType)

Method Summary

Modifier and TypeMethod and Description
public MVMap<K, V>
create(MVStore
which will own this map
store
,
Map<String, Object>
configuration
config
)

Overrides org.h2.mvstore.MVMap.BasicBuilder.create.

Implements org.h2.mvstore.MVMap.MapBuilder.create.

Create a new map of the given type.

protected MVMap<K, V>
create(Map<String, Object>
config map
config
)

Overrides org.h2.mvstore.MVMap.Builder.create.

Create map from config.

pack-priv static String
private void
Inherited from org.h2.mvstore.MVMap.Builder:
keyTypesingleWritervalueType

Field Detail

defaultDataTypeback to summary
private final DataType<T> defaultDataType
typeRegistryback to summary
private final MVMap<String, DataType<?>> typeRegistry

Constructor Detail

TxMapBuilderback to summary
pack-priv TxMapBuilder(MVMap<String, DataType<?>> typeRegistry, DataType<?> defaultDataType)

Method Detail

createback to summary
public MVMap<K, V> create(MVStore store, Map<String, Object> config)

Overrides org.h2.mvstore.MVMap.BasicBuilder.create.

Implements org.h2.mvstore.MVMap.MapBuilder.create.

Doc from org.h2.mvstore.MVMap.MapBuilder.create.

Create a new map of the given type.

Parameters
store:MVStore

which will own this map

config:Map<String, Object>

configuration

Returns:MVMap<K, V>

the map

Annotations
@SuppressWarnings:unchecked
@Override
createback to summary
protected MVMap<K, V> create(Map<String, Object> config)

Overrides org.h2.mvstore.MVMap.Builder.create.

Doc from org.h2.mvstore.MVMap.BasicBuilder.create.

Create map from config.

Parameters
config:Map<String, Object>

config map

Returns:MVMap<K, V>

new map

Annotations
@Override
@SuppressWarnings:unchecked
getDataTypeRegistrationKeyback to summary
pack-priv static String getDataTypeRegistrationKey(DataType<?> dataType)
registerDataTypeback to summary
private void registerDataType(DataType<?> dataType)
org.h2.mvstore.tx back to summary

private final Class TransactionStore.TxMapBuilder.TMVMap<K, V>

extends MVMap<K, V>
Class Inheritance

Field Summary

Modifier and TypeField and Description
private final String
Inherited from org.h2.mvstore.MVMap:
store

Constructor Summary

AccessConstructor and Description
pack-priv
TMVMap(Map<String, Object> config, DataType<K> keyType, DataType<V> valueType)

private
TMVMap(MVMap<K, V> source)

Field Detail

typeback to summary
private final String type

Constructor Detail

TMVMapback to summary
pack-priv TMVMap(Map<String, Object> config, DataType<K> keyType, DataType<V> valueType)
TMVMapback to summary
private TMVMap(MVMap<K, V> source)

Method Detail

asStringback to summary
protected String asString(String name)

Overrides org.h2.mvstore.MVMap.asString.

Doc from org.h2.mvstore.MVMap.asString.

Get the map metadata as a string.

Parameters
name:String

the map name (or null)

Returns:String

the string

Annotations
@Override
cloneItback to summary
protected MVMap<K, V> cloneIt()

Overrides org.h2.mvstore.MVMap.cloneIt.

Doc from org.h2.mvstore.MVMap.cloneIt.

Clone the current map.

Returns:MVMap<K, V>

clone of this.

Annotations
@Override
getTypeback to summary
public String getType()

Overrides org.h2.mvstore.MVMap.getType.

Doc from org.h2.mvstore.MVMap.getType.

Get the map type. When opening an existing map, the map type must match.

Returns:String

the map type

Annotations
@Override