[Groonga-commit] groonga/groonga [master] Use ctx->impl->levels instead of ctx->impl->opened.

Back to archive index

null+****@clear***** null+****@clear*****
2010年 6月 19日 (土) 00:29:11 JST


Daijiro MORI	2010-06-18 15:29:11 +0000 (Fri, 18 Jun 2010)

  New Revision: 9a88cb99e57648a900cab0cc381049a449ff13d0

  Log:
    Use ctx->impl->levels instead of ctx->impl->opened.

  Modified files:
    lib/ctx.c
    lib/output.c
    lib/output.h
    lib/proc.c
    lib/ql.h

  Modified: lib/ctx.c (+0 -1)
===================================================================
--- lib/ctx.c    2010-06-18 05:43:12 +0000 (8b0742f)
+++ lib/ctx.c    2010-06-18 15:29:11 +0000 (dc2eabc)
@@ -1004,7 +1004,6 @@ grn_ctx_send(grn_ctx *ctx, const char *str, unsigned int str_len, int flags)
       } else {
         ctx->impl->mime_type = "application/json";
         ctx->impl->output_type = GRN_CONTENT_JSON;
-        ctx->impl->opened = 1;
         grn_timeval_now(ctx, &ctx->impl->tv);
         GRN_LOG(ctx, GRN_LOG_NONE, "%08x|>%.*s", (intptr_t)ctx, str_len, str);
         if (str_len && *str == '/') {

  Modified: lib/output.c (+88 -25)
===================================================================
--- lib/output.c    2010-06-18 05:43:12 +0000 (ca2347a)
+++ lib/output.c    2010-06-18 15:29:11 +0000 (e1a561a)
@@ -19,14 +19,41 @@
 #include "output.h"
 #include "ql.h"
 
+#define LEVELS (&ctx->impl->levels)
+#define DEPTH (GRN_BULK_VSIZE(LEVELS)>>2)
+#define CURR_LEVEL (DEPTH ? (GRN_UINT32_VALUE_AT(LEVELS, (DEPTH - 1))) : 0)
+#define INCR_DEPTH(i) GRN_UINT32_PUT(ctx, LEVELS, i)
+#define DECR_DEPTH (DEPTH ? grn_bulk_truncate(ctx, LEVELS, GRN_BULK_VSIZE(LEVELS) - sizeof(uint32_t)) : 0)
+#define INCR_LENGTH (DEPTH ? (GRN_UINT32_VALUE_AT(LEVELS, (DEPTH - 1)) += 2) : 0)
+
+static void
+put_delimiter(grn_ctx *ctx)
+{
+  uint32_t level = CURR_LEVEL;
+  grn_obj *outbuf = ctx->impl->outbuf;
+  grn_content_type output_type = ctx->impl->output_type;
+  if (level < 2) { return; }
+  switch (output_type) {
+  case GRN_CONTENT_JSON:
+    GRN_TEXT_PUTC(ctx, outbuf, ((level & 3) == 3) ? ':' : ',');
+    break;
+  case GRN_CONTENT_XML:
+    break;
+  case GRN_CONTENT_TSV:
+    if (DEPTH > 1) { GRN_TEXT_PUTC(ctx, outbuf, '\t'); }
+  case GRN_CONTENT_NONE:
+    break;
+  }
+}
+
 void
 grn_output_array_open(grn_ctx *ctx, const char *name, int nelements)
 {
   grn_obj *outbuf = ctx->impl->outbuf;
   grn_content_type output_type = ctx->impl->output_type;
+  put_delimiter(ctx);
   switch (output_type) {
   case GRN_CONTENT_JSON:
-    if (!ctx->impl->opened) { GRN_TEXT_PUTC(ctx, outbuf, ','); }
     GRN_TEXT_PUTC(ctx, outbuf, '[');
     break;
   case GRN_CONTENT_XML:
@@ -34,12 +61,12 @@ grn_output_array_open(grn_ctx *ctx, const char *name, int nelements)
                   "<SEGMENTS>\n<SEGMENT>\n<RESULTPAGE>\n");
     break;
   case GRN_CONTENT_TSV:
-    if (!ctx->impl->opened) { GRN_TEXT_PUTC(ctx, outbuf, '\t'); }
-    grn_text_itoa(ctx, outbuf, ctx->rc);
+    if (DEPTH > 1) { GRN_TEXT_PUTS(ctx, outbuf, "[\t"); }
+    break;
   case GRN_CONTENT_NONE:
     break;
   }
-  ctx->impl->opened = 1;
+  INCR_DEPTH(0);
 }
 
 void
@@ -52,15 +79,24 @@ grn_output_array_close(grn_ctx *ctx)
     GRN_TEXT_PUTC(ctx, outbuf, ']');
     break;
   case GRN_CONTENT_TSV:
-    GRN_TEXT_PUTC(ctx, outbuf, '\n');
+    if (DEPTH > 1) {
+      if (DEPTH == 2) {
+        GRN_TEXT_PUTC(ctx, outbuf, '\n');
+      } else {
+        if (CURR_LEVEL >= 2) { GRN_TEXT_PUTC(ctx, outbuf, '\t'); }
+        GRN_TEXT_PUTC(ctx, outbuf, ']');
+      }
+    }
     break;
   case GRN_CONTENT_XML:
-    GRN_TEXT_PUTS(ctx, outbuf, "</RESULTPAGE>\n</SEGMENT>\n</SEGMENTS>\n");
+    GRN_TEXT_PUTS(ctx, outbuf, "</RESULTPAGE>\n</SEGMENT>\n</SEGMENTS>");
+    if (DEPTH > 1) { GRN_TEXT_PUTC(ctx, outbuf, '\n'); }
     break;
   case GRN_CONTENT_NONE:
     break;
   }
