/** * Takes a delta full of operations by the same creator from the head of the * queue, removing those operations from the queue. It will contain all * operations from the head of the queue up until but not including the first * change in creator. Hence if all operations in the queue have the same * creator, it will contain all those operations and the queue will become * empty. * * @return A non-empty delta without signature or version information, * containing operations which all have the same creator address in * their context. * @throws NoSuchElementException If the queue is empty. */ public List<WaveletOperation> take() { Item item = takeMergedAndOptimisedItem(queue); if (isEmpty()) { tailCreator = null; } return item.opSequence; }
/** * Closes this concurrency control. */ public void close() { if (unsavedDataListener != null) { unsavedDataListener.onClose(everythingIsCommitted()); } if (!clientOperationQueue.isEmpty()) { logger.error().log("Concurrency control closed with pending operations. Data has been lost"); } }
/** Asserts that the queue is empty and size estimate is zero. */ private void assertQueueIsEmpty() { assertTrue("Expected empty queue", queue.isEmpty()); assertEquals("Expected queue size zero", 0, queue.estimateSize()); }
/** * Asserts that the queue is not empty and the size estimate is as expected. */ private void assertQueueSize(int size) { assertFalse("Expected non-empty queue", queue.isEmpty()); int estimate = queue.estimateSize(); assertEquals("Expected queue size " + size + ", was " + estimate, size, estimate); }
/** Asserts that the queue is empty and size estimate is zero. */ private void assertQueueIsEmpty() { assertTrue("Expected empty queue", queue.isEmpty()); assertEquals("Expected queue size zero", 0, queue.estimateSize()); }
/** * Asserts that the queue is not empty and the size estimate is as expected. */ private void assertQueueSize(int size) { assertFalse("Expected non-empty queue", queue.isEmpty()); int estimate = queue.estimateSize(); assertEquals("Expected queue size " + size + ", was " + estimate, size, estimate); }
/** * Asserts that the queue is not empty and the size estimate is within * expected bounds (inclusive). */ private void assertQueueSizeBetween(int minSize, int maxSize) { assertFalse("Expected non-empty queue", queue.isEmpty()); int estimate = queue.estimateSize(); assertTrue("Expected queue size >= " + minSize + ", was " + estimate, estimate >= minSize); assertTrue("Expected queue size <= " + maxSize + ", was " + estimate, estimate <= maxSize); }
/** * Asserts that the queue is not empty and the size estimate is within * expected bounds (inclusive). */ private void assertQueueSizeBetween(int minSize, int maxSize) { assertFalse("Expected non-empty queue", queue.isEmpty()); int estimate = queue.estimateSize(); assertTrue("Expected queue size >= " + minSize + ", was " + estimate, estimate >= minSize); assertTrue("Expected queue size <= " + maxSize + ", was " + estimate, estimate <= maxSize); }
if (clientOperationQueue.isEmpty()) { logger.trace().log("Nothing to send");
/** * Tests that a new head delta which is being inserted does not merge with the * existing head if the creators do not match, hence the old head is moved to * be the second delta but otherwise remains unmodified. */ public void testInsertHeadMakesNewDeltaIfCreatorDiffersFromExistingHead() { queue.add(BOB_C); MergingSequence head = new MergingSequence(); head.add(JIM_A); head.add(JIM_B); queue.insertHead(head); assertEquals(list(JIM_A, JIM_B), copyList(queue.take())); assertEquals(list(BOB_C), copyList(queue.take())); assertTrue(queue.isEmpty()); }
/** * Tests that a new head delta which is being inserted does not merge with the * existing head if the creators do not match, hence the old head is moved to * be the second delta but otherwise remains unmodified. */ public void testInsertHeadMakesNewDeltaIfCreatorDiffersFromExistingHead() { queue.add(BOB_C); MergingSequence head = new MergingSequence(); head.add(JIM_A); head.add(JIM_B); queue.insertHead(head); assertEquals(list(JIM_A, JIM_B), copyList(queue.take())); assertEquals(list(BOB_C), copyList(queue.take())); assertTrue(queue.isEmpty()); }
/** * Tests that a new head delta can be inserted in at the start of an empty * queue. Future adds of ops by the same creator do not merge with it. */ public void testHeadInsertedIntoEmptyQueueAllowsFutureMerging() { MergingSequence head = new MergingSequence(); head.add(BOB_A); head.add(BOB_B); queue.insertHead(head); queue.add(BOB_C); assertQueueSizeBetween(1, 3); // Delta pushed to head is not mergable to a single delta. assertEquals(list(BOB_A, BOB_B), copyList(queue.take())); assertEquals(list(BOB_C), copyList(queue.take())); assertTrue(queue.isEmpty()); }
/** * Tests that a new head delta can be inserted in a non-empty queue. * It does not merge with the existing head. */ public void testInsertHeadMergesIfSameCreatorAsExistingHead() { queue.add(BOB_C); MergingSequence head = new MergingSequence(); head.add(BOB_A); head.add(BOB_B); queue.insertHead(head); assertQueueSizeBetween(1, 3); // Delta pushed to head is not mergable to a single delta. assertEquals(list(BOB_A, BOB_B), copyList(queue.take())); assertEquals(list(BOB_C), copyList(queue.take())); assertTrue(queue.isEmpty()); }
/** * Tests that a new head delta can be inserted in a non-empty queue. * It does not merge with the existing head. */ public void testInsertHeadMergesIfSameCreatorAsExistingHead() { queue.add(BOB_C); MergingSequence head = new MergingSequence(); head.add(BOB_A); head.add(BOB_B); queue.insertHead(head); assertQueueSizeBetween(1, 3); // Delta pushed to head is not mergable to a single delta. assertEquals(list(BOB_A, BOB_B), copyList(queue.take())); assertEquals(list(BOB_C), copyList(queue.take())); assertTrue(queue.isEmpty()); }
/** * Tests that a new head delta can be inserted in at the start of an empty * queue. Future adds of ops by the same creator do not merge with it. */ public void testHeadInsertedIntoEmptyQueueAllowsFutureMerging() { MergingSequence head = new MergingSequence(); head.add(BOB_A); head.add(BOB_B); queue.insertHead(head); queue.add(BOB_C); assertQueueSizeBetween(1, 3); // Delta pushed to head is not mergable to a single delta. assertEquals(list(BOB_A, BOB_B), copyList(queue.take())); assertEquals(list(BOB_C), copyList(queue.take())); assertTrue(queue.isEmpty()); }