tabbing
[iotcloud.git] / version2 / src / C / Transaction.h
index dab4949a4e93b30a3f1dd53e6d3dc1a91a17718f..04fbb9a819c60be7a3790665206c40b38ae7bcde 100644 (file)
 #include "common.h"
 
 class Transaction {
- private:
+private:
        Hashtable<int32_t, TransactionPart *> parts = NULL;
        Set<int32_t> missingParts = NULL;
        Vector<int32_t> partsPendingSend = NULL;
-  bool isComplete = false;
-  bool hasLastPart = false;
-       Hashset<KeyValue *> * keyValueGuardSet = NULL;
-       Hashset<KeyValue *> * keyValueUpdateSet = NULL;
+       bool isComplete = false;
+       bool hasLastPart = false;
+       Hashset<KeyValue *> *keyValueGuardSet = NULL;
+       Hashset<KeyValue *> *keyValueUpdateSet = NULL;
        bool isDead = false;
-  int64_t sequenceNumber = -1;
+       int64_t sequenceNumber = -1;
        int64_t clientLocalSequenceNumber = -1;
        int64_t arbitratorId = -1;
        int64_t machineId = -1;
        Pair<uint64_t, uint64_t> transactionId = NULL;
-       
-  int nextPartToSend = 0;
+
+       int nextPartToSend = 0;
        bool didSendAPartToServer = false;
-       
-  TransactionStatus transactionStatus = NULL;
 
- bool hadServerFailure = false;
+       TransactionStatus transactionStatus = NULL;
+
+       bool hadServerFailure = false;
+
+       public Transaction() {
+               parts = new Hashtable<int32_t, TransactionPart>();
+               keyValueGuardSet = new HashSet<KeyValue>();
+               keyValueUpdateSet = new HashSet<KeyValue>();
+               partsPendingSend = new Vector<int32_t>();
+       }
+
+       public void addPartEncode(TransactionPart newPart) {
+               parts.put(newPart.getPartNumber(), newPart);
+               partsPendingSend.add(newPart.getPartNumber());
+
+               sequenceNumber = newPart.getSequenceNumber();
+               arbitratorId = newPart.getArbitratorId();
+               transactionId = newPart.getTransactionId();
+               clientLocalSequenceNumber = newPart.getClientLocalSequenceNumber();
+               machineId = newPart.getMachineId();
+
+               isComplete = true;
+       }
+
+       public void addPartDecode(TransactionPart newPart) {
+
+               if (isDead) {
+                       // If dead then just kill this part and move on
+                       newPart.setDead();
+                       return;
+               }
+
+               sequenceNumber = newPart.getSequenceNumber();
+               arbitratorId = newPart.getArbitratorId();
+               transactionId = newPart.getTransactionId();
+               clientLocalSequenceNumber = newPart.getClientLocalSequenceNumber();
+               machineId = newPart.getMachineId();
+
+               TransactionPart previoslySeenPart = parts.put(newPart.getPartNumber(), newPart);
+
+               if (previoslySeenPart != NULL) {
+                       // Set dead the old one since the new one is a rescued version of this part
+                       previoslySeenPart.setDead();
+               } else if (newPart.isLastPart()) {
+                       missingParts = new HashSet<int32_t>();
+                       hasLastPart = true;
+
+                       for (int i = 0; i < newPart.getPartNumber(); i++) {
+                               if (parts.get(i) == NULL) {
+                                       missingParts.add(i);
+                               }
+                       }
+               }
+
+               if (!isComplete && hasLastPart) {
+
+                       // We have seen this part so remove it from the set of missing parts
+                       missingParts.remove(newPart.getPartNumber());
+
+                       // Check if all the parts have been seen
+                       if (missingParts.size() == 0) {
+
+                               // We have all the parts
+                               isComplete = true;
 
-    public Transaction() {
-        parts = new Hashtable<int32_t, TransactionPart>();
-        keyValueGuardSet = new HashSet<KeyValue>();
-        keyValueUpdateSet = new HashSet<KeyValue>();
-        partsPendingSend = new Vector<int32_t>();
-    }
-
-    public void addPartEncode(TransactionPart newPart) {
-        parts.put(newPart.getPartNumber(), newPart);
-        partsPendingSend.add(newPart.getPartNumber());
-
-        sequenceNumber = newPart.getSequenceNumber();
-        arbitratorId = newPart.getArbitratorId();
-        transactionId = newPart.getTransactionId();
-        clientLocalSequenceNumber = newPart.getClientLocalSequenceNumber();
-        machineId = newPart.getMachineId();
-
-        isComplete = true;
-    }
-
-    public void addPartDecode(TransactionPart newPart) {
+                               // Decode all the parts and create the key value guard and update sets
+                               decodeTransactionData();
+                       }
+               }
+       }
 
-        if (isDead) {
-            // If dead then just kill this part and move on
-            newPart.setDead();
-            return;
-        }
+       public void addUpdateKV(KeyValue kv) {
+               keyValueUpdateSet.add(kv);
+       }
 
-        sequenceNumber = newPart.getSequenceNumber();
-        arbitratorId = newPart.getArbitratorId();
-        transactionId = newPart.getTransactionId();
-        clientLocalSequenceNumber = newPart.getClientLocalSequenceNumber();
-        machineId = newPart.getMachineId();
+       public void addGuardKV(KeyValue kv) {
+               keyValueGuardSet.add(kv);
+       }
 
-        TransactionPart previoslySeenPart = parts.put(newPart.getPartNumber(), newPart);
 
-        if (previoslySeenPart != NULL) {
-            // Set dead the old one since the new one is a rescued version of this part
-            previoslySeenPart.setDead();
-        } else if (newPart.isLastPart()) {
-            missingParts = new HashSet<int32_t>();
-            hasLastPart = true;
-
-            for (int i = 0; i < newPart.getPartNumber(); i++) {
-                if (parts.get(i) == NULL) {
-                    missingParts.add(i);
-                }
-            }
-        }
+       public int64_t getSequenceNumber() {
+               return sequenceNumber;
+       }
 
-        if (!isComplete && hasLastPart) {
-
-            // We have seen this part so remove it from the set of missing parts
-            missingParts.remove(newPart.getPartNumber());
-
-            // Check if all the parts have been seen
-            if (missingParts.size() == 0) {
+       public void setSequenceNumber(int64_t _sequenceNumber) {
+               sequenceNumber = _sequenceNumber;
 
-                // We have all the parts
-                isComplete = true;
+               for (int32_t i : parts.keySet()) {
+                       parts.get(i).setSequenceNumber(sequenceNumber);
+               }
+       }
+
+       public int64_t getClientLocalSequenceNumber() {
+               return clientLocalSequenceNumber;
+       }
+
+       public Hashtable<int32_t, TransactionPart> getParts() {
+               return parts;
+       }
 
-                // Decode all the parts and create the key value guard and update sets
-                decodeTransactionData();
-            }
-        }
-    }
-
-    public void addUpdateKV(KeyValue kv) {
-        keyValueUpdateSet.add(kv);
-    }
-
-    public void addGuardKV(KeyValue kv) {
-        keyValueGuardSet.add(kv);
-    }
-
-
-    public int64_t getSequenceNumber() {
-        return sequenceNumber;
-    }
-
-    public void setSequenceNumber(int64_t _sequenceNumber) {
-        sequenceNumber = _sequenceNumber;
-
-        for (int32_t i : parts.keySet()) {
-            parts.get(i).setSequenceNumber(sequenceNumber);
-        }
-    }
-
-    public int64_t getClientLocalSequenceNumber() {
-        return clientLocalSequenceNumber;
-    }
-
-    public Hashtable<int32_t, TransactionPart> getParts() {
-        return parts;
-    }
-
-    public bool didSendAPartToServer() {
-        return didSendAPartToServer;
-    }
-
-    public void resetNextPartToSend() {
-        nextPartToSend = 0;
-    }
-
-    public TransactionPart getNextPartToSend() {
-        if ((partsPendingSend.size() == 0) || (partsPendingSend.size() == nextPartToSend)) {
-            return NULL;
-        }
-        TransactionPart part = parts.get(partsPendingSend.get(nextPartToSend));
-        nextPartToSend++;
-        return part;
-    }
-
-
-    public void setServerFailure() {
-        hadServerFailure = true;
-    }
-
-    public bool getServerFailure() {
-        return hadServerFailure;
-    }
-
-
-    public void resetServerFailure() {
-        hadServerFailure = false;
-    }
-
-
-    public void setTransactionStatus(TransactionStatus _transactionStatus) {
-        transactionStatus = _transactionStatus;
-    }
-
-    public TransactionStatus getTransactionStatus() {
-        return transactionStatus;
-    }
-
-    public void removeSentParts(Vector<int32_t> sentParts) {
-        nextPartToSend = 0;
-        if(partsPendingSend.removeAll(sentParts))
-        {
-            didSendAPartToServer = true;
-            transactionStatus.setTransactionSequenceNumber(sequenceNumber);
-        }
-    }
-
-    public bool didSendAllParts() {
-        return partsPendingSend.isEmpty();
-    }
-
-    public Set<KeyValue> getKeyValueUpdateSet() {
-        return keyValueUpdateSet;
-    }
-
-    public int getNumberOfParts() {
-        return parts.size();
-    }
-
-    public int64_t getMachineId() {
-        return machineId;
-    }
-
-    public int64_t getArbitrator() {
-        return arbitratorId;
-    }
-
-    public bool isComplete() {
-        return isComplete;
-    }
-
-    public Pair<int64_t, int64_t> getId() {
-        return transactionId;
-    }
-
-    public void setDead() {
-        if (isDead) {
-            // Already dead
-            return;
-        }
-
-        // Set dead
-        isDead = true;
-
-        // Make all the parts of this transaction dead
-        for (int32_t partNumber : parts.keySet()) {
-            TransactionPart part = parts.get(partNumber);
-            part.setDead();
-        }
-    }
-
-    public TransactionPart getPart(int index) {
-        return parts.get(index);
-    }
-
-    private void decodeTransactionData() {
-
-        // Calculate the size of the data section
-        int dataSize = 0;
-        for (int i = 0; i < parts.keySet().size(); i++) {
-            TransactionPart tp = parts.get(i);
-            dataSize += tp.getDataSize();
-        }
-
-        char[] combinedData = new char[dataSize];
-        int currentPosition = 0;
-
-        // Stitch all the data sections together
-        for (int i = 0; i < parts.keySet().size(); i++) {
-            TransactionPart tp = parts.get(i);
-            System.arraycopy(tp.getData(), 0, combinedData, currentPosition, tp.getDataSize());
-            currentPosition += tp.getDataSize();
-        }
-
-        // Decoder Object
-        ByteBuffer bbDecode = ByteBuffer.wrap(combinedData);
-
-        // Decode how many key value pairs need to be decoded
-        int numberOfKVGuards = bbDecode.getInt();
-        int numberOfKVUpdates = bbDecode.getInt();
-
-        // Decode all the guard key values
-        for (int i = 0; i < numberOfKVGuards; i++) {
-            KeyValue kv = (KeyValue)KeyValue.decode(bbDecode);
-            keyValueGuardSet.add(kv);
-        }
-
-        // Decode all the updates key values
-        for (int i = 0; i < numberOfKVUpdates; i++) {
-            KeyValue kv = (KeyValue)KeyValue.decode(bbDecode);
-            keyValueUpdateSet.add(kv);
-        }
-    }
-
-    public bool evaluateGuard(Hashtable<IoTString, KeyValue> committedKeyValueTable, Hashtable<IoTString, KeyValue> speculatedKeyValueTable, Hashtable<IoTString, KeyValue> pendingTransactionSpeculatedKeyValueTable) {
-        for (KeyValue kvGuard : keyValueGuardSet) {
-
-            // First check if the key is in the speculative table, this is the value of the latest assumption
-            KeyValue kv = NULL;
-
-            // If we have a speculation table then use it first
-            if (pendingTransactionSpeculatedKeyValueTable != NULL) {
-                kv = pendingTransactionSpeculatedKeyValueTable.get(kvGuard.getKey());
-            }
-
-            // If we have a speculation table then use it first
-            if ((kv == NULL) && (speculatedKeyValueTable != NULL)) {
-                kv = speculatedKeyValueTable.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 = committedKeyValueTable.get(kvGuard.getKey());
-            }
-
-            if (kvGuard.getValue() != NULL) {
-                if ((kv == NULL) || (!kvGuard.getValue().equals(kv.getValue()))) {
-
-
-                    if (kv != NULL) {
-                        System.out.println(kvGuard.getValue() + "       " + kv.getValue());
-                    } else {
-                        System.out.println(kvGuard.getValue() + "       " + kv);
-                    }
-
-                    return false;
-                }
-            } else {
-                if (kv != NULL) {
-                    return false;
-                }
-            }
-        }
-        return true;
-    }
+       public bool didSendAPartToServer() {
+               return didSendAPartToServer;
+       }
+
+       public void resetNextPartToSend() {
+               nextPartToSend = 0;
+       }
+
+       public TransactionPart getNextPartToSend() {
+               if ((partsPendingSend.size() == 0) || (partsPendingSend.size() == nextPartToSend)) {
+                       return NULL;
+               }
+               TransactionPart part = parts.get(partsPendingSend.get(nextPartToSend));
+               nextPartToSend++;
+               return part;
+       }
+
+
+       public void setServerFailure() {
+               hadServerFailure = true;
+       }
+
+       public bool getServerFailure() {
+               return hadServerFailure;
+       }
+
+
+       public void resetServerFailure() {
+               hadServerFailure = false;
+       }
+
+
+       public void setTransactionStatus(TransactionStatus _transactionStatus) {
+               transactionStatus = _transactionStatus;
+       }
+
+       public TransactionStatus getTransactionStatus() {
+               return transactionStatus;
+       }
+
+       public void removeSentParts(Vector<int32_t> sentParts) {
+               nextPartToSend = 0;
+               if (partsPendingSend.removeAll(sentParts))
+               {
+                       didSendAPartToServer = true;
+                       transactionStatus.setTransactionSequenceNumber(sequenceNumber);
+               }
+       }
+
+       public bool didSendAllParts() {
+               return partsPendingSend.isEmpty();
+       }
+
+       public Set<KeyValue> getKeyValueUpdateSet() {
+               return keyValueUpdateSet;
+       }
+
+       public int getNumberOfParts() {
+               return parts.size();
+       }
+
+       public int64_t getMachineId() {
+               return machineId;
+       }
+
+       public int64_t getArbitrator() {
+               return arbitratorId;
+       }
+
+       public bool isComplete() {
+               return isComplete;
+       }
+
+       public Pair<int64_t, int64_t> getId() {
+               return transactionId;
+       }
+
+       public void setDead() {
+               if (isDead) {
+                       // Already dead
+                       return;
+               }
+
+               // Set dead
+               isDead = true;
+
+               // Make all the parts of this transaction dead
+               for (int32_t partNumber : parts.keySet()) {
+                       TransactionPart part = parts.get(partNumber);
+                       part.setDead();
+               }
+       }
+
+       public TransactionPart getPart(int index) {
+               return parts.get(index);
+       }
+
+       private void decodeTransactionData() {
+
+               // Calculate the size of the data section
+               int dataSize = 0;
+               for (int i = 0; i < parts.keySet().size(); i++) {
+                       TransactionPart tp = parts.get(i);
+                       dataSize += tp.getDataSize();
+               }
+
+               char[] combinedData = new char[dataSize];
+               int currentPosition = 0;
+
+               // Stitch all the data sections together
+               for (int i = 0; i < parts.keySet().size(); i++) {
+                       TransactionPart tp = parts.get(i);
+                       System.arraycopy(tp.getData(), 0, combinedData, currentPosition, tp.getDataSize());
+                       currentPosition += tp.getDataSize();
+               }
+
+               // Decoder Object
+               ByteBuffer bbDecode = ByteBuffer.wrap(combinedData);
+
+               // Decode how many key value pairs need to be decoded
+               int numberOfKVGuards = bbDecode.getInt();
+               int numberOfKVUpdates = bbDecode.getInt();
+
+               // Decode all the guard key values
+               for (int i = 0; i < numberOfKVGuards; i++) {
+                       KeyValue kv = (KeyValue)KeyValue.decode(bbDecode);
+                       keyValueGuardSet.add(kv);
+               }
+
+               // Decode all the updates key values
+               for (int i = 0; i < numberOfKVUpdates; i++) {
+                       KeyValue kv = (KeyValue)KeyValue.decode(bbDecode);
+                       keyValueUpdateSet.add(kv);
+               }
+       }
+
+       public bool evaluateGuard(Hashtable<IoTString, KeyValue> committedKeyValueTable, Hashtable<IoTString, KeyValue> speculatedKeyValueTable, Hashtable<IoTString, KeyValue> pendingTransactionSpeculatedKeyValueTable) {
+               for (KeyValue kvGuard : keyValueGuardSet) {
+
+                       // First check if the key is in the speculative table, this is the value of the latest assumption
+                       KeyValue kv = NULL;
+
+                       // If we have a speculation table then use it first
+                       if (pendingTransactionSpeculatedKeyValueTable != NULL) {
+                               kv = pendingTransactionSpeculatedKeyValueTable.get(kvGuard.getKey());
+                       }
+
+                       // If we have a speculation table then use it first
+                       if ((kv == NULL) && (speculatedKeyValueTable != NULL)) {
+                               kv = speculatedKeyValueTable.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 = committedKeyValueTable.get(kvGuard.getKey());
+                       }
+
+                       if (kvGuard.getValue() != NULL) {
+                               if ((kv == NULL) || (!kvGuard.getValue().equals(kv.getValue()))) {
+
+
+                                       if (kv != NULL) {
+                                               System.out.println(kvGuard.getValue() + "       " + kv.getValue());
+                                       } else {
+                                               System.out.println(kvGuard.getValue() + "       " + kv);
+                                       }
+
+                                       return false;
+                               }
+                       } else {
+                               if (kv != NULL) {
+                                       return false;
+                               }
+                       }
+               }
+               return true;
+       }
 };
 #endif