Class: Droonga::DistributedSearchPlanner::QueryTransformer

Inherits:
Object
  • Object
show all
Defined in:
lib/droonga/plugin/planner/distributed_search_planner.rb

Constant Summary collapse

ASCENDING_OPERATOR =
"<"
DESCENDING_OPERATOR =
">"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(query) ⇒ QueryTransformer

Returns a new instance of QueryTransformer.



100
101
102
103
104
105
106
107
# File 'lib/droonga/plugin/planner/distributed_search_planner.rb', line 100

def initialize(query)
  @query = query
  @output = @query["output"]
  @reducers = {}
  @mappers = {}
  @output_records = true
  transform!
end

Instance Attribute Details

#mappersObject (readonly)

Returns the value of attribute mappers.



98
99
100
# File 'lib/droonga/plugin/planner/distributed_search_planner.rb', line 98

def mappers
  @mappers
end

#reducersObject (readonly)

Returns the value of attribute reducers.



98
99
100
# File 'lib/droonga/plugin/planner/distributed_search_planner.rb', line 98

def reducers
  @reducers
end

Instance Method Details

#array_style_attributesObject



303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
# File 'lib/droonga/plugin/planner/distributed_search_planner.rb', line 303

def array_style_attributes
  attributes = @output["attributes"] || []
  if attributes.is_a?(Hash)
    attributes.keys.collect do |key|
      attribute = attributes[key]
      case attribute
      when String
        {
          "label"  => key,
          "source" => attribute,
        }
      when Hash
        attribute["label"] = key
        attribute
      end
    end
  else
    attributes
  end
end

#build_count_mapper_and_reducer!Object



234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
# File 'lib/droonga/plugin/planner/distributed_search_planner.rb', line 234

def build_count_mapper_and_reducer!
  return unless @output["elements"].include?("count")

  @reducers["count"] = {
    "type" => "sum",
  }
  if unifiable?
    @query["sortBy"]["limit"] = -1 if @query["sortBy"].is_a?(Hash)
    @output["limit"] = -1
    mapper = {
      "target" => "records",
    }
    unless @output["elements"].include?("records")
      @records_limit = -1
      @output["elements"] << "records"
      @output["attributes"] ||= ["_key"]
      @output_records = false
    end
    @mappers["count"] = mapper
  end
end

#build_records_mapper_and_reducer!Object



256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
# File 'lib/droonga/plugin/planner/distributed_search_planner.rb', line 256

def build_records_mapper_and_reducer!
  # Skip reducing phase for a result with no record output.
  return if !@output["elements"].include?("records") || @records_limit.zero?

  # Append sort key attributes to the list of output attributes
  # temporarily, for the reducing phase. After all extra columns
  # are removed on the gathering phase.
  final_attributes = output_attribute_names
  update_output_attributes!

  @reducers["records"] = build_records_reducer

  mapper = {}
  if @output_records
    mapper["format"]     = @records_format unless @records_format == "simple"
    mapper["attributes"] = final_attributes unless final_attributes.empty?
    mapper["offset"]     = @records_offset unless @records_offset.zero?
    mapper["limit"]      = @records_limit unless @records_limit.zero?
  else
    mapper["no_output"] = true
  end
  @mappers["records"] = mapper
end

#build_records_reducerObject



359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
# File 'lib/droonga/plugin/planner/distributed_search_planner.rb', line 359

def build_records_reducer
  attributes = source_column_names
  key_column_index = attributes.index("_key")

  operators = @sort_keys.collect do |sort_key|
    operator = ASCENDING_OPERATOR
    if sort_key[0] == "-"
      operator = DESCENDING_OPERATOR
      sort_key = sort_key[1..-1]
    end
    {
      "operator" => operator,
      "column"   => attributes.index(sort_key),
    }
  end

  reducer = {
    "type"      => "sort",
    "operators" => operators,
  }
  if unifiable? && !key_column_index.nil?
    reducer["key_column"] = key_column_index
  end

  # On the reducing phase, we apply only "limit". We cannot apply
  # "offset" on this phase because the collector merges a pair of
  # results step by step even if there are three or more results.
  # Instead, we apply "offset" on the gathering phase.
  reducer["limit"] = @output["limit"]

  reducer
end

#calculate_offset_and_limit!Object



126
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
# File 'lib/droonga/plugin/planner/distributed_search_planner.rb', line 126

def calculate_offset_and_limit!
  @original_sort_offset = sort_offset
  @original_output_offset = output_offset
  @original_sort_limit = sort_limit
  @original_output_limit = output_limit

  calculate_sort_offset!
  calculate_output_offset!

  # We have to calculate limit based on offset.
  # <A, B = limited integer (0...MAXINT)>
  # | sort limit | output limit | => | worker's sort limit      | worker's output limit   | final limit |
  # =============================    ====================================================================
  # | UNLIMITED  | UNLIMITED    | => | UNLIMITED                | UNLIMITED               | UNLIMITED   |
  # | UNLIMITED  | B            | => | final_offset + B         | final_offset + B        | B           |
  # | A          | UNLIMITED    | => | final_offset + A         | final_offset + A        | A           |
  # | A          | B            | => | final_offset + max(A, B) | final_offset + min(A, B)| min(A, B)   |

  # XXX final_limit and final_offset calculated in many times

  @records_offset = final_offset
  @records_limit = final_limit

  updated_sort_limit = nil
  updated_output_limit = nil
  if final_limit == UNLIMITED
    updated_output_limit = UNLIMITED
  else
    if rich_sort?
      updated_sort_limit = final_offset + [sort_limit, output_limit].max
    end
    updated_output_limit = final_offset + final_limit
  end

  if updated_sort_limit && updated_sort_limit != @query["sortBy"]["limit"]
    @query["sortBy"]["limit"] = updated_sort_limit
  end
  if updated_output_limit && @output["limit"] && updated_output_limit != @output["limit"]
    @output["limit"] = updated_output_limit
  end
