alephant-lookup 0.1.2 → 0.1.3

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: ff6ba6dbad05c1c01c5363957f3811447c4f3ce5
4
- data.tar.gz: 0e0e0e340627956a2262bf27f92890238a504e73
3
+ metadata.gz: ee47867bea727abbcf73e15a82c9e5faefea229f
4
+ data.tar.gz: b8d17607728acc5524896fd71c60d4dbcd15edb8
5
5
  SHA512:
6
- metadata.gz: fdaf1f60f7e8c4dadd04abd4a2b2911d8c70f2b79e13d0ecf4c22a96bfca7adb82520bb77331a4a769dd96d0ffb2493dd4cf79617b72d6addd5265b17a182d7b
7
- data.tar.gz: ddad00ab88263ad247740a44721e95d8a8cb191317fad62646a8cdd7a2a1f7f42c7de7235806d89619efef582c8e2195f39b62edbbc0453cc820f8bcb3121dff
6
+ metadata.gz: 1840b7398c7c5aa69d141f575e03e61b43e50c3fd718afa8b16916b3187f058783cf2844bcbcc9817b3e902594270ce3c80bb36cd7a299f4b021d7c5ccffcd8f
7
+ data.tar.gz: ae986df6e7645c0f6376af0af41caf511aa008db4b19d17947163c881eb09210668758d746d16bf17114396f58f59b09fff4723281f71ed95e6cf8d06fa59d79
@@ -6,9 +6,9 @@ module Alephant
6
6
  module Lookup
7
7
  @@lookup_tables = {}
8
8
 
9
- def self.create(table_name, component_id = nil)
9
+ def self.create(table_name)
10
10
  @@lookup_tables[table_name] ||= LookupTable.new(table_name)
11
- LookupHelper.new(@@lookup_tables[table_name], component_id)
11
+ LookupHelper.new(@@lookup_tables[table_name])
12
12
  end
13
13
  end
14
14
  end
@@ -6,43 +6,28 @@ require 'alephant/lookup/location_write'
6
6
  module Alephant
7
7
  module Lookup
8
8
  class LookupHelper
9
- attr_reader :component_id
9
+ attr_reader :lookup_table
10
10
 
11
- def initialize(lookup_table, component_id = nil)
11
+ def initialize(lookup_table)
12
12
  @lookup_table = lookup_table
13
- @component_id = component_id
14
- create_lookup_table
15
- end
16
-
17
- def read(opts, ident = nil)
18
- ident = @component_id || ident
19
- reader = LocationRead.new(@lookup_table)
20
- reader.read(LookupQuery.new(ident, opts)).location
21
- end
22
-
23
- def write(opts, location, ident = nil)
24
- ident = @component_id || ident
25
- batch_write(opts, location, ident)
26
- batch_process
27
- end
28
-
29
- def batch_write(opts, location, ident = nil)
30
- ident = @component_id || ident
31
- @batch_write ||= LocationWrite.new(@lookup_table)
32
- @batch_write << LookupQuery.new(ident, opts, location)
13
+ @lookup_table.create
33
14
  end
34
15
 
35
- def batch_process
36
- @batch_write.process!
37
- @batch_write = nil
16
+ def read(id, opts, batch_version)
17
+ LookupQuery.new(lookup_table.table_name, id, opts, batch_version).run!
38
18
  end
39
19
 
40
- private
41
-
42
- def create_lookup_table
43
- @lookup_table.create
20
+ def write(id, opts, batch_version, location)
21
+ LookupLocation.new(id, batch_version, opts, location).tap do |l|
22
+ lookup_table.table.batch_put([
23
+ {
24
+ :component_key => l.component_key,
25
+ :batch_version => l.batch_version,
26
+ :location => l.location
27
+ }
28
+ ])
29
+ end
44
30
  end
45
-
46
31
  end
47
32
  end
48
33
  end
@@ -1,23 +1,29 @@
1
+ require 'crimp'
2
+
1
3
  module Alephant
2
4
  module Lookup
3
5
  class LookupLocation
4
- S3_LOCATION_FIELD = 'location'
5
-
6
- attr_reader :component_id, :opts, :location
6
+ attr_reader :component_id, :component_key, :opts, :opts_hash, :batch_version
7
+ attr_accessor :location
7
8
 
8
- def initialize(component_id, opts, location)
9
+ def initialize(component_id, batch_version, opts, location = nil)
9
10
  @component_id = component_id
11
+ @batch_version = batch_version
10
12
  @opts = opts
13
+ @opts_hash = hash_for(opts)
11
14
  @location = location
12
15
  end
13
16
 
14
- def to_h
15
- {
16
- :component_id => @component_id,
17
- :opts => opts,
18
- S3_LOCATION_FIELD => @location
19
- }
17
+ def component_key
18
+ "#{component_id}/#{opts_hash}"
19
+ end
20
+
21
+ private
22
+
23
+ def hash_for(opts)
24
+ Crimp.signature opts
20
25
  end
26
+
21
27
  end
22
28
  end
23
29
  end
@@ -3,27 +3,50 @@ require 'alephant/lookup/lookup_location'
3
3
 
4
4
  module Alephant
5
5
  module Lookup
6
- class LookupQuery < LookupLocation
7
- attr_reader :opts_hash
8
- attr_writer :location
6
+ class LookupQuery
7
+ attr_reader :table_name, :lookup_location
9
8
 
10
- def initialize(component_id, opts, location = nil)
11
- super(component_id, opts, location)
12
- @opts_hash = hash_for(opts)
9
+ def initialize(table_name, component_id, batch_version, opts)
10
+ @client = AWS::DynamoDB::Client::V20120810.new
11
+ @table_name = table_name
12
+ @lookup_location = LookupLocation.new(component_id, batch_version, opts)
13
13
  end
14
14
 
15
- def to_h
16
- {
17
- :component_id => @component_id,
18
- :opts_hash => @opts_hash,
19
- S3_LOCATION_FIELD => @location
20
- }
15
+ def run!
16
+ lookup_location.tap do |l|
17
+ l.location = s3_location_from(
18
+ @client.query(to_q)
19
+ )
20
+ end
21
21
  end
22
22
 
23
23
  private
24
24
 
25
- def hash_for(opts)
26
- Crimp.signature opts
25
+ def s3_location_from(result)
26
+ result[:count] == 1 ? result[:member].first['location'][:s] : nil
27
+ end
28
+
29
+ def to_q
30
+ {
31
+ :table_name => table_name,
32
+ :consistent_read => true,
33
+ :select => 'SPECIFIC_ATTRIBUTES',
34
+ :attributes_to_get => ['location'],
35
+ :key_conditions => {
36
+ 'component_key' => {
37
+ :comparison_operator => 'EQ',
38
+ :attribute_value_list => [
39
+ { 's' => @lookup_location.component_key }
40
+ ],
41
+ },
42
+ 'batch_version' => {
43
+ :comparison_operator => 'EQ',
44
+ :attribute_value_list => [
45
+ { 'n' => @lookup_location.batch_version.to_s }
46
+ ]
47
+ }
48
+ }
49
+ }
27
50
  end
28
51
  end
29
52
  end
@@ -14,10 +14,10 @@ module Alephant
14
14
  }
15
15
  SCHEMA = {
16
16
  :hash_key => {
17
- :component_id => :string
17
+ :component_key => :string
18
18
  },
19
19
  :range_key => {
20
- :opts_hash => :string
20
+ :batch_version => :number
21
21
  }
22
22
  }
23
23
 
@@ -1,5 +1,5 @@
1
1
  module Alephant
2
2
  module Lookup
3
- VERSION = "0.1.2"
3
+ VERSION = "0.1.3"
4
4
  end
5
5
  end
data/spec/lookup_spec.rb CHANGED
@@ -6,9 +6,8 @@ describe Alephant::Lookup do
6
6
  Alephant::Lookup::LookupHelper
7
7
  .any_instance
8
8
  .stub(:initialize)
9
- .and_return(double())
10
9
 
11
- expect(subject.create(:table_name, :component_id)).to be_a Alephant::Lookup::LookupHelper
10
+ expect(subject.create(:table_name)).to be_a Alephant::Lookup::LookupHelper
12
11
  end
13
12
  end
14
13
 
@@ -19,93 +18,88 @@ describe Alephant::Lookup do
19
18
  it 'calls create on lookup_table' do
20
19
  table = double()
21
20
  table.should_receive(:create)
22
- subject.new(table, :component_id)
21
+ subject.new(table)
23
22
  end
24
23
  end
25
24
 
26
- describe '#read(opts)' do
27
- let (:lookup_table) { Alephant::Lookup::LookupTable }
28
- let (:s3_location) { '/s3-render-example/test/html/england_council_election_results/responsive' }
29
- let (:location_read){ Alephant::Lookup::LocationRead }
30
-
31
- it 'returns correct S3 Location' do
32
- subject
33
- .any_instance
34
- .stub(:create_lookup_table)
25
+ describe '#read(id, opts, batch_version)' do
26
+ let(:expected_query) do
27
+ {
28
+ :table_name=>"table_name",
29
+ :consistent_read=>true,
30
+ :select=>"SPECIFIC_ATTRIBUTES",
31
+ :attributes_to_get=>["location"],
32
+ :key_conditions=>{
33
+ "component_key"=> {
34
+ :comparison_operator=>"EQ",
35
+ :attribute_value_list=>[{"s"=>"id/9dd916afd5516828a91d259967fd394a"}]
36
+ },
37
+ "batch_version"=>{
38
+ :comparison_operator=>"EQ",
39
+ :attribute_value_list=>[{"n"=>"{:variant=>\"foo\"}"}]
40
+ }
41
+ }
42
+ }
43
+ end
35
44
 
36
- lookup_table
45
+ it 'queries DynamoDb and returns a location' do
46
+ AWS::DynamoDB::Client::V20120810
37
47
  .any_instance
38
48
  .stub(:initialize)
39
49
 
40
- location = Alephant::Lookup::LookupLocation.new(:component_id,:opts_hash, s3_location)
41
-
42
- location_read
50
+ AWS::DynamoDB::Client::V20120810
43
51
  .any_instance
44
- .stub(:read)
45
- .and_return(location)
52
+ .should_receive(:query)
53
+ .with(expected_query)
54
+ .and_return(
55
+ {
56
+ :count => 1,
57
+ :member => [
58
+ { 'location' => { :s => '/location' } }
59
+ ]
60
+ }
61
+ )
46
62
 
47
- pal_opts = {
48
- :id => :england_council_election_results,
49
- :env => :test,
50
- :type => :responsive
51
- }
63
+ table = double().as_null_object
64
+ table.stub(:table_name).and_return('table_name')
52
65
 
53
- instance = subject.new(lookup_table.new, pal_opts[:id])
54
- read_location = instance.read(pal_opts)
66
+ instance = subject.new(table)
67
+ lookup = instance.read('id',{:variant => "foo"},0)
55
68
 
56
- expect(read_location).to eq(s3_location)
69
+ expect(lookup.location).to eq('/location')
57
70
  end
58
71
  end
59
72
 
60
73
  describe '#write(opts, location)' do
61
-
62
- let (:lookup_table) { Alephant::Lookup::LookupTable }
63
- let (:s3_location) { '/s3-render-example/test/html/england_council_election_results/responsive' }
64
- let (:location_write){ Alephant::Lookup::LocationWrite }
65
-
66
- it 'returns lookup_table.update_location_for(component_id, opts_hash, data)' do
67
-
68
- subject
69
- .any_instance
70
- .stub(:create_lookup_table)
71
-
72
- lookup_table
74
+ it 'does not fail' do
75
+ AWS::DynamoDB
73
76
  .any_instance
74
77
  .stub(:initialize)
78
+ .and_return(
79
+ double().as_null_object
80
+ )
75
81
 
76
- lookup_table
77
- .any_instance
78
- .stub(:table_name)
79
- .and_return('table_name')
80
-
81
- pal_opts = {
82
- :id => :england_council_election_results,
83
- :env => :test,
84
- :type => :responsive
85
- }
82
+ ddb_table = double().as_null_object
83
+ ddb_table
84
+ .should_receive(:batch_put)
85
+ .with([{
86
+ :component_key => "id/7e0c33c476b1089500d5f172102ec03e",
87
+ :batch_version => "0",
88
+ :location => "/location"
89
+ }])
86
90
 
87
- AWS::DynamoDB::BatchWrite
88
- .any_instance
89
- .should_receive(:put)
90
- .with(
91
- 'table_name',
92
- [
93
- {
94
- :component_id=> :england_council_election_results,
95
- :opts_hash=>"52a25baaaa8c4527ddc869feaa285c3a",
96
- "location"=>:s3_location
97
- }
98
- ]
99
- )
91
+ lookup_table = double().as_null_object
92
+ lookup_table
93
+ .should_receive(:table)
94
+ .and_return(ddb_table)
100
95
 
101
- AWS::DynamoDB::BatchWrite
96
+ Alephant::Lookup::LookupHelper
102
97
  .any_instance
103
- .should_receive(:process!)
98
+ .stub(:lookup_table)
99
+ .and_return(lookup_table)
104
100
 
