Skip to content

Commit

Permalink
always sort tiles
Browse files Browse the repository at this point in the history
  • Loading branch information
msbarry committed Sep 21, 2023
1 parent 09aa6e6 commit 045a3c9
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.LinkedHashMap;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;
Expand All @@ -41,8 +42,8 @@ public class TileWeights {
.withLineSeparator("\n");
private static final ObjectWriter WRITER = MAPPER.writer(SCHEMA);
private static final ObjectReader READER = MAPPER.readerFor(Row.class).with(SCHEMA);
private final Map<Integer, Long> byZoom = new LinkedHashMap<>();
private final Map<TileCoord, Long> weights = new LinkedHashMap<>();
private final Map<Integer, Long> byZoom = new HashMap<>();
private final Map<TileCoord, Long> weights = new HashMap<>();

public long getWeight(TileCoord coord) {
return weights.getOrDefault(coord, 0L);
Expand All @@ -69,7 +70,11 @@ public void writeToFile(Path path) throws IOException {
new BufferedOutputStream(Files.newOutputStream(path, CREATE, TRUNCATE_EXISTING, WRITE)));
var writer = WRITER.writeValues(output)
) {
for (var entry : weights.entrySet()) {
var sorted = weights.entrySet().stream()
.sorted(Comparator.comparingInt(e -> e.getKey().encoded()))
.iterator();
while (sorted.hasNext()) {
var entry = sorted.next();
TileCoord coord = entry.getKey();
writer.write(new Row(coord.z(), coord.x(), coord.y(), entry.getValue()));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,6 @@ TileWeights run(int threads, int topN, int maxZoom, List<LocalDate> toDownload)
counts.entrySet().stream()
.sorted(Comparator.comparingLong(e -> -e.getValue()))
.limit(topN)
.sorted(Comparator.comparingInt(Map.Entry::getKey))
.forEach(entry -> tileWeights.put(TileCoord.decode(entry.getKey()), entry.getValue()));
result.complete(tileWeights);
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,28 +52,13 @@ void testWriteToFile(@TempDir Path path) throws IOException {
var read = TileWeights.readFromFile(file);
assertEquals("""
z x y loads
1 0 0 2
0 0 0 1
1 0 0 2
""", new String(new GZIPInputStream(Files.newInputStream(file)).readAllBytes()));
assertEquals(1, read.getWeight(TileCoord.ofXYZ(0, 0, 0)));
assertEquals(2, read.getWeight(TileCoord.ofXYZ(0, 0, 1)));
}

@Test
void testWriteToFileDifferentOrder(@TempDir Path path) throws IOException {
Path file = path.resolve("test.tsv.gz");
new TileWeights()
.put(TileCoord.ofXYZ(0, 0, 0), 1)
.put(TileCoord.ofXYZ(0, 0, 1), 1)
.put(TileCoord.ofXYZ(0, 0, 1), 1)
.writeToFile(file);
assertEquals("""
z x y loads
0 0 0 1
1 0 0 2
""", new String(new GZIPInputStream(Files.newInputStream(file)).readAllBytes()));
}

@Test
void testReadCorruptFile(@TempDir Path path) throws IOException {
Path file = path.resolve("test.tsv.gz");
Expand Down

0 comments on commit 045a3c9

Please sign in to comment.