attempt to fix #12, #49 and #65
[awesomized/libmemcached] / libmemcached / stats.cc
index 01a8baf6d51303799a08c3c17e656a3b72df2c26..9e6083e9d841a9f0d6f7425d9ddff3e827e1f33e 100644 (file)
@@ -346,7 +346,7 @@ static memcached_return_t set_data(memcached_stat_st *memc_stat, const char *key
   return MEMCACHED_SUCCESS;
 }
 
-char *memcached_stat_get_value(const memcached_st *, memcached_stat_st *memc_stat,
+char *memcached_stat_get_value(const memcached_st* shell, memcached_stat_st *memc_stat,
                                const char *key, memcached_return_t *error)
 {
   memcached_return_t not_used;
@@ -456,13 +456,15 @@ char *memcached_stat_get_value(const memcached_st *, memcached_stat_st *memc_sta
   }
   else
   {
-    *error= MEMCACHED_NOTFOUND;
+    Memcached* memc= (Memcached*)memcached2Memcached(shell);
+    *error= memcached_set_error(*memc, MEMCACHED_INVALID_ARGUMENTS, MEMCACHED_AT, memcached_literal_param("Invalid key provided"));
     return NULL;
   }
 
   if (length >= SMALL_STRING_LEN || length < 0)
   {
-    *error= MEMCACHED_FAILURE;
+    Memcached* memc= (Memcached*)memcached2Memcached(shell);
+    *error= memcached_set_error(*memc, MEMCACHED_FAILURE, MEMCACHED_AT, memcached_literal_param("Internal failure occured with buffer, please report this bug."));
     return NULL;
   }
 
@@ -477,11 +479,12 @@ char *memcached_stat_get_value(const memcached_st *, memcached_stat_st *memc_sta
 static memcached_return_t binary_stats_fetch(memcached_stat_st *memc_stat,
                                              const char *args,
                                              const size_t args_length,
-                                             org::libmemcached::Instance* instance,
+                                             memcached_instance_st* instance,
                                              struct local_context *check)
 {
   char buffer[MEMCACHED_DEFAULT_COMMAND_SIZE];
   protocol_binary_request_stats request= {}; // = {.bytes= {0}};
+  memcached_return_t rc;
 
   initialize_binary_request(instance, request.message.header);
 
@@ -499,10 +502,9 @@ static memcached_return_t binary_stats_fetch(memcached_stat_st *memc_stat,
       { args, args_length }
     };
 
-    if (memcached_vdo(instance, vector, 2, true) != MEMCACHED_SUCCESS)
+    if (memcached_failed(rc = memcached_vdo(instance, vector, 2, true)))
     {
-      memcached_io_reset(instance);
-      return MEMCACHED_WRITE_FAILURE;
+      return rc;
     }
   }
   else
@@ -512,17 +514,16 @@ static memcached_return_t binary_stats_fetch(memcached_stat_st *memc_stat,
       { request.bytes, sizeof(request.bytes) }
     };
 
-    if (memcached_vdo(instance, vector, 1, true) != MEMCACHED_SUCCESS)
+    if (memcached_failed(rc = memcached_vdo(instance, vector, 1, true)))
     {
-      memcached_io_reset(instance);
-      return MEMCACHED_WRITE_FAILURE;
+      return rc;
     }
   }
 
   memcached_server_response_decrement(instance);
   while (1)
   {
-    memcached_return_t rc= memcached_response(instance, buffer, sizeof(buffer), NULL);
+    rc= memcached_response(instance, buffer, sizeof(buffer), NULL);
 
     if (rc == MEMCACHED_END)
     {
@@ -531,7 +532,6 @@ static memcached_return_t binary_stats_fetch(memcached_stat_st *memc_stat,
 
     if (rc != MEMCACHED_SUCCESS)
     {
-      memcached_io_reset(instance);
       return rc;
     }
 
@@ -567,7 +567,7 @@ static memcached_return_t binary_stats_fetch(memcached_stat_st *memc_stat,
 static memcached_return_t ascii_stats_fetch(memcached_stat_st *memc_stat,
                                             const char *args,
                                             const size_t args_length,
-                                            org::libmemcached::Instance* instance,
+                                            memcached_instance_st* instance,
                                             struct local_context *check)
 {
   libmemcached_io_vector_st vector[]=
@@ -658,8 +658,7 @@ memcached_stat_st *memcached_stat(memcached_st *shell, char *args, memcached_ret
   if (args)
   {
     args_length= strlen(args);
-    rc= memcached_validate_key_length(args_length, self->flags.binary_protocol);
-    if (memcached_failed(rc))
+    if (memcached_failed(rc= memcached_key_test(*self, (const char **)&args, &args_length, 1)))
     {
       *error= memcached_set_error(*self, rc, MEMCACHED_AT);
       return NULL;
@@ -684,7 +683,7 @@ memcached_stat_st *memcached_stat(memcached_st *shell, char *args, memcached_ret
     stat_instance->pid= -1;
     stat_instance->root= self;
 
-    org::libmemcached::Instance* instance= memcached_instance_fetch(self, x);
+    memcached_instance_st* instance= memcached_instance_fetch(self, x);
 
     memcached_return_t temp_return;
     if (memcached_is_binary(self))
@@ -746,12 +745,12 @@ memcached_return_t memcached_stat_servername(memcached_stat_st *memc_stat, char
     if (args)
     {
       args_length= strlen(args);
-      rc= memcached_validate_key_length(args_length, memc.flags.binary_protocol);
+      rc= memcached_key_test(*memc_ptr, (const char **)&args, &args_length, 1);
     }
 
     if (memcached_success(rc))
     {
-      org::libmemcached::Instance* instance= memcached_instance_fetch(memc_ptr, 0);
+      memcached_instance_st* instance= memcached_instance_fetch(memc_ptr, 0);
       if (memc.flags.binary_protocol)
       {
         rc= binary_stats_fetch(memc_stat, args, args_length, instance, NULL);
@@ -813,7 +812,7 @@ void memcached_stat_free(const memcached_st *, memcached_stat_st *memc_stat)
 }
 
 static memcached_return_t call_stat_fn(memcached_st *memc,
-                                       org::libmemcached::Instance* instance,
+                                       memcached_instance_st* instance,
                                        void *context)
 {
   if (memc)