tresse 1.0.0 → 1.2.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (4) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +25 -0
  3. data/lib/tresse.rb +64 -38
  4. metadata +2 -2
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 07e2b40e7224b8da9af9768fb077d0a9552b04ba
4
- data.tar.gz: d5e471d9d944580185fe2aa97e53fec105f87ae8
3
+ metadata.gz: 80b7de6139b9cc1d3e7bacd2f86b7c6b0685d431
4
+ data.tar.gz: 673e625a9a20149d09120348e2ab299a06605c36
5
5
  SHA512:
6
- metadata.gz: e69445a655dc2d80002b087ff8355d1e344a052919be4944b52c94bef65f4065b3d8eb0cf090a0f0e8276cebc028f002399a8b638d892584b51eaefa2cc881fa
7
- data.tar.gz: e812e89f78b44ad82726bc978bd36c780957c645b4fc55278279b770f87ed219c5995ea4ed02a16d5196c78b6d0ebeb87e43ba313d847d0789cff2bb2ff63c2b
6
+ metadata.gz: 073fe8f17b480a03c32c1a22443ad3024f99c5eeff3da696d9e2313899e447f65daf6104af0f601943d1fb0ec618091ee23d75087a33fb6b3ea1f6ff151b1ca1
7
+ data.tar.gz: dd3631c389f68d0855a9f73b57f183a3a4c93a7565dc2c57d82a170ca369acbbd211da856310ca8e5242e92917ffab0c13b07d2986f01d30464cdd600bd69050
data/CHANGELOG.md CHANGED
@@ -2,6 +2,31 @@
2
2
  # CHANGELOG.md
3
3
 
4
4
 
5
+ ## tresse 1.2.0 released 2019-09-17
6
+
7
+ * Re-raise errors at reduction stage
8
+
9
+
10
+ ## tresse 1.1.3 released 2019-09-17
11
+
12
+ * Fix ready for reduction check (prevent it from hanging)
13
+
14
+
15
+ ## tresse 1.1.2 released 2019-09-17
16
+
17
+ * Let Tresse::Group#flatten accept non-arrays
18
+
19
+
20
+ ## tresse 1.1.1 released 2019-09-17
21
+
22
+ * Refine Tresse::Group#source_each
23
+
24
+
25
+ ## tresse 1.1.0 released 2019-09-17
26
+
27
+ * Introduce Tresse::Group#source_each(collection, &block)
28
+
29
+
5
30
  ## tresse 1.0.0 released 2019-09-16
6
31
 
7
32
  * #source / #map / #reduce
data/lib/tresse.rb CHANGED
@@ -4,7 +4,7 @@ require 'thread'
4
4
 
5
5
  module Tresse
6
6
 
7
- VERSION = '1.0.0'
7
+ VERSION = '1.2.0'
8
8
 
9
9
  class << self
10
10
 
@@ -12,15 +12,6 @@ module Tresse
12
12
 
13
13
  @work_queue = Queue.new
14
14
  @work_threads = 8.times.collect { |i| make_work_thread }
15
-
16
- @on_error =
17
- lambda do |where, err|
18
- puts "-" * 80
19
- p where
20
- p err
21
- puts err.backtrace
22
- puts "-" * 80
23
- end
24
15
  end
25
16
 
26
17
  def enqueue(batch)
@@ -30,11 +21,6 @@ module Tresse
30
21
  batch.group
31
22
  end
32
23
 
33
- def on_error(&block)
34
-
35
- @on_error = block
36
- end
37
-
38
24
  def max_work_thread_count
39
25
 
40
26
  @work_threads.size
@@ -73,7 +59,7 @@ module Tresse
73
59
 
74
60
  rescue => err
75
61
 
76
- @on_error.call(:in_worker_thread, err)
62
+ batch.error = err
77
63
  end
78
64
  end
79
65
  end
@@ -83,11 +69,19 @@ module Tresse
83
69
  self.init
84
70
 
85
71
 
72
+ def self.call_block(block, args)
73
+
74
+ block.call(*args[0, block.arity.abs])
75
+ end
76
+
77
+
86
78
  class Batch
87
79
 
88
80
  attr_reader :group
89
81
  attr_reader :map_index
82
+ attr_reader :completed
90
83
  attr_accessor :value
84
+ attr_reader :error
91
85
 
92
86
  def initialize(group, block_or_group)
93
87
 
@@ -96,6 +90,7 @@ module Tresse
96
90
 
97
91
  @map_index = -1
