[Groonga-commit] droonga/fluent-plugin-droonga at f32f54c [master] Add watch schema creator

Back to archive index

Yoji Shidara null+****@clear*****
Tue Nov 5 13:43:19 JST 2013


Yoji Shidara	2013-11-05 13:43:19 +0900 (Tue, 05 Nov 2013)

  New Revision: f32f54c4d19503ec794eb481eb2b4797b6716b28
  https://github.com/droonga/fluent-plugin-droonga/commit/f32f54c4d19503ec794eb481eb2b4797b6716b28

  Message:
    Add watch schema creator

  Added files:
    lib/droonga/watch_schema.rb
    test/test_watch_schema.rb

  Added: lib/droonga/watch_schema.rb (+104 -0) 100644
===================================================================
--- /dev/null
+++ lib/droonga/watch_schema.rb    2013-11-05 13:43:19 +0900 (e40e80d)
@@ -0,0 +1,104 @@
+# -*- 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 WatchSchema
+    def initialize(database_path)
+      @database_path = database_path
+    end
+
+    def ensure_created
+      ensure_database
+      ensure_tables
+      nil
+    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_tables
+      create_context do |context|
+        context.open_database(@database_path) do
+          Groonga::Schema.define(:context => context) do |schema|
+            schema.create_table("Keyword",
+                         :type => :patricia_trie,
+                         :key_type => "ShortText",
+                         :key_normalize => true,
+                         :force => true) do |table|
+                         end
+
+            schema.create_table("Query",
+                         :type => :hash,
+                         :key_type => "ShortText",
+                         :force => true) do |table|
+                         end
+
+            schema.create_table("Route",
+                         :type => :hash,
+                         :key_type => "ShortText",
+                         :force => true) do |table|
+                         end
+
+            schema.create_table("Subscriber",
+                         :type => :hash,
+                         :key_type => "ShortText",
+                         :force => true) do |table|
+              table.time("last_modified")
+                         end
+
+            schema.change_table("Query") do |table|
+              table.reference("keywords", "Keyword", :type => :vector)
+            end
+
+            schema.change_table("Subscriber") do |table|
+              table.reference("route", "Route")
+              table.reference("subscriptions", "Query", :type => :vector)
+            end
+
+            schema.change_table("Keyword") do |table|
+              table.index("Query", "keywords", :name => "queries")
+            end
+
+            schema.change_table("Query") do |table|
+              table.index("Subscriber", "subscriptions", :name => "subscribers")
+            end
+          end
+        end
+      end
+    end
+
+    def create_context
+      context = Groonga::Context.new
+      begin
+        yield(context)
+      ensure
+        context.close
+      end
+    end
+  end
+end

  Added: test/test_watch_schema.rb (+59 -0) 100644
===================================================================
--- /dev/null
+++ test/test_watch_schema.rb    2013-11-05 13:43:19 +0900 (543d37a)
@@ -0,0 +1,59 @@
+# 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 "helper"
+
+require "droonga/watch_schema"
+
+class WatchSchemaTest < Test::Unit::TestCase
+  def setup
+    @database_path = @temporary_directory + "droonga/watch/db"
+  end
+
+  def test_ensure_created
+    schema = Droonga::WatchSchema.new(@database_path.to_s)
+
+    assert_not_predicate(@database_path, :exist?)
+    schema.ensure_created
+    assert_predicate(@database_path, :exist?)
+
+    context = Groonga::Context.new
+    dumped_commands = nil
+    context.open_database(@database_path.to_s) do |database|
+      dumped_commands = Groonga::DatabaseDumper.dump(:context => context,
+                                                     :database => database)
+    end
+    context.close
+    assert_equal(<<-SCHEMA, dumped_commands)
+table_create Keyword TABLE_PAT_KEY|KEY_NORMALIZE --key_type ShortText
+
+table_create Query TABLE_HASH_KEY --key_type ShortText
+
+table_create Route TABLE_HASH_KEY --key_type ShortText
+
+table_create Subscriber TABLE_HASH_KEY --key_type ShortText
+column_create Subscriber last_modified COLUMN_SCALAR Time
+
+column_create Query keywords COLUMN_VECTOR Keyword
+
+column_create Subscriber route COLUMN_SCALAR Route
+column_create Subscriber subscriptions COLUMN_VECTOR Query
+
+column_create Keyword queries COLUMN_INDEX Query keywords
+
+column_create Query subscribers COLUMN_INDEX Subscriber subscriptions
+SCHEMA
+  end
+end
-------------- next part --------------
HTML����������������������������...
Download 



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