| 1 |
/* |
| 2 |
* Copyright 2004-2023 H2 Group. Multiple-Licensed under the MPL 2.0, |
| 3 |
* and the EPL 1.0 (https://h2database.com/html/license.html). |
| 4 |
* Initial Developer: H2 Group |
| 5 |
*/ |
| 6 |
package org.h2.mvstore.tx; |
| 7 |
|
| 8 |
import org.h2.mvstore.MVMap; |
| 9 |
import org.h2.value.VersionedValue; |
| 10 |
|
| 11 |
/** |
| 12 |
* Class RollbackDecisionMaker process undo log record during transaction rollback. |
| 13 |
* |
| 14 |
* @author <a href='mailto:[email protected]'>Andrei Tokar</a> |
| 15 |
*/ |
| 16 |
final class RollbackDecisionMaker extends MVMap.DecisionMaker<Record<?,?>> { |
| 17 |
private final TransactionStore store; |
| 18 |
private final long transactionId; |
| 19 |
private final long toLogId; |
| 20 |
private final TransactionStore.RollbackListener listener; |
| 21 |
private MVMap.Decision decision; |
| 22 |
|
| 23 |
RollbackDecisionMaker(TransactionStore store, long transactionId, long toLogId, |
| 24 |
TransactionStore.RollbackListener listener) { |
| 25 |
this.store = store; |
| 26 |
this.transactionId = transactionId; |
| 27 |
this.toLogId = toLogId; |
| 28 |
this.listener = listener; |
| 29 |
} |
| 30 |
|
| 31 |
@SuppressWarnings({"unchecked","rawtypes"}) |
| 32 |
@Override |
| 33 |
public MVMap.Decision decide(Record existingValue, Record providedValue) { |
| 34 |
assert decision == null; |
| 35 |
if (existingValue == null) { |
| 36 |
// normally existingValue will always be there except of db initialization |
| 37 |
// where some undo log entry was captured on disk but actual map entry was not |
| 38 |
decision = MVMap.Decision.ABORT; |
| 39 |
} else { |
| 40 |
VersionedValue<Object> valueToRestore = existingValue.oldValue; |
| 41 |
long operationId; |
| 42 |
if (valueToRestore == null || |
| 43 |
(operationId = valueToRestore.getOperationId()) == 0 || |
| 44 |
TransactionStore.getTransactionId(operationId) == transactionId |
| 45 |
&& TransactionStore.getLogId(operationId) < toLogId) { |
| 46 |
int mapId = existingValue.mapId; |
| 47 |
MVMap<Object, VersionedValue<Object>> map = store.openMap(mapId); |
| 48 |
if (map != null && !map.isClosed()) { |
| 49 |
Object key = existingValue.key; |
| 50 |
VersionedValue<Object> previousValue = map.operate(key, valueToRestore, |
| 51 |
MVMap.DecisionMaker.DEFAULT); |
| 52 |
listener.onRollback(map, key, previousValue, valueToRestore); |
| 53 |
} |
| 54 |
} |
| 55 |
decision = MVMap.Decision.REMOVE; |
| 56 |
} |
| 57 |
return decision; |
| 58 |
} |
| 59 |
|
| 60 |
@Override |
| 61 |
public void reset() { |
| 62 |
decision = null; |
| 63 |
} |
| 64 |
|
| 65 |
@Override |
| 66 |
public String toString() { |
| 67 |
return "rollback-" + transactionId; |
| 68 |
} |
| 69 |
} |