-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;
- Map<Transaction, List<Integer>> lastTransactionPartsSent = NULL;
- List<Entry> lastPendingSendArbitrationEntriesToDelete = NULL;
- NewKey lastNewKey = NULL;
-
-
- /* Data Structures */
- Map<IoTString, KeyValue> committedKeyValueTable = NULL; // Table of committed key value pairs
- Map<IoTString, KeyValue> speculatedKeyValueTable = NULL; // Table of speculated key value pairs, if there is a speculative value
- Map<IoTString, KeyValue> pendingTransactionSpeculatedKeyValueTable = NULL; // Table of speculated key value pairs, if there is a speculative value from the pending transactions
- Map<IoTString, NewKey> liveNewKeyTable = NULL; // Table of live new keys
- HashMap<Long, Pair<Long, Liveness>> lastMessageTable = NULL; // Last message sent by a client machine id -> (Seq Num, Slot or LastMessage);
- HashMap<Long, HashSet<RejectedMessage>> rejectedMessageWatchListTable = NULL; // Table of machine Ids and the set of rejected messages they have not seen yet
- Map<IoTString, Long> arbitratorTable = NULL; // Table of keys and their arbitrators
- Map<Pair<int64_t, int64_t>, Abort> liveAbortTable = NULL; // Table live abort messages
- Map<Long, Map<Pair<Long, Integer>, TransactionPart>> newTransactionParts = NULL; // transaction parts that are seen in this latest round of slots from the server
- Map<Long, Map<Pair<Long, Integer>, CommitPart>> newCommitParts = NULL; // commit parts that are seen in this latest round of slots from the server
- Map<int64_t, int64_t> lastArbitratedTransactionNumberByArbitratorTable = NULL; // Last transaction sequence number that an arbitrator arbitrated on
- Map<Long, Transaction> liveTransactionBySequenceNumberTable = NULL; // live transaction grouped by the sequence number
- Map<Pair<int64_t, int64_t>, Transaction> liveTransactionByTransactionIdTable = NULL; // live transaction grouped by the transaction ID
- Map<Long, Map<Long, Commit>> liveCommitsTable = NULL;
- Map<IoTString, Commit> liveCommitsByKeyTable = NULL;
- Map<int64_t, int64_t> lastCommitSeenSequenceNumberByArbitratorTable = NULL;
- Vector<Long> rejectedSlotList = NULL; // List of rejected slots that have yet to be sent to the server
- List<Transaction> pendingTransactionQueue = NULL;
- List<ArbitrationRound> pendingSendArbitrationRounds = NULL;
- List<Entry> pendingSendArbitrationEntriesToDelete = NULL;
- Map<Transaction, List<Integer>> transactionPartsSent = NULL;
- Map<Long, TransactionStatus> outstandingTransactionStatus = NULL;
- Map<Long, Abort> liveAbortsGeneratedByLocal = NULL;
- Set<Pair<int64_t, int64_t>> offlineTransactionsCommittedAndAtServer = NULL;
- Map<Long, Pair<String, Integer>> localCommunicationTable = NULL;
- Map<int64_t, int64_t> lastTransactionSeenFromMachineFromServer = NULL;
- Map<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 HashMap<IoTString, KeyValue>();
- speculatedKeyValueTable = new HashMap<IoTString, KeyValue>();
- pendingTransactionSpeculatedKeyValueTable = new HashMap<IoTString, KeyValue>();
- liveNewKeyTable = new HashMap<IoTString, NewKey>();
- lastMessageTable = new HashMap<Long, Pair<Long, Liveness>>();
- rejectedMessageWatchListTable = new HashMap<Long, HashSet<RejectedMessage>>();
- arbitratorTable = new HashMap<IoTString, Long>();
- liveAbortTable = new HashMap<Pair<int64_t, int64_t>, Abort>();
- newTransactionParts = new HashMap<Long, Map<Pair<Long, Integer>, TransactionPart>>();
- newCommitParts = new HashMap<Long, Map<Pair<Long, Integer>, CommitPart>>();
- lastArbitratedTransactionNumberByArbitratorTable = new HashMap<int64_t, int64_t>();
- liveTransactionBySequenceNumberTable = new HashMap<Long, Transaction>();
- liveTransactionByTransactionIdTable = new HashMap<Pair<int64_t, int64_t>, Transaction>();
- liveCommitsTable = new HashMap<Long, Map<Long, Commit>>();
- liveCommitsByKeyTable = new HashMap<IoTString, Commit>();
- lastCommitSeenSequenceNumberByArbitratorTable = new HashMap<int64_t, int64_t>();
- rejectedSlotList = new Vector<Long>();
- pendingTransactionQueue = new ArrayList<Transaction>();
- pendingSendArbitrationEntriesToDelete = new ArrayList<Entry>();
- transactionPartsSent = new HashMap<Transaction, List<Integer>>();
- outstandingTransactionStatus = new HashMap<Long, TransactionStatus>();
- liveAbortsGeneratedByLocal = new HashMap<Long, Abort>();
- offlineTransactionsCommittedAndAtServer = new HashSet<Pair<int64_t, int64_t>>();
- localCommunicationTable = new HashMap<Long, Pair<String, Integer>>();
- lastTransactionSeenFromMachineFromServer = new HashMap<int64_t, int64_t>();
- pendingSendArbitrationRounds = new ArrayList<ArbitrationRound>();
- lastArbitrationDataLocalSequenceNumberSeenFromArbitrator = new HashMap<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++;
- }
-
- Vector<Entry> entries = s.getEntries();
-
- for (Entry e : entries) {
- if (e.isLive()) {
- int type = e.getType();
-
-
- if (type == 6) {
- RejectedMessage rej = (RejectedMessage)e;
- casdasd++;
-
- System.out.println(rej.getMachineID());
- }
-
-
- types[type] = types[type] + 1;
- num++;
- livec++;
- } else {
- deadc++;
- }
- }
- }