forked from cwtch.im/tapir
Concurrent-safe Bloom Filter
This commit is contained in:
parent
56d2a00855
commit
d0cc2814db
|
@ -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
|
||||
|
|
Loading…
Reference in New Issue