Cleanedup test cases (they had some valgrind warnings).
[awesomized/libmemcached] / lib / memcached_get.c
index cbe94926ba0f169d2b27e00bed9facb64f01e10f..98e24d9fbf938ba3c72a04eaa6ec0c783bad7c2d 100644 (file)
@@ -4,6 +4,7 @@
 static memcached_return memcached_value_fetch(memcached_st *ptr, char *key, size_t *key_length, 
                                               memcached_string_st *value,
                                               uint16_t *flags,
+                                              uint64_t *cas,
                                               char load_key,
                                               unsigned int server_key)
 {
@@ -12,7 +13,6 @@ static memcached_return memcached_value_fetch(memcached_st *ptr, char *key, size
   char *string_ptr;
   char *end_ptr;
 
-  memset(buffer, 0, MEMCACHED_DEFAULT_COMMAND_SIZE);
   end_ptr= buffer + MEMCACHED_DEFAULT_COMMAND_SIZE;
 
   *flags= 0;
@@ -32,10 +32,9 @@ static memcached_return memcached_value_fetch(memcached_st *ptr, char *key, size
     /* We load the key */
     if (load_key)
     {
-      memset(key, 0, MEMCACHED_MAX_KEY);
       *key_length= 0;
 
-      for (; end_ptr > string_ptr && *string_ptr != ' '; string_ptr++)
+      for (; isalnum(*string_ptr); string_ptr++)
       {
         *key= *string_ptr;
         key++;
@@ -43,7 +42,7 @@ static memcached_return memcached_value_fetch(memcached_st *ptr, char *key, size
       }
     }
     else /* Skip characters */
-      for (; end_ptr > string_ptr && *string_ptr != ' '; string_ptr++);
+      for (; isalnum(*string_ptr); string_ptr++);
 
     if (end_ptr == string_ptr)
         goto read_error;
@@ -52,7 +51,7 @@ static memcached_return memcached_value_fetch(memcached_st *ptr, char *key, size
     string_ptr++;
     if (end_ptr == string_ptr)
         goto read_error;
-    for (next_ptr= string_ptr; end_ptr > string_ptr && *string_ptr != ' '; string_ptr++);
+    for (next_ptr= string_ptr; isdigit(*string_ptr); string_ptr++);
     *flags= (uint16_t)strtol(next_ptr, &string_ptr, 10);
 
     if (end_ptr == string_ptr)
@@ -63,14 +62,25 @@ static memcached_return memcached_value_fetch(memcached_st *ptr, char *key, size
     if (end_ptr == string_ptr)
         goto read_error;
 
-    for (next_ptr= string_ptr; end_ptr > string_ptr && *string_ptr != ' '; string_ptr++);
+    for (next_ptr= string_ptr; isdigit(*string_ptr); string_ptr++);
     value_length= (size_t)strtoll(next_ptr, &string_ptr, 10);
 
     if (end_ptr == string_ptr)
         goto read_error;
 
-    /* Skip past the \r\n */
-    string_ptr+= 2;
+    /* Skip spaces */
+    if (*string_ptr == '\r')
+    {
+      /* Skip past the \r\n */
+      string_ptr+= 2;
+    }
+    else
+    {
+      string_ptr++;
+      for (next_ptr= string_ptr; isdigit(*string_ptr); string_ptr++);
+      if (cas)
+        *cas= (size_t)strtoll(next_ptr, &string_ptr, 10);
+    }
 
     if (end_ptr < string_ptr)
         goto read_error;
@@ -171,7 +181,7 @@ char *memcached_get(memcached_st *ptr, char *key, size_t key_length,
     goto error;
 
   *error= memcached_value_fetch(ptr, key, &key_length, result_buffer, 
-                                flags, 0, server_key);
+                                flags, NULL, 0, server_key);
   *value_length= memcached_string_length(result_buffer);
   if (*error == MEMCACHED_END && *value_length == 0)
   {
@@ -199,15 +209,14 @@ char *memcached_get(memcached_st *ptr, char *key, size_t key_length,
 
   LIBMEMCACHED_MEMCACHED_GET_END();
 
-
-  return  memcached_string_c_copy(result_buffer);
+  return memcached_string_c_copy(result_buffer);
 
 error:
   *value_length= 0;
 
   LIBMEMCACHED_MEMCACHED_GET_END();
 
-    return NULL;
+  return NULL;
 }
 
 memcached_return memcached_mget(memcached_st *ptr, 
@@ -216,7 +225,9 @@ memcached_return memcached_mget(memcached_st *ptr,
 {
   unsigned int x;
   memcached_return rc= MEMCACHED_NOTFOUND;
-  char *cursor_key_exec;
+  char *get_command= "get ";
+  uint8_t get_command_length= 4
+
   LIBMEMCACHED_MEMCACHED_MGET_START();
   ptr->cursor_server= 0;
 
@@ -226,8 +237,11 @@ memcached_return memcached_mget(memcached_st *ptr,
   if (ptr->number_of_hosts == 0)
     return MEMCACHED_NO_SERVERS;
 
-  cursor_key_exec= (char *)malloc(sizeof(char) * ptr->number_of_hosts);
-  memset(cursor_key_exec, 0, sizeof(char) * ptr->number_of_hosts);
+  if (ptr->flags & MEM_SUPPORT_CAS)
+  {
+    get_command= "gets ";
+    get_command_length= 5;
+  }
 
   for (x= 0; x < number_of_keys; x++)
   {
@@ -235,32 +249,34 @@ memcached_return memcached_mget(memcached_st *ptr,
 
     server_key= memcached_generate_hash(ptr, keys[x], key_length[x]);
 
-    if (cursor_key_exec[server_key] == 0)
+    if (ptr->hosts[server_key].cursor_active == 0)
     {
       rc= memcached_connect(ptr, server_key);
 
-      if ((memcached_io_write(ptr, server_key, "get ", 4, 0)) == -1)
+      if ((memcached_io_write(ptr, server_key, get_command, get_command_length, 0)) == -1)
       {
-        memcached_quit(ptr);
+        memcached_quit_server(ptr, server_key);
         rc= MEMCACHED_SOME_ERRORS;
-        break;
+        continue;
       }
+      ptr->hosts[server_key].cursor_active= 1;
     }
 
     if ((memcached_io_write(ptr, server_key, keys[x], key_length[x], 0)) == -1)
     {
-      memcached_quit(ptr);
+      ptr->hosts[server_key].cursor_active = 0;
+      memcached_quit_server(ptr, server_key);
       rc= MEMCACHED_SOME_ERRORS;
-      break;
+      continue;
     }
 
     if ((memcached_io_write(ptr, server_key, " ", 1, 0)) == -1)
     {
-      memcached_quit(ptr);
+      ptr->hosts[server_key].cursor_active = 0;
+      memcached_quit_server(ptr, server_key);
       rc= MEMCACHED_SOME_ERRORS;
-      break;
+      continue;
     }
-    cursor_key_exec[server_key]= 1;
   }
 
 
@@ -269,24 +285,17 @@ memcached_return memcached_mget(memcached_st *ptr,
   */
   for (x= 0; x < ptr->number_of_hosts; x++)
   {
-    if (cursor_key_exec[x])
+    if (ptr->hosts[x].cursor_active == 1)
     {
       /* We need to doo something about non-connnected hosts in the future */
       if ((memcached_io_write(ptr, x, "\r\n", 2, 1)) == -1)
       {
-        memcached_quit(ptr);
+        memcached_quit_server(ptr, x);
         rc= MEMCACHED_SOME_ERRORS;
-        break;
       }
-
-      ptr->hosts[x].cursor_active= 1;
     }
-    else
-      ptr->hosts[x].cursor_active= 0;
   }
 
-  free(cursor_key_exec);
-
   LIBMEMCACHED_MEMCACHED_MGET_END();
   return rc;
 }
@@ -308,22 +317,32 @@ char *memcached_fetch(memcached_st *ptr, char *key, size_t *key_length,
     }
 
     *error = memcached_value_fetch(ptr, key, key_length, result_buffer, 
-                                   flags, 1, ptr->cursor_server);
+                                   flags, NULL, 1, ptr->cursor_server);
     *value_length= memcached_string_length(result_buffer);
     
     if (*error == MEMCACHED_NOTFOUND)
+    {
+      ptr->hosts[ptr->cursor_server].cursor_active = 0;
       ptr->cursor_server++;
+    }
     else if (*error == MEMCACHED_END && *value_length == 0)
+    {
       return NULL;
+    }
     else if (*error == MEMCACHED_END)
     {
       WATCHPOINT_ASSERT(0); /* If this happens we have somehow messed up the fetch */
+      *value_length= 0;
       return NULL;
     }
     else if (*error != MEMCACHED_SUCCESS)
+    {
       return NULL;
+    }
     else
+    {
       return  memcached_string_c_copy(result_buffer);
+    }
 
   }
 
@@ -338,6 +357,8 @@ memcached_result_st *memcached_fetch_result(memcached_st *ptr,
   if (result == NULL)
     result= memcached_result_create(ptr, NULL);
 
+  WATCHPOINT_ASSERT(result->value.is_allocated != MEMCACHED_USED);
+
   while (ptr->cursor_server < ptr->number_of_hosts)
   {
     if (!ptr->hosts[ptr->cursor_server].cursor_active)
@@ -346,26 +367,42 @@ memcached_result_st *memcached_fetch_result(memcached_st *ptr,
       continue;
     }
 
+    result->cas= 0; /* We do this so we do not send in any junk */
     *error= memcached_value_fetch(ptr, result->key, &result->key_length, 
                                        &result->value, 
                                        &result->flags,
+                                       &result->cas,
                                        1, ptr->cursor_server);
     
     if (*error == MEMCACHED_NOTFOUND)
+    {
+      ptr->hosts[ptr->cursor_server].cursor_active = 0;
       ptr->cursor_server++;
+    }
     else if (*error == MEMCACHED_END && memcached_string_length((memcached_string_st *)(&result->value)) == 0)
-      return NULL;
+    {
+      break;
+    }
     else if (*error == MEMCACHED_END)
     {
       WATCHPOINT_ASSERT(0); /* If this happens we have somehow messed up the fetch */
-      return NULL;
+      break;
     }
     else if (*error != MEMCACHED_SUCCESS)
-      return NULL;
+    {
+      break;
+    }
     else
+    {
       return result;
-
+    }
   }
 
+  /* An error has occurred */
+  if (result->is_allocated == MEMCACHED_ALLOCATED)
+    memcached_result_free(result);
+  else
+    memcached_string_reset(&result->value);
+
   return NULL;
 }