Debian Codegraph @

lib/mrb/scripts/expression_size_estimator.rb

raw symbols SCIP
1 module Groonga
2 class ExpressionSizeEstimator
3 def initialize(expression, table)
4 @expression = expression
5 @table = table
6 @table_size = @table.size
7 end
8
9 def estimate
10 builder = ScanInfoBuilder.new(@expression, Operator::OR, false)
11 data_list = builder.build
12 return @table_size if data_list.nil?
13
14 begin
15 estimate_data_list(data_list)
16 ensure
17 data_list.each(&:unref)
18 end
19 end
20
21 private
22 def estimate_data_list(data_list)
23 current_size = 0
24 sizes = []
25 data_list.each do |data|
26 if (data.flags & ScanInfo::Flags::POP) != 0
27 size = sizes.pop
28 case data.logical_op
29 when Operator::AND, Operator::AND_NOT
30 current_size = size if size < current_size
31 when Operator::OR
32 current_size = size if size > current_size
33 else
34 message = "invalid logical operator: <#{data.logical_op.inspect}>"
35 raise InvalidArgument, message
36 end
37 else
38 if (data.flags & ScanInfo::Flags::PUSH) != 0
39 sizes.push(current_size)
40 current_size = 0
41 end
42
43 estimator = ScanInfoDataSizeEstimator.new(data, @table)
44 size = estimator.estimate
45 case data.logical_op
46 when Operator::AND
47 current_size = size if size < current_size
48 when Operator::AND_NOT
49 size = @table_size - size
50 size = 0 if size < 0
51 current_size = size if size < current_size
52 when Operator::OR
53 current_size = size if size > current_size
54 else
55 message = "invalid logical operator: <#{data.logical_op.inspect}>"
56 raise InvalidArgument, message
57 end
58 end
59 end
60 current_size
61 end
62 end
63 end