Class: Bud::PushElement

Inherits:
BudCollection show all
Defined in:
lib/bud/executor/elements.rb

Overview

p.insert(1) p.insert(nil)

Direct Known Subclasses

PushStatefulElement, ScannerElement

Instance Attribute Summary (collapse)

Attributes inherited from BudCollection

#accumulate_tick_deltas, #bud_instance, #cols, #is_source, #key_cols, #new_delta, #pending, #scanner_cnt, #struct, #tabname

Instance Method Summary (collapse)

Methods inherited from BudCollection

#[], #bootstrap, #canonicalize_col, #close, #do_insert, #each_delta, #each_raw, #each_tick_delta, #empty?, #exists?, #flat_map, #flush_deltas, #has_key?, #init_schema, #inspect, #invalidate_at_tick, #keys, #length, #non_temporal_predecessors, #null_tuple, #pending_merge, #positive_predecessors, #prep_aggpairs, #qualified_tabname, #register_coll_expr, #rename, #schema, #tick_metrics, #to_push_elem, #uniquify_tabname, #val_cols, #values

Constructor Details

- (PushElement) initialize(name_in, bud_instance, collection_name = nil, given_schema = nil, defer_schema = false, &blk)



17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/bud/executor/elements.rb', line 17

def initialize(name_in, bud_instance, collection_name=nil, given_schema=nil, defer_schema=false, &blk)
  super(name_in, bud_instance, given_schema, defer_schema)
  @blk = blk
  @outputs = Set.new
  @pendings = Set.new
  @deletes = Set.new
  @delete_keys = Set.new
  @wired_by = []
  @elem_name = name_in
  @found_delta = false
  @collection_name = collection_name
  @invalidated = true
  @rescan = true
end

Instance Attribute Details

- (Object) elem_name

Returns the value of attribute elem_name



14
15
16
# File 'lib/bud/executor/elements.rb', line 14

def elem_name
  @elem_name
end

- (Object) found_delta (readonly)

Returns the value of attribute found_delta



15
16
17
# File 'lib/bud/executor/elements.rb', line 15

def found_delta
  @found_delta
end

- (Object) invalidated

Returns the value of attribute invalidated



13
14
15
# File 'lib/bud/executor/elements.rb', line 13

def invalidated
  @invalidated
end

- (Object) outputs (readonly)

Returns the value of attribute outputs



15
16
17
# File 'lib/bud/executor/elements.rb', line 15

def outputs
  @outputs
end

- (Object) pendings (readonly)

Returns the value of attribute pendings



15
16
17
# File 'lib/bud/executor/elements.rb', line 15

def pendings
  @pendings
end

- (Object) rescan

Returns the value of attribute rescan



13
14
15
# File 'lib/bud/executor/elements.rb', line 13

def rescan
  @rescan
end

- (Object) wired_by (readonly)

Returns the value of attribute wired_by



15
16
17
# File 'lib/bud/executor/elements.rb', line 15

def wired_by
  @wired_by
end

Instance Method Details

- (Object) *(elem2, &blk)



234
235
236
# File 'lib/bud/executor/elements.rb', line 234

def *(elem2, &blk)
  join(elem2, &blk)
end

- (Object) <<(i)



184
185
186
# File 'lib/bud/executor/elements.rb', line 184

def <<(i)
  insert(i, nil)
end

- (Object) add_rescan_invalidate(rescan, invalidate)



151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
# File 'lib/bud/executor/elements.rb', line 151

def add_rescan_invalidate(rescan, invalidate)
  # If any sources are in rescan mode, then put this node in rescan
  srcs = non_temporal_predecessors
  if srcs.any?{|p| rescan.member? p}
    rescan << self
  end

  # Pass the current state to each output collection and see if they end up
  # marking this node for rescan
  invalidate_tables(rescan, invalidate)

  # Finally, if this node is in rescan, pass the request on to all source
  # elements
  if rescan.member? self
    rescan.merge(srcs)
  end
end

- (Boolean) all?(name = nil, bud_instance = nil, the_schema = nil, &blk)



329
330
331
# File 'lib/bud/executor/elements.rb', line 329

def all?(name=nil, bud_instance=nil, the_schema=nil, &blk)
  push_predicate(:all?, name, bud_instance, the_schema, &blk)
end

- (Boolean) any?(name = nil, bud_instance = nil, the_schema = nil, &blk)



332
333
334
# File 'lib/bud/executor/elements.rb', line 332

def any?(name=nil, bud_instance=nil, the_schema=nil, &blk)
  push_predicate(:any?, name, bud_instance, the_schema, &blk)
end

- (Object) argagg(aggname, gbkey_cols, collection, &blk)



