← Index
NYTProf Performance Profile   « block view • line view • sub view »
For 05.Domain_and_Item.t
  Run on Tue May 4 17:21:41 2010
Reported on Tue May 4 17:23:03 2010

File /data/SimpleDB-Class/author.t/../lib/SimpleDB/Class.pm
Statements Executed 268
Statement Execution Time 2.04ms
Subroutines — ordered by exclusive time
Calls P F Exclusive
Time
Inclusive
Time
Subroutine
1111.56ms211msSimpleDB::Class::::BEGIN@141SimpleDB::Class::BEGIN@141
1111.44ms43.0msSimpleDB::Class::::BEGIN@139SimpleDB::Class::BEGIN@139
111962µs42.3msSimpleDB::Class::::BEGIN@140SimpleDB::Class::BEGIN@140
111815µs11.8msSimpleDB::Class::::BEGIN@142SimpleDB::Class::BEGIN@142
6164494µs685µsSimpleDB::Class::::add_domain_prefixSimpleDB::Class::add_domain_prefix
111347µs228msSimpleDB::Class::::BEGIN@138SimpleDB::Class::BEGIN@138
752133µs745µsSimpleDB::Class::::domainSimpleDB::Class::domain
22153µs145msSimpleDB::Class::::list_domainsSimpleDB::Class::list_domains
11137µs2.02msSimpleDB::Class::::BEGIN@137SimpleDB::Class::BEGIN@137
11135µs8.42msSimpleDB::Class::::__ANON__[../lib/SimpleDB/Class.pm:328]SimpleDB::Class::__ANON__[../lib/SimpleDB/Class.pm:328]
11116µs40µsSimpleDB::Class::::__ANON__[../lib/SimpleDB/Class.pm:219]SimpleDB::Class::__ANON__[../lib/SimpleDB/Class.pm:219]
11114µs134µsSimpleDB::Class::::BEGIN@450SimpleDB::Class::BEGIN@450
11114µs56.9msSimpleDB::Class::::load_namespacesSimpleDB::Class::load_namespaces
1114µs4µsSimpleDB::Class::::__ANON__[../lib/SimpleDB/Class.pm:341]SimpleDB::Class::__ANON__[../lib/SimpleDB/Class.pm:341]
Call graph for these subroutines as a Graphviz dot language file.
Line State
ments
Time
on line
Calls Time
in subs
Code
1package SimpleDB::Class;
2
3=head1 NAME
4
5SimpleDB::Class - An Object Relational Mapper (ORM) for the Amazon SimpleDB service.
6
7=head1 SYNOPSIS
8
9 package Library;
10
11 use Moose;
12 extends 'SimpleDB::Class';
13
14 __PACKAGE__->load_namespaces();
15
16 1;
17
18 package Library::Book;
19
20 use Moose;
21 extends 'SimpleDB::Class::Item';
22
23 __PACKAGE__->set_domain_name('book');
24 __PACKAGE__->add_attributes(
25 title => { isa => 'Str', default => 'Untitled' },
26 publish_date => { isa => 'Date' },
27 edition => { isa => 'Int', default => 1 },
28 isbn => { isa => 'Str' },
29 publisherId => { isa => 'Str' },
30 author => { isa => 'Str' },
31 );
32 __PACKAGE__->belongs_to('publisher', 'Library::Publisher', 'publisherId');
33
34 1;
35
36 package Library::Publisher;
37
38 use Moose;
39 extends 'SimpleDB::Class::Item';
40
41 __PACKAGE__->set_domain_name('publisher');
42 __PACKAGE__->add_attributes({
43 name => { isa => 'Str' },
44 });
45 __PACKAGE__->has_many('books', 'Library::Book', 'publisherId');
46
47 1;
48
49 use 5.010;
50 use Library;
51 use DateTime;
52
53 my $library = Library->new(access_key => 'xxx', secret_key => 'yyy', cache_servers=>\@servers );
54
55 my $specific_book = $library->domain('book')->find('id goes here');
56
57 my $books = $library->domain('publisher')->find($id)->books;
58 my $books = $library->domain('book')->search( where => {publish_date => ['between', DateTime->new(year=>2001), DateTime->new(year=>2003)]} );
59 while (my $book = $books->next) {
60 say $book->title;
61 }
62
63=head1 DESCRIPTION
64
65SimpleDB::Class gives you a way to persist your objects in Amazon's SimpleDB service search them easily. It hides the mess of web services, pseudo 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.
66
67On top of being a simple to use ORM that functions in a manner similar to L<DBIx::Class>, SimpleDB::Class has some other niceties that make dealing with SimpleDB easier:
68
69=over
70
71=item *
72
73It 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.
74
75=item *
76
77It automatically formats dates and integers for sortability in SimpleDB.
78
79=item *
80
81It automatically casts date fields as L<DateTime> objects.
82
83=item *
84
85It automatically serializes hashes into JSON so they can be stored in SimpleDB domain attributes, and deserializes on retrieval.
86
87=item *
88
89It gives you an easy way to handle pagination of data. See L<SimpleDB::Class::ResultSet/"paginate">.
90
91=item *
92
93It uses L<Moose> for everything, which makes it easy to use Moose's introspection features or method insertion features.
94
95=item *
96
97It automatically generates UUID based ItemNames (unique IDs) if you don't want to supply an ID yourself.
98
99=item *
100
101It has built in domain prefixing to account for the fact that you can't create multiple SimpleDB instances under the same account.
102
103=item *
104
105It ignores attributes in your items that aren't in your L<SimpleDB::Class::Item> subclasses.
106
107=item *
108
109L<SimpleDB::Class::ResultSet>s automatically fetch additional items from SimpleDB if a next token is provided, so you don't have to care that SimpleDB sends you back data in small packets.
110
111=item *
112
113It allows for multiple similar object types to be stored in a single domain and then cast into different classes at retrieval time. See L<SimpleDB::Class::Item/"recast_using"> for details.
114
115=item *
116
117It allows for mass updates and deletes on L<SimpleDB::Class::ResultSet>s, which is a nice level of automation to keep your code small.
118
119=back
120
121=head2 Eventual Consistency
122
123SimpleDB 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. L<SimpleDB::Class> gets around this problem for the post part because it caches all L<SimpleDB::Class::Item>s 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 L<SimpleDB::Class::Domain> and L<SimpleDB::Class::ResultSet> 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 L<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.
124
125At the end of February 2010 Amazon added a C<ConsistentRead> option to SimpleDB, which means you don't have to care about eventual consistency if you wish to sacrifice some performance. We have exposed this as an option for you to turn on in the methods like C<search> in L<SimpleDB::Class::Domain> where you have to be concerned about eventual consistency.
126
127Does 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.
128
129For more information about eventual consistency visit L<http://en.wikipedia.org/wiki/Eventual_consistency> or the eventual consistency section of the Amazon SimpleDB Developer's Guide at L<http://docs.amazonwebservices.com/AmazonSimpleDB/2009-04-15/DeveloperGuide/EventualConsistencySummary.html>.
130
131=head1 METHODS
132
133The following methods are available from this class.
134
135=cut
136
137328µs24.00ms
# spent 2.02ms (37µs+1.98) within SimpleDB::Class::BEGIN@137 which was called # once (37µs+1.98ms) by Class::MOP::__ANON__[/usr/local/lib/perl5/site_perl/5.10.1/darwin-2level/Class/MOP.pm:103] at line 137
use Moose;
# spent 2.02ms making 1 call to SimpleDB::Class::BEGIN@137 # spent 1.98ms making 1 call to Moose::Exporter::__ANON__[Moose/Exporter.pm:389]
1383146µs2253ms
# spent 228ms (347µs+228) within SimpleDB::Class::BEGIN@138 which was called # once (347µs+228ms) by Class::MOP::__ANON__[/usr/local/lib/perl5/site_perl/5.10.1/darwin-2level/Class/MOP.pm:103] at line 138
use MooseX::ClassAttribute;
# spent 228ms making 1 call to SimpleDB::Class::BEGIN@138 # spent 24.7ms making 1 call to Moose::Exporter::__ANON__[Moose/Exporter.pm:389]
1393207µs143.0ms
# spent 43.0ms (1.44+41.6) within SimpleDB::Class::BEGIN@139 which was called # once (1.44ms+41.6ms) by Class::MOP::__ANON__[/usr/local/lib/perl5/site_perl/5.10.1/darwin-2level/Class/MOP.pm:103] at line 139
use SimpleDB::Class::Cache;
# spent 43.0ms making 1 call to SimpleDB::Class::BEGIN@139
1403197µs142.3ms
# spent 42.3ms (962µs+41.4) within SimpleDB::Class::BEGIN@140 which was called # once (962µs+41.4ms) by Class::MOP::__ANON__[/usr/local/lib/perl5/site_perl/5.10.1/darwin-2level/Class/MOP.pm:103] at line 140
use SimpleDB::Client;
# spent 42.3ms making 1 call to SimpleDB::Class::BEGIN@140
1413143µs1211ms
# spent 211ms (1.56+209) within SimpleDB::Class::BEGIN@141 which was called # once (1.56ms+209ms) by Class::MOP::__ANON__[/usr/local/lib/perl5/site_perl/5.10.1/darwin-2level/Class/MOP.pm:103] at line 141
use SimpleDB::Class::Domain;
# spent 211ms making 1 call to SimpleDB::Class::BEGIN@141
1423525µs211.9ms
# spent 11.8ms (815µs+11.0) within SimpleDB::Class::BEGIN@142 which was called # once (815µs+11.0ms) by Class::MOP::__ANON__[/usr/local/lib/perl5/site_perl/5.10.1/darwin-2level/Class/MOP.pm:103] at line 142
use Module::Find;
# spent 11.8ms making 1 call to SimpleDB::Class::BEGIN@142 # spent 48µs making 1 call to Exporter::import
143
144#--------------------------------------------------------
145
146=head2 new ( params )
147
148=head3 params
149
150A hash containing the parameters to pass in to this method.
151
152=head4 access_key
153
154The access key given to you from Amazon when you sign up for the SimpleDB service at this URL: L<http://aws.amazon.com/simpledb/>
155
156=head4 secret_key
157
158The secret access key given to you from Amazon.
159
160=head4 cache_servers
161
162An array reference of cache servers. See L<SimpleDB::Class::Cache> for details.
163
164=head4 simpledb_uri
165
166An optional L<URI> object to connect to an alternate SimpleDB server. See also L<SimpleDB::Client/"simpledb_uri">.
167
168=head4 domain_prefix
169
170An optional string that is prepended to all domain names wherever they are used in the system.
171
172=cut
173
174#--------------------------------------------------------
175
176=head2 load_namespaces ( [ namespace ] )
177
178Class 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.
179
180=head3 namespace
181
182Specify a specific namespace like Library::SimpleDB if you don't want everything in the Library namespace to be loaded.
183
184=cut
185
186
# spent 56.9ms (14µs+56.9) within SimpleDB::Class::load_namespaces which was called # once (14µs+56.9ms) by main::BEGIN@13 at line 6 of lib/Foo.pm
sub load_namespaces {
18712µs my ($class, $namespace) = @_;
18811µs $namespace ||= $class; # if no namespace is set
18919µs156.9ms useall $namespace;
# spent 56.9ms making 1 call to Module::Find::useall
190}
191
192#--------------------------------------------------------
193
194=head2 cache_servers ( )
195
196Returns the cache server array reference passed into the constructor.
197
198=cut
199
20013µs11.52mshas cache_servers => (
# spent 1.52ms making 1 call to Moose::has
201 is => 'ro',
202 required => 1,
203);
204
205#--------------------------------------------------------
206
207=head2 cache ( )
208
209Returns a reference to the L<SimpleDB::Class::Cache> instance.
210
211=cut
212
213has cache => (
214 is => 'ro',
215 lazy => 1,
216
# spent 40µs (16+23) within SimpleDB::Class::__ANON__[../lib/SimpleDB/Class.pm:219] which was called # once (16µs+23µs) by Class::MOP::Mixin::AttributeCore::default at line 53 of Class/MOP/Mixin/AttributeCore.pm
default => sub {
2171900ns my $self = shift;
218114µs224µs return SimpleDB::Class::Cache->new(servers=>$self->cache_servers);
# spent 21µs making 1 call to SimpleDB::Class::Cache::new # spent 3µs making 1 call to SimpleDB::Class::cache_servers
219 },
22014µs11.35ms);
# spent 1.35ms making 1 call to Moose::has
221
222#--------------------------------------------------------
223
224=head2 access_key ( )
225
226Returns the access key passed to the constructor.
227
228=cut
229
23012µs1945µshas 'access_key' => (
# spent 945µs making 1 call to Moose::has
231 is => 'ro',
232 required => 1,
233 documentation => 'The AWS SimpleDB access key id provided by Amazon.',
234);
235
236#--------------------------------------------------------
237
238=head2 secret_key ( )
239
240Returns the secret key passed to the constructor.
241
242=cut
243
24411µs1956µshas 'secret_key' => (
# spent 956µs making 1 call to Moose::has
245 is => 'ro',
246 required => 1,
247 documentation => 'The AWS SimpleDB secret access key id provided by Amazon.',
248);
249
250#--------------------------------------------------------
251
252=head2 simpledb_uri ( )
253
254Returns the L<URI> object passed into the constructor, if any. See also L<SimpleDB::Client/"simpledb_uri">.
255
256=head2 has_simpledb_uri ( )
257
258Returns a boolean indicating whether the user has overridden the URI.
259
260=cut
261
26213µs11.23mshas simpledb_uri => (
# spent 1.23ms making 1 call to Moose::has
263 is => 'ro',
264 predicate => 'has_simpledb_uri',
265 default => undef,
266);
267
268
269#--------------------------------------------------------
270
271=head2 domain_prefix ( )
272
273Returns the value passed into the constructor.
274
275=head2 has_domain_prefix ( )
276
277Returns a boolean indicating whether the user has specified a domain_prefix.
278
279=cut
280
28112µs11.24mshas domain_prefix => (
# spent 1.24ms making 1 call to Moose::has
282 is => 'ro',
283 predicate => 'has_domain_prefix',
284 default => undef,
285);
286
287#--------------------------------------------------------
288
289=head2 add_domain_prefix ( domain )
290
291If the domain_prefix is set, this method will apply it do a passed in domain name. If it's not, it will simply return the domain anme as is.
292
293B<NOTE:> This is used mostly internally to SimpleDB::Class, so unless you're extending SimpleDB::Class itself, rather than just using it, you don't have to use this method.
294
295=head3 domain
296
297The domain to apply the prefix to.
298
299=cut
300
301
# spent 685µs (494+192) within SimpleDB::Class::add_domain_prefix which was called 61 times, avg 11µs/call: # 19 times (146µs+59µs) by SimpleDB::Class::SQL::to_sql at line 393 of ../lib/SimpleDB/Class/SQL.pm, avg 11µs/call # 18 times (150µs+59µs) by SimpleDB::Class::ResultSet::next at line 460 of ../lib/SimpleDB/Class/ResultSet.pm, avg 12µs/call # 12 times (95µs+36µs) by SimpleDB::Class::Item::put at line 477 of ../lib/SimpleDB/Class/Item.pm, avg 11µs/call # 6 times (50µs+20µs) by SimpleDB::Class::Domain::find at line 152 of ../lib/SimpleDB/Class/Domain.pm, avg 12µs/call # 3 times (27µs+11µs) by SimpleDB::Class::Domain::delete at line 118 of ../lib/SimpleDB/Class/Domain.pm, avg 13µs/call # 3 times (26µs+8µs) by SimpleDB::Class::Domain::create at line 102 of ../lib/SimpleDB/Class/Domain.pm, avg 11µs/call
sub add_domain_prefix {
3026169µs my ($self, $domain) = @_;
30361186µs61192µs if ($self->has_domain_prefix) {
# spent 192µs making 61 calls to SimpleDB::Class::has_domain_prefix, avg 3µs/call
304 return $self->domain_prefix . $domain;
305 }
30661163µs return $domain;
307}
308
309
310#--------------------------------------------------------
311
312=head2 http ( )
313
314Returns the L<SimpleDB::Client> instance used to connect to the SimpleDB service.
315
316=cut
317
318has http => (
319 is => 'ro',
320 lazy => 1,
321
# spent 8.42ms (35µs+8.39) within SimpleDB::Class::__ANON__[../lib/SimpleDB/Class.pm:328] which was called # once (35µs+8.39ms) by Class::MOP::Mixin::AttributeCore::default at line 53 of Class/MOP/Mixin/AttributeCore.pm
default => sub {
3221800ns my $self = shift;
323117µs214µs my %params = (access_key=>$self->access_key, secret_key=>$self->secret_key);
# spent 10µs making 1 call to SimpleDB::Class::access_key # spent 3µs making 1 call to SimpleDB::Class::secret_key
32414µs13µs if ($self->has_simpledb_uri) {
# spent 3µs making 1 call to SimpleDB::Class::has_simpledb_uri
325 $params{simpledb_uri} = $self->simpledb_uri;
326 }
327112µs18.37ms return SimpleDB::Client->new(%params);
# spent 8.37ms making 1 call to SimpleDB::Client::new
328 },
32914µs1976µs);
# spent 976µs making 1 call to Moose::has
330
331#--------------------------------------------------------
332
333=head2 domain_names ( )
334
335Class method. Returns a hashref of the domain names and class names registered from subclassing L<SimpleDB::Class::Domain> and calling set_name.
336
337=cut
338
33915µsclass_has 'domain_names' => (
340 is => 'rw',
341
# spent 4µs within SimpleDB::Class::__ANON__[../lib/SimpleDB/Class.pm:341] which was called # once (4µs+0s) by Class::MOP::Class:::around at line 78 of MooseX/ClassAttribute/Role/Meta/Attribute.pm
default => sub{{}},
34216µs111.3ms);
# spent 11.3ms making 1 call to MooseX::ClassAttribute::class_has
343
344#--------------------------------------------------------
345
346=head2 domain ( moniker )
347
348Returns an instanciated L<SimpleDB::Class::Domain> based upon its L<SimpleDB::Class::Item> classname or its domain name.
349
350=head3 moniker
351
352Can either be the L<SimpleDB::Class::Item> subclass name, or the domain name.
353
354=cut
355
356
# spent 745µs (133+611) within SimpleDB::Class::domain which was called 7 times, avg 106µs/call: # 3 times (41µs+205µs) by Foo::Domain::children or Foo::Parent::domains at line 207 of ../lib/SimpleDB/Class/Item.pm, avg 82µs/call # once (37µs+137µs) by main::RUNTIME at line 20 of 05.Domain_and_Item.t # once (21µs+111µs) by main::RUNTIME at line 76 of 05.Domain_and_Item.t # once (18µs+80µs) by main::RUNTIME at line 24 of 05.Domain_and_Item.t # once (16µs+78µs) by SimpleDB::Class::Item::__ANON__[../lib/SimpleDB/Class/Item.pm:280] at line 279 of ../lib/SimpleDB/Class/Item.pm
sub domain {
357710µs my ($self, $moniker) = @_;
358737µs734µs my $class = $self->domain_names->{$moniker};
# spent 34µs making 7 calls to SimpleDB::Class::domain_names, avg 5µs/call
35973µs $class ||= $moniker;
360736µs7577µs my $d = SimpleDB::Class::Domain->new(simpledb=>$self, item_class=>$class);
# spent 577µs making 7 calls to SimpleDB::Class::Domain::new, avg 82µs/call
361724µs return $d;
362}
363
364#--------------------------------------------------------
365
366=head2 list_domains ( )
367
368Retrieves the list of domain names from your SimpleDB account and returns them as an array reference.
369
370=cut
371
372
# spent 145ms (53µs+144) within SimpleDB::Class::list_domains which was called 2 times, avg 72.3ms/call: # once (20µs+75.6ms) by main::RUNTIME at line 146 of 05.Domain_and_Item.t # once (32µs+68.9ms) by main::RUNTIME at line 30 of 05.Domain_and_Item.t
sub list_domains {
37323µs my ($self) = @_;
374220µs4144ms my $result = $self->http->send_request('ListDomains');
# spent 144ms making 2 calls to SimpleDB::Client::send_request, avg 72.2ms/call # spent 15µs making 2 calls to SimpleDB::Class::http, avg 8µs/call
37524µs my $domains = $result->{ListDomainsResult}{DomainName};
37622µs unless (ref $domains eq 'ARRAY') {
377 $domains = [$domains];
378 }
379214µs return $domains;
380}
381
382=head1 PREREQS
383
384This package requires the following modules:
385
386L<JSON>
387L<Sub::Name>
388L<DateTime>
389L<DateTime::Format::Strptime>
390L<Moose>
391L<MooseX::Types>
392L<MooseX::ClassAttribute>
393L<Module::Find>
394L<UUID::Tiny>
395L<Exception::Class>
396L<Memcached::libmemcached>
397L<Clone>
398L<SimpleDB::Client>
399
400=head1 TODO
401
402Still left to figure out:
403
404=over
405
406=item *
407
408Creating 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).
409
410=item *
411
412More exception handling.
413
414=item *
415
416More tests.
417
418=item *
419
420All the other stuff I forgot about or didn't know when I designed this thing.
421
422=back
423
424=head1 SUPPORT
425
426=over
427
428=item Repository
429
430L<http://github.com/plainblack/SimpleDB-Class>
431
432=item Bug Reports
433
434L<http://rt.cpan.org/Public/Dist/Display.html?Name=SimpleDB-Class>
435
436=back
437
438=head1 AUTHOR
439
440JT Smith <jt_at_plainblack_com>
441
442I have to give credit where credit is due: SimpleDB::Class is heavily inspired by L<DBIx::Class> by Matt Trout (and others).
443
444=head1 LEGAL
445
446SimpleDB::Class is Copyright 2009-2010 Plain Black Corporation (L<http://www.plainblack.com/>) and is licensed under the same terms as Perl itself.
447
448=cut
449
450346µs2254µs
# spent 134µs (14+120) within SimpleDB::Class::BEGIN@450 which was called # once (14µs+120µs) by Class::MOP::__ANON__[/usr/local/lib/perl5/site_perl/5.10.1/darwin-2level/Class/MOP.pm:103] at line 450
no Moose;
# spent 134µs making 1 call to SimpleDB::Class::BEGIN@450 # spent 120µs making 1 call to Moose::Exporter::__ANON__[Moose/Exporter.pm:478]
451180µs216.5ms__PACKAGE__->meta->make_immutable;
# spent 16.5ms making 1 call to Class::MOP::Class::make_immutable # spent 11µs making 1 call to SimpleDB::Class::meta