Class: Droonga::DistributedSearchPlanner::QueryTransformer

Inherits:
Object
  • Object
show all
Defined in:
lib/droonga/plugin/distributor/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.



119
120
121
122
123
124
125
126
# File 'lib/droonga/plugin/distributor/distributed_search_planner.rb', line 119

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.



117
118
119
# File 'lib/droonga/plugin/distributor/distributed_search_planner.rb', line 117

def mappers
  @mappers
end

#reducersObject (readonly)

Returns the value of attribute reducers.



117
118
119
# File 'lib/droonga/plugin/distributor/distributed_search_planner.rb', line 117

def reducers
  @reducers
end

Instance Method Details

#array_style_attributesObject



311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
# File 'lib/droonga/plugin/distributor/distributed_search_planner.rb', line 311

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



242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
# File 'lib/droonga/plugin/distributor/distributed_search_planner.rb', line 242

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 = {
      "type" => "count",
      "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



265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
# File 'lib/droonga/plugin/distributor/distributed_search_planner.rb', line 265

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 = {
    "type" => "sort",
    "offset" => @records_offset,
    "limit" => @records_limit,
    "format" => @records_format,
    "attributes" => final_attributes,
  }
  mapper["no_output"] = true unless @output_records
  @mappers["records"] = mapper
end

#build_records_reducerObject



367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
# File 'lib/droonga/plugin/distributor/distributed_search_planner.rb', line 367

def build_records_reducer
  attributes = @output["attributes"]
  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



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/plugin/distributor/distributed_search_planner.rb', line 143

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

  if final_limit == UNLIMITED
    @output["limit"] = UNLIMITED
  else
    if rich_sort?
      @query["sortBy"]["limit"] = final_offset + [sort_limit, output_limit].max
    end
    @output["limit"] = final_offset + final_limit
  end
end

#calculate_output_offset!Object



208
209
210
# File 'lib/droonga/plugin/distributor/distributed_search_planner.rb', line 208

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

#calculate_sort_offset!Object



176
177
178
179
180
181
182
# File 'lib/droonga/plugin/distributor/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



216
217
218
219
220
221
222
223
224
225
226
227
228
# File 'lib/droonga/plugin/distributor/distributed_search_planner.rb', line 216

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



212
213
214
# File 'lib/droonga/plugin/distributor/distributed_search_planner.rb', line 212

def final_offset
  @original_sort_offset + @original_output_offset
end

#have_records?Boolean

Returns:

  • (Boolean)


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

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

#output_attribute_namesObject



288
289
290
291
292
293
294
295
296
297
298
299
300
301
# File 'lib/droonga/plugin/distributor/distributed_search_planner.rb', line 288

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/plugin/distributor/distributed_search_planner.rb', line 204

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

#output_offsetObject



192
193
194
# File 'lib/droonga/plugin/distributor/distributed_search_planner.rb', line 192

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

#rich_sort?Boolean

Returns:

  • (Boolean)


234
235
236
# File 'lib/droonga/plugin/distributor/distributed_search_planner.rb', line 234

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

#sort_attribute_namesObject



352
353
354
355
356
357
358
359
360
361
362
# File 'lib/droonga/plugin/distributor/distributed_search_planner.rb', line 352

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/plugin/distributor/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/plugin/distributor/distributed_search_planner.rb', line 184

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

#source_column_namesObject



332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
# File 'lib/droonga/plugin/distributor/distributed_search_planner.rb', line 332

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



128
129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/droonga/plugin/distributor/distributed_search_planner.rb', line 128

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"
  @output["format"] = "simple"

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


238
239
240
# File 'lib/droonga/plugin/distributor/distributed_search_planner.rb', line 238

def unifiable?
  @output["unifiable"]
end

#update_output_attributes!Object



303
304
305
306
307
308
309
# File 'lib/droonga/plugin/distributor/distributed_search_planner.rb', line 303

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