SQLite

Check-in [3e3b5e861a]
Login

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

Overview
Comment:Add some assert() statements to querySharedCacheTableLock(). (CVS 6421)
Downloads: Tarball | ZIP archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA1: 3e3b5e861aeff2e4ef568c422236fdf7fa22bed3
User & Date: danielk1977 2009-04-01 09:41:54.000
Context
2009-04-01
16:25
Remove an unreachable branch from allocateSpace() in btree.c. Add comments and asserts to the same function. (CVS 6422) (check-in: f8e15a542d user: danielk1977 tags: trunk)
09:41
Add some assert() statements to querySharedCacheTableLock(). (CVS 6421) (check-in: 3e3b5e861a user: danielk1977 tags: trunk)
07:38
add a sanity check to make sure the configure script and VERSION file are always in sync (CVS 6420) (check-in: 1b9da6d73f user: vapier tags: trunk)
Changes
Unified Diff Ignore Whitespace Patch
Changes to src/btree.c.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
/*
** 2004 April 6
**
** 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: btree.c,v 1.582 2009/03/30 18:50:05 danielk1977 Exp $
**
** This file implements a external (disk-based) database using BTrees.
** See the header comment on "btreeInt.h" for additional information.
** Including a description of file format and an overview of operation.
*/
#include "btreeInt.h"












|







1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
/*
** 2004 April 6
**
** 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: btree.c,v 1.583 2009/04/01 09:41:54 danielk1977 Exp $
**
** This file implements a external (disk-based) database using BTrees.
** See the header comment on "btreeInt.h" for additional information.
** Including a description of file format and an overview of operation.
*/
#include "btreeInt.h"

99
100
101
102
103
104
105







