[Groonga-commit] groonga/groonga at 25cc3bc [master] grndb: support detecting database wasn't closed successfully case

アーカイブの一覧に戻る

Kouhei Sutou null+****@clear*****
Wed May 11 15:23:46 JST 2016


Kouhei Sutou	2016-05-11 15:23:46 +0900 (Wed, 11 May 2016)

  New Revision: 25cc3bc091bef2ee2d24be7942e940ceca704fca
  https://github.com/groonga/groonga/commit/25cc3bc091bef2ee2d24be7942e940ceca704fca

  Message:
    grndb: support detecting database wasn't closed successfully case

  Modified files:
    lib/mrb/scripts/command_line/grndb.rb
    test/command_line/suite/grndb/test_check.rb

  Modified: lib/mrb/scripts/command_line/grndb.rb (+40 -8)
===================================================================
--- lib/mrb/scripts/command_line/grndb.rb    2016-05-11 14:43:50 +0900 (890b8ea)
+++ lib/mrb/scripts/command_line/grndb.rb    2016-05-11 15:23:46 +0900 (212b02e)
@@ -101,14 +101,6 @@ module Groonga
       end
 
       def check(database, options, arguments)
-        if database.locked?
-          message =
-            "Database is locked. " +
-            "It may be broken. " +
-            "Re-create the database."
-          failed(message)
-        end
-
         checker = Checker.new
         checker.program_path = @program_path
         checker.database_path = @database_path
@@ -117,6 +109,8 @@ module Groonga
           failed(message)
         end
 
+        checker.check_database
+
         target_name = options[:target]
         if target_name
           checker.check_one(target_name)
@@ -136,6 +130,11 @@ module Groonga
           @checked = {}
         end
 
+        def check_database
+          check_database_locked
+          check_database_dirty
+        end
+
         def check_one(target_name)
           target = @context[target_name]
           if target.nil?
@@ -172,6 +171,39 @@ module Groonga
         end
 
         private
+        def check_database_locked
+          return unles****@datab*****?
+
+          message =
+            "Database is locked. " +
+            "It may be broken. " +
+            "Re-create the database."
+          failed(message)
+        end
+
+        def check_database_dirty
+          return unles****@datab*****?
+
+          last_modified =****@datab*****_modified
+          if File.stat(@database.path).mtime > last_modified
+            return
+          end
+
+          open_database_cursor do |cursor|
+            cursor.each do |id|
+              next if ID.builtin?(id)
+              path = "%s.%07x" % [@database.path, id]
+              return if File.stat(path).mtime > last_modified
+            end
+          end
+
+          message =
+            "Database wasn't closed successfully. " +
+            "It may be broken. " +
+            "Re-create the database."
+          failed(message)
+        end
+
         def check_object(object)
           return if****@check*****?(object.id)
           @checked[object.id] = true

  Modified: test/command_line/suite/grndb/test_check.rb (+28 -0)
===================================================================
--- test/command_line/suite/grndb/test_check.rb    2016-05-11 14:43:50 +0900 (73b4624)
+++ test/command_line/suite/grndb/test_check.rb    2016-05-11 15:23:46 +0900 (2a2445d)
@@ -12,6 +12,34 @@ Database is locked. It may be broken. Re-create the database.
     MESSAGE
   end
 
+  def test_dirty_database
+    groonga("table_create", "Users", "TABLE_HASH_KEY", "ShortText")
+    IO.pipe do |to_groonga_read, to_groonga_write|
+      IO.pipe do |from_groonga_read, from_groonga_write|
+        pid = spawn("groonga", @database_path.to_s,
+                    :in => to_groonga_read,
+                    :out => from_groonga_write)
+        to_groonga_read.close
+        from_groonga_write.close
+        to_groonga_write.puts(<<-COMMAND)
+load --table Users
+[
+{"_key": "Alice"}
+]
+        COMMAND
+        to_groonga_write.flush
+        from_groonga_read.gets
+        Process.kill(:KILL, pid)
+      end
+    end
+    error = assert_raise(CommandRunner::Error) do
+      grndb("check")
+    end
+    assert_equal(<<-MESSAGE, error.error_output)
+Database wasn't closed successfully. It may be broken. Re-create the database.
+    MESSAGE
+  end
+
   def test_nonexistent_table
     groonga("table_create", "Users", "TABLE_HASH_KEY", "ShortText")
     _id, _name, path, *_ = JSON.parse(groonga("table_list").output)[1][1]
-------------- next part --------------
HTML����������������������������...
ダウンロード 



More information about the Groonga-commit mailing list
アーカイブの一覧に戻る