/ Check-in [95079832]
Login

Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Overview
Comment:Account for read-uncommitted cursors in sqlite3BtreeClearTable(). (CVS 2977)
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA1:950798326860de40926e82e10134f09e6ea86245
User & Date: danielk1977 2006-01-19 08:43:31
Context
2006-01-19
11:28
Fix the #ifdef around sqlite3OutstandingMallocs() in test1.c. (CVS 2978) check-in: 218c6184 user: drh tags: trunk
08:43
Account for read-uncommitted cursors in sqlite3BtreeClearTable(). (CVS 2977) check-in: 95079832 user: danielk1977 tags: trunk
07:18
Fix a bug with internally saving cursors open on index tables. Also increase coverage of util.c and btree.c. (CVS 2976) check-in: a628d84d user: danielk1977 tags: trunk
Changes
Hide Diffs Unified Diffs Ignore Whitespace Patch

Changes to src/btree.c.

5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
....
5533
5534
5535
5536
5537
5538
5539

5540
5541
5542





5543
5544

5545

5546
5547
5548
5549
5550
5551
5552
5553
5554



5555



5556
5557
5558
5559
5560
5561
5562
** 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: btree.c,v 1.301 2006/01/19 07:18:14 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:
**     "Sorting And Searching", pages 473-480. Addison-Wesley
**     Publishing Company, Reading, Massachusetts.
................................................................................
** read cursors on the table.  Open write cursors are moved to the
** root of the table.
*/
int sqlite3BtreeClearTable(Btree *p, int iTable){
  int rc;
  BtCursor *pCur;
  BtShared *pBt = p->pBt;

  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);
    }
  }
  rc = clearDatabasePage(pBt, (Pgno)iTable, 0, 0);
#if 0
  if( rc ){
    sqlite3BtreeRollback(pBt);
  }
#endif



  return rc;



}

/*
** Erase all information in a table and add the root of the table to
** the freelist.  Except, the root of the principle table (the one on
** page 1) is never added to the freelist.
**







|







 







>



>
>
>
>
>
|
|
>
|
>
|
|
|
<
<
<
<

<
>
>
>
|
>
>
>







5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
....
5533
5534
5535
5536
5537
5538
5539
5540
5541
5542
5543
5544
5545
5546
5547
5548
5549
5550
5551
5552
5553
5554
5555
5556




5557

5558
5559
5560
5561
5562
5563
5564
5565
5566
5567
5568
5569
5570
5571
** 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: 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
**
**     Donald E. Knuth, THE ART OF COMPUTER PROGRAMMING, Volume 3:
**     "Sorting And Searching", pages 473-480. Addison-Wesley
**     Publishing Company, Reading, Massachusetts.
................................................................................
** read cursors on the table.  Open write cursors are moved to the
** root of the table.
*/
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;
  }

  /* 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);
      }
    }




  }


  /* Save the position of all cursors open on this table */
  if( SQLITE_OK!=(rc = saveAllCursors(pBt, iTable, 0)) ){
    return rc;
  }

  return clearDatabasePage(pBt, (Pgno)iTable, 0, 0);
}

