BloomFilter Class
public closed class BloomFilterNo documentation is available.
Methods
CreateAdler32(BloomFilterSize)
BloomFilter.g.cspublic static BloomFilterAdler32 CreateAdler32(BloomFilterSize size)No documentation is available.
CreateCrc32(BloomFilterSize)
BloomFilter.g.cspublic static BloomFilterCrc32 CreateCrc32(BloomFilterSize size)No documentation is available.
CreateCrc64(BloomFilterSize)
BloomFilter.g.cspublic static BloomFilterCrc64 CreateCrc64(BloomFilterSize size)No documentation is available.
CreateXXHash128(BloomFilterSize)
BloomFilter.g.cspublic static BloomFilterXXHash128 CreateXXHash128(BloomFilterSize size)No documentation is available.
CreateXXHash3(BloomFilterSize)
BloomFilter.g.cspublic static BloomFilterXXHash3 CreateXXHash3(BloomFilterSize size)No documentation is available.
CreateXXHash32(BloomFilterSize)
BloomFilter.g.cspublic static BloomFilterXXHash32 CreateXXHash32(BloomFilterSize size)No documentation is available.
CreateXXHash64(BloomFilterSize)
BloomFilter.g.cspublic static BloomFilterXXHash64 CreateXXHash64(BloomFilterSize size)No documentation is available.
GetEstimateCount()
BloomFilter.csLoad overloads
Load(Stream)
BloomFilter.Snapshot.csCreates a filter from a snapshot written by BloomFilter.Save.
public static BloomFilter Load(Stream stream)Parameters
streamStream- The stream to read the snapshot from. It is left positioned after the snapshot.
Returns
BloomFilter A new filter of the hash algorithm, size and content of the snapshot, such as a BloomFilterXXHash128.
Exceptions
InvalidDataException- The data is not a Bloom filter snapshot, is truncated or corrupted, uses an unsupported version, contains a counting Bloom filter, or was written on a machine with another byte order.
Remarks
The header is validated before the filter is allocated. When the stream is seekable, its length is also checked against the size of the filter first. The snapshot ends with a checksum, verified once it is read.
Load<TFilter>(Stream)
BloomFilter.Snapshot.csCreates a filter of a given type from a snapshot written by BloomFilter.Save.
public static TFilter Load<TFilter>(Stream stream) where TFilter : BloomFilterType parameters
TFilter- The type of the filter, such as
BloomFilterXXHash128.
Parameters
streamStream- The stream to read the snapshot from. It is left positioned after the snapshot.
Returns
TFilter A new filter of the size and content of the snapshot.
Exceptions
InvalidDataException- The snapshot contains a filter of another type, or is not valid (see
BloomFilter.Load).
LoadAsync overloads
LoadAsync(Stream, CancellationToken)
BloomFilter.Snapshot.csCreates a filter from a snapshot written by BloomFilter.Save or BloomFilter.SaveAsync.
public static Task<BloomFilter> LoadAsync(Stream stream, CancellationToken cancellationToken = default)Parameters
streamStream- The stream to read the snapshot from. It is left positioned after the snapshot.
cancellationTokenCancellationToken- A token to cancel the operation.
Returns
Task<BloomFilter> A new filter of the hash algorithm, size and content of the snapshot.
Exceptions
InvalidDataException- The snapshot is not valid (see
BloomFilter.Load).
LoadAsync<TFilter>(Stream, CancellationToken)
BloomFilter.Snapshot.csCreates a filter of a given type from a snapshot written by BloomFilter.Save or BloomFilter.SaveAsync.
public static Task<TFilter> LoadAsync<TFilter>(Stream stream, CancellationToken cancellationToken = default) where TFilter : BloomFilterType parameters
TFilter- The type of the filter, such as
BloomFilterXXHash128.
Parameters
streamStream- The stream to read the snapshot from. It is left positioned after the snapshot.
cancellationTokenCancellationToken- A token to cancel the operation.
Returns
Task<TFilter> A new filter of the size and content of the snapshot.
Exceptions
InvalidDataException- The snapshot contains a filter of another type, or is not valid (see
BloomFilter.Load).
Save(Stream)
BloomFilter.Snapshot.csWrites a snapshot of the filter to a stream: its hash algorithm, its size and its bits.
Parameters
streamStream- The stream to write the snapshot to.
Remarks
The bits are copied through a small buffer, so saving a large filter does not allocate a second copy of it.
Stop adding values while the snapshot is written. Each bit is read atomically, but values added during the write may be partially saved: some of their bits are in the snapshot, others are not, so the restored filter may report them as absent.
Keys are hashed from their in-memory bytes, so the snapshot records the byte order of the machine, and BloomFilter.Load rejects a snapshot written on a machine with another byte order.
Example
var filter = BloomFilter.CreateXXHash128(BloomFilterSize.CreateOptimalSize(expectedItemCount: 1000, falsePositiveProbability: 0.01));
filter.Add("alice@example.com");
// Save the filter to a stream, such as a FileStream, and load it after a restart
using var stream = new MemoryStream();
filter.Save(stream);
stream.Position = 0;
var restored = BloomFilter.Load<BloomFilterXXHash128>(stream);
Assert.True(restored.MayContain("alice@example.com"));
Assert.False(restored.MayContain("bob@example.com"));SaveAsync(Stream, CancellationToken)
BloomFilter.Snapshot.csWrites a snapshot of the filter to a stream: its hash algorithm, its size and its bits.
public Task SaveAsync(Stream stream, CancellationToken cancellationToken = default)Parameters
streamStream- The stream to write the snapshot to.
cancellationTokenCancellationToken- A token to cancel the operation.
Returns
Task A task that completes when the snapshot is written.
Remarks
See BloomFilter.Save. Stop adding values until the task completes.
UnionWith(BloomFilter)
BloomFilter.csAdds to this filter every value represented by other, by combining their bits with a bitwise OR.
public void UnionWith(BloomFilter other)Parameters
otherBloomFilter- The filter whose values are added. It must use the same hash algorithm (the same concrete type), the same number of bits and the same number of hash functions as this filter. It is not modified.
Exceptions
ArgumentNullExceptionotherisnull.ArgumentExceptionotheruses another hash algorithm, number of bits or number of hash functions.
Remarks
After the call, MayContain returns true for every value added to this filter or to other before the call: the union never introduces a false negative. Calling the method with the filter itself does nothing.
The result holds the values of both filters in the same number of bits, so more bits are set than in either filter. Its false positive probability is higher than the one the filters were sized for when the total number of values exceeds the expected item count. Size each filter for the total number of values when you plan to merge them.
The compatibility checks happen before any bit is written: when they fail, this filter is left unchanged.
The method can run while other threads call Add or MayContain on either filter. The bits of this filter are set with atomic operations, so no value added concurrently to this filter is lost. The source is read one 64-bit word at a time, not as a snapshot: a value added to other during the call may or may not be included, and a MayContain on this filter during the call may return false for a value of other that is not merged yet. Once the method has returned, every value that was in other when it was called is included.
Counting Bloom filters do not support this operation.