Class: Groonga::Schema

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

Overview

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

Groonga::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, ViewDefinition, ViewRemoveDefinition

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",
}

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 スキーマ定義時に使用するGroonga::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 のテーブルを変更する。以下の省略形。

<pre> Groonga::Schema.define do |schema|

schema.change_table(name, options) do |table|
  # ...
end

end </pre>

ブロックにはGroonga::Schema::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

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



351
352
353
354
355
# File 'lib/groonga/schema.rb', line 351

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

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

名前がnameのビューを変更する。以下の省略形。

<pre> Groonga::Schema.define do |schema|

schema.change_view(name, options) do |view|
  # ...
end

end </pre>

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

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

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



440
441
442
443
444
# File 'lib/groonga/schema.rb', line 440

def change_view(name, options={}, &block)
  define do |schema|
    schema.change_view(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

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

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

Examples:

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

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

Overloads:

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

    Parameters:

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

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

    Options Hash (options):

    • :force (Object)

      The force

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

    • :type (Object) — default: :array

      The type

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

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

      The context

      スキーマ定義時に使用するGroonga::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 を指定するとGroonga::Table#groupで グループ化したときに、Groonga::Record#n_sub_recordsでグループに 含まれるレコードの件数を取得できる。

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

    Parameters:

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

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

    Options Hash (options):

    • :force (Object)

      The force

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

    • :type (Object) — default: :array

      The type

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

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

      The context

      スキーマ定義時に使用するGroonga::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 を指定するとGroonga::Table#groupで グループ化したときに、Groonga::Record#n_sub_recordsでグループに 含まれるレコードの件数を取得できる。

    • :key_type (Object)

      The key_type

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

    • :default_tokenizer (Object)

      The default_tokenizer

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

    • :key_normalize (Object)

      The key_normalize

      true を指定するとキーを正規化する。

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

    Parameters:

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

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

    Options Hash (options):

    • :force (Object)

      The force

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

    • :type (Object) — default: :array

      The type

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

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

      The context

      スキーマ定義時に使用するGroonga::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 を指定するとGroonga::Table#groupで グループ化したときに、Groonga::Record#n_sub_recordsでグループに 含まれるレコードの件数を取得できる。

    • :key_normalize (Object)

      The key_normalize

      true を指定するとキーを正規化する。

    • :key_with_sis (Object)

      The key_with_sis

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



315
316
317
318
319
# File 'lib/groonga/schema.rb', line 315

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

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

名前がnameのビューを作成する。以下の省略形。

<pre> Groonga::Schema.define do |schema|

schema.create_view(name, options) do |view|
  # ...
end

end </pre> ブロックにはGroonga::Schema::ViewDefinitionオブジェ クトがわたるので、そのオブジェクトを利用してビュー の詳細を定義する。

Parameters:

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

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

Options Hash (options):

  • :force (Object)

    The force

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

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

    The context

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

  • :path (Object)

    The path

    ビューを保存するパスを指定する。 パスを指定すると永続ビューになる。

  • :persistent (Object) — default: true

    The persistent

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



403
404
405
406
407
# File 'lib/groonga/schema.rb', line 403

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

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

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

<pre> schema = Groonga::Scheme.new(options) # … schema.define </pre>

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

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

Yields:

  • (schema)


166
167
168
169
170
# File 'lib/groonga/schema.rb', line 166

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

.dump(options = {}) ⇒ Object

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

dump.rb:

<pre> File.open(“/tmp/groonga-schema.rb”, “w”) do |schema|

dumped_text = Groonga::Schema.dump

end </pre>

restore.rb:

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

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

dump.rb:

<pre> File.open(“/tmp/groonga-schema.grn”, “w”) do |schema|

dumped_text = Groonga::Schema.dump(:syntax => :command)

end </pre>

<pre> !!!text % groonga db/path < /tmp/groonga-schema.grn </pre>

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

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

  • :syntax (Object)

    The syntax

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



523
524
525
526
527
# File 'lib/groonga/schema.rb', line 523

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

以下と同様:

<pre> Groonga::Schema.change_table(table_name) do |table|

table.remove_column(column_name)

end </pre>



453
454
455
456
457
# File 'lib/groonga/schema.rb', line 453

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

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



326
327
328
329
330
# File 'lib/groonga/schema.rb', line 326

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

.remove_view(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

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



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

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

.rename_column(table_name, current_column_name, new_column_name) ⇒ Object

This is a syntax sugar of the following:

<pre> Groonga::Schema.define do |schema|

schema.rename_column(table_name,
                     current_column_name, new_column_name)

end </pre>



467
468
469
470
471
# File 'lib/groonga/schema.rb', line 467

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:

<pre> Groonga::Schema.define do |schema|

schema.rename_table(current_name, new_name, options)

end </pre>



366
367
368
369
370
# File 'lib/groonga/schema.rb', line 366

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

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



530
531
532
533
534
# File 'lib/groonga/schema.rb', line 530

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

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

Yields:

  • (definition)


854
855
856
857
858
859
# File 'lib/groonga/schema.rb', line 854

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

#change_view(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

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

Yields:

  • (definition)


932
933
934
935
936
937
# File 'lib/groonga/schema.rb', line 932

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

#contextObject



966
967
968
# File 'lib/groonga/schema.rb', line 966

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

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

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

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

Overloads:

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

    :typeにデフォルトの:arrayを使用した場合

    Parameters:

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

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

    Options Hash (options):

    • :force (Object)

      The force.

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

    • :type (Object) — default: :array

      The type

      テーブルの型を指定する。 :array , :hash , :patricia_trie , :double_array_trie のいずれかを指定する。

    • :context (Groonga::Context)

      The context.

      スキーマ定義時に使用するGroonga::Contextを指定する。 省略した場合はGroonga::Schema.newで指定した Groonga::Contextを使用する。Groonga::Schema.newで指 定していない場合はGroonga::Context.defaultを使用する。

    • :path (Object)

      The path

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

    • :persistent (Object) — default: true

      The persistent

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

    • :value_type (Object)

      The value_type

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

    • :sub_records (Object)

      The sub_records

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

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

    :typeに:hashを使用した場合

    Parameters:

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

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

    Options Hash (options):

    • :force (Object)

      The force

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

    • :type (Object) — default: :array

      The type

      テーブルの型を指定する。 :array , :hash , :patricia_trie , :double_array_trie のいずれかを指定する。

    • :context (Groonga::Context)

      The context

      スキーマ定義時に使用するGroonga::Contextを指定する。 省略した場合はGroonga::Schema.newで指定した Groonga::Contextを使用する。Groonga::Schema.newで指 定していない場合はGroonga::Context.defaultを使用する。

    • :path (Object)

      The path

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

    • :persistent (Object) — default: true

      The persistent

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

    • :value_type (Object)

      The value_type

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

    • :sub_records (Object)

      The sub_records

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

    • :key_type (Object)

      The key_type

      キーの種類を示すオブジェクトを指定する。 キーの種類には型名(“Int32”や“ShortText”など)または Groonga::Typeまたはテーブル(Groonga::Array、 Groonga::Hash、Groonga::PatriciaTrie、 Groonga::DoubleArrayTrieのどれか)を指定する。

      Groonga::Typeを指定した場合は、その型が示す範囲の 値をキーとして使用する。ただし、キーの最大サイズは 4096バイトであるため、Groonga::Type::TEXTや Groonga::Type::LONG_TEXTは使用できない。

      テーブルを指定した場合はレコードIDをキーとして使用 する。指定したテーブルのGroonga::Recordをキーとし て使用することもでき、その場合は自動的に Groonga::RecordからレコードIDを取得する。

      省略した場合は文字列をキーとして使用する。この場合、 4096バイトまで使用可能である。

    • :default_tokenizer (Object)

      The default_tokenizer

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

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

    :typeに:double_array_trieを使用した場合

    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):

    • :force (Object)

      The force

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

    • :context (Groonga::Context)

      The context

      スキーマ定義時に使用するGroonga::Contextを指定する。 省略した場合はGroonga::Schema.newで指定した Groonga::Contextを使用する。Groonga::Schema.newで指 定していない場合はGroonga::Context.defaultを使用する。

    • :path (Object)

      The path

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

    • :persistent (Object) — default: true

      The persistent

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

    • :value_type (Object)

      The value_type

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

    • :sub_records (Object)

      The sub_records

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

    • :key_normalize (Object)

      The key_normalize

      true を指定するとキーを正規化する。

    • :key_type (Object)

      The key_type

      キーの種類を示すオブジェクトを指定する。 キーの種類には型名(“Int32”や“ShortText”など)または Groonga::Typeまたはテーブル(Groonga::Array、 Groonga::Hash、Groonga::PatriciaTrie、 Groonga::DoubleArrayTrieのどれか)を指定する。

      Groonga::Typeを指定した場合は、その型が示す範囲の 値をキーとして使用する。ただし、キーの最大サイズは 4096バイトであるため、Groonga::Type::TEXTや Groonga::Type::LONG_TEXTは使用できない。

      テーブルを指定した場合はレコードIDをキーとして使用 する。指定したテーブルのGroonga::Recordをキーとし て使用することもでき、その場合は自動的に Groonga::RecordからレコードIDを取得する。

      省略した場合は文字列をキーとして使用する。この場合、 4096バイトまで使用可能である。

    • :default_tokenizer (Object)

      The default_tokenizer

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

Yields:

  • (definition)


823
824
825
826
827
# File 'lib/groonga/schema.rb', line 823

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

#create_view(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):

  • :force (Object)

    The force

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

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

    The context

    スキーマ定義時に使用するGroonga::Contextを指定する。 Groonga::Schema.newで指定していない場合は Groonga::Context.defaultを使用する。

  • :path (Object)

    The path

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

  • :persistent (Object) — default: true

    The persistent

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

Yields:

  • (definition)


900
901
902
903
904
# File 'lib/groonga/schema.rb', line 900

def create_view(name, options={})
  definition = ViewDefinition.new(name, @options.merge(options || {}))
  yield(definition)
  @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

スキーマの内容を文字列で返す。返された値は Groonga::Schema#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

以下と同様:

<pre> schema.change_table(table_name) do |table|

table.remove_column(column_name)

end </pre>



946
947
948
949
950
# File 'lib/groonga/schema.rb', line 946

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

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



839
840
841
842
# File 'lib/groonga/schema.rb', line 839

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

#remove_view(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 (Groonga::Context) — default: Groonga::Context.default

    The context

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



917
918
919
920
# File 'lib/groonga/schema.rb', line 917

def remove_view(name, options={})
  definition = ViewRemoveDefinition.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:

<pre> schema.change_table(table_name) do |table|

table.rename_column(current_column_name, new_column_name)

end </pre>



959
960
961
962
963
# File 'lib/groonga/schema.rb', line 959

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 Groonga::Context to be used in renaming.



870
871
872
873
874
# File 'lib/groonga/schema.rb', line 870

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

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

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



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

def restore(dumped_text)
  instance_eval(dumped_text)
end