/*
** Erase all information in a table and add the root of the table to
** the freelist.  Except, the root of the principle table (the one on
** page 1) is never added to the freelist.
**

Changes to test/malloc5.test.

8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
...
188
189
190
191
192
193
194

195
196
197
198
199
200
201
202
203
204
205
206

207
208
209
210
211
212
213
#    May you share freely, never taking more than you give.
#
#***********************************************************************
#
# 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 $

#---------------------------------------------------------------------------
# NOTES ON EXPECTED BEHAVIOUR
#
#---------------------------------------------------------------------------


................................................................................
  execsql {BEGIN;}
  execsql {DELETE FROM abc;}
  for {set i 0} {$i < 10000} {incr i} {
    execsql "INSERT INTO abc VALUES($i, $i, '[string repeat X 100]');"
  }
  execsql {COMMIT;}
  set ::nMaxBytes [sqlite_malloc_outstanding -maxbytes]

  expr $::nMaxBytes > 1000000
} {1}
do_test malloc5-4.2 {
  sqlite3_release_memory
  sqlite_malloc_outstanding -clearmaxbytes
  sqlite3_soft_heap_limit 100000
  execsql {BEGIN;}
  for {set i 0} {$i < 10000} {incr i} {
    execsql "INSERT INTO abc VALUES($i, $i, '[string repeat X 100]');"
  }
  execsql {COMMIT;}
  set ::nMaxBytes [sqlite_malloc_outstanding -maxbytes]

  expr $::nMaxBytes <= 100000
} {1}
do_test malloc5-4.3 {
  # Check that the content of table abc is at least roughly as expected.
  execsql {
    SELECT count(*), sum(a), sum(b) FROM abc;
  }







|







 







>












>







8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
...
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
#    May you share freely, never taking more than you give.
#
#***********************************************************************
#
# 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.7 2006/01/19 08:43:32 danielk1977 Exp $

#---------------------------------------------------------------------------
# NOTES ON EXPECTED BEHAVIOUR
#
#---------------------------------------------------------------------------


................................................................................
  execsql {BEGIN;}
  execsql {DELETE FROM abc;}
  for {set i 0} {$i < 10000} {incr i} {
    execsql "INSERT INTO abc VALUES($i, $i, '[string repeat X 100]');"
  }
  execsql {COMMIT;}
  set ::nMaxBytes [sqlite_malloc_outstanding -maxbytes]
  if {$::nMaxBytes==""} {set ::nMaxBytes 1000001}
  expr $::nMaxBytes > 1000000
} {1}
do_test malloc5-4.2 {
  sqlite3_release_memory
  sqlite_malloc_outstanding -clearmaxbytes
  sqlite3_soft_heap_limit 100000
  execsql {BEGIN;}
  for {set i 0} {$i < 10000} {incr i} {
    execsql "INSERT INTO abc VALUES($i, $i, '[string repeat X 100]');"
  }
  execsql {COMMIT;}
  set ::nMaxBytes [sqlite_malloc_outstanding -maxbytes]
  if {$::nMaxBytes==""} {set ::nMaxBytes 0}
  expr $::nMaxBytes <= 100000
} {1}
do_test malloc5-4.3 {
  # Check that the content of table abc is at least roughly as expected.
  execsql {
    SELECT count(*), sum(a), sum(b) FROM abc;
  }

Changes to test/shared.test.

5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
..
56
57
58
59
60
61
62



63
64
65
66
67
68
69
#
#    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: shared.test,v 1.18 2006/01/19 07:18:15 danielk1977 Exp $

set testdir [file dirname $argv0]
source $testdir/tester.tcl
db close

ifcapable !shared_cache {
  finish_test
................................................................................
#             external locking protocol is still working.
# shared-3.*: Simple test of read-uncommitted mode.
# shared-4.*: Check that the schema is locked and unlocked correctly.
# shared-5.*: Test that creating/dropping schema items works when databases
#             are attached in different orders to different handles.
# shared-6.*: Locking, UNION ALL queries and sub-queries.
# shared-7.*: Autovacuum and shared-cache.



#

do_test shared-$av.1.1 {
  # Open a second database on the file test.db. It should use the same pager
  # cache and schema as the original connection. Verify that only 1 file is 
  # opened.
  sqlite3 db2 test.db







|







 







>
>
>







5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
..
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#
#    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: shared.test,v 1.19 2006/01/19 08:43:32 danielk1977 Exp $

set testdir [file dirname $argv0]
source $testdir/tester.tcl
db close

ifcapable !shared_cache {
  finish_test
................................................................................
#             external locking protocol is still working.
# shared-3.*: Simple test of read-uncommitted mode.
# shared-4.*: Check that the schema is locked and unlocked correctly.
# shared-5.*: Test that creating/dropping schema items works when databases
#             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 {
  # Open a second database on the file test.db. It should use the same pager
  # cache and schema as the original connection. Verify that only 1 file is 
  # opened.
  sqlite3 db2 test.db

Added test/shared2.test.











































































































































































>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
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