Update tests for style/correctness.
[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 "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 (rc != MEMCACHED_SUCCESS and rc != MEMCACHED_BUFFERED)
31 {
32 fprintf(stderr, "Failure on insert (%s) of %.*s\n",
33 memcached_last_error_message(memc),
34 (unsigned int)pairs[x].key_length, pairs[x].key);
35 }
36 else
37 {
38 pairs_sent++;
39 }
40 }
41
42 return pairs_sent;
43 }
44
45 /*
46 Execute a memcached_get() on a set of pairs.
47 Return the number of rows retrieved.
48 */
49 unsigned int execute_get(memcached_st *memc, pairs_st *pairs, unsigned int number_of)
50 {
51 memcached_return_t rc;
52 unsigned int x;
53 unsigned int retrieved;
54
55
56 for (retrieved= 0,x= 0; x < number_of; x++)
57 {
58 char *value;
59 size_t value_length;
60 uint32_t flags;
61 unsigned int fetch_key;
62
63 fetch_key= (unsigned int)((unsigned int)random() % number_of);
64
65 value= memcached_get(memc, pairs[fetch_key].key, pairs[fetch_key].key_length,
66 &value_length, &flags, &rc);
67
68 if (rc != MEMCACHED_SUCCESS)
69 {
70 fprintf(stderr, "Failure on read(%s) of %.*s\n",
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, "Failed to execute mget: %s\n",
126 memcached_strerror(memc, rc));
127 memcached_quit(memc);
128 return 0;
129 }
130 }
131 else
132 {
133 fprintf(stderr, "Failed to execute mget: %s\n",
134 memcached_strerror(memc, rc));
135 memcached_quit(memc);
136 return 0;
137 }
138
139 return retrieved;
140 }