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