-  ctx->impl->opened = 0;
+  DECR_DEPTH;
+  INCR_LENGTH;
 }
 
 void
@@ -68,9 +104,9 @@ grn_output_map_open(grn_ctx *ctx, const char *name, int nelements)
 {
   grn_obj *outbuf = ctx->impl->outbuf;
   grn_content_type output_type = ctx->impl->output_type;
+  put_delimiter(ctx);
   switch (output_type) {
   case GRN_CONTENT_JSON:
-    if (!ctx->impl->opened) { GRN_TEXT_PUTC(ctx, outbuf, ','); }
     GRN_TEXT_PUTS(ctx, outbuf, "{");
     break;
   case GRN_CONTENT_XML:
@@ -78,12 +114,12 @@ grn_output_map_open(grn_ctx *ctx, const char *name, int nelements)
                   "<SEGMENTS>\n<SEGMENT>\n<RESULTPAGE>\n");
     break;
   case GRN_CONTENT_TSV:
-    if (!ctx->impl->opened) { GRN_TEXT_PUTC(ctx, outbuf, '\t'); }
-    grn_text_itoa(ctx, outbuf, ctx->rc);
+    if (DEPTH > 1) { GRN_TEXT_PUTS(ctx, outbuf, "{\t"); }
+    break;
   case GRN_CONTENT_NONE:
     break;
   }
-  ctx->impl->opened = 1;
+  INCR_DEPTH(1);
 }
 
 void
@@ -96,15 +132,24 @@ grn_output_map_close(grn_ctx *ctx)
     GRN_TEXT_PUTS(ctx, outbuf, "}");
     break;
   case GRN_CONTENT_TSV:
-    GRN_TEXT_PUTC(ctx, outbuf, '\n');
+    if (DEPTH) {
+      if (DEPTH == 1) {
+        GRN_TEXT_PUTC(ctx, outbuf, '\n');
+      } else {
+        if (CURR_LEVEL >= 2) { GRN_TEXT_PUTC(ctx, outbuf, '\t'); }
+        GRN_TEXT_PUTC(ctx, outbuf, '}');
+      }
+    }
     break;
   case GRN_CONTENT_XML:
-    GRN_TEXT_PUTS(ctx, outbuf, "</RESULTPAGE>\n</SEGMENT>\n</SEGMENTS>\n");
+    GRN_TEXT_PUTS(ctx, outbuf, "</RESULTPAGE>\n</SEGMENT>\n</SEGMENTS>");
+    if (DEPTH > 1) { GRN_TEXT_PUTC(ctx, outbuf, '\n'); }
     break;
   case GRN_CONTENT_NONE:
     break;
   }
-  ctx->impl->opened = 0;
+  DECR_DEPTH;
+  INCR_LENGTH;
 }
 
 void
