diff --git a/src/main/java/io/github/hectorvent/floci/core/storage/StorageBackedMap.java b/src/main/java/io/github/hectorvent/floci/core/storage/StorageBackedMap.java index c56d548d26..83b8eb2336 100644 --- a/src/main/java/io/github/hectorvent/floci/core/storage/StorageBackedMap.java +++ b/src/main/java/io/github/hectorvent/floci/core/storage/StorageBackedMap.java @@ -1,14 +1,21 @@ package io.github.hectorvent.floci.core.storage; +import java.util.AbstractCollection; import java.util.AbstractMap; import java.util.AbstractSet; import java.util.ArrayList; +import java.util.Collection; import java.util.Iterator; import java.util.List; import java.util.Map; +import java.util.NoSuchElementException; import java.util.Objects; import java.util.Set; +import java.util.Spliterator; +import java.util.Spliterators; +import java.util.function.Consumer; import java.util.function.Function; +import java.util.function.Supplier; /** * A mutable {@link Map} facade over {@link StorageBackend}. @@ -103,20 +110,36 @@ public class StorageBackedMap return new Iterator<>() { private String currentKey; + // One-element lookahead: a key in the snapshot may be deleted before + // we reach it, so skip keys whose value is gone instead of yielding null. + private String nextKey; + private V nextValue; @Override public boolean hasNext() { - return keys.hasNext(); + while (nextValue == null && keys.hasNext()) { + nextKey = keys.next(); + nextValue = StorageBackedMap.this.get(nextKey); + } + return nextValue != null; } @Override public Entry next() { - currentKey = keys.next(); - String entryKey = currentKey; + if (!hasNext()) { + throw new NoSuchElementException(); + } + currentKey = nextKey; + String entryKey = nextKey; + V entryValue = nextValue; + nextKey = null; + nextValue = null; return new Entry<>() { + private V value = entryValue; + @Override public String getKey() { @@ -126,13 +149,15 @@ public class StorageBackedMap @Override public V getValue() { - return StorageBackedMap.this.get(entryKey); + return value; } @Override - public V setValue(V value) + public V setValue(V newValue) { - return StorageBackedMap.this.put(entryKey, value); + V previous = StorageBackedMap.this.put(entryKey, newValue); + value = newValue; + return previous; } }; } @@ -154,6 +179,134 @@ public class StorageBackedMap { return snapshotKeys().size(); } + + @Override + public Spliterator> spliterator() + { + return unsized(this::iterator); + } + }; + } + + @Override + public Set keySet() + { + return new AbstractSet<>() + { + @Override + public Iterator iterator() + { + Iterator> entries = entrySet().iterator(); + return new Iterator<>() + { + @Override + public boolean hasNext() + { + return entries.hasNext(); + } + + @Override + public String next() + { + return entries.next().getKey(); + } + + @Override + public void remove() + { + entries.remove(); + } + }; + } + + @Override + public int size() + { + return StorageBackedMap.this.size(); + } + + @Override + public boolean contains(Object key) + { + return containsKey(key); + } + + @Override + public Spliterator spliterator() + { + return unsized(this::iterator); + } + }; + } + + @Override + public Collection values() + { + return new AbstractCollection<>() + { + @Override + public Iterator iterator() + { + Iterator> entries = entrySet().iterator(); + return new Iterator<>() + { + @Override + public boolean hasNext() + { + return entries.hasNext(); + } + + @Override + public V next() + { + return entries.next().getValue(); + } + + @Override + public void remove() + { + entries.remove(); + } + }; + } + + @Override + public int size() + { + return StorageBackedMap.this.size(); + } + + @Override + public Spliterator spliterator() + { + return unsized(this::iterator); + } + }; + } + + // size() is a separate key snapshot from the one an iterator walks, and the + // iterator skips keys deleted in between, so the views must not report SIZED: + // a sized stream throws "End size N is less than fixed size M" on a mismatch. + // The iterator is taken on first traversal or split, not here, so the + // spliterator stays late-binding like the Collection.spliterator() default. + private static Spliterator unsized(Supplier> iterators) + { + return new Spliterators.AbstractSpliterator<>(Long.MAX_VALUE, Spliterator.NONNULL) + { + private Iterator iterator; + + @Override + public boolean tryAdvance(Consumer action) + { + if (iterator == null) { + iterator = iterators.get(); + } + if (!iterator.hasNext()) { + return false; + } + action.accept(iterator.next()); + return true; + } }; } diff --git a/src/test/java/io/github/hectorvent/floci/core/storage/StorageBackedMapTest.java b/src/test/java/io/github/hectorvent/floci/core/storage/StorageBackedMapTest.java index 459625870f..fe490a423f 100644 --- a/src/test/java/io/github/hectorvent/floci/core/storage/StorageBackedMapTest.java +++ b/src/test/java/io/github/hectorvent/floci/core/storage/StorageBackedMapTest.java @@ -2,8 +2,12 @@ package io.github.hectorvent.floci.core.storage; import org.junit.jupiter.api.Test; +import java.util.ArrayList; import java.util.Iterator; +import java.util.List; import java.util.Map; +import java.util.NoSuchElementException; +import java.util.Set; import java.util.concurrent.CountDownLatch; import java.util.concurrent.ExecutionException; import java.util.concurrent.ExecutorService; @@ -12,9 +16,12 @@ import java.util.concurrent.Future; import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeoutException; import java.util.concurrent.atomic.AtomicInteger; +import java.util.stream.Collectors; +import java.util.stream.Stream; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertNotNull; import static org.junit.jupiter.api.Assertions.assertNull; import static org.junit.jupiter.api.Assertions.assertThrows; import static org.junit.jupiter.api.Assertions.assertTrue; @@ -152,6 +159,155 @@ class StorageBackedMapTest assertTrue(map.isEmpty()); } + @Test + void valuesSkipsKeyDeletedAfterIterationStarted() + { + StorageBackedMap map = mapOf("alpha", "beta", "gamma"); + + List seen = new ArrayList<>(); + Iterator iterator = map.values().iterator(); + while (iterator.hasNext()) { + String value = iterator.next(); + if (seen.isEmpty()) { + map.remove(otherKey(value)); + } + seen.add(value); + } + + assertEquals(2, seen.size()); + assertFalse(seen.contains(null)); + } + + @Test + void entrySetSkipsKeyDeletedAfterIterationStarted() + { + StorageBackedMap map = mapOf("alpha", "beta", "gamma"); + + List> seen = new ArrayList<>(); + for (Map.Entry entry : map.entrySet()) { + if (seen.isEmpty()) { + map.remove(otherKey(entry.getKey())); + } + seen.add(entry); + } + + assertEquals(2, seen.size()); + for (Map.Entry entry : seen) { + assertNotNull(entry.getValue()); + assertEquals(entry.getKey() + "-value", entry.getValue()); + } + } + + @Test + void valuesStreamSurvivesKeyDeletedDuringStream() + { + StorageBackedMap map = mapOf("alpha", "beta", "gamma"); + AtomicInteger calls = new AtomicInteger(); + + List values = map.values().stream() + .map(value -> { + if (calls.getAndIncrement() == 0) { + map.remove(otherKey(value)); + } + return value; + }) + .toList(); + + assertEquals(2, values.size()); + assertFalse(values.contains(null)); + } + + @Test + void keySetStreamSurvivesKeyDeletedDuringStream() + { + StorageBackedMap map = mapOf("alpha", "beta", "gamma"); + AtomicInteger calls = new AtomicInteger(); + + List keys = map.keySet().stream() + .map(key -> { + if (calls.getAndIncrement() == 0) { + map.remove(otherKey(key)); + } + return key; + }) + .toList(); + + assertEquals(2, keys.size()); + assertFalse(keys.contains(null)); + } + + @Test + void viewStreamsSeeKeyAddedBeforeTerminalOperation() + { + StorageBackedMap map = mapOf("alpha"); + + Stream keys = map.keySet().stream(); + Stream values = map.values().stream(); + Stream> entries = map.entrySet().stream(); + map.put("beta", "beta-value"); + + assertEquals(Set.of("alpha", "beta"), keys.collect(Collectors.toSet())); + assertEquals(Set.of("alpha-value", "beta-value"), values.collect(Collectors.toSet())); + assertEquals(Set.of("alpha", "beta"), + entries.map(Map.Entry::getKey).collect(Collectors.toSet())); + } + + @Test + void iteratorRemoveAfterLookaheadRemovesTheYieldedKey() + { + StorageBackedMap map = mapOf("alpha", "beta", "gamma"); + + Iterator> iterator = map.entrySet().iterator(); + String removed = iterator.next().getKey(); + // hasNext() reads one element ahead; remove() must still hit the yielded key. + assertTrue(iterator.hasNext()); + iterator.remove(); + + assertFalse(map.containsKey(removed)); + assertEquals(2, map.size()); + } + + @Test + void iteratorThrowsNoSuchElementAtExhaustion() + { + StorageBackedMap map = mapOf("alpha"); + + Iterator iterator = map.values().iterator(); + iterator.next(); + + assertFalse(iterator.hasNext()); + assertThrows(NoSuchElementException.class, iterator::next); + } + + @Test + void entrySetValueWritesThrough() + { + StorageBackedMap map = mapOf("alpha"); + + Map.Entry entry = map.entrySet().iterator().next(); + assertEquals("alpha-value", entry.setValue("updated")); + + assertEquals("updated", entry.getValue()); + assertEquals("updated", map.get("alpha")); + } + + private static StorageBackedMap mapOf(String... keys) + { + StorageBackedMap map = new StorageBackedMap<>(new InMemoryStorage<>()); + for (String key : keys) { + map.put(key, key + "-value"); + } + return map; + } + + // Some key other than the one just yielded, so the deletion lands on a key the + // iterator has snapshotted but not reached, whatever order storage returns keys in. + private static String otherKey(String yielded) + { + String key = yielded.replace("-value", ""); + return key.equals("gamma") ? "alpha" : "gamma"; + } + private static void await(CountDownLatch latch) { try {