attempt to fix #12, #49 and #65
[awesomized/libmemcached] / libmemcached / delete.cc
index 7ef05b07aef8f756945bc3e179e6aa1e52c1d8f2..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 binary_delete(memcached_st *ptr,
-                                               uint32_t server_key,
-                                               const char *key,
-                                               size_t key_length,
-                                               bool flush);
-
-memcached_return_t memcached_delete_by_key(memcached_st *ptr,
-                                           const char *group_key, size_t group_key_length,
-                                           const char *key, size_t key_length,
-                                           time_t expiration)
+static inline memcached_return_t ascii_delete(memcached_instance_st* instance,
+                                              uint32_t ,
+                                              const char *key,
+                                              const size_t key_length,
+                                              const bool reply,
+                                              const bool is_buffering)
 {
-  bool to_write;
-  char buffer[MEMCACHED_DEFAULT_COMMAND_SIZE];
-  memcached_server_write_instance_st instance;
-
-  LIBMEMCACHED_MEMCACHED_DELETE_START();
-
-  memcached_return_t rc;
-  if (memcached_failed(rc= initialize_query(ptr)))
+  libmemcached_io_vector_st vector[]=
   {
-    return rc;
-  }
-
-  rc= memcached_validate_key_length(key_length,
-                                    ptr->flags.binary_protocol);
-
-  unlikely (memcached_failed(rc))
-    return rc;
+    { 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") }
+  };
 
-  unlikely (memcached_server_count(ptr) == 0)
-    return MEMCACHED_NO_SERVERS;
+  /* Send command header, only flush if we are NOT buffering */
+  return memcached_vdo(instance, vector, 6, is_buffering ? false : true);
+}
 
-  uint32_t server_key= memcached_generate_hash_with_redistribution(ptr, group_key, group_key_length);
-  instance= memcached_server_instance_fetch(ptr, server_key);
+static inline memcached_return_t binary_delete(memcached_instance_st* instance,
+                                               uint32_t server_key,
+                                               const char *key,
+                                               const size_t key_length,
+                                               const bool reply,
+                                               const bool is_buffering)
+{
+  protocol_binary_request_delete request= {};
 
-  to_write= (ptr->flags.buffer_requests) ? false : true;
+  bool should_flush= is_buffering ? false : true;
 
-  bool no_reply= (ptr->flags.no_reply);
+  initialize_binary_request(instance, request.message.header);
 
-  if (ptr->flags.binary_protocol)
+  if (reply)
   {
-    likely (! expiration)
-    {
-      rc= binary_delete(ptr, server_key, key, key_length, to_write);
-    }
-    else
-    {
-      rc= MEMCACHED_INVALID_ARGUMENTS;
-    }
+    request.message.header.request.opcode= PROTOCOL_BINARY_CMD_DELETE;
   }
   else
   {
-    int send_length;
+    request.message.header.request.opcode= PROTOCOL_BINARY_CMD_DELETEQ;
+  }
+  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(instance->root->_namespace)));
 
-    unlikely (expiration)
-    {
-       if ((instance->major_version == 1 &&
-            instance->minor_version > 2) ||
-           instance->major_version > 1)
-       {
-         rc= MEMCACHED_INVALID_ARGUMENTS;
-         goto error;
-       }
-       else
-       {
-          /* ensure that we are connected, otherwise we might bump the
-           * command counter before connection */
-          if ((rc= memcached_connect(instance)) != MEMCACHED_SUCCESS)
-          {
-            WATCHPOINT_ERROR(rc);
-            return rc;
-          }
+  libmemcached_io_vector_st vector[]=
+  {
+    { NULL, 0 },
+    { request.bytes, sizeof(request.bytes) },
+    { memcached_array_string(instance->root->_namespace), memcached_array_size(instance->root->_namespace) },
+    { key, key_length }
+  };
 
-          if (instance->minor_version == 0)
-          {
-             if (no_reply || ! to_write)
-             {
-                /* We might get out of sync with the server if we
-                 * send this command to a server newer than 1.2.x..
-                 * disable no_reply and buffered mode.
-                 */
-                to_write= true;
-                if (no_reply)
-                   memcached_server_response_increment(instance);
-                no_reply= false;
-             }
-          }
-          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,
-                                no_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, no_reply ? " noreply" :"");
-    }
+  memcached_return_t rc= memcached_vdo(instance, vector,  4, should_flush);
 
-    if (send_length >= MEMCACHED_DEFAULT_COMMAND_SIZE || send_length < 0)
-    {
-      rc= MEMCACHED_WRITE_FAILURE;
-      goto error;
-    }
+  if (memcached_has_replicas(instance))
+  {
+    request.message.header.request.opcode= PROTOCOL_BINARY_CMD_DELETEQ;
 
-    if (ptr->flags.use_udp && ! to_write)
+    for (uint32_t x= 0; x < memcached_has_replicas(instance); ++x)
     {
-      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);
-    }
+      ++server_key;
 
