| 1 | module Groonga |
| 2 | class Command |
| 3 | include Loggable |
| 4 | include QueryLoggable |
| 5 | |
| 6 | @@classes = {} |
| 7 | class << self |
| 8 | def register_class(name, klass) |
| 9 | @@classes[name] = klass |
| 10 | end |
| 11 | |
| 12 | def find_class(name) |
| 13 | @@classes[name] |
| 14 | end |
| 15 | end |
| 16 | |
| 17 | private |
| 18 | def context |
| 19 | @context ||= Context.instance |
| 20 | end |
| 21 | |
| 22 | def writer |
| 23 | @writer ||= context.writer |
| 24 | end |
| 25 | |
| 26 | def cache_key(input) |
| 27 | nil |
| 28 | end |
| 29 | |
| 30 | def cache_output(key, options={}) |
| 31 | if key.nil? |
| 32 | yield |
| 33 | else |
| 34 | cache = Cache.current |
| 35 | cached_value = cache.fetch(key) |
| 36 | if cached_value |
| 37 | context.output = cached_value |
| 38 | query_logger.log(:cache, ":", "cache(#{cached_value.bytesize})") |
| 39 | else |
| 40 | yield |
| 41 | cache.update(key, context.output) if options[:update] |
| 42 | end |
| 43 | end |
| 44 | end |
| 45 | |
| 46 | def run_internal(input) |
| 47 | begin |
| 48 | options = { |
| 49 | :update => (input["cache"] != "no"), |
| 50 | } |
| 51 | cache_output(cache_key(input), options) do |
| 52 | run_body(input) |
| 53 | end |
| 54 | rescue GroongaError => groonga_error |
| 55 | context.set_groonga_error(groonga_error) |
| 56 | nil |
| 57 | rescue => error |
| 58 | context.record_error(:command_error, error) |
| 59 | nil |
| 60 | end |
| 61 | end |
| 62 | end |
| 63 | end |