File: db_cleanse.php

package info (click to toggle)
boinc 5.4.11-4%2Betch1
  • links: PTS
  • area: main
  • in suites: etch
  • size: 21,440 kB
  • ctags: 16,986
  • sloc: cpp: 70,682; ansic: 45,747; php: 35,513; xml: 10,487; sh: 9,324; python: 4,291; makefile: 1,958; asm: 1,258; perl: 914; sql: 395; csh: 126; pascal: 124
file content (99 lines) | stat: -rw-r--r-- 2,667 bytes parent folder | download | duplicates (3)
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
94
95
96
97
98
99
<?php

// script to delete results with no corresponding workunit.
// In theory these shouldn't exist,
// but (because of crashes or bugs) they sometimes do.
// db_purge doesn't get rid of them; this does

require_once("../inc/util.inc");
require_once("../inc/db.inc");

set_time_limit(0);

// get lists of current results and WUs.
// Do selects in this order so that we'll get all relevant results
//
function get_lists() {
    $config = get_config();
    $db_name = parse_config($config, "<db_name>");
    $db_host = parse_config($config, "<db_host>");
    system("mysql $db_name -h $db_host -e \"select workunitid, id from result \" | tail +2 | sort -n > dbc_res.dat");
    system("mysql $db_name -h $db_host -e \"select id from workunit\" | tail +2 | sort -n > dbc_wu.dat");
}

// N.B.: on Linux we can use join -v to find results without WU.
// But for some reason this doesn't work on Solaris (*&^@#).
// So we roll our own.
// Note: it's
//
function join_lists() {
    $fwu = fopen('dbc_wu.dat', 'r');
    $fres = fopen('dbc_res.dat', 'r');
    $fout = fopen('dbc_out.dat', 'w');
    $wuid = 0;
    $n = 0;
    while (1) {
        $n++;
        if ($n % 1000 == 0) echo "$n\n";
        $x = fgets($fres);
        if (!$x) break;
        list($reswuid, $resid) = sscanf($x, "%d %d");
        if (feof($fwu)) {
            fputs($fout, "$resid\n");
            continue;
        }
        while ($wuid < $reswuid) {
            $y = fgets($fwu);
            if (!$y) {
                $wuid = 999999999999;
                break;
            }
            sscanf($y, "%d", $wuid);
        }
        if ($wuid == $reswuid) continue;
        if ($wuid > $reswuid) {
            fputs($fout, "$resid\n");
            continue;
        }
    }
}

// It would be better to have db_purge delete the results
// (and write them to XML archive)
// but it's not clear how to do this.
// So just delete them.
//
function delete_results() {
    db_init();
    $f = fopen('dbc_out.dat', 'r');
    while (1) {
        $x = fgets($f);
        if (!$x) break;
        $n = sscanf($x, "%d", $resid);
        if ($n != 1) {
            echo "bad line: $x\n";
            continue;
        }
        $result = lookup_result($resid);
        if (!$result) {
            echo "no result $resultid\n";
            continue;
        }
        $wu = lookup_wu($result->workunitid);
        if ($wu) {
            echo "result has WU: $resid\n";
            continue;
        }
        echo "deleting $resid\n";

        // uncomment the following to actually delete

        //mysql_query("delete from result where id=$resid");
    }
}

get_lists();
join_lists();
delete_results();

?>