#include <mem_config.h>
#include "clients/execute.h"
unsigned
int
execute_set(memcached_st *memc, pairs_st *pairs, unsigned
int
number_of)
{
unsigned
int
x;
unsigned
int
pairs_sent;
for
(x= 0, pairs_sent= 0; x < number_of; x++)
{
memcached_return_t rc= memcached_set(memc, pairs[x].key, pairs[x].key_length,
pairs[x].value, pairs[x].value_length,
0, 0);
if
(memcached_failed(rc))
{
fprintf
(stderr,
"%s:%d Failure on insert (%s) of %.*s\n"
,
__FILE__, __LINE__,
memcached_last_error_message(memc),
(unsigned
int
)pairs[x].key_length, pairs[x].key);
memcached_quit(memc);
}
else
{
pairs_sent++;
}
}
return
pairs_sent;
}
unsigned
int
execute_get(memcached_st *memc, pairs_st *pairs, unsigned
int
number_of)
{
unsigned
int
x;
unsigned
int
retrieved;
for
(retrieved= 0,x= 0; x < number_of; x++)
{
size_t
value_length;
uint32_t flags;
unsigned
int
fetch_key= (unsigned
int
)((unsigned
int
)random() % number_of);
memcached_return_t rc;
char
*value= memcached_get(memc, pairs[fetch_key].key, pairs[fetch_key].key_length,
&value_length, &flags, &rc);
if
(memcached_failed(rc))
{
fprintf
(stderr,
"%s:%d Failure on read(%s) of %.*s\n"
,
__FILE__, __LINE__,
memcached_last_error_message(memc),
(unsigned
int
)pairs[fetch_key].key_length, pairs[fetch_key].key);
}
else
{
retrieved++;
}
::
free
(value);
}
return
retrieved;
}
static
memcached_return_t callback_counter(
const
memcached_st *ptr,
memcached_result_st *result,
void
*context)
{
(
void
)ptr;
(
void
)result;
unsigned
int
*counter= (unsigned
int
*)context;
*counter= *counter + 1;
return
MEMCACHED_SUCCESS;
}
unsigned
int
execute_mget(memcached_st *memc,
const
char
*
const
*keys,
size_t
*key_length,
unsigned
int
number_of)
{
unsigned
int
retrieved= 0;
memcached_execute_fn callbacks[]= { callback_counter };
memcached_return_t rc;
rc= memcached_mget_execute(memc, keys, key_length,
(
size_t
)number_of, callbacks, &retrieved, 1);
if
(rc == MEMCACHED_SUCCESS || rc == MEMCACHED_NOTFOUND ||
rc == MEMCACHED_BUFFERED || rc == MEMCACHED_END)
{
rc= memcached_fetch_execute(memc, callbacks, (
void
*)&retrieved, 1);
if
(rc != MEMCACHED_SUCCESS && rc != MEMCACHED_NOTFOUND && rc != MEMCACHED_END)
{
fprintf
(stderr,
"%s:%d Failed to execute mget: %s\n"
,
__FILE__, __LINE__,
memcached_strerror(memc, rc));
memcached_quit(memc);
return
0;
}
}
else
{
fprintf
(stderr,
"%s:%d Failed to execute mget: %s\n"
,
__FILE__, __LINE__,
memcached_strerror(memc, rc));
memcached_quit(memc);
return
0;
}
return
retrieved;
}