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
];
24 unsigned int server_key
;
25 bool no_reply
= ptr
->flags
.no_reply
;
27 unlikely (ptr
->hosts
== NULL
|| ptr
->number_of_hosts
== 0)
28 return MEMCACHED_NO_SERVERS
;
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(ptr
, master_key
, master_key_length
);
35 send_length
= (size_t)snprintf(buffer
, MEMCACHED_DEFAULT_COMMAND_SIZE
,
36 "%s %s%.*s %" PRIu64
"%s\r\n", verb
,
39 offset
, no_reply
? " noreply" : "");
40 unlikely (send_length
>= MEMCACHED_DEFAULT_COMMAND_SIZE
)
41 return MEMCACHED_WRITE_FAILURE
;
43 rc
= memcached_do(&ptr
->hosts
[server_key
], buffer
, send_length
, 1);
44 if (no_reply
|| rc
!= MEMCACHED_SUCCESS
)
47 rc
= memcached_response(&ptr
->hosts
[server_key
], buffer
, MEMCACHED_DEFAULT_COMMAND_SIZE
, NULL
);
50 So why recheck responce? Because the protocol is brain dead :)
51 The number returned might end up equaling one of the string
52 values. Less chance of a mistake with strncmp() so we will
53 use it. We still called memcached_response() though since it
54 worked its magic for non-blocking IO.
56 if (!strncmp(buffer
, "ERROR\r\n", 7))
59 rc
= MEMCACHED_PROTOCOL_ERROR
;
61 else if (!strncmp(buffer
, "NOT_FOUND\r\n", 11))
64 rc
= MEMCACHED_NOTFOUND
;
68 *value
= strtoull(buffer
, (char **)NULL
, 10);
69 rc
= MEMCACHED_SUCCESS
;
75 static memcached_return_t
binary_incr_decr(memcached_st
*ptr
, uint8_t cmd
,
76 const char *master_key
, size_t master_key_length
,
77 const char *key
, size_t key_length
,
78 uint64_t offset
, uint64_t initial
,
82 unsigned int server_key
;
83 bool no_reply
= ptr
->flags
.no_reply
;
85 unlikely (ptr
->hosts
== NULL
|| ptr
->number_of_hosts
== 0)
86 return MEMCACHED_NO_SERVERS
;
88 server_key
= memcached_generate_hash(ptr
, master_key
, master_key_length
);
92 if(cmd
== PROTOCOL_BINARY_CMD_DECREMENT
)
93 cmd
= PROTOCOL_BINARY_CMD_DECREMENTQ
;
94 if(cmd
== PROTOCOL_BINARY_CMD_INCREMENT
)
95 cmd
= PROTOCOL_BINARY_CMD_INCREMENTQ
;
97 protocol_binary_request_incr request
= {.bytes
= {0}};
99 request
.message
.header
.request
.magic
= PROTOCOL_BINARY_REQ
;
100 request
.message
.header
.request
.opcode
= cmd
;
101 request
.message
.header
.request
.keylen
= htons((uint16_t) key_length
);
102 request
.message
.header
.request
.extlen
= 20;
103 request
.message
.header
.request
.datatype
= PROTOCOL_BINARY_RAW_BYTES
;
104 request
.message
.header
.request
.bodylen
= htonl((uint32_t) (key_length
+ request
.message
.header
.request
.extlen
));
105 request
.message
.body
.delta
= htonll(offset
);
106 request
.message
.body
.initial
= htonll(initial
);
107 request
.message
.body
.expiration
= htonl((uint32_t) expiration
);
109 if ((memcached_do(&ptr
->hosts
[server_key
], request
.bytes
,
110 sizeof(request
.bytes
), 0)!=MEMCACHED_SUCCESS
) ||
111 (memcached_io_write(&ptr
->hosts
[server_key
], key
, key_length
, 1) == -1))
113 memcached_io_reset(&ptr
->hosts
[server_key
]);
114 return MEMCACHED_WRITE_FAILURE
;
118 return MEMCACHED_SUCCESS
;
119 return memcached_response(&ptr
->hosts
[server_key
], (char*)value
, sizeof(*value
), NULL
);
122 memcached_return_t
memcached_increment(memcached_st
*ptr
,
123 const char *key
, size_t key_length
,
127 return memcached_increment_by_key(ptr
, key
, key_length
, key
, key_length
, offset
, value
);
130 memcached_return_t
memcached_decrement(memcached_st
*ptr
,
131 const char *key
, size_t key_length
,
135 return memcached_decrement_by_key(ptr
, key
, key_length
, key
, key_length
, offset
, value
);
138 memcached_return_t
memcached_increment_by_key(memcached_st
*ptr
,
139 const char *master_key
, size_t master_key_length
,
140 const char *key
, size_t key_length
,
144 memcached_return_t rc
= memcached_validate_key_length(key_length
, ptr
->flags
.binary_protocol
);
145 unlikely (rc
!= MEMCACHED_SUCCESS
)
148 LIBMEMCACHED_MEMCACHED_INCREMENT_START();
149 if (ptr
->flags
.binary_protocol
)
151 rc
= binary_incr_decr(ptr
, PROTOCOL_BINARY_CMD_INCREMENT
,
152 master_key
, master_key_length
, key
, key_length
,
153 (uint64_t)offset
, 0, MEMCACHED_EXPIRATION_NOT_ADD
,
158 rc
= memcached_auto(ptr
, "incr", master_key
, master_key_length
, key
, key_length
, offset
, value
);
161 LIBMEMCACHED_MEMCACHED_INCREMENT_END();
166 memcached_return_t
memcached_decrement_by_key(memcached_st
*ptr
,
167 const char *master_key
, size_t master_key_length
,
168 const char *key
, size_t key_length
,
172 memcached_return_t rc
= memcached_validate_key_length(key_length
, ptr
->flags
.binary_protocol
);
173 unlikely (rc
!= MEMCACHED_SUCCESS
)
176 LIBMEMCACHED_MEMCACHED_DECREMENT_START();
177 if (ptr
->flags
.binary_protocol
)
178 rc
= binary_incr_decr(ptr
, PROTOCOL_BINARY_CMD_DECREMENT
,
179 master_key
, master_key_length
, key
, key_length
,
180 (uint64_t)offset
, 0, MEMCACHED_EXPIRATION_NOT_ADD
,
183 rc
= memcached_auto(ptr
, "decr", master_key
, master_key_length
, key
, key_length
, offset
, value
);
185 LIBMEMCACHED_MEMCACHED_DECREMENT_END();
190 memcached_return_t
memcached_increment_with_initial(memcached_st
*ptr
,
198 return memcached_increment_with_initial_by_key(ptr
, key
, key_length
,
200 offset
, initial
, expiration
, value
);
203 memcached_return_t
memcached_increment_with_initial_by_key(memcached_st
*ptr
,
204 const char *master_key
,
205 size_t master_key_length
,
213 memcached_return_t rc
= memcached_validate_key_length(key_length
, ptr
->flags
.binary_protocol
);
214 unlikely (rc
!= MEMCACHED_SUCCESS
)
217 LIBMEMCACHED_MEMCACHED_INCREMENT_WITH_INITIAL_START();
218 if (ptr
->flags
.binary_protocol
)
219 rc
= binary_incr_decr(ptr
, PROTOCOL_BINARY_CMD_INCREMENT
,
220 master_key
, master_key_length
, key
, key_length
,
221 offset
, initial
, (uint32_t)expiration
,
224 rc
= MEMCACHED_PROTOCOL_ERROR
;
226 LIBMEMCACHED_MEMCACHED_INCREMENT_WITH_INITIAL_END();
231 memcached_return_t
memcached_decrement_with_initial(memcached_st
*ptr
,
239 return memcached_decrement_with_initial_by_key(ptr
, key
, key_length
,
241 offset
, initial
, expiration
, value
);
244 memcached_return_t
memcached_decrement_with_initial_by_key(memcached_st
*ptr
,
245 const char *master_key
,
246 size_t master_key_length
,
254 memcached_return_t rc
= memcached_validate_key_length(key_length
, ptr
->flags
.binary_protocol
);
255 unlikely (rc
!= MEMCACHED_SUCCESS
)
258 LIBMEMCACHED_MEMCACHED_INCREMENT_WITH_INITIAL_START();
259 if (ptr
->flags
.binary_protocol
)
261 rc
= binary_incr_decr(ptr
, PROTOCOL_BINARY_CMD_DECREMENT
,
262 master_key
, master_key_length
, key
, key_length
,
263 offset
, initial
, (uint32_t)expiration
,
268 rc
= MEMCACHED_PROTOCOL_ERROR
;
271 LIBMEMCACHED_MEMCACHED_INCREMENT_WITH_INITIAL_END();