Add DeleteFunc

This shares DeletePrefixs's implementation.
This commit is contained in:
Bjørn Erik Pedersen
2020-08-12 17:30:28 +02:00
parent 223703f7f0
commit d56665a86e
6 changed files with 90 additions and 2 deletions

View File

@@ -54,13 +54,13 @@ func (b *bucket) delete(key string) *Item {
// the item from the map. I'm pretty sure this is 100% fine, but it is unique.
// (We do this so that the write to the channel is under the read lock and not the
// write lock)
func (b *bucket) deletePrefix(prefix string, deletables chan *Item) int {
func (b *bucket) deleteFunc(matches func(key string, item interface{}) bool, deletables chan *Item) int {
lookup := b.lookup
items := make([]*Item, 0, len(lookup)/10)
b.RLock()
for key, item := range lookup {
if strings.HasPrefix(key, prefix) {
if matches(key, item) {
deletables <- item
items = append(items, item)
}
@@ -80,6 +80,12 @@ func (b *bucket) deletePrefix(prefix string, deletables chan *Item) int {
return len(items)
}
func (b *bucket) deletePrefix(prefix string, deletables chan *Item) int {
return b.deleteFunc(func(key string, item interface{}) bool {
return strings.HasPrefix(key, prefix)
}, deletables)
}
func (b *bucket) clear() {
b.Lock()
b.lookup = make(map[string]*Item)