Kouhei Sutou
null+****@clear*****
Mon Jul 27 18:38:26 JST 2015
Kouhei Sutou 2015-07-27 18:38:26 +0900 (Mon, 27 Jul 2015) New Revision: f6bbd31d5f6e82204c98068478a40fb32b901576 https://github.com/groonga/groonga/commit/f6bbd31d5f6e82204c98068478a40fb32b901576 Message: mrb: support query log Added files: lib/mrb/mrb_query_logger.c lib/mrb/mrb_query_logger.h lib/mrb/scripts/query_logger.rb lib/mrb/scripts/query_logger/Makefile.am lib/mrb/scripts/query_logger/flag.rb lib/mrb/scripts/query_logger/sources.am Modified files: configure.ac lib/CMakeLists.txt lib/ctx_impl_mrb.c lib/mrb/scripts/Makefile.am lib/mrb/scripts/context.rb lib/mrb/scripts/sources.am lib/mrb/sources.am Modified: configure.ac (+1 -0) =================================================================== --- configure.ac 2015-07-27 18:04:46 +0900 (92d1eb8) +++ configure.ac 2015-07-27 18:38:26 +0900 (7a3888a) @@ -240,6 +240,7 @@ AC_CONFIG_FILES([ lib/mrb/scripts/context/Makefile lib/mrb/scripts/initialize/Makefile lib/mrb/scripts/logger/Makefile + lib/mrb/scripts/query_logger/Makefile include/Makefile include/groonga/Makefile plugins/Makefile Modified: lib/CMakeLists.txt (+8 -0) =================================================================== --- lib/CMakeLists.txt 2015-07-27 18:04:46 +0900 (45b2c92) +++ lib/CMakeLists.txt 2015-07-27 18:38:26 +0900 (ac559ba) @@ -124,4 +124,12 @@ if(GRN_WITH_MRUBY) install( FILES ${LOGGER_RUBY_SCRIPTS} DESTINATION "${GRN_RELATIVE_RUBY_SCRIPTS_DIR}/logger") + + read_file_list(${CMAKE_CURRENT_SOURCE_DIR}/mrb/scripts/query_logger/sources.am + QUERY_LOGGER_RUBY_SCRIPTS) + string(REGEX REPLACE "([^;]+)" "mrb/scripts/query_logger/\\1" + QUERY_LOGGER_RUBY_SCRIPTS "${QUERY_LOGGER_RUBY_SCRIPTS}") + install( + FILES ${QUERY_LOGGER_RUBY_SCRIPTS} + DESTINATION "${GRN_RELATIVE_RUBY_SCRIPTS_DIR}/query_logger") endif() Modified: lib/ctx_impl_mrb.c (+2 -0) =================================================================== --- lib/ctx_impl_mrb.c 2015-07-27 18:04:46 +0900 (5db89a3) +++ lib/ctx_impl_mrb.c 2015-07-27 18:38:26 +0900 (d26684d) @@ -31,6 +31,7 @@ # include "mrb/mrb_command_version.h" # include "mrb/mrb_ctx.h" # include "mrb/mrb_logger.h" +# include "mrb/mrb_query_logger.h" # include "mrb/mrb_void.h" # include "mrb/mrb_bulk.h" # include "mrb/mrb_cache.h" @@ -140,6 +141,7 @@ grn_ctx_impl_mrb_init_bindings(grn_ctx *ctx) grn_mrb_command_version_init(ctx); grn_mrb_ctx_init(ctx); grn_mrb_logger_init(ctx); + grn_mrb_query_logger_init(ctx); grn_mrb_void_init(ctx); grn_mrb_bulk_init(ctx); grn_mrb_cache_init(ctx); Added: lib/mrb/mrb_query_logger.c (+75 -0) 100644 =================================================================== --- /dev/null +++ lib/mrb/mrb_query_logger.c 2015-07-27 18:38:26 +0900 (5e4f4fd) @@ -0,0 +1,75 @@ +/* -*- c-basic-offset: 2 -*- */ +/* + Copyright(C) 2015 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_ctx_impl.h" + +#ifdef GRN_WITH_MRUBY +#include <mruby.h> +#include <mruby/class.h> +#include <mruby/data.h> +#include <mruby/variable.h> +#include <mruby/string.h> + +#include "../grn_mrb.h" +#include "mrb_query_logger.h" + +static mrb_value +query_logger_need_log_p(mrb_state *mrb, mrb_value self) +{ + grn_ctx *ctx = (grn_ctx *)mrb->ud; + mrb_int flag; + + mrb_get_args(mrb, "i", &flag); + + return mrb_bool_value(grn_query_logger_pass(ctx, flag)); +} + +static mrb_value +query_logger_log_raw(mrb_state *mrb, mrb_value self) +{ + grn_ctx *ctx = (grn_ctx *)mrb->ud; + mrb_int flag; + char *mark; + char *message; + mrb_int message_size; + + mrb_get_args(mrb, "izs", &flag, &mark, &message, &message_size); + grn_query_logger_put(ctx, flag, mark, "%.*s", message_size, message); + + return self; +} + +void +grn_mrb_query_logger_init(grn_ctx *ctx) +{ + grn_mrb_data *data = &(ctx->impl->mrb); + mrb_state *mrb = data->state; + struct RClass *module = data->module; + struct RClass *klass; + + klass = mrb_define_class_under(mrb, module, "QueryLogger", mrb->object_class); + + mrb_define_method(mrb, klass, "need_log?", query_logger_need_log_p, + MRB_ARGS_REQ(1)); + mrb_define_method(mrb, klass, "log_raw", query_logger_log_raw, + MRB_ARGS_REQ(3)); + + grn_mrb_load(ctx, "query_logger/flag.rb"); + grn_mrb_load(ctx, "query_logger.rb"); +} +#endif Added: lib/mrb/mrb_query_logger.h (+34 -0) 100644 =================================================================== --- /dev/null +++ lib/mrb/mrb_query_logger.h 2015-07-27 18:38:26 +0900 (41a7e28) @@ -0,0 +1,34 @@ +/* -*- c-basic-offset: 2 -*- */ +/* + Copyright(C) 2015 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 +*/ + +#ifndef GRN_MRB_QUERY_LOGGER_H +#define GRN_MRB_QUERY_LOGGER_H + +#include "../grn_ctx.h" + +#ifdef __cplusplus +extern "C" { +#endif + +void grn_mrb_query_logger_init(grn_ctx *ctx); + +#ifdef __cplusplus +} +#endif + +#endif /* GRN_MRB_QUERY_LOGGER_H */ Modified: lib/mrb/scripts/Makefile.am (+2 -1) =================================================================== --- lib/mrb/scripts/Makefile.am 2015-07-27 18:04:46 +0900 (9b6acf8) +++ lib/mrb/scripts/Makefile.am 2015-07-27 18:38:26 +0900 (890dcf6) @@ -2,7 +2,8 @@ SUBDIRS = \ command_line \ context \ initialize \ - logger + logger \ + query_logger include sources.am Modified: lib/mrb/scripts/context.rb (+4 -0) =================================================================== --- lib/mrb/scripts/context.rb 2015-07-27 18:04:46 +0900 (4d8419f) +++ lib/mrb/scripts/context.rb 2015-07-27 18:38:26 +0900 (3d1282d) @@ -16,6 +16,10 @@ module Groonga @logger ||= Logger.new end + def query_logger + @query_logger ||= QueryLogger.new + end + def writer @writer ||= Writer.new end Added: lib/mrb/scripts/query_logger.rb (+9 -0) 100644 =================================================================== --- /dev/null +++ lib/mrb/scripts/query_logger.rb 2015-07-27 18:38:26 +0900 (386ec0e) @@ -0,0 +1,9 @@ +module Groonga + class QueryLogger + def log(flag, mark, message) + flag = Flag.find(flag) if flag.is_a?(Symbol) + flag = flag.to_i if flag.is_a?(Flag) + log_raw(flag, mark, message) + end + end +end Added: lib/mrb/scripts/query_logger/Makefile.am (+9 -0) 100644 =================================================================== --- /dev/null +++ lib/mrb/scripts/query_logger/Makefile.am 2015-07-27 18:38:26 +0900 (14b4f61) @@ -0,0 +1,9 @@ +include sources.am + +EXTRA_DIST = \ + $(RUBY_SCRIPT_FILES) + +if WITH_MRUBY +ruby_scripts_query_loggerdir = $(ruby_scriptsdir)/query_logger +ruby_scripts_query_logger_DATA = $(RUBY_SCRIPT_FILES) +endif Added: lib/mrb/scripts/query_logger/flag.rb (+37 -0) 100644 =================================================================== --- /dev/null +++ lib/mrb/scripts/query_logger/flag.rb 2015-07-27 18:38:26 +0900 (1e4c3dd) @@ -0,0 +1,37 @@ +module Groonga + class QueryLogger + class Flag + @@names = {} + def self.find(name) + @@names[name] + end + + attr_reader :name + def initialize(name, flag) + @@names[name] = self + @name = name + @flag = flag + end + + def to_i + @flag + end + + NONE = new(:none, 0x00) + COMMAND = new(:command, 0x01 << 0) + RESULT_CODE = new(:result_code, 0x01 << 1) + DESTINATION = new(:destination, 0x01 << 2) + CACHE = new(:cache, 0x01 << 3) + SIZE = new(:size, 0x01 << 4) + SCORE = new(:score, 0x01 << 5) + + all_flags = COMMAND.to_i | + RESULT_CODE.to_i | + DESTINATION.to_i | + CACHE.to_i | + SIZE.to_i | + SCORE.to_i + ALL = new(:all, all_flags) + end + end +end Added: lib/mrb/scripts/query_logger/sources.am (+2 -0) 100644 =================================================================== --- /dev/null +++ lib/mrb/scripts/query_logger/sources.am 2015-07-27 18:38:26 +0900 (44871a8) @@ -0,0 +1,2 @@ +RUBY_SCRIPT_FILES = \ + flag.rb Modified: lib/mrb/scripts/sources.am (+1 -0) =================================================================== --- lib/mrb/scripts/sources.am 2015-07-27 18:04:46 +0900 (81cc925) +++ lib/mrb/scripts/sources.am 2015-07-27 18:38:26 +0900 (afd5b94) @@ -14,6 +14,7 @@ RUBY_SCRIPT_FILES = \ object.rb \ operator.rb \ plugin_loader.rb \ + query_logger.rb \ record.rb \ require.rb \ scan_info.rb \ Modified: lib/mrb/sources.am (+2 -0) =================================================================== --- lib/mrb/sources.am 2015-07-27 18:04:46 +0900 (9433781) +++ lib/mrb/sources.am 2015-07-27 18:38:26 +0900 (2b494f6) @@ -53,6 +53,8 @@ libgrnmrb_la_SOURCES = \ mrb_patricia_trie.h \ mrb_procedure.c \ mrb_procedure.h \ + mrb_query_logger.c \ + mrb_query_logger.h \ mrb_record.c \ mrb_record.h \ mrb_table.c \ -------------- next part -------------- HTML����������������������������...Download