Refactor of generate tests.
[awesomized/libmemcached] / lib / memcached_stats.c
index 48e62ccee299d949c3cadd00d964e1a891ad3425..56ec80df08a55a1901c39fc6ba2a7fad769a9745 100644 (file)
@@ -1,7 +1,7 @@
 /*
 */
 
-#include <memcached.h>
+#include "common.h"
 
 static char *memcached_stat_keys[] = {
   "pid",
@@ -11,10 +11,6 @@ static char *memcached_stat_keys[] = {
   "pointer_size",
   "rusage_user",
   "rusage_system",
-  "rusage_user_seconds",
-  "rusage_user_microseconds",
-  "rusage_system_seconds",
-  "rusage_system_microseconds",
   "curr_items",
   "total_items",
   "bytes",
@@ -36,107 +32,107 @@ static char *memcached_stat_keys[] = {
 
 static void set_data(memcached_stat_st *stat, char *key, char *value)
 {
-  if (!memcmp("pid", key, strlen("pid")))
+
+  if(strlen(key) < 1) 
   {
-    stat->pid= strtol(value, (char **)NULL, 10);
+    fprintf(stderr, "Invalid key %s\n", key);
   }
-  else if (!memcmp("uptime", key, strlen("uptime")))
+  else if (!strcmp("pid", key))
   {
-    stat->uptime= strtol(value, (char **)NULL, 10);
+    stat->pid= strtol(value, (char **)NULL, 10);
   }
-  else if (!memcmp("time", key, strlen("time")))
+  else if (!strcmp("uptime", key))
   {
-    stat->time= strtoll(value, (char **)NULL, 10);
-  }
-  else if (!memcmp("version", key, strlen("version")))
-  {
-    memcpy(stat->version, value, 8);
+    stat->uptime= strtol(value, (char **)NULL, 10);
   }
-  else if (!memcmp("pointer_size", key, strlen("pointer_size")))
+  else if (!strcmp("time", key))
   {
-    stat->pointer_size= strtoll(value, (char **)NULL, 10);
+    stat->time= strtol(value, (char **)NULL, 10);
   }
-  else if (!memcmp("rusage_user", key, strlen("rusage_user")))
+  else if (!strcmp("version", key))
   {
-    stat->rusage_user= strtoll(value, (char **)NULL, 10);
+    memcpy(stat->version, value, strlen(value));
+    stat->version[strlen(value)]= 0;
   }
-  else if (!memcmp("rusage_system", key, strlen("rusage_system")))
+  else if (!strcmp("pointer_size", key))
   {
-    stat->rusage_system= strtoll(value, (char **)NULL, 10);
+    stat->pointer_size= strtol(value, (char **)NULL, 10);
   }
-  else if (!memcmp("rusage_user_seconds", key, strlen("rusage_user_seconds")))
+  else if (!strcmp("rusage_user", key))
   {
-    stat->rusage_user_seconds= strtoll(value, (char **)NULL, 10);
+    char *walk_ptr;
+    for (walk_ptr= value; (!ispunct(*walk_ptr)); walk_ptr++);
+    *walk_ptr= 0;
+    walk_ptr++;
+    stat->rusage_user_seconds= strtol(value, (char **)NULL, 10);
+    stat->rusage_user_microseconds= strtol(walk_ptr, (char **)NULL, 10);
   }
-  else if (!memcmp("rusage_user_microseconds", key, strlen("rusage_user_microseconds")))
+  else if (!strcmp("rusage_system", key))
   {
-    stat->rusage_user_microseconds= strtoll(value, (char **)NULL, 10);
+    char *walk_ptr;
+    for (walk_ptr= value; (!ispunct(*walk_ptr)); walk_ptr++);
+    *walk_ptr= 0;
+    walk_ptr++;
+    stat->rusage_system_seconds= strtol(value, (char **)NULL, 10);
+    stat->rusage_system_microseconds= strtol(walk_ptr, (char **)NULL, 10);
   }
-  else if (!memcmp("rusage_system_seconds", key, strlen("rusage_system_seconds")))
+  else if (!strcmp("curr_items", key))
   {
-    stat->rusage_system_seconds= strtoll(value, (char **)NULL, 10);
+    stat->curr_items= strtol(value, (char **)NULL, 10); 
   }
-  else if (!memcmp("rusage_system_microseconds", key, strlen("rusage_system_microseconds")))
+  else if (!strcmp("total_items", key))
   {
-    stat->rusage_system_microseconds= strtoll(value, (char **)NULL, 10);
+    stat->total_items= strtol(value, (char **)NULL, 10);
   }
-  else if (!memcmp("curr_items", key, strlen("curr_items")))
-  {
-    stat->curr_items= strtoll(value, (char **)NULL, 10);
-  }
-  else if (!memcmp("total_items", key, strlen("total_items")))
-  {
-    stat->total_items= strtoll(value, (char **)NULL, 10);
-  }
-  else if (!memcmp("bytes", key, strlen("bytes")))
+  else if (!strcmp("bytes", key))
   {
     stat->bytes= strtoll(value, (char **)NULL, 10);
   }
-  else if (!memcmp("curr_connections", key, strlen("curr_connections")))
+  else if (!strcmp("curr_connections", key))
   {
     stat->curr_connections= strtoll(value, (char **)NULL, 10);
   }
-  else if (!memcmp("total_connections", key, strlen("total_connections")))
+  else if (!strcmp("total_connections", key))
   {
     stat->total_connections= strtoll(value, (char **)NULL, 10);
   }
-  else if (!memcmp("connection_structures", key, strlen("connection_structures")))
+  else if (!strcmp("connection_structures", key))
   {
-    stat->connection_structures= strtoll(value, (char **)NULL, 10);
+    stat->connection_structures= strtol(value, (char **)NULL, 10);
   }
-  else if (!memcmp("cmd_get", key, strlen("cmd_get")))
+  else if (!strcmp("cmd_get", key))
   {
     stat->cmd_get= strtoll(value, (char **)NULL, 10);
   }
-  else if (!memcmp("cmd_set", key, strlen("cmd_set")))
+  else if (!strcmp("cmd_set", key))
   {
     stat->cmd_set= strtoll(value, (char **)NULL, 10);
   }
-  else if (!memcmp("get_hits", key, strlen("get_hits")))
+  else if (!strcmp("get_hits", key))
   {
     stat->get_hits= strtoll(value, (char **)NULL, 10);
   }
-  else if (!memcmp("get_misses", key, strlen("get_misses")))
+  else if (!strcmp("get_misses", key))
   {
-    stat->get_misses= strtoll(value, (char **)NULL, 10);
+    stat->get_misses= (uint64_t)strtoll(value, (char **)NULL, 10);
   }
-  else if (!memcmp("evictions", key, strlen("evictions")))
+  else if (!strcmp("evictions", key))
   {
-    stat->evictions= strtoll(value, (char **)NULL, 10);
+    stat->evictions= (uint64_t)strtoll(value, (char **)NULL, 10);
   }
-  else if (!memcmp("bytes_read", key, strlen("bytes_read")))
+  else if (!strcmp("bytes_read", key))
   {
     stat->bytes_read= strtoll(value, (char **)NULL, 10);
   }
-  else if (!memcmp("bytes_written", key, strlen("bytes_written")))
+  else if (!strcmp("bytes_written", key))
   {
     stat->bytes_written= strtoll(value, (char **)NULL, 10);
   }
-  else if (!memcmp("limit_maxbytes", key, strlen("limit_maxbytes")))
+  else if (!strcmp("limit_maxbytes", key))
   {
-    stat->limit_maxbytes= strtoll(value, (char **)NULL, 10);
+    stat->limit_maxbytes= strtol(value, (char **)NULL, 10);
   }
-  else if (!memcmp("threads", key, strlen("threads")))
+  else if (!strcmp("threads", key))
   {
     stat->threads= strtol(key, (char **)NULL, 10);
   }
@@ -146,123 +142,73 @@ static void set_data(memcached_stat_st *stat, char *key, char *value)
   }
 }
 
-memcached_return memcached_stat_get_value(memcached_stat_st *stat, char *key
-                                          char *value, size_t value_length)
+char *memcached_stat_get_value(memcached_st *ptr, memcached_stat_st *stat
+                               char *key, memcached_return *error)
 {
-  memcached_return rc;
+  char buffer[SMALL_STRING_LEN];
+  size_t length;
+  char *ret;
 
-  rc= MEMCACHED_SUCCESS;
+  *error= MEMCACHED_SUCCESS;
 
   if (!memcmp("pid", key, strlen("pid")))
-  {
-    snprintf(value, value_length,"%u", stat->pid);
-  }
+    length= snprintf(buffer, SMALL_STRING_LEN,"%u", stat->pid);
   else if (!memcmp("uptime", key, strlen("uptime")))
-  {
-    snprintf(value, value_length,"%u", stat->uptime);
-  }
+    length= snprintf(buffer, SMALL_STRING_LEN,"%u", stat->uptime);
   else if (!memcmp("time", key, strlen("time")))
-  {
-    snprintf(value, value_length,"%llu", (unsigned long long)stat->time);
-  }
+    length= snprintf(buffer, SMALL_STRING_LEN,"%llu", (unsigned long long)stat->time);
   else if (!memcmp("version", key, strlen("version")))
-  {
-    snprintf(value, value_length,"%s", stat->version);
-  }
+    length= snprintf(buffer, SMALL_STRING_LEN,"%s", stat->version);
   else if (!memcmp("pointer_size", key, strlen("pointer_size")))
-  {
-    snprintf(value, value_length,"%u", stat->pointer_size);
-  }
+    length= snprintf(buffer, SMALL_STRING_LEN,"%u", stat->pointer_size);
   else if (!memcmp("rusage_user", key, strlen("rusage_user")))
-  {
-    snprintf(value, value_length,"%u", stat->rusage_user);
-  }
+    length= snprintf(buffer, SMALL_STRING_LEN,"%u.%u", stat->rusage_user_seconds, stat->rusage_user_microseconds);
   else if (!memcmp("rusage_system", key, strlen("rusage_system")))
-  {
-    snprintf(value, value_length,"%u", stat->rusage_system);
-  }
-  else if (!memcmp("rusage_user_seconds", key, strlen("rusage_user_seconds")))
-  {
-    snprintf(value, value_length,"%u", stat->rusage_user_seconds);
-  }
-  else if (!memcmp("rusage_user_microseconds", key, strlen("rusage_user_microseconds")))
-  {
-    snprintf(value, value_length,"%u", stat->rusage_user_microseconds);
-  }
-  else if (!memcmp("rusage_system_seconds", key, strlen("rusage_system_seconds")))
-  {
-    snprintf(value, value_length,"%u", stat->rusage_system_seconds);
-  }
-  else if (!memcmp("rusage_system_microseconds", key, strlen("rusage_system_microseconds")))
-  {
-    snprintf(value, value_length,"%u", stat->rusage_system_microseconds);
-  }
+    length= snprintf(buffer, SMALL_STRING_LEN,"%u.%u", stat->rusage_system_seconds, stat->rusage_system_microseconds);
   else if (!memcmp("curr_items", key, strlen("curr_items")))
-  {
-    snprintf(value, value_length,"%u", stat->curr_items);
-  }
+    length= snprintf(buffer, SMALL_STRING_LEN,"%u", stat->curr_items);
   else if (!memcmp("total_items", key, strlen("total_items")))
-  {
-    snprintf(value, value_length,"%u", stat->total_items);
-  }
+    length= snprintf(buffer, SMALL_STRING_LEN,"%u", stat->total_items);
   else if (!memcmp("bytes", key, strlen("bytes")))
-  {
-    snprintf(value, value_length,"%llu", stat->bytes);
-  }
+    length= snprintf(buffer, SMALL_STRING_LEN,"%llu", (unsigned long long)stat->bytes);
   else if (!memcmp("curr_connections", key, strlen("curr_connections")))
-  {
-    snprintf(value, value_length,"%u", stat->curr_connections);
-  }
+    length= snprintf(buffer, SMALL_STRING_LEN,"%u", stat->curr_connections);
   else if (!memcmp("total_connections", key, strlen("total_connections")))
-  {
-    snprintf(value, value_length,"%u", stat->total_connections);
-  }
+    length= snprintf(buffer, SMALL_STRING_LEN,"%u", stat->total_connections);
   else if (!memcmp("connection_structures", key, strlen("connection_structures")))
-  {
-    snprintf(value, value_length,"%u", stat->connection_structures);
-  }
+    length= snprintf(buffer, SMALL_STRING_LEN,"%u", stat->connection_structures);
   else if (!memcmp("cmd_get", key, strlen("cmd_get")))
-  {
-    snprintf(value, value_length,"%llu", stat->cmd_get);
-  }
+    length= snprintf(buffer, SMALL_STRING_LEN,"%llu", (unsigned long long)stat->cmd_get);
   else if (!memcmp("cmd_set", key, strlen("cmd_set")))
-  {
-    snprintf(value, value_length,"%llu", stat->cmd_set);
-  }
+    length= snprintf(buffer, SMALL_STRING_LEN,"%llu", (unsigned long long)stat->cmd_set);
   else if (!memcmp("get_hits", key, strlen("get_hits")))
-  {
-    snprintf(value, value_length,"%llu", stat->get_hits);
-  }
+    length= snprintf(buffer, SMALL_STRING_LEN,"%llu", (unsigned long long)stat->get_hits);
   else if (!memcmp("get_misses", key, strlen("get_misses")))
-  {
-    snprintf(value, value_length,"%llu", stat->get_misses);
-  }
+    length= snprintf(buffer, SMALL_STRING_LEN,"%llu", (unsigned long long)stat->get_misses);
   else if (!memcmp("evictions", key, strlen("evictions")))
-  {
-    snprintf(value, value_length,"%llu", stat->evictions);
-  }
+    length= snprintf(buffer, SMALL_STRING_LEN,"%llu", (unsigned long long)stat->evictions);
   else if (!memcmp("bytes_read", key, strlen("bytes_read")))
-  {
-    snprintf(value, value_length,"%llu", stat->bytes_read);
-  }
+    length= snprintf(buffer, SMALL_STRING_LEN,"%llu", (unsigned long long)stat->bytes_read);
   else if (!memcmp("bytes_written", key, strlen("bytes_written")))
-  {
-    snprintf(value, value_length,"%llu", stat->bytes_written);
-  }
+    length= snprintf(buffer, SMALL_STRING_LEN,"%llu", (unsigned long long)stat->bytes_written);
   else if (!memcmp("limit_maxbytes", key, strlen("limit_maxbytes")))
-  {
-    snprintf(value, value_length,"%u", stat->limit_maxbytes);
-  }
+    length= snprintf(buffer, SMALL_STRING_LEN,"%u", stat->limit_maxbytes);
   else if (!memcmp("threads", key, strlen("threads")))
-  {
-    snprintf(value, value_length,"%u", stat->threads);
-  }
+    length= snprintf(buffer, SMALL_STRING_LEN,"%u", stat->threads);
   else
   {
-    rc= MEMCACHED_NOTFOUND;
+    *error= MEMCACHED_NOTFOUND;
+    return NULL;
   }
 
-  return rc;
+  if (ptr->call_malloc)
+    ret= ptr->call_malloc(ptr, length + 1);
+  else
+    ret= malloc(length + 1);
+  memcpy(ret, buffer, length);
+  ret[length]= '\0';
+
+  return ret;
 }
 
 static memcached_return memcached_stats_fetch(memcached_st *ptr,
@@ -271,56 +217,54 @@ static memcached_return memcached_stats_fetch(memcached_st *ptr,
                                               unsigned int server_key)
 {
   memcached_return rc;
-  char buffer[HUGE_STRING_LEN];
-  size_t send_length, sent_length;
-
-  rc= memcached_connect(ptr);
-
-  if (rc != MEMCACHED_SUCCESS)
-    return rc;
+  char buffer[MEMCACHED_DEFAULT_COMMAND_SIZE];
+  size_t send_length;
 
   if (args)
-    send_length= snprintf(buffer, HUGE_STRING_LEN
+    send_length= snprintf(buffer, MEMCACHED_DEFAULT_COMMAND_SIZE
                           "stats %s\r\n", args);
   else
-    send_length= snprintf(buffer, HUGE_STRING_LEN
+    send_length= snprintf(buffer, MEMCACHED_DEFAULT_COMMAND_SIZE
                           "stats\r\n");
 
   if (send_length >= MEMCACHED_DEFAULT_COMMAND_SIZE)
     return MEMCACHED_WRITE_FAILURE;
 
-  sent_length= write(ptr->hosts[server_key].fd, buffer, send_length);
-
-  if (sent_length == -1 || sent_length != send_length)
-    return MEMCACHED_WRITE_FAILURE;
-
-  rc= memcached_response(ptr, buffer, HUGE_STRING_LEN, 0);
+  rc= memcached_do(&ptr->hosts[server_key], buffer, send_length, 1);
+  if (rc != MEMCACHED_SUCCESS)
+      goto error;
 
-  if (rc == MEMCACHED_SUCCESS)
+  while (1)
   {
-    char *string_ptr, *end_ptr;
-    char *key, *value;
+    rc= memcached_response(&ptr->hosts[server_key], buffer, MEMCACHED_DEFAULT_COMMAND_SIZE, NULL);
 
-    string_ptr= buffer;
-    while (1)
+    if (rc == MEMCACHED_STAT)
     {
-      if (memcmp(string_ptr, "STAT ", 5))
-        break;
-      string_ptr+= 5;
-      for (end_ptr= string_ptr; *end_ptr != ' '; end_ptr++);
+      char *string_ptr, *end_ptr;
+      char *key, *value;
+
+      string_ptr= buffer;
+      string_ptr+= 5; /* Move past STAT */
+      for (end_ptr= string_ptr; isgraph(*end_ptr); end_ptr++);
       key= string_ptr;
       key[(size_t)(end_ptr-string_ptr)]= 0;
 
       string_ptr= end_ptr + 1;
-      for (end_ptr= string_ptr; *end_ptr != '\r'; end_ptr++);
+      for (end_ptr= string_ptr; !(isspace(*end_ptr)); end_ptr++);
       value= string_ptr;
       value[(size_t)(end_ptr-string_ptr)]= 0;
       string_ptr= end_ptr + 2;
       set_data(stat, key, value);
     }
+    else
+      break;
   }
 
-  return rc;
+error:
+  if (rc == MEMCACHED_END)
+    return MEMCACHED_SUCCESS;
+  else
+    return rc;
 }
 
 memcached_stat_st *memcached_stat(memcached_st *ptr, char *args, memcached_return *error)
@@ -328,51 +272,51 @@ memcached_stat_st *memcached_stat(memcached_st *ptr, char *args, memcached_retur
   unsigned int x;
   memcached_return rc;
   memcached_stat_st *stats;
-  rc= memcached_connect(ptr);
 
-  if (rc != MEMCACHED_SUCCESS)
-  {
-    *error= rc;
-    return NULL;
-  }
+  if (ptr->call_malloc)
+    stats= (memcached_stat_st *)ptr->call_malloc(ptr, sizeof(memcached_stat_st)*(ptr->number_of_hosts));
+  else
+    stats= (memcached_stat_st *)malloc(sizeof(memcached_stat_st)*(ptr->number_of_hosts));
 
-  stats= (memcached_stat_st *)malloc(sizeof(memcached_st)*(ptr->number_of_hosts+1));
-  if (stats)
+  if (!stats)
   {
     *error= MEMCACHED_MEMORY_ALLOCATION_FAILURE;
+    if (ptr->call_free)
+      ptr->call_free(ptr, stats);
+    else
+      free(stats);
+
     return NULL;
   }
-  memset(stats, 0, sizeof(memcached_st)*(ptr->number_of_hosts+1));
+  memset(stats, 0, sizeof(memcached_stat_st)*(ptr->number_of_hosts));
 
+  rc= MEMCACHED_SUCCESS;
   for (x= 0; x < ptr->number_of_hosts; x++)
   {
-    rc= memcached_stats_fetch(ptr, stats+x, args, x);
-    if (rc != MEMCACHED_SUCCESS)
+    memcached_return temp_return;
+
+    temp_return= memcached_stats_fetch(ptr, stats + x, args, x);
+    if (temp_return != MEMCACHED_SUCCESS)
       rc= MEMCACHED_SOME_ERRORS;
   }
 
-  *error= x == 0 ? MEMCACHED_SUCCESS : rc;
+  *error= rc;
   return stats;
 }
 
-memcached_return memcached_stat_hostname(memcached_stat_st *stat, char *args, 
-                                         char *hostname, unsigned int port)
+memcached_return memcached_stat_servername(memcached_stat_st *stat, char *args, 
+                                           char *hostname, unsigned int port)
 {
   memcached_return rc;
   memcached_st memc;
 
-  memcached_init(&memc);
+  memcached_create(&memc);
 
   memcached_server_add(&memc, hostname, port);
 
-  rc= memcached_connect(&memc);
-
-  if (rc != MEMCACHED_SUCCESS)
-    return rc;
-
   rc= memcached_stats_fetch(&memc, stat, args, 0);
 
-  memcached_deinit(&memc);
+  memcached_free(&memc);
 
   return rc;
 }
@@ -381,11 +325,41 @@ memcached_return memcached_stat_hostname(memcached_stat_st *stat, char *args,
   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_stat_st *stat, memcached_return *error)
+char ** memcached_stat_get_keys(memcached_st *ptr, memcached_stat_st *stat, 
+                                memcached_return *error)
 {
-  char **list= (char **)malloc(sizeof(memcached_stat_keys));
+  char **list;
+  size_t length= sizeof(memcached_stat_keys);
+
+  if (ptr->call_malloc)
+    list= (char **)ptr->call_malloc(ptr, length);
+  else
+    list= (char **)malloc(length);
+
+  if (!list)
+  {
+    *error= MEMCACHED_MEMORY_ALLOCATION_FAILURE;
+    return NULL;
+  }
+  memset(list, 0, sizeof(memcached_stat_keys));
 
   memcpy(list, memcached_stat_keys, sizeof(memcached_stat_keys));
 
+  *error= MEMCACHED_SUCCESS;
+
   return list;
 }
+
+void memcached_stat_free(memcached_st *ptr, memcached_stat_st *stat)
+{
+  if (stat == NULL)
+  {
+    WATCHPOINT_ASSERT(0); /* Be polite, but when debugging catch this as an error */
+    return;
+  }
+
+  if (ptr && ptr->call_free)
+    ptr->call_free(ptr, stat);
+  else
+    free(stat);
+}