Add test cases for add server for NULL cases.
[m6w6/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 && rc != MEMCACHED_BUFFERED)
31 {
32 fprintf(stderr, "Failured on insert of %.*s\n",
33 (unsigned int)pairs[x].key_length, pairs[x].key);
34 }
35 else
36 {
37 pairs_sent++;
38 }
39 }
40
41 return pairs_sent;
42 }
43
44 /*
45 Execute a memcached_get() on a set of pairs.
46 Return the number of rows retrieved.
47 */
48 unsigned int execute_get(memcached_st *memc, pairs_st *pairs, unsigned int number_of)
49 {
50 memcached_return_t rc;
51 unsigned int x;
52 unsigned int retrieved;
53
54
55 for (retrieved= 0,x= 0; x < number_of; x++)
56 {
57 char *value;
58 size_t value_length;
59 uint32_t flags;
60 unsigned int fetch_key;
61
62 fetch_key= (unsigned int)((unsigned int)random() % number_of);
63
64 value= memcached_get(memc, pairs[fetch_key].key, pairs[fetch_key].key_length,
65 &value_length, &flags, &rc);
66
67 if (rc != MEMCACHED_SUCCESS)
68 fprintf(stderr, "Failured on read of %.*s\n",
69 (unsigned int)pairs[fetch_key].key_length, pairs[fetch_key].key);
70 else
71 retrieved++;
72
73 free(value);
74 }
75
76 return retrieved;
77 }
78
79 /**
80 * Callback function to count the number of results
81 */
82 static memcached_return_t callback_counter(const memcached_st *ptr,
83 memcached_result_st *result,
84 void *context)
85 {
86 (void)ptr;
87 (void)result;
88 unsigned int *counter= (unsigned int *)context;
89 *counter= *counter + 1;
90
91 return MEMCACHED_SUCCESS;
92 }
93
94 /**
95 * Try to run a large mget to get all of the keys
96 * @param memc memcached handle
97 * @param keys the keys to get
98 * @param key_length the length of the keys
99 * @param number_of the number of keys to try to get
100 * @return the number of keys received
101 */
102 unsigned int execute_mget(memcached_st *memc,
103 const char * const *keys,
104 size_t *key_length,
105 unsigned int number_of)
106 {
107 unsigned int retrieved= 0;
108 memcached_execute_fn callbacks[]= { callback_counter };
109 memcached_return_t rc;
110 rc= memcached_mget_execute(memc, keys, key_length,
111 (size_t)number_of, callbacks, &retrieved, 1);
112
113 if (rc == MEMCACHED_SUCCESS || rc == MEMCACHED_NOTFOUND ||
114 rc == MEMCACHED_BUFFERED || rc == MEMCACHED_END)
115 {
116 rc= memcached_fetch_execute(memc, callbacks, (void *)&retrieved, 1);
117 if (rc != MEMCACHED_SUCCESS && rc != MEMCACHED_NOTFOUND && rc != MEMCACHED_END)
118 {
119 fprintf(stderr, "Failed to execute mget: %s\n",
120 memcached_strerror(memc, rc));
121 memcached_quit(memc);
122 return 0;
123 }
124 }
125 else
126 {
127 fprintf(stderr, "Failed to execute mget: %s\n",
128 memcached_strerror(memc, rc));
129 memcached_quit(memc);
130 return 0;
131 }
132
133 return retrieved;
134 }