Skip to content

Commit

Permalink
Hanle hash collisions
Browse files Browse the repository at this point in the history
  • Loading branch information
abhinav-upadhyay committed Jan 31, 2024
1 parent 10c68a1 commit 833ff01
Showing 1 changed file with 16 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,6 @@ private static class Table {

public void put(long cityStartOffset, long cityLength, long nameHash, int temperature) {
final long uhash = nameHash ^ (nameHash >> 29);
// final long uhash = nameHash;
int index = (int) uhash & TABLE_MASK;
Row row = table[index];
if (row == null) {
Expand All @@ -72,7 +71,21 @@ public void put(long cityStartOffset, long cityLength, long nameHash, int temper
for (; i < cityLength; i++) {
array[i] = UNSAFE.getByte(cityStartOffset++);
}
table[index] = new Row(new String(array, 0, i), temperature, temperature, 1, temperature, (int) uhash);
table[index] = new Row(new String(array, 0, i), temperature, temperature, 1, temperature, uhash);
return;
}

while (row.hash != uhash) {
index = (int) ((index + (uhash)) & TABLE_MASK);
int i = 0;
for (; i < cityLength - 1;) {
array[i++] = UNSAFE.getByte(cityStartOffset++);
array[i++] = UNSAFE.getByte(cityStartOffset++);
}
for (; i < cityLength; i++) {
array[i] = UNSAFE.getByte(cityStartOffset++);
}
table[index] = new Row(new String(array, 0, i), temperature, temperature, 1, temperature, uhash);
return;
}

Expand Down Expand Up @@ -214,7 +227,7 @@ public static void main(String[] args) throws IOException {
final long fileSize = fc.size();
final long startAddress = fc.map(FileChannel.MapMode.READ_ONLY, 0, fileSize, Arena.global()).address();
final long endAddress = startAddress + fileSize;
final long[][] segments = findSegments(startAddress, endAddress, fileSize, fileSize > 1024 * 1024 * 1024 ? 8 : 1);
final long[][] segments = findSegments(startAddress, endAddress, fileSize, fileSize > 1024 * 1024 * 1024 ? 12 : 1);
final List<Table> collect = Arrays.stream(segments).parallel().map(s -> readFile(s[0], s[1])).toList();
Map<String, Row> finalMap = new TreeMap<>();
for (final Table t : collect) {
Expand Down

0 comments on commit 833ff01

Please sign in to comment.