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
76 changes: 61 additions & 15 deletions java/tsfile/src/main/java/org/apache/tsfile/utils/BloomFilter.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,10 @@
import java.nio.charset.StandardCharsets;
import java.util.BitSet;
import java.util.Collection;
import java.util.Iterator;
import java.util.Objects;
import java.util.Set;
import java.util.function.BiConsumer;

import static org.apache.tsfile.utils.RamUsageEstimator.sizeOfLongArray;

Expand Down Expand Up @@ -151,29 +154,72 @@ public BitSet mightContainBatch(IDeviceID device, Collection<String> measurement
if (measurements.isEmpty()) {
return matches;
}
String deviceName = device.toString();
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;
String prefix = device.toString() + TsFileConstant.PATH_SEPARATOR;
Murmur128Hash.PrefixHasher hasher = createPrefixHasher(prefix);
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);
}
if (mightContainMeasurement(prefix, hasher, measurement)) {
matches.set(index);
}
index++;
}
return matches;
}

/**
* Removes measurements that this filter rules out, using the same path rules as {@link
* #mightContainBatch}. Calls {@code onAbsent} with the device and each removed measurement
* immediately after removal. Measurements left in the set may still be absent from the file.
*
* <p>If the set contains {@code null}, this method treats it as absent, removes it, and calls
* {@code onAbsent.accept(device, null)}.
*
* @param measurements a mutable set whose iterator supports removal
* @param onAbsent receives each removal and must not modify the set
* @throws NullPointerException if any argument is null
*/
Comment thread
ColinLeeo marked this conversation as resolved.
Resolved
public void removeNotExistingMeasurements(
IDeviceID device, Set<String> measurements, BiConsumer<IDeviceID, String> onAbsent) {
Comment on lines +181 to +182
Objects.requireNonNull(device);
Objects.requireNonNull(measurements);
Objects.requireNonNull(onAbsent);
if (measurements.isEmpty()) {
return;
}
String prefix = device.toString() + TsFileConstant.PATH_SEPARATOR;
Murmur128Hash.PrefixHasher hasher = createPrefixHasher(prefix);
Iterator<String> iterator = measurements.iterator();
while (iterator.hasNext()) {
String measurement = iterator.next();
if (!mightContainMeasurement(prefix, hasher, measurement)) {
iterator.remove();
onAbsent.accept(device, measurement);
}
}
}

private Murmur128Hash.PrefixHasher createPrefixHasher(String prefix) {
// 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.
if (!StandardCharsets.UTF_8.equals(Charset.defaultCharset())) {
return null;
}
return new Murmur128Hash.PrefixHasher(
prefix.getBytes(StandardCharsets.UTF_8), SEEDS, hashFunctionSize);
}

private boolean mightContainMeasurement(
String prefix, Murmur128Hash.PrefixHasher hasher, String measurement) {
if (measurement == null) {
return false;
}
boolean matchesFullPath =
hasher == null ? contains(prefix + measurement) : mightContain(hasher, measurement);
// For an empty device the prefix is just the separator. Also check the bare measurement.
return matchesFullPath
|| (TsFileConstant.PATH_SEPARATOR.equals(prefix) && contains(measurement));
}
Comment on lines +211 to +221

private boolean mightContain(Murmur128Hash.PrefixHasher hasher, String measurement) {
hasher.setSuffix(measurement.getBytes(StandardCharsets.UTF_8));
for (int i = 0; i < hashFunctionSize; i++) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,10 @@
import java.util.Collections;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertThrows;
import static org.junit.Assert.assertTrue;
import static org.junit.Assume.assumeTrue;
Expand Down Expand Up @@ -107,6 +109,27 @@ public void testBatchMatchesIndividualPaths() {
}
}

@Test
public void testRemoveNotExistingMeasurements() {
IDeviceID device = IDeviceID.Factory.DEFAULT_FACTORY.create("root.d");
BloomFilter filter = BloomFilter.getEmptyBloomFilter(0.01, 100);
filter.add(new FullPath(device, ""));
filter.add(new FullPath(device, "温度"));
Set<String> pending = new LinkedHashSet<>(Arrays.asList("missing", "", null, "温度"));
Set<String> removed = new LinkedHashSet<>();

filter.removeNotExistingMeasurements(
device,
pending,
(id, name) -> {
assertEquals(device, id);
assertFalse(pending.contains(name));
assertTrue(removed.add(name));
});
assertEquals(new LinkedHashSet<>(Arrays.asList("missing", null)), removed);
assertEquals(new LinkedHashSet<>(Arrays.asList("", "温度")), pending);
}

@Test
public void testBatchRejectsNullArguments() {
BloomFilter filter = BloomFilter.getEmptyBloomFilter(0.05, 10);
Expand All @@ -132,6 +155,11 @@ public void testBatchChecksBothEmptyDevicePathForms() {
filter.add(path);
}
assertEquals(expected, filter.mightContainBatch(device, measurements));
Set<String> pending = new LinkedHashSet<>(measurements);
Set<String> removed = new LinkedHashSet<>();
filter.removeNotExistingMeasurements(device, pending, (id, name) -> removed.add(name));
assertEquals(new LinkedHashSet<>(Arrays.asList("s1", "")), pending);
assertEquals(new LinkedHashSet<>(Arrays.asList(null, "missing")), removed);
}
}

Expand Down
5 changes: 5 additions & 0 deletions skills/tsfile/references/java.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,11 @@ Use an ordered collection when matching positions back to measurement names.
An empty measurement name probes the time column. For an empty device ID, the
lookup checks both supported path forms, for example `s1` and `.s1`.

For a mutable set of candidates, call
`filter.removeNotExistingMeasurements(device, pending, onAbsent)`.
It removes each name that the filter rules out and calls `onAbsent` with the
device and removed name. Pass the remaining set directly to the metadata reader.

## Guidance

- Prefer `Tablet` for batch writes and `TSRecord` for genuinely record-oriented
Expand Down
Loading