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