Class: Groonga::Context

Inherits:
Object
  • Object
show all
Defined in:
ext/groonga/rb-grn-context.c,
lib/groonga/context.rb

Overview

groonga全体に渡る情報を管理するオブジェクト。通常のアプリ ケーションでは1つのコンテキストを作成し、それを利用する。 複数のコンテキストを利用する必要はない。

デフォルトで使用されるコンテキストは default でアクセスできる。コンテキ ストを指定できる箇所でコンテキストの指定を省略したり nil を指定した場合は default が利用さ れる。

また、デフォルトのコンテキストは必要になると暗黙のうちに 作成される。そのため、コンテキストを意識することは少ない。

暗黙のうちに作成されるコンテキストにオプションを指定する 場合は default_options= を使用 する。

Instance Method Summary collapse

Instance Method Details

#create_database(path = nil) ⇒ Object

path に新しくデータベースを作成する。 path を省略すると 一時データベースとなる。

Examples:

#一時データベースを作成:
context.create_database

#永続データベースを作成:
context.create_database("/tmp/db.groonga")


40
41
42
43
44
45
46
47
# File 'lib/groonga/context.rb', line 40

def create_database(path=nil)
  options = {:context => self}
  if path
    options[:path] = path
  end

  Database.create(options)
end

#open_database(path, &block) ⇒ Object

path にある既存のデータベースを開く。ブロックを指定した場 合はブロックに開いたデータベースを渡し、ブロックを抜けると きに閉じる。



25
26
27
28
29
# File 'lib/groonga/context.rb', line 25

def open_database(path, &block)
  options = {:context => self}

  Database.open(path, options, &block)
end

#register_plugin(name_or_options) ⇒ Object

groongaのプラグインディレクトリにあるプラグイン name を登録する。 path を指定するとプラグインディレクトリ以 外にあるプラグインを登録することができる。



52
53
54
55
56
57
58
59
60
# File 'lib/groonga/context.rb', line 52

def register_plugin(name_or_options)
  options = {:context => self}
  if name_or_options.is_a?(String)
    name = name_or_options
    Plugin.register(name, options)
  else
    Plugin.register(name_or_options.merge(options))
  end
end

#restore(dumped_commands) ⇒ Object

Restore commands dumped by “grndump” command.

Examples:

dumped_commands = File.read("dump.grn")
context.restore(dumped_commands)

Parameters:

  • dumped_commands (String)

    commands dumped by grndump.



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/groonga/context.rb', line 86

def restore(dumped_commands)
  buffer = ""
  dumped_commands.each_line do |line|
    line = line.chomp
    case line
    when /\\\z/
      buffer << $PREMATCH
    else
      buffer << line
      send(buffer)
      receive
      buffer.clear
    end
  end
  unless buffer.empty?
    send(buffer)
    receive
  end
end

#select(table, options = {}) ⇒ Object

table から指定した条件にマッチするレコードの値を取得 する。 table はテーブル名かテーブルオブジェクトを指定 する。

options に指定できるキーは以下の通り。

Parameters:

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

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

Options Hash (options):

  • output_columns (Array)

    The output_columns

    値を取得するカラムを指定する。

  • XXX (Array)

    TODO TODO



74
75
76
77
# File 'lib/groonga/context.rb', line 74

def select(table, options={})
  select = Command::Select.new(self, table, options)
  select.exec
end