Class: Droonga::Plugins::Search::DistributedSearchPlanner::QueryTransformer

Inherits:
Object
  • Object
show all
Defined in:
lib/droonga/plugins/search/distributed_search_planner.rb

Constant Summary collapse

ASCENDING_OPERATOR =
"<"
DESCENDING_OPERATOR =
">"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(dataset, query) ⇒ QueryTransformer

Returns a new instance of QueryTransformer.



111
112
113
114
115
116
117
118
119
# File 'lib/droonga/plugins/search/distributed_search_planner.rb', line 111

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

Instance Attribute Details

#mappersObject (readonly)

Returns the value of attribute mappers.



109
110
111
# File 'lib/droonga/plugins/search/distributed_search_planner.rb', line 109

def mappers
  @mappers
end

#reducersObject (readonly)

Returns the value of attribute reducers.



109
110
111
# File 'lib/droonga/plugins/search/distributed_search_planner.rb', line 109

def reducers
  @reducers
end

Instance Method Details

#array_style_attributesObject



352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
# File 'lib/droonga/plugins/search/distributed_search_planner.rb', line 352

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_attributes_mapper_and_reducer!Object



293
294
295
296
297
298
299
300
301
302
303
# File 'lib/droonga/plugins/search/distributed_search_planner.rb', line 293

def build_attributes_mapper_and_reducer!
  return unless @output["elements"].include?("attributes")

  @reducers["attributes"] = {
    "type" => "or",
  }

  @mappers["attributes"] = {
    "names" => output_attribute_names,
  }
end

#build_count_mapper_and_reducer!Object



257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
# File 'lib/droonga/plugins/search/distributed_search_planner.rb', line 257

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

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

#build_elapsed_time_mapper_and_reducer!Object



285
286
287
288
289
290
291
# File 'lib/droonga/plugins/search/distributed_search_planner.rb', line 285

def build_elapsed_time_mapper_and_reducer!
  return unless @output["elements"].include?("elapsedTime")

  @reducers["elapsedTime"] = {
    "type" => "sum",
  }
end

#build_records_mapper_and_reducer!Object



305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
# File 'lib/droonga/plugins/search/distributed_search_planner.rb', line 305

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



408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
# File 'lib/droonga/plugins/search/distributed_search_planner.rb', line 408

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? and !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



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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
# File 'lib/droonga/plugins/search/distributed_search_planner.rb', line 140

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

  return unless @dataset.sliced?

  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 and updated_sort_limit != @query["sortBy"]["limit"]
    @query["sortBy"]["limit"] = updated_sort_limit
  end
  if updated_output_limit and @output["limit"] and updated_output_limit != @output["limit"]
    @output["limit"] = updated_output_limit
  end
end

#calculate_output_offset!Object



217
218
219
220
# File 'lib/droonga/plugins/search/distributed_search_planner.rb', line 217

def calculate_output_offset!
  return unless @dataset.sliced?
  @output["offset"] = 0 if have_records? and @output["offset"]
end

#calculate_sort_offset!Object



184
185
186
187
188
189
190
191
# File 'lib/droonga/plugins/search/distributed_search_planner.rb', line 184

def calculate_sort_offset!
  return unless @dataset.sliced?
  # 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



228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
# File 'lib/droonga/plugins/search/distributed_search_planner.rb', line 228

def final_limit
  return UNLIMITED unless @dataset.sliced?

  if @original_sort_limit == UNLIMITED and
      @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



222
223
224
225
226
# File 'lib/droonga/plugins/search/distributed_search_planner.rb', line 222

def final_offset
  return 0 unless @dataset.sliced?

  @original_sort_offset + @original_output_offset
end

#have_records?Boolean

Returns:

  • (Boolean)


245
246
247
# File 'lib/droonga/plugins/search/distributed_search_planner.rb', line 245

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

#output_attribute_namesObject



329
330
331
332
333
334
335
336
337
338
339
340
341
342
# File 'lib/droonga/plugins/search/distributed_search_planner.rb', line 329

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



213
214
215
# File 'lib/droonga/plugins/search/distributed_search_planner.rb', line 213

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

#output_offsetObject



201
202
203
# File 'lib/droonga/plugins/search/distributed_search_planner.rb', line 201

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

#rich_sort?Boolean

Returns:

  • (Boolean)


249
250
251
# File 'lib/droonga/plugins/search/distributed_search_planner.rb', line 249

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

#sort_attribute_namesObject



393
394
395
396
397
398
399
400
401
402
403
# File 'lib/droonga/plugins/search/distributed_search_planner.rb', line 393

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



205
206
207
208
209
210
211
# File 'lib/droonga/plugins/search/distributed_search_planner.rb', line 205

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

#sort_offsetObject



193
194
195
196
197
198
199
# File 'lib/droonga/plugins/search/distributed_search_planner.rb', line 193

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

#source_column_namesObject



373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
# File 'lib/droonga/plugins/search/distributed_search_planner.rb', line 373

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



121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/droonga/plugins/search/distributed_search_planner.rb', line 121

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"] and @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_elapsed_time_mapper_and_reducer!
  build_attributes_mapper_and_reducer!
  build_records_mapper_and_reducer!
end

#unifiable?Boolean

Returns:

  • (Boolean)


253
254
255
# File 'lib/droonga/plugins/search/distributed_search_planner.rb', line 253

def unifiable?
  @output["unifiable"]
end

#update_output_attributes!Object



344
345
346
347
348
349
350
# File 'lib/droonga/plugins/search/distributed_search_planner.rb', line 344

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