Cleans up the assert and some other things that Fedora 15 found.
[m6w6/libmemcached] / libmemcached / get.cc
index 29d01bc8c293d6d2a844b541af67e871a7f4fafb..794d3d152e8d6dd47513246ab62e637bc09335b0 100644 (file)
@@ -1,15 +1,41 @@
-/* LibMemcached
- * Copyright (C) 2006-2009 Brian Aker
- * All rights reserved.
+/*  vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
+ * 
+ *  Libmemcached library
  *
- * Use and distribution licensed under the BSD license.  See
- * the COPYING file in the parent directory for full text.
+ *  Copyright (C) 2011 Data Differential, http://datadifferential.com/
+ *  Copyright (C) 2006-2009 Brian Aker All rights reserved.
  *
- * Summary: Get functions for libmemcached
+ *  Redistribution and use in source and binary forms, with or without
+ *  modification, are permitted provided that the following conditions are
+ *  met:
+ *
+ *      * Redistributions of source code must retain the above copyright
+ *  notice, this list of conditions and the following disclaimer.
+ *
+ *      * Redistributions in binary form must reproduce the above
+ *  copyright notice, this list of conditions and the following disclaimer
+ *  in the documentation and/or other materials provided with the
+ *  distribution.
+ *
+ *      * The names of its contributors may not be used to endorse or
+ *  promote products derived from this software without specific prior
+ *  written permission.
+ *
+ *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ *  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ *  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+ *  A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+ *  OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ *  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+ *  LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ *  DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ *  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ *  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+ *  OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  *
  */
 
-#include "common.h"
+#include <libmemcached/common.h>
 
 /*
   What happens if no servers exist?
@@ -40,24 +66,46 @@ char *memcached_get_by_key(memcached_st *ptr,
                            uint32_t *flags,
                            memcached_return_t *error)
 {
-  char *value;
-  size_t dummy_length;
-  uint32_t dummy_flags;
-  memcached_return_t dummy_error;
+  memcached_return_t unused;
+  if (error == NULL)
+    error= &unused;
 
   unlikely (ptr->flags.use_udp)
   {
-    *error= MEMCACHED_NOT_SUPPORTED;
+    if (value_length) 
+      *value_length= 0;
+
+    *error= memcached_set_error(*ptr, MEMCACHED_NOT_SUPPORTED, MEMCACHED_AT);
     return NULL;
   }
 
+  uint64_t query_id= ptr->query_id;
+  (void)query_id;
+
   /* Request the key */
   *error= memcached_mget_by_key_real(ptr, group_key, group_key_length,
-                                     (const char * const *)&key,
-                                     &key_length, 1, false);
+                                     (const char * const *)&key, &key_length, 
+                                     1, false);
+  assert_msg(ptr->query_id == query_id +1, "Programmer error, the query_id was not incremented.");
+
+
+  if (memcached_failed(*error))
+  {
+    if (memcached_has_current_error(*ptr)) // Find the most accurate error
+    {
+      *error= memcached_last_error(ptr);
+    }
+
+    if (value_length) 
+      *value_length= 0;
+
+    return NULL;
+  }
+
+  char *value= memcached_fetch(ptr, NULL, NULL,
+                               value_length, flags, error);
+  assert_msg(ptr->query_id == query_id +1, "Programmer error, the query_id was not incremented.");
 
-  value= memcached_fetch(ptr, NULL, NULL,
-                         value_length, flags, error);
   /* This is for historical reasons */
   if (*error == MEMCACHED_END)
     *error= MEMCACHED_NOTFOUND;
@@ -66,10 +114,8 @@ char *memcached_get_by_key(memcached_st *ptr,
   {
     if (ptr->get_key_failure && *error == MEMCACHED_NOTFOUND)
     {
-      memcached_return_t rc;
-
       memcached_result_reset(&ptr->result);
-      rc= ptr->get_key_failure(ptr, key, key_length, &ptr->result);
+      memcached_return_t rc= ptr->get_key_failure(ptr, key, key_length, &ptr->result);
 
       /* On all failure drop to returning NULL */
       if (rc == MEMCACHED_SUCCESS || rc == MEMCACHED_BUFFERED)
@@ -88,7 +134,9 @@ char *memcached_get_by_key(memcached_st *ptr,
                             (memcached_result_flags(&ptr->result)));
 
           if (rc == MEMCACHED_BUFFERED && latch == 0)
+          {
             memcached_behavior_set(ptr, MEMCACHED_BEHAVIOR_BUFFER_REQUESTS, 0);
+          }
         }
         else
         {
@@ -104,18 +152,25 @@ char *memcached_get_by_key(memcached_st *ptr,
           *error= rc;
           *value_length= memcached_result_length(&ptr->result);
           *flags= memcached_result_flags(&ptr->result);
-          return memcached_string_c_copy(&ptr->result.value);
+          return memcached_string_take_value(&ptr->result.value);
         }
       }
     }
+    assert_msg(ptr->query_id == query_id +1, "Programmer error, the query_id was not incremented.");
 
     return NULL;
   }
 
