DBI

 view release on metacpan or  search on metacpan

lib/DBI/ProfileData.pm  view on Meta::CPAN

each of each has a set of statistics and set of keys.  Each record
must have a unique set of keys, but there is no requirement that every
record have the same number of keys.

=head1 METHODS

The following methods are supported by DBI::ProfileData objects.

=cut

our $VERSION = "2.010008";

use Carp qw(croak);
use Symbol;
use Fcntl qw(:flock);

use DBI::Profile qw(dbi_profile_merge);

# some constants for use with node data arrays
sub COUNT     () { 0 };
sub TOTAL     () { 1 };
sub FIRST     () { 2 };
sub SHORTEST  () { 3 };
sub LONGEST   () { 4 };
sub FIRST_AT  () { 5 };
sub LAST_AT   () { 6 };
sub PATH      () { 7 };


my $HAS_FLOCK = (defined $ENV{DBI_PROFILE_FLOCK})
    ? $ENV{DBI_PROFILE_FLOCK}
    : do { local $@; eval { flock STDOUT, 0; 1 } };


=head2 $prof = DBI::ProfileData->new(File => "dbi.prof")

=head2 $prof = DBI::ProfileData->new(File => "dbi.prof", Filter => sub { ... })

=head2 $prof = DBI::ProfileData->new(Files => [ "dbi.prof.1", "dbi.prof.2" ])

Creates a new DBI::ProfileData object.  Takes either a single file
through the File option or a list of Files in an array ref.  If
multiple files are specified then the header data from the first file
is used.

=head3 Files

Reference to an array of file names to read.

=head3 File

Name of file to read. Takes precedence over C<Files>.

=head3 DeleteFiles

If true, the files are deleted after being read.

Actually the files are renamed with a C<deleteme> suffix before being read,
and then, after reading all the files, they're all deleted together.

The files are locked while being read which, combined with the rename, makes it
safe to 'consume' files that are still being generated by L<DBI::ProfileDumper>.

=head3 Filter

The C<Filter> parameter can be used to supply a code reference that can
manipulate the profile data as it is being read. This is most useful for
editing SQL statements so that slightly different statements in the raw data
will be merged and aggregated in the loaded data. For example:

  Filter => sub {
      my ($path_ref, $data_ref) = @_;
      s/foo = '.*?'/foo = '...'/ for @$path_ref;
  }

Here's an example that performs some normalization on the SQL. It converts all
numbers to C<N> and all quoted strings to C<S>.  It can also convert digits to
N within names. Finally, it summarizes long "IN (...)" clauses.

It's aggressive and simplistic, but it's often sufficient, and serves as an
example that you can tailor to suit your own needs:

  Filter => sub {
      my ($path_ref, $data_ref) = @_;
      local $_ = $path_ref->[0]; # whichever element contains the SQL Statement
      s/\b\d+\b/N/g;             # 42 -> N
      s/\b0x[0-9A-Fa-f]+\b/N/g;  # 0xFE -> N
      s/'.*?'/'S'/g;             # single quoted strings (doesn't handle escapes)
      s/".*?"/"S"/g;             # double quoted strings (doesn't handle escapes)
      # convert names like log_20001231 into log_NNNNNNNN, controlled by $opt{n}
      s/([a-z_]+)(\d{$opt{n},})/$1.('N' x length($2))/ieg if $opt{n};
      # abbreviate massive "in (...)" statements and similar
      s!(([NS],){100,})!sprintf("$2,{repeated %d times}",length($1)/2)!eg;
  }

It's often better to perform this kinds of normalization in the DBI while the
data is being collected, to avoid too much memory being used by storing profile
data for many different SQL statement. See L<DBI::Profile>.

=cut

sub new {
    my $pkg = shift;
    my $self = {
                Files        => [ "dbi.prof" ],
		Filter       => undef,
                DeleteFiles  => 0,
                LockFile     => $HAS_FLOCK,
                _header      => {},
                _nodes       => [],
                _node_lookup => {},
                _sort        => 'none',
                @_
               };
    bless $self, $pkg;

    # File (singular) overrides Files (plural)
    $self->{Files} = [ $self->{File} ] if exists $self->{File};

    $self->_read_files();
    return $self;



( run in 1.761 second using v1.01-cache-2.11-cpan-39bf76dae61 )