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 24set testdir [file dirname $argv0] 25source $testdir/tester.tcl 26set testprefix incrvacuum3 27 28# If this build of the library does not support auto-vacuum, omit this 29# whole file. 30ifcapable {!autovacuum || !pragma} { 31 finish_test 32 return 33} 34 35proc check_on_disk {} { 36 37 # Copy the wal and journal files for database "test.db" to "test2.db". 38 forcedelete test2.db test2.db-journal test2.db-wal 39 if {[file exists test.db-journal]} { 40 forcecopy test.db-journal test2.db-journal 41 } 42 if {[file exists test.db-wal]} { 43 forcecopy test.db-wal test2.db-wal 44 } 45 46 # Now copy the database file itself. Do this using open/read/puts 47 # instead of the [file copy] command in order to avoid attempting 48 # to read the 512 bytes begining at offset $sqlite_pending_byte. 49 # 50 set sz [file size test.db] 51 set fd [open test.db] 52 set fd2 [open test2.db w] 53 fconfigure $fd -encoding binary -translation binary 54 fconfigure $fd2 -encoding binary -translation binary 55 if {$sz>$::sqlite_pending_byte} { 56 puts -nonewline $fd2 [read $fd $::sqlite_pending_byte] 57 seek $fd [expr $::sqlite_pending_byte+512] 58 seek $fd2 [expr $::sqlite_pending_byte+512] 59 } 60 puts -nonewline $fd2 [read $fd] 61 close $fd2 62 close $fd 63 64 # Open "test2.db" and check it is Ok. 65 sqlite3 dbcheck test2.db 66 set ret [dbcheck eval { PRAGMA integrity_check }] 67 dbcheck close 68 set ret 69} 70 71# Run these tests once in rollback journal mode, and once in wal mode. 72# 73foreach {T jrnl_mode} { 74 1 delete 75 2 wal 76} { 77 catch { db close } 78 forcedelete test.db test.db-journal test.db-wal 79 sqlite3 db test.db 80 db eval { 81 PRAGMA cache_size = 5; 82 PRAGMA page_size = 1024; 83 PRAGMA auto_vacuum = 2; 84 } 85 db eval "PRAGMA journal_mode = $jrnl_mode" 86 87 foreach {tn sql} { 88 1 { 89 CREATE TABLE t1(x UNIQUE); 90 INSERT INTO t1 VALUES(randomblob(400)); 91 INSERT INTO t1 VALUES(randomblob(400)); 92 INSERT INTO t1 SELECT randomblob(400) FROM t1; -- 4 93 INSERT INTO t1 SELECT randomblob(400) FROM t1; -- 8 94 INSERT INTO t1 SELECT randomblob(400) FROM t1; -- 16 95 INSERT INTO t1 SELECT randomblob(400) FROM t1; -- 32 96 INSERT INTO t1 SELECT randomblob(400) FROM t1; -- 64 97 INSERT INTO t1 SELECT randomblob(400) FROM t1; -- 128 98 INSERT INTO t1 SELECT randomblob(400) FROM t1; -- 256 99 } 100 101 2 { 102 DELETE FROM t1 WHERE rowid%8; 103 } 104 105 3 { 106 BEGIN; 107 PRAGMA incremental_vacuum = 100; 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 ROLLBACK; 112 } 113 114 4 { 115 BEGIN; 116 SAVEPOINT one; 117 PRAGMA incremental_vacuum = 100; 118 SAVEPOINT two; 119 INSERT INTO t1 SELECT randomblob(400) FROM t1; -- 64 120 INSERT INTO t1 SELECT randomblob(400) FROM t1; -- 128 121 INSERT INTO t1 SELECT randomblob(400) FROM t1; -- 256 122 } 123 124 5 { ROLLBACK to two } 125 126 6 { ROLLBACK to one } 127 128 7 { 129 INSERT INTO t1 SELECT randomblob(400) FROM t1; -- 64 130 PRAGMA incremental_vacuum = 1000; 131 INSERT INTO t1 SELECT randomblob(400) FROM t1; -- 128 132 INSERT INTO t1 SELECT randomblob(400) FROM t1; -- 256 133 ROLLBACK; 134 } 135 136 8 { 137 BEGIN; 138 INSERT INTO t1 SELECT randomblob(400) FROM t1; -- 64 139 PRAGMA incremental_vacuum = 1000; 140 INSERT INTO t1 SELECT randomblob(400) FROM t1; -- 128 141 COMMIT; 142 } 143 } { 144 do_execsql_test $T.1.$tn.1 $sql 145 do_execsql_test $T.1.$tn.2 {PRAGMA integrity_check} ok 146 do_test $T.1.$tn.3 { check_on_disk } ok 147 } 148 149 do_execsql_test $T.1.x.1 { PRAGMA freelist_count } 0 150 do_execsql_test $T.1.x.2 { SELECT count(*) FROM t1 } 128 151} 152 153finish_test 154