From ed4293119a8c77048eabe09eb447d2550f277822 Mon Sep 17 00:00:00 2001 From: danielk1977 Date: Thu, 19 Jan 2006 08:43:31 +0000 Subject: [PATCH] Account for read-uncommitted cursors in sqlite3BtreeClearTable(). (CVS 2977) FossilOrigin-Name: 950798326860de40926e82e10134f09e6ea86245 --- manifest | 17 +++++----- manifest.uuid | 2 +- src/btree.c | 31 +++++++++++------ test/malloc5.test | 4 ++- test/shared.test | 5 ++- test/shared2.test | 85 +++++++++++++++++++++++++++++++++++++++++++++++ 6 files changed, 122 insertions(+), 22 deletions(-) create mode 100644 test/shared2.test diff --git a/manifest b/manifest index 3a74484303..42b9d5f45b 100644 --- a/manifest +++ b/manifest @@ -1,5 +1,5 @@ -C Fix\sa\sbug\swith\sinternally\ssaving\scursors\sopen\son\sindex\stables.\sAlso\sincrease\scoverage\sof\sutil.c\sand\sbtree.c.\s(CVS\s2976) -D 2006-01-19T07:18:14 +C Account\sfor\sread-uncommitted\scursors\sin\ssqlite3BtreeClearTable().\s(CVS\s2977) +D 2006-01-19T08:43:31 F Makefile.in ab3ffd8d469cef4477257169b82810030a6bb967 F Makefile.linux-gcc aee18d8a05546dcf1888bd4547e442008a49a092 F README 9c4e2d6706bdcc3efdd773ce752a8cdab4f90028 @@ -34,7 +34,7 @@ F src/alter.c 90b779cf00489535cab6490df6dc050f40e4e874 F src/analyze.c 7d2b7ab9a9c2fd6e55700f69064dfdd3e36d7a8a F src/attach.c 0081040e9a5d13669b6712e947688c10f030bfc1 F src/auth.c 9ae84d2d94eb96195e04515715e08e85963e96c2 -F src/btree.c 1f760cdb5bd763e8c8a7e4cf822a4266274e3faa +F src/btree.c 2ea7d4372e8ec0a8894ec13e890aedf976e4bbd1 F src/btree.h 5663c4f43e8521546ccebc8fc95acb013b8f3184 F src/build.c 15224e2fd348ad32b9044aaa5bdc912e4067da15 F src/callback.c 1bf497306c32229114f826707054df7ebe10abf2 @@ -190,7 +190,7 @@ F test/malloc.test ce6d1e7e79f9db967b51e1975b50760af66db90d F test/malloc2.test e6e321db96d6c94cb18bf82ad7215070c41e624e F test/malloc3.test 265644c655497242f7c0a1bb5b36c8500a5fc27c F test/malloc4.test 2e29d155eb4b4808019ef47eeedfcbe9e09e0f05 -F test/malloc5.test 7c0a02ab2fef82af6febacc02e033514f1674066 +F test/malloc5.test 7425272e263325fda7d32cb55706e52b5c09e7e0 F test/manydb.test 8de36b8d33aab5ef295b11d9e95310aeded31af8 F test/memdb.test ed97df0414a6e33e043e3eeffccb54d06098d0ae F test/memleak.test df2b2b96e77f8ba159a332299535b1e5f18e49ac @@ -225,7 +225,8 @@ F test/select5.test 07a90ab3c7e3f0a241a9cdea1d997b2c8a89ff0b F test/select6.test f459a19bdac0501c4d3eb1a4df4b7a76f1bb8ad4 F test/select7.test 1bf795b948c133a15a2a5e99d3270e652ec58ce6 F test/server1.test e328b8e641ba8fe9273132cfef497383185dc1f5 -F test/shared.test 688bffac6928329f2e4d188de6c674652b7d61b7 +F test/shared.test 9982a65ccf3f4eee844a19f3ab0bcd7a158a76e5 +F test/shared2.test 909fc0f0277684ed29cc1b36c8e159188aec7f28 F test/sort.test 0e4456e729e5a92a625907c63dcdedfbe72c5dc5 F test/subquery.test ae324ee928c5fb463a3ce08a8860d6e7f1ca5797 F test/subselect.test 2d13fb7f450db3595adcdd24079a0dd1d2d6abc2 @@ -341,7 +342,7 @@ F www/tclsqlite.tcl bb0d1357328a42b1993d78573e587c6dcbc964b9 F www/vdbe.tcl 87a31ace769f20d3627a64fa1fade7fed47b90d0 F www/version3.tcl a99cf5f6d8bd4d5537584a2b342f0fb9fa601d8b F www/whentouse.tcl 97e2b5cd296f7d8057e11f44427dea8a4c2db513 -P df91f685cacbcb3e048eaa03260b33bbcfcb321c -R 72a56160d91187b3cc45d20fb8121c09 +P a628d84d3185fb7742cc929e758bfd59c811ca0b +R 8df41433786382b17ec3ae6b6ca9db68 U danielk1977 -Z 2d080ce01ccda1df734492ab6955a12f +Z d31ef3f0be10e8c94b80e49b253c3a40 diff --git a/manifest.uuid b/manifest.uuid index 99c567f35a..5f8911bc7d 100644 --- a/manifest.uuid +++ b/manifest.uuid @@ -1 +1 @@ -a628d84d3185fb7742cc929e758bfd59c811ca0b \ No newline at end of file +950798326860de40926e82e10134f09e6ea86245 \ No newline at end of file diff --git a/src/btree.c b/src/btree.c index 3e3dc9027f..59b824bcd0 100644 --- a/src/btree.c +++ b/src/btree.c @@ -9,7 +9,7 @@ ** May you share freely, never taking more than you give. ** ************************************************************************* -** $Id: btree.c,v 1.301 2006/01/19 07:18:14 danielk1977 Exp $ +** $Id: btree.c,v 1.302 2006/01/19 08:43:31 danielk1977 Exp $ ** ** This file implements a external (disk-based) database using BTrees. ** For a detailed discussion of BTrees, refer to @@ -5537,22 +5537,31 @@ int sqlite3BtreeClearTable(Btree *p, int iTable){ int rc; BtCursor *pCur; BtShared *pBt = p->pBt; + sqlite3 *db = p->pSqlite; if( p->inTrans!=TRANS_WRITE ){ return pBt->readOnly ? SQLITE_READONLY : SQLITE_ERROR; } - for(pCur=pBt->pCursor; pCur; pCur=pCur->pNext){ - if( pCur->pgnoRoot==(Pgno)iTable ){ - if( pCur->wrFlag==0 ) return SQLITE_LOCKED; - moveToRoot(pCur); + + /* If this connection is not in read-uncommitted mode and currently has + ** a read-cursor open on the table being cleared, return SQLITE_LOCKED. + */ + if( 0==db || 0==(db->flags&SQLITE_ReadUncommitted) ){ + for(pCur=pBt->pCursor; pCur; pCur=pCur->pNext){ + if( pCur->pBtree==p && pCur->pgnoRoot==(Pgno)iTable ){ + if( 0==pCur->wrFlag ){ + return SQLITE_LOCKED; + } + moveToRoot(pCur); + } } } - rc = clearDatabasePage(pBt, (Pgno)iTable, 0, 0); -#if 0 - if( rc ){ - sqlite3BtreeRollback(pBt); + + /* Save the position of all cursors open on this table */ + if( SQLITE_OK!=(rc = saveAllCursors(pBt, iTable, 0)) ){ + return rc; } -#endif - return rc; + + return clearDatabasePage(pBt, (Pgno)iTable, 0, 0); } /* diff --git a/test/malloc5.test b/test/malloc5.test index 21d3be00b6..50c1dda05b 100644 --- a/test/malloc5.test +++ b/test/malloc5.test @@ -12,7 +12,7 @@ # This file contains test cases focused on the two memory-management APIs, # sqlite3_soft_heap_limit() and sqlite3_release_memory(). # -# $Id: malloc5.test,v 1.6 2006/01/16 15:32:23 danielk1977 Exp $ +# $Id: malloc5.test,v 1.7 2006/01/19 08:43:32 danielk1977 Exp $ #--------------------------------------------------------------------------- # NOTES ON EXPECTED BEHAVIOUR @@ -192,6 +192,7 @@ do_test malloc5-4.1 { } execsql {COMMIT;} set ::nMaxBytes [sqlite_malloc_outstanding -maxbytes] + if {$::nMaxBytes==""} {set ::nMaxBytes 1000001} expr $::nMaxBytes > 1000000 } {1} do_test malloc5-4.2 { @@ -204,6 +205,7 @@ do_test malloc5-4.2 { } execsql {COMMIT;} set ::nMaxBytes [sqlite_malloc_outstanding -maxbytes] + if {$::nMaxBytes==""} {set ::nMaxBytes 0} expr $::nMaxBytes <= 100000 } {1} do_test malloc5-4.3 { diff --git a/test/shared.test b/test/shared.test index bbf48503c4..fe5483f6e6 100644 --- a/test/shared.test +++ b/test/shared.test @@ -9,7 +9,7 @@ # #*********************************************************************** # -# $Id: shared.test,v 1.18 2006/01/19 07:18:15 danielk1977 Exp $ +# $Id: shared.test,v 1.19 2006/01/19 08:43:32 danielk1977 Exp $ set testdir [file dirname $argv0] source $testdir/tester.tcl @@ -60,6 +60,9 @@ incr av # are attached in different orders to different handles. # shared-6.*: Locking, UNION ALL queries and sub-queries. # shared-7.*: Autovacuum and shared-cache. +# shared-8.*: Tests related to the text encoding of shared-cache databases. +# shared-9.*: TEMP triggers and shared-cache databases. +# shared-10.*: Tests of sqlite3_close(). # do_test shared-$av.1.1 { diff --git a/test/shared2.test b/test/shared2.test new file mode 100644 index 0000000000..c77010440b --- /dev/null +++ b/test/shared2.test @@ -0,0 +1,85 @@ +# 2005 January 19 +# +# The author disclaims copyright to this source code. In place of +# a legal notice, here is a blessing: +# +# 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: shared2.test,v 1.1 2006/01/19 08:43:32 danielk1977 Exp $ + +set testdir [file dirname $argv0] +source $testdir/tester.tcl +db close + +ifcapable !shared_cache { + finish_test + return +} +set ::enable_shared_cache [sqlite3_enable_shared_cache 1] + + +# Test that if we delete all rows from a table any read-uncommitted +# cursors are correctly invalidated. Test on both table and index btrees. +do_test shared2-1.1 { + sqlite3 db1 test.db + sqlite3 db2 test.db + + # Set up some data. Table "numbers" has 64 rows after this block + # is executed. + execsql { + BEGIN; + CREATE TABLE numbers(a PRIMARY KEY, b); + INSERT INTO numbers(oid) VALUES(NULL); + INSERT INTO numbers(oid) SELECT NULL FROM numbers; + INSERT INTO numbers(oid) SELECT NULL FROM numbers; + INSERT INTO numbers(oid) SELECT NULL FROM numbers; + INSERT INTO numbers(oid) SELECT NULL FROM numbers; + INSERT INTO numbers(oid) SELECT NULL FROM numbers; + INSERT INTO numbers(oid) SELECT NULL FROM numbers; + UPDATE numbers set a = oid, b = 'abcdefghijklmnopqrstuvwxyz0123456789'; + COMMIT; + } db1 +} {} +do_test shared2-1.2 { + # Put connection 2 in read-uncommitted mode and start a SELECT on table + # 'numbers'. Half way through the SELECT, use connection 1 to delete the + # contents of this table. + execsql { + pragma read_uncommitted = 1; + } db2 + set count [execsql {SELECT count(*) FROM numbers} db2] + db2 eval {SELECT a FROM numbers ORDER BY oid} { + if {$a==32} { + execsql { + BEGIN; + DELETE FROM numbers; + } db1 + } + } + list $a $count +} {32 64} +do_test shared2-1.3 { + # Same test as 1.2, except scan using the index this time. + execsql { + ROLLBACK; + } db1 + set count [execsql {SELECT count(*) FROM numbers} db2] + db2 eval {SELECT a, b FROM numbers ORDER BY a} { + if {$a==32} { + execsql { + DELETE FROM numbers; + } db1 + } + } + list $a $count +} {32 64} + +db1 close +db2 close + +sqlite3_enable_shared_cache $::enable_shared_cache +finish_test -- 2.39.5