Class: RedisMemo::MemoizeQuery::CachedSelect::BindParams

Inherits:
Object
  • Object
show all
Defined in:
lib/redis_memo/memoize_query/cached_select/bind_params.rb

Instance Method Summary collapse

Instance Method Details

#memoizable?Boolean

Returns:

  • (Boolean)


111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/redis_memo/memoize_query/cached_select/bind_params.rb', line 111

def memoizable?
  return false if params.empty?

  params.each do |model, attrs_set|
    return false if attrs_set.empty?

    attrs_set.each do |attrs|
      return false unless RedisMemo::MemoizeQuery
        .memoized_columns(model)
        .include?(attrs.keys.sort)
    end
  end

  true
end

#paramsObject



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/redis_memo/memoize_query/cached_select/bind_params.rb', line 5

def params
  #
  # Bind params is hash of sets: each key is a model class, each value is a
  # set of hashes for memoized column conditions. Example:
  #
  #   {
  #      Site => [
  #        {name: 'a', city: 'b'},
  #        {name: 'a', city: 'c'},
  #        {name: 'b', city: 'b'},
  #        {name: 'b', city: 'c'},
  #      ],
  #   }
  #
  @params ||= Hash.new do |models, model|
    models[model] = []
  end
end

#product(other) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/redis_memo/memoize_query/cached_select/bind_params.rb', line 42

def product(other)
  #  Example:
  #
  #  and(
  #    [{a: 1}, {a: 2}],
  #    [{b: 1}, {b: 2}],
  #  )
  #
  #  =>
  #
  #  [
  #    {a: 1, b: 1},
  #    {a: 1, b: 2},
  #    {a: 2, b: 1},
  #    {a: 2, b: 2},
  #  ]
  return unless other

  # The tree is almost always right-heavy. Merge into the right node for better
  # performance.
  params.each do |model, attrs_set|
    next if attrs_set.empty?

    # The other model does not have any conditions so far: carry the
    # attributes over to the other node
    if other.params[model].empty?
      other.params[model] = attrs_set
      next
    end

    # Distribute the current attrs into the other
    other_attrs_set_size = other.params[model].size
    other_attrs_set = other.params[model]
    merged_attrs_set = Array.new(other_attrs_set_size * attrs_set.size)

    attrs_set.each_with_index do |attrs, i|
      other_attrs_set.each_with_index do |other_attrs, j|
        k = i * other_attrs_set_size + j
        merged_attrs = merged_attrs_set[k] = other_attrs.dup
        attrs.each do |name, val|
          # Conflict detected. For example:
          #
          #   (a = 1 or b = 1) and (a = 2 or b = 2)
          #
          #  Keep:     a = 1 and b = 2, a = 2 and b = 1
          #  Discard:  a = 1 and a = 2, b = 1 and b = 2
          if merged_attrs.include?(name) && merged_attrs[name] != val
            merged_attrs_set[k] = nil
            break
          end

          merged_attrs[name] = val
        end
      end
    end

    merged_attrs_set.compact!
    other.params[model] = merged_attrs_set
  end

  other
end

#union(other) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/redis_memo/memoize_query/cached_select/bind_params.rb', line 24

def union(other)
  return unless other

  # The tree is almost always right-heavy. Merge into the right node for better
  # performance.
  other.params.merge!(params) do |_, other_attrs_set, attrs_set|
    if other_attrs_set.empty?
      attrs_set
    elsif attrs_set.empty?
      other_attrs_set
    else
      attrs_set + other_attrs_set
    end
  end

  other
end

#uniq!Object



105
106
107
108
109
# File 'lib/redis_memo/memoize_query/cached_select/bind_params.rb', line 105

def uniq!
  params.each do |_, attrs_set|
    attrs_set.uniq!
  end
end