fix(storage): make scan() return mutable list for non-InMemory backends (#517)

PersistentStorage, HybridStorage, and WalStorage used Stream.toList(),
which returns an unmodifiable list in Java 16+. InMemoryStorage built an
ArrayList, so callers that mutate the scan result work in unit tests
(all using in-memory backend) but fail at runtime on persistent backends.

This was the root cause of #482: CloudWatchLogsService.describeLogGroups
calls result.sort(...) on the scan result, which throws
UnsupportedOperationException when running against any non-memory store.
Same latent bug at CloudWatchLogsService.getLogEvents and
CognitoService.listGroups (callers copy into ArrayList first, so they
happen to work).

Rather than patch the two call sites, align all StorageBackend
implementations on a documented "returns a new mutable list" contract.
Tests added to each backend to lock in the behavior.
This commit is contained in:
TiM authored and GitHub committed 2026-04-18 18:36:20 -05:00
1 parent 65063144c9
commit 677bad0e16
8 files changed
+49 -3

No files matched your search

@@ -10,6 +10,7 @@ import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardCopyOption;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
@@ -21,6 +22,7 @@ import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.function.Predicate;
import java.util.stream.Collectors;
/**
* Hybrid storage: in-memory reads with async flush to disk.
@@ -75,7 +77,7 @@ public class HybridStorage<K, V> implements StorageBackend<K, V> {
return store.entrySet().stream()
.filter(e -> keyFilter.test(e.getKey()))
.map(Map.Entry::getValue)
.toList();
.collect(Collectors.toCollection(ArrayList::new));
}
@Override
@@ -10,6 +10,7 @@ import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardCopyOption;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
@@ -17,6 +18,7 @@ import java.util.Optional;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.function.Predicate;
import java.util.stream.Collectors;
/**
* JSON file-backed persistent storage.
@@ -64,7 +66,7 @@ public class PersistentStorage<K, V> implements StorageBackend<K, V> {
return store.entrySet().stream()
.filter(e -> keyFilter.test(e.getKey()))
.map(Map.Entry::getValue)
.toList();
.collect(Collectors.toCollection(ArrayList::new));
}
@Override
@@ -19,6 +19,10 @@ public interface StorageBackend<K, V> {
void delete(K key);
/**
* Return a new mutable list of values whose keys pass the filter. Callers may sort,
* filter, or otherwise mutate the returned list without affecting the underlying store.
*/
List<V> scan(Predicate<K> keyFilter);
/** Return all keys in this store. */
@@ -11,6 +11,7 @@ import java.io.*;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardCopyOption;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
@@ -19,6 +20,7 @@ import java.util.Set;
import java.util.concurrent.*;
import java.util.concurrent.locks.ReentrantReadWriteLock;
import java.util.function.Predicate;
import java.util.stream.Collectors;
/**
* Write-Ahead Log storage: in-memory reads with append-only binary WAL for durability.
@@ -105,7 +107,7 @@ public class WalStorage<K, V> implements StorageBackend<K, V> {
return store.entrySet().stream()
.filter(e -> keyFilter.test(e.getKey()))
.map(Map.Entry::getValue)
.toList();
.collect(Collectors.toCollection(ArrayList::new));
}
@Override