this.indexEntries.remove(e.key()); this.cache.remove(getCacheKey(e)); });
this.indexEntries.remove(pendingMerge.getMergeOffset()); this.pendingMergers.remove(sourceMetadata.getId()); sourceEntries.forEach(this::addToIndex);
/** * Tests the remove(), size(), get(), getFirst(), getLast() methods. */ @Test public void testRemove() { val index = createIndex(); val keys = populate(index); // Remove the items, in order. keys.sort(KEY_COMPARATOR); val keysToRemove = new LinkedList<Long>(keys); int expectedSize = index.size(); while (keysToRemove.size() > 0) { // Remove either the first or the last key - this helps test getFirst/getLast properly. long key = expectedSize % 2 == 0 ? keysToRemove.removeLast() : keysToRemove.removeFirst(); val entry = index.get(key); val removedEntry = index.remove(key); expectedSize--; Assert.assertEquals("Unexpected removed entry for key " + key, entry, removedEntry); Assert.assertEquals("Unexpected size after removing key " + key, expectedSize, index.size()); Assert.assertNull("Entry was not removed for key " + key, index.get(key)); if (expectedSize == 0) { Assert.assertNull("Unexpected value from getFirst() when index is empty.", index.getFirst()); Assert.assertNull("Unexpected value from getLast() when index is empty.", index.getLast()); } else { Assert.assertEquals("Unexpected value from getFirst() after removing key " + key, (long) keysToRemove.getFirst(), index.getFirst().key()); Assert.assertEquals("Unexpected value from getLast() after removing key " + key, (long) keysToRemove.getLast(), index.getLast().key()); } } }
/** * Tests the forEach() method. */ @Test public void testForEach() { // Create an index and populate it. val index = createIndex(); val validKeys = populate(index); // Extract the keys using forEach - they should be ordered naturally. val actualKeys = new ArrayList<Long>(); index.forEach(e -> actualKeys.add(e.key())); // Order the inserted keys using the same comparator we used for the index. validKeys.sort(KEY_COMPARATOR); // Verify that modifying the index while looping through it does throw an exception. AssertExtensions.assertThrows( "forEach did not throw when a new item was added during enumeration.", () -> index.forEach(e -> index.put(new TestEntry(index.size()))), ex -> ex instanceof ConcurrentModificationException); AssertExtensions.assertThrows( "forEach did not throw when an item was removed during enumeration.", () -> index.forEach(e -> index.remove(e.key())), ex -> ex instanceof ConcurrentModificationException); AssertExtensions.assertThrows( "forEach did not throw when the index was cleared during enumeration.", () -> index.forEach(e -> index.clear()), ex -> ex instanceof ConcurrentModificationException); }
/** * Tests various operations on already sorted input. */ @Test public void testSortedInput() { val index = createIndex(); for (int key = 0; key < ITEM_COUNT; key++) { index.put(new TestEntry(key)); } //Get + GetCeiling. for (int key = 0; key < ITEM_COUNT; key++) { Assert.assertEquals("Unexpected value from get() for key " + key, key, (long) index.get(key).key()); Assert.assertEquals("Unexpected value from getCeiling() for key " + key, key, (long) index.getCeiling(key).key()); } // Remove + get. for (long key = 0; key < ITEM_COUNT; key++) { long removedKey = index.remove(key).key(); Assert.assertEquals("Unexpected value from remove(). ", key, removedKey); Assert.assertNull("Unexpected value from get() for removed key " + key, index.get(key)); if (key == ITEM_COUNT - 1) { Assert.assertNull("Unexpected value from getCeiling() for removed key " + key, index.getCeiling(key)); } else { Assert.assertEquals("Unexpected value from getCeiling() for removed key " + key, key + 1, (long) index.getCeiling(key).key()); } } }