Added buffered IO to write calls
[m6w6/libmemcached] / lib / memcached_storage.c
index 40d36394234f024d892ddd1fff142cdc5c20aa4d..0c27c76681fdfc310c39bf99488decaec842becc 100644 (file)
@@ -7,7 +7,8 @@
 
 */
 
-#include <memcached.h>
+#include "common.h"
+#include "memcached_io.h"
 
 static memcached_return memcached_send(memcached_st *ptr, 
                                        char *key, size_t key_length, 
@@ -16,43 +17,87 @@ static memcached_return memcached_send(memcached_st *ptr,
                                        uint16_t  flags,
                                        char *verb)
 {
-  size_t send_length;
+  size_t write_length;
+  ssize_t sent_length;
   memcached_return rc;
   char buffer[MEMCACHED_DEFAULT_COMMAND_SIZE];
+  unsigned int server_key;
 
-  rc= memcached_connect(ptr);
+  assert(value);
+  assert(value_length);
+
+  memset(buffer, 0, MEMCACHED_DEFAULT_COMMAND_SIZE);
 
+  rc= memcached_connect(ptr);
   if (rc != MEMCACHED_SUCCESS)
     return rc;
 
-  send_length= snprintf(buffer, MEMCACHED_DEFAULT_COMMAND_SIZE, 
-                        "%s %.*s %u %u %u\r\n", verb,
-                        key_length, key, flags, expiration, value_length);
-  if ((send(ptr->hosts[0].fd, buffer, send_length, 0) == -1))
+  assert(ptr->write_buffer_offset == 0);
+
+  server_key= memcached_generate_hash(key, key_length) % ptr->number_of_hosts;
+
+  write_length= snprintf(buffer, MEMCACHED_DEFAULT_COMMAND_SIZE, 
+                        "%s %.*s %x %llu %zu\r\n", verb,
+                        (int)key_length, key, flags, 
+                        (unsigned long long)expiration, value_length);
+  if (write_length >= MEMCACHED_DEFAULT_COMMAND_SIZE)
+  {
+    rc= MEMCACHED_WRITE_FAILURE;
+    goto error;
+  }
+
+  if ((sent_length= memcached_io_write(ptr, server_key, buffer, write_length)) == -1)
   {
-    fprintf(stderr, "failed set on %.*s TCP\n", key_length+1, key);
+    rc= MEMCACHED_WRITE_FAILURE;
+    goto error;
+  }
+  assert(write_length == sent_length);
 
+  /* 
+    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_flush(ptr, server_key)) == -1)
     return MEMCACHED_WRITE_FAILURE;
+
+  if ((sent_length= memcached_io_write(ptr, server_key, value, value_length)) == -1)
+  {
+    rc= MEMCACHED_WRITE_FAILURE;
+    goto error;
   }
+  assert(value_length == sent_length);
 
-  send_length= snprintf(buffer, MEMCACHED_DEFAULT_COMMAND_SIZE, 
-                        "%.*s\r\n", 
-                        value_length, value);
-  if ((send(ptr->hosts[0].fd, buffer, send_length, 0) == -1))
+  if ((sent_length= memcached_io_write(ptr, server_key, "\r\n", 2)) == -1)
   {
-    fprintf(stderr, "failed set on %.*s TCP\n", key_length+1, key);
+    rc= MEMCACHED_WRITE_FAILURE;
+    goto error;
+  }
 
+  assert(2 == sent_length);
+
+  if ((sent_length= memcached_io_flush(ptr, server_key)) == -1)
     return MEMCACHED_WRITE_FAILURE;
-  }
-  
-  send_length= read(ptr->hosts[0].fd, buffer, MEMCACHED_DEFAULT_COMMAND_SIZE);
 
-  if (send_length && buffer[0] == 'S')  /* STORED */
+  //assert(sent_length == write_length + value_length + 2);
+
+  sent_length= recv(ptr->hosts[server_key].fd, buffer, MEMCACHED_DEFAULT_COMMAND_SIZE, 0);
+
+  if (sent_length && buffer[0] == 'S')  /* STORED */
     return MEMCACHED_SUCCESS;
-  else if (send_length && buffer[0] == 'N')  /* NOT_STORED */
+  else if (write_length && buffer[0] == 'N')  /* NOT_STORED */
     return MEMCACHED_NOTSTORED;
+  else if (write_length && buffer[0] == 'E')  /* ERROR */
+  {
+    printf("BUFFER :%s:\n", buffer);
+    return MEMCACHED_PROTOCOL_ERROR;
+  }
   else
     return MEMCACHED_READ_FAILURE;
+
+error:
+  memcached_io_reset(ptr, server_key);
+
+  return rc;
 }
 
 memcached_return memcached_set(memcached_st *ptr, char *key, size_t key_length, 
@@ -60,8 +105,12 @@ memcached_return memcached_set(memcached_st *ptr, char *key, size_t key_length,
                                time_t expiration,
                                uint16_t  flags)
 {
-  return  memcached_send(ptr, key, key_length, value, value_length,
+  memcached_return rc;
+  LIBMEMCACHED_MEMCACHED_SET_START();
+  rc= memcached_send(ptr, key, key_length, value, value_length,
                          expiration, flags, "set");
+  LIBMEMCACHED_MEMCACHED_SET_END();
+  return rc;
 }
 
 memcached_return memcached_add(memcached_st *ptr, char *key, size_t key_length,
@@ -69,8 +118,12 @@ memcached_return memcached_add(memcached_st *ptr, char *key, size_t key_length,
                                time_t expiration,
                                uint16_t  flags)
 {
-  return  memcached_send(ptr, key, key_length, value, value_length,
+  memcached_return rc;
+  LIBMEMCACHED_MEMCACHED_ADD_START();
+  rc= memcached_send(ptr, key, key_length, value, value_length,
                          expiration, flags, "add");
+  LIBMEMCACHED_MEMCACHED_ADD_END();
+  return rc;
 }
 
 memcached_return memcached_replace(memcached_st *ptr, char *key, size_t key_length,
@@ -78,6 +131,10 @@ memcached_return memcached_replace(memcached_st *ptr, char *key, size_t key_leng
                                    time_t expiration,
                                    uint16_t  flags)
 {
-  return  memcached_send(ptr, key, key_length, value, value_length,
+  memcached_return rc;
+  LIBMEMCACHED_MEMCACHED_REPLACE_START();
+  rc= memcached_send(ptr, key, key_length, value, value_length,
                          expiration, flags, "replace");
+  LIBMEMCACHED_MEMCACHED_REPLACE_END();
+  return rc;
 }