Skip to content

Commit 35e22af

Browse files
committed
Use BufferedMurmur3Hasher in TsidBuilder
1 parent 8b46142 commit 35e22af

File tree

1 file changed

+7
-36
lines changed

1 file changed

+7
-36
lines changed

server/src/main/java/org/elasticsearch/cluster/routing/TsidBuilder.java

Lines changed: 7 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
package org.elasticsearch.cluster.routing;
1111

1212
import org.apache.lucene.util.BytesRef;
13-
import org.elasticsearch.common.hash.Murmur3Hasher;
13+
import org.elasticsearch.common.hash.BufferedMurmur3Hasher;
1414
import org.elasticsearch.common.hash.MurmurHash3;
1515
import org.elasticsearch.common.util.ByteUtils;
1616
import org.elasticsearch.index.mapper.RoutingPathFields;
@@ -32,7 +32,7 @@
3232
public class TsidBuilder {
3333

3434
private static final int MAX_TSID_VALUE_FIELDS = 16;
35-
private final Murmur3Hasher murmur3Hasher = new Murmur3Hasher(0L);
35+
private final BufferedMurmur3Hasher murmur3Hasher = new BufferedMurmur3Hasher(0L);
3636

3737
private final List<Dimension> dimensions = new ArrayList<>();
3838

@@ -166,7 +166,7 @@ public <T, E extends Exception> TsidBuilder add(T value, ThrowingTsidFunnel<T, E
166166

167167
private void addDimension(String path, MurmurHash3.Hash128 valueHash) {
168168
murmur3Hasher.reset();
169-
addString(murmur3Hasher, path);
169+
murmur3Hasher.addString(path);
170170
MurmurHash3.Hash128 pathHash = murmur3Hasher.digestHash();
171171
dimensions.add(new Dimension(path, pathHash, valueHash, dimensions.size()));
172172
}
@@ -198,7 +198,7 @@ public MurmurHash3.Hash128 hash() {
198198
Collections.sort(dimensions);
199199
murmur3Hasher.reset();
200200
for (Dimension dim : dimensions) {
201-
addLongs(murmur3Hasher, dim.pathHash.h1, dim.pathHash.h2, dim.valueHash.h1, dim.valueHash.h2);
201+
murmur3Hasher.addLongs(dim.pathHash.h1, dim.pathHash.h2, dim.valueHash.h1, dim.valueHash.h2);
202202
}
203203
return murmur3Hasher.digestHash();
204204
}
@@ -237,7 +237,7 @@ public BytesRef buildTsid() {
237237
murmur3Hasher.reset();
238238
for (int i = 0; i < dimensions.size(); i++) {
239239
Dimension dim = dimensions.get(i);
240-
addLong(murmur3Hasher, dim.pathHash.h1 ^ dim.pathHash.h2);
240+
murmur3Hasher.addLong(dim.pathHash.h1 ^ dim.pathHash.h2);
241241
}
242242
ByteUtils.writeIntLE((int) murmur3Hasher.digestHash(hashBuffer).h1, hash, index);
243243
index += 4;
@@ -253,15 +253,15 @@ public BytesRef buildTsid() {
253253
}
254254
MurmurHash3.Hash128 valueHash = dim.valueHash();
255255
murmur3Hasher.reset();
256-
addLong(murmur3Hasher, valueHash.h1 ^ valueHash.h2);
256+
murmur3Hasher.addLong(valueHash.h1 ^ valueHash.h2);
257257
hash[index++] = (byte) murmur3Hasher.digestHash(hashBuffer).h1;
258258
previousPath = path;
259259
}
260260

261261
murmur3Hasher.reset();
262262
for (int i = 0; i < dimensions.size(); i++) {
263263
Dimension dim = dimensions.get(i);
264-
addLongs(murmur3Hasher, dim.pathHash.h1, dim.pathHash.h2, dim.valueHash.h1, dim.valueHash.h2);
264+
murmur3Hasher.addLongs(dim.pathHash.h1, dim.pathHash.h2, dim.valueHash.h1, dim.valueHash.h2);
265265
}
266266
index = writeHash128(murmur3Hasher.digestHash(hashBuffer), hash, index);
267267
return new BytesRef(hash, 0, index);
@@ -314,33 +314,4 @@ public int compareTo(Dimension o) {
314314
return Integer.compare(insertionOrder, o.insertionOrder);
315315
}
316316
}
317-
318-
// these methods will be replaced with a more optimized version when https://github.com/elastic/elasticsearch/pull/133226 is merged
319-
320-
private static void addString(Murmur3Hasher murmur3Hasher, String path) {
321-
BytesRef bytesRef = new BytesRef(path);
322-
murmur3Hasher.update(bytesRef.bytes, bytesRef.offset, bytesRef.length);
323-
}
324-
325-
private static void addLong(Murmur3Hasher murmur3Hasher, long value) {
326-
byte[] bytes = new byte[8];
327-
ByteUtils.writeLongLE(value, bytes, 0);
328-
murmur3Hasher.update(bytes);
329-
}
330-
331-
private static void addLongs(Murmur3Hasher murmur3Hasher, long v1, long v2) {
332-
byte[] bytes = new byte[16];
333-
ByteUtils.writeLongLE(v1, bytes, 0);
334-
ByteUtils.writeLongLE(v2, bytes, 8);
335-
murmur3Hasher.update(bytes);
336-
}
337-
338-
private static void addLongs(Murmur3Hasher murmur3Hasher, long v1, long v2, long v3, long v4) {
339-
byte[] bytes = new byte[32];
340-
ByteUtils.writeLongLE(v1, bytes, 0);
341-
ByteUtils.writeLongLE(v2, bytes, 8);
342-
ByteUtils.writeLongLE(v3, bytes, 16);
343-
ByteUtils.writeLongLE(v4, bytes, 24);
344-
murmur3Hasher.update(bytes);
345-
}
346317
}

0 commit comments

Comments
 (0)