10 #include <memcached.h>
12 #include "client_options.h"
13 #include "utilities.h"
16 /* Use this for string generation */
17 static const char ALPHANUMERICS
[]=
18 "0123456789ABCDEFGHIJKLMNOPQRSTWXYZabcdefghijklmnopqrstuvwxyz";
20 #define ALPHANUMERICS_SIZE (sizeof(ALPHANUMERICS)-1)
23 typedef struct pairs_st pairs_st
;
33 void options_parse(int argc
, char *argv
[]);
34 static pairs_st
*pairs_generate(void);
35 static void pairs_free(pairs_st
*pairs
);
36 static void get_random_string(char *buffer
, size_t size
);
38 static int opt_verbose
= 0;
39 static int opt_default_pairs
= 100;
40 static int opt_displayflag
= 0;
41 static char *opt_servers
= NULL
;
43 int main(int argc
, char *argv
[])
51 memc
= memcached_init(NULL
);
52 options_parse(argc
, argv
);
57 parse_opt_servers(memc
, opt_servers
);
59 pairs
= pairs_generate();
62 for (x
= 0; x
< opt_default_pairs
; x
++)
64 printf("Key(%u) %.10s \t%.10s\n", x
, pairs
[x
].key
, pairs
[x
].value
);
65 rc
= memcached_set(memc
, pairs
[x
].key
, pairs
[x
].key_length
,
66 pairs
[x
].value
, pairs
[x
].value_length
,
68 if (rc
!= MEMCACHED_SUCCESS
)
69 fprintf(stderr
, "Failured on insert of %.*s\n",
70 (unsigned int)pairs
[x
].key_length
, pairs
[x
].key
);
73 for (x
= 0; x
< opt_default_pairs
; x
++)
75 printf("Key(%u) %.10s \n", x
, pairs
[x
].key
);
80 value
= memcached_get(memc
, pairs
[x
].key
, pairs
[x
].key_length
,
85 if (rc
!= MEMCACHED_SUCCESS
)
86 fprintf(stderr
, "Failured on read of %.*s\n",
87 (unsigned int)pairs
[x
].key_length
, pairs
[x
].key
);
88 printf("\t%.10s\n", value
);
96 memcached_deinit(memc
);
101 void options_parse(int argc
, char *argv
[])
103 static struct option long_options
[]=
105 {"version", no_argument
, NULL
, OPT_VERSION
},
106 {"help", no_argument
, NULL
, OPT_HELP
},
107 {"verbose", no_argument
, &opt_verbose
, OPT_VERBOSE
},
108 {"debug", no_argument
, &opt_verbose
, OPT_DEBUG
},
109 {"servers", required_argument
, NULL
, OPT_SERVERS
},
110 {"flag", no_argument
, &opt_displayflag
, OPT_FLAG
},
111 {"default-pairs", required_argument
, NULL
, OPT_SLAP_DEFAULT_PAIRS
},
120 option_rv
= getopt_long(argc
, argv
, "Vhvds:", long_options
, &option_index
);
121 if (option_rv
== -1) break;
126 case OPT_VERBOSE
: /* --verbose or -v */
127 opt_verbose
= OPT_VERBOSE
;
129 case OPT_DEBUG
: /* --debug or -d */
130 opt_verbose
= OPT_DEBUG
;
132 case OPT_VERSION
: /* --version or -V */
133 printf("memcache tools, memcat, v1.0\n");
136 case OPT_HELP
: /* --help or -h */
137 printf("useful help messages go here\n");
140 case OPT_SERVERS
: /* --servers or -s */
141 opt_servers
= strdup(optarg
);
143 case OPT_SLAP_DEFAULT_PAIRS
:
144 opt_default_pairs
= strtol(optarg
, (char **)NULL
, 10);
147 /* getopt_long already printed an error message. */
155 static void pairs_free(pairs_st
*pairs
)
159 for (x
= 0; x
< opt_default_pairs
; x
++)
162 free(pairs
[x
].value
);
168 static pairs_st
*pairs_generate(void)
173 pairs
= (pairs_st
*)malloc(sizeof(pairs_st
) * opt_default_pairs
);
178 for (x
= 0; x
< opt_default_pairs
; x
++)
180 pairs
[x
].key
= (char *)malloc(sizeof(char) * 100);
183 get_random_string(pairs
[x
].key
, 100);
184 pairs
[x
].key_length
= 100;
186 pairs
[x
].value
= (char *)malloc(sizeof(char) * 400);
189 get_random_string(pairs
[x
].value
, 400);
190 pairs
[x
].value_length
= 400;
195 fprintf(stderr
, "Memory Allocation failure in pairs_generate.\n");
199 static void get_random_string(char *buffer
, size_t size
)
201 char *buffer_ptr
= buffer
;
204 *buffer_ptr
++= ALPHANUMERICS
[random() % ALPHANUMERICS_SIZE
];
205 *buffer_ptr
++= ALPHANUMERICS
[random() % ALPHANUMERICS_SIZE
];