import java.nio.ByteBuffer;
import java.util.Set;
import java.util.HashSet;
+import java.util.Map;
class Transaction extends Entry {
private long seqnum;
private long machineid;
private Set<KeyValue> keyValueUpdateSet = null;
- private Guard guard;
+ private Set<KeyValue> keyValueGuardSet = null;
private Long arbitrator;
- public Transaction(Slot slot, long _seqnum, long _machineid, Long _arbitrator, Set<KeyValue> _keyValueUpdateSet, Guard _guard) {
+ public Transaction(Slot slot, long _seqnum, long _machineid, Long _arbitrator, Set<KeyValue> _keyValueUpdateSet, Set<KeyValue> _keyValueGuardSet) {
super(slot);
seqnum = _seqnum;
machineid = _machineid;
arbitrator = _arbitrator;
- keyValueUpdateSet = new HashSet<KeyValue>();
+ // keyValueUpdateSet = new HashSet<KeyValue>();
+ // keyValueGuardSet = new HashSet<KeyValue>();
- for (KeyValue kv : _keyValueUpdateSet) {
- KeyValue kvCopy = kv.getCopy();
- keyValueUpdateSet.add(kvCopy);
- }
+ // for (KeyValue kv : _keyValueUpdateSet) {
+ // KeyValue kvCopy = kv.getCopy();
+ // keyValueUpdateSet.add(kvCopy);
+ // }
+
+ // for (KeyValue kv : _keyValueGuardSet) {
+ // KeyValue kvCopy = kv.getCopy();
+ // keyValueGuardSet.add(kvCopy);
+ // }
- guard = _guard.getCopy();
+ keyValueUpdateSet = _keyValueUpdateSet;
+ keyValueGuardSet = _keyValueGuardSet;
}
public long getMachineID() {
return keyValueUpdateSet;
}
- public Guard getGuard() {
- return guard;
+ public Set<KeyValue> getkeyValueGuardSet() {
+ return keyValueGuardSet;
+ }
+
+ public boolean evaluateGuard(Map<IoTString, KeyValue> keyValTableCommitted, Map<IoTString, KeyValue> keyValTableSpeculative) {
+ for (KeyValue kvGuard : keyValueGuardSet) {
+
+ // First check if the key is in the speculative table, this is the value of the latest assumption
+ KeyValue kv = keyValTableSpeculative.get(kvGuard.getKey());
+
+ if (kv == null) {
+
+ // if it is not in the speculative table then check the committed table and use that
+ // value as our latest assumption
+ kv = keyValTableCommitted.get(kvGuard.getKey());
+ // System.out.println("Replaced With Commit Table");
+ }
+
+ if (kvGuard.getValue() != null) {
+ if ((kv == null) || (!kvGuard.getValue().equals(kv.getValue()))) {
+ // System.out.println("Fail 1 " + (kv == null) + " " + kvGuard.getValue() + " " + kv.getValue());
+ return false;
+ }
+ } else {
+ if (kv != null) {
+ // System.out.println("Fail 2 " + kv.getValue());
+ return false;
+ }
+ }
+ }
+ return true;
}
public byte getType() {
public int getSize() {
int size = 3 * Long.BYTES + Byte.BYTES; // seq, machine id, entry type
size += Integer.BYTES; // number of KV's
+ size += Integer.BYTES; // number of Guard KV's
// Size of each KV
for (KeyValue kv : keyValueUpdateSet) {
size += kv.getSize();
}
- // Size of the guard
- size += guard.getSize();
+ // Size of each Guard KV
+ for (KeyValue kv : keyValueGuardSet) {
+ size += kv.getSize();
+ }
return size;
}
-
public void encode(ByteBuffer bb) {
bb.put(Entry.TypeTransaction);
bb.putLong(seqnum);
kv.encode(bb);
}
- guard.encode(bb);
+ bb.putInt(keyValueGuardSet.size());
+ for (KeyValue kv : keyValueGuardSet) {
+ kv.encode(bb);
+ }
}
static Entry decode(Slot slot, ByteBuffer bb) {
long arbitrator = bb.getLong();
int numberOfKeys = bb.getInt();
- Set<KeyValue> kvSet = new HashSet<KeyValue>();
+ Set<KeyValue> kvSetUpdates = new HashSet<KeyValue>();
for (int i = 0; i < numberOfKeys; i++) {
KeyValue kv = KeyValue.decode(bb);
- kvSet.add(kv);
+ kvSetUpdates.add(kv);
}
- Guard guard = Guard.decode(bb);
+ int numberOfGuards = bb.getInt();
+ Set<KeyValue> kvSetGuards = new HashSet<KeyValue>();
+ for (int i = 0; i < numberOfGuards; i++) {
+ KeyValue kv = KeyValue.decode(bb);
+ kvSetGuards.add(kv);
+ }
- return new Transaction(slot, seqnum, machineid, arbitrator, kvSet, guard);
+ return new Transaction(slot, seqnum, machineid, arbitrator, kvSetUpdates, kvSetGuards);
}
public Entry getCopy(Slot s) {
- return new Transaction(s, seqnum, machineid, arbitrator, keyValueUpdateSet, guard);
+ return new Transaction(s, seqnum, machineid, arbitrator, keyValueUpdateSet, keyValueGuardSet);
}
}
\ No newline at end of file