f877851e8adb50743779f0cee7844af535df3f1d
[awesomized/libmemcached] / clients / execute.cc
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 <config.h>
18 #include "clients/execute.h"
19
20 unsigned int execute_set(memcached_st *memc, pairs_st *pairs, unsigned int number_of)
21 {
22 unsigned int x;
23 unsigned int pairs_sent;
24
25 for (x= 0, pairs_sent= 0; x < number_of; x++)
26 {
27 memcached_return_t rc= memcached_set(memc, pairs[x].key, pairs[x].key_length,
28 pairs[x].value, pairs[x].value_length,
29 0, 0);
30 if (memcached_failed(rc))
31 {
32 fprintf(stderr, "%s:%d Failure on insert (%s) of %.*s\n",
33 __FILE__, __LINE__,
34 memcached_last_error_message(memc),
35 (unsigned int)pairs[x].key_length, pairs[x].key);
36
37 // We will try to reconnect and see if that fixes the issue
38 memcached_quit(memc);
39 }
40 else
41 {
42 pairs_sent++;
43 }
44 }
45
46 return pairs_sent;
47 }
48
49 /*
50 Execute a memcached_get() on a set of pairs.
51 Return the number of rows retrieved.
52 */
53 unsigned int execute_get(memcached_st *memc, pairs_st *pairs, unsigned int number_of)
54 {
55 unsigned int x;
56 unsigned int retrieved;
57
58
59 for (retrieved= 0,x= 0; x < number_of; x++)
60 {
61 size_t value_length;
62 uint32_t flags;
63
64 unsigned int fetch_key= (unsigned int)((unsigned int)random() % number_of);
65
66 memcached_return_t rc;
67 char *value= memcached_get(memc, pairs[fetch_key].key, pairs[fetch_key].key_length,
68 &value_length, &flags, &rc);
69
70 if (memcached_failed(rc))
71 {
72 fprintf(stderr, "%s:%d Failure on read(%s) of %.*s\n",
73 __FILE__, __LINE__,
74 memcached_last_error_message(memc),
75 (unsigned int)pairs[fetch_key].key_length, pairs[fetch_key].key);
76 }
77 else
78 {
79 retrieved++;
80 }
81
82 ::free(value);
83 }
84
85 return retrieved;
86 }
87
88 /**
89 * Callback function to count the number of results
90 */
91 static memcached_return_t callback_counter(const memcached_st *ptr,
92 memcached_result_st *result,
93 void *context)
94 {
95 (void)ptr;
96 (void)result;
97 unsigned int *counter= (unsigned int *)context;
98 *counter= *counter + 1;
99
100 return MEMCACHED_SUCCESS;
101 }
102
103 /**
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
110 */
111 unsigned int execute_mget(memcached_st *memc,
112 const char * const *keys,
113 size_t *key_length,
114 unsigned int number_of)
115 {
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);
121
122 if (rc == MEMCACHED_SUCCESS || rc == MEMCACHED_NOTFOUND ||
123 rc == MEMCACHED_BUFFERED || rc == MEMCACHED_END)
124 {
125 rc= memcached_fetch_execute(memc, callbacks, (void *)&retrieved, 1);
126 if (rc != MEMCACHED_SUCCESS && rc != MEMCACHED_NOTFOUND && rc != MEMCACHED_END)
127 {
128 fprintf(stderr, "%s:%d Failed to execute mget: %s\n",
129 __FILE__, __LINE__,
130 memcached_strerror(memc, rc));
131 memcached_quit(memc);
132 return 0;
133 }
134 }
135 else
136 {
137 fprintf(stderr, "%s:%d Failed to execute mget: %s\n",
138 __FILE__, __LINE__,
139 memcached_strerror(memc, rc));
140 memcached_quit(memc);
141 return 0;
142 }
143
144 return retrieved;
145 }