[Groonga-commit] groonga/groonga at b749281 [master] Add no columns version record inspect

Back to archive index

Kouhei Sutou null+****@clear*****
Thu Feb 15 17:54:15 JST 2018


Kouhei Sutou	2018-02-15 17:54:15 +0900 (Thu, 15 Feb 2018)

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

  Message:
    Add no columns version record inspect
    
    It's for compact display case such as inspecting condition in query
    log.

  Modified files:
    lib/grn_util.h
    lib/util.c

  Modified: lib/grn_util.h (+7 -1)
===================================================================
--- lib/grn_util.h    2018-02-15 17:37:28 +0900 (b9ed347a9)
+++ lib/grn_util.h    2018-02-15 17:54:15 +0900 (cf5e21b19)
@@ -1,6 +1,6 @@
 /* -*- c-basic-offset: 2 -*- */
 /*
-  Copyright(C) 2010-2016 Brazil
+  Copyright(C) 2010-2018 Brazil
 
   This library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Lesser General Public
@@ -30,6 +30,12 @@ GRN_API grn_rc grn_normalize_offset_and_limit(grn_ctx *ctx, int size, int *offse
 GRN_API char *grn_path_separator_to_system(char *dest, char *groonga_path);
 
 void grn_p_record(grn_ctx *ctx, grn_obj *table, grn_id id);
+grn_rc grn_record_inspect_without_columns(grn_ctx *ctx,
+                                          grn_obj *buffer,
+                                          grn_obj *record);
+grn_rc grn_uvector_record_inspect_without_columns(grn_ctx *ctx,
+                                                  grn_obj *buffer,
+                                                  grn_obj *records);
 
 /*
  * grn_mkstemp generates a unique filename from path_template, creates a

  Modified: lib/util.c (+85 -34)
===================================================================
--- lib/util.c    2018-02-15 17:37:28 +0900 (dd6f4410f)
+++ lib/util.c    2018-02-15 17:54:15 +0900 (81f9fba28)
@@ -1222,10 +1222,12 @@ grn_json_load_open_brace_inspect(grn_ctx *ctx, grn_obj *buf, grn_obj *obj)
 }
 
 static grn_rc
-grn_record_inspect(grn_ctx *ctx, grn_obj *buf, grn_obj *obj)
+grn_record_inspect_common(grn_ctx *ctx,
+                          grn_obj *buf,
+                          grn_obj *obj,
+                          grn_bool with_columns)
 {
   grn_obj *table;
-  grn_hash *cols;
 
   table = grn_ctx_at(ctx, obj->header.domain);
   GRN_TEXT_PUTS(ctx, buf, "#<record:");
@@ -1245,41 +1247,59 @@ grn_record_inspect(grn_ctx *ctx, grn_obj *buf, grn_obj *obj)
   } else {
     grn_id id;
 
-  id = GRN_RECORD_VALUE(obj);
-  grn_text_lltoa(ctx, buf, id);
+    id = GRN_RECORD_VALUE(obj);
+    grn_text_lltoa(ctx, buf, id);
+
+    if (table && grn_table_at(ctx, table, id)) {
+      if (table->header.type != GRN_TABLE_NO_KEY) {
+        grn_obj key;
+        GRN_TEXT_PUTS(ctx, buf, " key:");
+        GRN_OBJ_INIT(&key, GRN_BULK, 0, table->header.domain);
+        grn_table_get_key2(ctx, table, id, &key);
+        grn_inspect(ctx, buf, &key);
+        GRN_OBJ_FIN(ctx, &key);
+      }
+
+      if (with_columns) {
+        grn_hash *columns;
+
+        columns = grn_hash_create(ctx,
+                                  NULL,
+                                  sizeof(grn_id),
+                                  0,
+                                  GRN_OBJ_TABLE_HASH_KEY|GRN_HASH_TINY);
+        if (columns) {
+          if (grn_table_columns(ctx, table, "", 0, (grn_obj *)columns)) {
+            grn_obj column_value;
+
+            GRN_VOID_INIT(&column_value);
+            GRN_HASH_EACH_BEGIN(ctx, columns, cursor, id) {
+              void *key;
+              grn_id column_id;
+              grn_obj *column;
+
+              grn_hash_cursor_get_key(ctx, cursor, &key);
+              column_id = *((grn_id *)key);
+              column = grn_ctx_at(ctx, column_id);
+              if (!column) {
+                continue;
+              }
 
-  if (table && grn_table_at(ctx, table, id)) {
-    if (table->header.type != GRN_TABLE_NO_KEY) {
-      grn_obj key;
-      GRN_TEXT_PUTS(ctx, buf, " key:");
-      GRN_OBJ_INIT(&key, GRN_BULK, 0, table->header.domain);
-      grn_table_get_key2(ctx, table, id, &key);
-      grn_inspect(ctx, buf, &key);
-      GRN_OBJ_FIN(ctx, &key);
-    }
-    if ((cols = grn_hash_create(ctx, NULL, sizeof(grn_id), 0,
-                                GRN_OBJ_TABLE_HASH_KEY|GRN_HASH_TINY))) {
-      if (grn_table_columns(ctx, table, "", 0, (grn_obj *)cols)) {
-        grn_id *key;
-        GRN_HASH_EACH(ctx, cols, column_id, &key, NULL, NULL, {
-            grn_obj *col = grn_ctx_at(ctx, *key);
-            if (col) {
-              grn_obj value;
-              GRN_TEXT_INIT(&value, 0);
               GRN_TEXT_PUTS(ctx, buf, " ");
-              grn_column_name_(ctx, col, buf);
+              grn_column_name_(ctx, column, buf);
               GRN_TEXT_PUTS(ctx, buf, ":");
-              grn_obj_get_value(ctx, col, id, &value);
-              grn_inspect(ctx, buf, &value);
-              GRN_OBJ_FIN(ctx, &value);
-            }
-          });
+              GRN_BULK_REWIND(&column_value);
+              grn_obj_get_value(ctx, column, id, &column_value);
+              grn_inspect(ctx, buf, &column_value);
+            } GRN_HASH_EACH_END(ctx, cursor);
+            GRN_OBJ_FIN(ctx, &column_value);
+          }
+          grn_hash_close(ctx, columns);
+        }
       }
-      grn_hash_close(ctx, cols);
+    } else {
+      GRN_TEXT_PUTS(ctx, buf, "(nonexistent)");
     }
-  } else {
-    GRN_TEXT_PUTS(ctx, buf, "(nonexistent)");
-  }
   }
 
   GRN_TEXT_PUTS(ctx, buf, ">");
@@ -1287,8 +1307,23 @@ grn_record_inspect(grn_ctx *ctx, grn_obj *buf, grn_obj *obj)
   return GRN_SUCCESS;
 }
 
+grn_rc
+grn_record_inspect_without_columns(grn_ctx *ctx, grn_obj *buf, grn_obj *obj)
+{
+  return grn_record_inspect_common(ctx, buf, obj, GRN_FALSE);
+}
+
+static grn_rc
+grn_record_inspect(grn_ctx *ctx, grn_obj *buf, grn_obj *obj)
+{
+  return grn_record_inspect_common(ctx, buf, obj, GRN_TRUE);
+}
+
 static grn_rc
-grn_uvector_record_inspect(grn_ctx *ctx, grn_obj *buf, grn_obj *obj)
+grn_uvector_record_inspect_common(grn_ctx *ctx,
+                                  grn_obj *buf,
+                                  grn_obj *obj,
+                                  grn_bool with_columns)
 {
   unsigned int i, n = 0;
   grn_obj record;
@@ -1307,7 +1342,7 @@ grn_uvector_record_inspect(grn_ctx *ctx, grn_obj *buf, grn_obj *obj)
     id = grn_uvector_get_element(ctx, obj, i, &weight);
     GRN_TEXT_PUTS(ctx, buf, "#<element record:");
     GRN_RECORD_SET(ctx, &record, id);
-    grn_inspect(ctx, buf, &record);
+    grn_record_inspect_common(ctx, buf, &record, with_columns);
     grn_text_printf(ctx, buf, ", weight:%u>", weight);
   }
   GRN_TEXT_PUTS(ctx, buf, "]");
@@ -1316,6 +1351,22 @@ grn_uvector_record_inspect(grn_ctx *ctx, grn_obj *buf, grn_obj *obj)
   return GRN_SUCCESS;
 }
 
+grn_rc
+grn_uvector_record_inspect_without_columns(grn_ctx *ctx,
+                                           grn_obj *buf,
+                                           grn_obj *obj)
+{
+  return grn_uvector_record_inspect_common(ctx, buf, obj, GRN_FALSE);
+}
+
+static grn_rc
+grn_uvector_record_inspect(grn_ctx *ctx,
+                           grn_obj *buf,
+                           grn_obj *obj)
+{
+  return grn_uvector_record_inspect_common(ctx, buf, obj, GRN_TRUE);
+}
+
 grn_obj *
 grn_inspect(grn_ctx *ctx, grn_obj *buffer, grn_obj *obj)
 {
-------------- next part --------------
HTML����������������������������...
URL: https://lists.osdn.me/mailman/archives/groonga-commit/attachments/20180215/206375ce/attachment-0001.htm 



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