The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.

NAME

SimpleDB::Class - An Object Relational Mapper (ORM) for the Amazon SimpleDB service.

VERSION

version 0.0700

SYNOPSIS

 package Library;

 use Moose;
 extends 'SimpleDB::Class';
 
 __PACKAGE__->load_namespaces();

 1;

 package Library::Book;

 use Moose;
 extends 'SimpleDB::Class::Item';

 __PACKAGE__->set_domain_name('book');
 __PACKAGE__->add_attributes(
     title          => { isa => 'Str', default => 'Untitled' },
     publish_date   => { isa => 'Date' },
     edition        => { isa => 'Int', default => 1 },
     isbn           => { isa => 'Str' },
     publisherId    => { isa => 'Str' },
     author         => { isa => 'Str' },
 );
 __PACKAGE__->belongs_to('publisher', 'Library::Publisher', 'publisherId');

 1;

 package Library::Publisher;

 use Moose;
 extends 'SimpleDB::Class::Item';

 __PACKAGE__->set_domain_name('publisher');
 __PACKAGE__->add_attributes({
     name   => { isa => 'Str' },
 });
 __PACKAGE__->has_many('books', 'Library::Book', 'publisherId');

 1;

 use 5.010;
 use Library;
 use DateTime;

 my $library = Library->new(access_key => 'xxx', secret_key => 'yyy', cache_servers=>\@servers );
  
 my $specific_book = $library->domain('book')->find('id goes here');

 my $books = $library->domain('publisher')->books;
 my $books = $library->domain('book')->search({publish_date => DateTime->new(year=>2001)});
 while (my $book = $books->next) {
    say $book->title;
 }

DESCRIPTION

SimpleDB::Class gives you a way to persist your objects in Amazon's SimpleDB service search them easily. It hides the mess of web services, sudo SQL, and XML document formats that you'd normally need to deal with to use the service, and gives you a tight clean Perl API to access it.

On top of being a simple to use ORM that functions in a manner similar to DBIx::Class, SimpleDB::Class has some other niceties that make dealing with SimpleDB easier:

  • It uses memcached to cache objects locally so that most of the time you don't have to care that SimpleDB is eventually consistent. This also speeds up many requests. See Eventual Consistency below for details.

  • It has cascading retries, which means it automatically attempts to retry failed requests (you have to plan for failure on the net).

  • It automatically formats dates and integers for sortability in SimpleDB.

  • It automatically casts date fields as DateTime objects.

  • It automatically serializes hashes into JSON so they can be stored in SimpleDB domain attributes, and deserializes on retrieval.

  • It uses Moose for everything, which makes it easy to use Moose's introspection features or method insertion features.

  • It automatically generates UUID based ItemNames (unique IDs) if you don't want to supply an ID yourself.

  • It automatically deals with the fact that you might have some attributes in your SimpleDB::Class::Items that aren't specified in your SimpleDB::Class::Domain subclasses, and creates accessors and mutators for them on the fly at retrieval time.

  • SimpleDB::Class::ResultSets automatically fetch additional items from SimpleDB if a next token is provided.

  • It allows for multiple similar object types to be stored in a single domain and then cast into different classes at retrieval time. See "recast_using" in SimpleDB::Class::Item for details.

  • It allows for mass updates and deletes on SimpleDB::Class::ResultSets.

  • It lets you search within a subset of a domain, by letting you do a secondary search on a SimpleDB::Class::ResultSet. And you can continue to narrow the results by searching over an over on each new result set.

Eventual Consistency

SimpleDB is eventually consistent, which means that if you do a write, and then read directly after the write you may not get what you just wrote. SimpleDB::Class gets around this problem for the post part because it caches all SimpleDB::Class::Items in memcached. That is to say that if an object can be read from cache, it will be. The one area where this falls short are some methods in SimpleDB::Class::Domain that perform searches on the database which look up items based upon their attributes rather than based upon id. Even in those cases, once an object is located we try to pull it from cache rather than using the data SimpleDB gave us, simply because the cache may be more current. However, a search result may return too few (inserts pending) or too many (deletes pending) results in SimpleDB::Class::ResultSet, or it may return an object which no longer fits certain criteria that you just searched for (updates pending). As long as you're aware of it, and write your programs accordingly, there shouldn't be a problem.