106
107
108
109
110
111
112
  BtShared *pBt = p->pBt;
  BtLock *pIter;

  assert( sqlite3BtreeHoldsMutex(p) );
  assert( eLock==READ_LOCK || eLock==WRITE_LOCK );
  assert( p->db!=0 );
  







  /* This is a no-op if the shared-cache is not enabled */
  if( !p->sharable ){
    return SQLITE_OK;
  }

  /* If some other connection is holding an exclusive lock, the
  ** requested lock may not be obtained.







>
>
>
>
>
>
>







99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
  BtShared *pBt = p->pBt;
  BtLock *pIter;

  assert( sqlite3BtreeHoldsMutex(p) );
  assert( eLock==READ_LOCK || eLock==WRITE_LOCK );
  assert( p->db!=0 );
  
  /* If requesting a write-lock, then the Btree must have an open write
  ** transaction on this file. And, obviously, for this to be so there 
  ** must be an open write transaction on the file itself.
  */
  assert( eLock==READ_LOCK || (p==pBt->pWriter && p->inTrans==TRANS_WRITE) );
  assert( eLock==READ_LOCK || pBt->inTransaction==TRANS_WRITE );
  
  /* This is a no-op if the shared-cache is not enabled */
  if( !p->sharable ){
    return SQLITE_OK;
  }

  /* If some other connection is holding an exclusive lock, the
  ** requested lock may not be obtained.
134
135
136
137
138
139
140











141
142
143
144
145
146
147
148
149
  */
  if( 
    0==(p->db->flags&SQLITE_ReadUncommitted) || 
    eLock==WRITE_LOCK ||
    iTab==MASTER_ROOT
  ){
    for(pIter=pBt->pLock; pIter; pIter=pIter->pNext){











      if( pIter->pBtree!=p && pIter->iTable==iTab && 
          (pIter->eLock!=eLock || eLock!=READ_LOCK) ){
        sqlite3ConnectionBlocked(p->db, pIter->pBtree->db);
        if( eLock==WRITE_LOCK ){
          assert( p==pBt->pWriter );
          pBt->isPending = 1;
        }
        return SQLITE_LOCKED_SHAREDCACHE;
      }







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







141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159

160
161
162
163
164
165
166
  */
  if( 
    0==(p->db->flags&SQLITE_ReadUncommitted) || 
    eLock==WRITE_LOCK ||
    iTab==MASTER_ROOT
  ){
    for(pIter=pBt->pLock; pIter; pIter=pIter->pNext){
      /* The condition (pIter->eLock!=eLock) in the following if(...) 
      ** statement is a simplification of:
      **
      **   (eLock==WRITE_LOCK || pIter->eLock==WRITE_LOCK)
      **
      ** since we know that if eLock==WRITE_LOCK, then no other connection
      ** may hold a WRITE_LOCK on any table in this file (since there can
      ** only be a single writer).
      */
      assert( pIter->eLock==READ_LOCK || pIter->eLock==WRITE_LOCK );
      assert( eLock==READ_LOCK || pIter->pBtree==p || pIter->eLock==READ_LOCK);
      if( pIter->pBtree!=p && pIter->iTable==iTab && pIter->eLock!=eLock ){

        sqlite3ConnectionBlocked(p->db, pIter->pBtree->db);
        if( eLock==WRITE_LOCK ){
          assert( p==pBt->pWriter );
          pBt->isPending = 1;
        }
        return SQLITE_LOCKED_SHAREDCACHE;
      }
Added test/shared6.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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# 2009 April 01
#
# 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: shared6.test,v 1.1 2009/04/01 09:41:54 danielk1977 Exp $

set testdir [file dirname $argv0]
source $testdir/tester.tcl
ifcapable !shared_cache { finish_test ; return }

do_test shared7-1.1.1 {
  execsql {
    CREATE TABLE t1(a, b);
    CREATE TABLE t2(c, d);
    CREATE TABLE t3(e, f);
  }
} {}
do_test shared7-1.1.2 {
  set ::enable_shared_cache [sqlite3_enable_shared_cache 1]
  sqlite3_enable_shared_cache
} {1}

do_test shared7-1.1.3 {
  sqlite3 db1 test.db
  sqlite3 db2 test.db
} {}

# Exclusive shared-cache locks. Test the following:
#
#   1.2.1: If [db1] has an exclusive lock, [db2] cannot read.
#   1.2.2: If [db1] has an exclusive lock, [db1] can read.
#   1.2.3: If [db1] has a non-exclusive write-lock, [db2] can read.
# 
do_test shared7-1.2.1 {
  execsql { SELECT * FROM t1 } db2    ;# Cache a compiled statement
  execsql { BEGIN EXCLUSIVE } db1
  catchsql { SELECT * FROM t1 } db2   ;# Execute the cached compiled statement
} {1 {database table is locked}}
do_test shared7-1.2.2 {
  execsql { SELECT * FROM t1 } db1
} {}
do_test shared7-1.2.3 {
  execsql {
    COMMIT;
    BEGIN;
    INSERT INTO t2 VALUES(3, 4);
  } db1
  execsql { SELECT * FROM t1 } db2
} {}
do_test shared7-1.2.X {
  execsql { COMMIT } db1
} {}

# Regular shared-cache locks. Verify the following:
#
#   1.3.1: If [db1] has a write-lock on t1, [db1] can read from t1.
#   1.3.2: If [db1] has a write-lock on t1, [db2] can read from t2.
#   1.3.3: If [db1] has a write-lock on t1, [db2] cannot read from t1.
#   1.3.4: If [db1] has a write-lock on t1, [db2] cannot write to t1.
#   1.3.5: If [db1] has a read-lock on t1, [db2] can read from t1.
#   1.3.6: If [db1] has a read-lock on t1, [db2] cannot write to t1.
#
do_test shared7-1.3.1 {
  execsql {
    BEGIN;
    INSERT INTO t1 VALUES(1, 2);
  } db1
  execsql { SELECT * FROM t1 } db1
} {1 2}
do_test shared7-1.3.2 {
  execsql { SELECT * FROM t2 } db2
} {3 4}
do_test shared7-1.3.3 {
  catchsql { SELECT * FROM t1 } db2
} {1 {database table is locked: t1}}
do_test shared7-1.3.4 {
  catchsql { INSERT INTO t2 VALUES(1, 2) } db2
} {1 {database table is locked}}
do_test shared7-1.3.5 {
  execsql {
    COMMIT;
    BEGIN;
    SELECT * FROM t1;
  } db1
  execsql { SELECT * FROM t1 } db2
} {1 2}
do_test shared7-1.3.5 {
  catchsql { INSERT INTO t1 VALUES(5, 6) } db2
} {1 {database table is locked: t1}}
do_test shared7-1.3.X {
  execsql { COMMIT } db1
} {}

# Read-uncommitted mode.
#
# For these tests, connection [db2] is in read-uncommitted mode.
#
#   1.4.1: If [db1] has a write-lock on t1, [db2] can still read from t1.
#   1.4.2: If [db1] has a write-lock on the db schema (sqlite_master table), 
#          [db2] cannot read from the schema.
#   1.4.3: If [db1] has a read-lock on t1, [db2] cannot write to t1.
#
do_test shared7-1.4.1 {
  execsql { PRAGMA read_uncommitted = 1 } db2
  execsql {
    BEGIN;
    INSERT INTO t1 VALUES(5, 6);
  } db1
  execsql { SELECT * FROM t1 } db2
} {1 2 5 6}
do_test shared7-1.4.2 {
  execsql { CREATE TABLE t4(a, b) } db1
  catchsql { SELECT * FROM t1 } db2
} {1 {database table is locked}}
do_test shared7-1.4.3 {
  execsql {
    COMMIT;
    BEGIN;
    SELECT * FROM t1;
  } db1
  catchsql { INSERT INTO t1 VALUES(7, 8) } db2
} {1 {database table is locked: t1}}

do_test shared7-1.X {
  db1 close
  db2 close
} {}

sqlite3_enable_shared_cache $::enable_shared_cache
finish_test