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 const char *master_key
, size_t master_key_length
,
46 const char *key
, size_t key_length
,
47 const 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((char **)&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%.*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%.*s %u %llu %zu\r\n", storage_op_string(verb
),
84 (int)key_length
, key
, flags
,
85 (unsigned long long)expiration
, value_length
);
87 if (write_length
>= MEMCACHED_DEFAULT_COMMAND_SIZE
)
89 rc
= MEMCACHED_WRITE_FAILURE
;
93 rc
= memcached_do(&ptr
->hosts
[server_key
], buffer
, write_length
, 0);
94 if (rc
!= MEMCACHED_SUCCESS
)
97 if ((sent_length
= memcached_io_write(&ptr
->hosts
[server_key
], value
, value_length
, 0)) == -1)
99 rc
= MEMCACHED_WRITE_FAILURE
;
103 if ((ptr
->flags
& MEM_BUFFER_REQUESTS
) && verb
== SET_OP
)
108 if ((sent_length
= memcached_io_write(&ptr
->hosts
[server_key
], "\r\n", 2, to_write
)) == -1)
110 rc
= MEMCACHED_WRITE_FAILURE
;
115 return MEMCACHED_BUFFERED
;
117 rc
= memcached_response(&ptr
->hosts
[server_key
], buffer
, MEMCACHED_DEFAULT_COMMAND_SIZE
, NULL
);
119 if (rc
== MEMCACHED_STORED
)
120 return MEMCACHED_SUCCESS
;
125 memcached_io_reset(&ptr
->hosts
[server_key
]);
130 memcached_return
memcached_set(memcached_st
*ptr
, const char *key
, size_t key_length
,
131 const 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 const char *key
, size_t key_length
,
146 const 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 const char *key
, size_t key_length
,
161 const 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 const char *key
, size_t key_length
,
176 const 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 const char *key
, size_t key_length
,
189 const 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 const char *key
, size_t key_length
,
202 const 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
, CAS_OP
);
214 memcached_return
memcached_set_by_key(memcached_st
*ptr
,
215 const char *master_key
, size_t master_key_length
,
216 const char *key
, size_t key_length
,
217 const 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 const char *master_key
, size_t master_key_length
,
232 const char *key
, size_t key_length
,
233 const 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 const char *master_key
, size_t master_key_length
,
248 const char *key
, size_t key_length
,
249 const 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 const char *master_key
, size_t master_key_length
,
264 const char *key
, size_t key_length
,
265 const 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 const char *master_key
, size_t master_key_length
,
278 const char *key
, size_t key_length
,
279 const 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 const char *master_key
, size_t master_key_length
,
292 const char *key
, size_t key_length
,
293 const char *value
, size_t value_length
,
299 rc
= memcached_send(ptr
, master_key
, master_key_length
,
300 key
, key_length
, value
, value_length
,
301 expiration
, flags
, cas
, CAS_OP
);