Adding Fidelius manual.
[iotcloud.git] / version2 / backup / src / java / iotcloud / PendingTransaction.java
1 package iotcloud;
2
3 import java.util.Set;
4 import java.util.Map;
5 import java.util.HashSet;
6
7 import javax.script.ScriptException;
8 import java.lang.NullPointerException;
9
10
11 class PendingTransaction {
12
13     private Set<KeyValue> keyValueUpdateSet = null;
14     private Set<KeyValue> keyValueGuardSet = null;
15     private long arbitrator = -1;
16     private long machineLocalTransSeqNum = -1;
17
18     public PendingTransaction() {
19         keyValueUpdateSet = new HashSet<KeyValue>();
20         keyValueGuardSet = new HashSet<KeyValue>();
21     }
22
23     /**
24      * Add a new key value to the updates
25      *
26      */
27     public void addKV(KeyValue newKV) {
28
29         KeyValue rmKV = null;
30
31         // Make sure there are no duplicates
32         for (KeyValue kv : keyValueUpdateSet) {
33             if (kv.getKey().equals(newKV.getKey())) {
34
35                 // Remove key if we are adding a newer version of the same key
36                 rmKV = kv;
37                 break;
38             }
39         }
40
41         // Remove key if we are adding a newer version of the same key
42         if (rmKV != null) {
43             keyValueUpdateSet.remove(rmKV);
44         }
45
46         // Add the key to the hash set
47         keyValueUpdateSet.add(newKV);
48     }
49
50
51     /**
52      * Add a new key value to the guard set
53      *
54      */
55     public void addKVGuard(KeyValue newKV) {
56         // Add the key to the hash set
57         keyValueGuardSet.add(newKV);
58     }
59
60     /**
61      * Checks if the arbitrator is the same
62      *
63      */
64     public boolean checkArbitrator(long arb) {
65         if (arbitrator == -1) {
66             arbitrator = arb;
67             return true;
68         }
69
70         return arb == arbitrator;
71     }
72
73     /**
74      * Get the transaction arbitrator
75      *
76      */
77     public long getArbitrator() {
78         return arbitrator;
79     }
80
81     /**
82      * Get the key value update set
83      *
84      */
85     public Set<KeyValue> getKVUpdates() {
86         return keyValueUpdateSet;
87     }
88
89
90     /**
91        * Get the key value update set
92        *
93        */
94     public Set<KeyValue> getKVGuard() {
95         return keyValueGuardSet;
96     }
97
98     public void setMachineLocalTransSeqNum(long _machineLocalTransSeqNum) {
99         machineLocalTransSeqNum = _machineLocalTransSeqNum;
100     }
101
102     public long getMachineLocalTransSeqNum() {
103         return machineLocalTransSeqNum;
104     }
105
106     public boolean evaluateGuard(Map<IoTString, KeyValue> keyValTableCommitted, Map<IoTString, KeyValue> keyValTableSpeculative, Map<IoTString, KeyValue> keyValTablePendingTransSpeculative) {
107         for (KeyValue kvGuard : keyValueGuardSet) {
108
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());
111
112
113             if (kv == null) {
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());
117             }
118
119
120             if (kv == null) {
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());
124             }
125
126             if (kvGuard.getValue() != null) {
127                 if ((kv == null) || (!kvGuard.getValue().equals(kv.getValue()))) {
128                     return false;
129                 }
130             } else {
131                 if (kv != null) {
132                     return false;
133                 }
134             }
135         }
136         return true;
137     }
138 }