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_databaseGroonga::Database #create_database {|database| ... } ⇒ Object #create_database(path) ⇒ Groonga::Database #create_database(path) {|database| ... } ⇒ Object

This is convenience method. It wraps Database.create for the context.

Overloads:

  • #create_databaseGroonga::Database

    Creates a new temproary database for the context.

    Examples:

    Creating a new temporary database

    temporary_database = context.create_database

    Returns:

  • #create_database {|database| ... } ⇒ Object

    Creates a new temproary database for the context. The database is closed after the passed block is finished.

    Examples:

    Creating a new temporary database with block

    context.create_database do |temporary_database|
      # ...
    end

    Yields:

    • (database)

      Yields a newly created temporary database for the context. The database is available only in the block.

    Yield Parameters:

    • database (Groonga::Database)

      A newly created temporary database for the context.

    Yield Returns:

    • (Object)

      The returned value from the block is the returned value from this method.

    Returns:

    • Returned value from the block.

  • #create_database(path) ⇒ Groonga::Database

    Creates a new persistent database for the context to the path.

    Examples:

    Creating a new persistent database to _“/tmp/db.groonga”_

    database = context.create_database("/tmp/db.groonga")

    Parameters:

    • path (String)

      Database path for a new persistent database.

    Returns:

  • #create_database(path) {|database| ... } ⇒ Object

    Creates a new persistent database for the context to the path. The database is closed after the passed block is finished.

    Examples:

    Creating a new persistent database to _“/tmp/db.groonga”_ database with block

    context.create_database("/tmp/db.groonga") do |persistent_database|
      # ...
    end

    Parameters:

    • path (String)

      Database path for a new persistent database.

    Yields:

    • (database)

      Yields a newly created persistent database for the context. The database is available only in the block.

    Yield Parameters:

    • database (Groonga::Database)

      A newly created persistent database for the context.

    Yield Returns:

    • (Object)

      The returned value from the block is the returned value from this method.

    Returns:

    • Returned value from the block.



99
100
101
102
103
104
105
106
# File 'lib/groonga/context.rb', line 99

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

  Database.create(options, &block)
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 を指定するとプラグインディレクトリ以外にあるプラグインを登録することができる。



111
112
113
114
115
116
117
118
119
# File 'lib/groonga/context.rb', line 111

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) {|command, response| ... } ⇒ void

This method returns an undefined value.

Restore commands dumped by “grndump” command.

If block is given, a response is yielded.

Examples:

Restore dumped commands as a String object.

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

Restore dumped commands from a File object.

File.open("dump.grn") do |file|
  context.restore(file)
end

Restore dumped commands and reports result.

dumped_commands = File.read("dump.grn")
context.restore(dumped_commands) do |command, response|
  puts("#{command} -> #{response}")
end

Parameters:

  • dumped_commands (#each_line)

    commands dumped by grndump. It can be a String object or any objects like an IO object such as a File object. It should have #each_line that iterates a line.

Yields:

  • (command, response)

    Yields a sent command and its response if block is given.

Yield Parameters:

  • command (String)

    A sent command.

  • response (String)

    A response for a command.



166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
# File 'lib/groonga/context.rb', line 166

def restore(dumped_commands)
  buffer = ""
  continued = false
  dumped_commands.each_line do |line|
    line = line.chomp
    case line
    when /\\\z/
      continued = true
      buffer << $PREMATCH
    else
      continued = false
      buffer << line
      send(buffer)
      _, response = receive
      if block_given?
        not_shared_command = continued ? buffer.dup : line
        yield(not_shared_command, response)
      end
      buffer.clear
    end
  end
  unless buffer.empty?
    send(buffer)
    _, response = receive
    yield(buffer.dup, response) if block_given?
  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



133
134
135
136
# File 'lib/groonga/context.rb', line 133

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