blob: 3732dc6b627dd812eff61365ff710cd2d9bca147 [file] [log] [blame]
drha059ad02001-04-17 20:09:11 +00001/*
drh9e572e62004-04-23 23:43:10 +00002** 2004 April 6
drha059ad02001-04-17 20:09:11 +00003**
drhb19a2bc2001-09-16 00:13:26 +00004** The author disclaims copyright to this source code. In place of
5** a legal notice, here is a blessing:
drha059ad02001-04-17 20:09:11 +00006**
drhb19a2bc2001-09-16 00:13:26 +00007** May you do good and not evil.
8** May you find forgiveness for yourself and forgive others.
9** May you share freely, never taking more than you give.
drha059ad02001-04-17 20:09:11 +000010**
11*************************************************************************
danielk197745d68822009-01-16 16:23:38 +000012** $Id: btree.c,v 1.560 2009/01/16 16:23:38 danielk1977 Exp $
drh8b2f49b2001-06-08 00:21:52 +000013**
14** This file implements a external (disk-based) database using BTrees.
drha3152892007-05-05 11:48:52 +000015** See the header comment on "btreeInt.h" for additional information.
16** Including a description of file format and an overview of operation.
drha059ad02001-04-17 20:09:11 +000017*/
drha3152892007-05-05 11:48:52 +000018#include "btreeInt.h"
paulb95a8862003-04-01 21:16:41 +000019
drh8c42ca92001-06-22 19:15:00 +000020/*
drha3152892007-05-05 11:48:52 +000021** The header string that appears at the beginning of every
22** SQLite database.
drh556b2a22005-06-14 16:04:05 +000023*/
drh556b2a22005-06-14 16:04:05 +000024static const char zMagicHeader[] = SQLITE_FILE_HEADER;
drh08ed44e2001-04-29 23:32:55 +000025
drh8c42ca92001-06-22 19:15:00 +000026/*
drha3152892007-05-05 11:48:52 +000027** Set this global variable to 1 to enable tracing using the TRACE
28** macro.
drh615ae552005-01-16 23:21:00 +000029*/
drhe8f52c52008-07-12 14:52:20 +000030#if 0
mlcreech3a00f902008-03-04 17:45:01 +000031int sqlite3BtreeTrace=0; /* True to enable tracing */
drhe8f52c52008-07-12 14:52:20 +000032# define TRACE(X) if(sqlite3BtreeTrace){printf X;fflush(stdout);}
33#else
34# define TRACE(X)
drh615ae552005-01-16 23:21:00 +000035#endif
drh615ae552005-01-16 23:21:00 +000036
drh86f8c192007-08-22 00:39:19 +000037
38
drhe53831d2007-08-17 01:14:38 +000039#ifndef SQLITE_OMIT_SHARED_CACHE
40/*
danielk1977502b4e02008-09-02 14:07:24 +000041** A list of BtShared objects that are eligible for participation
42** in shared cache. This variable has file scope during normal builds,
43** but the test harness needs to access it so we make it global for
44** test builds.
drhe53831d2007-08-17 01:14:38 +000045*/
46#ifdef SQLITE_TEST
drh78f82d12008-09-02 00:52:52 +000047BtShared *SQLITE_WSD sqlite3SharedCacheList = 0;
drhe53831d2007-08-17 01:14:38 +000048#else
drh78f82d12008-09-02 00:52:52 +000049static BtShared *SQLITE_WSD sqlite3SharedCacheList = 0;
drhe53831d2007-08-17 01:14:38 +000050#endif
drhe53831d2007-08-17 01:14:38 +000051#endif /* SQLITE_OMIT_SHARED_CACHE */
52
53#ifndef SQLITE_OMIT_SHARED_CACHE
54/*
55** Enable or disable the shared pager and schema features.
56**
57** This routine has no effect on existing database connections.
58** The shared cache setting effects only future calls to
59** sqlite3_open(), sqlite3_open16(), or sqlite3_open_v2().
60*/
61int sqlite3_enable_shared_cache(int enable){
danielk1977502b4e02008-09-02 14:07:24 +000062 sqlite3GlobalConfig.sharedCacheEnabled = enable;
drhe53831d2007-08-17 01:14:38 +000063 return SQLITE_OK;
64}
65#endif
66
drhd677b3d2007-08-20 22:48:41 +000067
drh615ae552005-01-16 23:21:00 +000068/*
drh66cbd152004-09-01 16:12:25 +000069** Forward declaration
70*/
danielk19773588ceb2008-06-10 17:30:26 +000071static int checkReadLocks(Btree*, Pgno, BtCursor*, i64);
drh66cbd152004-09-01 16:12:25 +000072
danielk1977aef0bf62005-12-30 16:28:01 +000073
74#ifdef SQLITE_OMIT_SHARED_CACHE
75 /*
76 ** The functions queryTableLock(), lockTable() and unlockAllTables()
77 ** manipulate entries in the BtShared.pLock linked list used to store
78 ** shared-cache table level locks. If the library is compiled with the
79 ** shared-cache feature disabled, then there is only ever one user
danielk1977da184232006-01-05 11:34:32 +000080 ** of each BtShared structure and so this locking is not necessary.
81 ** So define the lock related functions as no-ops.
danielk1977aef0bf62005-12-30 16:28:01 +000082 */
83 #define queryTableLock(a,b,c) SQLITE_OK
84 #define lockTable(a,b,c) SQLITE_OK
danielk1977da184232006-01-05 11:34:32 +000085 #define unlockAllTables(a)
drhe53831d2007-08-17 01:14:38 +000086#endif
danielk1977aef0bf62005-12-30 16:28:01 +000087
drhe53831d2007-08-17 01:14:38 +000088#ifndef SQLITE_OMIT_SHARED_CACHE
danielk1977da184232006-01-05 11:34:32 +000089/*
danielk1977aef0bf62005-12-30 16:28:01 +000090** Query to see if btree handle p may obtain a lock of type eLock
91** (READ_LOCK or WRITE_LOCK) on the table with root-page iTab. Return
92** SQLITE_OK if the lock may be obtained (by calling lockTable()), or
danielk1977c87d34d2006-01-06 13:00:28 +000093** SQLITE_LOCKED if not.
danielk1977aef0bf62005-12-30 16:28:01 +000094*/
95static int queryTableLock(Btree *p, Pgno iTab, u8 eLock){
96 BtShared *pBt = p->pBt;
97 BtLock *pIter;
98
drh1fee73e2007-08-29 04:00:57 +000099 assert( sqlite3BtreeHoldsMutex(p) );
drhfa67c3c2008-07-11 02:21:40 +0000100 assert( eLock==READ_LOCK || eLock==WRITE_LOCK );
101 assert( p->db!=0 );
drhd677b3d2007-08-20 22:48:41 +0000102
danielk1977da184232006-01-05 11:34:32 +0000103 /* This is a no-op if the shared-cache is not enabled */
drhe53831d2007-08-17 01:14:38 +0000104 if( !p->sharable ){
danielk1977da184232006-01-05 11:34:32 +0000105 return SQLITE_OK;
106 }
107
danielk1977641b0f42007-12-21 04:47:25 +0000108 /* If some other connection is holding an exclusive lock, the
109 ** requested lock may not be obtained.
110 */
111 if( pBt->pExclusive && pBt->pExclusive!=p ){
112 return SQLITE_LOCKED;
113 }
114
danielk1977da184232006-01-05 11:34:32 +0000115 /* This (along with lockTable()) is where the ReadUncommitted flag is
116 ** dealt with. If the caller is querying for a read-lock and the flag is
117 ** set, it is unconditionally granted - even if there are write-locks
118 ** on the table. If a write-lock is requested, the ReadUncommitted flag
119 ** is not considered.
120 **
121 ** In function lockTable(), if a read-lock is demanded and the
122 ** ReadUncommitted flag is set, no entry is added to the locks list
123 ** (BtShared.pLock).
124 **
125 ** To summarize: If the ReadUncommitted flag is set, then read cursors do
126 ** not create or respect table locks. The locking procedure for a
127 ** write-cursor does not change.
128 */
129 if(
drhe5fe6902007-12-07 18:55:28 +0000130 0==(p->db->flags&SQLITE_ReadUncommitted) ||
danielk1977da184232006-01-05 11:34:32 +0000131 eLock==WRITE_LOCK ||
drh47ded162006-01-06 01:42:58 +0000132 iTab==MASTER_ROOT
danielk1977da184232006-01-05 11:34:32 +0000133 ){
134 for(pIter=pBt->pLock; pIter; pIter=pIter->pNext){
135 if( pIter->pBtree!=p && pIter->iTable==iTab &&
136 (pIter->eLock!=eLock || eLock!=READ_LOCK) ){
danielk1977c87d34d2006-01-06 13:00:28 +0000137 return SQLITE_LOCKED;
danielk1977da184232006-01-05 11:34:32 +0000138 }
danielk1977aef0bf62005-12-30 16:28:01 +0000139 }
140 }
141 return SQLITE_OK;
142}
drhe53831d2007-08-17 01:14:38 +0000143#endif /* !SQLITE_OMIT_SHARED_CACHE */
danielk1977aef0bf62005-12-30 16:28:01 +0000144
drhe53831d2007-08-17 01:14:38 +0000145#ifndef SQLITE_OMIT_SHARED_CACHE
danielk1977aef0bf62005-12-30 16:28:01 +0000146/*
147** Add a lock on the table with root-page iTable to the shared-btree used
148** by Btree handle p. Parameter eLock must be either READ_LOCK or
149** WRITE_LOCK.
150**
151** SQLITE_OK is returned if the lock is added successfully. SQLITE_BUSY and
152** SQLITE_NOMEM may also be returned.
153*/
154static int lockTable(Btree *p, Pgno iTable, u8 eLock){
155 BtShared *pBt = p->pBt;
156 BtLock *pLock = 0;
157 BtLock *pIter;
158
drh1fee73e2007-08-29 04:00:57 +0000159 assert( sqlite3BtreeHoldsMutex(p) );
drhfa67c3c2008-07-11 02:21:40 +0000160 assert( eLock==READ_LOCK || eLock==WRITE_LOCK );
161 assert( p->db!=0 );
drhd677b3d2007-08-20 22:48:41 +0000162
danielk1977da184232006-01-05 11:34:32 +0000163 /* This is a no-op if the shared-cache is not enabled */
drhe53831d2007-08-17 01:14:38 +0000164 if( !p->sharable ){
danielk1977da184232006-01-05 11:34:32 +0000165 return SQLITE_OK;
166 }
167
danielk1977aef0bf62005-12-30 16:28:01 +0000168 assert( SQLITE_OK==queryTableLock(p, iTable, eLock) );
169
danielk1977da184232006-01-05 11:34:32 +0000170 /* If the read-uncommitted flag is set and a read-lock is requested,
171 ** return early without adding an entry to the BtShared.pLock list. See
172 ** comment in function queryTableLock() for more info on handling
173 ** the ReadUncommitted flag.
174 */
175 if(
drhe5fe6902007-12-07 18:55:28 +0000176 (p->db->flags&SQLITE_ReadUncommitted) &&
danielk1977da184232006-01-05 11:34:32 +0000177 (eLock==READ_LOCK) &&
drh47ded162006-01-06 01:42:58 +0000178 iTable!=MASTER_ROOT
danielk1977da184232006-01-05 11:34:32 +0000179 ){
180 return SQLITE_OK;
181 }
182
danielk1977aef0bf62005-12-30 16:28:01 +0000183 /* First search the list for an existing lock on this table. */
184 for(pIter=pBt->pLock; pIter; pIter=pIter->pNext){
185 if( pIter->iTable==iTable && pIter->pBtree==p ){
186 pLock = pIter;
187 break;
188 }
189 }
190
191 /* If the above search did not find a BtLock struct associating Btree p
192 ** with table iTable, allocate one and link it into the list.
193 */
194 if( !pLock ){
drh17435752007-08-16 04:30:38 +0000195 pLock = (BtLock *)sqlite3MallocZero(sizeof(BtLock));
danielk1977aef0bf62005-12-30 16:28:01 +0000196 if( !pLock ){
197 return SQLITE_NOMEM;
198 }
199 pLock->iTable = iTable;
200 pLock->pBtree = p;
201 pLock->pNext = pBt->pLock;
202 pBt->pLock = pLock;
203 }
204
205 /* Set the BtLock.eLock variable to the maximum of the current lock
206 ** and the requested lock. This means if a write-lock was already held
207 ** and a read-lock requested, we don't incorrectly downgrade the lock.
208 */
209 assert( WRITE_LOCK>READ_LOCK );
danielk19775118b912005-12-30 16:31:53 +0000210 if( eLock>pLock->eLock ){
211 pLock->eLock = eLock;
212 }
danielk1977aef0bf62005-12-30 16:28:01 +0000213
214 return SQLITE_OK;
215}
drhe53831d2007-08-17 01:14:38 +0000216#endif /* !SQLITE_OMIT_SHARED_CACHE */
danielk1977aef0bf62005-12-30 16:28:01 +0000217
drhe53831d2007-08-17 01:14:38 +0000218#ifndef SQLITE_OMIT_SHARED_CACHE
danielk1977aef0bf62005-12-30 16:28:01 +0000219/*
220** Release all the table locks (locks obtained via calls to the lockTable()
221** procedure) held by Btree handle p.
222*/
223static void unlockAllTables(Btree *p){
danielk1977641b0f42007-12-21 04:47:25 +0000224 BtShared *pBt = p->pBt;
225 BtLock **ppIter = &pBt->pLock;
danielk1977da184232006-01-05 11:34:32 +0000226
drh1fee73e2007-08-29 04:00:57 +0000227 assert( sqlite3BtreeHoldsMutex(p) );
drhe53831d2007-08-17 01:14:38 +0000228 assert( p->sharable || 0==*ppIter );
danielk1977da184232006-01-05 11:34:32 +0000229
danielk1977aef0bf62005-12-30 16:28:01 +0000230 while( *ppIter ){
231 BtLock *pLock = *ppIter;
danielk1977641b0f42007-12-21 04:47:25 +0000232 assert( pBt->pExclusive==0 || pBt->pExclusive==pLock->pBtree );
danielk1977aef0bf62005-12-30 16:28:01 +0000233 if( pLock->pBtree==p ){
234 *ppIter = pLock->pNext;
drh17435752007-08-16 04:30:38 +0000235 sqlite3_free(pLock);
danielk1977aef0bf62005-12-30 16:28:01 +0000236 }else{
237 ppIter = &pLock->pNext;
238 }
239 }
danielk1977641b0f42007-12-21 04:47:25 +0000240
241 if( pBt->pExclusive==p ){
242 pBt->pExclusive = 0;
243 }
danielk1977aef0bf62005-12-30 16:28:01 +0000244}
245#endif /* SQLITE_OMIT_SHARED_CACHE */
246
drh980b1a72006-08-16 16:42:48 +0000247static void releasePage(MemPage *pPage); /* Forward reference */
248
drh1fee73e2007-08-29 04:00:57 +0000249/*
250** Verify that the cursor holds a mutex on the BtShared
251*/
252#ifndef NDEBUG
253static int cursorHoldsMutex(BtCursor *p){
drhff0587c2007-08-29 17:43:19 +0000254 return sqlite3_mutex_held(p->pBt->mutex);
drh1fee73e2007-08-29 04:00:57 +0000255}
256#endif
257
258
danielk197792d4d7a2007-05-04 12:05:56 +0000259#ifndef SQLITE_OMIT_INCRBLOB
260/*
261** Invalidate the overflow page-list cache for cursor pCur, if any.
262*/
263static void invalidateOverflowCache(BtCursor *pCur){
drh1fee73e2007-08-29 04:00:57 +0000264 assert( cursorHoldsMutex(pCur) );
drh17435752007-08-16 04:30:38 +0000265 sqlite3_free(pCur->aOverflow);
danielk197792d4d7a2007-05-04 12:05:56 +0000266 pCur->aOverflow = 0;
267}
268
269/*
270** Invalidate the overflow page-list cache for all cursors opened
271** on the shared btree structure pBt.
272*/
273static void invalidateAllOverflowCache(BtShared *pBt){
274 BtCursor *p;
drh1fee73e2007-08-29 04:00:57 +0000275 assert( sqlite3_mutex_held(pBt->mutex) );
danielk197792d4d7a2007-05-04 12:05:56 +0000276 for(p=pBt->pCursor; p; p=p->pNext){
277 invalidateOverflowCache(p);
278 }
279}
280#else
281 #define invalidateOverflowCache(x)
282 #define invalidateAllOverflowCache(x)
283#endif
284
drh980b1a72006-08-16 16:42:48 +0000285/*
286** Save the current cursor position in the variables BtCursor.nKey
287** and BtCursor.pKey. The cursor's state is set to CURSOR_REQUIRESEEK.
288*/
289static int saveCursorPosition(BtCursor *pCur){
290 int rc;
291
292 assert( CURSOR_VALID==pCur->eState );
293 assert( 0==pCur->pKey );
drh1fee73e2007-08-29 04:00:57 +0000294 assert( cursorHoldsMutex(pCur) );
drh980b1a72006-08-16 16:42:48 +0000295
296 rc = sqlite3BtreeKeySize(pCur, &pCur->nKey);
297
298 /* If this is an intKey table, then the above call to BtreeKeySize()
299 ** stores the integer key in pCur->nKey. In this case this value is
300 ** all that is required. Otherwise, if pCur is not open on an intKey
301 ** table, then malloc space for and store the pCur->nKey bytes of key
302 ** data.
303 */
danielk197771d5d2c2008-09-29 11:49:47 +0000304 if( rc==SQLITE_OK && 0==pCur->apPage[0]->intKey){
drhf49661a2008-12-10 16:45:50 +0000305 void *pKey = sqlite3Malloc( (int)pCur->nKey );
drh980b1a72006-08-16 16:42:48 +0000306 if( pKey ){
drhf49661a2008-12-10 16:45:50 +0000307 rc = sqlite3BtreeKey(pCur, 0, (int)pCur->nKey, pKey);
drh980b1a72006-08-16 16:42:48 +0000308 if( rc==SQLITE_OK ){
309 pCur->pKey = pKey;
310 }else{
drh17435752007-08-16 04:30:38 +0000311 sqlite3_free(pKey);
drh980b1a72006-08-16 16:42:48 +0000312 }
313 }else{
314 rc = SQLITE_NOMEM;
315 }
316 }
danielk197771d5d2c2008-09-29 11:49:47 +0000317 assert( !pCur->apPage[0]->intKey || !pCur->pKey );
drh980b1a72006-08-16 16:42:48 +0000318
319 if( rc==SQLITE_OK ){
danielk197771d5d2c2008-09-29 11:49:47 +0000320 int i;
321 for(i=0; i<=pCur->iPage; i++){
322 releasePage(pCur->apPage[i]);
323 pCur->apPage[i] = 0;
324 }
325 pCur->iPage = -1;
drh980b1a72006-08-16 16:42:48 +0000326 pCur->eState = CURSOR_REQUIRESEEK;
327 }
328
danielk197792d4d7a2007-05-04 12:05:56 +0000329 invalidateOverflowCache(pCur);
drh980b1a72006-08-16 16:42:48 +0000330 return rc;
331}
332
333/*
334** Save the positions of all cursors except pExcept open on the table
335** with root-page iRoot. Usually, this is called just before cursor
336** pExcept is used to modify the table (BtreeDelete() or BtreeInsert()).
337*/
338static int saveAllCursors(BtShared *pBt, Pgno iRoot, BtCursor *pExcept){
339 BtCursor *p;
drh1fee73e2007-08-29 04:00:57 +0000340 assert( sqlite3_mutex_held(pBt->mutex) );
drhd0679ed2007-08-28 22:24:34 +0000341 assert( pExcept==0 || pExcept->pBt==pBt );
drh980b1a72006-08-16 16:42:48 +0000342 for(p=pBt->pCursor; p; p=p->pNext){
343 if( p!=pExcept && (0==iRoot || p->pgnoRoot==iRoot) &&
344 p->eState==CURSOR_VALID ){
345 int rc = saveCursorPosition(p);
346 if( SQLITE_OK!=rc ){
347 return rc;
348 }
349 }
350 }
351 return SQLITE_OK;
352}
353
354/*
drhbf700f32007-03-31 02:36:44 +0000355** Clear the current cursor position.
356*/
danielk1977be51a652008-10-08 17:58:48 +0000357void sqlite3BtreeClearCursor(BtCursor *pCur){
drh1fee73e2007-08-29 04:00:57 +0000358 assert( cursorHoldsMutex(pCur) );
drh17435752007-08-16 04:30:38 +0000359 sqlite3_free(pCur->pKey);
drhbf700f32007-03-31 02:36:44 +0000360 pCur->pKey = 0;
361 pCur->eState = CURSOR_INVALID;
362}
363
364/*
drh980b1a72006-08-16 16:42:48 +0000365** Restore the cursor to the position it was in (or as close to as possible)
366** when saveCursorPosition() was called. Note that this call deletes the
367** saved position info stored by saveCursorPosition(), so there can be
drha3460582008-07-11 21:02:53 +0000368** at most one effective restoreCursorPosition() call after each
drh980b1a72006-08-16 16:42:48 +0000369** saveCursorPosition().
drh980b1a72006-08-16 16:42:48 +0000370*/
drha3460582008-07-11 21:02:53 +0000371int sqlite3BtreeRestoreCursorPosition(BtCursor *pCur){
drhbf700f32007-03-31 02:36:44 +0000372 int rc;
drh1fee73e2007-08-29 04:00:57 +0000373 assert( cursorHoldsMutex(pCur) );
drhfb982642007-08-30 01:19:59 +0000374 assert( pCur->eState>=CURSOR_REQUIRESEEK );
375 if( pCur->eState==CURSOR_FAULT ){
376 return pCur->skip;
377 }
drh980b1a72006-08-16 16:42:48 +0000378 pCur->eState = CURSOR_INVALID;
drhe63d9992008-08-13 19:11:48 +0000379 rc = sqlite3BtreeMoveto(pCur, pCur->pKey, pCur->nKey, 0, &pCur->skip);
drh980b1a72006-08-16 16:42:48 +0000380 if( rc==SQLITE_OK ){
drh17435752007-08-16 04:30:38 +0000381 sqlite3_free(pCur->pKey);
drh980b1a72006-08-16 16:42:48 +0000382 pCur->pKey = 0;
drhbf700f32007-03-31 02:36:44 +0000383 assert( pCur->eState==CURSOR_VALID || pCur->eState==CURSOR_INVALID );
drh980b1a72006-08-16 16:42:48 +0000384 }
385 return rc;
386}
387
drha3460582008-07-11 21:02:53 +0000388#define restoreCursorPosition(p) \
drhfb982642007-08-30 01:19:59 +0000389 (p->eState>=CURSOR_REQUIRESEEK ? \
drha3460582008-07-11 21:02:53 +0000390 sqlite3BtreeRestoreCursorPosition(p) : \
drh16a9b832007-05-05 18:39:25 +0000391 SQLITE_OK)
drh980b1a72006-08-16 16:42:48 +0000392
drha3460582008-07-11 21:02:53 +0000393/*
394** Determine whether or not a cursor has moved from the position it
drhdfe88ec2008-11-03 20:55:06 +0000395** was last placed at. Cursors can move when the row they are pointing
drha3460582008-07-11 21:02:53 +0000396** at is deleted out from under them.
397**
398** This routine returns an error code if something goes wrong. The
399** integer *pHasMoved is set to one if the cursor has moved and 0 if not.
400*/
401int sqlite3BtreeCursorHasMoved(BtCursor *pCur, int *pHasMoved){
402 int rc;
403
404 rc = restoreCursorPosition(pCur);
405 if( rc ){
406 *pHasMoved = 1;
407 return rc;
408 }
409 if( pCur->eState!=CURSOR_VALID || pCur->skip!=0 ){
410 *pHasMoved = 1;
411 }else{
412 *pHasMoved = 0;
413 }
414 return SQLITE_OK;
415}
416
danielk1977599fcba2004-11-08 07:13:13 +0000417#ifndef SQLITE_OMIT_AUTOVACUUM
danielk1977afcdd022004-10-31 16:25:42 +0000418/*
drha3152892007-05-05 11:48:52 +0000419** Given a page number of a regular database page, return the page
420** number for the pointer-map page that contains the entry for the
421** input page number.
danielk1977afcdd022004-10-31 16:25:42 +0000422*/
danielk1977266664d2006-02-10 08:24:21 +0000423static Pgno ptrmapPageno(BtShared *pBt, Pgno pgno){
danielk197789d40042008-11-17 14:20:56 +0000424 int nPagesPerMapPage;
425 Pgno iPtrMap, ret;
drh1fee73e2007-08-29 04:00:57 +0000426 assert( sqlite3_mutex_held(pBt->mutex) );
drhd677b3d2007-08-20 22:48:41 +0000427 nPagesPerMapPage = (pBt->usableSize/5)+1;
428 iPtrMap = (pgno-2)/nPagesPerMapPage;
429 ret = (iPtrMap*nPagesPerMapPage) + 2;
danielk1977266664d2006-02-10 08:24:21 +0000430 if( ret==PENDING_BYTE_PAGE(pBt) ){
431 ret++;
432 }
433 return ret;
434}
danielk1977a19df672004-11-03 11:37:07 +0000435
danielk1977afcdd022004-10-31 16:25:42 +0000436/*
danielk1977afcdd022004-10-31 16:25:42 +0000437** Write an entry into the pointer map.
danielk1977687566d2004-11-02 12:56:41 +0000438**
439** This routine updates the pointer map entry for page number 'key'
440** so that it maps to type 'eType' and parent page number 'pgno'.
441** An error code is returned if something goes wrong, otherwise SQLITE_OK.
danielk1977afcdd022004-10-31 16:25:42 +0000442*/
danielk1977aef0bf62005-12-30 16:28:01 +0000443static int ptrmapPut(BtShared *pBt, Pgno key, u8 eType, Pgno parent){
danielk19773b8a05f2007-03-19 17:44:26 +0000444 DbPage *pDbPage; /* The pointer map page */
445 u8 *pPtrmap; /* The pointer map data */
446 Pgno iPtrmap; /* The pointer map page number */
447 int offset; /* Offset in pointer map page */
danielk1977afcdd022004-10-31 16:25:42 +0000448 int rc;
449
drh1fee73e2007-08-29 04:00:57 +0000450 assert( sqlite3_mutex_held(pBt->mutex) );
danielk1977266664d2006-02-10 08:24:21 +0000451 /* The master-journal page number must never be used as a pointer map page */
452 assert( 0==PTRMAP_ISPAGE(pBt, PENDING_BYTE_PAGE(pBt)) );
453
danielk1977ac11ee62005-01-15 12:45:51 +0000454 assert( pBt->autoVacuum );
danielk1977fdb7cdb2005-01-17 02:12:18 +0000455 if( key==0 ){
drh49285702005-09-17 15:20:26 +0000456 return SQLITE_CORRUPT_BKPT;
danielk1977fdb7cdb2005-01-17 02:12:18 +0000457 }
danielk1977266664d2006-02-10 08:24:21 +0000458 iPtrmap = PTRMAP_PAGENO(pBt, key);
danielk19773b8a05f2007-03-19 17:44:26 +0000459 rc = sqlite3PagerGet(pBt->pPager, iPtrmap, &pDbPage);
danielk1977687566d2004-11-02 12:56:41 +0000460 if( rc!=SQLITE_OK ){
danielk1977afcdd022004-10-31 16:25:42 +0000461 return rc;
462 }
danielk19778c666b12008-07-18 09:34:57 +0000463 offset = PTRMAP_PTROFFSET(iPtrmap, key);
danielk19773b8a05f2007-03-19 17:44:26 +0000464 pPtrmap = (u8 *)sqlite3PagerGetData(pDbPage);
danielk1977afcdd022004-10-31 16:25:42 +0000465
drh615ae552005-01-16 23:21:00 +0000466 if( eType!=pPtrmap[offset] || get4byte(&pPtrmap[offset+1])!=parent ){
467 TRACE(("PTRMAP_UPDATE: %d->(%d,%d)\n", key, eType, parent));
danielk19773b8a05f2007-03-19 17:44:26 +0000468 rc = sqlite3PagerWrite(pDbPage);
danielk19775558a8a2005-01-17 07:53:44 +0000469 if( rc==SQLITE_OK ){
470 pPtrmap[offset] = eType;
471 put4byte(&pPtrmap[offset+1], parent);
danielk1977afcdd022004-10-31 16:25:42 +0000472 }
danielk1977afcdd022004-10-31 16:25:42 +0000473 }
474
danielk19773b8a05f2007-03-19 17:44:26 +0000475 sqlite3PagerUnref(pDbPage);
danielk19775558a8a2005-01-17 07:53:44 +0000476 return rc;
danielk1977afcdd022004-10-31 16:25:42 +0000477}
478
479/*
480** Read an entry from the pointer map.
danielk1977687566d2004-11-02 12:56:41 +0000481**
482** This routine retrieves the pointer map entry for page 'key', writing
483** the type and parent page number to *pEType and *pPgno respectively.
484** An error code is returned if something goes wrong, otherwise SQLITE_OK.
danielk1977afcdd022004-10-31 16:25:42 +0000485*/
danielk1977aef0bf62005-12-30 16:28:01 +0000486static int ptrmapGet(BtShared *pBt, Pgno key, u8 *pEType, Pgno *pPgno){
danielk19773b8a05f2007-03-19 17:44:26 +0000487 DbPage *pDbPage; /* The pointer map page */
danielk1977afcdd022004-10-31 16:25:42 +0000488 int iPtrmap; /* Pointer map page index */
489 u8 *pPtrmap; /* Pointer map page data */
490 int offset; /* Offset of entry in pointer map */
491 int rc;
492
drh1fee73e2007-08-29 04:00:57 +0000493 assert( sqlite3_mutex_held(pBt->mutex) );
drhd677b3d2007-08-20 22:48:41 +0000494
danielk1977266664d2006-02-10 08:24:21 +0000495 iPtrmap = PTRMAP_PAGENO(pBt, key);
danielk19773b8a05f2007-03-19 17:44:26 +0000496 rc = sqlite3PagerGet(pBt->pPager, iPtrmap, &pDbPage);
danielk1977afcdd022004-10-31 16:25:42 +0000497 if( rc!=0 ){
498 return rc;
499 }
danielk19773b8a05f2007-03-19 17:44:26 +0000500 pPtrmap = (u8 *)sqlite3PagerGetData(pDbPage);
danielk1977afcdd022004-10-31 16:25:42 +0000501
danielk19778c666b12008-07-18 09:34:57 +0000502 offset = PTRMAP_PTROFFSET(iPtrmap, key);
drh43617e92006-03-06 20:55:46 +0000503 assert( pEType!=0 );
504 *pEType = pPtrmap[offset];
danielk1977687566d2004-11-02 12:56:41 +0000505 if( pPgno ) *pPgno = get4byte(&pPtrmap[offset+1]);
danielk1977afcdd022004-10-31 16:25:42 +0000506
danielk19773b8a05f2007-03-19 17:44:26 +0000507 sqlite3PagerUnref(pDbPage);
drh49285702005-09-17 15:20:26 +0000508 if( *pEType<1 || *pEType>5 ) return SQLITE_CORRUPT_BKPT;
danielk1977afcdd022004-10-31 16:25:42 +0000509 return SQLITE_OK;
510}
511
danielk197785d90ca2008-07-19 14:25:15 +0000512#else /* if defined SQLITE_OMIT_AUTOVACUUM */
513 #define ptrmapPut(w,x,y,z) SQLITE_OK
514 #define ptrmapGet(w,x,y,z) SQLITE_OK
515 #define ptrmapPutOvfl(y,z) SQLITE_OK
516#endif
danielk1977afcdd022004-10-31 16:25:42 +0000517
drh0d316a42002-08-11 20:10:47 +0000518/*
drh271efa52004-05-30 19:19:05 +0000519** Given a btree page and a cell index (0 means the first cell on
520** the page, 1 means the second cell, and so forth) return a pointer
521** to the cell content.
522**
523** This routine works only for pages that do not contain overflow cells.
drh3aac2dd2004-04-26 14:10:20 +0000524*/
drh1688c862008-07-18 02:44:17 +0000525#define findCell(P,I) \
526 ((P)->aData + ((P)->maskPage & get2byte(&(P)->aData[(P)->cellOffset+2*(I)])))
drh43605152004-05-29 21:46:49 +0000527
528/*
drh93a960a2008-07-10 00:32:42 +0000529** This a more complex version of findCell() that works for
drh43605152004-05-29 21:46:49 +0000530** pages that do contain overflow cells. See insert
531*/
532static u8 *findOverflowCell(MemPage *pPage, int iCell){
533 int i;
drh1fee73e2007-08-29 04:00:57 +0000534 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
drh43605152004-05-29 21:46:49 +0000535 for(i=pPage->nOverflow-1; i>=0; i--){
drh6d08b4d2004-07-20 12:45:22 +0000536 int k;
537 struct _OvflCell *pOvfl;
538 pOvfl = &pPage->aOvfl[i];
539 k = pOvfl->idx;
540 if( k<=iCell ){
541 if( k==iCell ){
542 return pOvfl->pCell;
drh43605152004-05-29 21:46:49 +0000543 }
544 iCell--;
545 }
546 }
danielk19771cc5ed82007-05-16 17:28:43 +0000547 return findCell(pPage, iCell);
drh43605152004-05-29 21:46:49 +0000548}
549
550/*
551** Parse a cell content block and fill in the CellInfo structure. There
drh16a9b832007-05-05 18:39:25 +0000552** are two versions of this function. sqlite3BtreeParseCell() takes a
553** cell index as the second argument and sqlite3BtreeParseCellPtr()
554** takes a pointer to the body of the cell as its second argument.
danielk19771cc5ed82007-05-16 17:28:43 +0000555**
556** Within this file, the parseCell() macro can be called instead of
557** sqlite3BtreeParseCellPtr(). Using some compilers, this will be faster.
drh43605152004-05-29 21:46:49 +0000558*/
drh16a9b832007-05-05 18:39:25 +0000559void sqlite3BtreeParseCellPtr(
drh3aac2dd2004-04-26 14:10:20 +0000560 MemPage *pPage, /* Page containing the cell */
drh43605152004-05-29 21:46:49 +0000561 u8 *pCell, /* Pointer to the cell text. */
drh6f11bef2004-05-13 01:12:56 +0000562 CellInfo *pInfo /* Fill in this structure */
drh3aac2dd2004-04-26 14:10:20 +0000563){
drhf49661a2008-12-10 16:45:50 +0000564 u16 n; /* Number bytes in cell content header */
drh271efa52004-05-30 19:19:05 +0000565 u32 nPayload; /* Number of bytes of cell payload */
drh43605152004-05-29 21:46:49 +0000566
drh1fee73e2007-08-29 04:00:57 +0000567 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
drhd677b3d2007-08-20 22:48:41 +0000568
drh43605152004-05-29 21:46:49 +0000569 pInfo->pCell = pCell;
drhab01f612004-05-22 02:55:23 +0000570 assert( pPage->leaf==0 || pPage->leaf==1 );
drh271efa52004-05-30 19:19:05 +0000571 n = pPage->childPtrSize;
572 assert( n==4-4*pPage->leaf );
drh504b6982006-01-22 21:52:56 +0000573 if( pPage->intKey ){
drh79df1f42008-07-18 00:57:33 +0000574 if( pPage->hasData ){
575 n += getVarint32(&pCell[n], nPayload);
576 }else{
577 nPayload = 0;
578 }
drh1bd10f82008-12-10 21:19:56 +0000579 n += getVarint(&pCell[n], (u64*)&pInfo->nKey);
drh79df1f42008-07-18 00:57:33 +0000580 pInfo->nData = nPayload;
drh504b6982006-01-22 21:52:56 +0000581 }else{
drh79df1f42008-07-18 00:57:33 +0000582 pInfo->nData = 0;
583 n += getVarint32(&pCell[n], nPayload);
584 pInfo->nKey = nPayload;
drh6f11bef2004-05-13 01:12:56 +0000585 }
drh72365832007-03-06 15:53:44 +0000586 pInfo->nPayload = nPayload;
drh504b6982006-01-22 21:52:56 +0000587 pInfo->nHeader = n;
drh79df1f42008-07-18 00:57:33 +0000588 if( likely(nPayload<=pPage->maxLocal) ){
drh271efa52004-05-30 19:19:05 +0000589 /* This is the (easy) common case where the entire payload fits
590 ** on the local page. No overflow is required.
591 */
592 int nSize; /* Total size of cell content in bytes */
drh79df1f42008-07-18 00:57:33 +0000593 nSize = nPayload + n;
drhf49661a2008-12-10 16:45:50 +0000594 pInfo->nLocal = (u16)nPayload;
drh6f11bef2004-05-13 01:12:56 +0000595 pInfo->iOverflow = 0;
drh79df1f42008-07-18 00:57:33 +0000596 if( (nSize & ~3)==0 ){
drh271efa52004-05-30 19:19:05 +0000597 nSize = 4; /* Minimum cell size is 4 */
drh43605152004-05-29 21:46:49 +0000598 }
drh1bd10f82008-12-10 21:19:56 +0000599 pInfo->nSize = (u16)nSize;
drh6f11bef2004-05-13 01:12:56 +0000600 }else{
drh271efa52004-05-30 19:19:05 +0000601 /* If the payload will not fit completely on the local page, we have
602 ** to decide how much to store locally and how much to spill onto
603 ** overflow pages. The strategy is to minimize the amount of unused
604 ** space on overflow pages while keeping the amount of local storage
605 ** in between minLocal and maxLocal.
606 **
607 ** Warning: changing the way overflow payload is distributed in any
608 ** way will result in an incompatible file format.
609 */
610 int minLocal; /* Minimum amount of payload held locally */
611 int maxLocal; /* Maximum amount of payload held locally */
612 int surplus; /* Overflow payload available for local storage */
613
614 minLocal = pPage->minLocal;
615 maxLocal = pPage->maxLocal;
616 surplus = minLocal + (nPayload - minLocal)%(pPage->pBt->usableSize - 4);
drh6f11bef2004-05-13 01:12:56 +0000617 if( surplus <= maxLocal ){
drhf49661a2008-12-10 16:45:50 +0000618 pInfo->nLocal = (u16)surplus;
drh6f11bef2004-05-13 01:12:56 +0000619 }else{
drhf49661a2008-12-10 16:45:50 +0000620 pInfo->nLocal = (u16)minLocal;
drh6f11bef2004-05-13 01:12:56 +0000621 }
drhf49661a2008-12-10 16:45:50 +0000622 pInfo->iOverflow = (u16)(pInfo->nLocal + n);
drh6f11bef2004-05-13 01:12:56 +0000623 pInfo->nSize = pInfo->iOverflow + 4;
624 }
drh3aac2dd2004-04-26 14:10:20 +0000625}
danielk19771cc5ed82007-05-16 17:28:43 +0000626#define parseCell(pPage, iCell, pInfo) \
627 sqlite3BtreeParseCellPtr((pPage), findCell((pPage), (iCell)), (pInfo))
drh16a9b832007-05-05 18:39:25 +0000628void sqlite3BtreeParseCell(
drh43605152004-05-29 21:46:49 +0000629 MemPage *pPage, /* Page containing the cell */
630 int iCell, /* The cell index. First cell is 0 */
631 CellInfo *pInfo /* Fill in this structure */
632){
danielk19771cc5ed82007-05-16 17:28:43 +0000633 parseCell(pPage, iCell, pInfo);
drh43605152004-05-29 21:46:49 +0000634}
drh3aac2dd2004-04-26 14:10:20 +0000635
636/*
drh43605152004-05-29 21:46:49 +0000637** Compute the total number of bytes that a Cell needs in the cell
638** data area of the btree-page. The return number includes the cell
639** data header and the local payload, but not any overflow page or
640** the space used by the cell pointer.
drh3b7511c2001-05-26 13:15:44 +0000641*/
danielk1977bc6ada42004-06-30 08:20:16 +0000642#ifndef NDEBUG
drha9121e42008-02-19 14:59:35 +0000643static u16 cellSize(MemPage *pPage, int iCell){
drh6f11bef2004-05-13 01:12:56 +0000644 CellInfo info;
drh16a9b832007-05-05 18:39:25 +0000645 sqlite3BtreeParseCell(pPage, iCell, &info);
drh43605152004-05-29 21:46:49 +0000646 return info.nSize;
647}
danielk1977bc6ada42004-06-30 08:20:16 +0000648#endif
drha9121e42008-02-19 14:59:35 +0000649static u16 cellSizePtr(MemPage *pPage, u8 *pCell){
drh43605152004-05-29 21:46:49 +0000650 CellInfo info;
drh16a9b832007-05-05 18:39:25 +0000651 sqlite3BtreeParseCellPtr(pPage, pCell, &info);
drh6f11bef2004-05-13 01:12:56 +0000652 return info.nSize;
drh3b7511c2001-05-26 13:15:44 +0000653}
654
danielk197779a40da2005-01-16 08:00:01 +0000655#ifndef SQLITE_OMIT_AUTOVACUUM
drh3b7511c2001-05-26 13:15:44 +0000656/*
danielk197726836652005-01-17 01:33:13 +0000657** If the cell pCell, part of page pPage contains a pointer
danielk197779a40da2005-01-16 08:00:01 +0000658** to an overflow page, insert an entry into the pointer-map
659** for the overflow page.
danielk1977ac11ee62005-01-15 12:45:51 +0000660*/
danielk197726836652005-01-17 01:33:13 +0000661static int ptrmapPutOvflPtr(MemPage *pPage, u8 *pCell){
drhfa67c3c2008-07-11 02:21:40 +0000662 CellInfo info;
663 assert( pCell!=0 );
664 sqlite3BtreeParseCellPtr(pPage, pCell, &info);
665 assert( (info.nData+(pPage->intKey?0:info.nKey))==info.nPayload );
666 if( (info.nData+(pPage->intKey?0:info.nKey))>info.nLocal ){
667 Pgno ovfl = get4byte(&pCell[info.iOverflow]);
668 return ptrmapPut(pPage->pBt, ovfl, PTRMAP_OVERFLOW1, pPage->pgno);
danielk1977ac11ee62005-01-15 12:45:51 +0000669 }
danielk197779a40da2005-01-16 08:00:01 +0000670 return SQLITE_OK;
danielk1977ac11ee62005-01-15 12:45:51 +0000671}
danielk197726836652005-01-17 01:33:13 +0000672/*
673** If the cell with index iCell on page pPage contains a pointer
674** to an overflow page, insert an entry into the pointer-map
675** for the overflow page.
676*/
677static int ptrmapPutOvfl(MemPage *pPage, int iCell){
678 u8 *pCell;
drh1fee73e2007-08-29 04:00:57 +0000679 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
danielk197726836652005-01-17 01:33:13 +0000680 pCell = findOverflowCell(pPage, iCell);
681 return ptrmapPutOvflPtr(pPage, pCell);
682}
danielk197779a40da2005-01-16 08:00:01 +0000683#endif
684
danielk1977ac11ee62005-01-15 12:45:51 +0000685
drhda200cc2004-05-09 11:51:38 +0000686/*
drh72f82862001-05-24 21:06:34 +0000687** Defragment the page given. All Cells are moved to the
drh3a4a2d42005-11-24 14:24:28 +0000688** end of the page and all free space is collected into one
689** big FreeBlk that occurs in between the header and cell
drh31beae92005-11-24 14:34:36 +0000690** pointer array and the cell content area.
drh365d68f2001-05-11 11:02:46 +0000691*/
shane0af3f892008-11-12 04:55:34 +0000692static int defragmentPage(MemPage *pPage){
drh43605152004-05-29 21:46:49 +0000693 int i; /* Loop counter */
694 int pc; /* Address of a i-th cell */
695 int addr; /* Offset of first byte after cell pointer array */
696 int hdr; /* Offset to the page header */
697 int size; /* Size of a cell */
698 int usableSize; /* Number of usable bytes on a page */
699 int cellOffset; /* Offset to the cell pointer array */
drh281b21d2008-08-22 12:57:08 +0000700 int cbrk; /* Offset to the cell content area */
drh43605152004-05-29 21:46:49 +0000701 int nCell; /* Number of cells on the page */
drh2e38c322004-09-03 18:38:44 +0000702 unsigned char *data; /* The page data */
703 unsigned char *temp; /* Temp area for cell content */
drh2af926b2001-05-15 00:39:25 +0000704
danielk19773b8a05f2007-03-19 17:44:26 +0000705 assert( sqlite3PagerIswriteable(pPage->pDbPage) );
drh9e572e62004-04-23 23:43:10 +0000706 assert( pPage->pBt!=0 );
drh90f5ecb2004-07-22 01:19:35 +0000707 assert( pPage->pBt->usableSize <= SQLITE_MAX_PAGE_SIZE );
drh43605152004-05-29 21:46:49 +0000708 assert( pPage->nOverflow==0 );
drh1fee73e2007-08-29 04:00:57 +0000709 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
drh26b79942007-11-28 16:19:56 +0000710 temp = sqlite3PagerTempSpace(pPage->pBt->pPager);
drh43605152004-05-29 21:46:49 +0000711 data = pPage->aData;
drh9e572e62004-04-23 23:43:10 +0000712 hdr = pPage->hdrOffset;
drh43605152004-05-29 21:46:49 +0000713 cellOffset = pPage->cellOffset;
714 nCell = pPage->nCell;
715 assert( nCell==get2byte(&data[hdr+3]) );
716 usableSize = pPage->pBt->usableSize;
drh281b21d2008-08-22 12:57:08 +0000717 cbrk = get2byte(&data[hdr+5]);
718 memcpy(&temp[cbrk], &data[cbrk], usableSize - cbrk);
719 cbrk = usableSize;
drh43605152004-05-29 21:46:49 +0000720 for(i=0; i<nCell; i++){
721 u8 *pAddr; /* The i-th cell pointer */
722 pAddr = &data[cellOffset + i*2];
723 pc = get2byte(pAddr);
shanedcc50b72008-11-13 18:29:50 +0000724 if( pc>=usableSize ){
shane0af3f892008-11-12 04:55:34 +0000725 return SQLITE_CORRUPT_BKPT;
726 }
drh43605152004-05-29 21:46:49 +0000727 size = cellSizePtr(pPage, &temp[pc]);
drh281b21d2008-08-22 12:57:08 +0000728 cbrk -= size;
danielk19770d065412008-11-12 18:21:36 +0000729 if( cbrk<cellOffset+2*nCell || pc+size>usableSize ){
shane0af3f892008-11-12 04:55:34 +0000730 return SQLITE_CORRUPT_BKPT;
731 }
danielk19770d065412008-11-12 18:21:36 +0000732 assert( cbrk+size<=usableSize && cbrk>=0 );
drh281b21d2008-08-22 12:57:08 +0000733 memcpy(&data[cbrk], &temp[pc], size);
734 put2byte(pAddr, cbrk);
drh2af926b2001-05-15 00:39:25 +0000735 }
drh281b21d2008-08-22 12:57:08 +0000736 assert( cbrk>=cellOffset+2*nCell );
737 put2byte(&data[hdr+5], cbrk);
drh43605152004-05-29 21:46:49 +0000738 data[hdr+1] = 0;
739 data[hdr+2] = 0;
740 data[hdr+7] = 0;
741 addr = cellOffset+2*nCell;
drh281b21d2008-08-22 12:57:08 +0000742 memset(&data[addr], 0, cbrk-addr);
drhc5053fb2008-11-27 02:22:10 +0000743 assert( sqlite3PagerIswriteable(pPage->pDbPage) );
danielk1977360e6342008-11-12 08:49:51 +0000744 if( cbrk-addr!=pPage->nFree ){
745 return SQLITE_CORRUPT_BKPT;
746 }
shane0af3f892008-11-12 04:55:34 +0000747 return SQLITE_OK;
drh365d68f2001-05-11 11:02:46 +0000748}
749
drha059ad02001-04-17 20:09:11 +0000750/*
drh43605152004-05-29 21:46:49 +0000751** Allocate nByte bytes of space on a page.
drhbd03cae2001-06-02 02:40:57 +0000752**
drh9e572e62004-04-23 23:43:10 +0000753** Return the index into pPage->aData[] of the first byte of
drhfa67c3c2008-07-11 02:21:40 +0000754** the new allocation. The caller guarantees that there is enough
755** space. This routine will never fail.
drh2af926b2001-05-15 00:39:25 +0000756**
drh72f82862001-05-24 21:06:34 +0000757** If the page contains nBytes of free space but does not contain
drh8b2f49b2001-06-08 00:21:52 +0000758** nBytes of contiguous free space, then this routine automatically
759** calls defragementPage() to consolidate all free space before
760** allocating the new chunk.
drh7e3b0a02001-04-28 16:52:40 +0000761*/
drh9e572e62004-04-23 23:43:10 +0000762static int allocateSpace(MemPage *pPage, int nByte){
drh3aac2dd2004-04-26 14:10:20 +0000763 int addr, pc, hdr;
drh9e572e62004-04-23 23:43:10 +0000764 int size;
drh24cd67e2004-05-10 16:18:47 +0000765 int nFrag;
drh43605152004-05-29 21:46:49 +0000766 int top;
767 int nCell;
768 int cellOffset;
drh9e572e62004-04-23 23:43:10 +0000769 unsigned char *data;
drh43605152004-05-29 21:46:49 +0000770
drh9e572e62004-04-23 23:43:10 +0000771 data = pPage->aData;
danielk19773b8a05f2007-03-19 17:44:26 +0000772 assert( sqlite3PagerIswriteable(pPage->pDbPage) );
drh9e572e62004-04-23 23:43:10 +0000773 assert( pPage->pBt );
drh1fee73e2007-08-29 04:00:57 +0000774 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
drhfa67c3c2008-07-11 02:21:40 +0000775 assert( nByte>=0 ); /* Minimum cell size is 4 */
776 assert( pPage->nFree>=nByte );
777 assert( pPage->nOverflow==0 );
drhf49661a2008-12-10 16:45:50 +0000778 pPage->nFree -= (u16)nByte;
drh9e572e62004-04-23 23:43:10 +0000779 hdr = pPage->hdrOffset;
drh43605152004-05-29 21:46:49 +0000780
781 nFrag = data[hdr+7];
782 if( nFrag<60 ){
783 /* Search the freelist looking for a slot big enough to satisfy the
784 ** space request. */
785 addr = hdr+1;
786 while( (pc = get2byte(&data[addr]))>0 ){
787 size = get2byte(&data[pc+2]);
788 if( size>=nByte ){
drhf49661a2008-12-10 16:45:50 +0000789 int x = size - nByte;
drh43605152004-05-29 21:46:49 +0000790 if( size<nByte+4 ){
791 memcpy(&data[addr], &data[pc], 2);
drhf49661a2008-12-10 16:45:50 +0000792 data[hdr+7] = (u8)(nFrag + x);
drh43605152004-05-29 21:46:49 +0000793 return pc;
794 }else{
drhf49661a2008-12-10 16:45:50 +0000795 put2byte(&data[pc+2], x);
796 return pc + x;
drh43605152004-05-29 21:46:49 +0000797 }
798 }
799 addr = pc;
drh9e572e62004-04-23 23:43:10 +0000800 }
801 }
drh43605152004-05-29 21:46:49 +0000802
803 /* Allocate memory from the gap in between the cell pointer array
804 ** and the cell content area.
805 */
806 top = get2byte(&data[hdr+5]);
807 nCell = get2byte(&data[hdr+3]);
808 cellOffset = pPage->cellOffset;
809 if( nFrag>=60 || cellOffset + 2*nCell > top - nByte ){
danielk1977474b7cc2008-07-09 11:49:46 +0000810 defragmentPage(pPage);
drh43605152004-05-29 21:46:49 +0000811 top = get2byte(&data[hdr+5]);
drh2af926b2001-05-15 00:39:25 +0000812 }
drh43605152004-05-29 21:46:49 +0000813 top -= nByte;
814 assert( cellOffset + 2*nCell <= top );
815 put2byte(&data[hdr+5], top);
drhc5053fb2008-11-27 02:22:10 +0000816 assert( sqlite3PagerIswriteable(pPage->pDbPage) );
drh43605152004-05-29 21:46:49 +0000817 return top;
drh7e3b0a02001-04-28 16:52:40 +0000818}
819
820/*
drh9e572e62004-04-23 23:43:10 +0000821** Return a section of the pPage->aData to the freelist.
822** The first byte of the new free block is pPage->aDisk[start]
823** and the size of the block is "size" bytes.
drh306dc212001-05-21 13:45:10 +0000824**
825** Most of the effort here is involved in coalesing adjacent
826** free blocks into a single big free block.
drh7e3b0a02001-04-28 16:52:40 +0000827*/
shanedcc50b72008-11-13 18:29:50 +0000828static int freeSpace(MemPage *pPage, int start, int size){
drh43605152004-05-29 21:46:49 +0000829 int addr, pbegin, hdr;
drh9e572e62004-04-23 23:43:10 +0000830 unsigned char *data = pPage->aData;
drh2af926b2001-05-15 00:39:25 +0000831
drh9e572e62004-04-23 23:43:10 +0000832 assert( pPage->pBt!=0 );
danielk19773b8a05f2007-03-19 17:44:26 +0000833 assert( sqlite3PagerIswriteable(pPage->pDbPage) );
drh9e572e62004-04-23 23:43:10 +0000834 assert( start>=pPage->hdrOffset+6+(pPage->leaf?0:4) );
danielk1977bc6ada42004-06-30 08:20:16 +0000835 assert( (start + size)<=pPage->pBt->usableSize );
drh1fee73e2007-08-29 04:00:57 +0000836 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
drh34004ce2008-07-11 16:15:17 +0000837 assert( size>=0 ); /* Minimum cell size is 4 */
drh9e572e62004-04-23 23:43:10 +0000838
drhfcce93f2006-02-22 03:08:32 +0000839#ifdef SQLITE_SECURE_DELETE
840 /* Overwrite deleted information with zeros when the SECURE_DELETE
841 ** option is enabled at compile-time */
842 memset(&data[start], 0, size);
843#endif
844
drh9e572e62004-04-23 23:43:10 +0000845 /* Add the space back into the linked list of freeblocks */
drh43605152004-05-29 21:46:49 +0000846 hdr = pPage->hdrOffset;
847 addr = hdr + 1;
drh3aac2dd2004-04-26 14:10:20 +0000848 while( (pbegin = get2byte(&data[addr]))<start && pbegin>0 ){
drhb6f41482004-05-14 01:58:11 +0000849 assert( pbegin<=pPage->pBt->usableSize-4 );
shanedcc50b72008-11-13 18:29:50 +0000850 if( pbegin<=addr ) {
851 return SQLITE_CORRUPT_BKPT;
852 }
drh3aac2dd2004-04-26 14:10:20 +0000853 addr = pbegin;
drh2af926b2001-05-15 00:39:25 +0000854 }
shanedcc50b72008-11-13 18:29:50 +0000855 if ( pbegin>pPage->pBt->usableSize-4 ) {
856 return SQLITE_CORRUPT_BKPT;
857 }
drh3aac2dd2004-04-26 14:10:20 +0000858 assert( pbegin>addr || pbegin==0 );
drha34b6762004-05-07 13:30:42 +0000859 put2byte(&data[addr], start);
860 put2byte(&data[start], pbegin);
861 put2byte(&data[start+2], size);
drhf49661a2008-12-10 16:45:50 +0000862 pPage->nFree += (u16)size;
drh9e572e62004-04-23 23:43:10 +0000863
864 /* Coalesce adjacent free blocks */
drh3aac2dd2004-04-26 14:10:20 +0000865 addr = pPage->hdrOffset + 1;
866 while( (pbegin = get2byte(&data[addr]))>0 ){
drhf49661a2008-12-10 16:45:50 +0000867 int pnext, psize, x;
drh3aac2dd2004-04-26 14:10:20 +0000868 assert( pbegin>addr );
drh43605152004-05-29 21:46:49 +0000869 assert( pbegin<=pPage->pBt->usableSize-4 );
drh9e572e62004-04-23 23:43:10 +0000870 pnext = get2byte(&data[pbegin]);
871 psize = get2byte(&data[pbegin+2]);
872 if( pbegin + psize + 3 >= pnext && pnext>0 ){
873 int frag = pnext - (pbegin+psize);
drhf49661a2008-12-10 16:45:50 +0000874 if( (frag<0) || (frag>(int)data[pPage->hdrOffset+7]) ){
shanedcc50b72008-11-13 18:29:50 +0000875 return SQLITE_CORRUPT_BKPT;
876 }
drhf49661a2008-12-10 16:45:50 +0000877 data[pPage->hdrOffset+7] -= (u8)frag;
878 x = get2byte(&data[pnext]);
879 put2byte(&data[pbegin], x);
880 x = pnext + get2byte(&data[pnext+2]) - pbegin;
881 put2byte(&data[pbegin+2], x);
drh9e572e62004-04-23 23:43:10 +0000882 }else{
drh3aac2dd2004-04-26 14:10:20 +0000883 addr = pbegin;
drh9e572e62004-04-23 23:43:10 +0000884 }
885 }
drh7e3b0a02001-04-28 16:52:40 +0000886
drh43605152004-05-29 21:46:49 +0000887 /* If the cell content area begins with a freeblock, remove it. */
888 if( data[hdr+1]==data[hdr+5] && data[hdr+2]==data[hdr+6] ){
889 int top;
890 pbegin = get2byte(&data[hdr+1]);
891 memcpy(&data[hdr+1], &data[pbegin], 2);
drhf49661a2008-12-10 16:45:50 +0000892 top = get2byte(&data[hdr+5]) + get2byte(&data[pbegin+2]);
893 put2byte(&data[hdr+5], top);
drh4b70f112004-05-02 21:12:19 +0000894 }
drhc5053fb2008-11-27 02:22:10 +0000895 assert( sqlite3PagerIswriteable(pPage->pDbPage) );
shanedcc50b72008-11-13 18:29:50 +0000896 return SQLITE_OK;
drh4b70f112004-05-02 21:12:19 +0000897}
898
899/*
drh271efa52004-05-30 19:19:05 +0000900** Decode the flags byte (the first byte of the header) for a page
901** and initialize fields of the MemPage structure accordingly.
drh44845222008-07-17 18:39:57 +0000902**
903** Only the following combinations are supported. Anything different
904** indicates a corrupt database files:
905**
906** PTF_ZERODATA
907** PTF_ZERODATA | PTF_LEAF
908** PTF_LEAFDATA | PTF_INTKEY
909** PTF_LEAFDATA | PTF_INTKEY | PTF_LEAF
drh271efa52004-05-30 19:19:05 +0000910*/
drh44845222008-07-17 18:39:57 +0000911static int decodeFlags(MemPage *pPage, int flagByte){
danielk1977aef0bf62005-12-30 16:28:01 +0000912 BtShared *pBt; /* A copy of pPage->pBt */
drh271efa52004-05-30 19:19:05 +0000913
914 assert( pPage->hdrOffset==(pPage->pgno==1 ? 100 : 0) );
drh1fee73e2007-08-29 04:00:57 +0000915 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
drhf49661a2008-12-10 16:45:50 +0000916 pPage->leaf = (u8)(flagByte>>3); assert( PTF_LEAF == 1<<3 );
drh44845222008-07-17 18:39:57 +0000917 flagByte &= ~PTF_LEAF;
918 pPage->childPtrSize = 4-4*pPage->leaf;
drh271efa52004-05-30 19:19:05 +0000919 pBt = pPage->pBt;
drh44845222008-07-17 18:39:57 +0000920 if( flagByte==(PTF_LEAFDATA | PTF_INTKEY) ){
921 pPage->intKey = 1;
922 pPage->hasData = pPage->leaf;
drh271efa52004-05-30 19:19:05 +0000923 pPage->maxLocal = pBt->maxLeaf;
924 pPage->minLocal = pBt->minLeaf;
drh44845222008-07-17 18:39:57 +0000925 }else if( flagByte==PTF_ZERODATA ){
926 pPage->intKey = 0;
927 pPage->hasData = 0;
drh271efa52004-05-30 19:19:05 +0000928 pPage->maxLocal = pBt->maxLocal;
929 pPage->minLocal = pBt->minLocal;
drh44845222008-07-17 18:39:57 +0000930 }else{
931 return SQLITE_CORRUPT_BKPT;
drh271efa52004-05-30 19:19:05 +0000932 }
drh44845222008-07-17 18:39:57 +0000933 return SQLITE_OK;
drh271efa52004-05-30 19:19:05 +0000934}
935
936/*
drh7e3b0a02001-04-28 16:52:40 +0000937** Initialize the auxiliary information for a disk block.
drh72f82862001-05-24 21:06:34 +0000938**
939** Return SQLITE_OK on success. If we see that the page does
drhda47d772002-12-02 04:25:19 +0000940** not contain a well-formed database page, then return
drh72f82862001-05-24 21:06:34 +0000941** SQLITE_CORRUPT. Note that a return of SQLITE_OK does not
942** guarantee that the page is well-formed. It only shows that
943** we failed to detect any corruption.
drh7e3b0a02001-04-28 16:52:40 +0000944*/
danielk197771d5d2c2008-09-29 11:49:47 +0000945int sqlite3BtreeInitPage(MemPage *pPage){
drh2af926b2001-05-15 00:39:25 +0000946
danielk197771d5d2c2008-09-29 11:49:47 +0000947 assert( pPage->pBt!=0 );
948 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
danielk19773b8a05f2007-03-19 17:44:26 +0000949 assert( pPage->pgno==sqlite3PagerPagenumber(pPage->pDbPage) );
drhbf4bca52007-09-06 22:19:14 +0000950 assert( pPage == sqlite3PagerGetExtra(pPage->pDbPage) );
951 assert( pPage->aData == sqlite3PagerGetData(pPage->pDbPage) );
danielk197771d5d2c2008-09-29 11:49:47 +0000952
953 if( !pPage->isInit ){
drhf49661a2008-12-10 16:45:50 +0000954 u16 pc; /* Address of a freeblock within pPage->aData[] */
955 u8 hdr; /* Offset to beginning of page header */
danielk197771d5d2c2008-09-29 11:49:47 +0000956 u8 *data; /* Equal to pPage->aData */
957 BtShared *pBt; /* The main btree structure */
drhf49661a2008-12-10 16:45:50 +0000958 u16 usableSize; /* Amount of usable space on each page */
959 u16 cellOffset; /* Offset from start of page to first cell pointer */
960 u16 nFree; /* Number of unused bytes on the page */
961 u16 top; /* First byte of the cell content area */
danielk197771d5d2c2008-09-29 11:49:47 +0000962
963 pBt = pPage->pBt;
964
danielk1977eaa06f62008-09-18 17:34:44 +0000965 hdr = pPage->hdrOffset;
966 data = pPage->aData;
967 if( decodeFlags(pPage, data[hdr]) ) return SQLITE_CORRUPT_BKPT;
968 assert( pBt->pageSize>=512 && pBt->pageSize<=32768 );
969 pPage->maskPage = pBt->pageSize - 1;
970 pPage->nOverflow = 0;
danielk1977eaa06f62008-09-18 17:34:44 +0000971 usableSize = pBt->usableSize;
972 pPage->cellOffset = cellOffset = hdr + 12 - 4*pPage->leaf;
973 top = get2byte(&data[hdr+5]);
974 pPage->nCell = get2byte(&data[hdr+3]);
975 if( pPage->nCell>MX_CELL(pBt) ){
976 /* To many cells for a single page. The page must be corrupt */
977 return SQLITE_CORRUPT_BKPT;
978 }
danielk1977eaa06f62008-09-18 17:34:44 +0000979
980 /* Compute the total free space on the page */
981 pc = get2byte(&data[hdr+1]);
982 nFree = data[hdr+7] + top - (cellOffset + 2*pPage->nCell);
983 while( pc>0 ){
drh1bd10f82008-12-10 21:19:56 +0000984 u16 next, size;
danielk1977eaa06f62008-09-18 17:34:44 +0000985 if( pc>usableSize-4 ){
986 /* Free block is off the page */
987 return SQLITE_CORRUPT_BKPT;
988 }
989 next = get2byte(&data[pc]);
990 size = get2byte(&data[pc+2]);
991 if( next>0 && next<=pc+size+3 ){
992 /* Free blocks must be in accending order */
993 return SQLITE_CORRUPT_BKPT;
994 }
995 nFree += size;
996 pc = next;
997 }
drhf49661a2008-12-10 16:45:50 +0000998 pPage->nFree = (u16)nFree;
danielk1977eaa06f62008-09-18 17:34:44 +0000999 if( nFree>=usableSize ){
1000 /* Free space cannot exceed total page size */
drh49285702005-09-17 15:20:26 +00001001 return SQLITE_CORRUPT_BKPT;
drhee696e22004-08-30 16:52:17 +00001002 }
drh9e572e62004-04-23 23:43:10 +00001003
drh1688c862008-07-18 02:44:17 +00001004#if 0
1005 /* Check that all the offsets in the cell offset array are within range.
1006 **
1007 ** Omitting this consistency check and using the pPage->maskPage mask
1008 ** to prevent overrunning the page buffer in findCell() results in a
1009 ** 2.5% performance gain.
1010 */
1011 {
1012 u8 *pOff; /* Iterator used to check all cell offsets are in range */
1013 u8 *pEnd; /* Pointer to end of cell offset array */
1014 u8 mask; /* Mask of bits that must be zero in MSB of cell offsets */
1015 mask = ~(((u8)(pBt->pageSize>>8))-1);
1016 pEnd = &data[cellOffset + pPage->nCell*2];
1017 for(pOff=&data[cellOffset]; pOff!=pEnd && !((*pOff)&mask); pOff+=2);
1018 if( pOff!=pEnd ){
1019 return SQLITE_CORRUPT_BKPT;
1020 }
danielk1977e16535f2008-06-11 18:15:29 +00001021 }
drh1688c862008-07-18 02:44:17 +00001022#endif
danielk1977e16535f2008-06-11 18:15:29 +00001023
danielk197771d5d2c2008-09-29 11:49:47 +00001024 pPage->isInit = 1;
1025 }
drh9e572e62004-04-23 23:43:10 +00001026 return SQLITE_OK;
drh7e3b0a02001-04-28 16:52:40 +00001027}
1028
1029/*
drh8b2f49b2001-06-08 00:21:52 +00001030** Set up a raw page so that it looks like a database page holding
1031** no entries.
drhbd03cae2001-06-02 02:40:57 +00001032*/
drh9e572e62004-04-23 23:43:10 +00001033static void zeroPage(MemPage *pPage, int flags){
1034 unsigned char *data = pPage->aData;
danielk1977aef0bf62005-12-30 16:28:01 +00001035 BtShared *pBt = pPage->pBt;
drhf49661a2008-12-10 16:45:50 +00001036 u8 hdr = pPage->hdrOffset;
1037 u16 first;
drh9e572e62004-04-23 23:43:10 +00001038
danielk19773b8a05f2007-03-19 17:44:26 +00001039 assert( sqlite3PagerPagenumber(pPage->pDbPage)==pPage->pgno );
drhbf4bca52007-09-06 22:19:14 +00001040 assert( sqlite3PagerGetExtra(pPage->pDbPage) == (void*)pPage );
1041 assert( sqlite3PagerGetData(pPage->pDbPage) == data );
danielk19773b8a05f2007-03-19 17:44:26 +00001042 assert( sqlite3PagerIswriteable(pPage->pDbPage) );
drh1fee73e2007-08-29 04:00:57 +00001043 assert( sqlite3_mutex_held(pBt->mutex) );
drh1af4a6e2008-07-18 03:32:51 +00001044 /*memset(&data[hdr], 0, pBt->usableSize - hdr);*/
drh1bd10f82008-12-10 21:19:56 +00001045 data[hdr] = (char)flags;
1046 first = hdr + 8 + 4*((flags&PTF_LEAF)==0 ?1:0);
drh43605152004-05-29 21:46:49 +00001047 memset(&data[hdr+1], 0, 4);
1048 data[hdr+7] = 0;
1049 put2byte(&data[hdr+5], pBt->usableSize);
drhb6f41482004-05-14 01:58:11 +00001050 pPage->nFree = pBt->usableSize - first;
drh271efa52004-05-30 19:19:05 +00001051 decodeFlags(pPage, flags);
drh9e572e62004-04-23 23:43:10 +00001052 pPage->hdrOffset = hdr;
drh43605152004-05-29 21:46:49 +00001053 pPage->cellOffset = first;
1054 pPage->nOverflow = 0;
drh1688c862008-07-18 02:44:17 +00001055 assert( pBt->pageSize>=512 && pBt->pageSize<=32768 );
1056 pPage->maskPage = pBt->pageSize - 1;
drh43605152004-05-29 21:46:49 +00001057 pPage->nCell = 0;
danielk197771d5d2c2008-09-29 11:49:47 +00001058 pPage->isInit = 1;
drhbd03cae2001-06-02 02:40:57 +00001059}
1060
drh897a8202008-09-18 01:08:15 +00001061
1062/*
1063** Convert a DbPage obtained from the pager into a MemPage used by
1064** the btree layer.
1065*/
1066static MemPage *btreePageFromDbPage(DbPage *pDbPage, Pgno pgno, BtShared *pBt){
1067 MemPage *pPage = (MemPage*)sqlite3PagerGetExtra(pDbPage);
1068 pPage->aData = sqlite3PagerGetData(pDbPage);
1069 pPage->pDbPage = pDbPage;
1070 pPage->pBt = pBt;
1071 pPage->pgno = pgno;
1072 pPage->hdrOffset = pPage->pgno==1 ? 100 : 0;
1073 return pPage;
1074}
1075
drhbd03cae2001-06-02 02:40:57 +00001076/*
drh3aac2dd2004-04-26 14:10:20 +00001077** Get a page from the pager. Initialize the MemPage.pBt and
1078** MemPage.aData elements if needed.
drh538f5702007-04-13 02:14:30 +00001079**
1080** If the noContent flag is set, it means that we do not care about
1081** the content of the page at this time. So do not go to the disk
1082** to fetch the content. Just fill in the content with zeros for now.
1083** If in the future we call sqlite3PagerWrite() on this page, that
1084** means we have started to be concerned about content and the disk
1085** read should occur at that point.
drh3aac2dd2004-04-26 14:10:20 +00001086*/
drh16a9b832007-05-05 18:39:25 +00001087int sqlite3BtreeGetPage(
1088 BtShared *pBt, /* The btree */
1089 Pgno pgno, /* Number of the page to fetch */
1090 MemPage **ppPage, /* Return the page in this parameter */
1091 int noContent /* Do not load page content if true */
1092){
drh3aac2dd2004-04-26 14:10:20 +00001093 int rc;
danielk19773b8a05f2007-03-19 17:44:26 +00001094 DbPage *pDbPage;
1095
drh1fee73e2007-08-29 04:00:57 +00001096 assert( sqlite3_mutex_held(pBt->mutex) );
drh538f5702007-04-13 02:14:30 +00001097 rc = sqlite3PagerAcquire(pBt->pPager, pgno, (DbPage**)&pDbPage, noContent);
drh3aac2dd2004-04-26 14:10:20 +00001098 if( rc ) return rc;
drh897a8202008-09-18 01:08:15 +00001099 *ppPage = btreePageFromDbPage(pDbPage, pgno, pBt);
drh3aac2dd2004-04-26 14:10:20 +00001100 return SQLITE_OK;
1101}
1102
1103/*
danielk197789d40042008-11-17 14:20:56 +00001104** Return the size of the database file in pages. If there is any kind of
1105** error, return ((unsigned int)-1).
danielk197767fd7a92008-09-10 17:53:35 +00001106*/
danielk197789d40042008-11-17 14:20:56 +00001107static Pgno pagerPagecount(BtShared *pBt){
1108 int nPage = -1;
danielk197767fd7a92008-09-10 17:53:35 +00001109 int rc;
danielk197789d40042008-11-17 14:20:56 +00001110 assert( pBt->pPage1 );
1111 rc = sqlite3PagerPagecount(pBt->pPager, &nPage);
1112 assert( rc==SQLITE_OK || nPage==-1 );
1113 return (Pgno)nPage;
danielk197767fd7a92008-09-10 17:53:35 +00001114}
1115
1116/*
drhde647132004-05-07 17:57:49 +00001117** Get a page from the pager and initialize it. This routine
1118** is just a convenience wrapper around separate calls to
drh16a9b832007-05-05 18:39:25 +00001119** sqlite3BtreeGetPage() and sqlite3BtreeInitPage().
drhde647132004-05-07 17:57:49 +00001120*/
1121static int getAndInitPage(
danielk1977aef0bf62005-12-30 16:28:01 +00001122 BtShared *pBt, /* The database file */
drhde647132004-05-07 17:57:49 +00001123 Pgno pgno, /* Number of the page to get */
danielk197771d5d2c2008-09-29 11:49:47 +00001124 MemPage **ppPage /* Write the page pointer here */
drhde647132004-05-07 17:57:49 +00001125){
1126 int rc;
danielk197745d68822009-01-16 16:23:38 +00001127 DbPage *pDbPage;
drh897a8202008-09-18 01:08:15 +00001128 MemPage *pPage;
1129
drh1fee73e2007-08-29 04:00:57 +00001130 assert( sqlite3_mutex_held(pBt->mutex) );
drh897a8202008-09-18 01:08:15 +00001131 if( pgno==0 ){
drh49285702005-09-17 15:20:26 +00001132 return SQLITE_CORRUPT_BKPT;
drhee696e22004-08-30 16:52:17 +00001133 }
danielk19779f580ad2008-09-10 14:45:57 +00001134
drh897a8202008-09-18 01:08:15 +00001135 /* It is often the case that the page we want is already in cache.
1136 ** If so, get it directly. This saves us from having to call
1137 ** pagerPagecount() to make sure pgno is within limits, which results
1138 ** in a measureable performance improvements.
1139 */
danielk197745d68822009-01-16 16:23:38 +00001140 pDbPage = sqlite3PagerLookup(pBt->pPager, pgno);
1141 if( pDbPage ){
drh897a8202008-09-18 01:08:15 +00001142 /* Page is already in cache */
danielk197745d68822009-01-16 16:23:38 +00001143 *ppPage = pPage = btreePageFromDbPage(pDbPage, pgno, pBt);
drh897a8202008-09-18 01:08:15 +00001144 rc = SQLITE_OK;
1145 }else{
1146 /* Page not in cache. Acquire it. */
danielk197789d40042008-11-17 14:20:56 +00001147 if( pgno>pagerPagecount(pBt) ){
drh897a8202008-09-18 01:08:15 +00001148 return SQLITE_CORRUPT_BKPT;
1149 }
1150 rc = sqlite3BtreeGetPage(pBt, pgno, ppPage, 0);
1151 if( rc ) return rc;
1152 pPage = *ppPage;
1153 }
danielk197771d5d2c2008-09-29 11:49:47 +00001154 if( !pPage->isInit ){
1155 rc = sqlite3BtreeInitPage(pPage);
drh897a8202008-09-18 01:08:15 +00001156 }
1157 if( rc!=SQLITE_OK ){
1158 releasePage(pPage);
1159 *ppPage = 0;
1160 }
drhde647132004-05-07 17:57:49 +00001161 return rc;
1162}
1163
1164/*
drh3aac2dd2004-04-26 14:10:20 +00001165** Release a MemPage. This should be called once for each prior
drh16a9b832007-05-05 18:39:25 +00001166** call to sqlite3BtreeGetPage.
drh3aac2dd2004-04-26 14:10:20 +00001167*/
drh4b70f112004-05-02 21:12:19 +00001168static void releasePage(MemPage *pPage){
drh3aac2dd2004-04-26 14:10:20 +00001169 if( pPage ){
drh30df0092008-12-23 15:58:06 +00001170 assert( pPage->nOverflow==0 || sqlite3PagerPageRefcount(pPage->pDbPage)>1 );
drh3aac2dd2004-04-26 14:10:20 +00001171 assert( pPage->aData );
1172 assert( pPage->pBt );
drhbf4bca52007-09-06 22:19:14 +00001173 assert( sqlite3PagerGetExtra(pPage->pDbPage) == (void*)pPage );
1174 assert( sqlite3PagerGetData(pPage->pDbPage)==pPage->aData );
drh1fee73e2007-08-29 04:00:57 +00001175 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
danielk19773b8a05f2007-03-19 17:44:26 +00001176 sqlite3PagerUnref(pPage->pDbPage);
drh3aac2dd2004-04-26 14:10:20 +00001177 }
1178}
1179
1180/*
drha6abd042004-06-09 17:37:22 +00001181** During a rollback, when the pager reloads information into the cache
1182** so that the cache is restored to its original state at the start of
1183** the transaction, for each page restored this routine is called.
1184**
1185** This routine needs to reset the extra data section at the end of the
1186** page to agree with the restored data.
1187*/
danielk1977eaa06f62008-09-18 17:34:44 +00001188static void pageReinit(DbPage *pData){
drh07d183d2005-05-01 22:52:42 +00001189 MemPage *pPage;
danielk19773b8a05f2007-03-19 17:44:26 +00001190 pPage = (MemPage *)sqlite3PagerGetExtra(pData);
danielk197771d5d2c2008-09-29 11:49:47 +00001191 if( pPage->isInit ){
drh1fee73e2007-08-29 04:00:57 +00001192 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
drha6abd042004-06-09 17:37:22 +00001193 pPage->isInit = 0;
danielk197771d5d2c2008-09-29 11:49:47 +00001194 if( sqlite3PagerPageRefcount(pData)>0 ){
1195 sqlite3BtreeInitPage(pPage);
1196 }
drha6abd042004-06-09 17:37:22 +00001197 }
1198}
1199
1200/*
drhe5fe6902007-12-07 18:55:28 +00001201** Invoke the busy handler for a btree.
1202*/
danielk19771ceedd32008-11-19 10:22:33 +00001203static int btreeInvokeBusyHandler(void *pArg){
drhe5fe6902007-12-07 18:55:28 +00001204 BtShared *pBt = (BtShared*)pArg;
1205 assert( pBt->db );
1206 assert( sqlite3_mutex_held(pBt->db->mutex) );
1207 return sqlite3InvokeBusyHandler(&pBt->db->busyHandler);
1208}
1209
1210/*
drhad3e0102004-09-03 23:32:18 +00001211** Open a database file.
1212**
drh382c0242001-10-06 16:33:02 +00001213** zFilename is the name of the database file. If zFilename is NULL
drh1bee3d72001-10-15 00:44:35 +00001214** a new database with a random name is created. This randomly named
drh23e11ca2004-05-04 17:27:28 +00001215** database file will be deleted when sqlite3BtreeClose() is called.
drhe53831d2007-08-17 01:14:38 +00001216** If zFilename is ":memory:" then an in-memory database is created
1217** that is automatically destroyed when it is closed.
drha059ad02001-04-17 20:09:11 +00001218*/
drh23e11ca2004-05-04 17:27:28 +00001219int sqlite3BtreeOpen(
drh3aac2dd2004-04-26 14:10:20 +00001220 const char *zFilename, /* Name of the file containing the BTree database */
drhe5fe6902007-12-07 18:55:28 +00001221 sqlite3 *db, /* Associated database handle */
drh3aac2dd2004-04-26 14:10:20 +00001222 Btree **ppBtree, /* Pointer to new Btree object written here */
drh33f4e022007-09-03 15:19:34 +00001223 int flags, /* Options */
1224 int vfsFlags /* Flags passed through to sqlite3_vfs.xOpen() */
drh6019e162001-07-02 17:51:45 +00001225){
drhd677b3d2007-08-20 22:48:41 +00001226 sqlite3_vfs *pVfs; /* The VFS to use for this btree */
drhe53831d2007-08-17 01:14:38 +00001227 BtShared *pBt = 0; /* Shared part of btree structure */
danielk1977aef0bf62005-12-30 16:28:01 +00001228 Btree *p; /* Handle to return */
danielk1977dddbcdc2007-04-26 14:42:34 +00001229 int rc = SQLITE_OK;
drhf49661a2008-12-10 16:45:50 +00001230 u8 nReserve;
drh90f5ecb2004-07-22 01:19:35 +00001231 unsigned char zDbHeader[100];
danielk1977aef0bf62005-12-30 16:28:01 +00001232
1233 /* Set the variable isMemdb to true for an in-memory database, or
1234 ** false for a file-based database. This symbol is only required if
1235 ** either of the shared-data or autovacuum features are compiled
1236 ** into the library.
1237 */
1238#if !defined(SQLITE_OMIT_SHARED_CACHE) || !defined(SQLITE_OMIT_AUTOVACUUM)
1239 #ifdef SQLITE_OMIT_MEMORYDB
drh980b1a72006-08-16 16:42:48 +00001240 const int isMemdb = 0;
danielk1977aef0bf62005-12-30 16:28:01 +00001241 #else
drh980b1a72006-08-16 16:42:48 +00001242 const int isMemdb = zFilename && !strcmp(zFilename, ":memory:");
danielk1977aef0bf62005-12-30 16:28:01 +00001243 #endif
1244#endif
1245
drhe5fe6902007-12-07 18:55:28 +00001246 assert( db!=0 );
1247 assert( sqlite3_mutex_held(db->mutex) );
drh153c62c2007-08-24 03:51:33 +00001248
drhe5fe6902007-12-07 18:55:28 +00001249 pVfs = db->pVfs;
drh17435752007-08-16 04:30:38 +00001250 p = sqlite3MallocZero(sizeof(Btree));
danielk1977aef0bf62005-12-30 16:28:01 +00001251 if( !p ){
1252 return SQLITE_NOMEM;
1253 }
1254 p->inTrans = TRANS_NONE;
drhe5fe6902007-12-07 18:55:28 +00001255 p->db = db;
danielk1977aef0bf62005-12-30 16:28:01 +00001256
drh198bf392006-01-06 21:52:49 +00001257#if !defined(SQLITE_OMIT_SHARED_CACHE) && !defined(SQLITE_OMIT_DISKIO)
drhe53831d2007-08-17 01:14:38 +00001258 /*
1259 ** If this Btree is a candidate for shared cache, try to find an
1260 ** existing BtShared object that we can share with
1261 */
drh34004ce2008-07-11 16:15:17 +00001262 if( isMemdb==0
drhe5fe6902007-12-07 18:55:28 +00001263 && (db->flags & SQLITE_Vtab)==0
drhe53831d2007-08-17 01:14:38 +00001264 && zFilename && zFilename[0]
drhe53831d2007-08-17 01:14:38 +00001265 ){
danielk1977502b4e02008-09-02 14:07:24 +00001266 if( sqlite3GlobalConfig.sharedCacheEnabled ){
danielk1977adfb9b02007-09-17 07:02:56 +00001267 int nFullPathname = pVfs->mxPathname+1;
drhe5ae5732008-06-15 02:51:47 +00001268 char *zFullPathname = sqlite3Malloc(nFullPathname);
drhff0587c2007-08-29 17:43:19 +00001269 sqlite3_mutex *mutexShared;
1270 p->sharable = 1;
drh34004ce2008-07-11 16:15:17 +00001271 db->flags |= SQLITE_SharedCache;
drhff0587c2007-08-29 17:43:19 +00001272 if( !zFullPathname ){
1273 sqlite3_free(p);
1274 return SQLITE_NOMEM;
1275 }
danielk1977adfb9b02007-09-17 07:02:56 +00001276 sqlite3OsFullPathname(pVfs, zFilename, nFullPathname, zFullPathname);
danielk197759f8c082008-06-18 17:09:10 +00001277 mutexShared = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER);
drhff0587c2007-08-29 17:43:19 +00001278 sqlite3_mutex_enter(mutexShared);
drh78f82d12008-09-02 00:52:52 +00001279 for(pBt=GLOBAL(BtShared*,sqlite3SharedCacheList); pBt; pBt=pBt->pNext){
drhff0587c2007-08-29 17:43:19 +00001280 assert( pBt->nRef>0 );
1281 if( 0==strcmp(zFullPathname, sqlite3PagerFilename(pBt->pPager))
1282 && sqlite3PagerVfs(pBt->pPager)==pVfs ){
1283 p->pBt = pBt;
1284 pBt->nRef++;
1285 break;
1286 }
1287 }
1288 sqlite3_mutex_leave(mutexShared);
1289 sqlite3_free(zFullPathname);
danielk1977aef0bf62005-12-30 16:28:01 +00001290 }
drhff0587c2007-08-29 17:43:19 +00001291#ifdef SQLITE_DEBUG
1292 else{
1293 /* In debug mode, we mark all persistent databases as sharable
1294 ** even when they are not. This exercises the locking code and
1295 ** gives more opportunity for asserts(sqlite3_mutex_held())
1296 ** statements to find locking problems.
1297 */
1298 p->sharable = 1;
1299 }
1300#endif
danielk1977aef0bf62005-12-30 16:28:01 +00001301 }
1302#endif
drha059ad02001-04-17 20:09:11 +00001303 if( pBt==0 ){
drhe53831d2007-08-17 01:14:38 +00001304 /*
1305 ** The following asserts make sure that structures used by the btree are
1306 ** the right size. This is to guard against size changes that result
1307 ** when compiling on a different architecture.
danielk197703aded42004-11-22 05:26:27 +00001308 */
drhe53831d2007-08-17 01:14:38 +00001309 assert( sizeof(i64)==8 || sizeof(i64)==4 );
1310 assert( sizeof(u64)==8 || sizeof(u64)==4 );
1311 assert( sizeof(u32)==4 );
1312 assert( sizeof(u16)==2 );
1313 assert( sizeof(Pgno)==4 );
1314
1315 pBt = sqlite3MallocZero( sizeof(*pBt) );
1316 if( pBt==0 ){
1317 rc = SQLITE_NOMEM;
1318 goto btree_open_out;
1319 }
danielk197771d5d2c2008-09-29 11:49:47 +00001320 rc = sqlite3PagerOpen(pVfs, &pBt->pPager, zFilename,
drh33f4e022007-09-03 15:19:34 +00001321 EXTRA_SIZE, flags, vfsFlags);
drhe53831d2007-08-17 01:14:38 +00001322 if( rc==SQLITE_OK ){
1323 rc = sqlite3PagerReadFileheader(pBt->pPager,sizeof(zDbHeader),zDbHeader);
1324 }
1325 if( rc!=SQLITE_OK ){
1326 goto btree_open_out;
1327 }
danielk19771ceedd32008-11-19 10:22:33 +00001328 sqlite3PagerSetBusyhandler(pBt->pPager, btreeInvokeBusyHandler, pBt);
drhe53831d2007-08-17 01:14:38 +00001329 p->pBt = pBt;
1330
drhe53831d2007-08-17 01:14:38 +00001331 sqlite3PagerSetReiniter(pBt->pPager, pageReinit);
1332 pBt->pCursor = 0;
1333 pBt->pPage1 = 0;
1334 pBt->readOnly = sqlite3PagerIsreadonly(pBt->pPager);
1335 pBt->pageSize = get2byte(&zDbHeader[16]);
1336 if( pBt->pageSize<512 || pBt->pageSize>SQLITE_MAX_PAGE_SIZE
1337 || ((pBt->pageSize-1)&pBt->pageSize)!=0 ){
danielk1977a1644fd2007-08-29 12:31:25 +00001338 pBt->pageSize = 0;
1339 sqlite3PagerSetPagesize(pBt->pPager, &pBt->pageSize);
drhe53831d2007-08-17 01:14:38 +00001340#ifndef SQLITE_OMIT_AUTOVACUUM
1341 /* If the magic name ":memory:" will create an in-memory database, then
1342 ** leave the autoVacuum mode at 0 (do not auto-vacuum), even if
1343 ** SQLITE_DEFAULT_AUTOVACUUM is true. On the other hand, if
1344 ** SQLITE_OMIT_MEMORYDB has been defined, then ":memory:" is just a
1345 ** regular file-name. In this case the auto-vacuum applies as per normal.
1346 */
1347 if( zFilename && !isMemdb ){
1348 pBt->autoVacuum = (SQLITE_DEFAULT_AUTOVACUUM ? 1 : 0);
1349 pBt->incrVacuum = (SQLITE_DEFAULT_AUTOVACUUM==2 ? 1 : 0);
1350 }
1351#endif
1352 nReserve = 0;
1353 }else{
1354 nReserve = zDbHeader[20];
drhe53831d2007-08-17 01:14:38 +00001355 pBt->pageSizeFixed = 1;
1356#ifndef SQLITE_OMIT_AUTOVACUUM
1357 pBt->autoVacuum = (get4byte(&zDbHeader[36 + 4*4])?1:0);
1358 pBt->incrVacuum = (get4byte(&zDbHeader[36 + 7*4])?1:0);
1359#endif
1360 }
1361 pBt->usableSize = pBt->pageSize - nReserve;
1362 assert( (pBt->pageSize & 7)==0 ); /* 8-byte alignment of pageSize */
danielk1977a1644fd2007-08-29 12:31:25 +00001363 sqlite3PagerSetPagesize(pBt->pPager, &pBt->pageSize);
drhe53831d2007-08-17 01:14:38 +00001364
1365#if !defined(SQLITE_OMIT_SHARED_CACHE) && !defined(SQLITE_OMIT_DISKIO)
1366 /* Add the new BtShared object to the linked list sharable BtShareds.
1367 */
1368 if( p->sharable ){
1369 sqlite3_mutex *mutexShared;
1370 pBt->nRef = 1;
danielk197759f8c082008-06-18 17:09:10 +00001371 mutexShared = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER);
danielk1977075c23a2008-09-01 18:34:20 +00001372 if( SQLITE_THREADSAFE && sqlite3GlobalConfig.bCoreMutex ){
danielk197759f8c082008-06-18 17:09:10 +00001373 pBt->mutex = sqlite3MutexAlloc(SQLITE_MUTEX_FAST);
drh3285db22007-09-03 22:00:39 +00001374 if( pBt->mutex==0 ){
1375 rc = SQLITE_NOMEM;
drhe5fe6902007-12-07 18:55:28 +00001376 db->mallocFailed = 0;
drh3285db22007-09-03 22:00:39 +00001377 goto btree_open_out;
1378 }
drhff0587c2007-08-29 17:43:19 +00001379 }
drhe53831d2007-08-17 01:14:38 +00001380 sqlite3_mutex_enter(mutexShared);
drh78f82d12008-09-02 00:52:52 +00001381 pBt->pNext = GLOBAL(BtShared*,sqlite3SharedCacheList);
1382 GLOBAL(BtShared*,sqlite3SharedCacheList) = pBt;
drhe53831d2007-08-17 01:14:38 +00001383 sqlite3_mutex_leave(mutexShared);
danielk1977951af802004-11-05 15:45:09 +00001384 }
drheee46cf2004-11-06 00:02:48 +00001385#endif
drh90f5ecb2004-07-22 01:19:35 +00001386 }
danielk1977aef0bf62005-12-30 16:28:01 +00001387
drhcfed7bc2006-03-13 14:28:05 +00001388#if !defined(SQLITE_OMIT_SHARED_CACHE) && !defined(SQLITE_OMIT_DISKIO)
drhe53831d2007-08-17 01:14:38 +00001389 /* If the new Btree uses a sharable pBtShared, then link the new
1390 ** Btree into the list of all sharable Btrees for the same connection.
drhabddb0c2007-08-20 13:14:28 +00001391 ** The list is kept in ascending order by pBt address.
danielk197754f01982006-01-18 15:25:17 +00001392 */
drhe53831d2007-08-17 01:14:38 +00001393 if( p->sharable ){
1394 int i;
1395 Btree *pSib;
drhe5fe6902007-12-07 18:55:28 +00001396 for(i=0; i<db->nDb; i++){
1397 if( (pSib = db->aDb[i].pBt)!=0 && pSib->sharable ){
drhe53831d2007-08-17 01:14:38 +00001398 while( pSib->pPrev ){ pSib = pSib->pPrev; }
1399 if( p->pBt<pSib->pBt ){
1400 p->pNext = pSib;
1401 p->pPrev = 0;
1402 pSib->pPrev = p;
1403 }else{
drhabddb0c2007-08-20 13:14:28 +00001404 while( pSib->pNext && pSib->pNext->pBt<p->pBt ){
drhe53831d2007-08-17 01:14:38 +00001405 pSib = pSib->pNext;
1406 }
1407 p->pNext = pSib->pNext;
1408 p->pPrev = pSib;
1409 if( p->pNext ){
1410 p->pNext->pPrev = p;
1411 }
1412 pSib->pNext = p;
1413 }
1414 break;
1415 }
1416 }
danielk1977aef0bf62005-12-30 16:28:01 +00001417 }
danielk1977aef0bf62005-12-30 16:28:01 +00001418#endif
1419 *ppBtree = p;
danielk1977dddbcdc2007-04-26 14:42:34 +00001420
1421btree_open_out:
1422 if( rc!=SQLITE_OK ){
1423 if( pBt && pBt->pPager ){
1424 sqlite3PagerClose(pBt->pPager);
1425 }
drh17435752007-08-16 04:30:38 +00001426 sqlite3_free(pBt);
1427 sqlite3_free(p);
danielk1977dddbcdc2007-04-26 14:42:34 +00001428 *ppBtree = 0;
1429 }
1430 return rc;
drha059ad02001-04-17 20:09:11 +00001431}
1432
1433/*
drhe53831d2007-08-17 01:14:38 +00001434** Decrement the BtShared.nRef counter. When it reaches zero,
1435** remove the BtShared structure from the sharing list. Return
1436** true if the BtShared.nRef counter reaches zero and return
1437** false if it is still positive.
1438*/
1439static int removeFromSharingList(BtShared *pBt){
1440#ifndef SQLITE_OMIT_SHARED_CACHE
1441 sqlite3_mutex *pMaster;
1442 BtShared *pList;
1443 int removed = 0;
1444
drhd677b3d2007-08-20 22:48:41 +00001445 assert( sqlite3_mutex_notheld(pBt->mutex) );
danielk197759f8c082008-06-18 17:09:10 +00001446 pMaster = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER);
drhe53831d2007-08-17 01:14:38 +00001447 sqlite3_mutex_enter(pMaster);
1448 pBt->nRef--;
1449 if( pBt->nRef<=0 ){
drh78f82d12008-09-02 00:52:52 +00001450 if( GLOBAL(BtShared*,sqlite3SharedCacheList)==pBt ){
1451 GLOBAL(BtShared*,sqlite3SharedCacheList) = pBt->pNext;
drhe53831d2007-08-17 01:14:38 +00001452 }else{
drh78f82d12008-09-02 00:52:52 +00001453 pList = GLOBAL(BtShared*,sqlite3SharedCacheList);
drh34004ce2008-07-11 16:15:17 +00001454 while( ALWAYS(pList) && pList->pNext!=pBt ){
drhe53831d2007-08-17 01:14:38 +00001455 pList=pList->pNext;
1456 }
drh34004ce2008-07-11 16:15:17 +00001457 if( ALWAYS(pList) ){
drhe53831d2007-08-17 01:14:38 +00001458 pList->pNext = pBt->pNext;
1459 }
1460 }
drh3285db22007-09-03 22:00:39 +00001461 if( SQLITE_THREADSAFE ){
1462 sqlite3_mutex_free(pBt->mutex);
1463 }
drhe53831d2007-08-17 01:14:38 +00001464 removed = 1;
1465 }
1466 sqlite3_mutex_leave(pMaster);
1467 return removed;
1468#else
1469 return 1;
1470#endif
1471}
1472
1473/*
drhf7141992008-06-19 00:16:08 +00001474** Make sure pBt->pTmpSpace points to an allocation of
1475** MX_CELL_SIZE(pBt) bytes.
1476*/
1477static void allocateTempSpace(BtShared *pBt){
1478 if( !pBt->pTmpSpace ){
1479 pBt->pTmpSpace = sqlite3PageMalloc( pBt->pageSize );
1480 }
1481}
1482
1483/*
1484** Free the pBt->pTmpSpace allocation
1485*/
1486static void freeTempSpace(BtShared *pBt){
1487 sqlite3PageFree( pBt->pTmpSpace);
1488 pBt->pTmpSpace = 0;
1489}
1490
1491/*
drha059ad02001-04-17 20:09:11 +00001492** Close an open database and invalidate all cursors.
1493*/
danielk1977aef0bf62005-12-30 16:28:01 +00001494int sqlite3BtreeClose(Btree *p){
danielk1977aef0bf62005-12-30 16:28:01 +00001495 BtShared *pBt = p->pBt;
1496 BtCursor *pCur;
1497
danielk1977aef0bf62005-12-30 16:28:01 +00001498 /* Close all cursors opened via this handle. */
drhe5fe6902007-12-07 18:55:28 +00001499 assert( sqlite3_mutex_held(p->db->mutex) );
drhe53831d2007-08-17 01:14:38 +00001500 sqlite3BtreeEnter(p);
drhe5fe6902007-12-07 18:55:28 +00001501 pBt->db = p->db;
danielk1977aef0bf62005-12-30 16:28:01 +00001502 pCur = pBt->pCursor;
1503 while( pCur ){
1504 BtCursor *pTmp = pCur;
1505 pCur = pCur->pNext;
1506 if( pTmp->pBtree==p ){
1507 sqlite3BtreeCloseCursor(pTmp);
1508 }
drha059ad02001-04-17 20:09:11 +00001509 }
danielk1977aef0bf62005-12-30 16:28:01 +00001510
danielk19778d34dfd2006-01-24 16:37:57 +00001511 /* Rollback any active transaction and free the handle structure.
1512 ** The call to sqlite3BtreeRollback() drops any table-locks held by
1513 ** this handle.
1514 */
danielk1977b597f742006-01-15 11:39:18 +00001515 sqlite3BtreeRollback(p);
drhe53831d2007-08-17 01:14:38 +00001516 sqlite3BtreeLeave(p);
danielk1977aef0bf62005-12-30 16:28:01 +00001517
danielk1977aef0bf62005-12-30 16:28:01 +00001518 /* If there are still other outstanding references to the shared-btree
1519 ** structure, return now. The remainder of this procedure cleans
1520 ** up the shared-btree.
1521 */
drhe53831d2007-08-17 01:14:38 +00001522 assert( p->wantToLock==0 && p->locked==0 );
1523 if( !p->sharable || removeFromSharingList(pBt) ){
1524 /* The pBt is no longer on the sharing list, so we can access
1525 ** it without having to hold the mutex.
1526 **
1527 ** Clean out and delete the BtShared object.
1528 */
1529 assert( !pBt->pCursor );
drhe53831d2007-08-17 01:14:38 +00001530 sqlite3PagerClose(pBt->pPager);
1531 if( pBt->xFreeSchema && pBt->pSchema ){
1532 pBt->xFreeSchema(pBt->pSchema);
1533 }
1534 sqlite3_free(pBt->pSchema);
drhf7141992008-06-19 00:16:08 +00001535 freeTempSpace(pBt);
drh65bbf292008-06-19 01:03:17 +00001536 sqlite3_free(pBt);
danielk1977aef0bf62005-12-30 16:28:01 +00001537 }
1538
drhe53831d2007-08-17 01:14:38 +00001539#ifndef SQLITE_OMIT_SHARED_CACHE
drhcab5ed72007-08-22 11:41:18 +00001540 assert( p->wantToLock==0 );
1541 assert( p->locked==0 );
1542 if( p->pPrev ) p->pPrev->pNext = p->pNext;
1543 if( p->pNext ) p->pNext->pPrev = p->pPrev;
danielk1977aef0bf62005-12-30 16:28:01 +00001544#endif
1545
drhe53831d2007-08-17 01:14:38 +00001546 sqlite3_free(p);
drha059ad02001-04-17 20:09:11 +00001547 return SQLITE_OK;
1548}
1549
1550/*
drhda47d772002-12-02 04:25:19 +00001551** Change the limit on the number of pages allowed in the cache.
drhcd61c282002-03-06 22:01:34 +00001552**
1553** The maximum number of cache pages is set to the absolute
1554** value of mxPage. If mxPage is negative, the pager will
1555** operate asynchronously - it will not stop to do fsync()s
1556** to insure data is written to the disk surface before
1557** continuing. Transactions still work if synchronous is off,
1558** and the database cannot be corrupted if this program
1559** crashes. But if the operating system crashes or there is
1560** an abrupt power failure when synchronous is off, the database
1561** could be left in an inconsistent and unrecoverable state.
1562** Synchronous is on by default so database corruption is not
1563** normally a worry.
drhf57b14a2001-09-14 18:54:08 +00001564*/
danielk1977aef0bf62005-12-30 16:28:01 +00001565int sqlite3BtreeSetCacheSize(Btree *p, int mxPage){
1566 BtShared *pBt = p->pBt;
drhe5fe6902007-12-07 18:55:28 +00001567 assert( sqlite3_mutex_held(p->db->mutex) );
drhd677b3d2007-08-20 22:48:41 +00001568 sqlite3BtreeEnter(p);
danielk19773b8a05f2007-03-19 17:44:26 +00001569 sqlite3PagerSetCachesize(pBt->pPager, mxPage);
drhd677b3d2007-08-20 22:48:41 +00001570 sqlite3BtreeLeave(p);
drhf57b14a2001-09-14 18:54:08 +00001571 return SQLITE_OK;
1572}
1573
1574/*
drh973b6e32003-02-12 14:09:42 +00001575** Change the way data is synced to disk in order to increase or decrease
1576** how well the database resists damage due to OS crashes and power
1577** failures. Level 1 is the same as asynchronous (no syncs() occur and
1578** there is a high probability of damage) Level 2 is the default. There
1579** is a very low but non-zero probability of damage. Level 3 reduces the
1580** probability of damage to near zero but with a write performance reduction.
1581*/
danielk197793758c82005-01-21 08:13:14 +00001582#ifndef SQLITE_OMIT_PAGER_PRAGMAS
drhac530b12006-02-11 01:25:50 +00001583int sqlite3BtreeSetSafetyLevel(Btree *p, int level, int fullSync){
danielk1977aef0bf62005-12-30 16:28:01 +00001584 BtShared *pBt = p->pBt;
drhe5fe6902007-12-07 18:55:28 +00001585 assert( sqlite3_mutex_held(p->db->mutex) );
drhd677b3d2007-08-20 22:48:41 +00001586 sqlite3BtreeEnter(p);
danielk19773b8a05f2007-03-19 17:44:26 +00001587 sqlite3PagerSetSafetyLevel(pBt->pPager, level, fullSync);
drhd677b3d2007-08-20 22:48:41 +00001588 sqlite3BtreeLeave(p);
drh973b6e32003-02-12 14:09:42 +00001589 return SQLITE_OK;
1590}
danielk197793758c82005-01-21 08:13:14 +00001591#endif
drh973b6e32003-02-12 14:09:42 +00001592
drh2c8997b2005-08-27 16:36:48 +00001593/*
1594** Return TRUE if the given btree is set to safety level 1. In other
1595** words, return TRUE if no sync() occurs on the disk files.
1596*/
danielk1977aef0bf62005-12-30 16:28:01 +00001597int sqlite3BtreeSyncDisabled(Btree *p){
1598 BtShared *pBt = p->pBt;
drhd677b3d2007-08-20 22:48:41 +00001599 int rc;
drhe5fe6902007-12-07 18:55:28 +00001600 assert( sqlite3_mutex_held(p->db->mutex) );
drhd677b3d2007-08-20 22:48:41 +00001601 sqlite3BtreeEnter(p);
drhd0679ed2007-08-28 22:24:34 +00001602 assert( pBt && pBt->pPager );
drhd677b3d2007-08-20 22:48:41 +00001603 rc = sqlite3PagerNosync(pBt->pPager);
1604 sqlite3BtreeLeave(p);
1605 return rc;
drh2c8997b2005-08-27 16:36:48 +00001606}
1607
danielk1977576ec6b2005-01-21 11:55:25 +00001608#if !defined(SQLITE_OMIT_PAGER_PRAGMAS) || !defined(SQLITE_OMIT_VACUUM)
drh973b6e32003-02-12 14:09:42 +00001609/*
drh90f5ecb2004-07-22 01:19:35 +00001610** Change the default pages size and the number of reserved bytes per page.
drh06f50212004-11-02 14:24:33 +00001611**
1612** The page size must be a power of 2 between 512 and 65536. If the page
1613** size supplied does not meet this constraint then the page size is not
1614** changed.
1615**
1616** Page sizes are constrained to be a power of two so that the region
1617** of the database file used for locking (beginning at PENDING_BYTE,
1618** the first byte past the 1GB boundary, 0x40000000) needs to occur
1619** at the beginning of a page.
danielk197728129562005-01-11 10:25:06 +00001620**
1621** If parameter nReserve is less than zero, then the number of reserved
1622** bytes per page is left unchanged.
drh90f5ecb2004-07-22 01:19:35 +00001623*/
danielk1977aef0bf62005-12-30 16:28:01 +00001624int sqlite3BtreeSetPageSize(Btree *p, int pageSize, int nReserve){
danielk1977a1644fd2007-08-29 12:31:25 +00001625 int rc = SQLITE_OK;
danielk1977aef0bf62005-12-30 16:28:01 +00001626 BtShared *pBt = p->pBt;
drhf49661a2008-12-10 16:45:50 +00001627 assert( nReserve>=-1 && nReserve<=255 );
drhd677b3d2007-08-20 22:48:41 +00001628 sqlite3BtreeEnter(p);
drh90f5ecb2004-07-22 01:19:35 +00001629 if( pBt->pageSizeFixed ){
drhd677b3d2007-08-20 22:48:41 +00001630 sqlite3BtreeLeave(p);
drh90f5ecb2004-07-22 01:19:35 +00001631 return SQLITE_READONLY;
1632 }
1633 if( nReserve<0 ){
1634 nReserve = pBt->pageSize - pBt->usableSize;
1635 }
drhf49661a2008-12-10 16:45:50 +00001636 assert( nReserve>=0 && nReserve<=255 );
drh06f50212004-11-02 14:24:33 +00001637 if( pageSize>=512 && pageSize<=SQLITE_MAX_PAGE_SIZE &&
1638 ((pageSize-1)&pageSize)==0 ){
drh07d183d2005-05-01 22:52:42 +00001639 assert( (pageSize & 7)==0 );
danielk1977aef0bf62005-12-30 16:28:01 +00001640 assert( !pBt->pPage1 && !pBt->pCursor );
drh1bd10f82008-12-10 21:19:56 +00001641 pBt->pageSize = (u16)pageSize;
drhf7141992008-06-19 00:16:08 +00001642 freeTempSpace(pBt);
danielk1977a1644fd2007-08-29 12:31:25 +00001643 rc = sqlite3PagerSetPagesize(pBt->pPager, &pBt->pageSize);
drh90f5ecb2004-07-22 01:19:35 +00001644 }
drhf49661a2008-12-10 16:45:50 +00001645 pBt->usableSize = pBt->pageSize - (u16)nReserve;
drhd677b3d2007-08-20 22:48:41 +00001646 sqlite3BtreeLeave(p);
danielk1977a1644fd2007-08-29 12:31:25 +00001647 return rc;
drh90f5ecb2004-07-22 01:19:35 +00001648}
1649
1650/*
1651** Return the currently defined page size
1652*/
danielk1977aef0bf62005-12-30 16:28:01 +00001653int sqlite3BtreeGetPageSize(Btree *p){
1654 return p->pBt->pageSize;
drh90f5ecb2004-07-22 01:19:35 +00001655}
danielk1977aef0bf62005-12-30 16:28:01 +00001656int sqlite3BtreeGetReserve(Btree *p){
drhd677b3d2007-08-20 22:48:41 +00001657 int n;
1658 sqlite3BtreeEnter(p);
1659 n = p->pBt->pageSize - p->pBt->usableSize;
1660 sqlite3BtreeLeave(p);
1661 return n;
drh2011d5f2004-07-22 02:40:37 +00001662}
drhf8e632b2007-05-08 14:51:36 +00001663
1664/*
1665** Set the maximum page count for a database if mxPage is positive.
1666** No changes are made if mxPage is 0 or negative.
1667** Regardless of the value of mxPage, return the maximum page count.
1668*/
1669int sqlite3BtreeMaxPageCount(Btree *p, int mxPage){
drhd677b3d2007-08-20 22:48:41 +00001670 int n;
1671 sqlite3BtreeEnter(p);
1672 n = sqlite3PagerMaxPageCount(p->pBt->pPager, mxPage);
1673 sqlite3BtreeLeave(p);
1674 return n;
drhf8e632b2007-05-08 14:51:36 +00001675}
danielk1977576ec6b2005-01-21 11:55:25 +00001676#endif /* !defined(SQLITE_OMIT_PAGER_PRAGMAS) || !defined(SQLITE_OMIT_VACUUM) */
drh90f5ecb2004-07-22 01:19:35 +00001677
1678/*
danielk1977951af802004-11-05 15:45:09 +00001679** Change the 'auto-vacuum' property of the database. If the 'autoVacuum'
1680** parameter is non-zero, then auto-vacuum mode is enabled. If zero, it
1681** is disabled. The default value for the auto-vacuum property is
1682** determined by the SQLITE_DEFAULT_AUTOVACUUM macro.
1683*/
danielk1977aef0bf62005-12-30 16:28:01 +00001684int sqlite3BtreeSetAutoVacuum(Btree *p, int autoVacuum){
danielk1977951af802004-11-05 15:45:09 +00001685#ifdef SQLITE_OMIT_AUTOVACUUM
drheee46cf2004-11-06 00:02:48 +00001686 return SQLITE_READONLY;
danielk1977951af802004-11-05 15:45:09 +00001687#else
danielk1977dddbcdc2007-04-26 14:42:34 +00001688 BtShared *pBt = p->pBt;
drhd677b3d2007-08-20 22:48:41 +00001689 int rc = SQLITE_OK;
drhf49661a2008-12-10 16:45:50 +00001690 u8 av = autoVacuum ?1:0;
drhd677b3d2007-08-20 22:48:41 +00001691
1692 sqlite3BtreeEnter(p);
danielk1977dddbcdc2007-04-26 14:42:34 +00001693 if( pBt->pageSizeFixed && av!=pBt->autoVacuum ){
drhd677b3d2007-08-20 22:48:41 +00001694 rc = SQLITE_READONLY;
1695 }else{
1696 pBt->autoVacuum = av;
danielk1977951af802004-11-05 15:45:09 +00001697 }
drhd677b3d2007-08-20 22:48:41 +00001698 sqlite3BtreeLeave(p);
1699 return rc;
danielk1977951af802004-11-05 15:45:09 +00001700#endif
1701}
1702
1703/*
1704** Return the value of the 'auto-vacuum' property. If auto-vacuum is
1705** enabled 1 is returned. Otherwise 0.
1706*/
danielk1977aef0bf62005-12-30 16:28:01 +00001707int sqlite3BtreeGetAutoVacuum(Btree *p){
danielk1977951af802004-11-05 15:45:09 +00001708#ifdef SQLITE_OMIT_AUTOVACUUM
danielk1977dddbcdc2007-04-26 14:42:34 +00001709 return BTREE_AUTOVACUUM_NONE;
danielk1977951af802004-11-05 15:45:09 +00001710#else
drhd677b3d2007-08-20 22:48:41 +00001711 int rc;
1712 sqlite3BtreeEnter(p);
1713 rc = (
danielk1977dddbcdc2007-04-26 14:42:34 +00001714 (!p->pBt->autoVacuum)?BTREE_AUTOVACUUM_NONE:
1715 (!p->pBt->incrVacuum)?BTREE_AUTOVACUUM_FULL:
1716 BTREE_AUTOVACUUM_INCR
1717 );
drhd677b3d2007-08-20 22:48:41 +00001718 sqlite3BtreeLeave(p);
1719 return rc;
danielk1977951af802004-11-05 15:45:09 +00001720#endif
1721}
1722
1723
1724/*
drha34b6762004-05-07 13:30:42 +00001725** Get a reference to pPage1 of the database file. This will
drh306dc212001-05-21 13:45:10 +00001726** also acquire a readlock on that file.
1727**
1728** SQLITE_OK is returned on success. If the file is not a
1729** well-formed database file, then SQLITE_CORRUPT is returned.
1730** SQLITE_BUSY is returned if the database is locked. SQLITE_NOMEM
drh4f0ee682007-03-30 20:43:40 +00001731** is returned if we run out of memory.
drh306dc212001-05-21 13:45:10 +00001732*/
danielk1977aef0bf62005-12-30 16:28:01 +00001733static int lockBtree(BtShared *pBt){
danielk1977f653d782008-03-20 11:04:21 +00001734 int rc;
drh3aac2dd2004-04-26 14:10:20 +00001735 MemPage *pPage1;
danielk197793f7af92008-05-09 16:57:50 +00001736 int nPage;
drhd677b3d2007-08-20 22:48:41 +00001737
drh1fee73e2007-08-29 04:00:57 +00001738 assert( sqlite3_mutex_held(pBt->mutex) );
drha34b6762004-05-07 13:30:42 +00001739 if( pBt->pPage1 ) return SQLITE_OK;
drh16a9b832007-05-05 18:39:25 +00001740 rc = sqlite3BtreeGetPage(pBt, 1, &pPage1, 0);
drh306dc212001-05-21 13:45:10 +00001741 if( rc!=SQLITE_OK ) return rc;
drh306dc212001-05-21 13:45:10 +00001742
1743 /* Do some checking to help insure the file we opened really is
1744 ** a valid database file.
1745 */
danielk1977ad0132d2008-06-07 08:58:22 +00001746 rc = sqlite3PagerPagecount(pBt->pPager, &nPage);
1747 if( rc!=SQLITE_OK ){
danielk197793f7af92008-05-09 16:57:50 +00001748 goto page1_init_failed;
1749 }else if( nPage>0 ){
danielk1977f653d782008-03-20 11:04:21 +00001750 int pageSize;
1751 int usableSize;
drhb6f41482004-05-14 01:58:11 +00001752 u8 *page1 = pPage1->aData;
danielk1977ad0132d2008-06-07 08:58:22 +00001753 rc = SQLITE_NOTADB;
drhb6f41482004-05-14 01:58:11 +00001754 if( memcmp(page1, zMagicHeader, 16)!=0 ){
drh72f82862001-05-24 21:06:34 +00001755 goto page1_init_failed;
drh306dc212001-05-21 13:45:10 +00001756 }
drh309169a2007-04-24 17:27:51 +00001757 if( page1[18]>1 ){
1758 pBt->readOnly = 1;
1759 }
1760 if( page1[19]>1 ){
drhb6f41482004-05-14 01:58:11 +00001761 goto page1_init_failed;
1762 }
drhe5ae5732008-06-15 02:51:47 +00001763
1764 /* The maximum embedded fraction must be exactly 25%. And the minimum
1765 ** embedded fraction must be 12.5% for both leaf-data and non-leaf-data.
1766 ** The original design allowed these amounts to vary, but as of
1767 ** version 3.6.0, we require them to be fixed.
1768 */
1769 if( memcmp(&page1[21], "\100\040\040",3)!=0 ){
1770 goto page1_init_failed;
1771 }
drh07d183d2005-05-01 22:52:42 +00001772 pageSize = get2byte(&page1[16]);
drh7dc385e2007-09-06 23:39:36 +00001773 if( ((pageSize-1)&pageSize)!=0 || pageSize<512 ||
1774 (SQLITE_MAX_PAGE_SIZE<32768 && pageSize>SQLITE_MAX_PAGE_SIZE)
1775 ){
drh07d183d2005-05-01 22:52:42 +00001776 goto page1_init_failed;
1777 }
1778 assert( (pageSize & 7)==0 );
danielk1977f653d782008-03-20 11:04:21 +00001779 usableSize = pageSize - page1[20];
1780 if( pageSize!=pBt->pageSize ){
1781 /* After reading the first page of the database assuming a page size
1782 ** of BtShared.pageSize, we have discovered that the page-size is
1783 ** actually pageSize. Unlock the database, leave pBt->pPage1 at
1784 ** zero and return SQLITE_OK. The caller will call this function
1785 ** again with the correct page-size.
1786 */
1787 releasePage(pPage1);
drhf49661a2008-12-10 16:45:50 +00001788 pBt->usableSize = (u16)usableSize;
1789 pBt->pageSize = (u16)pageSize;
drhf7141992008-06-19 00:16:08 +00001790 freeTempSpace(pBt);
danielk1977f653d782008-03-20 11:04:21 +00001791 sqlite3PagerSetPagesize(pBt->pPager, &pBt->pageSize);
1792 return SQLITE_OK;
1793 }
1794 if( usableSize<500 ){
drhb6f41482004-05-14 01:58:11 +00001795 goto page1_init_failed;
1796 }
drh1bd10f82008-12-10 21:19:56 +00001797 pBt->pageSize = (u16)pageSize;
1798 pBt->usableSize = (u16)usableSize;
drh057cd3a2005-02-15 16:23:02 +00001799#ifndef SQLITE_OMIT_AUTOVACUUM
1800 pBt->autoVacuum = (get4byte(&page1[36 + 4*4])?1:0);
danielk197727b1f952007-06-25 08:16:58 +00001801 pBt->incrVacuum = (get4byte(&page1[36 + 7*4])?1:0);
drh057cd3a2005-02-15 16:23:02 +00001802#endif
drh306dc212001-05-21 13:45:10 +00001803 }
drhb6f41482004-05-14 01:58:11 +00001804
1805 /* maxLocal is the maximum amount of payload to store locally for
1806 ** a cell. Make sure it is small enough so that at least minFanout
1807 ** cells can will fit on one page. We assume a 10-byte page header.
1808 ** Besides the payload, the cell must store:
drh43605152004-05-29 21:46:49 +00001809 ** 2-byte pointer to the cell
drhb6f41482004-05-14 01:58:11 +00001810 ** 4-byte child pointer
1811 ** 9-byte nKey value
1812 ** 4-byte nData value
1813 ** 4-byte overflow page pointer
drh43605152004-05-29 21:46:49 +00001814 ** So a cell consists of a 2-byte poiner, a header which is as much as
1815 ** 17 bytes long, 0 to N bytes of payload, and an optional 4 byte overflow
1816 ** page pointer.
drhb6f41482004-05-14 01:58:11 +00001817 */
drhe5ae5732008-06-15 02:51:47 +00001818 pBt->maxLocal = (pBt->usableSize-12)*64/255 - 23;
1819 pBt->minLocal = (pBt->usableSize-12)*32/255 - 23;
drh43605152004-05-29 21:46:49 +00001820 pBt->maxLeaf = pBt->usableSize - 35;
drhe5ae5732008-06-15 02:51:47 +00001821 pBt->minLeaf = (pBt->usableSize-12)*32/255 - 23;
drh2e38c322004-09-03 18:38:44 +00001822 assert( pBt->maxLeaf + 23 <= MX_CELL_SIZE(pBt) );
drh3aac2dd2004-04-26 14:10:20 +00001823 pBt->pPage1 = pPage1;
drhb6f41482004-05-14 01:58:11 +00001824 return SQLITE_OK;
drh306dc212001-05-21 13:45:10 +00001825
drh72f82862001-05-24 21:06:34 +00001826page1_init_failed:
drh3aac2dd2004-04-26 14:10:20 +00001827 releasePage(pPage1);
1828 pBt->pPage1 = 0;
drh72f82862001-05-24 21:06:34 +00001829 return rc;
drh306dc212001-05-21 13:45:10 +00001830}
1831
1832/*
drhb8ef32c2005-03-14 02:01:49 +00001833** This routine works like lockBtree() except that it also invokes the
1834** busy callback if there is lock contention.
1835*/
danielk1977aef0bf62005-12-30 16:28:01 +00001836static int lockBtreeWithRetry(Btree *pRef){
drhb8ef32c2005-03-14 02:01:49 +00001837 int rc = SQLITE_OK;
drhd677b3d2007-08-20 22:48:41 +00001838
drh1fee73e2007-08-29 04:00:57 +00001839 assert( sqlite3BtreeHoldsMutex(pRef) );
danielk1977aef0bf62005-12-30 16:28:01 +00001840 if( pRef->inTrans==TRANS_NONE ){
1841 u8 inTransaction = pRef->pBt->inTransaction;
1842 btreeIntegrity(pRef);
1843 rc = sqlite3BtreeBeginTrans(pRef, 0);
1844 pRef->pBt->inTransaction = inTransaction;
1845 pRef->inTrans = TRANS_NONE;
1846 if( rc==SQLITE_OK ){
1847 pRef->pBt->nTransaction--;
1848 }
1849 btreeIntegrity(pRef);
drhb8ef32c2005-03-14 02:01:49 +00001850 }
1851 return rc;
1852}
1853
1854
1855/*
drhb8ca3072001-12-05 00:21:20 +00001856** If there are no outstanding cursors and we are not in the middle
1857** of a transaction but there is a read lock on the database, then
1858** this routine unrefs the first page of the database file which
1859** has the effect of releasing the read lock.
1860**
1861** If there are any outstanding cursors, this routine is a no-op.
1862**
1863** If there is a transaction in progress, this routine is a no-op.
1864*/
danielk1977aef0bf62005-12-30 16:28:01 +00001865static void unlockBtreeIfUnused(BtShared *pBt){
drh1fee73e2007-08-29 04:00:57 +00001866 assert( sqlite3_mutex_held(pBt->mutex) );
danielk1977aef0bf62005-12-30 16:28:01 +00001867 if( pBt->inTransaction==TRANS_NONE && pBt->pCursor==0 && pBt->pPage1!=0 ){
danielk19773b8a05f2007-03-19 17:44:26 +00001868 if( sqlite3PagerRefcount(pBt->pPager)>=1 ){
drhde4fcfd2008-01-19 23:50:26 +00001869 assert( pBt->pPage1->aData );
1870#if 0
drh24c9a2e2007-01-05 02:00:47 +00001871 if( pBt->pPage1->aData==0 ){
1872 MemPage *pPage = pBt->pPage1;
drhbf4bca52007-09-06 22:19:14 +00001873 pPage->aData = sqlite3PagerGetData(pPage->pDbPage);
drh24c9a2e2007-01-05 02:00:47 +00001874 pPage->pBt = pBt;
1875 pPage->pgno = 1;
1876 }
drhde4fcfd2008-01-19 23:50:26 +00001877#endif
drh24c9a2e2007-01-05 02:00:47 +00001878 releasePage(pBt->pPage1);
drh51c6d962004-06-06 00:42:25 +00001879 }
drh3aac2dd2004-04-26 14:10:20 +00001880 pBt->pPage1 = 0;
drh3aac2dd2004-04-26 14:10:20 +00001881 pBt->inStmt = 0;
drhb8ca3072001-12-05 00:21:20 +00001882 }
1883}
1884
1885/*
drh9e572e62004-04-23 23:43:10 +00001886** Create a new database by initializing the first page of the
drh8c42ca92001-06-22 19:15:00 +00001887** file.
drh8b2f49b2001-06-08 00:21:52 +00001888*/
danielk1977aef0bf62005-12-30 16:28:01 +00001889static int newDatabase(BtShared *pBt){
drh9e572e62004-04-23 23:43:10 +00001890 MemPage *pP1;
1891 unsigned char *data;
drh8c42ca92001-06-22 19:15:00 +00001892 int rc;
danielk1977ad0132d2008-06-07 08:58:22 +00001893 int nPage;
drhd677b3d2007-08-20 22:48:41 +00001894
drh1fee73e2007-08-29 04:00:57 +00001895 assert( sqlite3_mutex_held(pBt->mutex) );
danielk1977ad0132d2008-06-07 08:58:22 +00001896 rc = sqlite3PagerPagecount(pBt->pPager, &nPage);
1897 if( rc!=SQLITE_OK || nPage>0 ){
1898 return rc;
1899 }
drh3aac2dd2004-04-26 14:10:20 +00001900 pP1 = pBt->pPage1;
drh9e572e62004-04-23 23:43:10 +00001901 assert( pP1!=0 );
1902 data = pP1->aData;
danielk19773b8a05f2007-03-19 17:44:26 +00001903 rc = sqlite3PagerWrite(pP1->pDbPage);
drh8b2f49b2001-06-08 00:21:52 +00001904 if( rc ) return rc;
drh9e572e62004-04-23 23:43:10 +00001905 memcpy(data, zMagicHeader, sizeof(zMagicHeader));
1906 assert( sizeof(zMagicHeader)==16 );
drhb6f41482004-05-14 01:58:11 +00001907 put2byte(&data[16], pBt->pageSize);
drh9e572e62004-04-23 23:43:10 +00001908 data[18] = 1;
1909 data[19] = 1;
drhf49661a2008-12-10 16:45:50 +00001910 assert( pBt->usableSize<=pBt->pageSize && pBt->usableSize+255>=pBt->pageSize);
1911 data[20] = (u8)(pBt->pageSize - pBt->usableSize);
drhe5ae5732008-06-15 02:51:47 +00001912 data[21] = 64;
1913 data[22] = 32;
1914 data[23] = 32;
drhb6f41482004-05-14 01:58:11 +00001915 memset(&data[24], 0, 100-24);
drhe6c43812004-05-14 12:17:46 +00001916 zeroPage(pP1, PTF_INTKEY|PTF_LEAF|PTF_LEAFDATA );
drhf2a611c2004-09-05 00:33:43 +00001917 pBt->pageSizeFixed = 1;
danielk1977003ba062004-11-04 02:57:33 +00001918#ifndef SQLITE_OMIT_AUTOVACUUM
danielk1977dddbcdc2007-04-26 14:42:34 +00001919 assert( pBt->autoVacuum==1 || pBt->autoVacuum==0 );
danielk1977418899a2007-06-24 10:14:00 +00001920 assert( pBt->incrVacuum==1 || pBt->incrVacuum==0 );
danielk1977dddbcdc2007-04-26 14:42:34 +00001921 put4byte(&data[36 + 4*4], pBt->autoVacuum);
danielk1977418899a2007-06-24 10:14:00 +00001922 put4byte(&data[36 + 7*4], pBt->incrVacuum);
danielk1977003ba062004-11-04 02:57:33 +00001923#endif
drh8b2f49b2001-06-08 00:21:52 +00001924 return SQLITE_OK;
1925}
1926
1927/*
danielk1977ee5741e2004-05-31 10:01:34 +00001928** Attempt to start a new transaction. A write-transaction
drh684917c2004-10-05 02:41:42 +00001929** is started if the second argument is nonzero, otherwise a read-
1930** transaction. If the second argument is 2 or more and exclusive
1931** transaction is started, meaning that no other process is allowed
1932** to access the database. A preexisting transaction may not be
drhb8ef32c2005-03-14 02:01:49 +00001933** upgraded to exclusive by calling this routine a second time - the
drh684917c2004-10-05 02:41:42 +00001934** exclusivity flag only works for a new transaction.
drh8b2f49b2001-06-08 00:21:52 +00001935**
danielk1977ee5741e2004-05-31 10:01:34 +00001936** A write-transaction must be started before attempting any
1937** changes to the database. None of the following routines
1938** will work unless a transaction is started first:
drh8b2f49b2001-06-08 00:21:52 +00001939**
drh23e11ca2004-05-04 17:27:28 +00001940** sqlite3BtreeCreateTable()
1941** sqlite3BtreeCreateIndex()
1942** sqlite3BtreeClearTable()
1943** sqlite3BtreeDropTable()
1944** sqlite3BtreeInsert()
1945** sqlite3BtreeDelete()
1946** sqlite3BtreeUpdateMeta()
danielk197713adf8a2004-06-03 16:08:41 +00001947**
drhb8ef32c2005-03-14 02:01:49 +00001948** If an initial attempt to acquire the lock fails because of lock contention
1949** and the database was previously unlocked, then invoke the busy handler
1950** if there is one. But if there was previously a read-lock, do not
1951** invoke the busy handler - just return SQLITE_BUSY. SQLITE_BUSY is
1952** returned when there is already a read-lock in order to avoid a deadlock.
1953**
1954** Suppose there are two processes A and B. A has a read lock and B has
1955** a reserved lock. B tries to promote to exclusive but is blocked because
1956** of A's read lock. A tries to promote to reserved but is blocked by B.
1957** One or the other of the two processes must give way or there can be
1958** no progress. By returning SQLITE_BUSY and not invoking the busy callback
1959** when A already has a read lock, we encourage A to give up and let B
1960** proceed.
drha059ad02001-04-17 20:09:11 +00001961*/
danielk1977aef0bf62005-12-30 16:28:01 +00001962int sqlite3BtreeBeginTrans(Btree *p, int wrflag){
1963 BtShared *pBt = p->pBt;
danielk1977ee5741e2004-05-31 10:01:34 +00001964 int rc = SQLITE_OK;
1965
drhd677b3d2007-08-20 22:48:41 +00001966 sqlite3BtreeEnter(p);
drhe5fe6902007-12-07 18:55:28 +00001967 pBt->db = p->db;
danielk1977aef0bf62005-12-30 16:28:01 +00001968 btreeIntegrity(p);
1969
danielk1977ee5741e2004-05-31 10:01:34 +00001970 /* If the btree is already in a write-transaction, or it
1971 ** is already in a read-transaction and a read-transaction
1972 ** is requested, this is a no-op.
1973 */
danielk1977aef0bf62005-12-30 16:28:01 +00001974 if( p->inTrans==TRANS_WRITE || (p->inTrans==TRANS_READ && !wrflag) ){
drhd677b3d2007-08-20 22:48:41 +00001975 goto trans_begun;
danielk1977ee5741e2004-05-31 10:01:34 +00001976 }
drhb8ef32c2005-03-14 02:01:49 +00001977
1978 /* Write transactions are not possible on a read-only database */
danielk1977ee5741e2004-05-31 10:01:34 +00001979 if( pBt->readOnly && wrflag ){
drhd677b3d2007-08-20 22:48:41 +00001980 rc = SQLITE_READONLY;
1981 goto trans_begun;
danielk1977ee5741e2004-05-31 10:01:34 +00001982 }
1983
danielk1977aef0bf62005-12-30 16:28:01 +00001984 /* If another database handle has already opened a write transaction
1985 ** on this shared-btree structure and a second write transaction is
1986 ** requested, return SQLITE_BUSY.
1987 */
1988 if( pBt->inTransaction==TRANS_WRITE && wrflag ){
drhd677b3d2007-08-20 22:48:41 +00001989 rc = SQLITE_BUSY;
1990 goto trans_begun;
danielk1977aef0bf62005-12-30 16:28:01 +00001991 }
1992
danielk1977641b0f42007-12-21 04:47:25 +00001993#ifndef SQLITE_OMIT_SHARED_CACHE
1994 if( wrflag>1 ){
1995 BtLock *pIter;
1996 for(pIter=pBt->pLock; pIter; pIter=pIter->pNext){
1997 if( pIter->pBtree!=p ){
1998 rc = SQLITE_BUSY;
1999 goto trans_begun;
2000 }
2001 }
2002 }
2003#endif
2004
drhb8ef32c2005-03-14 02:01:49 +00002005 do {
drh8a9c17f2008-05-02 14:23:54 +00002006 if( pBt->pPage1==0 ){
2007 do{
2008 rc = lockBtree(pBt);
2009 }while( pBt->pPage1==0 && rc==SQLITE_OK );
drh8c42ca92001-06-22 19:15:00 +00002010 }
drh309169a2007-04-24 17:27:51 +00002011
drhb8ef32c2005-03-14 02:01:49 +00002012 if( rc==SQLITE_OK && wrflag ){
drh309169a2007-04-24 17:27:51 +00002013 if( pBt->readOnly ){
2014 rc = SQLITE_READONLY;
2015 }else{
2016 rc = sqlite3PagerBegin(pBt->pPage1->pDbPage, wrflag>1);
2017 if( rc==SQLITE_OK ){
2018 rc = newDatabase(pBt);
2019 }
drhb8ef32c2005-03-14 02:01:49 +00002020 }
2021 }
2022
2023 if( rc==SQLITE_OK ){
drhb8ef32c2005-03-14 02:01:49 +00002024 if( wrflag ) pBt->inStmt = 0;
2025 }else{
2026 unlockBtreeIfUnused(pBt);
2027 }
danielk1977aef0bf62005-12-30 16:28:01 +00002028 }while( rc==SQLITE_BUSY && pBt->inTransaction==TRANS_NONE &&
danielk19771ceedd32008-11-19 10:22:33 +00002029 btreeInvokeBusyHandler(pBt) );
danielk1977aef0bf62005-12-30 16:28:01 +00002030
2031 if( rc==SQLITE_OK ){
2032 if( p->inTrans==TRANS_NONE ){
2033 pBt->nTransaction++;
2034 }
2035 p->inTrans = (wrflag?TRANS_WRITE:TRANS_READ);
2036 if( p->inTrans>pBt->inTransaction ){
2037 pBt->inTransaction = p->inTrans;
2038 }
danielk1977641b0f42007-12-21 04:47:25 +00002039#ifndef SQLITE_OMIT_SHARED_CACHE
2040 if( wrflag>1 ){
2041 assert( !pBt->pExclusive );
2042 pBt->pExclusive = p;
2043 }
2044#endif
danielk1977aef0bf62005-12-30 16:28:01 +00002045 }
2046
drhd677b3d2007-08-20 22:48:41 +00002047
2048trans_begun:
danielk1977fd7f0452008-12-17 17:30:26 +00002049 if( rc==SQLITE_OK && wrflag ){
danielk197712dd5492008-12-18 15:45:07 +00002050 /* This call makes sure that the pager has the correct number of
2051 ** open savepoints. If the second parameter is greater than 0 and
2052 ** the sub-journal is not already open, then it will be opened here.
2053 */
danielk1977fd7f0452008-12-17 17:30:26 +00002054 rc = sqlite3PagerOpenSavepoint(pBt->pPager, p->db->nSavepoint);
2055 }
danielk197712dd5492008-12-18 15:45:07 +00002056
danielk1977aef0bf62005-12-30 16:28:01 +00002057 btreeIntegrity(p);
drhd677b3d2007-08-20 22:48:41 +00002058 sqlite3BtreeLeave(p);
drhb8ca3072001-12-05 00:21:20 +00002059 return rc;
drha059ad02001-04-17 20:09:11 +00002060}
2061
danielk1977687566d2004-11-02 12:56:41 +00002062#ifndef SQLITE_OMIT_AUTOVACUUM
2063
2064/*
2065** Set the pointer-map entries for all children of page pPage. Also, if
2066** pPage contains cells that point to overflow pages, set the pointer
2067** map entries for the overflow pages as well.
2068*/
2069static int setChildPtrmaps(MemPage *pPage){
2070 int i; /* Counter variable */
2071 int nCell; /* Number of cells in page pPage */
danielk19772df71c72007-05-24 07:22:42 +00002072 int rc; /* Return code */
danielk1977aef0bf62005-12-30 16:28:01 +00002073 BtShared *pBt = pPage->pBt;
drhf49661a2008-12-10 16:45:50 +00002074 u8 isInitOrig = pPage->isInit;
danielk1977687566d2004-11-02 12:56:41 +00002075 Pgno pgno = pPage->pgno;
2076
drh1fee73e2007-08-29 04:00:57 +00002077 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
danielk197771d5d2c2008-09-29 11:49:47 +00002078 rc = sqlite3BtreeInitPage(pPage);
danielk19772df71c72007-05-24 07:22:42 +00002079 if( rc!=SQLITE_OK ){
2080 goto set_child_ptrmaps_out;
2081 }
danielk1977687566d2004-11-02 12:56:41 +00002082 nCell = pPage->nCell;
2083
2084 for(i=0; i<nCell; i++){
danielk19771cc5ed82007-05-16 17:28:43 +00002085 u8 *pCell = findCell(pPage, i);
danielk1977687566d2004-11-02 12:56:41 +00002086
danielk197726836652005-01-17 01:33:13 +00002087 rc = ptrmapPutOvflPtr(pPage, pCell);
2088 if( rc!=SQLITE_OK ){
2089 goto set_child_ptrmaps_out;
danielk1977687566d2004-11-02 12:56:41 +00002090 }
danielk197726836652005-01-17 01:33:13 +00002091
danielk1977687566d2004-11-02 12:56:41 +00002092 if( !pPage->leaf ){
2093 Pgno childPgno = get4byte(pCell);
2094 rc = ptrmapPut(pBt, childPgno, PTRMAP_BTREE, pgno);
danielk197700a696d2008-09-29 16:41:31 +00002095 if( rc!=SQLITE_OK ) goto set_child_ptrmaps_out;
danielk1977687566d2004-11-02 12:56:41 +00002096 }
2097 }
2098
2099 if( !pPage->leaf ){
2100 Pgno childPgno = get4byte(&pPage->aData[pPage->hdrOffset+8]);
2101 rc = ptrmapPut(pBt, childPgno, PTRMAP_BTREE, pgno);
2102 }
2103
2104set_child_ptrmaps_out:
2105 pPage->isInit = isInitOrig;
2106 return rc;
2107}
2108
2109/*
2110** Somewhere on pPage, which is guarenteed to be a btree page, not an overflow
2111** page, is a pointer to page iFrom. Modify this pointer so that it points to
2112** iTo. Parameter eType describes the type of pointer to be modified, as
2113** follows:
2114**
2115** PTRMAP_BTREE: pPage is a btree-page. The pointer points at a child
2116** page of pPage.
2117**
2118** PTRMAP_OVERFLOW1: pPage is a btree-page. The pointer points at an overflow
2119** page pointed to by one of the cells on pPage.
2120**
2121** PTRMAP_OVERFLOW2: pPage is an overflow-page. The pointer points at the next
2122** overflow page in the list.
2123*/
danielk1977fdb7cdb2005-01-17 02:12:18 +00002124static int modifyPagePointer(MemPage *pPage, Pgno iFrom, Pgno iTo, u8 eType){
drh1fee73e2007-08-29 04:00:57 +00002125 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
drhc5053fb2008-11-27 02:22:10 +00002126 assert( sqlite3PagerIswriteable(pPage->pDbPage) );
danielk1977687566d2004-11-02 12:56:41 +00002127 if( eType==PTRMAP_OVERFLOW2 ){
danielk1977f78fc082004-11-02 14:40:32 +00002128 /* The pointer is always the first 4 bytes of the page in this case. */
danielk1977fdb7cdb2005-01-17 02:12:18 +00002129 if( get4byte(pPage->aData)!=iFrom ){
drh49285702005-09-17 15:20:26 +00002130 return SQLITE_CORRUPT_BKPT;
danielk1977fdb7cdb2005-01-17 02:12:18 +00002131 }
danielk1977f78fc082004-11-02 14:40:32 +00002132 put4byte(pPage->aData, iTo);
danielk1977687566d2004-11-02 12:56:41 +00002133 }else{
drhf49661a2008-12-10 16:45:50 +00002134 u8 isInitOrig = pPage->isInit;
danielk1977687566d2004-11-02 12:56:41 +00002135 int i;
2136 int nCell;
2137
danielk197771d5d2c2008-09-29 11:49:47 +00002138 sqlite3BtreeInitPage(pPage);
danielk1977687566d2004-11-02 12:56:41 +00002139 nCell = pPage->nCell;
2140
danielk1977687566d2004-11-02 12:56:41 +00002141 for(i=0; i<nCell; i++){
danielk19771cc5ed82007-05-16 17:28:43 +00002142 u8 *pCell = findCell(pPage, i);
danielk1977687566d2004-11-02 12:56:41 +00002143 if( eType==PTRMAP_OVERFLOW1 ){
2144 CellInfo info;
drh16a9b832007-05-05 18:39:25 +00002145 sqlite3BtreeParseCellPtr(pPage, pCell, &info);
danielk1977687566d2004-11-02 12:56:41 +00002146 if( info.iOverflow ){
2147 if( iFrom==get4byte(&pCell[info.iOverflow]) ){
2148 put4byte(&pCell[info.iOverflow], iTo);
2149 break;
2150 }
2151 }
2152 }else{
2153 if( get4byte(pCell)==iFrom ){
2154 put4byte(pCell, iTo);
2155 break;
2156 }
2157 }
2158 }
2159
2160 if( i==nCell ){
danielk1977fdb7cdb2005-01-17 02:12:18 +00002161 if( eType!=PTRMAP_BTREE ||
2162 get4byte(&pPage->aData[pPage->hdrOffset+8])!=iFrom ){
drh49285702005-09-17 15:20:26 +00002163 return SQLITE_CORRUPT_BKPT;
danielk1977fdb7cdb2005-01-17 02:12:18 +00002164 }
danielk1977687566d2004-11-02 12:56:41 +00002165 put4byte(&pPage->aData[pPage->hdrOffset+8], iTo);
2166 }
2167
2168 pPage->isInit = isInitOrig;
2169 }
danielk1977fdb7cdb2005-01-17 02:12:18 +00002170 return SQLITE_OK;
danielk1977687566d2004-11-02 12:56:41 +00002171}
2172
danielk1977003ba062004-11-04 02:57:33 +00002173
danielk19777701e812005-01-10 12:59:51 +00002174/*
2175** Move the open database page pDbPage to location iFreePage in the
2176** database. The pDbPage reference remains valid.
2177*/
danielk1977003ba062004-11-04 02:57:33 +00002178static int relocatePage(
danielk1977aef0bf62005-12-30 16:28:01 +00002179 BtShared *pBt, /* Btree */
danielk19777701e812005-01-10 12:59:51 +00002180 MemPage *pDbPage, /* Open page to move */
2181 u8 eType, /* Pointer map 'type' entry for pDbPage */
2182 Pgno iPtrPage, /* Pointer map 'page-no' entry for pDbPage */
danielk19774c999992008-07-16 18:17:55 +00002183 Pgno iFreePage, /* The location to move pDbPage to */
2184 int isCommit
danielk1977003ba062004-11-04 02:57:33 +00002185){
2186 MemPage *pPtrPage; /* The page that contains a pointer to pDbPage */
2187 Pgno iDbPage = pDbPage->pgno;
2188 Pager *pPager = pBt->pPager;
2189 int rc;
2190
danielk1977a0bf2652004-11-04 14:30:04 +00002191 assert( eType==PTRMAP_OVERFLOW2 || eType==PTRMAP_OVERFLOW1 ||
2192 eType==PTRMAP_BTREE || eType==PTRMAP_ROOTPAGE );
drh1fee73e2007-08-29 04:00:57 +00002193 assert( sqlite3_mutex_held(pBt->mutex) );
drhd0679ed2007-08-28 22:24:34 +00002194 assert( pDbPage->pBt==pBt );
danielk1977003ba062004-11-04 02:57:33 +00002195
drh85b623f2007-12-13 21:54:09 +00002196 /* Move page iDbPage from its current location to page number iFreePage */
danielk1977003ba062004-11-04 02:57:33 +00002197 TRACE(("AUTOVACUUM: Moving %d to free page %d (ptr page %d type %d)\n",
2198 iDbPage, iFreePage, iPtrPage, eType));
danielk19774c999992008-07-16 18:17:55 +00002199 rc = sqlite3PagerMovepage(pPager, pDbPage->pDbPage, iFreePage, isCommit);
danielk1977003ba062004-11-04 02:57:33 +00002200 if( rc!=SQLITE_OK ){
2201 return rc;
2202 }
2203 pDbPage->pgno = iFreePage;
2204
2205 /* If pDbPage was a btree-page, then it may have child pages and/or cells
2206 ** that point to overflow pages. The pointer map entries for all these
2207 ** pages need to be changed.
2208 **
2209 ** If pDbPage is an overflow page, then the first 4 bytes may store a
2210 ** pointer to a subsequent overflow page. If this is the case, then
2211 ** the pointer map needs to be updated for the subsequent overflow page.
2212 */
danielk1977a0bf2652004-11-04 14:30:04 +00002213 if( eType==PTRMAP_BTREE || eType==PTRMAP_ROOTPAGE ){
danielk1977003ba062004-11-04 02:57:33 +00002214 rc = setChildPtrmaps(pDbPage);
2215 if( rc!=SQLITE_OK ){
2216 return rc;
2217 }
2218 }else{
2219 Pgno nextOvfl = get4byte(pDbPage->aData);
2220 if( nextOvfl!=0 ){
danielk1977003ba062004-11-04 02:57:33 +00002221 rc = ptrmapPut(pBt, nextOvfl, PTRMAP_OVERFLOW2, iFreePage);
2222 if( rc!=SQLITE_OK ){
2223 return rc;
2224 }
2225 }
2226 }
2227
2228 /* Fix the database pointer on page iPtrPage that pointed at iDbPage so
2229 ** that it points at iFreePage. Also fix the pointer map entry for
2230 ** iPtrPage.
2231 */
danielk1977a0bf2652004-11-04 14:30:04 +00002232 if( eType!=PTRMAP_ROOTPAGE ){
drh16a9b832007-05-05 18:39:25 +00002233 rc = sqlite3BtreeGetPage(pBt, iPtrPage, &pPtrPage, 0);
danielk1977a0bf2652004-11-04 14:30:04 +00002234 if( rc!=SQLITE_OK ){
2235 return rc;
2236 }
danielk19773b8a05f2007-03-19 17:44:26 +00002237 rc = sqlite3PagerWrite(pPtrPage->pDbPage);
danielk1977a0bf2652004-11-04 14:30:04 +00002238 if( rc!=SQLITE_OK ){
2239 releasePage(pPtrPage);
2240 return rc;
2241 }
danielk1977fdb7cdb2005-01-17 02:12:18 +00002242 rc = modifyPagePointer(pPtrPage, iDbPage, iFreePage, eType);
danielk1977003ba062004-11-04 02:57:33 +00002243 releasePage(pPtrPage);
danielk1977fdb7cdb2005-01-17 02:12:18 +00002244 if( rc==SQLITE_OK ){
2245 rc = ptrmapPut(pBt, iFreePage, eType, iPtrPage);
2246 }
danielk1977003ba062004-11-04 02:57:33 +00002247 }
danielk1977003ba062004-11-04 02:57:33 +00002248 return rc;
2249}
2250
danielk1977dddbcdc2007-04-26 14:42:34 +00002251/* Forward declaration required by incrVacuumStep(). */
drh4f0c5872007-03-26 22:05:01 +00002252static int allocateBtreePage(BtShared *, MemPage **, Pgno *, Pgno, u8);
danielk1977687566d2004-11-02 12:56:41 +00002253
2254/*
danielk1977dddbcdc2007-04-26 14:42:34 +00002255** Perform a single step of an incremental-vacuum. If successful,
2256** return SQLITE_OK. If there is no work to do (and therefore no
2257** point in calling this function again), return SQLITE_DONE.
2258**
2259** More specificly, this function attempts to re-organize the
2260** database so that the last page of the file currently in use
2261** is no longer in use.
2262**
2263** If the nFin parameter is non-zero, the implementation assumes
2264** that the caller will keep calling incrVacuumStep() until
2265** it returns SQLITE_DONE or an error, and that nFin is the
2266** number of pages the database file will contain after this
2267** process is complete.
2268*/
danielk19773460d192008-12-27 15:23:13 +00002269static int incrVacuumStep(BtShared *pBt, Pgno nFin, Pgno iLastPg){
danielk1977dddbcdc2007-04-26 14:42:34 +00002270 Pgno nFreeList; /* Number of pages still on the free-list */
2271
drh1fee73e2007-08-29 04:00:57 +00002272 assert( sqlite3_mutex_held(pBt->mutex) );
danielk1977dddbcdc2007-04-26 14:42:34 +00002273
2274 if( !PTRMAP_ISPAGE(pBt, iLastPg) && iLastPg!=PENDING_BYTE_PAGE(pBt) ){
2275 int rc;
2276 u8 eType;
2277 Pgno iPtrPage;
2278
2279 nFreeList = get4byte(&pBt->pPage1->aData[36]);
2280 if( nFreeList==0 || nFin==iLastPg ){
2281 return SQLITE_DONE;
2282 }
2283
2284 rc = ptrmapGet(pBt, iLastPg, &eType, &iPtrPage);
2285 if( rc!=SQLITE_OK ){
2286 return rc;
2287 }
2288 if( eType==PTRMAP_ROOTPAGE ){
2289 return SQLITE_CORRUPT_BKPT;
2290 }
2291
2292 if( eType==PTRMAP_FREEPAGE ){
2293 if( nFin==0 ){
2294 /* Remove the page from the files free-list. This is not required
danielk19774ef24492007-05-23 09:52:41 +00002295 ** if nFin is non-zero. In that case, the free-list will be
danielk1977dddbcdc2007-04-26 14:42:34 +00002296 ** truncated to zero after this function returns, so it doesn't
2297 ** matter if it still contains some garbage entries.
2298 */
2299 Pgno iFreePg;
2300 MemPage *pFreePg;
2301 rc = allocateBtreePage(pBt, &pFreePg, &iFreePg, iLastPg, 1);
2302 if( rc!=SQLITE_OK ){
2303 return rc;
2304 }
2305 assert( iFreePg==iLastPg );
2306 releasePage(pFreePg);
2307 }
2308 } else {
2309 Pgno iFreePg; /* Index of free page to move pLastPg to */
2310 MemPage *pLastPg;
2311
drh16a9b832007-05-05 18:39:25 +00002312 rc = sqlite3BtreeGetPage(pBt, iLastPg, &pLastPg, 0);
danielk1977dddbcdc2007-04-26 14:42:34 +00002313 if( rc!=SQLITE_OK ){
2314 return rc;
2315 }
2316
danielk1977b4626a32007-04-28 15:47:43 +00002317 /* If nFin is zero, this loop runs exactly once and page pLastPg
2318 ** is swapped with the first free page pulled off the free list.
2319 **
2320 ** On the other hand, if nFin is greater than zero, then keep
2321 ** looping until a free-page located within the first nFin pages
2322 ** of the file is found.
2323 */
danielk1977dddbcdc2007-04-26 14:42:34 +00002324 do {
2325 MemPage *pFreePg;
2326 rc = allocateBtreePage(pBt, &pFreePg, &iFreePg, 0, 0);
2327 if( rc!=SQLITE_OK ){
2328 releasePage(pLastPg);
2329 return rc;
2330 }
2331 releasePage(pFreePg);
2332 }while( nFin!=0 && iFreePg>nFin );
2333 assert( iFreePg<iLastPg );
danielk1977b4626a32007-04-28 15:47:43 +00002334
2335 rc = sqlite3PagerWrite(pLastPg->pDbPage);
danielk1977662278e2007-11-05 15:30:12 +00002336 if( rc==SQLITE_OK ){
danielk19774c999992008-07-16 18:17:55 +00002337 rc = relocatePage(pBt, pLastPg, eType, iPtrPage, iFreePg, nFin!=0);
danielk1977662278e2007-11-05 15:30:12 +00002338 }
danielk1977dddbcdc2007-04-26 14:42:34 +00002339 releasePage(pLastPg);
2340 if( rc!=SQLITE_OK ){
2341 return rc;
danielk1977662278e2007-11-05 15:30:12 +00002342 }
danielk1977dddbcdc2007-04-26 14:42:34 +00002343 }
2344 }
2345
danielk19773460d192008-12-27 15:23:13 +00002346 if( nFin==0 ){
2347 iLastPg--;
2348 while( iLastPg==PENDING_BYTE_PAGE(pBt)||PTRMAP_ISPAGE(pBt, iLastPg) ){
2349 iLastPg--;
2350 }
2351 sqlite3PagerTruncateImage(pBt->pPager, iLastPg);
danielk1977dddbcdc2007-04-26 14:42:34 +00002352 }
2353 return SQLITE_OK;
2354}
2355
2356/*
2357** A write-transaction must be opened before calling this function.
2358** It performs a single unit of work towards an incremental vacuum.
2359**
2360** If the incremental vacuum is finished after this function has run,
2361** SQLITE_DONE is returned. If it is not finished, but no error occured,
2362** SQLITE_OK is returned. Otherwise an SQLite error code.
2363*/
2364int sqlite3BtreeIncrVacuum(Btree *p){
drhd677b3d2007-08-20 22:48:41 +00002365 int rc;
danielk1977dddbcdc2007-04-26 14:42:34 +00002366 BtShared *pBt = p->pBt;
drhd677b3d2007-08-20 22:48:41 +00002367
2368 sqlite3BtreeEnter(p);
drhe5fe6902007-12-07 18:55:28 +00002369 pBt->db = p->db;
danielk1977dddbcdc2007-04-26 14:42:34 +00002370 assert( pBt->inTransaction==TRANS_WRITE && p->inTrans==TRANS_WRITE );
2371 if( !pBt->autoVacuum ){
drhd677b3d2007-08-20 22:48:41 +00002372 rc = SQLITE_DONE;
2373 }else{
2374 invalidateAllOverflowCache(pBt);
danielk197745d68822009-01-16 16:23:38 +00002375 rc = incrVacuumStep(pBt, 0, sqlite3PagerImageSize(pBt->pPager));
danielk1977dddbcdc2007-04-26 14:42:34 +00002376 }
drhd677b3d2007-08-20 22:48:41 +00002377 sqlite3BtreeLeave(p);
2378 return rc;
danielk1977dddbcdc2007-04-26 14:42:34 +00002379}
2380
2381/*
danielk19773b8a05f2007-03-19 17:44:26 +00002382** This routine is called prior to sqlite3PagerCommit when a transaction
danielk1977687566d2004-11-02 12:56:41 +00002383** is commited for an auto-vacuum database.
danielk197724168722007-04-02 05:07:47 +00002384**
2385** If SQLITE_OK is returned, then *pnTrunc is set to the number of pages
2386** the database file should be truncated to during the commit process.
2387** i.e. the database has been reorganized so that only the first *pnTrunc
2388** pages are in use.
danielk1977687566d2004-11-02 12:56:41 +00002389*/
danielk19773460d192008-12-27 15:23:13 +00002390static int autoVacuumCommit(BtShared *pBt){
danielk1977dddbcdc2007-04-26 14:42:34 +00002391 int rc = SQLITE_OK;
danielk1977687566d2004-11-02 12:56:41 +00002392 Pager *pPager = pBt->pPager;
drhf94a1732008-09-30 17:18:17 +00002393 VVA_ONLY( int nRef = sqlite3PagerRefcount(pPager) );
danielk1977687566d2004-11-02 12:56:41 +00002394
drh1fee73e2007-08-29 04:00:57 +00002395 assert( sqlite3_mutex_held(pBt->mutex) );
danielk197792d4d7a2007-05-04 12:05:56 +00002396 invalidateAllOverflowCache(pBt);
danielk1977dddbcdc2007-04-26 14:42:34 +00002397 assert(pBt->autoVacuum);
2398 if( !pBt->incrVacuum ){
danielk19773460d192008-12-27 15:23:13 +00002399 Pgno nFin;
2400 Pgno nFree;
2401 Pgno nPtrmap;
2402 Pgno iFree;
2403 const int pgsz = pBt->pageSize;
2404 Pgno nOrig = pagerPagecount(pBt);
danielk1977687566d2004-11-02 12:56:41 +00002405
danielk19773460d192008-12-27 15:23:13 +00002406 if( PTRMAP_ISPAGE(pBt, nOrig) ){
2407 return SQLITE_CORRUPT_BKPT;
2408 }
2409 if( nOrig==PENDING_BYTE_PAGE(pBt) ){
2410 nOrig--;
2411 }
2412 nFree = get4byte(&pBt->pPage1->aData[36]);
2413 nPtrmap = (nFree-nOrig+PTRMAP_PAGENO(pBt, nOrig)+pgsz/5)/(pgsz/5);
2414 nFin = nOrig - nFree - nPtrmap;
2415 if( nOrig>PENDING_BYTE_PAGE(pBt) && nFin<=PENDING_BYTE_PAGE(pBt) ){
2416 nFin--;
2417 }
2418 while( PTRMAP_ISPAGE(pBt, nFin) || nFin==PENDING_BYTE_PAGE(pBt) ){
2419 nFin--;
danielk1977dddbcdc2007-04-26 14:42:34 +00002420 }
danielk1977687566d2004-11-02 12:56:41 +00002421
danielk19773460d192008-12-27 15:23:13 +00002422 for(iFree=nOrig; iFree>nFin && rc==SQLITE_OK; iFree--){
2423 rc = incrVacuumStep(pBt, nFin, iFree);
danielk1977dddbcdc2007-04-26 14:42:34 +00002424 }
danielk19773460d192008-12-27 15:23:13 +00002425 if( (rc==SQLITE_DONE || rc==SQLITE_OK) && nFree>0 ){
danielk1977dddbcdc2007-04-26 14:42:34 +00002426 rc = SQLITE_OK;
danielk19773460d192008-12-27 15:23:13 +00002427 rc = sqlite3PagerWrite(pBt->pPage1->pDbPage);
2428 put4byte(&pBt->pPage1->aData[32], 0);
2429 put4byte(&pBt->pPage1->aData[36], 0);
2430 sqlite3PagerTruncateImage(pBt->pPager, nFin);
danielk1977dddbcdc2007-04-26 14:42:34 +00002431 }
2432 if( rc!=SQLITE_OK ){
2433 sqlite3PagerRollback(pPager);
2434 }
danielk1977687566d2004-11-02 12:56:41 +00002435 }
2436
danielk19773b8a05f2007-03-19 17:44:26 +00002437 assert( nRef==sqlite3PagerRefcount(pPager) );
danielk1977687566d2004-11-02 12:56:41 +00002438 return rc;
2439}
danielk1977dddbcdc2007-04-26 14:42:34 +00002440
shane831c3292008-11-10 17:14:58 +00002441#endif /* ifndef SQLITE_OMIT_AUTOVACUUM */
danielk1977687566d2004-11-02 12:56:41 +00002442
2443/*
drh80e35f42007-03-30 14:06:34 +00002444** This routine does the first phase of a two-phase commit. This routine
2445** causes a rollback journal to be created (if it does not already exist)
2446** and populated with enough information so that if a power loss occurs
2447** the database can be restored to its original state by playing back
2448** the journal. Then the contents of the journal are flushed out to
2449** the disk. After the journal is safely on oxide, the changes to the
2450** database are written into the database file and flushed to oxide.
2451** At the end of this call, the rollback journal still exists on the
2452** disk and we are still holding all locks, so the transaction has not
2453** committed. See sqlite3BtreeCommit() for the second phase of the
2454** commit process.
2455**
2456** This call is a no-op if no write-transaction is currently active on pBt.
2457**
2458** Otherwise, sync the database file for the btree pBt. zMaster points to
2459** the name of a master journal file that should be written into the
2460** individual journal file, or is NULL, indicating no master journal file
2461** (single database transaction).
2462**
2463** When this is called, the master journal should already have been
2464** created, populated with this journal pointer and synced to disk.
2465**
2466** Once this is routine has returned, the only thing required to commit
2467** the write-transaction for this database file is to delete the journal.
2468*/
2469int sqlite3BtreeCommitPhaseOne(Btree *p, const char *zMaster){
2470 int rc = SQLITE_OK;
2471 if( p->inTrans==TRANS_WRITE ){
2472 BtShared *pBt = p->pBt;
drhd677b3d2007-08-20 22:48:41 +00002473 sqlite3BtreeEnter(p);
drhe5fe6902007-12-07 18:55:28 +00002474 pBt->db = p->db;
drh80e35f42007-03-30 14:06:34 +00002475#ifndef SQLITE_OMIT_AUTOVACUUM
2476 if( pBt->autoVacuum ){
danielk19773460d192008-12-27 15:23:13 +00002477 rc = autoVacuumCommit(pBt);
drh80e35f42007-03-30 14:06:34 +00002478 if( rc!=SQLITE_OK ){
drhd677b3d2007-08-20 22:48:41 +00002479 sqlite3BtreeLeave(p);
drh80e35f42007-03-30 14:06:34 +00002480 return rc;
2481 }
2482 }
2483#endif
drh49b9d332009-01-02 18:10:42 +00002484 rc = sqlite3PagerCommitPhaseOne(pBt->pPager, zMaster, 0);
drhd677b3d2007-08-20 22:48:41 +00002485 sqlite3BtreeLeave(p);
drh80e35f42007-03-30 14:06:34 +00002486 }
2487 return rc;
2488}
2489
2490/*
drh2aa679f2001-06-25 02:11:07 +00002491** Commit the transaction currently in progress.
drh5e00f6c2001-09-13 13:46:56 +00002492**
drh6e345992007-03-30 11:12:08 +00002493** This routine implements the second phase of a 2-phase commit. The
2494** sqlite3BtreeSync() routine does the first phase and should be invoked
2495** prior to calling this routine. The sqlite3BtreeSync() routine did
2496** all the work of writing information out to disk and flushing the
2497** contents so that they are written onto the disk platter. All this
2498** routine has to do is delete or truncate the rollback journal
2499** (which causes the transaction to commit) and drop locks.
2500**
drh5e00f6c2001-09-13 13:46:56 +00002501** This will release the write lock on the database file. If there
2502** are no active cursors, it also releases the read lock.
drha059ad02001-04-17 20:09:11 +00002503*/
drh80e35f42007-03-30 14:06:34 +00002504int sqlite3BtreeCommitPhaseTwo(Btree *p){
danielk1977aef0bf62005-12-30 16:28:01 +00002505 BtShared *pBt = p->pBt;
2506
drhd677b3d2007-08-20 22:48:41 +00002507 sqlite3BtreeEnter(p);
drhe5fe6902007-12-07 18:55:28 +00002508 pBt->db = p->db;
danielk1977aef0bf62005-12-30 16:28:01 +00002509 btreeIntegrity(p);
danielk1977aef0bf62005-12-30 16:28:01 +00002510
2511 /* If the handle has a write-transaction open, commit the shared-btrees
2512 ** transaction and set the shared state to TRANS_READ.
2513 */
2514 if( p->inTrans==TRANS_WRITE ){
danielk19777f7bc662006-01-23 13:47:47 +00002515 int rc;
danielk1977aef0bf62005-12-30 16:28:01 +00002516 assert( pBt->inTransaction==TRANS_WRITE );
2517 assert( pBt->nTransaction>0 );
drh80e35f42007-03-30 14:06:34 +00002518 rc = sqlite3PagerCommitPhaseTwo(pBt->pPager);
danielk19777f7bc662006-01-23 13:47:47 +00002519 if( rc!=SQLITE_OK ){
drhd677b3d2007-08-20 22:48:41 +00002520 sqlite3BtreeLeave(p);
danielk19777f7bc662006-01-23 13:47:47 +00002521 return rc;
2522 }
danielk1977aef0bf62005-12-30 16:28:01 +00002523 pBt->inTransaction = TRANS_READ;
2524 pBt->inStmt = 0;
danielk1977ee5741e2004-05-31 10:01:34 +00002525 }
danielk19777f7bc662006-01-23 13:47:47 +00002526 unlockAllTables(p);
danielk1977aef0bf62005-12-30 16:28:01 +00002527
2528 /* If the handle has any kind of transaction open, decrement the transaction
2529 ** count of the shared btree. If the transaction count reaches 0, set
2530 ** the shared state to TRANS_NONE. The unlockBtreeIfUnused() call below
2531 ** will unlock the pager.
2532 */
2533 if( p->inTrans!=TRANS_NONE ){
2534 pBt->nTransaction--;
2535 if( 0==pBt->nTransaction ){
2536 pBt->inTransaction = TRANS_NONE;
2537 }
2538 }
2539
2540 /* Set the handles current transaction state to TRANS_NONE and unlock
2541 ** the pager if this call closed the only read or write transaction.
2542 */
2543 p->inTrans = TRANS_NONE;
drh5e00f6c2001-09-13 13:46:56 +00002544 unlockBtreeIfUnused(pBt);
danielk1977aef0bf62005-12-30 16:28:01 +00002545
2546 btreeIntegrity(p);
drhd677b3d2007-08-20 22:48:41 +00002547 sqlite3BtreeLeave(p);
danielk19777f7bc662006-01-23 13:47:47 +00002548 return SQLITE_OK;
drha059ad02001-04-17 20:09:11 +00002549}
2550
drh80e35f42007-03-30 14:06:34 +00002551/*
2552** Do both phases of a commit.
2553*/
2554int sqlite3BtreeCommit(Btree *p){
2555 int rc;
drhd677b3d2007-08-20 22:48:41 +00002556 sqlite3BtreeEnter(p);
drh80e35f42007-03-30 14:06:34 +00002557 rc = sqlite3BtreeCommitPhaseOne(p, 0);
2558 if( rc==SQLITE_OK ){
2559 rc = sqlite3BtreeCommitPhaseTwo(p);
2560 }
drhd677b3d2007-08-20 22:48:41 +00002561 sqlite3BtreeLeave(p);
drh80e35f42007-03-30 14:06:34 +00002562 return rc;
2563}
2564
danielk1977fbcd5852004-06-15 02:44:18 +00002565#ifndef NDEBUG
2566/*
2567** Return the number of write-cursors open on this handle. This is for use
2568** in assert() expressions, so it is only compiled if NDEBUG is not
2569** defined.
drhfb982642007-08-30 01:19:59 +00002570**
2571** For the purposes of this routine, a write-cursor is any cursor that
2572** is capable of writing to the databse. That means the cursor was
2573** originally opened for writing and the cursor has not be disabled
2574** by having its state changed to CURSOR_FAULT.
danielk1977fbcd5852004-06-15 02:44:18 +00002575*/
danielk1977aef0bf62005-12-30 16:28:01 +00002576static int countWriteCursors(BtShared *pBt){
danielk1977fbcd5852004-06-15 02:44:18 +00002577 BtCursor *pCur;
2578 int r = 0;
2579 for(pCur=pBt->pCursor; pCur; pCur=pCur->pNext){
drhfb982642007-08-30 01:19:59 +00002580 if( pCur->wrFlag && pCur->eState!=CURSOR_FAULT ) r++;
danielk1977fbcd5852004-06-15 02:44:18 +00002581 }
2582 return r;
2583}
2584#endif
2585
drhc39e0002004-05-07 23:50:57 +00002586/*
drhfb982642007-08-30 01:19:59 +00002587** This routine sets the state to CURSOR_FAULT and the error
2588** code to errCode for every cursor on BtShared that pBtree
2589** references.
2590**
2591** Every cursor is tripped, including cursors that belong
2592** to other database connections that happen to be sharing
2593** the cache with pBtree.
2594**
2595** This routine gets called when a rollback occurs.
2596** All cursors using the same cache must be tripped
2597** to prevent them from trying to use the btree after
2598** the rollback. The rollback may have deleted tables
2599** or moved root pages, so it is not sufficient to
2600** save the state of the cursor. The cursor must be
2601** invalidated.
2602*/
2603void sqlite3BtreeTripAllCursors(Btree *pBtree, int errCode){
2604 BtCursor *p;
2605 sqlite3BtreeEnter(pBtree);
2606 for(p=pBtree->pBt->pCursor; p; p=p->pNext){
danielk1977bc2ca9e2008-11-13 14:28:28 +00002607 int i;
danielk1977be51a652008-10-08 17:58:48 +00002608 sqlite3BtreeClearCursor(p);
drhfb982642007-08-30 01:19:59 +00002609 p->eState = CURSOR_FAULT;
2610 p->skip = errCode;
danielk1977bc2ca9e2008-11-13 14:28:28 +00002611 for(i=0; i<=p->iPage; i++){
2612 releasePage(p->apPage[i]);
2613 p->apPage[i] = 0;
2614 }
drhfb982642007-08-30 01:19:59 +00002615 }
2616 sqlite3BtreeLeave(pBtree);
2617}
2618
2619/*
drhecdc7532001-09-23 02:35:53 +00002620** Rollback the transaction in progress. All cursors will be
2621** invalided by this operation. Any attempt to use a cursor
2622** that was open at the beginning of this operation will result
2623** in an error.
drh5e00f6c2001-09-13 13:46:56 +00002624**
2625** This will release the write lock on the database file. If there
2626** are no active cursors, it also releases the read lock.
drha059ad02001-04-17 20:09:11 +00002627*/
danielk1977aef0bf62005-12-30 16:28:01 +00002628int sqlite3BtreeRollback(Btree *p){
danielk19778d34dfd2006-01-24 16:37:57 +00002629 int rc;
danielk1977aef0bf62005-12-30 16:28:01 +00002630 BtShared *pBt = p->pBt;
drh24cd67e2004-05-10 16:18:47 +00002631 MemPage *pPage1;
danielk1977aef0bf62005-12-30 16:28:01 +00002632
drhd677b3d2007-08-20 22:48:41 +00002633 sqlite3BtreeEnter(p);
drhe5fe6902007-12-07 18:55:28 +00002634 pBt->db = p->db;
danielk19772b8c13e2006-01-24 14:21:24 +00002635 rc = saveAllCursors(pBt, 0, 0);
danielk19778d34dfd2006-01-24 16:37:57 +00002636#ifndef SQLITE_OMIT_SHARED_CACHE
danielk19772b8c13e2006-01-24 14:21:24 +00002637 if( rc!=SQLITE_OK ){
danielk19778d34dfd2006-01-24 16:37:57 +00002638 /* This is a horrible situation. An IO or malloc() error occured whilst
2639 ** trying to save cursor positions. If this is an automatic rollback (as
2640 ** the result of a constraint, malloc() failure or IO error) then
2641 ** the cache may be internally inconsistent (not contain valid trees) so
2642 ** we cannot simply return the error to the caller. Instead, abort
2643 ** all queries that may be using any of the cursors that failed to save.
2644 */
drhfb982642007-08-30 01:19:59 +00002645 sqlite3BtreeTripAllCursors(p, rc);
danielk19772b8c13e2006-01-24 14:21:24 +00002646 }
danielk19778d34dfd2006-01-24 16:37:57 +00002647#endif
danielk1977aef0bf62005-12-30 16:28:01 +00002648 btreeIntegrity(p);
2649 unlockAllTables(p);
2650
2651 if( p->inTrans==TRANS_WRITE ){
danielk19778d34dfd2006-01-24 16:37:57 +00002652 int rc2;
danielk1977aef0bf62005-12-30 16:28:01 +00002653
danielk19778d34dfd2006-01-24 16:37:57 +00002654 assert( TRANS_WRITE==pBt->inTransaction );
danielk19773b8a05f2007-03-19 17:44:26 +00002655 rc2 = sqlite3PagerRollback(pBt->pPager);
danielk19778d34dfd2006-01-24 16:37:57 +00002656 if( rc2!=SQLITE_OK ){
2657 rc = rc2;
2658 }
2659
drh24cd67e2004-05-10 16:18:47 +00002660 /* The rollback may have destroyed the pPage1->aData value. So
drh16a9b832007-05-05 18:39:25 +00002661 ** call sqlite3BtreeGetPage() on page 1 again to make
2662 ** sure pPage1->aData is set correctly. */
2663 if( sqlite3BtreeGetPage(pBt, 1, &pPage1, 0)==SQLITE_OK ){
drh24cd67e2004-05-10 16:18:47 +00002664 releasePage(pPage1);
2665 }
danielk1977fbcd5852004-06-15 02:44:18 +00002666 assert( countWriteCursors(pBt)==0 );
danielk1977aef0bf62005-12-30 16:28:01 +00002667 pBt->inTransaction = TRANS_READ;
drh24cd67e2004-05-10 16:18:47 +00002668 }
danielk1977aef0bf62005-12-30 16:28:01 +00002669
2670 if( p->inTrans!=TRANS_NONE ){
2671 assert( pBt->nTransaction>0 );
2672 pBt->nTransaction--;
2673 if( 0==pBt->nTransaction ){
2674 pBt->inTransaction = TRANS_NONE;
2675 }
2676 }
2677
2678 p->inTrans = TRANS_NONE;
danielk1977ee5741e2004-05-31 10:01:34 +00002679 pBt->inStmt = 0;
drh5e00f6c2001-09-13 13:46:56 +00002680 unlockBtreeIfUnused(pBt);
danielk1977aef0bf62005-12-30 16:28:01 +00002681
2682 btreeIntegrity(p);
drhd677b3d2007-08-20 22:48:41 +00002683 sqlite3BtreeLeave(p);
drha059ad02001-04-17 20:09:11 +00002684 return rc;
2685}
2686
2687/*
drhab01f612004-05-22 02:55:23 +00002688** Start a statement subtransaction. The subtransaction can
2689** can be rolled back independently of the main transaction.
2690** You must start a transaction before starting a subtransaction.
2691** The subtransaction is ended automatically if the main transaction
drh663fc632002-02-02 18:49:19 +00002692** commits or rolls back.
2693**
drhab01f612004-05-22 02:55:23 +00002694** Only one subtransaction may be active at a time. It is an error to try
2695** to start a new subtransaction if another subtransaction is already active.
2696**
2697** Statement subtransactions are used around individual SQL statements
2698** that are contained within a BEGIN...COMMIT block. If a constraint
2699** error occurs within the statement, the effect of that one statement
2700** can be rolled back without having to rollback the entire transaction.
drh663fc632002-02-02 18:49:19 +00002701*/
danielk1977aef0bf62005-12-30 16:28:01 +00002702int sqlite3BtreeBeginStmt(Btree *p){
drh663fc632002-02-02 18:49:19 +00002703 int rc;
danielk1977aef0bf62005-12-30 16:28:01 +00002704 BtShared *pBt = p->pBt;
drhd677b3d2007-08-20 22:48:41 +00002705 sqlite3BtreeEnter(p);
drhe5fe6902007-12-07 18:55:28 +00002706 pBt->db = p->db;
drh64022502009-01-09 14:11:04 +00002707 assert( p->inTrans==TRANS_WRITE );
2708 assert( !pBt->inStmt );
2709 assert( pBt->readOnly==0 );
2710 if( NEVER(p->inTrans!=TRANS_WRITE || pBt->inStmt || pBt->readOnly) ){
2711 rc = SQLITE_INTERNAL;
drhd677b3d2007-08-20 22:48:41 +00002712 }else{
2713 assert( pBt->inTransaction==TRANS_WRITE );
drh64022502009-01-09 14:11:04 +00002714 /* At the pager level, a statement transaction is a savepoint with
2715 ** an index greater than all savepoints created explicitly using
2716 ** SQL statements. It is illegal to open, release or rollback any
2717 ** such savepoints while the statement transaction savepoint is active.
2718 */
2719 rc = sqlite3PagerOpenSavepoint(pBt->pPager, p->db->nSavepoint+1);
drhd677b3d2007-08-20 22:48:41 +00002720 pBt->inStmt = 1;
drh0d65dc02002-02-03 00:56:09 +00002721 }
drhd677b3d2007-08-20 22:48:41 +00002722 sqlite3BtreeLeave(p);
drh663fc632002-02-02 18:49:19 +00002723 return rc;
2724}
2725
drh663fc632002-02-02 18:49:19 +00002726/*
drhab01f612004-05-22 02:55:23 +00002727** Commit the statment subtransaction currently in progress. If no
2728** subtransaction is active, this is a no-op.
drh663fc632002-02-02 18:49:19 +00002729*/
danielk1977aef0bf62005-12-30 16:28:01 +00002730int sqlite3BtreeCommitStmt(Btree *p){
drh663fc632002-02-02 18:49:19 +00002731 int rc;
danielk1977aef0bf62005-12-30 16:28:01 +00002732 BtShared *pBt = p->pBt;
drhd677b3d2007-08-20 22:48:41 +00002733 sqlite3BtreeEnter(p);
drhe5fe6902007-12-07 18:55:28 +00002734 pBt->db = p->db;
drh64022502009-01-09 14:11:04 +00002735 assert( pBt->readOnly==0 );
2736 if( pBt->inStmt ){
danielk1977fd7f0452008-12-17 17:30:26 +00002737 int iStmtpoint = p->db->nSavepoint;
2738 rc = sqlite3PagerSavepoint(pBt->pPager, SAVEPOINT_RELEASE, iStmtpoint);
drh663fc632002-02-02 18:49:19 +00002739 }else{
2740 rc = SQLITE_OK;
2741 }
drh3aac2dd2004-04-26 14:10:20 +00002742 pBt->inStmt = 0;
drhd677b3d2007-08-20 22:48:41 +00002743 sqlite3BtreeLeave(p);
drh663fc632002-02-02 18:49:19 +00002744 return rc;
2745}
2746
2747/*
drhab01f612004-05-22 02:55:23 +00002748** Rollback the active statement subtransaction. If no subtransaction
2749** is active this routine is a no-op.
drh663fc632002-02-02 18:49:19 +00002750**
drhab01f612004-05-22 02:55:23 +00002751** All cursors will be invalidated by this operation. Any attempt
drh663fc632002-02-02 18:49:19 +00002752** to use a cursor that was open at the beginning of this operation
2753** will result in an error.
2754*/
danielk1977aef0bf62005-12-30 16:28:01 +00002755int sqlite3BtreeRollbackStmt(Btree *p){
danielk197797a227c2006-01-20 16:32:04 +00002756 int rc = SQLITE_OK;
danielk1977aef0bf62005-12-30 16:28:01 +00002757 BtShared *pBt = p->pBt;
drhd677b3d2007-08-20 22:48:41 +00002758 sqlite3BtreeEnter(p);
drhe5fe6902007-12-07 18:55:28 +00002759 pBt->db = p->db;
drh64022502009-01-09 14:11:04 +00002760 assert( pBt->readOnly==0 );
2761 if( pBt->inStmt ){
danielk1977fd7f0452008-12-17 17:30:26 +00002762 int iStmtpoint = p->db->nSavepoint;
2763 rc = sqlite3PagerSavepoint(pBt->pPager, SAVEPOINT_ROLLBACK, iStmtpoint);
2764 if( rc==SQLITE_OK ){
2765 rc = sqlite3PagerSavepoint(pBt->pPager, SAVEPOINT_RELEASE, iStmtpoint);
2766 }
danielk197797a227c2006-01-20 16:32:04 +00002767 pBt->inStmt = 0;
2768 }
drhd677b3d2007-08-20 22:48:41 +00002769 sqlite3BtreeLeave(p);
drh663fc632002-02-02 18:49:19 +00002770 return rc;
2771}
2772
2773/*
danielk1977fd7f0452008-12-17 17:30:26 +00002774** The second argument to this function, op, is always SAVEPOINT_ROLLBACK
2775** or SAVEPOINT_RELEASE. This function either releases or rolls back the
danielk197712dd5492008-12-18 15:45:07 +00002776** savepoint identified by parameter iSavepoint, depending on the value
2777** of op.
2778**
2779** Normally, iSavepoint is greater than or equal to zero. However, if op is
2780** SAVEPOINT_ROLLBACK, then iSavepoint may also be -1. In this case the
2781** contents of the entire transaction are rolled back. This is different
2782** from a normal transaction rollback, as no locks are released and the
2783** transaction remains open.
danielk1977fd7f0452008-12-17 17:30:26 +00002784*/
2785int sqlite3BtreeSavepoint(Btree *p, int op, int iSavepoint){
2786 int rc = SQLITE_OK;
2787 if( p && p->inTrans==TRANS_WRITE ){
2788 BtShared *pBt = p->pBt;
2789 assert( pBt->inStmt==0 );
2790 assert( op==SAVEPOINT_RELEASE || op==SAVEPOINT_ROLLBACK );
2791 assert( iSavepoint>=0 || (iSavepoint==-1 && op==SAVEPOINT_ROLLBACK) );
2792 sqlite3BtreeEnter(p);
2793 pBt->db = p->db;
2794 rc = sqlite3PagerSavepoint(pBt->pPager, op, iSavepoint);
drh9f0bbf92009-01-02 21:08:09 +00002795 if( rc==SQLITE_OK ){
2796 rc = newDatabase(pBt);
2797 }
danielk1977fd7f0452008-12-17 17:30:26 +00002798 sqlite3BtreeLeave(p);
2799 }
2800 return rc;
2801}
2802
2803/*
drh8b2f49b2001-06-08 00:21:52 +00002804** Create a new cursor for the BTree whose root is on the page
2805** iTable. The act of acquiring a cursor gets a read lock on
2806** the database file.
drh1bee3d72001-10-15 00:44:35 +00002807**
2808** If wrFlag==0, then the cursor can only be used for reading.
drhf74b8d92002-09-01 23:20:45 +00002809** If wrFlag==1, then the cursor can be used for reading or for
2810** writing if other conditions for writing are also met. These
2811** are the conditions that must be met in order for writing to
2812** be allowed:
drh6446c4d2001-12-15 14:22:18 +00002813**
drhf74b8d92002-09-01 23:20:45 +00002814** 1: The cursor must have been opened with wrFlag==1
2815**
drhfe5d71d2007-03-19 11:54:10 +00002816** 2: Other database connections that share the same pager cache
2817** but which are not in the READ_UNCOMMITTED state may not have
2818** cursors open with wrFlag==0 on the same table. Otherwise
2819** the changes made by this write cursor would be visible to
2820** the read cursors in the other database connection.
drhf74b8d92002-09-01 23:20:45 +00002821**
2822** 3: The database must be writable (not on read-only media)
2823**
2824** 4: There must be an active transaction.
2825**
drh6446c4d2001-12-15 14:22:18 +00002826** No checking is done to make sure that page iTable really is the
2827** root page of a b-tree. If it is not, then the cursor acquired
2828** will not work correctly.
danielk197771d5d2c2008-09-29 11:49:47 +00002829**
2830** It is assumed that the sqlite3BtreeCursorSize() bytes of memory
2831** pointed to by pCur have been zeroed by the caller.
drha059ad02001-04-17 20:09:11 +00002832*/
drhd677b3d2007-08-20 22:48:41 +00002833static int btreeCursor(
danielk1977cd3e8f72008-03-25 09:47:35 +00002834 Btree *p, /* The btree */
2835 int iTable, /* Root page of table to open */
2836 int wrFlag, /* 1 to write. 0 read-only */
2837 struct KeyInfo *pKeyInfo, /* First arg to comparison function */
2838 BtCursor *pCur /* Space for new cursor */
drh3aac2dd2004-04-26 14:10:20 +00002839){
drha059ad02001-04-17 20:09:11 +00002840 int rc;
danielk197789d40042008-11-17 14:20:56 +00002841 Pgno nPage;
danielk1977aef0bf62005-12-30 16:28:01 +00002842 BtShared *pBt = p->pBt;
drhecdc7532001-09-23 02:35:53 +00002843
drh1fee73e2007-08-29 04:00:57 +00002844 assert( sqlite3BtreeHoldsMutex(p) );
drhf49661a2008-12-10 16:45:50 +00002845 assert( wrFlag==0 || wrFlag==1 );
drh8dcd7ca2004-08-08 19:43:29 +00002846 if( wrFlag ){
drh64022502009-01-09 14:11:04 +00002847 assert( !pBt->readOnly );
2848 if( NEVER(pBt->readOnly) ){
drh8dcd7ca2004-08-08 19:43:29 +00002849 return SQLITE_READONLY;
2850 }
danielk19773588ceb2008-06-10 17:30:26 +00002851 if( checkReadLocks(p, iTable, 0, 0) ){
drh8dcd7ca2004-08-08 19:43:29 +00002852 return SQLITE_LOCKED;
2853 }
drha0c9a112004-03-10 13:42:37 +00002854 }
danielk1977aef0bf62005-12-30 16:28:01 +00002855
drh4b70f112004-05-02 21:12:19 +00002856 if( pBt->pPage1==0 ){
danielk1977aef0bf62005-12-30 16:28:01 +00002857 rc = lockBtreeWithRetry(p);
drha059ad02001-04-17 20:09:11 +00002858 if( rc!=SQLITE_OK ){
drha059ad02001-04-17 20:09:11 +00002859 return rc;
2860 }
2861 }
drh8b2f49b2001-06-08 00:21:52 +00002862 pCur->pgnoRoot = (Pgno)iTable;
danielk197789d40042008-11-17 14:20:56 +00002863 rc = sqlite3PagerPagecount(pBt->pPager, (int *)&nPage);
2864 if( rc!=SQLITE_OK ){
2865 return rc;
2866 }
2867 if( iTable==1 && nPage==0 ){
drh24cd67e2004-05-10 16:18:47 +00002868 rc = SQLITE_EMPTY;
2869 goto create_cursor_exception;
2870 }
danielk197771d5d2c2008-09-29 11:49:47 +00002871 rc = getAndInitPage(pBt, pCur->pgnoRoot, &pCur->apPage[0]);
drhbd03cae2001-06-02 02:40:57 +00002872 if( rc!=SQLITE_OK ){
2873 goto create_cursor_exception;
drha059ad02001-04-17 20:09:11 +00002874 }
danielk1977aef0bf62005-12-30 16:28:01 +00002875
danielk1977aef0bf62005-12-30 16:28:01 +00002876 /* Now that no other errors can occur, finish filling in the BtCursor
2877 ** variables, link the cursor into the BtShared list and set *ppCur (the
2878 ** output argument to this function).
2879 */
drh1e968a02008-03-25 00:22:21 +00002880 pCur->pKeyInfo = pKeyInfo;
danielk1977aef0bf62005-12-30 16:28:01 +00002881 pCur->pBtree = p;
drhd0679ed2007-08-28 22:24:34 +00002882 pCur->pBt = pBt;
drhf49661a2008-12-10 16:45:50 +00002883 pCur->wrFlag = (u8)wrFlag;
drha059ad02001-04-17 20:09:11 +00002884 pCur->pNext = pBt->pCursor;
2885 if( pCur->pNext ){
2886 pCur->pNext->pPrev = pCur;
2887 }
2888 pBt->pCursor = pCur;
danielk1977da184232006-01-05 11:34:32 +00002889 pCur->eState = CURSOR_INVALID;
drhbd03cae2001-06-02 02:40:57 +00002890
danielk1977aef0bf62005-12-30 16:28:01 +00002891 return SQLITE_OK;
drhd677b3d2007-08-20 22:48:41 +00002892
drhbd03cae2001-06-02 02:40:57 +00002893create_cursor_exception:
danielk197771d5d2c2008-09-29 11:49:47 +00002894 releasePage(pCur->apPage[0]);
drh5e00f6c2001-09-13 13:46:56 +00002895 unlockBtreeIfUnused(pBt);
drhbd03cae2001-06-02 02:40:57 +00002896 return rc;
drha059ad02001-04-17 20:09:11 +00002897}
drhd677b3d2007-08-20 22:48:41 +00002898int sqlite3BtreeCursor(
danielk1977cd3e8f72008-03-25 09:47:35 +00002899 Btree *p, /* The btree */
2900 int iTable, /* Root page of table to open */
2901 int wrFlag, /* 1 to write. 0 read-only */
2902 struct KeyInfo *pKeyInfo, /* First arg to xCompare() */
2903 BtCursor *pCur /* Write new cursor here */
drhd677b3d2007-08-20 22:48:41 +00002904){
2905 int rc;
2906 sqlite3BtreeEnter(p);
drhe5fe6902007-12-07 18:55:28 +00002907 p->pBt->db = p->db;
danielk1977cd3e8f72008-03-25 09:47:35 +00002908 rc = btreeCursor(p, iTable, wrFlag, pKeyInfo, pCur);
drhd677b3d2007-08-20 22:48:41 +00002909 sqlite3BtreeLeave(p);
2910 return rc;
2911}
danielk1977cd3e8f72008-03-25 09:47:35 +00002912int sqlite3BtreeCursorSize(){
2913 return sizeof(BtCursor);
2914}
2915
drhd677b3d2007-08-20 22:48:41 +00002916
drha059ad02001-04-17 20:09:11 +00002917
2918/*
drh5e00f6c2001-09-13 13:46:56 +00002919** Close a cursor. The read lock on the database file is released
drhbd03cae2001-06-02 02:40:57 +00002920** when the last cursor is closed.
drha059ad02001-04-17 20:09:11 +00002921*/
drh3aac2dd2004-04-26 14:10:20 +00002922int sqlite3BtreeCloseCursor(BtCursor *pCur){
drhff0587c2007-08-29 17:43:19 +00002923 Btree *pBtree = pCur->pBtree;
danielk1977cd3e8f72008-03-25 09:47:35 +00002924 if( pBtree ){
danielk197771d5d2c2008-09-29 11:49:47 +00002925 int i;
danielk1977cd3e8f72008-03-25 09:47:35 +00002926 BtShared *pBt = pCur->pBt;
2927 sqlite3BtreeEnter(pBtree);
2928 pBt->db = pBtree->db;
danielk1977be51a652008-10-08 17:58:48 +00002929 sqlite3BtreeClearCursor(pCur);
danielk1977cd3e8f72008-03-25 09:47:35 +00002930 if( pCur->pPrev ){
2931 pCur->pPrev->pNext = pCur->pNext;
2932 }else{
2933 pBt->pCursor = pCur->pNext;
2934 }
2935 if( pCur->pNext ){
2936 pCur->pNext->pPrev = pCur->pPrev;
2937 }
danielk197771d5d2c2008-09-29 11:49:47 +00002938 for(i=0; i<=pCur->iPage; i++){
2939 releasePage(pCur->apPage[i]);
2940 }
danielk1977cd3e8f72008-03-25 09:47:35 +00002941 unlockBtreeIfUnused(pBt);
2942 invalidateOverflowCache(pCur);
2943 /* sqlite3_free(pCur); */
2944 sqlite3BtreeLeave(pBtree);
drha059ad02001-04-17 20:09:11 +00002945 }
drh8c42ca92001-06-22 19:15:00 +00002946 return SQLITE_OK;
drha059ad02001-04-17 20:09:11 +00002947}
2948
drh7e3b0a02001-04-28 16:52:40 +00002949/*
drh5e2f8b92001-05-28 00:41:15 +00002950** Make a temporary cursor by filling in the fields of pTempCur.
2951** The temporary cursor is not on the cursor list for the Btree.
2952*/
drh16a9b832007-05-05 18:39:25 +00002953void sqlite3BtreeGetTempCursor(BtCursor *pCur, BtCursor *pTempCur){
danielk197771d5d2c2008-09-29 11:49:47 +00002954 int i;
drh1fee73e2007-08-29 04:00:57 +00002955 assert( cursorHoldsMutex(pCur) );
danielk197771d5d2c2008-09-29 11:49:47 +00002956 memcpy(pTempCur, pCur, sizeof(BtCursor));
drh5e2f8b92001-05-28 00:41:15 +00002957 pTempCur->pNext = 0;
2958 pTempCur->pPrev = 0;
danielk197771d5d2c2008-09-29 11:49:47 +00002959 for(i=0; i<=pTempCur->iPage; i++){
2960 sqlite3PagerRef(pTempCur->apPage[i]->pDbPage);
drhecdc7532001-09-23 02:35:53 +00002961 }
danielk197736e20932008-11-26 07:40:30 +00002962 assert( pTempCur->pKey==0 );
drh5e2f8b92001-05-28 00:41:15 +00002963}
2964
2965/*
drhbd03cae2001-06-02 02:40:57 +00002966** Delete a temporary cursor such as was made by the CreateTemporaryCursor()
drh5e2f8b92001-05-28 00:41:15 +00002967** function above.
2968*/
drh16a9b832007-05-05 18:39:25 +00002969void sqlite3BtreeReleaseTempCursor(BtCursor *pCur){
danielk197771d5d2c2008-09-29 11:49:47 +00002970 int i;
drh1fee73e2007-08-29 04:00:57 +00002971 assert( cursorHoldsMutex(pCur) );
danielk197771d5d2c2008-09-29 11:49:47 +00002972 for(i=0; i<=pCur->iPage; i++){
2973 sqlite3PagerUnref(pCur->apPage[i]->pDbPage);
drhecdc7532001-09-23 02:35:53 +00002974 }
danielk197736e20932008-11-26 07:40:30 +00002975 sqlite3_free(pCur->pKey);
drh5e2f8b92001-05-28 00:41:15 +00002976}
2977
2978/*
drh86057612007-06-26 01:04:48 +00002979** Make sure the BtCursor* given in the argument has a valid
2980** BtCursor.info structure. If it is not already valid, call
danielk19771cc5ed82007-05-16 17:28:43 +00002981** sqlite3BtreeParseCell() to fill it in.
drhab01f612004-05-22 02:55:23 +00002982**
2983** BtCursor.info is a cache of the information in the current cell.
drh16a9b832007-05-05 18:39:25 +00002984** Using this cache reduces the number of calls to sqlite3BtreeParseCell().
drh86057612007-06-26 01:04:48 +00002985**
2986** 2007-06-25: There is a bug in some versions of MSVC that cause the
2987** compiler to crash when getCellInfo() is implemented as a macro.
2988** But there is a measureable speed advantage to using the macro on gcc
2989** (when less compiler optimizations like -Os or -O0 are used and the
2990** compiler is not doing agressive inlining.) So we use a real function
2991** for MSVC and a macro for everything else. Ticket #2457.
drh9188b382004-05-14 21:12:22 +00002992*/
drh9188b382004-05-14 21:12:22 +00002993#ifndef NDEBUG
danielk19771cc5ed82007-05-16 17:28:43 +00002994 static void assertCellInfo(BtCursor *pCur){
drh9188b382004-05-14 21:12:22 +00002995 CellInfo info;
danielk197771d5d2c2008-09-29 11:49:47 +00002996 int iPage = pCur->iPage;
drh51c6d962004-06-06 00:42:25 +00002997 memset(&info, 0, sizeof(info));
danielk197771d5d2c2008-09-29 11:49:47 +00002998 sqlite3BtreeParseCell(pCur->apPage[iPage], pCur->aiIdx[iPage], &info);
drh9188b382004-05-14 21:12:22 +00002999 assert( memcmp(&info, &pCur->info, sizeof(info))==0 );
drh9188b382004-05-14 21:12:22 +00003000 }
danielk19771cc5ed82007-05-16 17:28:43 +00003001#else
3002 #define assertCellInfo(x)
3003#endif
drh86057612007-06-26 01:04:48 +00003004#ifdef _MSC_VER
3005 /* Use a real function in MSVC to work around bugs in that compiler. */
3006 static void getCellInfo(BtCursor *pCur){
3007 if( pCur->info.nSize==0 ){
danielk197771d5d2c2008-09-29 11:49:47 +00003008 int iPage = pCur->iPage;
3009 sqlite3BtreeParseCell(pCur->apPage[iPage],pCur->aiIdx[iPage],&pCur->info);
drha2c20e42008-03-29 16:01:04 +00003010 pCur->validNKey = 1;
drh86057612007-06-26 01:04:48 +00003011 }else{
3012 assertCellInfo(pCur);
3013 }
3014 }
3015#else /* if not _MSC_VER */
3016 /* Use a macro in all other compilers so that the function is inlined */
danielk197771d5d2c2008-09-29 11:49:47 +00003017#define getCellInfo(pCur) \
3018 if( pCur->info.nSize==0 ){ \
3019 int iPage = pCur->iPage; \
3020 sqlite3BtreeParseCell(pCur->apPage[iPage],pCur->aiIdx[iPage],&pCur->info); \
3021 pCur->validNKey = 1; \
3022 }else{ \
3023 assertCellInfo(pCur); \
drh86057612007-06-26 01:04:48 +00003024 }
3025#endif /* _MSC_VER */
drh9188b382004-05-14 21:12:22 +00003026
3027/*
drh3aac2dd2004-04-26 14:10:20 +00003028** Set *pSize to the size of the buffer needed to hold the value of
3029** the key for the current entry. If the cursor is not pointing
3030** to a valid entry, *pSize is set to 0.
3031**
drh4b70f112004-05-02 21:12:19 +00003032** For a table with the INTKEY flag set, this routine returns the key
drh3aac2dd2004-04-26 14:10:20 +00003033** itself, not the number of bytes in the key.
drh7e3b0a02001-04-28 16:52:40 +00003034*/
drh4a1c3802004-05-12 15:15:47 +00003035int sqlite3BtreeKeySize(BtCursor *pCur, i64 *pSize){
drhd677b3d2007-08-20 22:48:41 +00003036 int rc;
3037
drh1fee73e2007-08-29 04:00:57 +00003038 assert( cursorHoldsMutex(pCur) );
drha3460582008-07-11 21:02:53 +00003039 rc = restoreCursorPosition(pCur);
danielk1977da184232006-01-05 11:34:32 +00003040 if( rc==SQLITE_OK ){
3041 assert( pCur->eState==CURSOR_INVALID || pCur->eState==CURSOR_VALID );
3042 if( pCur->eState==CURSOR_INVALID ){
3043 *pSize = 0;
3044 }else{
drh86057612007-06-26 01:04:48 +00003045 getCellInfo(pCur);
danielk1977da184232006-01-05 11:34:32 +00003046 *pSize = pCur->info.nKey;
3047 }
drh72f82862001-05-24 21:06:34 +00003048 }
danielk1977da184232006-01-05 11:34:32 +00003049 return rc;
drha059ad02001-04-17 20:09:11 +00003050}
drh2af926b2001-05-15 00:39:25 +00003051
drh72f82862001-05-24 21:06:34 +00003052/*
drh0e1c19e2004-05-11 00:58:56 +00003053** Set *pSize to the number of bytes of data in the entry the
3054** cursor currently points to. Always return SQLITE_OK.
3055** Failure is not possible. If the cursor is not currently
3056** pointing to an entry (which can happen, for example, if
3057** the database is empty) then *pSize is set to 0.
3058*/
3059int sqlite3BtreeDataSize(BtCursor *pCur, u32 *pSize){
drhd677b3d2007-08-20 22:48:41 +00003060 int rc;
3061
drh1fee73e2007-08-29 04:00:57 +00003062 assert( cursorHoldsMutex(pCur) );
drha3460582008-07-11 21:02:53 +00003063 rc = restoreCursorPosition(pCur);
danielk1977da184232006-01-05 11:34:32 +00003064 if( rc==SQLITE_OK ){
3065 assert( pCur->eState==CURSOR_INVALID || pCur->eState==CURSOR_VALID );
3066 if( pCur->eState==CURSOR_INVALID ){
3067 /* Not pointing at a valid entry - set *pSize to 0. */
3068 *pSize = 0;
3069 }else{
drh86057612007-06-26 01:04:48 +00003070 getCellInfo(pCur);
danielk1977da184232006-01-05 11:34:32 +00003071 *pSize = pCur->info.nData;
3072 }
drh0e1c19e2004-05-11 00:58:56 +00003073 }
danielk1977da184232006-01-05 11:34:32 +00003074 return rc;
drh0e1c19e2004-05-11 00:58:56 +00003075}
3076
3077/*
danielk1977d04417962007-05-02 13:16:30 +00003078** Given the page number of an overflow page in the database (parameter
3079** ovfl), this function finds the page number of the next page in the
3080** linked list of overflow pages. If possible, it uses the auto-vacuum
3081** pointer-map data instead of reading the content of page ovfl to do so.
3082**
3083** If an error occurs an SQLite error code is returned. Otherwise:
3084**
danielk197745d68822009-01-16 16:23:38 +00003085** Unless pPgnoNext is NULL, the page number of the next overflow
3086** page in the linked list is written to *pPgnoNext. If page ovfl
3087** is the last page in its linked list, *pPgnoNext is set to zero.
danielk1977d04417962007-05-02 13:16:30 +00003088**
danielk197745d68822009-01-16 16:23:38 +00003089** If ppPage is not NULL, *ppPage is set to the MemPage* handle
3090** for page ovfl. The underlying pager page may have been requested
3091** with the noContent flag set, so the page data accessable via
3092** this handle may not be trusted.
danielk1977d04417962007-05-02 13:16:30 +00003093*/
3094static int getOverflowPage(
3095 BtShared *pBt,
3096 Pgno ovfl, /* Overflow page */
danielk197745d68822009-01-16 16:23:38 +00003097 MemPage **ppPage, /* OUT: MemPage handle */
danielk1977d04417962007-05-02 13:16:30 +00003098 Pgno *pPgnoNext /* OUT: Next overflow page number */
3099){
3100 Pgno next = 0;
drh1bd10f82008-12-10 21:19:56 +00003101 int rc = SQLITE_OK;
danielk1977d04417962007-05-02 13:16:30 +00003102
drh1fee73e2007-08-29 04:00:57 +00003103 assert( sqlite3_mutex_held(pBt->mutex) );
danielk197745d68822009-01-16 16:23:38 +00003104 /* One of these must not be NULL. Otherwise, why call this function? */
3105 assert(ppPage || pPgnoNext);
3106
3107 /* If pPgnoNext is NULL, then this function is being called to obtain
3108 ** a MemPage* reference only. No page-data is required in this case.
3109 */
3110 if( !pPgnoNext ){
3111 return sqlite3BtreeGetPage(pBt, ovfl, ppPage, 1);
3112 }
danielk1977d04417962007-05-02 13:16:30 +00003113
3114#ifndef SQLITE_OMIT_AUTOVACUUM
3115 /* Try to find the next page in the overflow list using the
3116 ** autovacuum pointer-map pages. Guess that the next page in
3117 ** the overflow list is page number (ovfl+1). If that guess turns
3118 ** out to be wrong, fall back to loading the data of page
3119 ** number ovfl to determine the next page number.
3120 */
3121 if( pBt->autoVacuum ){
3122 Pgno pgno;
3123 Pgno iGuess = ovfl+1;
3124 u8 eType;
3125
3126 while( PTRMAP_ISPAGE(pBt, iGuess) || iGuess==PENDING_BYTE_PAGE(pBt) ){
3127 iGuess++;
3128 }
3129
danielk197789d40042008-11-17 14:20:56 +00003130 if( iGuess<=pagerPagecount(pBt) ){
danielk1977d04417962007-05-02 13:16:30 +00003131 rc = ptrmapGet(pBt, iGuess, &eType, &pgno);
danielk197745d68822009-01-16 16:23:38 +00003132 if( rc!=SQLITE_OK ){
3133 return rc;
3134 }
3135 if( eType==PTRMAP_OVERFLOW2 && pgno==ovfl ){
danielk1977d04417962007-05-02 13:16:30 +00003136 next = iGuess;
3137 }
3138 }
3139 }
3140#endif
3141
danielk197745d68822009-01-16 16:23:38 +00003142 if( next==0 || ppPage ){
3143 MemPage *pPage = 0;
3144
3145 rc = sqlite3BtreeGetPage(pBt, ovfl, &pPage, next!=0);
danielk1977d04417962007-05-02 13:16:30 +00003146 assert(rc==SQLITE_OK || pPage==0);
3147 if( next==0 && rc==SQLITE_OK ){
3148 next = get4byte(pPage->aData);
3149 }
danielk1977d04417962007-05-02 13:16:30 +00003150
danielk197745d68822009-01-16 16:23:38 +00003151 if( ppPage ){
3152 *ppPage = pPage;
3153 }else{
3154 releasePage(pPage);
3155 }
danielk1977443c0592009-01-16 15:21:05 +00003156 }
danielk197745d68822009-01-16 16:23:38 +00003157 *pPgnoNext = next;
3158
3159 return rc;
danielk1977d04417962007-05-02 13:16:30 +00003160}
3161
danielk1977da107192007-05-04 08:32:13 +00003162/*
3163** Copy data from a buffer to a page, or from a page to a buffer.
3164**
3165** pPayload is a pointer to data stored on database page pDbPage.
3166** If argument eOp is false, then nByte bytes of data are copied
3167** from pPayload to the buffer pointed at by pBuf. If eOp is true,
3168** then sqlite3PagerWrite() is called on pDbPage and nByte bytes
3169** of data are copied from the buffer pBuf to pPayload.
3170**
3171** SQLITE_OK is returned on success, otherwise an error code.
3172*/
3173static int copyPayload(
3174 void *pPayload, /* Pointer to page data */
3175 void *pBuf, /* Pointer to buffer */
3176 int nByte, /* Number of bytes to copy */
3177 int eOp, /* 0 -> copy from page, 1 -> copy to page */
3178 DbPage *pDbPage /* Page containing pPayload */
3179){
3180 if( eOp ){
3181 /* Copy data from buffer to page (a write operation) */
3182 int rc = sqlite3PagerWrite(pDbPage);
3183 if( rc!=SQLITE_OK ){
3184 return rc;
3185 }
3186 memcpy(pPayload, pBuf, nByte);
3187 }else{
3188 /* Copy data from page to buffer (a read operation) */
3189 memcpy(pBuf, pPayload, nByte);
3190 }
3191 return SQLITE_OK;
3192}
danielk1977d04417962007-05-02 13:16:30 +00003193
3194/*
danielk19779f8d6402007-05-02 17:48:45 +00003195** This function is used to read or overwrite payload information
3196** for the entry that the pCur cursor is pointing to. If the eOp
3197** parameter is 0, this is a read operation (data copied into
3198** buffer pBuf). If it is non-zero, a write (data copied from
3199** buffer pBuf).
3200**
3201** A total of "amt" bytes are read or written beginning at "offset".
3202** Data is read to or from the buffer pBuf.
drh72f82862001-05-24 21:06:34 +00003203**
3204** This routine does not make a distinction between key and data.
danielk19779f8d6402007-05-02 17:48:45 +00003205** It just reads or writes bytes from the payload area. Data might
3206** appear on the main page or be scattered out on multiple overflow
3207** pages.
danielk1977da107192007-05-04 08:32:13 +00003208**
danielk1977dcbb5d32007-05-04 18:36:44 +00003209** If the BtCursor.isIncrblobHandle flag is set, and the current
danielk1977da107192007-05-04 08:32:13 +00003210** cursor entry uses one or more overflow pages, this function
3211** allocates space for and lazily popluates the overflow page-list
3212** cache array (BtCursor.aOverflow). Subsequent calls use this
3213** cache to make seeking to the supplied offset more efficient.
3214**
3215** Once an overflow page-list cache has been allocated, it may be
3216** invalidated if some other cursor writes to the same table, or if
3217** the cursor is moved to a different row. Additionally, in auto-vacuum
3218** mode, the following events may invalidate an overflow page-list cache.
3219**
3220** * An incremental vacuum,
3221** * A commit in auto_vacuum="full" mode,
3222** * Creating a table (may require moving an overflow page).
drh72f82862001-05-24 21:06:34 +00003223*/
danielk19779f8d6402007-05-02 17:48:45 +00003224static int accessPayload(
drh3aac2dd2004-04-26 14:10:20 +00003225 BtCursor *pCur, /* Cursor pointing to entry to read from */
danielk197789d40042008-11-17 14:20:56 +00003226 u32 offset, /* Begin reading this far into payload */
3227 u32 amt, /* Read this many bytes */
drh3aac2dd2004-04-26 14:10:20 +00003228 unsigned char *pBuf, /* Write the bytes into this buffer */
danielk19779f8d6402007-05-02 17:48:45 +00003229 int skipKey, /* offset begins at data if this is true */
3230 int eOp /* zero to read. non-zero to write. */
drh3aac2dd2004-04-26 14:10:20 +00003231){
3232 unsigned char *aPayload;
danielk1977da107192007-05-04 08:32:13 +00003233 int rc = SQLITE_OK;
drhfa1a98a2004-05-14 19:08:17 +00003234 u32 nKey;
danielk19772dec9702007-05-02 16:48:37 +00003235 int iIdx = 0;
danielk197771d5d2c2008-09-29 11:49:47 +00003236 MemPage *pPage = pCur->apPage[pCur->iPage]; /* Btree page of current entry */
danielk19770d065412008-11-12 18:21:36 +00003237 BtShared *pBt = pCur->pBt; /* Btree this cursor belongs to */
drh3aac2dd2004-04-26 14:10:20 +00003238
danielk1977da107192007-05-04 08:32:13 +00003239 assert( pPage );
danielk1977da184232006-01-05 11:34:32 +00003240 assert( pCur->eState==CURSOR_VALID );
danielk197771d5d2c2008-09-29 11:49:47 +00003241 assert( pCur->aiIdx[pCur->iPage]<pPage->nCell );
drh1fee73e2007-08-29 04:00:57 +00003242 assert( cursorHoldsMutex(pCur) );
danielk1977da107192007-05-04 08:32:13 +00003243
drh86057612007-06-26 01:04:48 +00003244 getCellInfo(pCur);
drh366fda62006-01-13 02:35:09 +00003245 aPayload = pCur->info.pCell + pCur->info.nHeader;
drhf49661a2008-12-10 16:45:50 +00003246 nKey = (pPage->intKey ? 0 : (int)pCur->info.nKey);
danielk1977da107192007-05-04 08:32:13 +00003247
drh3aac2dd2004-04-26 14:10:20 +00003248 if( skipKey ){
drhfa1a98a2004-05-14 19:08:17 +00003249 offset += nKey;
drh3aac2dd2004-04-26 14:10:20 +00003250 }
danielk19770d065412008-11-12 18:21:36 +00003251 if( offset+amt > nKey+pCur->info.nData
3252 || &aPayload[pCur->info.nLocal] > &pPage->aData[pBt->usableSize]
3253 ){
danielk1977da107192007-05-04 08:32:13 +00003254 /* Trying to read or write past the end of the data is an error */
danielk197767fd7a92008-09-10 17:53:35 +00003255 return SQLITE_CORRUPT_BKPT;
drh3aac2dd2004-04-26 14:10:20 +00003256 }
danielk1977da107192007-05-04 08:32:13 +00003257
3258 /* Check if data must be read/written to/from the btree page itself. */
drhfa1a98a2004-05-14 19:08:17 +00003259 if( offset<pCur->info.nLocal ){
drh2af926b2001-05-15 00:39:25 +00003260 int a = amt;
drhfa1a98a2004-05-14 19:08:17 +00003261 if( a+offset>pCur->info.nLocal ){
3262 a = pCur->info.nLocal - offset;
drh2af926b2001-05-15 00:39:25 +00003263 }
danielk1977da107192007-05-04 08:32:13 +00003264 rc = copyPayload(&aPayload[offset], pBuf, a, eOp, pPage->pDbPage);
drh2aa679f2001-06-25 02:11:07 +00003265 offset = 0;
drha34b6762004-05-07 13:30:42 +00003266 pBuf += a;
drh2af926b2001-05-15 00:39:25 +00003267 amt -= a;
drhdd793422001-06-28 01:54:48 +00003268 }else{
drhfa1a98a2004-05-14 19:08:17 +00003269 offset -= pCur->info.nLocal;
drhbd03cae2001-06-02 02:40:57 +00003270 }
danielk1977da107192007-05-04 08:32:13 +00003271
3272 if( rc==SQLITE_OK && amt>0 ){
danielk197789d40042008-11-17 14:20:56 +00003273 const u32 ovflSize = pBt->usableSize - 4; /* Bytes content per ovfl page */
danielk1977da107192007-05-04 08:32:13 +00003274 Pgno nextPage;
3275
drhfa1a98a2004-05-14 19:08:17 +00003276 nextPage = get4byte(&aPayload[pCur->info.nLocal]);
danielk1977da107192007-05-04 08:32:13 +00003277
danielk19772dec9702007-05-02 16:48:37 +00003278#ifndef SQLITE_OMIT_INCRBLOB
danielk1977dcbb5d32007-05-04 18:36:44 +00003279 /* If the isIncrblobHandle flag is set and the BtCursor.aOverflow[]
danielk1977da107192007-05-04 08:32:13 +00003280 ** has not been allocated, allocate it now. The array is sized at
3281 ** one entry for each overflow page in the overflow chain. The
3282 ** page number of the first overflow page is stored in aOverflow[0],
3283 ** etc. A value of 0 in the aOverflow[] array means "not yet known"
3284 ** (the cache is lazily populated).
3285 */
danielk1977dcbb5d32007-05-04 18:36:44 +00003286 if( pCur->isIncrblobHandle && !pCur->aOverflow ){
danielk19772dec9702007-05-02 16:48:37 +00003287 int nOvfl = (pCur->info.nPayload-pCur->info.nLocal+ovflSize-1)/ovflSize;
drh17435752007-08-16 04:30:38 +00003288 pCur->aOverflow = (Pgno *)sqlite3MallocZero(sizeof(Pgno)*nOvfl);
danielk19772dec9702007-05-02 16:48:37 +00003289 if( nOvfl && !pCur->aOverflow ){
danielk1977da107192007-05-04 08:32:13 +00003290 rc = SQLITE_NOMEM;
danielk19772dec9702007-05-02 16:48:37 +00003291 }
3292 }
danielk1977da107192007-05-04 08:32:13 +00003293
3294 /* If the overflow page-list cache has been allocated and the
3295 ** entry for the first required overflow page is valid, skip
3296 ** directly to it.
3297 */
danielk19772dec9702007-05-02 16:48:37 +00003298 if( pCur->aOverflow && pCur->aOverflow[offset/ovflSize] ){
3299 iIdx = (offset/ovflSize);
3300 nextPage = pCur->aOverflow[iIdx];
3301 offset = (offset%ovflSize);
3302 }
3303#endif
danielk1977da107192007-05-04 08:32:13 +00003304
3305 for( ; rc==SQLITE_OK && amt>0 && nextPage; iIdx++){
3306
3307#ifndef SQLITE_OMIT_INCRBLOB
3308 /* If required, populate the overflow page-list cache. */
3309 if( pCur->aOverflow ){
3310 assert(!pCur->aOverflow[iIdx] || pCur->aOverflow[iIdx]==nextPage);
3311 pCur->aOverflow[iIdx] = nextPage;
3312 }
3313#endif
3314
danielk1977d04417962007-05-02 13:16:30 +00003315 if( offset>=ovflSize ){
3316 /* The only reason to read this page is to obtain the page
danielk1977da107192007-05-04 08:32:13 +00003317 ** number for the next page in the overflow chain. The page
drhfd131da2007-08-07 17:13:03 +00003318 ** data is not required. So first try to lookup the overflow
3319 ** page-list cache, if any, then fall back to the getOverflowPage()
danielk1977da107192007-05-04 08:32:13 +00003320 ** function.
danielk1977d04417962007-05-02 13:16:30 +00003321 */
danielk19772dec9702007-05-02 16:48:37 +00003322#ifndef SQLITE_OMIT_INCRBLOB
danielk1977da107192007-05-04 08:32:13 +00003323 if( pCur->aOverflow && pCur->aOverflow[iIdx+1] ){
3324 nextPage = pCur->aOverflow[iIdx+1];
3325 } else
danielk19772dec9702007-05-02 16:48:37 +00003326#endif
danielk1977da107192007-05-04 08:32:13 +00003327 rc = getOverflowPage(pBt, nextPage, 0, &nextPage);
danielk1977da107192007-05-04 08:32:13 +00003328 offset -= ovflSize;
danielk1977d04417962007-05-02 13:16:30 +00003329 }else{
danielk19779f8d6402007-05-02 17:48:45 +00003330 /* Need to read this page properly. It contains some of the
3331 ** range of data that is being read (eOp==0) or written (eOp!=0).
danielk1977d04417962007-05-02 13:16:30 +00003332 */
3333 DbPage *pDbPage;
danielk1977cfe9a692004-06-16 12:00:29 +00003334 int a = amt;
danielk1977d04417962007-05-02 13:16:30 +00003335 rc = sqlite3PagerGet(pBt->pPager, nextPage, &pDbPage);
danielk1977da107192007-05-04 08:32:13 +00003336 if( rc==SQLITE_OK ){
3337 aPayload = sqlite3PagerGetData(pDbPage);
3338 nextPage = get4byte(aPayload);
3339 if( a + offset > ovflSize ){
3340 a = ovflSize - offset;
danielk19779f8d6402007-05-02 17:48:45 +00003341 }
danielk1977da107192007-05-04 08:32:13 +00003342 rc = copyPayload(&aPayload[offset+4], pBuf, a, eOp, pDbPage);
3343 sqlite3PagerUnref(pDbPage);
3344 offset = 0;
3345 amt -= a;
3346 pBuf += a;
danielk19779f8d6402007-05-02 17:48:45 +00003347 }
danielk1977cfe9a692004-06-16 12:00:29 +00003348 }
drh2af926b2001-05-15 00:39:25 +00003349 }
drh2af926b2001-05-15 00:39:25 +00003350 }
danielk1977cfe9a692004-06-16 12:00:29 +00003351
danielk1977da107192007-05-04 08:32:13 +00003352 if( rc==SQLITE_OK && amt>0 ){
drh49285702005-09-17 15:20:26 +00003353 return SQLITE_CORRUPT_BKPT;
drha7fcb052001-12-14 15:09:55 +00003354 }
danielk1977da107192007-05-04 08:32:13 +00003355 return rc;
drh2af926b2001-05-15 00:39:25 +00003356}
3357
drh72f82862001-05-24 21:06:34 +00003358/*
drh3aac2dd2004-04-26 14:10:20 +00003359** Read part of the key associated with cursor pCur. Exactly
drha34b6762004-05-07 13:30:42 +00003360** "amt" bytes will be transfered into pBuf[]. The transfer
drh3aac2dd2004-04-26 14:10:20 +00003361** begins at "offset".
drh8c1238a2003-01-02 14:43:55 +00003362**
drh3aac2dd2004-04-26 14:10:20 +00003363** Return SQLITE_OK on success or an error code if anything goes
3364** wrong. An error is returned if "offset+amt" is larger than
3365** the available payload.
drh72f82862001-05-24 21:06:34 +00003366*/
drha34b6762004-05-07 13:30:42 +00003367int sqlite3BtreeKey(BtCursor *pCur, u32 offset, u32 amt, void *pBuf){
drhd677b3d2007-08-20 22:48:41 +00003368 int rc;
3369
drh1fee73e2007-08-29 04:00:57 +00003370 assert( cursorHoldsMutex(pCur) );
drha3460582008-07-11 21:02:53 +00003371 rc = restoreCursorPosition(pCur);
danielk1977da184232006-01-05 11:34:32 +00003372 if( rc==SQLITE_OK ){
3373 assert( pCur->eState==CURSOR_VALID );
danielk197771d5d2c2008-09-29 11:49:47 +00003374 assert( pCur->iPage>=0 && pCur->apPage[pCur->iPage] );
3375 if( pCur->apPage[0]->intKey ){
danielk1977da184232006-01-05 11:34:32 +00003376 return SQLITE_CORRUPT_BKPT;
3377 }
danielk197771d5d2c2008-09-29 11:49:47 +00003378 assert( pCur->aiIdx[pCur->iPage]<pCur->apPage[pCur->iPage]->nCell );
drh16a9b832007-05-05 18:39:25 +00003379 rc = accessPayload(pCur, offset, amt, (unsigned char*)pBuf, 0, 0);
drh6575a222005-03-10 17:06:34 +00003380 }
danielk1977da184232006-01-05 11:34:32 +00003381 return rc;
drh3aac2dd2004-04-26 14:10:20 +00003382}
3383
3384/*
drh3aac2dd2004-04-26 14:10:20 +00003385** Read part of the data associated with cursor pCur. Exactly
drha34b6762004-05-07 13:30:42 +00003386** "amt" bytes will be transfered into pBuf[]. The transfer
drh3aac2dd2004-04-26 14:10:20 +00003387** begins at "offset".
3388**
3389** Return SQLITE_OK on success or an error code if anything goes
3390** wrong. An error is returned if "offset+amt" is larger than
3391** the available payload.
drh72f82862001-05-24 21:06:34 +00003392*/
drh3aac2dd2004-04-26 14:10:20 +00003393int sqlite3BtreeData(BtCursor *pCur, u32 offset, u32 amt, void *pBuf){
drhd677b3d2007-08-20 22:48:41 +00003394 int rc;
3395
danielk19773588ceb2008-06-10 17:30:26 +00003396#ifndef SQLITE_OMIT_INCRBLOB
3397 if ( pCur->eState==CURSOR_INVALID ){
3398 return SQLITE_ABORT;
3399 }
3400#endif
3401
drh1fee73e2007-08-29 04:00:57 +00003402 assert( cursorHoldsMutex(pCur) );
drha3460582008-07-11 21:02:53 +00003403 rc = restoreCursorPosition(pCur);
danielk1977da184232006-01-05 11:34:32 +00003404 if( rc==SQLITE_OK ){
3405 assert( pCur->eState==CURSOR_VALID );
danielk197771d5d2c2008-09-29 11:49:47 +00003406 assert( pCur->iPage>=0 && pCur->apPage[pCur->iPage] );
3407 assert( pCur->aiIdx[pCur->iPage]<pCur->apPage[pCur->iPage]->nCell );
drh16a9b832007-05-05 18:39:25 +00003408 rc = accessPayload(pCur, offset, amt, pBuf, 1, 0);
danielk1977da184232006-01-05 11:34:32 +00003409 }
3410 return rc;
drh2af926b2001-05-15 00:39:25 +00003411}
3412
drh72f82862001-05-24 21:06:34 +00003413/*
drh0e1c19e2004-05-11 00:58:56 +00003414** Return a pointer to payload information from the entry that the
3415** pCur cursor is pointing to. The pointer is to the beginning of
3416** the key if skipKey==0 and it points to the beginning of data if
drhe51c44f2004-05-30 20:46:09 +00003417** skipKey==1. The number of bytes of available key/data is written
3418** into *pAmt. If *pAmt==0, then the value returned will not be
3419** a valid pointer.
drh0e1c19e2004-05-11 00:58:56 +00003420**
3421** This routine is an optimization. It is common for the entire key
3422** and data to fit on the local page and for there to be no overflow
3423** pages. When that is so, this routine can be used to access the
3424** key and data without making a copy. If the key and/or data spills
drh16a9b832007-05-05 18:39:25 +00003425** onto overflow pages, then accessPayload() must be used to reassembly
drh0e1c19e2004-05-11 00:58:56 +00003426** the key/data and copy it into a preallocated buffer.
3427**
3428** The pointer returned by this routine looks directly into the cached
3429** page of the database. The data might change or move the next time
3430** any btree routine is called.
3431*/
3432static const unsigned char *fetchPayload(
3433 BtCursor *pCur, /* Cursor pointing to entry to read from */
drhe51c44f2004-05-30 20:46:09 +00003434 int *pAmt, /* Write the number of available bytes here */
drh0e1c19e2004-05-11 00:58:56 +00003435 int skipKey /* read beginning at data if this is true */
3436){
3437 unsigned char *aPayload;
3438 MemPage *pPage;
drhfa1a98a2004-05-14 19:08:17 +00003439 u32 nKey;
danielk197789d40042008-11-17 14:20:56 +00003440 u32 nLocal;
drh0e1c19e2004-05-11 00:58:56 +00003441
danielk197771d5d2c2008-09-29 11:49:47 +00003442 assert( pCur!=0 && pCur->iPage>=0 && pCur->apPage[pCur->iPage]);
danielk1977da184232006-01-05 11:34:32 +00003443 assert( pCur->eState==CURSOR_VALID );
drh1fee73e2007-08-29 04:00:57 +00003444 assert( cursorHoldsMutex(pCur) );
danielk197771d5d2c2008-09-29 11:49:47 +00003445 pPage = pCur->apPage[pCur->iPage];
3446 assert( pCur->aiIdx[pCur->iPage]<pPage->nCell );
drh86057612007-06-26 01:04:48 +00003447 getCellInfo(pCur);
drh43605152004-05-29 21:46:49 +00003448 aPayload = pCur->info.pCell;
drhfa1a98a2004-05-14 19:08:17 +00003449 aPayload += pCur->info.nHeader;
drh0e1c19e2004-05-11 00:58:56 +00003450 if( pPage->intKey ){
drhfa1a98a2004-05-14 19:08:17 +00003451 nKey = 0;
3452 }else{
drhf49661a2008-12-10 16:45:50 +00003453 nKey = (int)pCur->info.nKey;
drh0e1c19e2004-05-11 00:58:56 +00003454 }
drh0e1c19e2004-05-11 00:58:56 +00003455 if( skipKey ){
drhfa1a98a2004-05-14 19:08:17 +00003456 aPayload += nKey;
3457 nLocal = pCur->info.nLocal - nKey;
drh0e1c19e2004-05-11 00:58:56 +00003458 }else{
drhfa1a98a2004-05-14 19:08:17 +00003459 nLocal = pCur->info.nLocal;
drhe51c44f2004-05-30 20:46:09 +00003460 if( nLocal>nKey ){
3461 nLocal = nKey;
3462 }
drh0e1c19e2004-05-11 00:58:56 +00003463 }
drhe51c44f2004-05-30 20:46:09 +00003464 *pAmt = nLocal;
drh0e1c19e2004-05-11 00:58:56 +00003465 return aPayload;
3466}
3467
3468
3469/*
drhe51c44f2004-05-30 20:46:09 +00003470** For the entry that cursor pCur is point to, return as
3471** many bytes of the key or data as are available on the local
3472** b-tree page. Write the number of available bytes into *pAmt.
drh0e1c19e2004-05-11 00:58:56 +00003473**
3474** The pointer returned is ephemeral. The key/data may move
drhd677b3d2007-08-20 22:48:41 +00003475** or be destroyed on the next call to any Btree routine,
3476** including calls from other threads against the same cache.
3477** Hence, a mutex on the BtShared should be held prior to calling
3478** this routine.
drh0e1c19e2004-05-11 00:58:56 +00003479**
3480** These routines is used to get quick access to key and data
3481** in the common case where no overflow pages are used.
drh0e1c19e2004-05-11 00:58:56 +00003482*/
drhe51c44f2004-05-30 20:46:09 +00003483const void *sqlite3BtreeKeyFetch(BtCursor *pCur, int *pAmt){
drh1fee73e2007-08-29 04:00:57 +00003484 assert( cursorHoldsMutex(pCur) );
danielk1977da184232006-01-05 11:34:32 +00003485 if( pCur->eState==CURSOR_VALID ){
3486 return (const void*)fetchPayload(pCur, pAmt, 0);
3487 }
3488 return 0;
drh0e1c19e2004-05-11 00:58:56 +00003489}
drhe51c44f2004-05-30 20:46:09 +00003490const void *sqlite3BtreeDataFetch(BtCursor *pCur, int *pAmt){
drh1fee73e2007-08-29 04:00:57 +00003491 assert( cursorHoldsMutex(pCur) );
danielk1977da184232006-01-05 11:34:32 +00003492 if( pCur->eState==CURSOR_VALID ){
3493 return (const void*)fetchPayload(pCur, pAmt, 1);
3494 }
3495 return 0;
drh0e1c19e2004-05-11 00:58:56 +00003496}
3497
3498
3499/*
drh8178a752003-01-05 21:41:40 +00003500** Move the cursor down to a new child page. The newPgno argument is the
drhab01f612004-05-22 02:55:23 +00003501** page number of the child page to move to.
drh72f82862001-05-24 21:06:34 +00003502*/
drh3aac2dd2004-04-26 14:10:20 +00003503static int moveToChild(BtCursor *pCur, u32 newPgno){
drh72f82862001-05-24 21:06:34 +00003504 int rc;
danielk197771d5d2c2008-09-29 11:49:47 +00003505 int i = pCur->iPage;
drh72f82862001-05-24 21:06:34 +00003506 MemPage *pNewPage;
drhd0679ed2007-08-28 22:24:34 +00003507 BtShared *pBt = pCur->pBt;
drh72f82862001-05-24 21:06:34 +00003508
drh1fee73e2007-08-29 04:00:57 +00003509 assert( cursorHoldsMutex(pCur) );
danielk1977da184232006-01-05 11:34:32 +00003510 assert( pCur->eState==CURSOR_VALID );
danielk197771d5d2c2008-09-29 11:49:47 +00003511 assert( pCur->iPage<BTCURSOR_MAX_DEPTH );
3512 if( pCur->iPage>=(BTCURSOR_MAX_DEPTH-1) ){
3513 return SQLITE_CORRUPT_BKPT;
3514 }
3515 rc = getAndInitPage(pBt, newPgno, &pNewPage);
drh6019e162001-07-02 17:51:45 +00003516 if( rc ) return rc;
danielk197771d5d2c2008-09-29 11:49:47 +00003517 pCur->apPage[i+1] = pNewPage;
3518 pCur->aiIdx[i+1] = 0;
3519 pCur->iPage++;
3520
drh271efa52004-05-30 19:19:05 +00003521 pCur->info.nSize = 0;
drha2c20e42008-03-29 16:01:04 +00003522 pCur->validNKey = 0;
drh4be295b2003-12-16 03:44:47 +00003523 if( pNewPage->nCell<1 ){
drh49285702005-09-17 15:20:26 +00003524 return SQLITE_CORRUPT_BKPT;
drh4be295b2003-12-16 03:44:47 +00003525 }
drh72f82862001-05-24 21:06:34 +00003526 return SQLITE_OK;
3527}
3528
danielk1977bf93c562008-09-29 15:53:25 +00003529#ifndef NDEBUG
3530/*
3531** Page pParent is an internal (non-leaf) tree page. This function
3532** asserts that page number iChild is the left-child if the iIdx'th
3533** cell in page pParent. Or, if iIdx is equal to the total number of
3534** cells in pParent, that page number iChild is the right-child of
3535** the page.
3536*/
3537static void assertParentIndex(MemPage *pParent, int iIdx, Pgno iChild){
3538 assert( iIdx<=pParent->nCell );
3539 if( iIdx==pParent->nCell ){
3540 assert( get4byte(&pParent->aData[pParent->hdrOffset+8])==iChild );
3541 }else{
3542 assert( get4byte(findCell(pParent, iIdx))==iChild );
3543 }
3544}
3545#else
3546# define assertParentIndex(x,y,z)
3547#endif
3548
drh72f82862001-05-24 21:06:34 +00003549/*
drh5e2f8b92001-05-28 00:41:15 +00003550** Move the cursor up to the parent page.
3551**
3552** pCur->idx is set to the cell index that contains the pointer
3553** to the page we are coming from. If we are coming from the
3554** right-most child page then pCur->idx is set to one more than
drhbd03cae2001-06-02 02:40:57 +00003555** the largest cell index.
drh72f82862001-05-24 21:06:34 +00003556*/
drh16a9b832007-05-05 18:39:25 +00003557void sqlite3BtreeMoveToParent(BtCursor *pCur){
drh1fee73e2007-08-29 04:00:57 +00003558 assert( cursorHoldsMutex(pCur) );
danielk1977da184232006-01-05 11:34:32 +00003559 assert( pCur->eState==CURSOR_VALID );
danielk197771d5d2c2008-09-29 11:49:47 +00003560 assert( pCur->iPage>0 );
3561 assert( pCur->apPage[pCur->iPage] );
danielk1977bf93c562008-09-29 15:53:25 +00003562 assertParentIndex(
3563 pCur->apPage[pCur->iPage-1],
3564 pCur->aiIdx[pCur->iPage-1],
3565 pCur->apPage[pCur->iPage]->pgno
3566 );
danielk197771d5d2c2008-09-29 11:49:47 +00003567 releasePage(pCur->apPage[pCur->iPage]);
3568 pCur->iPage--;
drh271efa52004-05-30 19:19:05 +00003569 pCur->info.nSize = 0;
drha2c20e42008-03-29 16:01:04 +00003570 pCur->validNKey = 0;
drh72f82862001-05-24 21:06:34 +00003571}
3572
3573/*
3574** Move the cursor to the root page
3575*/
drh5e2f8b92001-05-28 00:41:15 +00003576static int moveToRoot(BtCursor *pCur){
drh3aac2dd2004-04-26 14:10:20 +00003577 MemPage *pRoot;
drh777e4c42006-01-13 04:31:58 +00003578 int rc = SQLITE_OK;
drhd677b3d2007-08-20 22:48:41 +00003579 Btree *p = pCur->pBtree;
3580 BtShared *pBt = p->pBt;
drhbd03cae2001-06-02 02:40:57 +00003581
drh1fee73e2007-08-29 04:00:57 +00003582 assert( cursorHoldsMutex(pCur) );
drhfb982642007-08-30 01:19:59 +00003583 assert( CURSOR_INVALID < CURSOR_REQUIRESEEK );
3584 assert( CURSOR_VALID < CURSOR_REQUIRESEEK );
3585 assert( CURSOR_FAULT > CURSOR_REQUIRESEEK );
3586 if( pCur->eState>=CURSOR_REQUIRESEEK ){
3587 if( pCur->eState==CURSOR_FAULT ){
3588 return pCur->skip;
3589 }
danielk1977be51a652008-10-08 17:58:48 +00003590 sqlite3BtreeClearCursor(pCur);
drhbf700f32007-03-31 02:36:44 +00003591 }
danielk197771d5d2c2008-09-29 11:49:47 +00003592
3593 if( pCur->iPage>=0 ){
3594 int i;
3595 for(i=1; i<=pCur->iPage; i++){
3596 releasePage(pCur->apPage[i]);
danielk1977d9f6c532008-09-19 16:39:38 +00003597 }
drh777e4c42006-01-13 04:31:58 +00003598 }else{
3599 if(
danielk197771d5d2c2008-09-29 11:49:47 +00003600 SQLITE_OK!=(rc = getAndInitPage(pBt, pCur->pgnoRoot, &pCur->apPage[0]))
drh777e4c42006-01-13 04:31:58 +00003601 ){
3602 pCur->eState = CURSOR_INVALID;
3603 return rc;
3604 }
drhc39e0002004-05-07 23:50:57 +00003605 }
danielk197771d5d2c2008-09-29 11:49:47 +00003606
3607 pRoot = pCur->apPage[0];
3608 assert( pRoot->pgno==pCur->pgnoRoot );
3609 pCur->iPage = 0;
3610 pCur->aiIdx[0] = 0;
drh271efa52004-05-30 19:19:05 +00003611 pCur->info.nSize = 0;
drha2c20e42008-03-29 16:01:04 +00003612 pCur->atLast = 0;
3613 pCur->validNKey = 0;
danielk197771d5d2c2008-09-29 11:49:47 +00003614
drh8856d6a2004-04-29 14:42:46 +00003615 if( pRoot->nCell==0 && !pRoot->leaf ){
3616 Pgno subpage;
3617 assert( pRoot->pgno==1 );
drh43605152004-05-29 21:46:49 +00003618 subpage = get4byte(&pRoot->aData[pRoot->hdrOffset+8]);
drh8856d6a2004-04-29 14:42:46 +00003619 assert( subpage>0 );
danielk1977da184232006-01-05 11:34:32 +00003620 pCur->eState = CURSOR_VALID;
drh4b70f112004-05-02 21:12:19 +00003621 rc = moveToChild(pCur, subpage);
danielk197771d5d2c2008-09-29 11:49:47 +00003622 }else{
3623 pCur->eState = ((pRoot->nCell>0)?CURSOR_VALID:CURSOR_INVALID);
drh8856d6a2004-04-29 14:42:46 +00003624 }
3625 return rc;
drh72f82862001-05-24 21:06:34 +00003626}
drh2af926b2001-05-15 00:39:25 +00003627
drh5e2f8b92001-05-28 00:41:15 +00003628/*
3629** Move the cursor down to the left-most leaf entry beneath the
3630** entry to which it is currently pointing.
drh777e4c42006-01-13 04:31:58 +00003631**
3632** The left-most leaf is the one with the smallest key - the first
3633** in ascending order.
drh5e2f8b92001-05-28 00:41:15 +00003634*/
3635static int moveToLeftmost(BtCursor *pCur){
3636 Pgno pgno;
drhd677b3d2007-08-20 22:48:41 +00003637 int rc = SQLITE_OK;
drh3aac2dd2004-04-26 14:10:20 +00003638 MemPage *pPage;
drh5e2f8b92001-05-28 00:41:15 +00003639
drh1fee73e2007-08-29 04:00:57 +00003640 assert( cursorHoldsMutex(pCur) );
danielk1977da184232006-01-05 11:34:32 +00003641 assert( pCur->eState==CURSOR_VALID );
danielk197771d5d2c2008-09-29 11:49:47 +00003642 while( rc==SQLITE_OK && !(pPage = pCur->apPage[pCur->iPage])->leaf ){
3643 assert( pCur->aiIdx[pCur->iPage]<pPage->nCell );
3644 pgno = get4byte(findCell(pPage, pCur->aiIdx[pCur->iPage]));
drh8178a752003-01-05 21:41:40 +00003645 rc = moveToChild(pCur, pgno);
drh5e2f8b92001-05-28 00:41:15 +00003646 }
drhd677b3d2007-08-20 22:48:41 +00003647 return rc;
drh5e2f8b92001-05-28 00:41:15 +00003648}
3649
drh2dcc9aa2002-12-04 13:40:25 +00003650/*
3651** Move the cursor down to the right-most leaf entry beneath the
3652** page to which it is currently pointing. Notice the difference
3653** between moveToLeftmost() and moveToRightmost(). moveToLeftmost()
3654** finds the left-most entry beneath the *entry* whereas moveToRightmost()
3655** finds the right-most entry beneath the *page*.
drh777e4c42006-01-13 04:31:58 +00003656**
3657** The right-most entry is the one with the largest key - the last
3658** key in ascending order.
drh2dcc9aa2002-12-04 13:40:25 +00003659*/
3660static int moveToRightmost(BtCursor *pCur){
3661 Pgno pgno;
drhd677b3d2007-08-20 22:48:41 +00003662 int rc = SQLITE_OK;
drh1bd10f82008-12-10 21:19:56 +00003663 MemPage *pPage = 0;
drh2dcc9aa2002-12-04 13:40:25 +00003664
drh1fee73e2007-08-29 04:00:57 +00003665 assert( cursorHoldsMutex(pCur) );
danielk1977da184232006-01-05 11:34:32 +00003666 assert( pCur->eState==CURSOR_VALID );
danielk197771d5d2c2008-09-29 11:49:47 +00003667 while( rc==SQLITE_OK && !(pPage = pCur->apPage[pCur->iPage])->leaf ){
drh43605152004-05-29 21:46:49 +00003668 pgno = get4byte(&pPage->aData[pPage->hdrOffset+8]);
danielk197771d5d2c2008-09-29 11:49:47 +00003669 pCur->aiIdx[pCur->iPage] = pPage->nCell;
drh8178a752003-01-05 21:41:40 +00003670 rc = moveToChild(pCur, pgno);
drh2dcc9aa2002-12-04 13:40:25 +00003671 }
drhd677b3d2007-08-20 22:48:41 +00003672 if( rc==SQLITE_OK ){
danielk197771d5d2c2008-09-29 11:49:47 +00003673 pCur->aiIdx[pCur->iPage] = pPage->nCell-1;
drhd677b3d2007-08-20 22:48:41 +00003674 pCur->info.nSize = 0;
drha2c20e42008-03-29 16:01:04 +00003675 pCur->validNKey = 0;
drhd677b3d2007-08-20 22:48:41 +00003676 }
danielk1977518002e2008-09-05 05:02:46 +00003677 return rc;
drh2dcc9aa2002-12-04 13:40:25 +00003678}
3679
drh5e00f6c2001-09-13 13:46:56 +00003680/* Move the cursor to the first entry in the table. Return SQLITE_OK
3681** on success. Set *pRes to 0 if the cursor actually points to something
drh77c679c2002-02-19 22:43:58 +00003682** or set *pRes to 1 if the table is empty.
drh5e00f6c2001-09-13 13:46:56 +00003683*/
drh3aac2dd2004-04-26 14:10:20 +00003684int sqlite3BtreeFirst(BtCursor *pCur, int *pRes){
drh5e00f6c2001-09-13 13:46:56 +00003685 int rc;
drhd677b3d2007-08-20 22:48:41 +00003686
drh1fee73e2007-08-29 04:00:57 +00003687 assert( cursorHoldsMutex(pCur) );
drhe5fe6902007-12-07 18:55:28 +00003688 assert( sqlite3_mutex_held(pCur->pBtree->db->mutex) );
drh5e00f6c2001-09-13 13:46:56 +00003689 rc = moveToRoot(pCur);
drhd677b3d2007-08-20 22:48:41 +00003690 if( rc==SQLITE_OK ){
3691 if( pCur->eState==CURSOR_INVALID ){
danielk197771d5d2c2008-09-29 11:49:47 +00003692 assert( pCur->apPage[pCur->iPage]->nCell==0 );
drhd677b3d2007-08-20 22:48:41 +00003693 *pRes = 1;
3694 rc = SQLITE_OK;
3695 }else{
danielk197771d5d2c2008-09-29 11:49:47 +00003696 assert( pCur->apPage[pCur->iPage]->nCell>0 );
drhd677b3d2007-08-20 22:48:41 +00003697 *pRes = 0;
3698 rc = moveToLeftmost(pCur);
3699 }
drh5e00f6c2001-09-13 13:46:56 +00003700 }
drh5e00f6c2001-09-13 13:46:56 +00003701 return rc;
3702}
drh5e2f8b92001-05-28 00:41:15 +00003703
drh9562b552002-02-19 15:00:07 +00003704/* Move the cursor to the last entry in the table. Return SQLITE_OK
3705** on success. Set *pRes to 0 if the cursor actually points to something
drh77c679c2002-02-19 22:43:58 +00003706** or set *pRes to 1 if the table is empty.
drh9562b552002-02-19 15:00:07 +00003707*/
drh3aac2dd2004-04-26 14:10:20 +00003708int sqlite3BtreeLast(BtCursor *pCur, int *pRes){
drh9562b552002-02-19 15:00:07 +00003709 int rc;
drhd677b3d2007-08-20 22:48:41 +00003710
drh1fee73e2007-08-29 04:00:57 +00003711 assert( cursorHoldsMutex(pCur) );
drhe5fe6902007-12-07 18:55:28 +00003712 assert( sqlite3_mutex_held(pCur->pBtree->db->mutex) );
drh9562b552002-02-19 15:00:07 +00003713 rc = moveToRoot(pCur);
drhd677b3d2007-08-20 22:48:41 +00003714 if( rc==SQLITE_OK ){
3715 if( CURSOR_INVALID==pCur->eState ){
danielk197771d5d2c2008-09-29 11:49:47 +00003716 assert( pCur->apPage[pCur->iPage]->nCell==0 );
drhd677b3d2007-08-20 22:48:41 +00003717 *pRes = 1;
3718 }else{
3719 assert( pCur->eState==CURSOR_VALID );
3720 *pRes = 0;
3721 rc = moveToRightmost(pCur);
drha2c20e42008-03-29 16:01:04 +00003722 getCellInfo(pCur);
drhf49661a2008-12-10 16:45:50 +00003723 pCur->atLast = rc==SQLITE_OK ?1:0;
drhd677b3d2007-08-20 22:48:41 +00003724 }
drh9562b552002-02-19 15:00:07 +00003725 }
drh9562b552002-02-19 15:00:07 +00003726 return rc;
3727}
3728
drhe14006d2008-03-25 17:23:32 +00003729/* Move the cursor so that it points to an entry near the key
drhe63d9992008-08-13 19:11:48 +00003730** specified by pIdxKey or intKey. Return a success code.
drh72f82862001-05-24 21:06:34 +00003731**
drhe63d9992008-08-13 19:11:48 +00003732** For INTKEY tables, the intKey parameter is used. pIdxKey
3733** must be NULL. For index tables, pIdxKey is used and intKey
3734** is ignored.
drh3aac2dd2004-04-26 14:10:20 +00003735**
drh5e2f8b92001-05-28 00:41:15 +00003736** If an exact match is not found, then the cursor is always
drhbd03cae2001-06-02 02:40:57 +00003737** left pointing at a leaf page which would hold the entry if it
drh5e2f8b92001-05-28 00:41:15 +00003738** were present. The cursor might point to an entry that comes
3739** before or after the key.
3740**
drh64022502009-01-09 14:11:04 +00003741** An integer is written into *pRes which is the result of
3742** comparing the key with the entry to which the cursor is
3743** pointing. The meaning of the integer written into
3744** *pRes is as follows:
drhbd03cae2001-06-02 02:40:57 +00003745**
3746** *pRes<0 The cursor is left pointing at an entry that
drh64022502009-01-09 14:11:04 +00003747** is smaller than intKey/pIdxKey or if the table is empty
drh1a844c32002-12-04 22:29:28 +00003748** and the cursor is therefore left point to nothing.
drhbd03cae2001-06-02 02:40:57 +00003749**
3750** *pRes==0 The cursor is left pointing at an entry that
drh64022502009-01-09 14:11:04 +00003751** exactly matches intKey/pIdxKey.
drhbd03cae2001-06-02 02:40:57 +00003752**
3753** *pRes>0 The cursor is left pointing at an entry that
drh64022502009-01-09 14:11:04 +00003754** is larger than intKey/pIdxKey.
drhd677b3d2007-08-20 22:48:41 +00003755**
drha059ad02001-04-17 20:09:11 +00003756*/
drhe63d9992008-08-13 19:11:48 +00003757int sqlite3BtreeMovetoUnpacked(
3758 BtCursor *pCur, /* The cursor to be moved */
3759 UnpackedRecord *pIdxKey, /* Unpacked index key */
3760 i64 intKey, /* The table key */
3761 int biasRight, /* If true, bias the search to the high end */
3762 int *pRes /* Write search results here */
drhe4d90812007-03-29 05:51:49 +00003763){
drh72f82862001-05-24 21:06:34 +00003764 int rc;
drhd677b3d2007-08-20 22:48:41 +00003765
drh1fee73e2007-08-29 04:00:57 +00003766 assert( cursorHoldsMutex(pCur) );
drhe5fe6902007-12-07 18:55:28 +00003767 assert( sqlite3_mutex_held(pCur->pBtree->db->mutex) );
drha2c20e42008-03-29 16:01:04 +00003768
3769 /* If the cursor is already positioned at the point we are trying
3770 ** to move to, then just return without doing any work */
danielk197771d5d2c2008-09-29 11:49:47 +00003771 if( pCur->eState==CURSOR_VALID && pCur->validNKey
3772 && pCur->apPage[0]->intKey
3773 ){
drhe63d9992008-08-13 19:11:48 +00003774 if( pCur->info.nKey==intKey ){
drha2c20e42008-03-29 16:01:04 +00003775 *pRes = 0;
3776 return SQLITE_OK;
3777 }
drhe63d9992008-08-13 19:11:48 +00003778 if( pCur->atLast && pCur->info.nKey<intKey ){
drha2c20e42008-03-29 16:01:04 +00003779 *pRes = -1;
3780 return SQLITE_OK;
3781 }
3782 }
3783
drh5e2f8b92001-05-28 00:41:15 +00003784 rc = moveToRoot(pCur);
drhd677b3d2007-08-20 22:48:41 +00003785 if( rc ){
3786 return rc;
3787 }
danielk197771d5d2c2008-09-29 11:49:47 +00003788 assert( pCur->apPage[pCur->iPage] );
3789 assert( pCur->apPage[pCur->iPage]->isInit );
danielk1977da184232006-01-05 11:34:32 +00003790 if( pCur->eState==CURSOR_INVALID ){
drhf328bc82004-05-10 23:29:49 +00003791 *pRes = -1;
danielk197771d5d2c2008-09-29 11:49:47 +00003792 assert( pCur->apPage[pCur->iPage]->nCell==0 );
drhc39e0002004-05-07 23:50:57 +00003793 return SQLITE_OK;
3794 }
danielk197771d5d2c2008-09-29 11:49:47 +00003795 assert( pCur->apPage[0]->intKey || pIdxKey );
drh14684382006-11-30 13:05:29 +00003796 for(;;){
drh72f82862001-05-24 21:06:34 +00003797 int lwr, upr;
3798 Pgno chldPg;
danielk197771d5d2c2008-09-29 11:49:47 +00003799 MemPage *pPage = pCur->apPage[pCur->iPage];
drh1a844c32002-12-04 22:29:28 +00003800 int c = -1; /* pRes return if table is empty must be -1 */
drh72f82862001-05-24 21:06:34 +00003801 lwr = 0;
3802 upr = pPage->nCell-1;
drh64022502009-01-09 14:11:04 +00003803 if( (!pPage->intKey && pIdxKey==0) || upr<0 ){
drh1e968a02008-03-25 00:22:21 +00003804 rc = SQLITE_CORRUPT_BKPT;
3805 goto moveto_finish;
drh4eec4c12005-01-21 00:22:37 +00003806 }
drhe4d90812007-03-29 05:51:49 +00003807 if( biasRight ){
drhf49661a2008-12-10 16:45:50 +00003808 pCur->aiIdx[pCur->iPage] = (u16)upr;
drhe4d90812007-03-29 05:51:49 +00003809 }else{
drhf49661a2008-12-10 16:45:50 +00003810 pCur->aiIdx[pCur->iPage] = (u16)((upr+lwr)/2);
drhe4d90812007-03-29 05:51:49 +00003811 }
drh64022502009-01-09 14:11:04 +00003812 for(;;){
danielk197713adf8a2004-06-03 16:08:41 +00003813 void *pCellKey;
drh4a1c3802004-05-12 15:15:47 +00003814 i64 nCellKey;
danielk197771d5d2c2008-09-29 11:49:47 +00003815 int idx = pCur->aiIdx[pCur->iPage];
drh366fda62006-01-13 02:35:09 +00003816 pCur->info.nSize = 0;
drha2c20e42008-03-29 16:01:04 +00003817 pCur->validNKey = 1;
drh3aac2dd2004-04-26 14:10:20 +00003818 if( pPage->intKey ){
drh777e4c42006-01-13 04:31:58 +00003819 u8 *pCell;
danielk197771d5d2c2008-09-29 11:49:47 +00003820 pCell = findCell(pPage, idx) + pPage->childPtrSize;
drhd172f862006-01-12 15:01:15 +00003821 if( pPage->hasData ){
danielk1977bab45c62006-01-16 15:14:27 +00003822 u32 dummy;
shane3f8d5cf2008-04-24 19:15:09 +00003823 pCell += getVarint32(pCell, dummy);
drhd172f862006-01-12 15:01:15 +00003824 }
drha2c20e42008-03-29 16:01:04 +00003825 getVarint(pCell, (u64*)&nCellKey);
drhe63d9992008-08-13 19:11:48 +00003826 if( nCellKey==intKey ){
drh3aac2dd2004-04-26 14:10:20 +00003827 c = 0;
drhe63d9992008-08-13 19:11:48 +00003828 }else if( nCellKey<intKey ){
drh41eb9e92008-04-02 18:33:07 +00003829 c = -1;
3830 }else{
drhe63d9992008-08-13 19:11:48 +00003831 assert( nCellKey>intKey );
drh41eb9e92008-04-02 18:33:07 +00003832 c = +1;
drh3aac2dd2004-04-26 14:10:20 +00003833 }
drh3aac2dd2004-04-26 14:10:20 +00003834 }else{
drhe51c44f2004-05-30 20:46:09 +00003835 int available;
danielk197713adf8a2004-06-03 16:08:41 +00003836 pCellKey = (void *)fetchPayload(pCur, &available, 0);
drh366fda62006-01-13 02:35:09 +00003837 nCellKey = pCur->info.nKey;
drhe51c44f2004-05-30 20:46:09 +00003838 if( available>=nCellKey ){
drhf49661a2008-12-10 16:45:50 +00003839 c = sqlite3VdbeRecordCompare((int)nCellKey, pCellKey, pIdxKey);
drhe51c44f2004-05-30 20:46:09 +00003840 }else{
drhf49661a2008-12-10 16:45:50 +00003841 pCellKey = sqlite3Malloc( (int)nCellKey );
danielk19776507ecb2008-03-25 09:56:44 +00003842 if( pCellKey==0 ){
3843 rc = SQLITE_NOMEM;
3844 goto moveto_finish;
3845 }
drhf49661a2008-12-10 16:45:50 +00003846 rc = sqlite3BtreeKey(pCur, 0, (int)nCellKey, (void*)pCellKey);
drh1bd10f82008-12-10 21:19:56 +00003847 c = sqlite3VdbeRecordCompare((int)nCellKey, pCellKey, pIdxKey);
drhfacf0302008-06-17 15:12:00 +00003848 sqlite3_free(pCellKey);
drh1e968a02008-03-25 00:22:21 +00003849 if( rc ) goto moveto_finish;
drhe51c44f2004-05-30 20:46:09 +00003850 }
drh3aac2dd2004-04-26 14:10:20 +00003851 }
drh72f82862001-05-24 21:06:34 +00003852 if( c==0 ){
drha2c20e42008-03-29 16:01:04 +00003853 pCur->info.nKey = nCellKey;
drh44845222008-07-17 18:39:57 +00003854 if( pPage->intKey && !pPage->leaf ){
danielk197771d5d2c2008-09-29 11:49:47 +00003855 lwr = idx;
drhfc70e6f2004-05-12 21:11:27 +00003856 upr = lwr - 1;
drh8b18dd42004-05-12 19:18:15 +00003857 break;
3858 }else{
drh64022502009-01-09 14:11:04 +00003859 *pRes = 0;
drh1e968a02008-03-25 00:22:21 +00003860 rc = SQLITE_OK;
3861 goto moveto_finish;
drh8b18dd42004-05-12 19:18:15 +00003862 }
drh72f82862001-05-24 21:06:34 +00003863 }
3864 if( c<0 ){
danielk197771d5d2c2008-09-29 11:49:47 +00003865 lwr = idx+1;
drh72f82862001-05-24 21:06:34 +00003866 }else{
danielk197771d5d2c2008-09-29 11:49:47 +00003867 upr = idx-1;
drh72f82862001-05-24 21:06:34 +00003868 }
drhf1d68b32007-03-29 04:43:26 +00003869 if( lwr>upr ){
drha2c20e42008-03-29 16:01:04 +00003870 pCur->info.nKey = nCellKey;
drhf1d68b32007-03-29 04:43:26 +00003871 break;
3872 }
drhf49661a2008-12-10 16:45:50 +00003873 pCur->aiIdx[pCur->iPage] = (u16)((lwr+upr)/2);
drh72f82862001-05-24 21:06:34 +00003874 }
3875 assert( lwr==upr+1 );
danielk197771d5d2c2008-09-29 11:49:47 +00003876 assert( pPage->isInit );
drh3aac2dd2004-04-26 14:10:20 +00003877 if( pPage->leaf ){
drha34b6762004-05-07 13:30:42 +00003878 chldPg = 0;
drh3aac2dd2004-04-26 14:10:20 +00003879 }else if( lwr>=pPage->nCell ){
drh43605152004-05-29 21:46:49 +00003880 chldPg = get4byte(&pPage->aData[pPage->hdrOffset+8]);
drh72f82862001-05-24 21:06:34 +00003881 }else{
danielk19771cc5ed82007-05-16 17:28:43 +00003882 chldPg = get4byte(findCell(pPage, lwr));
drh72f82862001-05-24 21:06:34 +00003883 }
3884 if( chldPg==0 ){
danielk197771d5d2c2008-09-29 11:49:47 +00003885 assert( pCur->aiIdx[pCur->iPage]<pCur->apPage[pCur->iPage]->nCell );
drh72f82862001-05-24 21:06:34 +00003886 if( pRes ) *pRes = c;
drh1e968a02008-03-25 00:22:21 +00003887 rc = SQLITE_OK;
3888 goto moveto_finish;
drh72f82862001-05-24 21:06:34 +00003889 }
drhf49661a2008-12-10 16:45:50 +00003890 pCur->aiIdx[pCur->iPage] = (u16)lwr;
drh271efa52004-05-30 19:19:05 +00003891 pCur->info.nSize = 0;
drha2c20e42008-03-29 16:01:04 +00003892 pCur->validNKey = 0;
drh8178a752003-01-05 21:41:40 +00003893 rc = moveToChild(pCur, chldPg);
drh1e968a02008-03-25 00:22:21 +00003894 if( rc ) goto moveto_finish;
drh72f82862001-05-24 21:06:34 +00003895 }
drh1e968a02008-03-25 00:22:21 +00003896moveto_finish:
drhe63d9992008-08-13 19:11:48 +00003897 return rc;
3898}
3899
3900/*
3901** In this version of BtreeMoveto, pKey is a packed index record
3902** such as is generated by the OP_MakeRecord opcode. Unpack the
3903** record and then call BtreeMovetoUnpacked() to do the work.
3904*/
3905int sqlite3BtreeMoveto(
3906 BtCursor *pCur, /* Cursor open on the btree to be searched */
3907 const void *pKey, /* Packed key if the btree is an index */
3908 i64 nKey, /* Integer key for tables. Size of pKey for indices */
3909 int bias, /* Bias search to the high end */
3910 int *pRes /* Write search results here */
3911){
3912 int rc; /* Status code */
3913 UnpackedRecord *pIdxKey; /* Unpacked index key */
drh23f79d02008-08-20 22:06:47 +00003914 UnpackedRecord aSpace[16]; /* Temp space for pIdxKey - to avoid a malloc */
drhe63d9992008-08-13 19:11:48 +00003915
drhe14006d2008-03-25 17:23:32 +00003916 if( pKey ){
drhf49661a2008-12-10 16:45:50 +00003917 assert( nKey==(i64)(int)nKey );
3918 pIdxKey = sqlite3VdbeRecordUnpack(pCur->pKeyInfo, (int)nKey, pKey,
drh23f79d02008-08-20 22:06:47 +00003919 aSpace, sizeof(aSpace));
drhe63d9992008-08-13 19:11:48 +00003920 if( pIdxKey==0 ) return SQLITE_NOMEM;
3921 }else{
3922 pIdxKey = 0;
3923 }
3924 rc = sqlite3BtreeMovetoUnpacked(pCur, pIdxKey, nKey, bias, pRes);
3925 if( pKey ){
3926 sqlite3VdbeDeleteUnpackedRecord(pIdxKey);
drhe14006d2008-03-25 17:23:32 +00003927 }
drh1e968a02008-03-25 00:22:21 +00003928 return rc;
drh72f82862001-05-24 21:06:34 +00003929}
3930
drhd677b3d2007-08-20 22:48:41 +00003931
drh72f82862001-05-24 21:06:34 +00003932/*
drhc39e0002004-05-07 23:50:57 +00003933** Return TRUE if the cursor is not pointing at an entry of the table.
3934**
3935** TRUE will be returned after a call to sqlite3BtreeNext() moves
3936** past the last entry in the table or sqlite3BtreePrev() moves past
3937** the first entry. TRUE is also returned if the table is empty.
3938*/
3939int sqlite3BtreeEof(BtCursor *pCur){
danielk1977da184232006-01-05 11:34:32 +00003940 /* TODO: What if the cursor is in CURSOR_REQUIRESEEK but all table entries
3941 ** have been deleted? This API will need to change to return an error code
3942 ** as well as the boolean result value.
3943 */
3944 return (CURSOR_VALID!=pCur->eState);
drhc39e0002004-05-07 23:50:57 +00003945}
3946
3947/*
drhb21c8cd2007-08-21 19:33:56 +00003948** Return the database connection handle for a cursor.
3949*/
3950sqlite3 *sqlite3BtreeCursorDb(const BtCursor *pCur){
drhe5fe6902007-12-07 18:55:28 +00003951 assert( sqlite3_mutex_held(pCur->pBtree->db->mutex) );
3952 return pCur->pBtree->db;
drhb21c8cd2007-08-21 19:33:56 +00003953}
3954
3955/*
drhbd03cae2001-06-02 02:40:57 +00003956** Advance the cursor to the next entry in the database. If
drh8c1238a2003-01-02 14:43:55 +00003957** successful then set *pRes=0. If the cursor
drhbd03cae2001-06-02 02:40:57 +00003958** was already pointing to the last entry in the database before
drh8c1238a2003-01-02 14:43:55 +00003959** this routine was called, then set *pRes=1.
drh72f82862001-05-24 21:06:34 +00003960*/
drhd094db12008-04-03 21:46:57 +00003961int sqlite3BtreeNext(BtCursor *pCur, int *pRes){
drh72f82862001-05-24 21:06:34 +00003962 int rc;
danielk197771d5d2c2008-09-29 11:49:47 +00003963 int idx;
danielk197797a227c2006-01-20 16:32:04 +00003964 MemPage *pPage;
drh8b18dd42004-05-12 19:18:15 +00003965
drh1fee73e2007-08-29 04:00:57 +00003966 assert( cursorHoldsMutex(pCur) );
drha3460582008-07-11 21:02:53 +00003967 rc = restoreCursorPosition(pCur);
danielk1977da184232006-01-05 11:34:32 +00003968 if( rc!=SQLITE_OK ){
3969 return rc;
3970 }
drh8c4d3a62007-04-06 01:03:32 +00003971 assert( pRes!=0 );
drh8c4d3a62007-04-06 01:03:32 +00003972 if( CURSOR_INVALID==pCur->eState ){
3973 *pRes = 1;
3974 return SQLITE_OK;
3975 }
danielk1977da184232006-01-05 11:34:32 +00003976 if( pCur->skip>0 ){
3977 pCur->skip = 0;
3978 *pRes = 0;
3979 return SQLITE_OK;
3980 }
3981 pCur->skip = 0;
danielk1977da184232006-01-05 11:34:32 +00003982
danielk197771d5d2c2008-09-29 11:49:47 +00003983 pPage = pCur->apPage[pCur->iPage];
3984 idx = ++pCur->aiIdx[pCur->iPage];
3985 assert( pPage->isInit );
3986 assert( idx<=pPage->nCell );
danielk19776a43f9b2004-11-16 04:57:24 +00003987
drh271efa52004-05-30 19:19:05 +00003988 pCur->info.nSize = 0;
drha2c20e42008-03-29 16:01:04 +00003989 pCur->validNKey = 0;
danielk197771d5d2c2008-09-29 11:49:47 +00003990 if( idx>=pPage->nCell ){
drha34b6762004-05-07 13:30:42 +00003991 if( !pPage->leaf ){
drh43605152004-05-29 21:46:49 +00003992 rc = moveToChild(pCur, get4byte(&pPage->aData[pPage->hdrOffset+8]));
drh5e2f8b92001-05-28 00:41:15 +00003993 if( rc ) return rc;
3994 rc = moveToLeftmost(pCur);
drh8c1238a2003-01-02 14:43:55 +00003995 *pRes = 0;
3996 return rc;
drh72f82862001-05-24 21:06:34 +00003997 }
drh5e2f8b92001-05-28 00:41:15 +00003998 do{
danielk197771d5d2c2008-09-29 11:49:47 +00003999 if( pCur->iPage==0 ){
drh8c1238a2003-01-02 14:43:55 +00004000 *pRes = 1;
danielk1977da184232006-01-05 11:34:32 +00004001 pCur->eState = CURSOR_INVALID;
drh5e2f8b92001-05-28 00:41:15 +00004002 return SQLITE_OK;
4003 }
drh16a9b832007-05-05 18:39:25 +00004004 sqlite3BtreeMoveToParent(pCur);
danielk197771d5d2c2008-09-29 11:49:47 +00004005 pPage = pCur->apPage[pCur->iPage];
4006 }while( pCur->aiIdx[pCur->iPage]>=pPage->nCell );
drh8c1238a2003-01-02 14:43:55 +00004007 *pRes = 0;
drh44845222008-07-17 18:39:57 +00004008 if( pPage->intKey ){
drh8b18dd42004-05-12 19:18:15 +00004009 rc = sqlite3BtreeNext(pCur, pRes);
4010 }else{
4011 rc = SQLITE_OK;
4012 }
4013 return rc;
drh8178a752003-01-05 21:41:40 +00004014 }
4015 *pRes = 0;
drh3aac2dd2004-04-26 14:10:20 +00004016 if( pPage->leaf ){
drh8178a752003-01-05 21:41:40 +00004017 return SQLITE_OK;
drh72f82862001-05-24 21:06:34 +00004018 }
drh5e2f8b92001-05-28 00:41:15 +00004019 rc = moveToLeftmost(pCur);
drh8c1238a2003-01-02 14:43:55 +00004020 return rc;
drh72f82862001-05-24 21:06:34 +00004021}
drhd677b3d2007-08-20 22:48:41 +00004022
drh72f82862001-05-24 21:06:34 +00004023
drh3b7511c2001-05-26 13:15:44 +00004024/*
drh2dcc9aa2002-12-04 13:40:25 +00004025** Step the cursor to the back to the previous entry in the database. If
drh8178a752003-01-05 21:41:40 +00004026** successful then set *pRes=0. If the cursor
drh2dcc9aa2002-12-04 13:40:25 +00004027** was already pointing to the first entry in the database before
drh8178a752003-01-05 21:41:40 +00004028** this routine was called, then set *pRes=1.
drh2dcc9aa2002-12-04 13:40:25 +00004029*/
drhd094db12008-04-03 21:46:57 +00004030int sqlite3BtreePrevious(BtCursor *pCur, int *pRes){
drh2dcc9aa2002-12-04 13:40:25 +00004031 int rc;
drh8178a752003-01-05 21:41:40 +00004032 MemPage *pPage;
danielk1977da184232006-01-05 11:34:32 +00004033
drh1fee73e2007-08-29 04:00:57 +00004034 assert( cursorHoldsMutex(pCur) );
drha3460582008-07-11 21:02:53 +00004035 rc = restoreCursorPosition(pCur);
danielk1977da184232006-01-05 11:34:32 +00004036 if( rc!=SQLITE_OK ){
4037 return rc;
4038 }
drha2c20e42008-03-29 16:01:04 +00004039 pCur->atLast = 0;
drh8c4d3a62007-04-06 01:03:32 +00004040 if( CURSOR_INVALID==pCur->eState ){
4041 *pRes = 1;
4042 return SQLITE_OK;
4043 }
danielk1977da184232006-01-05 11:34:32 +00004044 if( pCur->skip<0 ){
4045 pCur->skip = 0;
4046 *pRes = 0;
4047 return SQLITE_OK;
4048 }
4049 pCur->skip = 0;
danielk1977da184232006-01-05 11:34:32 +00004050
danielk197771d5d2c2008-09-29 11:49:47 +00004051 pPage = pCur->apPage[pCur->iPage];
4052 assert( pPage->isInit );
drha34b6762004-05-07 13:30:42 +00004053 if( !pPage->leaf ){
danielk197771d5d2c2008-09-29 11:49:47 +00004054 int idx = pCur->aiIdx[pCur->iPage];
4055 rc = moveToChild(pCur, get4byte(findCell(pPage, idx)));
drhd677b3d2007-08-20 22:48:41 +00004056 if( rc ){
4057 return rc;
4058 }
drh2dcc9aa2002-12-04 13:40:25 +00004059 rc = moveToRightmost(pCur);
4060 }else{
danielk197771d5d2c2008-09-29 11:49:47 +00004061 while( pCur->aiIdx[pCur->iPage]==0 ){
4062 if( pCur->iPage==0 ){
danielk1977da184232006-01-05 11:34:32 +00004063 pCur->eState = CURSOR_INVALID;
drhc39e0002004-05-07 23:50:57 +00004064 *pRes = 1;
drh2dcc9aa2002-12-04 13:40:25 +00004065 return SQLITE_OK;
4066 }
drh16a9b832007-05-05 18:39:25 +00004067 sqlite3BtreeMoveToParent(pCur);
drh2dcc9aa2002-12-04 13:40:25 +00004068 }
drh271efa52004-05-30 19:19:05 +00004069 pCur->info.nSize = 0;
drha2c20e42008-03-29 16:01:04 +00004070 pCur->validNKey = 0;
danielk197771d5d2c2008-09-29 11:49:47 +00004071
4072 pCur->aiIdx[pCur->iPage]--;
4073 pPage = pCur->apPage[pCur->iPage];
drh44845222008-07-17 18:39:57 +00004074 if( pPage->intKey && !pPage->leaf ){
drh8b18dd42004-05-12 19:18:15 +00004075 rc = sqlite3BtreePrevious(pCur, pRes);
4076 }else{
4077 rc = SQLITE_OK;
4078 }
drh2dcc9aa2002-12-04 13:40:25 +00004079 }
drh8178a752003-01-05 21:41:40 +00004080 *pRes = 0;
drh2dcc9aa2002-12-04 13:40:25 +00004081 return rc;
4082}
4083
4084/*
drh3b7511c2001-05-26 13:15:44 +00004085** Allocate a new page from the database file.
4086**
danielk19773b8a05f2007-03-19 17:44:26 +00004087** The new page is marked as dirty. (In other words, sqlite3PagerWrite()
drh3b7511c2001-05-26 13:15:44 +00004088** has already been called on the new page.) The new page has also
4089** been referenced and the calling routine is responsible for calling
danielk19773b8a05f2007-03-19 17:44:26 +00004090** sqlite3PagerUnref() on the new page when it is done.
drh3b7511c2001-05-26 13:15:44 +00004091**
4092** SQLITE_OK is returned on success. Any other return value indicates
4093** an error. *ppPage and *pPgno are undefined in the event of an error.
danielk19773b8a05f2007-03-19 17:44:26 +00004094** Do not invoke sqlite3PagerUnref() on *ppPage if an error is returned.
drhbea00b92002-07-08 10:59:50 +00004095**
drh199e3cf2002-07-18 11:01:47 +00004096** If the "nearby" parameter is not 0, then a (feeble) effort is made to
4097** locate a page close to the page number "nearby". This can be used in an
drhbea00b92002-07-08 10:59:50 +00004098** attempt to keep related pages close to each other in the database file,
4099** which in turn can make database access faster.
danielk1977cb1a7eb2004-11-05 12:27:02 +00004100**
4101** If the "exact" parameter is not 0, and the page-number nearby exists
4102** anywhere on the free-list, then it is guarenteed to be returned. This
4103** is only used by auto-vacuum databases when allocating a new table.
drh3b7511c2001-05-26 13:15:44 +00004104*/
drh4f0c5872007-03-26 22:05:01 +00004105static int allocateBtreePage(
danielk1977aef0bf62005-12-30 16:28:01 +00004106 BtShared *pBt,
danielk1977cb1a7eb2004-11-05 12:27:02 +00004107 MemPage **ppPage,
4108 Pgno *pPgno,
4109 Pgno nearby,
4110 u8 exact
4111){
drh3aac2dd2004-04-26 14:10:20 +00004112 MemPage *pPage1;
drh8c42ca92001-06-22 19:15:00 +00004113 int rc;
drh3aac2dd2004-04-26 14:10:20 +00004114 int n; /* Number of pages on the freelist */
4115 int k; /* Number of leaves on the trunk of the freelist */
drhd3627af2006-12-18 18:34:51 +00004116 MemPage *pTrunk = 0;
4117 MemPage *pPrevTrunk = 0;
drh30e58752002-03-02 20:41:57 +00004118
drh1fee73e2007-08-29 04:00:57 +00004119 assert( sqlite3_mutex_held(pBt->mutex) );
drh3aac2dd2004-04-26 14:10:20 +00004120 pPage1 = pBt->pPage1;
4121 n = get4byte(&pPage1->aData[36]);
4122 if( n>0 ){
drh91025292004-05-03 19:49:32 +00004123 /* There are pages on the freelist. Reuse one of those pages. */
danielk1977cb1a7eb2004-11-05 12:27:02 +00004124 Pgno iTrunk;
danielk1977cb1a7eb2004-11-05 12:27:02 +00004125 u8 searchList = 0; /* If the free-list must be searched for 'nearby' */
4126
4127 /* If the 'exact' parameter was true and a query of the pointer-map
4128 ** shows that the page 'nearby' is somewhere on the free-list, then
4129 ** the entire-list will be searched for that page.
4130 */
4131#ifndef SQLITE_OMIT_AUTOVACUUM
danielk197789d40042008-11-17 14:20:56 +00004132 if( exact && nearby<=pagerPagecount(pBt) ){
danielk1977cb1a7eb2004-11-05 12:27:02 +00004133 u8 eType;
4134 assert( nearby>0 );
4135 assert( pBt->autoVacuum );
4136 rc = ptrmapGet(pBt, nearby, &eType, 0);
4137 if( rc ) return rc;
4138 if( eType==PTRMAP_FREEPAGE ){
4139 searchList = 1;
4140 }
4141 *pPgno = nearby;
4142 }
4143#endif
4144
4145 /* Decrement the free-list count by 1. Set iTrunk to the index of the
4146 ** first free-list trunk page. iPrevTrunk is initially 1.
4147 */
danielk19773b8a05f2007-03-19 17:44:26 +00004148 rc = sqlite3PagerWrite(pPage1->pDbPage);
drh3b7511c2001-05-26 13:15:44 +00004149 if( rc ) return rc;
drh3aac2dd2004-04-26 14:10:20 +00004150 put4byte(&pPage1->aData[36], n-1);
danielk1977cb1a7eb2004-11-05 12:27:02 +00004151
4152 /* The code within this loop is run only once if the 'searchList' variable
4153 ** is not true. Otherwise, it runs once for each trunk-page on the
4154 ** free-list until the page 'nearby' is located.
4155 */
4156 do {
4157 pPrevTrunk = pTrunk;
4158 if( pPrevTrunk ){
4159 iTrunk = get4byte(&pPrevTrunk->aData[0]);
drhbea00b92002-07-08 10:59:50 +00004160 }else{
danielk1977cb1a7eb2004-11-05 12:27:02 +00004161 iTrunk = get4byte(&pPage1->aData[32]);
drhbea00b92002-07-08 10:59:50 +00004162 }
drh16a9b832007-05-05 18:39:25 +00004163 rc = sqlite3BtreeGetPage(pBt, iTrunk, &pTrunk, 0);
danielk1977cb1a7eb2004-11-05 12:27:02 +00004164 if( rc ){
drhd3627af2006-12-18 18:34:51 +00004165 pTrunk = 0;
4166 goto end_allocate_page;
danielk1977cb1a7eb2004-11-05 12:27:02 +00004167 }
4168
4169 k = get4byte(&pTrunk->aData[4]);
4170 if( k==0 && !searchList ){
4171 /* The trunk has no leaves and the list is not being searched.
4172 ** So extract the trunk page itself and use it as the newly
4173 ** allocated page */
4174 assert( pPrevTrunk==0 );
danielk19773b8a05f2007-03-19 17:44:26 +00004175 rc = sqlite3PagerWrite(pTrunk->pDbPage);
drhd3627af2006-12-18 18:34:51 +00004176 if( rc ){
4177 goto end_allocate_page;
4178 }
danielk1977cb1a7eb2004-11-05 12:27:02 +00004179 *pPgno = iTrunk;
4180 memcpy(&pPage1->aData[32], &pTrunk->aData[0], 4);
4181 *ppPage = pTrunk;
4182 pTrunk = 0;
4183 TRACE(("ALLOCATE: %d trunk - %d free pages left\n", *pPgno, n-1));
drh45b1fac2008-07-04 17:52:42 +00004184 }else if( k>pBt->usableSize/4 - 2 ){
danielk1977cb1a7eb2004-11-05 12:27:02 +00004185 /* Value of k is out of range. Database corruption */
drhd3627af2006-12-18 18:34:51 +00004186 rc = SQLITE_CORRUPT_BKPT;
4187 goto end_allocate_page;
danielk1977cb1a7eb2004-11-05 12:27:02 +00004188#ifndef SQLITE_OMIT_AUTOVACUUM
4189 }else if( searchList && nearby==iTrunk ){
4190 /* The list is being searched and this trunk page is the page
4191 ** to allocate, regardless of whether it has leaves.
4192 */
4193 assert( *pPgno==iTrunk );
4194 *ppPage = pTrunk;
4195 searchList = 0;
danielk19773b8a05f2007-03-19 17:44:26 +00004196 rc = sqlite3PagerWrite(pTrunk->pDbPage);
drhd3627af2006-12-18 18:34:51 +00004197 if( rc ){
4198 goto end_allocate_page;
4199 }
danielk1977cb1a7eb2004-11-05 12:27:02 +00004200 if( k==0 ){
4201 if( !pPrevTrunk ){
4202 memcpy(&pPage1->aData[32], &pTrunk->aData[0], 4);
4203 }else{
4204 memcpy(&pPrevTrunk->aData[0], &pTrunk->aData[0], 4);
4205 }
4206 }else{
4207 /* The trunk page is required by the caller but it contains
4208 ** pointers to free-list leaves. The first leaf becomes a trunk
4209 ** page in this case.
4210 */
4211 MemPage *pNewTrunk;
4212 Pgno iNewTrunk = get4byte(&pTrunk->aData[8]);
drh16a9b832007-05-05 18:39:25 +00004213 rc = sqlite3BtreeGetPage(pBt, iNewTrunk, &pNewTrunk, 0);
danielk1977cb1a7eb2004-11-05 12:27:02 +00004214 if( rc!=SQLITE_OK ){
drhd3627af2006-12-18 18:34:51 +00004215 goto end_allocate_page;
danielk1977cb1a7eb2004-11-05 12:27:02 +00004216 }
danielk19773b8a05f2007-03-19 17:44:26 +00004217 rc = sqlite3PagerWrite(pNewTrunk->pDbPage);
danielk1977cb1a7eb2004-11-05 12:27:02 +00004218 if( rc!=SQLITE_OK ){
4219 releasePage(pNewTrunk);
drhd3627af2006-12-18 18:34:51 +00004220 goto end_allocate_page;
danielk1977cb1a7eb2004-11-05 12:27:02 +00004221 }
4222 memcpy(&pNewTrunk->aData[0], &pTrunk->aData[0], 4);
4223 put4byte(&pNewTrunk->aData[4], k-1);
4224 memcpy(&pNewTrunk->aData[8], &pTrunk->aData[12], (k-1)*4);
drhd3627af2006-12-18 18:34:51 +00004225 releasePage(pNewTrunk);
danielk1977cb1a7eb2004-11-05 12:27:02 +00004226 if( !pPrevTrunk ){
drhc5053fb2008-11-27 02:22:10 +00004227 assert( sqlite3PagerIswriteable(pPage1->pDbPage) );
danielk1977cb1a7eb2004-11-05 12:27:02 +00004228 put4byte(&pPage1->aData[32], iNewTrunk);
4229 }else{
danielk19773b8a05f2007-03-19 17:44:26 +00004230 rc = sqlite3PagerWrite(pPrevTrunk->pDbPage);
drhd3627af2006-12-18 18:34:51 +00004231 if( rc ){
4232 goto end_allocate_page;
4233 }
danielk1977cb1a7eb2004-11-05 12:27:02 +00004234 put4byte(&pPrevTrunk->aData[0], iNewTrunk);
4235 }
danielk1977cb1a7eb2004-11-05 12:27:02 +00004236 }
4237 pTrunk = 0;
4238 TRACE(("ALLOCATE: %d trunk - %d free pages left\n", *pPgno, n-1));
4239#endif
4240 }else{
4241 /* Extract a leaf from the trunk */
4242 int closest;
4243 Pgno iPage;
4244 unsigned char *aData = pTrunk->aData;
danielk19773b8a05f2007-03-19 17:44:26 +00004245 rc = sqlite3PagerWrite(pTrunk->pDbPage);
drhd3627af2006-12-18 18:34:51 +00004246 if( rc ){
4247 goto end_allocate_page;
4248 }
danielk1977cb1a7eb2004-11-05 12:27:02 +00004249 if( nearby>0 ){
4250 int i, dist;
4251 closest = 0;
4252 dist = get4byte(&aData[8]) - nearby;
4253 if( dist<0 ) dist = -dist;
4254 for(i=1; i<k; i++){
4255 int d2 = get4byte(&aData[8+i*4]) - nearby;
4256 if( d2<0 ) d2 = -d2;
4257 if( d2<dist ){
4258 closest = i;
4259 dist = d2;
4260 }
4261 }
4262 }else{
4263 closest = 0;
4264 }
4265
4266 iPage = get4byte(&aData[8+closest*4]);
4267 if( !searchList || iPage==nearby ){
danielk197789d40042008-11-17 14:20:56 +00004268 Pgno nPage;
shane1f9e6aa2008-06-09 19:27:11 +00004269 *pPgno = iPage;
danielk197789d40042008-11-17 14:20:56 +00004270 nPage = pagerPagecount(pBt);
danielk1977ad0132d2008-06-07 08:58:22 +00004271 if( *pPgno>nPage ){
danielk1977cb1a7eb2004-11-05 12:27:02 +00004272 /* Free page off the end of the file */
danielk197743e377a2008-05-05 12:09:32 +00004273 rc = SQLITE_CORRUPT_BKPT;
4274 goto end_allocate_page;
danielk1977cb1a7eb2004-11-05 12:27:02 +00004275 }
4276 TRACE(("ALLOCATE: %d was leaf %d of %d on trunk %d"
4277 ": %d more free pages\n",
4278 *pPgno, closest+1, k, pTrunk->pgno, n-1));
4279 if( closest<k-1 ){
4280 memcpy(&aData[8+closest*4], &aData[4+k*4], 4);
4281 }
4282 put4byte(&aData[4], k-1);
drhc5053fb2008-11-27 02:22:10 +00004283 assert( sqlite3PagerIswriteable(pTrunk->pDbPage) );
danielk197745d68822009-01-16 16:23:38 +00004284 rc = sqlite3BtreeGetPage(pBt, *pPgno, ppPage, 1);
danielk1977cb1a7eb2004-11-05 12:27:02 +00004285 if( rc==SQLITE_OK ){
danielk197745d68822009-01-16 16:23:38 +00004286 sqlite3PagerDontRollback((*ppPage)->pDbPage);
danielk19773b8a05f2007-03-19 17:44:26 +00004287 rc = sqlite3PagerWrite((*ppPage)->pDbPage);
danielk1977aac0a382005-01-16 11:07:06 +00004288 if( rc!=SQLITE_OK ){
4289 releasePage(*ppPage);
4290 }
danielk1977cb1a7eb2004-11-05 12:27:02 +00004291 }
4292 searchList = 0;
4293 }
drhee696e22004-08-30 16:52:17 +00004294 }
danielk1977cb1a7eb2004-11-05 12:27:02 +00004295 releasePage(pPrevTrunk);
drhd3627af2006-12-18 18:34:51 +00004296 pPrevTrunk = 0;
danielk1977cb1a7eb2004-11-05 12:27:02 +00004297 }while( searchList );
drh3b7511c2001-05-26 13:15:44 +00004298 }else{
drh3aac2dd2004-04-26 14:10:20 +00004299 /* There are no pages on the freelist, so create a new page at the
4300 ** end of the file */
danielk197789d40042008-11-17 14:20:56 +00004301 int nPage = pagerPagecount(pBt);
danielk1977ad0132d2008-06-07 08:58:22 +00004302 *pPgno = nPage + 1;
danielk1977afcdd022004-10-31 16:25:42 +00004303
4304#ifndef SQLITE_OMIT_AUTOVACUUM
danielk1977266664d2006-02-10 08:24:21 +00004305 if( pBt->autoVacuum && PTRMAP_ISPAGE(pBt, *pPgno) ){
danielk1977afcdd022004-10-31 16:25:42 +00004306 /* If *pPgno refers to a pointer-map page, allocate two new pages
4307 ** at the end of the file instead of one. The first allocated page
4308 ** becomes a new pointer-map page, the second is used by the caller.
4309 */
4310 TRACE(("ALLOCATE: %d from end of file (pointer-map page)\n", *pPgno));
danielk1977599fcba2004-11-08 07:13:13 +00004311 assert( *pPgno!=PENDING_BYTE_PAGE(pBt) );
danielk1977afcdd022004-10-31 16:25:42 +00004312 (*pPgno)++;
drh72190432008-01-31 14:54:43 +00004313 if( *pPgno==PENDING_BYTE_PAGE(pBt) ){ (*pPgno)++; }
danielk1977afcdd022004-10-31 16:25:42 +00004314 }
4315#endif
4316
danielk1977599fcba2004-11-08 07:13:13 +00004317 assert( *pPgno!=PENDING_BYTE_PAGE(pBt) );
drh16a9b832007-05-05 18:39:25 +00004318 rc = sqlite3BtreeGetPage(pBt, *pPgno, ppPage, 0);
drh3b7511c2001-05-26 13:15:44 +00004319 if( rc ) return rc;
danielk19773b8a05f2007-03-19 17:44:26 +00004320 rc = sqlite3PagerWrite((*ppPage)->pDbPage);
danielk1977aac0a382005-01-16 11:07:06 +00004321 if( rc!=SQLITE_OK ){
4322 releasePage(*ppPage);
4323 }
drh3a4c1412004-05-09 20:40:11 +00004324 TRACE(("ALLOCATE: %d from end of file\n", *pPgno));
drh3b7511c2001-05-26 13:15:44 +00004325 }
danielk1977599fcba2004-11-08 07:13:13 +00004326
4327 assert( *pPgno!=PENDING_BYTE_PAGE(pBt) );
drhd3627af2006-12-18 18:34:51 +00004328
4329end_allocate_page:
4330 releasePage(pTrunk);
4331 releasePage(pPrevTrunk);
danielk1977b247c212008-11-21 09:09:01 +00004332 if( rc==SQLITE_OK ){
4333 if( sqlite3PagerPageRefcount((*ppPage)->pDbPage)>1 ){
4334 releasePage(*ppPage);
4335 return SQLITE_CORRUPT_BKPT;
4336 }
4337 (*ppPage)->isInit = 0;
danielk1977eaa06f62008-09-18 17:34:44 +00004338 }
drh3b7511c2001-05-26 13:15:44 +00004339 return rc;
4340}
4341
4342/*
danielk197745d68822009-01-16 16:23:38 +00004343** Add a page of the database file to the freelist.
drh5e2f8b92001-05-28 00:41:15 +00004344**
danielk197745d68822009-01-16 16:23:38 +00004345** sqlite3PagerUnref() is NOT called for pPage.
drh3b7511c2001-05-26 13:15:44 +00004346*/
danielk197745d68822009-01-16 16:23:38 +00004347static int freePage(MemPage *pPage){
4348 BtShared *pBt = pPage->pBt;
4349 MemPage *pPage1 = pBt->pPage1;
4350 int rc, n, k;
drh8b2f49b2001-06-08 00:21:52 +00004351
danielk197745d68822009-01-16 16:23:38 +00004352 /* Prepare the page for freeing */
4353 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
4354 assert( pPage->pgno>1 );
4355 pPage->isInit = 0;
drh3aac2dd2004-04-26 14:10:20 +00004356
drha34b6762004-05-07 13:30:42 +00004357 /* Increment the free page count on pPage1 */
danielk19773b8a05f2007-03-19 17:44:26 +00004358 rc = sqlite3PagerWrite(pPage1->pDbPage);
danielk197745d68822009-01-16 16:23:38 +00004359 if( rc ) return rc;
4360 n = get4byte(&pPage1->aData[36]);
4361 put4byte(&pPage1->aData[36], n+1);
drh3aac2dd2004-04-26 14:10:20 +00004362
drhfcce93f2006-02-22 03:08:32 +00004363#ifdef SQLITE_SECURE_DELETE
4364 /* If the SQLITE_SECURE_DELETE compile-time option is enabled, then
4365 ** always fully overwrite deleted information with zeros.
4366 */
danielk197745d68822009-01-16 16:23:38 +00004367 rc = sqlite3PagerWrite(pPage->pDbPage);
4368 if( rc ) return rc;
drhfcce93f2006-02-22 03:08:32 +00004369 memset(pPage->aData, 0, pPage->pBt->pageSize);
4370#endif
4371
danielk1977687566d2004-11-02 12:56:41 +00004372 /* If the database supports auto-vacuum, write an entry in the pointer-map
danielk1977cb1a7eb2004-11-05 12:27:02 +00004373 ** to indicate that the page is free.
danielk1977687566d2004-11-02 12:56:41 +00004374 */
danielk197785d90ca2008-07-19 14:25:15 +00004375 if( ISAUTOVACUUM ){
danielk197745d68822009-01-16 16:23:38 +00004376 rc = ptrmapPut(pBt, pPage->pgno, PTRMAP_FREEPAGE, 0);
4377 if( rc ) return rc;
danielk1977687566d2004-11-02 12:56:41 +00004378 }
danielk1977687566d2004-11-02 12:56:41 +00004379
danielk197745d68822009-01-16 16:23:38 +00004380 if( n==0 ){
4381 /* This is the first free page */
4382 rc = sqlite3PagerWrite(pPage->pDbPage);
4383 if( rc ) return rc;
4384 memset(pPage->aData, 0, 8);
4385 put4byte(&pPage1->aData[32], pPage->pgno);
4386 TRACE(("FREE-PAGE: %d first\n", pPage->pgno));
4387 }else{
4388 /* Other free pages already exist. Retrive the first trunk page
4389 ** of the freelist and find out how many leaves it has. */
4390 MemPage *pTrunk;
4391 rc = sqlite3BtreeGetPage(pBt, get4byte(&pPage1->aData[32]), &pTrunk, 0);
4392 if( rc ) return rc;
4393 k = get4byte(&pTrunk->aData[4]);
4394 if( k>=pBt->usableSize/4 - 8 ){
4395 /* The trunk is full. Turn the page being freed into a new
4396 ** trunk page with no leaves.
drh45b1fac2008-07-04 17:52:42 +00004397 **
4398 ** Note that the trunk page is not really full until it contains
4399 ** usableSize/4 - 2 entries, not usableSize/4 - 8 entries as we have
4400 ** coded. But due to a coding error in versions of SQLite prior to
4401 ** 3.6.0, databases with freelist trunk pages holding more than
4402 ** usableSize/4 - 8 entries will be reported as corrupt. In order
4403 ** to maintain backwards compatibility with older versions of SQLite,
4404 ** we will contain to restrict the number of entries to usableSize/4 - 8
4405 ** for now. At some point in the future (once everyone has upgraded
4406 ** to 3.6.0 or later) we should consider fixing the conditional above
4407 ** to read "usableSize/4-2" instead of "usableSize/4-8".
4408 */
danielk197745d68822009-01-16 16:23:38 +00004409 rc = sqlite3PagerWrite(pPage->pDbPage);
4410 if( rc==SQLITE_OK ){
4411 put4byte(pPage->aData, pTrunk->pgno);
4412 put4byte(&pPage->aData[4], 0);
4413 put4byte(&pPage1->aData[32], pPage->pgno);
4414 TRACE(("FREE-PAGE: %d new trunk page replacing %d\n",
4415 pPage->pgno, pTrunk->pgno));
4416 }
4417 }else if( k<0 ){
4418 rc = SQLITE_CORRUPT;
4419 }else{
4420 /* Add the newly freed page as a leaf on the current trunk */
danielk19773b8a05f2007-03-19 17:44:26 +00004421 rc = sqlite3PagerWrite(pTrunk->pDbPage);
drhf5345442007-04-09 12:45:02 +00004422 if( rc==SQLITE_OK ){
danielk197745d68822009-01-16 16:23:38 +00004423 put4byte(&pTrunk->aData[4], k+1);
4424 put4byte(&pTrunk->aData[8+k*4], pPage->pgno);
drhfcce93f2006-02-22 03:08:32 +00004425#ifndef SQLITE_SECURE_DELETE
danielk197745d68822009-01-16 16:23:38 +00004426 rc = sqlite3PagerDontWrite(pPage->pDbPage);
drhfcce93f2006-02-22 03:08:32 +00004427#endif
drhf5345442007-04-09 12:45:02 +00004428 }
drh3a4c1412004-05-09 20:40:11 +00004429 TRACE(("FREE-PAGE: %d leaf on trunk page %d\n",pPage->pgno,pTrunk->pgno));
drh3aac2dd2004-04-26 14:10:20 +00004430 }
danielk197745d68822009-01-16 16:23:38 +00004431 releasePage(pTrunk);
drh3b7511c2001-05-26 13:15:44 +00004432 }
drh3b7511c2001-05-26 13:15:44 +00004433 return rc;
4434}
4435
4436/*
drh3aac2dd2004-04-26 14:10:20 +00004437** Free any overflow pages associated with the given Cell.
drh3b7511c2001-05-26 13:15:44 +00004438*/
drh3aac2dd2004-04-26 14:10:20 +00004439static int clearCell(MemPage *pPage, unsigned char *pCell){
danielk1977aef0bf62005-12-30 16:28:01 +00004440 BtShared *pBt = pPage->pBt;
drh6f11bef2004-05-13 01:12:56 +00004441 CellInfo info;
drh3aac2dd2004-04-26 14:10:20 +00004442 Pgno ovflPgno;
drh6f11bef2004-05-13 01:12:56 +00004443 int rc;
drh94440812007-03-06 11:42:19 +00004444 int nOvfl;
4445 int ovflPageSize;
drh3b7511c2001-05-26 13:15:44 +00004446
drh1fee73e2007-08-29 04:00:57 +00004447 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
drh16a9b832007-05-05 18:39:25 +00004448 sqlite3BtreeParseCellPtr(pPage, pCell, &info);
drh6f11bef2004-05-13 01:12:56 +00004449 if( info.iOverflow==0 ){
drha34b6762004-05-07 13:30:42 +00004450 return SQLITE_OK; /* No overflow pages. Return without doing anything */
drh3aac2dd2004-04-26 14:10:20 +00004451 }
drh6f11bef2004-05-13 01:12:56 +00004452 ovflPgno = get4byte(&pCell[info.iOverflow]);
drh94440812007-03-06 11:42:19 +00004453 ovflPageSize = pBt->usableSize - 4;
drh72365832007-03-06 15:53:44 +00004454 nOvfl = (info.nPayload - info.nLocal + ovflPageSize - 1)/ovflPageSize;
4455 assert( ovflPgno==0 || nOvfl>0 );
4456 while( nOvfl-- ){
danielk197745d68822009-01-16 16:23:38 +00004457 MemPage *pOvfl;
danielk197789d40042008-11-17 14:20:56 +00004458 if( ovflPgno==0 || ovflPgno>pagerPagecount(pBt) ){
drh49285702005-09-17 15:20:26 +00004459 return SQLITE_CORRUPT_BKPT;
danielk1977a1cb1832005-02-12 08:59:55 +00004460 }
danielk197745d68822009-01-16 16:23:38 +00004461
4462 rc = getOverflowPage(pBt, ovflPgno, &pOvfl, (nOvfl==0)?0:&ovflPgno);
drh3b7511c2001-05-26 13:15:44 +00004463 if( rc ) return rc;
danielk197745d68822009-01-16 16:23:38 +00004464 rc = freePage(pOvfl);
4465 sqlite3PagerUnref(pOvfl->pDbPage);
4466 if( rc ) return rc;
drh3b7511c2001-05-26 13:15:44 +00004467 }
drh5e2f8b92001-05-28 00:41:15 +00004468 return SQLITE_OK;
drh3b7511c2001-05-26 13:15:44 +00004469}
4470
4471/*
drh91025292004-05-03 19:49:32 +00004472** Create the byte sequence used to represent a cell on page pPage
4473** and write that byte sequence into pCell[]. Overflow pages are
4474** allocated and filled in as necessary. The calling procedure
4475** is responsible for making sure sufficient space has been allocated
4476** for pCell[].
4477**
4478** Note that pCell does not necessary need to point to the pPage->aData
4479** area. pCell might point to some temporary storage. The cell will
4480** be constructed in this temporary area then copied into pPage->aData
4481** later.
drh3b7511c2001-05-26 13:15:44 +00004482*/
4483static int fillInCell(
drh3aac2dd2004-04-26 14:10:20 +00004484 MemPage *pPage, /* The page that contains the cell */
drh4b70f112004-05-02 21:12:19 +00004485 unsigned char *pCell, /* Complete text of the cell */
drh4a1c3802004-05-12 15:15:47 +00004486 const void *pKey, i64 nKey, /* The key */
drh4b70f112004-05-02 21:12:19 +00004487 const void *pData,int nData, /* The data */
drhb026e052007-05-02 01:34:31 +00004488 int nZero, /* Extra zero bytes to append to pData */
drh4b70f112004-05-02 21:12:19 +00004489 int *pnSize /* Write cell size here */
drh3b7511c2001-05-26 13:15:44 +00004490){
drh3b7511c2001-05-26 13:15:44 +00004491 int nPayload;
drh8c6fa9b2004-05-26 00:01:53 +00004492 const u8 *pSrc;
drha34b6762004-05-07 13:30:42 +00004493 int nSrc, n, rc;
drh3aac2dd2004-04-26 14:10:20 +00004494 int spaceLeft;
4495 MemPage *pOvfl = 0;
drh9b171272004-05-08 02:03:22 +00004496 MemPage *pToRelease = 0;
drh3aac2dd2004-04-26 14:10:20 +00004497 unsigned char *pPrior;
4498 unsigned char *pPayload;
danielk1977aef0bf62005-12-30 16:28:01 +00004499 BtShared *pBt = pPage->pBt;
drh3aac2dd2004-04-26 14:10:20 +00004500 Pgno pgnoOvfl = 0;
drh4b70f112004-05-02 21:12:19 +00004501 int nHeader;
drh6f11bef2004-05-13 01:12:56 +00004502 CellInfo info;
drh3b7511c2001-05-26 13:15:44 +00004503
drh1fee73e2007-08-29 04:00:57 +00004504 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
drhd677b3d2007-08-20 22:48:41 +00004505
drhc5053fb2008-11-27 02:22:10 +00004506 /* pPage is not necessarily writeable since pCell might be auxiliary
4507 ** buffer space that is separate from the pPage buffer area */
4508 assert( pCell<pPage->aData || pCell>=&pPage->aData[pBt->pageSize]
4509 || sqlite3PagerIswriteable(pPage->pDbPage) );
4510
drh91025292004-05-03 19:49:32 +00004511 /* Fill in the header. */
drh43605152004-05-29 21:46:49 +00004512 nHeader = 0;
drh91025292004-05-03 19:49:32 +00004513 if( !pPage->leaf ){
4514 nHeader += 4;
4515 }
drh8b18dd42004-05-12 19:18:15 +00004516 if( pPage->hasData ){
drhb026e052007-05-02 01:34:31 +00004517 nHeader += putVarint(&pCell[nHeader], nData+nZero);
drh6f11bef2004-05-13 01:12:56 +00004518 }else{
drhb026e052007-05-02 01:34:31 +00004519 nData = nZero = 0;
drh91025292004-05-03 19:49:32 +00004520 }
drh6f11bef2004-05-13 01:12:56 +00004521 nHeader += putVarint(&pCell[nHeader], *(u64*)&nKey);
drh16a9b832007-05-05 18:39:25 +00004522 sqlite3BtreeParseCellPtr(pPage, pCell, &info);
drh6f11bef2004-05-13 01:12:56 +00004523 assert( info.nHeader==nHeader );
4524 assert( info.nKey==nKey );
danielk197789d40042008-11-17 14:20:56 +00004525 assert( info.nData==(u32)(nData+nZero) );
drh6f11bef2004-05-13 01:12:56 +00004526
4527 /* Fill in the payload */
drhb026e052007-05-02 01:34:31 +00004528 nPayload = nData + nZero;
drh3aac2dd2004-04-26 14:10:20 +00004529 if( pPage->intKey ){
4530 pSrc = pData;
4531 nSrc = nData;
drh91025292004-05-03 19:49:32 +00004532 nData = 0;
drhf49661a2008-12-10 16:45:50 +00004533 }else{
4534 /* TBD: Perhaps raise SQLITE_CORRUPT if nKey is larger than 31 bits? */
4535 nPayload += (int)nKey;
drh3aac2dd2004-04-26 14:10:20 +00004536 pSrc = pKey;
drhf49661a2008-12-10 16:45:50 +00004537 nSrc = (int)nKey;
drh3aac2dd2004-04-26 14:10:20 +00004538 }
drh6f11bef2004-05-13 01:12:56 +00004539 *pnSize = info.nSize;
4540 spaceLeft = info.nLocal;
drh3aac2dd2004-04-26 14:10:20 +00004541 pPayload = &pCell[nHeader];
drh6f11bef2004-05-13 01:12:56 +00004542 pPrior = &pCell[info.iOverflow];
drh3b7511c2001-05-26 13:15:44 +00004543
drh3b7511c2001-05-26 13:15:44 +00004544 while( nPayload>0 ){
4545 if( spaceLeft==0 ){
danielk1977afcdd022004-10-31 16:25:42 +00004546#ifndef SQLITE_OMIT_AUTOVACUUM
4547 Pgno pgnoPtrmap = pgnoOvfl; /* Overflow page pointer-map entry page */
danielk1977b39f70b2007-05-17 18:28:11 +00004548 if( pBt->autoVacuum ){
4549 do{
4550 pgnoOvfl++;
4551 } while(
4552 PTRMAP_ISPAGE(pBt, pgnoOvfl) || pgnoOvfl==PENDING_BYTE_PAGE(pBt)
4553 );
danielk1977b39f70b2007-05-17 18:28:11 +00004554 }
danielk1977afcdd022004-10-31 16:25:42 +00004555#endif
drhf49661a2008-12-10 16:45:50 +00004556 rc = allocateBtreePage(pBt, &pOvfl, &pgnoOvfl, pgnoOvfl, 0);
danielk1977afcdd022004-10-31 16:25:42 +00004557#ifndef SQLITE_OMIT_AUTOVACUUM
danielk1977a19df672004-11-03 11:37:07 +00004558 /* If the database supports auto-vacuum, and the second or subsequent
4559 ** overflow page is being allocated, add an entry to the pointer-map
danielk19774ef24492007-05-23 09:52:41 +00004560 ** for that page now.
4561 **
4562 ** If this is the first overflow page, then write a partial entry
4563 ** to the pointer-map. If we write nothing to this pointer-map slot,
4564 ** then the optimistic overflow chain processing in clearCell()
4565 ** may misinterpret the uninitialised values and delete the
4566 ** wrong pages from the database.
danielk1977afcdd022004-10-31 16:25:42 +00004567 */
danielk19774ef24492007-05-23 09:52:41 +00004568 if( pBt->autoVacuum && rc==SQLITE_OK ){
4569 u8 eType = (pgnoPtrmap?PTRMAP_OVERFLOW2:PTRMAP_OVERFLOW1);
4570 rc = ptrmapPut(pBt, pgnoOvfl, eType, pgnoPtrmap);
danielk197789a4be82007-05-23 13:34:32 +00004571 if( rc ){
4572 releasePage(pOvfl);
4573 }
danielk1977afcdd022004-10-31 16:25:42 +00004574 }
4575#endif
drh3b7511c2001-05-26 13:15:44 +00004576 if( rc ){
drh9b171272004-05-08 02:03:22 +00004577 releasePage(pToRelease);
drh3b7511c2001-05-26 13:15:44 +00004578 return rc;
4579 }
drhc5053fb2008-11-27 02:22:10 +00004580
4581 /* If pToRelease is not zero than pPrior points into the data area
4582 ** of pToRelease. Make sure pToRelease is still writeable. */
4583 assert( pToRelease==0 || sqlite3PagerIswriteable(pToRelease->pDbPage) );
4584
4585 /* If pPrior is part of the data area of pPage, then make sure pPage
4586 ** is still writeable */
4587 assert( pPrior<pPage->aData || pPrior>=&pPage->aData[pBt->pageSize]
4588 || sqlite3PagerIswriteable(pPage->pDbPage) );
4589
drh3aac2dd2004-04-26 14:10:20 +00004590 put4byte(pPrior, pgnoOvfl);
drh9b171272004-05-08 02:03:22 +00004591 releasePage(pToRelease);
4592 pToRelease = pOvfl;
drh3aac2dd2004-04-26 14:10:20 +00004593 pPrior = pOvfl->aData;
4594 put4byte(pPrior, 0);
4595 pPayload = &pOvfl->aData[4];
drhb6f41482004-05-14 01:58:11 +00004596 spaceLeft = pBt->usableSize - 4;
drh3b7511c2001-05-26 13:15:44 +00004597 }
4598 n = nPayload;
4599 if( n>spaceLeft ) n = spaceLeft;
drhc5053fb2008-11-27 02:22:10 +00004600
4601 /* If pToRelease is not zero than pPayload points into the data area
4602 ** of pToRelease. Make sure pToRelease is still writeable. */
4603 assert( pToRelease==0 || sqlite3PagerIswriteable(pToRelease->pDbPage) );
4604
4605 /* If pPayload is part of the data area of pPage, then make sure pPage
4606 ** is still writeable */
4607 assert( pPayload<pPage->aData || pPayload>=&pPage->aData[pBt->pageSize]
4608 || sqlite3PagerIswriteable(pPage->pDbPage) );
4609
drhb026e052007-05-02 01:34:31 +00004610 if( nSrc>0 ){
4611 if( n>nSrc ) n = nSrc;
4612 assert( pSrc );
4613 memcpy(pPayload, pSrc, n);
4614 }else{
4615 memset(pPayload, 0, n);
4616 }
drh3b7511c2001-05-26 13:15:44 +00004617 nPayload -= n;
drhde647132004-05-07 17:57:49 +00004618 pPayload += n;
drh9b171272004-05-08 02:03:22 +00004619 pSrc += n;
drh3aac2dd2004-04-26 14:10:20 +00004620 nSrc -= n;
drh3b7511c2001-05-26 13:15:44 +00004621 spaceLeft -= n;
drh3aac2dd2004-04-26 14:10:20 +00004622 if( nSrc==0 ){
4623 nSrc = nData;
4624 pSrc = pData;
4625 }
drhdd793422001-06-28 01:54:48 +00004626 }
drh9b171272004-05-08 02:03:22 +00004627 releasePage(pToRelease);
drh3b7511c2001-05-26 13:15:44 +00004628 return SQLITE_OK;
4629}
4630
drh14acc042001-06-10 19:56:58 +00004631/*
4632** Remove the i-th cell from pPage. This routine effects pPage only.
4633** The cell content is not freed or deallocated. It is assumed that
4634** the cell content has been copied someplace else. This routine just
4635** removes the reference to the cell from pPage.
4636**
4637** "sz" must be the number of bytes in the cell.
drh14acc042001-06-10 19:56:58 +00004638*/
shane0af3f892008-11-12 04:55:34 +00004639static int dropCell(MemPage *pPage, int idx, int sz){
drh43605152004-05-29 21:46:49 +00004640 int i; /* Loop counter */
4641 int pc; /* Offset to cell content of cell being deleted */
4642 u8 *data; /* pPage->aData */
4643 u8 *ptr; /* Used to move bytes around within data[] */
shanedcc50b72008-11-13 18:29:50 +00004644 int rc; /* The return code */
drh43605152004-05-29 21:46:49 +00004645
drh8c42ca92001-06-22 19:15:00 +00004646 assert( idx>=0 && idx<pPage->nCell );
drh43605152004-05-29 21:46:49 +00004647 assert( sz==cellSize(pPage, idx) );
danielk19773b8a05f2007-03-19 17:44:26 +00004648 assert( sqlite3PagerIswriteable(pPage->pDbPage) );
drh1fee73e2007-08-29 04:00:57 +00004649 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
drhda200cc2004-05-09 11:51:38 +00004650 data = pPage->aData;
drh43605152004-05-29 21:46:49 +00004651 ptr = &data[pPage->cellOffset + 2*idx];
shane0af3f892008-11-12 04:55:34 +00004652 pc = get2byte(ptr);
drhc5053fb2008-11-27 02:22:10 +00004653 if( (pc<pPage->hdrOffset+6+(pPage->leaf?0:4))
4654 || (pc+sz>pPage->pBt->usableSize) ){
shane0af3f892008-11-12 04:55:34 +00004655 return SQLITE_CORRUPT_BKPT;
4656 }
shanedcc50b72008-11-13 18:29:50 +00004657 rc = freeSpace(pPage, pc, sz);
4658 if( rc!=SQLITE_OK ){
4659 return rc;
4660 }
drh43605152004-05-29 21:46:49 +00004661 for(i=idx+1; i<pPage->nCell; i++, ptr+=2){
4662 ptr[0] = ptr[2];
4663 ptr[1] = ptr[3];
drh14acc042001-06-10 19:56:58 +00004664 }
4665 pPage->nCell--;
drh43605152004-05-29 21:46:49 +00004666 put2byte(&data[pPage->hdrOffset+3], pPage->nCell);
4667 pPage->nFree += 2;
shane0af3f892008-11-12 04:55:34 +00004668 return SQLITE_OK;
drh14acc042001-06-10 19:56:58 +00004669}
4670
4671/*
4672** Insert a new cell on pPage at cell index "i". pCell points to the
4673** content of the cell.
4674**
4675** If the cell content will fit on the page, then put it there. If it
drh43605152004-05-29 21:46:49 +00004676** will not fit, then make a copy of the cell content into pTemp if
4677** pTemp is not null. Regardless of pTemp, allocate a new entry
4678** in pPage->aOvfl[] and make it point to the cell content (either
4679** in pTemp or the original pCell) and also record its index.
4680** Allocating a new entry in pPage->aCell[] implies that
4681** pPage->nOverflow is incremented.
danielk1977a3ad5e72005-01-07 08:56:44 +00004682**
4683** If nSkip is non-zero, then do not copy the first nSkip bytes of the
4684** cell. The caller will overwrite them after this function returns. If
drh4b238df2005-01-08 15:43:18 +00004685** nSkip is non-zero, then pCell may not point to an invalid memory location
danielk1977a3ad5e72005-01-07 08:56:44 +00004686** (but pCell+nSkip is always valid).
drh14acc042001-06-10 19:56:58 +00004687*/
danielk1977e80463b2004-11-03 03:01:16 +00004688static int insertCell(
drh24cd67e2004-05-10 16:18:47 +00004689 MemPage *pPage, /* Page into which we are copying */
drh43605152004-05-29 21:46:49 +00004690 int i, /* New cell becomes the i-th cell of the page */
4691 u8 *pCell, /* Content of the new cell */
4692 int sz, /* Bytes of content in pCell */
danielk1977a3ad5e72005-01-07 08:56:44 +00004693 u8 *pTemp, /* Temp storage space for pCell, if needed */
4694 u8 nSkip /* Do not write the first nSkip bytes of the cell */
drh24cd67e2004-05-10 16:18:47 +00004695){
drh43605152004-05-29 21:46:49 +00004696 int idx; /* Where to write new cell content in data[] */
4697 int j; /* Loop counter */
4698 int top; /* First byte of content for any cell in data[] */
4699 int end; /* First byte past the last cell pointer in data[] */
4700 int ins; /* Index in data[] where new cell pointer is inserted */
4701 int hdr; /* Offset into data[] of the page header */
4702 int cellOffset; /* Address of first cell pointer in data[] */
4703 u8 *data; /* The content of the whole page */
4704 u8 *ptr; /* Used for moving information around in data[] */
4705
4706 assert( i>=0 && i<=pPage->nCell+pPage->nOverflow );
drhf49661a2008-12-10 16:45:50 +00004707 assert( pPage->nCell<=MX_CELL(pPage->pBt) && MX_CELL(pPage->pBt)<=5460 );
4708 assert( pPage->nOverflow<=ArraySize(pPage->aOvfl) );
drh43605152004-05-29 21:46:49 +00004709 assert( sz==cellSizePtr(pPage, pCell) );
drh1fee73e2007-08-29 04:00:57 +00004710 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
drh43605152004-05-29 21:46:49 +00004711 if( pPage->nOverflow || sz+2>pPage->nFree ){
drh24cd67e2004-05-10 16:18:47 +00004712 if( pTemp ){
danielk1977a3ad5e72005-01-07 08:56:44 +00004713 memcpy(pTemp+nSkip, pCell+nSkip, sz-nSkip);
drh43605152004-05-29 21:46:49 +00004714 pCell = pTemp;
drh24cd67e2004-05-10 16:18:47 +00004715 }
drh43605152004-05-29 21:46:49 +00004716 j = pPage->nOverflow++;
danielk197789d40042008-11-17 14:20:56 +00004717 assert( j<(int)(sizeof(pPage->aOvfl)/sizeof(pPage->aOvfl[0])) );
drh43605152004-05-29 21:46:49 +00004718 pPage->aOvfl[j].pCell = pCell;
drhf49661a2008-12-10 16:45:50 +00004719 pPage->aOvfl[j].idx = (u16)i;
drh43605152004-05-29 21:46:49 +00004720 pPage->nFree = 0;
drh14acc042001-06-10 19:56:58 +00004721 }else{
danielk19776e465eb2007-08-21 13:11:00 +00004722 int rc = sqlite3PagerWrite(pPage->pDbPage);
4723 if( rc!=SQLITE_OK ){
4724 return rc;
4725 }
4726 assert( sqlite3PagerIswriteable(pPage->pDbPage) );
drh43605152004-05-29 21:46:49 +00004727 data = pPage->aData;
4728 hdr = pPage->hdrOffset;
4729 top = get2byte(&data[hdr+5]);
4730 cellOffset = pPage->cellOffset;
4731 end = cellOffset + 2*pPage->nCell + 2;
4732 ins = cellOffset + 2*i;
4733 if( end > top - sz ){
shane0af3f892008-11-12 04:55:34 +00004734 rc = defragmentPage(pPage);
4735 if( rc!=SQLITE_OK ){
4736 return rc;
4737 }
drh43605152004-05-29 21:46:49 +00004738 top = get2byte(&data[hdr+5]);
4739 assert( end + sz <= top );
4740 }
4741 idx = allocateSpace(pPage, sz);
4742 assert( idx>0 );
4743 assert( end <= get2byte(&data[hdr+5]) );
shane0af3f892008-11-12 04:55:34 +00004744 if (idx+sz > pPage->pBt->usableSize) {
shane34ac18d2008-11-11 22:18:20 +00004745 return SQLITE_CORRUPT_BKPT;
shane0af3f892008-11-12 04:55:34 +00004746 }
drh43605152004-05-29 21:46:49 +00004747 pPage->nCell++;
4748 pPage->nFree -= 2;
danielk1977a3ad5e72005-01-07 08:56:44 +00004749 memcpy(&data[idx+nSkip], pCell+nSkip, sz-nSkip);
drh43605152004-05-29 21:46:49 +00004750 for(j=end-2, ptr=&data[j]; j>ins; j-=2, ptr-=2){
4751 ptr[0] = ptr[-2];
4752 ptr[1] = ptr[-1];
drhda200cc2004-05-09 11:51:38 +00004753 }
drh43605152004-05-29 21:46:49 +00004754 put2byte(&data[ins], idx);
4755 put2byte(&data[hdr+3], pPage->nCell);
danielk1977a19df672004-11-03 11:37:07 +00004756#ifndef SQLITE_OMIT_AUTOVACUUM
4757 if( pPage->pBt->autoVacuum ){
4758 /* The cell may contain a pointer to an overflow page. If so, write
4759 ** the entry for the overflow page into the pointer map.
4760 */
4761 CellInfo info;
drh16a9b832007-05-05 18:39:25 +00004762 sqlite3BtreeParseCellPtr(pPage, pCell, &info);
drh72365832007-03-06 15:53:44 +00004763 assert( (info.nData+(pPage->intKey?0:info.nKey))==info.nPayload );
danielk1977a19df672004-11-03 11:37:07 +00004764 if( (info.nData+(pPage->intKey?0:info.nKey))>info.nLocal ){
4765 Pgno pgnoOvfl = get4byte(&pCell[info.iOverflow]);
danielk19776e465eb2007-08-21 13:11:00 +00004766 rc = ptrmapPut(pPage->pBt, pgnoOvfl, PTRMAP_OVERFLOW1, pPage->pgno);
danielk1977a19df672004-11-03 11:37:07 +00004767 if( rc!=SQLITE_OK ) return rc;
4768 }
4769 }
4770#endif
drh14acc042001-06-10 19:56:58 +00004771 }
danielk1977e80463b2004-11-03 03:01:16 +00004772
danielk1977e80463b2004-11-03 03:01:16 +00004773 return SQLITE_OK;
drh14acc042001-06-10 19:56:58 +00004774}
4775
4776/*
drhfa1a98a2004-05-14 19:08:17 +00004777** Add a list of cells to a page. The page should be initially empty.
4778** The cells are guaranteed to fit on the page.
4779*/
4780static void assemblePage(
4781 MemPage *pPage, /* The page to be assemblied */
4782 int nCell, /* The number of cells to add to this page */
drh43605152004-05-29 21:46:49 +00004783 u8 **apCell, /* Pointers to cell bodies */
drha9121e42008-02-19 14:59:35 +00004784 u16 *aSize /* Sizes of the cells */
drhfa1a98a2004-05-14 19:08:17 +00004785){
4786 int i; /* Loop counter */
4787 int totalSize; /* Total size of all cells */
4788 int hdr; /* Index of page header */
drh43605152004-05-29 21:46:49 +00004789 int cellptr; /* Address of next cell pointer */
4790 int cellbody; /* Address of next cell body */
drhfa1a98a2004-05-14 19:08:17 +00004791 u8 *data; /* Data for the page */
4792
drh43605152004-05-29 21:46:49 +00004793 assert( pPage->nOverflow==0 );
drh1fee73e2007-08-29 04:00:57 +00004794 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
drhf49661a2008-12-10 16:45:50 +00004795 assert( nCell>=0 && nCell<=MX_CELL(pPage->pBt) && MX_CELL(pPage->pBt)<=5460 );
drhfa1a98a2004-05-14 19:08:17 +00004796 totalSize = 0;
4797 for(i=0; i<nCell; i++){
4798 totalSize += aSize[i];
4799 }
drh43605152004-05-29 21:46:49 +00004800 assert( totalSize+2*nCell<=pPage->nFree );
drhfa1a98a2004-05-14 19:08:17 +00004801 assert( pPage->nCell==0 );
drhc5053fb2008-11-27 02:22:10 +00004802 assert( sqlite3PagerIswriteable(pPage->pDbPage) );
drh43605152004-05-29 21:46:49 +00004803 cellptr = pPage->cellOffset;
drhfa1a98a2004-05-14 19:08:17 +00004804 data = pPage->aData;
4805 hdr = pPage->hdrOffset;
drh43605152004-05-29 21:46:49 +00004806 put2byte(&data[hdr+3], nCell);
drh09d0deb2005-08-02 17:13:09 +00004807 if( nCell ){
4808 cellbody = allocateSpace(pPage, totalSize);
4809 assert( cellbody>0 );
4810 assert( pPage->nFree >= 2*nCell );
4811 pPage->nFree -= 2*nCell;
4812 for(i=0; i<nCell; i++){
4813 put2byte(&data[cellptr], cellbody);
4814 memcpy(&data[cellbody], apCell[i], aSize[i]);
4815 cellptr += 2;
4816 cellbody += aSize[i];
4817 }
4818 assert( cellbody==pPage->pBt->usableSize );
drhfa1a98a2004-05-14 19:08:17 +00004819 }
drhf49661a2008-12-10 16:45:50 +00004820 pPage->nCell = (u16)nCell;
drhfa1a98a2004-05-14 19:08:17 +00004821}
4822
drh14acc042001-06-10 19:56:58 +00004823/*
drhc3b70572003-01-04 19:44:07 +00004824** The following parameters determine how many adjacent pages get involved
4825** in a balancing operation. NN is the number of neighbors on either side
4826** of the page that participate in the balancing operation. NB is the
4827** total number of pages that participate, including the target page and
4828** NN neighbors on either side.
4829**
4830** The minimum value of NN is 1 (of course). Increasing NN above 1
4831** (to 2 or 3) gives a modest improvement in SELECT and DELETE performance
4832** in exchange for a larger degradation in INSERT and UPDATE performance.
4833** The value of NN appears to give the best results overall.
4834*/
4835#define NN 1 /* Number of neighbors on either side of pPage */
4836#define NB (NN*2+1) /* Total pages involved in the balance */
4837
drh43605152004-05-29 21:46:49 +00004838/* Forward reference */
danielk197771d5d2c2008-09-29 11:49:47 +00004839static int balance(BtCursor*, int);
danielk1977ac245ec2005-01-14 13:50:11 +00004840
drh615ae552005-01-16 23:21:00 +00004841#ifndef SQLITE_OMIT_QUICKBALANCE
drhf222e712005-01-14 22:55:49 +00004842/*
4843** This version of balance() handles the common special case where
4844** a new entry is being inserted on the extreme right-end of the
4845** tree, in other words, when the new entry will become the largest
4846** entry in the tree.
4847**
4848** Instead of trying balance the 3 right-most leaf pages, just add
4849** a new page to the right-hand side and put the one new entry in
4850** that page. This leaves the right side of the tree somewhat
4851** unbalanced. But odds are that we will be inserting new entries
4852** at the end soon afterwards so the nearly empty page will quickly
4853** fill up. On average.
4854**
4855** pPage is the leaf page which is the right-most page in the tree.
4856** pParent is its parent. pPage must have a single overflow entry
4857** which is also the right-most entry on the page.
4858*/
danielk197771d5d2c2008-09-29 11:49:47 +00004859static int balance_quick(BtCursor *pCur){
danielk1977ac245ec2005-01-14 13:50:11 +00004860 int rc;
danielk1977eaa06f62008-09-18 17:34:44 +00004861 MemPage *pNew = 0;
danielk1977ac245ec2005-01-14 13:50:11 +00004862 Pgno pgnoNew;
4863 u8 *pCell;
drha9121e42008-02-19 14:59:35 +00004864 u16 szCell;
danielk1977ac245ec2005-01-14 13:50:11 +00004865 CellInfo info;
danielk197771d5d2c2008-09-29 11:49:47 +00004866 MemPage *pPage = pCur->apPage[pCur->iPage];
4867 MemPage *pParent = pCur->apPage[pCur->iPage-1];
danielk1977aef0bf62005-12-30 16:28:01 +00004868 BtShared *pBt = pPage->pBt;
danielk197779a40da2005-01-16 08:00:01 +00004869 int parentIdx = pParent->nCell; /* pParent new divider cell index */
4870 int parentSize; /* Size of new divider cell */
4871 u8 parentCell[64]; /* Space for the new divider cell */
danielk1977ac245ec2005-01-14 13:50:11 +00004872
drh1fee73e2007-08-29 04:00:57 +00004873 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
drhd677b3d2007-08-20 22:48:41 +00004874
danielk1977ac245ec2005-01-14 13:50:11 +00004875 /* Allocate a new page. Insert the overflow cell from pPage
4876 ** into it. Then remove the overflow cell from pPage.
4877 */
drh4f0c5872007-03-26 22:05:01 +00004878 rc = allocateBtreePage(pBt, &pNew, &pgnoNew, 0, 0);
danielk1977eaa06f62008-09-18 17:34:44 +00004879 if( rc==SQLITE_OK ){
4880 pCell = pPage->aOvfl[0].pCell;
4881 szCell = cellSizePtr(pPage, pCell);
drhc5053fb2008-11-27 02:22:10 +00004882 assert( sqlite3PagerIswriteable(pNew->pDbPage) );
danielk1977eaa06f62008-09-18 17:34:44 +00004883 zeroPage(pNew, pPage->aData[0]);
4884 assemblePage(pNew, 1, &pCell, &szCell);
4885 pPage->nOverflow = 0;
4886
danielk1977eaa06f62008-09-18 17:34:44 +00004887 /* pPage is currently the right-child of pParent. Change this
4888 ** so that the right-child is the new page allocated above and
4889 ** pPage is the next-to-right child.
4890 **
4891 ** Ignore the return value of the call to fillInCell(). fillInCell()
4892 ** may only return other than SQLITE_OK if it is required to allocate
4893 ** one or more overflow pages. Since an internal table B-Tree cell
4894 ** may never spill over onto an overflow page (it is a maximum of
4895 ** 13 bytes in size), it is not neccessary to check the return code.
4896 **
4897 ** Similarly, the insertCell() function cannot fail if the page
4898 ** being inserted into is already writable and the cell does not
4899 ** contain an overflow pointer. So ignore this return code too.
4900 */
4901 assert( pPage->nCell>0 );
4902 pCell = findCell(pPage, pPage->nCell-1);
4903 sqlite3BtreeParseCellPtr(pPage, pCell, &info);
4904 fillInCell(pParent, parentCell, 0, info.nKey, 0, 0, 0, &parentSize);
4905 assert( parentSize<64 );
4906 assert( sqlite3PagerIswriteable(pParent->pDbPage) );
4907 insertCell(pParent, parentIdx, parentCell, parentSize, 0, 4);
4908 put4byte(findOverflowCell(pParent,parentIdx), pPage->pgno);
4909 put4byte(&pParent->aData[pParent->hdrOffset+8], pgnoNew);
4910
4911 /* If this is an auto-vacuum database, update the pointer map
4912 ** with entries for the new page, and any pointer from the
4913 ** cell on the page to an overflow page.
4914 */
4915 if( ISAUTOVACUUM ){
4916 rc = ptrmapPut(pBt, pgnoNew, PTRMAP_BTREE, pParent->pgno);
4917 if( rc==SQLITE_OK ){
4918 rc = ptrmapPutOvfl(pNew, 0);
4919 }
danielk1977ac11ee62005-01-15 12:45:51 +00004920 }
danielk1977e08a3c42008-09-18 18:17:03 +00004921
4922 /* Release the reference to the new page. */
4923 releasePage(pNew);
danielk1977ac11ee62005-01-15 12:45:51 +00004924 }
4925
danielk1977eaa06f62008-09-18 17:34:44 +00004926 /* At this point the pPage->nFree variable is not set correctly with
4927 ** respect to the content of the page (because it was set to 0 by
4928 ** insertCell). So call sqlite3BtreeInitPage() to make sure it is
4929 ** correct.
4930 **
4931 ** This has to be done even if an error will be returned. Normally, if
4932 ** an error occurs during tree balancing, the contents of MemPage are
4933 ** not important, as they will be recalculated when the page is rolled
4934 ** back. But here, in balance_quick(), it is possible that pPage has
4935 ** not yet been marked dirty or written into the journal file. Therefore
4936 ** it will not be rolled back and so it is important to make sure that
4937 ** the page data and contents of MemPage are consistent.
4938 */
4939 pPage->isInit = 0;
danielk197771d5d2c2008-09-29 11:49:47 +00004940 sqlite3BtreeInitPage(pPage);
danielk1977a4124bd2008-12-23 10:37:47 +00004941 assert( pPage->nOverflow==0 );
danielk1977eaa06f62008-09-18 17:34:44 +00004942
danielk1977e08a3c42008-09-18 18:17:03 +00004943 /* If everything else succeeded, balance the parent page, in
4944 ** case the divider cell inserted caused it to become overfull.
danielk197779a40da2005-01-16 08:00:01 +00004945 */
danielk1977eaa06f62008-09-18 17:34:44 +00004946 if( rc==SQLITE_OK ){
danielk197771d5d2c2008-09-29 11:49:47 +00004947 releasePage(pPage);
4948 pCur->iPage--;
4949 rc = balance(pCur, 0);
danielk1977eaa06f62008-09-18 17:34:44 +00004950 }
4951 return rc;
danielk1977ac245ec2005-01-14 13:50:11 +00004952}
drh615ae552005-01-16 23:21:00 +00004953#endif /* SQLITE_OMIT_QUICKBALANCE */
drh43605152004-05-29 21:46:49 +00004954
drhc3b70572003-01-04 19:44:07 +00004955/*
drhab01f612004-05-22 02:55:23 +00004956** This routine redistributes Cells on pPage and up to NN*2 siblings
drh8b2f49b2001-06-08 00:21:52 +00004957** of pPage so that all pages have about the same amount of free space.
drh0c6cc4e2004-06-15 02:13:26 +00004958** Usually NN siblings on either side of pPage is used in the balancing,
4959** though more siblings might come from one side if pPage is the first
drhab01f612004-05-22 02:55:23 +00004960** or last child of its parent. If pPage has fewer than 2*NN siblings
drh8b2f49b2001-06-08 00:21:52 +00004961** (something which can only happen if pPage is the root page or a
drh14acc042001-06-10 19:56:58 +00004962** child of root) then all available siblings participate in the balancing.
drh8b2f49b2001-06-08 00:21:52 +00004963**
drh0c6cc4e2004-06-15 02:13:26 +00004964** The number of siblings of pPage might be increased or decreased by one or
4965** two in an effort to keep pages nearly full but not over full. The root page
drhab01f612004-05-22 02:55:23 +00004966** is special and is allowed to be nearly empty. If pPage is
drh8c42ca92001-06-22 19:15:00 +00004967** the root page, then the depth of the tree might be increased
drh8b2f49b2001-06-08 00:21:52 +00004968** or decreased by one, as necessary, to keep the root page from being
drhab01f612004-05-22 02:55:23 +00004969** overfull or completely empty.
drh14acc042001-06-10 19:56:58 +00004970**
drh8b2f49b2001-06-08 00:21:52 +00004971** Note that when this routine is called, some of the Cells on pPage
drh4b70f112004-05-02 21:12:19 +00004972** might not actually be stored in pPage->aData[]. This can happen
drh8b2f49b2001-06-08 00:21:52 +00004973** if the page is overfull. Part of the job of this routine is to
drh4b70f112004-05-02 21:12:19 +00004974** make sure all Cells for pPage once again fit in pPage->aData[].
drh14acc042001-06-10 19:56:58 +00004975**
drh8c42ca92001-06-22 19:15:00 +00004976** In the course of balancing the siblings of pPage, the parent of pPage
4977** might become overfull or underfull. If that happens, then this routine
4978** is called recursively on the parent.
4979**
drh5e00f6c2001-09-13 13:46:56 +00004980** If this routine fails for any reason, it might leave the database
4981** in a corrupted state. So if this routine fails, the database should
4982** be rolled back.
drh8b2f49b2001-06-08 00:21:52 +00004983*/
danielk197771d5d2c2008-09-29 11:49:47 +00004984static int balance_nonroot(BtCursor *pCur){
4985 MemPage *pPage; /* The over or underfull page to balance */
drh8b2f49b2001-06-08 00:21:52 +00004986 MemPage *pParent; /* The parent of pPage */
drh16a9b832007-05-05 18:39:25 +00004987 BtShared *pBt; /* The whole database */
danielk1977634f2982005-03-28 08:44:07 +00004988 int nCell = 0; /* Number of cells in apCell[] */
4989 int nMaxCells = 0; /* Allocated size of apCell, szCell, aFrom. */
danielk1977a4124bd2008-12-23 10:37:47 +00004990 int nOld = 0; /* Number of pages in apOld[] */
4991 int nNew = 0; /* Number of pages in apNew[] */
drh8b2f49b2001-06-08 00:21:52 +00004992 int nDiv; /* Number of cells in apDiv[] */
drh14acc042001-06-10 19:56:58 +00004993 int i, j, k; /* Loop counters */
drha34b6762004-05-07 13:30:42 +00004994 int idx; /* Index of pPage in pParent->aCell[] */
4995 int nxDiv; /* Next divider slot in pParent->aCell[] */
drh14acc042001-06-10 19:56:58 +00004996 int rc; /* The return code */
drh91025292004-05-03 19:49:32 +00004997 int leafCorrection; /* 4 if pPage is a leaf. 0 if not */
drh8b18dd42004-05-12 19:18:15 +00004998 int leafData; /* True if pPage is a leaf of a LEAFDATA tree */
drh91025292004-05-03 19:49:32 +00004999 int usableSpace; /* Bytes in pPage beyond the header */
5000 int pageFlags; /* Value of pPage->aData[0] */
drh6019e162001-07-02 17:51:45 +00005001 int subtotal; /* Subtotal of bytes in cells on one page */
drhe5ae5732008-06-15 02:51:47 +00005002 int iSpace1 = 0; /* First unused byte of aSpace1[] */
5003 int iSpace2 = 0; /* First unused byte of aSpace2[] */
drhfacf0302008-06-17 15:12:00 +00005004 int szScratch; /* Size of scratch memory requested */
drhc3b70572003-01-04 19:44:07 +00005005 MemPage *apOld[NB]; /* pPage and up to two siblings */
5006 Pgno pgnoOld[NB]; /* Page numbers for each page in apOld[] */
drh4b70f112004-05-02 21:12:19 +00005007 MemPage *apCopy[NB]; /* Private copies of apOld[] pages */
drha2fce642004-06-05 00:01:44 +00005008 MemPage *apNew[NB+2]; /* pPage and up to NB siblings after balancing */
5009 Pgno pgnoNew[NB+2]; /* Page numbers for each page in apNew[] */
drh4b70f112004-05-02 21:12:19 +00005010 u8 *apDiv[NB]; /* Divider cells in pParent */
drha2fce642004-06-05 00:01:44 +00005011 int cntNew[NB+2]; /* Index in aCell[] of cell after i-th page */
5012 int szNew[NB+2]; /* Combined size of cells place on i-th page */
danielk197750f059b2005-03-29 02:54:03 +00005013 u8 **apCell = 0; /* All cells begin balanced */
drha9121e42008-02-19 14:59:35 +00005014 u16 *szCell; /* Local size of all cells in apCell[] */
drhe5ae5732008-06-15 02:51:47 +00005015 u8 *aCopy[NB]; /* Space for holding data of apCopy[] */
5016 u8 *aSpace1; /* Space for copies of dividers cells before balance */
5017 u8 *aSpace2 = 0; /* Space for overflow dividers cells after balance */
danielk1977ac11ee62005-01-15 12:45:51 +00005018 u8 *aFrom = 0;
drh8b2f49b2001-06-08 00:21:52 +00005019
danielk197771d5d2c2008-09-29 11:49:47 +00005020 pPage = pCur->apPage[pCur->iPage];
drh1fee73e2007-08-29 04:00:57 +00005021 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
drhf94a1732008-09-30 17:18:17 +00005022 VVA_ONLY( pCur->pagesShuffled = 1 );
drhd677b3d2007-08-20 22:48:41 +00005023
drh14acc042001-06-10 19:56:58 +00005024 /*
drh43605152004-05-29 21:46:49 +00005025 ** Find the parent page.
drh8b2f49b2001-06-08 00:21:52 +00005026 */
danielk197771d5d2c2008-09-29 11:49:47 +00005027 assert( pCur->iPage>0 );
5028 assert( pPage->isInit );
danielk19776e465eb2007-08-21 13:11:00 +00005029 assert( sqlite3PagerIswriteable(pPage->pDbPage) || pPage->nOverflow==1 );
drh4b70f112004-05-02 21:12:19 +00005030 pBt = pPage->pBt;
danielk197771d5d2c2008-09-29 11:49:47 +00005031 pParent = pCur->apPage[pCur->iPage-1];
drh43605152004-05-29 21:46:49 +00005032 assert( pParent );
danielk19773b8a05f2007-03-19 17:44:26 +00005033 if( SQLITE_OK!=(rc = sqlite3PagerWrite(pParent->pDbPage)) ){
danielk1977a4124bd2008-12-23 10:37:47 +00005034 goto balance_cleanup;
danielk197707cb5602006-01-20 10:55:05 +00005035 }
danielk1977474b7cc2008-07-09 11:49:46 +00005036
drh43605152004-05-29 21:46:49 +00005037 TRACE(("BALANCE: begin page %d child of %d\n", pPage->pgno, pParent->pgno));
drh2e38c322004-09-03 18:38:44 +00005038
drh615ae552005-01-16 23:21:00 +00005039#ifndef SQLITE_OMIT_QUICKBALANCE
drhf222e712005-01-14 22:55:49 +00005040 /*
5041 ** A special case: If a new entry has just been inserted into a
5042 ** table (that is, a btree with integer keys and all data at the leaves)
drh09d0deb2005-08-02 17:13:09 +00005043 ** and the new entry is the right-most entry in the tree (it has the
drhf222e712005-01-14 22:55:49 +00005044 ** largest key) then use the special balance_quick() routine for
5045 ** balancing. balance_quick() is much faster and results in a tighter
5046 ** packing of data in the common case.
5047 */
danielk1977ac245ec2005-01-14 13:50:11 +00005048 if( pPage->leaf &&
5049 pPage->intKey &&
danielk1977ac245ec2005-01-14 13:50:11 +00005050 pPage->nOverflow==1 &&
5051 pPage->aOvfl[0].idx==pPage->nCell &&
danielk197771d5d2c2008-09-29 11:49:47 +00005052 pParent->pgno!=1 &&
danielk1977ac245ec2005-01-14 13:50:11 +00005053 get4byte(&pParent->aData[pParent->hdrOffset+8])==pPage->pgno
5054 ){
drh44845222008-07-17 18:39:57 +00005055 assert( pPage->intKey );
danielk1977ac11ee62005-01-15 12:45:51 +00005056 /*
5057 ** TODO: Check the siblings to the left of pPage. It may be that
5058 ** they are not full and no new page is required.
5059 */
danielk197771d5d2c2008-09-29 11:49:47 +00005060 return balance_quick(pCur);
danielk1977ac245ec2005-01-14 13:50:11 +00005061 }
5062#endif
5063
danielk19776e465eb2007-08-21 13:11:00 +00005064 if( SQLITE_OK!=(rc = sqlite3PagerWrite(pPage->pDbPage)) ){
danielk1977a4124bd2008-12-23 10:37:47 +00005065 goto balance_cleanup;
danielk19776e465eb2007-08-21 13:11:00 +00005066 }
5067
drh2e38c322004-09-03 18:38:44 +00005068 /*
drh4b70f112004-05-02 21:12:19 +00005069 ** Find the cell in the parent page whose left child points back
drh14acc042001-06-10 19:56:58 +00005070 ** to pPage. The "idx" variable is the index of that cell. If pPage
5071 ** is the rightmost child of pParent then set idx to pParent->nCell
drh8b2f49b2001-06-08 00:21:52 +00005072 */
danielk1977bf93c562008-09-29 15:53:25 +00005073 idx = pCur->aiIdx[pCur->iPage-1];
5074 assertParentIndex(pParent, idx, pPage->pgno);
drh8b2f49b2001-06-08 00:21:52 +00005075
5076 /*
drh4b70f112004-05-02 21:12:19 +00005077 ** Find sibling pages to pPage and the cells in pParent that divide
drhc3b70572003-01-04 19:44:07 +00005078 ** the siblings. An attempt is made to find NN siblings on either
5079 ** side of pPage. More siblings are taken from one side, however, if
5080 ** pPage there are fewer than NN siblings on the other side. If pParent
5081 ** has NB or fewer children then all children of pParent are taken.
drh14acc042001-06-10 19:56:58 +00005082 */
drhc3b70572003-01-04 19:44:07 +00005083 nxDiv = idx - NN;
5084 if( nxDiv + NB > pParent->nCell ){
5085 nxDiv = pParent->nCell - NB + 1;
drh8b2f49b2001-06-08 00:21:52 +00005086 }
drhc3b70572003-01-04 19:44:07 +00005087 if( nxDiv<0 ){
5088 nxDiv = 0;
5089 }
drh8b2f49b2001-06-08 00:21:52 +00005090 nDiv = 0;
drhc3b70572003-01-04 19:44:07 +00005091 for(i=0, k=nxDiv; i<NB; i++, k++){
drh14acc042001-06-10 19:56:58 +00005092 if( k<pParent->nCell ){
danielk19771cc5ed82007-05-16 17:28:43 +00005093 apDiv[i] = findCell(pParent, k);
drh8b2f49b2001-06-08 00:21:52 +00005094 nDiv++;
drha34b6762004-05-07 13:30:42 +00005095 assert( !pParent->leaf );
drh43605152004-05-29 21:46:49 +00005096 pgnoOld[i] = get4byte(apDiv[i]);
drh14acc042001-06-10 19:56:58 +00005097 }else if( k==pParent->nCell ){
drh43605152004-05-29 21:46:49 +00005098 pgnoOld[i] = get4byte(&pParent->aData[pParent->hdrOffset+8]);
drh14acc042001-06-10 19:56:58 +00005099 }else{
5100 break;
drh8b2f49b2001-06-08 00:21:52 +00005101 }
danielk197771d5d2c2008-09-29 11:49:47 +00005102 rc = getAndInitPage(pBt, pgnoOld[i], &apOld[i]);
drh6019e162001-07-02 17:51:45 +00005103 if( rc ) goto balance_cleanup;
danielk197771d5d2c2008-09-29 11:49:47 +00005104 /* apOld[i]->idxParent = k; */
drh91025292004-05-03 19:49:32 +00005105 apCopy[i] = 0;
5106 assert( i==nOld );
drh14acc042001-06-10 19:56:58 +00005107 nOld++;
danielk1977634f2982005-03-28 08:44:07 +00005108 nMaxCells += 1+apOld[i]->nCell+apOld[i]->nOverflow;
drh8b2f49b2001-06-08 00:21:52 +00005109 }
5110
drha9121e42008-02-19 14:59:35 +00005111 /* Make nMaxCells a multiple of 4 in order to preserve 8-byte
drh8d97f1f2005-05-05 18:14:13 +00005112 ** alignment */
drha9121e42008-02-19 14:59:35 +00005113 nMaxCells = (nMaxCells + 3)&~3;
drh8d97f1f2005-05-05 18:14:13 +00005114
drh8b2f49b2001-06-08 00:21:52 +00005115 /*
danielk1977634f2982005-03-28 08:44:07 +00005116 ** Allocate space for memory structures
5117 */
drhfacf0302008-06-17 15:12:00 +00005118 szScratch =
drha9121e42008-02-19 14:59:35 +00005119 nMaxCells*sizeof(u8*) /* apCell */
5120 + nMaxCells*sizeof(u16) /* szCell */
5121 + (ROUND8(sizeof(MemPage))+pBt->pageSize)*NB /* aCopy */
drhe5ae5732008-06-15 02:51:47 +00005122 + pBt->pageSize /* aSpace1 */
drhfacf0302008-06-17 15:12:00 +00005123 + (ISAUTOVACUUM ? nMaxCells : 0); /* aFrom */
5124 apCell = sqlite3ScratchMalloc( szScratch );
danielk1977634f2982005-03-28 08:44:07 +00005125 if( apCell==0 ){
5126 rc = SQLITE_NOMEM;
5127 goto balance_cleanup;
5128 }
drha9121e42008-02-19 14:59:35 +00005129 szCell = (u16*)&apCell[nMaxCells];
danielk1977634f2982005-03-28 08:44:07 +00005130 aCopy[0] = (u8*)&szCell[nMaxCells];
drh66e80082008-12-16 13:46:29 +00005131 assert( ((aCopy[0] - (u8*)0) & 7)==0 ); /* 8-byte alignment required */
danielk1977634f2982005-03-28 08:44:07 +00005132 for(i=1; i<NB; i++){
drhc96d8532005-05-03 12:30:33 +00005133 aCopy[i] = &aCopy[i-1][pBt->pageSize+ROUND8(sizeof(MemPage))];
drh66e80082008-12-16 13:46:29 +00005134 assert( ((aCopy[i] - (u8*)0) & 7)==0 ); /* 8-byte alignment required */
danielk1977634f2982005-03-28 08:44:07 +00005135 }
drhe5ae5732008-06-15 02:51:47 +00005136 aSpace1 = &aCopy[NB-1][pBt->pageSize+ROUND8(sizeof(MemPage))];
drh66e80082008-12-16 13:46:29 +00005137 assert( ((aSpace1 - (u8*)0) & 7)==0 ); /* 8-byte alignment required */
danielk197785d90ca2008-07-19 14:25:15 +00005138 if( ISAUTOVACUUM ){
drhe5ae5732008-06-15 02:51:47 +00005139 aFrom = &aSpace1[pBt->pageSize];
danielk1977634f2982005-03-28 08:44:07 +00005140 }
drhfacf0302008-06-17 15:12:00 +00005141 aSpace2 = sqlite3PageMalloc(pBt->pageSize);
drhe5ae5732008-06-15 02:51:47 +00005142 if( aSpace2==0 ){
5143 rc = SQLITE_NOMEM;
5144 goto balance_cleanup;
5145 }
danielk1977634f2982005-03-28 08:44:07 +00005146
5147 /*
drh14acc042001-06-10 19:56:58 +00005148 ** Make copies of the content of pPage and its siblings into aOld[].
5149 ** The rest of this function will use data from the copies rather
5150 ** that the original pages since the original pages will be in the
5151 ** process of being overwritten.
5152 */
5153 for(i=0; i<nOld; i++){
drhbf4bca52007-09-06 22:19:14 +00005154 MemPage *p = apCopy[i] = (MemPage*)aCopy[i];
5155 memcpy(p, apOld[i], sizeof(MemPage));
5156 p->aData = (void*)&p[1];
5157 memcpy(p->aData, apOld[i]->aData, pBt->pageSize);
drh14acc042001-06-10 19:56:58 +00005158 }
5159
5160 /*
5161 ** Load pointers to all cells on sibling pages and the divider cells
5162 ** into the local apCell[] array. Make copies of the divider cells
drhe5ae5732008-06-15 02:51:47 +00005163 ** into space obtained form aSpace1[] and remove the the divider Cells
drhb6f41482004-05-14 01:58:11 +00005164 ** from pParent.
drh4b70f112004-05-02 21:12:19 +00005165 **
5166 ** If the siblings are on leaf pages, then the child pointers of the
5167 ** divider cells are stripped from the cells before they are copied
drhe5ae5732008-06-15 02:51:47 +00005168 ** into aSpace1[]. In this way, all cells in apCell[] are without
drh4b70f112004-05-02 21:12:19 +00005169 ** child pointers. If siblings are not leaves, then all cell in
5170 ** apCell[] include child pointers. Either way, all cells in apCell[]
5171 ** are alike.
drh96f5b762004-05-16 16:24:36 +00005172 **
5173 ** leafCorrection: 4 if pPage is a leaf. 0 if pPage is not a leaf.
5174 ** leafData: 1 if pPage holds key+data and pParent holds only keys.
drh8b2f49b2001-06-08 00:21:52 +00005175 */
5176 nCell = 0;
drh4b70f112004-05-02 21:12:19 +00005177 leafCorrection = pPage->leaf*4;
drh44845222008-07-17 18:39:57 +00005178 leafData = pPage->hasData;
drh8b2f49b2001-06-08 00:21:52 +00005179 for(i=0; i<nOld; i++){
drh4b70f112004-05-02 21:12:19 +00005180 MemPage *pOld = apCopy[i];
drh43605152004-05-29 21:46:49 +00005181 int limit = pOld->nCell+pOld->nOverflow;
5182 for(j=0; j<limit; j++){
danielk1977634f2982005-03-28 08:44:07 +00005183 assert( nCell<nMaxCells );
drh43605152004-05-29 21:46:49 +00005184 apCell[nCell] = findOverflowCell(pOld, j);
5185 szCell[nCell] = cellSizePtr(pOld, apCell[nCell]);
danielk197785d90ca2008-07-19 14:25:15 +00005186 if( ISAUTOVACUUM ){
danielk1977ac11ee62005-01-15 12:45:51 +00005187 int a;
drhf49661a2008-12-10 16:45:50 +00005188 aFrom[nCell] = (u8)i; assert( i>=0 && i<6 );
danielk1977ac11ee62005-01-15 12:45:51 +00005189 for(a=0; a<pOld->nOverflow; a++){
5190 if( pOld->aOvfl[a].pCell==apCell[nCell] ){
5191 aFrom[nCell] = 0xFF;
5192 break;
5193 }
5194 }
5195 }
drh14acc042001-06-10 19:56:58 +00005196 nCell++;
drh8b2f49b2001-06-08 00:21:52 +00005197 }
5198 if( i<nOld-1 ){
drha9121e42008-02-19 14:59:35 +00005199 u16 sz = cellSizePtr(pParent, apDiv[i]);
drh8b18dd42004-05-12 19:18:15 +00005200 if( leafData ){
drh96f5b762004-05-16 16:24:36 +00005201 /* With the LEAFDATA flag, pParent cells hold only INTKEYs that
5202 ** are duplicates of keys on the child pages. We need to remove
5203 ** the divider cells from pParent, but the dividers cells are not
5204 ** added to apCell[] because they are duplicates of child cells.
5205 */
drh8b18dd42004-05-12 19:18:15 +00005206 dropCell(pParent, nxDiv, sz);
drh4b70f112004-05-02 21:12:19 +00005207 }else{
drhb6f41482004-05-14 01:58:11 +00005208 u8 *pTemp;
danielk1977634f2982005-03-28 08:44:07 +00005209 assert( nCell<nMaxCells );
drhb6f41482004-05-14 01:58:11 +00005210 szCell[nCell] = sz;
drhe5ae5732008-06-15 02:51:47 +00005211 pTemp = &aSpace1[iSpace1];
5212 iSpace1 += sz;
5213 assert( sz<=pBt->pageSize/4 );
5214 assert( iSpace1<=pBt->pageSize );
drhb6f41482004-05-14 01:58:11 +00005215 memcpy(pTemp, apDiv[i], sz);
5216 apCell[nCell] = pTemp+leafCorrection;
danielk197785d90ca2008-07-19 14:25:15 +00005217 if( ISAUTOVACUUM ){
danielk1977ac11ee62005-01-15 12:45:51 +00005218 aFrom[nCell] = 0xFF;
5219 }
drhb6f41482004-05-14 01:58:11 +00005220 dropCell(pParent, nxDiv, sz);
drhf49661a2008-12-10 16:45:50 +00005221 assert( leafCorrection==0 || leafCorrection==4 );
5222 szCell[nCell] -= (u16)leafCorrection;
drh43605152004-05-29 21:46:49 +00005223 assert( get4byte(pTemp)==pgnoOld[i] );
drh8b18dd42004-05-12 19:18:15 +00005224 if( !pOld->leaf ){
5225 assert( leafCorrection==0 );
5226 /* The right pointer of the child page pOld becomes the left
5227 ** pointer of the divider cell */
drh43605152004-05-29 21:46:49 +00005228 memcpy(apCell[nCell], &pOld->aData[pOld->hdrOffset+8], 4);
drh8b18dd42004-05-12 19:18:15 +00005229 }else{
5230 assert( leafCorrection==4 );
danielk197739c96042007-05-12 10:41:47 +00005231 if( szCell[nCell]<4 ){
5232 /* Do not allow any cells smaller than 4 bytes. */
5233 szCell[nCell] = 4;
5234 }
drh8b18dd42004-05-12 19:18:15 +00005235 }
5236 nCell++;
drh4b70f112004-05-02 21:12:19 +00005237 }
drh8b2f49b2001-06-08 00:21:52 +00005238 }
5239 }
5240
5241 /*
drh6019e162001-07-02 17:51:45 +00005242 ** Figure out the number of pages needed to hold all nCell cells.
5243 ** Store this number in "k". Also compute szNew[] which is the total
5244 ** size of all cells on the i-th page and cntNew[] which is the index
drh4b70f112004-05-02 21:12:19 +00005245 ** in apCell[] of the cell that divides page i from page i+1.
drh6019e162001-07-02 17:51:45 +00005246 ** cntNew[k] should equal nCell.
5247 **
drh96f5b762004-05-16 16:24:36 +00005248 ** Values computed by this block:
5249 **
5250 ** k: The total number of sibling pages
5251 ** szNew[i]: Spaced used on the i-th sibling page.
5252 ** cntNew[i]: Index in apCell[] and szCell[] for the first cell to
5253 ** the right of the i-th sibling page.
5254 ** usableSpace: Number of bytes of space available on each sibling.
5255 **
drh8b2f49b2001-06-08 00:21:52 +00005256 */
drh43605152004-05-29 21:46:49 +00005257 usableSpace = pBt->usableSize - 12 + leafCorrection;
drh6019e162001-07-02 17:51:45 +00005258 for(subtotal=k=i=0; i<nCell; i++){
danielk1977634f2982005-03-28 08:44:07 +00005259 assert( i<nMaxCells );
drh43605152004-05-29 21:46:49 +00005260 subtotal += szCell[i] + 2;
drh4b70f112004-05-02 21:12:19 +00005261 if( subtotal > usableSpace ){
drh6019e162001-07-02 17:51:45 +00005262 szNew[k] = subtotal - szCell[i];
5263 cntNew[k] = i;
drh8b18dd42004-05-12 19:18:15 +00005264 if( leafData ){ i--; }
drh6019e162001-07-02 17:51:45 +00005265 subtotal = 0;
5266 k++;
5267 }
5268 }
5269 szNew[k] = subtotal;
5270 cntNew[k] = nCell;
5271 k++;
drh96f5b762004-05-16 16:24:36 +00005272
5273 /*
5274 ** The packing computed by the previous block is biased toward the siblings
5275 ** on the left side. The left siblings are always nearly full, while the
5276 ** right-most sibling might be nearly empty. This block of code attempts
5277 ** to adjust the packing of siblings to get a better balance.
5278 **
5279 ** This adjustment is more than an optimization. The packing above might
5280 ** be so out of balance as to be illegal. For example, the right-most
5281 ** sibling might be completely empty. This adjustment is not optional.
5282 */
drh6019e162001-07-02 17:51:45 +00005283 for(i=k-1; i>0; i--){
drh96f5b762004-05-16 16:24:36 +00005284 int szRight = szNew[i]; /* Size of sibling on the right */
5285 int szLeft = szNew[i-1]; /* Size of sibling on the left */
5286 int r; /* Index of right-most cell in left sibling */
5287 int d; /* Index of first cell to the left of right sibling */
5288
5289 r = cntNew[i-1] - 1;
5290 d = r + 1 - leafData;
danielk1977634f2982005-03-28 08:44:07 +00005291 assert( d<nMaxCells );
5292 assert( r<nMaxCells );
drh43605152004-05-29 21:46:49 +00005293 while( szRight==0 || szRight+szCell[d]+2<=szLeft-(szCell[r]+2) ){
5294 szRight += szCell[d] + 2;
5295 szLeft -= szCell[r] + 2;
drh6019e162001-07-02 17:51:45 +00005296 cntNew[i-1]--;
drh96f5b762004-05-16 16:24:36 +00005297 r = cntNew[i-1] - 1;
5298 d = r + 1 - leafData;
drh6019e162001-07-02 17:51:45 +00005299 }
drh96f5b762004-05-16 16:24:36 +00005300 szNew[i] = szRight;
5301 szNew[i-1] = szLeft;
drh6019e162001-07-02 17:51:45 +00005302 }
drh09d0deb2005-08-02 17:13:09 +00005303
5304 /* Either we found one or more cells (cntnew[0])>0) or we are the
5305 ** a virtual root page. A virtual root page is when the real root
5306 ** page is page 1 and we are the only child of that page.
5307 */
5308 assert( cntNew[0]>0 || (pParent->pgno==1 && pParent->nCell==0) );
drh8b2f49b2001-06-08 00:21:52 +00005309
5310 /*
drh6b308672002-07-08 02:16:37 +00005311 ** Allocate k new pages. Reuse old pages where possible.
drh8b2f49b2001-06-08 00:21:52 +00005312 */
drh4b70f112004-05-02 21:12:19 +00005313 assert( pPage->pgno>1 );
5314 pageFlags = pPage->aData[0];
drh14acc042001-06-10 19:56:58 +00005315 for(i=0; i<k; i++){
drhda200cc2004-05-09 11:51:38 +00005316 MemPage *pNew;
drh6b308672002-07-08 02:16:37 +00005317 if( i<nOld ){
drhda200cc2004-05-09 11:51:38 +00005318 pNew = apNew[i] = apOld[i];
drh6b308672002-07-08 02:16:37 +00005319 pgnoNew[i] = pgnoOld[i];
5320 apOld[i] = 0;
danielk19773b8a05f2007-03-19 17:44:26 +00005321 rc = sqlite3PagerWrite(pNew->pDbPage);
drhf5345442007-04-09 12:45:02 +00005322 nNew++;
danielk197728129562005-01-11 10:25:06 +00005323 if( rc ) goto balance_cleanup;
drh6b308672002-07-08 02:16:37 +00005324 }else{
drh7aa8f852006-03-28 00:24:44 +00005325 assert( i>0 );
drh4f0c5872007-03-26 22:05:01 +00005326 rc = allocateBtreePage(pBt, &pNew, &pgnoNew[i], pgnoNew[i-1], 0);
drh6b308672002-07-08 02:16:37 +00005327 if( rc ) goto balance_cleanup;
drhda200cc2004-05-09 11:51:38 +00005328 apNew[i] = pNew;
drhf5345442007-04-09 12:45:02 +00005329 nNew++;
drh6b308672002-07-08 02:16:37 +00005330 }
drh8b2f49b2001-06-08 00:21:52 +00005331 }
5332
danielk1977299b1872004-11-22 10:02:10 +00005333 /* Free any old pages that were not reused as new pages.
5334 */
5335 while( i<nOld ){
5336 rc = freePage(apOld[i]);
5337 if( rc ) goto balance_cleanup;
5338 releasePage(apOld[i]);
5339 apOld[i] = 0;
5340 i++;
5341 }
5342
drh8b2f49b2001-06-08 00:21:52 +00005343 /*
drhf9ffac92002-03-02 19:00:31 +00005344 ** Put the new pages in accending order. This helps to
5345 ** keep entries in the disk file in order so that a scan
5346 ** of the table is a linear scan through the file. That
5347 ** in turn helps the operating system to deliver pages
5348 ** from the disk more rapidly.
5349 **
5350 ** An O(n^2) insertion sort algorithm is used, but since
drhc3b70572003-01-04 19:44:07 +00005351 ** n is never more than NB (a small constant), that should
5352 ** not be a problem.
drhf9ffac92002-03-02 19:00:31 +00005353 **
drhc3b70572003-01-04 19:44:07 +00005354 ** When NB==3, this one optimization makes the database
5355 ** about 25% faster for large insertions and deletions.
drhf9ffac92002-03-02 19:00:31 +00005356 */
5357 for(i=0; i<k-1; i++){
5358 int minV = pgnoNew[i];
5359 int minI = i;
5360 for(j=i+1; j<k; j++){
drh7d02cb72003-06-04 16:24:39 +00005361 if( pgnoNew[j]<(unsigned)minV ){
drhf9ffac92002-03-02 19:00:31 +00005362 minI = j;
5363 minV = pgnoNew[j];
5364 }
5365 }
5366 if( minI>i ){
5367 int t;
5368 MemPage *pT;
5369 t = pgnoNew[i];
5370 pT = apNew[i];
5371 pgnoNew[i] = pgnoNew[minI];
5372 apNew[i] = apNew[minI];
5373 pgnoNew[minI] = t;
5374 apNew[minI] = pT;
5375 }
5376 }
drha2fce642004-06-05 00:01:44 +00005377 TRACE(("BALANCE: old: %d %d %d new: %d(%d) %d(%d) %d(%d) %d(%d) %d(%d)\n",
drh24cd67e2004-05-10 16:18:47 +00005378 pgnoOld[0],
5379 nOld>=2 ? pgnoOld[1] : 0,
5380 nOld>=3 ? pgnoOld[2] : 0,
drh10c0fa62004-05-18 12:50:17 +00005381 pgnoNew[0], szNew[0],
5382 nNew>=2 ? pgnoNew[1] : 0, nNew>=2 ? szNew[1] : 0,
5383 nNew>=3 ? pgnoNew[2] : 0, nNew>=3 ? szNew[2] : 0,
drha2fce642004-06-05 00:01:44 +00005384 nNew>=4 ? pgnoNew[3] : 0, nNew>=4 ? szNew[3] : 0,
5385 nNew>=5 ? pgnoNew[4] : 0, nNew>=5 ? szNew[4] : 0));
drh24cd67e2004-05-10 16:18:47 +00005386
drhf9ffac92002-03-02 19:00:31 +00005387 /*
drh14acc042001-06-10 19:56:58 +00005388 ** Evenly distribute the data in apCell[] across the new pages.
5389 ** Insert divider cells into pParent as necessary.
5390 */
5391 j = 0;
5392 for(i=0; i<nNew; i++){
danielk1977ac11ee62005-01-15 12:45:51 +00005393 /* Assemble the new sibling page. */
drh14acc042001-06-10 19:56:58 +00005394 MemPage *pNew = apNew[i];
drh19642e52005-03-29 13:17:45 +00005395 assert( j<nMaxCells );
drh4b70f112004-05-02 21:12:19 +00005396 assert( pNew->pgno==pgnoNew[i] );
drh10131482008-07-11 03:34:09 +00005397 zeroPage(pNew, pageFlags);
drhfa1a98a2004-05-14 19:08:17 +00005398 assemblePage(pNew, cntNew[i]-j, &apCell[j], &szCell[j]);
drh09d0deb2005-08-02 17:13:09 +00005399 assert( pNew->nCell>0 || (nNew==1 && cntNew[0]==0) );
drh43605152004-05-29 21:46:49 +00005400 assert( pNew->nOverflow==0 );
danielk1977ac11ee62005-01-15 12:45:51 +00005401
danielk1977ac11ee62005-01-15 12:45:51 +00005402 /* If this is an auto-vacuum database, update the pointer map entries
5403 ** that point to the siblings that were rearranged. These can be: left
5404 ** children of cells, the right-child of the page, or overflow pages
5405 ** pointed to by cells.
5406 */
danielk197785d90ca2008-07-19 14:25:15 +00005407 if( ISAUTOVACUUM ){
danielk1977ac11ee62005-01-15 12:45:51 +00005408 for(k=j; k<cntNew[i]; k++){
danielk1977634f2982005-03-28 08:44:07 +00005409 assert( k<nMaxCells );
danielk1977ac11ee62005-01-15 12:45:51 +00005410 if( aFrom[k]==0xFF || apCopy[aFrom[k]]->pgno!=pNew->pgno ){
danielk197779a40da2005-01-16 08:00:01 +00005411 rc = ptrmapPutOvfl(pNew, k-j);
danielk197787c52b52008-07-19 11:49:07 +00005412 if( rc==SQLITE_OK && leafCorrection==0 ){
5413 rc = ptrmapPut(pBt, get4byte(apCell[k]), PTRMAP_BTREE, pNew->pgno);
5414 }
danielk197779a40da2005-01-16 08:00:01 +00005415 if( rc!=SQLITE_OK ){
5416 goto balance_cleanup;
danielk1977ac11ee62005-01-15 12:45:51 +00005417 }
5418 }
5419 }
5420 }
danielk1977ac11ee62005-01-15 12:45:51 +00005421
5422 j = cntNew[i];
5423
5424 /* If the sibling page assembled above was not the right-most sibling,
5425 ** insert a divider cell into the parent page.
5426 */
drh14acc042001-06-10 19:56:58 +00005427 if( i<nNew-1 && j<nCell ){
drh8b18dd42004-05-12 19:18:15 +00005428 u8 *pCell;
drh24cd67e2004-05-10 16:18:47 +00005429 u8 *pTemp;
drh8b18dd42004-05-12 19:18:15 +00005430 int sz;
danielk1977634f2982005-03-28 08:44:07 +00005431
5432 assert( j<nMaxCells );
drh8b18dd42004-05-12 19:18:15 +00005433 pCell = apCell[j];
5434 sz = szCell[j] + leafCorrection;
drhe5ae5732008-06-15 02:51:47 +00005435 pTemp = &aSpace2[iSpace2];
drh4b70f112004-05-02 21:12:19 +00005436 if( !pNew->leaf ){
drh43605152004-05-29 21:46:49 +00005437 memcpy(&pNew->aData[8], pCell, 4);
danielk197785d90ca2008-07-19 14:25:15 +00005438 if( ISAUTOVACUUM
danielk197787c52b52008-07-19 11:49:07 +00005439 && (aFrom[j]==0xFF || apCopy[aFrom[j]]->pgno!=pNew->pgno)
5440 ){
5441 rc = ptrmapPut(pBt, get4byte(pCell), PTRMAP_BTREE, pNew->pgno);
5442 if( rc!=SQLITE_OK ){
5443 goto balance_cleanup;
5444 }
5445 }
drh8b18dd42004-05-12 19:18:15 +00005446 }else if( leafData ){
drhfd131da2007-08-07 17:13:03 +00005447 /* If the tree is a leaf-data tree, and the siblings are leaves,
danielk1977ac11ee62005-01-15 12:45:51 +00005448 ** then there is no divider cell in apCell[]. Instead, the divider
5449 ** cell consists of the integer key for the right-most cell of
5450 ** the sibling-page assembled above only.
5451 */
drh6f11bef2004-05-13 01:12:56 +00005452 CellInfo info;
drh8b18dd42004-05-12 19:18:15 +00005453 j--;
drh16a9b832007-05-05 18:39:25 +00005454 sqlite3BtreeParseCellPtr(pNew, apCell[j], &info);
drhe5ae5732008-06-15 02:51:47 +00005455 pCell = pTemp;
drhb026e052007-05-02 01:34:31 +00005456 fillInCell(pParent, pCell, 0, info.nKey, 0, 0, 0, &sz);
drh8b18dd42004-05-12 19:18:15 +00005457 pTemp = 0;
drh4b70f112004-05-02 21:12:19 +00005458 }else{
5459 pCell -= 4;
danielk19774aeff622007-05-12 09:30:47 +00005460 /* Obscure case for non-leaf-data trees: If the cell at pCell was
drh85b623f2007-12-13 21:54:09 +00005461 ** previously stored on a leaf node, and its reported size was 4
danielk19774aeff622007-05-12 09:30:47 +00005462 ** bytes, then it may actually be smaller than this
5463 ** (see sqlite3BtreeParseCellPtr(), 4 bytes is the minimum size of
drh85b623f2007-12-13 21:54:09 +00005464 ** any cell). But it is important to pass the correct size to
danielk19774aeff622007-05-12 09:30:47 +00005465 ** insertCell(), so reparse the cell now.
5466 **
5467 ** Note that this can never happen in an SQLite data file, as all
5468 ** cells are at least 4 bytes. It only happens in b-trees used
5469 ** to evaluate "IN (SELECT ...)" and similar clauses.
5470 */
5471 if( szCell[j]==4 ){
5472 assert(leafCorrection==4);
5473 sz = cellSizePtr(pParent, pCell);
5474 }
drh4b70f112004-05-02 21:12:19 +00005475 }
drhe5ae5732008-06-15 02:51:47 +00005476 iSpace2 += sz;
5477 assert( sz<=pBt->pageSize/4 );
5478 assert( iSpace2<=pBt->pageSize );
danielk1977a3ad5e72005-01-07 08:56:44 +00005479 rc = insertCell(pParent, nxDiv, pCell, sz, pTemp, 4);
danielk1977e80463b2004-11-03 03:01:16 +00005480 if( rc!=SQLITE_OK ) goto balance_cleanup;
drhc5053fb2008-11-27 02:22:10 +00005481 assert( sqlite3PagerIswriteable(pParent->pDbPage) );
drh43605152004-05-29 21:46:49 +00005482 put4byte(findOverflowCell(pParent,nxDiv), pNew->pgno);
danielk197785d90ca2008-07-19 14:25:15 +00005483
danielk1977ac11ee62005-01-15 12:45:51 +00005484 /* If this is an auto-vacuum database, and not a leaf-data tree,
5485 ** then update the pointer map with an entry for the overflow page
5486 ** that the cell just inserted points to (if any).
5487 */
danielk197785d90ca2008-07-19 14:25:15 +00005488 if( ISAUTOVACUUM && !leafData ){
danielk197779a40da2005-01-16 08:00:01 +00005489 rc = ptrmapPutOvfl(pParent, nxDiv);
5490 if( rc!=SQLITE_OK ){
5491 goto balance_cleanup;
danielk1977ac11ee62005-01-15 12:45:51 +00005492 }
5493 }
drh14acc042001-06-10 19:56:58 +00005494 j++;
5495 nxDiv++;
5496 }
danielk197787c52b52008-07-19 11:49:07 +00005497
danielk197787c52b52008-07-19 11:49:07 +00005498 /* Set the pointer-map entry for the new sibling page. */
danielk197785d90ca2008-07-19 14:25:15 +00005499 if( ISAUTOVACUUM ){
danielk197787c52b52008-07-19 11:49:07 +00005500 rc = ptrmapPut(pBt, pNew->pgno, PTRMAP_BTREE, pParent->pgno);
5501 if( rc!=SQLITE_OK ){
5502 goto balance_cleanup;
5503 }
5504 }
drh14acc042001-06-10 19:56:58 +00005505 }
drh6019e162001-07-02 17:51:45 +00005506 assert( j==nCell );
drh7aa8f852006-03-28 00:24:44 +00005507 assert( nOld>0 );
5508 assert( nNew>0 );
drh4b70f112004-05-02 21:12:19 +00005509 if( (pageFlags & PTF_LEAF)==0 ){
danielk197787c52b52008-07-19 11:49:07 +00005510 u8 *zChild = &apCopy[nOld-1]->aData[8];
5511 memcpy(&apNew[nNew-1]->aData[8], zChild, 4);
danielk197785d90ca2008-07-19 14:25:15 +00005512 if( ISAUTOVACUUM ){
danielk197787c52b52008-07-19 11:49:07 +00005513 rc = ptrmapPut(pBt, get4byte(zChild), PTRMAP_BTREE, apNew[nNew-1]->pgno);
5514 if( rc!=SQLITE_OK ){
5515 goto balance_cleanup;
5516 }
5517 }
drh14acc042001-06-10 19:56:58 +00005518 }
drhc5053fb2008-11-27 02:22:10 +00005519 assert( sqlite3PagerIswriteable(pParent->pDbPage) );
drh43605152004-05-29 21:46:49 +00005520 if( nxDiv==pParent->nCell+pParent->nOverflow ){
drh4b70f112004-05-02 21:12:19 +00005521 /* Right-most sibling is the right-most child of pParent */
drh43605152004-05-29 21:46:49 +00005522 put4byte(&pParent->aData[pParent->hdrOffset+8], pgnoNew[nNew-1]);
drh4b70f112004-05-02 21:12:19 +00005523 }else{
5524 /* Right-most sibling is the left child of the first entry in pParent
5525 ** past the right-most divider entry */
drh43605152004-05-29 21:46:49 +00005526 put4byte(findOverflowCell(pParent, nxDiv), pgnoNew[nNew-1]);
drh14acc042001-06-10 19:56:58 +00005527 }
5528
5529 /*
drh3a4c1412004-05-09 20:40:11 +00005530 ** Balance the parent page. Note that the current page (pPage) might
danielk1977ac11ee62005-01-15 12:45:51 +00005531 ** have been added to the freelist so it might no longer be initialized.
drh3a4c1412004-05-09 20:40:11 +00005532 ** But the parent page will always be initialized.
drh8b2f49b2001-06-08 00:21:52 +00005533 */
danielk197771d5d2c2008-09-29 11:49:47 +00005534 assert( pParent->isInit );
drhfacf0302008-06-17 15:12:00 +00005535 sqlite3ScratchFree(apCell);
drhe5ae5732008-06-15 02:51:47 +00005536 apCell = 0;
danielk1977a4124bd2008-12-23 10:37:47 +00005537 TRACE(("BALANCE: finished with %d: old=%d new=%d cells=%d\n",
5538 pPage->pgno, nOld, nNew, nCell));
5539 pPage->nOverflow = 0;
danielk197771d5d2c2008-09-29 11:49:47 +00005540 releasePage(pPage);
5541 pCur->iPage--;
5542 rc = balance(pCur, 0);
drhda200cc2004-05-09 11:51:38 +00005543
drh8b2f49b2001-06-08 00:21:52 +00005544 /*
drh14acc042001-06-10 19:56:58 +00005545 ** Cleanup before returning.
drh8b2f49b2001-06-08 00:21:52 +00005546 */
drh14acc042001-06-10 19:56:58 +00005547balance_cleanup:
drhfacf0302008-06-17 15:12:00 +00005548 sqlite3PageFree(aSpace2);
5549 sqlite3ScratchFree(apCell);
drh8b2f49b2001-06-08 00:21:52 +00005550 for(i=0; i<nOld; i++){
drh91025292004-05-03 19:49:32 +00005551 releasePage(apOld[i]);
drh8b2f49b2001-06-08 00:21:52 +00005552 }
drh14acc042001-06-10 19:56:58 +00005553 for(i=0; i<nNew; i++){
drh91025292004-05-03 19:49:32 +00005554 releasePage(apNew[i]);
drh8b2f49b2001-06-08 00:21:52 +00005555 }
danielk1977a4124bd2008-12-23 10:37:47 +00005556 pCur->apPage[pCur->iPage]->nOverflow = 0;
danielk1977eaa06f62008-09-18 17:34:44 +00005557
drh8b2f49b2001-06-08 00:21:52 +00005558 return rc;
5559}
5560
5561/*
drh43605152004-05-29 21:46:49 +00005562** This routine is called for the root page of a btree when the root
5563** page contains no cells. This is an opportunity to make the tree
5564** shallower by one level.
5565*/
danielk197771d5d2c2008-09-29 11:49:47 +00005566static int balance_shallower(BtCursor *pCur){
5567 MemPage *pPage; /* Root page of B-Tree */
drh43605152004-05-29 21:46:49 +00005568 MemPage *pChild; /* The only child page of pPage */
5569 Pgno pgnoChild; /* Page number for pChild */
drh2e38c322004-09-03 18:38:44 +00005570 int rc = SQLITE_OK; /* Return code from subprocedures */
danielk1977aef0bf62005-12-30 16:28:01 +00005571 BtShared *pBt; /* The main BTree structure */
drh2e38c322004-09-03 18:38:44 +00005572 int mxCellPerPage; /* Maximum number of cells per page */
5573 u8 **apCell; /* All cells from pages being balanced */
drha9121e42008-02-19 14:59:35 +00005574 u16 *szCell; /* Local size of all cells */
drh43605152004-05-29 21:46:49 +00005575
danielk197771d5d2c2008-09-29 11:49:47 +00005576 assert( pCur->iPage==0 );
5577 pPage = pCur->apPage[0];
5578
drh43605152004-05-29 21:46:49 +00005579 assert( pPage->nCell==0 );
drh1fee73e2007-08-29 04:00:57 +00005580 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
drh2e38c322004-09-03 18:38:44 +00005581 pBt = pPage->pBt;
5582 mxCellPerPage = MX_CELL(pBt);
drhe5ae5732008-06-15 02:51:47 +00005583 apCell = sqlite3Malloc( mxCellPerPage*(sizeof(u8*)+sizeof(u16)) );
drh2e38c322004-09-03 18:38:44 +00005584 if( apCell==0 ) return SQLITE_NOMEM;
drha9121e42008-02-19 14:59:35 +00005585 szCell = (u16*)&apCell[mxCellPerPage];
drh43605152004-05-29 21:46:49 +00005586 if( pPage->leaf ){
5587 /* The table is completely empty */
5588 TRACE(("BALANCE: empty table %d\n", pPage->pgno));
5589 }else{
5590 /* The root page is empty but has one child. Transfer the
5591 ** information from that one child into the root page if it
5592 ** will fit. This reduces the depth of the tree by one.
5593 **
5594 ** If the root page is page 1, it has less space available than
5595 ** its child (due to the 100 byte header that occurs at the beginning
5596 ** of the database fle), so it might not be able to hold all of the
5597 ** information currently contained in the child. If this is the
5598 ** case, then do not do the transfer. Leave page 1 empty except
5599 ** for the right-pointer to the child page. The child page becomes
5600 ** the virtual root of the tree.
5601 */
drhf94a1732008-09-30 17:18:17 +00005602 VVA_ONLY( pCur->pagesShuffled = 1 );
drh43605152004-05-29 21:46:49 +00005603 pgnoChild = get4byte(&pPage->aData[pPage->hdrOffset+8]);
5604 assert( pgnoChild>0 );
danielk197789d40042008-11-17 14:20:56 +00005605 assert( pgnoChild<=pagerPagecount(pPage->pBt) );
drh16a9b832007-05-05 18:39:25 +00005606 rc = sqlite3BtreeGetPage(pPage->pBt, pgnoChild, &pChild, 0);
drh2e38c322004-09-03 18:38:44 +00005607 if( rc ) goto end_shallow_balance;
drh43605152004-05-29 21:46:49 +00005608 if( pPage->pgno==1 ){
danielk197771d5d2c2008-09-29 11:49:47 +00005609 rc = sqlite3BtreeInitPage(pChild);
drh2e38c322004-09-03 18:38:44 +00005610 if( rc ) goto end_shallow_balance;
drh43605152004-05-29 21:46:49 +00005611 assert( pChild->nOverflow==0 );
5612 if( pChild->nFree>=100 ){
5613 /* The child information will fit on the root page, so do the
5614 ** copy */
5615 int i;
5616 zeroPage(pPage, pChild->aData[0]);
5617 for(i=0; i<pChild->nCell; i++){
danielk19771cc5ed82007-05-16 17:28:43 +00005618 apCell[i] = findCell(pChild,i);
drh43605152004-05-29 21:46:49 +00005619 szCell[i] = cellSizePtr(pChild, apCell[i]);
5620 }
5621 assemblePage(pPage, pChild->nCell, apCell, szCell);
danielk1977ae825582004-11-23 09:06:55 +00005622 /* Copy the right-pointer of the child to the parent. */
drhc5053fb2008-11-27 02:22:10 +00005623 assert( sqlite3PagerIswriteable(pPage->pDbPage) );
danielk1977ae825582004-11-23 09:06:55 +00005624 put4byte(&pPage->aData[pPage->hdrOffset+8],
5625 get4byte(&pChild->aData[pChild->hdrOffset+8]));
drh9bf9e9c2008-12-05 20:01:43 +00005626 rc = freePage(pChild);
drh43605152004-05-29 21:46:49 +00005627 TRACE(("BALANCE: child %d transfer to page 1\n", pChild->pgno));
5628 }else{
5629 /* The child has more information that will fit on the root.
5630 ** The tree is already balanced. Do nothing. */
5631 TRACE(("BALANCE: child %d will not fit on page 1\n", pChild->pgno));
5632 }
5633 }else{
5634 memcpy(pPage->aData, pChild->aData, pPage->pBt->usableSize);
5635 pPage->isInit = 0;
danielk197771d5d2c2008-09-29 11:49:47 +00005636 rc = sqlite3BtreeInitPage(pPage);
drh43605152004-05-29 21:46:49 +00005637 assert( rc==SQLITE_OK );
5638 freePage(pChild);
5639 TRACE(("BALANCE: transfer child %d into root %d\n",
5640 pChild->pgno, pPage->pgno));
5641 }
danielk1977ac11ee62005-01-15 12:45:51 +00005642 assert( pPage->nOverflow==0 );
shane831c3292008-11-10 17:14:58 +00005643#ifndef SQLITE_OMIT_AUTOVACUUM
drh9bf9e9c2008-12-05 20:01:43 +00005644 if( ISAUTOVACUUM && rc==SQLITE_OK ){
danielk197700a696d2008-09-29 16:41:31 +00005645 rc = setChildPtrmaps(pPage);
danielk1977ac11ee62005-01-15 12:45:51 +00005646 }
shane831c3292008-11-10 17:14:58 +00005647#endif
drh43605152004-05-29 21:46:49 +00005648 releasePage(pChild);
5649 }
drh2e38c322004-09-03 18:38:44 +00005650end_shallow_balance:
drh17435752007-08-16 04:30:38 +00005651 sqlite3_free(apCell);
drh2e38c322004-09-03 18:38:44 +00005652 return rc;
drh43605152004-05-29 21:46:49 +00005653}
5654
5655
5656/*
5657** The root page is overfull
5658**
5659** When this happens, Create a new child page and copy the
5660** contents of the root into the child. Then make the root
5661** page an empty page with rightChild pointing to the new
5662** child. Finally, call balance_internal() on the new child
5663** to cause it to split.
5664*/
danielk197771d5d2c2008-09-29 11:49:47 +00005665static int balance_deeper(BtCursor *pCur){
drh43605152004-05-29 21:46:49 +00005666 int rc; /* Return value from subprocedures */
danielk197771d5d2c2008-09-29 11:49:47 +00005667 MemPage *pPage; /* Pointer to the root page */
drh43605152004-05-29 21:46:49 +00005668 MemPage *pChild; /* Pointer to a new child page */
5669 Pgno pgnoChild; /* Page number of the new child page */
danielk1977aef0bf62005-12-30 16:28:01 +00005670 BtShared *pBt; /* The BTree */
drh43605152004-05-29 21:46:49 +00005671 int usableSize; /* Total usable size of a page */
5672 u8 *data; /* Content of the parent page */
5673 u8 *cdata; /* Content of the child page */
5674 int hdr; /* Offset to page header in parent */
drh281b21d2008-08-22 12:57:08 +00005675 int cbrk; /* Offset to content of first cell in parent */
drh43605152004-05-29 21:46:49 +00005676
danielk197771d5d2c2008-09-29 11:49:47 +00005677 assert( pCur->iPage==0 );
5678 assert( pCur->apPage[0]->nOverflow>0 );
5679
drhf94a1732008-09-30 17:18:17 +00005680 VVA_ONLY( pCur->pagesShuffled = 1 );
danielk197771d5d2c2008-09-29 11:49:47 +00005681 pPage = pCur->apPage[0];
drh43605152004-05-29 21:46:49 +00005682 pBt = pPage->pBt;
drh1fee73e2007-08-29 04:00:57 +00005683 assert( sqlite3_mutex_held(pBt->mutex) );
drhc5053fb2008-11-27 02:22:10 +00005684 assert( sqlite3PagerIswriteable(pPage->pDbPage) );
drh4f0c5872007-03-26 22:05:01 +00005685 rc = allocateBtreePage(pBt, &pChild, &pgnoChild, pPage->pgno, 0);
drh43605152004-05-29 21:46:49 +00005686 if( rc ) return rc;
danielk19773b8a05f2007-03-19 17:44:26 +00005687 assert( sqlite3PagerIswriteable(pChild->pDbPage) );
drh43605152004-05-29 21:46:49 +00005688 usableSize = pBt->usableSize;
5689 data = pPage->aData;
5690 hdr = pPage->hdrOffset;
drh281b21d2008-08-22 12:57:08 +00005691 cbrk = get2byte(&data[hdr+5]);
drh43605152004-05-29 21:46:49 +00005692 cdata = pChild->aData;
5693 memcpy(cdata, &data[hdr], pPage->cellOffset+2*pPage->nCell-hdr);
drh281b21d2008-08-22 12:57:08 +00005694 memcpy(&cdata[cbrk], &data[cbrk], usableSize-cbrk);
danielk1977bc2ca9e2008-11-13 14:28:28 +00005695
5696 assert( pChild->isInit==0 );
danielk197771d5d2c2008-09-29 11:49:47 +00005697 rc = sqlite3BtreeInitPage(pChild);
5698 if( rc==SQLITE_OK ){
5699 int nCopy = pPage->nOverflow*sizeof(pPage->aOvfl[0]);
5700 memcpy(pChild->aOvfl, pPage->aOvfl, nCopy);
5701 pChild->nOverflow = pPage->nOverflow;
5702 if( pChild->nOverflow ){
5703 pChild->nFree = 0;
5704 }
5705 assert( pChild->nCell==pPage->nCell );
drhc5053fb2008-11-27 02:22:10 +00005706 assert( sqlite3PagerIswriteable(pPage->pDbPage) );
danielk197771d5d2c2008-09-29 11:49:47 +00005707 zeroPage(pPage, pChild->aData[0] & ~PTF_LEAF);
5708 put4byte(&pPage->aData[pPage->hdrOffset+8], pgnoChild);
5709 TRACE(("BALANCE: copy root %d into %d\n", pPage->pgno, pChild->pgno));
5710 if( ISAUTOVACUUM ){
danielk197771d5d2c2008-09-29 11:49:47 +00005711 rc = ptrmapPut(pBt, pChild->pgno, PTRMAP_BTREE, pPage->pgno);
shane831c3292008-11-10 17:14:58 +00005712#ifndef SQLITE_OMIT_AUTOVACUUM
danielk197771d5d2c2008-09-29 11:49:47 +00005713 if( rc==SQLITE_OK ){
danielk197700a696d2008-09-29 16:41:31 +00005714 rc = setChildPtrmaps(pChild);
danielk1977ac11ee62005-01-15 12:45:51 +00005715 }
drh30df0092008-12-23 15:58:06 +00005716 if( rc ){
5717 pChild->nOverflow = 0;
5718 }
shane831c3292008-11-10 17:14:58 +00005719#endif
danielk1977ac11ee62005-01-15 12:45:51 +00005720 }
danielk197787c52b52008-07-19 11:49:07 +00005721 }
danielk19776b456a22005-03-21 04:04:02 +00005722
danielk197771d5d2c2008-09-29 11:49:47 +00005723 if( rc==SQLITE_OK ){
5724 pCur->iPage++;
5725 pCur->apPage[1] = pChild;
danielk1977bf93c562008-09-29 15:53:25 +00005726 pCur->aiIdx[0] = 0;
danielk197771d5d2c2008-09-29 11:49:47 +00005727 rc = balance_nonroot(pCur);
5728 }else{
5729 releasePage(pChild);
5730 }
5731
drh43605152004-05-29 21:46:49 +00005732 return rc;
5733}
5734
5735/*
danielk197771d5d2c2008-09-29 11:49:47 +00005736** The page that pCur currently points to has just been modified in
5737** some way. This function figures out if this modification means the
5738** tree needs to be balanced, and if so calls the appropriate balancing
5739** routine.
5740**
5741** Parameter isInsert is true if a new cell was just inserted into the
5742** page, or false otherwise.
drh43605152004-05-29 21:46:49 +00005743*/
danielk197771d5d2c2008-09-29 11:49:47 +00005744static int balance(BtCursor *pCur, int isInsert){
drh43605152004-05-29 21:46:49 +00005745 int rc = SQLITE_OK;
danielk197771d5d2c2008-09-29 11:49:47 +00005746 MemPage *pPage = pCur->apPage[pCur->iPage];
5747
drh1fee73e2007-08-29 04:00:57 +00005748 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
danielk197771d5d2c2008-09-29 11:49:47 +00005749 if( pCur->iPage==0 ){
danielk19776e465eb2007-08-21 13:11:00 +00005750 rc = sqlite3PagerWrite(pPage->pDbPage);
5751 if( rc==SQLITE_OK && pPage->nOverflow>0 ){
danielk197771d5d2c2008-09-29 11:49:47 +00005752 rc = balance_deeper(pCur);
danielk1977a4124bd2008-12-23 10:37:47 +00005753 assert( pCur->apPage[0]==pPage );
drh9bf9e9c2008-12-05 20:01:43 +00005754 assert( pPage->nOverflow==0 || rc!=SQLITE_OK );
drh43605152004-05-29 21:46:49 +00005755 }
danielk1977687566d2004-11-02 12:56:41 +00005756 if( rc==SQLITE_OK && pPage->nCell==0 ){
danielk197771d5d2c2008-09-29 11:49:47 +00005757 rc = balance_shallower(pCur);
danielk1977a4124bd2008-12-23 10:37:47 +00005758 assert( pCur->apPage[0]==pPage );
drh9bf9e9c2008-12-05 20:01:43 +00005759 assert( pPage->nOverflow==0 || rc!=SQLITE_OK );
drh43605152004-05-29 21:46:49 +00005760 }
5761 }else{
danielk1977ac245ec2005-01-14 13:50:11 +00005762 if( pPage->nOverflow>0 ||
danielk197771d5d2c2008-09-29 11:49:47 +00005763 (!isInsert && pPage->nFree>pPage->pBt->usableSize*2/3) ){
5764 rc = balance_nonroot(pCur);
drh43605152004-05-29 21:46:49 +00005765 }
5766 }
5767 return rc;
5768}
5769
5770/*
drh8dcd7ca2004-08-08 19:43:29 +00005771** This routine checks all cursors that point to table pgnoRoot.
drh980b1a72006-08-16 16:42:48 +00005772** If any of those cursors were opened with wrFlag==0 in a different
5773** database connection (a database connection that shares the pager
5774** cache with the current connection) and that other connection
5775** is not in the ReadUncommmitted state, then this routine returns
5776** SQLITE_LOCKED.
danielk1977299b1872004-11-22 10:02:10 +00005777**
danielk19773588ceb2008-06-10 17:30:26 +00005778** As well as cursors with wrFlag==0, cursors with wrFlag==1 and
5779** isIncrblobHandle==1 are also considered 'read' cursors. Incremental
5780** blob cursors are used for both reading and writing.
5781**
5782** When pgnoRoot is the root page of an intkey table, this function is also
5783** responsible for invalidating incremental blob cursors when the table row
5784** on which they are opened is deleted or modified. Cursors are invalidated
5785** according to the following rules:
5786**
5787** 1) When BtreeClearTable() is called to completely delete the contents
5788** of a B-Tree table, pExclude is set to zero and parameter iRow is
5789** set to non-zero. In this case all incremental blob cursors open
5790** on the table rooted at pgnoRoot are invalidated.
5791**
5792** 2) When BtreeInsert(), BtreeDelete() or BtreePutData() is called to
5793** modify a table row via an SQL statement, pExclude is set to the
5794** write cursor used to do the modification and parameter iRow is set
5795** to the integer row id of the B-Tree entry being modified. Unless
5796** pExclude is itself an incremental blob cursor, then all incremental
5797** blob cursors open on row iRow of the B-Tree are invalidated.
5798**
5799** 3) If both pExclude and iRow are set to zero, no incremental blob
5800** cursors are invalidated.
drhf74b8d92002-09-01 23:20:45 +00005801*/
danielk19773588ceb2008-06-10 17:30:26 +00005802static int checkReadLocks(
5803 Btree *pBtree,
5804 Pgno pgnoRoot,
5805 BtCursor *pExclude,
5806 i64 iRow
5807){
danielk1977299b1872004-11-22 10:02:10 +00005808 BtCursor *p;
drh980b1a72006-08-16 16:42:48 +00005809 BtShared *pBt = pBtree->pBt;
drhe5fe6902007-12-07 18:55:28 +00005810 sqlite3 *db = pBtree->db;
drh1fee73e2007-08-29 04:00:57 +00005811 assert( sqlite3BtreeHoldsMutex(pBtree) );
danielk1977299b1872004-11-22 10:02:10 +00005812 for(p=pBt->pCursor; p; p=p->pNext){
drh980b1a72006-08-16 16:42:48 +00005813 if( p==pExclude ) continue;
drh980b1a72006-08-16 16:42:48 +00005814 if( p->pgnoRoot!=pgnoRoot ) continue;
danielk19773588ceb2008-06-10 17:30:26 +00005815#ifndef SQLITE_OMIT_INCRBLOB
5816 if( p->isIncrblobHandle && (
5817 (!pExclude && iRow)
5818 || (pExclude && !pExclude->isIncrblobHandle && p->info.nKey==iRow)
5819 )){
5820 p->eState = CURSOR_INVALID;
5821 }
5822#endif
5823 if( p->eState!=CURSOR_VALID ) continue;
5824 if( p->wrFlag==0
5825#ifndef SQLITE_OMIT_INCRBLOB
5826 || p->isIncrblobHandle
5827#endif
5828 ){
drhe5fe6902007-12-07 18:55:28 +00005829 sqlite3 *dbOther = p->pBtree->db;
drh980b1a72006-08-16 16:42:48 +00005830 if( dbOther==0 ||
5831 (dbOther!=db && (dbOther->flags & SQLITE_ReadUncommitted)==0) ){
5832 return SQLITE_LOCKED;
5833 }
danielk1977299b1872004-11-22 10:02:10 +00005834 }
5835 }
drhf74b8d92002-09-01 23:20:45 +00005836 return SQLITE_OK;
5837}
5838
5839/*
drh3b7511c2001-05-26 13:15:44 +00005840** Insert a new record into the BTree. The key is given by (pKey,nKey)
5841** and the data is given by (pData,nData). The cursor is used only to
drh91025292004-05-03 19:49:32 +00005842** define what table the record should be inserted into. The cursor
drh4b70f112004-05-02 21:12:19 +00005843** is left pointing at a random location.
5844**
5845** For an INTKEY table, only the nKey value of the key is used. pKey is
5846** ignored. For a ZERODATA table, the pData and nData are both ignored.
drh3b7511c2001-05-26 13:15:44 +00005847*/
drh3aac2dd2004-04-26 14:10:20 +00005848int sqlite3BtreeInsert(
drh5c4d9702001-08-20 00:33:58 +00005849 BtCursor *pCur, /* Insert data into the table of this cursor */
drh4a1c3802004-05-12 15:15:47 +00005850 const void *pKey, i64 nKey, /* The key of the new record */
drhe4d90812007-03-29 05:51:49 +00005851 const void *pData, int nData, /* The data of the new record */
drhb026e052007-05-02 01:34:31 +00005852 int nZero, /* Number of extra 0 bytes to append to data */
drhe4d90812007-03-29 05:51:49 +00005853 int appendBias /* True if this is likely an append */
drh3b7511c2001-05-26 13:15:44 +00005854){
drh3b7511c2001-05-26 13:15:44 +00005855 int rc;
5856 int loc;
drh14acc042001-06-10 19:56:58 +00005857 int szNew;
danielk197771d5d2c2008-09-29 11:49:47 +00005858 int idx;
drh3b7511c2001-05-26 13:15:44 +00005859 MemPage *pPage;
drhd677b3d2007-08-20 22:48:41 +00005860 Btree *p = pCur->pBtree;
5861 BtShared *pBt = p->pBt;
drha34b6762004-05-07 13:30:42 +00005862 unsigned char *oldCell;
drh2e38c322004-09-03 18:38:44 +00005863 unsigned char *newCell = 0;
drh3b7511c2001-05-26 13:15:44 +00005864
drh1fee73e2007-08-29 04:00:57 +00005865 assert( cursorHoldsMutex(pCur) );
drh64022502009-01-09 14:11:04 +00005866 assert( pBt->inTransaction==TRANS_WRITE );
drhf74b8d92002-09-01 23:20:45 +00005867 assert( !pBt->readOnly );
drh64022502009-01-09 14:11:04 +00005868 assert( pCur->wrFlag );
danielk19773588ceb2008-06-10 17:30:26 +00005869 if( checkReadLocks(pCur->pBtree, pCur->pgnoRoot, pCur, nKey) ){
drhf74b8d92002-09-01 23:20:45 +00005870 return SQLITE_LOCKED; /* The table pCur points to has a read lock */
5871 }
drhfb982642007-08-30 01:19:59 +00005872 if( pCur->eState==CURSOR_FAULT ){
5873 return pCur->skip;
5874 }
danielk1977da184232006-01-05 11:34:32 +00005875
5876 /* Save the positions of any other cursors open on this table */
danielk1977be51a652008-10-08 17:58:48 +00005877 sqlite3BtreeClearCursor(pCur);
danielk19772e94d4d2006-01-09 05:36:27 +00005878 if(
danielk19772e94d4d2006-01-09 05:36:27 +00005879 SQLITE_OK!=(rc = saveAllCursors(pBt, pCur->pgnoRoot, pCur)) ||
drhe63d9992008-08-13 19:11:48 +00005880 SQLITE_OK!=(rc = sqlite3BtreeMoveto(pCur, pKey, nKey, appendBias, &loc))
danielk19772e94d4d2006-01-09 05:36:27 +00005881 ){
danielk1977da184232006-01-05 11:34:32 +00005882 return rc;
5883 }
5884
danielk197771d5d2c2008-09-29 11:49:47 +00005885 pPage = pCur->apPage[pCur->iPage];
drh4a1c3802004-05-12 15:15:47 +00005886 assert( pPage->intKey || nKey>=0 );
drh44845222008-07-17 18:39:57 +00005887 assert( pPage->leaf || !pPage->intKey );
drh3a4c1412004-05-09 20:40:11 +00005888 TRACE(("INSERT: table=%d nkey=%lld ndata=%d page=%d %s\n",
5889 pCur->pgnoRoot, nKey, nData, pPage->pgno,
5890 loc==0 ? "overwrite" : "new entry"));
danielk197771d5d2c2008-09-29 11:49:47 +00005891 assert( pPage->isInit );
danielk197752ae7242008-03-25 14:24:56 +00005892 allocateTempSpace(pBt);
5893 newCell = pBt->pTmpSpace;
drh2e38c322004-09-03 18:38:44 +00005894 if( newCell==0 ) return SQLITE_NOMEM;
drhb026e052007-05-02 01:34:31 +00005895 rc = fillInCell(pPage, newCell, pKey, nKey, pData, nData, nZero, &szNew);
drh2e38c322004-09-03 18:38:44 +00005896 if( rc ) goto end_insert;
drh43605152004-05-29 21:46:49 +00005897 assert( szNew==cellSizePtr(pPage, newCell) );
drh2e38c322004-09-03 18:38:44 +00005898 assert( szNew<=MX_CELL_SIZE(pBt) );
danielk197771d5d2c2008-09-29 11:49:47 +00005899 idx = pCur->aiIdx[pCur->iPage];
danielk1977da184232006-01-05 11:34:32 +00005900 if( loc==0 && CURSOR_VALID==pCur->eState ){
drha9121e42008-02-19 14:59:35 +00005901 u16 szOld;
danielk197771d5d2c2008-09-29 11:49:47 +00005902 assert( idx<pPage->nCell );
danielk19776e465eb2007-08-21 13:11:00 +00005903 rc = sqlite3PagerWrite(pPage->pDbPage);
5904 if( rc ){
5905 goto end_insert;
5906 }
danielk197771d5d2c2008-09-29 11:49:47 +00005907 oldCell = findCell(pPage, idx);
drh4b70f112004-05-02 21:12:19 +00005908 if( !pPage->leaf ){
drh43605152004-05-29 21:46:49 +00005909 memcpy(newCell, oldCell, 4);
drh4b70f112004-05-02 21:12:19 +00005910 }
drh43605152004-05-29 21:46:49 +00005911 szOld = cellSizePtr(pPage, oldCell);
drh4b70f112004-05-02 21:12:19 +00005912 rc = clearCell(pPage, oldCell);
drh2e38c322004-09-03 18:38:44 +00005913 if( rc ) goto end_insert;
shane0af3f892008-11-12 04:55:34 +00005914 rc = dropCell(pPage, idx, szOld);
5915 if( rc!=SQLITE_OK ) {
5916 goto end_insert;
5917 }
drh7c717f72001-06-24 20:39:41 +00005918 }else if( loc<0 && pPage->nCell>0 ){
drh4b70f112004-05-02 21:12:19 +00005919 assert( pPage->leaf );
danielk197771d5d2c2008-09-29 11:49:47 +00005920 idx = ++pCur->aiIdx[pCur->iPage];
drh271efa52004-05-30 19:19:05 +00005921 pCur->info.nSize = 0;
drha2c20e42008-03-29 16:01:04 +00005922 pCur->validNKey = 0;
drh14acc042001-06-10 19:56:58 +00005923 }else{
drh4b70f112004-05-02 21:12:19 +00005924 assert( pPage->leaf );
drh3b7511c2001-05-26 13:15:44 +00005925 }
danielk197771d5d2c2008-09-29 11:49:47 +00005926 rc = insertCell(pPage, idx, newCell, szNew, 0, 0);
drh9bf9e9c2008-12-05 20:01:43 +00005927 if( rc==SQLITE_OK ){
5928 rc = balance(pCur, 1);
5929 }
5930
5931 /* Must make sure nOverflow is reset to zero even if the balance()
5932 ** fails. Internal data structure corruption will result otherwise. */
danielk1977a4124bd2008-12-23 10:37:47 +00005933 pCur->apPage[pCur->iPage]->nOverflow = 0;
drh9bf9e9c2008-12-05 20:01:43 +00005934
danielk1977299b1872004-11-22 10:02:10 +00005935 if( rc==SQLITE_OK ){
5936 moveToRoot(pCur);
5937 }
drh2e38c322004-09-03 18:38:44 +00005938end_insert:
drh5e2f8b92001-05-28 00:41:15 +00005939 return rc;
5940}
5941
5942/*
drh4b70f112004-05-02 21:12:19 +00005943** Delete the entry that the cursor is pointing to. The cursor
drhf94a1732008-09-30 17:18:17 +00005944** is left pointing at a arbitrary location.
drh3b7511c2001-05-26 13:15:44 +00005945*/
drh3aac2dd2004-04-26 14:10:20 +00005946int sqlite3BtreeDelete(BtCursor *pCur){
danielk197771d5d2c2008-09-29 11:49:47 +00005947 MemPage *pPage = pCur->apPage[pCur->iPage];
5948 int idx;
drh4b70f112004-05-02 21:12:19 +00005949 unsigned char *pCell;
drh5e2f8b92001-05-28 00:41:15 +00005950 int rc;
danielk1977cfe9a692004-06-16 12:00:29 +00005951 Pgno pgnoChild = 0;
drhd677b3d2007-08-20 22:48:41 +00005952 Btree *p = pCur->pBtree;
5953 BtShared *pBt = p->pBt;
drh8b2f49b2001-06-08 00:21:52 +00005954
drh1fee73e2007-08-29 04:00:57 +00005955 assert( cursorHoldsMutex(pCur) );
danielk197771d5d2c2008-09-29 11:49:47 +00005956 assert( pPage->isInit );
drh64022502009-01-09 14:11:04 +00005957 assert( pBt->inTransaction==TRANS_WRITE );
drhf74b8d92002-09-01 23:20:45 +00005958 assert( !pBt->readOnly );
drhfb982642007-08-30 01:19:59 +00005959 if( pCur->eState==CURSOR_FAULT ){
5960 return pCur->skip;
5961 }
drh64022502009-01-09 14:11:04 +00005962 if( NEVER(pCur->aiIdx[pCur->iPage]>=pPage->nCell) ){
drhbd03cae2001-06-02 02:40:57 +00005963 return SQLITE_ERROR; /* The cursor is not pointing to anything */
5964 }
drh64022502009-01-09 14:11:04 +00005965 assert( pCur->wrFlag );
danielk19773588ceb2008-06-10 17:30:26 +00005966 if( checkReadLocks(pCur->pBtree, pCur->pgnoRoot, pCur, pCur->info.nKey) ){
drhf74b8d92002-09-01 23:20:45 +00005967 return SQLITE_LOCKED; /* The table pCur points to has a read lock */
5968 }
danielk1977da184232006-01-05 11:34:32 +00005969
5970 /* Restore the current cursor position (a no-op if the cursor is not in
5971 ** CURSOR_REQUIRESEEK state) and save the positions of any other cursors
danielk19773b8a05f2007-03-19 17:44:26 +00005972 ** open on the same table. Then call sqlite3PagerWrite() on the page
danielk1977da184232006-01-05 11:34:32 +00005973 ** that the entry will be deleted from.
5974 */
5975 if(
drha3460582008-07-11 21:02:53 +00005976 (rc = restoreCursorPosition(pCur))!=0 ||
drhd1167392006-01-23 13:00:35 +00005977 (rc = saveAllCursors(pBt, pCur->pgnoRoot, pCur))!=0 ||
danielk19773b8a05f2007-03-19 17:44:26 +00005978 (rc = sqlite3PagerWrite(pPage->pDbPage))!=0
danielk1977da184232006-01-05 11:34:32 +00005979 ){
5980 return rc;
5981 }
danielk1977e6efa742004-11-10 11:55:10 +00005982
drh85b623f2007-12-13 21:54:09 +00005983 /* Locate the cell within its page and leave pCell pointing to the
danielk1977e6efa742004-11-10 11:55:10 +00005984 ** data. The clearCell() call frees any overflow pages associated with the
5985 ** cell. The cell itself is still intact.
5986 */
danielk197771d5d2c2008-09-29 11:49:47 +00005987 idx = pCur->aiIdx[pCur->iPage];
5988 pCell = findCell(pPage, idx);
drh4b70f112004-05-02 21:12:19 +00005989 if( !pPage->leaf ){
drh43605152004-05-29 21:46:49 +00005990 pgnoChild = get4byte(pCell);
drh4b70f112004-05-02 21:12:19 +00005991 }
danielk197728129562005-01-11 10:25:06 +00005992 rc = clearCell(pPage, pCell);
drhd677b3d2007-08-20 22:48:41 +00005993 if( rc ){
drhd677b3d2007-08-20 22:48:41 +00005994 return rc;
5995 }
danielk1977e6efa742004-11-10 11:55:10 +00005996
drh4b70f112004-05-02 21:12:19 +00005997 if( !pPage->leaf ){
drh14acc042001-06-10 19:56:58 +00005998 /*
drh5e00f6c2001-09-13 13:46:56 +00005999 ** The entry we are about to delete is not a leaf so if we do not
drh9ca7d3b2001-06-28 11:50:21 +00006000 ** do something we will leave a hole on an internal page.
6001 ** We have to fill the hole by moving in a cell from a leaf. The
6002 ** next Cell after the one to be deleted is guaranteed to exist and
danielk1977299b1872004-11-22 10:02:10 +00006003 ** to be a leaf so we can use it.
drh5e2f8b92001-05-28 00:41:15 +00006004 */
drh14acc042001-06-10 19:56:58 +00006005 BtCursor leafCur;
drh1bd10f82008-12-10 21:19:56 +00006006 MemPage *pLeafPage = 0;
danielk197771d5d2c2008-09-29 11:49:47 +00006007
drh4b70f112004-05-02 21:12:19 +00006008 unsigned char *pNext;
danielk1977299b1872004-11-22 10:02:10 +00006009 int notUsed;
danielk19776b456a22005-03-21 04:04:02 +00006010 unsigned char *tempCell = 0;
drh44845222008-07-17 18:39:57 +00006011 assert( !pPage->intKey );
drh16a9b832007-05-05 18:39:25 +00006012 sqlite3BtreeGetTempCursor(pCur, &leafCur);
danielk1977299b1872004-11-22 10:02:10 +00006013 rc = sqlite3BtreeNext(&leafCur, &notUsed);
danielk19776b456a22005-03-21 04:04:02 +00006014 if( rc==SQLITE_OK ){
danielk19772f78fc62008-09-30 09:31:45 +00006015 assert( leafCur.aiIdx[leafCur.iPage]==0 );
danielk197771d5d2c2008-09-29 11:49:47 +00006016 pLeafPage = leafCur.apPage[leafCur.iPage];
danielk197771d5d2c2008-09-29 11:49:47 +00006017 rc = sqlite3PagerWrite(pLeafPage->pDbPage);
danielk19776b456a22005-03-21 04:04:02 +00006018 }
6019 if( rc==SQLITE_OK ){
danielk19772f78fc62008-09-30 09:31:45 +00006020 int leafCursorInvalid = 0;
drha9121e42008-02-19 14:59:35 +00006021 u16 szNext;
danielk19776b456a22005-03-21 04:04:02 +00006022 TRACE(("DELETE: table=%d delete internal from %d replace from leaf %d\n",
danielk197771d5d2c2008-09-29 11:49:47 +00006023 pCur->pgnoRoot, pPage->pgno, pLeafPage->pgno));
6024 dropCell(pPage, idx, cellSizePtr(pPage, pCell));
danielk19772f78fc62008-09-30 09:31:45 +00006025 pNext = findCell(pLeafPage, 0);
danielk197771d5d2c2008-09-29 11:49:47 +00006026 szNext = cellSizePtr(pLeafPage, pNext);
danielk19776b456a22005-03-21 04:04:02 +00006027 assert( MX_CELL_SIZE(pBt)>=szNext+4 );
danielk197752ae7242008-03-25 14:24:56 +00006028 allocateTempSpace(pBt);
6029 tempCell = pBt->pTmpSpace;
danielk19776b456a22005-03-21 04:04:02 +00006030 if( tempCell==0 ){
6031 rc = SQLITE_NOMEM;
6032 }
danielk19778ea1cfa2008-01-01 06:19:02 +00006033 if( rc==SQLITE_OK ){
danielk197771d5d2c2008-09-29 11:49:47 +00006034 rc = insertCell(pPage, idx, pNext-4, szNext+4, tempCell, 0);
danielk19778ea1cfa2008-01-01 06:19:02 +00006035 }
danielk19772f78fc62008-09-30 09:31:45 +00006036
drhf94a1732008-09-30 17:18:17 +00006037
6038 /* The "if" statement in the next code block is critical. The
6039 ** slightest error in that statement would allow SQLite to operate
6040 ** correctly most of the time but produce very rare failures. To
6041 ** guard against this, the following macros help to verify that
6042 ** the "if" statement is well tested.
6043 */
6044 testcase( pPage->nOverflow==0 && pPage->nFree<pBt->usableSize*2/3
6045 && pLeafPage->nFree+2+szNext > pBt->usableSize*2/3 );
6046 testcase( pPage->nOverflow==0 && pPage->nFree==pBt->usableSize*2/3
6047 && pLeafPage->nFree+2+szNext > pBt->usableSize*2/3 );
6048 testcase( pPage->nOverflow==0 && pPage->nFree==pBt->usableSize*2/3+1
6049 && pLeafPage->nFree+2+szNext > pBt->usableSize*2/3 );
6050 testcase( pPage->nOverflow>0 && pPage->nFree<=pBt->usableSize*2/3
6051 && pLeafPage->nFree+2+szNext > pBt->usableSize*2/3 );
6052 testcase( (pPage->nOverflow>0 || (pPage->nFree > pBt->usableSize*2/3))
6053 && pLeafPage->nFree+2+szNext == pBt->usableSize*2/3 );
6054
6055
danielk19772f78fc62008-09-30 09:31:45 +00006056 if( (pPage->nOverflow>0 || (pPage->nFree > pBt->usableSize*2/3)) &&
6057 (pLeafPage->nFree+2+szNext > pBt->usableSize*2/3)
6058 ){
drhf94a1732008-09-30 17:18:17 +00006059 /* This branch is taken if the internal node is now either overflowing
6060 ** or underfull and the leaf node will be underfull after the just cell
danielk19772f78fc62008-09-30 09:31:45 +00006061 ** copied to the internal node is deleted from it. This is a special
6062 ** case because the call to balance() to correct the internal node
6063 ** may change the tree structure and invalidate the contents of
6064 ** the leafCur.apPage[] and leafCur.aiIdx[] arrays, which will be
6065 ** used by the balance() required to correct the underfull leaf
6066 ** node.
6067 **
6068 ** The formula used in the expression above are based on facets of
6069 ** the SQLite file-format that do not change over time.
6070 */
drhf94a1732008-09-30 17:18:17 +00006071 testcase( pPage->nFree==pBt->usableSize*2/3+1 );
6072 testcase( pLeafPage->nFree+2+szNext==pBt->usableSize*2/3+1 );
danielk19772f78fc62008-09-30 09:31:45 +00006073 leafCursorInvalid = 1;
6074 }
6075
danielk19778ea1cfa2008-01-01 06:19:02 +00006076 if( rc==SQLITE_OK ){
drhc5053fb2008-11-27 02:22:10 +00006077 assert( sqlite3PagerIswriteable(pPage->pDbPage) );
danielk197771d5d2c2008-09-29 11:49:47 +00006078 put4byte(findOverflowCell(pPage, idx), pgnoChild);
drhf94a1732008-09-30 17:18:17 +00006079 VVA_ONLY( pCur->pagesShuffled = 0 );
danielk197771d5d2c2008-09-29 11:49:47 +00006080 rc = balance(pCur, 0);
danielk19778ea1cfa2008-01-01 06:19:02 +00006081 }
danielk19772f78fc62008-09-30 09:31:45 +00006082
6083 if( rc==SQLITE_OK && leafCursorInvalid ){
6084 /* The leaf-node is now underfull and so the tree needs to be
6085 ** rebalanced. However, the balance() operation on the internal
6086 ** node above may have modified the structure of the B-Tree and
6087 ** so the current contents of leafCur.apPage[] and leafCur.aiIdx[]
6088 ** may not be trusted.
6089 **
6090 ** It is not possible to copy the ancestry from pCur, as the same
6091 ** balance() call has invalidated the pCur->apPage[] and aiIdx[]
6092 ** arrays.
drh7b682802008-09-30 14:06:28 +00006093 **
6094 ** The call to saveCursorPosition() below internally saves the
6095 ** key that leafCur is currently pointing to. Currently, there
6096 ** are two copies of that key in the tree - one here on the leaf
6097 ** page and one on some internal node in the tree. The copy on
6098 ** the leaf node is always the next key in tree-order after the
6099 ** copy on the internal node. So, the call to sqlite3BtreeNext()
6100 ** calls restoreCursorPosition() to point the cursor to the copy
6101 ** stored on the internal node, then advances to the next entry,
6102 ** which happens to be the copy of the key on the internal node.
danielk1977a69fda22008-09-30 16:48:10 +00006103 ** Net effect: leafCur is pointing back to the duplicate cell
6104 ** that needs to be removed, and the leafCur.apPage[] and
6105 ** leafCur.aiIdx[] arrays are correct.
danielk19772f78fc62008-09-30 09:31:45 +00006106 */
drhf94a1732008-09-30 17:18:17 +00006107 VVA_ONLY( Pgno leafPgno = pLeafPage->pgno );
danielk19772f78fc62008-09-30 09:31:45 +00006108 rc = saveCursorPosition(&leafCur);
6109 if( rc==SQLITE_OK ){
6110 rc = sqlite3BtreeNext(&leafCur, &notUsed);
6111 }
6112 pLeafPage = leafCur.apPage[leafCur.iPage];
6113 assert( pLeafPage->pgno==leafPgno );
6114 assert( leafCur.aiIdx[leafCur.iPage]==0 );
6115 }
6116
danielk19770cd1bbd2008-11-26 07:25:52 +00006117 if( SQLITE_OK==rc
6118 && SQLITE_OK==(rc = sqlite3PagerWrite(pLeafPage->pDbPage))
6119 ){
danielk19772f78fc62008-09-30 09:31:45 +00006120 dropCell(pLeafPage, 0, szNext);
drhf94a1732008-09-30 17:18:17 +00006121 VVA_ONLY( leafCur.pagesShuffled = 0 );
danielk197771d5d2c2008-09-29 11:49:47 +00006122 rc = balance(&leafCur, 0);
drhf94a1732008-09-30 17:18:17 +00006123 assert( leafCursorInvalid || !leafCur.pagesShuffled
6124 || !pCur->pagesShuffled );
danielk19778ea1cfa2008-01-01 06:19:02 +00006125 }
danielk19776b456a22005-03-21 04:04:02 +00006126 }
drh16a9b832007-05-05 18:39:25 +00006127 sqlite3BtreeReleaseTempCursor(&leafCur);
drh5e2f8b92001-05-28 00:41:15 +00006128 }else{
danielk1977299b1872004-11-22 10:02:10 +00006129 TRACE(("DELETE: table=%d delete from leaf %d\n",
6130 pCur->pgnoRoot, pPage->pgno));
shanedcc50b72008-11-13 18:29:50 +00006131 rc = dropCell(pPage, idx, cellSizePtr(pPage, pCell));
6132 if( rc==SQLITE_OK ){
6133 rc = balance(pCur, 0);
6134 }
drh5e2f8b92001-05-28 00:41:15 +00006135 }
danielk19776b456a22005-03-21 04:04:02 +00006136 if( rc==SQLITE_OK ){
6137 moveToRoot(pCur);
6138 }
drh5e2f8b92001-05-28 00:41:15 +00006139 return rc;
drh3b7511c2001-05-26 13:15:44 +00006140}
drh8b2f49b2001-06-08 00:21:52 +00006141
6142/*
drhc6b52df2002-01-04 03:09:29 +00006143** Create a new BTree table. Write into *piTable the page
6144** number for the root page of the new table.
6145**
drhab01f612004-05-22 02:55:23 +00006146** The type of type is determined by the flags parameter. Only the
6147** following values of flags are currently in use. Other values for
6148** flags might not work:
6149**
6150** BTREE_INTKEY|BTREE_LEAFDATA Used for SQL tables with rowid keys
6151** BTREE_ZERODATA Used for SQL indices
drh8b2f49b2001-06-08 00:21:52 +00006152*/
drhd677b3d2007-08-20 22:48:41 +00006153static int btreeCreateTable(Btree *p, int *piTable, int flags){
danielk1977aef0bf62005-12-30 16:28:01 +00006154 BtShared *pBt = p->pBt;
drh8b2f49b2001-06-08 00:21:52 +00006155 MemPage *pRoot;
6156 Pgno pgnoRoot;
6157 int rc;
drhd677b3d2007-08-20 22:48:41 +00006158
drh1fee73e2007-08-29 04:00:57 +00006159 assert( sqlite3BtreeHoldsMutex(p) );
drh64022502009-01-09 14:11:04 +00006160 assert( pBt->inTransaction==TRANS_WRITE );
danielk197728129562005-01-11 10:25:06 +00006161 assert( !pBt->readOnly );
danielk1977e6efa742004-11-10 11:55:10 +00006162
danielk1977003ba062004-11-04 02:57:33 +00006163#ifdef SQLITE_OMIT_AUTOVACUUM
drh4f0c5872007-03-26 22:05:01 +00006164 rc = allocateBtreePage(pBt, &pRoot, &pgnoRoot, 1, 0);
drhd677b3d2007-08-20 22:48:41 +00006165 if( rc ){
6166 return rc;
6167 }
danielk1977003ba062004-11-04 02:57:33 +00006168#else
danielk1977687566d2004-11-02 12:56:41 +00006169 if( pBt->autoVacuum ){
danielk1977003ba062004-11-04 02:57:33 +00006170 Pgno pgnoMove; /* Move a page here to make room for the root-page */
6171 MemPage *pPageMove; /* The page to move to. */
6172
danielk197720713f32007-05-03 11:43:33 +00006173 /* Creating a new table may probably require moving an existing database
6174 ** to make room for the new tables root page. In case this page turns
6175 ** out to be an overflow page, delete all overflow page-map caches
6176 ** held by open cursors.
6177 */
danielk197792d4d7a2007-05-04 12:05:56 +00006178 invalidateAllOverflowCache(pBt);
danielk197720713f32007-05-03 11:43:33 +00006179
danielk1977003ba062004-11-04 02:57:33 +00006180 /* Read the value of meta[3] from the database to determine where the
6181 ** root page of the new table should go. meta[3] is the largest root-page
6182 ** created so far, so the new root-page is (meta[3]+1).
6183 */
danielk1977aef0bf62005-12-30 16:28:01 +00006184 rc = sqlite3BtreeGetMeta(p, 4, &pgnoRoot);
drhd677b3d2007-08-20 22:48:41 +00006185 if( rc!=SQLITE_OK ){
6186 return rc;
6187 }
danielk1977003ba062004-11-04 02:57:33 +00006188 pgnoRoot++;
6189
danielk1977599fcba2004-11-08 07:13:13 +00006190 /* The new root-page may not be allocated on a pointer-map page, or the
6191 ** PENDING_BYTE page.
6192 */
drh72190432008-01-31 14:54:43 +00006193 while( pgnoRoot==PTRMAP_PAGENO(pBt, pgnoRoot) ||
danielk1977599fcba2004-11-08 07:13:13 +00006194 pgnoRoot==PENDING_BYTE_PAGE(pBt) ){
danielk1977003ba062004-11-04 02:57:33 +00006195 pgnoRoot++;
6196 }
6197 assert( pgnoRoot>=3 );
6198
6199 /* Allocate a page. The page that currently resides at pgnoRoot will
6200 ** be moved to the allocated page (unless the allocated page happens
6201 ** to reside at pgnoRoot).
6202 */
drh4f0c5872007-03-26 22:05:01 +00006203 rc = allocateBtreePage(pBt, &pPageMove, &pgnoMove, pgnoRoot, 1);
danielk1977003ba062004-11-04 02:57:33 +00006204 if( rc!=SQLITE_OK ){
danielk1977687566d2004-11-02 12:56:41 +00006205 return rc;
6206 }
danielk1977003ba062004-11-04 02:57:33 +00006207
6208 if( pgnoMove!=pgnoRoot ){
danielk1977f35843b2007-04-07 15:03:17 +00006209 /* pgnoRoot is the page that will be used for the root-page of
6210 ** the new table (assuming an error did not occur). But we were
6211 ** allocated pgnoMove. If required (i.e. if it was not allocated
6212 ** by extending the file), the current page at position pgnoMove
6213 ** is already journaled.
6214 */
danielk1977003ba062004-11-04 02:57:33 +00006215 u8 eType;
6216 Pgno iPtrPage;
6217
6218 releasePage(pPageMove);
danielk1977f35843b2007-04-07 15:03:17 +00006219
6220 /* Move the page currently at pgnoRoot to pgnoMove. */
drh16a9b832007-05-05 18:39:25 +00006221 rc = sqlite3BtreeGetPage(pBt, pgnoRoot, &pRoot, 0);
danielk1977003ba062004-11-04 02:57:33 +00006222 if( rc!=SQLITE_OK ){
6223 return rc;
6224 }
6225 rc = ptrmapGet(pBt, pgnoRoot, &eType, &iPtrPage);
drhccae6022005-02-26 17:31:26 +00006226 if( rc!=SQLITE_OK || eType==PTRMAP_ROOTPAGE || eType==PTRMAP_FREEPAGE ){
danielk1977003ba062004-11-04 02:57:33 +00006227 releasePage(pRoot);
6228 return rc;
6229 }
drhccae6022005-02-26 17:31:26 +00006230 assert( eType!=PTRMAP_ROOTPAGE );
6231 assert( eType!=PTRMAP_FREEPAGE );
danielk197745d68822009-01-16 16:23:38 +00006232 rc = sqlite3PagerWrite(pRoot->pDbPage);
6233 if( rc!=SQLITE_OK ){
6234 releasePage(pRoot);
6235 return rc;
6236 }
danielk19774c999992008-07-16 18:17:55 +00006237 rc = relocatePage(pBt, pRoot, eType, iPtrPage, pgnoMove, 0);
danielk1977003ba062004-11-04 02:57:33 +00006238 releasePage(pRoot);
danielk1977f35843b2007-04-07 15:03:17 +00006239
6240 /* Obtain the page at pgnoRoot */
danielk1977003ba062004-11-04 02:57:33 +00006241 if( rc!=SQLITE_OK ){
6242 return rc;
6243 }
drh16a9b832007-05-05 18:39:25 +00006244 rc = sqlite3BtreeGetPage(pBt, pgnoRoot, &pRoot, 0);
danielk1977003ba062004-11-04 02:57:33 +00006245 if( rc!=SQLITE_OK ){
6246 return rc;
6247 }
danielk19773b8a05f2007-03-19 17:44:26 +00006248 rc = sqlite3PagerWrite(pRoot->pDbPage);
danielk1977003ba062004-11-04 02:57:33 +00006249 if( rc!=SQLITE_OK ){
6250 releasePage(pRoot);
6251 return rc;
6252 }
6253 }else{
6254 pRoot = pPageMove;
6255 }
6256
danielk197742741be2005-01-08 12:42:39 +00006257 /* Update the pointer-map and meta-data with the new root-page number. */
danielk1977003ba062004-11-04 02:57:33 +00006258 rc = ptrmapPut(pBt, pgnoRoot, PTRMAP_ROOTPAGE, 0);
6259 if( rc ){
6260 releasePage(pRoot);
6261 return rc;
6262 }
danielk1977aef0bf62005-12-30 16:28:01 +00006263 rc = sqlite3BtreeUpdateMeta(p, 4, pgnoRoot);
danielk1977003ba062004-11-04 02:57:33 +00006264 if( rc ){
6265 releasePage(pRoot);
6266 return rc;
6267 }
danielk197742741be2005-01-08 12:42:39 +00006268
danielk1977003ba062004-11-04 02:57:33 +00006269 }else{
drh4f0c5872007-03-26 22:05:01 +00006270 rc = allocateBtreePage(pBt, &pRoot, &pgnoRoot, 1, 0);
danielk1977003ba062004-11-04 02:57:33 +00006271 if( rc ) return rc;
danielk1977687566d2004-11-02 12:56:41 +00006272 }
6273#endif
danielk19773b8a05f2007-03-19 17:44:26 +00006274 assert( sqlite3PagerIswriteable(pRoot->pDbPage) );
drhde647132004-05-07 17:57:49 +00006275 zeroPage(pRoot, flags | PTF_LEAF);
danielk19773b8a05f2007-03-19 17:44:26 +00006276 sqlite3PagerUnref(pRoot->pDbPage);
drh8b2f49b2001-06-08 00:21:52 +00006277 *piTable = (int)pgnoRoot;
6278 return SQLITE_OK;
6279}
drhd677b3d2007-08-20 22:48:41 +00006280int sqlite3BtreeCreateTable(Btree *p, int *piTable, int flags){
6281 int rc;
6282 sqlite3BtreeEnter(p);
drhe5fe6902007-12-07 18:55:28 +00006283 p->pBt->db = p->db;
drhd677b3d2007-08-20 22:48:41 +00006284 rc = btreeCreateTable(p, piTable, flags);
6285 sqlite3BtreeLeave(p);
6286 return rc;
6287}
drh8b2f49b2001-06-08 00:21:52 +00006288
6289/*
6290** Erase the given database page and all its children. Return
6291** the page to the freelist.
6292*/
drh4b70f112004-05-02 21:12:19 +00006293static int clearDatabasePage(
danielk1977aef0bf62005-12-30 16:28:01 +00006294 BtShared *pBt, /* The BTree that contains the table */
drh4b70f112004-05-02 21:12:19 +00006295 Pgno pgno, /* Page number to clear */
danielk1977c7af4842008-10-27 13:59:33 +00006296 int freePageFlag, /* Deallocate page if true */
6297 int *pnChange
drh4b70f112004-05-02 21:12:19 +00006298){
danielk19776b456a22005-03-21 04:04:02 +00006299 MemPage *pPage = 0;
drh8b2f49b2001-06-08 00:21:52 +00006300 int rc;
drh4b70f112004-05-02 21:12:19 +00006301 unsigned char *pCell;
6302 int i;
drh8b2f49b2001-06-08 00:21:52 +00006303
drh1fee73e2007-08-29 04:00:57 +00006304 assert( sqlite3_mutex_held(pBt->mutex) );
danielk197789d40042008-11-17 14:20:56 +00006305 if( pgno>pagerPagecount(pBt) ){
drh49285702005-09-17 15:20:26 +00006306 return SQLITE_CORRUPT_BKPT;
danielk1977a1cb1832005-02-12 08:59:55 +00006307 }
6308
danielk197771d5d2c2008-09-29 11:49:47 +00006309 rc = getAndInitPage(pBt, pgno, &pPage);
danielk19776b456a22005-03-21 04:04:02 +00006310 if( rc ) goto cleardatabasepage_out;
drh4b70f112004-05-02 21:12:19 +00006311 for(i=0; i<pPage->nCell; i++){
danielk19771cc5ed82007-05-16 17:28:43 +00006312 pCell = findCell(pPage, i);
drh4b70f112004-05-02 21:12:19 +00006313 if( !pPage->leaf ){
danielk197762c14b32008-11-19 09:05:26 +00006314 rc = clearDatabasePage(pBt, get4byte(pCell), 1, pnChange);
danielk19776b456a22005-03-21 04:04:02 +00006315 if( rc ) goto cleardatabasepage_out;
drh8b2f49b2001-06-08 00:21:52 +00006316 }
drh4b70f112004-05-02 21:12:19 +00006317 rc = clearCell(pPage, pCell);
danielk19776b456a22005-03-21 04:04:02 +00006318 if( rc ) goto cleardatabasepage_out;
drh8b2f49b2001-06-08 00:21:52 +00006319 }
drha34b6762004-05-07 13:30:42 +00006320 if( !pPage->leaf ){
danielk197762c14b32008-11-19 09:05:26 +00006321 rc = clearDatabasePage(pBt, get4byte(&pPage->aData[8]), 1, pnChange);
danielk19776b456a22005-03-21 04:04:02 +00006322 if( rc ) goto cleardatabasepage_out;
danielk1977c7af4842008-10-27 13:59:33 +00006323 }else if( pnChange ){
6324 assert( pPage->intKey );
6325 *pnChange += pPage->nCell;
drh2aa679f2001-06-25 02:11:07 +00006326 }
6327 if( freePageFlag ){
drh4b70f112004-05-02 21:12:19 +00006328 rc = freePage(pPage);
danielk19773b8a05f2007-03-19 17:44:26 +00006329 }else if( (rc = sqlite3PagerWrite(pPage->pDbPage))==0 ){
drh3a4c1412004-05-09 20:40:11 +00006330 zeroPage(pPage, pPage->aData[0] | PTF_LEAF);
drh2aa679f2001-06-25 02:11:07 +00006331 }
danielk19776b456a22005-03-21 04:04:02 +00006332
6333cleardatabasepage_out:
drh4b70f112004-05-02 21:12:19 +00006334 releasePage(pPage);
drh2aa679f2001-06-25 02:11:07 +00006335 return rc;
drh8b2f49b2001-06-08 00:21:52 +00006336}
6337
6338/*
drhab01f612004-05-22 02:55:23 +00006339** Delete all information from a single table in the database. iTable is
6340** the page number of the root of the table. After this routine returns,
6341** the root page is empty, but still exists.
6342**
6343** This routine will fail with SQLITE_LOCKED if there are any open
6344** read cursors on the table. Open write cursors are moved to the
6345** root of the table.
danielk1977c7af4842008-10-27 13:59:33 +00006346**
6347** If pnChange is not NULL, then table iTable must be an intkey table. The
6348** integer value pointed to by pnChange is incremented by the number of
6349** entries in the table.
drh8b2f49b2001-06-08 00:21:52 +00006350*/
danielk1977c7af4842008-10-27 13:59:33 +00006351int sqlite3BtreeClearTable(Btree *p, int iTable, int *pnChange){
drh8b2f49b2001-06-08 00:21:52 +00006352 int rc;
danielk1977aef0bf62005-12-30 16:28:01 +00006353 BtShared *pBt = p->pBt;
drhd677b3d2007-08-20 22:48:41 +00006354 sqlite3BtreeEnter(p);
drhe5fe6902007-12-07 18:55:28 +00006355 pBt->db = p->db;
drh64022502009-01-09 14:11:04 +00006356 assert( p->inTrans==TRANS_WRITE );
6357 if( (rc = checkReadLocks(p, iTable, 0, 1))!=SQLITE_OK ){
drhd677b3d2007-08-20 22:48:41 +00006358 /* nothing to do */
6359 }else if( SQLITE_OK!=(rc = saveAllCursors(pBt, iTable, 0)) ){
6360 /* nothing to do */
6361 }else{
danielk197762c14b32008-11-19 09:05:26 +00006362 rc = clearDatabasePage(pBt, (Pgno)iTable, 0, pnChange);
drh8b2f49b2001-06-08 00:21:52 +00006363 }
drhd677b3d2007-08-20 22:48:41 +00006364 sqlite3BtreeLeave(p);
6365 return rc;
drh8b2f49b2001-06-08 00:21:52 +00006366}
6367
6368/*
6369** Erase all information in a table and add the root of the table to
6370** the freelist. Except, the root of the principle table (the one on
drhab01f612004-05-22 02:55:23 +00006371** page 1) is never added to the freelist.
6372**
6373** This routine will fail with SQLITE_LOCKED if there are any open
6374** cursors on the table.
drh205f48e2004-11-05 00:43:11 +00006375**
6376** If AUTOVACUUM is enabled and the page at iTable is not the last
6377** root page in the database file, then the last root page
6378** in the database file is moved into the slot formerly occupied by
6379** iTable and that last slot formerly occupied by the last root page
6380** is added to the freelist instead of iTable. In this say, all
6381** root pages are kept at the beginning of the database file, which
6382** is necessary for AUTOVACUUM to work right. *piMoved is set to the
6383** page number that used to be the last root page in the file before
6384** the move. If no page gets moved, *piMoved is set to 0.
6385** The last root page is recorded in meta[3] and the value of
6386** meta[3] is updated by this procedure.
drh8b2f49b2001-06-08 00:21:52 +00006387*/
danielk197789d40042008-11-17 14:20:56 +00006388static int btreeDropTable(Btree *p, Pgno iTable, int *piMoved){
drh8b2f49b2001-06-08 00:21:52 +00006389 int rc;
danielk1977a0bf2652004-11-04 14:30:04 +00006390 MemPage *pPage = 0;
danielk1977aef0bf62005-12-30 16:28:01 +00006391 BtShared *pBt = p->pBt;
danielk1977a0bf2652004-11-04 14:30:04 +00006392
drh1fee73e2007-08-29 04:00:57 +00006393 assert( sqlite3BtreeHoldsMutex(p) );
drh64022502009-01-09 14:11:04 +00006394 assert( p->inTrans==TRANS_WRITE );
danielk1977a0bf2652004-11-04 14:30:04 +00006395
danielk1977e6efa742004-11-10 11:55:10 +00006396 /* It is illegal to drop a table if any cursors are open on the
6397 ** database. This is because in auto-vacuum mode the backend may
6398 ** need to move another root-page to fill a gap left by the deleted
6399 ** root page. If an open cursor was using this page a problem would
6400 ** occur.
6401 */
6402 if( pBt->pCursor ){
6403 return SQLITE_LOCKED;
drh5df72a52002-06-06 23:16:05 +00006404 }
danielk1977a0bf2652004-11-04 14:30:04 +00006405
drh16a9b832007-05-05 18:39:25 +00006406 rc = sqlite3BtreeGetPage(pBt, (Pgno)iTable, &pPage, 0);
drh2aa679f2001-06-25 02:11:07 +00006407 if( rc ) return rc;
danielk1977c7af4842008-10-27 13:59:33 +00006408 rc = sqlite3BtreeClearTable(p, iTable, 0);
danielk19776b456a22005-03-21 04:04:02 +00006409 if( rc ){
6410 releasePage(pPage);
6411 return rc;
6412 }
danielk1977a0bf2652004-11-04 14:30:04 +00006413
drh205f48e2004-11-05 00:43:11 +00006414 *piMoved = 0;
danielk1977a0bf2652004-11-04 14:30:04 +00006415
drh4b70f112004-05-02 21:12:19 +00006416 if( iTable>1 ){
danielk1977a0bf2652004-11-04 14:30:04 +00006417#ifdef SQLITE_OMIT_AUTOVACUUM
drha34b6762004-05-07 13:30:42 +00006418 rc = freePage(pPage);
danielk1977a0bf2652004-11-04 14:30:04 +00006419 releasePage(pPage);
6420#else
6421 if( pBt->autoVacuum ){
6422 Pgno maxRootPgno;
danielk1977aef0bf62005-12-30 16:28:01 +00006423 rc = sqlite3BtreeGetMeta(p, 4, &maxRootPgno);
danielk1977a0bf2652004-11-04 14:30:04 +00006424 if( rc!=SQLITE_OK ){
6425 releasePage(pPage);
6426 return rc;
6427 }
6428
6429 if( iTable==maxRootPgno ){
6430 /* If the table being dropped is the table with the largest root-page
6431 ** number in the database, put the root page on the free list.
6432 */
6433 rc = freePage(pPage);
6434 releasePage(pPage);
6435 if( rc!=SQLITE_OK ){
6436 return rc;
6437 }
6438 }else{
6439 /* The table being dropped does not have the largest root-page
6440 ** number in the database. So move the page that does into the
6441 ** gap left by the deleted root-page.
6442 */
6443 MemPage *pMove;
6444 releasePage(pPage);
drh16a9b832007-05-05 18:39:25 +00006445 rc = sqlite3BtreeGetPage(pBt, maxRootPgno, &pMove, 0);
danielk1977a0bf2652004-11-04 14:30:04 +00006446 if( rc!=SQLITE_OK ){
6447 return rc;
6448 }
danielk19774c999992008-07-16 18:17:55 +00006449 rc = relocatePage(pBt, pMove, PTRMAP_ROOTPAGE, 0, iTable, 0);
danielk1977a0bf2652004-11-04 14:30:04 +00006450 releasePage(pMove);
6451 if( rc!=SQLITE_OK ){
6452 return rc;
6453 }
drh16a9b832007-05-05 18:39:25 +00006454 rc = sqlite3BtreeGetPage(pBt, maxRootPgno, &pMove, 0);
danielk1977a0bf2652004-11-04 14:30:04 +00006455 if( rc!=SQLITE_OK ){
6456 return rc;
6457 }
6458 rc = freePage(pMove);
6459 releasePage(pMove);
6460 if( rc!=SQLITE_OK ){
6461 return rc;
6462 }
6463 *piMoved = maxRootPgno;
6464 }
6465
danielk1977599fcba2004-11-08 07:13:13 +00006466 /* Set the new 'max-root-page' value in the database header. This
6467 ** is the old value less one, less one more if that happens to
6468 ** be a root-page number, less one again if that is the
6469 ** PENDING_BYTE_PAGE.
6470 */
danielk197787a6e732004-11-05 12:58:25 +00006471 maxRootPgno--;
danielk1977599fcba2004-11-08 07:13:13 +00006472 if( maxRootPgno==PENDING_BYTE_PAGE(pBt) ){
6473 maxRootPgno--;
6474 }
danielk1977266664d2006-02-10 08:24:21 +00006475 if( maxRootPgno==PTRMAP_PAGENO(pBt, maxRootPgno) ){
danielk197787a6e732004-11-05 12:58:25 +00006476 maxRootPgno--;
6477 }
danielk1977599fcba2004-11-08 07:13:13 +00006478 assert( maxRootPgno!=PENDING_BYTE_PAGE(pBt) );
6479
danielk1977aef0bf62005-12-30 16:28:01 +00006480 rc = sqlite3BtreeUpdateMeta(p, 4, maxRootPgno);
danielk1977a0bf2652004-11-04 14:30:04 +00006481 }else{
6482 rc = freePage(pPage);
6483 releasePage(pPage);
6484 }
6485#endif
drh2aa679f2001-06-25 02:11:07 +00006486 }else{
danielk1977a0bf2652004-11-04 14:30:04 +00006487 /* If sqlite3BtreeDropTable was called on page 1. */
drha34b6762004-05-07 13:30:42 +00006488 zeroPage(pPage, PTF_INTKEY|PTF_LEAF );
danielk1977a0bf2652004-11-04 14:30:04 +00006489 releasePage(pPage);
drh8b2f49b2001-06-08 00:21:52 +00006490 }
drh8b2f49b2001-06-08 00:21:52 +00006491 return rc;
6492}
drhd677b3d2007-08-20 22:48:41 +00006493int sqlite3BtreeDropTable(Btree *p, int iTable, int *piMoved){
6494 int rc;
6495 sqlite3BtreeEnter(p);
drhe5fe6902007-12-07 18:55:28 +00006496 p->pBt->db = p->db;
drhd677b3d2007-08-20 22:48:41 +00006497 rc = btreeDropTable(p, iTable, piMoved);
6498 sqlite3BtreeLeave(p);
6499 return rc;
6500}
drh8b2f49b2001-06-08 00:21:52 +00006501
drh001bbcb2003-03-19 03:14:00 +00006502
drh8b2f49b2001-06-08 00:21:52 +00006503/*
drh23e11ca2004-05-04 17:27:28 +00006504** Read the meta-information out of a database file. Meta[0]
6505** is the number of free pages currently in the database. Meta[1]
drha3b321d2004-05-11 09:31:31 +00006506** through meta[15] are available for use by higher layers. Meta[0]
6507** is read-only, the others are read/write.
6508**
6509** The schema layer numbers meta values differently. At the schema
6510** layer (and the SetCookie and ReadCookie opcodes) the number of
6511** free pages is not visible. So Cookie[0] is the same as Meta[1].
drh8b2f49b2001-06-08 00:21:52 +00006512*/
danielk1977aef0bf62005-12-30 16:28:01 +00006513int sqlite3BtreeGetMeta(Btree *p, int idx, u32 *pMeta){
drh1bd10f82008-12-10 21:19:56 +00006514 DbPage *pDbPage = 0;
drh8b2f49b2001-06-08 00:21:52 +00006515 int rc;
drh4b70f112004-05-02 21:12:19 +00006516 unsigned char *pP1;
danielk1977aef0bf62005-12-30 16:28:01 +00006517 BtShared *pBt = p->pBt;
drh8b2f49b2001-06-08 00:21:52 +00006518
drhd677b3d2007-08-20 22:48:41 +00006519 sqlite3BtreeEnter(p);
drhe5fe6902007-12-07 18:55:28 +00006520 pBt->db = p->db;
drhd677b3d2007-08-20 22:48:41 +00006521
danielk1977da184232006-01-05 11:34:32 +00006522 /* Reading a meta-data value requires a read-lock on page 1 (and hence
6523 ** the sqlite_master table. We grab this lock regardless of whether or
6524 ** not the SQLITE_ReadUncommitted flag is set (the table rooted at page
6525 ** 1 is treated as a special case by queryTableLock() and lockTable()).
6526 */
6527 rc = queryTableLock(p, 1, READ_LOCK);
6528 if( rc!=SQLITE_OK ){
drhd677b3d2007-08-20 22:48:41 +00006529 sqlite3BtreeLeave(p);
danielk1977da184232006-01-05 11:34:32 +00006530 return rc;
6531 }
6532
drh23e11ca2004-05-04 17:27:28 +00006533 assert( idx>=0 && idx<=15 );
danielk1977d9f6c532008-09-19 16:39:38 +00006534 if( pBt->pPage1 ){
6535 /* The b-tree is already holding a reference to page 1 of the database
6536 ** file. In this case the required meta-data value can be read directly
6537 ** from the page data of this reference. This is slightly faster than
6538 ** requesting a new reference from the pager layer.
6539 */
6540 pP1 = (unsigned char *)pBt->pPage1->aData;
6541 }else{
6542 /* The b-tree does not have a reference to page 1 of the database file.
6543 ** Obtain one from the pager layer.
6544 */
danielk1977ea897302008-09-19 15:10:58 +00006545 rc = sqlite3PagerGet(pBt->pPager, 1, &pDbPage);
6546 if( rc ){
6547 sqlite3BtreeLeave(p);
6548 return rc;
6549 }
6550 pP1 = (unsigned char *)sqlite3PagerGetData(pDbPage);
drhd677b3d2007-08-20 22:48:41 +00006551 }
drh23e11ca2004-05-04 17:27:28 +00006552 *pMeta = get4byte(&pP1[36 + idx*4]);
danielk1977ea897302008-09-19 15:10:58 +00006553
danielk1977d9f6c532008-09-19 16:39:38 +00006554 /* If the b-tree is not holding a reference to page 1, then one was
6555 ** requested from the pager layer in the above block. Release it now.
6556 */
danielk1977ea897302008-09-19 15:10:58 +00006557 if( !pBt->pPage1 ){
6558 sqlite3PagerUnref(pDbPage);
6559 }
drhae157872004-08-14 19:20:09 +00006560
danielk1977599fcba2004-11-08 07:13:13 +00006561 /* If autovacuumed is disabled in this build but we are trying to
6562 ** access an autovacuumed database, then make the database readonly.
6563 */
danielk1977003ba062004-11-04 02:57:33 +00006564#ifdef SQLITE_OMIT_AUTOVACUUM
drhae157872004-08-14 19:20:09 +00006565 if( idx==4 && *pMeta>0 ) pBt->readOnly = 1;
danielk1977003ba062004-11-04 02:57:33 +00006566#endif
drhae157872004-08-14 19:20:09 +00006567
danielk1977da184232006-01-05 11:34:32 +00006568 /* Grab the read-lock on page 1. */
6569 rc = lockTable(p, 1, READ_LOCK);
drhd677b3d2007-08-20 22:48:41 +00006570 sqlite3BtreeLeave(p);
danielk1977da184232006-01-05 11:34:32 +00006571 return rc;
drh8b2f49b2001-06-08 00:21:52 +00006572}
6573
6574/*
drh23e11ca2004-05-04 17:27:28 +00006575** Write meta-information back into the database. Meta[0] is
6576** read-only and may not be written.
drh8b2f49b2001-06-08 00:21:52 +00006577*/
danielk1977aef0bf62005-12-30 16:28:01 +00006578int sqlite3BtreeUpdateMeta(Btree *p, int idx, u32 iMeta){
6579 BtShared *pBt = p->pBt;
drh4b70f112004-05-02 21:12:19 +00006580 unsigned char *pP1;
drha34b6762004-05-07 13:30:42 +00006581 int rc;
drh23e11ca2004-05-04 17:27:28 +00006582 assert( idx>=1 && idx<=15 );
drhd677b3d2007-08-20 22:48:41 +00006583 sqlite3BtreeEnter(p);
drhe5fe6902007-12-07 18:55:28 +00006584 pBt->db = p->db;
drh64022502009-01-09 14:11:04 +00006585 assert( p->inTrans==TRANS_WRITE );
6586 assert( pBt->pPage1!=0 );
6587 pP1 = pBt->pPage1->aData;
6588 rc = sqlite3PagerWrite(pBt->pPage1->pDbPage);
6589 if( rc==SQLITE_OK ){
6590 put4byte(&pP1[36 + idx*4], iMeta);
danielk19774152e672007-09-12 17:01:45 +00006591#ifndef SQLITE_OMIT_AUTOVACUUM
drh64022502009-01-09 14:11:04 +00006592 if( idx==7 ){
6593 assert( pBt->autoVacuum || iMeta==0 );
6594 assert( iMeta==0 || iMeta==1 );
6595 pBt->incrVacuum = (u8)iMeta;
drhd677b3d2007-08-20 22:48:41 +00006596 }
drh64022502009-01-09 14:11:04 +00006597#endif
drh5df72a52002-06-06 23:16:05 +00006598 }
drhd677b3d2007-08-20 22:48:41 +00006599 sqlite3BtreeLeave(p);
6600 return rc;
drh8b2f49b2001-06-08 00:21:52 +00006601}
drh8c42ca92001-06-22 19:15:00 +00006602
drhf328bc82004-05-10 23:29:49 +00006603/*
6604** Return the flag byte at the beginning of the page that the cursor
6605** is currently pointing to.
6606*/
6607int sqlite3BtreeFlags(BtCursor *pCur){
danielk1977da184232006-01-05 11:34:32 +00006608 /* TODO: What about CURSOR_REQUIRESEEK state? Probably need to call
drha3460582008-07-11 21:02:53 +00006609 ** restoreCursorPosition() here.
danielk1977da184232006-01-05 11:34:32 +00006610 */
danielk1977e448dc42008-01-02 11:50:51 +00006611 MemPage *pPage;
drha3460582008-07-11 21:02:53 +00006612 restoreCursorPosition(pCur);
danielk197771d5d2c2008-09-29 11:49:47 +00006613 pPage = pCur->apPage[pCur->iPage];
drh1fee73e2007-08-29 04:00:57 +00006614 assert( cursorHoldsMutex(pCur) );
drh64022502009-01-09 14:11:04 +00006615 assert( pPage!=0 );
drhd0679ed2007-08-28 22:24:34 +00006616 assert( pPage->pBt==pCur->pBt );
drh64022502009-01-09 14:11:04 +00006617 return pPage->aData[pPage->hdrOffset];
drhf328bc82004-05-10 23:29:49 +00006618}
6619
drhdd793422001-06-28 01:54:48 +00006620
drhdd793422001-06-28 01:54:48 +00006621/*
drh5eddca62001-06-30 21:53:53 +00006622** Return the pager associated with a BTree. This routine is used for
6623** testing and debugging only.
drhdd793422001-06-28 01:54:48 +00006624*/
danielk1977aef0bf62005-12-30 16:28:01 +00006625Pager *sqlite3BtreePager(Btree *p){
6626 return p->pBt->pPager;
drhdd793422001-06-28 01:54:48 +00006627}
drh5eddca62001-06-30 21:53:53 +00006628
drhb7f91642004-10-31 02:22:47 +00006629#ifndef SQLITE_OMIT_INTEGRITY_CHECK
drh5eddca62001-06-30 21:53:53 +00006630/*
6631** Append a message to the error message string.
6632*/
drh2e38c322004-09-03 18:38:44 +00006633static void checkAppendMsg(
6634 IntegrityCk *pCheck,
6635 char *zMsg1,
6636 const char *zFormat,
6637 ...
6638){
6639 va_list ap;
drh1dcdbc02007-01-27 02:24:54 +00006640 if( !pCheck->mxErr ) return;
6641 pCheck->mxErr--;
6642 pCheck->nErr++;
drh2e38c322004-09-03 18:38:44 +00006643 va_start(ap, zFormat);
drhf089aa42008-07-08 19:34:06 +00006644 if( pCheck->errMsg.nChar ){
6645 sqlite3StrAccumAppend(&pCheck->errMsg, "\n", 1);
drh5eddca62001-06-30 21:53:53 +00006646 }
drhf089aa42008-07-08 19:34:06 +00006647 if( zMsg1 ){
6648 sqlite3StrAccumAppend(&pCheck->errMsg, zMsg1, -1);
6649 }
6650 sqlite3VXPrintf(&pCheck->errMsg, 1, zFormat, ap);
6651 va_end(ap);
drhc890fec2008-08-01 20:10:08 +00006652 if( pCheck->errMsg.mallocFailed ){
6653 pCheck->mallocFailed = 1;
6654 }
drh5eddca62001-06-30 21:53:53 +00006655}
drhb7f91642004-10-31 02:22:47 +00006656#endif /* SQLITE_OMIT_INTEGRITY_CHECK */
drh5eddca62001-06-30 21:53:53 +00006657
drhb7f91642004-10-31 02:22:47 +00006658#ifndef SQLITE_OMIT_INTEGRITY_CHECK
drh5eddca62001-06-30 21:53:53 +00006659/*
6660** Add 1 to the reference count for page iPage. If this is the second
6661** reference to the page, add an error message to pCheck->zErrMsg.
6662** Return 1 if there are 2 ore more references to the page and 0 if
6663** if this is the first reference to the page.
6664**
6665** Also check that the page number is in bounds.
6666*/
danielk197789d40042008-11-17 14:20:56 +00006667static int checkRef(IntegrityCk *pCheck, Pgno iPage, char *zContext){
drh5eddca62001-06-30 21:53:53 +00006668 if( iPage==0 ) return 1;
danielk197789d40042008-11-17 14:20:56 +00006669 if( iPage>pCheck->nPage ){
drh2e38c322004-09-03 18:38:44 +00006670 checkAppendMsg(pCheck, zContext, "invalid page number %d", iPage);
drh5eddca62001-06-30 21:53:53 +00006671 return 1;
6672 }
6673 if( pCheck->anRef[iPage]==1 ){
drh2e38c322004-09-03 18:38:44 +00006674 checkAppendMsg(pCheck, zContext, "2nd reference to page %d", iPage);
drh5eddca62001-06-30 21:53:53 +00006675 return 1;
6676 }
6677 return (pCheck->anRef[iPage]++)>1;
6678}
6679
danielk1977afcdd022004-10-31 16:25:42 +00006680#ifndef SQLITE_OMIT_AUTOVACUUM
6681/*
6682** Check that the entry in the pointer-map for page iChild maps to
6683** page iParent, pointer type ptrType. If not, append an error message
6684** to pCheck.
6685*/
6686static void checkPtrmap(
6687 IntegrityCk *pCheck, /* Integrity check context */
6688 Pgno iChild, /* Child page number */
6689 u8 eType, /* Expected pointer map type */
6690 Pgno iParent, /* Expected pointer map parent page number */
6691 char *zContext /* Context description (used for error msg) */
6692){
6693 int rc;
6694 u8 ePtrmapType;
6695 Pgno iPtrmapParent;
6696
6697 rc = ptrmapGet(pCheck->pBt, iChild, &ePtrmapType, &iPtrmapParent);
6698 if( rc!=SQLITE_OK ){
drhe43ba702008-12-05 22:40:08 +00006699 if( rc==SQLITE_NOMEM ) pCheck->mallocFailed = 1;
danielk1977afcdd022004-10-31 16:25:42 +00006700 checkAppendMsg(pCheck, zContext, "Failed to read ptrmap key=%d", iChild);
6701 return;
6702 }
6703
6704 if( ePtrmapType!=eType || iPtrmapParent!=iParent ){
6705 checkAppendMsg(pCheck, zContext,
6706 "Bad ptr map entry key=%d expected=(%d,%d) got=(%d,%d)",
6707 iChild, eType, iParent, ePtrmapType, iPtrmapParent);
6708 }
6709}
6710#endif
6711
drh5eddca62001-06-30 21:53:53 +00006712/*
6713** Check the integrity of the freelist or of an overflow page list.
6714** Verify that the number of pages on the list is N.
6715*/
drh30e58752002-03-02 20:41:57 +00006716static void checkList(
6717 IntegrityCk *pCheck, /* Integrity checking context */
6718 int isFreeList, /* True for a freelist. False for overflow page list */
6719 int iPage, /* Page number for first page in the list */
6720 int N, /* Expected number of pages in the list */
6721 char *zContext /* Context for error messages */
6722){
6723 int i;
drh3a4c1412004-05-09 20:40:11 +00006724 int expected = N;
6725 int iFirst = iPage;
drh1dcdbc02007-01-27 02:24:54 +00006726 while( N-- > 0 && pCheck->mxErr ){
danielk19773b8a05f2007-03-19 17:44:26 +00006727 DbPage *pOvflPage;
6728 unsigned char *pOvflData;
drh5eddca62001-06-30 21:53:53 +00006729 if( iPage<1 ){
drh2e38c322004-09-03 18:38:44 +00006730 checkAppendMsg(pCheck, zContext,
6731 "%d of %d pages missing from overflow list starting at %d",
drh3a4c1412004-05-09 20:40:11 +00006732 N+1, expected, iFirst);
drh5eddca62001-06-30 21:53:53 +00006733 break;
6734 }
6735 if( checkRef(pCheck, iPage, zContext) ) break;
danielk19773b8a05f2007-03-19 17:44:26 +00006736 if( sqlite3PagerGet(pCheck->pPager, (Pgno)iPage, &pOvflPage) ){
drh2e38c322004-09-03 18:38:44 +00006737 checkAppendMsg(pCheck, zContext, "failed to get page %d", iPage);
drh5eddca62001-06-30 21:53:53 +00006738 break;
6739 }
danielk19773b8a05f2007-03-19 17:44:26 +00006740 pOvflData = (unsigned char *)sqlite3PagerGetData(pOvflPage);
drh30e58752002-03-02 20:41:57 +00006741 if( isFreeList ){
danielk19773b8a05f2007-03-19 17:44:26 +00006742 int n = get4byte(&pOvflData[4]);
danielk1977687566d2004-11-02 12:56:41 +00006743#ifndef SQLITE_OMIT_AUTOVACUUM
6744 if( pCheck->pBt->autoVacuum ){
6745 checkPtrmap(pCheck, iPage, PTRMAP_FREEPAGE, 0, zContext);
6746 }
6747#endif
drh45b1fac2008-07-04 17:52:42 +00006748 if( n>pCheck->pBt->usableSize/4-2 ){
drh2e38c322004-09-03 18:38:44 +00006749 checkAppendMsg(pCheck, zContext,
6750 "freelist leaf count too big on page %d", iPage);
drhee696e22004-08-30 16:52:17 +00006751 N--;
6752 }else{
6753 for(i=0; i<n; i++){
danielk19773b8a05f2007-03-19 17:44:26 +00006754 Pgno iFreePage = get4byte(&pOvflData[8+i*4]);
danielk1977687566d2004-11-02 12:56:41 +00006755#ifndef SQLITE_OMIT_AUTOVACUUM
6756 if( pCheck->pBt->autoVacuum ){
6757 checkPtrmap(pCheck, iFreePage, PTRMAP_FREEPAGE, 0, zContext);
6758 }
6759#endif
6760 checkRef(pCheck, iFreePage, zContext);
drhee696e22004-08-30 16:52:17 +00006761 }
6762 N -= n;
drh30e58752002-03-02 20:41:57 +00006763 }
drh30e58752002-03-02 20:41:57 +00006764 }
danielk1977afcdd022004-10-31 16:25:42 +00006765#ifndef SQLITE_OMIT_AUTOVACUUM
danielk1977687566d2004-11-02 12:56:41 +00006766 else{
6767 /* If this database supports auto-vacuum and iPage is not the last
6768 ** page in this overflow list, check that the pointer-map entry for
6769 ** the following page matches iPage.
6770 */
6771 if( pCheck->pBt->autoVacuum && N>0 ){
danielk19773b8a05f2007-03-19 17:44:26 +00006772 i = get4byte(pOvflData);
danielk1977687566d2004-11-02 12:56:41 +00006773 checkPtrmap(pCheck, i, PTRMAP_OVERFLOW2, iPage, zContext);
6774 }
danielk1977afcdd022004-10-31 16:25:42 +00006775 }
6776#endif
danielk19773b8a05f2007-03-19 17:44:26 +00006777 iPage = get4byte(pOvflData);
6778 sqlite3PagerUnref(pOvflPage);
drh5eddca62001-06-30 21:53:53 +00006779 }
6780}
drhb7f91642004-10-31 02:22:47 +00006781#endif /* SQLITE_OMIT_INTEGRITY_CHECK */
drh5eddca62001-06-30 21:53:53 +00006782
drhb7f91642004-10-31 02:22:47 +00006783#ifndef SQLITE_OMIT_INTEGRITY_CHECK
drh5eddca62001-06-30 21:53:53 +00006784/*
6785** Do various sanity checks on a single page of a tree. Return
6786** the tree depth. Root pages return 0. Parents of root pages
6787** return 1, and so forth.
6788**
6789** These checks are done:
6790**
6791** 1. Make sure that cells and freeblocks do not overlap
6792** but combine to completely cover the page.
drhda200cc2004-05-09 11:51:38 +00006793** NO 2. Make sure cell keys are in order.
6794** NO 3. Make sure no key is less than or equal to zLowerBound.
6795** NO 4. Make sure no key is greater than or equal to zUpperBound.
drh5eddca62001-06-30 21:53:53 +00006796** 5. Check the integrity of overflow pages.
6797** 6. Recursively call checkTreePage on all children.
6798** 7. Verify that the depth of all children is the same.
drh6019e162001-07-02 17:51:45 +00006799** 8. Make sure this page is at least 33% full or else it is
drh5eddca62001-06-30 21:53:53 +00006800** the root of the tree.
6801*/
6802static int checkTreePage(
drhaaab5722002-02-19 13:39:21 +00006803 IntegrityCk *pCheck, /* Context for the sanity check */
drh5eddca62001-06-30 21:53:53 +00006804 int iPage, /* Page number of the page to check */
drh74161702006-02-24 02:53:49 +00006805 char *zParentContext /* Parent context */
drh5eddca62001-06-30 21:53:53 +00006806){
6807 MemPage *pPage;
drhda200cc2004-05-09 11:51:38 +00006808 int i, rc, depth, d2, pgno, cnt;
drh43605152004-05-29 21:46:49 +00006809 int hdr, cellStart;
6810 int nCell;
drhda200cc2004-05-09 11:51:38 +00006811 u8 *data;
danielk1977aef0bf62005-12-30 16:28:01 +00006812 BtShared *pBt;
drh4f26bb62005-09-08 14:17:20 +00006813 int usableSize;
drh5eddca62001-06-30 21:53:53 +00006814 char zContext[100];
shane0af3f892008-11-12 04:55:34 +00006815 char *hit = 0;
drh5eddca62001-06-30 21:53:53 +00006816
drh5bb3eb92007-05-04 13:15:55 +00006817 sqlite3_snprintf(sizeof(zContext), zContext, "Page %d: ", iPage);
danielk1977ef73ee92004-11-06 12:26:07 +00006818
drh5eddca62001-06-30 21:53:53 +00006819 /* Check that the page exists
6820 */
drhd9cb6ac2005-10-20 07:28:17 +00006821 pBt = pCheck->pBt;
drhb6f41482004-05-14 01:58:11 +00006822 usableSize = pBt->usableSize;
drh5eddca62001-06-30 21:53:53 +00006823 if( iPage==0 ) return 0;
6824 if( checkRef(pCheck, iPage, zParentContext) ) return 0;
drh16a9b832007-05-05 18:39:25 +00006825 if( (rc = sqlite3BtreeGetPage(pBt, (Pgno)iPage, &pPage, 0))!=0 ){
drhe43ba702008-12-05 22:40:08 +00006826 if( rc==SQLITE_NOMEM ) pCheck->mallocFailed = 1;
drh2e38c322004-09-03 18:38:44 +00006827 checkAppendMsg(pCheck, zContext,
6828 "unable to get the page. error code=%d", rc);
drh5eddca62001-06-30 21:53:53 +00006829 return 0;
6830 }
danielk197771d5d2c2008-09-29 11:49:47 +00006831 if( (rc = sqlite3BtreeInitPage(pPage))!=0 ){
drh64022502009-01-09 14:11:04 +00006832 assert( rc==SQLITE_CORRUPT ); /* The only possible error from InitPage */
drh16a9b832007-05-05 18:39:25 +00006833 checkAppendMsg(pCheck, zContext,
6834 "sqlite3BtreeInitPage() returns error code %d", rc);
drh91025292004-05-03 19:49:32 +00006835 releasePage(pPage);
drh5eddca62001-06-30 21:53:53 +00006836 return 0;
6837 }
6838
6839 /* Check out all the cells.
6840 */
6841 depth = 0;
drh1dcdbc02007-01-27 02:24:54 +00006842 for(i=0; i<pPage->nCell && pCheck->mxErr; i++){
drh6f11bef2004-05-13 01:12:56 +00006843 u8 *pCell;
danielk197789d40042008-11-17 14:20:56 +00006844 u32 sz;
drh6f11bef2004-05-13 01:12:56 +00006845 CellInfo info;
drh5eddca62001-06-30 21:53:53 +00006846
6847 /* Check payload overflow pages
6848 */
drh5bb3eb92007-05-04 13:15:55 +00006849 sqlite3_snprintf(sizeof(zContext), zContext,
6850 "On tree page %d cell %d: ", iPage, i);
danielk19771cc5ed82007-05-16 17:28:43 +00006851 pCell = findCell(pPage,i);
drh16a9b832007-05-05 18:39:25 +00006852 sqlite3BtreeParseCellPtr(pPage, pCell, &info);
drh6f11bef2004-05-13 01:12:56 +00006853 sz = info.nData;
drhf49661a2008-12-10 16:45:50 +00006854 if( !pPage->intKey ) sz += (int)info.nKey;
drh72365832007-03-06 15:53:44 +00006855 assert( sz==info.nPayload );
drh6f11bef2004-05-13 01:12:56 +00006856 if( sz>info.nLocal ){
drhb6f41482004-05-14 01:58:11 +00006857 int nPage = (sz - info.nLocal + usableSize - 5)/(usableSize - 4);
danielk1977afcdd022004-10-31 16:25:42 +00006858 Pgno pgnoOvfl = get4byte(&pCell[info.iOverflow]);
6859#ifndef SQLITE_OMIT_AUTOVACUUM
6860 if( pBt->autoVacuum ){
danielk1977687566d2004-11-02 12:56:41 +00006861 checkPtrmap(pCheck, pgnoOvfl, PTRMAP_OVERFLOW1, iPage, zContext);
danielk1977afcdd022004-10-31 16:25:42 +00006862 }
6863#endif
6864 checkList(pCheck, 0, pgnoOvfl, nPage, zContext);
drh5eddca62001-06-30 21:53:53 +00006865 }
6866
6867 /* Check sanity of left child page.
6868 */
drhda200cc2004-05-09 11:51:38 +00006869 if( !pPage->leaf ){
drh43605152004-05-29 21:46:49 +00006870 pgno = get4byte(pCell);
danielk1977afcdd022004-10-31 16:25:42 +00006871#ifndef SQLITE_OMIT_AUTOVACUUM
6872 if( pBt->autoVacuum ){
6873 checkPtrmap(pCheck, pgno, PTRMAP_BTREE, iPage, zContext);
6874 }
6875#endif
danielk197762c14b32008-11-19 09:05:26 +00006876 d2 = checkTreePage(pCheck, pgno, zContext);
drhda200cc2004-05-09 11:51:38 +00006877 if( i>0 && d2!=depth ){
6878 checkAppendMsg(pCheck, zContext, "Child page depth differs");
6879 }
6880 depth = d2;
drh5eddca62001-06-30 21:53:53 +00006881 }
drh5eddca62001-06-30 21:53:53 +00006882 }
drhda200cc2004-05-09 11:51:38 +00006883 if( !pPage->leaf ){
drh43605152004-05-29 21:46:49 +00006884 pgno = get4byte(&pPage->aData[pPage->hdrOffset+8]);
drh5bb3eb92007-05-04 13:15:55 +00006885 sqlite3_snprintf(sizeof(zContext), zContext,
6886 "On page %d at right child: ", iPage);
danielk1977afcdd022004-10-31 16:25:42 +00006887#ifndef SQLITE_OMIT_AUTOVACUUM
6888 if( pBt->autoVacuum ){
danielk1977687566d2004-11-02 12:56:41 +00006889 checkPtrmap(pCheck, pgno, PTRMAP_BTREE, iPage, 0);
danielk1977afcdd022004-10-31 16:25:42 +00006890 }
6891#endif
danielk197762c14b32008-11-19 09:05:26 +00006892 checkTreePage(pCheck, pgno, zContext);
drhda200cc2004-05-09 11:51:38 +00006893 }
drh5eddca62001-06-30 21:53:53 +00006894
6895 /* Check for complete coverage of the page
6896 */
drhda200cc2004-05-09 11:51:38 +00006897 data = pPage->aData;
6898 hdr = pPage->hdrOffset;
drhf7141992008-06-19 00:16:08 +00006899 hit = sqlite3PageMalloc( pBt->pageSize );
drhc890fec2008-08-01 20:10:08 +00006900 if( hit==0 ){
6901 pCheck->mallocFailed = 1;
6902 }else{
shane5780ebd2008-11-11 17:36:30 +00006903 u16 contentOffset = get2byte(&data[hdr+5]);
6904 if (contentOffset > usableSize) {
6905 checkAppendMsg(pCheck, 0,
6906 "Corruption detected in header on page %d",iPage,0);
shane0af3f892008-11-12 04:55:34 +00006907 goto check_page_abort;
shane5780ebd2008-11-11 17:36:30 +00006908 }
6909 memset(hit+contentOffset, 0, usableSize-contentOffset);
6910 memset(hit, 1, contentOffset);
drh2e38c322004-09-03 18:38:44 +00006911 nCell = get2byte(&data[hdr+3]);
6912 cellStart = hdr + 12 - 4*pPage->leaf;
6913 for(i=0; i<nCell; i++){
6914 int pc = get2byte(&data[cellStart+i*2]);
danielk1977daca5432008-08-25 11:57:16 +00006915 u16 size = 1024;
drh2e38c322004-09-03 18:38:44 +00006916 int j;
danielk1977daca5432008-08-25 11:57:16 +00006917 if( pc<=usableSize ){
6918 size = cellSizePtr(pPage, &data[pc]);
6919 }
danielk19777701e812005-01-10 12:59:51 +00006920 if( (pc+size-1)>=usableSize || pc<0 ){
6921 checkAppendMsg(pCheck, 0,
6922 "Corruption detected in cell %d on page %d",i,iPage,0);
6923 }else{
6924 for(j=pc+size-1; j>=pc; j--) hit[j]++;
6925 }
drh2e38c322004-09-03 18:38:44 +00006926 }
6927 for(cnt=0, i=get2byte(&data[hdr+1]); i>0 && i<usableSize && cnt<10000;
6928 cnt++){
6929 int size = get2byte(&data[i+2]);
6930 int j;
danielk19777701e812005-01-10 12:59:51 +00006931 if( (i+size-1)>=usableSize || i<0 ){
6932 checkAppendMsg(pCheck, 0,
6933 "Corruption detected in cell %d on page %d",i,iPage,0);
6934 }else{
6935 for(j=i+size-1; j>=i; j--) hit[j]++;
6936 }
drh2e38c322004-09-03 18:38:44 +00006937 i = get2byte(&data[i]);
6938 }
6939 for(i=cnt=0; i<usableSize; i++){
6940 if( hit[i]==0 ){
6941 cnt++;
6942 }else if( hit[i]>1 ){
6943 checkAppendMsg(pCheck, 0,
6944 "Multiple uses for byte %d of page %d", i, iPage);
6945 break;
6946 }
6947 }
6948 if( cnt!=data[hdr+7] ){
6949 checkAppendMsg(pCheck, 0,
6950 "Fragmented space is %d byte reported as %d on page %d",
6951 cnt, data[hdr+7], iPage);
drh5eddca62001-06-30 21:53:53 +00006952 }
6953 }
shane0af3f892008-11-12 04:55:34 +00006954check_page_abort:
6955 if (hit) sqlite3PageFree(hit);
drh6019e162001-07-02 17:51:45 +00006956
drh4b70f112004-05-02 21:12:19 +00006957 releasePage(pPage);
drhda200cc2004-05-09 11:51:38 +00006958 return depth+1;
drh5eddca62001-06-30 21:53:53 +00006959}
drhb7f91642004-10-31 02:22:47 +00006960#endif /* SQLITE_OMIT_INTEGRITY_CHECK */
drh5eddca62001-06-30 21:53:53 +00006961
drhb7f91642004-10-31 02:22:47 +00006962#ifndef SQLITE_OMIT_INTEGRITY_CHECK
drh5eddca62001-06-30 21:53:53 +00006963/*
6964** This routine does a complete check of the given BTree file. aRoot[] is
6965** an array of pages numbers were each page number is the root page of
6966** a table. nRoot is the number of entries in aRoot.
6967**
drhc890fec2008-08-01 20:10:08 +00006968** Write the number of error seen in *pnErr. Except for some memory
drhe43ba702008-12-05 22:40:08 +00006969** allocation errors, an error message held in memory obtained from
drhc890fec2008-08-01 20:10:08 +00006970** malloc is returned if *pnErr is non-zero. If *pnErr==0 then NULL is
drhe43ba702008-12-05 22:40:08 +00006971** returned. If a memory allocation error occurs, NULL is returned.
drh5eddca62001-06-30 21:53:53 +00006972*/
drh1dcdbc02007-01-27 02:24:54 +00006973char *sqlite3BtreeIntegrityCheck(
6974 Btree *p, /* The btree to be checked */
6975 int *aRoot, /* An array of root pages numbers for individual trees */
6976 int nRoot, /* Number of entries in aRoot[] */
6977 int mxErr, /* Stop reporting errors after this many */
6978 int *pnErr /* Write number of errors seen to this variable */
6979){
danielk197789d40042008-11-17 14:20:56 +00006980 Pgno i;
drh5eddca62001-06-30 21:53:53 +00006981 int nRef;
drhaaab5722002-02-19 13:39:21 +00006982 IntegrityCk sCheck;
danielk1977aef0bf62005-12-30 16:28:01 +00006983 BtShared *pBt = p->pBt;
drhf089aa42008-07-08 19:34:06 +00006984 char zErr[100];
drh5eddca62001-06-30 21:53:53 +00006985
drhd677b3d2007-08-20 22:48:41 +00006986 sqlite3BtreeEnter(p);
drhe5fe6902007-12-07 18:55:28 +00006987 pBt->db = p->db;
danielk19773b8a05f2007-03-19 17:44:26 +00006988 nRef = sqlite3PagerRefcount(pBt->pPager);
danielk1977aef0bf62005-12-30 16:28:01 +00006989 if( lockBtreeWithRetry(p)!=SQLITE_OK ){
drhc890fec2008-08-01 20:10:08 +00006990 *pnErr = 1;
drhd677b3d2007-08-20 22:48:41 +00006991 sqlite3BtreeLeave(p);
drhc890fec2008-08-01 20:10:08 +00006992 return sqlite3DbStrDup(0, "cannot acquire a read lock on the database");
drhefc251d2001-07-01 22:12:01 +00006993 }
drh5eddca62001-06-30 21:53:53 +00006994 sCheck.pBt = pBt;
6995 sCheck.pPager = pBt->pPager;
danielk197789d40042008-11-17 14:20:56 +00006996 sCheck.nPage = pagerPagecount(sCheck.pBt);
drh1dcdbc02007-01-27 02:24:54 +00006997 sCheck.mxErr = mxErr;
6998 sCheck.nErr = 0;
drhc890fec2008-08-01 20:10:08 +00006999 sCheck.mallocFailed = 0;
drh1dcdbc02007-01-27 02:24:54 +00007000 *pnErr = 0;
drh0de8c112002-07-06 16:32:14 +00007001 if( sCheck.nPage==0 ){
7002 unlockBtreeIfUnused(pBt);
drhd677b3d2007-08-20 22:48:41 +00007003 sqlite3BtreeLeave(p);
drh0de8c112002-07-06 16:32:14 +00007004 return 0;
7005 }
drhe5ae5732008-06-15 02:51:47 +00007006 sCheck.anRef = sqlite3Malloc( (sCheck.nPage+1)*sizeof(sCheck.anRef[0]) );
danielk1977ac245ec2005-01-14 13:50:11 +00007007 if( !sCheck.anRef ){
7008 unlockBtreeIfUnused(pBt);
drh1dcdbc02007-01-27 02:24:54 +00007009 *pnErr = 1;
drhd677b3d2007-08-20 22:48:41 +00007010 sqlite3BtreeLeave(p);
drhc890fec2008-08-01 20:10:08 +00007011 return 0;
danielk1977ac245ec2005-01-14 13:50:11 +00007012 }
drhda200cc2004-05-09 11:51:38 +00007013 for(i=0; i<=sCheck.nPage; i++){ sCheck.anRef[i] = 0; }
drh42cac6d2004-11-20 20:31:11 +00007014 i = PENDING_BYTE_PAGE(pBt);
drh1f595712004-06-15 01:40:29 +00007015 if( i<=sCheck.nPage ){
7016 sCheck.anRef[i] = 1;
7017 }
drhf089aa42008-07-08 19:34:06 +00007018 sqlite3StrAccumInit(&sCheck.errMsg, zErr, sizeof(zErr), 20000);
drh5eddca62001-06-30 21:53:53 +00007019
7020 /* Check the integrity of the freelist
7021 */
drha34b6762004-05-07 13:30:42 +00007022 checkList(&sCheck, 1, get4byte(&pBt->pPage1->aData[32]),
7023 get4byte(&pBt->pPage1->aData[36]), "Main freelist: ");
drh5eddca62001-06-30 21:53:53 +00007024
7025 /* Check all the tables.
7026 */
danielk197789d40042008-11-17 14:20:56 +00007027 for(i=0; (int)i<nRoot && sCheck.mxErr; i++){
drh4ff6dfa2002-03-03 23:06:00 +00007028 if( aRoot[i]==0 ) continue;
danielk1977687566d2004-11-02 12:56:41 +00007029#ifndef SQLITE_OMIT_AUTOVACUUM
danielk1977687566d2004-11-02 12:56:41 +00007030 if( pBt->autoVacuum && aRoot[i]>1 ){
7031 checkPtrmap(&sCheck, aRoot[i], PTRMAP_ROOTPAGE, 0, 0);
7032 }
7033#endif
danielk197762c14b32008-11-19 09:05:26 +00007034 checkTreePage(&sCheck, aRoot[i], "List of tree roots: ");
drh5eddca62001-06-30 21:53:53 +00007035 }
7036
7037 /* Make sure every page in the file is referenced
7038 */
drh1dcdbc02007-01-27 02:24:54 +00007039 for(i=1; i<=sCheck.nPage && sCheck.mxErr; i++){
danielk1977afcdd022004-10-31 16:25:42 +00007040#ifdef SQLITE_OMIT_AUTOVACUUM
drh5eddca62001-06-30 21:53:53 +00007041 if( sCheck.anRef[i]==0 ){
drh2e38c322004-09-03 18:38:44 +00007042 checkAppendMsg(&sCheck, 0, "Page %d is never used", i);
drh5eddca62001-06-30 21:53:53 +00007043 }
danielk1977afcdd022004-10-31 16:25:42 +00007044#else
7045 /* If the database supports auto-vacuum, make sure no tables contain
7046 ** references to pointer-map pages.
7047 */
7048 if( sCheck.anRef[i]==0 &&
danielk1977266664d2006-02-10 08:24:21 +00007049 (PTRMAP_PAGENO(pBt, i)!=i || !pBt->autoVacuum) ){
danielk1977afcdd022004-10-31 16:25:42 +00007050 checkAppendMsg(&sCheck, 0, "Page %d is never used", i);
7051 }
7052 if( sCheck.anRef[i]!=0 &&
danielk1977266664d2006-02-10 08:24:21 +00007053 (PTRMAP_PAGENO(pBt, i)==i && pBt->autoVacuum) ){
danielk1977afcdd022004-10-31 16:25:42 +00007054 checkAppendMsg(&sCheck, 0, "Pointer map page %d is referenced", i);
7055 }
7056#endif
drh5eddca62001-06-30 21:53:53 +00007057 }
7058
drh64022502009-01-09 14:11:04 +00007059 /* Make sure this analysis did not leave any unref() pages.
7060 ** This is an internal consistency check; an integrity check
7061 ** of the integrity check.
drh5eddca62001-06-30 21:53:53 +00007062 */
drh5e00f6c2001-09-13 13:46:56 +00007063 unlockBtreeIfUnused(pBt);
drh64022502009-01-09 14:11:04 +00007064 if( NEVER(nRef != sqlite3PagerRefcount(pBt->pPager)) ){
drh2e38c322004-09-03 18:38:44 +00007065 checkAppendMsg(&sCheck, 0,
drh5eddca62001-06-30 21:53:53 +00007066 "Outstanding page count goes from %d to %d during this analysis",
danielk19773b8a05f2007-03-19 17:44:26 +00007067 nRef, sqlite3PagerRefcount(pBt->pPager)
drh5eddca62001-06-30 21:53:53 +00007068 );
drh5eddca62001-06-30 21:53:53 +00007069 }
7070
7071 /* Clean up and report errors.
7072 */
drhd677b3d2007-08-20 22:48:41 +00007073 sqlite3BtreeLeave(p);
drh17435752007-08-16 04:30:38 +00007074 sqlite3_free(sCheck.anRef);
drhc890fec2008-08-01 20:10:08 +00007075 if( sCheck.mallocFailed ){
7076 sqlite3StrAccumReset(&sCheck.errMsg);
7077 *pnErr = sCheck.nErr+1;
7078 return 0;
7079 }
drh1dcdbc02007-01-27 02:24:54 +00007080 *pnErr = sCheck.nErr;
drhf089aa42008-07-08 19:34:06 +00007081 if( sCheck.nErr==0 ) sqlite3StrAccumReset(&sCheck.errMsg);
7082 return sqlite3StrAccumFinish(&sCheck.errMsg);
drh5eddca62001-06-30 21:53:53 +00007083}
drhb7f91642004-10-31 02:22:47 +00007084#endif /* SQLITE_OMIT_INTEGRITY_CHECK */
paulb95a8862003-04-01 21:16:41 +00007085
drh73509ee2003-04-06 20:44:45 +00007086/*
7087** Return the full pathname of the underlying database file.
drhd0679ed2007-08-28 22:24:34 +00007088**
7089** The pager filename is invariant as long as the pager is
7090** open so it is safe to access without the BtShared mutex.
drh73509ee2003-04-06 20:44:45 +00007091*/
danielk1977aef0bf62005-12-30 16:28:01 +00007092const char *sqlite3BtreeGetFilename(Btree *p){
7093 assert( p->pBt->pPager!=0 );
danielk19773b8a05f2007-03-19 17:44:26 +00007094 return sqlite3PagerFilename(p->pBt->pPager);
drh73509ee2003-04-06 20:44:45 +00007095}
7096
7097/*
danielk197745d68822009-01-16 16:23:38 +00007098** Return the pathname of the directory that contains the database file.
7099**
7100** The pager directory name is invariant as long as the pager is
7101** open so it is safe to access without the BtShared mutex.
7102*/
7103const char *sqlite3BtreeGetDirname(Btree *p){
7104 assert( p->pBt->pPager!=0 );
7105 return sqlite3PagerDirname(p->pBt->pPager);
7106}
7107
7108/*
danielk19775865e3d2004-06-14 06:03:57 +00007109** Return the pathname of the journal file for this database. The return
7110** value of this routine is the same regardless of whether the journal file
7111** has been created or not.
drhd0679ed2007-08-28 22:24:34 +00007112**
7113** The pager journal filename is invariant as long as the pager is
7114** open so it is safe to access without the BtShared mutex.
danielk19775865e3d2004-06-14 06:03:57 +00007115*/
danielk1977aef0bf62005-12-30 16:28:01 +00007116const char *sqlite3BtreeGetJournalname(Btree *p){
7117 assert( p->pBt->pPager!=0 );
danielk19773b8a05f2007-03-19 17:44:26 +00007118 return sqlite3PagerJournalname(p->pBt->pPager);
danielk19775865e3d2004-06-14 06:03:57 +00007119}
7120
drhb7f91642004-10-31 02:22:47 +00007121#ifndef SQLITE_OMIT_VACUUM
danielk19775865e3d2004-06-14 06:03:57 +00007122/*
drhf7c57532003-04-25 13:22:51 +00007123** Copy the complete content of pBtFrom into pBtTo. A transaction
7124** must be active for both files.
7125**
danielk1977f653d782008-03-20 11:04:21 +00007126** The size of file pTo may be reduced by this operation.
7127** If anything goes wrong, the transaction on pTo is rolled back.
7128**
7129** If successful, CommitPhaseOne() may be called on pTo before returning.
7130** The caller should finish committing the transaction on pTo by calling
7131** sqlite3BtreeCommit().
drh73509ee2003-04-06 20:44:45 +00007132*/
drhd677b3d2007-08-20 22:48:41 +00007133static int btreeCopyFile(Btree *pTo, Btree *pFrom){
drhf7c57532003-04-25 13:22:51 +00007134 int rc = SQLITE_OK;
danielk1977f653d782008-03-20 11:04:21 +00007135 Pgno i;
7136
7137 Pgno nFromPage; /* Number of pages in pFrom */
7138 Pgno nToPage; /* Number of pages in pTo */
7139 Pgno nNewPage; /* Number of pages in pTo after the copy */
7140
7141 Pgno iSkip; /* Pending byte page in pTo */
7142 int nToPageSize; /* Page size of pTo in bytes */
7143 int nFromPageSize; /* Page size of pFrom in bytes */
drhf7c57532003-04-25 13:22:51 +00007144
danielk1977aef0bf62005-12-30 16:28:01 +00007145 BtShared *pBtTo = pTo->pBt;
7146 BtShared *pBtFrom = pFrom->pBt;
drhe5fe6902007-12-07 18:55:28 +00007147 pBtTo->db = pTo->db;
7148 pBtFrom->db = pFrom->db;
danielk1977f653d782008-03-20 11:04:21 +00007149
7150 nToPageSize = pBtTo->pageSize;
7151 nFromPageSize = pBtFrom->pageSize;
danielk1977aef0bf62005-12-30 16:28:01 +00007152
drh64022502009-01-09 14:11:04 +00007153 assert( pTo->inTrans==TRANS_WRITE );
7154 assert( pFrom->inTrans==TRANS_WRITE );
7155 if( NEVER(pBtTo->pCursor) ){
danielk1977f653d782008-03-20 11:04:21 +00007156 return SQLITE_BUSY;
drhf7c57532003-04-25 13:22:51 +00007157 }
drh538f5702007-04-13 02:14:30 +00007158
danielk197789d40042008-11-17 14:20:56 +00007159 nToPage = pagerPagecount(pBtTo);
7160 nFromPage = pagerPagecount(pBtFrom);
danielk1977f653d782008-03-20 11:04:21 +00007161 iSkip = PENDING_BYTE_PAGE(pBtTo);
7162
7163 /* Variable nNewPage is the number of pages required to store the
7164 ** contents of pFrom using the current page-size of pTo.
drh538f5702007-04-13 02:14:30 +00007165 */
drhf49661a2008-12-10 16:45:50 +00007166 nNewPage = (Pgno)
7167 (((i64)nFromPage*(i64)nFromPageSize+(i64)nToPageSize-1)/(i64)nToPageSize);
danielk1977f653d782008-03-20 11:04:21 +00007168
7169 for(i=1; rc==SQLITE_OK && (i<=nToPage || i<=nNewPage); i++){
7170
7171 /* Journal the original page.
7172 **
7173 ** iSkip is the page number of the locking page (PENDING_BYTE_PAGE)
7174 ** in database *pTo (before the copy). This page is never written
7175 ** into the journal file. Unless i==iSkip or the page was not
7176 ** present in pTo before the copy operation, journal page i from pTo.
7177 */
7178 if( i!=iSkip && i<=nToPage ){
danielk19774abd5442008-05-05 15:26:50 +00007179 DbPage *pDbPage = 0;
danielk1977f653d782008-03-20 11:04:21 +00007180 rc = sqlite3PagerGet(pBtTo->pPager, i, &pDbPage);
danielk19774abd5442008-05-05 15:26:50 +00007181 if( rc==SQLITE_OK ){
7182 rc = sqlite3PagerWrite(pDbPage);
danielk1977df2566a2008-05-07 19:11:03 +00007183 if( rc==SQLITE_OK && i>nFromPage ){
7184 /* Yeah. It seems wierd to call DontWrite() right after Write(). But
7185 ** that is because the names of those procedures do not exactly
7186 ** represent what they do. Write() really means "put this page in the
7187 ** rollback journal and mark it as dirty so that it will be written
7188 ** to the database file later." DontWrite() undoes the second part of
7189 ** that and prevents the page from being written to the database. The
7190 ** page is still on the rollback journal, though. And that is the
7191 ** whole point of this block: to put pages on the rollback journal.
7192 */
danielk197745d68822009-01-16 16:23:38 +00007193 rc = sqlite3PagerDontWrite(pDbPage);
danielk1977df2566a2008-05-07 19:11:03 +00007194 }
7195 sqlite3PagerUnref(pDbPage);
danielk1977f653d782008-03-20 11:04:21 +00007196 }
danielk1977f653d782008-03-20 11:04:21 +00007197 }
7198
7199 /* Overwrite the data in page i of the target database */
7200 if( rc==SQLITE_OK && i!=iSkip && i<=nNewPage ){
7201
7202 DbPage *pToPage = 0;
7203 sqlite3_int64 iOff;
7204
7205 rc = sqlite3PagerGet(pBtTo->pPager, i, &pToPage);
7206 if( rc==SQLITE_OK ){
7207 rc = sqlite3PagerWrite(pToPage);
7208 }
7209
7210 for(
7211 iOff=(i-1)*nToPageSize;
7212 rc==SQLITE_OK && iOff<i*nToPageSize;
7213 iOff += nFromPageSize
7214 ){
7215 DbPage *pFromPage = 0;
drhf49661a2008-12-10 16:45:50 +00007216 Pgno iFrom = (Pgno)(iOff/nFromPageSize)+1;
danielk1977f653d782008-03-20 11:04:21 +00007217
7218 if( iFrom==PENDING_BYTE_PAGE(pBtFrom) ){
7219 continue;
7220 }
7221
7222 rc = sqlite3PagerGet(pBtFrom->pPager, iFrom, &pFromPage);
7223 if( rc==SQLITE_OK ){
7224 char *zTo = sqlite3PagerGetData(pToPage);
7225 char *zFrom = sqlite3PagerGetData(pFromPage);
7226 int nCopy;
7227
7228 if( nFromPageSize>=nToPageSize ){
7229 zFrom += ((i-1)*nToPageSize - ((iFrom-1)*nFromPageSize));
7230 nCopy = nToPageSize;
7231 }else{
7232 zTo += (((iFrom-1)*nFromPageSize) - (i-1)*nToPageSize);
7233 nCopy = nFromPageSize;
7234 }
7235
7236 memcpy(zTo, zFrom, nCopy);
danielk19772f78fc62008-09-30 09:31:45 +00007237 sqlite3PagerUnref(pFromPage);
danielk1977f653d782008-03-20 11:04:21 +00007238 }
7239 }
7240
danielk1977eaa06f62008-09-18 17:34:44 +00007241 if( pToPage ){
7242 MemPage *p = (MemPage *)sqlite3PagerGetExtra(pToPage);
7243 p->isInit = 0;
7244 sqlite3PagerUnref(pToPage);
7245 }
danielk1977f653d782008-03-20 11:04:21 +00007246 }
drh2e6d11b2003-04-25 15:37:57 +00007247 }
danielk1977f653d782008-03-20 11:04:21 +00007248
7249 /* If things have worked so far, the database file may need to be
7250 ** truncated. The complex part is that it may need to be truncated to
7251 ** a size that is not an integer multiple of nToPageSize - the current
7252 ** page size used by the pager associated with B-Tree pTo.
7253 **
7254 ** For example, say the page-size of pTo is 2048 bytes and the original
7255 ** number of pages is 5 (10 KB file). If pFrom has a page size of 1024
7256 ** bytes and 9 pages, then the file needs to be truncated to 9KB.
7257 */
7258 if( rc==SQLITE_OK ){
danielk1977076dce52009-01-06 18:21:08 +00007259 sqlite3_file *pFile = sqlite3PagerFile(pBtTo->pPager);
7260 i64 iSize = (i64)nFromPageSize * (i64)nFromPage;
7261 i64 iNow = (i64)((nToPage>nNewPage)?nToPage:nNewPage) * (i64)nToPageSize;
7262 i64 iPending = ((i64)PENDING_BYTE_PAGE(pBtTo)-1) *(i64)nToPageSize;
7263
7264 assert( iSize<=iNow );
7265
7266 /* Commit phase one syncs the journal file associated with pTo
7267 ** containing the original data. It does not sync the database file
7268 ** itself. After doing this it is safe to use OsTruncate() and other
7269 ** file APIs on the database file directly.
7270 */
7271 pBtTo->db = pTo->db;
7272 rc = sqlite3PagerCommitPhaseOne(pBtTo->pPager, 0, 1);
7273 if( iSize<iNow && rc==SQLITE_OK ){
7274 rc = sqlite3OsTruncate(pFile, iSize);
7275 }
7276
7277 /* The loop that copied data from database pFrom to pTo did not
7278 ** populate the locking page of database pTo. If the page-size of
7279 ** pFrom is smaller than that of pTo, this means some data will
7280 ** not have been copied.
7281 **
7282 ** This block copies the missing data from database pFrom to pTo
7283 ** using file APIs. This is safe because at this point we know that
7284 ** all of the original data from pTo has been synced into the
7285 ** journal file. At this point it would be safe to do anything at
7286 ** all to the database file except truncate it to zero bytes.
7287 */
7288 if( rc==SQLITE_OK && nFromPageSize<nToPageSize && iSize>iPending){
7289 i64 iOff;
7290 for(
7291 iOff=iPending;
7292 rc==SQLITE_OK && iOff<(iPending+nToPageSize);
7293 iOff += nFromPageSize
7294 ){
7295 DbPage *pFromPage = 0;
7296 Pgno iFrom = (Pgno)(iOff/nFromPageSize)+1;
7297
7298 if( iFrom==PENDING_BYTE_PAGE(pBtFrom) || iFrom>nFromPage ){
7299 continue;
7300 }
7301
7302 rc = sqlite3PagerGet(pBtFrom->pPager, iFrom, &pFromPage);
7303 if( rc==SQLITE_OK ){
7304 char *zFrom = sqlite3PagerGetData(pFromPage);
7305 rc = sqlite3OsWrite(pFile, zFrom, nFromPageSize, iOff);
7306 sqlite3PagerUnref(pFromPage);
danielk1977f653d782008-03-20 11:04:21 +00007307 }
7308 }
danielk1977f653d782008-03-20 11:04:21 +00007309 }
drh2e6d11b2003-04-25 15:37:57 +00007310 }
drh538f5702007-04-13 02:14:30 +00007311
danielk1977076dce52009-01-06 18:21:08 +00007312 /* Sync the database file */
7313 if( rc==SQLITE_OK ){
7314 rc = sqlite3PagerSync(pBtTo->pPager);
7315 }
7316 if( rc==SQLITE_OK ){
7317 pBtTo->pageSizeFixed = 0;
7318 }else{
danielk1977aef0bf62005-12-30 16:28:01 +00007319 sqlite3BtreeRollback(pTo);
drhf7c57532003-04-25 13:22:51 +00007320 }
danielk1977f653d782008-03-20 11:04:21 +00007321
danielk1977076dce52009-01-06 18:21:08 +00007322 return rc;
drh73509ee2003-04-06 20:44:45 +00007323}
drhd677b3d2007-08-20 22:48:41 +00007324int sqlite3BtreeCopyFile(Btree *pTo, Btree *pFrom){
7325 int rc;
7326 sqlite3BtreeEnter(pTo);
7327 sqlite3BtreeEnter(pFrom);
7328 rc = btreeCopyFile(pTo, pFrom);
7329 sqlite3BtreeLeave(pFrom);
7330 sqlite3BtreeLeave(pTo);
7331 return rc;
7332}
7333
drhb7f91642004-10-31 02:22:47 +00007334#endif /* SQLITE_OMIT_VACUUM */
danielk19771d850a72004-05-31 08:26:49 +00007335
7336/*
7337** Return non-zero if a transaction is active.
7338*/
danielk1977aef0bf62005-12-30 16:28:01 +00007339int sqlite3BtreeIsInTrans(Btree *p){
drhe5fe6902007-12-07 18:55:28 +00007340 assert( p==0 || sqlite3_mutex_held(p->db->mutex) );
danielk1977aef0bf62005-12-30 16:28:01 +00007341 return (p && (p->inTrans==TRANS_WRITE));
danielk19771d850a72004-05-31 08:26:49 +00007342}
7343
7344/*
7345** Return non-zero if a statement transaction is active.
7346*/
danielk1977aef0bf62005-12-30 16:28:01 +00007347int sqlite3BtreeIsInStmt(Btree *p){
drh1fee73e2007-08-29 04:00:57 +00007348 assert( sqlite3BtreeHoldsMutex(p) );
drh64022502009-01-09 14:11:04 +00007349 return ALWAYS(p->pBt) && p->pBt->inStmt;
danielk19771d850a72004-05-31 08:26:49 +00007350}
danielk197713adf8a2004-06-03 16:08:41 +00007351
7352/*
danielk19772372c2b2006-06-27 16:34:56 +00007353** Return non-zero if a read (or write) transaction is active.
7354*/
7355int sqlite3BtreeIsInReadTrans(Btree *p){
drh64022502009-01-09 14:11:04 +00007356 assert( p );
drhe5fe6902007-12-07 18:55:28 +00007357 assert( sqlite3_mutex_held(p->db->mutex) );
drh64022502009-01-09 14:11:04 +00007358 return p->inTrans!=TRANS_NONE;
danielk19772372c2b2006-06-27 16:34:56 +00007359}
7360
7361/*
danielk1977da184232006-01-05 11:34:32 +00007362** This function returns a pointer to a blob of memory associated with
drh85b623f2007-12-13 21:54:09 +00007363** a single shared-btree. The memory is used by client code for its own
danielk1977da184232006-01-05 11:34:32 +00007364** purposes (for example, to store a high-level schema associated with
7365** the shared-btree). The btree layer manages reference counting issues.
7366**
7367** The first time this is called on a shared-btree, nBytes bytes of memory
7368** are allocated, zeroed, and returned to the caller. For each subsequent
7369** call the nBytes parameter is ignored and a pointer to the same blob
7370** of memory returned.
7371**
danielk1977171bfed2008-06-23 09:50:50 +00007372** If the nBytes parameter is 0 and the blob of memory has not yet been
7373** allocated, a null pointer is returned. If the blob has already been
7374** allocated, it is returned as normal.
7375**
danielk1977da184232006-01-05 11:34:32 +00007376** Just before the shared-btree is closed, the function passed as the
7377** xFree argument when the memory allocation was made is invoked on the
drh17435752007-08-16 04:30:38 +00007378** blob of allocated memory. This function should not call sqlite3_free()
danielk1977da184232006-01-05 11:34:32 +00007379** on the memory, the btree layer does that.
7380*/
7381void *sqlite3BtreeSchema(Btree *p, int nBytes, void(*xFree)(void *)){
7382 BtShared *pBt = p->pBt;
drh27641702007-08-22 02:56:42 +00007383 sqlite3BtreeEnter(p);
danielk1977171bfed2008-06-23 09:50:50 +00007384 if( !pBt->pSchema && nBytes ){
drh17435752007-08-16 04:30:38 +00007385 pBt->pSchema = sqlite3MallocZero(nBytes);
danielk1977da184232006-01-05 11:34:32 +00007386 pBt->xFreeSchema = xFree;
7387 }
drh27641702007-08-22 02:56:42 +00007388 sqlite3BtreeLeave(p);
danielk1977da184232006-01-05 11:34:32 +00007389 return pBt->pSchema;
7390}
7391
danielk1977c87d34d2006-01-06 13:00:28 +00007392/*
7393** Return true if another user of the same shared btree as the argument
7394** handle holds an exclusive lock on the sqlite_master table.
7395*/
7396int sqlite3BtreeSchemaLocked(Btree *p){
drh27641702007-08-22 02:56:42 +00007397 int rc;
drhe5fe6902007-12-07 18:55:28 +00007398 assert( sqlite3_mutex_held(p->db->mutex) );
drh27641702007-08-22 02:56:42 +00007399 sqlite3BtreeEnter(p);
7400 rc = (queryTableLock(p, MASTER_ROOT, READ_LOCK)!=SQLITE_OK);
7401 sqlite3BtreeLeave(p);
7402 return rc;
danielk1977c87d34d2006-01-06 13:00:28 +00007403}
7404
drha154dcd2006-03-22 22:10:07 +00007405
7406#ifndef SQLITE_OMIT_SHARED_CACHE
7407/*
7408** Obtain a lock on the table whose root page is iTab. The
7409** lock is a write lock if isWritelock is true or a read lock
7410** if it is false.
7411*/
danielk1977c00da102006-01-07 13:21:04 +00007412int sqlite3BtreeLockTable(Btree *p, int iTab, u8 isWriteLock){
danielk19772e94d4d2006-01-09 05:36:27 +00007413 int rc = SQLITE_OK;
drh6a9ad3d2008-04-02 16:29:30 +00007414 if( p->sharable ){
7415 u8 lockType = READ_LOCK + isWriteLock;
7416 assert( READ_LOCK+1==WRITE_LOCK );
7417 assert( isWriteLock==0 || isWriteLock==1 );
7418 sqlite3BtreeEnter(p);
7419 rc = queryTableLock(p, iTab, lockType);
7420 if( rc==SQLITE_OK ){
7421 rc = lockTable(p, iTab, lockType);
7422 }
7423 sqlite3BtreeLeave(p);
danielk1977c00da102006-01-07 13:21:04 +00007424 }
7425 return rc;
7426}
drha154dcd2006-03-22 22:10:07 +00007427#endif
danielk1977b82e7ed2006-01-11 14:09:31 +00007428
danielk1977b4e9af92007-05-01 17:49:49 +00007429#ifndef SQLITE_OMIT_INCRBLOB
7430/*
7431** Argument pCsr must be a cursor opened for writing on an
7432** INTKEY table currently pointing at a valid table entry.
7433** This function modifies the data stored as part of that entry.
7434** Only the data content may only be modified, it is not possible
7435** to change the length of the data stored.
7436*/
danielk1977dcbb5d32007-05-04 18:36:44 +00007437int sqlite3BtreePutData(BtCursor *pCsr, u32 offset, u32 amt, void *z){
drh1fee73e2007-08-29 04:00:57 +00007438 assert( cursorHoldsMutex(pCsr) );
drhe5fe6902007-12-07 18:55:28 +00007439 assert( sqlite3_mutex_held(pCsr->pBtree->db->mutex) );
danielk1977dcbb5d32007-05-04 18:36:44 +00007440 assert(pCsr->isIncrblobHandle);
danielk19773588ceb2008-06-10 17:30:26 +00007441
drha3460582008-07-11 21:02:53 +00007442 restoreCursorPosition(pCsr);
danielk19773588ceb2008-06-10 17:30:26 +00007443 assert( pCsr->eState!=CURSOR_REQUIRESEEK );
7444 if( pCsr->eState!=CURSOR_VALID ){
7445 return SQLITE_ABORT;
danielk1977dcbb5d32007-05-04 18:36:44 +00007446 }
7447
danielk1977d04417962007-05-02 13:16:30 +00007448 /* Check some preconditions:
danielk1977dcbb5d32007-05-04 18:36:44 +00007449 ** (a) the cursor is open for writing,
7450 ** (b) there is no read-lock on the table being modified and
7451 ** (c) the cursor points at a valid row of an intKey table.
danielk1977d04417962007-05-02 13:16:30 +00007452 */
danielk1977d04417962007-05-02 13:16:30 +00007453 if( !pCsr->wrFlag ){
danielk1977dcbb5d32007-05-04 18:36:44 +00007454 return SQLITE_READONLY;
danielk1977d04417962007-05-02 13:16:30 +00007455 }
drhd0679ed2007-08-28 22:24:34 +00007456 assert( !pCsr->pBt->readOnly
7457 && pCsr->pBt->inTransaction==TRANS_WRITE );
danielk19773588ceb2008-06-10 17:30:26 +00007458 if( checkReadLocks(pCsr->pBtree, pCsr->pgnoRoot, pCsr, 0) ){
danielk1977d04417962007-05-02 13:16:30 +00007459 return SQLITE_LOCKED; /* The table pCur points to has a read lock */
7460 }
danielk197771d5d2c2008-09-29 11:49:47 +00007461 if( pCsr->eState==CURSOR_INVALID || !pCsr->apPage[pCsr->iPage]->intKey ){
danielk1977d04417962007-05-02 13:16:30 +00007462 return SQLITE_ERROR;
danielk1977b4e9af92007-05-01 17:49:49 +00007463 }
7464
danielk19779f8d6402007-05-02 17:48:45 +00007465 return accessPayload(pCsr, offset, amt, (unsigned char *)z, 0, 1);
danielk1977b4e9af92007-05-01 17:49:49 +00007466}
danielk19772dec9702007-05-02 16:48:37 +00007467
7468/*
7469** Set a flag on this cursor to cache the locations of pages from the
danielk1977da107192007-05-04 08:32:13 +00007470** overflow list for the current row. This is used by cursors opened
7471** for incremental blob IO only.
7472**
7473** This function sets a flag only. The actual page location cache
7474** (stored in BtCursor.aOverflow[]) is allocated and used by function
7475** accessPayload() (the worker function for sqlite3BtreeData() and
7476** sqlite3BtreePutData()).
danielk19772dec9702007-05-02 16:48:37 +00007477*/
7478void sqlite3BtreeCacheOverflow(BtCursor *pCur){
drh1fee73e2007-08-29 04:00:57 +00007479 assert( cursorHoldsMutex(pCur) );
drhe5fe6902007-12-07 18:55:28 +00007480 assert( sqlite3_mutex_held(pCur->pBtree->db->mutex) );
danielk1977dcbb5d32007-05-04 18:36:44 +00007481 assert(!pCur->isIncrblobHandle);
danielk19772dec9702007-05-02 16:48:37 +00007482 assert(!pCur->aOverflow);
danielk1977dcbb5d32007-05-04 18:36:44 +00007483 pCur->isIncrblobHandle = 1;
danielk19772dec9702007-05-02 16:48:37 +00007484}
danielk1977b4e9af92007-05-01 17:49:49 +00007485#endif