Skip to content

Commit 6199a2a

Browse files
chayimofekshenawa
andauthored
Making command structs digestable (redis#2716)
* intial move * adding stringcmdable * moving module commands to align with other changes --------- Co-authored-by: ofekshenawa <[email protected]> Co-authored-by: ofekshenawa <[email protected]>
1 parent e07f7e6 commit 6199a2a

24 files changed

+3650
-3559
lines changed

.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,5 @@
22
testdata/*
33
.idea/
44
.DS_Store
5+
*.tar.gz
6+
*.dic

Makefile

+3
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@ bench: testdeps
2121

2222
.PHONY: all test testdeps bench fmt
2323

24+
build:
25+
go build .
26+
2427
testdata/redis:
2528
mkdir -p $@
2629
wget -qO- https://download.redis.io/releases/redis-7.2.1.tar.gz | tar xvz --strip-components=1 -C $@

acl_commands.go

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package redis
2+
3+
import "context"
4+
5+
type ACLCmdable interface {
6+
ACLDryRun(ctx context.Context, username string, command ...interface{}) *StringCmd
7+
ACLLog(ctx context.Context, count int64) *ACLLogCmd
8+
ACLLogReset(ctx context.Context) *StatusCmd
9+
}
10+
11+
func (c cmdable) ACLDryRun(ctx context.Context, username string, command ...interface{}) *StringCmd {
12+
args := make([]interface{}, 0, 3+len(command))
13+
args = append(args, "acl", "dryrun", username)
14+
args = append(args, command...)
15+
cmd := NewStringCmd(ctx, args...)
16+
_ = c(ctx, cmd)
17+
return cmd
18+
}
19+
20+
func (c cmdable) ACLLog(ctx context.Context, count int64) *ACLLogCmd {
21+
args := make([]interface{}, 0, 3)
22+
args = append(args, "acl", "log")
23+
if count > 0 {
24+
args = append(args, count)
25+
}
26+
cmd := NewACLLogCmd(ctx, args...)
27+
_ = c(ctx, cmd)
28+
return cmd
29+
}
30+
31+
func (c cmdable) ACLLogReset(ctx context.Context) *StatusCmd {
32+
cmd := NewStatusCmd(ctx, "acl", "log", "reset")
33+
_ = c(ctx, cmd)
34+
return cmd
35+
}

bitmap_commands.go

+129
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
package redis
2+
3+
import "context"
4+
5+
type BitMapCmdable interface {
6+
GetBit(ctx context.Context, key string, offset int64) *IntCmd
7+
SetBit(ctx context.Context, key string, offset int64, value int) *IntCmd
8+
BitCount(ctx context.Context, key string, bitCount *BitCount) *IntCmd
9+
BitOpAnd(ctx context.Context, destKey string, keys ...string) *IntCmd
10+
BitOpOr(ctx context.Context, destKey string, keys ...string) *IntCmd
11+
BitOpXor(ctx context.Context, destKey string, keys ...string) *IntCmd
12+
BitOpNot(ctx context.Context, destKey string, key string) *IntCmd
13+
BitPos(ctx context.Context, key string, bit int64, pos ...int64) *IntCmd
14+
BitPosSpan(ctx context.Context, key string, bit int8, start, end int64, span string) *IntCmd
15+
BitField(ctx context.Context, key string, values ...interface{}) *IntSliceCmd
16+
}
17+
18+
func (c cmdable) GetBit(ctx context.Context, key string, offset int64) *IntCmd {
19+
cmd := NewIntCmd(ctx, "getbit", key, offset)
20+
_ = c(ctx, cmd)
21+
return cmd
22+
}
23+
24+
func (c cmdable) SetBit(ctx context.Context, key string, offset int64, value int) *IntCmd {
25+
cmd := NewIntCmd(
26+
ctx,
27+
"setbit",
28+
key,
29+
offset,
30+
value,
31+
)
32+
_ = c(ctx, cmd)
33+
return cmd
34+
}
35+
36+
type BitCount struct {
37+
Start, End int64
38+
}
39+
40+
func (c cmdable) BitCount(ctx context.Context, key string, bitCount *BitCount) *IntCmd {
41+
args := []interface{}{"bitcount", key}
42+
if bitCount != nil {
43+
args = append(
44+
args,
45+
bitCount.Start,
46+
bitCount.End,
47+
)
48+
}
49+
cmd := NewIntCmd(ctx, args...)
50+
_ = c(ctx, cmd)
51+
return cmd
52+
}
53+
54+
func (c cmdable) bitOp(ctx context.Context, op, destKey string, keys ...string) *IntCmd {
55+
args := make([]interface{}, 3+len(keys))
56+
args[0] = "bitop"
57+
args[1] = op
58+
args[2] = destKey
59+
for i, key := range keys {
60+
args[3+i] = key
61+
}
62+
cmd := NewIntCmd(ctx, args...)
63+
_ = c(ctx, cmd)
64+
return cmd
65+
}
66+
67+
func (c cmdable) BitOpAnd(ctx context.Context, destKey string, keys ...string) *IntCmd {
68+
return c.bitOp(ctx, "and", destKey, keys...)
69+
}
70+
71+
func (c cmdable) BitOpOr(ctx context.Context, destKey string, keys ...string) *IntCmd {
72+
return c.bitOp(ctx, "or", destKey, keys...)
73+
}
74+
75+
func (c cmdable) BitOpXor(ctx context.Context, destKey string, keys ...string) *IntCmd {
76+
return c.bitOp(ctx, "xor", destKey, keys...)
77+
}
78+
79+
func (c cmdable) BitOpNot(ctx context.Context, destKey string, key string) *IntCmd {
80+
return c.bitOp(ctx, "not", destKey, key)
81+
}
82+
83+
// BitPos is an API before Redis version 7.0, cmd: bitpos key bit start end
84+
// if you need the `byte | bit` parameter, please use `BitPosSpan`.
85+
func (c cmdable) BitPos(ctx context.Context, key string, bit int64, pos ...int64) *IntCmd {
86+
args := make([]interface{}, 3+len(pos))
87+
args[0] = "bitpos"
88+
args[1] = key
89+
args[2] = bit
90+
switch len(pos) {
91+
case 0:
92+
case 1:
93+
args[3] = pos[0]
94+
case 2:
95+
args[3] = pos[0]
96+
args[4] = pos[1]
97+
default:
98+
panic("too many arguments")
99+
}
100+
cmd := NewIntCmd(ctx, args...)
101+
_ = c(ctx, cmd)
102+
return cmd
103+
}
104+
105+
// BitPosSpan supports the `byte | bit` parameters in redis version 7.0,
106+
// the bitpos command defaults to using byte type for the `start-end` range,
107+
// which means it counts in bytes from start to end. you can set the value
108+
// of "span" to determine the type of `start-end`.
109+
// span = "bit", cmd: bitpos key bit start end bit
110+
// span = "byte", cmd: bitpos key bit start end byte
111+
func (c cmdable) BitPosSpan(ctx context.Context, key string, bit int8, start, end int64, span string) *IntCmd {
112+
cmd := NewIntCmd(ctx, "bitpos", key, bit, start, end, span)
113+
_ = c(ctx, cmd)
114+
return cmd
115+
}
116+
117+
// BitField accepts multiple values:
118+
// - BitField("set", "i1", "offset1", "value1","cmd2", "type2", "offset2", "value2")
119+
// - BitField([]string{"cmd1", "type1", "offset1", "value1","cmd2", "type2", "offset2", "value2"})
120+
// - BitField([]interface{}{"cmd1", "type1", "offset1", "value1","cmd2", "type2", "offset2", "value2"})
121+
func (c cmdable) BitField(ctx context.Context, key string, values ...interface{}) *IntSliceCmd {
122+
args := make([]interface{}, 2, 2+len(values))
123+
args[0] = "bitfield"
124+
args[1] = key
125+
args = appendArgs(args, values)
126+
cmd := NewIntSliceCmd(ctx, args...)
127+
_ = c(ctx, cmd)
128+
return cmd
129+
}

0 commit comments

Comments
 (0)