AryaWu/sqlite
0
1# 2013 Feb 252#3# The author disclaims copyright to this source code. In place of4# 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, focusing12# 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, and18#19# * Avoid moving pages to locations from which they may need to be moved20# a second time if an incremental-vacuum proccess is allowed to vacuum21# the entire database.22#23 24set testdir [file dirname $argv0]25source $testdir/tester.tcl26set testprefix incrvacuum327 28# If this build of the library does not support auto-vacuum, omit this29# whole file.30ifcapable {!autovacuum || !pragma} {31 finish_test32 return33}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-wal39 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/puts47 # instead of the [file copy] command in order to avoid attempting48 # 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 -translation binary54 fconfigure $fd2 -translation binary55 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 $fd262 close $fd63 64 # Open "test2.db" and check it is Ok.65 sqlite3 dbcheck test2.db66 set ret [dbcheck eval { PRAGMA integrity_check }]67 dbcheck close68 set ret69}70 71# Run these tests once in rollback journal mode, and once in wal mode.72#73foreach {T jrnl_mode} {74 1 delete75 2 wal76} {77 catch { db close }78 forcedelete test.db test.db-journal test.db-wal79 sqlite3 db test.db80 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; -- 493 INSERT INTO t1 SELECT randomblob(400) FROM t1; -- 894 INSERT INTO t1 SELECT randomblob(400) FROM t1; -- 1695 INSERT INTO t1 SELECT randomblob(400) FROM t1; -- 3296 INSERT INTO t1 SELECT randomblob(400) FROM t1; -- 6497 INSERT INTO t1 SELECT randomblob(400) FROM t1; -- 12898 INSERT INTO t1 SELECT randomblob(400) FROM t1; -- 25699 }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; -- 64109 INSERT INTO t1 SELECT randomblob(400) FROM t1; -- 128110 INSERT INTO t1 SELECT randomblob(400) FROM t1; -- 256111 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; -- 64120 INSERT INTO t1 SELECT randomblob(400) FROM t1; -- 128121 INSERT INTO t1 SELECT randomblob(400) FROM t1; -- 256122 }123 124 5 { ROLLBACK to two }125 126 6 { ROLLBACK to one }127 128 7 { 129 INSERT INTO t1 SELECT randomblob(400) FROM t1; -- 64130 PRAGMA incremental_vacuum = 1000;131 INSERT INTO t1 SELECT randomblob(400) FROM t1; -- 128132 INSERT INTO t1 SELECT randomblob(400) FROM t1; -- 256133 ROLLBACK;134 }135 136 8 { 137 BEGIN;138 INSERT INTO t1 SELECT randomblob(400) FROM t1; -- 64139 PRAGMA incremental_vacuum = 1000;140 INSERT INTO t1 SELECT randomblob(400) FROM t1; -- 128141 COMMIT;142 }143 } {144 do_execsql_test $T.1.$tn.1 $sql145 do_execsql_test $T.1.$tn.2 {PRAGMA integrity_check} ok146 do_test $T.1.$tn.3 { check_on_disk } ok147 }148 149 do_execsql_test $T.1.x.1 { PRAGMA freelist_count } 0150 do_execsql_test $T.1.x.2 { SELECT count(*) FROM t1 } 128151}152 153finish_test154 