Class: Locomotive::RelationalAlgebra::SurrogateList

Inherits:
Object
  • Object
show all
Defined in:
lib/locomotive/relational_algebra/query_information.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(hash) ⇒ SurrogateList

Returns a new instance of SurrogateList.



22
23
24
# File 'lib/locomotive/relational_algebra/query_information.rb', line 22

def initialize(hash)
  self.surrogates = hash   
end

Instance Attribute Details

#surrogatesObject

Returns the value of attribute surrogates.



19
20
21
# File 'lib/locomotive/relational_algebra/query_information.rb', line 19

def surrogates
  @surrogates
end

Instance Method Details

#+(sur) ⇒ Object



31
32
33
# File 'lib/locomotive/relational_algebra/query_information.rb', line 31

def +(sur)
  SurrogateList.new(surrogates.merge(sur.surrogates))
end

#cloneObject



156
157
158
159
160
# File 'lib/locomotive/relational_algebra/query_information.rb', line 156

def clone
  # only clone the attributes since we don't modify
  # the plan
  SurrogateList.new( surrogates.map { |k,v| [k.clone,v] }.to_hash )
end

#delete_if(&lambda) ⇒ Object



35
36
37
# File 'lib/locomotive/relational_algebra/query_information.rb', line 35

def delete_if(&lambda)
  SurrogateList.new(self.clone.surrogates.delete_if(&lambda))
end

#filter_and_adapt(items) ⇒ Object



162
163
164
165
166
167
168
169
170
171
# File 'lib/locomotive/relational_algebra/query_information.rb', line 162

def filter_and_adapt(items)
   item_min = items.min
   # we are modifying the structure itself
   # so we have to clone it (sideeffect)
   surr_new = self.delete_if do |it, itbl|
     !items.member?(it)
   end
  # adapt the keys
  surr_new.map { |k,p| [Item.new(k.id - (item_min.id - 1)), p] }
end

#itapp(q_0, *itbls) ⇒ Object



39
40
41
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
# File 'lib/locomotive/relational_algebra/query_information.rb', line 39

def itapp(q_0, *itbls)
  if itbls.any? { |i| self.keys != i.keys }
    raise ITblsNotEqual,
          "some itbls keys are not equal"
  end
    
  if self.empty? and 
     itbls.all? { |i| i.empty? }
    return SurrogateList.new( {} )
  end
    
  c = self.first.first
    
  q1_in = self[c]
  qs_in = itbls.map { |itbl| itbl[c] }

  q1 = q1_in.plan
  qs = qs_in.map { |q| q.plan }
  cols_q1, itbls_q1 = q1_in.column_structure, q1_in.surrogates
  ord, ord_, item_, item__ = Iter.new(2), Iter.new(4),
                                 Iter.new(3), Item.new(3)

  # (1)
  q1_ = q1.attach(AttachItem.new(ord, RAtomic.new(1, RNat.type)))
  q = qs.zip(2..qs.size+1).reduce(q1_) do |p1,p2|
        p1.union(
           p2.first.attach(AttachItem.new(ord, RAtomic.new(p2.last, RNat.type))))
      end.row_num(item_, [], [Iter.new(1), ord, Pos.new(1)])
  
  #(2)
  c_new = Iter.new(5)
  q_ = q_0.project( ord => [ord_],
                    item_ => [item__],
                    c     => [c_new] ).
           theta_join(q, [Equivalence.new(ord_, ord),
                          Equivalence.new(c_new, Iter.new(1))] ).
           project( { item__ => [Iter.new(1)],
                      Pos.new(1) => [Pos.new(1)],
                      item_ => itbls_q1.keys  }.
                      merge(
                        (cols_q1 - itbls_q1.keys).items.collect do |col|
                          [col, [col]]
                        end.to_hash) )

   # (3)          
   itbl_ = q1_in.surrogates.itapp(q, *qs_in.map { |q| q.surrogates })
   # (4)
   itbl__ = self.delete_if { |k,v| k == c}.
              itapp(q_0, *itbls.map { |i| i.delete_if { |k,v| k == c} })
   # (5)
   SurrogateList.new( { c => QueryInformationNode.new(
                               q_, cols_q1, itbl_) } ) + itbl__
end

#itsel(q_0) ⇒ Object



93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/locomotive/relational_algebra/query_information.rb', line 93

def itsel(q_0)
  return SurrogateList.new({}) if self.empty? 

  c = self.first.first
  cols, itbls, q = self[c].column_structure, self[c].surrogates, self[c].plan
  c_ = (self.keys + cols.items).max.inc

  # (1)
  q_ = q.equi_join(q_0.project( c => [c_]), Iter.new(1), c_).
         project( [Iter.new(1), Pos.new(1)] + cols.items )

  itbls_ = itbls.itsel(q_)
  itbls__ = self.delete_if { |k,v| k == c }.itsel(q_0)

  SurrogateList.new(
    { c => QueryInformationNode.new(q_, cols, itbls_) }.
    merge( itbls__.to_a.to_hash ) )
end

#joinObject



112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
# File 'lib/locomotive/relational_algebra/query_information.rb', line 112

def join
  itbls = surrogates.to_a

  itbls.rest.reduce(itbls.first.last) do |qi,surr|
    # the query_information node
    qin_j = surr.last
     
    cols_j  = surr.last.column_structure
    itbls_j = surr.last.surrogates
    cols_c  = qi.column_structure
    itbls_c = qi.surrogates

    # adapt cols
    cols_j_ = cols_j.clone
    cols_j_.items.each { |i| i.inc!(qi.column_structure.count) }
    # adapt itbls
    itbls_j_ = itbls_j.map { |k,q| [k.inc(qi.column_structure.count), q] }

    # calculate new plan
    q_j = qin_j.plan.project({ Iter.new(1) => [Iter.new(2)],
                               Pos.new(1)  => [Pos.new(2)] }.
                              merge(
                                cols_j.items.zip(cols_j_.items).
                                  map do |old,new|
                                    [old,[new]]
                                  end.to_hash))

    q_ = q_j.theta_join(qi.plan, [Equivalence.new(Iter.new(2), Iter.new(1)),
                                          Equivalence.new(Pos.new(2), Pos.new(1))]).
                     project([Iter.new(1), Pos.new(1)] + cols_c.items + cols_j_.items)


    QueryInformationNode.new(q_, cols_c + cols_j_, itbls_c + itbls_j_)
  end
end

#map(&block) ⇒ Object



26
27
28
29
# File 'lib/locomotive/relational_algebra/query_information.rb', line 26

def map(&block)
  SurrogateList.new(
    surrogates.map(&block).to_hash)
end

#set(var, plan) ⇒ Object



148
149
150
151
152
153
154
# File 'lib/locomotive/relational_algebra/query_information.rb', line 148

def set(var, plan)
  self.map do |item, itbl|
    [item,QueryInformationNode.new(itbl.plan.set(var, plan), 
                                   itbl.column_structure,
                                   itbl.surrogates.set(var,plan))]
  end
end