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