attempt to fix #12, #49 and #65
[awesomized/libmemcached] / libmemcached / exist.cc
index d2dc34dd6a1126f7e8afc463405d82a0da698525..5cb99e60661ee68210db150e39f09901649cfd38 100644 (file)
@@ -36,7 +36,7 @@
 
 #include <libmemcached/common.h>
 
-static memcached_return_t ascii_exist(memcached_st *memc, memcached_server_write_instance_st instance, const char *key, size_t key_length)
+static memcached_return_t ascii_exist(Memcached *memc, memcached_instance_st* instance, const char *key, size_t key_length)
 {
   libmemcached_io_vector_st vector[]=
   {
@@ -52,37 +52,35 @@ static memcached_return_t ascii_exist(memcached_st *memc, memcached_server_write
   };
 
   /* Send command header */
-  memcached_return_t rc=  memcached_vdo(instance, vector, 9, true);
-  if (rc == MEMCACHED_SUCCESS)
+  memcached_return_t rc;
+  if (memcached_fatal(rc= memcached_vdo(instance, vector, 9, true)))
   {
-    char buffer[MEMCACHED_DEFAULT_COMMAND_SIZE];
-    rc= memcached_response(instance, buffer, MEMCACHED_DEFAULT_COMMAND_SIZE, NULL);
-
-    if (rc == MEMCACHED_NOTSTORED)
-    {
-      rc= MEMCACHED_SUCCESS;
-    }
-
-    if (rc == MEMCACHED_STORED)
-    {
-      rc= MEMCACHED_NOTFOUND;
-    }
+    return rc;
   }
 
-  if (rc == MEMCACHED_WRITE_FAILURE)
+  char buffer[MEMCACHED_DEFAULT_COMMAND_SIZE];
+  rc= memcached_response(instance, buffer, MEMCACHED_DEFAULT_COMMAND_SIZE, NULL);
+
+  if (rc == MEMCACHED_NOTSTORED)
   {
-    memcached_io_reset(instance);
+    rc= MEMCACHED_SUCCESS;
+  }
+
+  if (rc == MEMCACHED_STORED)
+  {
+    rc= MEMCACHED_NOTFOUND;
   }
 
   return rc;
 }
 
-static memcached_return_t binary_exist(memcached_st *memc, memcached_server_write_instance_st instance, const char *key, size_t key_length)
+static memcached_return_t binary_exist(Memcached *memc, memcached_instance_st* instance, const char *key, size_t key_length)
 {
   protocol_binary_request_set request= {};
   size_t send_length= sizeof(request.bytes);
 
-  request.message.header.request.magic= PROTOCOL_BINARY_REQ;
+  initialize_binary_request(instance, request.message.header);
+
   request.message.header.request.opcode= PROTOCOL_BINARY_CMD_ADD;
   request.message.header.request.keylen= htons((uint16_t)(key_length + memcached_array_size(memc->_namespace)));
   request.message.header.request.datatype= PROTOCOL_BINARY_RAW_BYTES;
@@ -104,19 +102,22 @@ static memcached_return_t binary_exist(memcached_st *memc, memcached_server_writ
 
   /* write the header */
   memcached_return_t rc;
-  if ((rc= memcached_vdo(instance, vector, 4, true)) != MEMCACHED_SUCCESS)
+  if (memcached_fatal(rc= memcached_vdo(instance, vector, 4, true)))
   {
-    memcached_io_reset(instance);
-    return (rc == MEMCACHED_SUCCESS) ? MEMCACHED_WRITE_FAILURE : rc;
+    return rc;
   }
 
   rc= memcached_response(instance, NULL, 0, NULL);
 
   if (rc == MEMCACHED_SUCCESS)
+  {
     rc= MEMCACHED_NOTFOUND;
+  }
 
   if (rc == MEMCACHED_DATA_EXISTS)
+  {
     rc= MEMCACHED_SUCCESS;
+  }
 
   return rc;
 }
@@ -126,10 +127,11 @@ memcached_return_t memcached_exist(memcached_st *memc, const char *key, size_t k
   return memcached_exist_by_key(memc, key, key_length, key, key_length);
 }
 
-memcached_return_t memcached_exist_by_key(memcached_st *memc,
+memcached_return_t memcached_exist_by_key(memcached_st *shell,
                                           const char *group_key, size_t group_key_length,
                                           const char *key, size_t key_length)
 {
+  Memcached* memc= memcached2Memcached(shell);
   memcached_return_t rc;
   if (memcached_failed(rc= initialize_query(memc, true)))
   {
@@ -138,19 +140,20 @@ memcached_return_t memcached_exist_by_key(memcached_st *memc,
 
   if (memcached_is_udp(memc))
   {
-    return MEMCACHED_NOT_SUPPORTED;
+    return memcached_set_error(*memc, MEMCACHED_NOT_SUPPORTED, MEMCACHED_AT);
   }
 
   uint32_t server_key= memcached_generate_hash_with_redistribution(memc, group_key, group_key_length);
-  memcached_server_write_instance_st instance;
-  instance= memcached_server_instance_fetch(memc, server_key);
+  memcached_instance_st* instance= memcached_instance_fetch(memc, server_key);
 
-  if (memc->flags.binary_protocol)
+  if (memcached_is_binary(memc))
   {
-    return binary_exist(memc, instance, key, key_length);
+    rc= binary_exist(memc, instance, key, key_length);
   }
   else
   {
-    return ascii_exist(memc, instance, key, key_length);
+    rc= ascii_exist(memc, instance, key, key_length);
   }
+
+  return rc;
 }