-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmetacmd_flags.go
358 lines (306 loc) · 7.41 KB
/
metacmd_flags.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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
package memdproto
import (
"bytes"
"fmt"
"io"
)
func writeFlags(dst io.Writer, flags ...io.WriterTo) (int64, error) {
var written int64
for _, f := range flags {
// we would like to insert a space before each flag, but not unless
// there's actually something to write. So write the contents of the
// flag into a scratch buffer, and then write the space and the contents
var buf bytes.Buffer
n64, err := f.WriteTo(&buf)
written += n64
if err != nil {
return written, err
}
if n64 > 0 {
n, err := dst.Write(space)
written += int64(n)
if err != nil {
return written, err
}
}
buf.WriteTo(dst)
}
return written, nil
}
// FlagKeyAsBase64 is a flag used in Meta Commands to indicate
// if the key used should be treated as a base64 encoded string
type FlagKeyAsBase64 struct{}
func (f *FlagKeyAsBase64) WriteTo(dst io.Writer) (int64, error) {
if f != nil {
n, err := dst.Write([]byte{'b'})
return int64(n), err
}
return 0, nil
}
// FlagRetrieveCas is a flag used in Meta Commands for operations
// regarding retrieval of a CAS value for an item.
//
// When used in a request, it indicates that the client wants to
// retrieve the CAS value for the item. When used in a response,
// it is suffixed with the actual CAS value
type FlagRetrieveCas uint64
func (f *FlagRetrieveCas) WriteTo(dst io.Writer) (int64, error) {
if f == nil {
return 0, nil
}
if *f == 0 {
n, err := fmt.Fprintf(dst, "c")
return int64(n), err
}
n, err := fmt.Fprintf(dst, "c%d", *f)
return int64(n), err
}
// FlagRetrieveClientFlags is a flag used in Meta Commands for
// operations regarding retrieval of client flags for an item.
//
// When used in a request, it indicates that the client wants to
// retrieve the client flags for the item. When used in a response,
// it is suffixed with the actual client flags
type FlagRetrieveClientFlags uint32
func (f *FlagRetrieveClientFlags) WriteTo(dst io.Writer) (int64, error) {
if f == nil {
return 0, nil
}
if *f == 0 {
n, err := fmt.Fprintf(dst, "f")
return int64(n), err
}
n, err := fmt.Fprintf(dst, "f%d", *f)
return int64(n), err
}
// FlagRetrieveExpiry is a flag used in Meta Commands for operations
// to retrieve if the item has been hit before this action.
//
// When used in a request, it indicates that the client wants to
// retrieve the "has been hit?" value for the item. When used in
// a response, it is suffixed with either a 0 or 1 to indicate the
// "has been hit?" value.
type FlagRetrievePreviousHit struct {
hit *uint8
}
func (f *FlagRetrievePreviousHit) WriteTo(dst io.Writer) (int64, error) {
if f == nil {
return 0, nil
}
if f.hit == nil {
n, err := fmt.Fprintf(dst, "h")
return int64(n), err
}
n, err := fmt.Fprintf(dst, "h%d", *f.hit)
return int64(n), err
}
type FlagRetrieveKey struct {
key *string
}
func (f *FlagRetrieveKey) WriteTo(dst io.Writer) (int64, error) {
if f == nil {
return 0, nil
}
if f.key == nil {
n, err := fmt.Fprintf(dst, "k")
return int64(n), err
}
n, err := fmt.Fprintf(dst, "k%s", *f.key)
return int64(n), err
}
type FlagRetrieveTimeSinceLastAccess struct {
value *uint64
}
func (f *FlagRetrieveTimeSinceLastAccess) WriteTo(dst io.Writer) (int64, error) {
if f == nil {
return 0, nil
}
if f.value == nil {
n, err := fmt.Fprintf(dst, "l")
return int64(n), err
}
n, err := fmt.Fprintf(dst, "l%d", *f.value)
return int64(n), err
}
type FlagVivifyOnMiss uint64
func (f *FlagVivifyOnMiss) WriteTo(dst io.Writer) (int64, error) {
if f == nil {
return 0, nil
}
n, err := fmt.Fprintf(dst, "N%d", *f)
return int64(n), err
}
// FlagOpague is a flag used in Meta Commands to send and receive
// opaque value
type FlagOpaque []byte
func (f FlagOpaque) WriteTo(dst io.Writer) (int64, error) {
if len(f) == 0 {
return 0, nil
}
if len(f) > 32 {
return 0, fmt.Errorf("opaque value too long")
}
n, err := fmt.Fprintf(dst, "O%s", f)
return int64(n), err
}
type FlagNoReply struct{}
func (f *FlagNoReply) WriteTo(dst io.Writer) (int64, error) {
if f == nil {
return 0, nil
}
n, err := fmt.Fprintf(dst, "q")
return int64(n), err
}
type FlagRecache uint64
func (f *FlagRecache) WriteTo(dst io.Writer) (int64, error) {
if f == nil {
return 0, nil
}
n, err := fmt.Fprintf(dst, "R%d", *f)
return int64(n), err
}
type FlagRetrieveSize struct {
value *uint64
}
func (f *FlagRetrieveSize) WriteTo(dst io.Writer) (int64, error) {
if f == nil {
return 0, nil
}
if f.value == nil {
n, err := fmt.Fprintf(dst, "s")
return int64(n), err
}
n, err := fmt.Fprintf(dst, "s%d", *f.value)
return int64(n), err
}
type FlagRetrieveRemainingTTL struct {
value *int64
}
func (f *FlagRetrieveRemainingTTL) WriteTo(dst io.Writer) (int64, error) {
if f == nil {
return 0, nil
}
if f.value == nil {
n, err := fmt.Fprintf(dst, "t")
return int64(n), err
}
n, err := fmt.Fprintf(dst, "t%d", *f.value)
return int64(n), err
}
type FlagUpdateTTL int64
func (f *FlagUpdateTTL) WriteTo(dst io.Writer) (int64, error) {
if f == nil {
return 0, nil
}
n, err := fmt.Fprintf(dst, "T%d", *f)
return int64(n), err
}
type FlagSkipLRUBump struct{}
func (f *FlagSkipLRUBump) WriteTo(dst io.Writer) (int64, error) {
if f == nil {
return 0, nil
}
n, err := fmt.Fprintf(dst, "u")
return int64(n), err
}
type FlagRetrieveValue struct{}
func (f *FlagRetrieveValue) WriteTo(dst io.Writer) (int64, error) {
if f == nil {
return 0, nil
}
n, err := fmt.Fprintf(dst, "v")
return int64(n), err
}
type FlagStale struct{}
func (f *FlagStale) WriteTo(dst io.Writer) (int64, error) {
if f == nil {
return 0, nil
}
n, err := fmt.Fprintf(dst, "X")
return int64(n), err
}
type FlagDontRecache struct{}
func (f *FlagDontRecache) WriteTo(dst io.Writer) (int64, error) {
if f == nil {
return 0, nil
}
n, err := fmt.Fprintf(dst, "Z")
return int64(n), err
}
type FlagRecacheResult struct {
won bool
}
func (f *FlagRecacheResult) WriteTo(dst io.Writer) (int64, error) {
if f == nil {
return 0, nil
}
if f.won {
n, err := fmt.Fprintf(dst, "W")
return int64(n), err
}
n, err := fmt.Fprintf(dst, "Z")
return int64(n), err
}
type MetaSetMode uint8
const (
MetaSetModeSet MetaSetMode = iota
MetaSetModeAdd
MetaSetModeAppend
MetaSetModePrepend
MetaSetModeReplace
MetaSetModeMax
)
func (m *MetaSetMode) WriteTo(dst io.Writer) (int64, error) {
if m == nil {
return 0, nil
}
flag := []byte{'M'}
switch *m {
case MetaSetModeSet:
flag = append(flag, 'S')
case MetaSetModeAdd:
flag = append(flag, 'E')
case MetaSetModeAppend:
flag = append(flag, 'A')
case MetaSetModePrepend:
flag = append(flag, 'P')
case MetaSetModeReplace:
flag = append(flag, 'R')
default:
return 0, fmt.Errorf("invalid MetaSetMode")
}
n, err := dst.Write(flag)
return int64(n), err
}
type FlagCompareCas uint64
func (f *FlagCompareCas) WriteTo(dst io.Writer) (int64, error) {
if f == nil {
return 0, nil
}
n, err := fmt.Fprintf(dst, "C%d", *f)
return int64(n), err
}
type FlagSetClientFlags uint32
func (f *FlagSetClientFlags) WriteTo(dst io.Writer) (int64, error) {
if f == nil {
return 0, nil
}
n, err := fmt.Fprintf(dst, "F%d", *f)
return int64(n), err
}
type FlagInvalidateOnOldCas struct{}
func (f *FlagInvalidateOnOldCas) WriteTo(dst io.Writer) (int64, error) {
if f == nil {
return 0, nil
}
n, err := dst.Write([]byte{'I'})
return int64(n), err
}
type FlagSetTTL int64
func (f *FlagSetTTL) WriteTo(dst io.Writer) (int64, error) {
if f == nil {
return 0, nil
}
n, err := fmt.Fprintf(dst, "T%d", *f)
return int64(n), err
}