Class: Groonga::Context
- Inherits:
-
Object
- Object
- Groonga::Context
- Defined in:
- ext/groonga/rb-grn-context.c,
lib/groonga/context.rb,
lib/groonga/context/command-executor.rb
Overview
groonga全体に渡る情報を管理するオブジェクト。通常のアプリ ケーションでは1つのコンテキストを作成し、それを利用する。 複数のコンテキストを利用する必要はない。
デフォルトで使用されるコンテキストは default でアクセスできる。コンテキ ストを指定できる箇所でコンテキストの指定を省略したり +nil+ を指定した場合は default が利用さ れる。
また、デフォルトのコンテキストは必要になると暗黙のうちに 作成される。そのため、コンテキストを意識することは少ない。
暗黙のうちに作成されるコンテキストにオプションを指定する 場合は default_options= を使用 する。
Defined Under Namespace
Classes: CommandExecutor
Instance Method Summary collapse
-
#conf ⇒ Groonga::Config
deprecated
Deprecated.
since 5.1.1. Use #config instead.
-
#config ⇒ Groonga::Config
The database level configuration sets of this context.
-
#create_database(path = nil, &block) ⇒ Object
This is convenience method.
- #execute_command(name, parameters = {}) ⇒ Object
- #object_created(object) ⇒ Object private
-
#open_database(path, &block) ⇒ Object
path にある既存のデータベースを開く。ブロックを指定した場 合はブロックに開いたデータベースを渡し、ブロックを抜けると きに閉じる。.
-
#pop_memory_pool ⇒ void
Pops the pushed memory pool.
-
#push_memory_pool ⇒ Object
Pushes a new memory pool to the context.
-
#register_plugin(name_or_options) ⇒ Object
groongaのプラグインディレクトリにあるプラグイン name を登録する。 path を指定するとプラグインディレクトリ以 外にあるプラグインを登録することができる。.
-
#restore(dumped_commands) {|command, response| ... } ⇒ void
Restore commands dumped by "grndump" command.
-
#select(table, options = {}) ⇒ Object
table から指定した条件にマッチするレコードの値を取得 する。 table はテーブル名かテーブルオブジェクトを指定 する。.
-
#unregister_plugin(name_or_path) ⇒ Object
Unregister a registered
name
plugin.
Instance Method Details
#conf ⇒ Groonga::Config
since 5.1.1. Use #config instead.
Returns The database level configuration sets of this context.
355 356 357 |
# File 'lib/groonga/context.rb', line 355 def conf config end |
#config ⇒ Groonga::Config
Returns The database level configuration sets of this context.
363 364 365 |
# File 'lib/groonga/context.rb', line 363 def config @config ||= Config.new(self) end |
#create_database ⇒ Groonga::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.
100 101 102 103 104 105 106 107 |
# File 'lib/groonga/context.rb', line 100 def create_database(path=nil, &block) = {:context => self} if path [:path] = path end Database.create(, &block) end |
#execute_command(name, parameters = {}) ⇒ Object
169 170 171 172 |
# File 'lib/groonga/context.rb', line 169 def execute_command(name, parameters={}) executor = CommandExecutor.new(self) executor.execute(name, parameters) end |
#object_created(object) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
344 345 346 347 348 |
# File 'lib/groonga/context.rb', line 344 def object_created(object) return if @memory_pools.empty? memory_pool = @memory_pools.last memory_pool.register(object) end |
#open_database(path, &block) ⇒ Object
path にある既存のデータベースを開く。ブロックを指定した場 合はブロックに開いたデータベースを渡し、ブロックを抜けると きに閉じる。
26 27 28 29 30 |
# File 'lib/groonga/context.rb', line 26 def open_database(path, &block) = {:context => self} Database.open(path, , &block) end |
#pop_memory_pool ⇒ void
This method returns an undefined value.
Pops the pushed memory pool.
338 339 340 341 |
# File 'lib/groonga/context.rb', line 338 def pop_memory_pool memory_pool = @memory_pools.pop memory_pool.close end |
#push_memory_pool ⇒ void #push_memory_pool({}) { ... } ⇒ Object
Pushes a new memory pool to the context. Temporary objects that are created between pushing a new memory pool and popping the new memory pool are closed automatically when popping the new memory pool.
It is useful for request and response style applications. These style applications can close temporary objects between a request and resopnse pair. There are some merits for closing temporary objects explicilty rather than closing implicitly by GC:
- Less memory consumption
- Faster
The "less memory consumption" merit is caused by temporary objects are closed each request and response pair. The max memory consumption in these applications is the same as the max memory consumption in a request and response pair. If temporary objects are closed by GC, the max memory consumption in these applications is the same as the max memory consumption between the current GC and the next GC. These applications process many request and response pairs during two GCs.
The "faster" merit is caused by reducing GC. You can reduce GC, your application run faster because GC is a heavy process. You can reduce GC because memory consumption is reduced.
You can nest #push_memory_pool and #pop_memory_pool pair.
319 320 321 322 323 324 325 326 327 328 329 |
# File 'lib/groonga/context.rb', line 319 def push_memory_pool memory_pool = MemoryPool.new @memory_pools.push(memory_pool) return unless block_given? begin yield ensure pop_memory_pool end end |
#register_plugin(name_or_options) ⇒ Object
groongaのプラグインディレクトリにあるプラグイン name を登録する。 path を指定するとプラグインディレクトリ以 外にあるプラグインを登録することができる。
112 113 114 115 116 117 118 119 120 |
# File 'lib/groonga/context.rb', line 112 def register_plugin() = {:context => self} if .is_a?(String) name = Plugin.register(name, ) else Plugin.register(.merge()) 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.
202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 |
# File 'lib/groonga/context.rb', line 202 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 に指定できるキーは以下の通り。
165 166 167 |
# File 'lib/groonga/context.rb', line 165 def select(table, ={}) execute_command("select", {:table => table}.merge()) end |
#unregister_plugin(name) ⇒ Object #unregister_plugin(path) ⇒ Object
Unregister a registered name
plugin.
You can unregister name
plugin by name if
name
plugin is installed to plugin directory.
You can also specify the path of name
plugin explicitly.
148 149 150 151 |
# File 'lib/groonga/context.rb', line 148 def unregister_plugin(name_or_path) = {:context => self} Plugin.unregister(name_or_path, ) end |