File: gt_n.pm

package info (click to toggle)
percona-toolkit 3.2.1-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, forky, sid, trixie
  • size: 68,916 kB
  • sloc: perl: 241,287; sql: 22,868; sh: 19,746; javascript: 6,799; makefile: 353; awk: 38; python: 30; sed: 1
file content (93 lines) | stat: -rw-r--r-- 2,060 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
package gt_n;

use strict;
use English qw(-no_match_vars);
use constant PTDEBUG  => $ENV{PTDEBUG};
use Data::Dumper;
$Data::Dumper::Indent    = 1;
$Data::Dumper::Sortkeys  = 1;
$Data::Dumper::Quotekeys = 0;


# Changes these two values for your table.
use constant MAX_ROWS => 5;
use constant WHERE    => 'status="ok"';


sub new {
   my ( $class, %args ) = @_;

   my $sql = "SELECT COUNT(*) FROM $args{db}.$args{tbl} WHERE " . WHERE;
   PTDEBUG && _d('Row count sql:', $sql);
   my $sth = $args{dbh}->prepare($sql);

   my $self = {
      %args,
      row_count_sth => $sth,
      done          => 0,
   };
   return bless $self, $class;
}

# Executes $self->{row_count_sth} and returns the number of rows.
sub get_row_count {
   my ( $self ) = @_;
   my $sth = $self->{row_count_sth};
   $sth->execute();
   my @row = $sth->fetchrow_array();
   PTDEBUG && _d('Row count:', $row[0]);
   $sth->finish();
   return $row[0];
}

sub before_begin {
   my ( $self, %args ) = @_;
   PTDEBUG && _d('before begin');
   # We don't need to do anything here.
   return;
}

sub is_archivable {
   my ( $self, %args ) = @_;
   PTDEBUG && _d('is archivable');

   if ( $self->{done} ) {
      PTDEBUG && _d("Already done, skipping row count");
      return 0;
   }

   my $n_rows = $self->get_row_count();
   if ( $n_rows <= MAX_ROWS ) {
      PTDEBUG && _d('Done archiving, row count <', MAX_ROWS,
         '; first non-archived row:', Dumper($args{row}));
      $self->{done} = 1;
      return 0;
   }

   return 1;  # Archive the row.
}

sub before_delete {
   my ( $self, %args ) = @_;
   # We don't need to do anything here.
   return;
}

sub after_finish {
   my ( $self ) = @_;
   PTDEBUG && _d('after finish');
   # Just to show in debug output how many rows are left at the end.
   my $n_rows = $self->get_row_count();
   return;
}

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";
}

1;