Fix in issue where no servers are to be found (introduced in last refactoring)
[m6w6/libmemcached] / lib / memcached_storage.c
index 8d2ed8c5d4adb0b3b312964f6f47830b3588a3f3..ee48cad317c9781f94d8f1750792386fc25a1bcd 100644 (file)
@@ -56,39 +56,28 @@ static inline memcached_return memcached_send(memcached_st *ptr,
   char buffer[MEMCACHED_DEFAULT_COMMAND_SIZE];
   unsigned int server_key;
 
-  WATCHPOINT_ASSERT(value);
-  WATCHPOINT_ASSERT(value_length);
+  WATCHPOINT_ASSERT(!(value == NULL && value_length > 0));
+  WATCHPOINT_ASSERT(!(value && value_length == 0));
 
   if (key_length == 0)
     return MEMCACHED_NO_KEY_PROVIDED;
 
-  /* Leaving this WATCHPOINT_ASSERT in since only a library fubar could blow this */
-#ifdef NOT_DONE
-  if (!(ptr->flags & MEM_NO_BLOCK) && ptr->write_buffer_offset != 0)
-    WATCHPOINT_ASSERT(0);
-#endif
-
-  if (ptr->hosts == NULL || ptr->number_of_hosts == 0)
+  if (ptr->number_of_hosts == 0)
     return MEMCACHED_NO_SERVERS;
-    
-  server_key= memcached_generate_hash(ptr, key, key_length);
-
-  rc= memcached_connect(ptr, server_key);
-  if (rc != MEMCACHED_SUCCESS)
-    return rc;
 
+  server_key= memcached_generate_hash(ptr, key, key_length);
 
   if (cas)
-    write_length= snprintf(buffer, MEMCACHED_DEFAULT_COMMAND_SIZE, 
-                           "%s %.*s %u %llu %zu\r\n", storage_op_string(verb),
-                           (int)key_length, key, flags, 
-                           (unsigned long long)expiration, value_length);
-  else
     write_length= snprintf(buffer, MEMCACHED_DEFAULT_COMMAND_SIZE, 
                            "%s %.*s %u %llu %zu %llu\r\n", storage_op_string(verb),
                            (int)key_length, key, flags, 
                            (unsigned long long)expiration, value_length, 
                            (unsigned long long)cas);
+  else
+    write_length= snprintf(buffer, MEMCACHED_DEFAULT_COMMAND_SIZE, 
+                           "%s %.*s %u %llu %zu\r\n", storage_op_string(verb),
+                           (int)key_length, key, flags, 
+                           (unsigned long long)expiration, value_length);
 
   if (write_length >= MEMCACHED_DEFAULT_COMMAND_SIZE)
   {
@@ -96,15 +85,9 @@ static inline memcached_return memcached_send(memcached_st *ptr,
     goto error;
   }
 
-  /* 
-    We have to flush after sending the command. Memcached is not smart enough
-    to just keep reading from the socket :(
-  */
-  if ((sent_length= memcached_io_write(ptr, server_key, buffer, write_length, 0)) == -1)
-  {
-    rc= MEMCACHED_WRITE_FAILURE;
+  rc=  memcached_do(ptr, server_key, buffer, write_length, 0);
+  if (rc != MEMCACHED_SUCCESS)
     goto error;
-  }
 
   if ((sent_length= memcached_io_write(ptr, server_key, value, value_length, 0)) == -1)
   {
@@ -124,7 +107,7 @@ static inline memcached_return memcached_send(memcached_st *ptr,
     goto error;
   }
 
-  if ((ptr->flags & MEM_NO_BLOCK) && verb == SET_OP)
+  if (to_write == 0)
   {
     rc= MEMCACHED_SUCCESS;
     memcached_server_response_increment(ptr, server_key);