Fix for Linux system for async protocol.
[awesomized/libmemcached] / lib / memcached_get.c
index db626f6fa07eefc54b9159c64adc51f05abb5e64..80423e6b56ff443b9d6ca28a3dd96b2f2e9bd703 100644 (file)
@@ -4,7 +4,7 @@
 static memcached_return memcached_value_fetch(memcached_st *ptr, char *key, size_t *key_length, 
                                               memcached_string_st *value,
                                               uint16_t *flags,
-                                              char load_key,
+                                              uint64_t *cas,
                                               unsigned int server_key)
 {
   memcached_return rc;
@@ -14,12 +14,14 @@ static memcached_return memcached_value_fetch(memcached_st *ptr, char *key, size
 
   end_ptr= buffer + MEMCACHED_DEFAULT_COMMAND_SIZE;
 
-  *flags= 0;
+  if (flags)
+    *flags= 0;
 
   memcached_string_reset(value);
 
   rc= memcached_response(ptr, buffer, MEMCACHED_DEFAULT_COMMAND_SIZE, server_key);
 
+
   if (rc == MEMCACHED_SUCCESS)
   {
     char *next_ptr;
@@ -29,11 +31,11 @@ static memcached_return memcached_value_fetch(memcached_st *ptr, char *key, size
     string_ptr+= 6; /* "VALUE " */
 
     /* We load the key */
-    if (load_key)
+    if (key)
     {
       *key_length= 0;
 
-      for (; end_ptr > string_ptr && *string_ptr != ' '; string_ptr++)
+      for (; isalnum(*string_ptr); string_ptr++)
       {
         *key= *string_ptr;
         key++;
@@ -41,7 +43,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;
@@ -50,8 +52,9 @@ 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++);
-    *flags= (uint16_t)strtol(next_ptr, &string_ptr, 10);
+    for (next_ptr= string_ptr; isdigit(*string_ptr); string_ptr++);
+    if (flags)
+      *flags= (uint16_t)strtol(next_ptr, &string_ptr, 10);
 
     if (end_ptr == string_ptr)
         goto read_error;
@@ -61,14 +64,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;
@@ -118,8 +132,6 @@ static memcached_return memcached_value_fetch(memcached_st *ptr, char *key, size
       return MEMCACHED_SUCCESS;
     }
   }
-  else if (rc == MEMCACHED_END)
-    rc= MEMCACHED_NOTFOUND;
 
   return rc;
 
@@ -135,77 +147,35 @@ char *memcached_get(memcached_st *ptr, char *key, size_t key_length,
                     uint16_t *flags,
                     memcached_return *error)
 {
-  char buffer[MEMCACHED_DEFAULT_COMMAND_SIZE];
-  char *buf_ptr= buffer;
-  unsigned int server_key;
-  memcached_string_st *result_buffer;
-  LIBMEMCACHED_MEMCACHED_GET_START();
-
-  if (key_length == 0)
-  {
-    *error= MEMCACHED_NO_KEY_PROVIDED;
-    return NULL;
-  }
+  char *value;
+  char *dummy_value;
+  size_t dummy_length;
+  uint16_t dummy_flags;
+  memcached_return dummy_error;
 
-  if (ptr->hosts == NULL || ptr->number_of_hosts == 0)
-  {
-    *error= MEMCACHED_NO_SERVERS;
-    return NULL;
-  }
+  /* Request the key */
+  *error= memcached_mget(ptr, &key, &key_length, 1);
 
-  server_key= memcached_generate_hash(ptr, key, key_length);
-  result_buffer= &ptr->result_buffer;
+  value= memcached_fetch(ptr, NULL, NULL, 
+                         value_length, flags, error);
 
-  *value_length= 0;
-  memcpy(buf_ptr, "get ", 4);
-  buf_ptr+= 4;
-  memcpy(buf_ptr, key, key_length);
-  buf_ptr+= key_length;
-  memcpy(buf_ptr, "\r\n", 2);
-  buf_ptr+= 2;
-
-  *error= memcached_do(ptr, server_key, buffer, (size_t)(buf_ptr - buffer), 1);
-  if (*error != MEMCACHED_SUCCESS)
-    goto error;
-
-  *error= memcached_value_fetch(ptr, key, &key_length, result_buffer, 
-                                flags, 0, server_key);
-  *value_length= memcached_string_length(result_buffer);
-  if (*error == MEMCACHED_END && *value_length == 0)
-  {
+  if (*error == MEMCACHED_END)
     *error= MEMCACHED_NOTFOUND;
-    goto error;
-  }
-  else if (*error == MEMCACHED_END)
-  {
-    WATCHPOINT_ASSERT(0); /* If this happens we have somehow messed up the fetch */
-  }
-  else if (*error == MEMCACHED_SUCCESS)
-  {
-    memcached_return rc;
-    /* We need to read END */
-    rc= memcached_response(ptr, buffer, MEMCACHED_DEFAULT_COMMAND_SIZE, server_key);
-
-    if (rc != MEMCACHED_END)
-    {
-      *error= MEMCACHED_PROTOCOL_ERROR;
-      goto error;
-    }
-  }
-  else 
-      goto error;
-
-  LIBMEMCACHED_MEMCACHED_GET_END();
 
+  if (value == NULL)
+    return NULL;
 
-  return  memcached_string_c_copy(result_buffer);
-
-error:
-  *value_length= 0;
+  /* We do a second read to clean the cursor */
+  dummy_value= memcached_fetch(ptr, NULL, NULL, 
+                               &dummy_length, &dummy_flags, 
+                               &dummy_error);
 
-  LIBMEMCACHED_MEMCACHED_GET_END();
+  /* Something is really wrong if this happens */
+  WATCHPOINT_ASSERT(dummy_value == NULL);
+  if (dummy_value)
+    free(dummy_value);
 
-    return NULL;
+  return value;
 }
 
 memcached_return memcached_mget(memcached_st *ptr, 
@@ -214,6 +184,9 @@ memcached_return memcached_mget(memcached_st *ptr,
 {
   unsigned int x;
   memcached_return rc= MEMCACHED_NOTFOUND;
+  char *get_command= "get ";
+  uint8_t get_command_length= 4
+
   LIBMEMCACHED_MEMCACHED_MGET_START();
   ptr->cursor_server= 0;
 
@@ -223,6 +196,14 @@ memcached_return memcached_mget(memcached_st *ptr,
   if (ptr->number_of_hosts == 0)
     return MEMCACHED_NO_SERVERS;
 
+  if (ptr->flags & MEM_SUPPORT_CAS)
+  {
+    get_command= "gets ";
+    get_command_length= 5;
+  }
+
+  memcached_finish(ptr);
+
   for (x= 0; x < number_of_keys; x++)
   {
     unsigned int server_key;
@@ -233,7 +214,7 @@ memcached_return memcached_mget(memcached_st *ptr,
     {
       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_server(ptr, server_key);
         rc= MEMCACHED_SOME_ERRORS;
@@ -244,7 +225,7 @@ memcached_return memcached_mget(memcached_st *ptr,
 
     if ((memcached_io_write(ptr, server_key, keys[x], key_length[x], 0)) == -1)
     {
-      ptr->hosts[server_key].cursor_active = 0;
+      ptr->hosts[server_key].cursor_active= 0;
       memcached_quit_server(ptr, server_key);
       rc= MEMCACHED_SOME_ERRORS;
       continue;
@@ -252,14 +233,13 @@ memcached_return memcached_mget(memcached_st *ptr,
 
     if ((memcached_io_write(ptr, server_key, " ", 1, 0)) == -1)
     {
-      ptr->hosts[server_key].cursor_active = 0;
+      ptr->hosts[server_key].cursor_active= 0;
       memcached_quit_server(ptr, server_key);
       rc= MEMCACHED_SOME_ERRORS;
       continue;
     }
   }
 
-
   /*
     Should we muddle on if some servers are dead?
   */
@@ -297,35 +277,22 @@ 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, ptr->cursor_server);
     *value_length= memcached_string_length(result_buffer);
     
-    if (*error == MEMCACHED_NOTFOUND)
+    if (*error == MEMCACHED_END) /* END means that we move on to the next */
     {
-      ptr->hosts[ptr->cursor_server].cursor_active = 0;
+      ptr->hosts[ptr->cursor_server].cursor_active= 0;
       ptr->cursor_server++;
+      continue;
     }
-    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
-    {
+    else if (*error == MEMCACHED_SUCCESS)
       return  memcached_string_c_copy(result_buffer);
-    }
-
+    else
+      return NULL;
   }
 
+  ptr->cursor_server= 0;
   *value_length= 0;
   return NULL;
 }
@@ -337,6 +304,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)
@@ -345,35 +314,62 @@ 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,
-                                       1, ptr->cursor_server);
+                                       &result->cas,
+                                       ptr->cursor_server);
     
-    if (*error == MEMCACHED_NOTFOUND)
+    if (*error == MEMCACHED_END) /* END means that we move on to the next */
     {
-      ptr->hosts[ptr->cursor_server].cursor_active = 0;
+      ptr->hosts[ptr->cursor_server].cursor_active= 0;
       ptr->cursor_server++;
+      continue;
     }
-    else if (*error == MEMCACHED_END && memcached_string_length((memcached_string_st *)(&result->value)) == 0)
-    {
-      return NULL;
-    }
-    else if (*error == MEMCACHED_END)
-    {
-      WATCHPOINT_ASSERT(0); /* If this happens we have somehow messed up the fetch */
-      return NULL;
-    }
-    else if (*error != MEMCACHED_SUCCESS)
-    {
-      return NULL;
-    }
-    else
-    {
+    else if (*error == MEMCACHED_SUCCESS)
       return result;
-    }
-
+    else
+      return NULL;
   }
 
+  /* An error has occurred */
+  if (result->is_allocated == MEMCACHED_ALLOCATED)
+    memcached_result_free(result);
+  else
+    memcached_string_reset(&result->value);
+
+  ptr->cursor_server= 0;
   return NULL;
 }
+
+memcached_return memcached_finish_server(memcached_st *ptr, unsigned int server_key)
+{
+  memcached_return rc;
+  memcached_string_st *result_buffer;
+
+  result_buffer= &ptr->result_buffer;
+
+  rc= MEMCACHED_SUCCESS;
+  while (rc == MEMCACHED_SUCCESS)
+  {
+    rc= memcached_value_fetch(ptr, NULL, NULL, result_buffer, 
+                               NULL, NULL, server_key);
+  }
+  ptr->hosts[server_key].cursor_active= 0;
+
+  return rc;
+}
+
+void memcached_finish(memcached_st *ptr)
+{
+  unsigned int x;
+
+  for (x= 0; x < ptr->number_of_hosts; x++)
+  {
+    if (ptr->hosts[x].cursor_active)
+      (void)memcached_finish_server(ptr, x);
+  }
+
+  ptr->cursor_server= 0;
+}