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 $db_file_id = shift;
# $log->debug(Dumper($image->tags()));
my @tags = $image->tags();
# $log->debug(Dumper(\@tags));
for my $tag (@tags) {
$log->debug(sprintf("tag: [%s] [%s]", $tag->[0], $tag->[1]));
my $row = $db->save_tag($db_file_id, $tag->[0], $tag->[1]);
# $log->debug(Dumper($row));
}
my %tags_data = map { $_->[0] => $_->[1]} @tags;
$db->save_tags($db_file_id, \%tags_data);
}
Mojo::IOLoop->start;

View file

@ -8,7 +8,7 @@ use feature qw(signatures say);
no warnings qw(experimental::signatures);
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) {
say STDERR ("[$db_file_id][$tag_name][$tag_value]");
eval {
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;
};
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;