Concurrent-safe Bloom Filter

This commit is contained in:
Sarah Jamie Lewis 2019-06-10 12:18:32 -07:00
parent 56d2a00855
commit d0cc2814db
1 changed files with 5 additions and 1 deletions

View File

@ -2,11 +2,13 @@ package primitives
import (
"crypto/sha256"
"sync"
)
// BloomFilter implements a bloom filter
type BloomFilter struct {
B []bool
lock sync.Mutex
}
// Init constructs a bloom filter of size m
@ -38,9 +40,11 @@ func (bf BloomFilter) Hash(msg []byte) []int {
return []int{pos1, pos2, pos3, pos4}
}
// Insert updates the BloomFilter
// Insert updates the BloomFilter (suitable for concurrent use)
func (bf *BloomFilter) Insert(msg []byte) {
pos := bf.Hash(msg)
bf.lock.Lock()
defer bf.lock.Unlock()
bf.B[pos[0]] = true
bf.B[pos[1]] = true
bf.B[pos[2]] = true