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