Skip to content

Commit

Permalink
Avoid creating station names > 100 bytes long
Browse files Browse the repository at this point in the history
  • Loading branch information
datdenkikniet committed Jan 6, 2024
1 parent 08f6194 commit 06c1635
Showing 1 changed file with 16 additions and 12 deletions.
28 changes: 16 additions & 12 deletions src/main/java/dev/morling/onebrc/WeatherStationFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -91,28 +91,32 @@ public static List<WeatherStation> getWeatherStationsList(long seed) throws Exce
}

var name = nameBuf.toString();
int actualLen = name.getBytes(StandardCharsets.UTF_8).length;
while (actualLen > 100) {
nameBuf.deleteCharAt(nameBuf.length() - 1);
if (Character.isWhitespace(nameBuf.charAt(nameBuf.length() - 1))) {
nameBuf.setCharAt(nameBuf.length() - 1, readNonSpace(nameSource));
int nameByteLen = name.getBytes(StandardCharsets.UTF_8).length;

while (names.contains(name) || nameByteLen > 100) {
// If the character length is over 100
if (nameByteLen > 100) {
nameBuf.deleteCharAt(nameBuf.length() - 1);
if (Character.isWhitespace(nameBuf.charAt(nameBuf.length() - 1))) {
nameBuf.setCharAt(nameBuf.length() - 1, readNonSpace(nameSource));
}
}
// Else: name is not unique
else {
nameBuf.setCharAt(rnd.nextInt(nameBuf.length()), readNonSpace(nameSource));
}
name = nameBuf.toString();
actualLen = name.getBytes(StandardCharsets.UTF_8).length;
}

while (names.contains(name)) {
nameBuf.setCharAt(rnd.nextInt(nameBuf.length()), readNonSpace(nameSource));
name = nameBuf.toString();
nameByteLen = name.getBytes(StandardCharsets.UTF_8).length;
}

if (name.indexOf(';') != -1) {
throw new Exception("Station name contains a semicolon!");
}

names.add(name);
minLen = Integer.min(minLen, actualLen);
maxLen = Integer.max(maxLen, actualLen);
minLen = Integer.min(minLen, nameByteLen);
maxLen = Integer.max(maxLen, nameByteLen);
var lat = Double.parseDouble(row.substring(row.indexOf(';') + 1));
// Guesstimate mean temperature using cosine of latitude
var avgTemp = (double) (30 * Math.cos(Math.toRadians(lat))) - 10;
Expand Down

0 comments on commit 06c1635

Please sign in to comment.