Class: Relaxo::QueryServer::Context

Inherits:
Object
  • Object
show all
Defined in:
lib/relaxo/query_server/context.rb

Overview

A query server context includes all state required for implementing the query server protocol.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(shell, options = {}) ⇒ Context

Returns a new instance of Context.



46
47
48
49
50
51
52
53
54
55
# File 'lib/relaxo/query_server/context.rb', line 46

def initialize(shell, options = {})
  @shell = shell
  @options = options
  
  @mapper = Mapper.new(self)
  @reducer = Reducer.new(self)
  @designer = Designer.new(self)
  
  @config = {}
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



57
58
59
# File 'lib/relaxo/query_server/context.rb', line 57

def config
  @config
end

#shellObject (readonly)

Returns the value of attribute shell.



58
59
60
# File 'lib/relaxo/query_server/context.rb', line 58

def shell
  @shell
end

Instance Method Details

#error_for_exception(exception) ⇒ Object

Return an error structure from the given exception.



61
62
63
# File 'lib/relaxo/query_server/context.rb', line 61

def error_for_exception(exception)
  ["error", exception.class.to_s, exception.message, exception.backtrace]
end

#log(message) ⇒ Object

Write a log message back to the shell.



99
100
101
# File 'lib/relaxo/query_server/context.rb', line 99

def log(message)
  @shell.write_object ['log', message]
end

#parse_function(text, scope, filename = 'query-server') ⇒ Object

Given a function as text, and an execution scope, return a callable object.



34
35
36
37
38
39
40
41
42
43
44
# File 'lib/relaxo/query_server/context.rb', line 34

def parse_function(text, scope, filename = 'query-server')
  safe_level = @options[:safe] || 0

  function = lambda { $SAFE = safe_level; eval(text, scope, filename) }.call

  unless function.respond_to? :call
    raise CompilationError.new('Expression does not evaluate to procedure!')
  end
  
  return function
end

#run(command) ⇒ Object

Process a single command as per the query server protocol.



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/relaxo/query_server/context.rb', line 66

def run(command)
  case command[0]
  # ** Map functionality
  when 'add_fun'
    @mapper.add_function command[1]; true
  when 'add_lib'
    @mapper.add_libraries command[1]
    @reducer.add_libraries command[1]; true
  when 'map_doc'
    @mapper.map command[1]
  when 'reset'
    @config = command[1] || {}
    @mapper = Mapper.new(self); true
  
  # ** Reduce functionality
  when 'reduce'
    @reducer.reduce command[1], command[2]
  when 'rereduce'
    @reducer.rereduce command[1], command[2]
  
  # ** Design document functionality
  when 'ddoc'
    if command[1] == 'new'
      @designer.create(command[2], command[3]); true
    else
      @designer.run(command[1], command[2], command[3])
    end
  end
rescue Exception => exception
  error_for_exception(exception)
end