Debian Codegraph @

src/main/org/h2/mvstore/tx/CommitDecisionMaker.java

raw
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 CommitDecisionMaker makes a decision during post-commit processing
13 * about how to transform uncommitted map entry into committed one,
14 * based on undo log information.
15 *
16 * @author <a href='mailto:[email protected]'>Andrei Tokar</a>
17 */
18 final class CommitDecisionMaker<V> extends MVMap.DecisionMaker<VersionedValue<V>> {
19 private long undoKey;
20 private MVMap.Decision decision;
21
22 void setUndoKey(long undoKey) {
23 this.undoKey = undoKey;
24 reset();
25 }
26
27 @Override
28 public MVMap.Decision decide(VersionedValue<V> existingValue, VersionedValue<V> providedValue) {
29 assert decision == null;
30 if (existingValue == null ||
31 // map entry was treated as already committed, and then
32 // it has been removed by another transaction (committed and closed by now)
33 existingValue.getOperationId() != undoKey) {
34 // this is not a final undo log entry for this key,
35 // or map entry was treated as already committed and then
36 // overwritten by another transaction
37 // see TxDecisionMaker.decide()
38
39 decision = MVMap.Decision.ABORT;
40 } else /* this is final undo log entry for this key */ if (existingValue.getCurrentValue() == null) {
41 decision = MVMap.Decision.REMOVE;
42 } else {
43 decision = MVMap.Decision.PUT;
44 }
45 return decision;
46 }
47
48 @SuppressWarnings("unchecked")
49 @Override
50 public <T extends VersionedValue<V>> T selectValue(T existingValue, T providedValue) {
51 assert decision == MVMap.Decision.PUT;
52 assert existingValue != null;
53 return (T) VersionedValueCommitted.getInstance(existingValue.getCurrentValue());
54 }
55
56 @Override
57 public void reset() {
58 decision = null;
59 }
60
61 @Override
62 public String toString() {
63 return "commit " + TransactionStore.getTransactionId(undoKey);
64 }
65 }