2 * Copyright (C) 2006-2009 Brian Aker
5 * Use and distribution licensed under the BSD license. See
6 * the COPYING file in the parent directory for full text.
13 Execute a memcached_set() a set of pairs.
14 Return the number of rows set.
18 #include "clients/execute.h"
20 unsigned int execute_set(memcached_st
*memc
, pairs_st
*pairs
, unsigned int number_of
)
23 unsigned int pairs_sent
;
25 for (x
= 0, pairs_sent
= 0; x
< number_of
; x
++)
27 memcached_return_t rc
= memcached_set(memc
, pairs
[x
].key
, pairs
[x
].key_length
,
28 pairs
[x
].value
, pairs
[x
].value_length
,
30 if (rc
!= MEMCACHED_SUCCESS
and rc
!= MEMCACHED_BUFFERED
)
32 fprintf(stderr
, "%s:%d Failure on insert (%s) of %.*s\n",
34 memcached_last_error_message(memc
),
35 (unsigned int)pairs
[x
].key_length
, pairs
[x
].key
);
37 // We will try to reconnect and see if that fixes the issue
50 Execute a memcached_get() on a set of pairs.
51 Return the number of rows retrieved.
53 unsigned int execute_get(memcached_st
*memc
, pairs_st
*pairs
, unsigned int number_of
)
55 memcached_return_t rc
;
57 unsigned int retrieved
;
60 for (retrieved
= 0,x
= 0; x
< number_of
; x
++)
65 unsigned int fetch_key
= (unsigned int)((unsigned int)random() % number_of
);
67 char *value
= memcached_get(memc
, pairs
[fetch_key
].key
, pairs
[fetch_key
].key_length
,
68 &value_length
, &flags
, &rc
);
70 if (rc
!= MEMCACHED_SUCCESS
)
72 fprintf(stderr
, "%s:%d Failure on read(%s) of %.*s\n",
74 memcached_last_error_message(memc
),
75 (unsigned int)pairs
[fetch_key
].key_length
, pairs
[fetch_key
].key
);
89 * Callback function to count the number of results
91 static memcached_return_t
callback_counter(const memcached_st
*ptr
,
92 memcached_result_st
*result
,
97 unsigned int *counter
= (unsigned int *)context
;
98 *counter
= *counter
+ 1;
100 return MEMCACHED_SUCCESS
;
104 * Try to run a large mget to get all of the keys
105 * @param memc memcached handle
106 * @param keys the keys to get
107 * @param key_length the length of the keys
108 * @param number_of the number of keys to try to get
109 * @return the number of keys received
111 unsigned int execute_mget(memcached_st
*memc
,
112 const char * const *keys
,
114 unsigned int number_of
)
116 unsigned int retrieved
= 0;
117 memcached_execute_fn callbacks
[]= { callback_counter
};
118 memcached_return_t rc
;
119 rc
= memcached_mget_execute(memc
, keys
, key_length
,
120 (size_t)number_of
, callbacks
, &retrieved
, 1);
122 if (rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_NOTFOUND
||
123 rc
== MEMCACHED_BUFFERED
|| rc
== MEMCACHED_END
)
125 rc
= memcached_fetch_execute(memc
, callbacks
, (void *)&retrieved
, 1);
126 if (rc
!= MEMCACHED_SUCCESS
&& rc
!= MEMCACHED_NOTFOUND
&& rc
!= MEMCACHED_END
)
128 fprintf(stderr
, "%s:%d Failed to execute mget: %s\n",
130 memcached_strerror(memc
, rc
));
131 memcached_quit(memc
);
137 fprintf(stderr
, "%s:%d Failed to execute mget: %s\n",
139 memcached_strerror(memc
, rc
));
140 memcached_quit(memc
);