public
Fork of dustin/memcached
Description: This is where my memcached work lives before svn munges the changes.
Homepage: http://www.danga.com/memcached/
Clone URL: git://github.com/tmaesaka/memcached.git
Binary protocol test now complies with the latest specification.
tmaesaka (author)
Thu Aug 14 23:57:09 -0700 2008
commit  67b4da9eb855ebe7695a197320232b8d25692f84
tree    f17d7afb763013877ae170d5bf29920e9361682d
parent  cb8145061d7abe40bfafd4abd4661d31522ed670
...
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
...
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
...
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
359
360
361
 
 
 
362
363
364
365
366
367
368
369
370
371
 
 
 
372
373
374
375
376
377
378
379
380
381
382
 
 
 
 
 
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
 
 
 
 
 
406
407
408
409
410
 
 
 
 
 
 
411
412
413
414
415
416
 
 
 
 
417
418
 
 
 
 
 
 
 
 
419
420
421
422
423
 
 
 
 
 
 
 
 
424
425
426
427
428
 
 
 
429
430
 
431
432
433
...
438
439
440
441
442
443
 
 
444
445
446
447
448
449
450
 
 
 
451
452
 
453
454
455
456
457
458
 
 
459
460
461
462
463
464
 
 
465
466
467
468
 
...
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
...
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
...
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
359
360
361
 
362
363
364
365
366
367
368
369
370
371
 
 
 
372
373
374
375
376
377
378
379
380
381
 
 
 
382
383
384
385
386
387
388
389
390
...
395
396
397
 
 
 
398
399
400
401
402
 
 
 
 
403
404
405
406
 
407
408
409
410
 
 
 
411
412
413
414
415
 
 
 
416
417
418
419
420
 
421
0
@@ -7,84 +7,73 @@ use FindBin qw($Bin);
0
 use lib "$Bin/lib";
0
 use MemcachedTest;
0
 
0
-ok("foo", "foo");
0
-=temporarily_disable
0
 my $server = new_memcached();
0
-
0
 ok($server, "started the server");
0
 
0
-# Based almost 100% off testClient.py which is Copyright (c) 2007 Dustin Sallings <dustin@spy.net>
0
+# Based almost 100% off testClient.py which is:
0
+# Copyright (c) 2007 Dustin Sallings <dustin@spy.net>
0
 
0
 # Command constants
0
-use constant CMD_GET => 0;
0
-use constant CMD_SET => 1;
0
-use constant CMD_ADD => 2;
0
-use constant CMD_REPLACE => 3;
0
-use constant CMD_DELETE => 4;
0
-use constant CMD_INCR => 5;
0
-use constant CMD_DECR => 6;
0
-use constant CMD_QUIT => 7;
0
-use constant CMD_FLUSH => 8;
0
-use constant CMD_GETQ => 9;
0
-use constant CMD_NOOP => 10;
0
-use constant CMD_VERSION => 11;
0
-
0
-# CAS, Flags, expiration
0
-use constant SET_PKT_FMT => "NNNN";
0
-
0
-# Flags, expiration, id
0
-use constant CAS_PKT_FMT => "NNNN";
0
-
0
-# How long until the deletion takes effect.
0
-use constant DEL_PKT_FMT => "N";
0
-
0
-# amount, initial value, expiration
0
+use constant CMD_GET => 0x00;
0
+use constant CMD_SET => 0x01;
0
+use constant CMD_ADD => 0x02;
0
+use constant CMD_REPLACE => 0x03;
0
+use constant CMD_DELETE => 0x04;
0
+use constant CMD_INCR => 0x05;
0
+use constant CMD_DECR => 0x06;
0
+use constant CMD_QUIT => 0x07;
0
+use constant CMD_FLUSH => 0x08;
0
+use constant CMD_GETQ => 0x09;
0
+use constant CMD_NOOP => 0x0A;
0
+use constant CMD_VERSION => 0x0B;
0
+use constant CMD_GETK => 0x0C;
0
+use constant CMD_GETKQ => 0x0D;
0
+use constant CMD_APPEND => 0x0E;
0
+use constant CMD_PREPEND => 0x0F;
0
+
0
+# REQ and RES formats are divided even though they currently share
0
+# the same format, since they _could_ differ in the future.
0
+use constant REQ_PKT_FMT => "CCnCCnNNNN";
0
+use constant RES_PKT_FMT => "CCnCCnNNNN";
0
 use constant INCRDECR_PKT_FMT => "NNNNN";
