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