vla / BloomFilter.NetCore

A bloom filter implementation
MIT License
147 stars 38 forks source link

Save bloom filter to disk #6

Closed xtaylord closed 5 months ago

xtaylord commented 1 year ago

Could a feature be included to allow filters to be saved to a file or loaded from file? Thanks

vla commented 1 year ago

Could a feature be included to allow filters to be saved to a file or loaded from file? Thanks

Thank you for your issues.

BloomFilter aims to reduce the number of disk accesses to improve performance.

Redis are recommended for persistence.

timaiv commented 1 year ago

Redis are recommended for persistence

I need to save bloom filters in the sql database and then import from the database. (Ex: blockchain logs usage etc)

faraway1989 commented 5 months ago
public static class IBloomFilterExtensions
{
    public static void GetBitArray(this IBloomFilter ins, out BitArray _hashBits1, out BitArray? _hashBits2)
    {
        _hashBits1 = (BitArray)typeof(FilterMemory).GetField("_hashBits1", System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic).GetValue(ins);
        _hashBits2 = (BitArray?)typeof(FilterMemory).GetField("_hashBits2", System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic).GetValue(ins);
    }
    public static void SetBitArray(this IBloomFilter ins, BitArray bits1, BitArray? bits2)
    {
        GetBitArray(ins, out var _hashBits1, out var _hashBits2);

        for (var i = 0; i < bits1.Length; i++)
            _hashBits1[i] = bits1[i];

        if (_hashBits2 == null || bits2 == null)
            return;

        for (var i = 0; i < bits2.Length; i++)
            _hashBits2[i] = bits2[i];
    }
}
vla commented 5 months ago
public static class IBloomFilterExtensions
{
    public static void GetBitArray(this IBloomFilter ins, out BitArray _hashBits1, out BitArray? _hashBits2)
    {
        _hashBits1 = (BitArray)typeof(FilterMemory).GetField("_hashBits1", System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic).GetValue(ins);
        _hashBits2 = (BitArray?)typeof(FilterMemory).GetField("_hashBits2", System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic).GetValue(ins);
    }
    public static void SetBitArray(this IBloomFilter ins, BitArray bits1, BitArray? bits2)
    {
        GetBitArray(ins, out var _hashBits1, out var _hashBits2);

        for (var i = 0; i < bits1.Length; i++)
            _hashBits1[i] = bits1[i];

        if (_hashBits2 == null || bits2 == null)
            return;

        for (var i = 0; i < bits2.Length; i++)
            _hashBits2[i] = bits2[i];
    }
}

Do you want to expose BitArray for external use?