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
14 static memcached_return_t
memcached_auto(memcached_st
*ptr
,
16 const char *master_key
, size_t master_key_length
,
17 const char *key
, size_t key_length
,
22 memcached_return_t rc
;
23 char buffer
[MEMCACHED_DEFAULT_COMMAND_SIZE
];
25 memcached_server_instance_st
*instance
;
26 bool no_reply
= ptr
->flags
.no_reply
;
28 unlikely (memcached_server_count(ptr
) == 0)
29 return MEMCACHED_NO_SERVERS
;
31 if (ptr
->flags
.verify_key
&& (memcached_key_test((const char **)&key
, &key_length
, 1) == MEMCACHED_BAD_KEY_PROVIDED
))
32 return MEMCACHED_BAD_KEY_PROVIDED
;
34 server_key
= memcached_generate_hash(ptr
, master_key
, master_key_length
);
35 instance
= memcached_server_instance_fetch(ptr
, server_key
);
37 send_length
= (size_t)snprintf(buffer
, MEMCACHED_DEFAULT_COMMAND_SIZE
,
38 "%s %.*s%.*s %" PRIu64
"%s\r\n", verb
,
39 (int)ptr
->prefix_key_length
,
42 offset
, no_reply
? " noreply" : "");
43 unlikely (send_length
>= MEMCACHED_DEFAULT_COMMAND_SIZE
)
44 return MEMCACHED_WRITE_FAILURE
;
46 rc
= memcached_do(instance
, buffer
, send_length
, 1);
47 if (no_reply
|| rc
!= MEMCACHED_SUCCESS
)
50 rc
= memcached_response(instance
, buffer
, MEMCACHED_DEFAULT_COMMAND_SIZE
, NULL
);
53 So why recheck responce? Because the protocol is brain dead :)
54 The number returned might end up equaling one of the string
55 values. Less chance of a mistake with strncmp() so we will
56 use it. We still called memcached_response() though since it
57 worked its magic for non-blocking IO.
59 if (!strncmp(buffer
, "ERROR\r\n", 7))
62 rc
= MEMCACHED_PROTOCOL_ERROR
;
64 else if (!strncmp(buffer
, "NOT_FOUND\r\n", 11))
67 rc
= MEMCACHED_NOTFOUND
;
71 *value
= strtoull(buffer
, (char **)NULL
, 10);
72 rc
= MEMCACHED_SUCCESS
;
78 static memcached_return_t
binary_incr_decr(memcached_st
*ptr
, uint8_t cmd
,
79 const char *master_key
, size_t master_key_length
,
80 const char *key
, size_t key_length
,
81 uint64_t offset
, uint64_t initial
,
86 memcached_server_instance_st
*instance
;
87 bool no_reply
= ptr
->flags
.no_reply
;
89 unlikely (memcached_server_count(ptr
) == 0)
90 return MEMCACHED_NO_SERVERS
;
92 server_key
= memcached_generate_hash(ptr
, master_key
, master_key_length
);
93 instance
= memcached_server_instance_fetch(ptr
, server_key
);
97 if(cmd
== PROTOCOL_BINARY_CMD_DECREMENT
)
98 cmd
= PROTOCOL_BINARY_CMD_DECREMENTQ
;
99 if(cmd
== PROTOCOL_BINARY_CMD_INCREMENT
)
100 cmd
= PROTOCOL_BINARY_CMD_INCREMENTQ
;
102 protocol_binary_request_incr request
= {.bytes
= {0}};
104 request
.message
.header
.request
.magic
= PROTOCOL_BINARY_REQ
;
105 request
.message
.header
.request
.opcode
= cmd
;
106 request
.message
.header
.request
.keylen
= htons((uint16_t) key_length
);
107 request
.message
.header
.request
.extlen
= 20;
108 request
.message
.header
.request
.datatype
= PROTOCOL_BINARY_RAW_BYTES
;
109 request
.message
.header
.request
.bodylen
= htonl((uint32_t) (key_length
+ request
.message
.header
.request
.extlen
));
110 request
.message
.body
.delta
= htonll(offset
);
111 request
.message
.body
.initial
= htonll(initial
);
112 request
.message
.body
.expiration
= htonl((uint32_t) expiration
);
114 if ((memcached_do(instance
, request
.bytes
,
115 sizeof(request
.bytes
), 0)!=MEMCACHED_SUCCESS
) ||
116 (memcached_io_write(instance
, key
, key_length
, 1) == -1))
118 memcached_io_reset(instance
);
119 return MEMCACHED_WRITE_FAILURE
;
123 return MEMCACHED_SUCCESS
;
124 return memcached_response(instance
, (char*)value
, sizeof(*value
), NULL
);
127 memcached_return_t
memcached_increment(memcached_st
*ptr
,
128 const char *key
, size_t key_length
,
132 return memcached_increment_by_key(ptr
, key
, key_length
, key
, key_length
, offset
, value
);
135 memcached_return_t
memcached_decrement(memcached_st
*ptr
,
136 const char *key
, size_t key_length
,
140 return memcached_decrement_by_key(ptr
, key
, key_length
, key
, key_length
, offset
, value
);
143 memcached_return_t
memcached_increment_by_key(memcached_st
*ptr
,
144 const char *master_key
, size_t master_key_length
,
145 const char *key
, size_t key_length
,
149 memcached_return_t rc
= memcached_validate_key_length(key_length
, ptr
->flags
.binary_protocol
);
150 unlikely (rc
!= MEMCACHED_SUCCESS
)
153 LIBMEMCACHED_MEMCACHED_INCREMENT_START();
154 if (ptr
->flags
.binary_protocol
)
156 rc
= binary_incr_decr(ptr
, PROTOCOL_BINARY_CMD_INCREMENT
,
157 master_key
, master_key_length
, key
, key_length
,
158 (uint64_t)offset
, 0, MEMCACHED_EXPIRATION_NOT_ADD
,
163 rc
= memcached_auto(ptr
, "incr", master_key
, master_key_length
, key
, key_length
, offset
, value
);
166 LIBMEMCACHED_MEMCACHED_INCREMENT_END();
171 memcached_return_t
memcached_decrement_by_key(memcached_st
*ptr
,
172 const char *master_key
, size_t master_key_length
,
173 const char *key
, size_t key_length
,
177 memcached_return_t rc
= memcached_validate_key_length(key_length
, ptr
->flags
.binary_protocol
);
178 unlikely (rc
!= MEMCACHED_SUCCESS
)
181 LIBMEMCACHED_MEMCACHED_DECREMENT_START();
182 if (ptr
->flags
.binary_protocol
)
183 rc
= binary_incr_decr(ptr
, PROTOCOL_BINARY_CMD_DECREMENT
,
184 master_key
, master_key_length
, key
, key_length
,
185 (uint64_t)offset
, 0, MEMCACHED_EXPIRATION_NOT_ADD
,
188 rc
= memcached_auto(ptr
, "decr", master_key
, master_key_length
, key
, key_length
, offset
, value
);
190 LIBMEMCACHED_MEMCACHED_DECREMENT_END();
195 memcached_return_t
memcached_increment_with_initial(memcached_st
*ptr
,
203 return memcached_increment_with_initial_by_key(ptr
, key
, key_length
,
205 offset
, initial
, expiration
, value
);
208 memcached_return_t
memcached_increment_with_initial_by_key(memcached_st
*ptr
,
209 const char *master_key
,
210 size_t master_key_length
,
218 memcached_return_t rc
= memcached_validate_key_length(key_length
, ptr
->flags
.binary_protocol
);
219 unlikely (rc
!= MEMCACHED_SUCCESS
)
222 LIBMEMCACHED_MEMCACHED_INCREMENT_WITH_INITIAL_START();
223 if (ptr
->flags
.binary_protocol
)
224 rc
= binary_incr_decr(ptr
, PROTOCOL_BINARY_CMD_INCREMENT
,
225 master_key
, master_key_length
, key
, key_length
,
226 offset
, initial
, (uint32_t)expiration
,
229 rc
= MEMCACHED_PROTOCOL_ERROR
;
231 LIBMEMCACHED_MEMCACHED_INCREMENT_WITH_INITIAL_END();
236 memcached_return_t
memcached_decrement_with_initial(memcached_st
*ptr
,
244 return memcached_decrement_with_initial_by_key(ptr
, key
, key_length
,
246 offset
, initial
, expiration
, value
);
249 memcached_return_t
memcached_decrement_with_initial_by_key(memcached_st
*ptr
,
250 const char *master_key
,
251 size_t master_key_length
,
259 memcached_return_t rc
= memcached_validate_key_length(key_length
, ptr
->flags
.binary_protocol
);
260 unlikely (rc
!= MEMCACHED_SUCCESS
)
263 LIBMEMCACHED_MEMCACHED_INCREMENT_WITH_INITIAL_START();
264 if (ptr
->flags
.binary_protocol
)
266 rc
= binary_incr_decr(ptr
, PROTOCOL_BINARY_CMD_DECREMENT
,
267 master_key
, master_key_length
, key
, key_length
,
268 offset
, initial
, (uint32_t)expiration
,
273 rc
= MEMCACHED_PROTOCOL_ERROR
;
276 LIBMEMCACHED_MEMCACHED_INCREMENT_WITH_INITIAL_END();