Skip to content

Commit

Permalink
Guard against IO resource leak with streams
Browse files Browse the repository at this point in the history
  • Loading branch information
findepi committed Jan 21, 2022
1 parent 0747a37 commit 43c3824
Show file tree
Hide file tree
Showing 7 changed files with 50 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import java.io.UncheckedIOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.nio.file.DirectoryStream;
import java.nio.file.Path;
import java.util.List;
import java.util.concurrent.Callable;
Expand Down Expand Up @@ -70,10 +71,12 @@ private static List<URL> buildClassPath(File path)
private static List<File> listFiles(File path)
{
try {
return stream(newDirectoryStream(path.toPath()))
.map(Path::toFile)
.sorted()
.collect(toImmutableList());
try (DirectoryStream<Path> directoryStream = newDirectoryStream(path.toPath())) {
return stream(directoryStream)
.map(Path::toFile)
.sorted()
.collect(toImmutableList());
}
}
catch (IOException e) {
throw new UncheckedIOException(e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import static com.google.common.io.Resources.getResource;
import static io.trino.jmh.Benchmarks.benchmark;
Expand Down Expand Up @@ -70,10 +71,13 @@ public void setUp()
queryRunner.createCatalog("memory", new MemoryConnectorFactory(), ImmutableMap.of());

Path path = new File(getResource("us-states.tsv").toURI()).toPath();
String polygonValues = Files.lines(path)
.map(line -> line.split("\t"))
.map(parts -> format("('%s', '%s')", parts[0], parts[1]))
.collect(Collectors.joining(","));
String polygonValues;
try (Stream<String> lines = Files.lines(path)) {
polygonValues = lines
.map(line -> line.split("\t"))
.map(parts -> format("('%s', '%s')", parts[0], parts[1]))
.collect(Collectors.joining(","));
}

queryRunner.execute(
format("CREATE TABLE memory.default.us_states AS SELECT ST_GeometryFromText(t.wkt) AS geom FROM (VALUES %s) as t (name, wkt)",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
import java.nio.file.Path;
import java.util.Optional;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import static com.google.common.io.Resources.getResource;
import static io.trino.jmh.Benchmarks.benchmark;
Expand Down Expand Up @@ -82,10 +83,13 @@ public void setUp()
queryRunner.createCatalog("memory", new MemoryConnectorFactory(), ImmutableMap.of());

Path path = new File(getResource("us-states.tsv").toURI()).toPath();
String polygonValues = Files.lines(path)
.map(line -> line.split("\t"))
.map(parts -> format("('%s', '%s')", parts[0], parts[1]))
.collect(Collectors.joining(","));
String polygonValues;
try (Stream<String> lines = Files.lines(path)) {
polygonValues = lines
.map(line -> line.split("\t"))
.map(parts -> format("('%s', '%s')", parts[0], parts[1]))
.collect(Collectors.joining(","));
}
queryRunner.execute(format("CREATE TABLE memory.default.polygons AS SELECT * FROM (VALUES %s) as t (name, wkt)", polygonValues));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;
import java.util.stream.Stream;

import static com.google.common.io.Resources.getResource;
import static io.trino.operator.scalar.ApplyFunction.APPLY_FUNCTION;
Expand Down Expand Up @@ -455,7 +456,10 @@ public void testGeometryToBingTiles()

// Input polygon too complex
String filePath = new File(getResource("too_large_polygon.txt").toURI()).getPath();
String largeWkt = Files.lines(Paths.get(filePath)).findFirst().get();
String largeWkt;
try (Stream<String> lines = Files.lines(Paths.get(filePath))) {
largeWkt = lines.findFirst().get();
}
assertInvalidFunction("geometry_to_bing_tiles(ST_GeometryFromText('" + largeWkt + "'), 16)", "The zoom level is too high or the geometry is too complex to compute a set of covering Bing tiles. Please use a lower zoom level or convert the geometry to its bounding box using the ST_Envelope function.");
assertFunction("cardinality(geometry_to_bing_tiles(ST_Envelope(ST_GeometryFromText('" + largeWkt + "')), 16))", BIGINT, 19939L);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import static com.google.common.io.Resources.getResource;
import static io.airlift.slice.Slices.utf8Slice;
Expand Down Expand Up @@ -178,15 +179,21 @@ public void testArea()
assertArea("POLYGON((90 0, 0 0, 0 90), (89 1, 1 1, 1 89))", 348.04E10);

Path geometryPath = new File(getResource("us-states.tsv").toURI()).toPath();
Map<String, String> stateGeometries = Files.lines(geometryPath)
.map(line -> line.split("\t"))
.collect(Collectors.toMap(parts -> parts[0], parts -> parts[1]));
Map<String, String> stateGeometries;
try (Stream<String> lines = Files.lines(geometryPath)) {
stateGeometries = lines
.map(line -> line.split("\t"))
.collect(Collectors.toMap(parts -> parts[0], parts -> parts[1]));
}

Path areaPath = new File(getResource("us-state-areas.tsv").toURI()).toPath();
Map<String, Double> stateAreas = Files.lines(areaPath)
.map(line -> line.split("\t"))
.filter(parts -> parts.length >= 2)
.collect(Collectors.toMap(parts -> parts[0], parts -> Double.valueOf(parts[1])));
Map<String, Double> stateAreas;
try (Stream<String> lines = Files.lines(areaPath)) {
stateAreas = lines
.map(line -> line.split("\t"))
.filter(parts -> parts.length >= 2)
.collect(Collectors.toMap(parts -> parts[0], parts -> Double.valueOf(parts[1])));
}

for (String state : stateGeometries.keySet()) {
assertArea(stateGeometries.get(state), stateAreas.get(state));
Expand Down
1 change: 1 addition & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -1848,6 +1848,7 @@
-Xep:MutablePublicArray:ERROR \
-Xep:NullOptional:ERROR \
-Xep:ObjectToString:ERROR \
-Xep:StreamResourceLeak:ERROR \
-Xep:UnnecessaryMethodReference:ERROR \
-Xep:UnnecessaryOptionalGet:ERROR \
-Xep:UnusedVariable:ERROR \
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
import java.util.Map;
import java.util.Optional;
import java.util.concurrent.Callable;
import java.util.stream.Stream;

import static io.trino.tests.product.launcher.cli.Commands.runCommand;
import static io.trino.tests.product.launcher.docker.DockerFiles.ROOT_PATH;
Expand Down Expand Up @@ -201,10 +202,12 @@ private String simplifyPath(String path)
private static long directorySize(Path directory)
{
try {
return Files.walk(directory)
.filter(path -> path.toFile().isFile())
.mapToLong(path -> path.toFile().length())
.sum();
try (Stream<Path> stream = Files.walk(directory)) {
return stream
.filter(path -> path.toFile().isFile())
.mapToLong(path -> path.toFile().length())
.sum();
}
}
catch (IOException e) {
log.warn(e, "Could not calculate directory size: %s", directory);
Expand Down

0 comments on commit 43c3824

Please sign in to comment.