@@ -112,13 +157,12 @@ grn_output_int32(grn_ctx *ctx, int value)
 {
   grn_obj *outbuf = ctx->impl->outbuf;
   grn_content_type output_type = ctx->impl->output_type;
+  put_delimiter(ctx);
   switch (output_type) {
   case GRN_CONTENT_JSON:
-    if (!ctx->impl->opened) { GRN_TEXT_PUTC(ctx, outbuf, ','); }
     grn_text_itoa(ctx, outbuf, value);
     break;
   case GRN_CONTENT_TSV:
-    if (!ctx->impl->opened) { GRN_TEXT_PUTC(ctx, outbuf, '\t'); }
     grn_text_itoa(ctx, outbuf, value);
     break;
   case GRN_CONTENT_XML:
@@ -127,7 +171,29 @@ grn_output_int32(grn_ctx *ctx, int value)
   case GRN_CONTENT_NONE:
     break;
   }
-  ctx->impl->opened = 0;
+  INCR_LENGTH;
+}
+
+void
+grn_output_int64(grn_ctx *ctx, long long value)
+{
+  grn_obj *outbuf = ctx->impl->outbuf;
+  grn_content_type output_type = ctx->impl->output_type;
+  put_delimiter(ctx);
+  switch (output_type) {
+  case GRN_CONTENT_JSON:
+    grn_text_lltoa(ctx, outbuf, value);
+    break;
+  case GRN_CONTENT_TSV:
+    grn_text_lltoa(ctx, outbuf, value);
+    break;
+  case GRN_CONTENT_XML:
+    grn_text_lltoa(ctx, outbuf, value);
+    break;
+  case GRN_CONTENT_NONE:
+    break;
+  }
+  INCR_LENGTH;
 }
 
 void
@@ -135,13 +201,12 @@ grn_output_str(grn_ctx *ctx, const char *value)
 {
   grn_obj *outbuf = ctx->impl->outbuf;
   grn_content_type output_type = ctx->impl->output_type;
+  put_delimiter(ctx);
   switch (output_type) {
   case GRN_CONTENT_JSON:
-    if (!ctx->impl->opened) { GRN_TEXT_PUTC(ctx, outbuf, ','); }
     grn_text_esc(ctx, outbuf, value, strlen(value));
     break;
   case GRN_CONTENT_TSV:
-    if (!ctx->impl->opened) { GRN_TEXT_PUTC(ctx, outbuf, '\t'); }
     grn_text_esc(ctx, outbuf, value, strlen(value));
     break;
   case GRN_CONTENT_XML:
@@ -150,7 +215,7 @@ grn_output_str(grn_ctx *ctx, const char *value)
   case GRN_CONTENT_NONE:
     break;
   }
-  ctx->impl->opened = 0;
+  INCR_LENGTH;
 }
 
 void
@@ -158,15 +223,13 @@ grn_output_obj(grn_ctx *ctx, grn_obj *obj, grn_obj_format *format)
 {
   grn_obj *outbuf = ctx->impl->outbuf;
   grn_content_type output_type = ctx->impl->output_type;
+  put_delimiter(ctx);
   switch (output_type) {
   case GRN_CONTENT_JSON:
-    if (!ctx->impl->opened) { GRN_TEXT_PUTC(ctx, outbuf, ','); }
     grn_text_otoj(ctx, outbuf, obj, format);
     break;
   case GRN_CONTENT_TSV:
-    if (!ctx->impl->opened) { GRN_TEXT_PUTC(ctx, outbuf, '\t'); }
-    GRN_TEXT_PUTC(ctx, outbuf, '\n');
-    /* TODO: implement grn_text_ototsv */
+    grn_text_otoj(ctx, outbuf, obj, format);
     break;
   case GRN_CONTENT_XML:
     grn_text_otoxml(ctx, outbuf, obj, format);
@@ -174,5 +237,5 @@ grn_output_obj(grn_ctx *ctx, grn_obj *obj, grn_obj_format *format)
   case GRN_CONTENT_NONE:
     break;
   }
-  ctx->impl->opened = 0;
+  INCR_LENGTH;
 }

  Modified: lib/output.h (+1 -0)