98
92
  @value = nil
93
+ @completed = false
99
94
  end
100
95
 
101
96
  def process
@@ -106,27 +101,31 @@ module Tresse
106
101
 
107
102
  def source
108
103
 
109
- args = [ group ] + [ nil ] * 7
110
- args = args[0, @bog.method(:call).arity]
111
-
112
- @value = @bog.call(*args)
104
+ @value = Tresse.call_block(@bog, [ group ] + [ nil ] * 7)
113
105
  end
114
106
 
115
107
  def map(type, block)
116
108
 
117
- args = [ @value, self ]
118
- args = args[0, block.method(:call).arity.abs]
119
-
120
- r = block.call(*args)
109
+ r = Tresse.call_block(block, [ @value, self ])
121
110
 
122
111
  @value = r if type == :map
123
112
  end
113
+
114
+ def complete
115
+
116
+ @completed = true
117
+ end
118
+
119
+ def error=(err)
120
+
121
+ @error = err
122
+ @group.send(:receive, self)
123
+ end
124
124
  end
125
125
 
126
126
  class Group
127
127
 
128
128
  attr_accessor :name
129
- #attr_reader :batches
130
129
 
131
130
  def initialize(name=nil)
132
131
 
@@ -137,18 +136,29 @@ module Tresse
137
136
  @maps = [ nil ]
138
137
 
139
138
  @reduce = nil
140
- @reduce_batches = []
139
+ @reduce_mutex = Mutex.new
141
140
  @reduction_queue = Queue.new
142
141
  end
143
142
 
144
143
  #
145
144
  # sourcing methods
146
145
 
147
- def source(o=nil, &block)
146
+ def source(&block)
147
+
148
+ @batches << Tresse::Batch.new(self, block)
149
+
150
+ self
151
+ end
148
152
 
149
- batch = Tresse::Batch.new(self, o ? o : block)
153
+ def source_each(collection, &block)
150
154
 
151
- @batches << batch
155
+ if collection.is_a?(Hash)
156
+ collection.each { |k, v|
157
+ source { Tresse.call_block(block, [ k, v ]) } }
158
+ else
159
+ collection.each_with_index { |e, i|
160
+ source { Tresse.call_block(block, [ e, i ]) } }
161
+ end
152
162
 
153
163
  self
154
164
  end
@@ -177,7 +187,14 @@ module Tresse
177
187
 
178
188
  def flatten
179
189
 
180
- do_reduce([], lambda { |a, e| a.concat(e) })
190
+ do_reduce(
191
+ [],
192
+ lambda { |a, e|
193
+ if e.respond_to?(:to_a) && ! e.is_a?(Hash)
194
+ a.concat(e.to_a)
195
+ else
196
+ a.push(e)
197
+ end })
181
198
  end
182
199
  alias values flatten
183
200
 
@@ -198,7 +215,11 @@ module Tresse
198
215
 
199
216
  launch
200
217
 
201
- @reduction_queue.pop
218
+ r = @reduction_queue.pop
219
+
220
+ raise r.error if r.is_a?(Tresse::Batch)
221
+
222
+ r
202
223
  end
203
224
 
204
225
  def launch
@@ -211,7 +232,9 @@ module Tresse
211
232
 
212
233
  def receive(batch)
213
234
 
214
- if batch.map_index == 0
235
+ if batch.error
236
+ @reduction_queue << batch
237
+ elsif batch.map_index == 0
215
238
  batch.source
216
239
  Tresse.enqueue(batch)
217
240
  elsif m = @maps[batch.map_index]
@@ -224,15 +247,18 @@ module Tresse
224
247
 
225
248
  def queue_for_reduction(batch)
226
249
 
227
- @reduce_batches << batch
250
+ @reduce_mutex.synchronize do
251
+
252
+ batch.complete
228
253
 
229
- return if @reduce_batches.size < @batches.size
230
- return unless @reduce
254
+ return unless @reduce
255
+ return if @batches.find { |b| ! b.completed }
231
256
 
232
- es = @batches.collect(&:value)
233
- target, block = @reduce
257
+ es = @batches.collect(&:value)
258
+ target, block = @reduce
234
259
 
235
- @reduction_queue << es.inject(target, &block)
260
+ @reduction_queue << es.inject(target, &block)
261
+ end
236
262
  end
237
263
  end
238
264
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: tresse
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - John Mettraux
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2019-09-16 00:00:00.000000000 Z
11
+ date: 2019-09-17 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rspec