Skip to content

Commit

Permalink
feat(map): added pattern matching support for deletion
Browse files Browse the repository at this point in the history
  • Loading branch information
HotPotatoC committed Dec 23, 2021
1 parent d13d602 commit 2fcbfc0
Showing 1 changed file with 44 additions and 7 deletions.
51 changes: 44 additions & 7 deletions datastructure/map.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ func (m *Map) Get(k string) (*Item, bool) {
}

if v.(*Item).HasFlag(ItemFlagExpireXX) && time.Now().After(v.(*Item).ExpiresAt) {
m.items.Delete(k)
m.delete(k)
atomic.AddInt64(&m.nSize, -1)
return nil, false
}
Expand All @@ -65,18 +65,55 @@ func (m *Map) Get(k string) (*Item, bool) {

// Delete deletes the key.
func (m *Map) Delete(k string) int64 {
if _, ok := m.items.Load(k); !ok {
return 0
}
m.items.Delete(k)

prevNSize := atomic.LoadInt64(&m.nSize)
atomic.AddInt64(&m.nSize, -1)
deletedN := m.delete(k)
atomic.AddInt64(&m.nSize, -deletedN)

// return the deleted amount
return prevNSize - atomic.LoadInt64(&m.nSize)
}

func (m *Map) delete(k string) int64 {
deletedN := int64(0)

_, loaded := m.items.LoadAndDelete(k)
if !loaded {
// If the given key was not loaded, attempt to check
// if it's a glob pattern or not

// First, check if the pattern is valid or not
_, err := filepath.Match(k, "")
if err != nil {
return 0
}

isGlobPattern := false
for i := 0; i < len(k); i++ {
if k[i] == '*' || k[i] == '?' || k[i] == '[' {
isGlobPattern = true
break
}
}

if !isGlobPattern {
return 0
}

// Search and delete each key that satisfies the pattern O(n)
m.items.Range(func(key, value interface{}) bool {
if match, _ := filepath.Match(k, key.(string)); match {
m.items.Delete(key)
deletedN++
}
return true
})
} else {
deletedN++
}

return deletedN
}

// Len returns the number of items in the map.
func (m *Map) Len() int64 {
return atomic.LoadInt64(&m.nSize)
Expand Down

0 comments on commit 2fcbfc0

Please sign in to comment.