Class: Groonga::Schema

Inherits:
Object
  • Object
show all
Defined in:
lib/groonga/schema.rb

Overview

groongaのスキーマ(データ構造)を管理するクラス。

Schema を使うことにより簡単にテーブルやカラムを 追加・削除することができる。

Sample schema

Examples:

上図のようなスキーマを定義する場合は以下のようになる。

Groonga::Schema.define do |schema|
  schema.create_table("Items") do |table|
    table.short_text("title")
  end

  schema.create_table("Users") do |table|
    table.short_text("name")
  end

  schema.create_table("comments") do |table|
    table.reference("item", "Items")
    table.reference("author", "Users")
    table.text("content")
    table.time("issued")
  end
end

Defined Under Namespace

Modules: Path Classes: ColumnCreationWithDifferentOptions, ColumnDefinition, ColumnNotExists, ColumnRemoveDefinition, ColumnRenameDefinition, Error, IndexColumnDefinition, TableCreationWithDifferentOptions, TableDefinition, TableNotExists, TableRemoveDefinition, TableRenameDefinition, UnguessableReferenceTable, UnknownIndexTarget, UnknownIndexTargetTable, UnknownOptions, UnknownTableType

Constant Summary collapse

NORMALIZE_TYPE_TABLE =
{
  "short_text" => "ShortText",
  "string" => "ShortText",
  "text" => "Text",
  "binary" => "LongText",
  "long_text" => "LongText",
  "int8" => "Int8",
  "integer8" => "Int8",
  "int16" => "Int16",
  "integer16" => "Int16",
  "int" => "Int32",
  "integer" => "Int32",
  "int32" => "Int32",
  "integer32" => "Int32",
  "decimal" => "Int64",
  "int64" => "Int64",
  "integer64" => "Int64",
  "uint8" => "UInt8",
  "unsigned_integer8" => "UInt8",
  "uint16" => "UInt16",
  "unsigned_integer16" => "UInt16",
  "uint" => "UInt32",
  "unsigned_integer" => "UInt32",
  "uint32" => "UInt32",
  "unsigned_integer32" => "UInt32",
  "uint64" => "UInt64",
  "unsigned_integer64" => "UInt64",
  "float" => "Float",
  "datetime" => "Time",
  "timestamp" => "Time",
  "time" => "Time",
  "date" => "Time",
  "boolean" => "Bool",
  "tokyo_geo_point" => "TokyoGeoPoint",
  "geo_point" => "WGS84GeoPoint",
  "wgs84_geo_point" => "WGS84GeoPoint",
  "delimit" => "TokenDelimit",
  "token_delimit" => "TokenDelimit",
  "unigram" => "TokenUnigram",
  "token_unigram" => "TokenUnigram",
  "bigram" => "TokenBigram",
  "token_bigram" => "TokenBigram",
  "bigram_split_symbol" => "TokenBigramSplitSymbol",
  "token_bigram_split_symbol" => "TokenBigramSplitSymbol",
  "bigram_split_symbol_alpha" => "TokenBigramSplitSymbolAlpha",
  "token_bigram_split_symbol_alpha" => "TokenBigramSplitSymbolAlpha",
  "bigram_split_symbol_alpha_digit" => "TokenBigramSplitSymbolAlphaDigit",
  "token_bigram_split_symbol_alpha_digit" =>
    "TokenBigramSplitSymbolAlphaDigit",
  "bigram_ignore_blank" => "TokenBigramIgnoreBlank",
  "token_bigram_ignore_blank" => "TokenBigramIgnoreBlank",
  "bigram_ignore_blank_split_symbol" =>
    "TokenBigramIgnoreBlankSplitSymbol",
  "token_bigram_ignore_blank_split_symbol" =>
    "TokenBigramIgnoreBlankSplitSymbol",
  "bigram_ignore_blank_split_symbol_alpha" =>
    "TokenBigramIgnoreBlankSplitSymbolAlpha",
  "token_bigram_ignore_blank_split_symbol_alpha" =>
    "TokenBigramIgnoreBlankSplitSymbolAlpha",
  "bigram_ignore_blank_split_symbol_alpha_digit" =>
    "TokenBigramIgnoreBlankSplitSymbolAlphaDigit",
  "token_bigram_ignore_blank_split_symbol_alpha_digit" =>
    "TokenBigramIgnoreBlankSplitSymbolAlphaDigit",
  "trigram" => "TokenTrigram",
  "token_trigram" => "TokenTrigram",
  "mecab" => "TokenMecab",
  "token_mecab"=> "TokenMecab",
  "regexp" => "TokenRegexp",
  "token_regexp"=> "TokenRegexp",
}

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Schema

スキーマ定義を開始する。

Parameters:

  • options (::Hash) (defaults to: {})

    The name and value pairs. Omitted names are initialized as the default value.

Options Hash (options):

  • :context (Object) — default: Groonga::Context.default

    The context スキーマ定義時に使用する Context を指定する。



620
621
622
623
624
# File 'lib/groonga/schema.rb', line 620

