class AWS::SimpleDB

This class is the starting point for working with Amazon SimpleDB.

To use Amazon SimpleDB you must first sign up here.

For more information about Amazon SimpleDB:

Credentials

You can setup default credentials for all AWS services via AWS.config:

AWS.config(
  :access_key_id => 'YOUR_ACCESS_KEY_ID',
  :secret_access_key => 'YOUR_SECRET_ACCESS_KEY')

Or you can set them directly on the SimpleDB interface:

sdb = AWS::SimpleDB.new(
  :access_key_id => 'YOUR_ACCESS_KEY_ID',
  :secret_access_key => 'YOUR_SECRET_ACCESS_KEY')

Understanding the SimpleDB Interface

SimpleDB stores data in a hierarchy of:

Domains > Items > Attributes

These are modeled with the following classes:

The collection classes listed above make it easy to enumerate, the objects they represent. They also make it easy to perform bulk operations on all objects in that collection.

Domains

Domains are like database tables. A domain must exist before you can write to it. To create a domain:

sdb = SimpleDB.new
domain = sdb.domains.create('mydomain')

For more information about working with domains see {DomainCollection} and {Domain}.

Items & Attributes

Items exist in SimpleDB when they have attributes. You can delete an item by removing all of its attributes. You create an item by adding an attribute to it.

The following example illustrates how you can reference an item that does not exist yet:

sdb = SimpleDB.new

# this domain is empty, it has no items
domain = sdb.domains.create('newdomain')
domain.items.collect(&:name)
#=> []

# this item doesn't exist yet, so it has no attributes
item = domain.items['newitem']
item.attributes.collect(&:name)
#=> []

# the item has no attributes
tags = item.attributes['tags']
tags.values
#=> []

To create the item in SimpleDB you just need to add an attribute.

tags.add %w(first new)

domain.items.collect(&:name)
#=> ['newitem']

item.attributes.collect(&:name)
#=> ['tags']

tags.values
#=> ['first', 'new']

For more information about working with items and attributes, see:

Lazy Execution

Requests are not made until necessary. This means you can drill down all the way to an attribute, by name, without making any requets to SimpleDB.

# makes no request to SimpleDB
sdb = SimpleDB.new
colors = sdb.domains['mydomain'].items['car'].attributes['colors']

# one request to get the values for 'colors'
puts colors.values

# one request to add blue and green
colors.add 'blue', 'green'

# one request to delete the colors attribute
colors.delete

Public Class Methods

consistent_reads(state = true) { || ... } click to toggle source

Call this method with a block. Code executed inside the block make consistent reads until the block ends.

AWS::SimpleDB.consistent_reads do
  # ...
end

Other Modes

You can also use this same function to disable consistent reads insie a block. This is useful if you have consistent reads enabled by default:

AWS::SimpleDB.consistent_reads(false) do
  # ...
end

@param [Boolean] state (true) When true, all SimpleDB read operations

will be consistent reads inside the block.  When false, all 
reads operations will not be consistent reads.  The previous state
will be restored after the block executes.

@return Returns the final block value.

# File lib/aws/simple_db.rb, line 192
def self.consistent_reads state = true, &block
  begin
    prev_state = Thread.current['_simple_db_consistent_reads_']
    Thread.current['_simple_db_consistent_reads_'] = state
    yield
  ensure
    Thread.current['_simple_db_consistent_reads_'] = prev_state
  end
end
consistent_reads_state() click to toggle source

@return [Boolean] Returns true if the ::consistent_reads block has

a true state, false otherwise.

@private

# File lib/aws/simple_db.rb, line 212
def self.consistent_reads_state
  Thread.current['_simple_db_consistent_reads_']
end
in_consistent_reads_block?() click to toggle source

@return [Boolean] Returns true if we are inside an AWS::SimpleDB

#consistent_reads method block.

@private

# File lib/aws/simple_db.rb, line 205
def self.in_consistent_reads_block?
  !Thread.current['_simple_db_consistent_reads_'].nil?
end

Public Instance Methods

do_with_condition(condition) click to toggle source
# File spec/aws/simple_db/attribute_collection_spec.rb, line 231
def do_with_condition(condition)
  attributes.put({
                   :add => { 'colors' => ['red', 'blue'] },
                   :replace => { 'wheels' => '4' }
                 }.merge(condition))
end
domains() click to toggle source

Returns a collection object that represents the domains in your account.

@return [DomainCollection] Returns a collection representing all your

domains.
# File lib/aws/simple_db.rb, line 166
def domains
  DomainCollection.new(:config => config)
end
expect_conditions(client, conditions) click to toggle source
# File spec/aws/simple_db/item_collection_spec.rb, line 34
def expect_conditions(client, conditions)
  expression = "SELECT itemName() FROM `#{domain.name}` " +
    conditions
  client.should_receive(:select).
    with(:select_expression => expression).
    and_return(empty_response)
end
expect_output_list(client, list, options = {}) click to toggle source
# File spec/aws/simple_db/item_collection_spec.rb, line 42
def expect_output_list(client, list, options = {})
  expression = "SELECT #{list} FROM `#{domain.name}`#{options[:suffix]}"
  client.should_receive(:select).
    with(hash_including(:select_expression => expression)).
    and_return(empty_response)
end
has_param?(param_name, value = nil) click to toggle source
# File spec/aws/simple_db/client_spec.rb, line 33
def has_param? param_name, value = nil
  client.params
end
should_delete(opts) click to toggle source
# File spec/aws/simple_db/attribute_collection_spec.rb, line 366
def should_delete(opts)
  client.should_receive(:delete_attributes).
    with({
           :domain_name => "domain-name",
           :item_name => "item-name"
         }.merge(opts))
end
should_expect(*conditions) click to toggle source
# File spec/aws/simple_db/attribute_collection_spec.rb, line 221
def should_expect(*conditions)
  client.should_receive(:put_attributes).with do |hash|
    attr = hash[:attributes]
    attr.length.should == 3
    attr.should include({ :value => "4", :name => "wheels", :replace => true })
    attr.should include({ :value => "red", :name => "colors", :replace => false })
    attr.should include({ :value => "blue", :name => "colors", :replace => false })
    hash[:expected].should == conditions
  end
end