294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
# File 'lib/bud/executor/elements.rb', line 294

def argagg(aggname, gbkey_cols, collection, &blk)
  gbkey_cols ||= []
  gbkey_cols = gbkey_cols.map{|c| canonicalize_col(c)}
  collection = canonicalize_col(collection)
  toplevel = @bud_instance.toplevel
  agg = toplevel.send(aggname, collection)[0]
  unless agg.class <= Bud::ArgExemplary
    raise Bud::Error, "#{aggname} not declared exemplary"
  end

  aggpairs = [[agg, collection]]
  aa = Bud::PushArgAgg.new("argagg#{Time.new.tv_usec}".to_sym, toplevel.this_rule_context,
                           @collection_name, gbkey_cols, aggpairs, schema, &blk)
  self.wire_to(aa)
  toplevel.push_elems[[self.object_id, :argagg, gbkey_cols, aggpairs, blk]] = aa
  return aa
end

- (Object) argmax(gbcols, col, &blk)



311
312
313
# File 'lib/bud/executor/elements.rb', line 311

def argmax(gbcols, col, &blk)
  argagg(:max, gbcols, col, &blk)
end

- (Object) argmin(gbcols, col, &blk)



314
315
316
# File 'lib/bud/executor/elements.rb', line 314

def argmin(gbcols, col, &blk)
  argagg(:min, gbcols, col, &blk)
end

- (Object) check_wiring



69
70
71
72
73
# File 'lib/bud/executor/elements.rb', line 69

def check_wiring
  if @blk.nil? and @outputs.empty? and @pendings.empty? and @deletes.empty? and @delete_keys.empty?
    raise Bud::Error, "no output specified for PushElement #{@qualified_tabname}"
  end
end

- (Object) each_with_index(&blk)



214
215
216
217
218
219
220
221
222
# File 'lib/bud/executor/elements.rb', line 214

def each_with_index(&blk)
  toplevel = @bud_instance.toplevel
  elem = Bud::PushEachWithIndex.new("each_with_index#{object_id}".to_sym,
                                    toplevel.this_rule_context,
                                    @collection_name)
  elem.set_block(&blk)
  self.wire_to(elem)
  toplevel.push_elems[[self.object_id, :each_with_index, blk]] = elem
end

- (Object) flush



189
190
# File 'lib/bud/executor/elements.rb', line 189

def flush
end

- (Object) group(keycols, *aggpairs, &blk)



269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
# File 'lib/bud/executor/elements.rb', line 269

def group(keycols, *aggpairs, &blk)
  # establish schema
  keycols ||= []
  keycols = keycols.map{|c| canonicalize_col(c)}
  keynames = keycols.map{|k| k[2]}
  aggcolsdups = aggpairs.map{|ap| ap[0].class.name.split("::").last}
  aggcols = []
  aggcolsdups.each_with_index do |n, i|
    aggcols << "#{n.downcase}_#{i}".to_sym
  end
  if aggcols.empty?
    the_schema = keynames
  else
    the_schema = { keynames => aggcols }
  end

  aggpairs = prep_aggpairs(aggpairs)
  toplevel = @bud_instance.toplevel
  g = Bud::PushGroup.new("grp#{Time.new.tv_usec}".to_sym, toplevel.this_rule_context,
                         @collection_name, keycols, aggpairs, the_schema, &blk)
  self.wire_to(g)
  toplevel.push_elems[[self.object_id, :group, keycols, aggpairs, blk]] = g
  return g
end

- (Boolean) include?(name = nil, bud_instance = nil, the_schema = nil, &blk)



335
336
337
# File 'lib/bud/executor/elements.rb', line 335

def include?(name=nil, bud_instance=nil, the_schema=nil, &blk)
  push_predicate(:include?, name, bud_instance, the_schema, &blk)
end

- (Object) insert(item, source = nil)



104
105
106
# File 'lib/bud/executor/elements.rb', line 104

def insert(item, source=nil)
  push_out(item)
end

- (Object) inspected



366
367
368
369
370
371
372
373
374
# File 'lib/bud/executor/elements.rb', line 366

def inspected
  toplevel = @bud_instance.toplevel
  if toplevel.push_elems[[self.object_id, :inspected]].nil?
    ins = pro{|i| [i.inspect]}
    self.wire_to(ins)
    toplevel.push_elems[[self.object_id, :inspected]] = ins
  end
  toplevel.push_elems[[self.object_id, :inspected]]
end

- (Object) invalidate_cache



191
192
# File 'lib/bud/executor/elements.rb', line 191

def invalidate_cache
end

- (Object) invalidate_tables(rescan, invalidate)



