Navigation Menu

Skip to content

Commit

Permalink
Add no columns version record inspect
Browse files Browse the repository at this point in the history
It's for compact display case such as inspecting condition in query
log.
  • Loading branch information
kou committed Feb 15, 2018
1 parent df968bf commit b749281
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 35 deletions.
8 changes: 7 additions & 1 deletion lib/grn_util.h
@@ -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
Expand Down Expand Up @@ -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
Expand Down
119 changes: 85 additions & 34 deletions lib/util.c
Expand Up @@ -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:");
Expand All @@ -1245,50 +1247,83 @@ 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, ">");

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;
Expand All @@ -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, "]");
Expand All @@ -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)
{
Expand Down

0 comments on commit b749281

Please sign in to comment.