-    rc= memcached_do(instance, buffer, (size_t)send_length, to_write);
-  }
+      if (server_key == memcached_server_count(instance->root))
+      {
+        server_key= 0;
+      }
 
-  if (rc != MEMCACHED_SUCCESS)
-    goto error;
+      memcached_instance_st* replica= memcached_instance_fetch(instance->root, server_key);
 
-  if (! to_write)
-  {
-    rc= MEMCACHED_BUFFERED;
-  }
-  else if (!no_reply)
-  {
-    rc= memcached_response(instance, buffer, MEMCACHED_DEFAULT_COMMAND_SIZE, NULL);
-    if (rc == MEMCACHED_DELETED)
-      rc= MEMCACHED_SUCCESS;
+      if (memcached_success(memcached_vdo(replica, vector, 4, should_flush)))
+      {
+        memcached_server_response_decrement(replica);
+      }
+    }
   }
 
-  if (rc == MEMCACHED_SUCCESS && ptr->delete_trigger)
-    ptr->delete_trigger(ptr, key, key_length);
-
-error:
-  LIBMEMCACHED_MEMCACHED_DELETE_END();
   return rc;
 }
 
-static inline memcached_return_t binary_delete(memcached_st *ptr,
-                                               uint32_t server_key,
-                                               const char *key,
-                                               size_t key_length,
-                                               bool flush)
+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_server_write_instance_st instance;
-  protocol_binary_request_delete request= {};
-
-  instance= memcached_server_instance_fetch(ptr, server_key);
+  Memcached* memc= memcached2Memcached(shell);
+  LIBMEMCACHED_MEMCACHED_DELETE_START();
 
-  request.message.header.request.magic= PROTOCOL_BINARY_REQ;
-  if (ptr->flags.no_reply)
+  memcached_return_t rc;
+  if (memcached_fatal(rc= initialize_query(memc, true)))
   {
-    request.message.header.request.opcode= PROTOCOL_BINARY_CMD_DELETEQ;
+    return rc;
   }
-  else
+
+  if (memcached_fatal(rc= memcached_key_test(*memc, (const char **)&key, &key_length, 1)))
   {
-    request.message.header.request.opcode= PROTOCOL_BINARY_CMD_DELETE;
+    return memcached_last_error(memc);
   }
-  request.message.header.request.keylen= htons((uint16_t)(key_length + memcached_array_size(ptr->_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)));
 
-  if (ptr->flags.use_udp && ! flush)
+  if (expiration)
   {
-    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);
+    return memcached_set_error(*memc, MEMCACHED_INVALID_ARGUMENTS, MEMCACHED_AT, 
+                               memcached_literal_param("Memcached server version does not allow expiration of deleted items"));
   }
 
-  struct libmemcached_io_vector_st vector[]=
+  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 (memc->delete_trigger)
   {
-    { sizeof(request.bytes), request.bytes},
-    { memcached_array_size(ptr->_namespace), memcached_array_string(ptr->_namespace) },
-    { key_length, key },
-  };
+    if (is_buffering)
+    {
+      return memcached_set_error(*memc, MEMCACHED_INVALID_ARGUMENTS, MEMCACHED_AT, 
+                                 memcached_literal_param("Delete triggers cannot be used if buffering is enabled"));
+    }
 
-  memcached_return_t rc= MEMCACHED_SUCCESS;
+    if (is_replying == false)
+    {
+      return memcached_set_error(*memc, MEMCACHED_INVALID_ARGUMENTS, MEMCACHED_AT, 
+                                 memcached_literal_param("Delete triggers cannot be used if MEMCACHED_BEHAVIOR_NOREPLY is set"));
+    }
+  }
 
-  if ((rc= memcached_vdo(instance, vector,  3, flush)) != MEMCACHED_SUCCESS)
+  if (memcached_is_binary(memc))
   {
-    memcached_io_reset(instance);
-    rc= (rc == MEMCACHED_SUCCESS) ? MEMCACHED_WRITE_FAILURE : rc;
+    rc= binary_delete(instance, server_key, key, key_length, is_replying, is_buffering);
   }
-
-  unlikely (ptr->number_of_replicas > 0)
+  else
   {
-    request.message.header.request.opcode= PROTOCOL_BINARY_CMD_DELETEQ;
+    rc= ascii_delete(instance, server_key, key, key_length, is_replying, is_buffering);
+  }
 
-    for (uint32_t x= 0; x < ptr->number_of_replicas; ++x)
+  if (rc == MEMCACHED_SUCCESS)
+  {
+    if (is_buffering == true)
     {
-      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)
-      {
-        memcached_io_reset(replica);
-      }
-      else
+      rc= MEMCACHED_BUFFERED;
+    }
+    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)
       {
-        memcached_server_response_decrement(replica);
+        rc= MEMCACHED_SUCCESS;
+        if (memc->delete_trigger)
+        {
+          memc->delete_trigger(memc, key, key_length);
+        }
       }
     }
   }
 
+  LIBMEMCACHED_MEMCACHED_DELETE_END();
   return rc;
 }