public void testOptimisesOpsOnSeparateBlips1() throws OperationException { MergingSequence delta = new MergingSequence(); delta.addAll(Arrays.asList( makeBlipContentOp("a", 1), makeBlipContentOp("a", 2), makeBlipContentOp("b", 3) )); delta.optimise(); assertEquals(2, delta.size()); }
/** * Adds the given operation to the tail of the operation queue. Merges with * the delta at the tail if the creators match, otherwise creates a new tail * delta. */ public void add(WaveletOperation op) { ParticipantId creator = op.getContext().getCreator(); if (queue.isEmpty() || !creator.equals(tailCreator) || (queue.getLast().state != ItemState.NONE)) { queue.addLast(new Item(new MergingSequence(), ItemState.NONE)); tailCreator = creator; } queue.getLast().opSequence.add(op); }
ParticipantId creator = resultDelta.get(0).getContext().getCreator(); boolean needOptimisation = item.state != ItemState.OPTIMISED; Item nextItem = queue.element(); MergingSequence nextDelta = nextItem.opSequence; ParticipantId nextCreator = nextDelta.get(0).getContext().getCreator(); resultDelta.addAll(nextDelta); queue.remove(); needOptimisation = true; resultDelta.optimise();
public void testOptimiseEmptyDoesNothing() { MergingSequence delta = new MergingSequence(); delta.optimise(); assertEquals(0, delta.size()); }
public void optimise() { int startSize = size(); if (startSize == 1) { return; String currentId = null; WaveletOperationContext lastOperationContext = null; this.clear(); if (blipOp instanceof BlipContentOperation) { if (!docOps.isEmpty() && !id.equals(currentId)) { composeDocOps(this, currentId, lastOperationContext, docOps); composeDocOps(this, currentId, lastOperationContext, docOps); add(waveletOp); composeDocOps(this, currentId, lastOperationContext, docOps);
/** * Sets up an expected transform that will accept an input client delta * which has the given op list. It will accept any input server delta and * output its own server delta. */ Expectation(WaveletOperation... inputClientOps) { this.inputClientOps = inputClientOps; this.outputServerDelta = new MergingSequence(); }
/** * Prepends the given delta onto the queue's head. No merging is * allows on this delta. This is because we don't know if the server have actually got * the previously sent delta, we can't change the delta once it's sent. * * @param newHead delta to use for the queue. Must only contain operations from a * single author. May be empty, in which case this call will do * nothing. */ public void insertHead(List<WaveletOperation> newHead) { if (newHead.isEmpty()) { return; } MergingSequence mergingHead = new MergingSequence(newHead); Item item = new Item(mergingHead, ItemState.SENT); ParticipantId creator = mergingHead.get(0).getContext().getCreator(); if (queue.isEmpty()) { queue.add(item); tailCreator = creator; } else { queue.addFirst(item); } }
private static void assertTimestamp(long timestamp, MergingSequence delta) { assertEquals(timestamp, ((WaveletBlipOperation) delta.get(0)).getBlipOp().getContext().getTimestamp()); }
/** * Constructs a delta from another delta's operations. Does not copy any * version or signature information. */ public MergingSequence(Iterable<WaveletOperation> ops) { for (WaveletOperation op : ops) { add(op); } }
/** * Estimates the number of operations in this queue. * * In order to provide a bounded execution time the result is an underestimate * of the true number of queued operations. */ public int estimateSize() { int estimate = 0; // Sum delta size for a fixed number of deltas at the start. int headDeltasToCount = ESTIMATE_DELTAS_TO_COUNT; Iterator<Item> itr = queue.iterator(); while ((headDeltasToCount > 0) && itr.hasNext()) { estimate += itr.next().opSequence.size(); --headDeltasToCount; } if (itr.hasNext()) { // Add size of the last delta as new ops are likely to be pushed into it. estimate += queue.getLast().opSequence.size(); // Add one for each other delta in the queue. if (queue.size() > (ESTIMATE_DELTAS_TO_COUNT + 1)) { estimate += queue.size() - (ESTIMATE_DELTAS_TO_COUNT + 1); } } return estimate; }
public void testOptimiseEmptyDoesNothing() { MergingSequence delta = new MergingSequence(); delta.optimise(); assertEquals(0, delta.size()); }
/** * Sets up an expected transform that will accept an input client delta * which has the given op list. It will accept any input server delta and * output its own server delta. */ Expectation(WaveletOperation... inputClientOps) { this.inputClientOps = inputClientOps; this.outputServerDelta = new MergingSequence(); }
private static void assertTimestamp(long timestamp, MergingSequence delta) { assertEquals(timestamp, ((WaveletBlipOperation) delta.get(0)).getBlipOp().getContext().getTimestamp()); }
public void testOptimisesDeltasWithNonDocOps() throws OperationException { MergingSequence delta = new MergingSequence(); delta.addAll(Arrays.asList( makeBlipContentOp("a", 2), makeBlipContentOp("a", 1), makeOtherBlipOp("a", 3) )); delta.optimise(); assertEquals(2, delta.size()); }
/** * Tests that inserting a new head delta into an empty queue makes the queue * non-empty if and only if the delta has ops in it. */ public void testQueueNonemptyAfterInsertingNonemptyHead() { queue.insertHead(new MergingSequence()); assertQueueIsEmpty(); MergingSequence head = new MergingSequence(); head.add(BOB_A); queue.insertHead(head); assertQueueSize(1); }
/** * Specifies that the output client delta should consist of the given * operations. */ public void transformTo(WaveletOperation... outputClientOps) { assertNull("Output client delta already specified", outputClientDelta); outputClientDelta = new MergingSequence(Arrays.asList(outputClientOps)); } }
public void testOptimisesOpsOnSeparateBlips1() throws OperationException { MergingSequence delta = new MergingSequence(); delta.addAll(Arrays.asList( makeBlipContentOp("a", 1), makeBlipContentOp("a", 2), makeBlipContentOp("b", 3) )); delta.optimise(); assertEquals(2, delta.size()); }
/** * Tests that inserting a new head delta into an empty queue makes the queue * non-empty if and only if the delta has ops in it. */ public void testQueueNonemptyAfterInsertingNonemptyHead() { queue.insertHead(new MergingSequence()); assertQueueIsEmpty(); MergingSequence head = new MergingSequence(); head.add(BOB_A); queue.insertHead(head); assertQueueSize(1); }
/** * Specifies that the output client delta should consist of the given * operations. */ public void transformTo(WaveletOperation... outputClientOps) { assertNull("Output client delta already specified", outputClientDelta); outputClientDelta = new MergingSequence(Arrays.asList(outputClientOps)); } }
public void testOptimisesDeltasWithNonDocOps() throws OperationException { MergingSequence delta = new MergingSequence(); delta.addAll(Arrays.asList( makeBlipContentOp("a", 2), makeBlipContentOp("a", 1), makeOtherBlipOp("a", 3) )); delta.optimise(); assertEquals(2, delta.size()); }