BloomFilter Class

public closed class BloomFilter

No documentation is available.

Methods

NameDescription
CreateAdler32(BloomFilterSize)
CreateCrc32(BloomFilterSize)
CreateCrc64(BloomFilterSize)
CreateXXHash128(BloomFilterSize)
CreateXXHash3(BloomFilterSize)
CreateXXHash32(BloomFilterSize)
CreateXXHash64(BloomFilterSize)
GetEstimateCount()
Load(Stream)Creates a filter from a snapshot written by BloomFilter.Save.
Load<TFilter>(Stream)Creates a filter of a given type from a snapshot written by BloomFilter.Save.
LoadAsync(Stream, CancellationToken)Creates a filter from a snapshot written by BloomFilter.Save or BloomFilter.SaveAsync.
LoadAsync<TFilter>(Stream, CancellationToken)Creates a filter of a given type from a snapshot written by BloomFilter.Save or BloomFilter.SaveAsync.
Save(Stream)Writes a snapshot of the filter to a stream: its hash algorithm, its size and its bits.
SaveAsync(Stream, CancellationToken)Writes a snapshot of the filter to a stream: its hash algorithm, its size and its bits.
UnionWith(BloomFilter)Adds to this filter every value represented by other, by combining their bits with a bitwise OR.

CreateAdler32(BloomFilterSize)

BloomFilter.g.cs
public static BloomFilterAdler32 CreateAdler32(BloomFilterSize size)

Parameters

size BloomFilterSize

Returns

BloomFilterAdler32

No documentation is available.

CreateCrc32(BloomFilterSize)

BloomFilter.g.cs
public static BloomFilterCrc32 CreateCrc32(BloomFilterSize size)

Parameters

size BloomFilterSize

Returns

BloomFilterCrc32

No documentation is available.

CreateCrc64(BloomFilterSize)

BloomFilter.g.cs
public static BloomFilterCrc64 CreateCrc64(BloomFilterSize size)

Parameters

size BloomFilterSize

Returns

BloomFilterCrc64

No documentation is available.

CreateXXHash128(BloomFilterSize)

BloomFilter.g.cs
public static BloomFilterXXHash128 CreateXXHash128(BloomFilterSize size)

No documentation is available.

CreateXXHash3(BloomFilterSize)

BloomFilter.g.cs
public static BloomFilterXXHash3 CreateXXHash3(BloomFilterSize size)

Parameters

size BloomFilterSize

Returns

BloomFilterXXHash3

No documentation is available.

CreateXXHash32(BloomFilterSize)

BloomFilter.g.cs
public static BloomFilterXXHash32 CreateXXHash32(BloomFilterSize size)

No documentation is available.

CreateXXHash64(BloomFilterSize)

BloomFilter.g.cs
public static BloomFilterXXHash64 CreateXXHash64(BloomFilterSize size)

No documentation is available.

GetEstimateCount()

BloomFilter.cs
public double GetEstimateCount()

Returns

double

No documentation is available.

Load overloads

Creates a filter from a snapshot written by BloomFilter.Save.

public static BloomFilter Load(Stream stream)
Parameters
stream Stream
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.cs

Creates a filter of a given type from a snapshot written by BloomFilter.Save.

public static TFilter Load<TFilter>(Stream stream) where TFilter : BloomFilter
Type parameters
TFilter
The type of the filter, such as BloomFilterXXHash128.
Parameters
stream Stream
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.cs

Creates a filter from a snapshot written by BloomFilter.Save or BloomFilter.SaveAsync.

public static Task<BloomFilter> LoadAsync(Stream stream, CancellationToken cancellationToken = default)
Parameters
stream Stream
The stream to read the snapshot from. It is left positioned after the snapshot.
cancellationToken CancellationToken
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.cs

Creates 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 : BloomFilter
Type parameters
TFilter
The type of the filter, such as BloomFilterXXHash128.
Parameters
stream Stream
The stream to read the snapshot from. It is left positioned after the snapshot.
cancellationToken CancellationToken
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).

Writes a snapshot of the filter to a stream: its hash algorithm, its size and its bits.

public void Save(Stream stream)

Parameters

stream Stream
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.cs

Writes 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

stream Stream
The stream to write the snapshot to.
cancellationToken CancellationToken
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.cs

Adds to this filter every value represented by other, by combining their bits with a bitwise OR.

public void UnionWith(BloomFilter other)

Parameters

other BloomFilter
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

ArgumentNullException
other is null.
ArgumentException
other uses 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.