em-bucketer 0.1.0 → 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 772ea9675357649b2fe6b347fe4b62597570e4cc
4
- data.tar.gz: 2cc15059f43071145dc0ea0dd215dcac32064fc2
3
+ metadata.gz: eaaf6c54984c095f03204b6682e10fb6a357ac82
4
+ data.tar.gz: 011accd36e8332183ddb1b4028416027c81e10cb
5
5
  SHA512:
6
- metadata.gz: c0066f7f7e86c782c6f926655f4bdf6fce7eb6a9760c2315bc2be62fc6362fa0bc02fb497a8141c1f5b3d324c941f350683e372a55eb97b8d3012224598ee685
7
- data.tar.gz: 72579c53678f6a894aba3369d76708841eed55c01f12cd11b4923200af5b23224f9b71ceaa04801fb0bc4eaf58e7d8d7d5d124c9ce9d55744d7788537950a42f
6
+ metadata.gz: a9ee2a1101c26baf9bce9383079a7192251231fccb02cceca8d46c2d90d6aa1e1cdeb9ce22228812fbfa56b707ae603a2b92d0241dc923e66828921a12847258
7
+ data.tar.gz: 985c4c1b96dee1b3ecba7e94aa7779c13733397aea0e3ef3fec52f63d9d9614e2f4894a95fd833453ca4892e0213bb6ce0f5f6c119b9181f6e443081b784a8a4
data/.yardopts ADDED
@@ -0,0 +1 @@
1
+ --markup markdown
@@ -101,6 +101,39 @@ module EventMachine::Bucketer
101
101
  end
102
102
  end
103
103
 
104
+ # Get at most `count` number of items from
105
+ # the bucket and remove them.
106
+ #
107
+ # @example get 100 items from the bucket
108
+ # bucketer.get_and_remove("1", 100) do |items|
109
+ # p "yay I got #{items.count} items"
110
+ # items.each do |i|
111
+ # p "got #{i}"
112
+ # end
113
+ # end
114
+ #
115
+ # @param bucket_id [String] the bucket id
116
+ # of the bucket you want to get
117
+ # @param count [Integer] the number of items
118
+ # you want from the bucket
119
+ # @yield [Array] the items you put
120
+ # into the bucket
121
+ def get_and_remove(bucket_id, count, &blk)
122
+ EM::Completion.new.tap do |c|
123
+ c.callback(&blk) if block_given?
124
+ get_bucket_from_db(bucket_id).callback do |bucket|
125
+ empty_bucket(bucket_id).callback do
126
+ values = []
127
+ EM::Iterator.new(bucket).each(get_and_remove_iterator(bucket_id, count, values, c), -> { c.succeed(values) })
128
+ end.errback do |e|
129
+ c.fail e
130
+ end
131
+ end.errback do |e|
132
+ c.fail e
133
+ end
134
+ end
135
+ end
136
+
104
137
  # Empty a bucket
105
138
  #
106
139
  # @param bucket_id [String] the bucket id
@@ -119,6 +152,24 @@ module EventMachine::Bucketer
119
152
 
120
153
  private
121
154
 
155
+ def get_and_remove_iterator(bucket_id, count, values, completion)
156
+ added = 0
157
+ proc do |tuple, iter|
158
+ key, val = tuple[0], tuple[1]
159
+ if added < count
160
+ added += 1
161
+ values << val
162
+ iter.next
163
+ else
164
+ add_item(bucket_id, key, val).callback do
165
+ iter.next
166
+ end.errback do |e|
167
+ completion.fail e
168
+ end
169
+ end
170
+ end
171
+ end
172
+
122
173
  def bucket_full?(bucket_id, &blk)
123
174
  bucket_size_from_db(bucket_id).callback do |size|
124
175
  blk.call size >= @bucket_threshold_size
@@ -1,5 +1,5 @@
1
1
  module EventMachine
2
2
  module Bucketer
3
- VERSION = "0.1.0"
3
+ VERSION = "0.1.1"
4
4
  end
5
5
  end
@@ -30,6 +30,25 @@ shared_examples "a bucketer" do
30
30
  end
31
31
  end
32
32
 
33
+ it 'handles multiple buckets' do
34
+ EM.run do
35
+ EM.add_timer(0.1) { fail "didn't reach EM.stop" }
36
+ bucketer.on_bucket_full do |bucket_id|
37
+ fail "shouldn't have called full"
38
+ end
39
+
40
+ add_n_items(bucketer, "1", 3) do
41
+ add_n_items(bucketer, "2", 3) do
42
+
43
+ bucketer.get_bucket("1") do |bucket|
44
+ expect(bucket).to eq([{:id => 0}, {:id => 1}, {:id => 2}])
45
+ EM.stop
46
+ end
47
+ end
48
+ end
49
+ end
50
+ end
51
+
33
52
  it 'calls on_bucket_full when a bucket fills up' do
34
53
  EM.run do
35
54
  EM.add_timer(0.1) { fail "didn't reach EM.stop" }
@@ -105,4 +124,23 @@ shared_examples "a bucketer" do
105
124
  end
106
125
  end
107
126
  end
127
+
128
+ describe '#get_and_remove' do
129
+ it 'gets 2 items and removes them' do
130
+ EM.run do
131
+ EM.add_timer(0.1) { fail "didn't reach EM.stop" }
132
+ add_n_items(bucketer, "1", 3) do
133
+
134
+ bucketer.get_and_remove("1", 2) do |bucket|
135
+ expect(bucket.count).to eq(2)
136
+
137
+ bucketer.get_bucket("1") do |remaining_bucket|
138
+ expect(remaining_bucket.count).to eq(1)
139
+ EM.stop
140
+ end
141
+ end
142
+ end
143
+ end
144
+ end
145
+ end
108
146
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: em-bucketer
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Richard Heycock
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2014-11-01 00:00:00.000000000 Z
12
+ date: 2014-11-03 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: eventmachine
@@ -131,6 +131,7 @@ extensions: []
131
131
  extra_rdoc_files: []
132
132
  files:
133
133
  - ".gitignore"
134
+ - ".yardopts"
134
135
  - Gemfile
135
136
  - LICENSE
136
137
  - README.md