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;
17 public PendingTransaction() {
18 keyValueUpdateSet = new HashSet<KeyValue>();
19 keyValueGuardSet = new HashSet<KeyValue>();
23 * Add a new key value to the updates
26 public void addKV(KeyValue newKV) {
30 // Make sure there are no duplicates
31 for (KeyValue kv : keyValueUpdateSet) {
32 if (kv.getKey().equals(newKV.getKey())) {
34 // Remove key if we are adding a newer version of the same key
40 // Remove key if we are adding a newer version of the same key
42 keyValueUpdateSet.remove(rmKV);
45 // Add the key to the hash set
46 keyValueUpdateSet.add(newKV);
51 * Add a new key value to the guard set
54 public void addKVGuard(KeyValue newKV) {
55 // Add the key to the hash set
56 keyValueGuardSet.add(newKV);
60 * Checks if the arbitrator is the same
63 public boolean checkArbitrator(long arb) {
64 if (arbitrator == -1) {
69 return arb == arbitrator;
73 * Get the transaction arbitrator
76 public long getArbitrator() {
81 * Get the key value update set
84 public Set<KeyValue> getKVUpdates() {
85 return keyValueUpdateSet;
90 * Get the key value update set
93 public Set<KeyValue> getKVGuard() {
94 return keyValueGuardSet;
97 public boolean evaluateGuard(Map<IoTString, KeyValue> keyValTableCommitted, Map<IoTString, KeyValue> keyValTableSpeculative, Map<IoTString, KeyValue> keyValTablePendingTransSpeculative) {
98 for (KeyValue kvGuard : keyValueGuardSet) {
100 // First check if the key is in the speculative table, this is the value of the latest assumption
101 KeyValue kv = keyValTablePendingTransSpeculative.get(kvGuard.getKey());
105 // if it is not in the pending trans table then check the speculative table and use that
106 // value as our latest assumption
107 kv = keyValTableSpeculative.get(kvGuard.getKey());
112 // if it is not in the speculative table then check the committed table and use that
113 // value as our latest assumption
114 kv = keyValTableCommitted.get(kvGuard.getKey());
117 if (kvGuard.getValue() != null) {
118 if ((kv == null) || (!kvGuard.getValue().equals(kv.getValue()))) {