From 4df8b58527c78c64e72454b64247d74bc64ebc0c Mon Sep 17 00:00:00 2001 From: "Andreas J. Koenig" Date: Tue, 2 Dec 2008 05:04:06 +0100 Subject: [PATCH] add support for "contains" to find specific entries in a recent_events query --- lib/File/Rsync/Mirror/Recent.pm | 27 +++++++++++++++++++++++---- lib/File/Rsync/Mirror/Recentfile.pm | 30 +++++++++++++++++++++++------- 2 files changed, 46 insertions(+), 11 deletions(-) diff --git a/lib/File/Rsync/Mirror/Recent.pm b/lib/File/Rsync/Mirror/Recent.pm index 018e850..62364d4 100644 --- a/lib/File/Rsync/Mirror/Recent.pm +++ b/lib/File/Rsync/Mirror/Recent.pm @@ -162,13 +162,32 @@ use accessors @accessors; =head2 $bool = $obj->contains ( %query ) -(TBD) +DEPRECATED: use news() directly! + +Similar to news() but returns a boolean answer: undef for false; a +hashref to the last matching recent_event in the interval. + +The query may contain the keys C, C, and C. Each +represents a condition that must be met. If there is more than one +such key, the conditions are ANDed. =cut sub contains { - my($self, %query) = @_; - + my($self, %opt) = @_; + my $contopt; + for my $allow (qw(epoch path type)) { + if (exists $opt{$allow}) { + $contopt->{$allow} = delete $opt{$allow}; + } + } + if (keys %opt) { + require Carp; + Carp::confess + (sprintf "unknown query: %s", join ", ", %opt); + } + my $ret = $self->news(max => 1, contains => $contopt); + return @$ret ? $ret->[0] : undef; } =head2 $arrayref = $obj->news ( %options ) @@ -248,7 +267,7 @@ sub news { =head2 overview ( %options ) -returns a string that summarizes the state of all recentfiles +returns a small table that summarizes the state of all recentfiles collected in this Recent object. $options{verbose}=1 increases the number of columns displayed. diff --git a/lib/File/Rsync/Mirror/Recentfile.pm b/lib/File/Rsync/Mirror/Recentfile.pm index cbbddd2..64752c7 100644 --- a/lib/File/Rsync/Mirror/Recentfile.pm +++ b/lib/File/Rsync/Mirror/Recentfile.pm @@ -1329,7 +1329,7 @@ sub read_recent_1 { Note: the code relies on the resource being written atomically. We cannot lock because we may have no write access. If the caller has write access (eg. aggregate() or update()), it has to care for any -necessary locking. +necessary locking and it MUST write atomically. If $options{after} is specified, only file events after this timestamp are returned. @@ -1340,7 +1340,13 @@ timestamp are returned. IF $options{'skip-deletes'} is specified, no files-to-be-deleted will be returned. -If $options{max} is specified only this many events are returned. +If $options{max} is specified only a maximum of this many events is +returned. + +If $options{contains} is specified the value must be a hash reference +containing a query. The query may contain the keys C, C, +and C. Each represents a condition that must be met. If there is +more than one such key, the conditions are ANDed. If $options{info} is specified, it must be a hashref. This hashref will be filled with metadata about the unfiltered recent_events of @@ -1418,14 +1424,24 @@ sub _recent_events_handle_options { $first_item = 0; } } - my @rre = splice @$re, $first_item, 1+$last_item-$first_item; + if (0 != $first_item || -1 != $last_item) { + @$re = splice @$re, $first_item, 1+$last_item-$first_item; + } if ($options->{'skip-deletes'}) { - @rre = grep { $_->{type} ne "delete" } @rre; + @$re = grep { $_->{type} ne "delete" } @$re; + } + if (my $contopt = $options->{contains}) { + for my $allow (qw(epoch path type)) { + if (exists $contopt->{$allow}) { + my $v = delete $contopt->{$allow}; + @$re = grep { $_->{$allow} eq $v } @$re; + } + } } - if ($options->{max} && @rre > $options->{max}) { - @rre = splice @rre, 0, $options->{max}; + if ($options->{max} && @$re > $options->{max}) { + @$re = splice @$re, 0, $options->{max}; } - \@rre; + $re; } sub _recent_events_protocol_x { -- 2.11.4.GIT