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