Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,17 @@
package org.apache.tsfile.utils;

import org.apache.tsfile.common.conf.TSFileConfig;
import org.apache.tsfile.common.constant.TsFileConstant;
import org.apache.tsfile.file.metadata.IDeviceID;
import org.apache.tsfile.read.common.Path;

import java.io.IOException;
import java.io.OutputStream;
import java.nio.ByteBuffer;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.BitSet;
import java.util.Collection;
import java.util.Objects;

import static org.apache.tsfile.utils.RamUsageEstimator.sizeOfLongArray;
Expand Down Expand Up @@ -131,6 +136,54 @@ public boolean contains(String value) {
return ret;
}

/**
* Tests measurements of one device against this filter. Bit i in the result corresponds to the
* ith measurement in iteration order. A null measurement has a clear bit. An empty measurement
* tests the device's time-column path. For an empty device, both path forms (with and without the
* leading separator) are checked.
*
* @throws NullPointerException if the device or the measurement collection is null
*/
public BitSet mightContainBatch(IDeviceID device, Collection<String> measurements) {
Objects.requireNonNull(device);
Objects.requireNonNull(measurements);
BitSet matches = new BitSet(measurements.size());
if (measurements.isEmpty()) {
return matches;
}
String deviceName = device.toString();
Comment thread
JackieTien97 marked this conversation as resolved.
Resolved
String prefix = deviceName + TsFileConstant.PATH_SEPARATOR;
// The file's existing hashes use the default charset. Stateful encodings and byte-order marks
// can make encoding the prefix and suffix separately differ from encoding the full path.
Murmur128Hash.PrefixHasher hasher =
StandardCharsets.UTF_8.equals(Charset.defaultCharset())
? new Murmur128Hash.PrefixHasher(
prefix.getBytes(StandardCharsets.UTF_8), SEEDS, hashFunctionSize)
: null;
int index = 0;
for (String measurement : measurements) {
if (measurement != null) {
boolean matchesFullPath =
hasher == null ? contains(prefix + measurement) : mightContain(hasher, measurement);
if (matchesFullPath || (deviceName.isEmpty() && contains(measurement))) {
matches.set(index);
}
}
index++;
}
return matches;
}

private boolean mightContain(Murmur128Hash.PrefixHasher hasher, String measurement) {
hasher.setSuffix(measurement.getBytes(StandardCharsets.UTF_8));
Comment thread
JackieTien97 marked this conversation as resolved.
Resolved
for (int i = 0; i < hashFunctionSize; i++) {
if (!bits.get(bitIndex(hasher.hash(i), size))) {
return false;
}
}
return true;
}

public byte[] serialize() {
return bits.toByteArray();
}
Expand Down Expand Up @@ -161,7 +214,10 @@ public int hashCode() {
}

private static int hash(String value, int cap, int seed) {
int res = Murmur128Hash.hash(value, seed);
return bitIndex(Murmur128Hash.hash(value, seed), cap);
}

