File Coverage

blib/lib/WebService/Amazon/DynamoDB/Server/Table.pm
Criterion Covered Total %
statement 41 49 83.6
branch 0 2 0.0
condition 2 10 20.0
subroutine 14 18 77.7
pod 7 9 77.7
total 64 88 72.7


line stmt bran cond sub pod time code
1             package WebService::Amazon::DynamoDB::Server::Table;
2             $WebService::Amazon::DynamoDB::Server::Table::VERSION = '0.001';
3 8     8   15714 use strict;
  8         10  
  8         254  
4 8     8   29 use warnings;
  8         11  
  8         155  
5              
6 8     8   601 use Future;
  8         9937  
  8         155  
7 8     8   505 use Future::Utils qw(repeat);
  8         1671  
  8         352  
8              
9 8     8   3073 use WebService::Amazon::DynamoDB::Server::Item;
  8         12  
  8         197  
10 8     8   500 use Mixin::Event::Dispatch::Bus;
  8         5020  
  8         154  
11              
12 8     8   31 use constant DYNAMODB_INDEX_OVERHEAD => 100;
  8         7  
  8         3896  
13              
14             =head2 new
15              
16             =cut
17              
18 14     14 1 22 sub new { my $class = shift; bless {@_}, $class }
  14         61  
19              
20             =head2 bus
21              
22             The event bus used by this instance.
23              
24             =cut
25              
26 0   0 0 1 0 sub bus { shift->{bus} //= Mixin::Event::Dispatch::Bus->new }
27              
28             =head2 name
29              
30             =cut
31              
32 65   50 65 1 456 sub name { shift->{TableName} // die 'invalid table - no name' }
33              
34 0     0 0 0 sub state { shift->{TableStatus} }
35              
36             =head2 item_by_id
37              
38             =cut
39              
40             sub item_by_id {
41 0     0 1 0 my ($self, @id) = @_;
42 0   0     0 my $k = $self->key_for_id(@id) // return Future->fail('bad key');
43 0 0       0 exists $self->{items}{$k} or return Future->fail('item not found');
44 0         0 Future->done($self->{items}{$k});
45             }
46              
47             =head2 key_for_id
48              
49             =cut
50              
51             sub key_for_id {
52 0     0 1 0 my ($self, @id) = @_;
53 0         0 join "\0", map Encode::encode("UTF-8", $_), @id;
54             }
55              
56             =head2 bytes_used
57              
58             =cut
59              
60             sub bytes_used {
61 2     2 1 7 my ($self) = @_;
62 2   33     12 $self->{bytes_used} //= do {
63 2         2 my $total = 0;
64             (repeat {
65             shift->bytes_used->on_done(sub {
66 3         111 $total += DYNAMODB_INDEX_OVERHEAD + shift
67             })
68 3     3   146 } foreach => [ @{$self->{items}} ],
  2         16  
69 2     2   81 otherwise => sub { Future->done($total) })
70 2         8 }
71             }
72              
73             =head2 validate_id_for_item_data
74              
75             =cut
76              
77             sub validate_id_for_item_data {
78 4     4 1 8 my ($self, $data) = @_;
79 4         4 my @id_fields = map $_->{AttributeName}, @{$self->{KeySchema}};
  4         18  
80             return Future->fail(
81             ValidationException =>
82 4         19 ) for grep !exists $data->{$_}, @id_fields;
83              
84 2         4 my ($id) = join "\0", map values %{$data->{$_}}, @id_fields;
  2         7  
85 2         5 Future->done($id);
86             }
87              
88             sub item_from_data {
89 2     2 0 6 my ($self, $data) = @_;
90 2         19 WebService::Amazon::DynamoDB::Server::Item->new(
91             attributes => $data
92             );
93             }
94              
95             1;
96              
97             __END__