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
);
77 memcached_deinit(memc
);
82 void options_parse(int argc
, char *argv
[])
84 static struct option long_options
[]=
86 {"version", no_argument
, NULL
, OPT_VERSION
},
87 {"help", no_argument
, NULL
, OPT_HELP
},
88 {"verbose", no_argument
, &opt_verbose
, OPT_VERBOSE
},
89 {"debug", no_argument
, &opt_verbose
, OPT_DEBUG
},
90 {"servers", required_argument
, NULL
, OPT_SERVERS
},
91 {"flag", no_argument
, &opt_displayflag
, OPT_FLAG
},
100 option_rv
= getopt_long(argc
, argv
, "Vhvds:", long_options
, &option_index
);
101 if (option_rv
== -1) break;
106 case OPT_VERBOSE
: /* --verbose or -v */
107 opt_verbose
= OPT_VERBOSE
;
109 case OPT_DEBUG
: /* --debug or -d */
110 opt_verbose
= OPT_DEBUG
;
112 case OPT_VERSION
: /* --version or -V */
113 printf("memcache tools, memcat, v1.0\n");
116 case OPT_HELP
: /* --help or -h */
117 printf("useful help messages go here\n");
120 case OPT_SERVERS
: /* --servers or -s */
121 opt_servers
= strdup(optarg
);
124 /* getopt_long already printed an error message. */
132 static void pairs_free(pairs_st
*pairs
)
136 for (x
= 0; x
< opt_default_pairs
; x
++)
139 free(pairs
[x
].value
);
145 static pairs_st
*pairs_generate(void)
150 pairs
= (pairs_st
*)malloc(sizeof(pairs_st
) * opt_default_pairs
);
155 for (x
= 0; x
< opt_default_pairs
; x
++)
157 pairs
[x
].key
= (char *)malloc(sizeof(char) * 100);
160 get_random_string(pairs
[x
].key
, 100);
161 pairs
[x
].key_length
= 100;
163 pairs
[x
].value
= (char *)malloc(sizeof(char) * 400);
166 get_random_string(pairs
[x
].value
, 400);
167 pairs
[x
].value_length
= 400;
172 fprintf(stderr
, "Memory Allocation failure in pairs_generate.\n");
176 static void get_random_string(char *buffer
, size_t size
)
178 char *buffer_ptr
= buffer
;
181 *buffer_ptr
++= ALPHANUMERICS
[random() % ALPHANUMERICS_SIZE
];
182 *buffer_ptr
++= ALPHANUMERICS
[random() % ALPHANUMERICS_SIZE
];