mirror of
https://github.com/HDT3213/godis.git
synced 2025-10-06 01:07:06 +08:00
add scan command
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
package dict
|
||||
|
||||
import (
|
||||
"github.com/hdt3213/godis/lib/wildcard"
|
||||
"math"
|
||||
"math/rand"
|
||||
"sort"
|
||||
@@ -435,3 +436,47 @@ 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
|
||||
}
|
||||
|
Reference in New Issue
Block a user