0
-
0
-use constant REQ_MAGIC_BYTE => 0x80;
0
-use constant RES_MAGIC_BYTE => 0x81;
0
-
0
-use constant PKT_FMT => "CCnCxxxNN";
0
-
0
-#min recv packet size
0
-use constant MIN_RECV_PACKET => length(pack(PKT_FMT));
0
+use constant MIN_RECV_BYTES => length(pack(RES_PKT_FMT));
0
+use constant REQ_MAGIC => 0x80;
0
+use constant RES_MAGIC => 0x81;
0
 
0
 my $mc = MC::Client->new;
0
+
0
 my $check = sub {
0
- my ($key, $orig_flags, $orig_value) = @_;
0
- my ($flags, $value) = $mc->get($key);
0
- is($flags, $orig_flags, "Flags is set properly");
0
- is($value, $orig_value, "Value is set properly");
0
+ my ($key, $orig_flags, $orig_val) = @_;
0
+ my ($flags, $val, $cas) = $mc->get($key);
0
+ is($flags, $orig_flags, "Flags is set properly");
0
 };
0
 
0
 my $set = sub {
0
- my ($key, $exp, $orig_flags, $orig_value) = @_;
0
- $mc->set($key, $exp, $orig_flags, $orig_value);
0
- $check->($key, $orig_flags, $orig_value);
0
+ my ($key, $exp, $orig_flags, $orig_value) = @_;
0
+ $mc->set($key, $orig_value, $orig_flags, $exp);
0
+ $check->($key, $orig_flags, $orig_value);
0
 };
0
 
0
 my $empty = sub {
0
- my $key = shift;
0
- my $rv =()= eval { $mc->get($key) };
0
- is($rv, 0, "Didn't get a result from get");
0
- ok($@->not_found, "We got a not found error when we expected one");
0
+ my $key = shift;
0
+ my $rv =()= eval { $mc->get($key) };
0
+ is($rv, 0, "Didn't get a result from get");
0
+ ok($@->not_found, "We got a not found error when we expected one");
0
 };
0
 
0
 my $delete = sub {
0
- my ($key, $when) = @_;
0
- $mc->delete($key, $when);
0
- $empty->($key);
0
+ my ($key, $when) = @_;
0
+ $mc->delete($key, $when);
0
+ $empty->($key);
0
 };
0
 
0
+diag "Test Version";
0
+my $v = $mc->version;
0
+ok(defined $v && length($v), "Proper version: $v");
0
+
0
 diag "Flushing...";
0
 $mc->flush;
0
 
0
-{
0
- diag "Test Version";
0
- my $v = $mc->version;
0
- ok(defined $v && length($v), "Proper version: $v");
0
-}
0
-
0
 diag "Noop";
0
 $mc->noop;
0
 
0
@@ -102,58 +91,43 @@ $empty->('x');
0
 $empty->('y');
0
 
0
 {
0
- diag "Reservation delete";
0
- $set->('y', 5, 19, "someothervalue");
0
- $delete->('y', 1);
0
- my $rv =()= eval { $mc->add('y', 5, 19, "yetanothervalue") };
0
- is($rv, 0, "Add didn't return anything");
0
- ok($@->exists, "We got an exists error like we expected");
0
- sleep 2;
0
- $mc->add('y', 5, 19, "wibblevalue");
0
+ diag "Add";
0
+ $empty->('i');
0
+ $mc->add('i', 'ex', 5, 10);
0
+ $check->('i', 5, "ex");
0
+
0
+ my $rv =()= eval { $mc->add('i', "ex2", 10, 5) };
0
+ is($rv, 0, "Add didn't return anything");
0
+ ok($@->exists, "Expected exists error received");
0
+ $check->('i', 5, "ex");
0
 }
0
 
