Skip to content

Commit

Permalink
添加了中文注释方便初学者学习
Browse files Browse the repository at this point in the history
  • Loading branch information
wangxiao1024 committed Jul 23, 2024
1 parent 42a2bc4 commit b9ba5db
Showing 1 changed file with 45 additions and 0 deletions.
45 changes: 45 additions & 0 deletions datastruct/dict/concurrent.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import (
"sync"
"sync/atomic"
"time"

"github.com/hdt3213/godis/lib/wildcard"
)

// ConcurrentDict 是一个使用分片锁的线程安全的哈希表
Expand Down Expand Up @@ -457,3 +459,46 @@ func (dict *ConcurrentDict) RWUnLocks(writeKeys []string, readKeys []string) {
}
}
}
func stringsToBytes(strSlice []string) [][]byte {
byteSlice := make([][]byte, len(strSlice))
for i, str := range strSlice {
byteSlice[i] = []byte(str)
}
return byteSlice
}

func (dict *ConcurrentDict) DictScan(cursor int, count int, pattern string) ([][]byte, int) {
size := dict.Len()
result := make([][]byte, 0)

if pattern == "*" && count >= size {
return stringsToBytes(dict.Keys()), 0
}

matchKey, err := wildcard.CompilePattern(pattern)
if err != nil {
return result, -1
}

shardCount := len(dict.table)
shardIndex := cursor

for shardIndex < shardCount {
shard := dict.table[shardIndex]
shard.mutex.RLock()
if len(result)+len(shard.m) > count && shardIndex > cursor {
shard.mutex.RUnlock()
return result, shardIndex
}

for key := range shard.m {
if pattern == "*" || matchKey.IsMatch(key) {
result = append(result, []byte(key))
}
}
shard.mutex.RUnlock()
shardIndex++
}

return result, 0
}

0 comments on commit b9ba5db

Please sign in to comment.