[Groonga-commit] groonga/fluent-plugin-droonga [master] Extract job queue related codes as classes

Back to archive index

Kouhei Sutou null+****@clear*****
Thu Apr 4 15:46:59 JST 2013


Kouhei Sutou	2013-04-04 15:46:59 +0900 (Thu, 04 Apr 2013)

  New Revision: 9a1d91d4341e9f6c9436eb79fce3c2b300598353
  https://github.com/groonga/fluent-plugin-droonga/commit/9a1d91d4341e9f6c9436eb79fce3c2b300598353

  Message:
    Extract job queue related codes as classes

  Added files:
    lib/droonga/job_queue.rb
    lib/droonga/job_queue_schema.rb
  Modified files:
    lib/fluent/plugin/out_droonga.rb

  Added: lib/droonga/job_queue.rb (+29 -0) 100644
===================================================================
--- /dev/null
+++ lib/droonga/job_queue.rb    2013-04-04 15:46:59 +0900 (4ac60c2)
@@ -0,0 +1,29 @@
+# -*- coding: utf-8 -*-
+#
+# Copyright (C) 2013 droonga project
+#
+# 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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+
+require "droonga/job_queue_schema"
+
+module Droonga
+  class JobQueue
+    class << self
+      def ensure_schema(database_path, queue_name)
+        schema = JobQueueSchema.new(database_path, queue_name)
+        schema.ensure_created
+      end
+    end
+  end
+end

  Added: lib/droonga/job_queue_schema.rb (+64 -0) 100644
===================================================================
--- /dev/null
+++ lib/droonga/job_queue_schema.rb    2013-04-04 15:46:59 +0900 (dd28dc4)
@@ -0,0 +1,64 @@
+# -*- coding: utf-8 -*-
+#
+# Copyright (C) 2013 droonga project
+#
+# 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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+
+require "fileutils"
+
+require "groonga"
+
+module Droonga
+  class JobQueueSchema
+    def initialize(database_path, queue_name)
+      @database_path = database_path
+      @queue_name = queue_name
+    end
+
+    def ensure_created
+      ensure_database
+      ensure_queue
+    end
+
+    private
+    def ensure_database
+      return if File.exist?(@database_path)
+      FileUtils.mkdir_p(File.dirname(@database_path))
+      create_context do |context|
+        context.create_database(@database_path) do
+        end
+      end
+    end
+
+    def ensure_queue
+      create_context do |context|
+        context.open_database(@database_path) do
+          Groonga::Schema.define(:context => context) do |schema|
+            schema.create_table(@queue_name, :type => :array) do
+            end
+          end
+        end
+      end
+    end
+
+    def create_context
+      context = Groonga::Context.new
+      begin
+        yield(context)
+      ensure
+        context.close
+      end
+    end
+  end
+end

  Modified: lib/fluent/plugin/out_droonga.rb (+2 -32)
===================================================================
--- lib/fluent/plugin/out_droonga.rb    2013-04-04 15:31:18 +0900 (4a7d328)
+++ lib/fluent/plugin/out_droonga.rb    2013-04-04 15:46:59 +0900 (0a9b6bf)
@@ -15,7 +15,7 @@
 # License along with this library; if not, write to the Free Software
 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
-require "fileutils"
+require "droonga/job_queue"
 require "droonga/worker"
 require "droonga/plugin"
 
@@ -32,8 +32,7 @@ module Fluent
 
     def configure(conf)
       super
-      ensure_database
-      ensure_queue
+      Droonga::JobQueue.ensure_schema(@database, @queue_name)
       load_handlers
     end
 
@@ -80,35 +79,6 @@ module Fluent
     end
 
     private
-    def ensure_database
-      return if File.exist?(@database)
-      FileUtils.mkdir_p(File.dirname(@database))
-      create_context do |context|
-        context.create_database(@database) do
-        end
-      end
-    end
-
-    def ensure_queue
-      create_context do |context|
-        context.open_database(@database) do
-          Groonga::Schema.define(:context => context) do |schema|
-            schema.create_table(@queue_name, :type => :array) do
-            end
-          end
-        end
-      end
-    end
-
-    def create_context
-      context = Groonga::Context.new
-      begin
-        yield(context)
-      ensure
-        context.close
-      end
-    end
-
     def load_handlers
       @handlers.each do |handler_name|
         plugin = Droonga::Plugin.new("handler", handler_name)
-------------- next part --------------
HTML����������������������������...
Download 



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