| 1 |
/* |
| 2 |
* Copyright 2004-2019 H2 Group. Multiple-Licensed under the MPL 2.0, |
| 3 |
* and the EPL 1.0 (http://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<Object[]> { |
| 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 |
@Override |
| 32 |
public MVMap.Decision decide(Object[] existingValue, Object[] providedValue) { |
| 33 |
assert decision == null; |
| 34 |
if (existingValue == null) { |
| 35 |
// normally existingValue will always be there except of db initialization |
| 36 |
// where some undo log entry was captured on disk but actual map entry was not |
| 37 |
decision = MVMap.Decision.ABORT; |
| 38 |
} else { |
| 39 |
VersionedValue valueToRestore = (VersionedValue) existingValue[2]; |
| 40 |
long operationId; |
| 41 |
if (valueToRestore == null || |
| 42 |
(operationId = valueToRestore.getOperationId()) == 0 || |
| 43 |
TransactionStore.getTransactionId(operationId) == transactionId |
| 44 |
&& TransactionStore.getLogId(operationId) < toLogId) { |
| 45 |
int mapId = (Integer) existingValue[0]; |
| 46 |
MVMap<Object, VersionedValue> map = store.openMap(mapId); |
| 47 |
if (map != null && !map.isClosed()) { |
| 48 |
Object key = existingValue[1]; |
| 49 |
VersionedValue previousValue = map.operate(key, valueToRestore, MVMap.DecisionMaker.DEFAULT); |
| 50 |
listener.onRollback(map, key, previousValue, valueToRestore); |
| 51 |
} |
| 52 |
} |
| 53 |
decision = MVMap.Decision.REMOVE; |
| 54 |
} |
| 55 |
return decision; |
| 56 |
} |
| 57 |
|
| 58 |
@Override |
| 59 |
public void reset() { |
| 60 |
decision = null; |
| 61 |
} |
| 62 |
|
| 63 |
@Override |
| 64 |
public String toString() { |
| 65 |
return "rollback-" + transactionId; |
| 66 |
} |
| 67 |
} |