1# 2015 Aug 8 2# 3# The author disclaims copyright to this source code. In place of 4# a legal notice, here is a blessing: 5# 6# May you do good and not evil. 7# May you find forgiveness for yourself and forgive others. 8# May you share freely, never taking more than you give. 9# 10#*********************************************************************** 11# 12 13if {![info exists testdir]} { 14 set testdir [file join [file dirname [info script]] .. .. test] 15} 16source $testdir/tester.tcl 17 18proc check_prestep_state {target state} { 19 set oal_exists [file exists $target-oal] 20 set wal_exists [file exists $target-wal] 21 set progress [rbu progress] 22 23 if {($progress==0 && $state!="oal" && $state!="done") 24 || ($oal_exists && $wal_exists) 25 || ($progress>0 && $state=="oal" && (!$oal_exists || $wal_exists)) 26 || ($state=="move" && (!$oal_exists || $wal_exists)) 27 || ($state=="checkpoint" && ($oal_exists || !$wal_exists)) 28 || ($state=="done" && ($oal_exists && $progress!=0)) 29 } { 30 error "B: state=$state progress=$progress oal=$oal_exists wal=$wal_exists" 31 } 32} 33 34proc check_poststep_state {rc target state} { 35 if {$rc=="SQLITE_OK" || $rc=="SQLITE_DONE"} { 36 set oal_exists [file exists $target-oal] 37 set wal_exists [file exists $target-wal] 38 if {$state=="move" && ($oal_exists || !$wal_exists)} { 39 error "A: state=$state progress=$progress oal=$oal_exists wal=$wal_exists" 40 } 41 } 42} 43 44# Run the RBU in file $rbu on target database $target until completion. 45# 46proc run_rbu {target rbu} { 47 sqlite3rbu rbu $target $rbu 48 while 1 { 49 set state [rbu state] 50 51 check_prestep_state $target $state 52 set rc [rbu step] 53 check_poststep_state $rc $target $state 54 55 if {$rc!="SQLITE_OK"} break 56 } 57 rbu close 58} 59 60proc step_rbu {target rbu} { 61 while 1 { 62 sqlite3rbu rbu $target $rbu 63 set state [rbu state] 64 check_prestep_state $target $state 65 set rc [rbu step] 66 check_poststep_state $rc $target $state 67 rbu close 68 if {$rc != "SQLITE_OK"} break 69 } 70 set rc 71} 72 73proc step_rbu_legacy {target rbu} { 74 while 1 { 75 sqlite3rbu rbu $target $rbu 76 set state [rbu state] 77 check_prestep_state $target $state 78 set rc [rbu step] 79 check_poststep_state $rc $target $state 80 rbu close 81 if {$rc != "SQLITE_OK"} break 82 sqlite3 tmpdb $rbu 83 tmpdb eval { DELETE FROM rbu_state WHERE k==10 } 84 tmpdb close 85 } 86 set rc 87} 88 89proc do_rbu_vacuum_test {tn step {statedb state.db}} { 90 forcedelete $statedb 91 if {$statedb=="" && $step==1} breakpoint 92 uplevel [list do_test $tn.1 [string map [list %state% $statedb %step% $step] { 93 if {%step%==0} { sqlite3rbu_vacuum rbu test.db {%state%}} 94 while 1 { 95 if {%step%==1} { sqlite3rbu_vacuum rbu test.db {%state%}} 96 set state [rbu state] 97 check_prestep_state test.db $state 98 set rc [rbu step] 99 check_poststep_state $rc test.db $state 100 if {$rc!="SQLITE_OK"} break 101 if {%step%==1} { rbu close } 102 } 103 rbu close 104 }] {SQLITE_DONE}] 105 106 uplevel [list do_execsql_test $tn.2 { 107 PRAGMA integrity_check 108 } ok] 109} 110 111