Updating memcached_server_instance_st structure.
[awesomized/libmemcached] / libmemcached / memcached.c
index 4547c6ff229fbc6d5aa1fe10e17902e9db53245d..2925de7f602b36c308fd0438e6e7dd103ead9ccc 100644 (file)
@@ -1,6 +1,14 @@
-/*
-  Memcached library
-*/
+/* LibMemcached
+ * Copyright (C) 2006-2010 Brian Aker
+ * All rights reserved.
+ *
+ * Use and distribution licensed under the BSD license.  See
+ * the COPYING file in the parent directory for full text.
+ *
+ * Summary: 
+ *
+ */
+
 #include "common.h"
 
 static const memcached_st global_copy= {
@@ -28,13 +36,18 @@ static const memcached_st global_copy= {
   }
 };
 
-static inline void _memcached_init(memcached_st *self)
+static inline bool _memcached_init(memcached_st *self)
 {
   self->state= global_copy.state;
   self->flags= global_copy.flags;
 
   self->distribution= MEMCACHED_DISTRIBUTION_MODULA;
-  self->hash= MEMCACHED_HASH_DEFAULT;
+
+  hashkit_st *hash_ptr;
+  hash_ptr= hashkit_create(&self->hashkit);
+  if (! hash_ptr)
+    return false;
+
   self->continuum_points_counter= 0;
 
   self->number_of_hosts= 0;
@@ -63,17 +76,20 @@ static inline void _memcached_init(memcached_st *self)
   self->next_distribution_rebuild= 0;
   self->prefix_key_length= 0;
   self->number_of_replicas= 0;
-  self->distribution_hash= MEMCACHED_HASH_DEFAULT;
+  hash_ptr= hashkit_create(&self->distribution_hashkit);
+  if (! hash_ptr)
+    return false;
   self->continuum= NULL;
 
-
-  memcached_set_memory_allocators(self, NULL, NULL, NULL, NULL);
+  self->allocators= memcached_allocators_return_default();
 
   self->on_clone= NULL;
   self->on_cleanup= NULL;
   self->get_key_failure= NULL;
   self->delete_trigger= NULL;
   self->callbacks= NULL;
+
+  return true;
 }
 
 memcached_st *memcached_create(memcached_st *ptr)
@@ -99,7 +115,11 @@ memcached_st *memcached_create(memcached_st *ptr)
   memcached_set_processing_input(ptr, false);
 #endif
 
-  _memcached_init(ptr);
+  if (! _memcached_init(ptr))
+  {
+    memcached_free(ptr);
+    return NULL;
+  }
 
   if (! memcached_result_create(ptr, &ptr->result))
   {
@@ -119,7 +139,7 @@ void server_list_free(memcached_st *ptr, memcached_server_st *servers)
   if (servers == NULL)
     return;
 
-  for (x= 0; x < memcached_servers_count(servers); x++)
+  for (x= 0; x < memcached_server_list_count(servers); x++)
   {
     if (servers[x].address_info)
     {
@@ -130,7 +150,7 @@ void server_list_free(memcached_st *ptr, memcached_server_st *servers)
 
   if (ptr)
   {
-    ptr->call_free(ptr, servers);
+    libmemcached_free(ptr, servers);
   }
   else
   {
@@ -159,11 +179,11 @@ void memcached_free(memcached_st *ptr)
     ptr->on_cleanup(ptr);
 
   if (ptr->continuum)
-    ptr->call_free(ptr, ptr->continuum);
+    libmemcached_free(ptr, ptr->continuum);
 
   if (memcached_is_allocated(ptr))
   {
-    ptr->call_free(ptr, ptr);
+    libmemcached_free(ptr, ptr);
   }
 }
 
@@ -172,7 +192,7 @@ void memcached_free(memcached_st *ptr)
   If source is NULL the call is the same as if a memcached_create() was
   called.
 */
-memcached_st *memcached_clone(memcached_st *clone, memcached_st *source)
+memcached_st *memcached_clone(memcached_st *clone, const memcached_st *source)
 {
   memcached_return_t rc= MEMCACHED_SUCCESS;
   memcached_st *new_clone;
@@ -197,8 +217,23 @@ memcached_st *memcached_clone(memcached_st *clone, memcached_st *source)
   new_clone->connect_timeout= source->connect_timeout;
   new_clone->retry_timeout= source->retry_timeout;
   new_clone->distribution= source->distribution;
-  new_clone->hash= source->hash;
-  new_clone->distribution_hash= source->distribution_hash;
+
+  hashkit_st *hash_ptr;
+
+  hash_ptr= hashkit_clone(&new_clone->hashkit, &source->hashkit);
+  if (! hash_ptr)
+  {
+    memcached_free(new_clone);
+    return NULL;
+  }
+
+  hash_ptr= hashkit_clone(&new_clone->distribution_hashkit, &source->distribution_hashkit);
+  if (! hash_ptr)
+  {
+    memcached_free(new_clone);
+    return NULL;
+  }
+
   new_clone->user_data= source->user_data;
 
   new_clone->snd_timeout= source->snd_timeout;
@@ -206,10 +241,9 @@ memcached_st *memcached_clone(memcached_st *clone, memcached_st *source)
 
   new_clone->on_clone= source->on_clone;
   new_clone->on_cleanup= source->on_cleanup;
-  new_clone->call_free= source->call_free;
-  new_clone->call_malloc= source->call_malloc;
-  new_clone->call_realloc= source->call_realloc;
-  new_clone->call_calloc= source->call_calloc;
+
+  new_clone->allocators= source->allocators;
+
   new_clone->get_key_failure= source->get_key_failure;
   new_clone->delete_trigger= source->delete_trigger;
   new_clone->server_failure_limit= source->server_failure_limit;
@@ -218,8 +252,8 @@ memcached_st *memcached_clone(memcached_st *clone, memcached_st *source)
   new_clone->io_key_prefetch= source->io_key_prefetch;
   new_clone->number_of_replicas= source->number_of_replicas;
 
-  if (memcached_server_list(source))
-    rc= memcached_server_push(new_clone, memcached_server_list(source));
+  if (memcached_server_count(source))
+    rc= memcached_push(new_clone, source);
 
   if (rc != MEMCACHED_SUCCESS)
   {
@@ -245,7 +279,7 @@ memcached_st *memcached_clone(memcached_st *clone, memcached_st *source)
   }
 
   if (source->on_clone)
-    source->on_clone(source, new_clone);
+    source->on_clone(new_clone, source);
 
   return new_clone;
 }
@@ -262,3 +296,18 @@ void *memcached_set_user_data(memcached_st *ptr, void *data)
 
   return ret;
 }
+
+memcached_return_t memcached_push(memcached_st *destination, const memcached_st *source)
+{
+  return memcached_server_push(destination, source->servers);
+}
+
+inline memcached_server_write_instance_st memcached_server_instance_fetch(memcached_st *ptr, uint32_t server_key)
+{
+  return &ptr->servers[server_key];
+}
+
+inline memcached_server_instance_st memcached_server_instance_by_position(const memcached_st *ptr, uint32_t server_key)
+{
+  return &ptr->servers[server_key];
+}