[Groonga-commit] groonga/groonga at dff5f4b [master] groonga: add --recover option

Back to archive index

Kouhei Sutou null+****@clear*****
Wed Dec 10 22:55:39 JST 2014


Kouhei Sutou	2014-12-10 22:55:39 +0900 (Wed, 10 Dec 2014)

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

  Message:
    groonga: add --recover option
    
    This feature is experimental.
    
    You should use this feature carefully. You should this feature when
    other process or thread doesn't open the target database. You may break
    existing database if other process or thread open the same database.

  Modified files:
    src/groonga.c

  Modified: src/groonga.c (+37 -1)
===================================================================
--- src/groonga.c    2014-12-10 22:40:17 +0900 (9a79336)
+++ src/groonga.c    2014-12-10 22:55:39 +0900 (e7094b0)
@@ -88,6 +88,7 @@ static int port = DEFAULT_GQTP_PORT;
 static int batchmode;
 static int number_of_lines = 0;
 static int newdb;
+static grn_bool is_recover_db = GRN_FALSE;
 static grn_bool is_daemon_mode = GRN_FALSE;
 static int (*do_client)(int argc, char **argv);
 static int (*do_server)(char *path);
@@ -313,6 +314,17 @@ do_alone(int argc, char **argv)
   db = (newdb || !path) ? grn_db_create(ctx, path, NULL) : grn_db_open(ctx, path);
   if (db) {
     grn_obj command;
+    if (is_recover_db) {
+      grn_rc rc;
+      rc = grn_db_recover(ctx, db);
+      if (rc != GRN_SUCCESS) {
+        fprintf(stderr, "Failed to recover database <%s>: %s",
+                path, ctx->errbuf);
+        exit_code = grn_rc_to_exit_code(ctx->rc);
+        grn_obj_close(ctx, db);
+        goto exit;
+      }
+    }
     GRN_TEXT_INIT(&command, 0);
     GRN_CTX_USER_DATA(ctx)->ptr = &command;
     grn_ctx_recv_handler_set(ctx, s_output, output);
@@ -336,6 +348,7 @@ do_alone(int argc, char **argv)
   } else {
     fprintf(stderr, "db open failed (%s): %s\n", path, ctx->errbuf);
   }
+exit :
   grn_ctx_fin(ctx);
   return exit_code;
 }
@@ -625,7 +638,17 @@ start_service(grn_ctx *ctx, const char *db_path,
     grn_obj *db;
     db = (newdb || !db_path) ? grn_db_create(ctx, db_path, NULL) : grn_db_open(ctx, db_path);
     if (db) {
-      exit_code = run_server(ctx, db, &ev, dispatcher, handler);
+      grn_rc rc = GRN_SUCCESS;
+      if (is_recover_db) {
+        rc = grn_db_recover(ctx, db);
+      }
+      if (rc != GRN_SUCCESS) {
+        fprintf(stderr, "Failed to recover database <%s>: %s",
+                db_path, ctx->errbuf);
+        exit_code = grn_rc_to_exit_code(ctx->rc);
+      } else {
+        exit_code = run_server(ctx, db, &ev, dispatcher, handler);
+      }
       grn_obj_close(ctx, db);
     } else {
       fprintf(stderr, "db open failed (%s)\n", db_path);
@@ -1912,6 +1935,7 @@ enum {
 
 #define MODE_MASK   0x007f
 #define MODE_NEW_DB 0x0100
+#define MODE_RECOVER_DB 0x0200
 
 static uint32_t
 get_core_number(void)
@@ -2326,6 +2350,16 @@ show_usage(FILE *output)
           "                       specify encoding for new database\n"
           "                       [none|euc|utf8|sjis|latin1|koi8r] (default: %s)\n"
           "\n"
+          "Database options:\n"
+          "  --recover:           check the existing database.\n"
+          "                       If the database has any problem,\n"
+          "                       this command tries to recover the database.\n"
+          "                       If the problem can't be recover automatically,\n"
+          "                       this command exits with non zero status.\n"
+          "                       CAUTION: Don't open the same database\n"
+          "                       by other process while recovering.\n"
+          "                       (except client mode)\n"
+          "\n"
           "Standalone/client options:\n"
           "      --file <path>:          read commands from specified file\n"
           "      --input-fd <FD>:        read commands from specified file descriptor\n"
@@ -2435,6 +2469,7 @@ main(int argc, char **argv)
     {'\0', "input-fd", NULL, 0, GETOPT_OP_NONE},
     {'\0', "output-fd", NULL, 0, GETOPT_OP_NONE},
     {'\0', "working-directory", NULL, 0, GETOPT_OP_NONE},
+    {'\0', "recover", NULL, MODE_RECOVER_DB, GETOPT_OP_ON},
     {'\0', NULL, NULL, 0, 0}
   };
   opts[0].arg = &port_arg;
@@ -2797,6 +2832,7 @@ main(int argc, char **argv)
   }
 
   newdb = (mode & MODE_NEW_DB);
+  is_recover_db = ((mode & MODE_RECOVER_DB) == MODE_RECOVER_DB);
   switch (mode & MODE_MASK) {
   case mode_alone :
     exit_code = do_alone(argc - i, argv + i);
-------------- next part --------------
HTML����������������������������...
Descargar 



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