11 #include "memcached_io.h"
20 } memcached_storage_action
;
23 static char *storage_op_string(memcached_storage_action verb
)
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
,
51 memcached_storage_action verb
)
57 char buffer
[MEMCACHED_DEFAULT_COMMAND_SIZE
];
58 unsigned int server_key
;
60 WATCHPOINT_ASSERT(!(value
== NULL
&& value_length
> 0));
61 WATCHPOINT_ASSERT(!(value
&& value_length
== 0));
64 return MEMCACHED_NO_KEY_PROVIDED
;
66 if (ptr
->number_of_hosts
== 0)
67 return MEMCACHED_NO_SERVERS
;
69 server_key
= memcached_generate_hash(ptr
, master_key
, master_key_length
);
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
);
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
);
83 if (write_length
>= MEMCACHED_DEFAULT_COMMAND_SIZE
)
85 rc
= MEMCACHED_WRITE_FAILURE
;
89 rc
= memcached_do(&ptr
->hosts
[server_key
], buffer
, write_length
, 0);
90 if (rc
!= MEMCACHED_SUCCESS
)
93 if ((sent_length
= memcached_io_write(&ptr
->hosts
[server_key
], value
, value_length
, 0)) == -1)
95 rc
= MEMCACHED_WRITE_FAILURE
;
99 if ((ptr
->flags
& MEM_BUFFER_REQUESTS
) && verb
== SET_OP
)
104 if ((sent_length
= memcached_io_write(&ptr
->hosts
[server_key
], "\r\n", 2, to_write
)) == -1)
106 rc
= MEMCACHED_WRITE_FAILURE
;
111 return MEMCACHED_BUFFERED
;
113 rc
= memcached_response(&ptr
->hosts
[server_key
], buffer
, MEMCACHED_DEFAULT_COMMAND_SIZE
, NULL
);
115 if (rc
== MEMCACHED_STORED
)
116 return MEMCACHED_SUCCESS
;
121 memcached_io_reset(&ptr
->hosts
[server_key
]);
126 memcached_return
memcached_set(memcached_st
*ptr
, char *key
, size_t key_length
,
127 char *value
, size_t value_length
,
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();
140 memcached_return
memcached_add(memcached_st
*ptr
,
141 char *key
, size_t key_length
,
142 char *value
, size_t value_length
,
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();
155 memcached_return
memcached_replace(memcached_st
*ptr
,
156 char *key
, size_t key_length
,
157 char *value
, size_t value_length
,
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();
170 memcached_return
memcached_prepend(memcached_st
*ptr
,
171 char *key
, size_t key_length
,
172 char *value
, size_t value_length
,
177 rc
= memcached_send(ptr
, key
, key_length
,
178 key
, key_length
, value
, value_length
,
179 expiration
, flags
, 0, PREPEND_OP
);
183 memcached_return
memcached_append(memcached_st
*ptr
,
184 char *key
, size_t key_length
,
185 char *value
, size_t value_length
,
190 rc
= memcached_send(ptr
, key
, key_length
,
191 key
, key_length
, value
, value_length
,
192 expiration
, flags
, 0, APPEND_OP
);
196 memcached_return
memcached_cas(memcached_st
*ptr
,
197 char *key
, size_t key_length
,
198 char *value
, size_t value_length
,
204 rc
= memcached_send(ptr
, key
, key_length
,
205 key
, key_length
, value
, value_length
,
206 expiration
, flags
, cas
, APPEND_OP
);
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
,
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();
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
,
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();
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
,
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();
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
,
266 rc
= memcached_send(ptr
, key
, key_length
,
267 key
, key_length
, value
, value_length
,
268 expiration
, flags
, 0, PREPEND_OP
);
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
,
280 rc
= memcached_send(ptr
, key
, key_length
,
281 key
, key_length
, value
, value_length
,
282 expiration
, flags
, 0, APPEND_OP
);
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
,
295 rc
= memcached_send(ptr
, key
, key_length
,
296 key
, key_length
, value
, value_length
,
297 expiration
, flags
, cas
, APPEND_OP
);