Fix for flags operation (aka not storing the final bit right). Updated
[awesomized/libmemcached] / libmemcached / memcached_storage.c
1 /*
2 Memcached library
3
4 memcached_set()
5 memcached_replace()
6 memcached_add()
7
8 */
9
10 #include "common.h"
11 #include "memcached_io.h"
12
13 typedef enum {
14 SET_OP,
15 REPLACE_OP,
16 ADD_OP,
17 PREPEND_OP,
18 APPEND_OP,
19 CAS_OP,
20 } memcached_storage_action;
21
22 /* Inline this */
23 static char *storage_op_string(memcached_storage_action verb)
24 {
25 switch (verb)
26 {
27 case SET_OP:
28 return "set";
29 case REPLACE_OP:
30 return "replace";
31 case ADD_OP:
32 return "add";
33 case PREPEND_OP:
34 return "prepend";
35 case APPEND_OP:
36 return "append";
37 case CAS_OP:
38 return "cas";
39 default:
40 return "tosserror"; /* This is impossible, fixes issue for compiler warning in VisualStudio */
41 };
42
43 return SET_OP;
44 }
45
46 static inline memcached_return memcached_send(memcached_st *ptr,
47 const char *master_key, size_t master_key_length,
48 const char *key, size_t key_length,
49 const char *value, size_t value_length,
50 time_t expiration,
51 uint32_t flags,
52 uint64_t cas,
53 memcached_storage_action verb)
54 {
55 char to_write;
56 size_t write_length;
57 ssize_t sent_length;
58 memcached_return rc;
59 char buffer[MEMCACHED_DEFAULT_COMMAND_SIZE];
60 unsigned int server_key;
61
62 WATCHPOINT_ASSERT(!(value == NULL && value_length > 0));
63
64 unlikely (key_length == 0)
65 return MEMCACHED_NO_KEY_PROVIDED;
66
67 unlikely (ptr->number_of_hosts == 0)
68 return MEMCACHED_NO_SERVERS;
69
70 if ((ptr->flags & MEM_VERIFY_KEY) && (memcachd_key_test((char **)&key, &key_length, 1) == MEMCACHED_BAD_KEY_PROVIDED))
71 return MEMCACHED_BAD_KEY_PROVIDED;
72
73 server_key= memcached_generate_hash(ptr, master_key, master_key_length);
74
75 if (cas)
76 write_length= snprintf(buffer, MEMCACHED_DEFAULT_COMMAND_SIZE,
77 "%s %s%.*s %u %llu %zu %llu\r\n", storage_op_string(verb),
78 ptr->prefix_key,
79 (int)key_length, key, flags,
80 (unsigned long long)expiration, value_length,
81 (unsigned long long)cas);
82 else
83 write_length= snprintf(buffer, MEMCACHED_DEFAULT_COMMAND_SIZE,
84 "%s %s%.*s %u %llu %zu\r\n", storage_op_string(verb),
85 ptr->prefix_key,
86 (int)key_length, key, flags,
87 (unsigned long long)expiration, value_length);
88
89 if (write_length >= MEMCACHED_DEFAULT_COMMAND_SIZE)
90 {
91 rc= MEMCACHED_WRITE_FAILURE;
92 goto error;
93 }
94
95 rc= memcached_do(&ptr->hosts[server_key], buffer, write_length, 0);
96 if (rc != MEMCACHED_SUCCESS)
97 goto error;
98
99 if ((sent_length= memcached_io_write(&ptr->hosts[server_key], value, value_length, 0)) == -1)
100 {
101 rc= MEMCACHED_WRITE_FAILURE;
102 goto error;
103 }
104
105 if ((ptr->flags & MEM_BUFFER_REQUESTS) && verb == SET_OP)
106 to_write= 0;
107 else
108 to_write= 1;
109
110 if ((sent_length= memcached_io_write(&ptr->hosts[server_key], "\r\n", 2, to_write)) == -1)
111 {
112 rc= MEMCACHED_WRITE_FAILURE;
113 goto error;
114 }
115
116 if (to_write == 0)
117 return MEMCACHED_BUFFERED;
118
119 rc= memcached_response(&ptr->hosts[server_key], buffer, MEMCACHED_DEFAULT_COMMAND_SIZE, NULL);
120
121 if (rc == MEMCACHED_STORED)
122 return MEMCACHED_SUCCESS;
123 else
124 return rc;
125
126 error:
127 memcached_io_reset(&ptr->hosts[server_key]);
128
129 return rc;
130 }
131
132 memcached_return memcached_set(memcached_st *ptr, const char *key, size_t key_length,
133 const char *value, size_t value_length,
134 time_t expiration,
135 uint32_t flags)
136 {
137 memcached_return rc;
138 LIBMEMCACHED_MEMCACHED_SET_START();
139 rc= memcached_send(ptr, key, key_length,
140 key, key_length, value, value_length,
141 expiration, flags, 0, SET_OP);
142 LIBMEMCACHED_MEMCACHED_SET_END();
143 return rc;
144 }
145
146 memcached_return memcached_add(memcached_st *ptr,
147 const char *key, size_t key_length,
148 const char *value, size_t value_length,
149 time_t expiration,
150 uint32_t flags)
151 {
152 memcached_return rc;
153 LIBMEMCACHED_MEMCACHED_ADD_START();
154 rc= memcached_send(ptr, key, key_length,
155 key, key_length, value, value_length,
156 expiration, flags, 0, ADD_OP);
157 LIBMEMCACHED_MEMCACHED_ADD_END();
158 return rc;
159 }
160
161 memcached_return memcached_replace(memcached_st *ptr,
162 const char *key, size_t key_length,
163 const char *value, size_t value_length,
164 time_t expiration,
165 uint32_t flags)
166 {
167 memcached_return rc;
168 LIBMEMCACHED_MEMCACHED_REPLACE_START();
169 rc= memcached_send(ptr, key, key_length,
170 key, key_length, value, value_length,
171 expiration, flags, 0, REPLACE_OP);
172 LIBMEMCACHED_MEMCACHED_REPLACE_END();
173 return rc;
174 }
175
176 memcached_return memcached_prepend(memcached_st *ptr,
177 const char *key, size_t key_length,
178 const char *value, size_t value_length,
179 time_t expiration,
180 uint32_t flags)
181 {
182 memcached_return rc;
183 rc= memcached_send(ptr, key, key_length,
184 key, key_length, value, value_length,
185 expiration, flags, 0, PREPEND_OP);
186 return rc;
187 }
188
189 memcached_return memcached_append(memcached_st *ptr,
190 const char *key, size_t key_length,
191 const char *value, size_t value_length,
192 time_t expiration,
193 uint32_t flags)
194 {
195 memcached_return rc;
196 rc= memcached_send(ptr, key, key_length,
197 key, key_length, value, value_length,
198 expiration, flags, 0, APPEND_OP);
199 return rc;
200 }
201
202 memcached_return memcached_cas(memcached_st *ptr,
203 const char *key, size_t key_length,
204 const char *value, size_t value_length,
205 time_t expiration,
206 uint32_t flags,
207 uint64_t cas)
208 {
209 memcached_return rc;
210 rc= memcached_send(ptr, key, key_length,
211 key, key_length, value, value_length,
212 expiration, flags, cas, CAS_OP);
213 return rc;
214 }
215
216 memcached_return memcached_set_by_key(memcached_st *ptr,
217 const char *master_key, size_t master_key_length,
218 const char *key, size_t key_length,
219 const char *value, size_t value_length,
220 time_t expiration,
221 uint32_t flags)
222 {
223 memcached_return rc;
224 LIBMEMCACHED_MEMCACHED_SET_START();
225 rc= memcached_send(ptr, key, key_length,
226 key, key_length, value, value_length,
227 expiration, flags, 0, SET_OP);
228 LIBMEMCACHED_MEMCACHED_SET_END();
229 return rc;
230 }
231
232 memcached_return memcached_add_by_key(memcached_st *ptr,
233 const char *master_key, size_t master_key_length,
234 const char *key, size_t key_length,
235 const char *value, size_t value_length,
236 time_t expiration,
237 uint32_t flags)
238 {
239 memcached_return rc;
240 LIBMEMCACHED_MEMCACHED_ADD_START();
241 rc= memcached_send(ptr, key, key_length,
242 key, key_length, value, value_length,
243 expiration, flags, 0, ADD_OP);
244 LIBMEMCACHED_MEMCACHED_ADD_END();
245 return rc;
246 }
247
248 memcached_return memcached_replace_by_key(memcached_st *ptr,
249 const char *master_key, size_t master_key_length,
250 const char *key, size_t key_length,
251 const char *value, size_t value_length,
252 time_t expiration,
253 uint32_t flags)
254 {
255 memcached_return rc;
256 LIBMEMCACHED_MEMCACHED_REPLACE_START();
257 rc= memcached_send(ptr, key, key_length,
258 key, key_length, value, value_length,
259 expiration, flags, 0, REPLACE_OP);
260 LIBMEMCACHED_MEMCACHED_REPLACE_END();
261 return rc;
262 }
263
264 memcached_return memcached_prepend_by_key(memcached_st *ptr,
265 const char *master_key, size_t master_key_length,
266 const char *key, size_t key_length,
267 const char *value, size_t value_length,
268 time_t expiration,
269 uint32_t flags)
270 {
271 memcached_return rc;
272 rc= memcached_send(ptr, key, key_length,
273 key, key_length, value, value_length,
274 expiration, flags, 0, PREPEND_OP);
275 return rc;
276 }
277
278 memcached_return memcached_append_by_key(memcached_st *ptr,
279 const char *master_key, size_t master_key_length,
280 const char *key, size_t key_length,
281 const char *value, size_t value_length,
282 time_t expiration,
283 uint32_t flags)
284 {
285 memcached_return rc;
286 rc= memcached_send(ptr, key, key_length,
287 key, key_length, value, value_length,
288 expiration, flags, 0, APPEND_OP);
289 return rc;
290 }
291
292 memcached_return memcached_cas_by_key(memcached_st *ptr,
293 const char *master_key, size_t master_key_length,
294 const char *key, size_t key_length,
295 const char *value, size_t value_length,
296 time_t expiration,
297 uint32_t flags,
298 uint64_t cas)
299 {
300 memcached_return rc;
301 rc= memcached_send(ptr, master_key, master_key_length,
302 key, key_length, value, value_length,
303 expiration, flags, cas, CAS_OP);
304 return rc;
305 }