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
)
56 memcached_return rc
[MEMCACHED_MAX_REPLICAS
];
57 char buffer
[MEMCACHED_DEFAULT_COMMAND_SIZE
];
58 unsigned int server_key
;
61 WATCHPOINT_ASSERT(!(value
== NULL
&& value_length
> 0));
62 WATCHPOINT_ASSERT(!(value
&& value_length
== 0));
64 unlikely (key_length
== 0)
65 return MEMCACHED_NO_KEY_PROVIDED
;
67 unlikely (ptr
->number_of_hosts
== 0)
68 return MEMCACHED_NO_SERVERS
;
70 if ((ptr
->flags
& MEM_VERIFY_KEY
) && (memcachd_key_test(&key
, &key_length
, 1) == MEMCACHED_BAD_KEY_PROVIDED
))
71 return MEMCACHED_BAD_KEY_PROVIDED
;
73 server_key
= memcached_generate_hash(ptr
, master_key
, master_key_length
);
76 write_length
= snprintf(buffer
, MEMCACHED_DEFAULT_COMMAND_SIZE
,
77 "%s %.*s %u %llu %zu %llu\r\n", storage_op_string(verb
),
78 (int)key_length
, key
, flags
,
79 (unsigned long long)expiration
, value_length
,
80 (unsigned long long)cas
);
82 write_length
= snprintf(buffer
, MEMCACHED_DEFAULT_COMMAND_SIZE
,
83 "%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
)
88 return MEMCACHED_WRITE_FAILURE
;
90 if ((ptr
->flags
& MEM_BUFFER_REQUESTS
) && verb
== SET_OP
)
97 rc
[replicas
]= memcached_do(&ptr
->hosts
[server_key
], buffer
, write_length
, 0);
99 if (rc
[replicas
] != MEMCACHED_SUCCESS
)
102 if ((sent_length
= memcached_io_write(&ptr
->hosts
[server_key
], value
, value_length
, 0)) == -1)
104 rc
[replicas
]= MEMCACHED_WRITE_FAILURE
;
108 if ((sent_length
= memcached_io_write(&ptr
->hosts
[server_key
], "\r\n", 2, to_write
)) == -1)
110 rc
[replicas
]= MEMCACHED_WRITE_FAILURE
;
115 return MEMCACHED_BUFFERED
;
117 rc
[replicas
]= memcached_response(&ptr
->hosts
[server_key
], buffer
, MEMCACHED_DEFAULT_COMMAND_SIZE
, NULL
);
119 /* On error we just jump to the next potential server */
121 if (replicas
> 1 && ptr
->distribution
== MEMCACHED_DISTRIBUTION_CONSISTENT
)
123 if (server_key
== (ptr
->number_of_hosts
- 1))
128 } while ((++replicas
) < ptr
->number_of_replicas
);
130 /* As long as one object gets stored, we count this as a success */
133 if (rc
[replicas
] == MEMCACHED_STORED
)
134 return MEMCACHED_SUCCESS
;
140 memcached_return
memcached_set(memcached_st
*ptr
, char *key
, size_t key_length
,
141 char *value
, size_t value_length
,
146 LIBMEMCACHED_MEMCACHED_SET_START();
147 rc
= memcached_send(ptr
, key
, key_length
,
148 key
, key_length
, value
, value_length
,
149 expiration
, flags
, 0, SET_OP
);
150 LIBMEMCACHED_MEMCACHED_SET_END();
154 memcached_return
memcached_add(memcached_st
*ptr
,
155 char *key
, size_t key_length
,
156 char *value
, size_t value_length
,
161 LIBMEMCACHED_MEMCACHED_ADD_START();
162 rc
= memcached_send(ptr
, key
, key_length
,
163 key
, key_length
, value
, value_length
,
164 expiration
, flags
, 0, ADD_OP
);
165 LIBMEMCACHED_MEMCACHED_ADD_END();
169 memcached_return
memcached_replace(memcached_st
*ptr
,
170 char *key
, size_t key_length
,
171 char *value
, size_t value_length
,
176 LIBMEMCACHED_MEMCACHED_REPLACE_START();
177 rc
= memcached_send(ptr
, key
, key_length
,
178 key
, key_length
, value
, value_length
,
179 expiration
, flags
, 0, REPLACE_OP
);
180 LIBMEMCACHED_MEMCACHED_REPLACE_END();
184 memcached_return
memcached_prepend(memcached_st
*ptr
,
185 char *key
, size_t key_length
,
186 char *value
, size_t value_length
,
191 rc
= memcached_send(ptr
, key
, key_length
,
192 key
, key_length
, value
, value_length
,
193 expiration
, flags
, 0, PREPEND_OP
);
197 memcached_return
memcached_append(memcached_st
*ptr
,
198 char *key
, size_t key_length
,
199 char *value
, size_t value_length
,
204 rc
= memcached_send(ptr
, key
, key_length
,
205 key
, key_length
, value
, value_length
,
206 expiration
, flags
, 0, APPEND_OP
);
210 memcached_return
memcached_cas(memcached_st
*ptr
,
211 char *key
, size_t key_length
,
212 char *value
, size_t value_length
,
218 rc
= memcached_send(ptr
, key
, key_length
,
219 key
, key_length
, value
, value_length
,
220 expiration
, flags
, cas
, CAS_OP
);
224 memcached_return
memcached_set_by_key(memcached_st
*ptr
,
225 char *master_key
, size_t master_key_length
,
226 char *key
, size_t key_length
,
227 char *value
, size_t value_length
,
232 LIBMEMCACHED_MEMCACHED_SET_START();
233 rc
= memcached_send(ptr
, key
, key_length
,
234 key
, key_length
, value
, value_length
,
235 expiration
, flags
, 0, SET_OP
);
236 LIBMEMCACHED_MEMCACHED_SET_END();
240 memcached_return
memcached_add_by_key(memcached_st
*ptr
,
241 char *master_key
, size_t master_key_length
,
242 char *key
, size_t key_length
,
243 char *value
, size_t value_length
,
248 LIBMEMCACHED_MEMCACHED_ADD_START();
249 rc
= memcached_send(ptr
, key
, key_length
,
250 key
, key_length
, value
, value_length
,
251 expiration
, flags
, 0, ADD_OP
);
252 LIBMEMCACHED_MEMCACHED_ADD_END();
256 memcached_return
memcached_replace_by_key(memcached_st
*ptr
,
257 char *master_key
, size_t master_key_length
,
258 char *key
, size_t key_length
,
259 char *value
, size_t value_length
,
264 LIBMEMCACHED_MEMCACHED_REPLACE_START();
265 rc
= memcached_send(ptr
, key
, key_length
,
266 key
, key_length
, value
, value_length
,
267 expiration
, flags
, 0, REPLACE_OP
);
268 LIBMEMCACHED_MEMCACHED_REPLACE_END();
272 memcached_return
memcached_prepend_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, PREPEND_OP
);
286 memcached_return
memcached_append_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
,
294 rc
= memcached_send(ptr
, key
, key_length
,
295 key
, key_length
, value
, value_length
,
296 expiration
, flags
, 0, APPEND_OP
);
300 memcached_return
memcached_cas_by_key(memcached_st
*ptr
,
301 char *master_key
, size_t master_key_length
,
302 char *key
, size_t key_length
,
303 char *value
, size_t value_length
,
309 rc
= memcached_send(ptr
, key
, key_length
,
310 key
, key_length
, value
, value_length
,
311 expiration
, flags
, cas
, CAS_OP
);