| 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 :similarity_threshold |
| 15 |
attr_accessor :start_position |
| 16 |
attr_accessor :weight |
| 17 |
def initialize(start) |
| 18 |
@start = start |
| 19 |
@end = 0 |
| 20 |
@op = Operator::NOP |
| 21 |
@logical_op = Operator::OR |
| 22 |
@query = nil |
| 23 |
@args = [] |
| 24 |
@search_indexes = [] |
| 25 |
@flags = ScanInfo::Flags::PUSH |
| 26 |
@max_interval = nil |
| 27 |
@similarity_threshold = nil |
| 28 |
@start_position = nil |
| 29 |
@weight = 0 |
| 30 |
end |
| 31 |
|
| 32 |
def match_resolve_index |
| 33 |
if near_search? |
| 34 |
match_near_resolve_index |
| 35 |
elsif similar_search? |
| 36 |
match_similar_resolve_index |
| 37 |
else |
| 38 |
match_generic_resolve_index |
| 39 |
end |
| 40 |
end |
| 41 |
|
| 42 |
def call_relational_resolve_indexes |
| 43 |
procedure, *args = *@args |
| 44 |
return unless procedure.selector? |
| 45 |
|
| 46 |
selector_op = procedure.selector_operator |
| 47 |
args.each do |arg| |
| 48 |
call_relational_resolve_index(arg, selector_op) |
| 49 |
end |
| 50 |
end |
| 51 |
|
| 52 |
private |
| 53 |
def near_search? |
| 54 |
(@op == Operator::NEAR or @op == Operator::NEAR2) and @args.size == 3 |
| 55 |
end |
| 56 |
|
| 57 |
def match_near_resolve_index |
| 58 |
arg = @args[0] |
| 59 |
case arg |
| 60 |
when Expression |
| 61 |
match_resolve_index_expression(arg) |
| 62 |
when Accessor |
| 63 |
match_resolve_index_accessor(arg) |
| 64 |
when Indexable |
| 65 |
match_resolve_index_indexable(arg) |
| 66 |
else |
| 67 |
message = |
| 68 |
"The first argument of NEAR/NEAR2 must be Expression, Accessor or Indexable: #{arg.class}" |
| 69 |
raise ErrorMessage, message |
| 70 |
end |
| 71 |
|
| 72 |
self.query = @args[1] |
| 73 |
self.max_interval = @args[2].value |
| 74 |
end |
| 75 |
|
| 76 |
def similar_search? |
| 77 |
@op == Operator::SIMILAR and @args.size == 3 |
| 78 |
end |
| 79 |
|
| 80 |
def match_similar_resolve_index |
| 81 |
arg = @args[0] |
| 82 |
case arg |
| 83 |
when Expression |
| 84 |
match_resolve_index_expression(arg) |
| 85 |
when Accessor |
| 86 |
match_resolve_index_accessor(arg) |
| 87 |
when Indexable |
| 88 |
match_resolve_index_indexable(arg) |
| 89 |
else |
| 90 |
message = |
| 91 |
"The first argument of SIMILAR must be Expression, Accessor or Indexable: #{arg.class}" |
| 92 |
raise ErrorMesesage, message |
| 93 |
end |
| 94 |
|
| 95 |
self.query = @args[1] |
| 96 |
self.similarity_threshold = @args[2].value |
| 97 |
end |
| 98 |
|
| 99 |
def match_generic_resolve_index |
| 100 |
@args.each do |arg| |
| 101 |
case arg |
| 102 |
when Expression |
| 103 |
match_resolve_index_expression(arg) |
| 104 |
when Accessor |
| 105 |
match_resolve_index_accessor(arg) |
| 106 |
when IndexColumn |
| 107 |
match_resolve_index_index_column(arg) |
| 108 |
when Indexable |
| 109 |
match_resolve_index_indexable(arg) |
| 110 |
when Procedure |
| 111 |
break |
| 112 |
else |
| 113 |
self.query = arg |
| 114 |
end |
| 115 |
end |
| 116 |
if @op == Operator::REGEXP and not index_searchable_regexp?(@query) |
| 117 |
@search_indexes.clear |
| 118 |
end |
| 119 |
end |
| 120 |
|
| 121 |
def index_searchable_regexp?(pattern) |
| 122 |
return false if pattern.nil? |
| 123 |
|
| 124 |
previous_char = nil |
| 125 |
pattern.value.each_char do |char| |
| 126 |
if previous_char == "\\" |
| 127 |
case char |
| 128 |
when "Z" |
| 129 |
return false |
| 130 |
when "b", "B" |
| 131 |
return false |
| 132 |
when "d", "D", "h", "H", "p", "s", "S", "w", "W" |
| 133 |
return false |
| 134 |
when "X" |
| 135 |
return false |
| 136 |
when "k", "g", "1", "2", "3", "4", "5", "6", "7", "8", "9" |
| 137 |
return false |
| 138 |
when "\\" |
| 139 |
previous_char = nil |
| 140 |
next |
| 141 |
end |
| 142 |
else |
| 143 |
case char |
| 144 |
when ".", "[", "]", "|", "?", "+", "*", "{", "}", "^", "$", "(", ")" |
| 145 |
return false |
| 146 |
end |
| 147 |
end |
| 148 |
previous_char = char |
| 149 |
end |
| 150 |
true |
| 151 |
end |
| 152 |
|
| 153 |
def match_resolve_index_expression(expression) |
| 154 |
codes = expression.codes |
| 155 |
n_codes = codes.size |
| 156 |
i = 0 |
| 157 |
while i < n_codes |
| 158 |
i = match_resolve_index_expression_codes(expression, codes, i, n_codes) |
| 159 |
end |
| 160 |
end |
| 161 |
|
| 162 |
def match_resolve_index_expression_codes(expression, codes, i, n_codes) |
| 163 |
code = codes[i] |
| 164 |
value = code.value |
| 165 |
return i + 1 if value.nil? |
| 166 |
|
| 167 |
case value |
| 168 |
when Accessor, Column |
| 169 |
index_info, offset = |
| 170 |
match_resolve_index_expression_find_index(expression, |
| 171 |
codes, i, n_codes) |
| 172 |
i += offset - 1 |
| 173 |
if index_info |
| 174 |
if value.is_a?(Accessor) |
| 175 |
self.flags |= ScanInfo::Flags::ACCESSOR |
| 176 |
end |
| 177 |
weight, offset = codes[i].weight |
| 178 |
i += offset |
| 179 |
put_search_index(index_info.index, index_info.section_id, weight) |
| 180 |
end |
| 181 |
when Procedure |
| 182 |
unless value.scorer? |
| 183 |
message = "procedure must be scorer: #{scorer.name}>" |
| 184 |
raise ErrorMessage, message |
| 185 |
end |
| 186 |
scorer = value |
| 187 |
i += 1 |
| 188 |
index_info, offset = |
| 189 |
match_resolve_index_expression_find_index(expression, |
| 190 |
codes, i, n_codes) |
| 191 |
i += offset |
| 192 |
if index_info |
| 193 |
scorer_args_expr_offset = 0 |
| 194 |
if codes[i].op != Operator::CALL |
| 195 |
scorer_args_expr_offset = i |
| 196 |
end |
| 197 |
while i < n_codes and codes[i].op != Operator::CALL |
| 198 |
i += 1 |
| 199 |
end |
| 200 |
weight, offset = codes[i].weight |
| 201 |
i += offset |
| 202 |
put_search_index(index_info.index, |
| 203 |
index_info.section_id, |
| 204 |
weight, |
| 205 |
scorer, |
| 206 |
expression, |
| 207 |
scorer_args_expr_offset) |
| 208 |
end |
| 209 |
when Table |
| 210 |
raise ErrorMessage, "invalid match target: <#{value.name}>" |
| 211 |
end |
| 212 |
i + 1 |
| 213 |
end |
| 214 |
|
| 215 |
def match_resolve_index_expression_find_index(expression, codes, i, n_codes) |
| 216 |
code = codes[i] |
| 217 |
value = code.value |
| 218 |
index_info = nil |
| 219 |
offset = 1 |
| 220 |
case value |
| 221 |
when Accessor |
| 222 |
accessor = value |
| 223 |
index_info = accessor.find_index(@op) |
| 224 |
if index_info |
| 225 |
if accessor.have_next? and index_info.index != accessor.object |
| 226 |
index_info = IndexInfo.new(accessor, index_info.section_id) |
| 227 |
end |
| 228 |
end |
| 229 |
when FixedSizeColumn, VariableSizeColumn |
| 230 |
index_info = value.find_index(@op) |
| 231 |
when IndexColumn |
| 232 |
index = value |
| 233 |
section_id = 0 |
| 234 |
rest_n_codes = n_codes - i |
| 235 |
if rest_n_codes >= 2 and |
| 236 |
codes[i + 1].value.is_a?(Bulk) and |
| 237 |
(codes[i + 1].value.domain == ID::UINT32 or |
| 238 |
codes[i + 1].value.domain == ID::INT32) and |
| 239 |
codes[i + 2].op == Operator::GET_MEMBER |
| 240 |
section_id = codes[i + 1].value.value + 1 |
| 241 |
offset += 2 |
| 242 |
end |
| 243 |
index_info = IndexInfo.new(index, section_id) |
| 244 |
end |
| 245 |
|
| 246 |
[index_info, offset] |
| 247 |
end |
| 248 |
|
| 249 |
def match_resolve_index_expression_accessor(expr_code) |
| 250 |
accessor = expr_code.value |
| 251 |
self.flags |= ScanInfo::Flags::ACCESSOR |
| 252 |
index_info = accessor.find_index(op) |
| 253 |
return if index_info.nil? |
| 254 |
|
| 255 |
section_id = index_info.section_id |
| 256 |
weight = expr_code.weight |
| 257 |
if accessor.next |
| 258 |
put_search_index(accessor, section_id, weight) |
| 259 |
else |
| 260 |
put_search_index(index_info.index, section_id, weight) |
| 261 |
end |
| 262 |
end |
| 263 |
|
| 264 |
def match_resolve_index_expression_data_column(expr_code) |
| 265 |
column = expr_code.value |
| 266 |
index_info = column.find_index(op) |
| 267 |
return if index_info.nil? |
| 268 |
put_search_index(index_info.index, index_info.section_id, expr_code.weight) |
| 269 |
end |
| 270 |
|
| 271 |
def match_resolve_index_index_column(index) |
| 272 |
put_search_index(index, 0, 1) |
| 273 |
end |
| 274 |
|
| 275 |
def match_resolve_index_indexable(indexable) |
| 276 |
index_info = indexable.find_index(op) |
| 277 |
return if index_info.nil? |
| 278 |
put_search_index(index_info.index, index_info.section_id, 1) |
| 279 |
end |
| 280 |
|
| 281 |
def match_resolve_index_accessor(accessor) |
| 282 |
self.flags |= ScanInfo::Flags::ACCESSOR |
| 283 |
index_info = accessor.find_index(op) |
| 284 |
return if index_info.nil? |
| 285 |
if accessor.next |
| 286 |
put_search_index(accessor, index_info.section_id, 1) |
| 287 |
else |
| 288 |
put_search_index(index_info.index, index_info.section_id, 1) |
| 289 |
end |
| 290 |
end |
| 291 |
|
| 292 |
def call_relational_resolve_index(object, selector_op) |
| 293 |
case object |
| 294 |
when Accessor |
| 295 |
call_relational_resolve_index_accessor(object, selector_op) |
| 296 |
when Bulk |
| 297 |
self.query = object |
| 298 |
when Indexable |
| 299 |
call_relational_resolve_index_indexable(object, selector_op) |
| 300 |
end |
| 301 |
end |
| 302 |
|
| 303 |
def call_relational_resolve_index_indexable(indexable, selector_op) |
| 304 |
index_info = indexable.find_index(selector_op) |
| 305 |
return if index_info.nil? |
| 306 |
put_search_index(index_info.index, index_info.section_id, 1) |
| 307 |
end |
| 308 |
|
| 309 |
def call_relational_resolve_index_accessor(accessor, selector_op) |
| 310 |
self.flags |= ScanInfo::Flags::ACCESSOR |
| 311 |
index_info = accessor.find_index(selector_op) |
| 312 |
return if index_info.nil? |
| 313 |
put_search_index(index_info.index, index_info.section_id, 1) |
| 314 |
end |
| 315 |
|
| 316 |
def put_search_index(index, section_id, weight, *args) |
| 317 |
search_index = ScanInfoSearchIndex.new(index, |
| 318 |
section_id, |
| 319 |
weight + @weight, |
| 320 |
*args) |
| 321 |
@search_indexes << search_index |
| 322 |
end |
| 323 |
end |
| 324 |
end |