-C Parse\sforeign\skey\sconstraints\sand\spopulate\sinternal\sdata\sstructures\nappropriately.\s\sConstraints\sare\sstill\snot\senforced.\s(CVS\s738)
-D 2002-08-31T18:53:06
+C Relax\sthe\slocking\srequirements\son\sBTree\scursors.\s\sAny\snumber\sof\sread\sand\nwrite\scursors\scan\sbe\sopen\sat\sthe\ssame\stime\snow,\sbut\sa\swrite\scannot\soccur\nas\slong\sas\sone\sor\smore\sread\scursors\sare\sopen.\n\nBefore\sthis\schange,\sone\sor\smore\sread\scursors\scould\sbe\sopen\son\sa\stable,\nor\sa\ssingle\swrite\scursor,\sbut\snot\sboth.\s\sBoth\spolicies\shave\sthe\ssame\ndesirable\seffect:\sthey\sprevent\swrites\sto\sa\stable\swhile\sa\ssequential\sscan\nof\sthat\stable\sis\sunderway.\s\sBut\sthe\snew\spolicy\sis\sa\slittle\sless\srestrictive.\nBoth\spolicies\sprevent\san\sUPDATE\sfrom\soccurring\sinside\sa\sSELECT\s(which\sis\nwhat\swe\swant)\sbut\sthe\snew\spolicy\sallows\sa\sSELECT\sto\soccur\sinside\san\nUPDATE.\s(CVS\s739)
+D 2002-09-01T23:20:45
F Makefile.in 420fada882179cb72ffd07313f3fd693f9f06640
F Makefile.linux-gcc b86a99c493a5bfb402d1d9178dcdc4bd4b32f906
F README f1de682fbbd94899d50aca13d387d1b3fd3be2dd
F publish.sh a7a8d23e6525bd25d4f5ba9b0fc6edc107d94050
F spec.template 238f7db425a78dc1bb7682e56e3834c7270a3f5e
F sqlite.1 83f4a9d37bdf2b7ef079a82d54eaf2e3509ee6ea
-F src/btree.c 9e21606581a5a4a5b18ad304d7a4f433101f1538
+F src/btree.c 8024b87635c2adf133f153f1bb595125ec1c7d7b
F src/btree.h 0ca6c2631338df62e4f7894252d9347ae234eda9
F src/build.c 0116afe4f67687206364c4d1e88dc07aefc661de
F src/delete.c c9f59ee217e062eb9de7b64b76b5cfff42b2f028
F src/trigger.c c90a292a4bef25e478fd5deda6d300319be6a023
F src/update.c f07e6ed2c517c92871e54d3f5886d1cf56121b11
F src/util.c c70d5da5357e01b58392faebae3c3620c1d71f14
-F src/vdbe.c d4969d78fdd2408706a329c7d4554b9596d1be94
+F src/vdbe.c 4a1744c9054965ee34c034ed4673231124b571c9
F src/vdbe.h 7cfeb3aab6a901336532d93494cdedbddf30b7ec
F src/where.c 53959c9d94adaf93b409271815e26eafa6ddd515
F test/all.test efd958d048c70a3247997c482f0b33561f7759f0
F www/sqlite.tcl ae3dcfb077e53833b59d4fcc94d8a12c50a44098
F www/tclsqlite.tcl 1db15abeb446aad0caf0b95b8b9579720e4ea331
F www/vdbe.tcl 2013852c27a02a091d39a766bc87cff329f21218
-P 5f51e13d56a58d7c263043cae9898d796017a369
-R 5f57a541da1cc0eea6b8b3a2339bca70
+P 170711ca65dc894d0486b9d575edb8f1708250fb
+R 665b6d3bd425ef91563749e2f71e8997
U drh
-Z 53eaaf70a55382e9bd46cf1caade9346
+Z c368bcd74fab4b1fea02ec6dd259bce0
** May you share freely, never taking more than you give.
**
*************************************************************************
-** $Id: btree.c,v 1.71 2002/08/15 13:50:49 drh Exp $
+** $Id: btree.c,v 1.72 2002/09/01 23:20:45 drh Exp $
**
** This file implements a external (disk-based) database using BTrees.
** For a detailed discussion of BTrees, refer to
u8 inCkpt; /* True if there is a checkpoint on the transaction */
u8 readOnly; /* True if the underlying file is readonly */
u8 needSwab; /* Need to byte-swapping */
- Hash locks; /* Key: root page number. Data: lock count */
};
typedef Btree Bt;
struct BtCursor {
Btree *pBt; /* The Btree to which this cursor belongs */
BtCursor *pNext, *pPrev; /* Forms a linked list of all cursors */
+ BtCursor *pShared; /* Loop of cursors with the same root page */
Pgno pgnoRoot; /* The root page of this tree */
MemPage *pPage; /* Page that contains the entry */
int idx; /* Index of the entry in pPage->apCell[] */
pBt->pCursor = 0;
pBt->page1 = 0;
pBt->readOnly = sqlitepager_isreadonly(pBt->pPager);
- sqliteHashInit(&pBt->locks, SQLITE_HASH_INT, 0);
*ppBtree = pBt;
return SQLITE_OK;
}
sqliteBtreeCloseCursor(pBt->pCursor);
}
sqlitepager_close(pBt->pPager);
- sqliteHashClear(&pBt->locks);
sqliteFree(pBt);
return SQLITE_OK;
}
int sqliteBtreeBeginTrans(Btree *pBt){
int rc;
if( pBt->inTrans ) return SQLITE_ERROR;
+ if( pBt->readOnly ) return SQLITE_READONLY;
if( pBt->page1==0 ){
rc = lockBtree(pBt);
if( rc!=SQLITE_OK ){
return rc;
}
}
- if( pBt->readOnly ){
- rc = SQLITE_OK;
- }else{
- rc = sqlitepager_begin(pBt->page1);
- if( rc==SQLITE_OK ){
- rc = newDatabase(pBt);
- }
+ rc = sqlitepager_begin(pBt->page1);
+ if( rc==SQLITE_OK ){
+ rc = newDatabase(pBt);
}
if( rc==SQLITE_OK ){
pBt->inTrans = 1;
*/
int sqliteBtreeCommit(Btree *pBt){
int rc;
- if( pBt->inTrans==0 ) return SQLITE_ERROR;
rc = pBt->readOnly ? SQLITE_OK : sqlitepager_commit(pBt->pPager);
pBt->inTrans = 0;
pBt->inCkpt = 0;
int sqliteBtreeBeginCkpt(Btree *pBt){
int rc;
if( !pBt->inTrans || pBt->inCkpt ){
- return SQLITE_ERROR;
+ return pBt->readOnly ? SQLITE_READONLY : SQLITE_ERROR;
}
rc = pBt->readOnly ? SQLITE_OK : sqlitepager_ckpt_begin(pBt->pPager);
pBt->inCkpt = 1;
** the database file.
**
** If wrFlag==0, then the cursor can only be used for reading.
-** If wrFlag==1, then the cursor can be used for reading or writing.
-** A read/write cursor requires exclusive access to its table. There
-** cannot be two or more cursors open on the same table if any one of
-** cursors is a read/write cursor. But there can be two or more
-** read-only cursors open on the same table.
-**
+** If wrFlag==1, then the cursor can be used for reading or for
+** writing if other conditions for writing are also met. These
+** are the conditions that must be met in order for writing to
+** be allowed:
+**
+** 1: The cursor must have been opened with wrFlag==1
+**
+** 2: No other cursors may be open with wrFlag==0 on the same table
+**
+** 3: The database must be writable (not on read-only media)
+**
+** 4: There must be an active transaction.
+**
+** Condition 2 warrants further discussion. If any cursor is opened
+** on a table with wrFlag==0, that prevents all other cursors from
+** writing to that table. This is a kind of "read-lock". When a cursor
+** is opened with wrFlag==0 it is guaranteed that the table will not
+** change as long as the cursor is open. This allows the cursor to
+** do a sequential scan of the table without having to worry about
+** entries being inserted or deleted during the scan. Cursors should
+** be opened with wrFlag==0 only if this read-lock property is needed.
+** That is to say, cursors should be opened with wrFlag==0 only if they
+** intend to use the sqliteBtreeNext() system call. All other cursors
+** should be opened with wrFlag==1 even if they never really intend
+** to write.
+**
** No checking is done to make sure that page iTable really is the
** root page of a b-tree. If it is not, then the cursor acquired
** will not work correctly.
*/
int sqliteBtreeCursor(Btree *pBt, int iTable, int wrFlag, BtCursor **ppCur){
int rc;
- BtCursor *pCur;
- ptr nLock;
+ BtCursor *pCur, *pRing;
if( pBt->page1==0 ){
rc = lockBtree(pBt);
return rc;
}
}
- if( wrFlag && pBt->readOnly ){
- *ppCur = 0;
- return SQLITE_READONLY;
- }
pCur = sqliteMalloc( sizeof(*pCur) );
if( pCur==0 ){
rc = SQLITE_NOMEM;
if( rc!=SQLITE_OK ){
goto create_cursor_exception;
}
- nLock = (ptr)sqliteHashFind(&pBt->locks, 0, iTable);
- if( nLock<0 || (nLock>0 && wrFlag) ){
- rc = SQLITE_LOCKED;
- goto create_cursor_exception;
- }
- nLock = wrFlag ? -1 : nLock+1;
- sqliteHashInsert(&pBt->locks, 0, iTable, (void*)nLock);
pCur->pBt = pBt;
pCur->wrFlag = wrFlag;
pCur->idx = 0;
pCur->pNext->pPrev = pCur;
}
pCur->pPrev = 0;
+ pRing = pBt->pCursor;
+ while( pRing && pRing->pgnoRoot!=pCur->pgnoRoot ){ pRing = pRing->pNext; }
+ if( pRing ){
+ pCur->pShared = pRing->pShared;
+ pRing->pShared = pCur;
+ }else{
+ pCur->pShared = pCur;
+ }
pBt->pCursor = pCur;
*ppCur = pCur;
return SQLITE_OK;
** when the last cursor is closed.
*/
int sqliteBtreeCloseCursor(BtCursor *pCur){
- ptr nLock;
Btree *pBt = pCur->pBt;
if( pCur->pPrev ){
pCur->pPrev->pNext = pCur->pNext;
if( pCur->pPage ){
sqlitepager_unref(pCur->pPage);
}
+ if( pCur->pShared!=pCur ){
+ BtCursor *pRing = pCur->pShared;
+ while( pRing->pShared!=pCur ){ pRing = pRing->pShared; }
+ pRing->pShared = pCur->pShared;
+ }
unlockBtreeIfUnused(pBt);
- nLock = (ptr)sqliteHashFind(&pBt->locks, 0, pCur->pgnoRoot);
- assert( nLock!=0 || sqlite_malloc_failed );
- nLock = nLock<0 ? 0 : nLock-1;
- sqliteHashInsert(&pBt->locks, 0, pCur->pgnoRoot, (void*)nLock);
sqliteFree(pCur);
return SQLITE_OK;
}
return rc;
}
+/*
+** This routine checks all cursors that point to the same table
+** as pCur points to. If any of those cursors were opened with
+** wrFlag==0 then this routine returns SQLITE_LOCKED. If all
+** cursors point to the same table were opened with wrFlag==1
+** then this routine returns SQLITE_OK.
+**
+** In addition to checking for read-locks (where a read-lock
+** means a cursor opened with wrFlag==0) this routine also moves
+** all cursors other than pCur so that they are pointing to the
+** first Cell on root page. This is necessary because an insert
+** or delete might change the number of cells on a page or delete
+** a page entirely and we do not want to leave any cursors
+** pointing to non-existant pages or cells.
+*/
+static int checkReadLocks(BtCursor *pCur){
+ BtCursor *p;
+ assert( pCur->wrFlag );
+ for(p=pCur->pShared; p!=pCur; p=p->pShared){
+ assert( p );
+ assert( p->pgnoRoot==pCur->pgnoRoot );
+ if( p->wrFlag==0 ) return SQLITE_LOCKED;
+ if( sqlitepager_pagenumber(p->pPage)!=p->pgnoRoot ){
+ moveToRoot(p);
+ }
+ }
+ return SQLITE_OK;
+}
+
/*
** Insert a new record into the BTree. The key is given by (pKey,nKey)
** and the data is given by (pData,nData). The cursor is used only to
if( pCur->pPage==0 ){
return SQLITE_ABORT; /* A rollback destroyed this cursor */
}
- if( !pCur->pBt->inTrans || nKey+nData==0 ){
- return SQLITE_ERROR; /* Must start a transaction first */
+ if( !pBt->inTrans || nKey+nData==0 ){
+ /* Must start a transaction before doing an insert */
+ return pBt->readOnly ? SQLITE_READONLY : SQLITE_ERROR;
}
+ assert( !pBt->readOnly );
if( !pCur->wrFlag ){
return SQLITE_PERM; /* Cursor not open for writing */
}
+ if( checkReadLocks(pCur) ){
+ return SQLITE_LOCKED; /* The table pCur points to has a read lock */
+ }
rc = sqliteBtreeMoveto(pCur, pKey, nKey, &loc);
if( rc ) return rc;
pPage = pCur->pPage;
if( pCur->pPage==0 ){
return SQLITE_ABORT; /* A rollback destroyed this cursor */
}
- if( !pCur->pBt->inTrans ){
- return SQLITE_ERROR; /* Must start a transaction first */
+ if( !pBt->inTrans ){
+ /* Must start a transaction before doing a delete */
+ return pBt->readOnly ? SQLITE_READONLY : SQLITE_ERROR;
}
+ assert( !pBt->readOnly );
if( pCur->idx >= pPage->nCell ){
return SQLITE_ERROR; /* The cursor is not pointing to anything */
}
if( !pCur->wrFlag ){
return SQLITE_PERM; /* Did not open this cursor for writing */
}
+ if( checkReadLocks(pCur) ){
+ return SQLITE_LOCKED; /* The table pCur points to has a read lock */
+ }
rc = sqlitepager_write(pPage);
if( rc ) return rc;
pCell = pPage->apCell[pCur->idx];
Pgno pgnoRoot;
int rc;
if( !pBt->inTrans ){
- return SQLITE_ERROR; /* Must start a transaction first */
+ /* Must start a transaction first */
+ return pBt->readOnly ? SQLITE_READONLY : SQLITE_ERROR;
}
if( pBt->readOnly ){
return SQLITE_READONLY;
*/
int sqliteBtreeClearTable(Btree *pBt, int iTable){
int rc;
- ptr nLock;
+ BtCursor *pCur;
if( !pBt->inTrans ){
- return SQLITE_ERROR; /* Must start a transaction first */
+ return pBt->readOnly ? SQLITE_READONLY : SQLITE_ERROR;
}
- if( pBt->readOnly ){
- return SQLITE_READONLY;
- }
- nLock = (ptr)sqliteHashFind(&pBt->locks, 0, iTable);
- if( nLock ){
- return SQLITE_LOCKED;
+ 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);
if( rc ){
int sqliteBtreeDropTable(Btree *pBt, int iTable){
int rc;
MemPage *pPage;
+ BtCursor *pCur;
if( !pBt->inTrans ){
- return SQLITE_ERROR; /* Must start a transaction first */
+ return pBt->readOnly ? SQLITE_READONLY : SQLITE_ERROR;
}
- if( pBt->readOnly ){
- return SQLITE_READONLY;
+ for(pCur=pBt->pCursor; pCur; pCur=pCur->pNext){
+ if( pCur->pgnoRoot==(Pgno)iTable ){
+ return SQLITE_LOCKED; /* Cannot drop a table that has a cursor */
+ }
}
rc = sqlitepager_get(pBt->pPager, (Pgno)iTable, (void**)&pPage);
if( rc ) return rc;
PageOne *pP1;
int rc, i;
if( !pBt->inTrans ){
- return SQLITE_ERROR; /* Must start a transaction first */
- }
- if( pBt->readOnly ){
- return SQLITE_READONLY;
+ return pBt->readOnly ? SQLITE_READONLY : SQLITE_ERROR;
}
pP1 = pBt->page1;
rc = sqlitepager_write(pP1);