Class: Droonga::Plugins::Groonga::Select::RequestConverter

Inherits:
Object
  • Object
show all
Defined in:
lib/droonga/plugins/groonga/select.rb

Constant Summary collapse

COMMAND_VERSION_1_ONLY_PATTERN =

for a backward compatibility for command_version=1, whitespace-separeted case (without functions) should be accepted.

/\A[^\s,()]+(\s+[^\s,()]+)+\z/

Instance Method Summary collapse

Instance Method Details

#apply_query_flags(condition, flags) ⇒ Object



118
119
120
121
122
123
124
125
# File 'lib/droonga/plugins/groonga/select.rb', line 118

def apply_query_flags(condition, flags)
  flags ||= DEFAULT_QUERY_FLAGS
  flags = flags.split("|")
  condition["allowPragma"] = flags.include?("ALLOW_PRAGMA")
  condition["allowColumn"] = flags.include?("ALLOW_COLUMN")
  condition["allowUpdate"] = flags.include?("ALLOW_UPDATE")
  condition["allowLeadingNot"] = flags.include?("ALLOW_LEADING_NOT")
end

#convert(select_request) ⇒ Object



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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/droonga/plugins/groonga/select.rb', line 26

def convert(select_request)
  @table = select_request["table"]
  @result_name = @table + "_result"

  output_columns = select_request["output_columns"] || "_id, _key, *"
  attributes = convert_output_columns(output_columns)
  offset = (select_request["offset"] || "0").to_i
  limit = (select_request["limit"] || "10").to_i

  output_offset = offset
  output_limit = limit

  sort_by = nil
  sort_keys = (select_request["sortby"] || "").split(",")
  unless sort_keys.empty?
    sort_by = {
      "keys" => sort_keys,
      "offset" => offset,
      "limit" => limit,
    }
    output_offset = 0
  end

  search_request = {
    "queries" => {
      @result_name => {
        "source" => @table,
        "output" => {
          "elements"   => [
            "startTime",
            "elapsedTime",
            "count",
            "attributes",
            "records",
          ],
          "attributes" => attributes,
          "offset" => output_offset,
          "limit" => output_limit,
        },
      }
    }
  }
  if sort_by
    search_request["queries"][@result_name]["sortBy"] = sort_by
  end

  condition = convert_condition(select_request)
  if condition
    search_request["queries"][@result_name]["condition"] = condition
  end

  drilldown_queries = convert_drilldown(select_request)
  if drilldown_queries
    search_request["queries"].merge!(drilldown_queries)
  end

  search_request
end

#convert_condition(select_request) ⇒ Object



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/droonga/plugins/groonga/select.rb', line 85

def convert_condition(select_request)
  match_columns = select_request["match_columns"]
  match_to = match_columns ? match_columns.split(/ *\|\| */) : []
  query = select_request["query"]
  filter = select_request["filter"]

  conditions = []
  if query
    condition = {
      "query"  => query,
      "matchTo"=> match_to,
      "defaultOperator"=> "&&",
    }
    apply_query_flags(condition, select_request["query_flags"])
    conditions << condition
  end

  if filter
    conditions << filter
  end

  condition = nil

  case conditions.size
  when 1
    condition = conditions.first
  when 2
    condition = ["&&"] + conditions
  end

  condition
end

#convert_drilldown(select_request) ⇒ Object



127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
# File 'lib/droonga/plugins/groonga/select.rb', line 127

def convert_drilldown(select_request)
  drilldown_keys = select_request["drilldown"]
  return nil if drilldown_keys.nil? or drilldown_keys.empty?

  drilldown_keys = drilldown_keys.split(",")

  sort_keys = (select_request["drilldown_sortby"] || "").split(",")
  columns   = convert_output_columns(select_request["drilldown_output_columns"] || "_key,_nsubrecs")
  offset    = (select_request["drilldown_offset"] || "0").to_i
  limit     = (select_request["drilldown_limit"] || "10").to_i

  queries = {}
  drilldown_keys.each_with_index do |key, index|
    query = {
      "source" => @result_name,
      "groupBy" => key,
      "output" => {
        "elements"   => [
          "count",
          "attributes",
          "records",
        ],
        "attributes" => columns,
        "limit" => limit,
      },
    }

    if sort_keys.empty?
      query["output"]["offset"] = offset
    else
      query["sortBy"] = {
        "keys"   => sort_keys,
        "offset" => offset,
        "limit"  => limit,
      }
    end

    queries["#{DRILLDOWN_RESULT_PREFIX}#{key}"] = query
  end
  queries
end

#convert_output_columns(output_columns) ⇒ Object



173
174
175
176
177
178
179
180
181
# File 'lib/droonga/plugins/groonga/select.rb', line 173

def convert_output_columns(output_columns)
  output_columns = output_columns.strip
  command_version_is_1 = output_columns =~ COMMAND_VERSION_1_ONLY_PATTERN
  if command_version_is_1
    output_columns.split(/\s+/)
  else
    output_columns.split(/\s*,\s*/)
  end
end