end

#calculate_output_offset!Object



200
201
202
# File 'lib/droonga/plugin/planner/distributed_search_planner.rb', line 200

def calculate_output_offset!
  @output["offset"] = 0 if have_records? && @output["offset"]
end

#calculate_sort_offset!Object



168
169
170
171
172
173
174
# File 'lib/droonga/plugin/planner/distributed_search_planner.rb', line 168

def calculate_sort_offset!
  # Offset for workers must be zero, because we have to apply "limit" and
  # "offset" on the last gathering phase instead of each reducing phase.
  if rich_sort?
    @query["sortBy"]["offset"] = 0
  end
end

#final_limitObject



208
209
210
211
212
213
214
215
216
217
218
219
220
# File 'lib/droonga/plugin/planner/distributed_search_planner.rb', line 208

def final_limit
  if @original_sort_limit == UNLIMITED && @original_output_limit == UNLIMITED
    UNLIMITED
  else
    if @original_sort_limit == UNLIMITED
      @original_output_limit
    elsif @original_output_limit == UNLIMITED
      @original_sort_limit
    else
      [@original_sort_limit, @original_output_limit].min
    end
  end
end

#final_offsetObject



204
205
206
# File 'lib/droonga/plugin/planner/distributed_search_planner.rb', line 204

def final_offset
  @original_sort_offset + @original_output_offset
end

#have_records?Boolean

Returns:

  • (Boolean)


222
223
224
# File 'lib/droonga/plugin/planner/distributed_search_planner.rb', line 222

def have_records?
  @output["elements"].include?("records")
end

#output_attribute_namesObject



280
281
282
283
284
285
286
287
288
289
290
291
292
293
# File 'lib/droonga/plugin/planner/distributed_search_planner.rb', line 280

def output_attribute_names
  attributes = @output["attributes"] || []
  if attributes.is_a?(Hash)
    attributes.keys
  else
    attributes.collect do |attribute|
      if attribute.is_a?(Hash)
        attribute["label"] || attribute["source"]
      else
        attribute
      end
    end
  end
end

#output_limitObject



196
197
198
# File 'lib/droonga/plugin/planner/distributed_search_planner.rb', line 196

def output_limit
  @output["limit"] || 0
end

#output_offsetObject



184
185
186
# File 'lib/droonga/plugin/planner/distributed_search_planner.rb', line 184

def output_offset
  @output["offset"] || 0
end

#rich_sort?Boolean

Returns:

  • (Boolean)


226
227
228
# File 'lib/droonga/plugin/planner/distributed_search_planner.rb', line 226

def rich_sort?
  @query["sortBy"].is_a?(Hash)
end

#sort_attribute_namesObject



344
345
346
347
348
349
350
351
352
353
354
# File 'lib/droonga/plugin/planner/distributed_search_planner.rb', line 344

def sort_attribute_names
  sort_attributes = @sort_keys.collect do |key|
    key = key[1..-1] if key[0] == "-"
    key
  end
  attributes = source_column_names
  sort_attributes.reject! do |attribute|
    attributes.include?(attribute)
  end
  sort_attributes
end

#sort_limitObject



188
189
190
191
192
193
194
# File 'lib/droonga/plugin/planner/distributed_search_planner.rb', line 188

def sort_limit
  if rich_sort?
    @query["sortBy"]["limit"] || UNLIMITED
  else
    UNLIMITED
  end
end

#sort_offsetObject



176
177
178
179
180
181
182
# File 'lib/droonga/plugin/planner/distributed_search_planner.rb', line 176

def sort_offset
  if rich_sort?
    @query["sortBy"]["offset"] || 0
  else
    0
  end
end

#source_column_namesObject



324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
# File 'lib/droonga/plugin/planner/distributed_search_planner.rb', line 324

def source_column_names
  attributes = @output["attributes"] || []
  if attributes.is_a?(Hash)
    attributes_hash = attributes
    attributes = []
    attributes_hash.each do |key, attribute|
      attributes << attribute["source"] || key
    end
    attributes
  else
    attributes.collect do |attribute|
      if attribute.is_a?(Hash)
        attribute["source"] || attribute["label"]
      else
        attribute
      end
    end
  end
end

#transform!Object



109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/droonga/plugin/planner/distributed_search_planner.rb', line 109

def transform!
  # The collector module supports only "simple" format search results.
  # So we have to override the format and restore it on the gathering
  # phase.
  @records_format = @output["format"] || "simple"
  if @output["format"] && @output["format"] != "simple"
    @output["format"] = "simple"
  end

  @sort_keys = @query["sortBy"] || []
  @sort_keys = @sort_keys["keys"] || [] if @sort_keys.is_a?(Hash)

  calculate_offset_and_limit!
  build_count_mapper_and_reducer!
  build_records_mapper_and_reducer!
end

#unifiable?Boolean

Returns:

  • (Boolean)


230
231
232
# File 'lib/droonga/plugin/planner/distributed_search_planner.rb', line 230

def unifiable?
  @output["unifiable"]
end

#update_output_attributes!Object



295
296
297
298
299
300
301
# File 'lib/droonga/plugin/planner/distributed_search_planner.rb', line 295

def update_output_attributes!
  @output["attributes"] = array_style_attributes
  @output["attributes"] += sort_attribute_names
  if unifiable? && !source_column_names.include?("_key")
    @output["attributes"] << "_key"
  end
end