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) {
32 // Make sure there are no duplicates
33 for (KeyValue kv : keyValueUpdateSet) {
34 if (kv.getKey().equals(newKV.getKey())) {
36 // Remove key if we are adding a newer version of the same key
42 // Remove key if we are adding a newer version of the same key
44 keyValueUpdateSet.remove(rmKV);
47 // Add the key to the hash set
48 keyValueUpdateSet.add(newKV);
51 public boolean checkArbitrator(long arb) {
52 if (arbitrator == -1) {
57 return arb == arbitrator;
63 * Get the key value update set
66 public Set<KeyValue> getKVUpdates() {
67 return keyValueUpdateSet;
74 public Guard getGuard() {
79 * Add a guard to this transaction
82 public void addGuard(Guard _guard) {
87 * Evaluate the guard expression for a given transaction using a set of key value pairs.
90 public boolean evaluate(Set<KeyValue> kvSet) throws ScriptException, NullPointerException {
92 // Evaluate the guard using the current KV Set
93 return guard.evaluate(kvSet);
97 * Add a boolean expression to the guard.
100 public void setGuardExpression(String expr) {
101 guard.setGuardExpression(expr);
105 * Trampoline static method.
108 public static String createExpression(IoTString keyName, IoTString keyValue, byte op) {
109 return Guard.createExpression(keyName, keyValue, op);