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));
63 unlikely (key_length
== 0)
64 return MEMCACHED_NO_KEY_PROVIDED
;
66 unlikely (ptr
->number_of_hosts
== 0)
67 return MEMCACHED_NO_SERVERS
;
69 if ((ptr
->flags
& MEM_VERIFY_KEY
) && (memcachd_key_test(&key
, &key_length
, 1) == MEMCACHED_BAD_KEY_PROVIDED
))
70 return MEMCACHED_BAD_KEY_PROVIDED
;
72 server_key
= memcached_generate_hash(ptr
, master_key
, master_key_length
);
75 write_length
= snprintf(buffer
, MEMCACHED_DEFAULT_COMMAND_SIZE
,
76 "%s %.*s %u %llu %zu %llu\r\n", storage_op_string(verb
),
77 (int)key_length
, key
, flags
,
78 (unsigned long long)expiration
, value_length
,
79 (unsigned long long)cas
);
81 write_length
= snprintf(buffer
, MEMCACHED_DEFAULT_COMMAND_SIZE
,
82 "%s %.*s %u %llu %zu\r\n", storage_op_string(verb
),
83 (int)key_length
, key
, flags
,
84 (unsigned long long)expiration
, value_length
);
86 if (write_length
>= MEMCACHED_DEFAULT_COMMAND_SIZE
)
88 rc
= MEMCACHED_WRITE_FAILURE
;
92 rc
= memcached_do(&ptr
->hosts
[server_key
], buffer
, write_length
, 0);
93 if (rc
!= MEMCACHED_SUCCESS
)
96 if ((sent_length
= memcached_io_write(&ptr
->hosts
[server_key
], value
, value_length
, 0)) == -1)
98 rc
= MEMCACHED_WRITE_FAILURE
;
102 if ((ptr
->flags
& MEM_BUFFER_REQUESTS
) && verb
== SET_OP
)
107 if ((sent_length
= memcached_io_write(&ptr
->hosts
[server_key
], "\r\n", 2, to_write
)) == -1)
109 rc
= MEMCACHED_WRITE_FAILURE
;
114 return MEMCACHED_BUFFERED
;
116 rc
= memcached_response(&ptr
->hosts
[server_key
], buffer
, MEMCACHED_DEFAULT_COMMAND_SIZE
, NULL
);
118 if (rc
== MEMCACHED_STORED
)
119 return MEMCACHED_SUCCESS
;
124 memcached_io_reset(&ptr
->hosts
[server_key
]);
129 memcached_return
memcached_set(memcached_st
*ptr
, char *key
, size_t key_length
,
130 char *value
, size_t value_length
,
135 LIBMEMCACHED_MEMCACHED_SET_START();
136 rc
= memcached_send(ptr
, key
, key_length
,
137 key
, key_length
, value
, value_length
,
138 expiration
, flags
, 0, SET_OP
);
139 LIBMEMCACHED_MEMCACHED_SET_END();
143 memcached_return
memcached_add(memcached_st
*ptr
,
144 char *key
, size_t key_length
,
145 char *value
, size_t value_length
,
150 LIBMEMCACHED_MEMCACHED_ADD_START();
151 rc
= memcached_send(ptr
, key
, key_length
,
152 key
, key_length
, value
, value_length
,
153 expiration
, flags
, 0, ADD_OP
);
154 LIBMEMCACHED_MEMCACHED_ADD_END();
158 memcached_return
memcached_replace(memcached_st
*ptr
,
159 char *key
, size_t key_length
,
160 char *value
, size_t value_length
,
165 LIBMEMCACHED_MEMCACHED_REPLACE_START();
166 rc
= memcached_send(ptr
, key
, key_length
,
167 key
, key_length
, value
, value_length
,
168 expiration
, flags
, 0, REPLACE_OP
);
169 LIBMEMCACHED_MEMCACHED_REPLACE_END();
173 memcached_return
memcached_prepend(memcached_st
*ptr
,
174 char *key
, size_t key_length
,
175 char *value
, size_t value_length
,
180 rc
= memcached_send(ptr
, key
, key_length
,
181 key
, key_length
, value
, value_length
,
182 expiration
, flags
, 0, PREPEND_OP
);
186 memcached_return
memcached_append(memcached_st
*ptr
,
187 char *key
, size_t key_length
,
188 char *value
, size_t value_length
,
193 rc
= memcached_send(ptr
, key
, key_length
,
194 key
, key_length
, value
, value_length
,
195 expiration
, flags
, 0, APPEND_OP
);
199 memcached_return
memcached_cas(memcached_st
*ptr
,
200 char *key
, size_t key_length
,
201 char *value
, size_t value_length
,
207 rc
= memcached_send(ptr
, key
, key_length
,
208 key
, key_length
, value
, value_length
,
209 expiration
, flags
, cas
, CAS_OP
);
213 memcached_return
memcached_set_by_key(memcached_st
*ptr
,
214 char *master_key
, size_t master_key_length
,
215 char *key
, size_t key_length
,
216 char *value
, size_t value_length
,
221 LIBMEMCACHED_MEMCACHED_SET_START();
222 rc
= memcached_send(ptr
, key
, key_length
,
223 key
, key_length
, value
, value_length
,
224 expiration
, flags
, 0, SET_OP
);
225 LIBMEMCACHED_MEMCACHED_SET_END();
229 memcached_return
memcached_add_by_key(memcached_st
*ptr
,
230 char *master_key
, size_t master_key_length
,
231 char *key
, size_t key_length
,
232 char *value
, size_t value_length
,
237 LIBMEMCACHED_MEMCACHED_ADD_START();
238 rc
= memcached_send(ptr
, key
, key_length
,
239 key
, key_length
, value
, value_length
,
240 expiration
, flags
, 0, ADD_OP
);
241 LIBMEMCACHED_MEMCACHED_ADD_END();
245 memcached_return
memcached_replace_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
,
253 LIBMEMCACHED_MEMCACHED_REPLACE_START();
254 rc
= memcached_send(ptr
, key
, key_length
,
255 key
, key_length
, value
, value_length
,
256 expiration
, flags
, 0, REPLACE_OP
);
257 LIBMEMCACHED_MEMCACHED_REPLACE_END();
261 memcached_return
memcached_prepend_by_key(memcached_st
*ptr
,
262 char *master_key
, size_t master_key_length
,
263 char *key
, size_t key_length
,
264 char *value
, size_t value_length
,
269 rc
= memcached_send(ptr
, key
, key_length
,
270 key
, key_length
, value
, value_length
,
271 expiration
, flags
, 0, PREPEND_OP
);
275 memcached_return
memcached_append_by_key(memcached_st
*ptr
,
276 char *master_key
, size_t master_key_length
,
277 char *key
, size_t key_length
,
278 char *value
, size_t value_length
,
283 rc
= memcached_send(ptr
, key
, key_length
,
284 key
, key_length
, value
, value_length
,
285 expiration
, flags
, 0, APPEND_OP
);
289 memcached_return
memcached_cas_by_key(memcached_st
*ptr
,
290 char *master_key
, size_t master_key_length
,
291 char *key
, size_t key_length
,
292 char *value
, size_t value_length
,
298 rc
= memcached_send(ptr
, master_key
, master_key_length
,
299 key
, key_length
, value
, value_length
,
300 expiration
, flags
, cas
, CAS_OP
);