b6355c1576f9c10406e634d542d93798073fe321
[m6w6/libmemcached] / libmemcached / memcached_auto.c
1 #include "common.h"
2
3 static memcached_return memcached_auto(memcached_st *ptr,
4 const char *verb,
5 const char *master_key, size_t master_key_length,
6 const char *key, size_t key_length,
7 uint64_t offset,
8 uint64_t *value)
9 {
10 size_t send_length;
11 memcached_return rc;
12 char buffer[MEMCACHED_DEFAULT_COMMAND_SIZE];
13 unsigned int server_key;
14 bool no_reply= ptr->flags.no_reply;
15
16 unlikely (ptr->hosts == NULL || ptr->number_of_hosts == 0)
17 return MEMCACHED_NO_SERVERS;
18
19 if (ptr->flags.verify_key && (memcached_key_test((const char **)&key, &key_length, 1) == MEMCACHED_BAD_KEY_PROVIDED))
20 return MEMCACHED_BAD_KEY_PROVIDED;
21
22 server_key= memcached_generate_hash(ptr, master_key, master_key_length);
23
24 send_length= (size_t)snprintf(buffer, MEMCACHED_DEFAULT_COMMAND_SIZE,
25 "%s %s%.*s %" PRIu64 "%s\r\n", verb,
26 ptr->prefix_key,
27 (int)key_length, key,
28 offset, no_reply ? " noreply" : "");
29 unlikely (send_length >= MEMCACHED_DEFAULT_COMMAND_SIZE)
30 return MEMCACHED_WRITE_FAILURE;
31
32 rc= memcached_do(&ptr->hosts[server_key], buffer, send_length, 1);
33 if (no_reply || rc != MEMCACHED_SUCCESS)
34 return rc;
35
36 rc= memcached_response(&ptr->hosts[server_key], buffer, MEMCACHED_DEFAULT_COMMAND_SIZE, NULL);
37
38 /*
39 So why recheck responce? Because the protocol is brain dead :)
40 The number returned might end up equaling one of the string
41 values. Less chance of a mistake with strncmp() so we will
42 use it. We still called memcached_response() though since it
43 worked its magic for non-blocking IO.
44 */
45 if (!strncmp(buffer, "ERROR\r\n", 7))
46 {
47 *value= 0;
48 rc= MEMCACHED_PROTOCOL_ERROR;
49 }
50 else if (!strncmp(buffer, "NOT_FOUND\r\n", 11))
51 {
52 *value= 0;
53 rc= MEMCACHED_NOTFOUND;
54 }
55 else
56 {
57 *value= strtoull(buffer, (char **)NULL, 10);
58 rc= MEMCACHED_SUCCESS;
59 }
60
61 return rc;
62 }
63
64 static memcached_return binary_incr_decr(memcached_st *ptr, uint8_t cmd,
65 const char *master_key, size_t master_key_length,
66 const char *key, size_t key_length,
67 uint64_t offset, uint64_t initial,
68 uint32_t expiration,
69 uint64_t *value)
70 {
71 unsigned int server_key;
72 bool no_reply= ptr->flags.no_reply;
73
74 unlikely (ptr->hosts == NULL || ptr->number_of_hosts == 0)
75 return MEMCACHED_NO_SERVERS;
76
77 server_key= memcached_generate_hash(ptr, master_key, master_key_length);
78
79 if (no_reply)
80 {
81 if(cmd == PROTOCOL_BINARY_CMD_DECREMENT)
82 cmd= PROTOCOL_BINARY_CMD_DECREMENTQ;
83 if(cmd == PROTOCOL_BINARY_CMD_INCREMENT)
84 cmd= PROTOCOL_BINARY_CMD_INCREMENTQ;
85 }
86 protocol_binary_request_incr request= {.bytes= {0}};
87
88 request.message.header.request.magic= PROTOCOL_BINARY_REQ;
89 request.message.header.request.opcode= cmd;
90 request.message.header.request.keylen= htons((uint16_t) key_length);
91 request.message.header.request.extlen= 20;
92 request.message.header.request.datatype= PROTOCOL_BINARY_RAW_BYTES;
93 request.message.header.request.bodylen= htonl((uint32_t) (key_length + request.message.header.request.extlen));
94 request.message.body.delta= htonll(offset);
95 request.message.body.initial= htonll(initial);
96 request.message.body.expiration= htonl((uint32_t) expiration);
97
98 if ((memcached_do(&ptr->hosts[server_key], request.bytes,
99 sizeof(request.bytes), 0)!=MEMCACHED_SUCCESS) ||
100 (memcached_io_write(&ptr->hosts[server_key], key, key_length, 1) == -1))
101 {
102 memcached_io_reset(&ptr->hosts[server_key]);
103 return MEMCACHED_WRITE_FAILURE;
104 }
105
106 if (no_reply)
107 return MEMCACHED_SUCCESS;
108 return memcached_response(&ptr->hosts[server_key], (char*)value, sizeof(*value), NULL);
109 }
110
111 memcached_return memcached_increment(memcached_st *ptr,
112 const char *key, size_t key_length,
113 uint32_t offset,
114 uint64_t *value)
115 {
116 return memcached_increment_by_key(ptr, key, key_length, key, key_length, offset, value);
117 }
118
119 memcached_return memcached_decrement(memcached_st *ptr,
120 const char *key, size_t key_length,
121 uint32_t offset,
122 uint64_t *value)
123 {
124 return memcached_decrement_by_key(ptr, key, key_length, key, key_length, offset, value);
125 }
126
127 memcached_return memcached_increment_by_key(memcached_st *ptr,
128 const char *master_key, size_t master_key_length,
129 const char *key, size_t key_length,
130 uint64_t offset,
131 uint64_t *value)
132 {
133 memcached_return rc= memcached_validate_key_length(key_length, ptr->flags.binary_protocol);
134 unlikely (rc != MEMCACHED_SUCCESS)
135 return rc;
136
137 LIBMEMCACHED_MEMCACHED_INCREMENT_START();
138 if (ptr->flags.binary_protocol)
139 {
140 rc= binary_incr_decr(ptr, PROTOCOL_BINARY_CMD_INCREMENT,
141 master_key, master_key_length, key, key_length,
142 (uint64_t)offset, 0, MEMCACHED_EXPIRATION_NOT_ADD,
143 value);
144 }
145 else
146 {
147 rc= memcached_auto(ptr, "incr", master_key, master_key_length, key, key_length, offset, value);
148 }
149
150 LIBMEMCACHED_MEMCACHED_INCREMENT_END();
151
152 return rc;
153 }
154
155 memcached_return memcached_decrement_by_key(memcached_st *ptr,
156 const char *master_key, size_t master_key_length,
157 const char *key, size_t key_length,
158 uint64_t offset,
159 uint64_t *value)
160 {
161 memcached_return rc= memcached_validate_key_length(key_length, ptr->flags.binary_protocol);
162 unlikely (rc != MEMCACHED_SUCCESS)
163 return rc;
164
165 LIBMEMCACHED_MEMCACHED_DECREMENT_START();
166 if (ptr->flags.binary_protocol)
167 rc= binary_incr_decr(ptr, PROTOCOL_BINARY_CMD_DECREMENT,
168 master_key, master_key_length, key, key_length,
169 (uint64_t)offset, 0, MEMCACHED_EXPIRATION_NOT_ADD,
170 value);
171 else
172 rc= memcached_auto(ptr, "decr", master_key, master_key_length, key, key_length, offset, value);
173
174 LIBMEMCACHED_MEMCACHED_DECREMENT_END();
175
176 return rc;
177 }
178
179 memcached_return memcached_increment_with_initial(memcached_st *ptr,
180 const char *key,
181 size_t key_length,
182 uint64_t offset,
183 uint64_t initial,
184 time_t expiration,
185 uint64_t *value)
186 {
187 return memcached_increment_with_initial_by_key(ptr, key, key_length,
188 key, key_length,
189 offset, initial, expiration, value);
190 }
191
192 memcached_return memcached_increment_with_initial_by_key(memcached_st *ptr,
193 const char *master_key,
194 size_t master_key_length,
195 const char *key,
196 size_t key_length,
197 uint64_t offset,
198 uint64_t initial,
199 time_t expiration,
200 uint64_t *value)
201 {
202 memcached_return rc= memcached_validate_key_length(key_length, ptr->flags.binary_protocol);
203 unlikely (rc != MEMCACHED_SUCCESS)
204 return rc;
205
206 LIBMEMCACHED_MEMCACHED_INCREMENT_WITH_INITIAL_START();
207 if (ptr->flags.binary_protocol)
208 rc= binary_incr_decr(ptr, PROTOCOL_BINARY_CMD_INCREMENT,
209 master_key, master_key_length, key, key_length,
210 offset, initial, (uint32_t)expiration,
211 value);
212 else
213 rc= MEMCACHED_PROTOCOL_ERROR;
214
215 LIBMEMCACHED_MEMCACHED_INCREMENT_WITH_INITIAL_END();
216
217 return rc;
218 }
219
220 memcached_return memcached_decrement_with_initial(memcached_st *ptr,
221 const char *key,
222 size_t key_length,
223 uint64_t offset,
224 uint64_t initial,
225 time_t expiration,
226 uint64_t *value)
227 {
228 return memcached_decrement_with_initial_by_key(ptr, key, key_length,
229 key, key_length,
230 offset, initial, expiration, value);
231 }
232
233 memcached_return memcached_decrement_with_initial_by_key(memcached_st *ptr,
234 const char *master_key,
235 size_t master_key_length,
236 const char *key,
237 size_t key_length,
238 uint64_t offset,
239 uint64_t initial,
240 time_t expiration,
241 uint64_t *value)
242 {
243 memcached_return rc= memcached_validate_key_length(key_length, ptr->flags.binary_protocol);
244 unlikely (rc != MEMCACHED_SUCCESS)
245 return rc;
246
247 LIBMEMCACHED_MEMCACHED_INCREMENT_WITH_INITIAL_START();
248 if (ptr->flags.binary_protocol)
249 {
250 rc= binary_incr_decr(ptr, PROTOCOL_BINARY_CMD_DECREMENT,
251 master_key, master_key_length, key, key_length,
252 offset, initial, (uint32_t)expiration,
253 value);
254 }
255 else
256 {
257 rc= MEMCACHED_PROTOCOL_ERROR;
258 }
259
260 LIBMEMCACHED_MEMCACHED_INCREMENT_WITH_INITIAL_END();
261
262 return rc;
263 }
264