Skip to content

Instantly share code, notes, and snippets.

@tbreuss
Created November 10, 2023 06:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tbreuss/900aaa68090e2f73c36cff9adea08ec4 to your computer and use it in GitHub Desktop.
Save tbreuss/900aaa68090e2f73c36cff9adea08ec4 to your computer and use it in GitHub Desktop.
Simple script to analyse the standard output results of Percona Toolkit pt-duplicate-key-checker
<?php
$types = [];
$fp = @fopen("/downloads/db1-duplicate-keys.txt", "r");
if ($fp) {
$i = 0;
$status = 0;
while (($buffer = fgets($fp, 4096)) !== false) {
if ($status == 0) {
if (line_is_comment($buffer)) {
$status = 1;
}
continue;
}
if ($status == 1) {
if (line_is_database_with_table($buffer)) {
$status = 2;
}
continue;
}
if ($status == 2) {
if (line_is_comment($buffer)) {
$status = 3;
}
continue;
}
if ($status == 3) {
if (line_is_empty($buffer)) {
$status = 4;
}
continue;
}
if ($status == 4) {
$type = recognize_type(trim($buffer));
if (isset($types[$type])) {
$types[$type] += 1;
} else {
$types[$type] = 1;
}
$status = 3;
continue;
}
$status = 0;
$i++;
}
if (!feof($fp)) {
echo "Error: unexpected fgets() fail\n";
}
fclose($fp);
}
echo json_encode($types, JSON_PRETTY_PRINT);
function line_is_comment(string $line): bool
{
return str_contains($line, '# ###');
}
function line_is_empty(string $line): bool
{
return trim($line) == '';
}
function line_is_database_with_table(string $line): bool
{
return preg_match('/^# [a-zA-Z0-9_]+\.[a-zA-Z0-9_]+/', $line);
}
function recognize_type(string $line): string
{
if (str_contains($line, 'is a duplicate of')) {
$type = 'is a duplicate of';
} elseif (str_contains($line, 'is a left-prefix of')) {
$type = 'is a left-prefix of';
} elseif (str_contains($line, 'is a duplicate constraint')) {
$type = 'is a duplicate constraint';
} else {
$type = 'unknown';
}
return $type;
}
// Outputs something like
// {
// "is a duplicate of": 82491,
// "is a left-prefix of": 17174,
// "is a duplicate constraint": 2
// }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment