alephant-sequencer 0.0.7 → 0.0.8
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/README.md +12 -7
- data/alephant-sequencer.gemspec +1 -0
- data/lib/alephant/sequencer/sequence_table.rb +9 -12
- data/lib/alephant/sequencer/sequencer.rb +4 -0
- data/lib/alephant/sequencer/version.rb +1 -1
- data/spec/sequencer_spec.rb +92 -37
- data/spec/spec_helper.rb +3 -2
- metadata +16 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 0390458d47e5ab213103d386505b95893d3b22ff
|
4
|
+
data.tar.gz: 6233dd2447347b73b0cb62349aa28fa3c6f74a3d
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: b587d839b93e7eca666ca6e947e8d5c941331b582d0629bf92fe541dab39c899dd9494d65c3c57e214e0c43dc6a374609039dd4fc7ff782da8ef17444ad5e5ed
|
7
|
+
data.tar.gz: 6b54e05af1e9997358adbe0abf83010f9701da8781310578bbb8df75a113dd3d962186e810e8380d05c4fdabbffdb323623b1e1ec0f76e8fae6a428d8e12c2cd
|
data/README.md
CHANGED
@@ -24,29 +24,34 @@ Or install it yourself as:
|
|
24
24
|
## Usage
|
25
25
|
|
26
26
|
```rb
|
27
|
+
require "alephant/sequencer"
|
27
28
|
|
28
|
-
|
29
|
+
# Ensure you check your AWS region configuration before you start using `AWS.config.region`
|
30
|
+
# To set the value use: AWS.config(region: 'eu-west-1')
|
29
31
|
|
30
|
-
|
31
|
-
|
32
|
+
table_name = "foo"
|
33
|
+
component_id = "bar_baz/e8c81cbbbeb3967a423bb49e352eed0e"
|
34
|
+
sequence_id = "$.sequence_number" # Optional JSONPath (specifying location of sequence_id)
|
32
35
|
|
33
|
-
sequencer = Sequencer.create(table_name,
|
36
|
+
sequencer = Alephant::Sequencer.create(table_name, component_id, sequence_id)
|
34
37
|
|
35
38
|
# Data from SQS message
|
36
|
-
|
39
|
+
json = JSON.generate({ :sequence_number => 3 })
|
40
|
+
msg = Struct.new(:body).new(json)
|
37
41
|
|
38
42
|
# Sets last seen id
|
39
|
-
sequencer.set_last_seen(
|
43
|
+
sequencer.set_last_seen(msg)
|
40
44
|
|
41
45
|
# Gets last seen id
|
42
46
|
sequencer.get_last_seen
|
43
47
|
# => 3
|
44
48
|
|
45
49
|
# Is the message sequential?
|
46
|
-
sequencer.sequential?(
|
50
|
+
sequencer.sequential?(msg)
|
47
51
|
|
48
52
|
# Reset sequence
|
49
53
|
sequencer.delete!
|
54
|
+
```
|
50
55
|
|
51
56
|
## Contributing
|
52
57
|
1. Fork it ( http://github.com/<my-github-username>/alephant-sequencer/fork )
|
data/alephant-sequencer.gemspec
CHANGED
@@ -1,21 +1,17 @@
|
|
1
|
-
require
|
2
|
-
require
|
3
|
-
require
|
1
|
+
require "aws-sdk"
|
2
|
+
require "thread"
|
3
|
+
require "timeout"
|
4
4
|
|
5
|
-
require
|
5
|
+
require "alephant/logger"
|
6
|
+
require "alephant/support/dynamodb/table"
|
6
7
|
|
7
8
|
module Alephant
|
8
9
|
module Sequencer
|
9
|
-
class SequenceTable
|
10
|
+
class SequenceTable < ::Alephant::Support::DynamoDB::Table
|
10
11
|
include ::Alephant::Logger
|
11
12
|
|
12
13
|
attr_reader :table_name
|
13
14
|
|
14
|
-
TIMEOUT = 120
|
15
|
-
DEFAULT_CONFIG = {
|
16
|
-
:write_units => 5,
|
17
|
-
:read_units => 10,
|
18
|
-
}
|
19
15
|
SCHEMA = {
|
20
16
|
:hash_key => {
|
21
17
|
:key => :string,
|
@@ -47,7 +43,7 @@ module Alephant
|
|
47
43
|
|
48
44
|
def sequence_for(ident)
|
49
45
|
rows = batch_get_value_for(ident)
|
50
|
-
rows.count >= 1 ? rows.first[
|
46
|
+
rows.count >= 1 ? rows.first["value"].to_i : nil
|
51
47
|
end
|
52
48
|
|
53
49
|
def set_sequence_for(ident, value, last_seen_check = nil)
|
@@ -78,12 +74,13 @@ module Alephant
|
|
78
74
|
end
|
79
75
|
|
80
76
|
private
|
77
|
+
|
81
78
|
def put_condition(last_seen_check)
|
82
79
|
last_seen_check.nil? ? unless_exists(:key) : if_value(last_seen_check)
|
83
80
|
end
|
84
81
|
|
85
82
|
def batch_get_value_for(ident)
|
86
|
-
table.batch_get([
|
83
|
+
table.batch_get(["value"],[ident],batch_get_opts)
|
87
84
|
end
|
88
85
|
|
89
86
|
def unless_exists(key)
|
data/spec/sequencer_spec.rb
CHANGED
@@ -1,18 +1,23 @@
|
|
1
|
-
require
|
1
|
+
require "spec_helper"
|
2
2
|
|
3
3
|
describe Alephant::Sequencer do
|
4
|
-
let(:ident)
|
4
|
+
let(:ident) { :ident }
|
5
5
|
let(:jsonpath) { "$.sequence_id" }
|
6
6
|
|
7
7
|
describe ".create(table_name, ident, jsonpath)" do
|
8
8
|
it "should return a Sequencer" do
|
9
|
-
Alephant::Sequencer::SequenceTable
|
10
|
-
|
9
|
+
Alephant::Sequencer::SequenceTable
|
10
|
+
.any_instance
|
11
|
+
.stub(:create)
|
12
|
+
|
13
|
+
expect(
|
14
|
+
subject.create(:table_name, ident, jsonpath)
|
15
|
+
).to be_a Alephant::Sequencer::Sequencer
|
11
16
|
end
|
12
17
|
end
|
13
18
|
|
14
19
|
describe Alephant::Sequencer::Sequencer do
|
15
|
-
let(:data)
|
20
|
+
let(:data) { double() }
|
16
21
|
let(:last_seen) { 42 }
|
17
22
|
|
18
23
|
def sequence_table
|
@@ -21,7 +26,7 @@ describe Alephant::Sequencer do
|
|
21
26
|
table.stub(:sequence_exists)
|
22
27
|
table.stub(:sequence_for)
|
23
28
|
table.stub(:set_sequence_for)
|
24
|
-
|
29
|
+
table.stub(:truncate!)
|
25
30
|
table
|
26
31
|
end
|
27
32
|
|
@@ -42,12 +47,10 @@ describe Alephant::Sequencer do
|
|
42
47
|
end
|
43
48
|
end
|
44
49
|
|
45
|
-
|
46
50
|
describe "#sequence(msg, &block)" do
|
47
51
|
let(:message) do
|
48
52
|
m = double()
|
49
53
|
m.stub(:body)
|
50
|
-
|
51
54
|
m
|
52
55
|
end
|
53
56
|
|
@@ -71,15 +74,19 @@ describe Alephant::Sequencer do
|
|
71
74
|
|
72
75
|
context "last_seen_id is nil" do
|
73
76
|
before(:each) do
|
74
|
-
Alephant::Sequencer::Sequencer
|
75
|
-
.
|
77
|
+
Alephant::Sequencer::Sequencer
|
78
|
+
.any_instance
|
79
|
+
.stub(:get_last_seen)
|
80
|
+
.and_return(nil)
|
76
81
|
|
77
82
|
Alephant::Sequencer::Sequencer
|
78
|
-
.stub(:sequence_id_from)
|
83
|
+
.stub(:sequence_id_from)
|
84
|
+
.and_return(stubbed_seen_high)
|
79
85
|
end
|
80
86
|
|
81
87
|
it "should not call set_last_seen(msg, last_seen_id)" do
|
82
|
-
Alephant::Sequencer::Sequencer
|
88
|
+
Alephant::Sequencer::Sequencer
|
89
|
+
.any_instance
|
83
90
|
.should_receive(:set_last_seen)
|
84
91
|
.with(message, nil)
|
85
92
|
|
@@ -90,15 +97,19 @@ describe Alephant::Sequencer do
|
|
90
97
|
|
91
98
|
context "last_seen_id == sequence_id_from(msg)" do
|
92
99
|
before(:each) do
|
93
|
-
Alephant::Sequencer::Sequencer
|
94
|
-
.
|
100
|
+
Alephant::Sequencer::Sequencer
|
101
|
+
.any_instance
|
102
|
+
.stub(:get_last_seen)
|
103
|
+
.and_return(stubbed_last_seen)
|
95
104
|
|
96
105
|
Alephant::Sequencer::Sequencer
|
97
|
-
.stub(:sequence_id_from)
|
106
|
+
.stub(:sequence_id_from)
|
107
|
+
.and_return(stubbed_last_seen)
|
98
108
|
end
|
99
109
|
|
100
110
|
it "should not call set_last_seen(msg, last_seen_id)" do
|
101
|
-
Alephant::Sequencer::Sequencer
|
111
|
+
Alephant::Sequencer::Sequencer
|
112
|
+
.any_instance
|
102
113
|
.should_not_receive(:set_last_seen)
|
103
114
|
|
104
115
|
subject = Alephant::Sequencer::Sequencer.new(sequence_table, ident, jsonpath)
|
@@ -108,15 +119,20 @@ describe Alephant::Sequencer do
|
|
108
119
|
|
109
120
|
context "last_seen_id > sequence_id_from(msg)" do
|
110
121
|
before(:each) do
|
111
|
-
Alephant::Sequencer::Sequencer
|
112
|
-
.
|
122
|
+
Alephant::Sequencer::Sequencer
|
123
|
+
.any_instance
|
124
|
+
.stub(:get_last_seen)
|
125
|
+
.and_return(stubbed_last_seen)
|
113
126
|
|
114
|
-
Alephant::Sequencer::Sequencer
|
115
|
-
.
|
127
|
+
Alephant::Sequencer::Sequencer
|
128
|
+
.any_instance
|
129
|
+
.stub(:sequence_id_from)
|
130
|
+
.and_return(stubbed_seen_low)
|
116
131
|
end
|
117
132
|
|
118
133
|
it "should not call set_last_seen(msg, last_seen_id)" do
|
119
|
-
Alephant::Sequencer::Sequencer
|
134
|
+
Alephant::Sequencer::Sequencer
|
135
|
+
.any_instance
|
120
136
|
.should_not_receive(:set_last_seen)
|
121
137
|
|
122
138
|
subject = Alephant::Sequencer::Sequencer.new(sequence_table, ident, jsonpath)
|
@@ -126,15 +142,19 @@ describe Alephant::Sequencer do
|
|
126
142
|
|
127
143
|
context "last_seen_id < sequence_id_from(msg)" do
|
128
144
|
before(:each) do
|
129
|
-
Alephant::Sequencer::Sequencer
|
130
|
-
.
|
145
|
+
Alephant::Sequencer::Sequencer
|
146
|
+
.any_instance
|
147
|
+
.stub(:get_last_seen)
|
148
|
+
.and_return(stubbed_last_seen)
|
131
149
|
|
132
150
|
Alephant::Sequencer::Sequencer
|
133
|
-
.stub(:sequence_id_from)
|
151
|
+
.stub(:sequence_id_from)
|
152
|
+
.and_return(stubbed_seen_high)
|
134
153
|
end
|
135
154
|
|
136
155
|
it "should call set_last_seen(msg, last_seen_id)" do
|
137
|
-
Alephant::Sequencer::Sequencer
|
156
|
+
Alephant::Sequencer::Sequencer
|
157
|
+
.any_instance
|
138
158
|
.should_receive(:set_last_seen)
|
139
159
|
.with(message, stubbed_last_seen)
|
140
160
|
|
@@ -149,17 +169,23 @@ describe Alephant::Sequencer do
|
|
149
169
|
table = double()
|
150
170
|
table.stub(:sequence_exists)
|
151
171
|
table.stub(:create)
|
152
|
-
table.should_receive(:sequence_for)
|
172
|
+
table.should_receive(:sequence_for)
|
173
|
+
.with(ident)
|
174
|
+
.and_return(:expected_value)
|
153
175
|
|
154
176
|
expect(
|
155
|
-
Alephant::Sequencer::Sequencer
|
177
|
+
Alephant::Sequencer::Sequencer
|
178
|
+
.new(table, ident, jsonpath)
|
179
|
+
.get_last_seen
|
156
180
|
).to eq(:expected_value)
|
157
181
|
end
|
158
182
|
end
|
159
183
|
|
160
184
|
describe "#set_last_seen(data)" do
|
161
185
|
before(:each) do
|
162
|
-
Alephant::Sequencer::Sequencer
|
186
|
+
Alephant::Sequencer::Sequencer
|
187
|
+
.stub(:sequence_id_from)
|
188
|
+
.and_return(last_seen)
|
163
189
|
end
|
164
190
|
|
165
191
|
it "calls set_sequence_for(ident, last_seen)" do
|
@@ -167,32 +193,46 @@ describe Alephant::Sequencer do
|
|
167
193
|
table.stub(:sequence_exists)
|
168
194
|
table.stub(:create)
|
169
195
|
table.stub(:sequence_for)
|
170
|
-
table.should_receive(:set_sequence_for)
|
196
|
+
table.should_receive(:set_sequence_for)
|
197
|
+
.with(ident, last_seen, nil)
|
171
198
|
|
172
|
-
Alephant::Sequencer::Sequencer
|
199
|
+
Alephant::Sequencer::Sequencer
|
200
|
+
.new(table, ident, jsonpath)
|
201
|
+
.set_last_seen(data)
|
173
202
|
end
|
174
203
|
end
|
175
204
|
|
176
205
|
describe ".sequence_id_from(data)" do
|
177
206
|
subject { Alephant::Sequencer::Sequencer }
|
207
|
+
|
178
208
|
it "should return the id described by the set jsonpath" do
|
179
|
-
msg = Struct.new(:body).new(
|
180
|
-
|
209
|
+
msg = Struct.new(:body).new("set_sequence_id" => 1)
|
210
|
+
|
211
|
+
expect(
|
212
|
+
subject.sequence_id_from(msg, "$.set_sequence_id")
|
213
|
+
).to eq(1)
|
181
214
|
end
|
182
215
|
end
|
183
216
|
|
184
217
|
describe "#sequential?(data, jsonpath)" do
|
185
|
-
|
186
218
|
before(:each) do
|
187
|
-
Alephant::Sequencer::Sequencer
|
188
|
-
|
219
|
+
Alephant::Sequencer::Sequencer
|
220
|
+
.any_instance
|
221
|
+
.stub(:get_last_seen)
|
222
|
+
.and_return(1)
|
223
|
+
|
224
|
+
data.stub(:body)
|
225
|
+
.and_return("sequence_id" => id_value)
|
189
226
|
end
|
190
227
|
|
191
228
|
context "jsonpath = '$.sequence_id'" do
|
192
|
-
let(:jsonpath) {
|
229
|
+
let(:jsonpath) { "$.sequence_id" }
|
230
|
+
|
193
231
|
subject { Alephant::Sequencer::Sequencer.new(sequence_table, :ident, jsonpath) }
|
232
|
+
|
194
233
|
context "sequential" do
|
195
234
|
let(:id_value) { 2 }
|
235
|
+
|
196
236
|
it "is true" do
|
197
237
|
expect(subject.sequential?(data)).to be_true
|
198
238
|
end
|
@@ -200,6 +240,7 @@ describe Alephant::Sequencer do
|
|
200
240
|
|
201
241
|
context "nonsequential" do
|
202
242
|
let(:id_value) { 0 }
|
243
|
+
|
203
244
|
it "is false" do
|
204
245
|
expect(subject.sequential?(data)).to be_false
|
205
246
|
end
|
@@ -207,11 +248,13 @@ describe Alephant::Sequencer do
|
|
207
248
|
end
|
208
249
|
|
209
250
|
context "jsonpath = nil" do
|
210
|
-
let(:jsonpath) {
|
251
|
+
let(:jsonpath) { "$.sequence_id" }
|
252
|
+
|
211
253
|
subject { Alephant::Sequencer::Sequencer.new(sequence_table, :ident, jsonpath) }
|
212
254
|
|
213
255
|
context "sequential" do
|
214
256
|
let(:id_value) { 2 }
|
257
|
+
|
215
258
|
it "is true" do
|
216
259
|
expect(subject.sequential?(data)).to be_true
|
217
260
|
end
|
@@ -219,12 +262,24 @@ describe Alephant::Sequencer do
|
|
219
262
|
|
220
263
|
context "nonsequential" do
|
221
264
|
let(:id_value) { 0 }
|
265
|
+
|
222
266
|
it "is false" do
|
223
267
|
expect(subject.sequential?(data)).to be_false
|
224
268
|
end
|
225
269
|
end
|
226
270
|
end
|
271
|
+
end
|
272
|
+
|
273
|
+
describe "#truncate!" do
|
274
|
+
it "verify SequenceTable#truncate!" do
|
275
|
+
table = double()
|
276
|
+
table.stub(:create)
|
277
|
+
table.stub(:sequence_exists)
|
278
|
+
table.should_receive(:truncate!)
|
227
279
|
|
280
|
+
subject = Alephant::Sequencer::Sequencer.new(table, ident, jsonpath)
|
281
|
+
subject.truncate!
|
282
|
+
end
|
228
283
|
end
|
229
284
|
end
|
230
285
|
end
|
data/spec/spec_helper.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: alephant-sequencer
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.8
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Robert Kenny
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-
|
11
|
+
date: 2014-04-02 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -164,6 +164,20 @@ dependencies:
|
|
164
164
|
version: '0'
|
165
165
|
prerelease: false
|
166
166
|
type: :runtime
|
167
|
+
- !ruby/object:Gem::Dependency
|
168
|
+
name: alephant-support
|
169
|
+
version_requirements: !ruby/object:Gem::Requirement
|
170
|
+
requirements:
|
171
|
+
- - '>='
|
172
|
+
- !ruby/object:Gem::Version
|
173
|
+
version: '0'
|
174
|
+
requirement: !ruby/object:Gem::Requirement
|
175
|
+
requirements:
|
176
|
+
- - '>='
|
177
|
+
- !ruby/object:Gem::Version
|
178
|
+
version: '0'
|
179
|
+
prerelease: false
|
180
|
+
type: :runtime
|
167
181
|
- !ruby/object:Gem::Dependency
|
168
182
|
name: jsonpath
|
169
183
|
version_requirements: !ruby/object:Gem::Requirement
|