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 unsigned int pairs_sent
;
26 for (x
= 0, pairs_sent
= 0; x
< number_of
; x
++)
28 memcached_return_t rc
= memcached_set(memc
, pairs
[x
].key
, pairs
[x
].key_length
,
29 pairs
[x
].value
, pairs
[x
].value_length
,
31 if (memcached_failed(rc
))
33 fprintf(stderr
, "%s:%d Failure on insert (%s) of %.*s\n",
35 memcached_last_error_message(memc
),
36 (unsigned int)pairs
[x
].key_length
, pairs
[x
].key
);
38 // We will try to reconnect and see if that fixes the issue
51 Execute a memcached_get() on a set of pairs.
52 Return the number of rows retrieved.
54 unsigned int execute_get(memcached_st
*memc
, pairs_st
*pairs
, unsigned int number_of
)
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 memcached_return_t rc
;
68 char *value
= memcached_get(memc
, pairs
[fetch_key
].key
, pairs
[fetch_key
].key_length
,
69 &value_length
, &flags
, &rc
);
71 if (memcached_failed(rc
))
73 fprintf(stderr
, "%s:%d Failure on read(%s) of %.*s\n",
75 memcached_last_error_message(memc
),
76 (unsigned int)pairs
[fetch_key
].key_length
, pairs
[fetch_key
].key
);
90 * Callback function to count the number of results
92 static memcached_return_t
callback_counter(const memcached_st
*ptr
,
93 memcached_result_st
*result
,
98 unsigned int *counter
= (unsigned int *)context
;
99 *counter
= *counter
+ 1;
101 return MEMCACHED_SUCCESS
;
105 * Try to run a large mget to get all of the keys
106 * @param memc memcached handle
107 * @param keys the keys to get
108 * @param key_length the length of the keys
109 * @param number_of the number of keys to try to get
110 * @return the number of keys received
112 unsigned int execute_mget(memcached_st
*memc
,
113 const char * const *keys
,
115 unsigned int number_of
)
117 unsigned int retrieved
= 0;
118 memcached_execute_fn callbacks
[]= { callback_counter
};
119 memcached_return_t rc
;
120 rc
= memcached_mget_execute(memc
, keys
, key_length
,
121 (size_t)number_of
, callbacks
, &retrieved
, 1);
123 if (rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_NOTFOUND
||
124 rc
== MEMCACHED_BUFFERED
|| rc
== MEMCACHED_END
)
126 rc
= memcached_fetch_execute(memc
, callbacks
, (void *)&retrieved
, 1);
127 if (rc
!= MEMCACHED_SUCCESS
&& rc
!= MEMCACHED_NOTFOUND
&& rc
!= MEMCACHED_END
)
129 fprintf(stderr
, "%s:%d Failed to execute mget: %s\n",
131 memcached_strerror(memc
, rc
));
132 memcached_quit(memc
);
138 fprintf(stderr
, "%s:%d Failed to execute mget: %s\n",
140 memcached_strerror(memc
, rc
));
141 memcached_quit(memc
);