Merge in ability for memcapable to handle ascii/binary as flags to the
[awesomized/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 }
39 };
40
41 static inline bool _memcached_init(memcached_st *self)
42 {
43 self->state= global_copy.state;
44 self->flags= global_copy.flags;
45
46 self->distribution= MEMCACHED_DISTRIBUTION_MODULA;
47
48 hashkit_st *hash_ptr;
49 hash_ptr= hashkit_create(&self->hashkit);
50 if (! hash_ptr)
51 return false;
52
53 self->continuum_points_counter= 0;
54
55 self->number_of_hosts= 0;
56 self->servers= NULL;
57 self->last_disconnected_server= NULL;
58
59 self->snd_timeout= 0;
60 self->rcv_timeout= 0;
61 self->server_failure_limit= 0;
62
63 /* TODO, Document why we picked these defaults */
64 self->io_msg_watermark= 500;
65 self->io_bytes_watermark= 65 * 1024;
66
67 self->tcp_keepidle= 0;
68
69 self->io_key_prefetch= 0;
70 self->cached_errno= 0;
71 self->poll_timeout= MEMCACHED_DEFAULT_TIMEOUT;
72 self->connect_timeout= MEMCACHED_DEFAULT_CONNECT_TIMEOUT;
73 self->retry_timeout= 0;
74 self->continuum_count= 0;
75
76 self->send_size= -1;
77 self->recv_size= -1;
78
79 self->user_data= NULL;
80 self->next_distribution_rebuild= 0;
81 self->prefix_key_length= 0;
82 self->number_of_replicas= 0;
83 hash_ptr= hashkit_create(&self->distribution_hashkit);
84 if (! hash_ptr)
85 return false;
86 self->continuum= NULL;
87
88 self->allocators= memcached_allocators_return_default();
89
90 self->on_clone= NULL;
91 self->on_cleanup= NULL;
92 self->get_key_failure= NULL;
93 self->delete_trigger= NULL;
94 self->callbacks= NULL;
95 self->sasl.callbacks= NULL;
96 self->sasl.is_allocated= false;
97
98 return true;
99 }
100
101 memcached_st *memcached_create(memcached_st *ptr)
102 {
103 if (ptr == NULL)
104 {
105 ptr= (memcached_st *)malloc(sizeof(memcached_st));
106
107 if (! ptr)
108 {
109 return NULL; /* MEMCACHED_MEMORY_ALLOCATION_FAILURE */
110 }
111
112 ptr->options.is_allocated= true;
113 }
114 else
115 {
116 ptr->options.is_allocated= false;
117 }
118
119 #if 0
120 memcached_set_purging(ptr, false);
121 memcached_set_processing_input(ptr, false);
122 #endif
123
124 if (! _memcached_init(ptr))
125 {
126 memcached_free(ptr);
127 return NULL;
128 }
129
130 if (! memcached_result_create(ptr, &ptr->result))
131 {
132 memcached_free(ptr);
133 return NULL;
134 }
135
136 WATCHPOINT_ASSERT_INITIALIZED(&ptr->result);
137
138 return ptr;
139 }
140
141 void memcached_servers_reset(memcached_st *ptr)
142 {
143 memcached_server_list_free(memcached_server_list(ptr));
144
145 memcached_server_list_set(ptr, NULL);
146 ptr->number_of_hosts= 0;
147 if (ptr->last_disconnected_server)
148 {
149 memcached_server_free(ptr->last_disconnected_server);
150 }
151 ptr->last_disconnected_server= NULL;
152 ptr->server_failure_limit= 0;
153 }
154
155 void memcached_reset_last_disconnected_server(memcached_st *ptr)
156 {
157 if (ptr->last_disconnected_server)
158 {
159 memcached_server_free(ptr->last_disconnected_server);
160 ptr->last_disconnected_server= NULL;
161 }
162 }
163
164 void memcached_free(memcached_st *ptr)
165 {
166 /* If we have anything open, lets close it now */
167 memcached_quit(ptr);
168 memcached_server_list_free(memcached_server_list(ptr));
169 memcached_result_free(&ptr->result);
170
171 if (ptr->last_disconnected_server)
172 memcached_server_free(ptr->last_disconnected_server);
173
174 if (ptr->on_cleanup)
175 ptr->on_cleanup(ptr);
176
177 if (ptr->continuum)
178 libmemcached_free(ptr, ptr->continuum);
179
180 if (ptr->sasl.callbacks)
181 {
182 #ifdef LIBMEMCACHED_WITH_SASL_SUPPORT
183 memcached_destroy_sasl_auth_data(ptr);
184 #endif
185 }
186
187 if (memcached_is_allocated(ptr))
188 {
189 libmemcached_free(ptr, ptr);
190 }
191 }
192
193 /*
194 clone is the destination, while source is the structure to clone.
195 If source is NULL the call is the same as if a memcached_create() was
196 called.
197 */
198 memcached_st *memcached_clone(memcached_st *clone, const memcached_st *source)
199 {
200 memcached_return_t rc= MEMCACHED_SUCCESS;
201 memcached_st *new_clone;
202
203 if (source == NULL)
204 return memcached_create(clone);
205
206 if (clone && memcached_is_allocated(clone))
207 {
208 return NULL;
209 }
210
211 new_clone= memcached_create(clone);
212
213 if (new_clone == NULL)
214 return NULL;
215
216 new_clone->flags= source->flags;
217 new_clone->send_size= source->send_size;
218 new_clone->recv_size= source->recv_size;
219 new_clone->poll_timeout= source->poll_timeout;
220 new_clone->connect_timeout= source->connect_timeout;
221 new_clone->retry_timeout= source->retry_timeout;
222 new_clone->distribution= source->distribution;
223
224 hashkit_st *hash_ptr;
225
226 hash_ptr= hashkit_clone(&new_clone->hashkit, &source->hashkit);
227 if (! hash_ptr)
228 {
229 memcached_free(new_clone);
230 return NULL;
231 }
232
233 hash_ptr= hashkit_clone(&new_clone->distribution_hashkit, &source->distribution_hashkit);
234 if (! hash_ptr)
235 {
236 memcached_free(new_clone);
237 return NULL;
238 }
239
240 new_clone->user_data= source->user_data;
241
242 new_clone->snd_timeout= source->snd_timeout;
243 new_clone->rcv_timeout= source->rcv_timeout;
244
245 new_clone->on_clone= source->on_clone;
246 new_clone->on_cleanup= source->on_cleanup;
247
248 new_clone->allocators= source->allocators;
249
250 new_clone->get_key_failure= source->get_key_failure;
251 new_clone->delete_trigger= source->delete_trigger;
252 new_clone->server_failure_limit= source->server_failure_limit;
253 new_clone->io_msg_watermark= source->io_msg_watermark;
254 new_clone->io_bytes_watermark= source->io_bytes_watermark;
255 new_clone->io_key_prefetch= source->io_key_prefetch;
256 new_clone->number_of_replicas= source->number_of_replicas;
257 new_clone->tcp_keepidle= source->tcp_keepidle;
258
259 if (memcached_server_count(source))
260 rc= memcached_push(new_clone, source);
261
262 if (rc != MEMCACHED_SUCCESS)
263 {
264 memcached_free(new_clone);
265
266 return NULL;
267 }
268
269
270 if (source->prefix_key_length)
271 {
272 strcpy(new_clone->prefix_key, source->prefix_key);
273 new_clone->prefix_key_length= source->prefix_key_length;
274 }
275
276 #ifdef LIBMEMCACHED_WITH_SASL_SUPPORT
277 if (source->sasl.callbacks)
278 {
279 if (memcached_clone_sasl(new_clone, source) != MEMCACHED_SUCCESS)
280 {
281 memcached_free(new_clone);
282 return NULL;
283 }
284 }
285 #endif
286
287 rc= run_distribution(new_clone);
288
289 if (rc != MEMCACHED_SUCCESS)
290 {
291 memcached_free(new_clone);
292
293 return NULL;
294 }
295
296 if (source->on_clone)
297 source->on_clone(new_clone, source);
298
299 return new_clone;
300 }
301
302 void *memcached_get_user_data(const memcached_st *ptr)
303 {
304 return ptr->user_data;
305 }
306
307 void *memcached_set_user_data(memcached_st *ptr, void *data)
308 {
309 void *ret= ptr->user_data;
310 ptr->user_data= data;
311
312 return ret;
313 }
314
315 memcached_return_t memcached_push(memcached_st *destination, const memcached_st *source)
316 {
317 return memcached_server_push(destination, source->servers);
318 }
319
320 memcached_server_write_instance_st memcached_server_instance_fetch(memcached_st *ptr, uint32_t server_key)
321 {
322 return &ptr->servers[server_key];
323 }
324
325 memcached_server_instance_st memcached_server_instance_by_position(const memcached_st *ptr, uint32_t server_key)
326 {
327 return &ptr->servers[server_key];
328 }