Skip to content

Commit

Permalink
properly close the getAllTiles iterator
Browse files Browse the repository at this point in the history
  • Loading branch information
bbilger committed Jan 6, 2024
1 parent d749f8f commit f73fa1f
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,19 +26,27 @@ abstract class ReadableStreamArchive<E> 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
public final byte[] getTile(int x, int y, int z) {
return getTile(TileCoord.ofXYZ(x, y, z));
}

/**
* Callers MUST make sure to close the iterator/derived stream!
*/
@Override
public final CloseableIterator<TileCoord> getAllTileCoords() {
return getAllTiles().map(Tile::coord);
}

/**
* Callers MUST make sure to close the iterator/derived stream!
*/
@Override
public final CloseableIterator<Tile> getAllTiles() {
return createIterator()
Expand All @@ -53,8 +61,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);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,9 @@ public static Geometry decodeSilently(VectorTile.VectorGeometry geom) {
}

public static Set<Tile> 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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<Tile> tiles = reader.getAllTiles().stream().sorted().toList();
try (
var reader = ReadableFilesArchive.newReader(tilesDir, Arguments.of());
var t = reader.getAllTiles();
var s = t.stream()
) {
final List<Tile> tiles = s.sorted().toList();
assertEquals(
List.of(
new Tile(TileCoord.ofXYZ(0, 0, 0), new byte[]{0}),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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, s.toList());
}
assertNull(reader.metadata());
assertNull(reader.metadata());
assertArrayEquals(expectedTiles.get(1).bytes(), reader.getTile(TileCoord.ofXYZ(1, 2, 3)));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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, s.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)));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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, s.toList());
}
assertEquals(metadataDeserialized, reader.metadata());
assertEquals(metadataDeserialized, reader.metadata());
}
}
Expand Down

0 comments on commit f73fa1f

Please sign in to comment.