bud 0.0.8 → 0.1.0.pre1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,104 @@
1
+ require 'bud/executor/elements'
2
+
3
+ module Bud
4
+ class PushGroup < PushStatefulElement
5
+ def initialize(elem_name, bud_instance, collection_name, keys_in, aggpairs_in, schema_in, &blk)
6
+ @groups = {}
7
+ if keys_in.nil?
8
+ @keys = []
9
+ else
10
+ @keys = keys_in.map{|k| k[1]}
11
+ end
12
+ # ap[1] is nil for Count
13
+ @aggpairs = aggpairs_in.map{|ap| ap[1].nil? ? [ap[0]] : [ap[0], ap[1][1]]}
14
+ super(elem_name, bud_instance, collection_name, schema_in, &blk)
15
+ end
16
+
17
+ def insert(item, source)
18
+ key = @keys.map{|k| item[k]}
19
+ @aggpairs.each_with_index do |ap, agg_ix|
20
+ agg_input = ap[1].nil? ? item : item[ap[1]]
21
+ agg = (@groups[key].nil? or @groups[key][agg_ix].nil?) ? ap[0].send(:init, agg_input) : ap[0].send(:trans, @groups[key][agg_ix], agg_input)[0]
22
+ @groups[key] ||= Array.new(@aggpairs.length)
23
+ @groups[key][agg_ix] = agg
24
+ push_out(nil)
25
+ end
26
+ end
27
+
28
+ def invalidate_cache
29
+ puts "Group #{qualified_tabname} invalidated" if $BUD_DEBUG
30
+ @groups.clear
31
+ end
32
+
33
+ def flush
34
+ @groups.each do |g, grps|
35
+ grp = @keys == $EMPTY ? [[]] : [g]
36
+ @aggpairs.each_with_index do |ap, agg_ix|
37
+ grp << ap[0].send(:final, grps[agg_ix])
38
+ end
39
+ outval = grp[0].flatten
40
+ (1..grp.length-1).each {|i| outval << grp[i]}
41
+ push_out(outval)
42
+ end
43
+ #@groups = {}
44
+ end
45
+ end
46
+
47
+ class PushArgAgg < PushGroup
48
+ def initialize(elem_name, bud_instance, collection_name, keys_in, aggpairs_in, schema_in, &blk)
49
+ raise "Multiple aggpairs #{aggpairs_in.map{|a| a.class.name}} in ArgAgg; only one allowed" if aggpairs_in.length > 1
50
+ super(elem_name, bud_instance, collection_name, keys_in, aggpairs_in, schema_in, &blk)
51
+ @agg = @aggpairs[0][0]
52
+ @aggcol = @aggpairs[0][1]
53
+ @winners = {}
54
+ end
55
+
56
+ public
57
+ def invalidate_cache
58
+ puts "#{self.class}/#{self.tabname} invalidated" if $BUD_DEBUG
59
+ @groups.clear
60
+ @winners.clear
61
+ end
62
+
63
+ def insert(item, source)
64
+ key = @keys.map{|k| item[k]}
65
+ @aggpairs.each_with_index do |ap, agg_ix|
66
+ agg_input = item[ap[1]]
67
+ if @groups[key].nil?
68
+ agg = ap[0].send(:init, agg_input)
69
+ @winners[key] = [item]
70
+ else
71
+ agg_result = ap[0].send(:trans, @groups[key][agg_ix], agg_input)
72
+ agg = agg_result[0]
73
+ case agg_result[1]
74
+ when :ignore
75
+ # do nothing
76
+ when :replace
77
+ @winners[key] = [item]
78
+ when :keep
79
+ @winners[key] << item
80
+ when :delete
81
+ agg_result[2..-1].each do |t|
82
+ @winners[key].delete t unless @winners[key].empty?
83
+ end
84
+ else
85
+ raise "strange result from argagg finalizer"
86
+ end
87
+ end
88
+ @groups[key] ||= Array.new(@aggpairs.length)
89
+ @groups[key][agg_ix] = agg
90
+ #push_out(nil)
91
+ end
92
+ end
93
+
94
+ def flush
95
+ @groups.keys.each {|g|
96
+ @winners[g].each{|t|
97
+ push_out(t, false)
98
+ }
99
+ }
100
+ #@groups = {}
101
+ #@winners = {}
102
+ end
103
+ end
104
+ end