Changed way Guard works, Sped up code
[iotcloud.git] / version2 / src / java / iotcloud / PendingTransaction.java
1 package iotcloud;
2
3 import java.util.Set;
4 import java.util.HashSet;
5
6 import javax.script.ScriptException;
7 import java.lang.NullPointerException;
8
9
10 class PendingTransaction {
11
12     private Set<KeyValue> keyValueUpdateSet = null;
13     private Set<KeyValue> keyValueGuardSet = null;
14     private long arbitrator = -1;
15
16     public PendingTransaction() {
17         keyValueUpdateSet = new HashSet<KeyValue>();
18         keyValueGuardSet = new HashSet<KeyValue>();
19     }
20
21     /**
22      * Add a new key value to the updates
23      *
24      */
25     public void addKV(KeyValue newKV) {
26
27         KeyValue rmKV = null;
28
29         // Make sure there are no duplicates
30         for (KeyValue kv : keyValueUpdateSet) {
31             if (kv.getKey().equals(newKV.getKey())) {
32
33                 // Remove key if we are adding a newer version of the same key
34                 rmKV = kv;
35                 break;
36             }
37         }
38
39         // Remove key if we are adding a newer version of the same key
40         if (rmKV != null) {
41             keyValueUpdateSet.remove(rmKV);
42         }
43
44         // Add the key to the hash set
45         keyValueUpdateSet.add(newKV);
46     }
47
48
49     /**
50      * Add a new key value to the guard set
51      *
52      */
53     public void addKVGuard(KeyValue newKV) {
54         // Add the key to the hash set
55         keyValueGuardSet.add(newKV);
56     }
57
58     /**
59      * Checks if the arbitrator is the same
60      *
61      */
62     public boolean checkArbitrator(long arb) {
63         if (arbitrator == -1) {
64             arbitrator = arb;
65             return true;
66         }
67
68         return arb == arbitrator;
69     }
70
71     /**
72      * Get the transaction arbitrator
73      *
74      */
75     public long getArbitrator() {
76         return arbitrator;
77     }
78
79     /**
80      * Get the key value update set
81      *
82      */
83     public Set<KeyValue> getKVUpdates() {
84         return keyValueUpdateSet;
85     }
86
87
88     /**
89        * Get the key value update set
90        *
91        */
92     public Set<KeyValue> getKVGuard() {
93         return keyValueGuardSet;
94     }
95 }