[Groonga-commit] droonga/grn2drn at 8d2c6f8 [master] Add schema converter

Back to archive index

Kouhei Sutou null+****@clear*****
Fri Mar 14 18:30:07 JST 2014


Kouhei Sutou	2014-03-14 18:30:07 +0900 (Fri, 14 Mar 2014)

  New Revision: 8d2c6f8e2bbb945ac19c60b44e447f141318ba15
  https://github.com/droonga/grn2drn/commit/8d2c6f8e2bbb945ac19c60b44e447f141318ba15

  Message:
    Add schema converter
    
    TODO: Test columns

  Added files:
    lib/grn2drn/schema-converter.rb
    test/test-schema-converter.rb

  Added: lib/grn2drn/schema-converter.rb (+180 -0) 100644
===================================================================
--- /dev/null
+++ lib/grn2drn/schema-converter.rb    2014-03-14 18:30:07 +0900 (1f60b24)
@@ -0,0 +1,180 @@
+# Copyright (C) 2014 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 "groonga/command/parser"
+
+module Grn2Drn
+  class SchemaConverter
+    def initialize(options={})
+      @options = options
+    end
+
+    def convert(input)
+      schema = Schema.new
+
+      command_parser = Groonga::Command::Parser.new
+      command_parser.on_command do |command|
+        case command.name
+        when "table_create"
+          schema.on_table_create_command(command)
+        when "column_create"
+          schema.on_column_create_command(command)
+        end
+      end
+
+      input.each_line do |line|
+        command_parser << line
+      end
+      command_parser.finish
+
+      schema.to_droonga_schema
+    end
+
+    class Schema
+      def initialize
+        @tables = {}
+      end
+
+      def on_table_create_command(command)
+        @tables[command[:name]] = Table.new(command)
+      end
+
+      def on_column_craete_command(command)
+        @tables[command.table].add_column(Column.new(command))
+      end
+
+      def to_droonga_schema
+        droonga_schema = {}
+        @tables.each do |name, table|
+          droonga_schema[name] = table.to_droonga_schema
+        end
+        droonga_schema
+      end
+    end
+
+    class Table
+      def initialize(table_create_command)
+        @command = table_create_command
+        @columns = []
+      end
+
+      def add_column(column_create_command)
+        @columns << Column.new(column_create_command)
+      end
+
+      def to_droonga_schema
+        schema = {}
+        set_schema_item(schema, "type", type)
+        set_schema_item(schema, "keyType", key_type)
+        set_schema_item(schema, "tokenizer", tokenizer)
+        set_schema_item(schema, "normalizer", normalizer)
+        set_schema_item(schema, "columns", @columns.collect(&:to_droonga_schema))
+        schema
+      end
+
+      private
+      def set_schema_item(schema, key, value)
+        return if value.nil?
+        schema[key] = value
+      end
+
+      def type
+        if****@comma*****_no_key?
+          "Array"
+        elsif****@comma*****_hash_key?
+          "Hash"
+        elsif****@comma*****_pat_key?
+          "PatriciaTrie"
+        elsif****@comma*****_dat_key?
+          "DoubleArrayTrie"
+        else
+          "Hash"
+        end
+      end
+
+      def key_type
+        type =****@comma*****_type
+        case type
+        when /\AInt/, /\AUInt/
+          "Integer"
+        else
+          type
+        end
+      end
+
+      def tokenizer
+        @command.default_tokenizer
+      end
+
+      def normalizer
+        @command.normalizer
+      end
+    end
+
+    class Column
+      def initialize(column_create_command)
+        @command = column_create_command
+      end
+
+      def to_droonga_schema
+        shcema = {}
+        set_schema_item(schema, "type", type)
+        set_schema_item(schema, "valueType", value_type)
+        set_schema_item(schema, "vectorOptions", vector_options)
+        set_schema_item(schema, "indexOptions", index_options)
+        schema
+      end
+
+      private
+      def set_schema_item(schema, key, value)
+        return if value.nil?
+        schema[key] = value
+      end
+
+      def type
+        if****@comma*****_scalar?
+          "Scalar"
+        elsif****@comma*****_vector?
+          "Vector"
+        elsif****@comma*****_index?
+          "Index"
+        else
+          "Scalar"
+        end
+      end
+
+      def value_type
+        @column.type
+      end
+
+      def weight_options
+        return nil unles****@colum*****_vector?
+        {
+          "weight" => @column.with_weight?,
+        }
+      end
+
+      def index_options
+        return nil unles****@colum*****_index?
+        {
+          "section"  => @column.with_section?,
+          "weight"   => @column.with_weight?,
+          "position" => @column.with_position?,
+          "sources"  => @column.sources,
+        }
+      end
+    end
+  end
+end

  Added: test/test-schema-converter.rb (+159 -0) 100644
