| 1 | |||
| 2 | package OOPS::Fsck; | ||
| 3 | |||
| 4 | use OOPS; | ||
| 5 | use OOPS::Setup; | ||
| 6 | require Exporter; | ||
| 7 | use strict; | ||
| 8 | use warnings; | ||
| 9 | |||
| 10 | our @ISA = qw(Exporter); | ||
| 11 | our @EXPORT = qw(fsck); | ||
| 12 | |||
| 13 | our $check_batchsize = 200; | ||
| 14 | our $silent = 0; | ||
| 15 | our $debug = 0; | ||
| 16 | |||
| 17 | sub fsck | ||
| 18 | { | ||
| 19 | my (%args) = @_; | ||
| 20 | |||
| 21 | my $dbms; | ||
| 22 | my $prefix; | ||
| 23 | my $hasbig = 1; | ||
| 24 | { | ||
| 25 | my $dbh; | ||
| 26 | ($dbh, $dbms, $prefix) = OOPS->dbiconnect(%args, readonly => 1); | ||
| 27 | } | ||
| 28 | require "OOPS/$dbms.pm"; | ||
| 29 | |||
| 30 | # | ||
| 31 | # We'll need to transform the queries... | ||
| 32 | # | ||
| 33 | my $TPsub = sub { | ||
| 34 | my ($query) = @_; | ||
| 35 | $query =~ s/TP_/$prefix/g; | ||
| 36 | return $query; | ||
| 37 | }; | ||
| 38 | |||
| 39 | my $corrections; | ||
| 40 | my $rows; | ||
| 41 | my $lastid = $OOPS::last_reserved_oid; | ||
| 42 | for (;;) { | ||
| 43 | my $some = 0; | ||
| 44 | transaction(sub { | ||
| 45 | my $dbh = OOPS->dbiconnect(%args); | ||
| 46 | my $refsQ = $dbh->prepare_cached(&$TPsub(<<END)) || die; | ||
| 47 | SELECT COUNT(*) | ||
| 48 | FROM TP_attribute | ||
| 49 | WHERE pval = ? | ||
| 50 | AND ptype = 'R' | ||
| 51 | END | ||
| 52 | my $getobjQ = $dbh->prepare(&$TPsub(<<END)) || die; | ||
| 53 | SELECT id, class, otype, refs | ||
| 54 | FROM TP_object | ||
| 55 | WHERE id > ? | ||
| 56 | LIMIT $check_batchsize | ||
| 57 | END | ||
| 58 | $getobjQ->execute($lastid) || die $dbh->errstr; | ||
| 59 | my $commit = 0; | ||
| 60 | my $last; | ||
| 61 | while (my ($id, $class, $otype, $refs) = $getobjQ->fetchrow_array()) { | ||
| 62 | $some++; | ||
| 63 | $last = $id; | ||
| 64 | $refsQ->execute($id) || die; | ||
| 65 | my ($found) = $refsQ->fetchrow_array(); | ||
| 66 | if ($found == $refs) { | ||
| 67 | print "# Fsck: Ref correct for *$id $otype/$class\n" if $debug; | ||
| 68 | } else { | ||
| 69 | print STDERR "Correcting refcount for *$id $otype/$class: was $refs, now $found\n" | ||
| 70 | if ! $silent; | ||
| 71 | my $update = $dbh->prepare_cached(&$TPsub(<<END)) || die; | ||
| 72 | UPDATE TP_object | ||
| 73 | SET refs = ? | ||
| 74 | WHERE id = ? | ||
| 75 | END | ||
| 76 | $update->execute($found, $id) || die; | ||
| 77 | $commit++; | ||
| 78 | } | ||
| 79 | } | ||
| 80 | $getobjQ->finish(); | ||
| 81 | $dbh->commit if $commit; | ||
| 82 | $corrections += $commit; | ||
| 83 | $rows += $some; | ||
| 84 | $lastid = $last; | ||
| 85 | }); | ||
| 86 | last unless $some; | ||
| 87 | } | ||
| 88 | my $missingobjs = 0; | ||
| 89 | $lastid = $OOPS::last_reserved_oid; | ||
| 90 | my $lastpkey = undef; | ||
| 91 | for (;;) { | ||
| 92 | my $done = 0; | ||
| 93 | transaction(sub { | ||
| 94 | my $dbh = OOPS->dbiconnect(%args); | ||
| 95 | my $found; | ||
| 96 | my $stop_at_id; | ||
| 97 | if (defined $lastpkey) { | ||
| 98 | print "# Fsck: continuing with $lastid/$lastpkey\n" if $debug; | ||
| 99 | my $getrefQ = $dbh->prepare(&$TPsub(<<END)) || die; | ||
| 100 | SELECT pkey, pval | ||
| 101 | FROM TP_attribute | ||
| 102 | WHERE id = ? | ||
| 103 | AND pkey >= ? | ||
| 104 | AND ptype = 'R' | ||
| 105 | ORDER BY pkey | ||
| 106 | LIMIT $check_batchsize | ||
| 107 | END | ||
| 108 | $getrefQ->execute($lastid, $lastpkey) || die $dbh->errstr; | ||
| 109 | $found = $getrefQ->fetchall_arrayref(); | ||
| 110 | if (@$found < $check_batchsize) { | ||
| 111 | print "# Fsck: done with $lastid\n" if $debug; | ||
| 112 | undef $lastpkey; | ||
| 113 | } else { | ||
| 114 | $lastpkey = $found->[$#$found][0]; | ||
| 115 | } | ||
| 116 | } else { | ||
| 117 | print "# Fsck: Searching from $lastid\n" if $debug; | ||
| 118 | my $getrefQ = $dbh->prepare(&$TPsub(<<END)) || die; | ||
| 119 | SELECT pkey, pval, id | ||
| 120 | FROM TP_attribute | ||
| 121 | WHERE id > ? | ||
| 122 | AND ptype = 'R' | ||
| 123 | ORDER BY id, pkey | ||
| 124 | LIMIT $check_batchsize | ||
| 125 | END | ||
| 126 | $getrefQ->execute($lastid) || die $dbh->errstr; | ||
| 127 | $found = $getrefQ->fetchall_arrayref(); | ||
| 128 | $stop_at_id = $found->[$#$found][2]; | ||
| 129 | die unless $stop_at_id; | ||
| 130 | if (@$found < $check_batchsize) { | ||
| 131 | print "# Fsck: all done\n" if $debug; | ||
| 132 | $done = 1; | ||
| 133 | } elsif ($found->[0][2] == $stop_at_id) { | ||
| 134 | print "# Fsck: all one batch\n" if $debug; | ||
| 135 | $lastpkey = $found->[$#$found][0]; | ||
| 136 | $stop_at_id = undef; | ||
| 137 | } | ||
| 138 | } | ||
| 139 | my $checkobjQ = $dbh->prepare(&$TPsub(<<END)) || die; | ||
| 140 | SELECT id | ||
| 141 | FROM TP_object | ||
| 142 | WHERE id = ? | ||
| 143 | END | ||
| 144 | my $commit = 0; | ||
| 145 | for my $row (@$found) { | ||
| 146 | my $fromid = $row->[2] || $lastid; | ||
| 147 | if ($stop_at_id and $fromid == $stop_at_id) { | ||
| 148 | last; | ||
| 149 | } | ||
| 150 | $lastid = $fromid; | ||
| 151 | my $toid = $row->[1]; | ||
| 152 | $checkobjQ->execute($toid) || die; | ||
| 153 | my ($fetchedid) = $checkobjQ->fetchrow_array(); | ||
| 154 | if (! $fetchedid) { | ||
| 155 | print STDERR "Reference to missing object *$toid from *$fromid/$row->[0]\n"; | ||
| 156 | my $cleanupQ = $dbh->prepare_cached(&$TPsub(<<END)) || die; | ||
| 157 | UPDATE TP_attribute | ||
| 158 | SET ptype = '', pval = null | ||
| 159 | WHERE id = ? | ||
| 160 | AND pkey = ? | ||
| 161 | END | ||
| 162 | $cleanupQ->execute($fromid, $row->[0]) || die; | ||
| 163 | $commit++; | ||
| 164 | } | ||
| 165 | } | ||
| 166 | |||
| 167 | $checkobjQ->finish(); | ||
| 168 | $dbh->commit if $commit; | ||
| 169 | $missingobjs += $commit; | ||
| 170 | }); | ||
| 171 | last if $done; | ||
| 172 | } | ||
| 173 | print STDERR "Rows: $rows, Corrections: $corrections, Missing Objects: $missingobjs\n" if ($corrections || $missingobjs || $debug) && ! $silent; | ||
| 174 | return ($rows, $corrections, $missingobjs) if wantarray; | ||
| 175 | return $corrections + $missingobjs; | ||
| 176 | } | ||
| 177 | |||
| 178 | 1; | ||
| 179 | |||
| 180 | |||
| 181 | __END__ |