/** * Removes all entries from the cache and the SortedIndex, regardless of their state. * * @throws IllegalStateException If the StreamSegmentReadIndex is not closed. */ private void removeAllEntries() { // A bit unusual, but we want to make sure we do not call this while the index is active. Preconditions.checkState(this.closed, "Cannot call removeAllEntries unless the ReadIndex is closed."); int count; synchronized (this.lock) { this.indexEntries.forEach(entry -> { if (entry.isDataEntry()) { CacheKey key = getCacheKey(entry); this.cache.remove(key); } }); count = this.indexEntries.size(); this.indexEntries.clear(); } log.info("{}: Cleared all cache entries ({}).", this.traceObjectId, count); }
/** * 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 the clear() method. */ @Test public void testClear() { val index = createIndex(); val keys = populate(index); Assert.assertNotNull("Unexpected return value for getFirst() on non-empty index.", index.getFirst()); Assert.assertNotNull("Unexpected return value for getLast() on non-empty index.", index.getLast()); index.clear(); Assert.assertEquals("Unexpected size of empty index.", 0, index.size()); Assert.assertNull("Unexpected return value for getFirst() on empty index.", index.getFirst()); Assert.assertNull("Unexpected return value for getLast() on empty index.", index.getLast()); for (long key : keys) { Assert.assertNull("Unexpected value for get() on empty index.", index.get(key)); Assert.assertNull("Unexpected value for getCeiling() on empty index.", index.getCeiling(key)); } }