4 import java.util.HashSet;
6 import javax.script.ScriptException;
7 import java.lang.NullPointerException;
10 class PendingTransaction {
12 static final byte Equal = Guard.Equal;
13 static final byte NotEqual = Guard.NotEqual;
15 private Set<KeyValue> keyValueUpdateSet;
17 private long arbitrator = -1;
19 public PendingTransaction() {
20 keyValueUpdateSet = new HashSet<KeyValue>();
25 * Add a new key value to the updates
28 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
35 keyValueUpdateSet.remove(kv);
40 // Add the key to the hash set
41 keyValueUpdateSet.add(newKV);
44 public boolean checkArbitrator(long arb) {
45 if (arbitrator == -1) {
50 return arb == arbitrator;
56 * Get the key value update set
59 public Set<KeyValue> getKVUpdates() {
60 return keyValueUpdateSet;
67 public Guard getGuard() {
72 * Add a guard to this transaction
75 public void addGuard(Guard _guard) {
80 * Evaluate the guard expression for a given transaction using a set of key value pairs.
83 public boolean evaluate(Set<KeyValue> kvSet) throws ScriptException, NullPointerException {
85 // Evaluate the guard using the current KV Set
86 return guard.evaluate(kvSet);
90 * Add a boolean expression to the guard.
93 public void setGuardExpression(String expr) {
94 guard.setGuardExpression(expr);
98 * Trampoline static method.
101 public static String createExpression(IoTString keyName, IoTString keyValue, byte op) {
102 return Guard.createExpression(keyName, keyValue, op);