Kouhei Sutou
null+****@clear*****
Tue Dec 23 21:37:21 JST 2014
Kouhei Sutou 2014-12-23 21:37:21 +0900 (Tue, 23 Dec 2014) New Revision: c04a1976b2a9503e05cc13dfe578fb8ce7a5e68f https://github.com/groonga/groonga/commit/c04a1976b2a9503e05cc13dfe578fb8ce7a5e68f Message: grndb: implement recover by mruby Modified files: lib/mrb/scripts/command/grndb.rb src/grndb.c Modified: lib/mrb/scripts/command/grndb.rb (+37 -2) =================================================================== --- lib/mrb/scripts/command/grndb.rb 2014-12-23 19:43:04 +0900 (7eed931) +++ lib/mrb/scripts/command/grndb.rb 2014-12-23 21:37:21 +0900 (b9ae2f3) @@ -2,11 +2,46 @@ module Groonga module Command class Grndb def initialize(argv) - @argv = argv + @command = argv[0] + @arguments = argv[1..-1] end def run - p @argv + slop = create_slop + rest = nil + begin + rest = slop.parse(@arguments) + rescue Slop::Error + $stderr.puts($!.message) + return false + end + + if slop.help? + $stdout.puts(slop.help) + return true + end + + if rest.size < 1 + $stderr.puts("database path is missing") + return false + end + + database = Groonga::Database.open(rest[0]) + begin + database.recover + ensure + database.close + end + + true + end + + private + def create_slop + slop = Slop.new + slop.banner = "Usage: #{File.basename(@command)} [options] DB_PATH" + slop.on("-h", "--help", "Display this help message.", :tail => true) + slop end end end Modified: src/grndb.c (+69 -31) =================================================================== --- src/grndb.c 2014-12-23 19:43:04 +0900 (8b305b1) +++ src/grndb.c 2014-12-23 21:37:21 +0900 (4416818) @@ -27,54 +27,92 @@ #include <mruby/array.h> static int +run_command(grn_ctx *ctx, int argc, char **argv) +{ + int exit_code = EXIT_SUCCESS; + grn_mrb_data *data = &(ctx->impl->mrb); + mrb_state *mrb = data->state; + mrb_value mrb_command_module; + mrb_value mrb_grndb_class; + + mrb_command_module = mrb_const_get(mrb, + mrb_obj_value(data->module), + mrb_intern_cstr(mrb, "Command")); + if (mrb->exc) { + goto exit; + } + + mrb_grndb_class = mrb_const_get(mrb, + mrb_command_module, + mrb_intern_cstr(mrb, "Grndb")); + if (mrb->exc) { + goto exit; + } + + { + int i; + mrb_value mrb_argv; + mrb_value mrb_grndb; + mrb_value mrb_result; + + mrb_argv = mrb_ary_new_capa(mrb, argc); + for (i = 0; i < argc; i++) { + mrb_ary_push(mrb, mrb_argv, mrb_str_new_cstr(mrb, argv[i])); + } + mrb_grndb = mrb_funcall(mrb, mrb_grndb_class, "new", 1, mrb_argv); + if (mrb->exc) { + goto exit; + } + + mrb_result = mrb_funcall(mrb, mrb_grndb, "run", 0); + if (mrb->exc) { + goto exit; + } + + if (!mrb_bool(mrb_result)) { + exit_code = EXIT_FAILURE; + } + } + +exit : + if (mrb->exc) { + mrb_print_error(mrb); + exit_code = EXIT_FAILURE; + } + + return exit_code; +} + +static int run(grn_ctx *ctx, int argc, char **argv) { + int exit_code = EXIT_SUCCESS; const char *grndb_rb = "command/grndb.rb"; + grn_mrb_data *data = &(ctx->impl->mrb); + mrb_state *mrb = data->state; + + mrb_gv_set(mrb, mrb_intern_lit(mrb, "$0"), mrb_str_new_cstr(mrb, argv[0])); grn_mrb_load(ctx, grndb_rb); if (ctx->rc != GRN_SUCCESS) { - fprintf(stderr, "Failed to load Ruby script: <%s>: %s", - grndb_rb, ctx->errbuf); - goto exit; + fprintf(stderr, "Failed to load Ruby script: <%s>: %s", + grndb_rb, ctx->errbuf); + goto exit; } - /* TODO: Handle error. */ { - grn_mrb_data *data = &(ctx->impl->mrb); - mrb_state *mrb = data->state; - mrb_value mrb_command_module; - mrb_value mrb_grndb_class; int arena_index; arena_index = mrb_gc_arena_save(mrb); - mrb_command_module = mrb_const_get(mrb, - mrb_obj_value(data->module), - mrb_intern_cstr(mrb, "Command")); - mrb_grndb_class = mrb_const_get(mrb, - mrb_command_module, - mrb_intern_cstr(mrb, "Grndb")); - { - int i; - mrb_value mrb_argv; - mrb_value mrb_grndb; - mrb_value mrb_result; - - mrb_argv = mrb_ary_new_capa(mrb, argc); - for (i = 0; i < argc; i++) { - mrb_ary_push(mrb, mrb_argv, mrb_str_new_cstr(mrb, argv[i])); - } - mrb_grndb = mrb_funcall(mrb, mrb_grndb_class, "new", 1, mrb_argv); - mrb_result = mrb_funcall(mrb, mrb_grndb, "run", 0); - } + exit_code = run_command(ctx, argc, argv); mrb_gc_arena_restore(mrb, arena_index); } exit : - if (ctx->rc == GRN_SUCCESS) { - return EXIT_SUCCESS; - } else { - return EXIT_FAILURE; + if (ctx->rc != GRN_SUCCESS) { + exit_code = EXIT_FAILURE; } + return exit_code; } int -------------- next part -------------- HTML����������������������������...Download