5 import java.util.HashSet;
7 import javax.script.ScriptException;
8 import java.lang.NullPointerException;
11 class PendingTransaction {
13 private Set<KeyValue> keyValueUpdateSet = null;
14 private Set<KeyValue> keyValueGuardSet = null;
15 private long arbitrator = -1;
16 private long machineLocalTransSeqNum = -1;
18 public PendingTransaction() {
19 keyValueUpdateSet = new HashSet<KeyValue>();
20 keyValueGuardSet = new HashSet<KeyValue>();
24 * Add a new key value to the updates
27 public void addKV(KeyValue newKV) {
31 // Make sure there are no duplicates
32 for (KeyValue kv : keyValueUpdateSet) {
33 if (kv.getKey().equals(newKV.getKey())) {
35 // Remove key if we are adding a newer version of the same key
41 // Remove key if we are adding a newer version of the same key
43 keyValueUpdateSet.remove(rmKV);
46 // Add the key to the hash set
47 keyValueUpdateSet.add(newKV);
52 * Add a new key value to the guard set
55 public void addKVGuard(KeyValue newKV) {
56 // Add the key to the hash set
57 keyValueGuardSet.add(newKV);
61 * Checks if the arbitrator is the same
64 public boolean checkArbitrator(long arb) {
65 if (arbitrator == -1) {
70 return arb == arbitrator;
74 * Get the transaction arbitrator
77 public long getArbitrator() {
82 * Get the key value update set
85 public Set<KeyValue> getKVUpdates() {
86 return keyValueUpdateSet;
91 * Get the key value update set
94 public Set<KeyValue> getKVGuard() {
95 return keyValueGuardSet;
98 public void setMachineLocalTransSeqNum(long _machineLocalTransSeqNum) {
99 machineLocalTransSeqNum = _machineLocalTransSeqNum;
102 public long getMachineLocalTransSeqNum() {
103 return machineLocalTransSeqNum;
106 public boolean evaluateGuard(Map<IoTString, KeyValue> keyValTableCommitted, Map<IoTString, KeyValue> keyValTableSpeculative, Map<IoTString, KeyValue> keyValTablePendingTransSpeculative) {
107 for (KeyValue kvGuard : keyValueGuardSet) {
109 // First check if the key is in the speculative table, this is the value of the latest assumption
110 KeyValue kv = keyValTablePendingTransSpeculative.get(kvGuard.getKey());
114 // if it is not in the pending trans table then check the speculative table and use that
115 // value as our latest assumption
116 kv = keyValTableSpeculative.get(kvGuard.getKey());
121 // if it is not in the speculative table then check the committed table and use that
122 // value as our latest assumption
123 kv = keyValTableCommitted.get(kvGuard.getKey());
126 if (kvGuard.getValue() != null) {
127 if ((kv == null) || (!kvGuard.getValue().equals(kv.getValue()))) {