169
170
171
172
173
174
175
176
177
178
179
180
181
182
# File 'lib/bud/executor/elements.rb', line 169

def invalidate_tables(rescan, invalidate)
  # Exchange rescan and invalidate information with tables. If this node is
  # in rescan, it may invalidate an output table (if it is a scratch). And
  # if the output table is going to be invalidated, this node marks itself
  # for rescan to enable a refill of that table at run-time.
  [@outputs, @pendings].each do |v|
    v.each do |o|
      unless o.class <= PushElement
        o.add_rescan_invalidate(rescan, invalidate)
        rescan << self if invalidate.member? o
      end
    end
  end
end

- (Object) join(elem2, &blk)



224
225
226
227
228
229
230
231
232
233
# File 'lib/bud/executor/elements.rb', line 224

def join(elem2, &blk)
  elem2 = elem2.to_push_elem unless elem2.kind_of? PushElement
  toplevel = @bud_instance.toplevel
  join = Bud::PushSHJoin.new([self, elem2], toplevel.this_rule_context, [])
  self.wire_to(join)
  elem2.wire_to(join)
  toplevel.push_elems[[self.object_id, :join, [self, elem2], toplevel, blk]] = join
  toplevel.push_joins[toplevel.this_stratum] << join
  return join
end

- (Boolean) member?(name = nil, bud_instance = nil, the_schema = nil, &blk)



338
339
340
# File 'lib/bud/executor/elements.rb', line 338

def member?(name=nil, bud_instance=nil, the_schema=nil, &blk)
  push_predicate(:member?, name, bud_instance, the_schema, &blk)
end

- (Object) merge(source) Also known as: <=



248
249
250
251
252
253
254
# File 'lib/bud/executor/elements.rb', line 248

def merge(source)
  if source.class <= PushElement and @bud_instance.wiring?
    source.wire_to(self)
  else
    source.each {|i| self << i}
  end
end

- (Boolean) none?(name = nil, bud_instance = nil, the_schema = nil, &blk)



341
342
343
# File 'lib/bud/executor/elements.rb', line 341

def none?(name=nil, bud_instance=nil, the_schema=nil, &blk)
  push_predicate(:none?, name, bud_instance, the_schema, &blk)
end

- (Object) notin(elem2, *preds, &blk)



238
239
240
241
242
243
244
245
246
# File 'lib/bud/executor/elements.rb', line 238

def notin(elem2, *preds, &blk)
  elem2 = elem2.to_push_elem unless elem2.kind_of? PushElement
  toplevel = @bud_instance.toplevel
  notin_elem = Bud::PushNotIn.new([self, elem2], toplevel.this_rule_context, preds, &blk)
  self.wire_to(notin_elem)
  elem2.wire_to(notin_elem)
  toplevel.push_elems[[self.object_id, :notin, [self, elem2], toplevel, blk]] = notin_elem
  return notin_elem
end

- (Boolean) on_include?(item, &blk)



357
358
359
360
361
362
363
364
365
# File 'lib/bud/executor/elements.rb', line 357

def on_include?(item, &blk)
  toplevel = @bud_instance.toplevel
  if toplevel.push_elems[[self.object_id, :on_include?, item, blk]].nil?
    inc = pro{|i| blk.call(item) if i == item and not blk.nil?}
    wire_to(inc)
    toplevel.push_elems[[self.object_id, :on_include?, item, blk]] = inc
  end
  toplevel.push_elems[[self.object_id, :on_include?, item, blk]]
end

- (Boolean) one?(name = nil, bud_instance = nil, the_schema = nil, &blk)



344
345
346
# File 'lib/bud/executor/elements.rb', line 344

def one?(name=nil, bud_instance=nil, the_schema=nil, &blk)
  push_predicate(:one?, name, bud_instance, the_schema, &blk)
end


37
38
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
# File 'lib/bud/executor/elements.rb', line 37

def print_wiring(depth=0, accum="")
  depth.times {print "  "}
  puts "#{accum} #{(self.object_id*2).to_s(16)}: #{qualified_tabname} (#{self.class})"

  [@outputs, @pendings, @deletes, @delete_keys].each do |kind|
    case kind.object_id
    when @outputs.object_id
      next_accum = "=> "
    when @pendings.object_id
      next_accum = "+> "
    when @deletes.object_id, @delete_keys.object_id
      next_accum = "-> "
    end

    kind.each do |o|
      if o.respond_to?(:print_wiring)
        o.print_wiring(depth+1, next_accum)
      else
        (depth+1).times {print "  "}
        print "#{next_accum} "
        if o.class <= Bud::BudCollection
          puts "#{(o.object_id*2).to_s(16)}: #{o.qualified_tabname} (#{o.class})"
        elsif o.class <= Bud::LatticeWrapper
          puts "#{o.inspect}"
        else
          puts "#{(o.object_id*2).to_s(16)}: (#{o.class.name})"
        end
      end
    end
  end
