Fix copyright headers in libhashkit.
[awesomized/libmemcached] / libmemcached / behavior.cc
index c91acc5d8099099699284034e80ad525f2855202..c381a513cfbceab5322de4507da5b29698e0403b 100644 (file)
 #include <ctime>
 #include <sys/types.h>
 
+bool memcached_is_consistent_distribution(const memcached_st* memc)
+{
+  switch (memc->distribution)
+  {
+  case MEMCACHED_DISTRIBUTION_CONSISTENT:
+  case MEMCACHED_DISTRIBUTION_CONSISTENT_KETAMA:
+  case MEMCACHED_DISTRIBUTION_CONSISTENT_KETAMA_SPY:
+  case MEMCACHED_DISTRIBUTION_CONSISTENT_WEIGHTED:
+    return true;
+
+  case MEMCACHED_DISTRIBUTION_MODULA:
+  case MEMCACHED_DISTRIBUTION_RANDOM:
+  case MEMCACHED_DISTRIBUTION_VIRTUAL_BUCKET:
+  case MEMCACHED_DISTRIBUTION_CONSISTENT_MAX:
+    break;
+  }
+
+  return false;
+}
+
 /*
   This function is used to modify the behavior of running client.
 
@@ -335,13 +355,17 @@ uint64_t memcached_behavior_get(memcached_st *ptr,
     return ptr->flags.verify_key;
 
   case MEMCACHED_BEHAVIOR_KETAMA_WEIGHTED:
-    return ptr->ketama.weighted;
+    if (memcached_is_consistent_distribution(ptr))
+    {
+      return memcached_is_weighted_ketama(ptr);
+    }
+    return false;
 
   case MEMCACHED_BEHAVIOR_DISTRIBUTION:
     return ptr->distribution;
 
   case MEMCACHED_BEHAVIOR_KETAMA:
-    return (ptr->distribution == MEMCACHED_DISTRIBUTION_CONSISTENT_KETAMA) ? (uint64_t) 1 : 0;
+    return memcached_is_consistent_distribution(ptr);
 
   case MEMCACHED_BEHAVIOR_HASH:
     return hashkit_get_function(&ptr->hashkit);
@@ -387,7 +411,7 @@ uint64_t memcached_behavior_get(memcached_st *ptr,
         return (uint64_t) ptr->send_size;
       }
 
-      memcached_server_write_instance_st instance= memcached_server_instance_fetch(ptr, 0);
+      org::libmemcached::Instance* instance= memcached_instance_fetch(ptr, 0);
 
       if (instance) // If we have an instance we test, otherwise we just set and pray
       {
@@ -421,7 +445,7 @@ uint64_t memcached_behavior_get(memcached_st *ptr,
       if (ptr->recv_size != -1) // If value is -1 then we are using the default
         return (uint64_t) ptr->recv_size;
 
-      memcached_server_write_instance_st instance= memcached_server_instance_fetch(ptr, 0);
+      org::libmemcached::Instance* instance= memcached_instance_fetch(ptr, 0);
 
       /**
         @note REFACTOR
@@ -498,7 +522,7 @@ memcached_return_t memcached_behavior_set_distribution(memcached_st *ptr, memcac
 
   case MEMCACHED_DISTRIBUTION_CONSISTENT:
   case MEMCACHED_DISTRIBUTION_CONSISTENT_KETAMA:
-    ptr->ketama.weighted= false;
+    memcached_set_weighted_ketama(ptr, false);
     break;
 
   case MEMCACHED_DISTRIBUTION_RANDOM:
@@ -508,7 +532,7 @@ memcached_return_t memcached_behavior_set_distribution(memcached_st *ptr, memcac
     break;
 
   case MEMCACHED_DISTRIBUTION_CONSISTENT_WEIGHTED:
-    ptr->ketama.weighted= true;
+    memcached_set_weighted_ketama(ptr, true);
     break;
 
   case MEMCACHED_DISTRIBUTION_VIRTUAL_BUCKET:
@@ -519,6 +543,7 @@ memcached_return_t memcached_behavior_set_distribution(memcached_st *ptr, memcac
     return memcached_set_error(*ptr, MEMCACHED_INVALID_ARGUMENTS, MEMCACHED_AT,
                                memcached_literal_param("Invalid memcached_server_distribution_t"));
   }
+  ptr->distribution= type;
 
   return run_distribution(ptr);
 }