3 import java.nio.ByteBuffer;
5 import java.util.HashSet;
7 class Transaction extends Entry {
10 private long machineid;
11 private Set<KeyValue> keyValueUpdateSet = null;
13 private Long arbitrator;
15 public Transaction(Slot slot, long _seqnum, long _machineid, Long _arbitrator, Set<KeyValue> _keyValueUpdateSet, Guard _guard) {
18 machineid = _machineid;
19 arbitrator = _arbitrator;
20 keyValueUpdateSet = new HashSet<KeyValue>();
22 for (KeyValue kv : _keyValueUpdateSet) {
23 KeyValue kvCopy = kv.getCopy();
24 keyValueUpdateSet.add(kvCopy);
27 guard = _guard.getCopy();
30 public long getMachineID() {
34 public long getArbitrator() {
38 public long getSequenceNumber() {
42 public Set<KeyValue> getkeyValueUpdateSet() {
43 return keyValueUpdateSet;
46 public Guard getGuard() {
50 public byte getType() {
51 return Entry.TypeTransaction;
54 public int getSize() {
55 int size = 3 * Long.BYTES + Byte.BYTES; // seq, machine id, entry type
56 size += Integer.BYTES; // number of KV's
59 for (KeyValue kv : keyValueUpdateSet) {
64 size += guard.getSize();
70 public void encode(ByteBuffer bb) {
71 bb.put(Entry.TypeTransaction);
73 bb.putLong(machineid);
74 bb.putLong(arbitrator);
76 bb.putInt(keyValueUpdateSet.size());
77 for (KeyValue kv : keyValueUpdateSet) {
84 static Entry decode(Slot slot, ByteBuffer bb) {
85 long seqnum = bb.getLong();
86 long machineid = bb.getLong();
87 long arbitrator = bb.getLong();
88 int numberOfKeys = bb.getInt();
90 Set<KeyValue> kvSet = new HashSet<KeyValue>();
91 for (int i = 0; i < numberOfKeys; i++) {
92 KeyValue kv = KeyValue.decode(bb);
96 Guard guard = Guard.decode(bb);
98 return new Transaction(slot, seqnum, machineid, arbitrator, kvSet, guard);
101 public Entry getCopy(Slot s) {
102 return new Transaction(s, seqnum, machineid, arbitrator, keyValueUpdateSet, guard);