From 6426f1002af9d23477c3aff7e3446458431d0c6b Mon Sep 17 00:00:00 2001 From: bbilger Date: Sat, 6 Jan 2024 22:16:59 +0100 Subject: [PATCH] properly close the getAllTiles iterator --- .../planetiler/stream/ReadableStreamArchive.java | 8 +++++--- .../test/java/com/onthegomap/planetiler/TestUtils.java | 4 +++- .../planetiler/files/ReadableFilesArchiveTest.java | 8 ++++++-- .../planetiler/stream/ReadableCsvStreamArchiveTest.java | 8 ++++++-- .../planetiler/stream/ReadableJsonStreamArchiveTest.java | 8 ++++++-- .../stream/ReadableProtoStreamArchiveTest.java | 9 +++++++-- 6 files changed, 33 insertions(+), 12 deletions(-) diff --git a/planetiler-core/src/main/java/com/onthegomap/planetiler/stream/ReadableStreamArchive.java b/planetiler-core/src/main/java/com/onthegomap/planetiler/stream/ReadableStreamArchive.java index f6f9ecff56..da7aa195f4 100644 --- a/planetiler-core/src/main/java/com/onthegomap/planetiler/stream/ReadableStreamArchive.java +++ b/planetiler-core/src/main/java/com/onthegomap/planetiler/stream/ReadableStreamArchive.java @@ -26,7 +26,9 @@ abstract class ReadableStreamArchive implements ReadableTileArchive { @Override public final byte[] getTile(TileCoord coord) { - return getAllTiles().stream().filter(c -> c.coord().equals(coord)).map(Tile::bytes).findFirst().orElse(null); + try (var tiles = getAllTiles(); var s = tiles.stream()) { + return s.filter(c -> c.coord().equals(coord)).map(Tile::bytes).findFirst().orElse(null); + } } @Override @@ -53,8 +55,8 @@ public final TileArchiveMetadata metadata() { } private TileArchiveMetadata loadMetadata() { - try (var it = createIterator()) { - return it.stream().map(this::mapEntryToMetadata).flatMap(Optional::stream).findFirst().orElse(null); + try (var i = createIterator(); var s = i.stream()) { + return s.map(this::mapEntryToMetadata).flatMap(Optional::stream).findFirst().orElse(null); } } diff --git a/planetiler-core/src/test/java/com/onthegomap/planetiler/TestUtils.java b/planetiler-core/src/test/java/com/onthegomap/planetiler/TestUtils.java index 675f80d6f8..001b69eddf 100644 --- a/planetiler-core/src/test/java/com/onthegomap/planetiler/TestUtils.java +++ b/planetiler-core/src/test/java/com/onthegomap/planetiler/TestUtils.java @@ -294,7 +294,9 @@ public static Geometry decodeSilently(VectorTile.VectorGeometry geom) { } public static Set getTiles(ReadableTileArchive db) { - return db.getAllTiles().stream().collect(Collectors.toSet()); + try (var t = db.getAllTiles(); var s = t.stream()) { + return s.collect(Collectors.toUnmodifiableSet()); + } } public static int getTilesDataCount(Mbtiles db) throws SQLException { diff --git a/planetiler-core/src/test/java/com/onthegomap/planetiler/files/ReadableFilesArchiveTest.java b/planetiler-core/src/test/java/com/onthegomap/planetiler/files/ReadableFilesArchiveTest.java index ad9d003854..127030bfa3 100644 --- a/planetiler-core/src/test/java/com/onthegomap/planetiler/files/ReadableFilesArchiveTest.java +++ b/planetiler-core/src/test/java/com/onthegomap/planetiler/files/ReadableFilesArchiveTest.java @@ -49,8 +49,12 @@ void testRead(@TempDir Path tempDir) throws IOException { Files.write(files.get(i), new byte[]{(byte) i}); } - try (var reader = ReadableFilesArchive.newReader(tilesDir, Arguments.of())) { - final List tiles = reader.getAllTiles().stream().sorted().toList(); + try ( + var reader = ReadableFilesArchive.newReader(tilesDir, Arguments.of()); + var t = reader.getAllTiles(); + var s = t.stream() + ) { + final List tiles = s.sorted().toList(); assertEquals( List.of( new Tile(TileCoord.ofXYZ(0, 0, 0), new byte[]{0}), diff --git a/planetiler-core/src/test/java/com/onthegomap/planetiler/stream/ReadableCsvStreamArchiveTest.java b/planetiler-core/src/test/java/com/onthegomap/planetiler/stream/ReadableCsvStreamArchiveTest.java index 31258995a7..98643b9d56 100644 --- a/planetiler-core/src/test/java/com/onthegomap/planetiler/stream/ReadableCsvStreamArchiveTest.java +++ b/planetiler-core/src/test/java/com/onthegomap/planetiler/stream/ReadableCsvStreamArchiveTest.java @@ -63,8 +63,12 @@ void testSimple(String columnSeparator, String lineSeparator, boolean pad, CsvBi ); try (var reader = ReadableCsvArchive.newReader(TileArchiveConfig.Format.CSV, csvFile, config)) { - assertEquals(expectedTiles, reader.getAllTiles().stream().toList()); - assertEquals(expectedTiles, reader.getAllTiles().stream().toList()); + try (var s = reader.getAllTiles().stream()) { + assertEquals(expectedTiles, s.toList()); + } + try (var s = reader.getAllTiles().stream()) { + assertEquals(expectedTiles, reader.getAllTiles().stream().toList()); + } assertNull(reader.metadata()); assertNull(reader.metadata()); assertArrayEquals(expectedTiles.get(1).bytes(), reader.getTile(TileCoord.ofXYZ(1, 2, 3))); diff --git a/planetiler-core/src/test/java/com/onthegomap/planetiler/stream/ReadableJsonStreamArchiveTest.java b/planetiler-core/src/test/java/com/onthegomap/planetiler/stream/ReadableJsonStreamArchiveTest.java index ab728f8ec4..2bcf1b9796 100644 --- a/planetiler-core/src/test/java/com/onthegomap/planetiler/stream/ReadableJsonStreamArchiveTest.java +++ b/planetiler-core/src/test/java/com/onthegomap/planetiler/stream/ReadableJsonStreamArchiveTest.java @@ -36,8 +36,12 @@ void testSimple(@TempDir Path tempDir) throws IOException { new Tile(TileCoord.ofXYZ(1, 2, 3), new byte[]{1}) ); try (var reader = ReadableJsonStreamArchive.newReader(jsonFile, config)) { - assertEquals(expectedTiles, reader.getAllTiles().stream().toList()); - assertEquals(expectedTiles, reader.getAllTiles().stream().toList()); + try (var s = reader.getAllTiles().stream()) { + assertEquals(expectedTiles, s.toList()); + } + try (var s = reader.getAllTiles().stream()) { + assertEquals(expectedTiles, reader.getAllTiles().stream().toList()); + } assertEquals(TestUtils.MAX_METADATA_DESERIALIZED, reader.metadata()); assertEquals(TestUtils.MAX_METADATA_DESERIALIZED, reader.metadata()); assertArrayEquals(expectedTiles.get(1).bytes(), reader.getTile(TileCoord.ofXYZ(1, 2, 3))); diff --git a/planetiler-core/src/test/java/com/onthegomap/planetiler/stream/ReadableProtoStreamArchiveTest.java b/planetiler-core/src/test/java/com/onthegomap/planetiler/stream/ReadableProtoStreamArchiveTest.java index 7681f98eef..29733c452b 100644 --- a/planetiler-core/src/test/java/com/onthegomap/planetiler/stream/ReadableProtoStreamArchiveTest.java +++ b/planetiler-core/src/test/java/com/onthegomap/planetiler/stream/ReadableProtoStreamArchiveTest.java @@ -52,8 +52,13 @@ void testSimple(boolean maxMetaData, @TempDir Path tempDir) throws IOException { new Tile(TileCoord.ofXYZ(1, 2, 3), new byte[]{1}) ); try (var reader = ReadableProtoStreamArchive.newReader(p, new StreamArchiveConfig(false, Arguments.of()))) { - assertEquals(expectedTiles, reader.getAllTiles().stream().toList()); - assertEquals(expectedTiles, reader.getAllTiles().stream().toList()); + try (var s = reader.getAllTiles().stream()) { + assertEquals(expectedTiles, s.toList()); + } + try (var s = reader.getAllTiles().stream()) { + assertEquals(expectedTiles, reader.getAllTiles().stream().toList()); + } + assertEquals(metadataDeserialized, reader.metadata()); assertEquals(metadataDeserialized, reader.metadata()); } }