Merging Trond
[m6w6/libmemcached] / libmemcached / memcached_storage.c
index 4b62606832cb1462e21468f5cd5f291be116d2b8..97d345bdfd3d791e210d20d69928556cc5bd2e45 100644 (file)
@@ -24,17 +24,17 @@ static char *storage_op_string(memcached_storage_action verb)
   switch (verb)
   {
   case SET_OP:
-    return "set";
+    return "set ";
   case REPLACE_OP:
-    return "replace";
+    return "replace ";
   case ADD_OP:
-    return "add";
+    return "add ";
   case PREPEND_OP:
-    return "prepend";
+    return "prepend ";
   case APPEND_OP:
-    return "append";
+    return "append ";
   case CAS_OP:
-    return "cas";
+    return "cas ";
   default:
     return "tosserror"; /* This is impossible, fixes issue for compiler warning in VisualStudio */
   };
@@ -77,7 +77,7 @@ static inline memcached_return memcached_send(memcached_st *ptr,
   unlikely (ptr->number_of_hosts == 0)
     return MEMCACHED_NO_SERVERS;
 
-  if ((ptr->flags & MEM_VERIFY_KEY) && (memcachd_key_test((char **)&key, &key_length, 1) == MEMCACHED_BAD_KEY_PROVIDED))
+  if ((ptr->flags & MEM_VERIFY_KEY) && (memcached_key_test((char **)&key, &key_length, 1) == MEMCACHED_BAD_KEY_PROVIDED))
     return MEMCACHED_BAD_KEY_PROVIDED;
 
   server_key= memcached_generate_hash(ptr, master_key, master_key_length);
@@ -97,13 +97,39 @@ static inline memcached_return memcached_send(memcached_st *ptr,
                            (unsigned long long)cas,
                            (ptr->flags & MEM_NOREPLY) ? " noreply" : "");
   else
-    write_length= snprintf(buffer, MEMCACHED_DEFAULT_COMMAND_SIZE, 
-                           "%s %s%.*s %u %llu %zu%s\r\n", 
-                           storage_op_string(verb),
-                           ptr->prefix_key,
-                           (int)key_length, key, flags, 
+  {
+    char *buffer_ptr= buffer;
+    const char *command= storage_op_string(verb);
+
+    /* Copy in the command, no space needed, we handle that in the command function*/
+    memcpy(buffer_ptr, command, strlen(command));
+
+    /* Copy in the key prefix, switch to the buffer_ptr */
+    buffer_ptr= memcpy(buffer_ptr + strlen(command) , ptr->prefix_key, strlen(ptr->prefix_key));
+
+    /* Copy in the key, adjust point if a key prefix was used. */
+    buffer_ptr= memcpy(buffer_ptr + (ptr->prefix_key ? strlen(ptr->prefix_key) : 0), 
+                       key, key_length);
+    buffer_ptr+= key_length;
+    buffer_ptr[0]=  ' '; 
+    buffer_ptr++;
+
+    write_length= (size_t)(buffer_ptr - buffer);
+    write_length+= snprintf(buffer_ptr, MEMCACHED_DEFAULT_COMMAND_SIZE, 
+                           "%u %llu %zu%s\r\n", 
+                           flags, 
                            (unsigned long long)expiration, value_length,
                            (ptr->flags & MEM_NOREPLY) ? " noreply" : "");
+  }
+
+  if (ptr->flags & MEM_USE_UDP && ptr->flags & MEM_BUFFER_REQUESTS)
+  {
+    size_t cmd_size= write_length + value_length + 2;
+    if (cmd_size > MAX_UDP_DATAGRAM_LENGTH - UDP_DATAGRAM_HEADER_LENGTH)
+      return MEMCACHED_WRITE_FAILURE;
+    if (cmd_size + ptr->hosts[server_key].write_buffer_offset > MAX_UDP_DATAGRAM_LENGTH)
+      memcached_io_write(&ptr->hosts[server_key], NULL, 0, 1);
+  }
 
   if (write_length >= MEMCACHED_DEFAULT_COMMAND_SIZE)
   {
@@ -135,10 +161,7 @@ static inline memcached_return memcached_send(memcached_st *ptr,
   }
 
   if (ptr->flags & MEM_NOREPLY)
-  {
-    memcached_server_response_decrement(&ptr->hosts[server_key]);
     return (to_write == 0) ? MEMCACHED_BUFFERED : MEMCACHED_SUCCESS;
-  }
 
   if (to_write == 0)
     return MEMCACHED_BUFFERED;
@@ -333,8 +356,11 @@ memcached_return memcached_cas_by_key(memcached_st *ptr,
   return rc;
 }
 
-static inline uint8_t get_com_code(memcached_storage_action verb, bool noreply) {
-   uint8_t ret;
+static inline uint8_t get_com_code(memcached_storage_action verb, bool noreply)
+{
+  /* 0 isn't a value we want, but GCC 4.2 seems to think ret can otherwise
+   * be used uninitialized in this function. FAIL */
+  uint8_t ret= 0;
 
   if (noreply)
     switch (verb)
@@ -395,7 +421,7 @@ static memcached_return memcached_send_binary(memcached_server_st* server,
   char flush;
   protocol_binary_request_set request= {.bytes= {0}};
   size_t send_length= sizeof(request.bytes);
-  bool noreply = server->root->flags & MEM_NOREPLY;
+  bool noreply= server->root->flags & MEM_NOREPLY;
 
   request.message.header.request.magic= PROTOCOL_BINARY_REQ;
   request.message.header.request.opcode= get_com_code(verb, noreply);
@@ -418,6 +444,15 @@ static memcached_return memcached_send_binary(memcached_server_st* server,
   
   flush= ((server->root->flags & MEM_BUFFER_REQUESTS) && verb == SET_OP) ? 0 : 1;
 
+  if ((server->root->flags & MEM_USE_UDP) && !flush)
+  {
+    size_t cmd_size= send_length + key_length + value_length;
+    if (cmd_size > MAX_UDP_DATAGRAM_LENGTH - UDP_DATAGRAM_HEADER_LENGTH)
+      return MEMCACHED_WRITE_FAILURE;
+    if (cmd_size + server->write_buffer_offset > MAX_UDP_DATAGRAM_LENGTH)
+      memcached_io_write(server,NULL,0, 1);
+  }
+  
   /* write the header */
   if ((memcached_do(server, (const char*)request.bytes, send_length, 0) != MEMCACHED_SUCCESS) ||
       (memcached_io_write(server, key, key_length, 0) == -1) ||
@@ -427,9 +462,6 @@ static memcached_return memcached_send_binary(memcached_server_st* server,
     return MEMCACHED_WRITE_FAILURE;
   }
   
-  if (noreply)
-    memcached_server_response_decrement(server);
-
   if (flush == 0)
     return MEMCACHED_BUFFERED;