4c36edc6dfc8ff2b8af9222410eef0b4aba3fab3
[awesomized/libmemcached] / lib / 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 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 };
40
41 return SET_OP;
42 }
43
44 static inline memcached_return memcached_send(memcached_st *ptr,
45 char *master_key, size_t master_key_length,
46 char *key, size_t key_length,
47 char *value, size_t value_length,
48 time_t expiration,
49 uint32_t flags,
50 uint64_t cas,
51 memcached_storage_action verb)
52 {
53 char to_write;
54 size_t write_length;
55 ssize_t sent_length;
56 memcached_return rc;
57 char buffer[MEMCACHED_DEFAULT_COMMAND_SIZE];
58 unsigned int server_key;
59
60 WATCHPOINT_ASSERT(!(value == NULL && value_length > 0));
61 WATCHPOINT_ASSERT(!(value && value_length == 0));
62
63 if (key_length == 0)
64 return MEMCACHED_NO_KEY_PROVIDED;
65
66 if (ptr->number_of_hosts == 0)
67 return MEMCACHED_NO_SERVERS;
68
69 server_key= memcached_generate_hash(ptr, master_key, master_key_length);
70
71 if (cas)
72 write_length= snprintf(buffer, MEMCACHED_DEFAULT_COMMAND_SIZE,
73 "%s %.*s %u %llu %zu %llu\r\n", storage_op_string(verb),
74 (int)key_length, key, flags,
75 (unsigned long long)expiration, value_length,
76 (unsigned long long)cas);
77 else
78 write_length= snprintf(buffer, MEMCACHED_DEFAULT_COMMAND_SIZE,
79 "%s %.*s %u %llu %zu\r\n", storage_op_string(verb),
80 (int)key_length, key, flags,
81 (unsigned long long)expiration, value_length);
82
83 if (write_length >= MEMCACHED_DEFAULT_COMMAND_SIZE)
84 {
85 rc= MEMCACHED_WRITE_FAILURE;
86 goto error;
87 }
88
89 rc= memcached_do(ptr, server_key, buffer, write_length, 0);
90 if (rc != MEMCACHED_SUCCESS)
91 goto error;
92
93 if ((sent_length= memcached_io_write(ptr, server_key, value, value_length, 0)) == -1)
94 {
95 rc= MEMCACHED_WRITE_FAILURE;
96 goto error;
97 }
98
99 if ((ptr->flags & MEM_NO_BLOCK) && verb == SET_OP)
100 to_write= 0;
101 else
102 to_write= 1;
103
104 if ((sent_length= memcached_io_write(ptr, server_key, "\r\n", 2, to_write)) == -1)
105 {
106 rc= MEMCACHED_WRITE_FAILURE;
107 goto error;
108 }
109
110 if (to_write == 0)
111 rc= MEMCACHED_SUCCESS;
112 else
113 rc= memcached_response(ptr, buffer, MEMCACHED_DEFAULT_COMMAND_SIZE, NULL, server_key);
114
115 if (rc == MEMCACHED_STORED)
116 return MEMCACHED_SUCCESS;
117 else
118 return rc;
119
120 error:
121 memcached_io_reset(ptr, server_key);
122
123 return rc;
124 }
125
126 memcached_return memcached_set(memcached_st *ptr, char *key, size_t key_length,
127 char *value, size_t value_length,
128 time_t expiration,
129 uint32_t flags)
130 {
131 memcached_return rc;
132 LIBMEMCACHED_MEMCACHED_SET_START();
133 rc= memcached_send(ptr, key, key_length,
134 key, key_length, value, value_length,
135 expiration, flags, 0, SET_OP);
136 LIBMEMCACHED_MEMCACHED_SET_END();
137 return rc;
138 }
139
140 memcached_return memcached_add(memcached_st *ptr,
141 char *key, size_t key_length,
142 char *value, size_t value_length,
143 time_t expiration,
144 uint32_t flags)
145 {
146 memcached_return rc;
147 LIBMEMCACHED_MEMCACHED_ADD_START();
148 rc= memcached_send(ptr, key, key_length,
149 key, key_length, value, value_length,
150 expiration, flags, 0, ADD_OP);
151 LIBMEMCACHED_MEMCACHED_ADD_END();
152 return rc;
153 }
154
155 memcached_return memcached_replace(memcached_st *ptr,
156 char *key, size_t key_length,
157 char *value, size_t value_length,
158 time_t expiration,
159 uint32_t flags)
160 {
161 memcached_return rc;
162 LIBMEMCACHED_MEMCACHED_REPLACE_START();
163 rc= memcached_send(ptr, key, key_length,
164 key, key_length, value, value_length,
165 expiration, flags, 0, REPLACE_OP);
166 LIBMEMCACHED_MEMCACHED_REPLACE_END();
167 return rc;
168 }
169
170 memcached_return memcached_prepend(memcached_st *ptr,
171 char *key, size_t key_length,
172 char *value, size_t value_length,
173 time_t expiration,
174 uint32_t flags)
175 {
176 memcached_return rc;
177 rc= memcached_send(ptr, key, key_length,
178 key, key_length, value, value_length,
179 expiration, flags, 0, PREPEND_OP);
180 return rc;
181 }
182
183 memcached_return memcached_append(memcached_st *ptr,
184 char *key, size_t key_length,
185 char *value, size_t value_length,
186 time_t expiration,
187 uint32_t flags)
188 {
189 memcached_return rc;
190 rc= memcached_send(ptr, key, key_length,
191 key, key_length, value, value_length,
192 expiration, flags, 0, APPEND_OP);
193 return rc;
194 }
195
196 memcached_return memcached_cas(memcached_st *ptr,
197 char *key, size_t key_length,
198 char *value, size_t value_length,
199 time_t expiration,
200 uint32_t flags,
201 uint64_t cas)
202 {
203 memcached_return rc;
204 rc= memcached_send(ptr, key, key_length,
205 key, key_length, value, value_length,
206 expiration, flags, cas, APPEND_OP);
207 return rc;
208 }
209
210 memcached_return memcached_set_by_key(memcached_st *ptr,
211 char *master_key, size_t master_key_length,
212 char *key, size_t key_length,
213 char *value, size_t value_length,
214 time_t expiration,
215 uint32_t flags)
216 {
217 memcached_return rc;
218 LIBMEMCACHED_MEMCACHED_SET_START();
219 rc= memcached_send(ptr, key, key_length,
220 key, key_length, value, value_length,
221 expiration, flags, 0, SET_OP);
222 LIBMEMCACHED_MEMCACHED_SET_END();
223 return rc;
224 }
225
226 memcached_return memcached_add_by_key(memcached_st *ptr,
227 char *master_key, size_t master_key_length,
228 char *key, size_t key_length,
229 char *value, size_t value_length,
230 time_t expiration,
231 uint32_t flags)
232 {
233 memcached_return rc;
234 LIBMEMCACHED_MEMCACHED_ADD_START();
235 rc= memcached_send(ptr, key, key_length,
236 key, key_length, value, value_length,
237 expiration, flags, 0, ADD_OP);
238 LIBMEMCACHED_MEMCACHED_ADD_END();
239 return rc;
240 }
241
242 memcached_return memcached_replace_by_key(memcached_st *ptr,
243 char *master_key, size_t master_key_length,
244 char *key, size_t key_length,
245 char *value, size_t value_length,
246 time_t expiration,
247 uint32_t flags)
248 {
249 memcached_return rc;
250 LIBMEMCACHED_MEMCACHED_REPLACE_START();
251 rc= memcached_send(ptr, key, key_length,
252 key, key_length, value, value_length,
253 expiration, flags, 0, REPLACE_OP);
254 LIBMEMCACHED_MEMCACHED_REPLACE_END();
255 return rc;
256 }
257
258 memcached_return memcached_prepend_by_key(memcached_st *ptr,
259 char *master_key, size_t master_key_length,
260 char *key, size_t key_length,
261 char *value, size_t value_length,
262 time_t expiration,
263 uint32_t flags)
264 {
265 memcached_return rc;
266 rc= memcached_send(ptr, key, key_length,
267 key, key_length, value, value_length,
268 expiration, flags, 0, PREPEND_OP);
269 return rc;
270 }
271
272 memcached_return memcached_append_by_key(memcached_st *ptr,
273 char *master_key, size_t master_key_length,
274 char *key, size_t key_length,
275 char *value, size_t value_length,
276 time_t expiration,
277 uint32_t flags)
278 {
279 memcached_return rc;
280 rc= memcached_send(ptr, key, key_length,
281 key, key_length, value, value_length,
282 expiration, flags, 0, APPEND_OP);
283 return rc;
284 }
285
286 memcached_return memcached_cas_by_key(memcached_st *ptr,
287 char *master_key, size_t master_key_length,
288 char *key, size_t key_length,
289 char *value, size_t value_length,
290 time_t expiration,
291 uint32_t flags,
292 uint64_t cas)
293 {
294 memcached_return rc;
295 rc= memcached_send(ptr, key, key_length,
296 key, key_length, value, value_length,
297 expiration, flags, cas, APPEND_OP);
298 return rc;
299 }