11 #include "memcached_io.h"
20 } memcached_storage_action
;
23 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
, server_key
, buffer
, write_length
, 0);
90 if (rc
!= MEMCACHED_SUCCESS
)
93 if ((sent_length
= memcached_io_write(ptr
, server_key
, value
, value_length
, 0)) == -1)
95 rc
= MEMCACHED_WRITE_FAILURE
;
99 if ((ptr
->flags
& MEM_NO_BLOCK
) && verb
== SET_OP
)
104 if ((sent_length
= memcached_io_write(ptr
, server_key
, "\r\n", 2, to_write
)) == -1)
106 rc
= MEMCACHED_WRITE_FAILURE
;
112 rc
= MEMCACHED_SUCCESS
;
113 memcached_server_response_increment(ptr
, server_key
);
117 rc
= memcached_response(ptr
, buffer
, MEMCACHED_DEFAULT_COMMAND_SIZE
, server_key
);
119 if (rc
== MEMCACHED_STORED
)
120 return MEMCACHED_SUCCESS
;
125 memcached_io_reset(ptr
, server_key
);
130 memcached_return
memcached_set(memcached_st
*ptr
, char *key
, size_t key_length
,
131 char *value
, size_t value_length
,
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();
144 memcached_return
memcached_add(memcached_st
*ptr
,
145 char *key
, size_t key_length
,
146 char *value
, size_t value_length
,
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();
159 memcached_return
memcached_replace(memcached_st
*ptr
,
160 char *key
, size_t key_length
,
161 char *value
, size_t value_length
,
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();
174 memcached_return
memcached_prepend(memcached_st
*ptr
,
175 char *key
, size_t key_length
,
176 char *value
, size_t value_length
,
181 rc
= memcached_send(ptr
, key
, key_length
,
182 key
, key_length
, value
, value_length
,
183 expiration
, flags
, 0, PREPEND_OP
);
187 memcached_return
memcached_append(memcached_st
*ptr
,
188 char *key
, size_t key_length
,
189 char *value
, size_t value_length
,
194 rc
= memcached_send(ptr
, key
, key_length
,
195 key
, key_length
, value
, value_length
,
196 expiration
, flags
, 0, APPEND_OP
);
200 memcached_return
memcached_cas(memcached_st
*ptr
,
201 char *key
, size_t key_length
,
202 char *value
, size_t value_length
,
208 rc
= memcached_send(ptr
, key
, key_length
,
209 key
, key_length
, value
, value_length
,
210 expiration
, flags
, cas
, APPEND_OP
);
214 memcached_return
memcached_set_by_key(memcached_st
*ptr
,
215 char *master_key
, size_t master_key_length
,
216 char *key
, size_t key_length
,
217 char *value
, size_t value_length
,
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();
230 memcached_return
memcached_add_by_key(memcached_st
*ptr
,
231 char *master_key
, size_t master_key_length
,
232 char *key
, size_t key_length
,
233 char *value
, size_t value_length
,
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();
246 memcached_return
memcached_replace_by_key(memcached_st
*ptr
,
247 char *master_key
, size_t master_key_length
,
248 char *key
, size_t key_length
,
249 char *value
, size_t value_length
,
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();
262 memcached_return
memcached_prepend_by_key(memcached_st
*ptr
,
263 char *master_key
, size_t master_key_length
,
264 char *key
, size_t key_length
,
265 char *value
, size_t value_length
,
270 rc
= memcached_send(ptr
, key
, key_length
,
271 key
, key_length
, value
, value_length
,
272 expiration
, flags
, 0, PREPEND_OP
);
276 memcached_return
memcached_append_by_key(memcached_st
*ptr
,
277 char *master_key
, size_t master_key_length
,
278 char *key
, size_t key_length
,
279 char *value
, size_t value_length
,
284 rc
= memcached_send(ptr
, key
, key_length
,
285 key
, key_length
, value
, value_length
,
286 expiration
, flags
, 0, APPEND_OP
);
290 memcached_return
memcached_cas_by_key(memcached_st
*ptr
,
291 char *master_key
, size_t master_key_length
,
292 char *key
, size_t key_length
,
293 char *value
, size_t value_length
,
299 rc
= memcached_send(ptr
, key
, key_length
,
300 key
, key_length
, value
, value_length
,
301 expiration
, flags
, cas
, APPEND_OP
);