aboutsummaryrefslogtreecommitdiff
path: root/pkg/commands/keys.go
blob: b23396496a7b142d73d617d661f237a7d07d21b1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
package commands

import (
	"errors"
	"path/filepath"
	"redisClone/pkg/core"
	"sort"
	"strconv"
)

func Keys(args []string, getShard func(k string) *core.Shard) interface{} {
	pattern := args[1]
	limit := -1
	if len(args) == 3 {
		val, err := strconv.Atoi(args[2])
		if err != nil {
			return errors.New("value is not an integer")
		}
		limit = val
	}

	return dbInstance.ExecuteReadAll(func(shards []*core.Shard) interface{} {
		var matches []string
		for _, shard := range shards {
			for key := range shard.Data {
				matched, err := filepath.Match(pattern, key)
				if err != nil {
					return errors.New("illegal glob pattern")
				}
				if matched {
					matches = append(matches, key)
				}
			}
		}
		sort.Strings(matches)
		if limit != -1 && len(matches) > limit {
			matches = matches[:limit]
		}
		return matches
	})
}