| 1 | require "scan_info_search_index" |
| 2 | |
| 3 | module Groonga |
| 4 | class ScanInfoData |
| 5 | attr_accessor :start |
| 6 | attr_accessor :end |
| 7 | attr_accessor :op |
| 8 | attr_accessor :logical_op |
| 9 | attr_accessor :query |
| 10 | attr_accessor :args |
| 11 | attr_accessor :search_indexes |
| 12 | attr_accessor :flags |
| 13 | attr_accessor :max_interval |
| 14 | attr_accessor :additional_last_interval |
| 15 | attr_accessor :max_element_intervals |
| 16 | attr_accessor :min_interval |
| 17 | attr_accessor :similarity_threshold |
| 18 | attr_accessor :quorum_threshold |
| 19 | attr_accessor :start_position |
| 20 | attr_accessor :weight |
| 21 | attr_accessor :weight_factor |
| 22 | def initialize(start) |
| 23 | @start = start |
| 24 | @end = start |
| 25 | @op = Operator::NOP |
| 26 | @logical_op = Operator::OR |
| 27 | @query = nil |
| 28 | @args = [] |
| 29 | @search_indexes = [] |
| 30 | @flags = ScanInfo::Flags::PUSH |
| 31 | @max_interval = nil |
| 32 | @additional_last_interval = nil |
| 33 | @max_element_intervals = nil |
| 34 | @min_interval = nil |
| 35 | @similarity_threshold = nil |
| 36 | @quorum_threshold = nil |
| 37 | @start_position = nil |
| 38 | @weight = 0 |
| 39 | @weight_factor = 1.0 |
| 40 | end |
| 41 | |
| 42 | def match_resolve_index |
| 43 | if near_search? |
| 44 | match_near_resolve_index |
| 45 | elsif similar_search? |
| 46 | match_similar_resolve_index |
| 47 | elsif quorum_match? |
| 48 | match_quorum_resolve_index |
| 49 | else |
| 50 | match_generic_resolve_index |
| 51 | end |
| 52 | end |
| 53 | |
| 54 | def call_relational_resolve_indexes |
| 55 | procedure, *args = *@args |
| 56 | return unless procedure.selector? |
| 57 | |
| 58 | selector_op = procedure.selector_operator |
| 59 | args.each do |arg| |
| 60 | call_relational_resolve_index(arg, selector_op) |
| 61 | end |
| 62 | end |
| 63 | |
| 64 | def unref |
| 65 | @search_indexes.each(&:unref) |
| 66 | end |
| 67 | |
| 68 | private |
| 69 | def near_search? |
| 70 | return true if ((@op == Operator::NEAR or |
| 71 | @op == Operator::NEAR_NO_OFFSET) and |
| 72 | (@args.size >= 3 and @args.size <= 5)) |
| 73 | return true if ((@op == Operator::NEAR_PHRASE or |
| 74 | @op == Operator::ORDERED_NEAR_PHRASE or |
| 75 | @op == Operator::NEAR_PHRASE_PRODUCT or |
| 76 | @op == Operator::ORDERED_NEAR_PHRASE_PRODUCT) and |
| 77 | (@args.size >= 4 and @args.size <= 6)) |
| 78 | false |
| 79 | end |
| 80 | |
| 81 | def match_near_resolve_index |
| 82 | arg = @args[0] |
| 83 | case arg |
| 84 | when Expression |
| 85 | match_resolve_index_expression(arg) |
| 86 | when Accessor |
| 87 | match_resolve_index_accessor(arg) |
| 88 | when Indexable |
| 89 | match_resolve_index_indexable(arg) |
| 90 | else |
| 91 | message = |
| 92 | "The first argument of " + |
| 93 | "NEAR/NEAR_NO_OFFSET/NEAR_PHRASE/ORDERED_NEAR_PHRASE/" + |
| 94 | "NEAR_PHRASE_PRODUCT/ORDERED_NEAR_PHRASE_PRODUCT must be " + |
| 95 | "Expression, Accessor or Indexable: #{arg.class}" |
| 96 | raise ErrorMessage, message |
| 97 | end |
| 98 | |
| 99 | self.query = @args[1] |
| 100 | self.max_interval = @args[2].value |
| 101 | case @op |
| 102 | when Operator::NEAR, |
| 103 | Operator::NEAR_NO_OFFSET |
| 104 | self.max_element_intervals = @args[3]&.value |
| 105 | self.min_interval = @args[4]&.value |
| 106 | when Operator::NEAR_PHRASE, |
| 107 | Operator::ORDERED_NEAR_PHRASE, |
| 108 | Operator::NEAR_PHRASE_PRODUCT, |
| 109 | Operator::ORDERED_NEAR_PHRASE_PRODUCT |
| 110 | self.additional_last_interval = @args[3].value |
| 111 | self.max_element_intervals = @args[4]&.value |
| 112 | self.min_interval = @args[5]&.value |
| 113 | end |
| 114 | end |
| 115 | |
| 116 | def similar_search? |
| 117 | @op == Operator::SIMILAR and @args.size == 3 |
| 118 | end |
| 119 | |
| 120 | def match_similar_resolve_index |
| 121 | arg = @args[0] |
| 122 | case arg |
| 123 | when Expression |
| 124 | match_resolve_index_expression(arg) |
| 125 | when Accessor |
| 126 | match_resolve_index_accessor(arg) |
| 127 | when IndexColumn |
| 128 | match_resolve_index_index_column(arg) |
| 129 | when Indexable |
| 130 | match_resolve_index_indexable(arg) |
| 131 | else |
| 132 | message = |
| 133 | "The first argument of SIMILAR must be Expression, Accessor or Indexable: #{arg.class}" |
| 134 | raise ErrorMessage, message |
| 135 | end |
| 136 | |
| 137 | self.query = @args[1] |
| 138 | self.similarity_threshold = @args[2].value |
| 139 | end |
| 140 | |
| 141 | def quorum_match? |
| 142 | @op == Operator::QUORUM and @args.size == 3 |
| 143 | end |
| 144 | |
| 145 | def match_quorum_resolve_index |
| 146 | arg = @args[0] |
| 147 | case arg |
| 148 | when Expression |
| 149 | match_resolve_index_expression(arg) |
| 150 | when Accessor |
| 151 | match_resolve_index_accessor(arg) |
| 152 | when Indexable |
| 153 | match_resolve_index_indexable(arg) |
| 154 | else |
| 155 | message = |
| 156 | "The first argument of QUORUM must be Expression, Accessor or Indexable: #{arg.class}" |
| 157 | raise ErrorMesesage, message |
| 158 | end |
| 159 | |
| 160 | self.query = @args[1] |
| 161 | self.quorum_threshold = @args[2].value |
| 162 | end |
| 163 | |
| 164 | def match_generic_resolve_index |
| 165 | @args.each do |arg| |
| 166 | case arg |
| 167 | when Expression |
| 168 | match_resolve_index_expression(arg) |
| 169 | when Accessor |
| 170 | match_resolve_index_accessor(arg) |
| 171 | when IndexColumn |
| 172 | match_resolve_index_index_column(arg) |
| 173 | when Indexable |
| 174 | match_resolve_index_indexable(arg) |
| 175 | when Procedure |
| 176 | break |
| 177 | else |
| 178 | self.query = arg |
| 179 | end |
| 180 | end |
| 181 | if @op == Operator::REGEXP and not index_searchable_regexp?(@query) |
| 182 | @search_indexes.clear |
| 183 | end |
| 184 | end |
| 185 | |
| 186 | def index_searchable_regexp?(pattern) |
| 187 | return false if pattern.nil? |
| 188 | |
| 189 | paren = :outside |
| 190 | dot = false |
| 191 | previous_char = nil |
| 192 | pattern.value.each_char do |char| |
| 193 | if previous_char == "\\" |
| 194 | case char |
| 195 | when "Z" |
| 196 | return false |
| 197 | when "b", "B" |
| 198 | return false |
| 199 | when "d", "D", "h", "H", "p", "s", "S", "w", "W" |
| 200 | return false |
| 201 | when "X" |
| 202 | return false |
| 203 | when "k", "g", "1", "2", "3", "4", "5", "6", "7", "8", "9" |
| 204 | return false |
| 205 | when "\\" |
| 206 | previous_char = nil |
| 207 | next |
| 208 | end |
| 209 | else |
| 210 | case paren |
| 211 | when :starting |
| 212 | case char |
| 213 | when "?" |
| 214 | return false if previous_char != "(" |
| 215 | when "-" |
| 216 | return false if previous_char != "?" |
| 217 | when "m" |
| 218 | return false if previous_char != "-" |
| 219 | when "i" |
| 220 | return false if previous_char != "m" |
| 221 | when "x" |
| 222 | return false if previous_char != "i" |
| 223 | when ":" |
| 224 | return false if previous_char != "x" |
| 225 | paren = :inside |
| 226 | else |
| 227 | return false |
| 228 | end |
| 229 | else |
| 230 | case char |
| 231 | when "(" |
| 232 | return false unless paren == :outside |
| 233 | paren = :starting |
| 234 | when ")" |
| 235 | return false unless paren == :inside |
| 236 | paren = :outside |
| 237 | when "." |
| 238 | return false if dot |
| 239 | dot = true |
| 240 | when "*" |
| 241 | return false unless dot |
| 242 | dot = false |
| 243 | when "[", "]", "|", "?", "+", "{", "}", "^", "$" |
| 244 | return false |
| 245 | else |
| 246 | return false if dot |
| 247 | end |
| 248 | end |
| 249 | end |
| 250 | previous_char = char |
| 251 | end |
| 252 | return false if dot |
| 253 | return false unless paren == :outside |
| 254 | true |
| 255 | end |
| 256 | |
| 257 | def match_resolve_index_expression(expression) |
| 258 | codes = expression.codes |
| 259 | n_codes = codes.size |
| 260 | i = 0 |
| 261 | while i < n_codes |
| 262 | i = match_resolve_index_expression_codes(expression, codes, i, n_codes) |
| 263 | end |
| 264 | end |
| 265 | |
| 266 | def match_resolve_index_expression_codes(expression, codes, i, n_codes) |
| 267 | code = codes[i] |
| 268 | value = code.value |
| 269 | return i + 1 if value.nil? |
| 270 | |
| 271 | case value |
| 272 | when Accessor, Column |
| 273 | index_info, offset = |
| 274 | match_resolve_index_expression_find_index(expression, |
| 275 | codes, i, n_codes) |
| 276 | i += offset - 1 |
| 277 | if index_info |
| 278 | if value.is_a?(Accessor) |
| 279 | self.flags |= ScanInfo::Flags::ACCESSOR |
| 280 | end |
| 281 | weight, offset = codes[i].weight |
| 282 | i += offset |
| 283 | put_search_index(index_info.index, |
| 284 | index_info.section_id, |
| 285 | index_info.start_position, |
| 286 | weight) |
| 287 | end |
| 288 | when Procedure |
| 289 | unless value.scorer? |
| 290 | message = "procedure must be scorer: #{scorer.name}>" |
| 291 | raise ErrorMessage, message |
| 292 | end |
| 293 | scorer = value |
| 294 | i += 1 |
| 295 | index_info, offset = |
| 296 | match_resolve_index_expression_find_index(expression, |
| 297 | codes, i, n_codes) |
| 298 | i += offset |
| 299 | if index_info |
| 300 | scorer_args_expr_offset = 0 |
| 301 | if codes[i].op != Operator::CALL |
| 302 | scorer_args_expr_offset = i |
| 303 | end |
| 304 | while i < n_codes and codes[i].op != Operator::CALL |
| 305 | i += 1 |
| 306 | end |
| 307 | weight, offset = codes[i].weight |
| 308 | i += offset |
| 309 | put_search_index(index_info.index, |
| 310 | index_info.section_id, |
| 311 | index_info.start_position, |
| 312 | weight, |
| 313 | scorer, |
| 314 | expression, |
| 315 | scorer_args_expr_offset) |
| 316 | end |
| 317 | when Table |
| 318 | raise ErrorMessage, "invalid match target: <#{value.name}>" |
| 319 | end |
| 320 | i + 1 |
| 321 | end |
| 322 | |
| 323 | def term_search_vector_column_index?(index) |
| 324 | source_ids = index.source_ids |
| 325 | return false unless source_ids.size == 1 |
| 326 | context = Context.instance |
| 327 | context.with_temporary_open_space do |
| 328 | source = context[source_ids[0]] |
| 329 | begin |
| 330 | return false unless source.vector? |
| 331 | return false if index.lexicon.have_tokenizer? |
| 332 | ensure |
| 333 | source.unref |
| 334 | end |
| 335 | end |
| 336 | true |
| 337 | end |
| 338 | |
| 339 | def match_resolve_index_expression_find_index(expression, codes, i, n_codes) |
| 340 | code = codes[i] |
| 341 | value = code.value |
| 342 | index_info = nil |
| 343 | offset = 1 |
| 344 | case value |
| 345 | when Accessor |
| 346 | accessor = value |
| 347 | index_info = accessor.find_index(@op) |
| 348 | if index_info |
| 349 | if accessor.have_next? and index_info.index != accessor.object |
| 350 | accessor.refer |
| 351 | index_info.index.unref |
| 352 | index_info = IndexInfo.new(accessor, index_info.section_id, nil) |
| 353 | end |
| 354 | end |
| 355 | when FixedSizeColumn, VariableSizeColumn |
| 356 | index_info = value.find_index(@op) |
| 357 | when IndexColumn |
| 358 | index = value |
| 359 | section_id = 0 |
| 360 | start_position = nil |
| 361 | rest_n_codes = n_codes - i |
| 362 | if rest_n_codes >= 2 and |
| 363 | codes[i + 1].value.is_a?(Bulk) and |
| 364 | (codes[i + 1].value.domain_id == ID::UINT32 or |
| 365 | codes[i + 1].value.domain_id == ID::INT32) and |
| 366 | codes[i + 2].op == Operator::GET_MEMBER |
| 367 | member = codes[i + 1].value.value |
| 368 | if term_search_vector_column_index?(index) |
| 369 | start_position = member |
| 370 | else |
| 371 | section_id = member + 1 |
| 372 | end |
| 373 | offset += 2 |
| 374 | end |
| 375 | index.refer |
| 376 | index_info = IndexInfo.new(index, section_id, start_position) |
| 377 | end |
| 378 | |
| 379 | [index_info, offset] |
| 380 | end |
| 381 | |
| 382 | def match_resolve_index_expression_accessor(expr_code) |
| 383 | accessor = expr_code.value |
| 384 | self.flags |= ScanInfo::Flags::ACCESSOR |
| 385 | index_info = accessor.find_index(op) |
| 386 | return if index_info.nil? |
| 387 | |
| 388 | section_id = index_info.section_id |
| 389 | start_position = index_info.start_position |
| 390 | weight = expr_code.weight |
| 391 | if accessor.next |
| 392 | accessor.refer |
| 393 | index_info.index.unref |
| 394 | put_search_index(accessor, section_id, start_position, weight) |
| 395 | else |
| 396 | put_search_index(index_info.index, section_id, start_position, weight) |
| 397 | end |
| 398 | end |
| 399 | |
| 400 | def match_resolve_index_expression_data_column(expr_code) |
| 401 | column = expr_code.value |
| 402 | index_info = column.find_index(op) |
| 403 | return if index_info.nil? |
| 404 | put_search_index(index_info.index, |
| 405 | index_info.section_id, |
| 406 | index_info.start_position, |
| 407 | expr_code.weight) |
| 408 | end |
| 409 | |
| 410 | def match_resolve_index_index_column(index) |
| 411 | index.refer |
| 412 | put_search_index(index, 0, nil, 1) |
| 413 | end |
| 414 | |
| 415 | def match_resolve_index_indexable(indexable) |
| 416 | index_info = indexable.find_index(op) |
| 417 | return if index_info.nil? |
| 418 | put_search_index(index_info.index, |
| 419 | index_info.section_id, |
| 420 | index_info.start_position, |
| 421 | 1) |
| 422 | end |
| 423 | |
| 424 | def match_resolve_index_accessor(accessor) |
| 425 | self.flags |= ScanInfo::Flags::ACCESSOR |
| 426 | index_info = accessor.find_index(op) |
| 427 | return if index_info.nil? |
| 428 | if accessor.next |
| 429 | accessor.refer |
| 430 | index_info.index.unref |
| 431 | put_search_index(accessor, |
| 432 | index_info.section_id, |
| 433 | index_info.start_position, |
| 434 | 1) |
| 435 | else |
| 436 | put_search_index(index_info.index, |
| 437 | index_info.section_id, |
| 438 | index_info.start_position, |
| 439 | 1) |
| 440 | end |
| 441 | end |
| 442 | |
| 443 | def call_relational_resolve_index(object, selector_op) |
| 444 | case object |
| 445 | when Accessor |
| 446 | call_relational_resolve_index_accessor(object, selector_op) |
| 447 | when Bulk |
| 448 | self.query = object |
| 449 | when Indexable |
| 450 | call_relational_resolve_index_indexable(object, selector_op) |
| 451 | end |
| 452 | end |
| 453 | |
| 454 | def call_relational_resolve_index_indexable(indexable, selector_op) |
| 455 | index_info = indexable.find_index(selector_op) |
| 456 | return if index_info.nil? |
| 457 | put_search_index(index_info.index, |
| 458 | index_info.section_id, |
| 459 | index_info.start_position, |
| 460 | 1) |
| 461 | end |
| 462 | |
| 463 | def call_relational_resolve_index_accessor(accessor, selector_op) |
| 464 | self.flags |= ScanInfo::Flags::ACCESSOR |
| 465 | index_info = accessor.find_index(selector_op) |
| 466 | return if index_info.nil? |
| 467 | put_search_index(index_info.index, |
| 468 | index_info.section_id, |
| 469 | index_info.start_position, |
| 470 | 1) |
| 471 | end |
| 472 | |
| 473 | def put_search_index(index, section_id, start_position, weight, *args) |
| 474 | start_position ||= @start_position || -1 |
| 475 | search_index = ScanInfoSearchIndex.new(index, |
| 476 | section_id, |
| 477 | start_position, |
| 478 | weight + @weight, |
| 479 | *args) |
| 480 | @search_indexes << search_index |
| 481 | end |
| 482 | end |
| 483 | end |