Kouhei Sutou
null+****@clear*****
Wed Feb 3 18:04:31 JST 2016
Kouhei Sutou 2016-02-03 18:04:31 +0900 (Wed, 03 Feb 2016) New Revision: f5c5bc1ef88bb001c2d37dd869bb0ffac7b0cbb4 https://github.com/groonga/groonga/commit/f5c5bc1ef88bb001c2d37dd869bb0ffac7b0cbb4 Message: inspect: support GRN_TABLE_HASH_KEY Added files: lib/proc/proc_inspect.c test/command/suite/inspect/table_hash_key.expected test/command/suite/inspect/table_hash_key.test Added: lib/proc/proc_inspect.c (+197 -0) 100644 =================================================================== --- /dev/null +++ lib/proc/proc_inspect.c 2016-02-03 18:04:31 +0900 (807488b) @@ -0,0 +1,197 @@ +/* -*- c-basic-offset: 2 -*- */ +/* + Copyright(C) 2016 Brazil + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License version 2.1 as published by the Free Software Foundation. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with this library; if not, write to the Free Software + Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA +*/ + +#include "grn_pat.h" + +#include "grn_proc.h" + +#include <groonga/plugin.h> + +static void +command_inspect_obj_name(grn_ctx *ctx, grn_obj *obj) +{ + char name[GRN_TABLE_MAX_KEY_SIZE]; + int name_size; + + name_size = grn_obj_name(ctx, obj, name, GRN_TABLE_MAX_KEY_SIZE); + grn_ctx_output_str(ctx, name, name_size); +} + +static void +command_inspect_obj_type(grn_ctx *ctx, uint8_t type) +{ + grn_ctx_output_map_open(ctx, "type", 2); + { + grn_ctx_output_cstr(ctx, "id"); + grn_ctx_output_uint64(ctx, type); + grn_ctx_output_cstr(ctx, "name"); + grn_ctx_output_cstr(ctx, grn_obj_type_to_string(type)); + } + grn_ctx_output_map_close(ctx); +} + +static void +command_inspect_type(grn_ctx *ctx, grn_obj *type) +{ + if (!type) { + grn_ctx_output_null(ctx); + } + + grn_ctx_output_map_open(ctx, "type", 4); + { + grn_ctx_output_cstr(ctx, "id"); + grn_ctx_output_uint64(ctx, grn_obj_id(ctx, type)); + grn_ctx_output_cstr(ctx, "name"); + command_inspect_obj_name(ctx, type); + grn_ctx_output_cstr(ctx, "type"); + command_inspect_obj_type(ctx, type->header.type); + grn_ctx_output_cstr(ctx, "size"); + if (type->header.type == GRN_TYPE) { + grn_ctx_output_uint64(ctx, grn_type_size(ctx, type)); + } else { + grn_ctx_output_uint64(ctx, sizeof(grn_id)); + } + } + grn_ctx_output_map_close(ctx); +} + +static void +command_inspect_table_hash_key_key(grn_ctx *ctx, grn_hash *hash) +{ + grn_ctx_output_map_open(ctx, "key", 3); + { + grn_ctx_output_cstr(ctx, "type"); + command_inspect_type(ctx, grn_ctx_at(ctx, hash->obj.header.domain)); + grn_ctx_output_cstr(ctx, "total_size"); + grn_ctx_output_uint64(ctx, grn_hash_total_key_size(ctx, hash)); + grn_ctx_output_cstr(ctx, "max_total_size"); + grn_ctx_output_uint64(ctx, grn_hash_max_total_key_size(ctx, hash)); + } + grn_ctx_output_map_close(ctx); +} + +static void +command_inspect_table_hash_key(grn_ctx *ctx, grn_obj *obj) +{ + grn_hash *hash = (grn_hash *)obj; + + grn_ctx_output_map_open(ctx, "object", 3); + { + grn_ctx_output_cstr(ctx, "name"); + command_inspect_obj_name(ctx, obj); + grn_ctx_output_cstr(ctx, "type"); + command_inspect_obj_type(ctx, obj->header.type); + grn_ctx_output_cstr(ctx, "key"); + command_inspect_table_hash_key_key(ctx, hash); + } + grn_ctx_output_map_close(ctx); +} + +static void +command_inspect_table_pat_key_key(grn_ctx *ctx, grn_pat *pat) +{ + grn_ctx_output_map_open(ctx, "key", 3); + { + grn_ctx_output_cstr(ctx, "type"); + command_inspect_type(ctx, grn_ctx_at(ctx, pat->obj.header.domain)); + grn_ctx_output_cstr(ctx, "total_size"); + grn_ctx_output_uint64(ctx, grn_pat_total_key_size(ctx, pat)); + grn_ctx_output_cstr(ctx, "max_total_size"); + grn_ctx_output_uint64(ctx, GRN_PAT_MAX_TOTAL_KEY_SIZE); + } + grn_ctx_output_map_close(ctx); +} + +static void +command_inspect_table_pat_key(grn_ctx *ctx, grn_obj *obj) +{ + grn_pat *pat = (grn_pat *)obj; + + grn_ctx_output_map_open(ctx, "object", 3); + { + grn_ctx_output_cstr(ctx, "name"); + command_inspect_obj_name(ctx, obj); + grn_ctx_output_cstr(ctx, "type"); + command_inspect_obj_type(ctx, obj->header.type); + grn_ctx_output_cstr(ctx, "key"); + command_inspect_table_pat_key_key(ctx, pat); + } + grn_ctx_output_map_close(ctx); +} + +static grn_obj * +command_inspect(grn_ctx *ctx, + int nargs, + grn_obj **args, + grn_user_data *user_data) +{ + grn_obj *target_name; + grn_obj *target; + + target_name = grn_plugin_proc_get_var(ctx, user_data, "target_name", -1); + if (GRN_TEXT_LEN(target_name) == 0) { + target = grn_ctx_db(ctx); + } else { + target = grn_ctx_get(ctx, + GRN_TEXT_VALUE(target_name), + GRN_TEXT_LEN(target_name)); + if (!target) { + GRN_PLUGIN_ERROR(ctx, + GRN_INVALID_ARGUMENT, + "[inspect] nonexistent target: <%.*s>", + (int)GRN_TEXT_LEN(target_name), + GRN_TEXT_VALUE(target_name)); + grn_ctx_output_null(ctx); + return NULL; + } + } + + switch (target->header.type) { + case GRN_TABLE_HASH_KEY : + command_inspect_table_hash_key(ctx, target); + break; + case GRN_TABLE_PAT_KEY : + command_inspect_table_pat_key(ctx, target); + break; + default : + { + GRN_PLUGIN_ERROR(ctx, + GRN_FUNCTION_NOT_IMPLEMENTED, + "[inspect] unsupported type: <%s>(%#x)", + grn_obj_type_to_string(target->header.type), + target->header.type); + grn_ctx_output_null(ctx); + break; + } + } + + return NULL; +} + +void +grn_proc_init_inspect(grn_ctx *ctx) +{ + grn_expr_var vars[1]; + + grn_plugin_expr_var_init(ctx, &(vars[0]), "target_name", -1); + grn_plugin_command_create(ctx, + "inspect", -1, + command_inspect, + 1, + vars); +} Added: test/command/suite/inspect/table_hash_key.expected (+36 -0) 100644 =================================================================== --- /dev/null +++ test/command/suite/inspect/table_hash_key.expected 2016-02-03 18:04:31 +0900 (a858863) @@ -0,0 +1,36 @@ +table_create Users TABLE_HASH_KEY ShortText +[[0,0.0,0.0],true] +load --table Users +[ +{"_key": "alice"}, +{"_key": "bob"} +] +[[0,0.0,0.0],2] +inspect Users +[ + [ + 0, + 0.0, + 0.0 + ], + { + "name": "Users", + "type": { + "id": 48, + "name": "table:hash_key" + }, + "key": { + "type": { + "id": 14, + "name": "ShortText", + "type": { + "id": 32, + "name": "type" + }, + "size": 4096 + }, + "total_size": 5, + "max_total_size": 4294967295 + } + } +] Added: test/command/suite/inspect/table_hash_key.test (+9 -0) 100644 =================================================================== --- /dev/null +++ test/command/suite/inspect/table_hash_key.test 2016-02-03 18:04:31 +0900 (c539ab8) @@ -0,0 +1,9 @@ +table_create Users TABLE_HASH_KEY ShortText + +load --table Users +[ +{"_key": "alice"}, +{"_key": "bob"} +] + +inspect Users -------------- next part -------------- HTML����������������������������... Download