1 /* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
5 * Copyright (C) 2011 Data Differential, http://datadifferential.com/
6 * Copyright (C) 2006-2009 Brian Aker All rights reserved.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions are
12 * * Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
15 * * Redistributions in binary form must reproduce the above
16 * copyright notice, this list of conditions and the following disclaimer
17 * in the documentation and/or other materials provided with the
20 * * The names of its contributors may not be used to endorse or
21 * promote products derived from this software without specific prior
24 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
25 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
26 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
27 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
28 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
29 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
30 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
31 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
32 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
33 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
34 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
38 #include <libmemcached/common.h>
40 #include <libmemcached/options.hpp>
41 #include <libmemcached/virtual_bucket.h>
43 static inline bool _memcached_init(Memcached
*self
)
45 self
->state
.is_purging
= false;
46 self
->state
.is_processing_input
= false;
47 self
->state
.is_time_for_rebuild
= false;
48 self
->state
.is_parsing
= false;
50 self
->flags
.auto_eject_hosts
= false;
51 self
->flags
.binary_protocol
= false;
52 self
->flags
.buffer_requests
= false;
53 self
->flags
.hash_with_namespace
= false;
54 self
->flags
.no_block
= false;
55 self
->flags
.reply
= true;
56 self
->flags
.randomize_replica_read
= false;
57 self
->flags
.support_cas
= false;
58 self
->flags
.tcp_nodelay
= false;
59 self
->flags
.use_sort_hosts
= false;
60 self
->flags
.use_udp
= false;
61 self
->flags
.verify_key
= false;
62 self
->flags
.tcp_keepalive
= false;
63 self
->flags
.is_aes
= false;
64 self
->flags
.is_fetching_version
= false;
66 self
->virtual_bucket
= NULL
;
68 self
->distribution
= MEMCACHED_DISTRIBUTION_MODULA
;
70 if (hashkit_create(&self
->hashkit
) == NULL
)
75 self
->server_info
.version
= 0;
77 self
->ketama
.continuum
= NULL
;
78 self
->ketama
.continuum_count
= 0;
79 self
->ketama
.continuum_points_counter
= 0;
80 self
->ketama
.next_distribution_rebuild
= 0;
81 self
->ketama
.weighted_
= false;
83 self
->number_of_hosts
= 0;
85 self
->last_disconnected_server
= NULL
;
89 self
->server_failure_limit
= MEMCACHED_SERVER_FAILURE_LIMIT
;
90 self
->query_id
= 1; // 0 is considered invalid
92 /* TODO, Document why we picked these defaults */
93 self
->io_msg_watermark
= 500;
94 self
->io_bytes_watermark
= 65 * 1024;
96 self
->tcp_keepidle
= 0;
98 self
->io_key_prefetch
= 0;
99 self
->poll_timeout
= MEMCACHED_DEFAULT_TIMEOUT
;
100 self
->connect_timeout
= MEMCACHED_DEFAULT_CONNECT_TIMEOUT
;
101 self
->retry_timeout
= MEMCACHED_SERVER_FAILURE_RETRY_TIMEOUT
;
102 self
->dead_timeout
= MEMCACHED_SERVER_FAILURE_DEAD_TIMEOUT
;
107 self
->user_data
= NULL
;
108 self
->number_of_replicas
= 0;
110 self
->allocators
= memcached_allocators_return_default();
112 self
->on_clone
= NULL
;
113 self
->on_cleanup
= NULL
;
114 self
->get_key_failure
= NULL
;
115 self
->delete_trigger
= NULL
;
116 self
->callbacks
= NULL
;
117 self
->sasl
.callbacks
= NULL
;
118 self
->sasl
.is_allocated
= false;
120 self
->error_messages
= NULL
;
121 self
->_namespace
= NULL
;
122 self
->configure
.initial_pool_size
= 1;
123 self
->configure
.max_pool_size
= 1;
124 self
->configure
.version
= -1;
125 self
->configure
.filename
= NULL
;
130 static void __memcached_free(Memcached
*ptr
, bool release_st
)
132 /* If we have anything open, lets close it now */
134 memcached_instance_list_free(memcached_instance_list(ptr
), memcached_instance_list_count(ptr
));
135 memcached_result_free(&ptr
->result
);
137 memcached_virtual_bucket_free(ptr
);
139 memcached_instance_free((org::libmemcached::Instance
*)ptr
->last_disconnected_server
);
143 ptr
->on_cleanup(ptr
);
146 libmemcached_free(ptr
, ptr
->ketama
.continuum
);
147 ptr
->ketama
.continuum
= NULL
;
149 memcached_array_free(ptr
->_namespace
);
150 ptr
->_namespace
= NULL
;
152 memcached_error_free(*ptr
);
154 if (LIBMEMCACHED_WITH_SASL_SUPPORT
and ptr
->sasl
.callbacks
)
156 memcached_destroy_sasl_auth_data(ptr
);
161 memcached_array_free(ptr
->configure
.filename
);
162 ptr
->configure
.filename
= NULL
;
165 hashkit_free(&ptr
->hashkit
);
167 if (memcached_is_allocated(ptr
) and release_st
)
169 libmemcached_free(ptr
, ptr
);
173 memcached_st
*memcached_create(memcached_st
*shell
)
177 shell
->options
.is_allocated
= false;
181 shell
= (memcached_st
*)libmemcached_xmalloc(NULL
, memcached_st
);
185 return NULL
; /* MEMCACHED_MEMORY_ALLOCATION_FAILURE */
188 shell
->options
.is_allocated
= true;
191 if (_memcached_init(shell
) == false)
193 memcached_free(shell
);
197 Memcached
* memc
= memcached2Memcached(shell
);
198 if (memcached_result_create(shell
, &memc
->result
) == NULL
)
200 memcached_free(shell
);
204 WATCHPOINT_ASSERT_INITIALIZED(&memc
->result
);
209 memcached_st
*memcached(const char *string
, size_t length
)
211 if (length
== 0 and string
)
216 if (length
and string
== NULL
)
223 if (bool(getenv("LIBMEMCACHED")))
225 string
= getenv("LIBMEMCACHED");
226 length
= string
? strlen(string
) : 0;
230 memcached_st
*memc
= memcached_create(NULL
);
236 if (length
== 0 or string
== NULL
)
241 memcached_return_t rc
= memcached_parse_configuration(memc
, string
, length
);
242 if (memcached_success(rc
) and memcached_parse_filename(memc
))
244 rc
= memcached_parse_configure_file(*memc
, memcached_parse_filename(memc
), memcached_parse_filename_length(memc
));
247 if (memcached_failed(rc
))
249 memcached_free(memc
);
256 memcached_return_t
memcached_reset(memcached_st
*shell
)
258 Memcached
* ptr
= memcached2Memcached(shell
);
259 WATCHPOINT_ASSERT(ptr
);
262 return MEMCACHED_INVALID_ARGUMENTS
;
265 bool stored_is_allocated
= memcached_is_allocated(ptr
);
266 uint64_t query_id
= ptr
->query_id
;
267 __memcached_free(ptr
, false);
268 memcached_create(ptr
);
269 memcached_set_allocated(ptr
, stored_is_allocated
);
270 ptr
->query_id
= query_id
;
272 if (ptr
->configure
.filename
)
274 return memcached_parse_configure_file(*ptr
, memcached_param_array(ptr
->configure
.filename
));
277 return MEMCACHED_SUCCESS
;
280 void memcached_servers_reset(memcached_st
*shell
)
282 Memcached
* self
= memcached2Memcached(shell
);
285 libmemcached_free(self
, self
->ketama
.continuum
);
286 self
->ketama
.continuum
= NULL
;
288 memcached_instance_list_free(memcached_instance_list(self
), self
->number_of_hosts
);
289 memcached_instance_set(self
, NULL
, 0);
291 memcached_reset_last_disconnected_server(self
);
295 void memcached_reset_last_disconnected_server(memcached_st
*shell
)
297 Memcached
* self
= memcached2Memcached(shell
);
300 memcached_instance_free((org::libmemcached::Instance
*)self
->last_disconnected_server
);
301 self
->last_disconnected_server
= NULL
;
305 void memcached_free(memcached_st
*ptr
)
309 __memcached_free(ptr
, true);
314 clone is the destination, while source is the structure to clone.
315 If source is NULL the call is the same as if a memcached_create() was
318 memcached_st
*memcached_clone(memcached_st
*clone
, const memcached_st
*source
)
322 return memcached_create(clone
);
325 if (clone
and memcached_is_allocated(clone
))
330 memcached_st
*new_clone
= memcached_create(clone
);
332 if (new_clone
== NULL
)
337 new_clone
->flags
= source
->flags
;
338 new_clone
->send_size
= source
->send_size
;
339 new_clone
->recv_size
= source
->recv_size
;
340 new_clone
->poll_timeout
= source
->poll_timeout
;
341 new_clone
->connect_timeout
= source
->connect_timeout
;
342 new_clone
->retry_timeout
= source
->retry_timeout
;
343 new_clone
->dead_timeout
= source
->dead_timeout
;
344 new_clone
->distribution
= source
->distribution
;
346 if (hashkit_clone(&new_clone
->hashkit
, &source
->hashkit
) == NULL
)
348 memcached_free(new_clone
);
352 new_clone
->user_data
= source
->user_data
;
354 new_clone
->snd_timeout
= source
->snd_timeout
;
355 new_clone
->rcv_timeout
= source
->rcv_timeout
;
357 new_clone
->on_clone
= source
->on_clone
;
358 new_clone
->on_cleanup
= source
->on_cleanup
;
360 new_clone
->allocators
= source
->allocators
;
362 new_clone
->get_key_failure
= source
->get_key_failure
;
363 new_clone
->delete_trigger
= source
->delete_trigger
;
364 new_clone
->server_failure_limit
= source
->server_failure_limit
;
365 new_clone
->io_msg_watermark
= source
->io_msg_watermark
;
366 new_clone
->io_bytes_watermark
= source
->io_bytes_watermark
;
367 new_clone
->io_key_prefetch
= source
->io_key_prefetch
;
368 new_clone
->number_of_replicas
= source
->number_of_replicas
;
369 new_clone
->tcp_keepidle
= source
->tcp_keepidle
;
371 if (memcached_server_count(source
))
373 if (memcached_failed(memcached_push(new_clone
, source
)))
380 new_clone
->_namespace
= memcached_array_clone(new_clone
, source
->_namespace
);
381 new_clone
->configure
.filename
= memcached_array_clone(new_clone
, source
->_namespace
);
382 new_clone
->configure
.version
= source
->configure
.version
;
384 if (LIBMEMCACHED_WITH_SASL_SUPPORT
and source
->sasl
.callbacks
)
386 if (memcached_failed(memcached_clone_sasl(new_clone
, source
)))
388 memcached_free(new_clone
);
393 if (memcached_failed(run_distribution(new_clone
)))
395 memcached_free(new_clone
);
400 if (source
->on_clone
)
402 source
->on_clone(new_clone
, source
);
408 void *memcached_get_user_data(const memcached_st
*shell
)
410 const Memcached
* memc
= memcached2Memcached(shell
);
413 return memc
->user_data
;
419 void *memcached_set_user_data(memcached_st
*shell
, void *data
)
421 Memcached
* memc
= memcached2Memcached(shell
);
424 void *ret
= memc
->user_data
;
425 memc
->user_data
= data
;
433 memcached_return_t
memcached_push(memcached_st
*destination
, const memcached_st
*source
)
435 return memcached_instance_push(destination
, (org::libmemcached::Instance
*)source
->servers
, source
->number_of_hosts
);
438 org::libmemcached::Instance
* memcached_instance_fetch(Memcached
*ptr
, uint32_t server_key
)
445 return &ptr
->servers
[server_key
];
448 memcached_server_instance_st
memcached_server_instance_by_position(const memcached_st
*shell
, uint32_t server_key
)
450 const Memcached
* memc
= memcached2Memcached(shell
);
453 return &memc
->servers
[server_key
];
459 org::libmemcached::Instance
* memcached_instance_by_position(const memcached_st
*shell
, uint32_t server_key
)
461 const Memcached
* memc
= memcached2Memcached(shell
);
464 return &memc
->servers
[server_key
];
470 uint64_t memcached_query_id(const memcached_st
*shell
)
472 const Memcached
* memc
= memcached2Memcached(shell
);
475 return memc
->query_id
;
481 org::libmemcached::Instance
* memcached_instance_list(const memcached_st
*shell
)
483 const Memcached
* memc
= memcached2Memcached(shell
);
486 return (org::libmemcached::Instance
*)memc
->servers
;