Remove embedded bloom.Filter mutex.
This prevents the caller from being able to accidentally lock or unlock access to the filter internal state. While here, remove several defers that do not gain us any readability, and only hurt our performance.
This commit is contained in:
parent
47c887338f
commit
3fd010412c
1 changed files with 31 additions and 36 deletions
|
@ -29,7 +29,7 @@ func minUint32(a, b uint32) uint32 {
|
||||||
// Filter defines a bitcoin bloom filter that provides easy manipulation of raw
|
// Filter defines a bitcoin bloom filter that provides easy manipulation of raw
|
||||||
// filter data.
|
// filter data.
|
||||||
type Filter struct {
|
type Filter struct {
|
||||||
sync.Mutex
|
mtx sync.Mutex
|
||||||
msgFilterLoad *btcwire.MsgFilterLoad
|
msgFilterLoad *btcwire.MsgFilterLoad
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -87,30 +87,28 @@ func LoadFilter(filter *btcwire.MsgFilterLoad) *Filter {
|
||||||
//
|
//
|
||||||
// This function is safe for concurrent access.
|
// This function is safe for concurrent access.
|
||||||
func (bf *Filter) IsLoaded() bool {
|
func (bf *Filter) IsLoaded() bool {
|
||||||
bf.Lock()
|
bf.mtx.Lock()
|
||||||
defer bf.Unlock()
|
loaded := bf.msgFilterLoad != nil
|
||||||
|
bf.mtx.Unlock()
|
||||||
return bf.msgFilterLoad != nil
|
return loaded
|
||||||
}
|
}
|
||||||
|
|
||||||
// Reload loads a new filter replacing any existing filter.
|
// Reload loads a new filter replacing any existing filter.
|
||||||
//
|
//
|
||||||
// This function is safe for concurrent access.
|
// This function is safe for concurrent access.
|
||||||
func (bf *Filter) Reload(filter *btcwire.MsgFilterLoad) {
|
func (bf *Filter) Reload(filter *btcwire.MsgFilterLoad) {
|
||||||
bf.Lock()
|
bf.mtx.Lock()
|
||||||
defer bf.Unlock()
|
|
||||||
|
|
||||||
bf.msgFilterLoad = filter
|
bf.msgFilterLoad = filter
|
||||||
|
bf.mtx.Unlock()
|
||||||
}
|
}
|
||||||
|
|
||||||
// Unload unloads the bloom filter.
|
// Unload unloads the bloom filter.
|
||||||
//
|
//
|
||||||
// This function is safe for concurrent access.
|
// This function is safe for concurrent access.
|
||||||
func (bf *Filter) Unload() {
|
func (bf *Filter) Unload() {
|
||||||
bf.Lock()
|
bf.mtx.Lock()
|
||||||
defer bf.Unlock()
|
|
||||||
|
|
||||||
bf.msgFilterLoad = nil
|
bf.msgFilterLoad = nil
|
||||||
|
bf.mtx.Unlock()
|
||||||
}
|
}
|
||||||
|
|
||||||
// hash returns the bit offset in the bloom filter which corresponds to the
|
// hash returns the bit offset in the bloom filter which corresponds to the
|
||||||
|
@ -156,10 +154,10 @@ func (bf *Filter) matches(data []byte) bool {
|
||||||
//
|
//
|
||||||
// This function is safe for concurrent access.
|
// This function is safe for concurrent access.
|
||||||
func (bf *Filter) Matches(data []byte) bool {
|
func (bf *Filter) Matches(data []byte) bool {
|
||||||
bf.Lock()
|
bf.mtx.Lock()
|
||||||
defer bf.Unlock()
|
match := bf.matches(data)
|
||||||
|
bf.mtx.Unlock()
|
||||||
return bf.matches(data)
|
return match
|
||||||
}
|
}
|
||||||
|
|
||||||
// matchesOutPoint returns true if the bloom filter might contain the passed
|
// matchesOutPoint returns true if the bloom filter might contain the passed
|
||||||
|
@ -180,10 +178,10 @@ func (bf *Filter) matchesOutPoint(outpoint *btcwire.OutPoint) bool {
|
||||||
//
|
//
|
||||||
// This function is safe for concurrent access.
|
// This function is safe for concurrent access.
|
||||||
func (bf *Filter) MatchesOutPoint(outpoint *btcwire.OutPoint) bool {
|
func (bf *Filter) MatchesOutPoint(outpoint *btcwire.OutPoint) bool {
|
||||||
bf.Lock()
|
bf.mtx.Lock()
|
||||||
defer bf.Unlock()
|
match := bf.matchesOutPoint(outpoint)
|
||||||
|
bf.mtx.Unlock()
|
||||||
return bf.matchesOutPoint(outpoint)
|
return match
|
||||||
}
|
}
|
||||||
|
|
||||||
// add adds the passed byte slice to the bloom filter.
|
// add adds the passed byte slice to the bloom filter.
|
||||||
|
@ -211,20 +209,18 @@ func (bf *Filter) add(data []byte) {
|
||||||
//
|
//
|
||||||
// This function is safe for concurrent access.
|
// This function is safe for concurrent access.
|
||||||
func (bf *Filter) Add(data []byte) {
|
func (bf *Filter) Add(data []byte) {
|
||||||
bf.Lock()
|
bf.mtx.Lock()
|
||||||
defer bf.Unlock()
|
|
||||||
|
|
||||||
bf.add(data)
|
bf.add(data)
|
||||||
|
bf.mtx.Unlock()
|
||||||
}
|
}
|
||||||
|
|
||||||
// AddShaHash adds the passed btcwire.ShaHash to the Filter.
|
// AddShaHash adds the passed btcwire.ShaHash to the Filter.
|
||||||
//
|
//
|
||||||
// This function is safe for concurrent access.
|
// This function is safe for concurrent access.
|
||||||
func (bf *Filter) AddShaHash(sha *btcwire.ShaHash) {
|
func (bf *Filter) AddShaHash(sha *btcwire.ShaHash) {
|
||||||
bf.Lock()
|
bf.mtx.Lock()
|
||||||
defer bf.Unlock()
|
|
||||||
|
|
||||||
bf.add(sha.Bytes())
|
bf.add(sha.Bytes())
|
||||||
|
bf.mtx.Unlock()
|
||||||
}
|
}
|
||||||
|
|
||||||
// addOutPoint adds the passed transaction outpoint to the bloom filter.
|
// addOutPoint adds the passed transaction outpoint to the bloom filter.
|
||||||
|
@ -243,10 +239,9 @@ func (bf *Filter) addOutPoint(outpoint *btcwire.OutPoint) {
|
||||||
//
|
//
|
||||||
// This function is safe for concurrent access.
|
// This function is safe for concurrent access.
|
||||||
func (bf *Filter) AddOutPoint(outpoint *btcwire.OutPoint) {
|
func (bf *Filter) AddOutPoint(outpoint *btcwire.OutPoint) {
|
||||||
bf.Lock()
|
bf.mtx.Lock()
|
||||||
defer bf.Unlock()
|
|
||||||
|
|
||||||
bf.addOutPoint(outpoint)
|
bf.addOutPoint(outpoint)
|
||||||
|
bf.mtx.Unlock()
|
||||||
}
|
}
|
||||||
|
|
||||||
// maybeAddOutpoint potentially adds the passed outpoint to the bloom filter
|
// maybeAddOutpoint potentially adds the passed outpoint to the bloom filter
|
||||||
|
@ -340,10 +335,10 @@ func (bf *Filter) matchTxAndUpdate(tx *btcutil.Tx) bool {
|
||||||
//
|
//
|
||||||
// This function is safe for concurrent access.
|
// This function is safe for concurrent access.
|
||||||
func (bf *Filter) MatchTxAndUpdate(tx *btcutil.Tx) bool {
|
func (bf *Filter) MatchTxAndUpdate(tx *btcutil.Tx) bool {
|
||||||
bf.Lock()
|
bf.mtx.Lock()
|
||||||
defer bf.Unlock()
|
match := bf.matchTxAndUpdate(tx)
|
||||||
|
bf.mtx.Unlock()
|
||||||
return bf.matchTxAndUpdate(tx)
|
return match
|
||||||
}
|
}
|
||||||
|
|
||||||
// MsgFilterLoad returns the underlying btcwire.MsgFilterLoad for the bloom
|
// MsgFilterLoad returns the underlying btcwire.MsgFilterLoad for the bloom
|
||||||
|
@ -351,8 +346,8 @@ func (bf *Filter) MatchTxAndUpdate(tx *btcutil.Tx) bool {
|
||||||
//
|
//
|
||||||
// This function is safe for concurrent access.
|
// This function is safe for concurrent access.
|
||||||
func (bf *Filter) MsgFilterLoad() *btcwire.MsgFilterLoad {
|
func (bf *Filter) MsgFilterLoad() *btcwire.MsgFilterLoad {
|
||||||
bf.Lock()
|
bf.mtx.Lock()
|
||||||
defer bf.Unlock()
|
msg := bf.msgFilterLoad
|
||||||
|
bf.mtx.Unlock()
|
||||||
return bf.msgFilterLoad
|
return msg
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue