Class: Droonga::Plugins::Search::DistributedSearchPlanner::QueryTransformer
- Inherits:
-
Object
- Object
- Droonga::Plugins::Search::DistributedSearchPlanner::QueryTransformer
- Defined in:
- lib/droonga/plugins/search/distributed_search_planner.rb
Constant Summary collapse
- ASCENDING_OPERATOR =
"<"- DESCENDING_OPERATOR =
">"
Instance Attribute Summary collapse
-
#mappers ⇒ Object
readonly
Returns the value of attribute mappers.
-
#reducers ⇒ Object
readonly
Returns the value of attribute reducers.
Instance Method Summary collapse
- #array_style_attributes ⇒ Object
- #build_count_mapper_and_reducer! ⇒ Object
- #build_elapsed_time_mapper_and_reducer! ⇒ Object
- #build_records_mapper_and_reducer! ⇒ Object
- #build_records_reducer ⇒ Object
- #calculate_offset_and_limit! ⇒ Object
- #calculate_output_offset! ⇒ Object
- #calculate_sort_offset! ⇒ Object
- #final_limit ⇒ Object
- #final_offset ⇒ Object
- #have_records? ⇒ Boolean
-
#initialize(query) ⇒ QueryTransformer
constructor
A new instance of QueryTransformer.
- #output_attribute_names ⇒ Object
- #output_limit ⇒ Object
- #output_offset ⇒ Object
- #rich_sort? ⇒ Boolean
- #sort_attribute_names ⇒ Object
- #sort_limit ⇒ Object
- #sort_offset ⇒ Object
- #source_column_names ⇒ Object
- #transform! ⇒ Object
- #unifiable? ⇒ Boolean
- #update_output_attributes! ⇒ Object
Constructor Details
#initialize(query) ⇒ QueryTransformer
Returns a new instance of QueryTransformer.
102 103 104 105 106 107 108 109 |
# File 'lib/droonga/plugins/search/distributed_search_planner.rb', line 102 def initialize(query) @query = query @output = @query["output"] @reducers = {} @mappers = {} @output_records = true transform! end |
Instance Attribute Details
#mappers ⇒ Object (readonly)
Returns the value of attribute mappers.
100 101 102 |
# File 'lib/droonga/plugins/search/distributed_search_planner.rb', line 100 def mappers @mappers end |
#reducers ⇒ Object (readonly)
Returns the value of attribute reducers.
100 101 102 |
# File 'lib/droonga/plugins/search/distributed_search_planner.rb', line 100 def reducers @reducers end |
Instance Method Details
#array_style_attributes ⇒ Object
315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 |
# File 'lib/droonga/plugins/search/distributed_search_planner.rb', line 315 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
238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 |
# File 'lib/droonga/plugins/search/distributed_search_planner.rb', line 238 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_elapsed_time_mapper_and_reducer! ⇒ Object
260 261 262 263 264 265 266 |
# File 'lib/droonga/plugins/search/distributed_search_planner.rb', line 260 def build_elapsed_time_mapper_and_reducer! return unless @output["elements"].include?("elapsedTime") @reducers["elapsedTime"] = { "type" => "sum", } end |
#build_records_mapper_and_reducer! ⇒ Object
268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 |
# File 'lib/droonga/plugins/search/distributed_search_planner.rb', line 268 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_reducer ⇒ Object
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 399 400 401 402 |
# File 'lib/droonga/plugins/search/distributed_search_planner.rb', line 371 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
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 168 169 |
# File 'lib/droonga/plugins/search/distributed_search_planner.rb', line 129 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
203 204 205 |
# File 'lib/droonga/plugins/search/distributed_search_planner.rb', line 203 def calculate_output_offset! @output["offset"] = 0 if have_records? and @output["offset"] end |
#calculate_sort_offset! ⇒ Object
171 172 173 174 175 176 177 |
# File 'lib/droonga/plugins/search/distributed_search_planner.rb', line 171 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_limit ⇒ Object
211 212 213 214 215 216 217 218 219 220 221 222 223 224 |
# File 'lib/droonga/plugins/search/distributed_search_planner.rb', line 211 def final_limit 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_offset ⇒ Object
207 208 209 |
# File 'lib/droonga/plugins/search/distributed_search_planner.rb', line 207 def final_offset @original_sort_offset + @original_output_offset end |
#have_records? ⇒ Boolean
226 227 228 |
# File 'lib/droonga/plugins/search/distributed_search_planner.rb', line 226 def have_records? @output["elements"].include?("records") end |
#output_attribute_names ⇒ Object
292 293 294 295 296 297 298 299 300 301 302 303 304 305 |
# File 'lib/droonga/plugins/search/distributed_search_planner.rb', line 292 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_limit ⇒ Object
199 200 201 |
# File 'lib/droonga/plugins/search/distributed_search_planner.rb', line 199 def output_limit @output["limit"] || 0 end |
#output_offset ⇒ Object
187 188 189 |
# File 'lib/droonga/plugins/search/distributed_search_planner.rb', line 187 def output_offset @output["offset"] || 0 end |
#rich_sort? ⇒ Boolean
230 231 232 |
# File 'lib/droonga/plugins/search/distributed_search_planner.rb', line 230 def rich_sort? @query["sortBy"].is_a?(Hash) end |
#sort_attribute_names ⇒ Object
356 357 358 359 360 361 362 363 364 365 366 |
# File 'lib/droonga/plugins/search/distributed_search_planner.rb', line 356 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_limit ⇒ Object
191 192 193 194 195 196 197 |
# File 'lib/droonga/plugins/search/distributed_search_planner.rb', line 191 def sort_limit if rich_sort? @query["sortBy"]["limit"] || UNLIMITED else UNLIMITED end end |
#sort_offset ⇒ Object
179 180 181 182 183 184 185 |
# File 'lib/droonga/plugins/search/distributed_search_planner.rb', line 179 def sort_offset if rich_sort? @query["sortBy"]["offset"] || 0 else 0 end end |
#source_column_names ⇒ Object
336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 |
# File 'lib/droonga/plugins/search/distributed_search_planner.rb', line 336 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
111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 |
# File 'lib/droonga/plugins/search/distributed_search_planner.rb', line 111 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_records_mapper_and_reducer! end |
#unifiable? ⇒ Boolean
234 235 236 |
# File 'lib/droonga/plugins/search/distributed_search_planner.rb', line 234 def unifiable? @output["unifiable"] end |
#update_output_attributes! ⇒ Object
307 308 309 310 311 312 313 |
# File 'lib/droonga/plugins/search/distributed_search_planner.rb', line 307 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 |