mirror of
https://github.com/percona/percona-toolkit.git
synced 2025-10-19 17:04:00 +00:00
Scaffolding for saving and reading reference results.
This commit is contained in:
147
lib/ResultWriter.pm
Normal file
147
lib/ResultWriter.pm
Normal file
@@ -0,0 +1,147 @@
|
||||
# This program is copyright 2013 Percona Ireland Ltd.
|
||||
# Feedback and improvements are welcome.
|
||||
#
|
||||
# THIS PROGRAM IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED
|
||||
# WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
|
||||
# MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
|
||||
#
|
||||
# This program is free software; you can redistribute it and/or modify it under
|
||||
# the terms of the GNU General Public License as published by the Free Software
|
||||
# Foundation, version 2; OR the Perl Artistic License. On UNIX and similar
|
||||
# systems, you can issue `man perlgpl' or `man perlartistic' to read these
|
||||
# licenses.
|
||||
#
|
||||
# You should have received a copy of the GNU General Public License along with
|
||||
# this program; if not, write to the Free Software Foundation, Inc., 59 Temple
|
||||
# Place, Suite 330, Boston, MA 02111-1307 USA.
|
||||
# ###########################################################################
|
||||
# ResultWriter package
|
||||
# ###########################################################################
|
||||
{
|
||||
package ResultWriter;
|
||||
|
||||
use strict;
|
||||
use warnings FATAL => 'all';
|
||||
use English qw(-no_match_vars);
|
||||
use constant PTDEBUG => $ENV{PTDEBUG} || 0;
|
||||
|
||||
use Data::Dumper;
|
||||
|
||||
use Lmo;
|
||||
|
||||
has 'dir' => (
|
||||
is => 'ro',
|
||||
isa => 'Str',
|
||||
required => 1,
|
||||
);
|
||||
|
||||
has 'pretty' => (
|
||||
is => 'ro',
|
||||
isa => 'Bool',
|
||||
required => 0,
|
||||
default => 0,
|
||||
);
|
||||
|
||||
has '_query_fh' => (
|
||||
is => 'rw',
|
||||
isa => 'Maybe[FileHandle]',
|
||||
required => 0,
|
||||
);
|
||||
|
||||
has '_meta_fh' => (
|
||||
is => 'rw',
|
||||
isa => 'Maybe[FileHandle]',
|
||||
required => 0,
|
||||
);
|
||||
|
||||
has '_results_fh' => (
|
||||
is => 'rw',
|
||||
isa => 'Maybe[FileHandle]',
|
||||
required => 0,
|
||||
);
|
||||
|
||||
sub BUILDARGS {
|
||||
my $class = shift;
|
||||
my $args = $class->SUPER::BUILDARGS(@_);
|
||||
|
||||
my $dir = $args->{dir};
|
||||
|
||||
my $query_file = "$dir/query";
|
||||
open my $_query_fh, '>', $query_file
|
||||
or die "Cannot open $query_file for writing: $OS_ERROR";
|
||||
|
||||
my $meta_file = "$dir/meta";
|
||||
open my $_meta_fh, '>', $meta_file
|
||||
or die "Cannot open $meta_file for writing: $OS_ERROR";
|
||||
|
||||
my $results_file = "$dir/results";
|
||||
open my $_results_fh, '>', $results_file
|
||||
or die "Cannot open $results_file for writing: $OS_ERROR";
|
||||
|
||||
my $self = {
|
||||
%$args,
|
||||
_query_fh => $_query_fh,
|
||||
_meta_fh => $_meta_fh,
|
||||
_results_fh => $_results_fh,
|
||||
};
|
||||
|
||||
return $self;
|
||||
}
|
||||
|
||||
|
||||
sub save {
|
||||
my ($self, %args) = @_;
|
||||
|
||||
my $event = $args{event};
|
||||
my $results = $args{results};
|
||||
|
||||
print { $self->_query_fh } $event->{arg}, "\n##\n";
|
||||
|
||||
if ( my $error = $results->{error} ) {
|
||||
print { $self->_meta_fh } $error, "\n##\n";
|
||||
print { $self->_results_fh } '', "\n##\n";
|
||||
}
|
||||
else {
|
||||
my $sth = $results->{sth};
|
||||
my $rows = $sth->fetchall_arrayref();
|
||||
eval {
|
||||
$sth->finish;
|
||||
delete $results->{sth};
|
||||
};
|
||||
print { $self->_meta_fh } $self->dumper($results, 'meta'), "\n##\n";
|
||||
print { $self->_results_fh } $self->dumper($rows, 'results'), "\n##\n";
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
sub dumper {
|
||||
my ($self, $data, $name) = @_;
|
||||
if ( $self->pretty ) {
|
||||
local $Data::Dumper::Indent = 1;
|
||||
local $Data::Dumper::Sortkeys = 1;
|
||||
local $Data::Dumper::Quotekeys = 0;
|
||||
return Data::Dumper->Dump([$data], [$name]);
|
||||
}
|
||||
else {
|
||||
local $Data::Dumper::Indent = 0;
|
||||
local $Data::Dumper::Sortkeys = 0;
|
||||
local $Data::Dumper::Quotekeys = 0;
|
||||
return Data::Dumper->Dump([$data], [$name]);
|
||||
}
|
||||
}
|
||||
|
||||
sub _d {
|
||||
my ($package, undef, $line) = caller 0;
|
||||
@_ = map { (my $temp = $_) =~ s/\n/\n# /g; $temp; }
|
||||
map { defined $_ ? $_ : 'undef' }
|
||||
@_;
|
||||
print STDERR "# $package:$line $PID ", join(' ', @_), "\n";
|
||||
}
|
||||
|
||||
no Lmo;
|
||||
1;
|
||||
}
|
||||
# ###########################################################################
|
||||
# End ResultWriter package
|
||||
# ###########################################################################
|
Reference in New Issue
Block a user