11 #include "memcached_io.h"
20 } memcached_storage_action
;
23 static char *storage_op_string(memcached_storage_action verb
)
44 static memcached_return
memcached_msend(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 unsigned int number_of_keys
,
52 memcached_storage_action verb
)
56 char buffer
[MEMCACHED_DEFAULT_COMMAND_SIZE
];
58 unsigned int master_server_key
= 0;
59 memcached_return rc
= MEMCACHED_SUCCESS
;
61 WATCHPOINT_ASSERT(key
&& key_length
&& value
&& value_length
);
64 return MEMCACHED_NO_KEY_PROVIDED
;
66 if (ptr
->number_of_hosts
== 0)
67 return MEMCACHED_NO_SERVERS
;
69 if (master_key
&& master_key_length
)
70 master_server_key
= memcached_generate_hash(ptr
, master_key
, master_key_length
);
72 for (x
= 0; x
< number_of_keys
; x
++)
74 unsigned int server_key
;
75 memcached_return tried
;
77 if (master_server_key
)
78 server_key
= master_server_key
;
80 server_key
= memcached_generate_hash(ptr
, key
[x
], key_length
[x
]);
84 write_length
= snprintf(buffer
, MEMCACHED_DEFAULT_COMMAND_SIZE
,
85 "%s %.*s %u %llu %zu %llu\r\n", storage_op_string(verb
),
86 (int)key_length
[x
], key
[x
], flags
,
87 (unsigned long long)expiration
, value_length
[x
],
88 (unsigned long long)cas
);
90 write_length
= snprintf(buffer
, MEMCACHED_DEFAULT_COMMAND_SIZE
,
91 "%s %.*s %u %llu %zu\r\n", storage_op_string(verb
),
92 (int)key_length
[x
], key
[x
], flags
,
93 (unsigned long long)expiration
, value_length
[x
]);
95 if (write_length
>= MEMCACHED_DEFAULT_COMMAND_SIZE
)
98 tried
= memcached_do(ptr
, server_key
, buffer
, write_length
, 0);
99 if (tried
!= MEMCACHED_SUCCESS
)
102 if ((sent_length
= memcached_io_write(ptr
, server_key
, value
[x
], value_length
[x
], 0)) == -1)
105 if ((sent_length
= memcached_io_write(ptr
, server_key
, "\r\n", 2, 0)) == -1)
111 memcached_quit_server(ptr
, server_key
, 1);
115 for (x
= 0; x
< ptr
->number_of_hosts
; x
++)
117 if (memcached_server_response_count(ptr
, x
))
119 /* We need to do something about non-connnected hosts in the future */
120 if ((memcached_io_write(ptr
, x
, NULL
, 0, 1)) == -1)
122 rc
= MEMCACHED_SOME_ERRORS
;
130 memcached_return
memcached_mset(memcached_st
*ptr
,
131 char **key
, size_t *key_length
,
132 char **value
, size_t *value_length
,
133 unsigned int number_of_keys
,
138 LIBMEMCACHED_MEMCACHED_SET_START();
139 rc
= memcached_msend(ptr
, NULL
, 0,
140 key
, key_length
, value
, value_length
,
142 expiration
, flags
, 0, SET_OP
);
143 LIBMEMCACHED_MEMCACHED_SET_END();
147 memcached_return
memcached_madd(memcached_st
*ptr
,
148 char **key
, size_t *key_length
,
149 char **value
, size_t *value_length
,
150 unsigned int number_of_keys
,
155 LIBMEMCACHED_MEMCACHED_ADD_START();
156 rc
= memcached_msend(ptr
, NULL
, 0,
157 key
, key_length
, value
, value_length
,
159 expiration
, flags
, 0, ADD_OP
);
160 LIBMEMCACHED_MEMCACHED_ADD_END();
164 memcached_return
memcached_mreplace(memcached_st
*ptr
,
165 char **key
, size_t *key_length
,
166 char **value
, size_t *value_length
,
167 unsigned int number_of_keys
,
172 LIBMEMCACHED_MEMCACHED_REPLACE_START();
173 rc
= memcached_msend(ptr
, NULL
, 0,
174 key
, key_length
, value
, value_length
,
176 expiration
, flags
, 0, REPLACE_OP
);
177 LIBMEMCACHED_MEMCACHED_REPLACE_END();
181 memcached_return
memcached_mprepend(memcached_st
*ptr
,
182 char **key
, size_t *key_length
,
183 char **value
, size_t *value_length
,
184 unsigned int number_of_keys
,
189 rc
= memcached_msend(ptr
, NULL
, 0,
190 key
, key_length
, value
, value_length
,
192 expiration
, flags
, 0, PREPEND_OP
);
196 memcached_return
memcached_mappend(memcached_st
*ptr
,
197 char **key
, size_t *key_length
,
198 char **value
, size_t *value_length
,
199 unsigned int number_of_keys
,
204 rc
= memcached_msend(ptr
, NULL
, 0,
205 key
, key_length
, value
, value_length
,
207 expiration
, flags
, 0, APPEND_OP
);
211 memcached_return
memcached_mcas(memcached_st
*ptr
,
212 char **key
, size_t *key_length
,
213 char **value
, size_t *value_length
,
214 unsigned int number_of_keys
,
220 rc
= memcached_msend(ptr
, NULL
, 0,
221 key
, key_length
, value
, value_length
,
223 expiration
, flags
, cas
, APPEND_OP
);
227 memcached_return
memcached_mset_by_key(memcached_st
*ptr
,
228 char *master_key
, size_t master_key_length
,
229 char **key
, size_t *key_length
,
230 char **value
, size_t *value_length
,
231 unsigned int number_of_keys
,
236 LIBMEMCACHED_MEMCACHED_SET_START();
237 rc
= memcached_msend(ptr
, master_key
, master_key_length
,
238 key
, key_length
, value
, value_length
,
240 expiration
, flags
, 0, SET_OP
);
241 LIBMEMCACHED_MEMCACHED_SET_END();
245 memcached_return
memcached_madd_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 unsigned int number_of_keys
,
254 LIBMEMCACHED_MEMCACHED_ADD_START();
255 rc
= memcached_msend(ptr
, master_key
, master_key_length
,
256 key
, key_length
, value
, value_length
,
258 expiration
, flags
, 0, ADD_OP
);
259 LIBMEMCACHED_MEMCACHED_ADD_END();
263 memcached_return
memcached_mreplace_by_key(memcached_st
*ptr
,
264 char *master_key
, size_t master_key_length
,
265 char **key
, size_t *key_length
,
266 char **value
, size_t *value_length
,
267 unsigned int number_of_keys
,
272 LIBMEMCACHED_MEMCACHED_REPLACE_START();
273 rc
= memcached_msend(ptr
, master_key
, master_key_length
,
274 key
, key_length
, value
, value_length
,
276 expiration
, flags
, 0, REPLACE_OP
);
277 LIBMEMCACHED_MEMCACHED_REPLACE_END();
281 memcached_return
memcached_mprepend_by_key(memcached_st
*ptr
,
282 char *master_key
, size_t master_key_length
,
283 char **key
, size_t *key_length
,
284 char **value
, size_t *value_length
,
285 unsigned int number_of_keys
,
290 rc
= memcached_msend(ptr
, master_key
, master_key_length
,
291 key
, key_length
, value
, value_length
,
293 expiration
, flags
, 0, PREPEND_OP
);
297 memcached_return
memcached_mappend_by_key(memcached_st
*ptr
,
298 char *master_key
, size_t master_key_length
,
299 char **key
, size_t *key_length
,
300 char **value
, size_t *value_length
,
301 unsigned int number_of_keys
,
306 rc
= memcached_msend(ptr
, master_key
, master_key_length
,
307 key
, key_length
, value
, value_length
,
309 expiration
, flags
, 0, APPEND_OP
);
313 memcached_return
memcached_mcas_by_key(memcached_st
*ptr
,
314 char *master_key
, size_t master_key_length
,
315 char **key
, size_t *key_length
,
316 char **value
, size_t *value_length
,
317 unsigned int number_of_keys
,
323 rc
= memcached_msend(ptr
, master_key
, master_key_length
,
324 key
, key_length
, value
, value_length
,
326 expiration
, flags
, cas
, APPEND_OP
);