-  (void)memcached_fetch(ptr, NULL, NULL,
-                        &dummy_length, &dummy_flags,
-                        &dummy_error);
+  size_t dummy_length;
+  uint32_t dummy_flags;
+  memcached_return_t dummy_error;
+
+  char *dummy_value= memcached_fetch(ptr, NULL, NULL,
+                                     &dummy_length, &dummy_flags,
+                                     &dummy_error);
   WATCHPOINT_ASSERT(dummy_length == 0);
+  WATCHPOINT_ASSERT(dummy_value == 0);
+  assert_msg(ptr->query_id == query_id +1, "Programmer error, the query_id was not incremented.");
 
   return value;
 }
@@ -148,7 +203,6 @@ static memcached_return_t memcached_mget_by_key_real(memcached_st *ptr,
   const char *get_command= "get ";
   uint8_t get_command_length= 4;
   unsigned int master_server_key= (unsigned int)-1; /* 0 is a valid server id! */
-  bool is_group_key_set= false;
 
   memcached_return_t rc;
   if (memcached_failed(rc= initialize_query(ptr)))
@@ -157,22 +211,29 @@ static memcached_return_t memcached_mget_by_key_real(memcached_st *ptr,
   }
 
   unlikely (ptr->flags.use_udp)
-    return MEMCACHED_NOT_SUPPORTED;
+  {
+    return memcached_set_error(*ptr, MEMCACHED_NOT_SUPPORTED, MEMCACHED_AT);
+  }
 
   LIBMEMCACHED_MEMCACHED_MGET_START();
 
   if (number_of_keys == 0)
-    return MEMCACHED_NOTFOUND;
+  {
+    return memcached_set_error(*ptr, MEMCACHED_NOTFOUND, MEMCACHED_AT, memcached_literal_param("number_of_keys was zero"));
+  }
 
-  if (ptr->flags.verify_key && (memcached_key_test(keys, key_length, number_of_keys) == MEMCACHED_BAD_KEY_PROVIDED))
+  if (memcached_failed(memcached_key_test(*ptr, keys, key_length, number_of_keys)))
   {
-    return MEMCACHED_BAD_KEY_PROVIDED;
+    return memcached_set_error(*ptr, MEMCACHED_BAD_KEY_PROVIDED, MEMCACHED_AT, memcached_literal_param("A bad key value was provided"));
   }
 
+  bool is_group_key_set= false;
   if (group_key && group_key_length)
   {
-    if (ptr->flags.verify_key and (memcached_key_test((const char * const *)&group_key, &group_key_length, 1) == MEMCACHED_BAD_KEY_PROVIDED))
-      return MEMCACHED_BAD_KEY_PROVIDED;
+    if (memcached_failed(memcached_key_test(*ptr, (const char * const *)&group_key, &group_key_length, 1)))
+    {
+      return memcached_set_error(*ptr, MEMCACHED_BAD_KEY_PROVIDED, MEMCACHED_AT, memcached_literal_param("A bad group key was provided."));
+    }
 
     master_server_key= memcached_generate_hash_with_redistribution(ptr, group_key, group_key_length);
     is_group_key_set= true;
@@ -238,7 +299,7 @@ static memcached_return_t memcached_mget_by_key_real(memcached_st *ptr,
     struct libmemcached_io_vector_st vector[]=
     {
       { get_command_length, get_command },
-      { memcached_array_size(ptr->prefix_key), memcached_array_string(ptr->prefix_key) },
+      { memcached_array_size(ptr->_namespace), memcached_array_string(ptr->_namespace) },
       { key_length[x], keys[x] },
       { 1, " " }
     };
@@ -248,8 +309,9 @@ static memcached_return_t memcached_mget_by_key_real(memcached_st *ptr,
     {
       rc= memcached_connect(instance);
 
-      if (rc != MEMCACHED_SUCCESS)
+      if (memcached_failed(rc))
       {
+        memcached_set_error(*instance, rc, MEMCACHED_AT);
         continue;
       }
       hosts_connected++;
@@ -278,10 +340,10 @@ static memcached_return_t memcached_mget_by_key_real(memcached_st *ptr,
   {
     LIBMEMCACHED_MEMCACHED_MGET_END();
 
-    if (rc != MEMCACHED_SUCCESS)
+    if (memcached_failed(rc))
       return rc;
 
-    return MEMCACHED_NO_SERVERS;
+    return memcached_set_error(*ptr, MEMCACHED_NO_SERVERS, MEMCACHED_AT);
   }
 
 
@@ -311,12 +373,14 @@ static memcached_return_t memcached_mget_by_key_real(memcached_st *ptr,
   LIBMEMCACHED_MEMCACHED_MGET_END();
 
   if (failures_occured_in_sending && success_happened)
+  {
     return MEMCACHED_SOME_ERRORS;
+  }
 
   if (success_happened)
     return MEMCACHED_SUCCESS;
 
-  return MEMCACHED_FAILURE;
+  return MEMCACHED_FAILURE; // Complete failure occurred
 }
 
 memcached_return_t memcached_mget_by_key(memcached_st *ptr,
@@ -389,7 +453,6 @@ static memcached_return_t simple_binary_mget(memcached_st *ptr,
   for (uint32_t x= 0; x < number_of_keys; ++x)
   {
     uint32_t server_key;
-    memcached_server_write_instance_st instance;
 
     if (is_group_key_set)
     {
@@ -400,12 +463,12 @@ static memcached_return_t simple_binary_mget(memcached_st *ptr,
       server_key= memcached_generate_hash_with_redistribution(ptr, keys[x], key_length[x]);
     }
 
-    instance= memcached_server_instance_fetch(ptr, server_key);
+    memcached_server_write_instance_st instance= memcached_server_instance_fetch(ptr, server_key);
 
     if (memcached_server_response_count(instance) == 0)
     {
       rc= memcached_connect(instance);
-      if (rc != MEMCACHED_SUCCESS)
+      if (memcached_failed(rc))
         continue;
     }
 
@@ -429,14 +492,14 @@ static memcached_return_t simple_binary_mget(memcached_st *ptr,
       return vk;
     }
 
-    request.message.header.request.keylen= htons((uint16_t)(key_length[x] + memcached_array_size(ptr->prefix_key)));
+    request.message.header.request.keylen= htons((uint16_t)(key_length[x] + memcached_array_size(ptr->_namespace)));
     request.message.header.request.datatype= PROTOCOL_BINARY_RAW_BYTES;
-    request.message.header.request.bodylen= htonl((uint32_t)( key_length[x] + memcached_array_size(ptr->prefix_key)));
+    request.message.header.request.bodylen= htonl((uint32_t)( key_length[x] + memcached_array_size(ptr->_namespace)));
 
     struct libmemcached_io_vector_st vector[]=
     {
       { sizeof(request.bytes), request.bytes },
-      { memcached_array_size(ptr->prefix_key), memcached_array_string(ptr->prefix_key) },
+      { memcached_array_size(ptr->_namespace), memcached_array_string(ptr->_namespace) },
       { key_length[x], keys[x] }
     };
 
@@ -516,8 +579,6 @@ static memcached_return_t replication_binary_mget(memcached_st *ptr,
 
     for (uint32_t x= 0; x < number_of_keys; ++x)
     {
-      memcached_server_write_instance_st instance;
-
       if (hash[x] == memcached_server_count(ptr))
         continue; /* Already successfully sent */
 
@@ -533,12 +594,12 @@ static memcached_return_t replication_binary_mget(memcached_st *ptr,
       if (dead_servers[server])
         continue;
 
-      instance= memcached_server_instance_fetch(ptr, server);
+      memcached_server_write_instance_st instance= memcached_server_instance_fetch(ptr, server);
 
       if (memcached_server_response_count(instance) == 0)
       {
         rc= memcached_connect(instance);
-        if (rc != MEMCACHED_SUCCESS)
+        if (memcached_failed(rc))
         {
           memcached_io_reset(instance);
           dead_servers[server]= true;
@@ -550,9 +611,9 @@ static memcached_return_t replication_binary_mget(memcached_st *ptr,
       protocol_binary_request_getk request= {};
       request.message.header.request.magic= PROTOCOL_BINARY_REQ;
       request.message.header.request.opcode= PROTOCOL_BINARY_CMD_GETK;
-      request.message.header.request.keylen= htons((uint16_t)(key_length[x] + memcached_array_size(ptr->prefix_key)));
+      request.message.header.request.keylen= htons((uint16_t)(key_length[x] + memcached_array_size(ptr->_namespace)));
       request.message.header.request.datatype= PROTOCOL_BINARY_RAW_BYTES;
-      request.message.header.request.bodylen= htonl((uint32_t)(key_length[x] + memcached_array_size(ptr->prefix_key)));
+      request.message.header.request.bodylen= htonl((uint32_t)(key_length[x] + memcached_array_size(ptr->_namespace)));
 
       /*
        * We need to disable buffering to actually know that the request was
@@ -567,7 +628,7 @@ static memcached_return_t replication_binary_mget(memcached_st *ptr,
       struct libmemcached_io_vector_st vector[]=
       {
         { sizeof(request.bytes), request.bytes },
-        { memcached_array_size(ptr->prefix_key), memcached_array_string(ptr->prefix_key) },
+        { memcached_array_size(ptr->_namespace), memcached_array_string(ptr->_namespace) },
         { key_length[x], keys[x] }
       };
 
@@ -598,51 +659,43 @@ static memcached_return_t binary_mget_by_key(memcached_st *ptr,
                                              size_t number_of_keys,
                                              bool mget_mode)
 {
-  memcached_return_t rc;
-
   if (ptr->number_of_replicas == 0)
   {
-    rc= simple_binary_mget(ptr, master_server_key, is_group_key_set,
-                           keys, key_length, number_of_keys, mget_mode);
+    return simple_binary_mget(ptr, master_server_key, is_group_key_set,
+                              keys, key_length, number_of_keys, mget_mode);
   }
-  else
-  {
-    uint32_t* hash;
-    bool* dead_servers;
 
-    hash= static_cast<uint32_t*>(libmemcached_malloc(ptr, sizeof(uint32_t) * number_of_keys));
-    dead_servers= static_cast<bool*>(libmemcached_calloc(ptr, memcached_server_count(ptr), sizeof(bool)));
+  uint32_t* hash= static_cast<uint32_t*>(libmemcached_malloc(ptr, sizeof(uint32_t) * number_of_keys));
+  bool* dead_servers= static_cast<bool*>(libmemcached_calloc(ptr, memcached_server_count(ptr), sizeof(bool)));
 
-    if (hash == NULL || dead_servers == NULL)
-    {
-      libmemcached_free(ptr, hash);
-      libmemcached_free(ptr, dead_servers);
-      return MEMCACHED_MEMORY_ALLOCATION_FAILURE;
-    }
+  if (hash == NULL || dead_servers == NULL)
+  {
+    libmemcached_free(ptr, hash);
+    libmemcached_free(ptr, dead_servers);
+    return MEMCACHED_MEMORY_ALLOCATION_FAILURE;
+  }
 
-    if (is_group_key_set)
+  if (is_group_key_set)
+  {
+    for (size_t x= 0; x < number_of_keys; x++)
     {
-      for (size_t x= 0; x < number_of_keys; x++)
-      {
-        hash[x]= master_server_key;
-      }
+      hash[x]= master_server_key;
     }
-    else
+  }
+  else
+  {
+    for (size_t x= 0; x < number_of_keys; x++)
     {
-      for (size_t x= 0; x < number_of_keys; x++)
-      {
-        hash[x]= memcached_generate_hash_with_redistribution(ptr, keys[x], key_length[x]);
-      }
+      hash[x]= memcached_generate_hash_with_redistribution(ptr, keys[x], key_length[x]);
     }
+  }
 
-    rc= replication_binary_mget(ptr, hash, dead_servers, keys,
-                                key_length, number_of_keys);
-
-    libmemcached_free(ptr, hash);
-    libmemcached_free(ptr, dead_servers);
+  memcached_return_t rc= replication_binary_mget(ptr, hash, dead_servers, keys,
+                                                 key_length, number_of_keys);
 
-    return MEMCACHED_SUCCESS;
-  }
+  WATCHPOINT_IFERROR(rc);
+  libmemcached_free(ptr, hash);
+  libmemcached_free(ptr, dead_servers);
 
-  return rc;
+  return MEMCACHED_SUCCESS;
 }