forked from distributedio/titan
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconstraint.go
146 lines (141 loc) · 3.44 KB
/
constraint.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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
package command
// Constraint is the rule of command
type Constraint struct {
Arity int // number of arguments, it is possible to use -N to say >= N
Flags Flag
FirstKey int
LastKey int
KeyStep int
}
// Flag is the redis command flag
type Flag int
// Command flags
const (
CmdWrite Flag = 1 << iota
CmdReadOnly
CmdDenyOOM
CmdModule
CmdAdmin
CmdPubsub
CmdNoScript
CmdRandom
CmdSortForScript
CmdLoading
CmdStale
CmdSkipMonitor
CmdAsking
CmdFast
CmdModuleGetKeys
CmdModuleNoCluster
)
// String returns the string representation of flag
func (f Flag) String() string {
switch f {
case CmdWrite:
return "write"
case CmdReadOnly:
return "readonly"
case CmdDenyOOM:
return "denyoom"
case CmdModule:
return "module"
case CmdAdmin:
return "admin"
case CmdPubsub:
return "pubsub"
case CmdNoScript:
return "noscript"
case CmdRandom:
return "random"
case CmdSortForScript:
return "sort_for_script"
case CmdLoading:
return "loading"
case CmdStale:
return "stale"
case CmdSkipMonitor:
return "skip_monitor"
case CmdAsking:
return "asking"
case CmdFast:
return "fast"
case CmdModuleGetKeys:
return "module_getkeys"
case CmdModuleNoCluster:
return "module_no_cluster"
}
return ""
}
// flags parse sflags to flags
// This is the meaning of the flags:
//
// w: write command (may modify the key space).
// r: read command (will never modify the key space).
// m: may increase memory usage once called. Don't allow if out of memory.
// a: admin command, like SAVE or SHUTDOWN.
// p: Pub/Sub related command.
// f: force replication of this command, regardless of server.dirty.
// s: command not allowed in scripts.
// R: random command. Command is not deterministic, that is, the same command
// with the same arguments, with the same key space, may have different
// results. For instance SPOP and RANDOMKEY are two random commands.
// S: Sort command output array if called from script, so that the output
// is deterministic.
// l: Allow command while loading the database.
// t: Allow command while a slave has stale data but is not allowed to
// server this data. Normally no command is accepted in this condition
// but just a few.
// M: Do not automatically propagate the command on MONITOR.
// k: Perform an implicit ASKING for this command, so the command will be
// accepted in cluster mode if the slot is marked as 'importing'.
// F: Fast command: O(1) or O(log(N)) command that should never delay
// its execution as long as the kernel scheduler is giving us time.
// Note that commands that may trigger a DEL as a side effect (like SET)
// are not fast commands.
func flags(s string) Flag {
flags := Flag(0)
for i := 0; i < len(s); i++ {
switch s[i] {
case 'w':
flags |= CmdWrite
case 'r':
flags |= CmdReadOnly
case 'm':
flags |= CmdDenyOOM
case 'a':
flags |= CmdAdmin
case 'p':
flags |= CmdPubsub
case 's':
flags |= CmdNoScript
case 'R':
flags |= CmdRandom
case 'S':
flags |= CmdSortForScript
case 'l':
flags |= CmdLoading
case 't':
flags |= CmdStale
case 'M':
flags |= CmdSkipMonitor
case 'k':
flags |= CmdAsking
case 'F':
flags |= CmdFast
default:
panic("Unsupported command flag")
}
}
return flags
}
func parseFlags(flags Flag) []string {
var s []string
// we have total 16 flags now
for i := uint(0); i < 16; i++ {
f := Flag(1 << i)
if f&flags != 0 {
s = append(s, f.String())
}
}
return s
}