def initialize(options={})
  @options = (options || {}).dup
  @options[:context] ||= Groonga::Context.default
  @definitions = []
end

Class Method Details

.change_table(name, options = {}, &block) ⇒ Object

名前が name のテーブルを変更する。以下の省略形。

Groonga::Schema.define do |schema|
  schema.change_table(name, options) do |table|
    # ...
  end
end

ブロックには TableDefinition オブジェ クトがわたるので、そのオブジェクトを利用してテーブル の詳細を定義する。

Parameters:

  • options (::Hash) (defaults to: {})

    The name and value pairs. Omitted names are initialized as the default value.

Options Hash (options):

  • :context (Object) — default: Groonga::Context.default

    The context

    スキーマ定義時に使用する Context を指定する。



416
417
418
419
420
# File 'lib/groonga/schema.rb', line 416

def change_table(name, options={}, &block)
  define do |schema|
    schema.change_table(name, options, &block)
  end
end

.create_table(name, options = {:type => :array}, &block) ⇒ Object .create_table(name, options = {:type => :hash}, &block) ⇒ Object .create_table(name, options = {:type => :patricia_trie}, &block) ⇒ Object .create_table(name, options = {:type => :double_array_trie}) ⇒ Object

名前が name のテーブルを作成する。以下の省略形。

Groonga::Schema.define do |schema|
  schema.create_table(name, options) do |table|
    # ...
  end
end

ブロックには TableDefinition オブジェ クトがわたるので、そのオブジェクトを利用してテーブル の詳細を定義する。

options に指定可能な値は以下の通り。

