rbu_common.tcl 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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. if {![info exists testdir]} {
  13. set testdir [file join [file dirname [info script]] .. .. test]
  14. }
  15. source $testdir/tester.tcl
  16. proc if_no_rbu_support {tcl} {
  17. set bOk 1
  18. ifcapable !rbu { set bOk 0 }
  19. if {[permutation]=="journaltest"} { set bOk 0 }
  20. if {$bOk==0} {
  21. set c [catch {uplevel 1 $tcl} r]
  22. return -code $c $r
  23. }
  24. }
  25. proc check_prestep_state {target state} {
  26. set oal_exists [file exists $target-oal]
  27. set wal_exists [file exists $target-wal]
  28. set progress [rbu progress]
  29. if {($progress==0 && $state!="oal" && $state!="done")
  30. || ($oal_exists && $wal_exists)
  31. || ($progress>0 && $state=="oal" && (!$oal_exists || $wal_exists))
  32. || ($state=="move" && (!$oal_exists || $wal_exists))
  33. || ($state=="checkpoint" && ($oal_exists || !$wal_exists))
  34. || ($state=="done" && ($oal_exists && $progress!=0))
  35. } {
  36. error "B: state=$state progress=$progress oal=$oal_exists wal=$wal_exists"
  37. }
  38. }
  39. proc check_poststep_state {rc target state} {
  40. if {$rc=="SQLITE_OK" || $rc=="SQLITE_DONE"} {
  41. set oal_exists [file exists $target-oal]
  42. set wal_exists [file exists $target-wal]
  43. if {$state=="move" && ($oal_exists || !$wal_exists)} {
  44. error "A: state=$state progress=$progress oal=$oal_exists wal=$wal_exists"
  45. }
  46. }
  47. }
  48. # Run the RBU in file $rbu on target database $target until completion.
  49. #
  50. proc run_rbu {target rbu} {
  51. sqlite3rbu rbu $target $rbu
  52. while 1 {
  53. set state [rbu state]
  54. check_prestep_state $target $state
  55. set rc [rbu step]
  56. check_poststep_state $rc $target $state
  57. if {$rc!="SQLITE_OK"} break
  58. }
  59. rbu close
  60. }
  61. proc step_rbu {target rbu} {
  62. while 1 {
  63. sqlite3rbu rbu $target $rbu
  64. set state [rbu state]
  65. check_prestep_state $target $state
  66. set rc [rbu step]
  67. check_poststep_state $rc $target $state
  68. rbu close
  69. if {$rc != "SQLITE_OK"} break
  70. }
  71. set rc
  72. }
  73. proc 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. proc do_rbu_vacuum_test {tn step {statedb state.db}} {
  89. forcedelete $statedb
  90. if {$statedb=="" && $step==1} breakpoint
  91. uplevel [list do_test $tn.1 [string map [list %state% $statedb %step% $step] {
  92. if {%step%==0} { sqlite3rbu_vacuum rbu test.db {%state%}}
  93. while 1 {
  94. if {%step%==1} { sqlite3rbu_vacuum rbu test.db {%state%}}
  95. set state [rbu state]
  96. check_prestep_state test.db $state
  97. set rc [rbu step]
  98. check_poststep_state $rc test.db $state
  99. if {$rc!="SQLITE_OK"} break
  100. if {%step%==1} { rbu close }
  101. }
  102. rbu close
  103. }] {SQLITE_DONE}]
  104. uplevel [list do_execsql_test $tn.2 {
  105. PRAGMA integrity_check
  106. } ok]
  107. }