/** * 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); }
/** * Gets a copy of all the ReadIndexEntries in this Index that are not RedirectReadIndices. All returned * entries have their offsets adjusted by the given amount. * * @param offsetAdjustment The amount to adjust the offset by. */ private List<MergedIndexEntry> getAllEntries(long offsetAdjustment) { Exceptions.checkArgument(offsetAdjustment >= 0, "offsetAdjustment", "offsetAdjustment must be a non-negative number."); synchronized (this.lock) { List<MergedIndexEntry> result = new ArrayList<>(this.indexEntries.size()); this.indexEntries.forEach(entry -> { if (entry.isDataEntry()) { result.add(new MergedIndexEntry(entry.getStreamSegmentOffset() + offsetAdjustment, this.metadata.getId(), (CacheIndexEntry) entry)); } }); return result; } }
ArrayList<ReadIndexEntry> toRemove = new ArrayList<>(); synchronized (this.lock) { this.indexEntries.forEach(entry -> {
/** * 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); }