Overloads:

  • .create_table(name, options = {:type => :array}, &block) ⇒ Object

    Create a table that manages records by ID when you specify :array to :type.

    You can identify a record only by record ID. You can't use key because key doesn't exist in the table.

    See description of TABLE_NO_KEY at Groonga documentation of tables for details.

    Parameters:

    • options (::Hash) (defaults to: {:type => :array})

      The name and value pairs. Omitted names are initialized as the default value.

    Options Hash (options):

    • :type (:array, :hash, :patricia_trie, :double_array_trie)

      The table type

      テーブルの型を指定する。 :array, :hash, :patricia_trie, :double_array_trie のいずれかを指定する。 (:key_typeの項も参照)

    • :force (Boolean) — default: false

      trueを指定すると既存の同名のテーブルが 存在していても、強制的にテーブルを作成する。

    • :context (Groonga::Context) — default: Groonga::Context.default

      The context

      スキーマ定義時に使用する Context を指定する。

    • :path (Object)

      The path

      テーブルを保存するパスを指定する。 パスを指定すると永続テーブルになる。

    • :persistent (Object) — default: true

      The persistent

      テーブルを永続テーブルとする。:pathを省略した場合は パス名は自動的に作成される。デフォルトでは永続テーブルとなる。

    • :value_type (Object) — default: nil

      The value_type

      値の型を指定する。省略すると値のための領域を確保しない。 値を保存したい場合は必ず指定すること。

    • :sub_records (Object)

      The sub_records

      trueを指定するとTable#groupで グループ化したときに、Record#n_sub_recordsでグループに 含まれるレコードの件数を取得できる。

  • .create_table(name, options = {:type => :hash}, &block) ⇒ Object

    Create a table that manages record keys by hash table when you specify :hash to :type.

    You can identify a record by key. The table is most fast table for finding a record by key. But the table doesn't support advanced key search features such as common prefix search and range search.

    See description of TABLE_HASH_KEY at Groonga documentation of tables for details.

    Parameters:

    • options (::Hash) (defaults to: {:type => :hash})

      The name and value pairs. Omitted names are initialized as the default value.

    Options Hash (options):

    • :type (:array, :hash, :patricia_trie, :double_array_trie)

      The table type

      テーブルの型を指定する。 :array, :hash, :patricia_trie, :double_array_trie のいずれかを指定する。 (:key_typeの項も参照)

    • :force (Boolean) — default: false

      trueを指定すると既存の同名のテーブルが 存在していても、強制的にテーブルを作成する。

    • :context (Groonga::Context) — default: Groonga::Context.default

      The context

      スキーマ定義時に使用する Context を指定する。

    • :path (Object)

      The path

      テーブルを保存するパスを指定する。 パスを指定すると永続テーブルになる。

    • :persistent (Object) — default: true

      The persistent

      テーブルを永続テーブルとする。:pathを省略した場合は パス名は自動的に作成される。デフォルトでは永続テーブルとなる。

    • :value_type (Object) — default: nil

      The value_type

      値の型を指定する。省略すると値のための領域を確保しない。 値を保存したい場合は必ず指定すること。

    • :sub_records (Object)

      The sub_records

      trueを指定するとTable#groupで グループ化したときに、Record#n_sub_recordsでグループに 含まれるレコードの件数を取得できる。

    • :key_type (Object)

      The key_type

      キーの種類を示すオブジェクトを指定する。キーの種類には型名 ("Int32""ShortText"など)または Typeまた はテーブル( ArrayHashPatriciaTrieDoubleArrayTrie のど れか)を指定する。 Type を指定した場合は、その 型が示す範囲の値をキーとして使用する。ただし、キーの最大サ イズは4096バイトであるため、 Type::TEXT や Type::LONG_TEXT は使用できない。テーブルを指定 した場合はレコードIDをキーとして使用する。指定したテーブル の Record をキーとして使用することもでき、その 場合は自動的に Record からレコードIDを取得する。 省略した場合は文字列をキーとして使用する。この場合、4096バ イトまで使用可能である。

    • :default_tokenizer (Object)

      The default_tokenizer

      IndexColumn で 使用するトークナイザを指定する。デフォルトでは 何も設定されていないので、テーブルに IndexColumn を定義する場合は "TokenBigram" などを指定する必要がある。

    • :token_filters (::Array<String, Groonga::Procedure>, nil) — default: nil

      The token filters to be used in the table.

      Here is an example to set two token filters.

      Groonga::Schema.define do |schema|
        schema.create_table("Terms",
                            # ...
                            :token_filters => [
                              "TokenFilterStem",
                              "TokenFilterStopWord",
                            ]) do |table|
          # ...
        end
      end
      
    • :key_normalize (Object) — default: false

      Keys are normalized if this value is true.

      @deprecated Use :normalizer => "NormalizerAuto" instead.

    • :normalizer (String, Groonga::Procedure, nil)

      The normalizer that is used by IndexColumn. You can specify this by normalizer name as String such as "NormalizerAuto" or normalizer object.

  • .create_table(name, options = {:type => :patricia_trie}, &block) ⇒ Object

    Create a table that manages record keys by patricia trie when you specify :patricia_trie to :type.

    You can identify a record by key. The table is most small table. The table supports advanced key search features such as prefix search and range search. The table is suitable for lexicon of full-text search and index of range search.

    See description of TABLE_PAT_KEY at Groonga documentation of tables for details.

    Parameters:

    • options (::Hash) (defaults to: {:type => :patricia_trie})

      The name and value pairs. Omitted names are initialized as the default value.

    Options Hash (options):

    • :type (:array, :hash, :patricia_trie, :double_array_trie)

      The table type

      テーブルの型を指定する。 :array, :hash, :patricia_trie, :double_array_trie のいずれかを指定する。 (:key_typeの項も参照)

    • :force (Boolean) — default: false

      trueを指定すると既存の同名のテーブルが 存在していても、強制的にテーブルを作成する。

    • :context (Groonga::Context) — default: Groonga::Context.default

      The context

      スキーマ定義時に使用する Context を指定する。

    • :path (Object)

      The path

      テーブルを保存するパスを指定する。 パスを指定すると永続テーブルになる。

    • :persistent (Object) — default: true

      The persistent

      テーブルを永続テーブルとする。:pathを省略した場合は パス名は自動的に作成される。デフォルトでは永続テーブルとなる。

    • :value_type (Object) — default: nil

      The value_type

      値の型を指定する。省略すると値のための領域を確保しない。 値を保存したい場合は必ず指定すること。

    • :sub_records (Object)

      The sub_records

      trueを指定するとTable#groupで グループ化したときに、Record#n_sub_recordsでグループに 含まれるレコードの件数を取得できる。

    • :key_type (Object)

      The key_type

      キーの種類を示すオブジェクトを指定する。キーの種類には型名 ("Int32""ShortText"など)または Typeまた はテーブル( ArrayHashPatriciaTrieDoubleArrayTrie のど れか)を指定する。 Type を指定した場合は、その 型が示す範囲の値をキーとして使用する。ただし、キーの最大サ イズは4096バイトであるため、 Type::TEXT や Type::LONG_TEXT は使用できない。テーブルを指定 した場合はレコードIDをキーとして使用する。指定したテーブル の Record をキーとして使用することもでき、その 場合は自動的に Record からレコードIDを取得する。 省略した場合は文字列をキーとして使用する。この場合、4096バ イトまで使用可能である。

    • :default_tokenizer (Object)

      The default_tokenizer

      IndexColumn で 使用するトークナイザを指定する。デフォルトでは 何も設定されていないので、テーブルに IndexColumn を定義する場合は "TokenBigram" などを指定する必要がある。

    • :token_filters (::Array<String, Groonga::Procedure>, nil) — default: nil

      The token filters to be used in the table.

      Here is an example to set two token filters.

      Groonga::Schema.define do |schema|
        schema.create_table("Terms",
                            # ...
                            :token_filters => [
                              "TokenFilterStem",
                              "TokenFilterStopWord",
                            ]) do |table|
          # ...
        end
      end
      
    • :key_normalize (Object) — default: false

      Keys are normalized if this value is true.

      @deprecated Use :normalizer => "NormalizerAuto" instead.

    • :normalizer (String, Groonga::Procedure, nil)

      The normalizer that is used by IndexColumn. You can specify this by normalizer name as String such as "NormalizerAuto" or normalizer object.

    • :key_with_sis (Boolean) — default: false

      +true+ を指定するとキーの文字列の 全suffixが自動的に登録される。

  • .create_table(name, options = {:type => :double_array_trie}) ⇒ Object

    Create a table that manages record keys by double array trie when you specify :double_array_trie to :type.

    You can identify a record by key. The table is fast table for finding a record by key. But the table is large. The table supports advanced key search features such as prefix search and range search.

    The table is the only table that supports renaming key. The table is used in Groonga database to manage object names such as ShortText and TokenBigram.

    See description of TABLE_DAT_KEY at Groonga documentation of tables for details.

    Parameters:

    • options (::Hash) (defaults to: {:type => :double_array_trie})

      The name and value pairs. Omitted names are initialized as the default value.

    Options Hash (options):

    • :type (:array, :hash, :patricia_trie, :double_array_trie)

      The table type

      テーブルの型を指定する。 :array, :hash, :patricia_trie, :double_array_trie のいずれかを指定する。 (:key_typeの項も参照)

    • :force (Boolean) — default: false

      trueを指定すると既存の同名のテーブルが 存在していても、強制的にテーブルを作成する。

    • :context (Groonga::Context) — default: Groonga::Context.default

      The context

      スキーマ定義時に使用する Context を指定する。

    • :path (Object)

      The path

      テーブルを保存するパスを指定する。 パスを指定すると永続テーブルになる。

    • :persistent (Object) — default: true

      The persistent

      テーブルを永続テーブルとする。:pathを省略した場合は パス名は自動的に作成される。デフォルトでは永続テーブルとなる。

    • :value_type (Object) — default: nil

      The value_type

      値の型を指定する。省略すると値のための領域を確保しない。 値を保存したい場合は必ず指定すること。

    • :sub_records (Object)

      The sub_records

      trueを指定するとTable#groupで グループ化したときに、Record#n_sub_recordsでグループに 含まれるレコードの件数を取得できる。

    • :key_type (Object)

      The key_type

      キーの種類を示すオブジェクトを指定する。キーの種類には型名 ("Int32""ShortText"など)または Typeまた はテーブル( ArrayHashPatriciaTrieDoubleArrayTrie のど れか)を指定する。 Type を指定した場合は、その 型が示す範囲の値をキーとして使用する。ただし、キーの最大サ イズは4096バイトであるため、 Type::TEXT や Type::LONG_TEXT は使用できない。テーブルを指定 した場合はレコードIDをキーとして使用する。指定したテーブル の Record をキーとして使用することもでき、その 場合は自動的に Record からレコードIDを取得する。 省略した場合は文字列をキーとして使用する。この場合、4096バ イトまで使用可能である。

    • :default_tokenizer (Object)

      The default_tokenizer

      IndexColumn で 使用するトークナイザを指定する。デフォルトでは 何も設定されていないので、テーブルに IndexColumn を定義する場合は "TokenBigram" などを指定する必要がある。

    • :token_filters (::Array<String, Groonga::Procedure>, nil) — default: nil

      The token filters to be used in the table.

      Here is an example to set two token filters.

      Groonga::Schema.define do |schema|
        schema.create_table("Terms",
                            # ...
                            :token_filters => [
                              "TokenFilterStem",
                              "TokenFilterStopWord",
                            ]) do |table|
          # ...
        end
      end
      
    • :key_normalize (Object) — default: false

      Keys are normalized if this value is true.

      @deprecated Use :normalizer => "NormalizerAuto" instead.

    • :normalizer (String, Groonga::Procedure, nil)

      The normalizer that is used by IndexColumn. You can specify this by normalizer name as String such as "NormalizerAuto" or normalizer object.



