2 * Copyright (C) 2011-2012 Data Differential, http://datadifferential.com/
3 * Copyright (C) 2006-2009 Brian Aker
6 * Use and distribution licensed under the BSD license. See
7 * the COPYING file in the parent directory for full text.
14 Execute a memcached_set() a set of pairs.
15 Return the number of rows set.
18 #include <mem_config.h>
19 #include "clients/execute.h"
21 unsigned int execute_set(memcached_st
*memc
, pairs_st
*pairs
, unsigned int number_of
)
24 for (; count
< number_of
; ++count
)
26 memcached_return_t rc
= memcached_set(memc
, pairs
[count
].key
, pairs
[count
].key_length
,
27 pairs
[count
].value
, pairs
[count
].value_length
,
29 if (memcached_failed(rc
))
31 fprintf(stderr
, "%s:%d Failure on %u insert (%s) of %.*s\n",
32 __FILE__
, __LINE__
, count
,
33 memcached_last_error_message(memc
),
34 (unsigned int)pairs
[count
].key_length
, pairs
[count
].key
);
36 // We will try to reconnect and see if that fixes the issue
47 Execute a memcached_get() on a set of pairs.
48 Return the number of rows retrieved.
50 unsigned int execute_get(memcached_st
*memc
, pairs_st
*pairs
, unsigned int number_of
)
53 unsigned int retrieved
;
56 for (retrieved
= 0,x
= 0; x
< number_of
; x
++)
61 unsigned int fetch_key
= (unsigned int)((unsigned int)random() % number_of
);
63 memcached_return_t rc
;
64 char *value
= memcached_get(memc
, pairs
[fetch_key
].key
, pairs
[fetch_key
].key_length
,
65 &value_length
, &flags
, &rc
);
67 if (memcached_failed(rc
))
69 fprintf(stderr
, "%s:%d Failure on read(%s) of %.*s\n",
71 memcached_last_error_message(memc
),
72 (unsigned int)pairs
[fetch_key
].key_length
, pairs
[fetch_key
].key
);
86 * Callback function to count the number of results
88 static memcached_return_t
callback_counter(const memcached_st
*ptr
,
89 memcached_result_st
*result
,
94 unsigned int *counter
= (unsigned int *)context
;
95 *counter
= *counter
+ 1;
97 return MEMCACHED_SUCCESS
;
101 * Try to run a large mget to get all of the keys
102 * @param memc memcached handle
103 * @param keys the keys to get
104 * @param key_length the length of the keys
105 * @param number_of the number of keys to try to get
106 * @return the number of keys received
108 unsigned int execute_mget(memcached_st
*memc
,
109 const char * const *keys
,
111 unsigned int number_of
)
113 unsigned int retrieved
= 0;
114 memcached_execute_fn callbacks
[]= { callback_counter
};
115 memcached_return_t rc
;
116 rc
= memcached_mget_execute(memc
, keys
, key_length
,
117 (size_t)number_of
, callbacks
, &retrieved
, 1);
119 if (rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_NOTFOUND
||
120 rc
== MEMCACHED_BUFFERED
|| rc
== MEMCACHED_END
)
122 rc
= memcached_fetch_execute(memc
, callbacks
, (void *)&retrieved
, 1);
123 if (rc
!= MEMCACHED_SUCCESS
&& rc
!= MEMCACHED_NOTFOUND
&& rc
!= MEMCACHED_END
)
125 fprintf(stderr
, "%s:%d Failed to execute mget: %s\n",
127 memcached_strerror(memc
, rc
));
128 memcached_quit(memc
);
134 fprintf(stderr
, "%s:%d Failed to execute mget: %s\n",
136 memcached_strerror(memc
, rc
));
137 memcached_quit(memc
);