9fce267502f94d6501c6ae47e1d42dc53468c7db
[awesomized/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 unsigned int 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 & MEM_NOREPLY);
15
16 unlikely (ptr->hosts == NULL || ptr->number_of_hosts == 0)
17 return MEMCACHED_NO_SERVERS;
18
19 if ((ptr->flags & MEM_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 %u%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 & MEM_NOREPLY);
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 uint32_t offset,
131 uint64_t *value)
132 {
133 memcached_return rc= memcached_validate_key_length(key_length, ptr->flags & MEM_BINARY_PROTOCOL);
134 unlikely (rc != MEMCACHED_SUCCESS)
135 return rc;
136
137 LIBMEMCACHED_MEMCACHED_INCREMENT_START();
138 if (ptr->flags & MEM_BINARY_PROTOCOL)
139 rc= binary_incr_decr(ptr, PROTOCOL_BINARY_CMD_INCREMENT,
140 master_key, master_key_length, key, key_length,
141 (uint64_t)offset, 0, MEMCACHED_EXPIRATION_NOT_ADD,
142 value);
143 else
144 rc= memcached_auto(ptr, "incr", master_key, master_key_length, key, key_length, offset, value);
145
146 LIBMEMCACHED_MEMCACHED_INCREMENT_END();
147
148 return rc;
149 }
150
151 memcached_return memcached_decrement_by_key(memcached_st *ptr,
152 const char *master_key, size_t master_key_length,
153 const char *key, size_t key_length,
154 uint32_t offset,
155 uint64_t *value)
156 {
157 memcached_return rc= memcached_validate_key_length(key_length, ptr->flags & MEM_BINARY_PROTOCOL);
158 unlikely (rc != MEMCACHED_SUCCESS)
159 return rc;
160
161 LIBMEMCACHED_MEMCACHED_DECREMENT_START();
162 if (ptr->flags & MEM_BINARY_PROTOCOL)
163 rc= binary_incr_decr(ptr, PROTOCOL_BINARY_CMD_DECREMENT,
164 master_key, master_key_length, key, key_length,
165 (uint64_t)offset, 0, MEMCACHED_EXPIRATION_NOT_ADD,
166 value);
167 else
168 rc= memcached_auto(ptr, "decr", master_key, master_key_length, key, key_length, offset, value);
169
170 LIBMEMCACHED_MEMCACHED_DECREMENT_END();
171
172 return rc;
173 }
174
175 memcached_return memcached_increment_with_initial(memcached_st *ptr,
176 const char *key,
177 size_t key_length,
178 uint64_t offset,
179 uint64_t initial,
180 time_t expiration,
181 uint64_t *value)
182 {
183 return memcached_increment_by_key_with_initial(ptr, key, key_length,
184 key, key_length,
185 offset, initial, expiration, value);
186 }
187
188 memcached_return memcached_increment_by_key_with_initial(memcached_st *ptr,
189 const char *master_key,
190 size_t master_key_length,
191 const char *key,
192 size_t key_length,
193 uint64_t offset,
194 uint64_t initial,
195 time_t expiration,
196 uint64_t *value)
197 {
198 memcached_return rc= memcached_validate_key_length(key_length, ptr->flags & MEM_BINARY_PROTOCOL);
199 unlikely (rc != MEMCACHED_SUCCESS)
200 return rc;
201
202 LIBMEMCACHED_MEMCACHED_INCREMENT_WITH_INITIAL_START();
203 if (ptr->flags & MEM_BINARY_PROTOCOL)
204 rc= binary_incr_decr(ptr, PROTOCOL_BINARY_CMD_INCREMENT,
205 master_key, master_key_length, key, key_length,
206 offset, initial, (uint32_t)expiration,
207 value);
208 else
209 rc= MEMCACHED_PROTOCOL_ERROR;
210
211 LIBMEMCACHED_MEMCACHED_INCREMENT_WITH_INITIAL_END();
212
213 return rc;
214 }
215
216 memcached_return memcached_decrement_with_initial(memcached_st *ptr,
217 const char *key,
218 size_t key_length,
219 uint64_t offset,
220 uint64_t initial,
221 time_t expiration,
222 uint64_t *value)
223 {
224 return memcached_decrement_by_key_with_initial(ptr, key, key_length,
225 key, key_length,
226 offset, initial, expiration, value);
227 }
228
229 memcached_return memcached_decrement_by_key_with_initial(memcached_st *ptr,
230 const char *master_key,
231 size_t master_key_length,
232 const char *key,
233 size_t key_length,
234 uint64_t offset,
235 uint64_t initial,
236 time_t expiration,
237 uint64_t *value)
238 {
239 memcached_return rc= memcached_validate_key_length(key_length, ptr->flags & MEM_BINARY_PROTOCOL);
240 unlikely (rc != MEMCACHED_SUCCESS)
241 return rc;
242
243 LIBMEMCACHED_MEMCACHED_INCREMENT_WITH_INITIAL_START();
244 if (ptr->flags & MEM_BINARY_PROTOCOL)
245 rc= binary_incr_decr(ptr, PROTOCOL_BINARY_CMD_DECREMENT,
246 master_key, master_key_length, key, key_length,
247 offset, initial, (uint32_t)expiration,
248 value);
249 else
250 rc= MEMCACHED_PROTOCOL_ERROR;
251
252 LIBMEMCACHED_MEMCACHED_INCREMENT_WITH_INITIAL_END();
253
254 return rc;
255 }
256