105
- subject.new(
106
- lookup_table.new,
107
- pal_opts[:id]
108
- ).write(pal_opts, :s3_location)
101
+ instance = subject.new(lookup_table)
102
+ instance.write('id',{},'0','/location')
109
103
  end
110
104
  end
111
105
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: alephant-lookup
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.1.3
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-03-04 00:00:00.000000000 Z
11
+ date: 2014-03-13 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rspec
@@ -195,8 +195,6 @@ files:
195
195
  - Rakefile
196
196
  - alephant-lookup.gemspec
197
197
  - lib/alephant/lookup.rb
198
- - lib/alephant/lookup/location_read.rb
199
- - lib/alephant/lookup/location_write.rb
200
198
  - lib/alephant/lookup/lookup_helper.rb
201
199
  - lib/alephant/lookup/lookup_location.rb
202
200
  - lib/alephant/lookup/lookup_query.rb
@@ -1,71 +0,0 @@
1
- require 'aws-sdk'
2
- require 'alephant/lookup/lookup_location'
3
- require 'alephant/lookup/lookup_query'
4
- require 'alephant/logger'
5
-
6
- module Alephant
7
- module Lookup
8
- class LocationRead
9
- include ::Alephant::Logger
10
-
11
- attr_reader :table_name
12
-
13
- def initialize(lookup_table)
14
- @client = AWS::DynamoDB::Client::V20120810.new
15
- @table_name = lookup_table.table_name
16
- end
17
-
18
- def read(lookup)
19
- logger.info("LocationRead#read: looking up #{lookup.to_h}")
20
- raise TypeError unless lookup.is_a? LookupQuery
21
-
22
- location = LookupLocation.new(
23
- lookup.component_id,
24
- lookup.opts_hash,
25
- s3_location_from(
26
- run_query(
27
- lookup.component_id,
28
- lookup.opts_hash
29
- )
30
- )
31
- )
32
- logger.info("LocationRead#read: got location #{location.to_h}")
33
-
34
- location
35
- end
36
-
37
- private
38
-
39
- def s3_location_from(result)
40
- result[:count] == 1 ? result[:member].first[LookupLocation::S3_LOCATION_FIELD][:s] : nil
41
- end
42
-
43
- def run_query(component_id, opts_hash)
44
- @client.query(query(component_id, opts_hash))
45
- end
46
-
47
- def query(component_id, opts_hash)
48
- {
49
- :table_name => table_name,
50
- :consistent_read => true,
51
- :select => 'SPECIFIC_ATTRIBUTES',
52
- :attributes_to_get => [LookupLocation::S3_LOCATION_FIELD],
53
- :key_conditions => {
54
- 'component_id' => {
55
- :comparison_operator => 'EQ',
56
- :attribute_value_list => [
57
- { 's' => component_id.to_s }
58
- ],
59
- },
60
- 'opts_hash' => {
61
- :comparison_operator => 'EQ',
62
- :attribute_value_list => [
63
- { 's' => opts_hash }
64
- ]
65
- }
66
- }
67
- }
68
- end
69
- end
70
- end
71
- end
@@ -1,50 +0,0 @@
1
- require 'aws-sdk'
2
- require 'alephant/lookup/lookup_query'
3
- require 'alephant/logger'
4
-
5
- module Alephant
6
- module Lookup
7
- class LocationWrite
8
- include ::Alephant::Logger
9
-
10
- attr_reader :table_name
11
- attr_reader :lookups
12
-
13
- def initialize(lookup_table)
14
- @table_name = lookup_table.table_name
15
- @batch = AWS::DynamoDB::BatchWrite.new
16
- @lookups = []
17
- @processed = false
18
- end
19
-
20
- def <<(lookup)
21
- raise TypeError unless lookup.is_a? LookupQuery
22
-
23
- @lookups << lookup
24
- end
25
-
26
- def processed?
27
- @processed
28
- end
29
-
30
- def process!
31
- logger.info("LocationWrite#process! #{processed? ? "not" : "is"} running batch put on #{table_name}")
32
- processed? ? false : process_batch_put
33
- end
34
-
35
- private
36
-
37
- def process_batch_put
38
- logger.info("LocationWrite#process_batch_put to #{table_name} for #{@lookups.map { |lookup| lookup.to_h }}")
39
-
40
- @batch.put(
41
- table_name,
42
- @lookups.map { |lookup| lookup.to_h }
43
- )
44
- @batch.process!
45
-
46
- processed = true
47
- end
48
- end
49
- end
50
- end