dustin (owner)

Revisions

gist: 119503 Download_button fork
public
Description:
Memcached Range Command Specification
Public Clone URL: git://gist.github.com/119503.git
Embed All Files: show embed
range.txt #
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
                Memcached Range Command Specification
                =====================================
 
Author: Dustin Sallings <dustin@spy.net>
Date: 2009-05-13 Wed
 
 
Table of Contents
=================
1 Overview
2 General Properties of Range Commands
    2.1 Abstract Representation of Ranges
    2.2 Binary Protocol Generalities
    2.3 Text Protocol Generalities
3 Supported commands
    3.1 get
    3.2 set
    3.3 append
    3.4 prepend
    3.5 delete
    3.6 incr
    3.7 decr
    3.8 replace (omitted)
4 Command Reference
5 Isolation
 
 
1 Overview
~~~~~~~~~~~
 
Several implementations of servers speaking the memcached protocol
have backends that allow for inexpensive operations over ranges of
keys.
 
The purpose of this document is to standardize the protocol for making
use of these operations.
 
2 General Properties of Range Commands
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 
Commands that take ranges all follow the same pattern of key
representation.
 
Each range representation will have a minimum and maximum range as
well as flags to indicate whether each end is inclusive or exclusive.
 
Either end may be null indicating there's no limit on the side where
the null appears.
 
2.1 Abstract Representation of Ranges
======================================
 
This document uses standard [interval notation], but you can see the
possibilities listed here:
 
- (null, null) :: Operate over all key.
- (null, "X") :: Operate over every key up to (not including) "X".
- (null, "X"] :: Operate over every key up to (and including) "X".
- ("F", null) :: Operate over every key from (not including) "F".
- ("F", "X") :: Operate over every key from "F" to "X" (not including
                either).
- ("F", "X"] :: Operate over every key from "F" to "X" (excluding "F",
                including "X").
- ["F", "X") :: Operate over every key from "F" to "X" (including "F",
                excluding "X").
- ["F", "X"] :: Operate over every key from "F" to "X" (including both).
 
2.2 Binary Protocol Generalities
=================================
 
2.2.1 Requests
---------------
 
In the binary protocol, we've got some extra space and can apply that
to defining ranges.
 
The "key" field will be used as the start key. A key length of zero
represents null as an empty key can't be stored.
 
The extra info will contain the following info:
 
  Fields Size
 -------------+------
  End key len 16
  Reserved 8
  Flags 8
  Max results 32
 
Flags is a bitmask indicating whether the ends are inclusive or
exclusive.
 
- (flags & 1) :: If non-zero, start key is inclusive.
- (flags & 2) :: If non-zero, end key is inclusive.
 
Max results represents the number of objects the operation is
permitted to affect. A value of zero indicates there should be no
limit.
 
A server *may* enforce a maximum limit and respond with an error if
the requested limit exceeds the limit enforced by the server (or is
zero).
 
The end key immediately follows the maximum results.
 
2.2.2 Responses
----------------
 
Each requests will generally have many responses and will return
similarly to the way stats works in the binary protocol. The final
response will be one with an empty key.
 