end

- (Object) pro(the_name = elem_name, the_schema = schema, &blk) Also known as: each, on_exists?



200
201
202
203
204
205
206
207
208
209
# File 'lib/bud/executor/elements.rb', line 200

def pro(the_name=elem_name, the_schema=schema, &blk)
  toplevel = @bud_instance.toplevel
  elem = Bud::PushElement.new("project#{object_id}".to_sym,
                              toplevel.this_rule_context,
                              @collection_name, the_schema)
  self.wire_to(elem)
  elem.set_block(&blk)
  toplevel.push_elems[[self.object_id, :pro, blk]] = elem
  return elem
end

- (Object) push_out(item, do_block = true)



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
147
# File 'lib/bud/executor/elements.rb', line 116

def push_out(item, do_block=true)
  if do_block && @blk
    item = item.to_a if @blk.arity > 1
    item = @blk.call item
    return if item.nil?
  end

  @outputs.each do |ou|
    if ou.class <= Bud::PushElement
      ou.insert(item, self)
    elsif ou.class <= Bud::BudCollection
      ou.do_insert(item, ou.new_delta)
    elsif ou.class <= Bud::LatticeWrapper
      ou.insert(item, self)
    else
      raise Bud::Error, "unexpected output target: #{ou.class}"
    end
  end

  # for the following, o is a BudCollection
  @deletes.each{|o| o.pending_delete([item])}
  @delete_keys.each{|o| o.pending_delete_keys([item])}

  # o is a LatticeWrapper or a BudCollection
  @pendings.each do |o|
    if o.class <= Bud::LatticeWrapper
      o <+ item
    else
      o.pending_merge([item])
    end
  end
end

- (Object) push_predicate(pred_symbol, name = nil, bud_instance = nil, the_schema = nil, &blk)



322
323
324
325
326
327
328
# File 'lib/bud/executor/elements.rb', line 322

def push_predicate(pred_symbol, name=nil, bud_instance=nil,
                   the_schema=nil, &blk)
  elem = Bud::PushPredicate.new(pred_symbol, name, bud_instance,
                                the_schema, &blk)
  wire_to(elem)
  elem
end

- (Object) reduce(initial, &blk)



348
349
350
351
352
353
354
# File 'lib/bud/executor/elements.rb', line 348

def reduce(initial, &blk)
  retval = Bud::PushReduce.new("reduce#{Time.new.tv_usec}".to_sym,
                               @bud_instance, @collection_name,
                               schema, initial, &blk)
  self.wire_to(retval)
  retval
end

- (Object) rescan_at_tick



100
101
102
# File 'lib/bud/executor/elements.rb', line 100

def rescan_at_tick
  false
end

- (Object) set_block(&blk)



75
76
77
# File 'lib/bud/executor/elements.rb', line 75

def set_block(&blk)
  @blk = blk
end

- (Object) sort(name = nil, bud_instance = nil, the_schema = nil, &blk)



317
318
319
320
321
# File 'lib/bud/executor/elements.rb', line 317

def sort(name=nil, bud_instance=nil, the_schema=nil, &blk)
  elem = Bud::PushSort.new(name, bud_instance, the_schema, &blk)
  wire_to(elem)
  elem
end

- (Object) stratum_end



193
194
# File 'lib/bud/executor/elements.rb', line 193

def stratum_end
end

- (Object) tick



108
109
110
# File 'lib/bud/executor/elements.rb', line 108

def tick
  invalidate_cache if @invalidated
end

- (Object) tick_deltas



112
113
114
# File 'lib/bud/executor/elements.rb', line 112

def tick_deltas
  @found_delta = false
end

- (Object) wire_to(element, kind = :output)



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/bud/executor/elements.rb', line 79

def wire_to(element, kind=:output)
  unless @bud_instance.wiring?
    raise Bud::Error, "wire_to called outside wiring phase"
  end

  case kind
  when :output
    @outputs << element
  when :pending
    @pendings << element
  when :delete
    @deletes << element
  when :delete_by_key
    @delete_keys << element
  else
    raise Bud::Error, "unrecognized wiring kind: #{kind}"
  end

  element.wired_by << self if element.respond_to? :wired_by
end

- (Object) wirings



32
33
34
# File 'lib/bud/executor/elements.rb', line 32

def wirings
  @wirings ||= @outputs + @pendings + @deletes + @delete_keys
end