Index: src/btree.c ================================================================== --- src/btree.c +++ src/btree.c @@ -7,11 +7,11 @@ ** May you do good and not evil. ** May you find forgiveness for yourself and forgive others. ** May you share freely, never taking more than you give. ** ************************************************************************* -** $Id: btree.c,v 1.208 2004/11/05 12:27:02 danielk1977 Exp $ +** $Id: btree.c,v 1.209 2004/11/05 12:58:25 danielk1977 Exp $ ** ** This file implements a external (disk-based) database using BTrees. ** For a detailed discussion of BTrees, refer to ** ** Donald E. Knuth, THE ART OF COMPUTER PROGRAMMING, Volume 3: @@ -4580,11 +4580,15 @@ return rc; } *piMoved = maxRootPgno; } - rc = sqlite3BtreeUpdateMeta(pBt, 4, maxRootPgno-1); + maxRootPgno--; + if( maxRootPgno==PTRMAP_PAGENO(pBt->pageSize, maxRootPgno) ){ + maxRootPgno--; + } + rc = sqlite3BtreeUpdateMeta(pBt, 4, maxRootPgno); }else{ rc = freePage(pPage); releasePage(pPage); } #endif Index: test/autovacuum.test ================================================================== --- test/autovacuum.test +++ test/autovacuum.test @@ -9,11 +9,11 @@ # #*********************************************************************** # This file implements regression tests for SQLite library. The # focus of this file is testing the SELECT statement. # -# $Id: autovacuum.test,v 1.7 2004/11/05 12:27:03 danielk1977 Exp $ +# $Id: autovacuum.test,v 1.8 2004/11/05 12:58:26 danielk1977 Exp $ set testdir [file dirname $argv0] source $testdir/tester.tcl # Return a string $len characters long. The returned string is $char repeated @@ -246,8 +246,49 @@ do_test autovacuum-2.4.3 { execsql { SELECT rootpage FROM sqlite_master ORDER by rootpage } } {3 4 5 6 7 8 9 10} + +# Right now there are 5 free pages in the database. Consume and then free +# a 520 pages. Then create 520 tables. This ensures that at least some of the +# desired root-pages reside on the second free-list trunk page, and that the +# trunk itself is required at some point. +do_test autovacuum-2.4.4 { + execsql " + INSERT INTO av3 VALUES ('[make_str abcde [expr 1020*520 + 500]]'); + DELETE FROM av3; + " +} {} +set root_page_list [list] +for {set i 3} {$i<=532} {incr i} { + # 207 and 412 are pointer-map pages. + if { $i!=207 && $i!=412 } { + lappend root_page_list $i + } +} +do_test autovacuum-2.4.5 { + for {set i 11} {$i<=530} {incr i} { + execsql "CREATE TABLE av$i (x)" + } + execsql { + SELECT rootpage FROM sqlite_master ORDER by rootpage + } +} $root_page_list + +# Just for fun, delete all those tables and see if the database is 1 page. +do_test autovacuum-2.4.6 { + execsql COMMIT; + file_pages +} 561 +integrity_check autovacuum-2.4.6 +do_test autovacuum-2.4.7 { + execsql BEGIN + for {set i 3} {$i<=530} {incr i} { + execsql "DROP TABLE av$i" + } + execsql COMMIT + file_pages +} 1 finish_test