===================================================================
--- lib/output.h    2010-06-18 05:43:12 +0000 (5cc3090)
+++ lib/output.h    2010-06-18 15:29:11 +0000 (83672ec)
@@ -38,6 +38,7 @@ void grn_output_array_close(grn_ctx *ctx);
 void grn_output_map_open(grn_ctx *ctx, const char *name, int nelements);
 void grn_output_map_close(grn_ctx *ctx);
 void grn_output_int32(grn_ctx *ctx, int value);
+void grn_output_int64(grn_ctx *ctx, long long value);
 void grn_output_str(grn_ctx *ctx, const char *value);
 void grn_output_obj(grn_ctx *ctx, grn_obj *obj, grn_obj_format *format);
 

  Modified: lib/proc.c (+54 -112)
===================================================================
--- lib/proc.c    2010-06-18 05:43:12 +0000 (1207bc7)
+++ lib/proc.c    2010-06-18 15:29:11 +0000 (ef623cf)
@@ -372,45 +372,18 @@ proc_load(grn_ctx *ctx, int nargs, grn_obj **args, grn_user_data *user_data)
 static grn_obj *
 proc_status(grn_ctx *ctx, int nargs, grn_obj **args, grn_user_data *user_data)
 {
-  grn_content_type ct = ctx->impl->output_type;
-  grn_obj *outbuf = ctx->impl->outbuf;
   grn_timeval now;
   grn_timeval_now(ctx, &now);
-  switch (ct) {
-  case GRN_CONTENT_TSV:
-    /* TODO: implement */
-    break;
-  case GRN_CONTENT_JSON:
-
-    {
-      GRN_TEXT_PUTS(ctx, outbuf, "{\"alloc_count\":");
-      grn_text_itoa(ctx, outbuf, grn_alloc_count());
-      GRN_TEXT_PUTS(ctx, outbuf, ",\"starttime\":");
-      grn_text_itoa(ctx, outbuf, grn_starttime.tv_sec);
-      GRN_TEXT_PUTS(ctx, outbuf, ",\"uptime\":");
-      grn_text_itoa(ctx, outbuf, now.tv_sec - grn_starttime.tv_sec);
-      GRN_TEXT_PUTS(ctx, outbuf, ",\"version\":\"");
-      GRN_TEXT_PUTS(ctx, outbuf, grn_get_version());
-      GRN_TEXT_PUTC(ctx, outbuf, '"');
-      GRN_TEXT_PUTC(ctx, outbuf, '}');
-    }
-    /*
-    grn_output_map_open(ctx, "STATUS", -1);
-    grn_output_str(ctx, "alloc_count");
-    grn_output_int32(ctx, grn_alloc_count());
-    grn_output_str(ctx, "starttime");
-    grn_output_int32(ctx, grn_starttime.tv_sec);
-    grn_output_str(ctx, "uptime");
-    grn_output_int32(ctx, now.tv_sec - grn_starttime.tv_sec);
-    grn_output_str(ctx, "version");
-    grn_output_str(ctx, grn_get_version());
-    grn_output_map_close(ctx);
-    */
-    break;
-  case GRN_CONTENT_XML:
-  case GRN_CONTENT_NONE:
-    break;
-  }
+  grn_output_map_open(ctx, "STATUS", -1);
+  grn_output_str(ctx, "alloc_count");
+  grn_output_int32(ctx, grn_alloc_count());
+  grn_output_str(ctx, "starttime");
+  grn_output_int32(ctx, grn_starttime.tv_sec);
+  grn_output_str(ctx, "uptime");
+  grn_output_int32(ctx, now.tv_sec - grn_starttime.tv_sec);
+  grn_output_str(ctx, "version");
+  grn_output_str(ctx, grn_get_version());
+  grn_output_map_close(ctx);
   return NULL;
 }
 
@@ -820,12 +793,11 @@ print_columninfo(grn_ctx *ctx, grn_obj *column, grn_obj *buf, grn_content_type o
 }
 
 static int
