Refactor test system to give me times on calling tests (frankly I still need
[m6w6/libmemcached] / lib / memcached_get.c
index c7c21d1ee43ad7518700d1c6f026ca1323dcd62a..251cd59201c0ac67236cf289b6ee48006185af2f 100644 (file)
@@ -85,17 +85,23 @@ static char *memcached_value_fetch(memcached_st *ptr, char *key, size_t *key_len
 
       /* We add two bytes so that we can walk the \r\n */
       value= (char *)malloc(((*value_length) +2) * sizeof(char));
-      memset(value, 0, ((*value_length) +2) * sizeof(char));
-
       if (!value)
       {
         *value_length= 0;
         *error= MEMCACHED_MEMORY_ALLOCATION_FAILURE;
         return NULL;
       }
+      memset(value, 0, ((*value_length) +2) * sizeof(char));
 
       value_ptr= value;
       read_length= 0;
+      /* 
+        We read the \r\n into the string since not doing so is more 
+        cycles then the waster of memory to do so.
+
+        We are null terminating through, which will most likely make
+        some people lazy about using the return length.
+      */
       to_read= (*value_length) + 2;
 
       read_length= memcached_io_read(ptr, server_key,
@@ -107,6 +113,9 @@ static char *memcached_value_fetch(memcached_st *ptr, char *key, size_t *key_len
         goto read_error;
       }
 
+      value[*value_length]= 0;
+      value[(*value_length) + 1]= 0;
+
       return value;
     }
   }
@@ -154,6 +163,8 @@ char *memcached_get(memcached_st *ptr, char *key, size_t key_length,
     *error= MEMCACHED_NOTFOUND;
     goto error;
   }
+  else if (*error == MEMCACHED_END)
+    assert(0); /* If this happens we have somehow messed up the fetch */
   else if (*error == MEMCACHED_SUCCESS)
   {
     memcached_return rc;
@@ -249,7 +260,10 @@ memcached_return memcached_mget(memcached_st *ptr,
       }
       memcached_string_free(ptr, string);
       cursor_key_exec[x]= NULL; /* Remove warning */
+      ptr->hosts[x].cursor_active= 1;
     }
+    else
+      ptr->hosts[x].cursor_active= 0;
   }
 
   free(cursor_key_exec);
@@ -267,16 +281,28 @@ char *memcached_fetch(memcached_st *ptr, char *key, size_t *key_length,
 
   while (ptr->cursor_server < ptr->number_of_hosts)
   {
+    if (!ptr->hosts[ptr->cursor_server].cursor_active)
+    {
+      ptr->cursor_server++;
+      continue;
+    }
+
     value_check= memcached_value_fetch(ptr, key, key_length, value_length, flags,
                                        error, 1, ptr->cursor_server);
     
     if (*error == MEMCACHED_NOTFOUND)
       ptr->cursor_server++;
+    else if (*error == MEMCACHED_END && *value_length == 0)
+      return NULL;
+    else if (*error == MEMCACHED_END)
+      assert(0); /* If this happens we have somehow messed up the fetch */
     else if (*error != MEMCACHED_SUCCESS)
       return NULL;
     else
       return value_check;
+
   }
 
+  *value_length= 0;
   return NULL;
 }