add 'save to file' ability to memcat
[m6w6/libmemcached] / clients / execute.c
1 /* LibMemcached
2 * Copyright (C) 2006-2009 Brian Aker
3 * All rights reserved.
4 *
5 * Use and distribution licensed under the BSD license. See
6 * the COPYING file in the parent directory for full text.
7 *
8 * Summary:
9 *
10 */
11
12 /*
13 Execute a memcached_set() a set of pairs.
14 Return the number of rows set.
15 */
16
17 #include "libmemcached/common.h"
18
19 #include "execute.h"
20
21 unsigned int execute_set(memcached_st *memc, pairs_st *pairs, unsigned int number_of)
22 {
23 memcached_return_t rc;
24 unsigned int x;
25 unsigned int pairs_sent;
26
27 for (x= 0, pairs_sent= 0; x < number_of; x++)
28 {
29 rc= memcached_set(memc, pairs[x].key, pairs[x].key_length,
30 pairs[x].value, pairs[x].value_length,
31 0, 0);
32 if (rc != MEMCACHED_SUCCESS && rc != MEMCACHED_BUFFERED)
33 fprintf(stderr, "Failured on insert of %.*s\n",
34 (unsigned int)pairs[x].key_length, pairs[x].key);
35 else
36 pairs_sent++;
37 }
38
39 return pairs_sent;
40 }
41
42 /*
43 Execute a memcached_get() on a set of pairs.
44 Return the number of rows retrieved.
45 */
46 unsigned int execute_get(memcached_st *memc, pairs_st *pairs, unsigned int number_of)
47 {
48 memcached_return_t rc;
49 unsigned int x;
50 unsigned int retrieved;
51
52
53 for (retrieved= 0,x= 0; x < number_of; x++)
54 {
55 char *value;
56 size_t value_length;
57 uint32_t flags;
58 unsigned int fetch_key;
59
60 fetch_key= (unsigned int)((unsigned int)random() % number_of);
61
62 value= memcached_get(memc, pairs[fetch_key].key, pairs[fetch_key].key_length,
63 &value_length, &flags, &rc);
64
65 if (rc != MEMCACHED_SUCCESS)
66 fprintf(stderr, "Failured on read of %.*s\n",
67 (unsigned int)pairs[fetch_key].key_length, pairs[fetch_key].key);
68 else
69 retrieved++;
70
71 free(value);
72 }
73
74 return retrieved;
75 }
76
77 /**
78 * Callback function to count the number of results
79 */
80 static memcached_return_t callback_counter(const memcached_st *ptr,
81 memcached_result_st *result,
82 void *context)
83 {
84 (void)ptr;
85 (void)result;
86 unsigned int *counter= (unsigned int *)context;
87 *counter= *counter + 1;
88
89 return MEMCACHED_SUCCESS;
90 }
91
92 /**
93 * Try to run a large mget to get all of the keys
94 * @param memc memcached handle
95 * @param keys the keys to get
96 * @param key_length the length of the keys
97 * @param number_of the number of keys to try to get
98 * @return the number of keys received
99 */
100 unsigned int execute_mget(memcached_st *memc,
101 const char * const *keys,
102 size_t *key_length,
103 unsigned int number_of)
104 {
105 unsigned int retrieved= 0;
106 memcached_execute_fn callbacks[1]= { [0]= &callback_counter };
107 memcached_return_t rc;
108 rc= memcached_mget_execute(memc, keys, key_length,
109 (size_t)number_of, callbacks, &retrieved, 1);
110
111 likely (rc == MEMCACHED_SUCCESS || rc == MEMCACHED_NOTFOUND ||
112 rc == MEMCACHED_BUFFERED || rc == MEMCACHED_END)
113 {
114 rc= memcached_fetch_execute(memc, callbacks, (void *)&retrieved, 1);
115 unlikely (rc != MEMCACHED_SUCCESS && rc != MEMCACHED_NOTFOUND &&
116 rc != MEMCACHED_END)
117 {
118 fprintf(stderr, "Failed to execute mget: %s\n",
119 memcached_strerror(memc, rc));
120 memcached_quit(memc);
121 return 0;
122 }
123 }
124 else
125 {
126 fprintf(stderr, "Failed to execute mget: %s\n",
127 memcached_strerror(memc, rc));
128 memcached_quit(memc);
129 return 0;
130 }
131
132 return retrieved;
133 }