11 #include "memcached_io.h"
20 } memcached_storage_action
;
23 static char *storage_op_string(memcached_storage_action verb
)
40 return "tosserror"; /* This is impossible, fixes issue for compiler warning in VisualStudio */
46 static inline memcached_return
memcached_send(memcached_st
*ptr
,
47 const char *master_key
, size_t master_key_length
,
48 const char *key
, size_t key_length
,
49 const char *value
, size_t value_length
,
53 memcached_storage_action verb
)
59 char buffer
[MEMCACHED_DEFAULT_COMMAND_SIZE
];
60 unsigned int server_key
;
62 WATCHPOINT_ASSERT(!(value
== NULL
&& 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((char **)&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%.*s %u %llu %zu %llu\r\n", storage_op_string(verb
),
79 (int)key_length
, key
, flags
,
80 (unsigned long long)expiration
, value_length
,
81 (unsigned long long)cas
);
83 write_length
= snprintf(buffer
, MEMCACHED_DEFAULT_COMMAND_SIZE
,
84 "%s %s%.*s %u %llu %zu\r\n", storage_op_string(verb
),
86 (int)key_length
, key
, flags
,
87 (unsigned long long)expiration
, value_length
);
89 if (write_length
>= MEMCACHED_DEFAULT_COMMAND_SIZE
)
91 rc
= MEMCACHED_WRITE_FAILURE
;
95 rc
= memcached_do(&ptr
->hosts
[server_key
], buffer
, write_length
, 0);
96 if (rc
!= MEMCACHED_SUCCESS
)
99 if ((sent_length
= memcached_io_write(&ptr
->hosts
[server_key
], value
, value_length
, 0)) == -1)
101 rc
= MEMCACHED_WRITE_FAILURE
;
105 if ((ptr
->flags
& MEM_BUFFER_REQUESTS
) && verb
== SET_OP
)
110 if ((sent_length
= memcached_io_write(&ptr
->hosts
[server_key
], "\r\n", 2, to_write
)) == -1)
112 rc
= MEMCACHED_WRITE_FAILURE
;
117 return MEMCACHED_BUFFERED
;
119 rc
= memcached_response(&ptr
->hosts
[server_key
], buffer
, MEMCACHED_DEFAULT_COMMAND_SIZE
, NULL
);
121 if (rc
== MEMCACHED_STORED
)
122 return MEMCACHED_SUCCESS
;
127 memcached_io_reset(&ptr
->hosts
[server_key
]);
132 memcached_return
memcached_set(memcached_st
*ptr
, const char *key
, size_t key_length
,
133 const char *value
, size_t value_length
,
138 LIBMEMCACHED_MEMCACHED_SET_START();
139 rc
= memcached_send(ptr
, key
, key_length
,
140 key
, key_length
, value
, value_length
,
141 expiration
, flags
, 0, SET_OP
);
142 LIBMEMCACHED_MEMCACHED_SET_END();
146 memcached_return
memcached_add(memcached_st
*ptr
,
147 const char *key
, size_t key_length
,
148 const char *value
, size_t value_length
,
153 LIBMEMCACHED_MEMCACHED_ADD_START();
154 rc
= memcached_send(ptr
, key
, key_length
,
155 key
, key_length
, value
, value_length
,
156 expiration
, flags
, 0, ADD_OP
);
157 LIBMEMCACHED_MEMCACHED_ADD_END();
161 memcached_return
memcached_replace(memcached_st
*ptr
,
162 const char *key
, size_t key_length
,
163 const char *value
, size_t value_length
,
168 LIBMEMCACHED_MEMCACHED_REPLACE_START();
169 rc
= memcached_send(ptr
, key
, key_length
,
170 key
, key_length
, value
, value_length
,
171 expiration
, flags
, 0, REPLACE_OP
);
172 LIBMEMCACHED_MEMCACHED_REPLACE_END();
176 memcached_return
memcached_prepend(memcached_st
*ptr
,
177 const char *key
, size_t key_length
,
178 const char *value
, size_t value_length
,
183 rc
= memcached_send(ptr
, key
, key_length
,
184 key
, key_length
, value
, value_length
,
185 expiration
, flags
, 0, PREPEND_OP
);
189 memcached_return
memcached_append(memcached_st
*ptr
,
190 const char *key
, size_t key_length
,
191 const char *value
, size_t value_length
,
196 rc
= memcached_send(ptr
, key
, key_length
,
197 key
, key_length
, value
, value_length
,
198 expiration
, flags
, 0, APPEND_OP
);
202 memcached_return
memcached_cas(memcached_st
*ptr
,
203 const char *key
, size_t key_length
,
204 const char *value
, size_t value_length
,
210 rc
= memcached_send(ptr
, key
, key_length
,
211 key
, key_length
, value
, value_length
,
212 expiration
, flags
, cas
, CAS_OP
);
216 memcached_return
memcached_set_by_key(memcached_st
*ptr
,
217 const char *master_key
, size_t master_key_length
,
218 const char *key
, size_t key_length
,
219 const char *value
, size_t value_length
,
224 LIBMEMCACHED_MEMCACHED_SET_START();
225 rc
= memcached_send(ptr
, key
, key_length
,
226 key
, key_length
, value
, value_length
,
227 expiration
, flags
, 0, SET_OP
);
228 LIBMEMCACHED_MEMCACHED_SET_END();
232 memcached_return
memcached_add_by_key(memcached_st
*ptr
,
233 const char *master_key
, size_t master_key_length
,
234 const char *key
, size_t key_length
,
235 const char *value
, size_t value_length
,
240 LIBMEMCACHED_MEMCACHED_ADD_START();
241 rc
= memcached_send(ptr
, key
, key_length
,
242 key
, key_length
, value
, value_length
,
243 expiration
, flags
, 0, ADD_OP
);
244 LIBMEMCACHED_MEMCACHED_ADD_END();
248 memcached_return
memcached_replace_by_key(memcached_st
*ptr
,
249 const char *master_key
, size_t master_key_length
,
250 const char *key
, size_t key_length
,
251 const char *value
, size_t value_length
,
256 LIBMEMCACHED_MEMCACHED_REPLACE_START();
257 rc
= memcached_send(ptr
, key
, key_length
,
258 key
, key_length
, value
, value_length
,
259 expiration
, flags
, 0, REPLACE_OP
);
260 LIBMEMCACHED_MEMCACHED_REPLACE_END();
264 memcached_return
memcached_prepend_by_key(memcached_st
*ptr
,
265 const char *master_key
, size_t master_key_length
,
266 const char *key
, size_t key_length
,
267 const char *value
, size_t value_length
,
272 rc
= memcached_send(ptr
, key
, key_length
,
273 key
, key_length
, value
, value_length
,
274 expiration
, flags
, 0, PREPEND_OP
);
278 memcached_return
memcached_append_by_key(memcached_st
*ptr
,
279 const char *master_key
, size_t master_key_length
,
280 const char *key
, size_t key_length
,
281 const char *value
, size_t value_length
,
286 rc
= memcached_send(ptr
, key
, key_length
,
287 key
, key_length
, value
, value_length
,
288 expiration
, flags
, 0, APPEND_OP
);
292 memcached_return
memcached_cas_by_key(memcached_st
*ptr
,
293 const char *master_key
, size_t master_key_length
,
294 const char *key
, size_t key_length
,
295 const char *value
, size_t value_length
,
301 rc
= memcached_send(ptr
, master_key
, master_key_length
,
302 key
, key_length
, value
, value_length
,
303 expiration
, flags
, cas
, CAS_OP
);