attempt to fix #12, #49 and #65
[awesomized/libmemcached] / libmemcached / delete.cc
index 6be7e224ec70e52ee1502098b7779ac1fda02f4d..9575a95722eeff922dc1b770aeb47215647fe2c0 100644 (file)
 #include <libmemcached/common.h>
 #include <libmemcached/memcached/protocol_binary.h>
 
-memcached_return_t memcached_delete(memcached_st *ptr, const char *key, size_t key_length,
+memcached_return_t memcached_delete(memcached_st *shell, const char *key, size_t key_length,
                                     time_t expiration)
 {
-  return memcached_delete_by_key(ptr, key, key_length,
-                                 key, key_length, expiration);
+  return memcached_delete_by_key(shell, key, key_length, key, key_length, expiration);
 }
 
-static inline memcached_return_t ascii_delete(memcached_st *ptr,
-                                              memcached_server_write_instance_st instance,
+static inline memcached_return_t ascii_delete(memcached_instance_st* instance,
                                               uint32_t ,
                                               const char *key,
-                                              size_t key_length,
-                                              uint64_t expiration,
-                                              bool& reply,
-                                              bool& flush)
+                                              const size_t key_length,
+                                              const bool reply,
+                                              const bool is_buffering)
 {
-  char buffer[MEMCACHED_DEFAULT_COMMAND_SIZE];
-  int send_length;
-
-  if (expiration)
-  {
-    if ((instance->major_version == 1 and
-         instance->minor_version > 2) or
-        instance->major_version > 1)
-    {
-      return memcached_set_error(*ptr, MEMCACHED_INVALID_ARGUMENTS, MEMCACHED_AT, 
-                                 memcached_literal_param("Memcached server version does not allow expiration of deleted items"));
-    }
-    else
-    {
-      /* ensure that we are connected, otherwise we might bump the
-       * command counter before connection */
-      memcached_return_t rc;
-      if ((rc= memcached_connect(instance)) != MEMCACHED_SUCCESS)
-      {
-        WATCHPOINT_ERROR(rc);
-        return rc;
-      }
-
-      if (instance->minor_version == 0)
-      {
-        if (reply == false or flush == false)
-        {
-          /* We might get out of sync with the server if we send this command
-           * to a server newer than 1.2.x..  enable reply and buffered mode.
-         */
-          flush= true;
-          if (reply == false)
-          {
-            memcached_server_response_increment(instance);
-          }
-          reply= true;
-        }
-      }
-
-      send_length= snprintf(buffer, MEMCACHED_DEFAULT_COMMAND_SIZE,
-                            "delete %.*s%.*s %u%s\r\n",
-                            memcached_print_array(ptr->_namespace),
-                            (int) key_length, key,
-                            (uint32_t)expiration,
-                            reply ? "" :  " noreply");
-    }
-  }
-  else
-  {
-    send_length= snprintf(buffer, MEMCACHED_DEFAULT_COMMAND_SIZE,
-                          "delete %.*s%.*s%s\r\n",
-                          memcached_print_array(ptr->_namespace),
-                          (int)key_length, key, 
-                          reply ? "" :  " noreply");
-  }
-
-  if (send_length >= MEMCACHED_DEFAULT_COMMAND_SIZE || send_length < 0)
-  {
-    return memcached_set_error(*ptr, MEMCACHED_MEMORY_ALLOCATION_FAILURE, MEMCACHED_AT, 
-                               memcached_literal_param("snprintf(MEMCACHED_DEFAULT_COMMAND_SIZE)"));
-  }
-
-  if (ptr->flags.use_udp and flush == false)
-  {
-    if (send_length > MAX_UDP_DATAGRAM_LENGTH - UDP_DATAGRAM_HEADER_LENGTH)
-    {
-      return MEMCACHED_WRITE_FAILURE;
-    }
-
-    if (send_length +instance->write_buffer_offset > MAX_UDP_DATAGRAM_LENGTH)
-    {
-      memcached_io_write(instance, NULL, 0, true);
-    }
-  }
+  libmemcached_io_vector_st vector[]=
+  {
+    { NULL, 0 },
+    { memcached_literal_param("delete ") },
+    { memcached_array_string(instance->root->_namespace), memcached_array_size(instance->root->_namespace) },
+    { key, key_length },
+    { " noreply", reply ? 0 : memcached_literal_param_size(" noreply") },
+    { memcached_literal_param("\r\n") }
+  };
 
-  return memcached_do(instance, buffer, (size_t)send_length, flush);
+  /* Send command header, only flush if we are NOT buffering */
+  return memcached_vdo(instance, vector, 6, is_buffering ? false : true);
 }
 
-static inline memcached_return_t binary_delete(memcached_st *ptr,
-                                               memcached_server_write_instance_st instance,
+static inline memcached_return_t binary_delete(memcached_instance_st* instance,
                                                uint32_t server_key,
                                                const char *key,
-                                               size_t key_length,
-                                               time_t expiration,
-                                               bool& reply,
-                                               bool& flush)
+                                               const size_t key_length,
+                                               const bool reply,
+                                               const bool is_buffering)
 {
   protocol_binary_request_delete request= {};
 
-  // No expiration is supported in the binary protocol
-  if (expiration)
-  {
-    return MEMCACHED_INVALID_ARGUMENTS;
-  }
+  bool should_flush= is_buffering ? false : true;
+
+  initialize_binary_request(instance, request.message.header);
 
-  request.message.header.request.magic= PROTOCOL_BINARY_REQ;
   if (reply)
   {
     request.message.header.request.opcode= PROTOCOL_BINARY_CMD_DELETE;
@@ -158,57 +86,36 @@ static inline memcached_return_t binary_delete(memcached_st *ptr,
   {
     request.message.header.request.opcode= PROTOCOL_BINARY_CMD_DELETEQ;
   }
-  request.message.header.request.keylen= htons((uint16_t)(key_length + memcached_array_size(ptr->_namespace)));
+  request.message.header.request.keylen= htons(uint16_t(key_length + memcached_array_size(instance->root->_namespace)));
   request.message.header.request.datatype= PROTOCOL_BINARY_RAW_BYTES;
-  request.message.header.request.bodylen= htonl((uint32_t)(key_length + memcached_array_size(ptr->_namespace)));
+  request.message.header.request.bodylen= htonl(uint32_t(key_length + memcached_array_size(instance->root->_namespace)));
 
-  if (ptr->flags.use_udp and flush == false)
+  libmemcached_io_vector_st vector[]=
   {
-    size_t cmd_size= sizeof(request.bytes) + key_length;
-    if (cmd_size > MAX_UDP_DATAGRAM_LENGTH - UDP_DATAGRAM_HEADER_LENGTH)
-    {
-      return MEMCACHED_WRITE_FAILURE;
-    }
-
-    if (cmd_size +instance->write_buffer_offset > MAX_UDP_DATAGRAM_LENGTH)
-    {
-      memcached_io_write(instance, NULL, 0, true);
-    }
-  }
-
-  struct libmemcached_io_vector_st vector[]=
-  {
-    { sizeof(request.bytes), request.bytes},
-    { memcached_array_size(ptr->_namespace), memcached_array_string(ptr->_namespace) },
-    { key_length, key },
+    { NULL, 0 },
+    { request.bytes, sizeof(request.bytes) },
+    { memcached_array_string(instance->root->_namespace), memcached_array_size(instance->root->_namespace) },
+    { key, key_length }
   };
 
-  memcached_return_t rc= MEMCACHED_SUCCESS;
-
-  if ((rc= memcached_vdo(instance, vector,  3, flush)) != MEMCACHED_SUCCESS)
-  {
-    memcached_io_reset(instance);
-  }
+  memcached_return_t rc= memcached_vdo(instance, vector,  4, should_flush);
 
-  if (ptr->number_of_replicas > 0)
+  if (memcached_has_replicas(instance))
   {
     request.message.header.request.opcode= PROTOCOL_BINARY_CMD_DELETEQ;
 
-    for (uint32_t x= 0; x < ptr->number_of_replicas; ++x)
+    for (uint32_t x= 0; x < memcached_has_replicas(instance); ++x)
     {
-      memcached_server_write_instance_st replica;
-
       ++server_key;
-      if (server_key == memcached_server_count(ptr))
-        server_key= 0;
-
-      replica= memcached_server_instance_fetch(ptr, server_key);
 
-      if (memcached_vdo(replica, vector, 3, flush) != MEMCACHED_SUCCESS)
+      if (server_key == memcached_server_count(instance->root))
       {
-        memcached_io_reset(replica);
+        server_key= 0;
       }
-      else
+
+      memcached_instance_st* replica= memcached_instance_fetch(instance->root, server_key);
+
+      if (memcached_success(memcached_vdo(replica, vector, 4, should_flush)))
       {
         memcached_server_response_decrement(replica);
       }
@@ -218,79 +125,85 @@ static inline memcached_return_t binary_delete(memcached_st *ptr,
   return rc;
 }
 
-memcached_return_t memcached_delete_by_key(memcached_st *ptr,
+memcached_return_t memcached_delete_by_key(memcached_st *shell,
                                            const char *group_key, size_t group_key_length,
                                            const char *key, size_t key_length,
                                            time_t expiration)
 {
+  Memcached* memc= memcached2Memcached(shell);
   LIBMEMCACHED_MEMCACHED_DELETE_START();
 
   memcached_return_t rc;
-  if (memcached_failed(rc= initialize_query(ptr)))
+  if (memcached_fatal(rc= initialize_query(memc, true)))
   {
     return rc;
   }
 
-  rc= memcached_validate_key_length(key_length, ptr->flags.binary_protocol);
-  if (memcached_failed(rc))
+  if (memcached_fatal(rc= memcached_key_test(*memc, (const char **)&key, &key_length, 1)))
   {
-    return rc;
+    return memcached_last_error(memc);
   }
+
+  if (expiration)
+  {
+    return memcached_set_error(*memc, MEMCACHED_INVALID_ARGUMENTS, MEMCACHED_AT, 
+                               memcached_literal_param("Memcached server version does not allow expiration of deleted items"));
+  }
+
+  uint32_t server_key= memcached_generate_hash_with_redistribution(memc, group_key, group_key_length);
+  memcached_instance_st* instance= memcached_instance_fetch(memc, server_key);
   
+  bool is_buffering= memcached_is_buffering(instance->root);
+  bool is_replying= memcached_is_replying(instance->root);
+
   // If a delete trigger exists, we need a response, so no buffering/noreply
-  if (ptr->delete_trigger)
+  if (memc->delete_trigger)
   {
-    if (ptr->flags.buffer_requests)
+    if (is_buffering)
     {
-      return memcached_set_error(*ptr, MEMCACHED_INVALID_ARGUMENTS, MEMCACHED_AT, 
+      return memcached_set_error(*memc, MEMCACHED_INVALID_ARGUMENTS, MEMCACHED_AT, 
                                  memcached_literal_param("Delete triggers cannot be used if buffering is enabled"));
     }
 
-    if (ptr->flags.no_reply)
+    if (is_replying == false)
     {
-      return memcached_set_error(*ptr, MEMCACHED_INVALID_ARGUMENTS, MEMCACHED_AT, 
+      return memcached_set_error(*memc, MEMCACHED_INVALID_ARGUMENTS, MEMCACHED_AT, 
                                  memcached_literal_param("Delete triggers cannot be used if MEMCACHED_BEHAVIOR_NOREPLY is set"));
     }
   }
 
-
-  uint32_t server_key= memcached_generate_hash_with_redistribution(ptr, group_key, group_key_length);
-  memcached_server_write_instance_st instance= memcached_server_instance_fetch(ptr, server_key);
-
-  bool to_write= (ptr->flags.buffer_requests) ? false : true;
-
-  // Invert the logic to make it simpler to read the code
-  bool reply= (ptr->flags.no_reply) ? false : true;
-
-  if (ptr->flags.binary_protocol)
+  if (memcached_is_binary(memc))
   {
-    rc= binary_delete(ptr, instance, server_key, key, key_length, expiration, reply, to_write);
+    rc= binary_delete(instance, server_key, key, key_length, is_replying, is_buffering);
   }
   else
   {
-    rc= ascii_delete(ptr, instance, server_key, key, key_length, expiration, reply, to_write);
+    rc= ascii_delete(instance, server_key, key, key_length, is_replying, is_buffering);
   }
 
   if (rc == MEMCACHED_SUCCESS)
   {
-    if (to_write == false)
+    if (is_buffering == true)
     {
       rc= MEMCACHED_BUFFERED;
     }
-    else if (reply)
+    else if (is_replying == false)
+    {
+      rc= MEMCACHED_SUCCESS;
+    }
+    else
     {
       char buffer[MEMCACHED_DEFAULT_COMMAND_SIZE];
       rc= memcached_response(instance, buffer, MEMCACHED_DEFAULT_COMMAND_SIZE, NULL);
       if (rc == MEMCACHED_DELETED)
       {
         rc= MEMCACHED_SUCCESS;
+        if (memc->delete_trigger)
+        {
+          memc->delete_trigger(memc, key, key_length);
+        }
       }
     }
-
-    if (rc == MEMCACHED_SUCCESS and ptr->delete_trigger)
-    {
-      ptr->delete_trigger(ptr, key, key_length);
-    }
   }
 
   LIBMEMCACHED_MEMCACHED_DELETE_END();