0
 {
0
- diag "Add";
0
- $empty->('i');
0
- $mc->add('i', 5, 19, "ex");
0
- $check->('i', 19, "ex");
0
-
0
- my $rv =()= eval { $mc->add('i', 5, 19, "ex2") };
0
- is($rv, 0, "Add didn't return anything");
0
- ok($@->exists, "Expected exists error received");
0
-
0
- $check->('i', 19, "ex");
0
+ diag "Replace";
0
+ $empty->('j');
0
+
0
+ my $rv =()= eval { $mc->replace('j', "ex", 19, 5) };
0
+ is($rv, 0, "Replace didn't return anything");
0
+ ok($@->not_found, "Expected not_found error received");
0
+ $empty->('j');
0
+ $mc->add('j', "ex2", 14, 5);
0
+ $check->('j', 14, "ex2");
0
+ $mc->replace('j', "ex3", 24, 5);
0
+ $check->('j', 24, "ex3");
0
 }
0
 
0
 {
0
- diag "Replace";
0
- $empty->('j');
0
-
0
- my $rv =()= eval { $mc->replace('j', 5, 19, "ex") };
0
- is($rv, 0, "Replace didn't return anything");
0
- ok($@->not_found, "Expected not_found error received");
0
-
0
- $empty->('j');
0
-
0
- $mc->add('j', 5, 14, "ex2");
0
- $check->('j', 14, "ex2");
0
-
0
- $mc->replace('j', 5, 24, "ex3");
0
- $check->('j', 24, "ex3");
0
-}
0
-
0
-{
0
- diag "MultiGet";
0
- $mc->add('xx', 5, 1, "ex");
0
- $mc->add('wye', 5, 2, "why");
0
- my $rv = $mc->getMulti(qw(xx wye zed));
0
+ diag "MultiGet";
0
+ $mc->add('xx', "ex", 1, 5);
0
+ $mc->add('wye', "why", 2, 5);
0
+ my $rv = $mc->get_multi(qw(xx wye zed));
0
 
0
     # CAS is returned with all gets.
0
     $rv->{xx}->[2] = 0;
0
     $rv->{wye}->[2] = 0;
0
- is_deeply($rv->{xx}, [1, 'ex', 0], "X is correct");
0
- is_deeply($rv->{wye}, [2, 'why', 0], "Y is correct");
0
- is(keys(%$rv), 2, "Got only two answers like we expect");
0
+ is_deeply($rv->{xx}, [1, 'ex', 0], "X is correct");
0
+ is_deeply($rv->{wye}, [2, 'why', 0], "Y is correct");
0
+ is(keys(%$rv), 2, "Got only two answers like we expect");
0
 }
0
 
0
 diag "Test increment";
0
@@ -170,264 +144,247 @@ is($mc->decr("x"), 4, "Decrease by one");
0
 is($mc->decr("x", 211), 0, "Floor is zero");
0
 
0
 {
0
- diag "CAS";
0
- $mc->flush;
0
+ diag "CAS";
0
+ $mc->flush;
0
 
0
- {
0
- my $rv =()= eval { $mc->set("x", 5, 19, "bad value", 0x7FFFFFFFFF) };
0
- is($rv, 0, "Empty return on expected failure");
0
- ok($@->not_found, "Error was 'not found' as expected");
0
- }
0
+ {
0
+ my $rv =()= eval { $mc->set("x", "bad value", 19, 5, 0x7FFFFFFFFF) };
0
+ is($rv, 0, "Empty return on expected failure");
0
+ ok($@->not_found, "Error was 'not found' as expected");
0
+ }
0
 
0
- $mc->add("x", 5, 19, "original value");
0
+ $mc->add("x", "original value", 5, 19);
0
 
0
- my ($flags, $val, $i) = $mc->get("x");
0
- is($val, "original value", "->gets returned proper value");
0
+ my ($flags, $val, $i) = $mc->get("x");
0
+ is($val, "original value", "->gets returned proper value");
0
 
0
     {
0
- my $rv =()= eval { $mc->set("x", 5, 19, "broken value", $i+1) };
0
- is($rv, 0, "Empty return on expected failure (1)");
0
- ok($@->exists, "Expected error state of 'exists' (1)");
0
- }
0
-
0
- $mc->set("x", 5, 19, "new value", $i);
0
+ my $rv =()= eval { $mc->set("x", "broken value", 19, 5, $i+1) };
0
+ is($rv, 0, "Empty return on expected failure (1)");
0
+ ok($@->exists, "Expected error state of 'exists' (1)");
0
+ }
0
 
0
- my ($newflags, $newval, $newi) = $mc->get("x");
0
- is($newval, "new value", "CAS properly overwrote value");
0
+ $mc->set("x", "new value", 19, 5, $i);
0
 
