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));
62 unlikely (key_length
== 0)
63 return MEMCACHED_NO_KEY_PROVIDED
;
65 unlikely (ptr
->number_of_hosts
== 0)
66 return MEMCACHED_NO_SERVERS
;
68 if ((ptr
->flags
& MEM_VERIFY_KEY
) && (memcachd_key_test(&key
, &key_length
, 1) == MEMCACHED_BAD_KEY_PROVIDED
))
69 return MEMCACHED_BAD_KEY_PROVIDED
;
71 server_key
= memcached_generate_hash(ptr
, master_key
, master_key_length
);
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
);
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
);
85 if (write_length
>= MEMCACHED_DEFAULT_COMMAND_SIZE
)
87 rc
= MEMCACHED_WRITE_FAILURE
;
91 rc
= memcached_do(&ptr
->hosts
[server_key
], buffer
, write_length
, 0);
92 if (rc
!= MEMCACHED_SUCCESS
)
95 if ((sent_length
= memcached_io_write(&ptr
->hosts
[server_key
], value
, value_length
, 0)) == -1)
97 rc
= MEMCACHED_WRITE_FAILURE
;
101 if ((ptr
->flags
& MEM_BUFFER_REQUESTS
) && verb
== SET_OP
)
106 if ((sent_length
= memcached_io_write(&ptr
->hosts
[server_key
], "\r\n", 2, to_write
)) == -1)
108 rc
= MEMCACHED_WRITE_FAILURE
;
113 return MEMCACHED_BUFFERED
;
115 rc
= memcached_response(&ptr
->hosts
[server_key
], buffer
, MEMCACHED_DEFAULT_COMMAND_SIZE
, NULL
);
117 if (rc
== MEMCACHED_STORED
)
118 return MEMCACHED_SUCCESS
;
123 memcached_io_reset(&ptr
->hosts
[server_key
]);
128 memcached_return
memcached_set(memcached_st
*ptr
, char *key
, size_t key_length
,
129 char *value
, size_t value_length
,
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();
142 memcached_return
memcached_add(memcached_st
*ptr
,
143 char *key
, size_t key_length
,
144 char *value
, size_t value_length
,
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();
157 memcached_return
memcached_replace(memcached_st
*ptr
,
158 char *key
, size_t key_length
,
159 char *value
, size_t value_length
,
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();
172 memcached_return
memcached_prepend(memcached_st
*ptr
,
173 char *key
, size_t key_length
,
174 char *value
, size_t value_length
,
179 rc
= memcached_send(ptr
, key
, key_length
,
180 key
, key_length
, value
, value_length
,
181 expiration
, flags
, 0, PREPEND_OP
);
185 memcached_return
memcached_append(memcached_st
*ptr
,
186 char *key
, size_t key_length
,
187 char *value
, size_t value_length
,
192 rc
= memcached_send(ptr
, key
, key_length
,
193 key
, key_length
, value
, value_length
,
194 expiration
, flags
, 0, APPEND_OP
);
198 memcached_return
memcached_cas(memcached_st
*ptr
,
199 char *key
, size_t key_length
,
200 char *value
, size_t value_length
,
206 rc
= memcached_send(ptr
, key
, key_length
,
207 key
, key_length
, value
, value_length
,
208 expiration
, flags
, cas
, CAS_OP
);
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
,
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();
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
,
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();
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
,
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();
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
,
268 rc
= memcached_send(ptr
, key
, key_length
,
269 key
, key_length
, value
, value_length
,
270 expiration
, flags
, 0, PREPEND_OP
);
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
,
282 rc
= memcached_send(ptr
, key
, key_length
,
283 key
, key_length
, value
, value_length
,
284 expiration
, flags
, 0, APPEND_OP
);
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
,
297 rc
= memcached_send(ptr
, master_key
, master_key_length
,
298 key
, key_length
, value
, value_length
,
299 expiration
, flags
, cas
, CAS_OP
);