From 0382a677df24a9d8b9178ca08e0e7ba10b94c3ce Mon Sep 17 00:00:00 2001 From: Isaac Connor Date: Mon, 13 Dec 2021 14:58:32 -0500 Subject: [PATCH] Add ExecuteInterval to Filter Object. Allow empty sort_field to have the effort of not sorting. Always add FOR SHARE which adds a read-only lock the returned records. Add SKIP LOCKED functionality so that filters can simply skip locked records. This should reduce contention. Also specify FOR UPDATE of the events table, otherwise we also lock Monitors and Storage. --- scripts/ZoneMinder/lib/ZoneMinder/Filter.pm | 26 +++++++++++++-------- 1 file changed, 16 insertions(+), 10 deletions(-) diff --git a/scripts/ZoneMinder/lib/ZoneMinder/Filter.pm b/scripts/ZoneMinder/lib/ZoneMinder/Filter.pm index 0db46ff0d..7b3efad46 100644 --- a/scripts/ZoneMinder/lib/ZoneMinder/Filter.pm +++ b/scripts/ZoneMinder/lib/ZoneMinder/Filter.pm @@ -56,6 +56,7 @@ $primary_key = 'Id'; %fields = map { $_ => $_ } qw( Id Name +ExecuteInterval Query_json AutoArchive AutoUnarchive @@ -106,7 +107,6 @@ sub Execute { $sql =~ s/zmSystemLoad/$load/g; } - $sql .= ' FOR UPDATE' if $$self{LockRows}; Debug("Filter::Execute SQL ($sql)"); my $sth = $ZoneMinder::Database::dbh->prepare_cached($sql) @@ -371,10 +371,7 @@ sub Sql { if ( @auto_terms ) { $sql .= ' AND ( '.join(' or ', @auto_terms).' )'; } - if ( !$filter_expr->{sort_field} ) { - $filter_expr->{sort_field} = 'StartDateTime'; - $filter_expr->{sort_asc} = 0; - } + my $sort_column = ''; if ( $filter_expr->{sort_field} eq 'Id' ) { $sort_column = 'E.Id'; @@ -406,14 +403,23 @@ sub Sql { $sort_column = 'E.MaxScore'; } elsif ( $filter_expr->{sort_field} eq 'DiskSpace' ) { $sort_column = 'E.DiskSpace'; - } else { - $sort_column = 'E.StartDateTime'; + } elsif ( $filter_expr->{sort_field} ne '' ) { + $sort_column = 'E.'.$filter_expr->{sort_field}; } - my $sort_order = $filter_expr->{sort_asc} ? 'ASC' : 'DESC'; - $sql .= ' ORDER BY '.$sort_column.' '.$sort_order; - if ( $filter_expr->{limit} ) { + if ( $sort_column ne '' ) { + $sql .= ' ORDER BY '.$sort_column.' '.($filter_expr->{sort_asc} ? 'ASC' : 'DESC'); + } + if ($filter_expr->{limit}) { $sql .= ' LIMIT 0,'.$filter_expr->{limit}; } + if ($$self{LockRows}) { + $sql .= ' FOR UPDATE OF E' + } else { + $sql .= ' FOR SHARE OF E' + } + if ($filter_expr->{skip_locked}) { + $sql .= ' SKIP LOCKED'; + } $self->{Sql} = $sql; } # end if has Sql return $self->{Sql};