[Groonga-commit] ranguba/groonga-client at 36e0cf7 [master] Support restoring DB for existing server

Back to archive index

Kouhei Sutou null+****@clear*****
Tue Dec 20 16:11:00 JST 2016


Kouhei Sutou	2016-12-20 16:11:00 +0900 (Tue, 20 Dec 2016)

  New Revision: 36e0cf77eca534de8d4df0f49f8907a868d39259
  https://github.com/ranguba/groonga-client/commit/36e0cf77eca534de8d4df0f49f8907a868d39259

  Message:
    Support restoring DB for existing server

  Modified files:
    lib/groonga/client/test/groonga-server-runner.rb

  Modified: lib/groonga/client/test/groonga-server-runner.rb (+58 -5)
===================================================================
--- lib/groonga/client/test/groonga-server-runner.rb    2016-12-20 16:10:21 +0900 (2de2a4a)
+++ lib/groonga/client/test/groonga-server-runner.rb    2016-12-20 16:11:00 +0900 (6dae983)
@@ -17,6 +17,8 @@
 require "rbconfig"
 require "fileutils"
 
+require "groonga/command/parser"
+
 module Groonga
   class Client
     module Test
@@ -32,6 +34,9 @@ module Groonga
         def run
           if groonga_server_running?
             @using_running_server = true
+            @dump = Groonga::Client.open(url: @url) do |client|
+              client.dump.body
+            end
           else
             return if****@groon*****?
             @tmp_dir = create_tmp_dir
@@ -50,11 +55,8 @@ module Groonga
         def stop
           if @using_running_server
             Groonga::Client.open(url: @url) do |client|
-              schema = client.schema
-              schema.tables.each do |name, _|
-                client.delete(table: name,
-                              filter: "true")
-              end
+              remove_all(client)
+              restore(client)
             end
           else
             if @pid
@@ -156,6 +158,57 @@ module Groonga
           Process.kill(:KILL, @pid)
           Process.waitpid(@pid)
         end
+
+        def remove_all(client)
+          schema = client.schema
+          schema.tables.each_value do |table|
+            table.columns.each_value do |column|
+              client.column_remove(:table => table.name,
+                                   :name => column.name)
+            end
+          end
+          schema.tables.each_value do |table|
+            client.table_remove(:name => table.name)
+          end
+          schema.plugins.each_value do |plugin|
+            client.plugin_unregister(:name => plugin.name)
+          end
+        end
+
+        def restore(client)
+          return if****@dump*****?
+
+          parser = Groonga::Command::Parser.new
+
+          parser.on_command do |command|
+            client.execute(command)
+          end
+
+          parser.on_load_columns do |command, columns|
+            command[:columns] ||= columns.join(",")
+          end
+
+          load_values = []
+          parser.on_load_value do |command, value|
+            unless command[:values]
+              load_values << value
+            end
+            command.original_source.clear
+          end
+
+          parser.on_load_complete do |command|
+            unless command[:values]
+              command[:values] = JSON.generate(load_values)
+              load_values.clear
+            end
+            client.execute(command)
+          end
+
+          @dump.each_line do |line|
+            parser << line
+          end
+          parser.finish
+        end
       end
     end
   end
-------------- next part --------------
HTML����������������������������...
Download 



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