-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathsort.go
93 lines (83 loc) · 2.08 KB
/
sort.go
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
package goredis
// SortCommand represents a redis Sort command
type SortCommand struct {
redis *Redis
key string
by string
limit bool
offset int
count int
get []string
order string
alpha bool
store string
}
// Sort doc: http://redis.io/commands/sort
// SORT key [BY pattern] [LIMIT offset count] [GET pattern [GET pattern ...]] [ASC|DESC] [ALPHA] [STORE destination]
func (r *Redis) Sort(key string) *SortCommand {
return &SortCommand{redis: r, key: key}
}
// By can also take a non-existent key, which causes SORT to skip the sorting operation.
func (s *SortCommand) By(pattern string) *SortCommand {
s.by = pattern
return s
}
// Limit takes the offset and count argument,
// specifying the number of elements to skip and the count argument,
// specifying the number of elements to return from starting at offset.
func (s *SortCommand) Limit(offset, count int) *SortCommand {
s.limit = true
s.offset = offset
s.count = count
return s
}
// Get sets sort GET arguments.
func (s *SortCommand) Get(patterns ...string) *SortCommand {
s.get = patterns
return s
}
// ASC sets sort order to ASC.
func (s *SortCommand) ASC() *SortCommand {
s.order = "ASC"
return s
}
// DESC sets sort order to DESC.
func (s *SortCommand) DESC() *SortCommand {
s.order = "DESC"
return s
}
// Alpha sets ALPHA to sort command.
func (s *SortCommand) Alpha(b bool) *SortCommand {
s.alpha = b
return s
}
// Store sets the sorted result store to.
func (s *SortCommand) Store(destination string) *SortCommand {
s.store = destination
return s
}
// Run performs redis sort command.
func (s *SortCommand) Run() (*Reply, error) {
args := packArgs("SORT", s.key)
if s.by != "" {
args = append(args, "BY", s.by)
}
if s.limit {
args = append(args, "LIMIT", s.offset, s.count)
}
if s.get != nil && len(s.get) > 0 {
for _, pattern := range s.get {
args = append(args, "GET", pattern)
}
}
if s.order != "" {
args = append(args, s.order)
}
if s.alpha {
args = append(args, "ALPHA")
}
if s.store != "" {
args = append(args, "STORE", s.store)
}
return s.redis.ExecuteCommand(args...)
}