line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Net::Amazon::DynamoDB::Table; |
2
|
1
|
|
|
1
|
|
2827
|
use Net::Amazon::DynamoDB::Lite; |
|
1
|
|
|
|
|
2745914
|
|
|
1
|
|
|
|
|
40
|
|
3
|
1
|
|
|
1
|
|
11
|
use Carp qw/cluck confess carp croak/; |
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
81
|
|
4
|
1
|
|
|
1
|
|
774
|
use DDP; |
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
5
|
|
|
|
|
|
|
use JSON::XS; |
6
|
|
|
|
|
|
|
use Moo; |
7
|
|
|
|
|
|
|
use Try::Tiny; |
8
|
|
|
|
|
|
|
use Test::Deep::NoTest; |
9
|
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
our $VERSION="0.01"; |
11
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
has table => (is => 'rw', required => 1); |
13
|
|
|
|
|
|
|
has hash_key => (is => 'rw', required => 1); |
14
|
|
|
|
|
|
|
has range_key => (is => 'rw', required => 0); |
15
|
|
|
|
|
|
|
has dynamodb => (is => 'lazy'); |
16
|
|
|
|
|
|
|
has region => (is => 'rw', required => 1); |
17
|
|
|
|
|
|
|
has access_key => (is => 'rw', lazy => 1, builder => 1); |
18
|
|
|
|
|
|
|
has secret_key => (is => 'rw', lazy => 1, builder => 1); |
19
|
|
|
|
|
|
|
|
20
|
|
|
|
|
|
|
sub _build_access_key { $ENV{AWS_ACCESS_KEY} } |
21
|
|
|
|
|
|
|
sub _build_secret_key { $ENV{AWS_SECRET_KEY} } |
22
|
|
|
|
|
|
|
|
23
|
|
|
|
|
|
|
sub _build_dynamodb { |
24
|
|
|
|
|
|
|
my $self = shift; |
25
|
|
|
|
|
|
|
Net::Amazon::DynamoDB::Lite->new( |
26
|
|
|
|
|
|
|
region => $self->region, |
27
|
|
|
|
|
|
|
access_key => $self->access_key, |
28
|
|
|
|
|
|
|
secret_key => $self->secret_key, |
29
|
|
|
|
|
|
|
); |
30
|
|
|
|
|
|
|
} |
31
|
|
|
|
|
|
|
|
32
|
|
|
|
|
|
|
sub get { |
33
|
|
|
|
|
|
|
my ($self, %args) = @_; |
34
|
|
|
|
|
|
|
my $hash_key = $self->hash_key; |
35
|
|
|
|
|
|
|
my $range_key = $self->range_key; |
36
|
|
|
|
|
|
|
my $primary_key = {}; |
37
|
|
|
|
|
|
|
|
38
|
|
|
|
|
|
|
die "Key or $hash_key param required" unless $args{Key} || $args{$hash_key}; |
39
|
|
|
|
|
|
|
|
40
|
|
|
|
|
|
|
if ($args{$hash_key}) { |
41
|
|
|
|
|
|
|
my $value = delete $args{$hash_key}; |
42
|
|
|
|
|
|
|
$primary_key->{$hash_key} = $self->inflate_attribute_value($value); |
43
|
|
|
|
|
|
|
} |
44
|
|
|
|
|
|
|
if ($range_key && $args{$range_key} ) { |
45
|
|
|
|
|
|
|
my $value = delete $args{$range_key}; |
46
|
|
|
|
|
|
|
$primary_key->{$range_key} = $self->inflate_attribute_value($value); |
47
|
|
|
|
|
|
|
} |
48
|
|
|
|
|
|
|
|
49
|
|
|
|
|
|
|
$args{Key} ||= $primary_key; |
50
|
|
|
|
|
|
|
$args{TableName} ||= $self->table; |
51
|
|
|
|
|
|
|
|
52
|
|
|
|
|
|
|
return $self->dynamodb->get_item(\%args); |
53
|
|
|
|
|
|
|
} |
54
|
|
|
|
|
|
|
|
55
|
|
|
|
|
|
|
sub put { |
56
|
|
|
|
|
|
|
my ($self, %args) = @_; |
57
|
|
|
|
|
|
|
|
58
|
|
|
|
|
|
|
die "Item param required" unless $args{Item}; |
59
|
|
|
|
|
|
|
|
60
|
|
|
|
|
|
|
$args{TableName} ||= $self->table; |
61
|
|
|
|
|
|
|
$args{Item} = $self->inflate_item($args{Item}); |
62
|
|
|
|
|
|
|
|
63
|
|
|
|
|
|
|
return $self->dynamodb->put_item(\%args); |
64
|
|
|
|
|
|
|
} |
65
|
|
|
|
|
|
|
|
66
|
|
|
|
|
|
|
sub delete { |
67
|
|
|
|
|
|
|
my ($self, %args) = @_; |
68
|
|
|
|
|
|
|
my $hash_key = $self->hash_key; |
69
|
|
|
|
|
|
|
my $range_key = $self->range_key; |
70
|
|
|
|
|
|
|
my $primary_key = {}; |
71
|
|
|
|
|
|
|
|
72
|
|
|
|
|
|
|
die "Key or $hash_key param required" unless $args{Key} || $args{$hash_key}; |
73
|
|
|
|
|
|
|
|
74
|
|
|
|
|
|
|
if ($args{$hash_key}) { |
75
|
|
|
|
|
|
|
my $value = delete $args{$hash_key}; |
76
|
|
|
|
|
|
|
$primary_key->{$hash_key} = $self->inflate_attribute_value($value); |
77
|
|
|
|
|
|
|
} |
78
|
|
|
|
|
|
|
if ($range_key && $args{$range_key} ) { |
79
|
|
|
|
|
|
|
my $value = delete $args{$range_key}; |
80
|
|
|
|
|
|
|
$primary_key->{$range_key} = $self->inflate_attribute_value($value); |
81
|
|
|
|
|
|
|
} |
82
|
|
|
|
|
|
|
|
83
|
|
|
|
|
|
|
$args{Key} ||= $primary_key; |
84
|
|
|
|
|
|
|
$args{TableName} ||= $self->table; |
85
|
|
|
|
|
|
|
|
86
|
|
|
|
|
|
|
return $self->dynamodb->delete_item(\%args); |
87
|
|
|
|
|
|
|
} |
88
|
|
|
|
|
|
|
|
89
|
|
|
|
|
|
|
sub inflate_item { |
90
|
|
|
|
|
|
|
my ($self, $item) = @_; |
91
|
|
|
|
|
|
|
my %new; |
92
|
|
|
|
|
|
|
|
93
|
|
|
|
|
|
|
for my $attr (keys %$item) { |
94
|
|
|
|
|
|
|
my $val = $item->{$attr}; |
95
|
|
|
|
|
|
|
$new{$attr} = $self->inflate_attribute_value($val); |
96
|
|
|
|
|
|
|
} |
97
|
|
|
|
|
|
|
|
98
|
|
|
|
|
|
|
return \%new; |
99
|
|
|
|
|
|
|
} |
100
|
|
|
|
|
|
|
|
101
|
|
|
|
|
|
|
sub inflate_hash_key { |
102
|
|
|
|
|
|
|
my ($self, $thing) = @_; |
103
|
|
|
|
|
|
|
my %new; |
104
|
|
|
|
|
|
|
|
105
|
|
|
|
|
|
|
if (ref($thing) eq 'HASH') { |
106
|
|
|
|
|
|
|
my %new; |
107
|
|
|
|
|
|
|
for my $key (keys %$thing) { |
108
|
|
|
|
|
|
|
$new{$key} = $self->inflate_attribute_value($thing->{$key}); |
109
|
|
|
|
|
|
|
} |
110
|
|
|
|
|
|
|
return \%new; |
111
|
|
|
|
|
|
|
} |
112
|
|
|
|
|
|
|
else { |
113
|
|
|
|
|
|
|
return $self->inflate_attribute_value($thing); |
114
|
|
|
|
|
|
|
} |
115
|
|
|
|
|
|
|
} |
116
|
|
|
|
|
|
|
|
117
|
|
|
|
|
|
|
sub inflate_attribute_value { |
118
|
|
|
|
|
|
|
my ($self, $thing) = @_; |
119
|
|
|
|
|
|
|
|
120
|
|
|
|
|
|
|
if (ref($thing) eq 'HASH') { |
121
|
|
|
|
|
|
|
my %vals; |
122
|
|
|
|
|
|
|
for my $key (keys %$thing) { |
123
|
|
|
|
|
|
|
$vals{$key} = $self->inflate_attribute_value($thing->{$key}); |
124
|
|
|
|
|
|
|
} |
125
|
|
|
|
|
|
|
return { M => \%vals }; |
126
|
|
|
|
|
|
|
} |
127
|
|
|
|
|
|
|
elsif (ref($thing) eq 'ARRAY') { |
128
|
|
|
|
|
|
|
my @vals; |
129
|
|
|
|
|
|
|
push @vals, $self->inflate_attribute_value($_) for @$thing; |
130
|
|
|
|
|
|
|
return { L => [@vals] }; |
131
|
|
|
|
|
|
|
} |
132
|
|
|
|
|
|
|
elsif (ref($thing) eq 'SCALAR') { |
133
|
|
|
|
|
|
|
return { B => MIME::Base64::encode_base64($$thing, '') }; |
134
|
|
|
|
|
|
|
} |
135
|
|
|
|
|
|
|
elsif ($self->isa_number($thing)) { |
136
|
|
|
|
|
|
|
return { N => "$thing" }; |
137
|
|
|
|
|
|
|
} |
138
|
|
|
|
|
|
|
else { |
139
|
|
|
|
|
|
|
return { S => $thing }; |
140
|
|
|
|
|
|
|
} |
141
|
|
|
|
|
|
|
} |
142
|
|
|
|
|
|
|
|
143
|
|
|
|
|
|
|
sub isa_number { |
144
|
|
|
|
|
|
|
my ($self, $thing) = @_; |
145
|
|
|
|
|
|
|
return 1 if B::svref_2object(\$thing)->FLAGS & (B::SVp_IOK | B::SVp_NOK) |
146
|
|
|
|
|
|
|
&& 0 + $thing eq $thing |
147
|
|
|
|
|
|
|
&& $thing * 0 == 0; |
148
|
|
|
|
|
|
|
return 0; |
149
|
|
|
|
|
|
|
} |
150
|
|
|
|
|
|
|
|
151
|
|
|
|
|
|
|
sub scan { |
152
|
|
|
|
|
|
|
my ($self, %args) = @_; |
153
|
|
|
|
|
|
|
$args{TableName} ||= $self->table; |
154
|
|
|
|
|
|
|
return $self->dynamodb->scan(\%args); |
155
|
|
|
|
|
|
|
} |
156
|
|
|
|
|
|
|
|
157
|
|
|
|
|
|
|
sub scan_as_hashref { |
158
|
|
|
|
|
|
|
my ($self, %args) = @_; |
159
|
|
|
|
|
|
|
$args{TableName} ||= $self->table; |
160
|
|
|
|
|
|
|
my $items_hashref = {}; |
161
|
|
|
|
|
|
|
my $items_arrayref = $self->scan(%args); |
162
|
|
|
|
|
|
|
my $hash_key_name = $self->hash_key; |
163
|
|
|
|
|
|
|
|
164
|
|
|
|
|
|
|
for my $item (@$items_arrayref) { |
165
|
|
|
|
|
|
|
my $hash_key_value = delete $item->{$hash_key_name}; |
166
|
|
|
|
|
|
|
$items_hashref->{$hash_key_value} = $item; |
167
|
|
|
|
|
|
|
} |
168
|
|
|
|
|
|
|
|
169
|
|
|
|
|
|
|
return $items_hashref; |
170
|
|
|
|
|
|
|
} |
171
|
|
|
|
|
|
|
|
172
|
|
|
|
|
|
|
1; |
173
|
|
|
|
|
|
|
__END__ |