0
- {
0
- my $rv =()= eval { $mc->set("x", 5, 19, "replay value", $i) };
0
- is($rv, 0, "Empty return on expected failure (2)");
0
- ok($@->exists, "Expected error state of 'exists' (2)");
0
- }
0
+ my ($newflags, $newval, $newi) = $mc->get("x");
0
+ is($newval, "new value", "CAS properly overwrote value");
0
 
0
- (undef, my $newval2) = $mc->get("x");
0
- is($newval2, "new value", "CAS replay didn't overwrite value");
0
+ {
0
+ my $rv =()= eval { $mc->set("x", "replay value", 19, 5, $i) };
0
+ is($rv, 0, "Empty return on expected failure (2)");
0
+ ok($@->exists, "Expected error state of 'exists' (2)");
0
+ }
0
 }
0
 
0
-$mc->flush;
0
-$mc->close;
0
-
0
-
0
 package MC::Client;
0
 
0
 use strict;
0
 use warnings;
0
-
0
 use fields qw(socket);
0
-
0
 use IO::Socket::INET;
0
 
0
 sub new {
0
- my $self = shift;
0
-
0
+ my $self = shift;
0
     my $sock = $server->sock;
0
+ $self = fields::new($self);
0
+ $self->{socket} = $sock;
0
+ return $self;
0
+}
0
 
0
- $self = fields::new($self);
0
-
0
- $self->{socket} = $sock;
0
+sub send_command {
0
+ my $self = shift;
0
+ die "Not enough args to send_command" unless @_ >= 4;
0
+ my ($cmd, $key, $val, $opaque, $extra_header, $cas) = @_;
0
+
0
+ $extra_header = '' unless defined $extra_header;
0
+ my $keylen = length($key);
0
+ my $vallen = length($val);
0
+ my $extralen = length($extra_header);
0
+ my $datatype = 0; # field for future use
0
+ my $reserved = 0; # field for future use
0
+ my $totallen = $keylen + $vallen + $extralen;
0
+ my $ident_hi = 0;
0
+ my $ident_lo = 0;
0
+
0
+ if ($cas) {
0
+ $ident_hi = int($cas / 2 ** 32);
0
+ $ident_lo = int($cas % 2 ** 32);
0
+ }
0
 
0
- return $self;
0
-}
0
+ my $msg = pack(::REQ_PKT_FMT, ::REQ_MAGIC, $cmd, $keylen, $extralen,
0
+ $datatype, $reserved, $totallen, $opaque, $ident_hi,
0
+ $ident_lo);
0
 
0
-sub close {
0
- my $self = shift;
0
- return $self->{socket}->close(@_);
0
+ return $self->{socket}->send($msg . $extra_header . $key . $val);
0
 }
0
 
