Don't use __attribute__((unused))
[m6w6/libmemcached] / libmemcached / stats.c
index 6b09e604ec0d969f6bffc18759d4b74f77730273..93012a8daa92725fcc8291af7b2df8f59575949e 100644 (file)
@@ -29,6 +29,13 @@ static const char *memcached_stat_keys[] = {
   NULL
 };
 
+struct local_context
+{
+  memcached_stat_fn func;
+  void *context;
+  const char *args;
+};
+
 
 static memcached_return_t set_data(memcached_stat_st *memc_stat, char *key, char *value)
 {
@@ -151,16 +158,18 @@ static memcached_return_t set_data(memcached_stat_st *memc_stat, char *key, char
              strcmp("listen_disabled_num", key) == 0 ||
              strcmp("conn_yields", key) == 0 ||
              strcmp("auth_cmds", key) == 0 ||
-             strcmp("auth_errors", key) == 0))
+             strcmp("auth_errors", key) == 0 ||
+             strcmp("reclaimed", key) == 0))
   {
     WATCHPOINT_STRING(key);
-    return MEMCACHED_UNKNOWN_STAT_KEY;
+    /* return MEMCACHED_UNKNOWN_STAT_KEY; */
+    return MEMCACHED_SUCCESS;
   }
 
   return MEMCACHED_SUCCESS;
 }
 