The binary protocol allows for commands to operate in a "quiet mode"
where most responses are quelled. No such facility is available for
the text protocol as it can't be done consistently without introducing
new verbs (and then can't be done safely).
 
2.3 Text Protocol Generalities
===============================
 
The general form of a range command is as follows (please see details
for how this varies by command):
 
CMD <start inclusion> <end inclusion> <max items> <start key> [end key]\r\n
 
The end key is optional. If not specified, it is considered null.
Null is not explicitly included for the start key as there is a
natural minimum of keys which can be used in its place.
 
The inclusion flags are either 0 or 1. If 0, the endpoint is not
included. If 1, the endpoint is included.
 
This *is* different from the rget as specified in memcachedb to allow
for infinite ranges.
 
3 Supported commands
~~~~~~~~~~~~~~~~~~~~~
 
3.1 get
========
 
Ranged get operations allow multiple key/value pairs returned for a
single key request.
 
3.1.1 Binary Protocol Details
------------------------------
 
The command ID for binary get is 0x30.
 
3.1.2 Text Protocol Details
----------------------------
 
The command for text get is rget.
 
3.2 set
========
 
Ranged set operations allow for a range of *existing* items to be set
to a specific value.
 
For example, if you have stats stored in a server with keys prefixed
as "stats." you can reset all of them in a single operation by setting
("stats.", "stats/") to "0". This range specifies everything that
begins with "stats." excluding "stats." itself.
 
Upon batch mutation, each modified value will receive a new distinct
CAS identifier.
 
3.2.1 Binary Protocol Details
------------------------------
 
The command ID for binary get is 0x31. The command ID for a binary
quiet get is 0x32.
 
Each modified value will respond as a plain set does although the key
will be included in the response. The final response will contain no
key.
 
In the case of a quiet request, no response will be given unless the
command fails.
 
3.2.2 Text Protocol Details
----------------------------
 
The command for text set is rset and has the following form:
 
rset <start inclusion> <end inclusion> <max items> <flags> <exptime> <bytes> <start key> [end key]\r\n
<data>\r\n
 
The response will be similar to a gets, but with no values included.
Example:
 
VALUE <key> <flags> 0 [<cas>]\r\n
\r\n
 
3.3 append
===========
 
Ranged append will append a value to every item within a range.
 
For both protocols, the response will be in the form of their
respective protocol-specific response in ranged set.
 
3.3.1 Binary Protocol Details
------------------------------
 
The command ID for binary range append is 0x33. The command ID for a
silent append is 0x34.
 
3.3.2 Text Protocol Details
----------------------------
 
The command for text append has the following orm:
 
rappend <start inclusion> <end inclusion> <max items> <bytes> <start key> [end key]\r\n
<data>\r\n
 
3.4 prepend
============
 
Prepend follows the same pattern of append.
 
3.4.1 Binary Protocol Details
------------------------------
 
The command ID for a binary range prepend is 0x35. The command ID for
a silent prepend is 0x36.
 
3.4.2 Text Protocol Details
----------------------------
 
The command for a text range prepend is "rprepend".
 
3.5 delete
===========
 
Ranged delete will remove all values in the specified range.
 
For both protocols, the response will be in the form of their
respective protocol-specific response in ranged set.
 
3.5.1 Binary Protocol Details
------------------------------
 
The command ID for a binary range delete is 0x37. The command ID for
a silent delete is 0x38.
 
3.5.2 Text Protocol Details
----------------------------
 
The text protocol follows the common form of a range command, but uses
the command "rdelete".
 
3.6 incr
=========
 
Ranged incr will increment all existing keys within a range by the
same amount. This follows the same patterns as existing increment
with the obvious exception that it will not initialize keys since it's
only modifing things that already exist.
 
3.6.1 Binary Protocol Details
------------------------------
 
The command ID for binary range increment is 0x39. The command ID for
a silent increment is 0x3a.
 
The extra data includes the standard incr/decr extra data *after* the
ranged extra data.
 
The responses will include a key and value for every modified item.
 
3.6.2 Text Protocol Details
----------------------------
 
The text command for range increment is "rincr" and is in the
following form:
 
rincr <start inclusion> <end inclusion> <max items> <value> <start key> [end key]\r\n
 
Responses are the same as a multi-gets.
 
3.7 decr
=========
 
Ranged decr follows ranged incr.
 
3.7.1 Binary Protocol Details
------------------------------
 
The command ID for binary range decrement is 0x3b. The command ID for
a silent range decrement is 0x3c.
 
3.7.2 Text Protocol Details
----------------------------
 
The command for range decrement is "rdecr".
 
3.8 replace (omitted)
======================
 
Ranged replace is explicitly omitted as it it would mean the same
thing as a set.
 
4 Command Reference
~~~~~~~~~~~~~~~~~~~~
 
The following table is a quick reference to all of the commands
defined herein. Commands omitted from the text column are intentional
as they are not defined outside of the binary protocol.
 
  Command Bin CMD ID Text Command
 ---------------+------------+--------------
  Get 0x30 rget
  Set 0x31 rset
  Quiet Set 0x32
  Append 0x33 rappend
  Quiet Append 0x34
  Prepend 0x35 rprepend
  Quiet Prepend 0x36
  Delete 0x37 rdelete
  Quiet Delete 0x38
  Incr 0x39 rincr
  Quiet Incr 0x3a
  Decr 0x3b rdecr
  Quiet Decr 0x3c
 
5 Isolation
~~~~~~~~~~~~
 
Isolation levels are specifically left unspecified. Operations
concurrently working on the same data set may interleave, mutually
exclude each other, or just stomp all over each other.
 
Implementors are encouraged to document their intentions.