Save exif_tags in one batch

This commit is contained in:
Denis Fedoseev 2018-09-10 11:49:15 +03:00
parent 8ff6d351c4
commit c3d690f98a
2 changed files with 19 additions and 10 deletions

View file

@ -377,14 +377,11 @@ sub save_tags {
my $image = shift; my $image = shift;
my $db_file_id = shift; my $db_file_id = shift;
# $log->debug(Dumper($image->tags()));
my @tags = $image->tags(); my @tags = $image->tags();
# $log->debug(Dumper(\@tags));
for my $tag (@tags) { my %tags_data = map { $_->[0] => $_->[1]} @tags;
$log->debug(sprintf("tag: [%s] [%s]", $tag->[0], $tag->[1])); $db->save_tags($db_file_id, \%tags_data);
my $row = $db->save_tag($db_file_id, $tag->[0], $tag->[1]);
# $log->debug(Dumper($row));
}
} }
Mojo::IOLoop->start; Mojo::IOLoop->start;

View file

@ -8,7 +8,7 @@ use feature qw(signatures say);
no warnings qw(experimental::signatures); no warnings qw(experimental::signatures);
use Data::Dumper; use Data::Dumper;
use DBIx::Struct; use DBIx::Struct qw(connector);
@ -93,10 +93,8 @@ sub add_album($self, $user_id, $album_name, $album_desc) {
} }
sub save_tag($self, $db_file_id, $tag_name, $tag_value) { sub save_tag($self, $db_file_id, $tag_name, $tag_value) {
say STDERR ("[$db_file_id][$tag_name][$tag_value]");
eval { eval {
my $row = new_row('exif_data', 'exif_tag' => $tag_name, 'tag_data' => $tag_value,'image_id' => $db_file_id, deleted => 0) || die "error!"; my $row = new_row('exif_data', 'exif_tag' => $tag_name, 'tag_data' => $tag_value,'image_id' => $db_file_id, deleted => 0) || die "error!";
say STDERR (Dumper($row));
return $row; return $row;
}; };
if ($@) { if ($@) {
@ -104,5 +102,19 @@ sub save_tag($self, $db_file_id, $tag_name, $tag_value) {
} }
} }
sub save_tags($self, $db_file_id, $tag_data) {
eval {
connector->txn(sub {
for my $key (keys %$tag_data) {
$self->save_tag($db_file_id, $key, $tag_data->{$key});
}
});
};
if ($@) {
say STDERR ("Error! $@");
}
}
1; 1;