379
380
381
382
383
# File 'lib/groonga/schema.rb', line 379

def create_table(name, options={}, &block)
  define do |schema|
    schema.create_table(name, options, &block)
  end
end

.define(options = {}) {|schema| ... } ⇒ Object

スキーマを定義する。ブロックには Groonga::Schema オブ ジェクトがわたるので、そのオブジェクトを利用してスキー マを定義する。以下の省略形。

schema = Groonga::Scheme.new(options)
# ...
schema.define

Parameters:

  • options (::Hash) (defaults to: {})

    The name and value pairs. Omitted names are initialized as the default value.

Options Hash (options):

  • :content (Groonga::Context) — default: Groonga::Context.default

    The context

    スキーマ定義時に使用する Context を指定する。

Yields:

  • (schema)


168
169
170
171
172
# File 'lib/groonga/schema.rb', line 168

def define(options={})
  schema = new(options)
  yield(schema)
  schema.define
end

.dump(options = {}) ⇒ Object

スキーマの内容を文字列をRubyスクリプト形式またはgrn式 形式で返す。デフォルトはRubyスクリプト形式である。 Rubyスクリプト形式で返された値は restore することによりスキーマ内に組み 込むことができる。

dump.rb:

File.open("/tmp/groonga-schema.rb", "w") do |schema|
  dumped_text = Groonga::Schema.dump
end

restore.rb:

dumped_text = Groonga::Schema.dump
Groonga::Database.create(:path => "/tmp/new-db.grn")
Groonga::Schema.restore(dumped_text)