0
-sub _sendCmd {
0
- my $self = shift;
0
- die "Not enough args to _sendCmd" unless @_ >= 4;
0
- my ($cmd, $key, $val, $opaque, $extraHeader) = @_;
0
+sub _handle_single_response {
0
+ my $self = shift;
0
+ my $myopaque = shift;
0
 
0
- $extraHeader = '' unless defined $extraHeader;
0
+ $self->{socket}->recv(my $response, ::MIN_RECV_BYTES);
0
+ Test::More::is(length($response), ::MIN_RECV_BYTES, "Expected read length");
0
 
0
- my $keylen = length($key);
0
- my $vallen = length($val);
0
- my $extralen = length($extraHeader);
0
+ my ($magic, $cmd, $keylen, $extralen, $datatype, $status, $remaining,
0
+ $opaque, $ident_hi, $ident_lo) = unpack(::RES_PKT_FMT, $response);
0
+ Test::More::is($magic, ::RES_MAGIC, "Got proper response magic");
0
 
0
- my $msg = pack(::PKT_FMT, ::REQ_MAGIC_BYTE, $cmd, $keylen, $extralen,
0
- $keylen + $vallen + $extralen, $opaque);
0
- return $self->{socket}->send($msg . $extraHeader . $key . $val);
0
-}
0
+ return ($opaque, '') if($remaining == 0);
0
 
0
-sub _handleSingleResponse {
0
- my $self = shift;
0
- my $myopaque = shift;
0
+ # fetch the value
0
+ $self->{socket}->recv(my $rv, $remaining);
0
 
0
- $self->{socket}->recv(my $response, ::MIN_RECV_PACKET);
0
+ if (defined $myopaque) {
0
+ Test::More::is($opaque, $myopaque, "Expected opaque");
0
+ } else {
0
+ Test::More::pass("Implicit pass since myopaque is undefined");
0
+ }
0
 
0
- Test::More::is(length($response), ::MIN_RECV_PACKET, "Expected read length");
0
+ my $cas = ($ident_hi * 2 ** 32) + $ident_lo;
0
 
0
- my ($magic, $cmd, $errcode, $extralen, $remaining,
0
- $opaque) = unpack(::PKT_FMT, $response);
0
+ if ($status) {
0
+ die MC::Error->new($status, $rv);
0
+ }
0
 
0
- Test::More::is($magic, ::RES_MAGIC_BYTE, "Got proper magic");
0
+ return ($opaque, $rv, $cas);
0
+}
0
 
0
- return ($opaque, "")
0
- if $remaining == 0;
0
+sub _do_command {
0
+ my $self = shift;
0
+ die unless @_ >= 3;
0
+ my ($cmd, $key, $val, $extra_header, $cas) = @_;
0
 
0
- $self->{socket}->recv(my $rv, $remaining);
0
+ $extra_header = '' unless defined $extra_header;
0
+ my $opaque = int(rand(2**32));
0
+ $self->send_command($cmd, $key, $val, $opaque, $extra_header, $cas);
0
+ (undef, my $rv, my $rcas) = $self->_handle_single_response($opaque);
0
+ return ($rv, $rcas);
0
+}
0
 
0
- if (defined $myopaque) {
0
- Test::More::is($opaque, $myopaque, "Expected opaque");
0
- } else {
0
- Test::More::pass("Implicit pass since myopaque is undefined");
0
- }
0
+sub _incrdecr {
0
+ my $self = shift;
0
+ my ($cmd, $key, $amt, $init, $exp) = @_;
0
 
0
- if ($errcode) {
0
- die MC::Error->new($errcode, $rv);
0
- }
0
+ my $amt_hi = int($amt / 2 ** 32);
0
+ my $amt_lo = int($amt % 2 ** 32);
0
 
0
- return ($opaque, $rv);
0
-}
0
+ my $init_hi = int($init / 2 ** 32);
0
+ my $init_lo = int($init % 2 ** 32);
0
 
0
-sub _doCmd {
0
- my $self = shift;
0
- die unless @_ >= 3;
0
- my ($cmd, $key, $val, $extraHeader) = @_;
0
+ my $extra_header = pack(::INCRDECR_PKT_FMT, $amt_hi, $amt_lo, $init_hi,
0
+ $init_lo, $exp);
0
 
0
- $extraHeader = '' unless defined $extraHeader;
0
+ my ($data, undef) = $self->_do_command($cmd, $key, '', $extra_header);
0
 
0
- my $opaque = int(rand(2**32));
0
+ my $header = substr $data, 0, 8, '';
0
+ my ($resp_hi, $resp_lo) = unpack "NN", $header;
0
+ my $resp = ($resp_hi * 2 ** 32) + $resp_lo;
0
 
0
- $self->_sendCmd($cmd, $key, $val, $opaque, $extraHeader);
0
- (undef, my $rv) = $self->_handleSingleResponse($opaque);
0
- return $rv;
0
+ return $resp;
0
 }
0
 
0
-sub __parseGet {
0
- my $self = shift;
0
- my $rv = shift; # currently contains 4 bytes of 'flag' followed by value
0
- my $header = substr $rv, 0, 12, '';
0
- my ($ident_hi, $ident_lo, $flags) = unpack "NNN", $header;
0
- my $ident = ($ident_hi * 2 ** 32) + $ident_lo;
0
+sub get {
0
+ my $self = shift;
0
+ my $key = shift;
0
+ my ($rv, $cas) = $self->_do_command(::CMD_GET, $key, '', '');
0
 
0
- return $flags, $rv, $ident;
0
-}
0
+ my $header = substr $rv, 0, 4, '';
0
+ my $flags = unpack("N", $header);
0
 
0
-sub get {
0
- my $self = shift;
0
- my $key = shift;
0
- my $parts = $self->_doCmd(::CMD_GET, $key, '');
0
- return $self->__parseGet($parts);
0
+ return ($flags, $rv, $cas);
0
 }
0
 
0
-sub _mutate {
0
- my $self = shift;
0
- my ($cmd, $key, $exp, $flags, $val, $ident) = @_;
0
+sub get_multi {
0
+ my $self = shift;
0
+ my @keys = @_;
0
 
0
- my $ident_hi = 0;
0
- my $ident_lo = 0;
0
- if ($ident) {
0
- $ident_hi = int($ident / 2 ** 32);
0
- $ident_lo = int($ident % 2 ** 32);
0
+ for (my $i = 0; $i < @keys; $i++) {
0
+ $self->send_command(::CMD_GETQ, $keys[$i], '', $i, '', 0);
0
     }
0
 
0
- return $self->_doCmd($cmd, $key, $val, pack(::SET_PKT_FMT, $ident_hi, $ident_lo, $flags, $exp));
0
-}
0
-
0
-sub set {
0
- my $self = shift;
0
- my ($key, $exp, $flags, $val, $ident) = @_;
0
-
0
- return $self->_mutate(::CMD_SET, $key, $exp, $flags, $val, $ident);
0
-}
0
+ my $terminal = @keys + 10;
0
+ $self->send_command(::CMD_NOOP, '', '', $terminal);
0
 
0
-sub __incrdecr {
0
- my $self = shift;
0
- my ($cmd, $key, $amt, $init, $exp) = @_;
0
+ my %return;
0
+ while (1) {
0
+ my ($opaque, $data) = $self->_handle_single_response;
0
+ last if $opaque == $terminal;
0
 
0
- my $amt_hi = int($amt / 2 ** 32);
0
- my $amt_lo = int($amt % 2 ** 32);
0
+ my $header = substr $data, 0, 4, '';
0
+ my $flags = unpack("N", $header);
0
 
0
- my $init_hi = int($init / 2 ** 32);
0
- my $init_lo = int($init % 2 ** 32);
0
+ $return{$keys[$opaque]} = [$flags, $data];
0
+ }
0
 
0
- my $data = $self->_doCmd($cmd, $key, '', pack(::INCRDECR_PKT_FMT, $amt_hi, $amt_lo, $init_hi, $init_lo, $exp));
0
- my $header = substr $data, 0, 12, '';
0
- my ($resp_hi, $resp_lo) = unpack "NN", $header;
0
- my $resp = ($resp_hi * 2 ** 32) + $resp_lo;
0
- return $resp;
0
+ return %return if wantarray;
0
+ return \%return;
0
 }
0
 
0
-sub incr {
0
- my $self = shift;
0
- my ($key, $amt, $init, $exp) = @_;
0
- $amt = 1 unless defined $amt;
0
- $init = 0 unless defined $init;
0
- $exp = 0 unless defined $exp;
0
-
0
- return $self->__incrdecr(::CMD_INCR, $key, $amt, $init, $exp);
0
+sub version {
0
+ my $self = shift;
0
+ return $self->_do_command(::CMD_VERSION, '', '');
0
 }
0
 
0
-sub decr {
0
- my $self = shift;
0
- my ($key, $amt, $init, $exp) = @_;
0
- $amt = 1 unless defined $amt;
0
- $init = 0 unless defined $init;
0
- $exp = 0 unless defined $exp;
0
-
0
- return $self->__incrdecr(::CMD_DECR, $key, $amt, $init, $exp);
0
+sub flush {
0
+ my $self = shift;
0
+ return $self->_do_command(::CMD_FLUSH, '', '');
0
 }
0
 
0
 sub add {
0
- my $self = shift;
0
- my ($key, $exp, $flags, $val) = @_;
0
- return $self->_mutate(::CMD_ADD, $key, $exp, $flags, $val);
0
-}
0
-sub replace {
0
- my $self = shift;
0
- my ($key, $exp, $flags, $val) = @_;
0
- return $self->_mutate(::CMD_REPLACE, $key, $exp, $flags, $val);
0
+ my $self = shift;
0
+ my ($key, $val, $flags, $expire) = @_;
0
+ my $extra_header = pack "NN", $flags, $expire;
0
+ my $cas = 0;
0
+ return $self->_do_command(::CMD_ADD, $key, $val, $extra_header, $cas);
0
 }
0
 
0
-sub getMulti {
0
- my $self = shift;
0
- my @keys = @_;
0
-
0
- for (my $i = 0; $i < @keys; $i++) {
0
- $self->_sendCmd(::CMD_GETQ, $keys[$i], '', $i);
0
- }
0
-
0
- my $terminal = @keys + 10;
0
- $self->_sendCmd(::CMD_NOOP, '', '', $terminal);
0
-
0
- my %return;
0
-
0
- while (1) {
0
- my ($opaque, $data) = $self->_handleSingleResponse;
0
- last if $opaque == $terminal;
0
-
0
- $return{$keys[$opaque]} = [$self->__parseGet($data)];
0
- }
0
- return %return if wantarray;
0
- return \%return;
0
+sub set {
0
+ my $self = shift;
0
+ my ($key, $val, $flags, $expire, $cas) = @_;
0
+ my $extra_header = pack "NN", $flags, $expire;
0
+ return $self->_do_command(::CMD_SET, $key, $val, $extra_header, $cas);
0
 }
0
 
0
-sub noop {
0
- my $self = shift;
0
- return $self->_doCmd(::CMD_NOOP, '', '');
0
+sub replace {
0
+ my $self = shift;
0
+ my ($key, $val, $flags, $expire) = @_;
0
+ my $extra_header = pack "NN", $flags, $expire;
0
+ my $cas = 0;
0
+ return $self->_do_command(::CMD_REPLACE, $key, $val, $extra_header, $cas);
0
 }
0
 
0
 sub delete {
0
- my $self = shift;
0
- my ($key, $when) = @_;
0
- $when = 0 unless defined $when;
0
+ my $self = shift;
0
+ my ($key) = @_;
0
+ return $self->_do_command(::CMD_DELETE, $key, '');
0
+}
0
 
0
- return $self->_doCmd(::CMD_DELETE, $key, '', pack(::DEL_PKT_FMT, $when));
0
+sub incr {
0
+ my $self = shift;
0
+ my ($key, $amt, $init, $exp) = @_;
0
+ $amt = 1 unless defined $amt;
0
+ $init = 0 unless defined $init;
0
+ $exp = 0 unless defined $exp;
0
+
0
+ return $self->_incrdecr(::CMD_INCR, $key, $amt, $init, $exp);
0
 }
0
 
0
-sub version {
0
- my $self = shift;
0
- return $self->_doCmd(::CMD_VERSION, '', '');
0
+sub decr {
0
+ my $self = shift;
0
+ my ($key, $amt, $init, $exp) = @_;
0
+ $amt = 1 unless defined $amt;
0
+ $init = 0 unless defined $init;
0
+ $exp = 0 unless defined $exp;
0
+
0
+ return $self->_incrdecr(::CMD_DECR, $key, $amt, $init, $exp);
0
 }
0
 
0
-sub flush {
0
- my $self = shift;
0
- return $self->_doCmd(::CMD_FLUSH, '', '');
0
+sub noop {
0
+ my $self = shift;
0
+ return $self->_do_command(::CMD_NOOP, '', '');
0
 }
0
 
0
+
0
 package MC::Error;
0
 
0
 use strict;
0
@@ -438,31 +395,27 @@ use constant ERR_NOT_FOUND => 0x1;
0
 use constant ERR_EXISTS => 0x2;
0
 
0
 use overload '""' => sub {
0
- my $self = shift;
0
-
0
- return "Memcache Error ($self->[0]): $self->[1]";
0
+ my $self = shift;
0
+ return "Memcache Error ($self->[0]): $self->[1]";
0
 };
0
 
0
 sub new {
0
- my $class = shift;
0
- my $error = [@_];
0
-
0
- my $self = bless $error, (ref $class || $class);
0
+ my $class = shift;
0
+ my $error = [@_];
0
+ my $self = bless $error, (ref $class || $class);
0
 
0
- return $self;
0
+ return $self;
0
 }
0
 
0
 sub not_found {
0
- my $self = shift;
0
-
0
- return $self->[0] == ERR_NOT_FOUND;
0
+ my $self = shift;
0
+ return $self->[0] == ERR_NOT_FOUND;
0
 }
0
 
0
 sub exists {
0
- my $self = shift;
0
-
0
- return $self->[0] == ERR_EXISTS;
0
+ my $self = shift;
0
+ return $self->[0] == ERR_EXISTS;
0
 }
0
 
0
 # vim: filetype=perl
0
-=cut
0
+

Comments

    No one has commented yet.