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.



105
106
107
108
109
110
111
112
113
# File 'lib/droonga/plugins/search/distributed_search_planner.rb', line 105

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.



103
104
105
# File 'lib/droonga/plugins/search/distributed_search_planner.rb', line 103

def mappers
  @mappers
end

#reducersObject (readonly)

Returns the value of attribute reducers.



103
104
105
# File 'lib/droonga/plugins/search/distributed_search_planner.rb', line 103

def reducers
  @reducers
end

Instance Method Details

#array_style_attributesObject



340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
# File 'lib/droonga/plugins/search/distributed_search_planner.rb', line 340

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



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

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



247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
# File 'lib/droonga/plugins/search/distributed_search_planner.rb', line 247

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

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

#build_elapsed_time_mapper_and_reducer!Object



273
274
275
276
277
278
279
# File 'lib/droonga/plugins/search/distributed_search_planner.rb', line 273

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

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

#build_records_mapper_and_reducer!Object



293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
# File 'lib/droonga/plugins/search/distributed_search_planner.rb', line 293

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



396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
# File 'lib/droonga/plugins/search/distributed_search_planner.rb', line 396

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



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
168
169
170
171
172
173
174
# File 'lib/droonga/plugins/search/distributed_search_planner.rb', line 134

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 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



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

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

#calculate_sort_offset!Object



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

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



218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
# File 'lib/droonga/plugins/search/distributed_search_planner.rb', line 218

def final_limit
  return @original_output_limit if @dataset.single_slice?

  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



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

def final_offset
  return @original_output_offset if @dataset.single_slice?

  @original_sort_offset + @original_output_offset
end

#have_records?Boolean

Returns:

  • (Boolean)


235
236
237
# File 'lib/droonga/plugins/search/distributed_search_planner.rb', line 235

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

#output_attribute_namesObject



317
318
319
320
321
322
323
324
325
326
327
328
329
330
# File 'lib/droonga/plugins/search/distributed_search_planner.rb', line 317

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



204
205
206
# File 'lib/droonga/plugins/search/distributed_search_planner.rb', line 204

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

#output_offsetObject



192
193
194
# File 'lib/droonga/plugins/search/distributed_search_planner.rb', line 192

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

#rich_sort?Boolean

Returns:

  • (Boolean)


239
240
241
# File 'lib/droonga/plugins/search/distributed_search_planner.rb', line 239

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

#sort_attribute_namesObject



381
382
383
384
385
386
387
388
389
390
391
# File 'lib/droonga/plugins/search/distributed_search_planner.rb', line 381

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



196
197
198
199
200
201
202
# File 'lib/droonga/plugins/search/distributed_search_planner.rb', line 196

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

#sort_offsetObject



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

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

#source_column_namesObject



361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
# File 'lib/droonga/plugins/search/distributed_search_planner.rb', line 361

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



115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/droonga/plugins/search/distributed_search_planner.rb', line 115

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)


243
244
245
# File 'lib/droonga/plugins/search/distributed_search_planner.rb', line 243

def unifiable?
  @output["unifiable"]
end

#update_output_attributes!Object



332
333
334
335
336
337
338
# File 'lib/droonga/plugins/search/distributed_search_planner.rb', line 332

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