grn式形式で返された値はgroongaコマンドで読み込むこと ができる。

dump.rb:

File.open("/tmp/groonga-schema.grn", "w") do |schema|
  dumped_text = Groonga::Schema.dump(:syntax => :command)
end
% groonga db/path < /tmp/groonga-schema.grn

Parameters:

  • options (::Hash) (defaults to: {})

    The name and value pairs. Omitted names are initialized as the default value.

Options Hash (options):

  • :context (Object) — default: Groonga::Context.default

    The context

    スキーマ定義時に使用する Context を指定する。

  • :syntax (Object)

    The syntax

    スキーマの文字列の形式を指定する。指定可能な値は以下の通り。 +:ruby+ Rubyスクリプト形式。省略した場合、+nil+ の場合も Rubyスクリプト形式になる。 +:command+ groongaコマンド形式。groongaコマンドで読み込む ことができる。



520
521
522
523
524
# File 'lib/groonga/schema.rb', line 520

def dump(options={})
  schema = new(:context => options[:context],
               :syntax => options[:syntax])
  schema.dump
end

.normalize_type(type, options = {}) ⇒ Object



605
606
607
608
609
610
611
# File 'lib/groonga/schema.rb', line 605

def normalize_type(type, options={})
  return type if type.nil?
  return type if type.is_a?(Groonga::Object)
  type = type.to_s if type.is_a?(Symbol)
  return type if (options[:context] || Groonga::Context.default)[type]
  NORMALIZE_TYPE_TABLE[type] || type
end

.remove_column(table_name, column_name) ⇒ Object

以下と同様:

Groonga::Schema.change_table(table_name) do |table|
  table.remove_column(column_name)
end


446
447
448
449
450
# File 'lib/groonga/schema.rb', line 446

def remove_column(table_name, column_name)
  change_table(table_name) do |table|
    table.remove_column(column_name)
  end
end

.remove_table(name, options = {}) ⇒ Object

名前が name のテーブルを削除する。

Parameters:

  • options (::Hash) (defaults to: {})

    The name and value pairs. Omitted names are initialized as the default value.

Options Hash (options):

  • :context (Object) — default: Groonga::Context.default

    スキーマ定義時に使用する Context を指定する。



390
391
392
393
394
# File 'lib/groonga/schema.rb', line 390

def remove_table(name, options={})
  define do |schema|
    schema.remove_table(name, options)
  end
end

.rename_column(table_name, current_column_name, new_column_name) ⇒ Object

This is a syntax sugar of the following:

Groonga::Schema.define do |schema|
  schema.rename_column(table_name,
                       current_column_name, new_column_name)
end


461
462
463
464
465
# File 'lib/groonga/schema.rb', line 461

def rename_column(table_name, current_column_name, new_column_name)
  define do |schema|
    schema.rename_column(table_name, current_column_name, new_column_name)
  end
end

.rename_table(current_name, new_name, options = {}) ⇒ Object

