Class: Rubber::Cloud::Aws::TableStore
- Inherits:
-
Object
- Object
- Rubber::Cloud::Aws::TableStore
- Defined in:
- lib/rubber/cloud/aws/table_store.rb
Defined Under Namespace
Classes: TableResponse
Constant Summary collapse
- RETRYABLE_EXCEPTIONS =
[Excon::Errors::Error]
Instance Attribute Summary collapse
-
#metadata ⇒ Object
readonly
Returns the value of attribute metadata.
Instance Method Summary collapse
- #decode(v) ⇒ Object
- #decode_attributes(data) ⇒ Object
- #delete(key, attributes = nil) ⇒ Object
- #encode(v) ⇒ Object
-
#ensure_table_key ⇒ Object
create the table if needed.
- #find(key = nil, attributes = nil, opts = {}) ⇒ Object
- #get(key, attributes = []) ⇒ Object
-
#initialize(provider, table_key) ⇒ TableStore
constructor
A new instance of TableStore.
- #logger ⇒ Object
- #put(key, attributes) ⇒ Object
Constructor Details
#initialize(provider, table_key) ⇒ TableStore
Returns a new instance of TableStore.
17 18 19 20 21 22 23 24 |
# File 'lib/rubber/cloud/aws/table_store.rb', line 17 def initialize(provider, table_key) raise "table_key required" unless table_key && table_key.size > 0 @table_provider = provider @table_key = table_key ensure_table_key end |
Instance Attribute Details
#metadata ⇒ Object (readonly)
Returns the value of attribute metadata.
11 12 13 |
# File 'lib/rubber/cloud/aws/table_store.rb', line 11 def @metadata end |
Instance Method Details
#decode(v) ⇒ Object
42 43 44 |
# File 'lib/rubber/cloud/aws/table_store.rb', line 42 def decode(v) JSON.parse(v).first end |
#decode_attributes(data) ⇒ Object
46 47 48 |
# File 'lib/rubber/cloud/aws/table_store.rb', line 46 def decode_attributes(data) Hash[data.collect {|k, v| [k, decode(v.first)] }] end |
#delete(key, attributes = nil) ⇒ Object
62 63 64 65 |
# File 'lib/rubber/cloud/aws/table_store.rb', line 62 def delete(key, attributes=nil) @table_provider.delete_attributes(@table_key, key, attributes) return true end |
#encode(v) ⇒ Object
38 39 40 |
# File 'lib/rubber/cloud/aws/table_store.rb', line 38 def encode(v) [v].to_json end |
#ensure_table_key ⇒ Object
create the table if needed
27 28 29 30 31 32 33 34 35 36 |
# File 'lib/rubber/cloud/aws/table_store.rb', line 27 def ensure_table_key Rubber::Util.retry_on_failure(*RETRYABLE_EXCEPTIONS) do begin @metadata = @table_provider.(@table_key) rescue Excon::Errors::BadRequest @table_provider.create_domain(@table_key) @metadata = @table_provider.(@table_key) end end end |
#find(key = nil, attributes = nil, opts = {}) ⇒ Object
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 |
# File 'lib/rubber/cloud/aws/table_store.rb', line 67 def find(key=nil, attributes=nil, opts={}) query = "select" query << " " + (attributes ? attributes.join(", ") : '*') query << " from `#{@table_key}`" query << " where ItemName = '#{key}'" if key query << " limit " + (opts[:limit] ? opts[:limit].to_s : "200") query_opts = {} query_opts["NextToken"] = opts[:offset].to_s if opts[:offset] response = @table_provider.select(query, query_opts) data = response.body['Items'] result = TableResponse.new data.each do |name, attribs| result[name] = decode_attributes(attribs) end result.next_offset = response.body['NextToken'] return result end |
#get(key, attributes = []) ⇒ Object
56 57 58 59 60 |
# File 'lib/rubber/cloud/aws/table_store.rb', line 56 def get(key, attributes=[]) response = @table_provider.get_attributes(@table_key, key, 'AttributeName' => attributes) data = response.body['Attributes'] return decode_attributes(data) end |
#logger ⇒ Object
13 14 15 |
# File 'lib/rubber/cloud/aws/table_store.rb', line 13 def logger Rubber.logger end |
#put(key, attributes) ⇒ Object
50 51 52 53 54 |
# File 'lib/rubber/cloud/aws/table_store.rb', line 50 def put(key, attributes) data = Hash[attributes.collect {|k, v| [k, encode(v)] }] @table_provider.put_attributes(@table_key, key, data, :replace => attributes.keys) return true end |