Fixes for Fedora 17.
[m6w6/libmemcached] / libmemcached / flush.cc
index 6a1364c27c31d6e12bc12d1690017e2e9c7d80fe..989555e2f968625f2a47600ead8099eeda26273a 100644 (file)
 #include <libmemcached/common.h>
 
 static memcached_return_t memcached_flush_binary(memcached_st *ptr, 
-                                                 time_t expiration);
-static memcached_return_t memcached_flush_textual(memcached_st *ptr, 
-                                                  time_t expiration);
-
-memcached_return_t memcached_flush(memcached_st *ptr, time_t expiration)
+                                                 time_t expiration,
+                                                 const bool reply)
 {
-  memcached_return_t rc;
-  if (memcached_failed(rc= initialize_query(ptr)))
-  {
-    return rc;
-  }
+  protocol_binary_request_flush request= {};
 
-  LIBMEMCACHED_MEMCACHED_FLUSH_START();
-  if (ptr->flags.binary_protocol)
-    rc= memcached_flush_binary(ptr, expiration);
-  else
-    rc= memcached_flush_textual(ptr, expiration);
-  LIBMEMCACHED_MEMCACHED_FLUSH_END();
-  return rc;
-}
+  request.message.header.request.magic= (uint8_t)PROTOCOL_BINARY_REQ;
+  request.message.header.request.opcode= PROTOCOL_BINARY_CMD_FLUSH;
+  request.message.header.request.extlen= 4;
+  request.message.header.request.datatype= PROTOCOL_BINARY_RAW_BYTES;
+  request.message.header.request.bodylen= htonl(request.message.header.request.extlen);
+  request.message.body.expiration= htonl((uint32_t) expiration);
 
-static memcached_return_t memcached_flush_textual(memcached_st *ptr, 
-                                                  time_t expiration)
-{
-  unlikely (memcached_server_count(ptr) == 0)
-    return MEMCACHED_NO_SERVERS;
+  memcached_return_t rc= MEMCACHED_SUCCESS;
 
-  for (unsigned int x= 0; x < memcached_server_count(ptr); x++)
+  for (uint32_t x= 0; x < memcached_server_count(ptr); x++)
   {
-    memcached_return_t rc;
-    char buffer[MEMCACHED_DEFAULT_COMMAND_SIZE];
-
-    bool no_reply= ptr->flags.no_reply;
-    memcached_server_write_instance_st instance=
-      memcached_server_instance_fetch(ptr, x);
+    memcached_server_write_instance_st instance= memcached_server_instance_fetch(ptr, x);
 
-    int send_length;
-    if (expiration)
+    if (reply)
     {
-      send_length= snprintf(buffer, MEMCACHED_DEFAULT_COMMAND_SIZE, 
-                            "flush_all %llu%s\r\n",
-                            (unsigned long long)expiration, no_reply ? " noreply" : "");
+      request.message.header.request.opcode= PROTOCOL_BINARY_CMD_FLUSH;
     }
     else
     {
-      send_length= snprintf(buffer, MEMCACHED_DEFAULT_COMMAND_SIZE, 
-                            "flush_all%s\r\n", no_reply ? " noreply" : "");
+      request.message.header.request.opcode= PROTOCOL_BINARY_CMD_FLUSHQ;
     }
 
-    if (send_length >= MEMCACHED_DEFAULT_COMMAND_SIZE || send_length < 0)
+    libmemcached_io_vector_st vector[]=
     {
-      return MEMCACHED_FAILURE;
-    }
+      { NULL, 0 },
+      { request.bytes, sizeof(request.bytes) }
+    };
 
-    rc= memcached_do(instance, buffer, (size_t)send_length, true);
+    memcached_return_t rrc;
+    if (memcached_failed(rrc= memcached_vdo(instance, vector, 2, true)))
+    {
+      if (instance->error_messages == NULL or instance->root->error_messages == NULL)
+      {
+        memcached_set_error(*instance, rrc, MEMCACHED_AT);
+      }
+      memcached_io_reset(instance);
+      rc= MEMCACHED_SOME_ERRORS;
+    } 
+  }
 
-    if (rc == MEMCACHED_SUCCESS && !no_reply)
-      (void)memcached_response(instance, buffer, MEMCACHED_DEFAULT_COMMAND_SIZE, NULL);
+  for (uint32_t x= 0; x < memcached_server_count(ptr); x++)
+  {
+    memcached_server_write_instance_st instance= memcached_server_instance_fetch(ptr, x);
+
+    if (memcached_server_response_count(instance) > 0)
+    {
+      (void)memcached_response(instance, NULL, 0, NULL);
+    }
   }
 