Does all this mean that this module makes SimpleDB as ACID compliant as a traditional RDBMS? No it does not. There are still no locks on domains (think tables), or items (think rows). So you probably shouldn't be storing sensitive financial transactions in this. We just provide an easy to use API that will allow you to more easily and a little more safely take advantage of Amazon's excellent SimpleDB service for things like storing logs, metadata, and game data.

For more information about eventual consistency visit http://en.wikipedia.org/wiki/Eventual_consistency or the eventual consistency section of the Amazon SimpleDB Developer's Guide at http://docs.amazonwebservices.com/AmazonSimpleDB/2009-04-15/DeveloperGuide/EventualConsistencySummary.html.

METHODS

The following methods are available from this class.

new ( params )

params

A hash containing the parameters to pass in to this method.

access_key

The access key given to you from Amazon when you sign up for the SimpleDB service at this URL: http://aws.amazon.com/simpledb/

secret_key

The secret access key given to you from Amazon.

cache_servers

An array reference of cache servers. See SimpleDB::Class::Cache for details.

load_namespaces ( [ namespace ] )

Class method. Loads all the modules in the current namespace, so if you subclass SimpleDB::Class with a package called Library (as in the example provided), then everything in the Library namespace would be loaded automatically. Should be called to load all the modules you subclass, so you don't have to manually use each of them.

namespace

Specify a specific namespace like Library::SimpleDB if you don't want everything in the Library namespace to be loaded.

cache_servers ( )

Returns the cache server array reference passed into the constructor.

cache ( )

Returns a reference to the SimpleDB::Class::Cache instance.

access_key ( )

Returns the access key passed to the constructor.

secret_key ( )

Returns the secret key passed to the constructor.

http ( )

Returns the SimpleDB::Class::HTTP instance used to connect to the SimpleDB service.

domain_names ( )

Class method. Returns a hashref of the domain names and class names registered from subclassing SimpleDB::Class::Domain and calling set_name.

domain ( moniker )

Returns an instanciated SimpleDB::Class::Domain based upon its SimpleDB::Class::Item classname or its domain name.

moniker

Can either be the SimpleDB::Class::Item subclass name, or the domain name.

list_domains ( )

Retrieves the list of domain names from your SimpleDB account and returns them as an array reference.

PREREQS

This package requires the following modules:

XML::Simple AnyEvent::HTTP Net::SSLeay Sub::Name DateTime DateTime::Format::Strptime Moose MooseX::ClassAttribute Digest::SHA URI Module::Find UUID::Tiny Exception::Class Memcached::libmemcached

TODO

Still left to figure out:

  • Creating multi-domain objects ( so you can put each country's data into it's own domain, but still search all country-oriented data at once).

  • More exception handling.

  • More tests.

  • All the other stuff I forgot about or didn't know when I designed this thing.

SUPPORT

Repository

http://github.com/plainblack/SimpleDB-Class

Bug Reports

http://rt.cpan.org/Public/Dist/Display.html?Name=SimpleDB-Class

SEE ALSO

There are other packages you can use to access SimpleDB. I chose not to use them because I wanted something a bit more robust that would allow me to easily map objects to SimpleDB Domain Items. If you're looking for a low level SimpleDB accessor, then you should check out these:

SimpleDB::Class::HTTP - This is our interface to AWS SimpleDB, and can work just fine as a stand alone component if you're looking for a simple way to quickly access SimpleDB.
Amazon::SimpleDB (http://developer.amazonwebservices.com/connect/entry.jspa?externalID=1136)

A complete and nicely functional low level library made by Amazon itself.

Amazon::SimpleDB

A low level SimpleDB accessor that's in its infancy and may be abandoned, but appears to be pretty functional, and of the same scope as Amazon's own module.

AUTHOR

JT Smith <jt_at_plainblack_com>

I have to give credit where credit is due: SimpleDB::Class is heavily inspired by DBIx::Class by Matt Trout (and others), and the Amazon::SimpleDB class distributed by Amazon itself (not to be confused with Amazon::SimpleDB written by Timothy Appnel).

LEGAL

SimpleDB::Class is Copyright 2009 Plain Black Corporation (http://www.plainblack.com/) and is licensed under the same terms as Perl itself.