This adds a couple of new options for options parsing.
[m6w6/libmemcached] / libmemcached / memcached.c
1 /* LibMemcached
2 * Copyright (C) 2006-2010 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 #include "common.h"
13
14 static const memcached_st global_copy= {
15 .state= {
16 .is_purging= false,
17 .is_processing_input= false,
18 .is_time_for_rebuild= false,
19 },
20 .flags= {
21 .auto_eject_hosts= false,
22 .binary_protocol= false,
23 .buffer_requests= false,
24 .cork= false,
25 .hash_with_prefix_key= false,
26 .ketama_weighted= false,
27 .no_block= false,
28 .no_reply= false,
29 .randomize_replica_read= false,
30 .reuse_memory= false,
31 .support_cas= false,
32 .tcp_nodelay= false,
33 .use_cache_lookups= false,
34 .use_sort_hosts= false,
35 .use_udp= false,
36 .verify_key= false,
37 .tcp_keepalive= false,
38 .load_from_file= false,
39 .ping_service= false
40 }
41 };
42
43 static inline bool _memcached_init(memcached_st *self)
44 {
45 self->state= global_copy.state;
46 self->flags= global_copy.flags;
47
48 self->distribution= MEMCACHED_DISTRIBUTION_MODULA;
49
50 hashkit_st *hash_ptr;
51 hash_ptr= hashkit_create(&self->hashkit);
52 if (! hash_ptr)
53 return false;
54
55 self->continuum_points_counter= 0;
56
57 self->number_of_hosts= 0;
58 self->servers= NULL;
59 self->last_disconnected_server= NULL;
60
61 self->snd_timeout= 0;
62 self->rcv_timeout= 0;
63 self->server_failure_limit= 0;
64
65 /* TODO, Document why we picked these defaults */
66 self->io_msg_watermark= 500;
67 self->io_bytes_watermark= 65 * 1024;
68
69 self->tcp_keepidle= 0;
70
71 self->io_key_prefetch= 0;
72 self->cached_errno= 0;
73 self->poll_timeout= MEMCACHED_DEFAULT_TIMEOUT;
74 self->connect_timeout= MEMCACHED_DEFAULT_CONNECT_TIMEOUT;
75 self->retry_timeout= 0;
76 self->continuum_count= 0;
77
78 self->send_size= -1;
79 self->recv_size= -1;
80
81 self->user_data= NULL;
82 self->next_distribution_rebuild= 0;
83 self->number_of_replicas= 0;
84 hash_ptr= hashkit_create(&self->distribution_hashkit);
85 if (! hash_ptr)
86 return false;
87 self->continuum= NULL;
88
89 self->allocators= memcached_allocators_return_default();
90
91 self->on_clone= NULL;
92 self->on_cleanup= NULL;
93 self->get_key_failure= NULL;
94 self->delete_trigger= NULL;
95 self->callbacks= NULL;
96 self->sasl.callbacks= NULL;
97 self->sasl.is_allocated= false;
98
99 self->error_messages= NULL;
100 self->prefix_key= NULL;
101
102 return true;
103 }
104
105 static void _free(memcached_st *ptr, bool release_st)
106 {
107 /* If we have anything open, lets close it now */
108 memcached_quit(ptr);
109 memcached_server_list_free(memcached_server_list(ptr));
110 memcached_result_free(&ptr->result);
111
112 if (ptr->last_disconnected_server)
113 memcached_server_free(ptr->last_disconnected_server);
114
115 if (ptr->on_cleanup)
116 ptr->on_cleanup(ptr);
117
118 if (ptr->continuum)
119 libmemcached_free(ptr, ptr->continuum);
120
121 memcached_array_free(ptr->prefix_key);
122 ptr->prefix_key= NULL;
123
124 memcached_error_free(ptr);
125
126 if (ptr->sasl.callbacks)
127 {
128 #ifdef LIBMEMCACHED_WITH_SASL_SUPPORT
129 memcached_destroy_sasl_auth_data(ptr);
130 #endif
131 }
132
133 if (memcached_is_allocated(ptr) && release_st)
134 {
135 libmemcached_free(ptr, ptr);
136 }
137 }
138
139 memcached_st *memcached_create(memcached_st *ptr)
140 {
141 if (ptr == NULL)
142 {
143 ptr= (memcached_st *)malloc(sizeof(memcached_st));
144
145 if (! ptr)
146 {
147 return NULL; /* MEMCACHED_MEMORY_ALLOCATION_FAILURE */
148 }
149
150 ptr->options.is_allocated= true;
151 }
152 else
153 {
154 ptr->options.is_allocated= false;
155 }
156
157 #if 0
158 memcached_set_purging(ptr, false);
159 memcached_set_processing_input(ptr, false);
160 #endif
161
162 if (! _memcached_init(ptr))
163 {
164 memcached_free(ptr);
165 return NULL;
166 }
167
168 if (! memcached_result_create(ptr, &ptr->result))
169 {
170 memcached_free(ptr);
171 return NULL;
172 }
173
174 WATCHPOINT_ASSERT_INITIALIZED(&ptr->result);
175
176 return ptr;
177 }
178
179 memcached_st *memcached_create_with_options(const char *string, size_t length)
180 {
181 memcached_st *self= memcached_create(NULL);
182
183 if (! self)
184 return NULL;
185
186 memcached_parse_options(self, string, length);
187
188 return self;
189 }
190
191 void memcached_reset(memcached_st *ptr)
192 {
193 WATCHPOINT_ASSERT(ptr);
194 if (! ptr)
195 return;
196
197 bool stored_is_allocated= memcached_is_allocated(ptr);
198 _free(ptr, false);
199 memcached_create(ptr);
200 memcached_set_allocated(ptr, stored_is_allocated);
201 }
202
203 void memcached_servers_reset(memcached_st *ptr)
204 {
205 memcached_server_list_free(memcached_server_list(ptr));
206
207 memcached_server_list_set(ptr, NULL);
208 ptr->number_of_hosts= 0;
209 if (ptr->last_disconnected_server)
210 {
211 memcached_server_free(ptr->last_disconnected_server);
212 }
213 ptr->last_disconnected_server= NULL;
214 ptr->server_failure_limit= 0;
215 }
216
217 void memcached_reset_last_disconnected_server(memcached_st *ptr)
218 {
219 if (ptr->last_disconnected_server)
220 {
221 memcached_server_free(ptr->last_disconnected_server);
222 ptr->last_disconnected_server= NULL;
223 }
224 }
225
226 void memcached_free(memcached_st *ptr)
227 {
228 _free(ptr, true);
229 }
230
231 /*
232 clone is the destination, while source is the structure to clone.
233 If source is NULL the call is the same as if a memcached_create() was
234 called.
235 */
236 memcached_st *memcached_clone(memcached_st *clone, const memcached_st *source)
237 {
238 memcached_return_t rc= MEMCACHED_SUCCESS;
239 memcached_st *new_clone;
240
241 if (source == NULL)
242 return memcached_create(clone);
243
244 if (clone && memcached_is_allocated(clone))
245 {
246 return NULL;
247 }
248
249 new_clone= memcached_create(clone);
250
251 if (new_clone == NULL)
252 return NULL;
253
254 new_clone->flags= source->flags;
255 new_clone->send_size= source->send_size;
256 new_clone->recv_size= source->recv_size;
257 new_clone->poll_timeout= source->poll_timeout;
258 new_clone->connect_timeout= source->connect_timeout;
259 new_clone->retry_timeout= source->retry_timeout;
260 new_clone->distribution= source->distribution;
261
262 hashkit_st *hash_ptr;
263
264 hash_ptr= hashkit_clone(&new_clone->hashkit, &source->hashkit);
265 if (! hash_ptr)
266 {
267 memcached_free(new_clone);
268 return NULL;
269 }
270
271 hash_ptr= hashkit_clone(&new_clone->distribution_hashkit, &source->distribution_hashkit);
272 if (! hash_ptr)
273 {
274 memcached_free(new_clone);
275 return NULL;
276 }
277
278 new_clone->user_data= source->user_data;
279
280 new_clone->snd_timeout= source->snd_timeout;
281 new_clone->rcv_timeout= source->rcv_timeout;
282
283 new_clone->on_clone= source->on_clone;
284 new_clone->on_cleanup= source->on_cleanup;
285
286 new_clone->allocators= source->allocators;
287
288 new_clone->get_key_failure= source->get_key_failure;
289 new_clone->delete_trigger= source->delete_trigger;
290 new_clone->server_failure_limit= source->server_failure_limit;
291 new_clone->io_msg_watermark= source->io_msg_watermark;
292 new_clone->io_bytes_watermark= source->io_bytes_watermark;
293 new_clone->io_key_prefetch= source->io_key_prefetch;
294 new_clone->number_of_replicas= source->number_of_replicas;
295 new_clone->tcp_keepidle= source->tcp_keepidle;
296
297 if (memcached_server_count(source))
298 rc= memcached_push(new_clone, source);
299
300 if (rc != MEMCACHED_SUCCESS)
301 {
302 memcached_free(new_clone);
303
304 return NULL;
305 }
306
307
308 new_clone->prefix_key= memcached_array_clone(new_clone, source->prefix_key);
309
310 #ifdef LIBMEMCACHED_WITH_SASL_SUPPORT
311 if (source->sasl.callbacks)
312 {
313 if (memcached_clone_sasl(new_clone, source) != MEMCACHED_SUCCESS)
314 {
315 memcached_free(new_clone);
316 return NULL;
317 }
318 }
319 #endif
320
321 rc= run_distribution(new_clone);
322
323 if (rc != MEMCACHED_SUCCESS)
324 {
325 memcached_free(new_clone);
326
327 return NULL;
328 }
329
330 if (source->on_clone)
331 source->on_clone(new_clone, source);
332
333 return new_clone;
334 }
335
336 void *memcached_get_user_data(const memcached_st *ptr)
337 {
338 return ptr->user_data;
339 }
340
341 void *memcached_set_user_data(memcached_st *ptr, void *data)
342 {
343 void *ret= ptr->user_data;
344 ptr->user_data= data;
345
346 return ret;
347 }
348
349 memcached_return_t memcached_push(memcached_st *destination, const memcached_st *source)
350 {
351 return memcached_server_push(destination, source->servers);
352 }
353
354 memcached_server_write_instance_st memcached_server_instance_fetch(memcached_st *ptr, uint32_t server_key)
355 {
356 return &ptr->servers[server_key];
357 }
358
359 memcached_server_instance_st memcached_server_instance_by_position(const memcached_st *ptr, uint32_t server_key)
360 {
361 return &ptr->servers[server_key];
362 }