-print_tableinfo(grn_ctx *ctx, grn_obj *table, grn_obj *buf, grn_content_type otype)
+print_tableinfo(grn_ctx *ctx, grn_obj *table)
 {
   grn_id id;
   grn_obj o;
   const char *path;
-
   switch (table->header.type) {
   case GRN_TABLE_HASH_KEY:
   case GRN_TABLE_PAT_KEY:
@@ -835,53 +807,22 @@ print_tableinfo(grn_ctx *ctx, grn_obj *table, grn_obj *buf, grn_content_type oty
   default:
     return 0;
   }
-
   id = grn_obj_id(ctx, table);
   path = grn_obj_path(ctx, table);
   GRN_TEXT_INIT(&o, 0);
-
-  switch (otype) {
-  case GRN_CONTENT_TSV:
-    grn_text_itoa(ctx, buf, id);
-    GRN_TEXT_PUTC(ctx, buf, '\t');
-    objid2name(ctx, id, &o);
-    grn_text_esc(ctx, buf, GRN_TEXT_VALUE(&o), GRN_TEXT_LEN(&o));
-    GRN_TEXT_PUTC(ctx, buf, '\t');
-    grn_text_esc(ctx, buf, path, GRN_STRLEN(path));
-    GRN_TEXT_PUTC(ctx, buf, '\t');
-    grn_table_create_flags_to_text(ctx, &o, table->header.flags);
-    grn_text_esc(ctx, buf, GRN_TEXT_VALUE(&o), GRN_TEXT_LEN(&o));
-    GRN_TEXT_PUTC(ctx, buf, '\t');
-    objid2name(ctx, table->header.domain, &o);
-    grn_text_esc(ctx, buf, GRN_TEXT_VALUE(&o), GRN_TEXT_LEN(&o));
-    GRN_TEXT_PUTC(ctx, buf, '\t');
-    objid2name(ctx, grn_obj_get_range(ctx, table), &o);
-    grn_text_esc(ctx, buf, GRN_TEXT_VALUE(&o), GRN_TEXT_LEN(&o));
-    break;
-  case GRN_CONTENT_JSON:
-    GRN_TEXT_PUTC(ctx, buf, '[');
-    grn_text_itoa(ctx, buf, id);
-    GRN_TEXT_PUTC(ctx, buf, ',');
-    objid2name(ctx, id, &o);
-    grn_text_otoj(ctx, buf, &o, NULL);
-    GRN_TEXT_PUTC(ctx, buf, ',');
-    grn_text_esc(ctx, buf, path, GRN_STRLEN(path));
-    GRN_TEXT_PUTC(ctx, buf, ',');
-    grn_table_create_flags_to_text(ctx, &o, table->header.flags);
-    grn_text_otoj(ctx, buf, &o, NULL);
-    GRN_TEXT_PUTC(ctx, buf, ',');
-    objid2name(ctx, table->header.domain, &o);
-    grn_text_otoj(ctx, buf, &o, NULL);
-    GRN_TEXT_PUTC(ctx, buf, ',');
-    objid2name(ctx, grn_obj_get_range(ctx, table), &o);
-    grn_text_otoj(ctx, buf, &o, NULL);
-    GRN_TEXT_PUTC(ctx, buf, ']');
-    break;
-  case GRN_CONTENT_XML:
-  case GRN_CONTENT_NONE:
-    break;
-  }
-  grn_obj_close(ctx, &o);
+  grn_output_array_open(ctx, "", -1);
+  grn_output_int64(ctx, id);
+  objid2name(ctx, id, &o);
+  grn_output_obj(ctx, &o, NULL);
+  grn_output_str(ctx, path);
+  grn_table_create_flags_to_text(ctx, &o, table->header.flags);
+  grn_output_obj(ctx, &o, NULL);
+  objid2name(ctx, table->header.domain, &o);
+  grn_output_obj(ctx, &o, NULL);
+  objid2name(ctx, grn_obj_get_range(ctx, table), &o);
+  grn_output_obj(ctx, &o, NULL);
+  grn_output_array_close(ctx);
+  GRN_OBJ_FIN(ctx, &o);
   return 1;
 }
 
@@ -944,42 +885,44 @@ proc_column_list(grn_ctx *ctx, int nargs, grn_obj **args, grn_user_data *user_da
 static grn_obj *
 proc_table_list(grn_ctx *ctx, int nargs, grn_obj **args, grn_user_data *user_data)
 {
-  grn_content_type ct = ctx->impl->output_type;
-  grn_obj *outbuf = ctx->impl->outbuf;
   grn_table_cursor *cur;
-
   if ((cur = grn_table_cursor_open(ctx, ctx->impl->db, NULL, 0, NULL, 0, 0, -1, 0))) {
     grn_id id;
-    char line_delimiter, column_delimiter;
-    switch (ct) {
-    case GRN_CONTENT_TSV:
-      line_delimiter = '\n';
-      column_delimiter = '\t';
-      GRN_TEXT_PUTS(ctx, outbuf, "id\tname\tpath\tflags\tdomain\trange");
-      break;
-    case GRN_CONTENT_JSON:
-      line_delimiter = ',';
-      column_delimiter = ',';
-      GRN_TEXT_PUTS(ctx, outbuf, "[[[\"id\", \"UInt32\"],[\"name\",\"ShortText\"],[\"path\",\"ShortText\"],[\"flags\",\"ShortText\"],[\"domain\", \"ShortText\"],[\"range\",\"ShortText\"]]");
-      break;
-    case GRN_CONTENT_XML:
-    case GRN_CONTENT_NONE:
-      break;
-    }
+    grn_output_array_open(ctx, "", -1);
+    grn_output_array_open(ctx, "", -1);
+    grn_output_array_open(ctx, "", -1);
+    grn_output_str(ctx, "id");
+    grn_output_str(ctx, "UInt32");
+    grn_output_array_close(ctx);
+    grn_output_array_open(ctx, "", -1);
+    grn_output_str(ctx, "name");
+    grn_output_str(ctx, "ShortText");
+    grn_output_array_close(ctx);
+    grn_output_array_open(ctx, "", -1);
+    grn_output_str(ctx, "path");
+    grn_output_str(ctx, "ShortText");
+    grn_output_array_close(ctx);
+    grn_output_array_open(ctx, "", -1);
+    grn_output_str(ctx, "flags");
+    grn_output_str(ctx, "ShortText");
+    grn_output_array_close(ctx);
+    grn_output_array_open(ctx, "", -1);
+    grn_output_str(ctx, "domain");
+    grn_output_str(ctx, "ShortText");
+    grn_output_array_close(ctx);
+    grn_output_array_open(ctx, "", -1);
+    grn_output_str(ctx, "range");
+    grn_output_str(ctx, "ShortText");
+    grn_output_array_close(ctx);
+    grn_output_array_close(ctx);
     while ((id = grn_table_cursor_next(ctx, cur)) != GRN_ID_NIL) {
       grn_obj *o;
-
       if ((o = grn_ctx_at(ctx, id))) {
-        GRN_TEXT_PUTC(ctx, outbuf, line_delimiter);
-        if (!print_tableinfo(ctx, o, outbuf, ct)) {
-          grn_bulk_truncate(ctx, outbuf, GRN_BULK_VSIZE(outbuf) - 1);
-        }
+        print_tableinfo(ctx, o);
         grn_obj_unlink(ctx, o);
       }
     }
-    if (ct == GRN_CONTENT_JSON) {
-      GRN_TEXT_PUTC(ctx, outbuf, ']');
-    }
+    grn_output_array_close(ctx);
     grn_table_cursor_close(ctx, cur);
   }
   return NULL;
@@ -1926,9 +1869,8 @@ proc_dump(grn_ctx *ctx, int nargs, grn_obj **args, grn_user_data *user_data)
 static grn_obj *
 proc_cache_limit(grn_ctx *ctx, int nargs, grn_obj **args, grn_user_data *user_data)
 {
-  grn_obj *outbuf = ctx->impl->outbuf;
   uint32_t *mp = grn_cach_max_nentries();
-  grn_text_lltoa(ctx, outbuf, *mp);
+  grn_output_int64(ctx, *mp);
   if (GRN_TEXT_LEN(VAR(0))) {
     const char *rest;
     uint32_t max = grn_atoui(GRN_TEXT_VALUE(VAR(0)),

  Modified: lib/ql.h (+0 -1)
===================================================================
--- lib/ql.h    2010-06-18 05:43:12 +0000 (dbbcc3a)
+++ lib/ql.h    2010-06-18 15:29:11 +0000 (3f6b3ca)
@@ -196,7 +196,6 @@ struct _grn_ctx_impl {
   /* output portion */
   grn_content_type output_type;
   const char *mime_type;
-  uint32_t opened;
   grn_obj names;
   grn_obj levels;
 




Groonga-commit メーリングリストの案内
Back to archive index