incrvacuum3.test 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. # 2013 Feb 25
  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. # This file implements regression tests for the SQLite library, focusing
  12. # on the incremental vacuum feature.
  13. #
  14. # The tests in this file were added at the same time as optimizations
  15. # were made to:
  16. #
  17. # * Truncate the database after a rollback mode commit, and
  18. #
  19. # * Avoid moving pages to locations from which they may need to be moved
  20. # a second time if an incremental-vacuum proccess is allowed to vacuum
  21. # the entire database.
  22. #
  23. set testdir [file dirname $argv0]
  24. source $testdir/tester.tcl
  25. set testprefix incrvacuum3
  26. # If this build of the library does not support auto-vacuum, omit this
  27. # whole file.
  28. ifcapable {!autovacuum || !pragma} {
  29. finish_test
  30. return
  31. }
  32. proc check_on_disk {} {
  33. # Copy the wal and journal files for database "test.db" to "test2.db".
  34. forcedelete test2.db test2.db-journal test2.db-wal
  35. if {[file exists test.db-journal]} {
  36. forcecopy test.db-journal test2.db-journal
  37. }
  38. if {[file exists test.db-wal]} {
  39. forcecopy test.db-wal test2.db-wal
  40. }
  41. # Now copy the database file itself. Do this using open/read/puts
  42. # instead of the [file copy] command in order to avoid attempting
  43. # to read the 512 bytes begining at offset $sqlite_pending_byte.
  44. #
  45. set sz [file size test.db]
  46. set fd [open test.db]
  47. set fd2 [open test2.db w]
  48. fconfigure $fd -encoding binary -translation binary
  49. fconfigure $fd2 -encoding binary -translation binary
  50. if {$sz>$::sqlite_pending_byte} {
  51. puts -nonewline $fd2 [read $fd $::sqlite_pending_byte]
  52. seek $fd [expr $::sqlite_pending_byte+512]
  53. seek $fd2 [expr $::sqlite_pending_byte+512]
  54. }
  55. puts -nonewline $fd2 [read $fd]
  56. close $fd2
  57. close $fd
  58. # Open "test2.db" and check it is Ok.
  59. sqlite3 dbcheck test2.db
  60. set ret [dbcheck eval { PRAGMA integrity_check }]
  61. dbcheck close
  62. set ret
  63. }
  64. # Run these tests once in rollback journal mode, and once in wal mode.
  65. #
  66. foreach {T jrnl_mode} {
  67. 1 delete
  68. 2 wal
  69. } {
  70. catch { db close }
  71. forcedelete test.db test.db-journal test.db-wal
  72. sqlite3 db test.db
  73. db eval {
  74. PRAGMA cache_size = 5;
  75. PRAGMA page_size = 1024;
  76. PRAGMA auto_vacuum = 2;
  77. }
  78. db eval "PRAGMA journal_mode = $jrnl_mode"
  79. foreach {tn sql} {
  80. 1 {
  81. CREATE TABLE t1(x UNIQUE);
  82. INSERT INTO t1 VALUES(randomblob(400));
  83. INSERT INTO t1 VALUES(randomblob(400));
  84. INSERT INTO t1 SELECT randomblob(400) FROM t1; -- 4
  85. INSERT INTO t1 SELECT randomblob(400) FROM t1; -- 8
  86. INSERT INTO t1 SELECT randomblob(400) FROM t1; -- 16
  87. INSERT INTO t1 SELECT randomblob(400) FROM t1; -- 32
  88. INSERT INTO t1 SELECT randomblob(400) FROM t1; -- 64
  89. INSERT INTO t1 SELECT randomblob(400) FROM t1; -- 128
  90. INSERT INTO t1 SELECT randomblob(400) FROM t1; -- 256
  91. }
  92. 2 {
  93. DELETE FROM t1 WHERE rowid%8;
  94. }
  95. 3 {
  96. BEGIN;
  97. PRAGMA incremental_vacuum = 100;
  98. INSERT INTO t1 SELECT randomblob(400) FROM t1; -- 64
  99. INSERT INTO t1 SELECT randomblob(400) FROM t1; -- 128
  100. INSERT INTO t1 SELECT randomblob(400) FROM t1; -- 256
  101. ROLLBACK;
  102. }
  103. 4 {
  104. BEGIN;
  105. SAVEPOINT one;
  106. PRAGMA incremental_vacuum = 100;
  107. SAVEPOINT two;
  108. INSERT INTO t1 SELECT randomblob(400) FROM t1; -- 64
  109. INSERT INTO t1 SELECT randomblob(400) FROM t1; -- 128
  110. INSERT INTO t1 SELECT randomblob(400) FROM t1; -- 256
  111. }
  112. 5 { ROLLBACK to two }
  113. 6 { ROLLBACK to one }
  114. 7 {
  115. INSERT INTO t1 SELECT randomblob(400) FROM t1; -- 64
  116. PRAGMA incremental_vacuum = 1000;
  117. INSERT INTO t1 SELECT randomblob(400) FROM t1; -- 128
  118. INSERT INTO t1 SELECT randomblob(400) FROM t1; -- 256
  119. ROLLBACK;
  120. }
  121. 8 {
  122. BEGIN;
  123. INSERT INTO t1 SELECT randomblob(400) FROM t1; -- 64
  124. PRAGMA incremental_vacuum = 1000;
  125. INSERT INTO t1 SELECT randomblob(400) FROM t1; -- 128
  126. COMMIT;
  127. }
  128. } {
  129. do_execsql_test $T.1.$tn.1 $sql
  130. do_execsql_test $T.1.$tn.2 {PRAGMA integrity_check} ok
  131. do_test $T.1.$tn.3 { check_on_disk } ok
  132. }
  133. do_execsql_test $T.1.x.1 { PRAGMA freelist_count } 0
  134. do_execsql_test $T.1.x.2 { SELECT count(*) FROM t1 } 128
  135. }
  136. finish_test