[Groonga-commit] groonga/groonga [master] Extract uvector output function

Back to archive index

Kouhei Sutou null+****@clear*****
Mon Oct 15 14:05:53 JST 2012


Kouhei Sutou	2012-10-15 14:05:53 +0900 (Mon, 15 Oct 2012)

  New Revision: dde0779dade47b0117b621dd2bae700892365890
  https://github.com/groonga/groonga/commit/dde0779dade47b0117b621dd2bae700892365890

  Log:
    Extract uvector output function

  Modified files:
    lib/output.c

  Modified: lib/output.c (+119 -109)
===================================================================
--- lib/output.c    2012-10-15 14:00:59 +0900 (5cc1482)
+++ lib/output.c    2012-10-15 14:05:53 +0900 (b315ca8)
@@ -658,6 +658,124 @@ grn_output_obj(grn_ctx *ctx, grn_obj *outbuf, grn_content_type output_type, grn_
 #else /* USE_GRN_TEXT_OTOJ */
 
 static inline void
+grn_output_uvector(grn_ctx *ctx, grn_obj *outbuf, grn_content_type output_type,
+                   grn_obj *uvector, grn_obj_format *format)
+{
+  grn_obj buf;
+  GRN_TEXT_INIT(&buf, 0);
+  if (format) {
+    int i, j;
+    grn_id *v = (grn_id *)GRN_BULK_HEAD(uvector), *ve = (grn_id *)GRN_BULK_CURR(uvector);
+    int ncolumns = GRN_BULK_VSIZE(&format->columns) / sizeof(grn_obj *);
+    grn_obj **columns = (grn_obj **)GRN_BULK_HEAD(&format->columns);
+    grn_output_array_open(ctx, outbuf, output_type, "RESULTSET", -1);
+    grn_output_array_open(ctx, outbuf, output_type, "NHITS", 1);
+    grn_text_itoa(ctx, outbuf, ve - v);
+    grn_output_array_close(ctx, outbuf, output_type);
+    if (v < ve) {
+      if (format->flags & GRN_OBJ_FORMAT_WITH_COLUMN_NAMES) {
+        grn_output_array_open(ctx, outbuf, output_type, "COLUMNS", -1);
+        for (j = 0; j < ncolumns; j++) {
+          grn_id range_id;
+          grn_output_array_open(ctx, outbuf, output_type, "COLUMN", -1);
+          GRN_BULK_REWIND(&buf);
+          grn_column_name_(ctx, columns[j], &buf);
+          grn_output_obj(ctx, outbuf, output_type, &buf, NULL);
+          /* column range */
+          range_id = grn_obj_get_range(ctx, columns[j]);
+          if (range_id == GRN_ID_NIL) {
+            GRN_TEXT_PUTS(ctx, outbuf, "null");
+          } else {
+            int name_len;
+            grn_obj *range_obj;
+            char name_buf[GRN_TABLE_MAX_KEY_SIZE];
+
+            range_obj = grn_ctx_at(ctx, range_id);
+            name_len = grn_obj_name(ctx, range_obj, name_buf,
+                                    GRN_TABLE_MAX_KEY_SIZE);
+            GRN_BULK_REWIND(&buf);
+            GRN_TEXT_PUT(ctx, &buf, name_buf, name_len);
+            grn_output_obj(ctx, outbuf, output_type, &buf, NULL);
+          }
+          grn_output_array_close(ctx, outbuf, output_type);
+        }
+        grn_output_array_close(ctx, outbuf, output_type);
+      }
+      for (i = 0;; i++) {
+        grn_output_array_open(ctx, outbuf, output_type, "HITS", -1);
+        for (j = 0; j < ncolumns; j++) {
+          GRN_BULK_REWIND(&buf);
+          grn_obj_get_value(ctx, columns[j], *v, &buf);
+          grn_output_obj(ctx, outbuf, output_type, &buf, NULL);
+        }
+        grn_output_array_close(ctx, outbuf, output_type);
+        v++;
+        if (v < ve) {
+
+        } else {
+          break;
+        }
+      }
+    }
+    grn_output_array_close(ctx, outbuf, output_type);
+  } else {
+    grn_obj *range = grn_ctx_at(ctx, uvector->header.domain);
+    if (range && range->header.type == GRN_TYPE) {
+      int value_size = ((struct _grn_type *)range)->obj.range;
+      char *v = (char *)GRN_BULK_HEAD(uvector),
+        *ve = (char *)GRN_BULK_CURR(uvector);
+      grn_output_array_open(ctx, outbuf, output_type, "VECTOR", -1);
+      if (v < ve) {
+        for (;;) {
+          grn_obj value;
+          GRN_OBJ_INIT(&value, GRN_BULK, 0, uvector->header.domain);
+          grn_bulk_write_from(ctx, &value, v, 0, value_size);
+          grn_output_obj(ctx, outbuf, output_type, &value, NULL);
+
+          v += value_size;
+          if (v < ve) {
+
+          } else {
+            break;
+          }
+        }
+      }
+      grn_output_array_close(ctx, outbuf, output_type);
+    } else {
+      grn_id *v = (grn_id *)GRN_BULK_HEAD(uvector),
+        *ve = (grn_id *)GRN_BULK_CURR(uvector);
+      grn_output_array_open(ctx, outbuf, output_type, "VECTOR", ve - v);
+      if (v < ve) {
+        grn_obj key;
+        GRN_OBJ_INIT(&key, GRN_BULK, 0, range->header.domain);
+        for (;;) {
+          if (range->header.type != GRN_TABLE_NO_KEY) {
+            grn_table_get_key2(ctx, range, *v, &key);
+            grn_output_obj(ctx, outbuf, output_type, &key, NULL);
+            GRN_BULK_REWIND(&key);
+          } else {
+            grn_obj id;
+            GRN_UINT32_INIT(&id, 0);
+            GRN_UINT32_SET(ctx, &id, *v);
+            grn_output_obj(ctx, outbuf, output_type, &id, NULL);
+            GRN_OBJ_FIN(ctx, &id);
+          }
+          v++;
+          if (v < ve) {
+
+          } else {
+            break;
+          }
+        }
+        GRN_OBJ_FIN(ctx, &key);
+      }
+      grn_output_array_close(ctx, outbuf, output_type);
+    }
+  }
+  GRN_OBJ_FIN(ctx, &buf);
+}
+
+static inline void
 grn_output_vector(grn_ctx *ctx, grn_obj *outbuf, grn_content_type output_type,
                   grn_obj *vector, grn_obj_format *format)
 {
@@ -934,115 +1052,7 @@ grn_output_obj(grn_ctx *ctx, grn_obj *outbuf, grn_content_type output_type,
     }
     break;
   case GRN_UVECTOR :
-    if (format) {
-      int i, j;
-      grn_id *v = (grn_id *)GRN_BULK_HEAD(obj), *ve = (grn_id *)GRN_BULK_CURR(obj);
-      int ncolumns = GRN_BULK_VSIZE(&format->columns) / sizeof(grn_obj *);
-      grn_obj **columns = (grn_obj **)GRN_BULK_HEAD(&format->columns);
-      grn_output_array_open(ctx, outbuf, output_type, "RESULTSET", -1);
-      grn_output_array_open(ctx, outbuf, output_type, "NHITS", 1);
-      grn_text_itoa(ctx, outbuf, ve - v);
-      grn_output_array_close(ctx, outbuf, output_type);
-      if (v < ve) {
-        if (format->flags & GRN_OBJ_FORMAT_WITH_COLUMN_NAMES) {
-          grn_output_array_open(ctx, outbuf, output_type, "COLUMNS", -1);
-          for (j = 0; j < ncolumns; j++) {
-            grn_id range_id;
-            grn_output_array_open(ctx, outbuf, output_type, "COLUMN", -1);
-            GRN_BULK_REWIND(&buf);
-            grn_column_name_(ctx, columns[j], &buf);
-            grn_output_obj(ctx, outbuf, output_type, &buf, NULL);
-            /* column range */
-            range_id = grn_obj_get_range(ctx, columns[j]);
-            if (range_id == GRN_ID_NIL) {
-              GRN_TEXT_PUTS(ctx, outbuf, "null");
-            } else {
-              int name_len;
-              grn_obj *range_obj;
-              char name_buf[GRN_TABLE_MAX_KEY_SIZE];
-
-              range_obj = grn_ctx_at(ctx, range_id);
-              name_len = grn_obj_name(ctx, range_obj, name_buf,
-                                      GRN_TABLE_MAX_KEY_SIZE);
-              GRN_BULK_REWIND(&buf);
-              GRN_TEXT_PUT(ctx, &buf, name_buf, name_len);
-              grn_output_obj(ctx, outbuf, output_type, &buf, NULL);
-            }
-            grn_output_array_close(ctx, outbuf, output_type);
-          }
-          grn_output_array_close(ctx, outbuf, output_type);
-        }
-        for (i = 0;; i++) {
-          grn_output_array_open(ctx, outbuf, output_type, "HITS", -1);
-          for (j = 0; j < ncolumns; j++) {
-            GRN_BULK_REWIND(&buf);
-            grn_obj_get_value(ctx, columns[j], *v, &buf);
-            grn_output_obj(ctx, outbuf, output_type, &buf, NULL);
-          }
-          grn_output_array_close(ctx, outbuf, output_type);
-          v++;
-          if (v < ve) {
-
-          } else {
-            break;
-          }
-        }
-      }
-      grn_output_array_close(ctx, outbuf, output_type);
-    } else {
-      grn_obj *range = grn_ctx_at(ctx, obj->header.domain);
-      if (range && range->header.type == GRN_TYPE) {
-        int value_size = ((struct _grn_type *)range)->obj.range;
-        char *v = (char *)GRN_BULK_HEAD(obj),
-             *ve = (char *)GRN_BULK_CURR(obj);
-        grn_output_array_open(ctx, outbuf, output_type, "VECTOR", -1);
-        if (v < ve) {
-          for (;;) {
-            grn_obj value;
-            GRN_OBJ_INIT(&value, GRN_BULK, 0, obj->header.domain);
-            grn_bulk_write_from(ctx, &value, v, 0, value_size);
-            grn_output_obj(ctx, outbuf, output_type, &value, NULL);
-
-            v += value_size;
-            if (v < ve) {
-
-            } else {
-              break;
-            }
-          }
-        }
-        grn_output_array_close(ctx, outbuf, output_type);
-      } else {
-        grn_id *v = (grn_id *)GRN_BULK_HEAD(obj),
-               *ve = (grn_id *)GRN_BULK_CURR(obj);
-        grn_output_array_open(ctx, outbuf, output_type, "VECTOR", ve - v);
-        if (v < ve) {
-          grn_obj key;
-          GRN_OBJ_INIT(&key, GRN_BULK, 0, range->header.domain);
-          for (;;) {
-            if (range->header.type != GRN_TABLE_NO_KEY) {
-              grn_table_get_key2(ctx, range, *v, &key);
-              grn_output_obj(ctx, outbuf, output_type, &key, NULL);
-              GRN_BULK_REWIND(&key);
-            } else {
-              grn_obj id;
-              GRN_UINT32_INIT(&id, 0);
-              GRN_UINT32_SET(ctx, &id, *v);
-              grn_output_obj(ctx, outbuf, output_type, &id, NULL);
-              GRN_OBJ_FIN(ctx, &id);
-            }
-            v++;
-            if (v < ve) {
-
-            } else {
-              break;
-            }
-          }
-          GRN_OBJ_FIN(ctx, &key);
-        }
-        grn_output_array_close(ctx, outbuf, output_type);
-      }
-    }
+    grn_output_uvector(ctx, outbuf, output_type, obj, format);
     break;
   case GRN_VECTOR :
     grn_output_vector(ctx, outbuf, output_type, obj, format);
-------------- next part --------------
HTML����������������������������...
Download 



More information about the Groonga-commit mailing list
Back to archive index