===================================================================
--- /dev/null
+++ test/test-schema-converter.rb    2014-03-14 18:30:07 +0900 (81253e1)
@@ -0,0 +1,159 @@
+# Copyright (C) 2014 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 "grn2drn/schema-converter"
+
+class SchemaConverterTest < Test::Unit::TestCase
+  def convert(groonga_commands)
+    converter = Grn2Drn::SchemaConverter.new
+    converter.convert(groonga_commands)
+  end
+
+  class TableCreateTest < self
+    def test_name
+      command = <<-COMMAND.chomp
+table_create Logs
+      COMMAND
+      assert_equal(["Logs"],
+                   convert(command).keys)
+    end
+
+    class TypeTest < self
+      def type(command)
+        convert(command).values.first["type"]
+      end
+
+      def test_default
+        command = <<-COMMAND.chomp
+table_create Logs
+        COMMAND
+        assert_equal("Hash", type(command))
+      end
+
+      def test_no_key
+        command = <<-COMMAND.chomp
+table_create Logs TABLE_NO_KEY
+        COMMAND
+        assert_equal("Array", type(command))
+      end
+
+      def test_hash
+        command = <<-COMMAND.chomp
+table_create Logs TABLE_HASH_KEY
+        COMMAND
+        assert_equal("Hash", type(command))
+      end
+
+      def test_pat_key
+        command = <<-COMMAND.chomp
+table_create Logs TABLE_PAT_KEY
+        COMMAND
+        assert_equal("PatriciaTrie", type(command))
+      end
+
+      def test_dat_key
+        command = <<-COMMAND.chomp
+table_create Logs TABLE_DAT_KEY
+        COMMAND
+        assert_equal("DoubleArrayTrie", type(command))
+      end
+    end
+
+    class KeyTypeTest < self
+      def key_type(command)
+        convert(command).values.first["keyType"]
+      end
+
+      def test_default
+        command = <<-COMMAND.chomp
+table_create Users TABLE_HASH_KEY
+        COMMAND
+        assert_nil(key_type(command))
+      end
+
+      def test_no_key
+        command = <<-COMMAND.chomp
+table_create Logs TABLE_NO_KEY
+        COMMAND
+        assert_equal({
+                       "type" => "Array",
+                       "columns" => [],
+                     },
+                     convert(command)["Logs"])
+      end
+
+      def test_same_type
+        command = <<-COMMAND.chomp
+table_create Users TABLE_HASH_KEY ShortText
+        COMMAND
+        assert_equal("ShortText", key_type(command))
+      end
+
+      def test_signed_integer
+        command = <<-COMMAND.chomp
+table_create Users TABLE_HASH_KEY Int32
+        COMMAND
+        assert_equal("Integer", key_type(command))
+      end
+
+      def test_unsigned_integer
+        command = <<-COMMAND.chomp
+table_create Users TABLE_HASH_KEY UInt32
+        COMMAND
+        assert_equal("Integer", key_type(command))
+      end
+    end
+
+    class TokenizerTest < self
+      def tokenizer(command)
+        convert(command).values.first["tokenizer"]
+      end
+
+      def test_default
+        command = <<-COMMAND.chomp
+table_create Users TABLE_HASH_KEY ShortText
+        COMMAND
+        assert_nil(tokenizer(command))
+      end
+
+      def test_specified
+        command = <<-COMMAND.chomp
+table_create Users TABLE_HASH_KEY ShortText --default_tokenizer TokenBigram
+        COMMAND
+        assert_equal("TokenBigram", tokenizer(command))
+      end
+    end
+
+    class NormalizerTest < self
+      def normalizer(command)
+        convert(command).values.first["normalizer"]
+      end
+
+      def test_default
+        command = <<-COMMAND.chomp
+table_create Users TABLE_HASH_KEY ShortText
+        COMMAND
+        assert_nil(normalizer(command))
+      end
+
+      def test_specified
+        command = <<-COMMAND.chomp
+table_create Users TABLE_HASH_KEY ShortText --normalizer NormalizerAuto
+        COMMAND
+        assert_equal("NormalizerAuto", normalizer(command))
+      end
+    end
+  end
+end
-------------- next part --------------
HTML����������������������������...
Download 



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