private static int bitIndex(int res, int cap) {
if (res == Integer.MIN_VALUE) {
res = 0;
}
Expand Down
122 changes: 97 additions & 25 deletions java/tsfile/src/main/java/org/apache/tsfile/utils/Murmur128Hash.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,13 @@
*/
package org.apache.tsfile.utils;

import java.util.Arrays;

public class Murmur128Hash {

private static final long C1 = 0x87c37b91114253d5L;
private static final long C2 = 0x4cf5ad432745937fL;

private Murmur128Hash() {
// util class
}
Expand Down Expand Up @@ -79,31 +84,21 @@ private static long fmix(long k) {
return k;
}

private static long innerHash(byte[] key, int offset, int length, long seed) {
static long innerHash(byte[] key, int offset, int length, long seed) {
return innerHash(key, offset, length, length, seed, seed);
}

// totalLength includes any complete prefix blocks already mixed into h1 and h2.
private static long innerHash(
byte[] key, int offset, int length, int totalLength, long h1, long h2) {
final int nblocks = length >> 4; // Process as 128-bit blocks.
long h1 = seed;
long h2 = seed;
long c1 = 0x87c37b91114253d5L;
long c2 = 0x4cf5ad432745937fL;
// ----------
// body
for (int i = 0; i < nblocks; i++) {
long k1 = getBlock(key, offset, i * 2);
long k2 = getBlock(key, offset, i * 2 + 1);
k1 *= c1;
k1 = rotl64(k1, 31);
k1 *= c2;
h1 ^= k1;
h1 = rotl64(h1, 27);
h1 += h2;
h1 = h1 * 5 + 0x52dce729;
k2 *= c2;
k2 = rotl64(k2, 33);
k2 *= c1;
h2 ^= k2;
h2 = rotl64(h2, 31);
h2 += h1;
h2 = h2 * 5 + 0x38495ab5;
h1 = mixH1(h1, h2, k1);
h2 = mixH2(h1, h2, k2);
}
// ----------
// tail
Expand Down Expand Up @@ -132,9 +127,9 @@ private static long innerHash(byte[] key, int offset, int length, long seed) {
// fallthrough
case 9:
k2 ^= key[offset + 8];
k2 *= c2;
k2 *= C2;
k2 = rotl64(k2, 33);
k2 *= c1;
k2 *= C1;
h2 ^= k2;
// fallthrough
case 8:
Expand All @@ -160,9 +155,9 @@ private static long innerHash(byte[] key, int offset, int length, long seed) {
// fallthrough
case 1:
k1 ^= (key[offset]);
k1 *= c1;
k1 *= C1;
k1 = rotl64(k1, 31);
k1 *= c2;
k1 *= C2;
h1 ^= k1;
break;
default: // 0
Expand All @@ -171,8 +166,8 @@ private static long innerHash(byte[] key, int offset, int length, long seed) {
}
// ----------
// finalization
h1 ^= length;
h2 ^= length;
h1 ^= totalLength;
h2 ^= totalLength;
h1 += h2;
h2 += h1;
h1 = fmix(h1);
Expand All @@ -181,4 +176,81 @@ private static long innerHash(byte[] key, int offset, int length, long seed) {
h2 += h1;
return h1 + h2;
}

private static long mixH1(long h1, long h2, long k1) {
k1 *= C1;
k1 = rotl64(k1, 31);
k1 *= C2;
h1 ^= k1;
h1 = rotl64(h1, 27);
h1 += h2;
return h1 * 5 + 0x52dce729;
}

private static long mixH2(long h1, long h2, long k2) {
k2 *= C2;
k2 = rotl64(k2, 33);
k2 *= C1;
h2 ^= k2;
h2 = rotl64(h2, 31);
h2 += h1;
return h2 * 5 + 0x38495ab5;
}

/** Reuses complete prefix blocks for all measurements in one Bloom filter lookup. */
static final class PrefixHasher {
private final long[] prefixH1;
private final long[] prefixH2;
private final byte[] prefix;
private final int prefixBlockBytes;
private final int[] seeds;
private final boolean[] initialized;
private byte[] buffer;
private int totalLength;

PrefixHasher(byte[] prefix, int[] seeds, int seedCount) {
this.prefix = prefix;
this.seeds = seeds;
prefixBlockBytes = prefix.length - (prefix.length % 16);
totalLength = prefix.length;
buffer = Arrays.copyOfRange(prefix, prefixBlockBytes, prefix.length);
prefixH1 = new long[seedCount];
prefixH2 = new long[seedCount];
initialized = new boolean[seedCount];
}

void setSuffix(byte[] suffix) {
totalLength = Math.addExact(prefix.length, suffix.length);
int remaining = totalLength - prefixBlockBytes;
int prefixTailLength = prefix.length - prefixBlockBytes;
if (buffer.length < remaining) {
int capacity = Math.max(remaining, buffer.length + (buffer.length >>> 1));
buffer = new byte[capacity];
System.arraycopy(prefix, prefixBlockBytes, buffer, 0, prefixTailLength);
}
System.arraycopy(suffix, 0, buffer, prefixTailLength, suffix.length);
}

int hash(int seedIndex) {
if (!initialized[seedIndex]) {
long h1 = seeds[seedIndex];
long h2 = h1;
for (int block = 0; block < prefixBlockBytes / 16; block++) {
h1 = mixH1(h1, h2, getBlock(prefix, 0, block * 2));
h2 = mixH2(h1, h2, getBlock(prefix, 0, block * 2 + 1));
}
prefixH1[seedIndex] = h1;
prefixH2[seedIndex] = h2;
initialized[seedIndex] = true;
}
return (int)
innerHash(
buffer,
0,
totalLength - prefixBlockBytes,
totalLength,
prefixH1[seedIndex],
prefixH2[seedIndex]);
}
}
}
Loading
Loading