Update for OSX/non binary platforms.
[awesomized/libmemcached] / libmemcached / behavior.c
index ab2c02306216968d3da96a092704a143843a1a35..0df9603365d97e8b6156fd5c628cb411f5810507 100644 (file)
@@ -9,7 +9,9 @@
  *
  */
 
-#include "common.h"
+#include <libmemcached/common.h>
+#include <libmemcached/virtual_bucket.h>
+
 #include <time.h>
 #include <sys/types.h>
 
@@ -103,7 +105,7 @@ memcached_return_t memcached_behavior_set(memcached_st *ptr,
     {
       (void)memcached_behavior_set_key_hash(ptr, MEMCACHED_HASH_MD5);
       (void)memcached_behavior_set_distribution_hash(ptr, MEMCACHED_HASH_MD5);
-      ptr->flags.ketama_weighted= set_flag(data);
+      ptr->ketama.weighted= set_flag(data);
       /**
         @note We try to keep the same distribution going. This should be deprecated and rewritten.
       */
@@ -169,55 +171,12 @@ memcached_return_t memcached_behavior_set(memcached_st *ptr,
       break;
   case MEMCACHED_BEHAVIOR_CORK:
       {
-        memcached_server_write_instance_st instance;
-        bool action= set_flag(data);
-
-        if (action == false)
-        {
-          ptr->flags.cork= set_flag(false);
-          return MEMCACHED_SUCCESS;
-        }
-
-        instance= memcached_server_instance_fetch(ptr, 0);
-        if (! instance)
-          return MEMCACHED_NO_SERVERS;
-
-
-        /* We just try the first host, and if it is down we return zero */
-        memcached_return_t rc;
-        rc= memcached_connect(instance);
-        if (rc != MEMCACHED_SUCCESS)
-        {
-          return rc;
-        }
-
-        /* Now we test! */
-        memcached_ternary_t enabled;
-        enabled= test_cork(instance, true);
-
-        switch (enabled)
-        {
-        case MEM_FALSE:
-          return ptr->cached_errno ? MEMCACHED_ERRNO : MEMCACHED_FAILURE ;
-        case MEM_TRUE:
-          {
-            enabled= test_cork(instance, false);
-
-            if (enabled == false) // Possible bug in OS?
-            {
-              memcached_quit_server(instance, false); // We should reset everything on this error.
-              return MEMCACHED_ERRNO;  // Errno will be true because we will have already set it.
-            }
-            ptr->flags.cork= true;
-            ptr->flags.tcp_nodelay= true;
-            memcached_quit(ptr); // We go on and reset the connections.
-          }
-          break;
-        case MEM_NOT:
-        default:
-          return memcached_set_error_string(ptr, MEMCACHED_NOT_SUPPORTED, 
-                                            memcached_string_with_size("MEMCACHED_BEHAVIOR_CORK is not supported on this platform."));
-        }
+#ifdef HAVE_MSG_MORE
+      break;
+#else
+      return memcached_set_error_string(ptr, MEMCACHED_NOT_SUPPORTED, 
+                                        memcached_string_with_size("MEMCACHED_BEHAVIOR_CORK is not supported on this platform."));
+#endif
       }
       break;
   case MEMCACHED_BEHAVIOR_LOAD_FROM_FILE:
@@ -269,7 +228,7 @@ uint64_t memcached_behavior_get(memcached_st *ptr,
   case MEMCACHED_BEHAVIOR_VERIFY_KEY:
     return ptr->flags.verify_key;
   case MEMCACHED_BEHAVIOR_KETAMA_WEIGHTED:
-    return ptr->flags.ketama_weighted;
+    return ptr->ketama.weighted;
   case MEMCACHED_BEHAVIOR_DISTRIBUTION:
     return ptr->distribution;
   case MEMCACHED_BEHAVIOR_KETAMA:
@@ -321,7 +280,7 @@ uint64_t memcached_behavior_get(memcached_st *ptr,
 
         if (getsockopt(instance->fd, SOL_SOCKET, SO_SNDBUF, &sock_size, &sock_length) < 0)
         {
-          ptr->cached_errno= errno;
+          memcached_set_errno(ptr, errno, NULL);
           return 0; /* Zero means error */
         }
       }
@@ -357,7 +316,7 @@ uint64_t memcached_behavior_get(memcached_st *ptr,
 
         if (getsockopt(instance->fd, SOL_SOCKET, SO_RCVBUF, &sock_size, &sock_length) < 0)
         {
-          ptr->cached_errno= errno;
+          memcached_set_errno(ptr, errno, NULL);
           return 0; /* Zero means error */
         }
 
@@ -377,7 +336,11 @@ uint64_t memcached_behavior_get(memcached_st *ptr,
   case MEMCACHED_BEHAVIOR_RANDOMIZE_REPLICA_READ:
     return ptr->flags.randomize_replica_read;
   case MEMCACHED_BEHAVIOR_CORK:
-    return ptr->flags.cork;
+#ifdef HAVE_MSG_MORE
+    return true;
+#else
+    return false;
+#endif
   case MEMCACHED_BEHAVIOR_TCP_KEEPALIVE:
     return ptr->flags.tcp_keepalive;
   case MEMCACHED_BEHAVIOR_LOAD_FROM_FILE:
@@ -396,6 +359,14 @@ memcached_return_t memcached_behavior_set_distribution(memcached_st *ptr, memcac
 {
   if (type < MEMCACHED_DISTRIBUTION_CONSISTENT_MAX)
   {
+    if (MEMCACHED_DISTRIBUTION_CONSISTENT_WEIGHTED)
+    {
+      ptr->ketama.weighted= true;
+    }
+    else
+    {
+      ptr->ketama.weighted= false;
+    }
     ptr->distribution= type;
     run_distribution(ptr);
     return MEMCACHED_SUCCESS;
@@ -492,7 +463,41 @@ const char *libmemcached_string_distribution(const memcached_server_distribution
   case MEMCACHED_DISTRIBUTION_CONSISTENT_KETAMA: return "MEMCACHED_DISTRIBUTION_CONSISTENT_KETAMA";
   case MEMCACHED_DISTRIBUTION_RANDOM: return "MEMCACHED_DISTRIBUTION_RANDOM";
   case MEMCACHED_DISTRIBUTION_CONSISTENT_KETAMA_SPY: return "MEMCACHED_DISTRIBUTION_CONSISTENT_KETAMA_SPY";
+  case MEMCACHED_DISTRIBUTION_CONSISTENT_WEIGHTED: return "MEMCACHED_DISTRIBUTION_CONSISTENT_WEIGHTED";
+  case MEMCACHED_DISTRIBUTION_VIRTUAL_BUCKET: return "MEMCACHED_DISTRIBUTION_VIRTUAL_BUCKET";
   default:
   case MEMCACHED_DISTRIBUTION_CONSISTENT_MAX: return "INVALID memcached_server_distribution_t";
   }
 }
+
+memcached_return_t memcached_bucket_set(memcached_st *self,
+                                        const uint32_t *host_map,
+                                        const uint32_t *forward_map,
+                                        const uint32_t buckets,
+                                        const uint32_t replicas)
+{
+  memcached_return_t rc;
+
+  if (! self)
+    return MEMCACHED_INVALID_ARGUMENTS;
+
+  if (! host_map)
+    return MEMCACHED_INVALID_ARGUMENTS;
+
+  memcached_server_distribution_t old;
+  old= memcached_behavior_get_distribution(self);
+
+  rc =memcached_behavior_set_distribution(self, MEMCACHED_DISTRIBUTION_VIRTUAL_BUCKET);
+  if (rc != MEMCACHED_SUCCESS)
+  {
+    return rc;
+  }
+
+  rc= memcached_virtual_bucket_create(self, host_map, forward_map, buckets, replicas);
+  if (rc != MEMCACHED_SUCCESS)
+  {
+    memcached_behavior_set_distribution(self, old);
+  }
+
+  return rc;
+}