-  return MEMCACHED_SUCCESS;
+  return rc;
 }
 
-static memcached_return_t memcached_flush_binary(memcached_st *ptr, 
-                                                 time_t expiration)
+static memcached_return_t memcached_flush_textual(memcached_st *ptr, 
+                                                  time_t expiration,
+                                                  const bool reply)
 {
-  protocol_binary_request_flush request= {};
-
-  unlikely (memcached_server_count(ptr) == 0)
-    return MEMCACHED_NO_SERVERS;
+  char buffer[MEMCACHED_MAXIMUM_INTEGER_DISPLAY_LENGTH +1];
+  int send_length= 0;
+  if (expiration)
+  {
+    send_length= snprintf(buffer, sizeof(buffer), "%llu", (unsigned long long)expiration);
+  }
 
-  request.message.header.request.magic= (uint8_t)PROTOCOL_BINARY_REQ;
-  request.message.header.request.opcode= PROTOCOL_BINARY_CMD_FLUSH;
-  request.message.header.request.extlen= 4;
-  request.message.header.request.datatype= PROTOCOL_BINARY_RAW_BYTES;
-  request.message.header.request.bodylen= htonl(request.message.header.request.extlen);
-  request.message.body.expiration= htonl((uint32_t) expiration);
+  if (size_t(send_length) >= sizeof(buffer) or send_length < 0)
+  {
+    return memcached_set_error(*ptr, MEMCACHED_MEMORY_ALLOCATION_FAILURE, MEMCACHED_AT, 
+                               memcached_literal_param("snprintf(MEMCACHED_DEFAULT_COMMAND_SIZE)"));
+  }
 
+  memcached_return_t rc= MEMCACHED_SUCCESS;
   for (uint32_t x= 0; x < memcached_server_count(ptr); x++)
   {
-    memcached_server_write_instance_st instance=
-      memcached_server_instance_fetch(ptr, x);
+    memcached_server_write_instance_st instance= memcached_server_instance_fetch(ptr, x);
 
-    if (ptr->flags.no_reply)
+    libmemcached_io_vector_st vector[]=
     {
-      request.message.header.request.opcode= PROTOCOL_BINARY_CMD_FLUSHQ;
-    }
-    else
+      { NULL, 0 },
+      { memcached_literal_param("flush_all ") },
+      { buffer, size_t(send_length) },
+      { " noreply", reply ? 0 : memcached_literal_param_size(" noreply") },
+      { memcached_literal_param("\r\n") }
+    };
+
+    memcached_return_t rrc= memcached_vdo(instance, vector, 5, true);
+    if (memcached_success(rrc) and reply == true)
     {
-      request.message.header.request.opcode= PROTOCOL_BINARY_CMD_FLUSH;
+      char response_buffer[MEMCACHED_DEFAULT_COMMAND_SIZE];
+      rrc= memcached_response(instance, response_buffer, sizeof(response_buffer), NULL);
     }
 
-    if (memcached_do(instance, request.bytes, sizeof(request.bytes), true) != MEMCACHED_SUCCESS) 
+    if (memcached_failed(rrc))
     {
-      memcached_io_reset(instance);
-      return MEMCACHED_WRITE_FAILURE;
-    } 
+      // If an error has already been reported, then don't add to it
+      if (instance->error_messages == NULL or instance->root->error_messages == NULL)
+      {
+        memcached_set_error(*instance, rrc, MEMCACHED_AT);
+      }
+      rc= MEMCACHED_SOME_ERRORS;
+    }
   }
 
-  for (uint32_t x= 0; x < memcached_server_count(ptr); x++)
+  return rc;
+}
+
+memcached_return_t memcached_flush(memcached_st *ptr, time_t expiration)
+{
+  memcached_return_t rc;
+  if (memcached_failed(rc= initialize_query(ptr, true)))
   {
-    memcached_server_write_instance_st instance=
-      memcached_server_instance_fetch(ptr, x);
+    return rc;
+  }
 
-    if (memcached_server_response_count(instance) > 0)
-      (void)memcached_response(instance, NULL, 0, NULL);
+  bool reply= memcached_is_replying(ptr);
+
+  LIBMEMCACHED_MEMCACHED_FLUSH_START();
+  if (memcached_is_binary(ptr))
+  {
+    rc= memcached_flush_binary(ptr, expiration, reply);
   }
+  else
+  {
+    rc= memcached_flush_textual(ptr, expiration, reply);
+  }
+  LIBMEMCACHED_MEMCACHED_FLUSH_END();
 
-  return MEMCACHED_SUCCESS;
+  return rc;
 }