@Override public boolean add(Operation item) { if (this.addCount.incrementAndGet() == this.corruptAtIndex) { // Still add the item, but report that we haven't added it. return false; } return super.add(item); } }
public void add(Entry entry, String clientId) throws DataLogWriterNotPrimaryException { ensureLock(clientId); ensureEnabled(); this.entries.add(entry); }
boolean added = this.inMemoryOperationLog.add(operation); if (!added) {
/** * Tests the functionality of the addIf() method. */ @Test public void testAddIf() { SequencedItemList<Item> list = new SequencedItemList<>(); for (int i = 0; i < ITEM_COUNT; i++) { final Item currentValue = new Item(i); // Happy case. boolean resultValue = list.add(currentValue); Assert.assertTrue("Unexpected return value from addIf for successful append.", resultValue); // Unhappy case resultValue = list.add(currentValue); Assert.assertFalse("Unexpected return value from addIf for unsuccessful append.", resultValue); } Iterator<Item> readResult = list.read(START, ITEM_COUNT * 2); checkRange("AddIf", 0, ITEM_COUNT - 1, readResult); }
/** * Tests the functionality of the clear() method. */ @Test public void testClear() { SequencedItemList<Item> list = new SequencedItemList<>(); for (int i = 0; i < ITEM_COUNT; i++) { list.add(new Item(i)); } list.clear(); Iterator<Item> readResult = list.read(START, ITEM_COUNT * 2); Assert.assertFalse("List should be empty.", readResult.hasNext()); }
public long add(Operation operation) { Exceptions.checkNotClosed(this.closed.get(), this); Preconditions.checkArgument(operation.getSequenceNumber() < 0, "Given operation already has a sequence number."); // If not a checkpoint op, see if we need to auto-add one. boolean isCheckpoint = operation instanceof MetadataCheckpointOperation; if (!isCheckpoint) { if (this.config.autoInsertCheckpointFrequency != DataSourceConfig.NO_METADATA_CHECKPOINT && this.metadata.getOperationSequenceNumber() - this.lastAddedCheckpoint.get() >= this.config.autoInsertCheckpointFrequency) { MetadataCheckpointOperation checkpointOperation = new MetadataCheckpointOperation(); this.lastAddedCheckpoint.set(add(checkpointOperation)); } } // Set the Sequence Number, after the possible recursive call to add a checkpoint (to maintain Seq No order). operation.setSequenceNumber(this.metadata.nextOperationSequenceNumber()); // We need to record the Truncation Marker/Point prior to actually adding the operation to the log (because it could // get picked up very fast by the Writer, so we need to have everything in place). if (isCheckpoint) { this.metadata.recordTruncationMarker(operation.getSequenceNumber(), new TestLogAddress(operation.getSequenceNumber())); this.metadata.setValidTruncationPoint(operation.getSequenceNumber()); } if (!this.log.add(operation)) { throw new IllegalStateException("Sequence numbers out of order."); } notifyAddProcessed(); return operation.getSequenceNumber(); }
SequencedItemList<Item> list = new SequencedItemList<>(); for (int i = 0; i < ITEM_COUNT; i++) { list.add(new Item(i)); Item firstValue = addIterator.next(); Assert.assertEquals("Unexpected first value, pre-modification.", 0, firstValue.getSequenceNumber()); list.add(new Item(ITEM_COUNT)); checkRange("Post-addition.", 1, ITEM_COUNT, addIterator);
/** * Tests the functionality of the truncate() method. */ @Test public void testTruncate() { SequencedItemList<Item> list = new SequencedItemList<>(); for (int i = 0; i < ITEM_COUNT; i++) { list.add(new Item(i)); } // Truncate 25% of items. list.truncate(ITEM_COUNT / 4 - 1); checkRange("Truncate 25%", ITEM_COUNT / 4, ITEM_COUNT - 1, list.read(START, ITEM_COUNT)); // Truncate the same 25% of items - verify no change. list.truncate(ITEM_COUNT / 4 - 1); checkRange("Re-truncate 25%", ITEM_COUNT / 4, ITEM_COUNT - 1, list.read(START, ITEM_COUNT)); // Truncate all items. list.truncate(END); Iterator<Item> readResult = list.read(START, ITEM_COUNT * 2); Assert.assertFalse("List should be empty.", readResult.hasNext()); }
/** * Tests the combination of the basic append() method and read(). */ @Test public void testAddRead() { SequencedItemList<Item> list = new SequencedItemList<>(); for (int i = 0; i < ITEM_COUNT; i++) { list.add(new Item(i)); } //Read 1/2 items Iterator<Item> readResult = list.read(START, ITEM_COUNT / 2); checkRange("Read first 50%", 0, ITEM_COUNT / 2 - 1, readResult); // Read all items readResult = list.read(START, ITEM_COUNT); checkRange("Read all items", 0, ITEM_COUNT - 1, readResult); // Try to read more items. readResult = list.read(START, ITEM_COUNT * 2); checkRange("Read more items than list has", 0, ITEM_COUNT - 1, readResult); // Read 25% of items, starting at the middle point. readResult = list.read(ITEM_COUNT / 2 - 1, ITEM_COUNT / 4); checkRange("Read 25% starting at 50%", ITEM_COUNT / 2, ITEM_COUNT / 2 + ITEM_COUNT / 4 - 1, readResult); }