-char *memcached_stat_get_value(memcached_st *ptr, memcached_stat_st *memc_stat,
+char *memcached_stat_get_value(const memcached_st *ptr, memcached_stat_st *memc_stat,
                                const char *key, memcached_return_t *error)
 {
   char buffer[SMALL_STRING_LEN];
@@ -219,17 +228,23 @@ char *memcached_stat_get_value(memcached_st *ptr, memcached_stat_st *memc_stat,
     return NULL;
   }
 
-  ret= ptr->call_malloc(ptr, (size_t) (length + 1));
+  if (length >= SMALL_STRING_LEN || length < 0)
+  {
+    *error= MEMCACHED_FAILURE;
+    return NULL;
+  }
+
+  ret= libmemcached_malloc(ptr, (size_t) (length + 1));
   memcpy(ret, buffer, (size_t) length);
   ret[length]= '\0';
 
   return ret;
 }
 
-static memcached_return_t binary_stats_fetch(memcached_st *ptr,
-                                             memcached_stat_st *memc_stat,
-                                             char *args,
-                                             unsigned int server_key)
+static memcached_return_t binary_stats_fetch(memcached_stat_st *memc_stat,
+                                             const char *args,
+                                             memcached_server_write_instance_st instance,
+                                             struct local_context *check)
 {
   memcached_return_t rc;
 
@@ -250,61 +265,78 @@ static memcached_return_t binary_stats_fetch(memcached_st *ptr,
     request.message.header.request.keylen= htons((uint16_t)len);
     request.message.header.request.bodylen= htonl((uint32_t) len);
 
-    if ((memcached_do(&ptr->hosts[server_key], request.bytes,
-                      sizeof(request.bytes), 0) != MEMCACHED_SUCCESS) ||
-        (memcached_io_write(&ptr->hosts[server_key], args, len, 1) == -1))
+    struct libmemcached_io_vector_st vector[]=
+    {
+      { .length= sizeof(request.bytes), .buffer= request.bytes },
+      { .length= len, .buffer= args }
+    };
+
+    if (memcached_vdo(instance, vector, 2, true) != MEMCACHED_SUCCESS)
     {
-      memcached_io_reset(&ptr->hosts[server_key]);
+      memcached_io_reset(instance);
       return MEMCACHED_WRITE_FAILURE;
     }
   }
   else
   {
-    if (memcached_do(&ptr->hosts[server_key], request.bytes,
-                     sizeof(request.bytes), 1) != MEMCACHED_SUCCESS)
+    if (memcached_do(instance, request.bytes,
+                     sizeof(request.bytes), true) != MEMCACHED_SUCCESS)
     {
-      memcached_io_reset(&ptr->hosts[server_key]);
+      memcached_io_reset(instance);
       return MEMCACHED_WRITE_FAILURE;
     }
   }
 
-  memcached_server_response_decrement(&ptr->hosts[server_key]);
+  memcached_server_response_decrement(instance);
   do
   {
-    rc= memcached_response(&ptr->hosts[server_key], buffer,
-                           sizeof(buffer), NULL);
+    rc= memcached_response(instance, buffer, sizeof(buffer), NULL);
+
     if (rc == MEMCACHED_END)
       break;
 
     unlikely (rc != MEMCACHED_SUCCESS)
     {
-      memcached_io_reset(&ptr->hosts[server_key]);
+      memcached_io_reset(instance);
       return rc;
     }
 
-    unlikely((set_data(memc_stat, buffer, buffer + strlen(buffer) + 1)) == MEMCACHED_UNKNOWN_STAT_KEY)
+    if (memc_stat)
     {
-      WATCHPOINT_ERROR(MEMCACHED_UNKNOWN_STAT_KEY);
-      WATCHPOINT_ASSERT(0);
+      unlikely((set_data(memc_stat, buffer, buffer + strlen(buffer) + 1)) == MEMCACHED_UNKNOWN_STAT_KEY)
+      {
+        WATCHPOINT_ERROR(MEMCACHED_UNKNOWN_STAT_KEY);
+        WATCHPOINT_ASSERT(0);
+      }
+    }
+
+    if (check && check->func)
+    {
+      size_t key_length= strlen(buffer);
+
+      check->func(instance,
+                  buffer, key_length,
+                  buffer+key_length+1, strlen(buffer+key_length+1),
+                  check->context);
     }
   } while (1);
 
   /* shit... memcached_response will decrement the counter, so I need to
    ** reset it.. todo: look at this and try to find a better solution.
  */
-  ptr->hosts[server_key].cursor_active= 0;
+  instance->cursor_active= 0;
 
   return MEMCACHED_SUCCESS;
 }
 
-static memcached_return_t ascii_stats_fetch(memcached_st *ptr,
-                                            memcached_stat_st *memc_stat,
-                                            char *args,
-                                            unsigned int server_key)
+static memcached_return_t ascii_stats_fetch(memcached_stat_st *memc_stat,
+                                            const char *args,
+                                            memcached_server_write_instance_st instance,
+                                            struct local_context *check)
 {
   memcached_return_t rc;
   char buffer[MEMCACHED_DEFAULT_COMMAND_SIZE];
-  size_t send_length;
+  int send_length;
 
   if (args)
     send_length= (size_t) snprintf(buffer, MEMCACHED_DEFAULT_COMMAND_SIZE,
@@ -313,16 +345,16 @@ static memcached_return_t ascii_stats_fetch(memcached_st *ptr,
     send_length= (size_t) snprintf(buffer, MEMCACHED_DEFAULT_COMMAND_SIZE,
                                    "stats\r\n");
 
-  if (send_length >= MEMCACHED_DEFAULT_COMMAND_SIZE)
+  if (send_length >= MEMCACHED_DEFAULT_COMMAND_SIZE || send_length < 0)
     return MEMCACHED_WRITE_FAILURE;
 
-  rc= memcached_do(&ptr->hosts[server_key], buffer, send_length, 1);
+  rc= memcached_do(instance, buffer, (size_t)send_length, true);
   if (rc != MEMCACHED_SUCCESS)
     goto error;
 
   while (1)
   {
-    rc= memcached_response(&ptr->hosts[server_key], buffer, MEMCACHED_DEFAULT_COMMAND_SIZE, NULL);
+    rc= memcached_response(instance, buffer, MEMCACHED_DEFAULT_COMMAND_SIZE, NULL);
 
     if (rc == MEMCACHED_STAT)
     {
@@ -340,10 +372,21 @@ static memcached_return_t ascii_stats_fetch(memcached_st *ptr,
       value= string_ptr;
       value[(size_t)(end_ptr-string_ptr)]= 0;
       string_ptr= end_ptr + 2;
-      unlikely((set_data(memc_stat, key, value)) == MEMCACHED_UNKNOWN_STAT_KEY)
+      if (memc_stat)
       {
-        WATCHPOINT_ERROR(MEMCACHED_UNKNOWN_STAT_KEY);
-        WATCHPOINT_ASSERT(0);
+        unlikely((set_data(memc_stat, key, value)) == MEMCACHED_UNKNOWN_STAT_KEY)
+        {
+          WATCHPOINT_ERROR(MEMCACHED_UNKNOWN_STAT_KEY);
+          WATCHPOINT_ASSERT(0);
+        }
+      }
+
+      if (check && check->func)
+      {
+        check->func(instance,
+                    key, strlen(key),
+                    value, strlen(value),
+                    check->context);
       }
     }
     else
@@ -359,7 +402,6 @@ error:
 
 memcached_stat_st *memcached_stat(memcached_st *ptr, char *args, memcached_return_t *error)
 {
-  unsigned int x;
   memcached_return_t rc;
   memcached_stat_st *stats;
 
@@ -369,23 +411,35 @@ memcached_stat_st *memcached_stat(memcached_st *ptr, char *args, memcached_retur
     return NULL;
   }
 
-  stats= ptr->call_calloc(ptr, ptr->number_of_hosts, sizeof(memcached_stat_st));
+  stats= libmemcached_calloc(ptr, memcached_server_count(ptr), sizeof(memcached_stat_st));
 
-  if (!stats)
+  if (! stats)
   {
     *error= MEMCACHED_MEMORY_ALLOCATION_FAILURE;
     return NULL;
   }
 
   rc= MEMCACHED_SUCCESS;
-  for (x= 0; x < ptr->number_of_hosts; x++)
+  for (uint32_t x= 0; x < memcached_server_count(ptr); x++)
   {
     memcached_return_t temp_return;
+    memcached_server_write_instance_st instance;
+    memcached_stat_st *stat_instance;
+
+    stat_instance= stats + x;
+
+    stat_instance->root= ptr;
+
+    instance= memcached_server_instance_fetch(ptr, x);
 
     if (ptr->flags.binary_protocol)
-      temp_return= binary_stats_fetch(ptr, stats + x, args, x);
+    {
+      temp_return= binary_stats_fetch(stat_instance, args, instance, NULL);
+    }
     else
-      temp_return= ascii_stats_fetch(ptr, stats + x, args, x);
+    {
+      temp_return= ascii_stats_fetch(stat_instance, args, instance, NULL);
+    }
 
     if (temp_return != MEMCACHED_SUCCESS)
       rc= MEMCACHED_SOME_ERRORS;
@@ -401,16 +455,25 @@ memcached_return_t memcached_stat_servername(memcached_stat_st *memc_stat, char
   memcached_return_t rc;
   memcached_st memc;
   memcached_st *memc_ptr;
+  memcached_server_write_instance_st instance;
+
+  memset(memc_stat, 0, sizeof(memcached_stat_st));
 
   memc_ptr= memcached_create(&memc);
   WATCHPOINT_ASSERT(memc_ptr);
 
   memcached_server_add(&memc, hostname, port);
 
+  instance= memcached_server_instance_fetch(memc_ptr, 0);
+
   if (memc.flags.binary_protocol)
-    rc= binary_stats_fetch(&memc, memc_stat, args, 0);
+  {
+    rc= binary_stats_fetch(memc_stat, args, instance, NULL);
+  }
   else
-    rc= ascii_stats_fetch(&memc, memc_stat, args, 0);
+  {
+    rc= ascii_stats_fetch(memc_stat, args, instance, NULL);
+  }
 
   memcached_free(&memc);
 
@@ -421,16 +484,18 @@ memcached_return_t memcached_stat_servername(memcached_stat_st *memc_stat, char
   We make a copy of the keys since at some point in the not so distant future
   we will add support for "found" keys.
 */
-char ** memcached_stat_get_keys(memcached_st *ptr, memcached_stat_st *memc_stat,
+char ** memcached_stat_get_keys(const memcached_st *ptr,
+                                memcached_stat_st *memc_stat,
                                 memcached_return_t *error)
 {
-  (void) memc_stat;
   char **list;
   size_t length= sizeof(memcached_stat_keys);
 
-  list= ptr->call_malloc(ptr, length);
+  (void)memc_stat;
+
+  list= libmemcached_malloc(ptr, length);
 
-  if (!list)
+  if (! list)
   {
     *error= MEMCACHED_MEMORY_ALLOCATION_FAILURE;
     return NULL;
@@ -443,7 +508,7 @@ char ** memcached_stat_get_keys(memcached_st *ptr, memcached_stat_st *memc_stat,
   return list;
 }
 
-void memcached_stat_free(memcached_st *ptr, memcached_stat_st *memc_stat)
+void memcached_stat_free(const memcached_st *ptr, memcached_stat_st *memc_stat)
 {
   if (memc_stat == NULL)
   {
@@ -451,8 +516,44 @@ void memcached_stat_free(memcached_st *ptr, memcached_stat_st *memc_stat)
     return;
   }
 
-  if (ptr)
-    ptr->call_free(ptr, memc_stat);
+  if (memc_stat->root)
+  {
+    libmemcached_free(memc_stat->root, memc_stat);
+  }
+  else if (ptr)
+  {
+    libmemcached_free(ptr, memc_stat);
+  }
   else
+  {
     free(memc_stat);
+  }
+}
+
+static memcached_return_t call_stat_fn(memcached_st *ptr,
+                                       memcached_server_write_instance_st instance,
+                                       void *context)
+{
+  memcached_return_t rc;
+  struct local_context *check= (struct local_context *)context;
+
+  if (ptr->flags.binary_protocol)
+  {
+    rc= binary_stats_fetch(NULL, check->args, instance, check);
+  }
+  else
+  {
+    rc= ascii_stats_fetch(NULL, check->args, instance, check);
+  }
+
+  return rc;
+}
+
+memcached_return_t memcached_stat_execute(memcached_st *memc, const char *args,  memcached_stat_fn func, void *context)
+{
+  memcached_version(memc);
+
+ struct local_context check= { .func= func, .context= context, .args= args };
+
+ return memcached_server_execute(memc, call_stat_fn, (void *)&check);
 }