Merge from Trond
[m6w6/libmemcached] / clients / execute.c
1 /*
2 Execute a memcached_set() a set of pairs.
3 Return the number of rows set.
4 */
5
6 #include "libmemcached/common.h"
7
8 #include "execute.h"
9
10 unsigned int execute_set(memcached_st *memc, pairs_st *pairs, unsigned int number_of)
11 {
12 memcached_return rc;
13 unsigned int x;
14 unsigned int pairs_sent;
15
16 for (x= 0, pairs_sent= 0; x < number_of; x++)
17 {
18 rc= memcached_set(memc, pairs[x].key, pairs[x].key_length,
19 pairs[x].value, pairs[x].value_length,
20 0, 0);
21 if (rc != MEMCACHED_SUCCESS && rc != MEMCACHED_BUFFERED)
22 fprintf(stderr, "Failured on insert of %.*s\n",
23 (unsigned int)pairs[x].key_length, pairs[x].key);
24 else
25 pairs_sent++;
26 }
27
28 return pairs_sent;
29 }
30
31 /*
32 Execute a memcached_get() on a set of pairs.
33 Return the number of rows retrieved.
34 */
35 unsigned int execute_get(memcached_st *memc, pairs_st *pairs, unsigned int number_of)
36 {
37 memcached_return rc;
38 unsigned int x;
39 unsigned int retrieved;
40
41
42 for (retrieved= 0,x= 0; x < number_of; x++)
43 {
44 char *value;
45 size_t value_length;
46 uint32_t flags;
47 unsigned int fetch_key;
48
49 fetch_key= (unsigned int)((unsigned int)random() % number_of);
50
51 value= memcached_get(memc, pairs[fetch_key].key, pairs[fetch_key].key_length,
52 &value_length, &flags, &rc);
53
54 if (rc != MEMCACHED_SUCCESS)
55 fprintf(stderr, "Failured on read of %.*s\n",
56 (unsigned int)pairs[fetch_key].key_length, pairs[fetch_key].key);
57 else
58 retrieved++;
59
60 free(value);
61 }
62
63 return retrieved;
64 }