2 * Copyright (C) 2006-2009 Brian Aker
5 * Use and distribution licensed under the BSD license. See
6 * the COPYING file in the parent directory for full text.
8 * Summary: Methods for adding or decrementing values from an object in memcached
12 #include "libmemcached/common.h"
14 static memcached_return_t
text_incr_decr(memcached_st
*ptr
,
16 const char *group_key
, size_t group_key_length
,
17 const char *key
, size_t key_length
,
21 memcached_return_t rc
;
22 char buffer
[MEMCACHED_DEFAULT_COMMAND_SIZE
];
24 memcached_server_write_instance_st instance
;
25 bool no_reply
= ptr
->flags
.no_reply
;
27 if (memcached_server_count(ptr
) == 0)
28 return memcached_set_error(ptr
, MEMCACHED_NO_SERVERS
, NULL
);
30 if (ptr
->flags
.verify_key
&& (memcached_key_test((const char **)&key
, &key_length
, 1) == MEMCACHED_BAD_KEY_PROVIDED
))
31 return MEMCACHED_BAD_KEY_PROVIDED
;
33 server_key
= memcached_generate_hash_with_redistribution(ptr
, group_key
, group_key_length
);
34 instance
= memcached_server_instance_fetch(ptr
, server_key
);
37 send_length
= snprintf(buffer
, MEMCACHED_DEFAULT_COMMAND_SIZE
,
38 "%s %.*s%.*s %" PRIu64
"%s\r\n", verb
,
39 memcached_print_array(ptr
->prefix_key
),
41 offset
, no_reply
? " noreply" : "");
42 if (send_length
>= MEMCACHED_DEFAULT_COMMAND_SIZE
|| send_length
< 0)
43 return MEMCACHED_WRITE_FAILURE
;
45 rc
= memcached_do(instance
, buffer
, (size_t)send_length
, true);
46 if (no_reply
|| rc
!= MEMCACHED_SUCCESS
)
49 rc
= memcached_response(instance
, buffer
, MEMCACHED_DEFAULT_COMMAND_SIZE
, NULL
);
52 So why recheck responce? Because the protocol is brain dead :)
53 The number returned might end up equaling one of the string
54 values. Less chance of a mistake with strncmp() so we will
55 use it. We still called memcached_response() though since it
56 worked its magic for non-blocking IO.
58 if (! strncmp(buffer
, "ERROR\r\n", 7))
61 rc
= MEMCACHED_PROTOCOL_ERROR
;
63 else if (! strncmp(buffer
, "CLIENT_ERROR\r\n", 14))
66 rc
= MEMCACHED_PROTOCOL_ERROR
;
68 else if (!strncmp(buffer
, "NOT_FOUND\r\n", 11))
71 rc
= MEMCACHED_NOTFOUND
;
75 *value
= strtoull(buffer
, (char **)NULL
, 10);
76 rc
= MEMCACHED_SUCCESS
;
82 static memcached_return_t
binary_incr_decr(memcached_st
*ptr
, uint8_t cmd
,
83 const char *group_key
, size_t group_key_length
,
84 const char *key
, size_t key_length
,
85 uint64_t offset
, uint64_t initial
,
90 memcached_server_write_instance_st instance
;
91 bool no_reply
= ptr
->flags
.no_reply
;
93 if (memcached_server_count(ptr
) == 0)
94 return memcached_set_error(ptr
, MEMCACHED_NO_SERVERS
, NULL
);
96 server_key
= memcached_generate_hash_with_redistribution(ptr
, group_key
, group_key_length
);
97 instance
= memcached_server_instance_fetch(ptr
, server_key
);
101 if(cmd
== PROTOCOL_BINARY_CMD_DECREMENT
)
102 cmd
= PROTOCOL_BINARY_CMD_DECREMENTQ
;
103 if(cmd
== PROTOCOL_BINARY_CMD_INCREMENT
)
104 cmd
= PROTOCOL_BINARY_CMD_INCREMENTQ
;
106 protocol_binary_request_incr request
= {.bytes
= {0}};
108 request
.message
.header
.request
.magic
= PROTOCOL_BINARY_REQ
;
109 request
.message
.header
.request
.opcode
= cmd
;
110 request
.message
.header
.request
.keylen
= htons((uint16_t)(key_length
+ memcached_array_size(ptr
->prefix_key
)));
111 request
.message
.header
.request
.extlen
= 20;
112 request
.message
.header
.request
.datatype
= PROTOCOL_BINARY_RAW_BYTES
;
113 request
.message
.header
.request
.bodylen
= htonl((uint32_t)(key_length
+ memcached_array_size(ptr
->prefix_key
) +request
.message
.header
.request
.extlen
));
114 request
.message
.body
.delta
= htonll(offset
);
115 request
.message
.body
.initial
= htonll(initial
);
116 request
.message
.body
.expiration
= htonl((uint32_t) expiration
);
118 struct libmemcached_io_vector_st vector
[]=
120 { .length
= sizeof(request
.bytes
), .buffer
= request
.bytes
},
121 { .length
= memcached_array_size(ptr
->prefix_key
), .buffer
= ptr
->prefix_key
},
122 { .length
= key_length
, .buffer
= key
}
125 memcached_return_t rc
;
126 if ((rc
= memcached_vdo(instance
, vector
, 3, true)) != MEMCACHED_SUCCESS
)
128 memcached_io_reset(instance
);
129 return (rc
== MEMCACHED_SUCCESS
) ? MEMCACHED_WRITE_FAILURE
: rc
;
133 return MEMCACHED_SUCCESS
;
134 return memcached_response(instance
, (char*)value
, sizeof(*value
), NULL
);
137 memcached_return_t
memcached_increment(memcached_st
*ptr
,
138 const char *key
, size_t key_length
,
142 uint64_t local_value
;
146 return memcached_increment_by_key(ptr
, key
, key_length
, key
, key_length
, offset
, value
);
149 memcached_return_t
memcached_decrement(memcached_st
*ptr
,
150 const char *key
, size_t key_length
,
154 uint64_t local_value
;
158 return memcached_decrement_by_key(ptr
, key
, key_length
, key
, key_length
, offset
, value
);
161 memcached_return_t
memcached_increment_by_key(memcached_st
*ptr
,
162 const char *group_key
, size_t group_key_length
,
163 const char *key
, size_t key_length
,
167 memcached_return_t rc
= memcached_validate_key_length(key_length
, ptr
->flags
.binary_protocol
);
168 unlikely (rc
!= MEMCACHED_SUCCESS
)
171 uint64_t local_value
;
175 LIBMEMCACHED_MEMCACHED_INCREMENT_START();
176 if (ptr
->flags
.binary_protocol
)
178 rc
= binary_incr_decr(ptr
, PROTOCOL_BINARY_CMD_INCREMENT
,
179 group_key
, group_key_length
, key
, key_length
,
180 (uint64_t)offset
, 0, MEMCACHED_EXPIRATION_NOT_ADD
,
185 rc
= text_incr_decr(ptr
, "incr", group_key
, group_key_length
, key
, key_length
, offset
, value
);
188 LIBMEMCACHED_MEMCACHED_INCREMENT_END();
193 memcached_return_t
memcached_decrement_by_key(memcached_st
*ptr
,
194 const char *group_key
, size_t group_key_length
,
195 const char *key
, size_t key_length
,
199 memcached_return_t rc
= memcached_validate_key_length(key_length
, ptr
->flags
.binary_protocol
);
200 unlikely (rc
!= MEMCACHED_SUCCESS
)
203 uint64_t local_value
;
207 LIBMEMCACHED_MEMCACHED_DECREMENT_START();
208 if (ptr
->flags
.binary_protocol
)
210 rc
= binary_incr_decr(ptr
, PROTOCOL_BINARY_CMD_DECREMENT
,
211 group_key
, group_key_length
, key
, key_length
,
212 (uint64_t)offset
, 0, MEMCACHED_EXPIRATION_NOT_ADD
,
217 rc
= text_incr_decr(ptr
, "decr", group_key
, group_key_length
, key
, key_length
, offset
, value
);
220 LIBMEMCACHED_MEMCACHED_DECREMENT_END();
225 memcached_return_t
memcached_increment_with_initial(memcached_st
*ptr
,
233 uint64_t local_value
;
237 return memcached_increment_with_initial_by_key(ptr
, key
, key_length
,
239 offset
, initial
, expiration
, value
);
242 memcached_return_t
memcached_increment_with_initial_by_key(memcached_st
*ptr
,
243 const char *group_key
,
244 size_t group_key_length
,
252 memcached_return_t rc
= memcached_validate_key_length(key_length
, ptr
->flags
.binary_protocol
);
253 unlikely (rc
!= MEMCACHED_SUCCESS
)
256 uint64_t local_value
;
260 LIBMEMCACHED_MEMCACHED_INCREMENT_WITH_INITIAL_START();
261 if (ptr
->flags
.binary_protocol
)
262 rc
= binary_incr_decr(ptr
, PROTOCOL_BINARY_CMD_INCREMENT
,
263 group_key
, group_key_length
, key
, key_length
,
264 offset
, initial
, (uint32_t)expiration
,
267 rc
= MEMCACHED_PROTOCOL_ERROR
;
269 LIBMEMCACHED_MEMCACHED_INCREMENT_WITH_INITIAL_END();
274 memcached_return_t
memcached_decrement_with_initial(memcached_st
*ptr
,
282 uint64_t local_value
;
286 return memcached_decrement_with_initial_by_key(ptr
, key
, key_length
,
288 offset
, initial
, expiration
, value
);
291 memcached_return_t
memcached_decrement_with_initial_by_key(memcached_st
*ptr
,
292 const char *group_key
,
293 size_t group_key_length
,
301 memcached_return_t rc
= memcached_validate_key_length(key_length
, ptr
->flags
.binary_protocol
);
302 unlikely (rc
!= MEMCACHED_SUCCESS
)
305 uint64_t local_value
;
309 LIBMEMCACHED_MEMCACHED_INCREMENT_WITH_INITIAL_START();
310 if (ptr
->flags
.binary_protocol
)
312 rc
= binary_incr_decr(ptr
, PROTOCOL_BINARY_CMD_DECREMENT
,
313 group_key
, group_key_length
, key
, key_length
,
314 offset
, initial
, (uint32_t)expiration
,
319 rc
= MEMCACHED_PROTOCOL_ERROR
;
322 LIBMEMCACHED_MEMCACHED_INCREMENT_WITH_INITIAL_END();