summaryrefslogtreecommitdiff
path: root/storage/mroonga/vendor/groonga/lib/mrb/scripts/command.rb
blob: 26dfe68cd010cf76f572ae9ad30f0bae93fd1357 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
module Groonga
  class Command
    @@classes = {}
    class << self
      def register_class(name, klass)
        @@classes[name] = klass
      end

      def find_class(name)
        @@classes[name]
      end
    end

    private
    def context
      @context ||= Context.instance
    end

    def writer
      @writer ||= context.writer
    end

    def query_logger
      @query_logger ||= context.query_logger
    end

    def cache_key(input)
      nil
    end

    def cache_output(key, options={})
      if key.nil?
        yield
      else
        cache = Cache.current
        cached_value = cache.fetch(key)
        if cached_value
          context.output = cached_value
          query_logger.log(:cache, ":", "cache(#{cached_value.bytesize})")
        else
          yield
          cache.update(key, context.output) if options[:update]
        end
      end
    end

    def run_internal(input)
      begin
        options = {
          :update => (input["cache"] != "no"),
        }
        cache_output(cache_key(input), options) do
          run_body(input)
        end
      rescue GroongaError => groonga_error
        context.set_groonga_error(groonga_error)
        nil
      rescue => error
        context.record_error(:command_error, error)
        nil
      end
    end
  end
end