(See Groonga::Schema#rename_table)

This is a syntax sugar of the following code:

Groonga::Schema.define do |schema|
  schema.rename_table(current_name, new_name, options)
end


432
433
434
435
436
# File 'lib/groonga/schema.rb', line 432

def rename_table(current_name, new_name, options={})
  define do |schema|
    schema.rename_table(current_name, new_name, options)
  end
end

.restore(dumped_text, options = {}) ⇒ Object

dump で文字列化したスキーマを組み込む。



527
528
529
530
531
# File 'lib/groonga/schema.rb', line 527

def restore(dumped_text, options={})
  define(options) do |schema|
    schema.restore(dumped_text)
  end
end

Instance Method Details

#change_table(name, options = {}) {|definition| ... } ⇒ Object

名前が name のテーブルを変更する。

テーブルの変更は #define を呼び出すまでは実行されないこ とに注意すること。

Parameters:

  • options (::Hash) (defaults to: {})

    The name and value pairs. Omitted names are initialized as the default value.

Options Hash (options):

  • :context (Object) — default: Groonga::Context.default

    The context

    スキーマ定義時に使用する Context を指定する。

Yields:

  • (definition)


700
701
702
703
704
705
# File 'lib/groonga/schema.rb', line 700

def change_table(name, options={})
  options = @options.merge(options || {}).merge(:change => true)
  definition = TableDefinition.new(name, options)
  yield(definition)
  @definitions << definition
end

#contextObject



751
752
753
# File 'lib/groonga/schema.rb', line 751

def context
  @options[:context] || Groonga::Context.default
end

#create_table(name, options = {:type => :array}, &block) ⇒ Object #create_table(name, options = {:type => :hash}, &block) ⇒ Object #create_table(name, options = {:type => :patricia_trie}, &block) ⇒ Object #create_table(name, options = {:type => :double_array_trie}) ⇒ Object

名前が name のテーブルを作成する。

テーブルの作成は #define を呼び出すまでは実行されないこ とに注意すること。

Overloads:

  • #create_table(name, options = {:type => :array}, &block) ⇒ Object

    Create a table that manages records by ID when you specify :array to :type.

    You can identify a record only by record ID. You can't use key because key doesn't exist in the table.

    See description of TABLE_NO_KEY at Groonga documentation of tables for details.

    Parameters:

    • options (::Hash) (defaults to: {:type => :array})

      The name and value pairs. Omitted names are initialized as the default value.

    Options Hash (options):

    • :type (:array, :hash, :patricia_trie, :double_array_trie)

      The table type

      テーブルの型を指定する。 :array, :hash, :patricia_trie, :double_array_trie のいずれかを指定する。 (:key_typeの項も参照)

    • :force (Boolean) — default: false

      trueを指定すると既存の同名のテーブルが 存在していても、強制的にテーブルを作成する。

    • :context (Groonga::Context) — default: Groonga::Context.default

      The context

      スキーマ定義時に使用する Context を指定する。

    • :path (Object)

      The path

      テーブルを保存するパスを指定する。 パスを指定すると永続テーブルになる。

    • :persistent (Object) — default: true

      The persistent

      テーブルを永続テーブルとする。:pathを省略した場合は パス名は自動的に作成される。デフォルトでは永続テーブルとなる。

    • :value_type (Object) — default: nil

      The value_type

      値の型を指定する。省略すると値のための領域を確保しない。 値を保存したい場合は必ず指定すること。

    • :sub_records (Object)

      The sub_records

      trueを指定するとTable#groupで グループ化したときに、Record#n_sub_recordsでグループに 含まれるレコードの件数を取得できる。

  • #create_table(name, options = {:type => :hash}, &block) ⇒ Object

    Create a table that manages record keys by hash table when you specify :hash to :type.

    You can identify a record by key. The table is most fast table for finding a record by key. But the table doesn't support advanced key search features such as common prefix search and range search.

    See description of TABLE_HASH_KEY at Groonga documentation of tables for details.

    Parameters:

    • options (::Hash) (defaults to: {:type => :hash})

      The name and value pairs. Omitted names are initialized as the default value.

    Options Hash (options):

    • :type (:array, :hash, :patricia_trie, :double_array_trie)

      The table type

      テーブルの型を指定する。 :array, :hash, :patricia_trie, :double_array_trie のいずれかを指定する。 (:key_typeの項も参照)

    • :force (Boolean) — default: false

      trueを指定すると既存の同名のテーブルが 存在していても、強制的にテーブルを作成する。

    • :context (Groonga::Context) — default: Groonga::Context.default

      The context

      スキーマ定義時に使用する Context を指定する。

    • :path (Object)

      The path

      テーブルを保存するパスを指定する。 パスを指定すると永続テーブルになる。

    • :persistent (Object) — default: true

      The persistent

      テーブルを永続テーブルとする。:pathを省略した場合は パス名は自動的に作成される。デフォルトでは永続テーブルとなる。

    • :value_type (Object) — default: nil

      The value_type

      値の型を指定する。省略すると値のための領域を確保しない。 値を保存したい場合は必ず指定すること。

    • :sub_records (Object)

      The sub_records

      trueを指定するとTable#groupで グループ化したときに、Record#n_sub_recordsでグループに 含まれるレコードの件数を取得できる。

    • :key_type (Object)

      The key_type

      キーの種類を示すオブジェクトを指定する。キーの種類には型名 ("Int32""ShortText"など)または Typeまた はテーブル( ArrayHashPatriciaTrieDoubleArrayTrie のど れか)を指定する。 Type を指定した場合は、その 型が示す範囲の値をキーとして使用する。ただし、キーの最大サ イズは4096バイトであるため、 Type::TEXT や Type::LONG_TEXT は使用できない。テーブルを指定 した場合はレコードIDをキーとして使用する。指定したテーブル の Record をキーとして使用することもでき、その 場合は自動的に Record からレコードIDを取得する。 省略した場合は文字列をキーとして使用する。この場合、4096バ イトまで使用可能である。

    • :default_tokenizer (Object)

      The default_tokenizer

      IndexColumn で 使用するトークナイザを指定する。デフォルトでは 何も設定されていないので、テーブルに IndexColumn を定義する場合は "TokenBigram" などを指定する必要がある。

    • :token_filters (::Array<String, Groonga::Procedure>, nil) — default: nil

      The token filters to be used in the table.

      Here is an example to set two token filters.

      Groonga::Schema.define do |schema|
        schema.create_table("Terms",
                            # ...
                            :token_filters => [
                              "TokenFilterStem",
                              "TokenFilterStopWord",
                            ]) do |table|
          # ...
        end
      end
      
    • :key_normalize (Object) — default: false

      Keys are normalized if this value is true.

      @deprecated Use :normalizer => "NormalizerAuto" instead.

    • :normalizer (String, Groonga::Procedure, nil)

      The normalizer that is used by IndexColumn. You can specify this by normalizer name as String such as "NormalizerAuto" or normalizer object.

  • #create_table(name, options = {:type => :patricia_trie}, &block) ⇒ Object

    Create a table that manages record keys by patricia trie when you specify :patricia_trie to :type.

    You can identify a record by key. The table is most small table. The table supports advanced key search features such as prefix search and range search. The table is suitable for lexicon of full-text search and index of range search.

    See description of TABLE_PAT_KEY at Groonga documentation of tables for details.

    Parameters:

    • options (::Hash) (defaults to: {:type => :patricia_trie})

      The name and value pairs. Omitted names are initialized as the default value.

    Options Hash (options):

    • :type (:array, :hash, :patricia_trie, :double_array_trie)

      The table type

      テーブルの型を指定する。 :array, :hash, :patricia_trie, :double_array_trie のいずれかを指定する。 (:key_typeの項も参照)

    • :force (Boolean) — default: false

      trueを指定すると既存の同名のテーブルが 存在していても、強制的にテーブルを作成する。

    • :context (Groonga::Context) — default: Groonga::Context.default

      The context

      スキーマ定義時に使用する Context を指定する。

    • :path (Object)

      The path

      テーブルを保存するパスを指定する。 パスを指定すると永続テーブルになる。

    • :persistent (Object) — default: true

      The persistent

      テーブルを永続テーブルとする。:pathを省略した場合は パス名は自動的に作成される。デフォルトでは永続テーブルとなる。

    • :value_type (Object) — default: nil

      The value_type

      値の型を指定する。省略すると値のための領域を確保しない。 値を保存したい場合は必ず指定すること。

    • :sub_records (Object)

      The sub_records

      trueを指定するとTable#groupで グループ化したときに、Record#n_sub_recordsでグループに 含まれるレコードの件数を取得できる。

    • :key_type (Object)

      The key_type

      キーの種類を示すオブジェクトを指定する。キーの種類には型名 ("Int32""ShortText"など)または Typeまた はテーブル( ArrayHashPatriciaTrieDoubleArrayTrie のど れか)を指定する。 Type を指定した場合は、その 型が示す範囲の値をキーとして使用する。ただし、キーの最大サ イズは4096バイトであるため、 Type::TEXT や Type::LONG_TEXT は使用できない。テーブルを指定 した場合はレコードIDをキーとして使用する。指定したテーブル の Record をキーとして使用することもでき、その 場合は自動的に Record からレコードIDを取得する。 省略した場合は文字列をキーとして使用する。この場合、4096バ イトまで使用可能である。

    • :default_tokenizer (Object)

      The default_tokenizer

      IndexColumn で 使用するトークナイザを指定する。デフォルトでは 何も設定されていないので、テーブルに IndexColumn を定義する場合は "TokenBigram" などを指定する必要がある。

    • :token_filters (::Array<String, Groonga::Procedure>, nil) — default: nil

      The token filters to be used in the table.

      Here is an example to set two token filters.

      Groonga::Schema.define do |schema|
        schema.create_table("Terms",
                            # ...
                            :token_filters => [
                              "TokenFilterStem",
                              "TokenFilterStopWord",
                            ]) do |table|
          # ...
        end
      end
      
    • :key_normalize (Object) — default: false

      Keys are normalized if this value is true.

      @deprecated Use :normalizer => "NormalizerAuto" instead.

    • :normalizer (String, Groonga::Procedure, nil)

      The normalizer that is used by IndexColumn. You can specify this by normalizer name as String such as "NormalizerAuto" or normalizer object.

    • :key_with_sis (Boolean) — default: false

      +true+ を指定するとキーの文字列の 全suffixが自動的に登録される。

  • #create_table(name, options = {:type => :double_array_trie}) ⇒ Object

    Create a table that manages record keys by double array trie when you specify :double_array_trie to :type.

    You can identify a record by key. The table is fast table for finding a record by key. But the table is large. The table supports advanced key search features such as prefix search and range search.

    The table is the only table that supports renaming key. The table is used in Groonga database to manage object names such as ShortText and TokenBigram.

    See description of TABLE_DAT_KEY at Groonga documentation of tables for details.

    Parameters:

    • options (::Hash) (defaults to: {:type => :double_array_trie})

      The name and value pairs. Omitted names are initialized as the default value.

    Options Hash (options):

    • :type (:array, :hash, :patricia_trie, :double_array_trie)

      The table type

      テーブルの型を指定する。 :array, :hash, :patricia_trie, :double_array_trie のいずれかを指定する。 (:key_typeの項も参照)

    • :force (Boolean) — default: false

      trueを指定すると既存の同名のテーブルが 存在していても、強制的にテーブルを作成する。

    • :context (Groonga::Context) — default: Groonga::Context.default

      The context

      スキーマ定義時に使用する Context を指定する。

    • :path (Object)

      The path

      テーブルを保存するパスを指定する。 パスを指定すると永続テーブルになる。

    • :persistent (Object) — default: true

      The persistent

      テーブルを永続テーブルとする。:pathを省略した場合は パス名は自動的に作成される。デフォルトでは永続テーブルとなる。

    • :value_type (Object) — default: nil

      The value_type

      値の型を指定する。省略すると値のための領域を確保しない。 値を保存したい場合は必ず指定すること。

    • :sub_records (Object)

      The sub_records

      trueを指定するとTable#groupで グループ化したときに、Record#n_sub_recordsでグループに 含まれるレコードの件数を取得できる。

    • :key_type (Object)

      The key_type

      キーの種類を示すオブジェクトを指定する。キーの種類には型名 ("Int32""ShortText"など)または Typeまた はテーブル( ArrayHashPatriciaTrieDoubleArrayTrie のど れか)を指定する。 Type を指定した場合は、その 型が示す範囲の値をキーとして使用する。ただし、キーの最大サ イズは4096バイトであるため、 Type::TEXT や Type::LONG_TEXT は使用できない。テーブルを指定 した場合はレコードIDをキーとして使用する。指定したテーブル の Record をキーとして使用することもでき、その 場合は自動的に Record からレコードIDを取得する。 省略した場合は文字列をキーとして使用する。この場合、4096バ イトまで使用可能である。

    • :default_tokenizer (Object)

      The default_tokenizer

      IndexColumn で 使用するトークナイザを指定する。デフォルトでは 何も設定されていないので、テーブルに IndexColumn を定義する場合は "TokenBigram" などを指定する必要がある。

    • :token_filters (::Array<String, Groonga::Procedure>, nil) — default: nil

      The token filters to be used in the table.

      Here is an example to set two token filters.

      Groonga::Schema.define do |schema|
        schema.create_table("Terms",
                            # ...
                            :token_filters => [
                              "TokenFilterStem",
                              "TokenFilterStopWord",
                            ]) do |table|
          # ...
        end
      end
      
    • :key_normalize (Object) — default: false

      Keys are normalized if this value is true.

      @deprecated Use :normalizer => "NormalizerAuto" instead.

    • :normalizer (String, Groonga::Procedure, nil)

      The normalizer that is used by IndexColumn. You can specify this by normalizer name as String such as "NormalizerAuto" or normalizer object.

Yields:

  • (definition)


669
670
671
672
673
# File 'lib/groonga/schema.rb', line 669

def create_table(name, options={})
  definition = TableDefinition.new(name, @options.merge(options || {}))
  yield(definition) if block_given?
  @definitions << definition
end

#defineObject

定義されたスキーマ定義を実際に実行する。



627
628
629
630
631
# File 'lib/groonga/schema.rb', line 627

def define
  @definitions.each do |definition|
    definition.define
  end
end

#dumpObject

スキーマの内容を文字列で返す。返された値は #restore することによりスキーマ内に組み込むことができる。



646
647
648
649
650
# File 'lib/groonga/schema.rb', line 646

def dump
  dumper = SchemaDumper.new(:context => @options[:context],
                            :syntax => @options[:syntax] || :ruby)
  dumper.dump
end

#remove_column(table_name, column_name) ⇒ Object

以下と同様:

schema.change_table(table_name) do |table|
  table.remove_column(column_name)
end


730
731
732
733
734
# File 'lib/groonga/schema.rb', line 730

def remove_column(table_name, column_name)
  change_table(table_name) do |table|
    table.remove_column(column_name)
  end
end

#remove_table(name, options = {}) ⇒ Object

名前が name のテーブルを削除する。

テーブルの削除は# define を呼び出すまでは実行されないこ とに注意すること。

Parameters:

  • options (::Hash) (defaults to: {})

    The name and value pairs. Omitted names are initialized as the default value.

Options Hash (options):

  • :context (Object) — default: Groonga::Context.default

    The context

    スキーマ定義時に使用する Context を指定する。



685
686
687
688
# File 'lib/groonga/schema.rb', line 685

def remove_table(name, options={})
  definition = TableRemoveDefinition.new(name, @options.merge(options || {}))
  @definitions << definition
end

#rename_column(table_name, current_column_name, new_column_name) ⇒ Object

It is a syntax sugar of the following:

schema.change_table(table_name) do |table|
  table.rename_column(current_column_name, new_column_name)
end


744
745
746
747
748
# File 'lib/groonga/schema.rb', line 744

def rename_column(table_name, current_column_name, new_column_name)
  change_table(table_name) do |table|
    table.rename_column(current_column_name, new_column_name)
  end
end

#rename_table(current_name, new_name, options = {}) ⇒ Object

Renames current_name table to _new_name.

Note that table renaming will will not be performed until #define is called.

Parameters:

  • options (::Hash) (defaults to: {})

    The name and value pairs. Omitted names are initialized as the default value.

Options Hash (options):

  • :context (Object) — default: Groonga::Context.default

    The Context to be used in renaming.



716
717
718
719
720
# File 'lib/groonga/schema.rb', line 716

def rename_table(current_name, new_name, options={})
  options = @options.merge(options || {})
  definition = TableRenameDefinition.new(current_name, new_name, options)
  @definitions << definition
end

#restore(dumped_text) ⇒ Object Also known as: load

#dump で返されたスキーマの内容を読み込む。

読み込まれた内容は #define を呼び出すまでは実行されない ことに注意すること。



637
638
639
# File 'lib/groonga/schema.rb', line 637

def restore(dumped_text)
  instance_eval(dumped_text)
end