-final class Table {
-
- /* Constants */
- static final int FREE_SLOTS = 2;// Number of slots that should be kept free // 10
- static final int SKIP_THRESHOLD = 10;
- static final double RESIZE_MULTIPLE = 1.2;
- static final double RESIZE_THRESHOLD = 0.75;
- static final int REJECTED_THRESHOLD = 5;
-
- /* Helper Objects */
- SlotBuffer buffer = NULL;
- CloudComm cloud = NULL;
- Random random = NULL;
- TableStatus liveTableStatus = NULL;
- PendingTransaction pendingTransactionBuilder = NULL;// Pending Transaction used in building a Pending Transaction
- Transaction lastPendingTransactionSpeculatedOn = NULL;// Last transaction that was speculated on from the pending transaction
- Transaction firstPendingTransaction = NULL; // first transaction in the pending transaction list
-
- /* Variables */
- int numberOfSlots = 0; // Number of slots stored in buffer
- int bufferResizeThreshold = 0;// Threshold on the number of live slots before a resize is needed
- int64_t liveSlotCount = 0;// Number of currently live slots
- int64_t oldestLiveSlotSequenceNumver = 0; // Smallest sequence number of the slot with a live entry
- int64_t localMachineId = 0; // Machine ID of this client device
- int64_t sequenceNumber = 0; // Largest sequence number a client has received
- int64_t localSequenceNumber = 0;
-
- // int smallestTableStatusSeen = -1; // Smallest Table Status that was seen in the latest slots sent from the server
- // int largestTableStatusSeen = -1; // Largest Table Status that was seen in the latest slots sent from the server
- int64_t localTransactionSequenceNumber = 0; // Local sequence number counter for transactions
- int64_t lastTransactionSequenceNumberSpeculatedOn = -1; // the last transaction that was speculated on
- int64_t oldestTransactionSequenceNumberSpeculatedOn = -1; // the oldest transaction that was speculated on
- int64_t localArbitrationSequenceNumber = 0;
- bool hadPartialSendToServer = false;
- bool attemptedToSendToServer = false;
- int64_t expectedsize;
- bool didFindTableStatus = false;
- int64_t currMaxSize = 0;
-
- Slot lastSlotAttemptedToSend = NULL;
- bool lastIsNewKey = false;
- int lastNewSize = 0;
- Hashtable<Transaction, Vector<int32_t> > lastTransactionPartsSent = NULL;
- Vector<Entry> lastPendingSendArbitrationEntriesToDelete = NULL;
- NewKey lastNewKey = NULL;
-
-
- /* Data Structures */
- Hashtable<IoTString, KeyValue> committedKeyValueTable = NULL; // Table of committed key value pairs
- Hashtable<IoTString, KeyValue> speculatedKeyValueTable = NULL;// Table of speculated key value pairs, if there is a speculative value
- Hashtable<IoTString, KeyValue> pendingTransactionSpeculatedKeyValueTable = NULL;// Table of speculated key value pairs, if there is a speculative value from the pending transactions
- Hashtable<IoTString, NewKey> liveNewKeyTable = NULL;// Table of live new keys
- Hashtable<int64_t Pair<int64_t Liveness> > lastMessageTable = NULL; // Last message sent by a client machine id -> (Seq Num, Slot or LastMessage);
- Hashtable<int64_t HashSet<RejectedMessage> > rejectedMessageWatchVectorTable = NULL;// Table of machine Ids and the set of rejected messages they have not seen yet
- Hashtable<IoTString, Long> arbitratorTable = NULL;// Table of keys and their arbitrators
- Hashtable<Pair<int64_t, int64_t>, Abort> liveAbortTable = NULL; // Table live abort messages
- Hashtable<int64_t Hashtable<Pair<int64_t int32_t>, TransactionPart> > newTransactionParts = NULL; // transaction parts that are seen in this latest round of slots from the server
- Hashtable<int64_t Hashtable<Pair<int64_t int32_t>, CommitPart> > newCommitParts = NULL; // commit parts that are seen in this latest round of slots from the server
- Hashtable<int64_t, int64_t> lastArbitratedTransactionNumberByArbitratorTable = NULL;// Last transaction sequence number that an arbitrator arbitrated on
- Hashtable<int64_t Transaction> liveTransactionBySequenceNumberTable = NULL; // live transaction grouped by the sequence number
- Hashtable<Pair<int64_t, int64_t>, Transaction> liveTransactionByTransactionIdTable = NULL;// live transaction grouped by the transaction ID
- Hashtable<int64_t Hashtable<int64_t Commit> > liveCommitsTable = NULL;
- Hashtable<IoTString, Commit> liveCommitsByKeyTable = NULL;
- Hashtable<int64_t, int64_t> lastCommitSeenSequenceNumberByArbitratorTable = NULL;
- Vector<Long> rejectedSlotVector = NULL; // Vector of rejected slots that have yet to be sent to the server
- Vector<Transaction> pendingTransactionQueue = NULL;
- Vector<ArbitrationRound> pendingSendArbitrationRounds = NULL;
- Vector<Entry> pendingSendArbitrationEntriesToDelete = NULL;
- Hashtable<Transaction, Vector<int32_t> > transactionPartsSent = NULL;
- Hashtable<int64_t TransactionStatus> outstandingTransactionStatus = NULL;
- Hashtable<int64_t Abort> liveAbortsGeneratedByLocal = NULL;
- Set<Pair<int64_t, int64_t> > offlineTransactionsCommittedAndAtServer = NULL;
- Hashtable<int64_t Pair<String, int32_t> > localCommunicationTable = NULL;
- Hashtable<int64_t, int64_t> lastTransactionSeenFromMachineFromServer = NULL;
- Hashtable<int64_t, int64_t> lastArbitrationDataLocalSequenceNumberSeenFromArbitrator = NULL;
-
-
- Table(String baseurl, String password, int64_t _localMachineId, int listeningPort) {
- localMachineId = _localMachineId;
- cloud = new CloudComm(this, baseurl, password, listeningPort);
-
- init();
- }
-
- Table(CloudComm _cloud, int64_t _localMachineId) {
- localMachineId = _localMachineId;
- cloud = _cloud;
-
- init();
- }
-
- /**
- * Init all the stuff needed for for table usage
- */
- void init() {
-
- // Init helper objects
- random = new Random();
- buffer = new SlotBuffer();
-
- // Set Variables
- oldestLiveSlotSequenceNumver = 1;
-
- // init data structs
- committedKeyValueTable = new Hashtable<IoTString, KeyValue>();
- speculatedKeyValueTable = new Hashtable<IoTString, KeyValue>();
- pendingTransactionSpeculatedKeyValueTable = new Hashtable<IoTString, KeyValue>();
- liveNewKeyTable = new Hashtable<IoTString, NewKey>();
- lastMessageTable = new Hashtable<int64_t Pair<int64_t Liveness> >();
- rejectedMessageWatchVectorTable = new Hashtable<int64_t HashSet<RejectedMessage> >();
- arbitratorTable = new Hashtable<IoTString, Long>();
- liveAbortTable = new Hashtable<Pair<int64_t, int64_t>, Abort>();
- newTransactionParts = new Hashtable<int64_t Hashtable<Pair<int64_t int32_t>, TransactionPart> >();
- newCommitParts = new Hashtable<int64_t Hashtable<Pair<int64_t int32_t>, CommitPart> >();
- lastArbitratedTransactionNumberByArbitratorTable = new Hashtable<int64_t, int64_t>();
- liveTransactionBySequenceNumberTable = new Hashtable<int64_t Transaction>();
- liveTransactionByTransactionIdTable = new Hashtable<Pair<int64_t, int64_t>, Transaction>();
- liveCommitsTable = new Hashtable<int64_t Hashtable<int64_t Commit> >();
- liveCommitsByKeyTable = new Hashtable<IoTString, Commit>();
- lastCommitSeenSequenceNumberByArbitratorTable = new Hashtable<int64_t, int64_t>();
- rejectedSlotVector = new Vector<Long>();
- pendingTransactionQueue = new Vector<Transaction>();
- pendingSendArbitrationEntriesToDelete = new Vector<Entry>();
- transactionPartsSent = new Hashtable<Transaction, Vector<int32_t> >();
- outstandingTransactionStatus = new Hashtable<int64_t TransactionStatus>();
- liveAbortsGeneratedByLocal = new Hashtable<int64_t Abort>();
- offlineTransactionsCommittedAndAtServer = new HashSet<Pair<int64_t, int64_t> >();
- localCommunicationTable = new Hashtable<int64_t Pair<String, int32_t> >();
- lastTransactionSeenFromMachineFromServer = new Hashtable<int64_t, int64_t>();
- pendingSendArbitrationRounds = new Vector<ArbitrationRound>();
- lastArbitrationDataLocalSequenceNumberSeenFromArbitrator = new Hashtable<int64_t, int64_t>();
-
-
- // Other init stuff
- numberOfSlots = buffer.capacity();
- setResizeThreshold();
- }
-
- // TODO: delete method
- synchronized void printSlots() {
- int64_t o = buffer.getOldestSeqNum();
- int64_t n = buffer.getNewestSeqNum();
-
- int[] types = new int[10];
-
- int num = 0;
-
- int livec = 0;
- int deadc = 0;
-
- int casdasd = 0;
-
- int liveslo = 0;
-
- for (int64_t i = o; i < (n + 1); i++) {
- Slot s = buffer.getSlot(i);
-
-
- if (s.isLive()) {
- liveslo++;
- }