blob: f08e7784aa334d50de4af86b8ea2f29d6974b886 [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*************************************************************************
danielk1977ad0132d2008-06-07 08:58:22 +000012** $Id: btree.c,v 1.459 2008/06/07 08:58:22 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*/
30#if SQLITE_TEST
mlcreech3a00f902008-03-04 17:45:01 +000031int sqlite3BtreeTrace=0; /* True to enable tracing */
drh615ae552005-01-16 23:21:00 +000032#endif
drh615ae552005-01-16 23:21:00 +000033
drh86f8c192007-08-22 00:39:19 +000034
35
drhe53831d2007-08-17 01:14:38 +000036#ifndef SQLITE_OMIT_SHARED_CACHE
37/*
38** A flag to indicate whether or not shared cache is enabled. Also,
39** a list of BtShared objects that are eligible for participation
drhd677b3d2007-08-20 22:48:41 +000040** in shared cache. The variables have file scope during normal builds,
drh86f8c192007-08-22 00:39:19 +000041** but the test harness needs to access these variables so we make them
drhd677b3d2007-08-20 22:48:41 +000042** global for test builds.
drhe53831d2007-08-17 01:14:38 +000043*/
44#ifdef SQLITE_TEST
45BtShared *sqlite3SharedCacheList = 0;
46int sqlite3SharedCacheEnabled = 0;
47#else
48static BtShared *sqlite3SharedCacheList = 0;
49static int sqlite3SharedCacheEnabled = 0;
50#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){
62 sqlite3SharedCacheEnabled = enable;
63 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*/
drh980b1a72006-08-16 16:42:48 +000071static int checkReadLocks(Btree*,Pgno,BtCursor*);
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) );
drhd677b3d2007-08-20 22:48:41 +0000100
danielk1977da184232006-01-05 11:34:32 +0000101 /* This is a no-op if the shared-cache is not enabled */
drhe53831d2007-08-17 01:14:38 +0000102 if( !p->sharable ){
danielk1977da184232006-01-05 11:34:32 +0000103 return SQLITE_OK;
104 }
105
danielk1977641b0f42007-12-21 04:47:25 +0000106 /* If some other connection is holding an exclusive lock, the
107 ** requested lock may not be obtained.
108 */
109 if( pBt->pExclusive && pBt->pExclusive!=p ){
110 return SQLITE_LOCKED;
111 }
112
danielk1977da184232006-01-05 11:34:32 +0000113 /* This (along with lockTable()) is where the ReadUncommitted flag is
114 ** dealt with. If the caller is querying for a read-lock and the flag is
115 ** set, it is unconditionally granted - even if there are write-locks
116 ** on the table. If a write-lock is requested, the ReadUncommitted flag
117 ** is not considered.
118 **
119 ** In function lockTable(), if a read-lock is demanded and the
120 ** ReadUncommitted flag is set, no entry is added to the locks list
121 ** (BtShared.pLock).
122 **
123 ** To summarize: If the ReadUncommitted flag is set, then read cursors do
124 ** not create or respect table locks. The locking procedure for a
125 ** write-cursor does not change.
126 */
127 if(
drhe5fe6902007-12-07 18:55:28 +0000128 !p->db ||
129 0==(p->db->flags&SQLITE_ReadUncommitted) ||
danielk1977da184232006-01-05 11:34:32 +0000130 eLock==WRITE_LOCK ||
drh47ded162006-01-06 01:42:58 +0000131 iTab==MASTER_ROOT
danielk1977da184232006-01-05 11:34:32 +0000132 ){
133 for(pIter=pBt->pLock; pIter; pIter=pIter->pNext){
134 if( pIter->pBtree!=p && pIter->iTable==iTab &&
135 (pIter->eLock!=eLock || eLock!=READ_LOCK) ){
danielk1977c87d34d2006-01-06 13:00:28 +0000136 return SQLITE_LOCKED;
danielk1977da184232006-01-05 11:34:32 +0000137 }
danielk1977aef0bf62005-12-30 16:28:01 +0000138 }
139 }
140 return SQLITE_OK;
141}
drhe53831d2007-08-17 01:14:38 +0000142#endif /* !SQLITE_OMIT_SHARED_CACHE */
danielk1977aef0bf62005-12-30 16:28:01 +0000143
drhe53831d2007-08-17 01:14:38 +0000144#ifndef SQLITE_OMIT_SHARED_CACHE
danielk1977aef0bf62005-12-30 16:28:01 +0000145/*
146** Add a lock on the table with root-page iTable to the shared-btree used
147** by Btree handle p. Parameter eLock must be either READ_LOCK or
148** WRITE_LOCK.
149**
150** SQLITE_OK is returned if the lock is added successfully. SQLITE_BUSY and
151** SQLITE_NOMEM may also be returned.
152*/
153static int lockTable(Btree *p, Pgno iTable, u8 eLock){
154 BtShared *pBt = p->pBt;
155 BtLock *pLock = 0;
156 BtLock *pIter;
157
drh1fee73e2007-08-29 04:00:57 +0000158 assert( sqlite3BtreeHoldsMutex(p) );
drhd677b3d2007-08-20 22:48:41 +0000159
danielk1977da184232006-01-05 11:34:32 +0000160 /* This is a no-op if the shared-cache is not enabled */
drhe53831d2007-08-17 01:14:38 +0000161 if( !p->sharable ){
danielk1977da184232006-01-05 11:34:32 +0000162 return SQLITE_OK;
163 }
164
danielk1977aef0bf62005-12-30 16:28:01 +0000165 assert( SQLITE_OK==queryTableLock(p, iTable, eLock) );
166
danielk1977da184232006-01-05 11:34:32 +0000167 /* If the read-uncommitted flag is set and a read-lock is requested,
168 ** return early without adding an entry to the BtShared.pLock list. See
169 ** comment in function queryTableLock() for more info on handling
170 ** the ReadUncommitted flag.
171 */
172 if(
drhe5fe6902007-12-07 18:55:28 +0000173 (p->db) &&
174 (p->db->flags&SQLITE_ReadUncommitted) &&
danielk1977da184232006-01-05 11:34:32 +0000175 (eLock==READ_LOCK) &&
drh47ded162006-01-06 01:42:58 +0000176 iTable!=MASTER_ROOT
danielk1977da184232006-01-05 11:34:32 +0000177 ){
178 return SQLITE_OK;
179 }
180
danielk1977aef0bf62005-12-30 16:28:01 +0000181 /* First search the list for an existing lock on this table. */
182 for(pIter=pBt->pLock; pIter; pIter=pIter->pNext){
183 if( pIter->iTable==iTable && pIter->pBtree==p ){
184 pLock = pIter;
185 break;
186 }
187 }
188
189 /* If the above search did not find a BtLock struct associating Btree p
190 ** with table iTable, allocate one and link it into the list.
191 */
192 if( !pLock ){
drh17435752007-08-16 04:30:38 +0000193 pLock = (BtLock *)sqlite3MallocZero(sizeof(BtLock));
danielk1977aef0bf62005-12-30 16:28:01 +0000194 if( !pLock ){
195 return SQLITE_NOMEM;
196 }
197 pLock->iTable = iTable;
198 pLock->pBtree = p;
199 pLock->pNext = pBt->pLock;
200 pBt->pLock = pLock;
201 }
202
203 /* Set the BtLock.eLock variable to the maximum of the current lock
204 ** and the requested lock. This means if a write-lock was already held
205 ** and a read-lock requested, we don't incorrectly downgrade the lock.
206 */
207 assert( WRITE_LOCK>READ_LOCK );
danielk19775118b912005-12-30 16:31:53 +0000208 if( eLock>pLock->eLock ){
209 pLock->eLock = eLock;
210 }
danielk1977aef0bf62005-12-30 16:28:01 +0000211
212 return SQLITE_OK;
213}
drhe53831d2007-08-17 01:14:38 +0000214#endif /* !SQLITE_OMIT_SHARED_CACHE */
danielk1977aef0bf62005-12-30 16:28:01 +0000215
drhe53831d2007-08-17 01:14:38 +0000216#ifndef SQLITE_OMIT_SHARED_CACHE
danielk1977aef0bf62005-12-30 16:28:01 +0000217/*
218** Release all the table locks (locks obtained via calls to the lockTable()
219** procedure) held by Btree handle p.
220*/
221static void unlockAllTables(Btree *p){
danielk1977641b0f42007-12-21 04:47:25 +0000222 BtShared *pBt = p->pBt;
223 BtLock **ppIter = &pBt->pLock;
danielk1977da184232006-01-05 11:34:32 +0000224
drh1fee73e2007-08-29 04:00:57 +0000225 assert( sqlite3BtreeHoldsMutex(p) );
drhe53831d2007-08-17 01:14:38 +0000226 assert( p->sharable || 0==*ppIter );
danielk1977da184232006-01-05 11:34:32 +0000227
danielk1977aef0bf62005-12-30 16:28:01 +0000228 while( *ppIter ){
229 BtLock *pLock = *ppIter;
danielk1977641b0f42007-12-21 04:47:25 +0000230 assert( pBt->pExclusive==0 || pBt->pExclusive==pLock->pBtree );
danielk1977aef0bf62005-12-30 16:28:01 +0000231 if( pLock->pBtree==p ){
232 *ppIter = pLock->pNext;
drh17435752007-08-16 04:30:38 +0000233 sqlite3_free(pLock);
danielk1977aef0bf62005-12-30 16:28:01 +0000234 }else{
235 ppIter = &pLock->pNext;
236 }
237 }
danielk1977641b0f42007-12-21 04:47:25 +0000238
239 if( pBt->pExclusive==p ){
240 pBt->pExclusive = 0;
241 }
danielk1977aef0bf62005-12-30 16:28:01 +0000242}
243#endif /* SQLITE_OMIT_SHARED_CACHE */
244
drh980b1a72006-08-16 16:42:48 +0000245static void releasePage(MemPage *pPage); /* Forward reference */
246
drh1fee73e2007-08-29 04:00:57 +0000247/*
248** Verify that the cursor holds a mutex on the BtShared
249*/
250#ifndef NDEBUG
251static int cursorHoldsMutex(BtCursor *p){
drhff0587c2007-08-29 17:43:19 +0000252 return sqlite3_mutex_held(p->pBt->mutex);
drh1fee73e2007-08-29 04:00:57 +0000253}
254#endif
255
256
danielk197792d4d7a2007-05-04 12:05:56 +0000257#ifndef SQLITE_OMIT_INCRBLOB
258/*
259** Invalidate the overflow page-list cache for cursor pCur, if any.
260*/
261static void invalidateOverflowCache(BtCursor *pCur){
drh1fee73e2007-08-29 04:00:57 +0000262 assert( cursorHoldsMutex(pCur) );
drh17435752007-08-16 04:30:38 +0000263 sqlite3_free(pCur->aOverflow);
danielk197792d4d7a2007-05-04 12:05:56 +0000264 pCur->aOverflow = 0;
265}
266
267/*
268** Invalidate the overflow page-list cache for all cursors opened
269** on the shared btree structure pBt.
270*/
271static void invalidateAllOverflowCache(BtShared *pBt){
272 BtCursor *p;
drh1fee73e2007-08-29 04:00:57 +0000273 assert( sqlite3_mutex_held(pBt->mutex) );
danielk197792d4d7a2007-05-04 12:05:56 +0000274 for(p=pBt->pCursor; p; p=p->pNext){
275 invalidateOverflowCache(p);
276 }
277}
278#else
279 #define invalidateOverflowCache(x)
280 #define invalidateAllOverflowCache(x)
281#endif
282
drh980b1a72006-08-16 16:42:48 +0000283/*
284** Save the current cursor position in the variables BtCursor.nKey
285** and BtCursor.pKey. The cursor's state is set to CURSOR_REQUIRESEEK.
286*/
287static int saveCursorPosition(BtCursor *pCur){
288 int rc;
289
290 assert( CURSOR_VALID==pCur->eState );
291 assert( 0==pCur->pKey );
drh1fee73e2007-08-29 04:00:57 +0000292 assert( cursorHoldsMutex(pCur) );
drh980b1a72006-08-16 16:42:48 +0000293
294 rc = sqlite3BtreeKeySize(pCur, &pCur->nKey);
295
296 /* If this is an intKey table, then the above call to BtreeKeySize()
297 ** stores the integer key in pCur->nKey. In this case this value is
298 ** all that is required. Otherwise, if pCur is not open on an intKey
299 ** table, then malloc space for and store the pCur->nKey bytes of key
300 ** data.
301 */
302 if( rc==SQLITE_OK && 0==pCur->pPage->intKey){
drh17435752007-08-16 04:30:38 +0000303 void *pKey = sqlite3_malloc(pCur->nKey);
drh980b1a72006-08-16 16:42:48 +0000304 if( pKey ){
305 rc = sqlite3BtreeKey(pCur, 0, pCur->nKey, pKey);
306 if( rc==SQLITE_OK ){
307 pCur->pKey = pKey;
308 }else{
drh17435752007-08-16 04:30:38 +0000309 sqlite3_free(pKey);
drh980b1a72006-08-16 16:42:48 +0000310 }
311 }else{
312 rc = SQLITE_NOMEM;
313 }
314 }
315 assert( !pCur->pPage->intKey || !pCur->pKey );
316
317 if( rc==SQLITE_OK ){
318 releasePage(pCur->pPage);
319 pCur->pPage = 0;
320 pCur->eState = CURSOR_REQUIRESEEK;
321 }
322
danielk197792d4d7a2007-05-04 12:05:56 +0000323 invalidateOverflowCache(pCur);
drh980b1a72006-08-16 16:42:48 +0000324 return rc;
325}
326
327/*
328** Save the positions of all cursors except pExcept open on the table
329** with root-page iRoot. Usually, this is called just before cursor
330** pExcept is used to modify the table (BtreeDelete() or BtreeInsert()).
331*/
332static int saveAllCursors(BtShared *pBt, Pgno iRoot, BtCursor *pExcept){
333 BtCursor *p;
drh1fee73e2007-08-29 04:00:57 +0000334 assert( sqlite3_mutex_held(pBt->mutex) );
drhd0679ed2007-08-28 22:24:34 +0000335 assert( pExcept==0 || pExcept->pBt==pBt );
drh980b1a72006-08-16 16:42:48 +0000336 for(p=pBt->pCursor; p; p=p->pNext){
337 if( p!=pExcept && (0==iRoot || p->pgnoRoot==iRoot) &&
338 p->eState==CURSOR_VALID ){
339 int rc = saveCursorPosition(p);
340 if( SQLITE_OK!=rc ){
341 return rc;
342 }
343 }
344 }
345 return SQLITE_OK;
346}
347
348/*
drhbf700f32007-03-31 02:36:44 +0000349** Clear the current cursor position.
350*/
351static void clearCursorPosition(BtCursor *pCur){
drh1fee73e2007-08-29 04:00:57 +0000352 assert( cursorHoldsMutex(pCur) );
drh17435752007-08-16 04:30:38 +0000353 sqlite3_free(pCur->pKey);
drhbf700f32007-03-31 02:36:44 +0000354 pCur->pKey = 0;
355 pCur->eState = CURSOR_INVALID;
356}
357
358/*
drh980b1a72006-08-16 16:42:48 +0000359** Restore the cursor to the position it was in (or as close to as possible)
360** when saveCursorPosition() was called. Note that this call deletes the
361** saved position info stored by saveCursorPosition(), so there can be
362** at most one effective restoreOrClearCursorPosition() call after each
363** saveCursorPosition().
364**
365** If the second argument argument - doSeek - is false, then instead of
drh85b623f2007-12-13 21:54:09 +0000366** returning the cursor to its saved position, any saved position is deleted
drh980b1a72006-08-16 16:42:48 +0000367** and the cursor state set to CURSOR_INVALID.
368*/
drh16a9b832007-05-05 18:39:25 +0000369int sqlite3BtreeRestoreOrClearCursorPosition(BtCursor *pCur){
drhbf700f32007-03-31 02:36:44 +0000370 int rc;
drh1fee73e2007-08-29 04:00:57 +0000371 assert( cursorHoldsMutex(pCur) );
drhfb982642007-08-30 01:19:59 +0000372 assert( pCur->eState>=CURSOR_REQUIRESEEK );
373 if( pCur->eState==CURSOR_FAULT ){
374 return pCur->skip;
375 }
danielk197732a0d8b2007-05-04 19:03:02 +0000376#ifndef SQLITE_OMIT_INCRBLOB
danielk1977dcbb5d32007-05-04 18:36:44 +0000377 if( pCur->isIncrblobHandle ){
378 return SQLITE_ABORT;
379 }
danielk197732a0d8b2007-05-04 19:03:02 +0000380#endif
drh980b1a72006-08-16 16:42:48 +0000381 pCur->eState = CURSOR_INVALID;
drhe14006d2008-03-25 17:23:32 +0000382 rc = sqlite3BtreeMoveto(pCur, pCur->pKey, 0, pCur->nKey, 0, &pCur->skip);
drh980b1a72006-08-16 16:42:48 +0000383 if( rc==SQLITE_OK ){
drh17435752007-08-16 04:30:38 +0000384 sqlite3_free(pCur->pKey);
drh980b1a72006-08-16 16:42:48 +0000385 pCur->pKey = 0;
drhbf700f32007-03-31 02:36:44 +0000386 assert( pCur->eState==CURSOR_VALID || pCur->eState==CURSOR_INVALID );
drh980b1a72006-08-16 16:42:48 +0000387 }
388 return rc;
389}
390
drhbf700f32007-03-31 02:36:44 +0000391#define restoreOrClearCursorPosition(p) \
drhfb982642007-08-30 01:19:59 +0000392 (p->eState>=CURSOR_REQUIRESEEK ? \
drh16a9b832007-05-05 18:39:25 +0000393 sqlite3BtreeRestoreOrClearCursorPosition(p) : \
394 SQLITE_OK)
drh980b1a72006-08-16 16:42:48 +0000395
danielk1977599fcba2004-11-08 07:13:13 +0000396#ifndef SQLITE_OMIT_AUTOVACUUM
danielk1977afcdd022004-10-31 16:25:42 +0000397/*
drha3152892007-05-05 11:48:52 +0000398** Given a page number of a regular database page, return the page
399** number for the pointer-map page that contains the entry for the
400** input page number.
danielk1977afcdd022004-10-31 16:25:42 +0000401*/
danielk1977266664d2006-02-10 08:24:21 +0000402static Pgno ptrmapPageno(BtShared *pBt, Pgno pgno){
drhd677b3d2007-08-20 22:48:41 +0000403 int nPagesPerMapPage, iPtrMap, ret;
drh1fee73e2007-08-29 04:00:57 +0000404 assert( sqlite3_mutex_held(pBt->mutex) );
drhd677b3d2007-08-20 22:48:41 +0000405 nPagesPerMapPage = (pBt->usableSize/5)+1;
406 iPtrMap = (pgno-2)/nPagesPerMapPage;
407 ret = (iPtrMap*nPagesPerMapPage) + 2;
danielk1977266664d2006-02-10 08:24:21 +0000408 if( ret==PENDING_BYTE_PAGE(pBt) ){
409 ret++;
410 }
411 return ret;
412}
danielk1977a19df672004-11-03 11:37:07 +0000413
danielk1977afcdd022004-10-31 16:25:42 +0000414/*
danielk1977afcdd022004-10-31 16:25:42 +0000415** Write an entry into the pointer map.
danielk1977687566d2004-11-02 12:56:41 +0000416**
417** This routine updates the pointer map entry for page number 'key'
418** so that it maps to type 'eType' and parent page number 'pgno'.
419** An error code is returned if something goes wrong, otherwise SQLITE_OK.
danielk1977afcdd022004-10-31 16:25:42 +0000420*/
danielk1977aef0bf62005-12-30 16:28:01 +0000421static int ptrmapPut(BtShared *pBt, Pgno key, u8 eType, Pgno parent){
danielk19773b8a05f2007-03-19 17:44:26 +0000422 DbPage *pDbPage; /* The pointer map page */
423 u8 *pPtrmap; /* The pointer map data */
424 Pgno iPtrmap; /* The pointer map page number */
425 int offset; /* Offset in pointer map page */
danielk1977afcdd022004-10-31 16:25:42 +0000426 int rc;
427
drh1fee73e2007-08-29 04:00:57 +0000428 assert( sqlite3_mutex_held(pBt->mutex) );
danielk1977266664d2006-02-10 08:24:21 +0000429 /* The master-journal page number must never be used as a pointer map page */
430 assert( 0==PTRMAP_ISPAGE(pBt, PENDING_BYTE_PAGE(pBt)) );
431
danielk1977ac11ee62005-01-15 12:45:51 +0000432 assert( pBt->autoVacuum );
danielk1977fdb7cdb2005-01-17 02:12:18 +0000433 if( key==0 ){
drh49285702005-09-17 15:20:26 +0000434 return SQLITE_CORRUPT_BKPT;
danielk1977fdb7cdb2005-01-17 02:12:18 +0000435 }
danielk1977266664d2006-02-10 08:24:21 +0000436 iPtrmap = PTRMAP_PAGENO(pBt, key);
danielk19773b8a05f2007-03-19 17:44:26 +0000437 rc = sqlite3PagerGet(pBt->pPager, iPtrmap, &pDbPage);
danielk1977687566d2004-11-02 12:56:41 +0000438 if( rc!=SQLITE_OK ){
danielk1977afcdd022004-10-31 16:25:42 +0000439 return rc;
440 }
danielk1977266664d2006-02-10 08:24:21 +0000441 offset = PTRMAP_PTROFFSET(pBt, key);
danielk19773b8a05f2007-03-19 17:44:26 +0000442 pPtrmap = (u8 *)sqlite3PagerGetData(pDbPage);
danielk1977afcdd022004-10-31 16:25:42 +0000443
drh615ae552005-01-16 23:21:00 +0000444 if( eType!=pPtrmap[offset] || get4byte(&pPtrmap[offset+1])!=parent ){
445 TRACE(("PTRMAP_UPDATE: %d->(%d,%d)\n", key, eType, parent));
danielk19773b8a05f2007-03-19 17:44:26 +0000446 rc = sqlite3PagerWrite(pDbPage);
danielk19775558a8a2005-01-17 07:53:44 +0000447 if( rc==SQLITE_OK ){
448 pPtrmap[offset] = eType;
449 put4byte(&pPtrmap[offset+1], parent);
danielk1977afcdd022004-10-31 16:25:42 +0000450 }
danielk1977afcdd022004-10-31 16:25:42 +0000451 }
452
danielk19773b8a05f2007-03-19 17:44:26 +0000453 sqlite3PagerUnref(pDbPage);
danielk19775558a8a2005-01-17 07:53:44 +0000454 return rc;
danielk1977afcdd022004-10-31 16:25:42 +0000455}
456
457/*
458** Read an entry from the pointer map.
danielk1977687566d2004-11-02 12:56:41 +0000459**
460** This routine retrieves the pointer map entry for page 'key', writing
461** the type and parent page number to *pEType and *pPgno respectively.
462** An error code is returned if something goes wrong, otherwise SQLITE_OK.
danielk1977afcdd022004-10-31 16:25:42 +0000463*/
danielk1977aef0bf62005-12-30 16:28:01 +0000464static int ptrmapGet(BtShared *pBt, Pgno key, u8 *pEType, Pgno *pPgno){
danielk19773b8a05f2007-03-19 17:44:26 +0000465 DbPage *pDbPage; /* The pointer map page */
danielk1977afcdd022004-10-31 16:25:42 +0000466 int iPtrmap; /* Pointer map page index */
467 u8 *pPtrmap; /* Pointer map page data */
468 int offset; /* Offset of entry in pointer map */
469 int rc;
470
drh1fee73e2007-08-29 04:00:57 +0000471 assert( sqlite3_mutex_held(pBt->mutex) );
drhd677b3d2007-08-20 22:48:41 +0000472
danielk1977266664d2006-02-10 08:24:21 +0000473 iPtrmap = PTRMAP_PAGENO(pBt, key);
danielk19773b8a05f2007-03-19 17:44:26 +0000474 rc = sqlite3PagerGet(pBt->pPager, iPtrmap, &pDbPage);
danielk1977afcdd022004-10-31 16:25:42 +0000475 if( rc!=0 ){
476 return rc;
477 }
danielk19773b8a05f2007-03-19 17:44:26 +0000478 pPtrmap = (u8 *)sqlite3PagerGetData(pDbPage);
danielk1977afcdd022004-10-31 16:25:42 +0000479
danielk1977266664d2006-02-10 08:24:21 +0000480 offset = PTRMAP_PTROFFSET(pBt, key);
drh43617e92006-03-06 20:55:46 +0000481 assert( pEType!=0 );
482 *pEType = pPtrmap[offset];
danielk1977687566d2004-11-02 12:56:41 +0000483 if( pPgno ) *pPgno = get4byte(&pPtrmap[offset+1]);
danielk1977afcdd022004-10-31 16:25:42 +0000484
danielk19773b8a05f2007-03-19 17:44:26 +0000485 sqlite3PagerUnref(pDbPage);
drh49285702005-09-17 15:20:26 +0000486 if( *pEType<1 || *pEType>5 ) return SQLITE_CORRUPT_BKPT;
danielk1977afcdd022004-10-31 16:25:42 +0000487 return SQLITE_OK;
488}
489
490#endif /* SQLITE_OMIT_AUTOVACUUM */
491
drh0d316a42002-08-11 20:10:47 +0000492/*
drh271efa52004-05-30 19:19:05 +0000493** Given a btree page and a cell index (0 means the first cell on
494** the page, 1 means the second cell, and so forth) return a pointer
495** to the cell content.
496**
497** This routine works only for pages that do not contain overflow cells.
drh3aac2dd2004-04-26 14:10:20 +0000498*/
danielk19771cc5ed82007-05-16 17:28:43 +0000499#define findCell(pPage, iCell) \
500 ((pPage)->aData + get2byte(&(pPage)->aData[(pPage)->cellOffset+2*(iCell)]))
drhe6e4d6b2007-08-05 23:52:05 +0000501#ifdef SQLITE_TEST
drh16a9b832007-05-05 18:39:25 +0000502u8 *sqlite3BtreeFindCell(MemPage *pPage, int iCell){
drh43605152004-05-29 21:46:49 +0000503 assert( iCell>=0 );
drh029f3f82007-06-20 15:14:10 +0000504 assert( iCell<get2byte(&pPage->aData[pPage->hdrOffset+3]) );
danielk19771cc5ed82007-05-16 17:28:43 +0000505 return findCell(pPage, iCell);
drh43605152004-05-29 21:46:49 +0000506}
drhe6e4d6b2007-08-05 23:52:05 +0000507#endif
drh43605152004-05-29 21:46:49 +0000508
509/*
drh16a9b832007-05-05 18:39:25 +0000510** This a more complex version of sqlite3BtreeFindCell() that works for
drh43605152004-05-29 21:46:49 +0000511** pages that do contain overflow cells. See insert
512*/
513static u8 *findOverflowCell(MemPage *pPage, int iCell){
514 int i;
drh1fee73e2007-08-29 04:00:57 +0000515 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
drh43605152004-05-29 21:46:49 +0000516 for(i=pPage->nOverflow-1; i>=0; i--){
drh6d08b4d2004-07-20 12:45:22 +0000517 int k;
518 struct _OvflCell *pOvfl;
519 pOvfl = &pPage->aOvfl[i];
520 k = pOvfl->idx;
521 if( k<=iCell ){
522 if( k==iCell ){
523 return pOvfl->pCell;
drh43605152004-05-29 21:46:49 +0000524 }
525 iCell--;
526 }
527 }
danielk19771cc5ed82007-05-16 17:28:43 +0000528 return findCell(pPage, iCell);
drh43605152004-05-29 21:46:49 +0000529}
530
531/*
532** Parse a cell content block and fill in the CellInfo structure. There
drh16a9b832007-05-05 18:39:25 +0000533** are two versions of this function. sqlite3BtreeParseCell() takes a
534** cell index as the second argument and sqlite3BtreeParseCellPtr()
535** takes a pointer to the body of the cell as its second argument.
danielk19771cc5ed82007-05-16 17:28:43 +0000536**
537** Within this file, the parseCell() macro can be called instead of
538** sqlite3BtreeParseCellPtr(). Using some compilers, this will be faster.
drh43605152004-05-29 21:46:49 +0000539*/
drh16a9b832007-05-05 18:39:25 +0000540void sqlite3BtreeParseCellPtr(
drh3aac2dd2004-04-26 14:10:20 +0000541 MemPage *pPage, /* Page containing the cell */
drh43605152004-05-29 21:46:49 +0000542 u8 *pCell, /* Pointer to the cell text. */
drh6f11bef2004-05-13 01:12:56 +0000543 CellInfo *pInfo /* Fill in this structure */
drh3aac2dd2004-04-26 14:10:20 +0000544){
drh271efa52004-05-30 19:19:05 +0000545 int n; /* Number bytes in cell content header */
546 u32 nPayload; /* Number of bytes of cell payload */
drh43605152004-05-29 21:46:49 +0000547
drh1fee73e2007-08-29 04:00:57 +0000548 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
drhd677b3d2007-08-20 22:48:41 +0000549
drh43605152004-05-29 21:46:49 +0000550 pInfo->pCell = pCell;
drhab01f612004-05-22 02:55:23 +0000551 assert( pPage->leaf==0 || pPage->leaf==1 );
drh271efa52004-05-30 19:19:05 +0000552 n = pPage->childPtrSize;
553 assert( n==4-4*pPage->leaf );
drh8b18dd42004-05-12 19:18:15 +0000554 if( pPage->hasData ){
shane3f8d5cf2008-04-24 19:15:09 +0000555 n += getVarint32(&pCell[n], nPayload);
drh8b18dd42004-05-12 19:18:15 +0000556 }else{
drh271efa52004-05-30 19:19:05 +0000557 nPayload = 0;
drh3aac2dd2004-04-26 14:10:20 +0000558 }
drh271efa52004-05-30 19:19:05 +0000559 pInfo->nData = nPayload;
drh504b6982006-01-22 21:52:56 +0000560 if( pPage->intKey ){
561 n += getVarint(&pCell[n], (u64 *)&pInfo->nKey);
562 }else{
563 u32 x;
shane3f8d5cf2008-04-24 19:15:09 +0000564 n += getVarint32(&pCell[n], x);
drh504b6982006-01-22 21:52:56 +0000565 pInfo->nKey = x;
566 nPayload += x;
drh6f11bef2004-05-13 01:12:56 +0000567 }
drh72365832007-03-06 15:53:44 +0000568 pInfo->nPayload = nPayload;
drh504b6982006-01-22 21:52:56 +0000569 pInfo->nHeader = n;
drh271efa52004-05-30 19:19:05 +0000570 if( nPayload<=pPage->maxLocal ){
571 /* This is the (easy) common case where the entire payload fits
572 ** on the local page. No overflow is required.
573 */
574 int nSize; /* Total size of cell content in bytes */
drh6f11bef2004-05-13 01:12:56 +0000575 pInfo->nLocal = nPayload;
576 pInfo->iOverflow = 0;
drh271efa52004-05-30 19:19:05 +0000577 nSize = nPayload + n;
578 if( nSize<4 ){
579 nSize = 4; /* Minimum cell size is 4 */
drh43605152004-05-29 21:46:49 +0000580 }
drh271efa52004-05-30 19:19:05 +0000581 pInfo->nSize = nSize;
drh6f11bef2004-05-13 01:12:56 +0000582 }else{
drh271efa52004-05-30 19:19:05 +0000583 /* If the payload will not fit completely on the local page, we have
584 ** to decide how much to store locally and how much to spill onto
585 ** overflow pages. The strategy is to minimize the amount of unused
586 ** space on overflow pages while keeping the amount of local storage
587 ** in between minLocal and maxLocal.
588 **
589 ** Warning: changing the way overflow payload is distributed in any
590 ** way will result in an incompatible file format.
591 */
592 int minLocal; /* Minimum amount of payload held locally */
593 int maxLocal; /* Maximum amount of payload held locally */
594 int surplus; /* Overflow payload available for local storage */
595
596 minLocal = pPage->minLocal;
597 maxLocal = pPage->maxLocal;
598 surplus = minLocal + (nPayload - minLocal)%(pPage->pBt->usableSize - 4);
drh6f11bef2004-05-13 01:12:56 +0000599 if( surplus <= maxLocal ){
600 pInfo->nLocal = surplus;
601 }else{
602 pInfo->nLocal = minLocal;
603 }
604 pInfo->iOverflow = pInfo->nLocal + n;
605 pInfo->nSize = pInfo->iOverflow + 4;
606 }
drh3aac2dd2004-04-26 14:10:20 +0000607}
danielk19771cc5ed82007-05-16 17:28:43 +0000608#define parseCell(pPage, iCell, pInfo) \
609 sqlite3BtreeParseCellPtr((pPage), findCell((pPage), (iCell)), (pInfo))
drh16a9b832007-05-05 18:39:25 +0000610void sqlite3BtreeParseCell(
drh43605152004-05-29 21:46:49 +0000611 MemPage *pPage, /* Page containing the cell */
612 int iCell, /* The cell index. First cell is 0 */
613 CellInfo *pInfo /* Fill in this structure */
614){
danielk19771cc5ed82007-05-16 17:28:43 +0000615 parseCell(pPage, iCell, pInfo);
drh43605152004-05-29 21:46:49 +0000616}
drh3aac2dd2004-04-26 14:10:20 +0000617
618/*
drh43605152004-05-29 21:46:49 +0000619** Compute the total number of bytes that a Cell needs in the cell
620** data area of the btree-page. The return number includes the cell
621** data header and the local payload, but not any overflow page or
622** the space used by the cell pointer.
drh3b7511c2001-05-26 13:15:44 +0000623*/
danielk1977bc6ada42004-06-30 08:20:16 +0000624#ifndef NDEBUG
drha9121e42008-02-19 14:59:35 +0000625static u16 cellSize(MemPage *pPage, int iCell){
drh6f11bef2004-05-13 01:12:56 +0000626 CellInfo info;
drh16a9b832007-05-05 18:39:25 +0000627 sqlite3BtreeParseCell(pPage, iCell, &info);
drh43605152004-05-29 21:46:49 +0000628 return info.nSize;
629}
danielk1977bc6ada42004-06-30 08:20:16 +0000630#endif
drha9121e42008-02-19 14:59:35 +0000631static u16 cellSizePtr(MemPage *pPage, u8 *pCell){
drh43605152004-05-29 21:46:49 +0000632 CellInfo info;
drh16a9b832007-05-05 18:39:25 +0000633 sqlite3BtreeParseCellPtr(pPage, pCell, &info);
drh6f11bef2004-05-13 01:12:56 +0000634 return info.nSize;
drh3b7511c2001-05-26 13:15:44 +0000635}
636
danielk197779a40da2005-01-16 08:00:01 +0000637#ifndef SQLITE_OMIT_AUTOVACUUM
drh3b7511c2001-05-26 13:15:44 +0000638/*
danielk197726836652005-01-17 01:33:13 +0000639** If the cell pCell, part of page pPage contains a pointer
danielk197779a40da2005-01-16 08:00:01 +0000640** to an overflow page, insert an entry into the pointer-map
641** for the overflow page.
danielk1977ac11ee62005-01-15 12:45:51 +0000642*/
danielk197726836652005-01-17 01:33:13 +0000643static int ptrmapPutOvflPtr(MemPage *pPage, u8 *pCell){
danielk197779a40da2005-01-16 08:00:01 +0000644 if( pCell ){
645 CellInfo info;
drh16a9b832007-05-05 18:39:25 +0000646 sqlite3BtreeParseCellPtr(pPage, pCell, &info);
drh72365832007-03-06 15:53:44 +0000647 assert( (info.nData+(pPage->intKey?0:info.nKey))==info.nPayload );
danielk197779a40da2005-01-16 08:00:01 +0000648 if( (info.nData+(pPage->intKey?0:info.nKey))>info.nLocal ){
649 Pgno ovfl = get4byte(&pCell[info.iOverflow]);
650 return ptrmapPut(pPage->pBt, ovfl, PTRMAP_OVERFLOW1, pPage->pgno);
651 }
danielk1977ac11ee62005-01-15 12:45:51 +0000652 }
danielk197779a40da2005-01-16 08:00:01 +0000653 return SQLITE_OK;
danielk1977ac11ee62005-01-15 12:45:51 +0000654}
danielk197726836652005-01-17 01:33:13 +0000655/*
656** If the cell with index iCell on page pPage contains a pointer
657** to an overflow page, insert an entry into the pointer-map
658** for the overflow page.
659*/
660static int ptrmapPutOvfl(MemPage *pPage, int iCell){
661 u8 *pCell;
drh1fee73e2007-08-29 04:00:57 +0000662 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
danielk197726836652005-01-17 01:33:13 +0000663 pCell = findOverflowCell(pPage, iCell);
664 return ptrmapPutOvflPtr(pPage, pCell);
665}
danielk197779a40da2005-01-16 08:00:01 +0000666#endif
667
danielk1977ac11ee62005-01-15 12:45:51 +0000668
drhda200cc2004-05-09 11:51:38 +0000669/*
drh72f82862001-05-24 21:06:34 +0000670** Defragment the page given. All Cells are moved to the
drh3a4a2d42005-11-24 14:24:28 +0000671** end of the page and all free space is collected into one
672** big FreeBlk that occurs in between the header and cell
drh31beae92005-11-24 14:34:36 +0000673** pointer array and the cell content area.
drh365d68f2001-05-11 11:02:46 +0000674*/
drh2e38c322004-09-03 18:38:44 +0000675static int defragmentPage(MemPage *pPage){
drh43605152004-05-29 21:46:49 +0000676 int i; /* Loop counter */
677 int pc; /* Address of a i-th cell */
678 int addr; /* Offset of first byte after cell pointer array */
679 int hdr; /* Offset to the page header */
680 int size; /* Size of a cell */
681 int usableSize; /* Number of usable bytes on a page */
682 int cellOffset; /* Offset to the cell pointer array */
683 int brk; /* Offset to the cell content area */
684 int nCell; /* Number of cells on the page */
drh2e38c322004-09-03 18:38:44 +0000685 unsigned char *data; /* The page data */
686 unsigned char *temp; /* Temp area for cell content */
drh2af926b2001-05-15 00:39:25 +0000687
danielk19773b8a05f2007-03-19 17:44:26 +0000688 assert( sqlite3PagerIswriteable(pPage->pDbPage) );
drh9e572e62004-04-23 23:43:10 +0000689 assert( pPage->pBt!=0 );
drh90f5ecb2004-07-22 01:19:35 +0000690 assert( pPage->pBt->usableSize <= SQLITE_MAX_PAGE_SIZE );
drh43605152004-05-29 21:46:49 +0000691 assert( pPage->nOverflow==0 );
drh1fee73e2007-08-29 04:00:57 +0000692 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
drh26b79942007-11-28 16:19:56 +0000693 temp = sqlite3PagerTempSpace(pPage->pBt->pPager);
drh43605152004-05-29 21:46:49 +0000694 data = pPage->aData;
drh9e572e62004-04-23 23:43:10 +0000695 hdr = pPage->hdrOffset;
drh43605152004-05-29 21:46:49 +0000696 cellOffset = pPage->cellOffset;
697 nCell = pPage->nCell;
698 assert( nCell==get2byte(&data[hdr+3]) );
699 usableSize = pPage->pBt->usableSize;
700 brk = get2byte(&data[hdr+5]);
701 memcpy(&temp[brk], &data[brk], usableSize - brk);
702 brk = usableSize;
703 for(i=0; i<nCell; i++){
704 u8 *pAddr; /* The i-th cell pointer */
705 pAddr = &data[cellOffset + i*2];
706 pc = get2byte(pAddr);
707 assert( pc<pPage->pBt->usableSize );
708 size = cellSizePtr(pPage, &temp[pc]);
709 brk -= size;
710 memcpy(&data[brk], &temp[pc], size);
711 put2byte(pAddr, brk);
drh2af926b2001-05-15 00:39:25 +0000712 }
drh43605152004-05-29 21:46:49 +0000713 assert( brk>=cellOffset+2*nCell );
714 put2byte(&data[hdr+5], brk);
715 data[hdr+1] = 0;
716 data[hdr+2] = 0;
717 data[hdr+7] = 0;
718 addr = cellOffset+2*nCell;
719 memset(&data[addr], 0, brk-addr);
drh2e38c322004-09-03 18:38:44 +0000720 return SQLITE_OK;
drh365d68f2001-05-11 11:02:46 +0000721}
722
drha059ad02001-04-17 20:09:11 +0000723/*
drh43605152004-05-29 21:46:49 +0000724** Allocate nByte bytes of space on a page.
drhbd03cae2001-06-02 02:40:57 +0000725**
drh9e572e62004-04-23 23:43:10 +0000726** Return the index into pPage->aData[] of the first byte of
drhbd03cae2001-06-02 02:40:57 +0000727** the new allocation. Or return 0 if there is not enough free
728** space on the page to satisfy the allocation request.
drh2af926b2001-05-15 00:39:25 +0000729**
drh72f82862001-05-24 21:06:34 +0000730** If the page contains nBytes of free space but does not contain
drh8b2f49b2001-06-08 00:21:52 +0000731** nBytes of contiguous free space, then this routine automatically
732** calls defragementPage() to consolidate all free space before
733** allocating the new chunk.
drh7e3b0a02001-04-28 16:52:40 +0000734*/
drh9e572e62004-04-23 23:43:10 +0000735static int allocateSpace(MemPage *pPage, int nByte){
drh3aac2dd2004-04-26 14:10:20 +0000736 int addr, pc, hdr;
drh9e572e62004-04-23 23:43:10 +0000737 int size;
drh24cd67e2004-05-10 16:18:47 +0000738 int nFrag;
drh43605152004-05-29 21:46:49 +0000739 int top;
740 int nCell;
741 int cellOffset;
drh9e572e62004-04-23 23:43:10 +0000742 unsigned char *data;
drh43605152004-05-29 21:46:49 +0000743
drh9e572e62004-04-23 23:43:10 +0000744 data = pPage->aData;
danielk19773b8a05f2007-03-19 17:44:26 +0000745 assert( sqlite3PagerIswriteable(pPage->pDbPage) );
drh9e572e62004-04-23 23:43:10 +0000746 assert( pPage->pBt );
drh1fee73e2007-08-29 04:00:57 +0000747 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
drh9e572e62004-04-23 23:43:10 +0000748 if( nByte<4 ) nByte = 4;
drh43605152004-05-29 21:46:49 +0000749 if( pPage->nFree<nByte || pPage->nOverflow>0 ) return 0;
750 pPage->nFree -= nByte;
drh9e572e62004-04-23 23:43:10 +0000751 hdr = pPage->hdrOffset;
drh43605152004-05-29 21:46:49 +0000752
753 nFrag = data[hdr+7];
754 if( nFrag<60 ){
755 /* Search the freelist looking for a slot big enough to satisfy the
756 ** space request. */
757 addr = hdr+1;
758 while( (pc = get2byte(&data[addr]))>0 ){
759 size = get2byte(&data[pc+2]);
760 if( size>=nByte ){
761 if( size<nByte+4 ){
762 memcpy(&data[addr], &data[pc], 2);
763 data[hdr+7] = nFrag + size - nByte;
764 return pc;
765 }else{
766 put2byte(&data[pc+2], size-nByte);
767 return pc + size - nByte;
768 }
769 }
770 addr = pc;
drh9e572e62004-04-23 23:43:10 +0000771 }
772 }
drh43605152004-05-29 21:46:49 +0000773
774 /* Allocate memory from the gap in between the cell pointer array
775 ** and the cell content area.
776 */
777 top = get2byte(&data[hdr+5]);
778 nCell = get2byte(&data[hdr+3]);
779 cellOffset = pPage->cellOffset;
780 if( nFrag>=60 || cellOffset + 2*nCell > top - nByte ){
drh2e38c322004-09-03 18:38:44 +0000781 if( defragmentPage(pPage) ) return 0;
drh43605152004-05-29 21:46:49 +0000782 top = get2byte(&data[hdr+5]);
drh2af926b2001-05-15 00:39:25 +0000783 }
drh43605152004-05-29 21:46:49 +0000784 top -= nByte;
785 assert( cellOffset + 2*nCell <= top );
786 put2byte(&data[hdr+5], top);
787 return top;
drh7e3b0a02001-04-28 16:52:40 +0000788}
789
790/*
drh9e572e62004-04-23 23:43:10 +0000791** Return a section of the pPage->aData to the freelist.
792** The first byte of the new free block is pPage->aDisk[start]
793** and the size of the block is "size" bytes.
drh306dc212001-05-21 13:45:10 +0000794**
795** Most of the effort here is involved in coalesing adjacent
796** free blocks into a single big free block.
drh7e3b0a02001-04-28 16:52:40 +0000797*/
drh9e572e62004-04-23 23:43:10 +0000798static void freeSpace(MemPage *pPage, int start, int size){
drh43605152004-05-29 21:46:49 +0000799 int addr, pbegin, hdr;
drh9e572e62004-04-23 23:43:10 +0000800 unsigned char *data = pPage->aData;
drh2af926b2001-05-15 00:39:25 +0000801
drh9e572e62004-04-23 23:43:10 +0000802 assert( pPage->pBt!=0 );
danielk19773b8a05f2007-03-19 17:44:26 +0000803 assert( sqlite3PagerIswriteable(pPage->pDbPage) );
drh9e572e62004-04-23 23:43:10 +0000804 assert( start>=pPage->hdrOffset+6+(pPage->leaf?0:4) );
danielk1977bc6ada42004-06-30 08:20:16 +0000805 assert( (start + size)<=pPage->pBt->usableSize );
drh1fee73e2007-08-29 04:00:57 +0000806 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
drh9e572e62004-04-23 23:43:10 +0000807 if( size<4 ) size = 4;
808
drhfcce93f2006-02-22 03:08:32 +0000809#ifdef SQLITE_SECURE_DELETE
810 /* Overwrite deleted information with zeros when the SECURE_DELETE
811 ** option is enabled at compile-time */
812 memset(&data[start], 0, size);
813#endif
814
drh9e572e62004-04-23 23:43:10 +0000815 /* Add the space back into the linked list of freeblocks */
drh43605152004-05-29 21:46:49 +0000816 hdr = pPage->hdrOffset;
817 addr = hdr + 1;
drh3aac2dd2004-04-26 14:10:20 +0000818 while( (pbegin = get2byte(&data[addr]))<start && pbegin>0 ){
drhb6f41482004-05-14 01:58:11 +0000819 assert( pbegin<=pPage->pBt->usableSize-4 );
drh3aac2dd2004-04-26 14:10:20 +0000820 assert( pbegin>addr );
821 addr = pbegin;
drh2af926b2001-05-15 00:39:25 +0000822 }
drhb6f41482004-05-14 01:58:11 +0000823 assert( pbegin<=pPage->pBt->usableSize-4 );
drh3aac2dd2004-04-26 14:10:20 +0000824 assert( pbegin>addr || pbegin==0 );
drha34b6762004-05-07 13:30:42 +0000825 put2byte(&data[addr], start);
826 put2byte(&data[start], pbegin);
827 put2byte(&data[start+2], size);
drh2af926b2001-05-15 00:39:25 +0000828 pPage->nFree += size;
drh9e572e62004-04-23 23:43:10 +0000829
830 /* Coalesce adjacent free blocks */
drh3aac2dd2004-04-26 14:10:20 +0000831 addr = pPage->hdrOffset + 1;
832 while( (pbegin = get2byte(&data[addr]))>0 ){
drh9e572e62004-04-23 23:43:10 +0000833 int pnext, psize;
drh3aac2dd2004-04-26 14:10:20 +0000834 assert( pbegin>addr );
drh43605152004-05-29 21:46:49 +0000835 assert( pbegin<=pPage->pBt->usableSize-4 );
drh9e572e62004-04-23 23:43:10 +0000836 pnext = get2byte(&data[pbegin]);
837 psize = get2byte(&data[pbegin+2]);
838 if( pbegin + psize + 3 >= pnext && pnext>0 ){
839 int frag = pnext - (pbegin+psize);
drh43605152004-05-29 21:46:49 +0000840 assert( frag<=data[pPage->hdrOffset+7] );
841 data[pPage->hdrOffset+7] -= frag;
drh9e572e62004-04-23 23:43:10 +0000842 put2byte(&data[pbegin], get2byte(&data[pnext]));
843 put2byte(&data[pbegin+2], pnext+get2byte(&data[pnext+2])-pbegin);
844 }else{
drh3aac2dd2004-04-26 14:10:20 +0000845 addr = pbegin;
drh9e572e62004-04-23 23:43:10 +0000846 }
847 }
drh7e3b0a02001-04-28 16:52:40 +0000848
drh43605152004-05-29 21:46:49 +0000849 /* If the cell content area begins with a freeblock, remove it. */
850 if( data[hdr+1]==data[hdr+5] && data[hdr+2]==data[hdr+6] ){
851 int top;
852 pbegin = get2byte(&data[hdr+1]);
853 memcpy(&data[hdr+1], &data[pbegin], 2);
854 top = get2byte(&data[hdr+5]);
855 put2byte(&data[hdr+5], top + get2byte(&data[pbegin+2]));
drh4b70f112004-05-02 21:12:19 +0000856 }
drh4b70f112004-05-02 21:12:19 +0000857}
858
859/*
drh271efa52004-05-30 19:19:05 +0000860** Decode the flags byte (the first byte of the header) for a page
861** and initialize fields of the MemPage structure accordingly.
862*/
863static void decodeFlags(MemPage *pPage, int flagByte){
danielk1977aef0bf62005-12-30 16:28:01 +0000864 BtShared *pBt; /* A copy of pPage->pBt */
drh271efa52004-05-30 19:19:05 +0000865
866 assert( pPage->hdrOffset==(pPage->pgno==1 ? 100 : 0) );
drh1fee73e2007-08-29 04:00:57 +0000867 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
drh271efa52004-05-30 19:19:05 +0000868 pPage->intKey = (flagByte & (PTF_INTKEY|PTF_LEAFDATA))!=0;
869 pPage->zeroData = (flagByte & PTF_ZERODATA)!=0;
870 pPage->leaf = (flagByte & PTF_LEAF)!=0;
871 pPage->childPtrSize = 4*(pPage->leaf==0);
872 pBt = pPage->pBt;
873 if( flagByte & PTF_LEAFDATA ){
874 pPage->leafData = 1;
875 pPage->maxLocal = pBt->maxLeaf;
876 pPage->minLocal = pBt->minLeaf;
877 }else{
878 pPage->leafData = 0;
879 pPage->maxLocal = pBt->maxLocal;
880 pPage->minLocal = pBt->minLocal;
881 }
882 pPage->hasData = !(pPage->zeroData || (!pPage->leaf && pPage->leafData));
883}
884
885/*
drh7e3b0a02001-04-28 16:52:40 +0000886** Initialize the auxiliary information for a disk block.
drh72f82862001-05-24 21:06:34 +0000887**
drhbd03cae2001-06-02 02:40:57 +0000888** The pParent parameter must be a pointer to the MemPage which
drh9e572e62004-04-23 23:43:10 +0000889** is the parent of the page being initialized. The root of a
890** BTree has no parent and so for that page, pParent==NULL.
drh5e2f8b92001-05-28 00:41:15 +0000891**
drh72f82862001-05-24 21:06:34 +0000892** Return SQLITE_OK on success. If we see that the page does
drhda47d772002-12-02 04:25:19 +0000893** not contain a well-formed database page, then return
drh72f82862001-05-24 21:06:34 +0000894** SQLITE_CORRUPT. Note that a return of SQLITE_OK does not
895** guarantee that the page is well-formed. It only shows that
896** we failed to detect any corruption.
drh7e3b0a02001-04-28 16:52:40 +0000897*/
drh16a9b832007-05-05 18:39:25 +0000898int sqlite3BtreeInitPage(
drh3aac2dd2004-04-26 14:10:20 +0000899 MemPage *pPage, /* The page to be initialized */
drh9e572e62004-04-23 23:43:10 +0000900 MemPage *pParent /* The parent. Might be NULL */
901){
drh271efa52004-05-30 19:19:05 +0000902 int pc; /* Address of a freeblock within pPage->aData[] */
drh271efa52004-05-30 19:19:05 +0000903 int hdr; /* Offset to beginning of page header */
904 u8 *data; /* Equal to pPage->aData */
danielk1977aef0bf62005-12-30 16:28:01 +0000905 BtShared *pBt; /* The main btree structure */
drh271efa52004-05-30 19:19:05 +0000906 int usableSize; /* Amount of usable space on each page */
907 int cellOffset; /* Offset from start of page to first cell pointer */
908 int nFree; /* Number of unused bytes on the page */
909 int top; /* First byte of the cell content area */
drh2af926b2001-05-15 00:39:25 +0000910
drh2e38c322004-09-03 18:38:44 +0000911 pBt = pPage->pBt;
912 assert( pBt!=0 );
913 assert( pParent==0 || pParent->pBt==pBt );
drh1fee73e2007-08-29 04:00:57 +0000914 assert( sqlite3_mutex_held(pBt->mutex) );
danielk19773b8a05f2007-03-19 17:44:26 +0000915 assert( pPage->pgno==sqlite3PagerPagenumber(pPage->pDbPage) );
drhbf4bca52007-09-06 22:19:14 +0000916 assert( pPage == sqlite3PagerGetExtra(pPage->pDbPage) );
917 assert( pPage->aData == sqlite3PagerGetData(pPage->pDbPage) );
drhee696e22004-08-30 16:52:17 +0000918 if( pPage->pParent!=pParent && (pPage->pParent!=0 || pPage->isInit) ){
919 /* The parent page should never change unless the file is corrupt */
drh49285702005-09-17 15:20:26 +0000920 return SQLITE_CORRUPT_BKPT;
drhee696e22004-08-30 16:52:17 +0000921 }
drh10617cd2004-05-14 15:27:27 +0000922 if( pPage->isInit ) return SQLITE_OK;
drhda200cc2004-05-09 11:51:38 +0000923 if( pPage->pParent==0 && pParent!=0 ){
924 pPage->pParent = pParent;
danielk19773b8a05f2007-03-19 17:44:26 +0000925 sqlite3PagerRef(pParent->pDbPage);
drh5e2f8b92001-05-28 00:41:15 +0000926 }
drhde647132004-05-07 17:57:49 +0000927 hdr = pPage->hdrOffset;
drha34b6762004-05-07 13:30:42 +0000928 data = pPage->aData;
drh271efa52004-05-30 19:19:05 +0000929 decodeFlags(pPage, data[hdr]);
drh43605152004-05-29 21:46:49 +0000930 pPage->nOverflow = 0;
drhc8629a12004-05-08 20:07:40 +0000931 pPage->idxShift = 0;
drh2e38c322004-09-03 18:38:44 +0000932 usableSize = pBt->usableSize;
drh43605152004-05-29 21:46:49 +0000933 pPage->cellOffset = cellOffset = hdr + 12 - 4*pPage->leaf;
934 top = get2byte(&data[hdr+5]);
935 pPage->nCell = get2byte(&data[hdr+3]);
drh2e38c322004-09-03 18:38:44 +0000936 if( pPage->nCell>MX_CELL(pBt) ){
drhee696e22004-08-30 16:52:17 +0000937 /* To many cells for a single page. The page must be corrupt */
drh49285702005-09-17 15:20:26 +0000938 return SQLITE_CORRUPT_BKPT;
drhee696e22004-08-30 16:52:17 +0000939 }
940 if( pPage->nCell==0 && pParent!=0 && pParent->pgno!=1 ){
941 /* All pages must have at least one cell, except for root pages */
drh49285702005-09-17 15:20:26 +0000942 return SQLITE_CORRUPT_BKPT;
drhee696e22004-08-30 16:52:17 +0000943 }
drh9e572e62004-04-23 23:43:10 +0000944
945 /* Compute the total free space on the page */
drh9e572e62004-04-23 23:43:10 +0000946 pc = get2byte(&data[hdr+1]);
drh43605152004-05-29 21:46:49 +0000947 nFree = data[hdr+7] + top - (cellOffset + 2*pPage->nCell);
drh9e572e62004-04-23 23:43:10 +0000948 while( pc>0 ){
949 int next, size;
drhee696e22004-08-30 16:52:17 +0000950 if( pc>usableSize-4 ){
951 /* Free block is off the page */
drh49285702005-09-17 15:20:26 +0000952 return SQLITE_CORRUPT_BKPT;
drhee696e22004-08-30 16:52:17 +0000953 }
drh9e572e62004-04-23 23:43:10 +0000954 next = get2byte(&data[pc]);
955 size = get2byte(&data[pc+2]);
drhee696e22004-08-30 16:52:17 +0000956 if( next>0 && next<=pc+size+3 ){
957 /* Free blocks must be in accending order */
drh49285702005-09-17 15:20:26 +0000958 return SQLITE_CORRUPT_BKPT;
drhee696e22004-08-30 16:52:17 +0000959 }
drh3add3672004-05-15 00:29:24 +0000960 nFree += size;
drh9e572e62004-04-23 23:43:10 +0000961 pc = next;
962 }
drh3add3672004-05-15 00:29:24 +0000963 pPage->nFree = nFree;
drhee696e22004-08-30 16:52:17 +0000964 if( nFree>=usableSize ){
965 /* Free space cannot exceed total page size */
drh49285702005-09-17 15:20:26 +0000966 return SQLITE_CORRUPT_BKPT;
drhee696e22004-08-30 16:52:17 +0000967 }
drh9e572e62004-04-23 23:43:10 +0000968
drhde647132004-05-07 17:57:49 +0000969 pPage->isInit = 1;
drh9e572e62004-04-23 23:43:10 +0000970 return SQLITE_OK;
drh7e3b0a02001-04-28 16:52:40 +0000971}
972
973/*
drh8b2f49b2001-06-08 00:21:52 +0000974** Set up a raw page so that it looks like a database page holding
975** no entries.
drhbd03cae2001-06-02 02:40:57 +0000976*/
drh9e572e62004-04-23 23:43:10 +0000977static void zeroPage(MemPage *pPage, int flags){
978 unsigned char *data = pPage->aData;
danielk1977aef0bf62005-12-30 16:28:01 +0000979 BtShared *pBt = pPage->pBt;
drh3aac2dd2004-04-26 14:10:20 +0000980 int hdr = pPage->hdrOffset;
drh9e572e62004-04-23 23:43:10 +0000981 int first;
982
danielk19773b8a05f2007-03-19 17:44:26 +0000983 assert( sqlite3PagerPagenumber(pPage->pDbPage)==pPage->pgno );
drhbf4bca52007-09-06 22:19:14 +0000984 assert( sqlite3PagerGetExtra(pPage->pDbPage) == (void*)pPage );
985 assert( sqlite3PagerGetData(pPage->pDbPage) == data );
danielk19773b8a05f2007-03-19 17:44:26 +0000986 assert( sqlite3PagerIswriteable(pPage->pDbPage) );
drh1fee73e2007-08-29 04:00:57 +0000987 assert( sqlite3_mutex_held(pBt->mutex) );
drhb6f41482004-05-14 01:58:11 +0000988 memset(&data[hdr], 0, pBt->usableSize - hdr);
drh9e572e62004-04-23 23:43:10 +0000989 data[hdr] = flags;
drh43605152004-05-29 21:46:49 +0000990 first = hdr + 8 + 4*((flags&PTF_LEAF)==0);
991 memset(&data[hdr+1], 0, 4);
992 data[hdr+7] = 0;
993 put2byte(&data[hdr+5], pBt->usableSize);
drhb6f41482004-05-14 01:58:11 +0000994 pPage->nFree = pBt->usableSize - first;
drh271efa52004-05-30 19:19:05 +0000995 decodeFlags(pPage, flags);
drh9e572e62004-04-23 23:43:10 +0000996 pPage->hdrOffset = hdr;
drh43605152004-05-29 21:46:49 +0000997 pPage->cellOffset = first;
998 pPage->nOverflow = 0;
drhda200cc2004-05-09 11:51:38 +0000999 pPage->idxShift = 0;
drh43605152004-05-29 21:46:49 +00001000 pPage->nCell = 0;
drhda200cc2004-05-09 11:51:38 +00001001 pPage->isInit = 1;
drhbd03cae2001-06-02 02:40:57 +00001002}
1003
1004/*
drh3aac2dd2004-04-26 14:10:20 +00001005** Get a page from the pager. Initialize the MemPage.pBt and
1006** MemPage.aData elements if needed.
drh538f5702007-04-13 02:14:30 +00001007**
1008** If the noContent flag is set, it means that we do not care about
1009** the content of the page at this time. So do not go to the disk
1010** to fetch the content. Just fill in the content with zeros for now.
1011** If in the future we call sqlite3PagerWrite() on this page, that
1012** means we have started to be concerned about content and the disk
1013** read should occur at that point.
drh3aac2dd2004-04-26 14:10:20 +00001014*/
drh16a9b832007-05-05 18:39:25 +00001015int sqlite3BtreeGetPage(
1016 BtShared *pBt, /* The btree */
1017 Pgno pgno, /* Number of the page to fetch */
1018 MemPage **ppPage, /* Return the page in this parameter */
1019 int noContent /* Do not load page content if true */
1020){
drh3aac2dd2004-04-26 14:10:20 +00001021 int rc;
drh3aac2dd2004-04-26 14:10:20 +00001022 MemPage *pPage;
danielk19773b8a05f2007-03-19 17:44:26 +00001023 DbPage *pDbPage;
1024
drh1fee73e2007-08-29 04:00:57 +00001025 assert( sqlite3_mutex_held(pBt->mutex) );
drh538f5702007-04-13 02:14:30 +00001026 rc = sqlite3PagerAcquire(pBt->pPager, pgno, (DbPage**)&pDbPage, noContent);
drh3aac2dd2004-04-26 14:10:20 +00001027 if( rc ) return rc;
danielk19773b8a05f2007-03-19 17:44:26 +00001028 pPage = (MemPage *)sqlite3PagerGetExtra(pDbPage);
1029 pPage->aData = sqlite3PagerGetData(pDbPage);
1030 pPage->pDbPage = pDbPage;
drh3aac2dd2004-04-26 14:10:20 +00001031 pPage->pBt = pBt;
1032 pPage->pgno = pgno;
drhde647132004-05-07 17:57:49 +00001033 pPage->hdrOffset = pPage->pgno==1 ? 100 : 0;
drh3aac2dd2004-04-26 14:10:20 +00001034 *ppPage = pPage;
1035 return SQLITE_OK;
1036}
1037
1038/*
drhde647132004-05-07 17:57:49 +00001039** Get a page from the pager and initialize it. This routine
1040** is just a convenience wrapper around separate calls to
drh16a9b832007-05-05 18:39:25 +00001041** sqlite3BtreeGetPage() and sqlite3BtreeInitPage().
drhde647132004-05-07 17:57:49 +00001042*/
1043static int getAndInitPage(
danielk1977aef0bf62005-12-30 16:28:01 +00001044 BtShared *pBt, /* The database file */
drhde647132004-05-07 17:57:49 +00001045 Pgno pgno, /* Number of the page to get */
1046 MemPage **ppPage, /* Write the page pointer here */
1047 MemPage *pParent /* Parent of the page */
1048){
1049 int rc;
drh1fee73e2007-08-29 04:00:57 +00001050 assert( sqlite3_mutex_held(pBt->mutex) );
drhee696e22004-08-30 16:52:17 +00001051 if( pgno==0 ){
drh49285702005-09-17 15:20:26 +00001052 return SQLITE_CORRUPT_BKPT;
drhee696e22004-08-30 16:52:17 +00001053 }
drh16a9b832007-05-05 18:39:25 +00001054 rc = sqlite3BtreeGetPage(pBt, pgno, ppPage, 0);
drh10617cd2004-05-14 15:27:27 +00001055 if( rc==SQLITE_OK && (*ppPage)->isInit==0 ){
drh16a9b832007-05-05 18:39:25 +00001056 rc = sqlite3BtreeInitPage(*ppPage, pParent);
danielk197743e377a2008-05-05 12:09:32 +00001057 if( rc!=SQLITE_OK ){
1058 releasePage(*ppPage);
1059 *ppPage = 0;
1060 }
drhde647132004-05-07 17:57:49 +00001061 }
1062 return rc;
1063}
1064
1065/*
drh3aac2dd2004-04-26 14:10:20 +00001066** Release a MemPage. This should be called once for each prior
drh16a9b832007-05-05 18:39:25 +00001067** call to sqlite3BtreeGetPage.
drh3aac2dd2004-04-26 14:10:20 +00001068*/
drh4b70f112004-05-02 21:12:19 +00001069static void releasePage(MemPage *pPage){
drh3aac2dd2004-04-26 14:10:20 +00001070 if( pPage ){
1071 assert( pPage->aData );
1072 assert( pPage->pBt );
drhbf4bca52007-09-06 22:19:14 +00001073 assert( sqlite3PagerGetExtra(pPage->pDbPage) == (void*)pPage );
1074 assert( sqlite3PagerGetData(pPage->pDbPage)==pPage->aData );
drh1fee73e2007-08-29 04:00:57 +00001075 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
danielk19773b8a05f2007-03-19 17:44:26 +00001076 sqlite3PagerUnref(pPage->pDbPage);
drh3aac2dd2004-04-26 14:10:20 +00001077 }
1078}
1079
1080/*
drh72f82862001-05-24 21:06:34 +00001081** This routine is called when the reference count for a page
1082** reaches zero. We need to unref the pParent pointer when that
1083** happens.
1084*/
danielk19773b8a05f2007-03-19 17:44:26 +00001085static void pageDestructor(DbPage *pData, int pageSize){
drh07d183d2005-05-01 22:52:42 +00001086 MemPage *pPage;
1087 assert( (pageSize & 7)==0 );
danielk19773b8a05f2007-03-19 17:44:26 +00001088 pPage = (MemPage *)sqlite3PagerGetExtra(pData);
drh1fee73e2007-08-29 04:00:57 +00001089 assert( pPage->isInit==0 || sqlite3_mutex_held(pPage->pBt->mutex) );
drh72f82862001-05-24 21:06:34 +00001090 if( pPage->pParent ){
1091 MemPage *pParent = pPage->pParent;
drhd0679ed2007-08-28 22:24:34 +00001092 assert( pParent->pBt==pPage->pBt );
drh72f82862001-05-24 21:06:34 +00001093 pPage->pParent = 0;
drha34b6762004-05-07 13:30:42 +00001094 releasePage(pParent);
drh72f82862001-05-24 21:06:34 +00001095 }
drh3aac2dd2004-04-26 14:10:20 +00001096 pPage->isInit = 0;
drh72f82862001-05-24 21:06:34 +00001097}
1098
1099/*
drha6abd042004-06-09 17:37:22 +00001100** During a rollback, when the pager reloads information into the cache
1101** so that the cache is restored to its original state at the start of
1102** the transaction, for each page restored this routine is called.
1103**
1104** This routine needs to reset the extra data section at the end of the
1105** page to agree with the restored data.
1106*/
danielk19773b8a05f2007-03-19 17:44:26 +00001107static void pageReinit(DbPage *pData, int pageSize){
drh07d183d2005-05-01 22:52:42 +00001108 MemPage *pPage;
1109 assert( (pageSize & 7)==0 );
danielk19773b8a05f2007-03-19 17:44:26 +00001110 pPage = (MemPage *)sqlite3PagerGetExtra(pData);
drha6abd042004-06-09 17:37:22 +00001111 if( pPage->isInit ){
drh1fee73e2007-08-29 04:00:57 +00001112 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
drha6abd042004-06-09 17:37:22 +00001113 pPage->isInit = 0;
drh16a9b832007-05-05 18:39:25 +00001114 sqlite3BtreeInitPage(pPage, pPage->pParent);
drha6abd042004-06-09 17:37:22 +00001115 }
1116}
1117
1118/*
drhe5fe6902007-12-07 18:55:28 +00001119** Invoke the busy handler for a btree.
1120*/
1121static int sqlite3BtreeInvokeBusyHandler(void *pArg, int n){
1122 BtShared *pBt = (BtShared*)pArg;
1123 assert( pBt->db );
1124 assert( sqlite3_mutex_held(pBt->db->mutex) );
1125 return sqlite3InvokeBusyHandler(&pBt->db->busyHandler);
1126}
1127
1128/*
drhad3e0102004-09-03 23:32:18 +00001129** Open a database file.
1130**
drh382c0242001-10-06 16:33:02 +00001131** zFilename is the name of the database file. If zFilename is NULL
drh1bee3d72001-10-15 00:44:35 +00001132** a new database with a random name is created. This randomly named
drh23e11ca2004-05-04 17:27:28 +00001133** database file will be deleted when sqlite3BtreeClose() is called.
drhe53831d2007-08-17 01:14:38 +00001134** If zFilename is ":memory:" then an in-memory database is created
1135** that is automatically destroyed when it is closed.
drha059ad02001-04-17 20:09:11 +00001136*/
drh23e11ca2004-05-04 17:27:28 +00001137int sqlite3BtreeOpen(
drh3aac2dd2004-04-26 14:10:20 +00001138 const char *zFilename, /* Name of the file containing the BTree database */
drhe5fe6902007-12-07 18:55:28 +00001139 sqlite3 *db, /* Associated database handle */
drh3aac2dd2004-04-26 14:10:20 +00001140 Btree **ppBtree, /* Pointer to new Btree object written here */
drh33f4e022007-09-03 15:19:34 +00001141 int flags, /* Options */
1142 int vfsFlags /* Flags passed through to sqlite3_vfs.xOpen() */
drh6019e162001-07-02 17:51:45 +00001143){
drhd677b3d2007-08-20 22:48:41 +00001144 sqlite3_vfs *pVfs; /* The VFS to use for this btree */
drhe53831d2007-08-17 01:14:38 +00001145 BtShared *pBt = 0; /* Shared part of btree structure */
danielk1977aef0bf62005-12-30 16:28:01 +00001146 Btree *p; /* Handle to return */
danielk1977dddbcdc2007-04-26 14:42:34 +00001147 int rc = SQLITE_OK;
drh90f5ecb2004-07-22 01:19:35 +00001148 int nReserve;
1149 unsigned char zDbHeader[100];
danielk1977aef0bf62005-12-30 16:28:01 +00001150
1151 /* Set the variable isMemdb to true for an in-memory database, or
1152 ** false for a file-based database. This symbol is only required if
1153 ** either of the shared-data or autovacuum features are compiled
1154 ** into the library.
1155 */
1156#if !defined(SQLITE_OMIT_SHARED_CACHE) || !defined(SQLITE_OMIT_AUTOVACUUM)
1157 #ifdef SQLITE_OMIT_MEMORYDB
drh980b1a72006-08-16 16:42:48 +00001158 const int isMemdb = 0;
danielk1977aef0bf62005-12-30 16:28:01 +00001159 #else
drh980b1a72006-08-16 16:42:48 +00001160 const int isMemdb = zFilename && !strcmp(zFilename, ":memory:");
danielk1977aef0bf62005-12-30 16:28:01 +00001161 #endif
1162#endif
1163
drhe5fe6902007-12-07 18:55:28 +00001164 assert( db!=0 );
1165 assert( sqlite3_mutex_held(db->mutex) );
drh153c62c2007-08-24 03:51:33 +00001166
drhe5fe6902007-12-07 18:55:28 +00001167 pVfs = db->pVfs;
drh17435752007-08-16 04:30:38 +00001168 p = sqlite3MallocZero(sizeof(Btree));
danielk1977aef0bf62005-12-30 16:28:01 +00001169 if( !p ){
1170 return SQLITE_NOMEM;
1171 }
1172 p->inTrans = TRANS_NONE;
drhe5fe6902007-12-07 18:55:28 +00001173 p->db = db;
danielk1977aef0bf62005-12-30 16:28:01 +00001174
drh198bf392006-01-06 21:52:49 +00001175#if !defined(SQLITE_OMIT_SHARED_CACHE) && !defined(SQLITE_OMIT_DISKIO)
drhe53831d2007-08-17 01:14:38 +00001176 /*
1177 ** If this Btree is a candidate for shared cache, try to find an
1178 ** existing BtShared object that we can share with
1179 */
1180 if( (flags & BTREE_PRIVATE)==0
1181 && isMemdb==0
drhe5fe6902007-12-07 18:55:28 +00001182 && (db->flags & SQLITE_Vtab)==0
drhe53831d2007-08-17 01:14:38 +00001183 && zFilename && zFilename[0]
drhe53831d2007-08-17 01:14:38 +00001184 ){
drhff0587c2007-08-29 17:43:19 +00001185 if( sqlite3SharedCacheEnabled ){
danielk1977adfb9b02007-09-17 07:02:56 +00001186 int nFullPathname = pVfs->mxPathname+1;
1187 char *zFullPathname = (char *)sqlite3_malloc(nFullPathname);
drhff0587c2007-08-29 17:43:19 +00001188 sqlite3_mutex *mutexShared;
1189 p->sharable = 1;
drhe5fe6902007-12-07 18:55:28 +00001190 if( db ){
1191 db->flags |= SQLITE_SharedCache;
danielk1977aef0bf62005-12-30 16:28:01 +00001192 }
drhff0587c2007-08-29 17:43:19 +00001193 if( !zFullPathname ){
1194 sqlite3_free(p);
1195 return SQLITE_NOMEM;
1196 }
danielk1977adfb9b02007-09-17 07:02:56 +00001197 sqlite3OsFullPathname(pVfs, zFilename, nFullPathname, zFullPathname);
drhff0587c2007-08-29 17:43:19 +00001198 mutexShared = sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_MASTER);
1199 sqlite3_mutex_enter(mutexShared);
1200 for(pBt=sqlite3SharedCacheList; pBt; pBt=pBt->pNext){
1201 assert( pBt->nRef>0 );
1202 if( 0==strcmp(zFullPathname, sqlite3PagerFilename(pBt->pPager))
1203 && sqlite3PagerVfs(pBt->pPager)==pVfs ){
1204 p->pBt = pBt;
1205 pBt->nRef++;
1206 break;
1207 }
1208 }
1209 sqlite3_mutex_leave(mutexShared);
1210 sqlite3_free(zFullPathname);
danielk1977aef0bf62005-12-30 16:28:01 +00001211 }
drhff0587c2007-08-29 17:43:19 +00001212#ifdef SQLITE_DEBUG
1213 else{
1214 /* In debug mode, we mark all persistent databases as sharable
1215 ** even when they are not. This exercises the locking code and
1216 ** gives more opportunity for asserts(sqlite3_mutex_held())
1217 ** statements to find locking problems.
1218 */
1219 p->sharable = 1;
1220 }
1221#endif
danielk1977aef0bf62005-12-30 16:28:01 +00001222 }
1223#endif
drha059ad02001-04-17 20:09:11 +00001224 if( pBt==0 ){
drhe53831d2007-08-17 01:14:38 +00001225 /*
1226 ** The following asserts make sure that structures used by the btree are
1227 ** the right size. This is to guard against size changes that result
1228 ** when compiling on a different architecture.
danielk197703aded42004-11-22 05:26:27 +00001229 */
drhe53831d2007-08-17 01:14:38 +00001230 assert( sizeof(i64)==8 || sizeof(i64)==4 );
1231 assert( sizeof(u64)==8 || sizeof(u64)==4 );
1232 assert( sizeof(u32)==4 );
1233 assert( sizeof(u16)==2 );
1234 assert( sizeof(Pgno)==4 );
1235
1236 pBt = sqlite3MallocZero( sizeof(*pBt) );
1237 if( pBt==0 ){
1238 rc = SQLITE_NOMEM;
1239 goto btree_open_out;
1240 }
drhe5fe6902007-12-07 18:55:28 +00001241 pBt->busyHdr.xFunc = sqlite3BtreeInvokeBusyHandler;
1242 pBt->busyHdr.pArg = pBt;
drh33f4e022007-09-03 15:19:34 +00001243 rc = sqlite3PagerOpen(pVfs, &pBt->pPager, zFilename,
1244 EXTRA_SIZE, flags, vfsFlags);
drhe53831d2007-08-17 01:14:38 +00001245 if( rc==SQLITE_OK ){
1246 rc = sqlite3PagerReadFileheader(pBt->pPager,sizeof(zDbHeader),zDbHeader);
1247 }
1248 if( rc!=SQLITE_OK ){
1249 goto btree_open_out;
1250 }
drhe5fe6902007-12-07 18:55:28 +00001251 sqlite3PagerSetBusyhandler(pBt->pPager, &pBt->busyHdr);
drhe53831d2007-08-17 01:14:38 +00001252 p->pBt = pBt;
1253
1254 sqlite3PagerSetDestructor(pBt->pPager, pageDestructor);
1255 sqlite3PagerSetReiniter(pBt->pPager, pageReinit);
1256 pBt->pCursor = 0;
1257 pBt->pPage1 = 0;
1258 pBt->readOnly = sqlite3PagerIsreadonly(pBt->pPager);
1259 pBt->pageSize = get2byte(&zDbHeader[16]);
1260 if( pBt->pageSize<512 || pBt->pageSize>SQLITE_MAX_PAGE_SIZE
1261 || ((pBt->pageSize-1)&pBt->pageSize)!=0 ){
danielk1977a1644fd2007-08-29 12:31:25 +00001262 pBt->pageSize = 0;
1263 sqlite3PagerSetPagesize(pBt->pPager, &pBt->pageSize);
drhe53831d2007-08-17 01:14:38 +00001264 pBt->maxEmbedFrac = 64; /* 25% */
1265 pBt->minEmbedFrac = 32; /* 12.5% */
1266 pBt->minLeafFrac = 32; /* 12.5% */
1267#ifndef SQLITE_OMIT_AUTOVACUUM
1268 /* If the magic name ":memory:" will create an in-memory database, then
1269 ** leave the autoVacuum mode at 0 (do not auto-vacuum), even if
1270 ** SQLITE_DEFAULT_AUTOVACUUM is true. On the other hand, if
1271 ** SQLITE_OMIT_MEMORYDB has been defined, then ":memory:" is just a
1272 ** regular file-name. In this case the auto-vacuum applies as per normal.
1273 */
1274 if( zFilename && !isMemdb ){
1275 pBt->autoVacuum = (SQLITE_DEFAULT_AUTOVACUUM ? 1 : 0);
1276 pBt->incrVacuum = (SQLITE_DEFAULT_AUTOVACUUM==2 ? 1 : 0);
1277 }
1278#endif
1279 nReserve = 0;
1280 }else{
1281 nReserve = zDbHeader[20];
1282 pBt->maxEmbedFrac = zDbHeader[21];
1283 pBt->minEmbedFrac = zDbHeader[22];
1284 pBt->minLeafFrac = zDbHeader[23];
1285 pBt->pageSizeFixed = 1;
1286#ifndef SQLITE_OMIT_AUTOVACUUM
1287 pBt->autoVacuum = (get4byte(&zDbHeader[36 + 4*4])?1:0);
1288 pBt->incrVacuum = (get4byte(&zDbHeader[36 + 7*4])?1:0);
1289#endif
1290 }
1291 pBt->usableSize = pBt->pageSize - nReserve;
1292 assert( (pBt->pageSize & 7)==0 ); /* 8-byte alignment of pageSize */
danielk1977a1644fd2007-08-29 12:31:25 +00001293 sqlite3PagerSetPagesize(pBt->pPager, &pBt->pageSize);
drhe53831d2007-08-17 01:14:38 +00001294
1295#if !defined(SQLITE_OMIT_SHARED_CACHE) && !defined(SQLITE_OMIT_DISKIO)
1296 /* Add the new BtShared object to the linked list sharable BtShareds.
1297 */
1298 if( p->sharable ){
1299 sqlite3_mutex *mutexShared;
1300 pBt->nRef = 1;
1301 mutexShared = sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_MASTER);
drh3285db22007-09-03 22:00:39 +00001302 if( SQLITE_THREADSAFE ){
1303 pBt->mutex = sqlite3_mutex_alloc(SQLITE_MUTEX_FAST);
1304 if( pBt->mutex==0 ){
1305 rc = SQLITE_NOMEM;
drhe5fe6902007-12-07 18:55:28 +00001306 db->mallocFailed = 0;
drh3285db22007-09-03 22:00:39 +00001307 goto btree_open_out;
1308 }
drhff0587c2007-08-29 17:43:19 +00001309 }
drhe53831d2007-08-17 01:14:38 +00001310 sqlite3_mutex_enter(mutexShared);
1311 pBt->pNext = sqlite3SharedCacheList;
1312 sqlite3SharedCacheList = pBt;
1313 sqlite3_mutex_leave(mutexShared);
danielk1977951af802004-11-05 15:45:09 +00001314 }
drheee46cf2004-11-06 00:02:48 +00001315#endif
drh90f5ecb2004-07-22 01:19:35 +00001316 }
danielk1977aef0bf62005-12-30 16:28:01 +00001317
drhcfed7bc2006-03-13 14:28:05 +00001318#if !defined(SQLITE_OMIT_SHARED_CACHE) && !defined(SQLITE_OMIT_DISKIO)
drhe53831d2007-08-17 01:14:38 +00001319 /* If the new Btree uses a sharable pBtShared, then link the new
1320 ** Btree into the list of all sharable Btrees for the same connection.
drhabddb0c2007-08-20 13:14:28 +00001321 ** The list is kept in ascending order by pBt address.
danielk197754f01982006-01-18 15:25:17 +00001322 */
drhe53831d2007-08-17 01:14:38 +00001323 if( p->sharable ){
1324 int i;
1325 Btree *pSib;
drhe5fe6902007-12-07 18:55:28 +00001326 for(i=0; i<db->nDb; i++){
1327 if( (pSib = db->aDb[i].pBt)!=0 && pSib->sharable ){
drhe53831d2007-08-17 01:14:38 +00001328 while( pSib->pPrev ){ pSib = pSib->pPrev; }
1329 if( p->pBt<pSib->pBt ){
1330 p->pNext = pSib;
1331 p->pPrev = 0;
1332 pSib->pPrev = p;
1333 }else{
drhabddb0c2007-08-20 13:14:28 +00001334 while( pSib->pNext && pSib->pNext->pBt<p->pBt ){
drhe53831d2007-08-17 01:14:38 +00001335 pSib = pSib->pNext;
1336 }
1337 p->pNext = pSib->pNext;
1338 p->pPrev = pSib;
1339 if( p->pNext ){
1340 p->pNext->pPrev = p;
1341 }
1342 pSib->pNext = p;
1343 }
1344 break;
1345 }
1346 }
danielk1977aef0bf62005-12-30 16:28:01 +00001347 }
danielk1977aef0bf62005-12-30 16:28:01 +00001348#endif
1349 *ppBtree = p;
danielk1977dddbcdc2007-04-26 14:42:34 +00001350
1351btree_open_out:
1352 if( rc!=SQLITE_OK ){
1353 if( pBt && pBt->pPager ){
1354 sqlite3PagerClose(pBt->pPager);
1355 }
drh17435752007-08-16 04:30:38 +00001356 sqlite3_free(pBt);
1357 sqlite3_free(p);
danielk1977dddbcdc2007-04-26 14:42:34 +00001358 *ppBtree = 0;
1359 }
1360 return rc;
drha059ad02001-04-17 20:09:11 +00001361}
1362
1363/*
drhe53831d2007-08-17 01:14:38 +00001364** Decrement the BtShared.nRef counter. When it reaches zero,
1365** remove the BtShared structure from the sharing list. Return
1366** true if the BtShared.nRef counter reaches zero and return
1367** false if it is still positive.
1368*/
1369static int removeFromSharingList(BtShared *pBt){
1370#ifndef SQLITE_OMIT_SHARED_CACHE
1371 sqlite3_mutex *pMaster;
1372 BtShared *pList;
1373 int removed = 0;
1374
drhd677b3d2007-08-20 22:48:41 +00001375 assert( sqlite3_mutex_notheld(pBt->mutex) );
drhe53831d2007-08-17 01:14:38 +00001376 pMaster = sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_MASTER);
1377 sqlite3_mutex_enter(pMaster);
1378 pBt->nRef--;
1379 if( pBt->nRef<=0 ){
1380 if( sqlite3SharedCacheList==pBt ){
1381 sqlite3SharedCacheList = pBt->pNext;
1382 }else{
1383 pList = sqlite3SharedCacheList;
1384 while( pList && pList->pNext!=pBt ){
1385 pList=pList->pNext;
1386 }
1387 if( pList ){
1388 pList->pNext = pBt->pNext;
1389 }
1390 }
drh3285db22007-09-03 22:00:39 +00001391 if( SQLITE_THREADSAFE ){
1392 sqlite3_mutex_free(pBt->mutex);
1393 }
drhe53831d2007-08-17 01:14:38 +00001394 removed = 1;
1395 }
1396 sqlite3_mutex_leave(pMaster);
1397 return removed;
1398#else
1399 return 1;
1400#endif
1401}
1402
1403/*
drha059ad02001-04-17 20:09:11 +00001404** Close an open database and invalidate all cursors.
1405*/
danielk1977aef0bf62005-12-30 16:28:01 +00001406int sqlite3BtreeClose(Btree *p){
danielk1977aef0bf62005-12-30 16:28:01 +00001407 BtShared *pBt = p->pBt;
1408 BtCursor *pCur;
1409
danielk1977aef0bf62005-12-30 16:28:01 +00001410 /* Close all cursors opened via this handle. */
drhe5fe6902007-12-07 18:55:28 +00001411 assert( sqlite3_mutex_held(p->db->mutex) );
drhe53831d2007-08-17 01:14:38 +00001412 sqlite3BtreeEnter(p);
drhe5fe6902007-12-07 18:55:28 +00001413 pBt->db = p->db;
danielk1977aef0bf62005-12-30 16:28:01 +00001414 pCur = pBt->pCursor;
1415 while( pCur ){
1416 BtCursor *pTmp = pCur;
1417 pCur = pCur->pNext;
1418 if( pTmp->pBtree==p ){
1419 sqlite3BtreeCloseCursor(pTmp);
1420 }
drha059ad02001-04-17 20:09:11 +00001421 }
danielk1977aef0bf62005-12-30 16:28:01 +00001422
danielk19778d34dfd2006-01-24 16:37:57 +00001423 /* Rollback any active transaction and free the handle structure.
1424 ** The call to sqlite3BtreeRollback() drops any table-locks held by
1425 ** this handle.
1426 */
danielk1977b597f742006-01-15 11:39:18 +00001427 sqlite3BtreeRollback(p);
drhe53831d2007-08-17 01:14:38 +00001428 sqlite3BtreeLeave(p);
danielk1977aef0bf62005-12-30 16:28:01 +00001429
danielk1977aef0bf62005-12-30 16:28:01 +00001430 /* If there are still other outstanding references to the shared-btree
1431 ** structure, return now. The remainder of this procedure cleans
1432 ** up the shared-btree.
1433 */
drhe53831d2007-08-17 01:14:38 +00001434 assert( p->wantToLock==0 && p->locked==0 );
1435 if( !p->sharable || removeFromSharingList(pBt) ){
1436 /* The pBt is no longer on the sharing list, so we can access
1437 ** it without having to hold the mutex.
1438 **
1439 ** Clean out and delete the BtShared object.
1440 */
1441 assert( !pBt->pCursor );
drhe53831d2007-08-17 01:14:38 +00001442 sqlite3PagerClose(pBt->pPager);
1443 if( pBt->xFreeSchema && pBt->pSchema ){
1444 pBt->xFreeSchema(pBt->pSchema);
1445 }
1446 sqlite3_free(pBt->pSchema);
danielk197752ae7242008-03-25 14:24:56 +00001447 sqlite3_free(pBt->pTmpSpace);
drhe53831d2007-08-17 01:14:38 +00001448 sqlite3_free(pBt);
danielk1977aef0bf62005-12-30 16:28:01 +00001449 }
1450
drhe53831d2007-08-17 01:14:38 +00001451#ifndef SQLITE_OMIT_SHARED_CACHE
drhcab5ed72007-08-22 11:41:18 +00001452 assert( p->wantToLock==0 );
1453 assert( p->locked==0 );
1454 if( p->pPrev ) p->pPrev->pNext = p->pNext;
1455 if( p->pNext ) p->pNext->pPrev = p->pPrev;
danielk1977aef0bf62005-12-30 16:28:01 +00001456#endif
1457
drhe53831d2007-08-17 01:14:38 +00001458 sqlite3_free(p);
drha059ad02001-04-17 20:09:11 +00001459 return SQLITE_OK;
1460}
1461
1462/*
drhda47d772002-12-02 04:25:19 +00001463** Change the limit on the number of pages allowed in the cache.
drhcd61c282002-03-06 22:01:34 +00001464**
1465** The maximum number of cache pages is set to the absolute
1466** value of mxPage. If mxPage is negative, the pager will
1467** operate asynchronously - it will not stop to do fsync()s
1468** to insure data is written to the disk surface before
1469** continuing. Transactions still work if synchronous is off,
1470** and the database cannot be corrupted if this program
1471** crashes. But if the operating system crashes or there is
1472** an abrupt power failure when synchronous is off, the database
1473** could be left in an inconsistent and unrecoverable state.
1474** Synchronous is on by default so database corruption is not
1475** normally a worry.
drhf57b14a2001-09-14 18:54:08 +00001476*/
danielk1977aef0bf62005-12-30 16:28:01 +00001477int sqlite3BtreeSetCacheSize(Btree *p, int mxPage){
1478 BtShared *pBt = p->pBt;
drhe5fe6902007-12-07 18:55:28 +00001479 assert( sqlite3_mutex_held(p->db->mutex) );
drhd677b3d2007-08-20 22:48:41 +00001480 sqlite3BtreeEnter(p);
danielk19773b8a05f2007-03-19 17:44:26 +00001481 sqlite3PagerSetCachesize(pBt->pPager, mxPage);
drhd677b3d2007-08-20 22:48:41 +00001482 sqlite3BtreeLeave(p);
drhf57b14a2001-09-14 18:54:08 +00001483 return SQLITE_OK;
1484}
1485
1486/*
drh973b6e32003-02-12 14:09:42 +00001487** Change the way data is synced to disk in order to increase or decrease
1488** how well the database resists damage due to OS crashes and power
1489** failures. Level 1 is the same as asynchronous (no syncs() occur and
1490** there is a high probability of damage) Level 2 is the default. There
1491** is a very low but non-zero probability of damage. Level 3 reduces the
1492** probability of damage to near zero but with a write performance reduction.
1493*/
danielk197793758c82005-01-21 08:13:14 +00001494#ifndef SQLITE_OMIT_PAGER_PRAGMAS
drhac530b12006-02-11 01:25:50 +00001495int sqlite3BtreeSetSafetyLevel(Btree *p, int level, int fullSync){
danielk1977aef0bf62005-12-30 16:28:01 +00001496 BtShared *pBt = p->pBt;
drhe5fe6902007-12-07 18:55:28 +00001497 assert( sqlite3_mutex_held(p->db->mutex) );
drhd677b3d2007-08-20 22:48:41 +00001498 sqlite3BtreeEnter(p);
danielk19773b8a05f2007-03-19 17:44:26 +00001499 sqlite3PagerSetSafetyLevel(pBt->pPager, level, fullSync);
drhd677b3d2007-08-20 22:48:41 +00001500 sqlite3BtreeLeave(p);
drh973b6e32003-02-12 14:09:42 +00001501 return SQLITE_OK;
1502}
danielk197793758c82005-01-21 08:13:14 +00001503#endif
drh973b6e32003-02-12 14:09:42 +00001504
drh2c8997b2005-08-27 16:36:48 +00001505/*
1506** Return TRUE if the given btree is set to safety level 1. In other
1507** words, return TRUE if no sync() occurs on the disk files.
1508*/
danielk1977aef0bf62005-12-30 16:28:01 +00001509int sqlite3BtreeSyncDisabled(Btree *p){
1510 BtShared *pBt = p->pBt;
drhd677b3d2007-08-20 22:48:41 +00001511 int rc;
drhe5fe6902007-12-07 18:55:28 +00001512 assert( sqlite3_mutex_held(p->db->mutex) );
drhd677b3d2007-08-20 22:48:41 +00001513 sqlite3BtreeEnter(p);
drhd0679ed2007-08-28 22:24:34 +00001514 assert( pBt && pBt->pPager );
drhd677b3d2007-08-20 22:48:41 +00001515 rc = sqlite3PagerNosync(pBt->pPager);
1516 sqlite3BtreeLeave(p);
1517 return rc;
drh2c8997b2005-08-27 16:36:48 +00001518}
1519
danielk1977576ec6b2005-01-21 11:55:25 +00001520#if !defined(SQLITE_OMIT_PAGER_PRAGMAS) || !defined(SQLITE_OMIT_VACUUM)
drh973b6e32003-02-12 14:09:42 +00001521/*
drh90f5ecb2004-07-22 01:19:35 +00001522** Change the default pages size and the number of reserved bytes per page.
drh06f50212004-11-02 14:24:33 +00001523**
1524** The page size must be a power of 2 between 512 and 65536. If the page
1525** size supplied does not meet this constraint then the page size is not
1526** changed.
1527**
1528** Page sizes are constrained to be a power of two so that the region
1529** of the database file used for locking (beginning at PENDING_BYTE,
1530** the first byte past the 1GB boundary, 0x40000000) needs to occur
1531** at the beginning of a page.
danielk197728129562005-01-11 10:25:06 +00001532**
1533** If parameter nReserve is less than zero, then the number of reserved
1534** bytes per page is left unchanged.
drh90f5ecb2004-07-22 01:19:35 +00001535*/
danielk1977aef0bf62005-12-30 16:28:01 +00001536int sqlite3BtreeSetPageSize(Btree *p, int pageSize, int nReserve){
danielk1977a1644fd2007-08-29 12:31:25 +00001537 int rc = SQLITE_OK;
danielk1977aef0bf62005-12-30 16:28:01 +00001538 BtShared *pBt = p->pBt;
drhd677b3d2007-08-20 22:48:41 +00001539 sqlite3BtreeEnter(p);
drh90f5ecb2004-07-22 01:19:35 +00001540 if( pBt->pageSizeFixed ){
drhd677b3d2007-08-20 22:48:41 +00001541 sqlite3BtreeLeave(p);
drh90f5ecb2004-07-22 01:19:35 +00001542 return SQLITE_READONLY;
1543 }
1544 if( nReserve<0 ){
1545 nReserve = pBt->pageSize - pBt->usableSize;
1546 }
drh06f50212004-11-02 14:24:33 +00001547 if( pageSize>=512 && pageSize<=SQLITE_MAX_PAGE_SIZE &&
1548 ((pageSize-1)&pageSize)==0 ){
drh07d183d2005-05-01 22:52:42 +00001549 assert( (pageSize & 7)==0 );
danielk1977aef0bf62005-12-30 16:28:01 +00001550 assert( !pBt->pPage1 && !pBt->pCursor );
danielk1977a1644fd2007-08-29 12:31:25 +00001551 pBt->pageSize = pageSize;
danielk197752ae7242008-03-25 14:24:56 +00001552 sqlite3_free(pBt->pTmpSpace);
1553 pBt->pTmpSpace = 0;
danielk1977a1644fd2007-08-29 12:31:25 +00001554 rc = sqlite3PagerSetPagesize(pBt->pPager, &pBt->pageSize);
drh90f5ecb2004-07-22 01:19:35 +00001555 }
1556 pBt->usableSize = pBt->pageSize - nReserve;
drhd677b3d2007-08-20 22:48:41 +00001557 sqlite3BtreeLeave(p);
danielk1977a1644fd2007-08-29 12:31:25 +00001558 return rc;
drh90f5ecb2004-07-22 01:19:35 +00001559}
1560
1561/*
1562** Return the currently defined page size
1563*/
danielk1977aef0bf62005-12-30 16:28:01 +00001564int sqlite3BtreeGetPageSize(Btree *p){
1565 return p->pBt->pageSize;
drh90f5ecb2004-07-22 01:19:35 +00001566}
danielk1977aef0bf62005-12-30 16:28:01 +00001567int sqlite3BtreeGetReserve(Btree *p){
drhd677b3d2007-08-20 22:48:41 +00001568 int n;
1569 sqlite3BtreeEnter(p);
1570 n = p->pBt->pageSize - p->pBt->usableSize;
1571 sqlite3BtreeLeave(p);
1572 return n;
drh2011d5f2004-07-22 02:40:37 +00001573}
drhf8e632b2007-05-08 14:51:36 +00001574
1575/*
1576** Set the maximum page count for a database if mxPage is positive.
1577** No changes are made if mxPage is 0 or negative.
1578** Regardless of the value of mxPage, return the maximum page count.
1579*/
1580int sqlite3BtreeMaxPageCount(Btree *p, int mxPage){
drhd677b3d2007-08-20 22:48:41 +00001581 int n;
1582 sqlite3BtreeEnter(p);
1583 n = sqlite3PagerMaxPageCount(p->pBt->pPager, mxPage);
1584 sqlite3BtreeLeave(p);
1585 return n;
drhf8e632b2007-05-08 14:51:36 +00001586}
danielk1977576ec6b2005-01-21 11:55:25 +00001587#endif /* !defined(SQLITE_OMIT_PAGER_PRAGMAS) || !defined(SQLITE_OMIT_VACUUM) */
drh90f5ecb2004-07-22 01:19:35 +00001588
1589/*
danielk1977951af802004-11-05 15:45:09 +00001590** Change the 'auto-vacuum' property of the database. If the 'autoVacuum'
1591** parameter is non-zero, then auto-vacuum mode is enabled. If zero, it
1592** is disabled. The default value for the auto-vacuum property is
1593** determined by the SQLITE_DEFAULT_AUTOVACUUM macro.
1594*/
danielk1977aef0bf62005-12-30 16:28:01 +00001595int sqlite3BtreeSetAutoVacuum(Btree *p, int autoVacuum){
danielk1977951af802004-11-05 15:45:09 +00001596#ifdef SQLITE_OMIT_AUTOVACUUM
drheee46cf2004-11-06 00:02:48 +00001597 return SQLITE_READONLY;
danielk1977951af802004-11-05 15:45:09 +00001598#else
danielk1977dddbcdc2007-04-26 14:42:34 +00001599 BtShared *pBt = p->pBt;
drhd677b3d2007-08-20 22:48:41 +00001600 int rc = SQLITE_OK;
danielk1977dddbcdc2007-04-26 14:42:34 +00001601 int av = (autoVacuum?1:0);
drhd677b3d2007-08-20 22:48:41 +00001602
1603 sqlite3BtreeEnter(p);
danielk1977dddbcdc2007-04-26 14:42:34 +00001604 if( pBt->pageSizeFixed && av!=pBt->autoVacuum ){
drhd677b3d2007-08-20 22:48:41 +00001605 rc = SQLITE_READONLY;
1606 }else{
1607 pBt->autoVacuum = av;
danielk1977951af802004-11-05 15:45:09 +00001608 }
drhd677b3d2007-08-20 22:48:41 +00001609 sqlite3BtreeLeave(p);
1610 return rc;
danielk1977951af802004-11-05 15:45:09 +00001611#endif
1612}
1613
1614/*
1615** Return the value of the 'auto-vacuum' property. If auto-vacuum is
1616** enabled 1 is returned. Otherwise 0.
1617*/
danielk1977aef0bf62005-12-30 16:28:01 +00001618int sqlite3BtreeGetAutoVacuum(Btree *p){
danielk1977951af802004-11-05 15:45:09 +00001619#ifdef SQLITE_OMIT_AUTOVACUUM
danielk1977dddbcdc2007-04-26 14:42:34 +00001620 return BTREE_AUTOVACUUM_NONE;
danielk1977951af802004-11-05 15:45:09 +00001621#else
drhd677b3d2007-08-20 22:48:41 +00001622 int rc;
1623 sqlite3BtreeEnter(p);
1624 rc = (
danielk1977dddbcdc2007-04-26 14:42:34 +00001625 (!p->pBt->autoVacuum)?BTREE_AUTOVACUUM_NONE:
1626 (!p->pBt->incrVacuum)?BTREE_AUTOVACUUM_FULL:
1627 BTREE_AUTOVACUUM_INCR
1628 );
drhd677b3d2007-08-20 22:48:41 +00001629 sqlite3BtreeLeave(p);
1630 return rc;
danielk1977951af802004-11-05 15:45:09 +00001631#endif
1632}
1633
1634
1635/*
drha34b6762004-05-07 13:30:42 +00001636** Get a reference to pPage1 of the database file. This will
drh306dc212001-05-21 13:45:10 +00001637** also acquire a readlock on that file.
1638**
1639** SQLITE_OK is returned on success. If the file is not a
1640** well-formed database file, then SQLITE_CORRUPT is returned.
1641** SQLITE_BUSY is returned if the database is locked. SQLITE_NOMEM
drh4f0ee682007-03-30 20:43:40 +00001642** is returned if we run out of memory.
drh306dc212001-05-21 13:45:10 +00001643*/
danielk1977aef0bf62005-12-30 16:28:01 +00001644static int lockBtree(BtShared *pBt){
danielk1977f653d782008-03-20 11:04:21 +00001645 int rc;
drh3aac2dd2004-04-26 14:10:20 +00001646 MemPage *pPage1;
danielk197793f7af92008-05-09 16:57:50 +00001647 int nPage;
drhd677b3d2007-08-20 22:48:41 +00001648
drh1fee73e2007-08-29 04:00:57 +00001649 assert( sqlite3_mutex_held(pBt->mutex) );
drha34b6762004-05-07 13:30:42 +00001650 if( pBt->pPage1 ) return SQLITE_OK;
drh16a9b832007-05-05 18:39:25 +00001651 rc = sqlite3BtreeGetPage(pBt, 1, &pPage1, 0);
drh306dc212001-05-21 13:45:10 +00001652 if( rc!=SQLITE_OK ) return rc;
drh306dc212001-05-21 13:45:10 +00001653
1654 /* Do some checking to help insure the file we opened really is
1655 ** a valid database file.
1656 */
danielk1977ad0132d2008-06-07 08:58:22 +00001657 rc = sqlite3PagerPagecount(pBt->pPager, &nPage);
1658 if( rc!=SQLITE_OK ){
danielk197793f7af92008-05-09 16:57:50 +00001659 goto page1_init_failed;
1660 }else if( nPage>0 ){
danielk1977f653d782008-03-20 11:04:21 +00001661 int pageSize;
1662 int usableSize;
drhb6f41482004-05-14 01:58:11 +00001663 u8 *page1 = pPage1->aData;
danielk1977ad0132d2008-06-07 08:58:22 +00001664 rc = SQLITE_NOTADB;
drhb6f41482004-05-14 01:58:11 +00001665 if( memcmp(page1, zMagicHeader, 16)!=0 ){
drh72f82862001-05-24 21:06:34 +00001666 goto page1_init_failed;
drh306dc212001-05-21 13:45:10 +00001667 }
drh309169a2007-04-24 17:27:51 +00001668 if( page1[18]>1 ){
1669 pBt->readOnly = 1;
1670 }
1671 if( page1[19]>1 ){
drhb6f41482004-05-14 01:58:11 +00001672 goto page1_init_failed;
1673 }
drh07d183d2005-05-01 22:52:42 +00001674 pageSize = get2byte(&page1[16]);
drh7dc385e2007-09-06 23:39:36 +00001675 if( ((pageSize-1)&pageSize)!=0 || pageSize<512 ||
1676 (SQLITE_MAX_PAGE_SIZE<32768 && pageSize>SQLITE_MAX_PAGE_SIZE)
1677 ){
drh07d183d2005-05-01 22:52:42 +00001678 goto page1_init_failed;
1679 }
1680 assert( (pageSize & 7)==0 );
danielk1977f653d782008-03-20 11:04:21 +00001681 usableSize = pageSize - page1[20];
1682 if( pageSize!=pBt->pageSize ){
1683 /* After reading the first page of the database assuming a page size
1684 ** of BtShared.pageSize, we have discovered that the page-size is
1685 ** actually pageSize. Unlock the database, leave pBt->pPage1 at
1686 ** zero and return SQLITE_OK. The caller will call this function
1687 ** again with the correct page-size.
1688 */
1689 releasePage(pPage1);
1690 pBt->usableSize = usableSize;
1691 pBt->pageSize = pageSize;
danielk197752ae7242008-03-25 14:24:56 +00001692 sqlite3_free(pBt->pTmpSpace);
1693 pBt->pTmpSpace = 0;
danielk1977f653d782008-03-20 11:04:21 +00001694 sqlite3PagerSetPagesize(pBt->pPager, &pBt->pageSize);
1695 return SQLITE_OK;
1696 }
1697 if( usableSize<500 ){
drhb6f41482004-05-14 01:58:11 +00001698 goto page1_init_failed;
1699 }
danielk1977f653d782008-03-20 11:04:21 +00001700 pBt->pageSize = pageSize;
1701 pBt->usableSize = usableSize;
drhb6f41482004-05-14 01:58:11 +00001702 pBt->maxEmbedFrac = page1[21];
1703 pBt->minEmbedFrac = page1[22];
1704 pBt->minLeafFrac = page1[23];
drh057cd3a2005-02-15 16:23:02 +00001705#ifndef SQLITE_OMIT_AUTOVACUUM
1706 pBt->autoVacuum = (get4byte(&page1[36 + 4*4])?1:0);
danielk197727b1f952007-06-25 08:16:58 +00001707 pBt->incrVacuum = (get4byte(&page1[36 + 7*4])?1:0);
drh057cd3a2005-02-15 16:23:02 +00001708#endif
drh306dc212001-05-21 13:45:10 +00001709 }
drhb6f41482004-05-14 01:58:11 +00001710
1711 /* maxLocal is the maximum amount of payload to store locally for
1712 ** a cell. Make sure it is small enough so that at least minFanout
1713 ** cells can will fit on one page. We assume a 10-byte page header.
1714 ** Besides the payload, the cell must store:
drh43605152004-05-29 21:46:49 +00001715 ** 2-byte pointer to the cell
drhb6f41482004-05-14 01:58:11 +00001716 ** 4-byte child pointer
1717 ** 9-byte nKey value
1718 ** 4-byte nData value
1719 ** 4-byte overflow page pointer
drh43605152004-05-29 21:46:49 +00001720 ** So a cell consists of a 2-byte poiner, a header which is as much as
1721 ** 17 bytes long, 0 to N bytes of payload, and an optional 4 byte overflow
1722 ** page pointer.
drhb6f41482004-05-14 01:58:11 +00001723 */
drh43605152004-05-29 21:46:49 +00001724 pBt->maxLocal = (pBt->usableSize-12)*pBt->maxEmbedFrac/255 - 23;
1725 pBt->minLocal = (pBt->usableSize-12)*pBt->minEmbedFrac/255 - 23;
1726 pBt->maxLeaf = pBt->usableSize - 35;
1727 pBt->minLeaf = (pBt->usableSize-12)*pBt->minLeafFrac/255 - 23;
drhb6f41482004-05-14 01:58:11 +00001728 if( pBt->minLocal>pBt->maxLocal || pBt->maxLocal<0 ){
1729 goto page1_init_failed;
1730 }
drh2e38c322004-09-03 18:38:44 +00001731 assert( pBt->maxLeaf + 23 <= MX_CELL_SIZE(pBt) );
drh3aac2dd2004-04-26 14:10:20 +00001732 pBt->pPage1 = pPage1;
drhb6f41482004-05-14 01:58:11 +00001733 return SQLITE_OK;
drh306dc212001-05-21 13:45:10 +00001734
drh72f82862001-05-24 21:06:34 +00001735page1_init_failed:
drh3aac2dd2004-04-26 14:10:20 +00001736 releasePage(pPage1);
1737 pBt->pPage1 = 0;
drh72f82862001-05-24 21:06:34 +00001738 return rc;
drh306dc212001-05-21 13:45:10 +00001739}
1740
1741/*
drhb8ef32c2005-03-14 02:01:49 +00001742** This routine works like lockBtree() except that it also invokes the
1743** busy callback if there is lock contention.
1744*/
danielk1977aef0bf62005-12-30 16:28:01 +00001745static int lockBtreeWithRetry(Btree *pRef){
drhb8ef32c2005-03-14 02:01:49 +00001746 int rc = SQLITE_OK;
drhd677b3d2007-08-20 22:48:41 +00001747
drh1fee73e2007-08-29 04:00:57 +00001748 assert( sqlite3BtreeHoldsMutex(pRef) );
danielk1977aef0bf62005-12-30 16:28:01 +00001749 if( pRef->inTrans==TRANS_NONE ){
1750 u8 inTransaction = pRef->pBt->inTransaction;
1751 btreeIntegrity(pRef);
1752 rc = sqlite3BtreeBeginTrans(pRef, 0);
1753 pRef->pBt->inTransaction = inTransaction;
1754 pRef->inTrans = TRANS_NONE;
1755 if( rc==SQLITE_OK ){
1756 pRef->pBt->nTransaction--;
1757 }
1758 btreeIntegrity(pRef);
drhb8ef32c2005-03-14 02:01:49 +00001759 }
1760 return rc;
1761}
1762
1763
1764/*
drhb8ca3072001-12-05 00:21:20 +00001765** If there are no outstanding cursors and we are not in the middle
1766** of a transaction but there is a read lock on the database, then
1767** this routine unrefs the first page of the database file which
1768** has the effect of releasing the read lock.
1769**
1770** If there are any outstanding cursors, this routine is a no-op.
1771**
1772** If there is a transaction in progress, this routine is a no-op.
1773*/
danielk1977aef0bf62005-12-30 16:28:01 +00001774static void unlockBtreeIfUnused(BtShared *pBt){
drh1fee73e2007-08-29 04:00:57 +00001775 assert( sqlite3_mutex_held(pBt->mutex) );
danielk1977aef0bf62005-12-30 16:28:01 +00001776 if( pBt->inTransaction==TRANS_NONE && pBt->pCursor==0 && pBt->pPage1!=0 ){
danielk19773b8a05f2007-03-19 17:44:26 +00001777 if( sqlite3PagerRefcount(pBt->pPager)>=1 ){
drhde4fcfd2008-01-19 23:50:26 +00001778 assert( pBt->pPage1->aData );
1779#if 0
drh24c9a2e2007-01-05 02:00:47 +00001780 if( pBt->pPage1->aData==0 ){
1781 MemPage *pPage = pBt->pPage1;
drhbf4bca52007-09-06 22:19:14 +00001782 pPage->aData = sqlite3PagerGetData(pPage->pDbPage);
drh24c9a2e2007-01-05 02:00:47 +00001783 pPage->pBt = pBt;
1784 pPage->pgno = 1;
1785 }
drhde4fcfd2008-01-19 23:50:26 +00001786#endif
drh24c9a2e2007-01-05 02:00:47 +00001787 releasePage(pBt->pPage1);
drh51c6d962004-06-06 00:42:25 +00001788 }
drh3aac2dd2004-04-26 14:10:20 +00001789 pBt->pPage1 = 0;
drh3aac2dd2004-04-26 14:10:20 +00001790 pBt->inStmt = 0;
drhb8ca3072001-12-05 00:21:20 +00001791 }
1792}
1793
1794/*
drh9e572e62004-04-23 23:43:10 +00001795** Create a new database by initializing the first page of the
drh8c42ca92001-06-22 19:15:00 +00001796** file.
drh8b2f49b2001-06-08 00:21:52 +00001797*/
danielk1977aef0bf62005-12-30 16:28:01 +00001798static int newDatabase(BtShared *pBt){
drh9e572e62004-04-23 23:43:10 +00001799 MemPage *pP1;
1800 unsigned char *data;
drh8c42ca92001-06-22 19:15:00 +00001801 int rc;
danielk1977ad0132d2008-06-07 08:58:22 +00001802 int nPage;
drhd677b3d2007-08-20 22:48:41 +00001803
drh1fee73e2007-08-29 04:00:57 +00001804 assert( sqlite3_mutex_held(pBt->mutex) );
danielk1977ad0132d2008-06-07 08:58:22 +00001805 rc = sqlite3PagerPagecount(pBt->pPager, &nPage);
1806 if( rc!=SQLITE_OK || nPage>0 ){
1807 return rc;
1808 }
drh3aac2dd2004-04-26 14:10:20 +00001809 pP1 = pBt->pPage1;
drh9e572e62004-04-23 23:43:10 +00001810 assert( pP1!=0 );
1811 data = pP1->aData;
danielk19773b8a05f2007-03-19 17:44:26 +00001812 rc = sqlite3PagerWrite(pP1->pDbPage);
drh8b2f49b2001-06-08 00:21:52 +00001813 if( rc ) return rc;
drh9e572e62004-04-23 23:43:10 +00001814 memcpy(data, zMagicHeader, sizeof(zMagicHeader));
1815 assert( sizeof(zMagicHeader)==16 );
drhb6f41482004-05-14 01:58:11 +00001816 put2byte(&data[16], pBt->pageSize);
drh9e572e62004-04-23 23:43:10 +00001817 data[18] = 1;
1818 data[19] = 1;
drhb6f41482004-05-14 01:58:11 +00001819 data[20] = pBt->pageSize - pBt->usableSize;
1820 data[21] = pBt->maxEmbedFrac;
1821 data[22] = pBt->minEmbedFrac;
1822 data[23] = pBt->minLeafFrac;
1823 memset(&data[24], 0, 100-24);
drhe6c43812004-05-14 12:17:46 +00001824 zeroPage(pP1, PTF_INTKEY|PTF_LEAF|PTF_LEAFDATA );
drhf2a611c2004-09-05 00:33:43 +00001825 pBt->pageSizeFixed = 1;
danielk1977003ba062004-11-04 02:57:33 +00001826#ifndef SQLITE_OMIT_AUTOVACUUM
danielk1977dddbcdc2007-04-26 14:42:34 +00001827 assert( pBt->autoVacuum==1 || pBt->autoVacuum==0 );
danielk1977418899a2007-06-24 10:14:00 +00001828 assert( pBt->incrVacuum==1 || pBt->incrVacuum==0 );
danielk1977dddbcdc2007-04-26 14:42:34 +00001829 put4byte(&data[36 + 4*4], pBt->autoVacuum);
danielk1977418899a2007-06-24 10:14:00 +00001830 put4byte(&data[36 + 7*4], pBt->incrVacuum);
danielk1977003ba062004-11-04 02:57:33 +00001831#endif
drh8b2f49b2001-06-08 00:21:52 +00001832 return SQLITE_OK;
1833}
1834
1835/*
danielk1977ee5741e2004-05-31 10:01:34 +00001836** Attempt to start a new transaction. A write-transaction
drh684917c2004-10-05 02:41:42 +00001837** is started if the second argument is nonzero, otherwise a read-
1838** transaction. If the second argument is 2 or more and exclusive
1839** transaction is started, meaning that no other process is allowed
1840** to access the database. A preexisting transaction may not be
drhb8ef32c2005-03-14 02:01:49 +00001841** upgraded to exclusive by calling this routine a second time - the
drh684917c2004-10-05 02:41:42 +00001842** exclusivity flag only works for a new transaction.
drh8b2f49b2001-06-08 00:21:52 +00001843**
danielk1977ee5741e2004-05-31 10:01:34 +00001844** A write-transaction must be started before attempting any
1845** changes to the database. None of the following routines
1846** will work unless a transaction is started first:
drh8b2f49b2001-06-08 00:21:52 +00001847**
drh23e11ca2004-05-04 17:27:28 +00001848** sqlite3BtreeCreateTable()
1849** sqlite3BtreeCreateIndex()
1850** sqlite3BtreeClearTable()
1851** sqlite3BtreeDropTable()
1852** sqlite3BtreeInsert()
1853** sqlite3BtreeDelete()
1854** sqlite3BtreeUpdateMeta()
danielk197713adf8a2004-06-03 16:08:41 +00001855**
drhb8ef32c2005-03-14 02:01:49 +00001856** If an initial attempt to acquire the lock fails because of lock contention
1857** and the database was previously unlocked, then invoke the busy handler
1858** if there is one. But if there was previously a read-lock, do not
1859** invoke the busy handler - just return SQLITE_BUSY. SQLITE_BUSY is
1860** returned when there is already a read-lock in order to avoid a deadlock.
1861**
1862** Suppose there are two processes A and B. A has a read lock and B has
1863** a reserved lock. B tries to promote to exclusive but is blocked because
1864** of A's read lock. A tries to promote to reserved but is blocked by B.
1865** One or the other of the two processes must give way or there can be
1866** no progress. By returning SQLITE_BUSY and not invoking the busy callback
1867** when A already has a read lock, we encourage A to give up and let B
1868** proceed.
drha059ad02001-04-17 20:09:11 +00001869*/
danielk1977aef0bf62005-12-30 16:28:01 +00001870int sqlite3BtreeBeginTrans(Btree *p, int wrflag){
1871 BtShared *pBt = p->pBt;
danielk1977ee5741e2004-05-31 10:01:34 +00001872 int rc = SQLITE_OK;
1873
drhd677b3d2007-08-20 22:48:41 +00001874 sqlite3BtreeEnter(p);
drhe5fe6902007-12-07 18:55:28 +00001875 pBt->db = p->db;
danielk1977aef0bf62005-12-30 16:28:01 +00001876 btreeIntegrity(p);
1877
danielk1977ee5741e2004-05-31 10:01:34 +00001878 /* If the btree is already in a write-transaction, or it
1879 ** is already in a read-transaction and a read-transaction
1880 ** is requested, this is a no-op.
1881 */
danielk1977aef0bf62005-12-30 16:28:01 +00001882 if( p->inTrans==TRANS_WRITE || (p->inTrans==TRANS_READ && !wrflag) ){
drhd677b3d2007-08-20 22:48:41 +00001883 goto trans_begun;
danielk1977ee5741e2004-05-31 10:01:34 +00001884 }
drhb8ef32c2005-03-14 02:01:49 +00001885
1886 /* Write transactions are not possible on a read-only database */
danielk1977ee5741e2004-05-31 10:01:34 +00001887 if( pBt->readOnly && wrflag ){
drhd677b3d2007-08-20 22:48:41 +00001888 rc = SQLITE_READONLY;
1889 goto trans_begun;
danielk1977ee5741e2004-05-31 10:01:34 +00001890 }
1891
danielk1977aef0bf62005-12-30 16:28:01 +00001892 /* If another database handle has already opened a write transaction
1893 ** on this shared-btree structure and a second write transaction is
1894 ** requested, return SQLITE_BUSY.
1895 */
1896 if( pBt->inTransaction==TRANS_WRITE && wrflag ){
drhd677b3d2007-08-20 22:48:41 +00001897 rc = SQLITE_BUSY;
1898 goto trans_begun;
danielk1977aef0bf62005-12-30 16:28:01 +00001899 }
1900
danielk1977641b0f42007-12-21 04:47:25 +00001901#ifndef SQLITE_OMIT_SHARED_CACHE
1902 if( wrflag>1 ){
1903 BtLock *pIter;
1904 for(pIter=pBt->pLock; pIter; pIter=pIter->pNext){
1905 if( pIter->pBtree!=p ){
1906 rc = SQLITE_BUSY;
1907 goto trans_begun;
1908 }
1909 }
1910 }
1911#endif
1912
drhb8ef32c2005-03-14 02:01:49 +00001913 do {
drh8a9c17f2008-05-02 14:23:54 +00001914 if( pBt->pPage1==0 ){
1915 do{
1916 rc = lockBtree(pBt);
1917 }while( pBt->pPage1==0 && rc==SQLITE_OK );
drh8c42ca92001-06-22 19:15:00 +00001918 }
drh309169a2007-04-24 17:27:51 +00001919
drhb8ef32c2005-03-14 02:01:49 +00001920 if( rc==SQLITE_OK && wrflag ){
drh309169a2007-04-24 17:27:51 +00001921 if( pBt->readOnly ){
1922 rc = SQLITE_READONLY;
1923 }else{
1924 rc = sqlite3PagerBegin(pBt->pPage1->pDbPage, wrflag>1);
1925 if( rc==SQLITE_OK ){
1926 rc = newDatabase(pBt);
1927 }
drhb8ef32c2005-03-14 02:01:49 +00001928 }
1929 }
1930
1931 if( rc==SQLITE_OK ){
drhb8ef32c2005-03-14 02:01:49 +00001932 if( wrflag ) pBt->inStmt = 0;
1933 }else{
1934 unlockBtreeIfUnused(pBt);
1935 }
danielk1977aef0bf62005-12-30 16:28:01 +00001936 }while( rc==SQLITE_BUSY && pBt->inTransaction==TRANS_NONE &&
drhe5fe6902007-12-07 18:55:28 +00001937 sqlite3BtreeInvokeBusyHandler(pBt, 0) );
danielk1977aef0bf62005-12-30 16:28:01 +00001938
1939 if( rc==SQLITE_OK ){
1940 if( p->inTrans==TRANS_NONE ){
1941 pBt->nTransaction++;
1942 }
1943 p->inTrans = (wrflag?TRANS_WRITE:TRANS_READ);
1944 if( p->inTrans>pBt->inTransaction ){
1945 pBt->inTransaction = p->inTrans;
1946 }
danielk1977641b0f42007-12-21 04:47:25 +00001947#ifndef SQLITE_OMIT_SHARED_CACHE
1948 if( wrflag>1 ){
1949 assert( !pBt->pExclusive );
1950 pBt->pExclusive = p;
1951 }
1952#endif
danielk1977aef0bf62005-12-30 16:28:01 +00001953 }
1954
drhd677b3d2007-08-20 22:48:41 +00001955
1956trans_begun:
danielk1977aef0bf62005-12-30 16:28:01 +00001957 btreeIntegrity(p);
drhd677b3d2007-08-20 22:48:41 +00001958 sqlite3BtreeLeave(p);
drhb8ca3072001-12-05 00:21:20 +00001959 return rc;
drha059ad02001-04-17 20:09:11 +00001960}
1961
danielk1977687566d2004-11-02 12:56:41 +00001962#ifndef SQLITE_OMIT_AUTOVACUUM
1963
1964/*
1965** Set the pointer-map entries for all children of page pPage. Also, if
1966** pPage contains cells that point to overflow pages, set the pointer
1967** map entries for the overflow pages as well.
1968*/
1969static int setChildPtrmaps(MemPage *pPage){
1970 int i; /* Counter variable */
1971 int nCell; /* Number of cells in page pPage */
danielk19772df71c72007-05-24 07:22:42 +00001972 int rc; /* Return code */
danielk1977aef0bf62005-12-30 16:28:01 +00001973 BtShared *pBt = pPage->pBt;
danielk1977687566d2004-11-02 12:56:41 +00001974 int isInitOrig = pPage->isInit;
1975 Pgno pgno = pPage->pgno;
1976
drh1fee73e2007-08-29 04:00:57 +00001977 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
danielk19772df71c72007-05-24 07:22:42 +00001978 rc = sqlite3BtreeInitPage(pPage, pPage->pParent);
1979 if( rc!=SQLITE_OK ){
1980 goto set_child_ptrmaps_out;
1981 }
danielk1977687566d2004-11-02 12:56:41 +00001982 nCell = pPage->nCell;
1983
1984 for(i=0; i<nCell; i++){
danielk19771cc5ed82007-05-16 17:28:43 +00001985 u8 *pCell = findCell(pPage, i);
danielk1977687566d2004-11-02 12:56:41 +00001986
danielk197726836652005-01-17 01:33:13 +00001987 rc = ptrmapPutOvflPtr(pPage, pCell);
1988 if( rc!=SQLITE_OK ){
1989 goto set_child_ptrmaps_out;
danielk1977687566d2004-11-02 12:56:41 +00001990 }
danielk197726836652005-01-17 01:33:13 +00001991
danielk1977687566d2004-11-02 12:56:41 +00001992 if( !pPage->leaf ){
1993 Pgno childPgno = get4byte(pCell);
1994 rc = ptrmapPut(pBt, childPgno, PTRMAP_BTREE, pgno);
1995 if( rc!=SQLITE_OK ) goto set_child_ptrmaps_out;
1996 }
1997 }
1998
1999 if( !pPage->leaf ){
2000 Pgno childPgno = get4byte(&pPage->aData[pPage->hdrOffset+8]);
2001 rc = ptrmapPut(pBt, childPgno, PTRMAP_BTREE, pgno);
2002 }
2003
2004set_child_ptrmaps_out:
2005 pPage->isInit = isInitOrig;
2006 return rc;
2007}
2008
2009/*
2010** Somewhere on pPage, which is guarenteed to be a btree page, not an overflow
2011** page, is a pointer to page iFrom. Modify this pointer so that it points to
2012** iTo. Parameter eType describes the type of pointer to be modified, as
2013** follows:
2014**
2015** PTRMAP_BTREE: pPage is a btree-page. The pointer points at a child
2016** page of pPage.
2017**
2018** PTRMAP_OVERFLOW1: pPage is a btree-page. The pointer points at an overflow
2019** page pointed to by one of the cells on pPage.
2020**
2021** PTRMAP_OVERFLOW2: pPage is an overflow-page. The pointer points at the next
2022** overflow page in the list.
2023*/
danielk1977fdb7cdb2005-01-17 02:12:18 +00002024static int modifyPagePointer(MemPage *pPage, Pgno iFrom, Pgno iTo, u8 eType){
drh1fee73e2007-08-29 04:00:57 +00002025 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
danielk1977687566d2004-11-02 12:56:41 +00002026 if( eType==PTRMAP_OVERFLOW2 ){
danielk1977f78fc082004-11-02 14:40:32 +00002027 /* The pointer is always the first 4 bytes of the page in this case. */
danielk1977fdb7cdb2005-01-17 02:12:18 +00002028 if( get4byte(pPage->aData)!=iFrom ){
drh49285702005-09-17 15:20:26 +00002029 return SQLITE_CORRUPT_BKPT;
danielk1977fdb7cdb2005-01-17 02:12:18 +00002030 }
danielk1977f78fc082004-11-02 14:40:32 +00002031 put4byte(pPage->aData, iTo);
danielk1977687566d2004-11-02 12:56:41 +00002032 }else{
2033 int isInitOrig = pPage->isInit;
2034 int i;
2035 int nCell;
2036
drh16a9b832007-05-05 18:39:25 +00002037 sqlite3BtreeInitPage(pPage, 0);
danielk1977687566d2004-11-02 12:56:41 +00002038 nCell = pPage->nCell;
2039
danielk1977687566d2004-11-02 12:56:41 +00002040 for(i=0; i<nCell; i++){
danielk19771cc5ed82007-05-16 17:28:43 +00002041 u8 *pCell = findCell(pPage, i);
danielk1977687566d2004-11-02 12:56:41 +00002042 if( eType==PTRMAP_OVERFLOW1 ){
2043 CellInfo info;
drh16a9b832007-05-05 18:39:25 +00002044 sqlite3BtreeParseCellPtr(pPage, pCell, &info);
danielk1977687566d2004-11-02 12:56:41 +00002045 if( info.iOverflow ){
2046 if( iFrom==get4byte(&pCell[info.iOverflow]) ){
2047 put4byte(&pCell[info.iOverflow], iTo);
2048 break;
2049 }
2050 }
2051 }else{
2052 if( get4byte(pCell)==iFrom ){
2053 put4byte(pCell, iTo);
2054 break;
2055 }
2056 }
2057 }
2058
2059 if( i==nCell ){
danielk1977fdb7cdb2005-01-17 02:12:18 +00002060 if( eType!=PTRMAP_BTREE ||
2061 get4byte(&pPage->aData[pPage->hdrOffset+8])!=iFrom ){
drh49285702005-09-17 15:20:26 +00002062 return SQLITE_CORRUPT_BKPT;
danielk1977fdb7cdb2005-01-17 02:12:18 +00002063 }
danielk1977687566d2004-11-02 12:56:41 +00002064 put4byte(&pPage->aData[pPage->hdrOffset+8], iTo);
2065 }
2066
2067 pPage->isInit = isInitOrig;
2068 }
danielk1977fdb7cdb2005-01-17 02:12:18 +00002069 return SQLITE_OK;
danielk1977687566d2004-11-02 12:56:41 +00002070}
2071
danielk1977003ba062004-11-04 02:57:33 +00002072
danielk19777701e812005-01-10 12:59:51 +00002073/*
2074** Move the open database page pDbPage to location iFreePage in the
2075** database. The pDbPage reference remains valid.
2076*/
danielk1977003ba062004-11-04 02:57:33 +00002077static int relocatePage(
danielk1977aef0bf62005-12-30 16:28:01 +00002078 BtShared *pBt, /* Btree */
danielk19777701e812005-01-10 12:59:51 +00002079 MemPage *pDbPage, /* Open page to move */
2080 u8 eType, /* Pointer map 'type' entry for pDbPage */
2081 Pgno iPtrPage, /* Pointer map 'page-no' entry for pDbPage */
2082 Pgno iFreePage /* The location to move pDbPage to */
danielk1977003ba062004-11-04 02:57:33 +00002083){
2084 MemPage *pPtrPage; /* The page that contains a pointer to pDbPage */
2085 Pgno iDbPage = pDbPage->pgno;
2086 Pager *pPager = pBt->pPager;
2087 int rc;
2088
danielk1977a0bf2652004-11-04 14:30:04 +00002089 assert( eType==PTRMAP_OVERFLOW2 || eType==PTRMAP_OVERFLOW1 ||
2090 eType==PTRMAP_BTREE || eType==PTRMAP_ROOTPAGE );
drh1fee73e2007-08-29 04:00:57 +00002091 assert( sqlite3_mutex_held(pBt->mutex) );
drhd0679ed2007-08-28 22:24:34 +00002092 assert( pDbPage->pBt==pBt );
danielk1977003ba062004-11-04 02:57:33 +00002093
drh85b623f2007-12-13 21:54:09 +00002094 /* Move page iDbPage from its current location to page number iFreePage */
danielk1977003ba062004-11-04 02:57:33 +00002095 TRACE(("AUTOVACUUM: Moving %d to free page %d (ptr page %d type %d)\n",
2096 iDbPage, iFreePage, iPtrPage, eType));
danielk19773b8a05f2007-03-19 17:44:26 +00002097 rc = sqlite3PagerMovepage(pPager, pDbPage->pDbPage, iFreePage);
danielk1977003ba062004-11-04 02:57:33 +00002098 if( rc!=SQLITE_OK ){
2099 return rc;
2100 }
2101 pDbPage->pgno = iFreePage;
2102
2103 /* If pDbPage was a btree-page, then it may have child pages and/or cells
2104 ** that point to overflow pages. The pointer map entries for all these
2105 ** pages need to be changed.
2106 **
2107 ** If pDbPage is an overflow page, then the first 4 bytes may store a
2108 ** pointer to a subsequent overflow page. If this is the case, then
2109 ** the pointer map needs to be updated for the subsequent overflow page.
2110 */
danielk1977a0bf2652004-11-04 14:30:04 +00002111 if( eType==PTRMAP_BTREE || eType==PTRMAP_ROOTPAGE ){
danielk1977003ba062004-11-04 02:57:33 +00002112 rc = setChildPtrmaps(pDbPage);
2113 if( rc!=SQLITE_OK ){
2114 return rc;
2115 }
2116 }else{
2117 Pgno nextOvfl = get4byte(pDbPage->aData);
2118 if( nextOvfl!=0 ){
danielk1977003ba062004-11-04 02:57:33 +00002119 rc = ptrmapPut(pBt, nextOvfl, PTRMAP_OVERFLOW2, iFreePage);
2120 if( rc!=SQLITE_OK ){
2121 return rc;
2122 }
2123 }
2124 }
2125
2126 /* Fix the database pointer on page iPtrPage that pointed at iDbPage so
2127 ** that it points at iFreePage. Also fix the pointer map entry for
2128 ** iPtrPage.
2129 */
danielk1977a0bf2652004-11-04 14:30:04 +00002130 if( eType!=PTRMAP_ROOTPAGE ){
drh16a9b832007-05-05 18:39:25 +00002131 rc = sqlite3BtreeGetPage(pBt, iPtrPage, &pPtrPage, 0);
danielk1977a0bf2652004-11-04 14:30:04 +00002132 if( rc!=SQLITE_OK ){
2133 return rc;
2134 }
danielk19773b8a05f2007-03-19 17:44:26 +00002135 rc = sqlite3PagerWrite(pPtrPage->pDbPage);
danielk1977a0bf2652004-11-04 14:30:04 +00002136 if( rc!=SQLITE_OK ){
2137 releasePage(pPtrPage);
2138 return rc;
2139 }
danielk1977fdb7cdb2005-01-17 02:12:18 +00002140 rc = modifyPagePointer(pPtrPage, iDbPage, iFreePage, eType);
danielk1977003ba062004-11-04 02:57:33 +00002141 releasePage(pPtrPage);
danielk1977fdb7cdb2005-01-17 02:12:18 +00002142 if( rc==SQLITE_OK ){
2143 rc = ptrmapPut(pBt, iFreePage, eType, iPtrPage);
2144 }
danielk1977003ba062004-11-04 02:57:33 +00002145 }
danielk1977003ba062004-11-04 02:57:33 +00002146 return rc;
2147}
2148
danielk1977ad0132d2008-06-07 08:58:22 +00002149static int pagerPagecount(Pager *pPager){
2150 int rc;
2151 int nPage;
2152 rc = sqlite3PagerPagecount(pPager, &nPage);
2153 return (rc==SQLITE_OK?nPage:-1);
2154}
2155
danielk1977dddbcdc2007-04-26 14:42:34 +00002156/* Forward declaration required by incrVacuumStep(). */
drh4f0c5872007-03-26 22:05:01 +00002157static int allocateBtreePage(BtShared *, MemPage **, Pgno *, Pgno, u8);
danielk1977687566d2004-11-02 12:56:41 +00002158
2159/*
danielk1977dddbcdc2007-04-26 14:42:34 +00002160** Perform a single step of an incremental-vacuum. If successful,
2161** return SQLITE_OK. If there is no work to do (and therefore no
2162** point in calling this function again), return SQLITE_DONE.
2163**
2164** More specificly, this function attempts to re-organize the
2165** database so that the last page of the file currently in use
2166** is no longer in use.
2167**
2168** If the nFin parameter is non-zero, the implementation assumes
2169** that the caller will keep calling incrVacuumStep() until
2170** it returns SQLITE_DONE or an error, and that nFin is the
2171** number of pages the database file will contain after this
2172** process is complete.
2173*/
2174static int incrVacuumStep(BtShared *pBt, Pgno nFin){
2175 Pgno iLastPg; /* Last page in the database */
2176 Pgno nFreeList; /* Number of pages still on the free-list */
2177
drh1fee73e2007-08-29 04:00:57 +00002178 assert( sqlite3_mutex_held(pBt->mutex) );
danielk1977dddbcdc2007-04-26 14:42:34 +00002179 iLastPg = pBt->nTrunc;
2180 if( iLastPg==0 ){
danielk1977ad0132d2008-06-07 08:58:22 +00002181 iLastPg = pagerPagecount(pBt->pPager);
danielk1977dddbcdc2007-04-26 14:42:34 +00002182 }
2183
2184 if( !PTRMAP_ISPAGE(pBt, iLastPg) && iLastPg!=PENDING_BYTE_PAGE(pBt) ){
2185 int rc;
2186 u8 eType;
2187 Pgno iPtrPage;
2188
2189 nFreeList = get4byte(&pBt->pPage1->aData[36]);
2190 if( nFreeList==0 || nFin==iLastPg ){
2191 return SQLITE_DONE;
2192 }
2193
2194 rc = ptrmapGet(pBt, iLastPg, &eType, &iPtrPage);
2195 if( rc!=SQLITE_OK ){
2196 return rc;
2197 }
2198 if( eType==PTRMAP_ROOTPAGE ){
2199 return SQLITE_CORRUPT_BKPT;
2200 }
2201
2202 if( eType==PTRMAP_FREEPAGE ){
2203 if( nFin==0 ){
2204 /* Remove the page from the files free-list. This is not required
danielk19774ef24492007-05-23 09:52:41 +00002205 ** if nFin is non-zero. In that case, the free-list will be
danielk1977dddbcdc2007-04-26 14:42:34 +00002206 ** truncated to zero after this function returns, so it doesn't
2207 ** matter if it still contains some garbage entries.
2208 */
2209 Pgno iFreePg;
2210 MemPage *pFreePg;
2211 rc = allocateBtreePage(pBt, &pFreePg, &iFreePg, iLastPg, 1);
2212 if( rc!=SQLITE_OK ){
2213 return rc;
2214 }
2215 assert( iFreePg==iLastPg );
2216 releasePage(pFreePg);
2217 }
2218 } else {
2219 Pgno iFreePg; /* Index of free page to move pLastPg to */
2220 MemPage *pLastPg;
2221
drh16a9b832007-05-05 18:39:25 +00002222 rc = sqlite3BtreeGetPage(pBt, iLastPg, &pLastPg, 0);
danielk1977dddbcdc2007-04-26 14:42:34 +00002223 if( rc!=SQLITE_OK ){
2224 return rc;
2225 }
2226
danielk1977b4626a32007-04-28 15:47:43 +00002227 /* If nFin is zero, this loop runs exactly once and page pLastPg
2228 ** is swapped with the first free page pulled off the free list.
2229 **
2230 ** On the other hand, if nFin is greater than zero, then keep
2231 ** looping until a free-page located within the first nFin pages
2232 ** of the file is found.
2233 */
danielk1977dddbcdc2007-04-26 14:42:34 +00002234 do {
2235 MemPage *pFreePg;
2236 rc = allocateBtreePage(pBt, &pFreePg, &iFreePg, 0, 0);
2237 if( rc!=SQLITE_OK ){
2238 releasePage(pLastPg);
2239 return rc;
2240 }
2241 releasePage(pFreePg);
2242 }while( nFin!=0 && iFreePg>nFin );
2243 assert( iFreePg<iLastPg );
danielk1977b4626a32007-04-28 15:47:43 +00002244
2245 rc = sqlite3PagerWrite(pLastPg->pDbPage);
danielk1977662278e2007-11-05 15:30:12 +00002246 if( rc==SQLITE_OK ){
2247 rc = relocatePage(pBt, pLastPg, eType, iPtrPage, iFreePg);
2248 }
danielk1977dddbcdc2007-04-26 14:42:34 +00002249 releasePage(pLastPg);
2250 if( rc!=SQLITE_OK ){
2251 return rc;
danielk1977662278e2007-11-05 15:30:12 +00002252 }
danielk1977dddbcdc2007-04-26 14:42:34 +00002253 }
2254 }
2255
2256 pBt->nTrunc = iLastPg - 1;
2257 while( pBt->nTrunc==PENDING_BYTE_PAGE(pBt)||PTRMAP_ISPAGE(pBt, pBt->nTrunc) ){
2258 pBt->nTrunc--;
2259 }
2260 return SQLITE_OK;
2261}
2262
2263/*
2264** A write-transaction must be opened before calling this function.
2265** It performs a single unit of work towards an incremental vacuum.
2266**
2267** If the incremental vacuum is finished after this function has run,
2268** SQLITE_DONE is returned. If it is not finished, but no error occured,
2269** SQLITE_OK is returned. Otherwise an SQLite error code.
2270*/
2271int sqlite3BtreeIncrVacuum(Btree *p){
drhd677b3d2007-08-20 22:48:41 +00002272 int rc;
danielk1977dddbcdc2007-04-26 14:42:34 +00002273 BtShared *pBt = p->pBt;
drhd677b3d2007-08-20 22:48:41 +00002274
2275 sqlite3BtreeEnter(p);
drhe5fe6902007-12-07 18:55:28 +00002276 pBt->db = p->db;
danielk1977dddbcdc2007-04-26 14:42:34 +00002277 assert( pBt->inTransaction==TRANS_WRITE && p->inTrans==TRANS_WRITE );
2278 if( !pBt->autoVacuum ){
drhd677b3d2007-08-20 22:48:41 +00002279 rc = SQLITE_DONE;
2280 }else{
2281 invalidateAllOverflowCache(pBt);
2282 rc = incrVacuumStep(pBt, 0);
danielk1977dddbcdc2007-04-26 14:42:34 +00002283 }
drhd677b3d2007-08-20 22:48:41 +00002284 sqlite3BtreeLeave(p);
2285 return rc;
danielk1977dddbcdc2007-04-26 14:42:34 +00002286}
2287
2288/*
danielk19773b8a05f2007-03-19 17:44:26 +00002289** This routine is called prior to sqlite3PagerCommit when a transaction
danielk1977687566d2004-11-02 12:56:41 +00002290** is commited for an auto-vacuum database.
danielk197724168722007-04-02 05:07:47 +00002291**
2292** If SQLITE_OK is returned, then *pnTrunc is set to the number of pages
2293** the database file should be truncated to during the commit process.
2294** i.e. the database has been reorganized so that only the first *pnTrunc
2295** pages are in use.
danielk1977687566d2004-11-02 12:56:41 +00002296*/
danielk197724168722007-04-02 05:07:47 +00002297static int autoVacuumCommit(BtShared *pBt, Pgno *pnTrunc){
danielk1977dddbcdc2007-04-26 14:42:34 +00002298 int rc = SQLITE_OK;
danielk1977687566d2004-11-02 12:56:41 +00002299 Pager *pPager = pBt->pPager;
danielk1977687566d2004-11-02 12:56:41 +00002300#ifndef NDEBUG
danielk19773b8a05f2007-03-19 17:44:26 +00002301 int nRef = sqlite3PagerRefcount(pPager);
danielk1977687566d2004-11-02 12:56:41 +00002302#endif
2303
drh1fee73e2007-08-29 04:00:57 +00002304 assert( sqlite3_mutex_held(pBt->mutex) );
danielk197792d4d7a2007-05-04 12:05:56 +00002305 invalidateAllOverflowCache(pBt);
danielk1977dddbcdc2007-04-26 14:42:34 +00002306 assert(pBt->autoVacuum);
2307 if( !pBt->incrVacuum ){
2308 Pgno nFin = 0;
danielk1977687566d2004-11-02 12:56:41 +00002309
danielk1977dddbcdc2007-04-26 14:42:34 +00002310 if( pBt->nTrunc==0 ){
2311 Pgno nFree;
2312 Pgno nPtrmap;
2313 const int pgsz = pBt->pageSize;
danielk1977ad0132d2008-06-07 08:58:22 +00002314 int nOrig = pagerPagecount(pBt->pPager);
danielk1977e5321f02007-04-27 07:05:44 +00002315
2316 if( PTRMAP_ISPAGE(pBt, nOrig) ){
2317 return SQLITE_CORRUPT_BKPT;
2318 }
danielk1977dddbcdc2007-04-26 14:42:34 +00002319 if( nOrig==PENDING_BYTE_PAGE(pBt) ){
2320 nOrig--;
danielk1977687566d2004-11-02 12:56:41 +00002321 }
danielk1977dddbcdc2007-04-26 14:42:34 +00002322 nFree = get4byte(&pBt->pPage1->aData[36]);
2323 nPtrmap = (nFree-nOrig+PTRMAP_PAGENO(pBt, nOrig)+pgsz/5)/(pgsz/5);
2324 nFin = nOrig - nFree - nPtrmap;
2325 if( nOrig>PENDING_BYTE_PAGE(pBt) && nFin<=PENDING_BYTE_PAGE(pBt) ){
2326 nFin--;
danielk1977ac11ee62005-01-15 12:45:51 +00002327 }
danielk1977dddbcdc2007-04-26 14:42:34 +00002328 while( PTRMAP_ISPAGE(pBt, nFin) || nFin==PENDING_BYTE_PAGE(pBt) ){
2329 nFin--;
2330 }
2331 }
danielk1977687566d2004-11-02 12:56:41 +00002332
danielk1977dddbcdc2007-04-26 14:42:34 +00002333 while( rc==SQLITE_OK ){
2334 rc = incrVacuumStep(pBt, nFin);
2335 }
2336 if( rc==SQLITE_DONE ){
2337 assert(nFin==0 || pBt->nTrunc==0 || nFin<=pBt->nTrunc);
2338 rc = SQLITE_OK;
danielk19770ba32df2008-05-07 07:13:16 +00002339 if( pBt->nTrunc && nFin ){
drh67f80b62007-07-23 19:26:17 +00002340 rc = sqlite3PagerWrite(pBt->pPage1->pDbPage);
danielk1977dddbcdc2007-04-26 14:42:34 +00002341 put4byte(&pBt->pPage1->aData[32], 0);
2342 put4byte(&pBt->pPage1->aData[36], 0);
2343 pBt->nTrunc = nFin;
2344 }
2345 }
2346 if( rc!=SQLITE_OK ){
2347 sqlite3PagerRollback(pPager);
2348 }
danielk1977687566d2004-11-02 12:56:41 +00002349 }
2350
danielk1977dddbcdc2007-04-26 14:42:34 +00002351 if( rc==SQLITE_OK ){
2352 *pnTrunc = pBt->nTrunc;
2353 pBt->nTrunc = 0;
2354 }
danielk19773b8a05f2007-03-19 17:44:26 +00002355 assert( nRef==sqlite3PagerRefcount(pPager) );
danielk1977687566d2004-11-02 12:56:41 +00002356 return rc;
2357}
danielk1977dddbcdc2007-04-26 14:42:34 +00002358
danielk1977687566d2004-11-02 12:56:41 +00002359#endif
2360
2361/*
drh80e35f42007-03-30 14:06:34 +00002362** This routine does the first phase of a two-phase commit. This routine
2363** causes a rollback journal to be created (if it does not already exist)
2364** and populated with enough information so that if a power loss occurs
2365** the database can be restored to its original state by playing back
2366** the journal. Then the contents of the journal are flushed out to
2367** the disk. After the journal is safely on oxide, the changes to the
2368** database are written into the database file and flushed to oxide.
2369** At the end of this call, the rollback journal still exists on the
2370** disk and we are still holding all locks, so the transaction has not
2371** committed. See sqlite3BtreeCommit() for the second phase of the
2372** commit process.
2373**
2374** This call is a no-op if no write-transaction is currently active on pBt.
2375**
2376** Otherwise, sync the database file for the btree pBt. zMaster points to
2377** the name of a master journal file that should be written into the
2378** individual journal file, or is NULL, indicating no master journal file
2379** (single database transaction).
2380**
2381** When this is called, the master journal should already have been
2382** created, populated with this journal pointer and synced to disk.
2383**
2384** Once this is routine has returned, the only thing required to commit
2385** the write-transaction for this database file is to delete the journal.
2386*/
2387int sqlite3BtreeCommitPhaseOne(Btree *p, const char *zMaster){
2388 int rc = SQLITE_OK;
2389 if( p->inTrans==TRANS_WRITE ){
2390 BtShared *pBt = p->pBt;
2391 Pgno nTrunc = 0;
drhd677b3d2007-08-20 22:48:41 +00002392 sqlite3BtreeEnter(p);
drhe5fe6902007-12-07 18:55:28 +00002393 pBt->db = p->db;
drh80e35f42007-03-30 14:06:34 +00002394#ifndef SQLITE_OMIT_AUTOVACUUM
2395 if( pBt->autoVacuum ){
2396 rc = autoVacuumCommit(pBt, &nTrunc);
2397 if( rc!=SQLITE_OK ){
drhd677b3d2007-08-20 22:48:41 +00002398 sqlite3BtreeLeave(p);
drh80e35f42007-03-30 14:06:34 +00002399 return rc;
2400 }
2401 }
2402#endif
danielk1977f653d782008-03-20 11:04:21 +00002403 rc = sqlite3PagerCommitPhaseOne(pBt->pPager, zMaster, nTrunc, 0);
drhd677b3d2007-08-20 22:48:41 +00002404 sqlite3BtreeLeave(p);
drh80e35f42007-03-30 14:06:34 +00002405 }
2406 return rc;
2407}
2408
2409/*
drh2aa679f2001-06-25 02:11:07 +00002410** Commit the transaction currently in progress.
drh5e00f6c2001-09-13 13:46:56 +00002411**
drh6e345992007-03-30 11:12:08 +00002412** This routine implements the second phase of a 2-phase commit. The
2413** sqlite3BtreeSync() routine does the first phase and should be invoked
2414** prior to calling this routine. The sqlite3BtreeSync() routine did
2415** all the work of writing information out to disk and flushing the
2416** contents so that they are written onto the disk platter. All this
2417** routine has to do is delete or truncate the rollback journal
2418** (which causes the transaction to commit) and drop locks.
2419**
drh5e00f6c2001-09-13 13:46:56 +00002420** This will release the write lock on the database file. If there
2421** are no active cursors, it also releases the read lock.
drha059ad02001-04-17 20:09:11 +00002422*/
drh80e35f42007-03-30 14:06:34 +00002423int sqlite3BtreeCommitPhaseTwo(Btree *p){
danielk1977aef0bf62005-12-30 16:28:01 +00002424 BtShared *pBt = p->pBt;
2425
drhd677b3d2007-08-20 22:48:41 +00002426 sqlite3BtreeEnter(p);
drhe5fe6902007-12-07 18:55:28 +00002427 pBt->db = p->db;
danielk1977aef0bf62005-12-30 16:28:01 +00002428 btreeIntegrity(p);
danielk1977aef0bf62005-12-30 16:28:01 +00002429
2430 /* If the handle has a write-transaction open, commit the shared-btrees
2431 ** transaction and set the shared state to TRANS_READ.
2432 */
2433 if( p->inTrans==TRANS_WRITE ){
danielk19777f7bc662006-01-23 13:47:47 +00002434 int rc;
danielk1977aef0bf62005-12-30 16:28:01 +00002435 assert( pBt->inTransaction==TRANS_WRITE );
2436 assert( pBt->nTransaction>0 );
drh80e35f42007-03-30 14:06:34 +00002437 rc = sqlite3PagerCommitPhaseTwo(pBt->pPager);
danielk19777f7bc662006-01-23 13:47:47 +00002438 if( rc!=SQLITE_OK ){
drhd677b3d2007-08-20 22:48:41 +00002439 sqlite3BtreeLeave(p);
danielk19777f7bc662006-01-23 13:47:47 +00002440 return rc;
2441 }
danielk1977aef0bf62005-12-30 16:28:01 +00002442 pBt->inTransaction = TRANS_READ;
2443 pBt->inStmt = 0;
danielk1977ee5741e2004-05-31 10:01:34 +00002444 }
danielk19777f7bc662006-01-23 13:47:47 +00002445 unlockAllTables(p);
danielk1977aef0bf62005-12-30 16:28:01 +00002446
2447 /* If the handle has any kind of transaction open, decrement the transaction
2448 ** count of the shared btree. If the transaction count reaches 0, set
2449 ** the shared state to TRANS_NONE. The unlockBtreeIfUnused() call below
2450 ** will unlock the pager.
2451 */
2452 if( p->inTrans!=TRANS_NONE ){
2453 pBt->nTransaction--;
2454 if( 0==pBt->nTransaction ){
2455 pBt->inTransaction = TRANS_NONE;
2456 }
2457 }
2458
2459 /* Set the handles current transaction state to TRANS_NONE and unlock
2460 ** the pager if this call closed the only read or write transaction.
2461 */
2462 p->inTrans = TRANS_NONE;
drh5e00f6c2001-09-13 13:46:56 +00002463 unlockBtreeIfUnused(pBt);
danielk1977aef0bf62005-12-30 16:28:01 +00002464
2465 btreeIntegrity(p);
drhd677b3d2007-08-20 22:48:41 +00002466 sqlite3BtreeLeave(p);
danielk19777f7bc662006-01-23 13:47:47 +00002467 return SQLITE_OK;
drha059ad02001-04-17 20:09:11 +00002468}
2469
drh80e35f42007-03-30 14:06:34 +00002470/*
2471** Do both phases of a commit.
2472*/
2473int sqlite3BtreeCommit(Btree *p){
2474 int rc;
drhd677b3d2007-08-20 22:48:41 +00002475 sqlite3BtreeEnter(p);
drh80e35f42007-03-30 14:06:34 +00002476 rc = sqlite3BtreeCommitPhaseOne(p, 0);
2477 if( rc==SQLITE_OK ){
2478 rc = sqlite3BtreeCommitPhaseTwo(p);
2479 }
drhd677b3d2007-08-20 22:48:41 +00002480 sqlite3BtreeLeave(p);
drh80e35f42007-03-30 14:06:34 +00002481 return rc;
2482}
2483
danielk1977fbcd5852004-06-15 02:44:18 +00002484#ifndef NDEBUG
2485/*
2486** Return the number of write-cursors open on this handle. This is for use
2487** in assert() expressions, so it is only compiled if NDEBUG is not
2488** defined.
drhfb982642007-08-30 01:19:59 +00002489**
2490** For the purposes of this routine, a write-cursor is any cursor that
2491** is capable of writing to the databse. That means the cursor was
2492** originally opened for writing and the cursor has not be disabled
2493** by having its state changed to CURSOR_FAULT.
danielk1977fbcd5852004-06-15 02:44:18 +00002494*/
danielk1977aef0bf62005-12-30 16:28:01 +00002495static int countWriteCursors(BtShared *pBt){
danielk1977fbcd5852004-06-15 02:44:18 +00002496 BtCursor *pCur;
2497 int r = 0;
2498 for(pCur=pBt->pCursor; pCur; pCur=pCur->pNext){
drhfb982642007-08-30 01:19:59 +00002499 if( pCur->wrFlag && pCur->eState!=CURSOR_FAULT ) r++;
danielk1977fbcd5852004-06-15 02:44:18 +00002500 }
2501 return r;
2502}
2503#endif
2504
drhc39e0002004-05-07 23:50:57 +00002505/*
drhfb982642007-08-30 01:19:59 +00002506** This routine sets the state to CURSOR_FAULT and the error
2507** code to errCode for every cursor on BtShared that pBtree
2508** references.
2509**
2510** Every cursor is tripped, including cursors that belong
2511** to other database connections that happen to be sharing
2512** the cache with pBtree.
2513**
2514** This routine gets called when a rollback occurs.
2515** All cursors using the same cache must be tripped
2516** to prevent them from trying to use the btree after
2517** the rollback. The rollback may have deleted tables
2518** or moved root pages, so it is not sufficient to
2519** save the state of the cursor. The cursor must be
2520** invalidated.
2521*/
2522void sqlite3BtreeTripAllCursors(Btree *pBtree, int errCode){
2523 BtCursor *p;
2524 sqlite3BtreeEnter(pBtree);
2525 for(p=pBtree->pBt->pCursor; p; p=p->pNext){
2526 clearCursorPosition(p);
2527 p->eState = CURSOR_FAULT;
2528 p->skip = errCode;
2529 }
2530 sqlite3BtreeLeave(pBtree);
2531}
2532
2533/*
drhecdc7532001-09-23 02:35:53 +00002534** Rollback the transaction in progress. All cursors will be
2535** invalided by this operation. Any attempt to use a cursor
2536** that was open at the beginning of this operation will result
2537** in an error.
drh5e00f6c2001-09-13 13:46:56 +00002538**
2539** This will release the write lock on the database file. If there
2540** are no active cursors, it also releases the read lock.
drha059ad02001-04-17 20:09:11 +00002541*/
danielk1977aef0bf62005-12-30 16:28:01 +00002542int sqlite3BtreeRollback(Btree *p){
danielk19778d34dfd2006-01-24 16:37:57 +00002543 int rc;
danielk1977aef0bf62005-12-30 16:28:01 +00002544 BtShared *pBt = p->pBt;
drh24cd67e2004-05-10 16:18:47 +00002545 MemPage *pPage1;
danielk1977aef0bf62005-12-30 16:28:01 +00002546
drhd677b3d2007-08-20 22:48:41 +00002547 sqlite3BtreeEnter(p);
drhe5fe6902007-12-07 18:55:28 +00002548 pBt->db = p->db;
danielk19772b8c13e2006-01-24 14:21:24 +00002549 rc = saveAllCursors(pBt, 0, 0);
danielk19778d34dfd2006-01-24 16:37:57 +00002550#ifndef SQLITE_OMIT_SHARED_CACHE
danielk19772b8c13e2006-01-24 14:21:24 +00002551 if( rc!=SQLITE_OK ){
danielk19778d34dfd2006-01-24 16:37:57 +00002552 /* This is a horrible situation. An IO or malloc() error occured whilst
2553 ** trying to save cursor positions. If this is an automatic rollback (as
2554 ** the result of a constraint, malloc() failure or IO error) then
2555 ** the cache may be internally inconsistent (not contain valid trees) so
2556 ** we cannot simply return the error to the caller. Instead, abort
2557 ** all queries that may be using any of the cursors that failed to save.
2558 */
drhfb982642007-08-30 01:19:59 +00002559 sqlite3BtreeTripAllCursors(p, rc);
danielk19772b8c13e2006-01-24 14:21:24 +00002560 }
danielk19778d34dfd2006-01-24 16:37:57 +00002561#endif
danielk1977aef0bf62005-12-30 16:28:01 +00002562 btreeIntegrity(p);
2563 unlockAllTables(p);
2564
2565 if( p->inTrans==TRANS_WRITE ){
danielk19778d34dfd2006-01-24 16:37:57 +00002566 int rc2;
danielk1977aef0bf62005-12-30 16:28:01 +00002567
danielk1977dddbcdc2007-04-26 14:42:34 +00002568#ifndef SQLITE_OMIT_AUTOVACUUM
2569 pBt->nTrunc = 0;
2570#endif
2571
danielk19778d34dfd2006-01-24 16:37:57 +00002572 assert( TRANS_WRITE==pBt->inTransaction );
danielk19773b8a05f2007-03-19 17:44:26 +00002573 rc2 = sqlite3PagerRollback(pBt->pPager);
danielk19778d34dfd2006-01-24 16:37:57 +00002574 if( rc2!=SQLITE_OK ){
2575 rc = rc2;
2576 }
2577
drh24cd67e2004-05-10 16:18:47 +00002578 /* The rollback may have destroyed the pPage1->aData value. So
drh16a9b832007-05-05 18:39:25 +00002579 ** call sqlite3BtreeGetPage() on page 1 again to make
2580 ** sure pPage1->aData is set correctly. */
2581 if( sqlite3BtreeGetPage(pBt, 1, &pPage1, 0)==SQLITE_OK ){
drh24cd67e2004-05-10 16:18:47 +00002582 releasePage(pPage1);
2583 }
danielk1977fbcd5852004-06-15 02:44:18 +00002584 assert( countWriteCursors(pBt)==0 );
danielk1977aef0bf62005-12-30 16:28:01 +00002585 pBt->inTransaction = TRANS_READ;
drh24cd67e2004-05-10 16:18:47 +00002586 }
danielk1977aef0bf62005-12-30 16:28:01 +00002587
2588 if( p->inTrans!=TRANS_NONE ){
2589 assert( pBt->nTransaction>0 );
2590 pBt->nTransaction--;
2591 if( 0==pBt->nTransaction ){
2592 pBt->inTransaction = TRANS_NONE;
2593 }
2594 }
2595
2596 p->inTrans = TRANS_NONE;
danielk1977ee5741e2004-05-31 10:01:34 +00002597 pBt->inStmt = 0;
drh5e00f6c2001-09-13 13:46:56 +00002598 unlockBtreeIfUnused(pBt);
danielk1977aef0bf62005-12-30 16:28:01 +00002599
2600 btreeIntegrity(p);
drhd677b3d2007-08-20 22:48:41 +00002601 sqlite3BtreeLeave(p);
drha059ad02001-04-17 20:09:11 +00002602 return rc;
2603}
2604
2605/*
drhab01f612004-05-22 02:55:23 +00002606** Start a statement subtransaction. The subtransaction can
2607** can be rolled back independently of the main transaction.
2608** You must start a transaction before starting a subtransaction.
2609** The subtransaction is ended automatically if the main transaction
drh663fc632002-02-02 18:49:19 +00002610** commits or rolls back.
2611**
drhab01f612004-05-22 02:55:23 +00002612** Only one subtransaction may be active at a time. It is an error to try
2613** to start a new subtransaction if another subtransaction is already active.
2614**
2615** Statement subtransactions are used around individual SQL statements
2616** that are contained within a BEGIN...COMMIT block. If a constraint
2617** error occurs within the statement, the effect of that one statement
2618** can be rolled back without having to rollback the entire transaction.
drh663fc632002-02-02 18:49:19 +00002619*/
danielk1977aef0bf62005-12-30 16:28:01 +00002620int sqlite3BtreeBeginStmt(Btree *p){
drh663fc632002-02-02 18:49:19 +00002621 int rc;
danielk1977aef0bf62005-12-30 16:28:01 +00002622 BtShared *pBt = p->pBt;
drhd677b3d2007-08-20 22:48:41 +00002623 sqlite3BtreeEnter(p);
drhe5fe6902007-12-07 18:55:28 +00002624 pBt->db = p->db;
danielk1977aef0bf62005-12-30 16:28:01 +00002625 if( (p->inTrans!=TRANS_WRITE) || pBt->inStmt ){
drhd677b3d2007-08-20 22:48:41 +00002626 rc = pBt->readOnly ? SQLITE_READONLY : SQLITE_ERROR;
2627 }else{
2628 assert( pBt->inTransaction==TRANS_WRITE );
2629 rc = pBt->readOnly ? SQLITE_OK : sqlite3PagerStmtBegin(pBt->pPager);
2630 pBt->inStmt = 1;
drh0d65dc02002-02-03 00:56:09 +00002631 }
drhd677b3d2007-08-20 22:48:41 +00002632 sqlite3BtreeLeave(p);
drh663fc632002-02-02 18:49:19 +00002633 return rc;
2634}
2635
2636
2637/*
drhab01f612004-05-22 02:55:23 +00002638** Commit the statment subtransaction currently in progress. If no
2639** subtransaction is active, this is a no-op.
drh663fc632002-02-02 18:49:19 +00002640*/
danielk1977aef0bf62005-12-30 16:28:01 +00002641int sqlite3BtreeCommitStmt(Btree *p){
drh663fc632002-02-02 18:49:19 +00002642 int rc;
danielk1977aef0bf62005-12-30 16:28:01 +00002643 BtShared *pBt = p->pBt;
drhd677b3d2007-08-20 22:48:41 +00002644 sqlite3BtreeEnter(p);
drhe5fe6902007-12-07 18:55:28 +00002645 pBt->db = p->db;
drh3aac2dd2004-04-26 14:10:20 +00002646 if( pBt->inStmt && !pBt->readOnly ){
danielk19773b8a05f2007-03-19 17:44:26 +00002647 rc = sqlite3PagerStmtCommit(pBt->pPager);
drh663fc632002-02-02 18:49:19 +00002648 }else{
2649 rc = SQLITE_OK;
2650 }
drh3aac2dd2004-04-26 14:10:20 +00002651 pBt->inStmt = 0;
drhd677b3d2007-08-20 22:48:41 +00002652 sqlite3BtreeLeave(p);
drh663fc632002-02-02 18:49:19 +00002653 return rc;
2654}
2655
2656/*
drhab01f612004-05-22 02:55:23 +00002657** Rollback the active statement subtransaction. If no subtransaction
2658** is active this routine is a no-op.
drh663fc632002-02-02 18:49:19 +00002659**
drhab01f612004-05-22 02:55:23 +00002660** All cursors will be invalidated by this operation. Any attempt
drh663fc632002-02-02 18:49:19 +00002661** to use a cursor that was open at the beginning of this operation
2662** will result in an error.
2663*/
danielk1977aef0bf62005-12-30 16:28:01 +00002664int sqlite3BtreeRollbackStmt(Btree *p){
danielk197797a227c2006-01-20 16:32:04 +00002665 int rc = SQLITE_OK;
danielk1977aef0bf62005-12-30 16:28:01 +00002666 BtShared *pBt = p->pBt;
drhd677b3d2007-08-20 22:48:41 +00002667 sqlite3BtreeEnter(p);
drhe5fe6902007-12-07 18:55:28 +00002668 pBt->db = p->db;
danielk197797a227c2006-01-20 16:32:04 +00002669 if( pBt->inStmt && !pBt->readOnly ){
danielk19773b8a05f2007-03-19 17:44:26 +00002670 rc = sqlite3PagerStmtRollback(pBt->pPager);
danielk197797a227c2006-01-20 16:32:04 +00002671 assert( countWriteCursors(pBt)==0 );
2672 pBt->inStmt = 0;
2673 }
drhd677b3d2007-08-20 22:48:41 +00002674 sqlite3BtreeLeave(p);
drh663fc632002-02-02 18:49:19 +00002675 return rc;
2676}
2677
2678/*
drh8b2f49b2001-06-08 00:21:52 +00002679** Create a new cursor for the BTree whose root is on the page
2680** iTable. The act of acquiring a cursor gets a read lock on
2681** the database file.
drh1bee3d72001-10-15 00:44:35 +00002682**
2683** If wrFlag==0, then the cursor can only be used for reading.
drhf74b8d92002-09-01 23:20:45 +00002684** If wrFlag==1, then the cursor can be used for reading or for
2685** writing if other conditions for writing are also met. These
2686** are the conditions that must be met in order for writing to
2687** be allowed:
drh6446c4d2001-12-15 14:22:18 +00002688**
drhf74b8d92002-09-01 23:20:45 +00002689** 1: The cursor must have been opened with wrFlag==1
2690**
drhfe5d71d2007-03-19 11:54:10 +00002691** 2: Other database connections that share the same pager cache
2692** but which are not in the READ_UNCOMMITTED state may not have
2693** cursors open with wrFlag==0 on the same table. Otherwise
2694** the changes made by this write cursor would be visible to
2695** the read cursors in the other database connection.
drhf74b8d92002-09-01 23:20:45 +00002696**
2697** 3: The database must be writable (not on read-only media)
2698**
2699** 4: There must be an active transaction.
2700**
drh6446c4d2001-12-15 14:22:18 +00002701** No checking is done to make sure that page iTable really is the
2702** root page of a b-tree. If it is not, then the cursor acquired
2703** will not work correctly.
drha059ad02001-04-17 20:09:11 +00002704*/
drhd677b3d2007-08-20 22:48:41 +00002705static int btreeCursor(
danielk1977cd3e8f72008-03-25 09:47:35 +00002706 Btree *p, /* The btree */
2707 int iTable, /* Root page of table to open */
2708 int wrFlag, /* 1 to write. 0 read-only */
2709 struct KeyInfo *pKeyInfo, /* First arg to comparison function */
2710 BtCursor *pCur /* Space for new cursor */
drh3aac2dd2004-04-26 14:10:20 +00002711){
drha059ad02001-04-17 20:09:11 +00002712 int rc;
danielk1977aef0bf62005-12-30 16:28:01 +00002713 BtShared *pBt = p->pBt;
drhecdc7532001-09-23 02:35:53 +00002714
drh1fee73e2007-08-29 04:00:57 +00002715 assert( sqlite3BtreeHoldsMutex(p) );
drh8dcd7ca2004-08-08 19:43:29 +00002716 if( wrFlag ){
drh8dcd7ca2004-08-08 19:43:29 +00002717 if( pBt->readOnly ){
2718 return SQLITE_READONLY;
2719 }
drh980b1a72006-08-16 16:42:48 +00002720 if( checkReadLocks(p, iTable, 0) ){
drh8dcd7ca2004-08-08 19:43:29 +00002721 return SQLITE_LOCKED;
2722 }
drha0c9a112004-03-10 13:42:37 +00002723 }
danielk1977aef0bf62005-12-30 16:28:01 +00002724
drh4b70f112004-05-02 21:12:19 +00002725 if( pBt->pPage1==0 ){
danielk1977aef0bf62005-12-30 16:28:01 +00002726 rc = lockBtreeWithRetry(p);
drha059ad02001-04-17 20:09:11 +00002727 if( rc!=SQLITE_OK ){
drha059ad02001-04-17 20:09:11 +00002728 return rc;
2729 }
drh1831f182007-04-24 17:35:59 +00002730 if( pBt->readOnly && wrFlag ){
2731 return SQLITE_READONLY;
2732 }
drha059ad02001-04-17 20:09:11 +00002733 }
drh8b2f49b2001-06-08 00:21:52 +00002734 pCur->pgnoRoot = (Pgno)iTable;
danielk1977ad0132d2008-06-07 08:58:22 +00002735 if( iTable==1 && pagerPagecount(pBt->pPager)==0 ){
drh24cd67e2004-05-10 16:18:47 +00002736 rc = SQLITE_EMPTY;
2737 goto create_cursor_exception;
2738 }
drhde647132004-05-07 17:57:49 +00002739 rc = getAndInitPage(pBt, pCur->pgnoRoot, &pCur->pPage, 0);
drhbd03cae2001-06-02 02:40:57 +00002740 if( rc!=SQLITE_OK ){
2741 goto create_cursor_exception;
drha059ad02001-04-17 20:09:11 +00002742 }
danielk1977aef0bf62005-12-30 16:28:01 +00002743
danielk1977aef0bf62005-12-30 16:28:01 +00002744 /* Now that no other errors can occur, finish filling in the BtCursor
2745 ** variables, link the cursor into the BtShared list and set *ppCur (the
2746 ** output argument to this function).
2747 */
drh1e968a02008-03-25 00:22:21 +00002748 pCur->pKeyInfo = pKeyInfo;
danielk1977aef0bf62005-12-30 16:28:01 +00002749 pCur->pBtree = p;
drhd0679ed2007-08-28 22:24:34 +00002750 pCur->pBt = pBt;
drhecdc7532001-09-23 02:35:53 +00002751 pCur->wrFlag = wrFlag;
drha059ad02001-04-17 20:09:11 +00002752 pCur->pNext = pBt->pCursor;
2753 if( pCur->pNext ){
2754 pCur->pNext->pPrev = pCur;
2755 }
2756 pBt->pCursor = pCur;
danielk1977da184232006-01-05 11:34:32 +00002757 pCur->eState = CURSOR_INVALID;
drhbd03cae2001-06-02 02:40:57 +00002758
danielk1977aef0bf62005-12-30 16:28:01 +00002759 return SQLITE_OK;
drhd677b3d2007-08-20 22:48:41 +00002760
drhbd03cae2001-06-02 02:40:57 +00002761create_cursor_exception:
drhbd03cae2001-06-02 02:40:57 +00002762 if( pCur ){
drh3aac2dd2004-04-26 14:10:20 +00002763 releasePage(pCur->pPage);
drhbd03cae2001-06-02 02:40:57 +00002764 }
drh5e00f6c2001-09-13 13:46:56 +00002765 unlockBtreeIfUnused(pBt);
drhbd03cae2001-06-02 02:40:57 +00002766 return rc;
drha059ad02001-04-17 20:09:11 +00002767}
drhd677b3d2007-08-20 22:48:41 +00002768int sqlite3BtreeCursor(
danielk1977cd3e8f72008-03-25 09:47:35 +00002769 Btree *p, /* The btree */
2770 int iTable, /* Root page of table to open */
2771 int wrFlag, /* 1 to write. 0 read-only */
2772 struct KeyInfo *pKeyInfo, /* First arg to xCompare() */
2773 BtCursor *pCur /* Write new cursor here */
drhd677b3d2007-08-20 22:48:41 +00002774){
2775 int rc;
2776 sqlite3BtreeEnter(p);
drhe5fe6902007-12-07 18:55:28 +00002777 p->pBt->db = p->db;
danielk1977cd3e8f72008-03-25 09:47:35 +00002778 rc = btreeCursor(p, iTable, wrFlag, pKeyInfo, pCur);
drhd677b3d2007-08-20 22:48:41 +00002779 sqlite3BtreeLeave(p);
2780 return rc;
2781}
danielk1977cd3e8f72008-03-25 09:47:35 +00002782int sqlite3BtreeCursorSize(){
2783 return sizeof(BtCursor);
2784}
2785
drhd677b3d2007-08-20 22:48:41 +00002786
drha059ad02001-04-17 20:09:11 +00002787
2788/*
drh5e00f6c2001-09-13 13:46:56 +00002789** Close a cursor. The read lock on the database file is released
drhbd03cae2001-06-02 02:40:57 +00002790** when the last cursor is closed.
drha059ad02001-04-17 20:09:11 +00002791*/
drh3aac2dd2004-04-26 14:10:20 +00002792int sqlite3BtreeCloseCursor(BtCursor *pCur){
drhff0587c2007-08-29 17:43:19 +00002793 Btree *pBtree = pCur->pBtree;
danielk1977cd3e8f72008-03-25 09:47:35 +00002794 if( pBtree ){
2795 BtShared *pBt = pCur->pBt;
2796 sqlite3BtreeEnter(pBtree);
2797 pBt->db = pBtree->db;
2798 clearCursorPosition(pCur);
2799 if( pCur->pPrev ){
2800 pCur->pPrev->pNext = pCur->pNext;
2801 }else{
2802 pBt->pCursor = pCur->pNext;
2803 }
2804 if( pCur->pNext ){
2805 pCur->pNext->pPrev = pCur->pPrev;
2806 }
2807 releasePage(pCur->pPage);
2808 unlockBtreeIfUnused(pBt);
2809 invalidateOverflowCache(pCur);
2810 /* sqlite3_free(pCur); */
2811 sqlite3BtreeLeave(pBtree);
drha059ad02001-04-17 20:09:11 +00002812 }
drh8c42ca92001-06-22 19:15:00 +00002813 return SQLITE_OK;
drha059ad02001-04-17 20:09:11 +00002814}
2815
drh7e3b0a02001-04-28 16:52:40 +00002816/*
drh5e2f8b92001-05-28 00:41:15 +00002817** Make a temporary cursor by filling in the fields of pTempCur.
2818** The temporary cursor is not on the cursor list for the Btree.
2819*/
drh16a9b832007-05-05 18:39:25 +00002820void sqlite3BtreeGetTempCursor(BtCursor *pCur, BtCursor *pTempCur){
drh1fee73e2007-08-29 04:00:57 +00002821 assert( cursorHoldsMutex(pCur) );
drh5e2f8b92001-05-28 00:41:15 +00002822 memcpy(pTempCur, pCur, sizeof(*pCur));
2823 pTempCur->pNext = 0;
2824 pTempCur->pPrev = 0;
drhecdc7532001-09-23 02:35:53 +00002825 if( pTempCur->pPage ){
danielk19773b8a05f2007-03-19 17:44:26 +00002826 sqlite3PagerRef(pTempCur->pPage->pDbPage);
drhecdc7532001-09-23 02:35:53 +00002827 }
drh5e2f8b92001-05-28 00:41:15 +00002828}
2829
2830/*
drhbd03cae2001-06-02 02:40:57 +00002831** Delete a temporary cursor such as was made by the CreateTemporaryCursor()
drh5e2f8b92001-05-28 00:41:15 +00002832** function above.
2833*/
drh16a9b832007-05-05 18:39:25 +00002834void sqlite3BtreeReleaseTempCursor(BtCursor *pCur){
drh1fee73e2007-08-29 04:00:57 +00002835 assert( cursorHoldsMutex(pCur) );
drhecdc7532001-09-23 02:35:53 +00002836 if( pCur->pPage ){
danielk19773b8a05f2007-03-19 17:44:26 +00002837 sqlite3PagerUnref(pCur->pPage->pDbPage);
drhecdc7532001-09-23 02:35:53 +00002838 }
drh5e2f8b92001-05-28 00:41:15 +00002839}
2840
2841/*
drh86057612007-06-26 01:04:48 +00002842** Make sure the BtCursor* given in the argument has a valid
2843** BtCursor.info structure. If it is not already valid, call
danielk19771cc5ed82007-05-16 17:28:43 +00002844** sqlite3BtreeParseCell() to fill it in.
drhab01f612004-05-22 02:55:23 +00002845**
2846** BtCursor.info is a cache of the information in the current cell.
drh16a9b832007-05-05 18:39:25 +00002847** Using this cache reduces the number of calls to sqlite3BtreeParseCell().
drh86057612007-06-26 01:04:48 +00002848**
2849** 2007-06-25: There is a bug in some versions of MSVC that cause the
2850** compiler to crash when getCellInfo() is implemented as a macro.
2851** But there is a measureable speed advantage to using the macro on gcc
2852** (when less compiler optimizations like -Os or -O0 are used and the
2853** compiler is not doing agressive inlining.) So we use a real function
2854** for MSVC and a macro for everything else. Ticket #2457.
drh9188b382004-05-14 21:12:22 +00002855*/
drh9188b382004-05-14 21:12:22 +00002856#ifndef NDEBUG
danielk19771cc5ed82007-05-16 17:28:43 +00002857 static void assertCellInfo(BtCursor *pCur){
drh9188b382004-05-14 21:12:22 +00002858 CellInfo info;
drh51c6d962004-06-06 00:42:25 +00002859 memset(&info, 0, sizeof(info));
drh16a9b832007-05-05 18:39:25 +00002860 sqlite3BtreeParseCell(pCur->pPage, pCur->idx, &info);
drh9188b382004-05-14 21:12:22 +00002861 assert( memcmp(&info, &pCur->info, sizeof(info))==0 );
drh9188b382004-05-14 21:12:22 +00002862 }
danielk19771cc5ed82007-05-16 17:28:43 +00002863#else
2864 #define assertCellInfo(x)
2865#endif
drh86057612007-06-26 01:04:48 +00002866#ifdef _MSC_VER
2867 /* Use a real function in MSVC to work around bugs in that compiler. */
2868 static void getCellInfo(BtCursor *pCur){
2869 if( pCur->info.nSize==0 ){
2870 sqlite3BtreeParseCell(pCur->pPage, pCur->idx, &pCur->info);
drha2c20e42008-03-29 16:01:04 +00002871 pCur->validNKey = 1;
drh86057612007-06-26 01:04:48 +00002872 }else{
2873 assertCellInfo(pCur);
2874 }
2875 }
2876#else /* if not _MSC_VER */
2877 /* Use a macro in all other compilers so that the function is inlined */
2878#define getCellInfo(pCur) \
2879 if( pCur->info.nSize==0 ){ \
danielk19771cc5ed82007-05-16 17:28:43 +00002880 sqlite3BtreeParseCell(pCur->pPage, pCur->idx, &pCur->info); \
drha2c20e42008-03-29 16:01:04 +00002881 pCur->validNKey = 1; \
drh86057612007-06-26 01:04:48 +00002882 }else{ \
2883 assertCellInfo(pCur); \
2884 }
2885#endif /* _MSC_VER */
drh9188b382004-05-14 21:12:22 +00002886
2887/*
drh3aac2dd2004-04-26 14:10:20 +00002888** Set *pSize to the size of the buffer needed to hold the value of
2889** the key for the current entry. If the cursor is not pointing
2890** to a valid entry, *pSize is set to 0.
2891**
drh4b70f112004-05-02 21:12:19 +00002892** For a table with the INTKEY flag set, this routine returns the key
drh3aac2dd2004-04-26 14:10:20 +00002893** itself, not the number of bytes in the key.
drh7e3b0a02001-04-28 16:52:40 +00002894*/
drh4a1c3802004-05-12 15:15:47 +00002895int sqlite3BtreeKeySize(BtCursor *pCur, i64 *pSize){
drhd677b3d2007-08-20 22:48:41 +00002896 int rc;
2897
drh1fee73e2007-08-29 04:00:57 +00002898 assert( cursorHoldsMutex(pCur) );
drhd677b3d2007-08-20 22:48:41 +00002899 rc = restoreOrClearCursorPosition(pCur);
danielk1977da184232006-01-05 11:34:32 +00002900 if( rc==SQLITE_OK ){
2901 assert( pCur->eState==CURSOR_INVALID || pCur->eState==CURSOR_VALID );
2902 if( pCur->eState==CURSOR_INVALID ){
2903 *pSize = 0;
2904 }else{
drh86057612007-06-26 01:04:48 +00002905 getCellInfo(pCur);
danielk1977da184232006-01-05 11:34:32 +00002906 *pSize = pCur->info.nKey;
2907 }
drh72f82862001-05-24 21:06:34 +00002908 }
danielk1977da184232006-01-05 11:34:32 +00002909 return rc;
drha059ad02001-04-17 20:09:11 +00002910}
drh2af926b2001-05-15 00:39:25 +00002911
drh72f82862001-05-24 21:06:34 +00002912/*
drh0e1c19e2004-05-11 00:58:56 +00002913** Set *pSize to the number of bytes of data in the entry the
2914** cursor currently points to. Always return SQLITE_OK.
2915** Failure is not possible. If the cursor is not currently
2916** pointing to an entry (which can happen, for example, if
2917** the database is empty) then *pSize is set to 0.
2918*/
2919int sqlite3BtreeDataSize(BtCursor *pCur, u32 *pSize){
drhd677b3d2007-08-20 22:48:41 +00002920 int rc;
2921
drh1fee73e2007-08-29 04:00:57 +00002922 assert( cursorHoldsMutex(pCur) );
drhd677b3d2007-08-20 22:48:41 +00002923 rc = restoreOrClearCursorPosition(pCur);
danielk1977da184232006-01-05 11:34:32 +00002924 if( rc==SQLITE_OK ){
2925 assert( pCur->eState==CURSOR_INVALID || pCur->eState==CURSOR_VALID );
2926 if( pCur->eState==CURSOR_INVALID ){
2927 /* Not pointing at a valid entry - set *pSize to 0. */
2928 *pSize = 0;
2929 }else{
drh86057612007-06-26 01:04:48 +00002930 getCellInfo(pCur);
danielk1977da184232006-01-05 11:34:32 +00002931 *pSize = pCur->info.nData;
2932 }
drh0e1c19e2004-05-11 00:58:56 +00002933 }
danielk1977da184232006-01-05 11:34:32 +00002934 return rc;
drh0e1c19e2004-05-11 00:58:56 +00002935}
2936
2937/*
danielk1977d04417962007-05-02 13:16:30 +00002938** Given the page number of an overflow page in the database (parameter
2939** ovfl), this function finds the page number of the next page in the
2940** linked list of overflow pages. If possible, it uses the auto-vacuum
2941** pointer-map data instead of reading the content of page ovfl to do so.
2942**
2943** If an error occurs an SQLite error code is returned. Otherwise:
2944**
2945** Unless pPgnoNext is NULL, the page number of the next overflow
2946** page in the linked list is written to *pPgnoNext. If page ovfl
drh85b623f2007-12-13 21:54:09 +00002947** is the last page in its linked list, *pPgnoNext is set to zero.
danielk1977d04417962007-05-02 13:16:30 +00002948**
2949** If ppPage is not NULL, *ppPage is set to the MemPage* handle
2950** for page ovfl. The underlying pager page may have been requested
2951** with the noContent flag set, so the page data accessable via
2952** this handle may not be trusted.
2953*/
2954static int getOverflowPage(
2955 BtShared *pBt,
2956 Pgno ovfl, /* Overflow page */
2957 MemPage **ppPage, /* OUT: MemPage handle */
2958 Pgno *pPgnoNext /* OUT: Next overflow page number */
2959){
2960 Pgno next = 0;
2961 int rc;
2962
drh1fee73e2007-08-29 04:00:57 +00002963 assert( sqlite3_mutex_held(pBt->mutex) );
danielk1977d04417962007-05-02 13:16:30 +00002964 /* One of these must not be NULL. Otherwise, why call this function? */
2965 assert(ppPage || pPgnoNext);
2966
2967 /* If pPgnoNext is NULL, then this function is being called to obtain
2968 ** a MemPage* reference only. No page-data is required in this case.
2969 */
2970 if( !pPgnoNext ){
drh16a9b832007-05-05 18:39:25 +00002971 return sqlite3BtreeGetPage(pBt, ovfl, ppPage, 1);
danielk1977d04417962007-05-02 13:16:30 +00002972 }
2973
2974#ifndef SQLITE_OMIT_AUTOVACUUM
2975 /* Try to find the next page in the overflow list using the
2976 ** autovacuum pointer-map pages. Guess that the next page in
2977 ** the overflow list is page number (ovfl+1). If that guess turns
2978 ** out to be wrong, fall back to loading the data of page
2979 ** number ovfl to determine the next page number.
2980 */
2981 if( pBt->autoVacuum ){
2982 Pgno pgno;
2983 Pgno iGuess = ovfl+1;
2984 u8 eType;
2985
2986 while( PTRMAP_ISPAGE(pBt, iGuess) || iGuess==PENDING_BYTE_PAGE(pBt) ){
2987 iGuess++;
2988 }
2989
danielk1977ad0132d2008-06-07 08:58:22 +00002990 if( iGuess<=pagerPagecount(pBt->pPager) ){
danielk1977d04417962007-05-02 13:16:30 +00002991 rc = ptrmapGet(pBt, iGuess, &eType, &pgno);
2992 if( rc!=SQLITE_OK ){
2993 return rc;
2994 }
2995 if( eType==PTRMAP_OVERFLOW2 && pgno==ovfl ){
2996 next = iGuess;
2997 }
2998 }
2999 }
3000#endif
3001
3002 if( next==0 || ppPage ){
3003 MemPage *pPage = 0;
3004
drh16a9b832007-05-05 18:39:25 +00003005 rc = sqlite3BtreeGetPage(pBt, ovfl, &pPage, next!=0);
danielk1977d04417962007-05-02 13:16:30 +00003006 assert(rc==SQLITE_OK || pPage==0);
3007 if( next==0 && rc==SQLITE_OK ){
3008 next = get4byte(pPage->aData);
3009 }
3010
3011 if( ppPage ){
3012 *ppPage = pPage;
3013 }else{
3014 releasePage(pPage);
3015 }
3016 }
3017 *pPgnoNext = next;
3018
3019 return rc;
3020}
3021
danielk1977da107192007-05-04 08:32:13 +00003022/*
3023** Copy data from a buffer to a page, or from a page to a buffer.
3024**
3025** pPayload is a pointer to data stored on database page pDbPage.
3026** If argument eOp is false, then nByte bytes of data are copied
3027** from pPayload to the buffer pointed at by pBuf. If eOp is true,
3028** then sqlite3PagerWrite() is called on pDbPage and nByte bytes
3029** of data are copied from the buffer pBuf to pPayload.
3030**
3031** SQLITE_OK is returned on success, otherwise an error code.
3032*/
3033static int copyPayload(
3034 void *pPayload, /* Pointer to page data */
3035 void *pBuf, /* Pointer to buffer */
3036 int nByte, /* Number of bytes to copy */
3037 int eOp, /* 0 -> copy from page, 1 -> copy to page */
3038 DbPage *pDbPage /* Page containing pPayload */
3039){
3040 if( eOp ){
3041 /* Copy data from buffer to page (a write operation) */
3042 int rc = sqlite3PagerWrite(pDbPage);
3043 if( rc!=SQLITE_OK ){
3044 return rc;
3045 }
3046 memcpy(pPayload, pBuf, nByte);
3047 }else{
3048 /* Copy data from page to buffer (a read operation) */
3049 memcpy(pBuf, pPayload, nByte);
3050 }
3051 return SQLITE_OK;
3052}
danielk1977d04417962007-05-02 13:16:30 +00003053
3054/*
danielk19779f8d6402007-05-02 17:48:45 +00003055** This function is used to read or overwrite payload information
3056** for the entry that the pCur cursor is pointing to. If the eOp
3057** parameter is 0, this is a read operation (data copied into
3058** buffer pBuf). If it is non-zero, a write (data copied from
3059** buffer pBuf).
3060**
3061** A total of "amt" bytes are read or written beginning at "offset".
3062** Data is read to or from the buffer pBuf.
drh72f82862001-05-24 21:06:34 +00003063**
3064** This routine does not make a distinction between key and data.
danielk19779f8d6402007-05-02 17:48:45 +00003065** It just reads or writes bytes from the payload area. Data might
3066** appear on the main page or be scattered out on multiple overflow
3067** pages.
danielk1977da107192007-05-04 08:32:13 +00003068**
danielk1977dcbb5d32007-05-04 18:36:44 +00003069** If the BtCursor.isIncrblobHandle flag is set, and the current
danielk1977da107192007-05-04 08:32:13 +00003070** cursor entry uses one or more overflow pages, this function
3071** allocates space for and lazily popluates the overflow page-list
3072** cache array (BtCursor.aOverflow). Subsequent calls use this
3073** cache to make seeking to the supplied offset more efficient.
3074**
3075** Once an overflow page-list cache has been allocated, it may be
3076** invalidated if some other cursor writes to the same table, or if
3077** the cursor is moved to a different row. Additionally, in auto-vacuum
3078** mode, the following events may invalidate an overflow page-list cache.
3079**
3080** * An incremental vacuum,
3081** * A commit in auto_vacuum="full" mode,
3082** * Creating a table (may require moving an overflow page).
drh72f82862001-05-24 21:06:34 +00003083*/
danielk19779f8d6402007-05-02 17:48:45 +00003084static int accessPayload(
drh3aac2dd2004-04-26 14:10:20 +00003085 BtCursor *pCur, /* Cursor pointing to entry to read from */
3086 int offset, /* Begin reading this far into payload */
3087 int amt, /* Read this many bytes */
3088 unsigned char *pBuf, /* Write the bytes into this buffer */
danielk19779f8d6402007-05-02 17:48:45 +00003089 int skipKey, /* offset begins at data if this is true */
3090 int eOp /* zero to read. non-zero to write. */
drh3aac2dd2004-04-26 14:10:20 +00003091){
3092 unsigned char *aPayload;
danielk1977da107192007-05-04 08:32:13 +00003093 int rc = SQLITE_OK;
drhfa1a98a2004-05-14 19:08:17 +00003094 u32 nKey;
danielk19772dec9702007-05-02 16:48:37 +00003095 int iIdx = 0;
drhd0679ed2007-08-28 22:24:34 +00003096 MemPage *pPage = pCur->pPage; /* Btree page of current cursor entry */
drh51f015e2007-10-16 19:45:29 +00003097 BtShared *pBt; /* Btree this cursor belongs to */
drh3aac2dd2004-04-26 14:10:20 +00003098
danielk1977da107192007-05-04 08:32:13 +00003099 assert( pPage );
danielk1977da184232006-01-05 11:34:32 +00003100 assert( pCur->eState==CURSOR_VALID );
drh3aac2dd2004-04-26 14:10:20 +00003101 assert( pCur->idx>=0 && pCur->idx<pPage->nCell );
danielk1977da107192007-05-04 08:32:13 +00003102 assert( offset>=0 );
drh1fee73e2007-08-29 04:00:57 +00003103 assert( cursorHoldsMutex(pCur) );
danielk1977da107192007-05-04 08:32:13 +00003104
drh86057612007-06-26 01:04:48 +00003105 getCellInfo(pCur);
drh366fda62006-01-13 02:35:09 +00003106 aPayload = pCur->info.pCell + pCur->info.nHeader;
danielk1977da107192007-05-04 08:32:13 +00003107 nKey = (pPage->intKey ? 0 : pCur->info.nKey);
3108
drh3aac2dd2004-04-26 14:10:20 +00003109 if( skipKey ){
drhfa1a98a2004-05-14 19:08:17 +00003110 offset += nKey;
drh3aac2dd2004-04-26 14:10:20 +00003111 }
drhfa1a98a2004-05-14 19:08:17 +00003112 if( offset+amt > nKey+pCur->info.nData ){
danielk1977da107192007-05-04 08:32:13 +00003113 /* Trying to read or write past the end of the data is an error */
drha34b6762004-05-07 13:30:42 +00003114 return SQLITE_ERROR;
drh3aac2dd2004-04-26 14:10:20 +00003115 }
danielk1977da107192007-05-04 08:32:13 +00003116
3117 /* Check if data must be read/written to/from the btree page itself. */
drhfa1a98a2004-05-14 19:08:17 +00003118 if( offset<pCur->info.nLocal ){
drh2af926b2001-05-15 00:39:25 +00003119 int a = amt;
drhfa1a98a2004-05-14 19:08:17 +00003120 if( a+offset>pCur->info.nLocal ){
3121 a = pCur->info.nLocal - offset;
drh2af926b2001-05-15 00:39:25 +00003122 }
danielk1977da107192007-05-04 08:32:13 +00003123 rc = copyPayload(&aPayload[offset], pBuf, a, eOp, pPage->pDbPage);
drh2aa679f2001-06-25 02:11:07 +00003124 offset = 0;
drha34b6762004-05-07 13:30:42 +00003125 pBuf += a;
drh2af926b2001-05-15 00:39:25 +00003126 amt -= a;
drhdd793422001-06-28 01:54:48 +00003127 }else{
drhfa1a98a2004-05-14 19:08:17 +00003128 offset -= pCur->info.nLocal;
drhbd03cae2001-06-02 02:40:57 +00003129 }
danielk1977da107192007-05-04 08:32:13 +00003130
drh51f015e2007-10-16 19:45:29 +00003131 pBt = pCur->pBt;
danielk1977da107192007-05-04 08:32:13 +00003132 if( rc==SQLITE_OK && amt>0 ){
3133 const int ovflSize = pBt->usableSize - 4; /* Bytes content per ovfl page */
3134 Pgno nextPage;
3135
drhfa1a98a2004-05-14 19:08:17 +00003136 nextPage = get4byte(&aPayload[pCur->info.nLocal]);
danielk1977da107192007-05-04 08:32:13 +00003137
danielk19772dec9702007-05-02 16:48:37 +00003138#ifndef SQLITE_OMIT_INCRBLOB
danielk1977dcbb5d32007-05-04 18:36:44 +00003139 /* If the isIncrblobHandle flag is set and the BtCursor.aOverflow[]
danielk1977da107192007-05-04 08:32:13 +00003140 ** has not been allocated, allocate it now. The array is sized at
3141 ** one entry for each overflow page in the overflow chain. The
3142 ** page number of the first overflow page is stored in aOverflow[0],
3143 ** etc. A value of 0 in the aOverflow[] array means "not yet known"
3144 ** (the cache is lazily populated).
3145 */
danielk1977dcbb5d32007-05-04 18:36:44 +00003146 if( pCur->isIncrblobHandle && !pCur->aOverflow ){
danielk19772dec9702007-05-02 16:48:37 +00003147 int nOvfl = (pCur->info.nPayload-pCur->info.nLocal+ovflSize-1)/ovflSize;
drh17435752007-08-16 04:30:38 +00003148 pCur->aOverflow = (Pgno *)sqlite3MallocZero(sizeof(Pgno)*nOvfl);
danielk19772dec9702007-05-02 16:48:37 +00003149 if( nOvfl && !pCur->aOverflow ){
danielk1977da107192007-05-04 08:32:13 +00003150 rc = SQLITE_NOMEM;
danielk19772dec9702007-05-02 16:48:37 +00003151 }
3152 }
danielk1977da107192007-05-04 08:32:13 +00003153
3154 /* If the overflow page-list cache has been allocated and the
3155 ** entry for the first required overflow page is valid, skip
3156 ** directly to it.
3157 */
danielk19772dec9702007-05-02 16:48:37 +00003158 if( pCur->aOverflow && pCur->aOverflow[offset/ovflSize] ){
3159 iIdx = (offset/ovflSize);
3160 nextPage = pCur->aOverflow[iIdx];
3161 offset = (offset%ovflSize);
3162 }
3163#endif
danielk1977da107192007-05-04 08:32:13 +00003164
3165 for( ; rc==SQLITE_OK && amt>0 && nextPage; iIdx++){
3166
3167#ifndef SQLITE_OMIT_INCRBLOB
3168 /* If required, populate the overflow page-list cache. */
3169 if( pCur->aOverflow ){
3170 assert(!pCur->aOverflow[iIdx] || pCur->aOverflow[iIdx]==nextPage);
3171 pCur->aOverflow[iIdx] = nextPage;
3172 }
3173#endif
3174
danielk1977d04417962007-05-02 13:16:30 +00003175 if( offset>=ovflSize ){
3176 /* The only reason to read this page is to obtain the page
danielk1977da107192007-05-04 08:32:13 +00003177 ** number for the next page in the overflow chain. The page
drhfd131da2007-08-07 17:13:03 +00003178 ** data is not required. So first try to lookup the overflow
3179 ** page-list cache, if any, then fall back to the getOverflowPage()
danielk1977da107192007-05-04 08:32:13 +00003180 ** function.
danielk1977d04417962007-05-02 13:16:30 +00003181 */
danielk19772dec9702007-05-02 16:48:37 +00003182#ifndef SQLITE_OMIT_INCRBLOB
danielk1977da107192007-05-04 08:32:13 +00003183 if( pCur->aOverflow && pCur->aOverflow[iIdx+1] ){
3184 nextPage = pCur->aOverflow[iIdx+1];
3185 } else
danielk19772dec9702007-05-02 16:48:37 +00003186#endif
danielk1977da107192007-05-04 08:32:13 +00003187 rc = getOverflowPage(pBt, nextPage, 0, &nextPage);
danielk1977da107192007-05-04 08:32:13 +00003188 offset -= ovflSize;
danielk1977d04417962007-05-02 13:16:30 +00003189 }else{
danielk19779f8d6402007-05-02 17:48:45 +00003190 /* Need to read this page properly. It contains some of the
3191 ** range of data that is being read (eOp==0) or written (eOp!=0).
danielk1977d04417962007-05-02 13:16:30 +00003192 */
3193 DbPage *pDbPage;
danielk1977cfe9a692004-06-16 12:00:29 +00003194 int a = amt;
danielk1977d04417962007-05-02 13:16:30 +00003195 rc = sqlite3PagerGet(pBt->pPager, nextPage, &pDbPage);
danielk1977da107192007-05-04 08:32:13 +00003196 if( rc==SQLITE_OK ){
3197 aPayload = sqlite3PagerGetData(pDbPage);
3198 nextPage = get4byte(aPayload);
3199 if( a + offset > ovflSize ){
3200 a = ovflSize - offset;
danielk19779f8d6402007-05-02 17:48:45 +00003201 }
danielk1977da107192007-05-04 08:32:13 +00003202 rc = copyPayload(&aPayload[offset+4], pBuf, a, eOp, pDbPage);
3203 sqlite3PagerUnref(pDbPage);
3204 offset = 0;
3205 amt -= a;
3206 pBuf += a;
danielk19779f8d6402007-05-02 17:48:45 +00003207 }
danielk1977cfe9a692004-06-16 12:00:29 +00003208 }
drh2af926b2001-05-15 00:39:25 +00003209 }
drh2af926b2001-05-15 00:39:25 +00003210 }
danielk1977cfe9a692004-06-16 12:00:29 +00003211
danielk1977da107192007-05-04 08:32:13 +00003212 if( rc==SQLITE_OK && amt>0 ){
drh49285702005-09-17 15:20:26 +00003213 return SQLITE_CORRUPT_BKPT;
drha7fcb052001-12-14 15:09:55 +00003214 }
danielk1977da107192007-05-04 08:32:13 +00003215 return rc;
drh2af926b2001-05-15 00:39:25 +00003216}
3217
drh72f82862001-05-24 21:06:34 +00003218/*
drh3aac2dd2004-04-26 14:10:20 +00003219** Read part of the key associated with cursor pCur. Exactly
drha34b6762004-05-07 13:30:42 +00003220** "amt" bytes will be transfered into pBuf[]. The transfer
drh3aac2dd2004-04-26 14:10:20 +00003221** begins at "offset".
drh8c1238a2003-01-02 14:43:55 +00003222**
drh3aac2dd2004-04-26 14:10:20 +00003223** Return SQLITE_OK on success or an error code if anything goes
3224** wrong. An error is returned if "offset+amt" is larger than
3225** the available payload.
drh72f82862001-05-24 21:06:34 +00003226*/
drha34b6762004-05-07 13:30:42 +00003227int sqlite3BtreeKey(BtCursor *pCur, u32 offset, u32 amt, void *pBuf){
drhd677b3d2007-08-20 22:48:41 +00003228 int rc;
3229
drh1fee73e2007-08-29 04:00:57 +00003230 assert( cursorHoldsMutex(pCur) );
drhd677b3d2007-08-20 22:48:41 +00003231 rc = restoreOrClearCursorPosition(pCur);
danielk1977da184232006-01-05 11:34:32 +00003232 if( rc==SQLITE_OK ){
3233 assert( pCur->eState==CURSOR_VALID );
3234 assert( pCur->pPage!=0 );
3235 if( pCur->pPage->intKey ){
3236 return SQLITE_CORRUPT_BKPT;
3237 }
3238 assert( pCur->pPage->intKey==0 );
3239 assert( pCur->idx>=0 && pCur->idx<pCur->pPage->nCell );
drh16a9b832007-05-05 18:39:25 +00003240 rc = accessPayload(pCur, offset, amt, (unsigned char*)pBuf, 0, 0);
drh6575a222005-03-10 17:06:34 +00003241 }
danielk1977da184232006-01-05 11:34:32 +00003242 return rc;
drh3aac2dd2004-04-26 14:10:20 +00003243}
3244
3245/*
drh3aac2dd2004-04-26 14:10:20 +00003246** Read part of the data associated with cursor pCur. Exactly
drha34b6762004-05-07 13:30:42 +00003247** "amt" bytes will be transfered into pBuf[]. The transfer
drh3aac2dd2004-04-26 14:10:20 +00003248** begins at "offset".
3249**
3250** Return SQLITE_OK on success or an error code if anything goes
3251** wrong. An error is returned if "offset+amt" is larger than
3252** the available payload.
drh72f82862001-05-24 21:06:34 +00003253*/
drh3aac2dd2004-04-26 14:10:20 +00003254int sqlite3BtreeData(BtCursor *pCur, u32 offset, u32 amt, void *pBuf){
drhd677b3d2007-08-20 22:48:41 +00003255 int rc;
3256
drh1fee73e2007-08-29 04:00:57 +00003257 assert( cursorHoldsMutex(pCur) );
drhd677b3d2007-08-20 22:48:41 +00003258 rc = restoreOrClearCursorPosition(pCur);
danielk1977da184232006-01-05 11:34:32 +00003259 if( rc==SQLITE_OK ){
3260 assert( pCur->eState==CURSOR_VALID );
3261 assert( pCur->pPage!=0 );
3262 assert( pCur->idx>=0 && pCur->idx<pCur->pPage->nCell );
drh16a9b832007-05-05 18:39:25 +00003263 rc = accessPayload(pCur, offset, amt, pBuf, 1, 0);
danielk1977da184232006-01-05 11:34:32 +00003264 }
3265 return rc;
drh2af926b2001-05-15 00:39:25 +00003266}
3267
drh72f82862001-05-24 21:06:34 +00003268/*
drh0e1c19e2004-05-11 00:58:56 +00003269** Return a pointer to payload information from the entry that the
3270** pCur cursor is pointing to. The pointer is to the beginning of
3271** the key if skipKey==0 and it points to the beginning of data if
drhe51c44f2004-05-30 20:46:09 +00003272** skipKey==1. The number of bytes of available key/data is written
3273** into *pAmt. If *pAmt==0, then the value returned will not be
3274** a valid pointer.
drh0e1c19e2004-05-11 00:58:56 +00003275**
3276** This routine is an optimization. It is common for the entire key
3277** and data to fit on the local page and for there to be no overflow
3278** pages. When that is so, this routine can be used to access the
3279** key and data without making a copy. If the key and/or data spills
drh16a9b832007-05-05 18:39:25 +00003280** onto overflow pages, then accessPayload() must be used to reassembly
drh0e1c19e2004-05-11 00:58:56 +00003281** the key/data and copy it into a preallocated buffer.
3282**
3283** The pointer returned by this routine looks directly into the cached
3284** page of the database. The data might change or move the next time
3285** any btree routine is called.
3286*/
3287static const unsigned char *fetchPayload(
3288 BtCursor *pCur, /* Cursor pointing to entry to read from */
drhe51c44f2004-05-30 20:46:09 +00003289 int *pAmt, /* Write the number of available bytes here */
drh0e1c19e2004-05-11 00:58:56 +00003290 int skipKey /* read beginning at data if this is true */
3291){
3292 unsigned char *aPayload;
3293 MemPage *pPage;
drhfa1a98a2004-05-14 19:08:17 +00003294 u32 nKey;
3295 int nLocal;
drh0e1c19e2004-05-11 00:58:56 +00003296
3297 assert( pCur!=0 && pCur->pPage!=0 );
danielk1977da184232006-01-05 11:34:32 +00003298 assert( pCur->eState==CURSOR_VALID );
drh1fee73e2007-08-29 04:00:57 +00003299 assert( cursorHoldsMutex(pCur) );
drh0e1c19e2004-05-11 00:58:56 +00003300 pPage = pCur->pPage;
drh0e1c19e2004-05-11 00:58:56 +00003301 assert( pCur->idx>=0 && pCur->idx<pPage->nCell );
drh86057612007-06-26 01:04:48 +00003302 getCellInfo(pCur);
drh43605152004-05-29 21:46:49 +00003303 aPayload = pCur->info.pCell;
drhfa1a98a2004-05-14 19:08:17 +00003304 aPayload += pCur->info.nHeader;
drh0e1c19e2004-05-11 00:58:56 +00003305 if( pPage->intKey ){
drhfa1a98a2004-05-14 19:08:17 +00003306 nKey = 0;
3307 }else{
3308 nKey = pCur->info.nKey;
drh0e1c19e2004-05-11 00:58:56 +00003309 }
drh0e1c19e2004-05-11 00:58:56 +00003310 if( skipKey ){
drhfa1a98a2004-05-14 19:08:17 +00003311 aPayload += nKey;
3312 nLocal = pCur->info.nLocal - nKey;
drh0e1c19e2004-05-11 00:58:56 +00003313 }else{
drhfa1a98a2004-05-14 19:08:17 +00003314 nLocal = pCur->info.nLocal;
drhe51c44f2004-05-30 20:46:09 +00003315 if( nLocal>nKey ){
3316 nLocal = nKey;
3317 }
drh0e1c19e2004-05-11 00:58:56 +00003318 }
drhe51c44f2004-05-30 20:46:09 +00003319 *pAmt = nLocal;
drh0e1c19e2004-05-11 00:58:56 +00003320 return aPayload;
3321}
3322
3323
3324/*
drhe51c44f2004-05-30 20:46:09 +00003325** For the entry that cursor pCur is point to, return as
3326** many bytes of the key or data as are available on the local
3327** b-tree page. Write the number of available bytes into *pAmt.
drh0e1c19e2004-05-11 00:58:56 +00003328**
3329** The pointer returned is ephemeral. The key/data may move
drhd677b3d2007-08-20 22:48:41 +00003330** or be destroyed on the next call to any Btree routine,
3331** including calls from other threads against the same cache.
3332** Hence, a mutex on the BtShared should be held prior to calling
3333** this routine.
drh0e1c19e2004-05-11 00:58:56 +00003334**
3335** These routines is used to get quick access to key and data
3336** in the common case where no overflow pages are used.
drh0e1c19e2004-05-11 00:58:56 +00003337*/
drhe51c44f2004-05-30 20:46:09 +00003338const void *sqlite3BtreeKeyFetch(BtCursor *pCur, int *pAmt){
drh1fee73e2007-08-29 04:00:57 +00003339 assert( cursorHoldsMutex(pCur) );
danielk1977da184232006-01-05 11:34:32 +00003340 if( pCur->eState==CURSOR_VALID ){
3341 return (const void*)fetchPayload(pCur, pAmt, 0);
3342 }
3343 return 0;
drh0e1c19e2004-05-11 00:58:56 +00003344}
drhe51c44f2004-05-30 20:46:09 +00003345const void *sqlite3BtreeDataFetch(BtCursor *pCur, int *pAmt){
drh1fee73e2007-08-29 04:00:57 +00003346 assert( cursorHoldsMutex(pCur) );
danielk1977da184232006-01-05 11:34:32 +00003347 if( pCur->eState==CURSOR_VALID ){
3348 return (const void*)fetchPayload(pCur, pAmt, 1);
3349 }
3350 return 0;
drh0e1c19e2004-05-11 00:58:56 +00003351}
3352
3353
3354/*
drh8178a752003-01-05 21:41:40 +00003355** Move the cursor down to a new child page. The newPgno argument is the
drhab01f612004-05-22 02:55:23 +00003356** page number of the child page to move to.
drh72f82862001-05-24 21:06:34 +00003357*/
drh3aac2dd2004-04-26 14:10:20 +00003358static int moveToChild(BtCursor *pCur, u32 newPgno){
drh72f82862001-05-24 21:06:34 +00003359 int rc;
3360 MemPage *pNewPage;
drh3aac2dd2004-04-26 14:10:20 +00003361 MemPage *pOldPage;
drhd0679ed2007-08-28 22:24:34 +00003362 BtShared *pBt = pCur->pBt;
drh72f82862001-05-24 21:06:34 +00003363
drh1fee73e2007-08-29 04:00:57 +00003364 assert( cursorHoldsMutex(pCur) );
danielk1977da184232006-01-05 11:34:32 +00003365 assert( pCur->eState==CURSOR_VALID );
drhde647132004-05-07 17:57:49 +00003366 rc = getAndInitPage(pBt, newPgno, &pNewPage, pCur->pPage);
drh6019e162001-07-02 17:51:45 +00003367 if( rc ) return rc;
drh428ae8c2003-01-04 16:48:09 +00003368 pNewPage->idxParent = pCur->idx;
drh3aac2dd2004-04-26 14:10:20 +00003369 pOldPage = pCur->pPage;
3370 pOldPage->idxShift = 0;
3371 releasePage(pOldPage);
drh72f82862001-05-24 21:06:34 +00003372 pCur->pPage = pNewPage;
3373 pCur->idx = 0;
drh271efa52004-05-30 19:19:05 +00003374 pCur->info.nSize = 0;
drha2c20e42008-03-29 16:01:04 +00003375 pCur->validNKey = 0;
drh4be295b2003-12-16 03:44:47 +00003376 if( pNewPage->nCell<1 ){
drh49285702005-09-17 15:20:26 +00003377 return SQLITE_CORRUPT_BKPT;
drh4be295b2003-12-16 03:44:47 +00003378 }
drh72f82862001-05-24 21:06:34 +00003379 return SQLITE_OK;
3380}
3381
3382/*
drh8856d6a2004-04-29 14:42:46 +00003383** Return true if the page is the virtual root of its table.
3384**
3385** The virtual root page is the root page for most tables. But
3386** for the table rooted on page 1, sometime the real root page
3387** is empty except for the right-pointer. In such cases the
3388** virtual root page is the page that the right-pointer of page
3389** 1 is pointing to.
3390*/
drh16a9b832007-05-05 18:39:25 +00003391int sqlite3BtreeIsRootPage(MemPage *pPage){
drhd677b3d2007-08-20 22:48:41 +00003392 MemPage *pParent;
3393
drh1fee73e2007-08-29 04:00:57 +00003394 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
drhd677b3d2007-08-20 22:48:41 +00003395 pParent = pPage->pParent;
drhda200cc2004-05-09 11:51:38 +00003396 if( pParent==0 ) return 1;
3397 if( pParent->pgno>1 ) return 0;
3398 if( get2byte(&pParent->aData[pParent->hdrOffset+3])==0 ) return 1;
drh8856d6a2004-04-29 14:42:46 +00003399 return 0;
3400}
3401
3402/*
drh5e2f8b92001-05-28 00:41:15 +00003403** Move the cursor up to the parent page.
3404**
3405** pCur->idx is set to the cell index that contains the pointer
3406** to the page we are coming from. If we are coming from the
3407** right-most child page then pCur->idx is set to one more than
drhbd03cae2001-06-02 02:40:57 +00003408** the largest cell index.
drh72f82862001-05-24 21:06:34 +00003409*/
drh16a9b832007-05-05 18:39:25 +00003410void sqlite3BtreeMoveToParent(BtCursor *pCur){
drh72f82862001-05-24 21:06:34 +00003411 MemPage *pParent;
drh8178a752003-01-05 21:41:40 +00003412 MemPage *pPage;
drh428ae8c2003-01-04 16:48:09 +00003413 int idxParent;
drh3aac2dd2004-04-26 14:10:20 +00003414
drh1fee73e2007-08-29 04:00:57 +00003415 assert( cursorHoldsMutex(pCur) );
danielk1977da184232006-01-05 11:34:32 +00003416 assert( pCur->eState==CURSOR_VALID );
drh8178a752003-01-05 21:41:40 +00003417 pPage = pCur->pPage;
3418 assert( pPage!=0 );
drh16a9b832007-05-05 18:39:25 +00003419 assert( !sqlite3BtreeIsRootPage(pPage) );
drh8178a752003-01-05 21:41:40 +00003420 pParent = pPage->pParent;
3421 assert( pParent!=0 );
3422 idxParent = pPage->idxParent;
danielk19773b8a05f2007-03-19 17:44:26 +00003423 sqlite3PagerRef(pParent->pDbPage);
drh3aac2dd2004-04-26 14:10:20 +00003424 releasePage(pPage);
drh72f82862001-05-24 21:06:34 +00003425 pCur->pPage = pParent;
drh271efa52004-05-30 19:19:05 +00003426 pCur->info.nSize = 0;
drha2c20e42008-03-29 16:01:04 +00003427 pCur->validNKey = 0;
drh428ae8c2003-01-04 16:48:09 +00003428 assert( pParent->idxShift==0 );
drh43605152004-05-29 21:46:49 +00003429 pCur->idx = idxParent;
drh72f82862001-05-24 21:06:34 +00003430}
3431
3432/*
3433** Move the cursor to the root page
3434*/
drh5e2f8b92001-05-28 00:41:15 +00003435static int moveToRoot(BtCursor *pCur){
drh3aac2dd2004-04-26 14:10:20 +00003436 MemPage *pRoot;
drh777e4c42006-01-13 04:31:58 +00003437 int rc = SQLITE_OK;
drhd677b3d2007-08-20 22:48:41 +00003438 Btree *p = pCur->pBtree;
3439 BtShared *pBt = p->pBt;
drhbd03cae2001-06-02 02:40:57 +00003440
drh1fee73e2007-08-29 04:00:57 +00003441 assert( cursorHoldsMutex(pCur) );
drhfb982642007-08-30 01:19:59 +00003442 assert( CURSOR_INVALID < CURSOR_REQUIRESEEK );
3443 assert( CURSOR_VALID < CURSOR_REQUIRESEEK );
3444 assert( CURSOR_FAULT > CURSOR_REQUIRESEEK );
3445 if( pCur->eState>=CURSOR_REQUIRESEEK ){
3446 if( pCur->eState==CURSOR_FAULT ){
3447 return pCur->skip;
3448 }
drhbf700f32007-03-31 02:36:44 +00003449 clearCursorPosition(pCur);
3450 }
drh777e4c42006-01-13 04:31:58 +00003451 pRoot = pCur->pPage;
danielk197797a227c2006-01-20 16:32:04 +00003452 if( pRoot && pRoot->pgno==pCur->pgnoRoot ){
drh777e4c42006-01-13 04:31:58 +00003453 assert( pRoot->isInit );
3454 }else{
3455 if(
3456 SQLITE_OK!=(rc = getAndInitPage(pBt, pCur->pgnoRoot, &pRoot, 0))
3457 ){
3458 pCur->eState = CURSOR_INVALID;
3459 return rc;
3460 }
3461 releasePage(pCur->pPage);
drh777e4c42006-01-13 04:31:58 +00003462 pCur->pPage = pRoot;
drhc39e0002004-05-07 23:50:57 +00003463 }
drh72f82862001-05-24 21:06:34 +00003464 pCur->idx = 0;
drh271efa52004-05-30 19:19:05 +00003465 pCur->info.nSize = 0;
drha2c20e42008-03-29 16:01:04 +00003466 pCur->atLast = 0;
3467 pCur->validNKey = 0;
drh8856d6a2004-04-29 14:42:46 +00003468 if( pRoot->nCell==0 && !pRoot->leaf ){
3469 Pgno subpage;
3470 assert( pRoot->pgno==1 );
drh43605152004-05-29 21:46:49 +00003471 subpage = get4byte(&pRoot->aData[pRoot->hdrOffset+8]);
drh8856d6a2004-04-29 14:42:46 +00003472 assert( subpage>0 );
danielk1977da184232006-01-05 11:34:32 +00003473 pCur->eState = CURSOR_VALID;
drh4b70f112004-05-02 21:12:19 +00003474 rc = moveToChild(pCur, subpage);
drh8856d6a2004-04-29 14:42:46 +00003475 }
danielk1977da184232006-01-05 11:34:32 +00003476 pCur->eState = ((pCur->pPage->nCell>0)?CURSOR_VALID:CURSOR_INVALID);
drh8856d6a2004-04-29 14:42:46 +00003477 return rc;
drh72f82862001-05-24 21:06:34 +00003478}
drh2af926b2001-05-15 00:39:25 +00003479
drh5e2f8b92001-05-28 00:41:15 +00003480/*
3481** Move the cursor down to the left-most leaf entry beneath the
3482** entry to which it is currently pointing.
drh777e4c42006-01-13 04:31:58 +00003483**
3484** The left-most leaf is the one with the smallest key - the first
3485** in ascending order.
drh5e2f8b92001-05-28 00:41:15 +00003486*/
3487static int moveToLeftmost(BtCursor *pCur){
3488 Pgno pgno;
drhd677b3d2007-08-20 22:48:41 +00003489 int rc = SQLITE_OK;
drh3aac2dd2004-04-26 14:10:20 +00003490 MemPage *pPage;
drh5e2f8b92001-05-28 00:41:15 +00003491
drh1fee73e2007-08-29 04:00:57 +00003492 assert( cursorHoldsMutex(pCur) );
danielk1977da184232006-01-05 11:34:32 +00003493 assert( pCur->eState==CURSOR_VALID );
drhd677b3d2007-08-20 22:48:41 +00003494 while( rc==SQLITE_OK && !(pPage = pCur->pPage)->leaf ){
drha34b6762004-05-07 13:30:42 +00003495 assert( pCur->idx>=0 && pCur->idx<pPage->nCell );
danielk19771cc5ed82007-05-16 17:28:43 +00003496 pgno = get4byte(findCell(pPage, pCur->idx));
drh8178a752003-01-05 21:41:40 +00003497 rc = moveToChild(pCur, pgno);
drh5e2f8b92001-05-28 00:41:15 +00003498 }
drhd677b3d2007-08-20 22:48:41 +00003499 return rc;
drh5e2f8b92001-05-28 00:41:15 +00003500}
3501
drh2dcc9aa2002-12-04 13:40:25 +00003502/*
3503** Move the cursor down to the right-most leaf entry beneath the
3504** page to which it is currently pointing. Notice the difference
3505** between moveToLeftmost() and moveToRightmost(). moveToLeftmost()
3506** finds the left-most entry beneath the *entry* whereas moveToRightmost()
3507** finds the right-most entry beneath the *page*.
drh777e4c42006-01-13 04:31:58 +00003508**
3509** The right-most entry is the one with the largest key - the last
3510** key in ascending order.
drh2dcc9aa2002-12-04 13:40:25 +00003511*/
3512static int moveToRightmost(BtCursor *pCur){
3513 Pgno pgno;
drhd677b3d2007-08-20 22:48:41 +00003514 int rc = SQLITE_OK;
drh3aac2dd2004-04-26 14:10:20 +00003515 MemPage *pPage;
drh2dcc9aa2002-12-04 13:40:25 +00003516
drh1fee73e2007-08-29 04:00:57 +00003517 assert( cursorHoldsMutex(pCur) );
danielk1977da184232006-01-05 11:34:32 +00003518 assert( pCur->eState==CURSOR_VALID );
drhd677b3d2007-08-20 22:48:41 +00003519 while( rc==SQLITE_OK && !(pPage = pCur->pPage)->leaf ){
drh43605152004-05-29 21:46:49 +00003520 pgno = get4byte(&pPage->aData[pPage->hdrOffset+8]);
drh3aac2dd2004-04-26 14:10:20 +00003521 pCur->idx = pPage->nCell;
drh8178a752003-01-05 21:41:40 +00003522 rc = moveToChild(pCur, pgno);
drh2dcc9aa2002-12-04 13:40:25 +00003523 }
drhd677b3d2007-08-20 22:48:41 +00003524 if( rc==SQLITE_OK ){
3525 pCur->idx = pPage->nCell - 1;
3526 pCur->info.nSize = 0;
drha2c20e42008-03-29 16:01:04 +00003527 pCur->validNKey = 0;
drhd677b3d2007-08-20 22:48:41 +00003528 }
drh2dcc9aa2002-12-04 13:40:25 +00003529 return SQLITE_OK;
3530}
3531
drh5e00f6c2001-09-13 13:46:56 +00003532/* Move the cursor to the first entry in the table. Return SQLITE_OK
3533** on success. Set *pRes to 0 if the cursor actually points to something
drh77c679c2002-02-19 22:43:58 +00003534** or set *pRes to 1 if the table is empty.
drh5e00f6c2001-09-13 13:46:56 +00003535*/
drh3aac2dd2004-04-26 14:10:20 +00003536int sqlite3BtreeFirst(BtCursor *pCur, int *pRes){
drh5e00f6c2001-09-13 13:46:56 +00003537 int rc;
drhd677b3d2007-08-20 22:48:41 +00003538
drh1fee73e2007-08-29 04:00:57 +00003539 assert( cursorHoldsMutex(pCur) );
drhe5fe6902007-12-07 18:55:28 +00003540 assert( sqlite3_mutex_held(pCur->pBtree->db->mutex) );
drh5e00f6c2001-09-13 13:46:56 +00003541 rc = moveToRoot(pCur);
drhd677b3d2007-08-20 22:48:41 +00003542 if( rc==SQLITE_OK ){
3543 if( pCur->eState==CURSOR_INVALID ){
3544 assert( pCur->pPage->nCell==0 );
3545 *pRes = 1;
3546 rc = SQLITE_OK;
3547 }else{
3548 assert( pCur->pPage->nCell>0 );
3549 *pRes = 0;
3550 rc = moveToLeftmost(pCur);
3551 }
drh5e00f6c2001-09-13 13:46:56 +00003552 }
drh5e00f6c2001-09-13 13:46:56 +00003553 return rc;
3554}
drh5e2f8b92001-05-28 00:41:15 +00003555
drh9562b552002-02-19 15:00:07 +00003556/* Move the cursor to the last entry in the table. Return SQLITE_OK
3557** on success. Set *pRes to 0 if the cursor actually points to something
drh77c679c2002-02-19 22:43:58 +00003558** or set *pRes to 1 if the table is empty.
drh9562b552002-02-19 15:00:07 +00003559*/
drh3aac2dd2004-04-26 14:10:20 +00003560int sqlite3BtreeLast(BtCursor *pCur, int *pRes){
drh9562b552002-02-19 15:00:07 +00003561 int rc;
drhd677b3d2007-08-20 22:48:41 +00003562
drh1fee73e2007-08-29 04:00:57 +00003563 assert( cursorHoldsMutex(pCur) );
drhe5fe6902007-12-07 18:55:28 +00003564 assert( sqlite3_mutex_held(pCur->pBtree->db->mutex) );
drh9562b552002-02-19 15:00:07 +00003565 rc = moveToRoot(pCur);
drhd677b3d2007-08-20 22:48:41 +00003566 if( rc==SQLITE_OK ){
3567 if( CURSOR_INVALID==pCur->eState ){
3568 assert( pCur->pPage->nCell==0 );
3569 *pRes = 1;
3570 }else{
3571 assert( pCur->eState==CURSOR_VALID );
3572 *pRes = 0;
3573 rc = moveToRightmost(pCur);
drha2c20e42008-03-29 16:01:04 +00003574 getCellInfo(pCur);
3575 pCur->atLast = rc==SQLITE_OK;
drhd677b3d2007-08-20 22:48:41 +00003576 }
drh9562b552002-02-19 15:00:07 +00003577 }
drh9562b552002-02-19 15:00:07 +00003578 return rc;
3579}
3580
drhe14006d2008-03-25 17:23:32 +00003581/* Move the cursor so that it points to an entry near the key
3582** specified by pKey/nKey/pUnKey. Return a success code.
drh72f82862001-05-24 21:06:34 +00003583**
drhe14006d2008-03-25 17:23:32 +00003584** For INTKEY tables, only the nKey parameter is used. pKey
3585** and pUnKey must be NULL. For index tables, either pUnKey
3586** must point to a key that has already been unpacked, or else
3587** pKey/nKey describes a blob containing the key.
drh3aac2dd2004-04-26 14:10:20 +00003588**
drh5e2f8b92001-05-28 00:41:15 +00003589** If an exact match is not found, then the cursor is always
drhbd03cae2001-06-02 02:40:57 +00003590** left pointing at a leaf page which would hold the entry if it
drh5e2f8b92001-05-28 00:41:15 +00003591** were present. The cursor might point to an entry that comes
3592** before or after the key.
3593**
drhbd03cae2001-06-02 02:40:57 +00003594** The result of comparing the key with the entry to which the
drhab01f612004-05-22 02:55:23 +00003595** cursor is written to *pRes if pRes!=NULL. The meaning of
drhbd03cae2001-06-02 02:40:57 +00003596** this value is as follows:
3597**
3598** *pRes<0 The cursor is left pointing at an entry that
drh1a844c32002-12-04 22:29:28 +00003599** is smaller than pKey or if the table is empty
3600** and the cursor is therefore left point to nothing.
drhbd03cae2001-06-02 02:40:57 +00003601**
3602** *pRes==0 The cursor is left pointing at an entry that
3603** exactly matches pKey.
3604**
3605** *pRes>0 The cursor is left pointing at an entry that
drh7c717f72001-06-24 20:39:41 +00003606** is larger than pKey.
drhd677b3d2007-08-20 22:48:41 +00003607**
drha059ad02001-04-17 20:09:11 +00003608*/
drhe4d90812007-03-29 05:51:49 +00003609int sqlite3BtreeMoveto(
3610 BtCursor *pCur, /* The cursor to be moved */
3611 const void *pKey, /* The key content for indices. Not used by tables */
drhe14006d2008-03-25 17:23:32 +00003612 UnpackedRecord *pUnKey,/* Unpacked version of pKey */
drhe4d90812007-03-29 05:51:49 +00003613 i64 nKey, /* Size of pKey. Or the key for tables */
3614 int biasRight, /* If true, bias the search to the high end */
3615 int *pRes /* Search result flag */
3616){
drh72f82862001-05-24 21:06:34 +00003617 int rc;
drh1e968a02008-03-25 00:22:21 +00003618 char aSpace[200];
drhd677b3d2007-08-20 22:48:41 +00003619
drh1fee73e2007-08-29 04:00:57 +00003620 assert( cursorHoldsMutex(pCur) );
drhe5fe6902007-12-07 18:55:28 +00003621 assert( sqlite3_mutex_held(pCur->pBtree->db->mutex) );
drha2c20e42008-03-29 16:01:04 +00003622
3623 /* If the cursor is already positioned at the point we are trying
3624 ** to move to, then just return without doing any work */
3625 if( pCur->eState==CURSOR_VALID && pCur->validNKey && pCur->pPage->intKey ){
3626 if( pCur->info.nKey==nKey ){
3627 *pRes = 0;
3628 return SQLITE_OK;
3629 }
3630 if( pCur->atLast && pCur->info.nKey<nKey ){
3631 *pRes = -1;
3632 return SQLITE_OK;
3633 }
3634 }
3635
3636
drh5e2f8b92001-05-28 00:41:15 +00003637 rc = moveToRoot(pCur);
drhd677b3d2007-08-20 22:48:41 +00003638 if( rc ){
3639 return rc;
3640 }
drhc39e0002004-05-07 23:50:57 +00003641 assert( pCur->pPage );
3642 assert( pCur->pPage->isInit );
danielk1977da184232006-01-05 11:34:32 +00003643 if( pCur->eState==CURSOR_INVALID ){
drhf328bc82004-05-10 23:29:49 +00003644 *pRes = -1;
drhc39e0002004-05-07 23:50:57 +00003645 assert( pCur->pPage->nCell==0 );
3646 return SQLITE_OK;
3647 }
drh1e968a02008-03-25 00:22:21 +00003648 if( pCur->pPage->intKey ){
drhe14006d2008-03-25 17:23:32 +00003649 /* We are given an SQL table to search. The key is the integer
3650 ** rowid contained in nKey. pKey and pUnKey should both be NULL */
3651 assert( pUnKey==0 );
3652 assert( pKey==0 );
3653 }else if( pUnKey==0 ){
3654 /* We are to search an SQL index using a key encoded as a blob.
3655 ** The blob is found at pKey and is nKey bytes in length. Unpack
3656 ** this key so that we can use it. */
3657 assert( pKey!=0 );
3658 pUnKey = sqlite3VdbeRecordUnpack(pCur->pKeyInfo, nKey, pKey,
drh1e968a02008-03-25 00:22:21 +00003659 aSpace, sizeof(aSpace));
drhe14006d2008-03-25 17:23:32 +00003660 if( pUnKey==0 ) return SQLITE_NOMEM;
3661 }else{
3662 /* We are to search an SQL index using a key that is already unpacked
3663 ** and handed to us in pUnKey. */
3664 assert( pKey==0 );
drh1e968a02008-03-25 00:22:21 +00003665 }
drh14684382006-11-30 13:05:29 +00003666 for(;;){
drh72f82862001-05-24 21:06:34 +00003667 int lwr, upr;
3668 Pgno chldPg;
3669 MemPage *pPage = pCur->pPage;
drh1a844c32002-12-04 22:29:28 +00003670 int c = -1; /* pRes return if table is empty must be -1 */
drh72f82862001-05-24 21:06:34 +00003671 lwr = 0;
3672 upr = pPage->nCell-1;
drhe14006d2008-03-25 17:23:32 +00003673 if( !pPage->intKey && pUnKey==0 ){
drh1e968a02008-03-25 00:22:21 +00003674 rc = SQLITE_CORRUPT_BKPT;
3675 goto moveto_finish;
drh4eec4c12005-01-21 00:22:37 +00003676 }
drhe4d90812007-03-29 05:51:49 +00003677 if( biasRight ){
3678 pCur->idx = upr;
3679 }else{
3680 pCur->idx = (upr+lwr)/2;
3681 }
drhf1d68b32007-03-29 04:43:26 +00003682 if( lwr<=upr ) for(;;){
danielk197713adf8a2004-06-03 16:08:41 +00003683 void *pCellKey;
drh4a1c3802004-05-12 15:15:47 +00003684 i64 nCellKey;
drh366fda62006-01-13 02:35:09 +00003685 pCur->info.nSize = 0;
drha2c20e42008-03-29 16:01:04 +00003686 pCur->validNKey = 1;
drh3aac2dd2004-04-26 14:10:20 +00003687 if( pPage->intKey ){
drh777e4c42006-01-13 04:31:58 +00003688 u8 *pCell;
danielk19771cc5ed82007-05-16 17:28:43 +00003689 pCell = findCell(pPage, pCur->idx) + pPage->childPtrSize;
drhd172f862006-01-12 15:01:15 +00003690 if( pPage->hasData ){
danielk1977bab45c62006-01-16 15:14:27 +00003691 u32 dummy;
shane3f8d5cf2008-04-24 19:15:09 +00003692 pCell += getVarint32(pCell, dummy);
drhd172f862006-01-12 15:01:15 +00003693 }
drha2c20e42008-03-29 16:01:04 +00003694 getVarint(pCell, (u64*)&nCellKey);
drh41eb9e92008-04-02 18:33:07 +00003695 if( nCellKey==nKey ){
drh3aac2dd2004-04-26 14:10:20 +00003696 c = 0;
drh41eb9e92008-04-02 18:33:07 +00003697 }else if( nCellKey<nKey ){
3698 c = -1;
3699 }else{
3700 assert( nCellKey>nKey );
3701 c = +1;
drh3aac2dd2004-04-26 14:10:20 +00003702 }
drh3aac2dd2004-04-26 14:10:20 +00003703 }else{
drhe51c44f2004-05-30 20:46:09 +00003704 int available;
danielk197713adf8a2004-06-03 16:08:41 +00003705 pCellKey = (void *)fetchPayload(pCur, &available, 0);
drh366fda62006-01-13 02:35:09 +00003706 nCellKey = pCur->info.nKey;
drhe51c44f2004-05-30 20:46:09 +00003707 if( available>=nCellKey ){
drhe14006d2008-03-25 17:23:32 +00003708 c = sqlite3VdbeRecordCompare(nCellKey, pCellKey, pUnKey);
drhe51c44f2004-05-30 20:46:09 +00003709 }else{
drh17435752007-08-16 04:30:38 +00003710 pCellKey = sqlite3_malloc( nCellKey );
danielk19776507ecb2008-03-25 09:56:44 +00003711 if( pCellKey==0 ){
3712 rc = SQLITE_NOMEM;
3713 goto moveto_finish;
3714 }
danielk197713adf8a2004-06-03 16:08:41 +00003715 rc = sqlite3BtreeKey(pCur, 0, nCellKey, (void *)pCellKey);
drhe14006d2008-03-25 17:23:32 +00003716 c = sqlite3VdbeRecordCompare(nCellKey, pCellKey, pUnKey);
drh17435752007-08-16 04:30:38 +00003717 sqlite3_free(pCellKey);
drh1e968a02008-03-25 00:22:21 +00003718 if( rc ) goto moveto_finish;
drhe51c44f2004-05-30 20:46:09 +00003719 }
drh3aac2dd2004-04-26 14:10:20 +00003720 }
drh72f82862001-05-24 21:06:34 +00003721 if( c==0 ){
drha2c20e42008-03-29 16:01:04 +00003722 pCur->info.nKey = nCellKey;
drh8b18dd42004-05-12 19:18:15 +00003723 if( pPage->leafData && !pPage->leaf ){
drhfc70e6f2004-05-12 21:11:27 +00003724 lwr = pCur->idx;
3725 upr = lwr - 1;
drh8b18dd42004-05-12 19:18:15 +00003726 break;
3727 }else{
drh8b18dd42004-05-12 19:18:15 +00003728 if( pRes ) *pRes = 0;
drh1e968a02008-03-25 00:22:21 +00003729 rc = SQLITE_OK;
3730 goto moveto_finish;
drh8b18dd42004-05-12 19:18:15 +00003731 }
drh72f82862001-05-24 21:06:34 +00003732 }
3733 if( c<0 ){
3734 lwr = pCur->idx+1;
3735 }else{
3736 upr = pCur->idx-1;
3737 }
drhf1d68b32007-03-29 04:43:26 +00003738 if( lwr>upr ){
drha2c20e42008-03-29 16:01:04 +00003739 pCur->info.nKey = nCellKey;
drhf1d68b32007-03-29 04:43:26 +00003740 break;
3741 }
3742 pCur->idx = (lwr+upr)/2;
drh72f82862001-05-24 21:06:34 +00003743 }
3744 assert( lwr==upr+1 );
drh7aa128d2002-06-21 13:09:16 +00003745 assert( pPage->isInit );
drh3aac2dd2004-04-26 14:10:20 +00003746 if( pPage->leaf ){
drha34b6762004-05-07 13:30:42 +00003747 chldPg = 0;
drh3aac2dd2004-04-26 14:10:20 +00003748 }else if( lwr>=pPage->nCell ){
drh43605152004-05-29 21:46:49 +00003749 chldPg = get4byte(&pPage->aData[pPage->hdrOffset+8]);
drh72f82862001-05-24 21:06:34 +00003750 }else{
danielk19771cc5ed82007-05-16 17:28:43 +00003751 chldPg = get4byte(findCell(pPage, lwr));
drh72f82862001-05-24 21:06:34 +00003752 }
3753 if( chldPg==0 ){
drhc39e0002004-05-07 23:50:57 +00003754 assert( pCur->idx>=0 && pCur->idx<pCur->pPage->nCell );
drh72f82862001-05-24 21:06:34 +00003755 if( pRes ) *pRes = c;
drh1e968a02008-03-25 00:22:21 +00003756 rc = SQLITE_OK;
3757 goto moveto_finish;
drh72f82862001-05-24 21:06:34 +00003758 }
drh428ae8c2003-01-04 16:48:09 +00003759 pCur->idx = lwr;
drh271efa52004-05-30 19:19:05 +00003760 pCur->info.nSize = 0;
drha2c20e42008-03-29 16:01:04 +00003761 pCur->validNKey = 0;
drh8178a752003-01-05 21:41:40 +00003762 rc = moveToChild(pCur, chldPg);
drh1e968a02008-03-25 00:22:21 +00003763 if( rc ) goto moveto_finish;
drh72f82862001-05-24 21:06:34 +00003764 }
drh1e968a02008-03-25 00:22:21 +00003765moveto_finish:
drhe14006d2008-03-25 17:23:32 +00003766 if( pKey ){
3767 /* If we created our own unpacked key at the top of this
3768 ** procedure, then destroy that key before returning. */
3769 sqlite3VdbeDeleteUnpackedRecord(pUnKey);
3770 }
drh1e968a02008-03-25 00:22:21 +00003771 return rc;
drh72f82862001-05-24 21:06:34 +00003772}
3773
drhd677b3d2007-08-20 22:48:41 +00003774
drh72f82862001-05-24 21:06:34 +00003775/*
drhc39e0002004-05-07 23:50:57 +00003776** Return TRUE if the cursor is not pointing at an entry of the table.
3777**
3778** TRUE will be returned after a call to sqlite3BtreeNext() moves
3779** past the last entry in the table or sqlite3BtreePrev() moves past
3780** the first entry. TRUE is also returned if the table is empty.
3781*/
3782int sqlite3BtreeEof(BtCursor *pCur){
danielk1977da184232006-01-05 11:34:32 +00003783 /* TODO: What if the cursor is in CURSOR_REQUIRESEEK but all table entries
3784 ** have been deleted? This API will need to change to return an error code
3785 ** as well as the boolean result value.
3786 */
3787 return (CURSOR_VALID!=pCur->eState);
drhc39e0002004-05-07 23:50:57 +00003788}
3789
3790/*
drhb21c8cd2007-08-21 19:33:56 +00003791** Return the database connection handle for a cursor.
3792*/
3793sqlite3 *sqlite3BtreeCursorDb(const BtCursor *pCur){
drhe5fe6902007-12-07 18:55:28 +00003794 assert( sqlite3_mutex_held(pCur->pBtree->db->mutex) );
3795 return pCur->pBtree->db;
drhb21c8cd2007-08-21 19:33:56 +00003796}
3797
3798/*
drhbd03cae2001-06-02 02:40:57 +00003799** Advance the cursor to the next entry in the database. If
drh8c1238a2003-01-02 14:43:55 +00003800** successful then set *pRes=0. If the cursor
drhbd03cae2001-06-02 02:40:57 +00003801** was already pointing to the last entry in the database before
drh8c1238a2003-01-02 14:43:55 +00003802** this routine was called, then set *pRes=1.
drh72f82862001-05-24 21:06:34 +00003803*/
drhd094db12008-04-03 21:46:57 +00003804int sqlite3BtreeNext(BtCursor *pCur, int *pRes){
drh72f82862001-05-24 21:06:34 +00003805 int rc;
danielk197797a227c2006-01-20 16:32:04 +00003806 MemPage *pPage;
drh8b18dd42004-05-12 19:18:15 +00003807
drh1fee73e2007-08-29 04:00:57 +00003808 assert( cursorHoldsMutex(pCur) );
drhbf700f32007-03-31 02:36:44 +00003809 rc = restoreOrClearCursorPosition(pCur);
danielk1977da184232006-01-05 11:34:32 +00003810 if( rc!=SQLITE_OK ){
3811 return rc;
3812 }
drh8c4d3a62007-04-06 01:03:32 +00003813 assert( pRes!=0 );
3814 pPage = pCur->pPage;
3815 if( CURSOR_INVALID==pCur->eState ){
3816 *pRes = 1;
3817 return SQLITE_OK;
3818 }
danielk1977da184232006-01-05 11:34:32 +00003819 if( pCur->skip>0 ){
3820 pCur->skip = 0;
3821 *pRes = 0;
3822 return SQLITE_OK;
3823 }
3824 pCur->skip = 0;
danielk1977da184232006-01-05 11:34:32 +00003825
drh8178a752003-01-05 21:41:40 +00003826 assert( pPage->isInit );
drh8178a752003-01-05 21:41:40 +00003827 assert( pCur->idx<pPage->nCell );
danielk19776a43f9b2004-11-16 04:57:24 +00003828
drh72f82862001-05-24 21:06:34 +00003829 pCur->idx++;
drh271efa52004-05-30 19:19:05 +00003830 pCur->info.nSize = 0;
drha2c20e42008-03-29 16:01:04 +00003831 pCur->validNKey = 0;
drh8178a752003-01-05 21:41:40 +00003832 if( pCur->idx>=pPage->nCell ){
drha34b6762004-05-07 13:30:42 +00003833 if( !pPage->leaf ){
drh43605152004-05-29 21:46:49 +00003834 rc = moveToChild(pCur, get4byte(&pPage->aData[pPage->hdrOffset+8]));
drh5e2f8b92001-05-28 00:41:15 +00003835 if( rc ) return rc;
3836 rc = moveToLeftmost(pCur);
drh8c1238a2003-01-02 14:43:55 +00003837 *pRes = 0;
3838 return rc;
drh72f82862001-05-24 21:06:34 +00003839 }
drh5e2f8b92001-05-28 00:41:15 +00003840 do{
drh16a9b832007-05-05 18:39:25 +00003841 if( sqlite3BtreeIsRootPage(pPage) ){
drh8c1238a2003-01-02 14:43:55 +00003842 *pRes = 1;
danielk1977da184232006-01-05 11:34:32 +00003843 pCur->eState = CURSOR_INVALID;
drh5e2f8b92001-05-28 00:41:15 +00003844 return SQLITE_OK;
3845 }
drh16a9b832007-05-05 18:39:25 +00003846 sqlite3BtreeMoveToParent(pCur);
drh8178a752003-01-05 21:41:40 +00003847 pPage = pCur->pPage;
3848 }while( pCur->idx>=pPage->nCell );
drh8c1238a2003-01-02 14:43:55 +00003849 *pRes = 0;
drh8b18dd42004-05-12 19:18:15 +00003850 if( pPage->leafData ){
3851 rc = sqlite3BtreeNext(pCur, pRes);
3852 }else{
3853 rc = SQLITE_OK;
3854 }
3855 return rc;
drh8178a752003-01-05 21:41:40 +00003856 }
3857 *pRes = 0;
drh3aac2dd2004-04-26 14:10:20 +00003858 if( pPage->leaf ){
drh8178a752003-01-05 21:41:40 +00003859 return SQLITE_OK;
drh72f82862001-05-24 21:06:34 +00003860 }
drh5e2f8b92001-05-28 00:41:15 +00003861 rc = moveToLeftmost(pCur);
drh8c1238a2003-01-02 14:43:55 +00003862 return rc;
drh72f82862001-05-24 21:06:34 +00003863}
drhd677b3d2007-08-20 22:48:41 +00003864
drh72f82862001-05-24 21:06:34 +00003865
drh3b7511c2001-05-26 13:15:44 +00003866/*
drh2dcc9aa2002-12-04 13:40:25 +00003867** Step the cursor to the back to the previous entry in the database. If
drh8178a752003-01-05 21:41:40 +00003868** successful then set *pRes=0. If the cursor
drh2dcc9aa2002-12-04 13:40:25 +00003869** was already pointing to the first entry in the database before
drh8178a752003-01-05 21:41:40 +00003870** this routine was called, then set *pRes=1.
drh2dcc9aa2002-12-04 13:40:25 +00003871*/
drhd094db12008-04-03 21:46:57 +00003872int sqlite3BtreePrevious(BtCursor *pCur, int *pRes){
drh2dcc9aa2002-12-04 13:40:25 +00003873 int rc;
3874 Pgno pgno;
drh8178a752003-01-05 21:41:40 +00003875 MemPage *pPage;
danielk1977da184232006-01-05 11:34:32 +00003876
drh1fee73e2007-08-29 04:00:57 +00003877 assert( cursorHoldsMutex(pCur) );
drhbf700f32007-03-31 02:36:44 +00003878 rc = restoreOrClearCursorPosition(pCur);
danielk1977da184232006-01-05 11:34:32 +00003879 if( rc!=SQLITE_OK ){
3880 return rc;
3881 }
drha2c20e42008-03-29 16:01:04 +00003882 pCur->atLast = 0;
drh8c4d3a62007-04-06 01:03:32 +00003883 if( CURSOR_INVALID==pCur->eState ){
3884 *pRes = 1;
3885 return SQLITE_OK;
3886 }
danielk1977da184232006-01-05 11:34:32 +00003887 if( pCur->skip<0 ){
3888 pCur->skip = 0;
3889 *pRes = 0;
3890 return SQLITE_OK;
3891 }
3892 pCur->skip = 0;
danielk1977da184232006-01-05 11:34:32 +00003893
drh8178a752003-01-05 21:41:40 +00003894 pPage = pCur->pPage;
drh8178a752003-01-05 21:41:40 +00003895 assert( pPage->isInit );
drh2dcc9aa2002-12-04 13:40:25 +00003896 assert( pCur->idx>=0 );
drha34b6762004-05-07 13:30:42 +00003897 if( !pPage->leaf ){
danielk19771cc5ed82007-05-16 17:28:43 +00003898 pgno = get4byte( findCell(pPage, pCur->idx) );
drh8178a752003-01-05 21:41:40 +00003899 rc = moveToChild(pCur, pgno);
drhd677b3d2007-08-20 22:48:41 +00003900 if( rc ){
3901 return rc;
3902 }
drh2dcc9aa2002-12-04 13:40:25 +00003903 rc = moveToRightmost(pCur);
3904 }else{
3905 while( pCur->idx==0 ){
drh16a9b832007-05-05 18:39:25 +00003906 if( sqlite3BtreeIsRootPage(pPage) ){
danielk1977da184232006-01-05 11:34:32 +00003907 pCur->eState = CURSOR_INVALID;
drhc39e0002004-05-07 23:50:57 +00003908 *pRes = 1;
drh2dcc9aa2002-12-04 13:40:25 +00003909 return SQLITE_OK;
3910 }
drh16a9b832007-05-05 18:39:25 +00003911 sqlite3BtreeMoveToParent(pCur);
drh8178a752003-01-05 21:41:40 +00003912 pPage = pCur->pPage;
drh2dcc9aa2002-12-04 13:40:25 +00003913 }
3914 pCur->idx--;
drh271efa52004-05-30 19:19:05 +00003915 pCur->info.nSize = 0;
drha2c20e42008-03-29 16:01:04 +00003916 pCur->validNKey = 0;
drh8237d452004-11-22 19:07:09 +00003917 if( pPage->leafData && !pPage->leaf ){
drh8b18dd42004-05-12 19:18:15 +00003918 rc = sqlite3BtreePrevious(pCur, pRes);
3919 }else{
3920 rc = SQLITE_OK;
3921 }
drh2dcc9aa2002-12-04 13:40:25 +00003922 }
drh8178a752003-01-05 21:41:40 +00003923 *pRes = 0;
drh2dcc9aa2002-12-04 13:40:25 +00003924 return rc;
3925}
3926
3927/*
drh3b7511c2001-05-26 13:15:44 +00003928** Allocate a new page from the database file.
3929**
danielk19773b8a05f2007-03-19 17:44:26 +00003930** The new page is marked as dirty. (In other words, sqlite3PagerWrite()
drh3b7511c2001-05-26 13:15:44 +00003931** has already been called on the new page.) The new page has also
3932** been referenced and the calling routine is responsible for calling
danielk19773b8a05f2007-03-19 17:44:26 +00003933** sqlite3PagerUnref() on the new page when it is done.
drh3b7511c2001-05-26 13:15:44 +00003934**
3935** SQLITE_OK is returned on success. Any other return value indicates
3936** an error. *ppPage and *pPgno are undefined in the event of an error.
danielk19773b8a05f2007-03-19 17:44:26 +00003937** Do not invoke sqlite3PagerUnref() on *ppPage if an error is returned.
drhbea00b92002-07-08 10:59:50 +00003938**
drh199e3cf2002-07-18 11:01:47 +00003939** If the "nearby" parameter is not 0, then a (feeble) effort is made to
3940** locate a page close to the page number "nearby". This can be used in an
drhbea00b92002-07-08 10:59:50 +00003941** attempt to keep related pages close to each other in the database file,
3942** which in turn can make database access faster.
danielk1977cb1a7eb2004-11-05 12:27:02 +00003943**
3944** If the "exact" parameter is not 0, and the page-number nearby exists
3945** anywhere on the free-list, then it is guarenteed to be returned. This
3946** is only used by auto-vacuum databases when allocating a new table.
drh3b7511c2001-05-26 13:15:44 +00003947*/
drh4f0c5872007-03-26 22:05:01 +00003948static int allocateBtreePage(
danielk1977aef0bf62005-12-30 16:28:01 +00003949 BtShared *pBt,
danielk1977cb1a7eb2004-11-05 12:27:02 +00003950 MemPage **ppPage,
3951 Pgno *pPgno,
3952 Pgno nearby,
3953 u8 exact
3954){
drh3aac2dd2004-04-26 14:10:20 +00003955 MemPage *pPage1;
drh8c42ca92001-06-22 19:15:00 +00003956 int rc;
drh3aac2dd2004-04-26 14:10:20 +00003957 int n; /* Number of pages on the freelist */
3958 int k; /* Number of leaves on the trunk of the freelist */
drhd3627af2006-12-18 18:34:51 +00003959 MemPage *pTrunk = 0;
3960 MemPage *pPrevTrunk = 0;
drh30e58752002-03-02 20:41:57 +00003961
drh1fee73e2007-08-29 04:00:57 +00003962 assert( sqlite3_mutex_held(pBt->mutex) );
drh3aac2dd2004-04-26 14:10:20 +00003963 pPage1 = pBt->pPage1;
3964 n = get4byte(&pPage1->aData[36]);
3965 if( n>0 ){
drh91025292004-05-03 19:49:32 +00003966 /* There are pages on the freelist. Reuse one of those pages. */
danielk1977cb1a7eb2004-11-05 12:27:02 +00003967 Pgno iTrunk;
danielk1977cb1a7eb2004-11-05 12:27:02 +00003968 u8 searchList = 0; /* If the free-list must be searched for 'nearby' */
3969
3970 /* If the 'exact' parameter was true and a query of the pointer-map
3971 ** shows that the page 'nearby' is somewhere on the free-list, then
3972 ** the entire-list will be searched for that page.
3973 */
3974#ifndef SQLITE_OMIT_AUTOVACUUM
danielk1977ad0132d2008-06-07 08:58:22 +00003975 if( exact && nearby<=pagerPagecount(pBt->pPager) ){
danielk1977cb1a7eb2004-11-05 12:27:02 +00003976 u8 eType;
3977 assert( nearby>0 );
3978 assert( pBt->autoVacuum );
3979 rc = ptrmapGet(pBt, nearby, &eType, 0);
3980 if( rc ) return rc;
3981 if( eType==PTRMAP_FREEPAGE ){
3982 searchList = 1;
3983 }
3984 *pPgno = nearby;
3985 }
3986#endif
3987
3988 /* Decrement the free-list count by 1. Set iTrunk to the index of the
3989 ** first free-list trunk page. iPrevTrunk is initially 1.
3990 */
danielk19773b8a05f2007-03-19 17:44:26 +00003991 rc = sqlite3PagerWrite(pPage1->pDbPage);
drh3b7511c2001-05-26 13:15:44 +00003992 if( rc ) return rc;
drh3aac2dd2004-04-26 14:10:20 +00003993 put4byte(&pPage1->aData[36], n-1);
danielk1977cb1a7eb2004-11-05 12:27:02 +00003994
3995 /* The code within this loop is run only once if the 'searchList' variable
3996 ** is not true. Otherwise, it runs once for each trunk-page on the
3997 ** free-list until the page 'nearby' is located.
3998 */
3999 do {
4000 pPrevTrunk = pTrunk;
4001 if( pPrevTrunk ){
4002 iTrunk = get4byte(&pPrevTrunk->aData[0]);
drhbea00b92002-07-08 10:59:50 +00004003 }else{
danielk1977cb1a7eb2004-11-05 12:27:02 +00004004 iTrunk = get4byte(&pPage1->aData[32]);
drhbea00b92002-07-08 10:59:50 +00004005 }
drh16a9b832007-05-05 18:39:25 +00004006 rc = sqlite3BtreeGetPage(pBt, iTrunk, &pTrunk, 0);
danielk1977cb1a7eb2004-11-05 12:27:02 +00004007 if( rc ){
drhd3627af2006-12-18 18:34:51 +00004008 pTrunk = 0;
4009 goto end_allocate_page;
danielk1977cb1a7eb2004-11-05 12:27:02 +00004010 }
4011
4012 k = get4byte(&pTrunk->aData[4]);
4013 if( k==0 && !searchList ){
4014 /* The trunk has no leaves and the list is not being searched.
4015 ** So extract the trunk page itself and use it as the newly
4016 ** allocated page */
4017 assert( pPrevTrunk==0 );
danielk19773b8a05f2007-03-19 17:44:26 +00004018 rc = sqlite3PagerWrite(pTrunk->pDbPage);
drhd3627af2006-12-18 18:34:51 +00004019 if( rc ){
4020 goto end_allocate_page;
4021 }
danielk1977cb1a7eb2004-11-05 12:27:02 +00004022 *pPgno = iTrunk;
4023 memcpy(&pPage1->aData[32], &pTrunk->aData[0], 4);
4024 *ppPage = pTrunk;
4025 pTrunk = 0;
4026 TRACE(("ALLOCATE: %d trunk - %d free pages left\n", *pPgno, n-1));
4027 }else if( k>pBt->usableSize/4 - 8 ){
4028 /* Value of k is out of range. Database corruption */
drhd3627af2006-12-18 18:34:51 +00004029 rc = SQLITE_CORRUPT_BKPT;
4030 goto end_allocate_page;
danielk1977cb1a7eb2004-11-05 12:27:02 +00004031#ifndef SQLITE_OMIT_AUTOVACUUM
4032 }else if( searchList && nearby==iTrunk ){
4033 /* The list is being searched and this trunk page is the page
4034 ** to allocate, regardless of whether it has leaves.
4035 */
4036 assert( *pPgno==iTrunk );
4037 *ppPage = pTrunk;
4038 searchList = 0;
danielk19773b8a05f2007-03-19 17:44:26 +00004039 rc = sqlite3PagerWrite(pTrunk->pDbPage);
drhd3627af2006-12-18 18:34:51 +00004040 if( rc ){
4041 goto end_allocate_page;
4042 }
danielk1977cb1a7eb2004-11-05 12:27:02 +00004043 if( k==0 ){
4044 if( !pPrevTrunk ){
4045 memcpy(&pPage1->aData[32], &pTrunk->aData[0], 4);
4046 }else{
4047 memcpy(&pPrevTrunk->aData[0], &pTrunk->aData[0], 4);
4048 }
4049 }else{
4050 /* The trunk page is required by the caller but it contains
4051 ** pointers to free-list leaves. The first leaf becomes a trunk
4052 ** page in this case.
4053 */
4054 MemPage *pNewTrunk;
4055 Pgno iNewTrunk = get4byte(&pTrunk->aData[8]);
drh16a9b832007-05-05 18:39:25 +00004056 rc = sqlite3BtreeGetPage(pBt, iNewTrunk, &pNewTrunk, 0);
danielk1977cb1a7eb2004-11-05 12:27:02 +00004057 if( rc!=SQLITE_OK ){
drhd3627af2006-12-18 18:34:51 +00004058 goto end_allocate_page;
danielk1977cb1a7eb2004-11-05 12:27:02 +00004059 }
danielk19773b8a05f2007-03-19 17:44:26 +00004060 rc = sqlite3PagerWrite(pNewTrunk->pDbPage);
danielk1977cb1a7eb2004-11-05 12:27:02 +00004061 if( rc!=SQLITE_OK ){
4062 releasePage(pNewTrunk);
drhd3627af2006-12-18 18:34:51 +00004063 goto end_allocate_page;
danielk1977cb1a7eb2004-11-05 12:27:02 +00004064 }
4065 memcpy(&pNewTrunk->aData[0], &pTrunk->aData[0], 4);
4066 put4byte(&pNewTrunk->aData[4], k-1);
4067 memcpy(&pNewTrunk->aData[8], &pTrunk->aData[12], (k-1)*4);
drhd3627af2006-12-18 18:34:51 +00004068 releasePage(pNewTrunk);
danielk1977cb1a7eb2004-11-05 12:27:02 +00004069 if( !pPrevTrunk ){
4070 put4byte(&pPage1->aData[32], iNewTrunk);
4071 }else{
danielk19773b8a05f2007-03-19 17:44:26 +00004072 rc = sqlite3PagerWrite(pPrevTrunk->pDbPage);
drhd3627af2006-12-18 18:34:51 +00004073 if( rc ){
4074 goto end_allocate_page;
4075 }
danielk1977cb1a7eb2004-11-05 12:27:02 +00004076 put4byte(&pPrevTrunk->aData[0], iNewTrunk);
4077 }
danielk1977cb1a7eb2004-11-05 12:27:02 +00004078 }
4079 pTrunk = 0;
4080 TRACE(("ALLOCATE: %d trunk - %d free pages left\n", *pPgno, n-1));
4081#endif
4082 }else{
4083 /* Extract a leaf from the trunk */
4084 int closest;
4085 Pgno iPage;
4086 unsigned char *aData = pTrunk->aData;
danielk19773b8a05f2007-03-19 17:44:26 +00004087 rc = sqlite3PagerWrite(pTrunk->pDbPage);
drhd3627af2006-12-18 18:34:51 +00004088 if( rc ){
4089 goto end_allocate_page;
4090 }
danielk1977cb1a7eb2004-11-05 12:27:02 +00004091 if( nearby>0 ){
4092 int i, dist;
4093 closest = 0;
4094 dist = get4byte(&aData[8]) - nearby;
4095 if( dist<0 ) dist = -dist;
4096 for(i=1; i<k; i++){
4097 int d2 = get4byte(&aData[8+i*4]) - nearby;
4098 if( d2<0 ) d2 = -d2;
4099 if( d2<dist ){
4100 closest = i;
4101 dist = d2;
4102 }
4103 }
4104 }else{
4105 closest = 0;
4106 }
4107
4108 iPage = get4byte(&aData[8+closest*4]);
4109 if( !searchList || iPage==nearby ){
4110 *pPgno = iPage;
danielk1977ad0132d2008-06-07 08:58:22 +00004111 int nPage;
4112 nPage = pagerPagecount(pBt->pPager);
4113 if( *pPgno>nPage ){
danielk1977cb1a7eb2004-11-05 12:27:02 +00004114 /* Free page off the end of the file */
danielk197743e377a2008-05-05 12:09:32 +00004115 rc = SQLITE_CORRUPT_BKPT;
4116 goto end_allocate_page;
danielk1977cb1a7eb2004-11-05 12:27:02 +00004117 }
4118 TRACE(("ALLOCATE: %d was leaf %d of %d on trunk %d"
4119 ": %d more free pages\n",
4120 *pPgno, closest+1, k, pTrunk->pgno, n-1));
4121 if( closest<k-1 ){
4122 memcpy(&aData[8+closest*4], &aData[4+k*4], 4);
4123 }
4124 put4byte(&aData[4], k-1);
drh16a9b832007-05-05 18:39:25 +00004125 rc = sqlite3BtreeGetPage(pBt, *pPgno, ppPage, 1);
danielk1977cb1a7eb2004-11-05 12:27:02 +00004126 if( rc==SQLITE_OK ){
drh538f5702007-04-13 02:14:30 +00004127 sqlite3PagerDontRollback((*ppPage)->pDbPage);
danielk19773b8a05f2007-03-19 17:44:26 +00004128 rc = sqlite3PagerWrite((*ppPage)->pDbPage);
danielk1977aac0a382005-01-16 11:07:06 +00004129 if( rc!=SQLITE_OK ){
4130 releasePage(*ppPage);
4131 }
danielk1977cb1a7eb2004-11-05 12:27:02 +00004132 }
4133 searchList = 0;
4134 }
drhee696e22004-08-30 16:52:17 +00004135 }
danielk1977cb1a7eb2004-11-05 12:27:02 +00004136 releasePage(pPrevTrunk);
drhd3627af2006-12-18 18:34:51 +00004137 pPrevTrunk = 0;
danielk1977cb1a7eb2004-11-05 12:27:02 +00004138 }while( searchList );
drh3b7511c2001-05-26 13:15:44 +00004139 }else{
drh3aac2dd2004-04-26 14:10:20 +00004140 /* There are no pages on the freelist, so create a new page at the
4141 ** end of the file */
danielk1977ad0132d2008-06-07 08:58:22 +00004142 int nPage = pagerPagecount(pBt->pPager);
4143 *pPgno = nPage + 1;
danielk1977afcdd022004-10-31 16:25:42 +00004144
4145#ifndef SQLITE_OMIT_AUTOVACUUM
danielk1977dddbcdc2007-04-26 14:42:34 +00004146 if( pBt->nTrunc ){
4147 /* An incr-vacuum has already run within this transaction. So the
4148 ** page to allocate is not from the physical end of the file, but
4149 ** at pBt->nTrunc.
4150 */
4151 *pPgno = pBt->nTrunc+1;
4152 if( *pPgno==PENDING_BYTE_PAGE(pBt) ){
4153 (*pPgno)++;
4154 }
4155 }
danielk1977266664d2006-02-10 08:24:21 +00004156 if( pBt->autoVacuum && PTRMAP_ISPAGE(pBt, *pPgno) ){
danielk1977afcdd022004-10-31 16:25:42 +00004157 /* If *pPgno refers to a pointer-map page, allocate two new pages
4158 ** at the end of the file instead of one. The first allocated page
4159 ** becomes a new pointer-map page, the second is used by the caller.
4160 */
4161 TRACE(("ALLOCATE: %d from end of file (pointer-map page)\n", *pPgno));
danielk1977599fcba2004-11-08 07:13:13 +00004162 assert( *pPgno!=PENDING_BYTE_PAGE(pBt) );
danielk1977afcdd022004-10-31 16:25:42 +00004163 (*pPgno)++;
drh72190432008-01-31 14:54:43 +00004164 if( *pPgno==PENDING_BYTE_PAGE(pBt) ){ (*pPgno)++; }
danielk1977afcdd022004-10-31 16:25:42 +00004165 }
danielk1977dddbcdc2007-04-26 14:42:34 +00004166 if( pBt->nTrunc ){
4167 pBt->nTrunc = *pPgno;
4168 }
danielk1977afcdd022004-10-31 16:25:42 +00004169#endif
4170
danielk1977599fcba2004-11-08 07:13:13 +00004171 assert( *pPgno!=PENDING_BYTE_PAGE(pBt) );
drh16a9b832007-05-05 18:39:25 +00004172 rc = sqlite3BtreeGetPage(pBt, *pPgno, ppPage, 0);
drh3b7511c2001-05-26 13:15:44 +00004173 if( rc ) return rc;
danielk19773b8a05f2007-03-19 17:44:26 +00004174 rc = sqlite3PagerWrite((*ppPage)->pDbPage);
danielk1977aac0a382005-01-16 11:07:06 +00004175 if( rc!=SQLITE_OK ){
4176 releasePage(*ppPage);
4177 }
drh3a4c1412004-05-09 20:40:11 +00004178 TRACE(("ALLOCATE: %d from end of file\n", *pPgno));
drh3b7511c2001-05-26 13:15:44 +00004179 }
danielk1977599fcba2004-11-08 07:13:13 +00004180
4181 assert( *pPgno!=PENDING_BYTE_PAGE(pBt) );
drhd3627af2006-12-18 18:34:51 +00004182
4183end_allocate_page:
4184 releasePage(pTrunk);
4185 releasePage(pPrevTrunk);
drh3b7511c2001-05-26 13:15:44 +00004186 return rc;
4187}
4188
4189/*
drh3aac2dd2004-04-26 14:10:20 +00004190** Add a page of the database file to the freelist.
drh5e2f8b92001-05-28 00:41:15 +00004191**
danielk19773b8a05f2007-03-19 17:44:26 +00004192** sqlite3PagerUnref() is NOT called for pPage.
drh3b7511c2001-05-26 13:15:44 +00004193*/
drh3aac2dd2004-04-26 14:10:20 +00004194static int freePage(MemPage *pPage){
danielk1977aef0bf62005-12-30 16:28:01 +00004195 BtShared *pBt = pPage->pBt;
drh3aac2dd2004-04-26 14:10:20 +00004196 MemPage *pPage1 = pBt->pPage1;
4197 int rc, n, k;
drh8b2f49b2001-06-08 00:21:52 +00004198
drh3aac2dd2004-04-26 14:10:20 +00004199 /* Prepare the page for freeing */
drh1fee73e2007-08-29 04:00:57 +00004200 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
drh3aac2dd2004-04-26 14:10:20 +00004201 assert( pPage->pgno>1 );
4202 pPage->isInit = 0;
4203 releasePage(pPage->pParent);
4204 pPage->pParent = 0;
4205
drha34b6762004-05-07 13:30:42 +00004206 /* Increment the free page count on pPage1 */
danielk19773b8a05f2007-03-19 17:44:26 +00004207 rc = sqlite3PagerWrite(pPage1->pDbPage);
drh3aac2dd2004-04-26 14:10:20 +00004208 if( rc ) return rc;
4209 n = get4byte(&pPage1->aData[36]);
4210 put4byte(&pPage1->aData[36], n+1);
4211
drhfcce93f2006-02-22 03:08:32 +00004212#ifdef SQLITE_SECURE_DELETE
4213 /* If the SQLITE_SECURE_DELETE compile-time option is enabled, then
4214 ** always fully overwrite deleted information with zeros.
4215 */
danielk19773b8a05f2007-03-19 17:44:26 +00004216 rc = sqlite3PagerWrite(pPage->pDbPage);
drhfcce93f2006-02-22 03:08:32 +00004217 if( rc ) return rc;
4218 memset(pPage->aData, 0, pPage->pBt->pageSize);
4219#endif
4220
danielk1977687566d2004-11-02 12:56:41 +00004221#ifndef SQLITE_OMIT_AUTOVACUUM
4222 /* If the database supports auto-vacuum, write an entry in the pointer-map
danielk1977cb1a7eb2004-11-05 12:27:02 +00004223 ** to indicate that the page is free.
danielk1977687566d2004-11-02 12:56:41 +00004224 */
4225 if( pBt->autoVacuum ){
4226 rc = ptrmapPut(pBt, pPage->pgno, PTRMAP_FREEPAGE, 0);
danielk1977a64a0352004-11-05 01:45:13 +00004227 if( rc ) return rc;
danielk1977687566d2004-11-02 12:56:41 +00004228 }
4229#endif
4230
drh3aac2dd2004-04-26 14:10:20 +00004231 if( n==0 ){
4232 /* This is the first free page */
danielk19773b8a05f2007-03-19 17:44:26 +00004233 rc = sqlite3PagerWrite(pPage->pDbPage);
drhda200cc2004-05-09 11:51:38 +00004234 if( rc ) return rc;
drh3aac2dd2004-04-26 14:10:20 +00004235 memset(pPage->aData, 0, 8);
drha34b6762004-05-07 13:30:42 +00004236 put4byte(&pPage1->aData[32], pPage->pgno);
drh3a4c1412004-05-09 20:40:11 +00004237 TRACE(("FREE-PAGE: %d first\n", pPage->pgno));
drh3aac2dd2004-04-26 14:10:20 +00004238 }else{
4239 /* Other free pages already exist. Retrive the first trunk page
4240 ** of the freelist and find out how many leaves it has. */
drha34b6762004-05-07 13:30:42 +00004241 MemPage *pTrunk;
drh16a9b832007-05-05 18:39:25 +00004242 rc = sqlite3BtreeGetPage(pBt, get4byte(&pPage1->aData[32]), &pTrunk, 0);
drh3b7511c2001-05-26 13:15:44 +00004243 if( rc ) return rc;
drh3aac2dd2004-04-26 14:10:20 +00004244 k = get4byte(&pTrunk->aData[4]);
drhee696e22004-08-30 16:52:17 +00004245 if( k>=pBt->usableSize/4 - 8 ){
drh3aac2dd2004-04-26 14:10:20 +00004246 /* The trunk is full. Turn the page being freed into a new
4247 ** trunk page with no leaves. */
danielk19773b8a05f2007-03-19 17:44:26 +00004248 rc = sqlite3PagerWrite(pPage->pDbPage);
drhb9ee4932007-09-07 14:32:06 +00004249 if( rc==SQLITE_OK ){
4250 put4byte(pPage->aData, pTrunk->pgno);
4251 put4byte(&pPage->aData[4], 0);
4252 put4byte(&pPage1->aData[32], pPage->pgno);
4253 TRACE(("FREE-PAGE: %d new trunk page replacing %d\n",
4254 pPage->pgno, pTrunk->pgno));
4255 }
4256 }else if( k<0 ){
4257 rc = SQLITE_CORRUPT;
drh3aac2dd2004-04-26 14:10:20 +00004258 }else{
4259 /* Add the newly freed page as a leaf on the current trunk */
danielk19773b8a05f2007-03-19 17:44:26 +00004260 rc = sqlite3PagerWrite(pTrunk->pDbPage);
drhf5345442007-04-09 12:45:02 +00004261 if( rc==SQLITE_OK ){
4262 put4byte(&pTrunk->aData[4], k+1);
4263 put4byte(&pTrunk->aData[8+k*4], pPage->pgno);
drhfcce93f2006-02-22 03:08:32 +00004264#ifndef SQLITE_SECURE_DELETE
drh538f5702007-04-13 02:14:30 +00004265 sqlite3PagerDontWrite(pPage->pDbPage);
drhfcce93f2006-02-22 03:08:32 +00004266#endif
drhf5345442007-04-09 12:45:02 +00004267 }
drh3a4c1412004-05-09 20:40:11 +00004268 TRACE(("FREE-PAGE: %d leaf on trunk page %d\n",pPage->pgno,pTrunk->pgno));
drh3aac2dd2004-04-26 14:10:20 +00004269 }
4270 releasePage(pTrunk);
drh3b7511c2001-05-26 13:15:44 +00004271 }
drh3b7511c2001-05-26 13:15:44 +00004272 return rc;
4273}
4274
4275/*
drh3aac2dd2004-04-26 14:10:20 +00004276** Free any overflow pages associated with the given Cell.
drh3b7511c2001-05-26 13:15:44 +00004277*/
drh3aac2dd2004-04-26 14:10:20 +00004278static int clearCell(MemPage *pPage, unsigned char *pCell){
danielk1977aef0bf62005-12-30 16:28:01 +00004279 BtShared *pBt = pPage->pBt;
drh6f11bef2004-05-13 01:12:56 +00004280 CellInfo info;
drh3aac2dd2004-04-26 14:10:20 +00004281 Pgno ovflPgno;
drh6f11bef2004-05-13 01:12:56 +00004282 int rc;
drh94440812007-03-06 11:42:19 +00004283 int nOvfl;
4284 int ovflPageSize;
drh3b7511c2001-05-26 13:15:44 +00004285
drh1fee73e2007-08-29 04:00:57 +00004286 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
drh16a9b832007-05-05 18:39:25 +00004287 sqlite3BtreeParseCellPtr(pPage, pCell, &info);
drh6f11bef2004-05-13 01:12:56 +00004288 if( info.iOverflow==0 ){
drha34b6762004-05-07 13:30:42 +00004289 return SQLITE_OK; /* No overflow pages. Return without doing anything */
drh3aac2dd2004-04-26 14:10:20 +00004290 }
drh6f11bef2004-05-13 01:12:56 +00004291 ovflPgno = get4byte(&pCell[info.iOverflow]);
drh94440812007-03-06 11:42:19 +00004292 ovflPageSize = pBt->usableSize - 4;
drh72365832007-03-06 15:53:44 +00004293 nOvfl = (info.nPayload - info.nLocal + ovflPageSize - 1)/ovflPageSize;
4294 assert( ovflPgno==0 || nOvfl>0 );
4295 while( nOvfl-- ){
drh3aac2dd2004-04-26 14:10:20 +00004296 MemPage *pOvfl;
danielk1977ad0132d2008-06-07 08:58:22 +00004297 if( ovflPgno==0 || ovflPgno>pagerPagecount(pBt->pPager) ){
drh49285702005-09-17 15:20:26 +00004298 return SQLITE_CORRUPT_BKPT;
danielk1977a1cb1832005-02-12 08:59:55 +00004299 }
danielk19778c0a9592007-04-30 16:55:00 +00004300
4301 rc = getOverflowPage(pBt, ovflPgno, &pOvfl, (nOvfl==0)?0:&ovflPgno);
drh3b7511c2001-05-26 13:15:44 +00004302 if( rc ) return rc;
drha34b6762004-05-07 13:30:42 +00004303 rc = freePage(pOvfl);
danielk19773b8a05f2007-03-19 17:44:26 +00004304 sqlite3PagerUnref(pOvfl->pDbPage);
danielk19776b456a22005-03-21 04:04:02 +00004305 if( rc ) return rc;
drh3b7511c2001-05-26 13:15:44 +00004306 }
drh5e2f8b92001-05-28 00:41:15 +00004307 return SQLITE_OK;
drh3b7511c2001-05-26 13:15:44 +00004308}
4309
4310/*
drh91025292004-05-03 19:49:32 +00004311** Create the byte sequence used to represent a cell on page pPage
4312** and write that byte sequence into pCell[]. Overflow pages are
4313** allocated and filled in as necessary. The calling procedure
4314** is responsible for making sure sufficient space has been allocated
4315** for pCell[].
4316**
4317** Note that pCell does not necessary need to point to the pPage->aData
4318** area. pCell might point to some temporary storage. The cell will
4319** be constructed in this temporary area then copied into pPage->aData
4320** later.
drh3b7511c2001-05-26 13:15:44 +00004321*/
4322static int fillInCell(
drh3aac2dd2004-04-26 14:10:20 +00004323 MemPage *pPage, /* The page that contains the cell */
drh4b70f112004-05-02 21:12:19 +00004324 unsigned char *pCell, /* Complete text of the cell */
drh4a1c3802004-05-12 15:15:47 +00004325 const void *pKey, i64 nKey, /* The key */
drh4b70f112004-05-02 21:12:19 +00004326 const void *pData,int nData, /* The data */
drhb026e052007-05-02 01:34:31 +00004327 int nZero, /* Extra zero bytes to append to pData */
drh4b70f112004-05-02 21:12:19 +00004328 int *pnSize /* Write cell size here */
drh3b7511c2001-05-26 13:15:44 +00004329){
drh3b7511c2001-05-26 13:15:44 +00004330 int nPayload;
drh8c6fa9b2004-05-26 00:01:53 +00004331 const u8 *pSrc;
drha34b6762004-05-07 13:30:42 +00004332 int nSrc, n, rc;
drh3aac2dd2004-04-26 14:10:20 +00004333 int spaceLeft;
4334 MemPage *pOvfl = 0;
drh9b171272004-05-08 02:03:22 +00004335 MemPage *pToRelease = 0;
drh3aac2dd2004-04-26 14:10:20 +00004336 unsigned char *pPrior;
4337 unsigned char *pPayload;
danielk1977aef0bf62005-12-30 16:28:01 +00004338 BtShared *pBt = pPage->pBt;
drh3aac2dd2004-04-26 14:10:20 +00004339 Pgno pgnoOvfl = 0;
drh4b70f112004-05-02 21:12:19 +00004340 int nHeader;
drh6f11bef2004-05-13 01:12:56 +00004341 CellInfo info;
drh3b7511c2001-05-26 13:15:44 +00004342
drh1fee73e2007-08-29 04:00:57 +00004343 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
drhd677b3d2007-08-20 22:48:41 +00004344
drh91025292004-05-03 19:49:32 +00004345 /* Fill in the header. */
drh43605152004-05-29 21:46:49 +00004346 nHeader = 0;
drh91025292004-05-03 19:49:32 +00004347 if( !pPage->leaf ){
4348 nHeader += 4;
4349 }
drh8b18dd42004-05-12 19:18:15 +00004350 if( pPage->hasData ){
drhb026e052007-05-02 01:34:31 +00004351 nHeader += putVarint(&pCell[nHeader], nData+nZero);
drh6f11bef2004-05-13 01:12:56 +00004352 }else{
drhb026e052007-05-02 01:34:31 +00004353 nData = nZero = 0;
drh91025292004-05-03 19:49:32 +00004354 }
drh6f11bef2004-05-13 01:12:56 +00004355 nHeader += putVarint(&pCell[nHeader], *(u64*)&nKey);
drh16a9b832007-05-05 18:39:25 +00004356 sqlite3BtreeParseCellPtr(pPage, pCell, &info);
drh6f11bef2004-05-13 01:12:56 +00004357 assert( info.nHeader==nHeader );
4358 assert( info.nKey==nKey );
drhb026e052007-05-02 01:34:31 +00004359 assert( info.nData==nData+nZero );
drh6f11bef2004-05-13 01:12:56 +00004360
4361 /* Fill in the payload */
drhb026e052007-05-02 01:34:31 +00004362 nPayload = nData + nZero;
drh3aac2dd2004-04-26 14:10:20 +00004363 if( pPage->intKey ){
4364 pSrc = pData;
4365 nSrc = nData;
drh91025292004-05-03 19:49:32 +00004366 nData = 0;
drh3aac2dd2004-04-26 14:10:20 +00004367 }else{
4368 nPayload += nKey;
4369 pSrc = pKey;
4370 nSrc = nKey;
4371 }
drh6f11bef2004-05-13 01:12:56 +00004372 *pnSize = info.nSize;
4373 spaceLeft = info.nLocal;
drh3aac2dd2004-04-26 14:10:20 +00004374 pPayload = &pCell[nHeader];
drh6f11bef2004-05-13 01:12:56 +00004375 pPrior = &pCell[info.iOverflow];
drh3b7511c2001-05-26 13:15:44 +00004376
drh3b7511c2001-05-26 13:15:44 +00004377 while( nPayload>0 ){
4378 if( spaceLeft==0 ){
danielk1977b39f70b2007-05-17 18:28:11 +00004379 int isExact = 0;
danielk1977afcdd022004-10-31 16:25:42 +00004380#ifndef SQLITE_OMIT_AUTOVACUUM
4381 Pgno pgnoPtrmap = pgnoOvfl; /* Overflow page pointer-map entry page */
danielk1977b39f70b2007-05-17 18:28:11 +00004382 if( pBt->autoVacuum ){
4383 do{
4384 pgnoOvfl++;
4385 } while(
4386 PTRMAP_ISPAGE(pBt, pgnoOvfl) || pgnoOvfl==PENDING_BYTE_PAGE(pBt)
4387 );
danielk197789a4be82007-05-23 13:34:32 +00004388 if( pgnoOvfl>1 ){
danielk1977b39f70b2007-05-17 18:28:11 +00004389 /* isExact = 1; */
4390 }
4391 }
danielk1977afcdd022004-10-31 16:25:42 +00004392#endif
danielk1977b39f70b2007-05-17 18:28:11 +00004393 rc = allocateBtreePage(pBt, &pOvfl, &pgnoOvfl, pgnoOvfl, isExact);
danielk1977afcdd022004-10-31 16:25:42 +00004394#ifndef SQLITE_OMIT_AUTOVACUUM
danielk1977a19df672004-11-03 11:37:07 +00004395 /* If the database supports auto-vacuum, and the second or subsequent
4396 ** overflow page is being allocated, add an entry to the pointer-map
danielk19774ef24492007-05-23 09:52:41 +00004397 ** for that page now.
4398 **
4399 ** If this is the first overflow page, then write a partial entry
4400 ** to the pointer-map. If we write nothing to this pointer-map slot,
4401 ** then the optimistic overflow chain processing in clearCell()
4402 ** may misinterpret the uninitialised values and delete the
4403 ** wrong pages from the database.
danielk1977afcdd022004-10-31 16:25:42 +00004404 */
danielk19774ef24492007-05-23 09:52:41 +00004405 if( pBt->autoVacuum && rc==SQLITE_OK ){
4406 u8 eType = (pgnoPtrmap?PTRMAP_OVERFLOW2:PTRMAP_OVERFLOW1);
4407 rc = ptrmapPut(pBt, pgnoOvfl, eType, pgnoPtrmap);
danielk197789a4be82007-05-23 13:34:32 +00004408 if( rc ){
4409 releasePage(pOvfl);
4410 }
danielk1977afcdd022004-10-31 16:25:42 +00004411 }
4412#endif
drh3b7511c2001-05-26 13:15:44 +00004413 if( rc ){
drh9b171272004-05-08 02:03:22 +00004414 releasePage(pToRelease);
drh3b7511c2001-05-26 13:15:44 +00004415 return rc;
4416 }
drh3aac2dd2004-04-26 14:10:20 +00004417 put4byte(pPrior, pgnoOvfl);
drh9b171272004-05-08 02:03:22 +00004418 releasePage(pToRelease);
4419 pToRelease = pOvfl;
drh3aac2dd2004-04-26 14:10:20 +00004420 pPrior = pOvfl->aData;
4421 put4byte(pPrior, 0);
4422 pPayload = &pOvfl->aData[4];
drhb6f41482004-05-14 01:58:11 +00004423 spaceLeft = pBt->usableSize - 4;
drh3b7511c2001-05-26 13:15:44 +00004424 }
4425 n = nPayload;
4426 if( n>spaceLeft ) n = spaceLeft;
drhb026e052007-05-02 01:34:31 +00004427 if( nSrc>0 ){
4428 if( n>nSrc ) n = nSrc;
4429 assert( pSrc );
4430 memcpy(pPayload, pSrc, n);
4431 }else{
4432 memset(pPayload, 0, n);
4433 }
drh3b7511c2001-05-26 13:15:44 +00004434 nPayload -= n;
drhde647132004-05-07 17:57:49 +00004435 pPayload += n;
drh9b171272004-05-08 02:03:22 +00004436 pSrc += n;
drh3aac2dd2004-04-26 14:10:20 +00004437 nSrc -= n;
drh3b7511c2001-05-26 13:15:44 +00004438 spaceLeft -= n;
drh3aac2dd2004-04-26 14:10:20 +00004439 if( nSrc==0 ){
4440 nSrc = nData;
4441 pSrc = pData;
4442 }
drhdd793422001-06-28 01:54:48 +00004443 }
drh9b171272004-05-08 02:03:22 +00004444 releasePage(pToRelease);
drh3b7511c2001-05-26 13:15:44 +00004445 return SQLITE_OK;
4446}
4447
4448/*
drhbd03cae2001-06-02 02:40:57 +00004449** Change the MemPage.pParent pointer on the page whose number is
drh8b2f49b2001-06-08 00:21:52 +00004450** given in the second argument so that MemPage.pParent holds the
drhbd03cae2001-06-02 02:40:57 +00004451** pointer in the third argument.
4452*/
danielk1977aef0bf62005-12-30 16:28:01 +00004453static int reparentPage(BtShared *pBt, Pgno pgno, MemPage *pNewParent, int idx){
drhbd03cae2001-06-02 02:40:57 +00004454 MemPage *pThis;
danielk19773b8a05f2007-03-19 17:44:26 +00004455 DbPage *pDbPage;
drhbd03cae2001-06-02 02:40:57 +00004456
drh1fee73e2007-08-29 04:00:57 +00004457 assert( sqlite3_mutex_held(pBt->mutex) );
drh43617e92006-03-06 20:55:46 +00004458 assert( pNewParent!=0 );
danielk1977afcdd022004-10-31 16:25:42 +00004459 if( pgno==0 ) return SQLITE_OK;
drh4b70f112004-05-02 21:12:19 +00004460 assert( pBt->pPager!=0 );
danielk19773b8a05f2007-03-19 17:44:26 +00004461 pDbPage = sqlite3PagerLookup(pBt->pPager, pgno);
4462 if( pDbPage ){
4463 pThis = (MemPage *)sqlite3PagerGetExtra(pDbPage);
drhda200cc2004-05-09 11:51:38 +00004464 if( pThis->isInit ){
drhbf4bca52007-09-06 22:19:14 +00004465 assert( pThis->aData==sqlite3PagerGetData(pDbPage) );
drhda200cc2004-05-09 11:51:38 +00004466 if( pThis->pParent!=pNewParent ){
danielk19773b8a05f2007-03-19 17:44:26 +00004467 if( pThis->pParent ) sqlite3PagerUnref(pThis->pParent->pDbPage);
drhda200cc2004-05-09 11:51:38 +00004468 pThis->pParent = pNewParent;
danielk19773b8a05f2007-03-19 17:44:26 +00004469 sqlite3PagerRef(pNewParent->pDbPage);
drhda200cc2004-05-09 11:51:38 +00004470 }
4471 pThis->idxParent = idx;
drhdd793422001-06-28 01:54:48 +00004472 }
danielk19773b8a05f2007-03-19 17:44:26 +00004473 sqlite3PagerUnref(pDbPage);
drhbd03cae2001-06-02 02:40:57 +00004474 }
danielk1977afcdd022004-10-31 16:25:42 +00004475
4476#ifndef SQLITE_OMIT_AUTOVACUUM
4477 if( pBt->autoVacuum ){
4478 return ptrmapPut(pBt, pgno, PTRMAP_BTREE, pNewParent->pgno);
4479 }
4480#endif
4481 return SQLITE_OK;
drhbd03cae2001-06-02 02:40:57 +00004482}
4483
danielk1977ac11ee62005-01-15 12:45:51 +00004484
4485
drhbd03cae2001-06-02 02:40:57 +00004486/*
drh4b70f112004-05-02 21:12:19 +00004487** Change the pParent pointer of all children of pPage to point back
4488** to pPage.
4489**
drhbd03cae2001-06-02 02:40:57 +00004490** In other words, for every child of pPage, invoke reparentPage()
drh5e00f6c2001-09-13 13:46:56 +00004491** to make sure that each child knows that pPage is its parent.
drhbd03cae2001-06-02 02:40:57 +00004492**
4493** This routine gets called after you memcpy() one page into
4494** another.
4495*/
danielk1977afcdd022004-10-31 16:25:42 +00004496static int reparentChildPages(MemPage *pPage){
drhbd03cae2001-06-02 02:40:57 +00004497 int i;
danielk1977aef0bf62005-12-30 16:28:01 +00004498 BtShared *pBt = pPage->pBt;
danielk1977afcdd022004-10-31 16:25:42 +00004499 int rc = SQLITE_OK;
drh4b70f112004-05-02 21:12:19 +00004500
drh1fee73e2007-08-29 04:00:57 +00004501 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
danielk1977afcdd022004-10-31 16:25:42 +00004502 if( pPage->leaf ) return SQLITE_OK;
danielk1977afcdd022004-10-31 16:25:42 +00004503
drhbd03cae2001-06-02 02:40:57 +00004504 for(i=0; i<pPage->nCell; i++){
danielk19771cc5ed82007-05-16 17:28:43 +00004505 u8 *pCell = findCell(pPage, i);
drhcfc2e7f2008-03-23 00:20:36 +00004506 rc = reparentPage(pBt, get4byte(pCell), pPage, i);
4507 if( rc!=SQLITE_OK ) return rc;
drhbd03cae2001-06-02 02:40:57 +00004508 }
drhcfc2e7f2008-03-23 00:20:36 +00004509 rc = reparentPage(pBt, get4byte(&pPage->aData[pPage->hdrOffset+8]),
4510 pPage, i);
4511 pPage->idxShift = 0;
danielk1977afcdd022004-10-31 16:25:42 +00004512 return rc;
drh14acc042001-06-10 19:56:58 +00004513}
4514
4515/*
4516** Remove the i-th cell from pPage. This routine effects pPage only.
4517** The cell content is not freed or deallocated. It is assumed that
4518** the cell content has been copied someplace else. This routine just
4519** removes the reference to the cell from pPage.
4520**
4521** "sz" must be the number of bytes in the cell.
drh14acc042001-06-10 19:56:58 +00004522*/
drh4b70f112004-05-02 21:12:19 +00004523static void dropCell(MemPage *pPage, int idx, int sz){
drh43605152004-05-29 21:46:49 +00004524 int i; /* Loop counter */
4525 int pc; /* Offset to cell content of cell being deleted */
4526 u8 *data; /* pPage->aData */
4527 u8 *ptr; /* Used to move bytes around within data[] */
4528
drh8c42ca92001-06-22 19:15:00 +00004529 assert( idx>=0 && idx<pPage->nCell );
drh43605152004-05-29 21:46:49 +00004530 assert( sz==cellSize(pPage, idx) );
danielk19773b8a05f2007-03-19 17:44:26 +00004531 assert( sqlite3PagerIswriteable(pPage->pDbPage) );
drh1fee73e2007-08-29 04:00:57 +00004532 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
drhda200cc2004-05-09 11:51:38 +00004533 data = pPage->aData;
drh43605152004-05-29 21:46:49 +00004534 ptr = &data[pPage->cellOffset + 2*idx];
4535 pc = get2byte(ptr);
4536 assert( pc>10 && pc+sz<=pPage->pBt->usableSize );
drhde647132004-05-07 17:57:49 +00004537 freeSpace(pPage, pc, sz);
drh43605152004-05-29 21:46:49 +00004538 for(i=idx+1; i<pPage->nCell; i++, ptr+=2){
4539 ptr[0] = ptr[2];
4540 ptr[1] = ptr[3];
drh14acc042001-06-10 19:56:58 +00004541 }
4542 pPage->nCell--;
drh43605152004-05-29 21:46:49 +00004543 put2byte(&data[pPage->hdrOffset+3], pPage->nCell);
4544 pPage->nFree += 2;
drh428ae8c2003-01-04 16:48:09 +00004545 pPage->idxShift = 1;
drh14acc042001-06-10 19:56:58 +00004546}
4547
4548/*
4549** Insert a new cell on pPage at cell index "i". pCell points to the
4550** content of the cell.
4551**
4552** If the cell content will fit on the page, then put it there. If it
drh43605152004-05-29 21:46:49 +00004553** will not fit, then make a copy of the cell content into pTemp if
4554** pTemp is not null. Regardless of pTemp, allocate a new entry
4555** in pPage->aOvfl[] and make it point to the cell content (either
4556** in pTemp or the original pCell) and also record its index.
4557** Allocating a new entry in pPage->aCell[] implies that
4558** pPage->nOverflow is incremented.
danielk1977a3ad5e72005-01-07 08:56:44 +00004559**
4560** If nSkip is non-zero, then do not copy the first nSkip bytes of the
4561** cell. The caller will overwrite them after this function returns. If
drh4b238df2005-01-08 15:43:18 +00004562** nSkip is non-zero, then pCell may not point to an invalid memory location
danielk1977a3ad5e72005-01-07 08:56:44 +00004563** (but pCell+nSkip is always valid).
drh14acc042001-06-10 19:56:58 +00004564*/
danielk1977e80463b2004-11-03 03:01:16 +00004565static int insertCell(
drh24cd67e2004-05-10 16:18:47 +00004566 MemPage *pPage, /* Page into which we are copying */
drh43605152004-05-29 21:46:49 +00004567 int i, /* New cell becomes the i-th cell of the page */
4568 u8 *pCell, /* Content of the new cell */
4569 int sz, /* Bytes of content in pCell */
danielk1977a3ad5e72005-01-07 08:56:44 +00004570 u8 *pTemp, /* Temp storage space for pCell, if needed */
4571 u8 nSkip /* Do not write the first nSkip bytes of the cell */
drh24cd67e2004-05-10 16:18:47 +00004572){
drh43605152004-05-29 21:46:49 +00004573 int idx; /* Where to write new cell content in data[] */
4574 int j; /* Loop counter */
4575 int top; /* First byte of content for any cell in data[] */
4576 int end; /* First byte past the last cell pointer in data[] */
4577 int ins; /* Index in data[] where new cell pointer is inserted */
4578 int hdr; /* Offset into data[] of the page header */
4579 int cellOffset; /* Address of first cell pointer in data[] */
4580 u8 *data; /* The content of the whole page */
4581 u8 *ptr; /* Used for moving information around in data[] */
4582
4583 assert( i>=0 && i<=pPage->nCell+pPage->nOverflow );
4584 assert( sz==cellSizePtr(pPage, pCell) );
drh1fee73e2007-08-29 04:00:57 +00004585 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
drh43605152004-05-29 21:46:49 +00004586 if( pPage->nOverflow || sz+2>pPage->nFree ){
drh24cd67e2004-05-10 16:18:47 +00004587 if( pTemp ){
danielk1977a3ad5e72005-01-07 08:56:44 +00004588 memcpy(pTemp+nSkip, pCell+nSkip, sz-nSkip);
drh43605152004-05-29 21:46:49 +00004589 pCell = pTemp;
drh24cd67e2004-05-10 16:18:47 +00004590 }
drh43605152004-05-29 21:46:49 +00004591 j = pPage->nOverflow++;
4592 assert( j<sizeof(pPage->aOvfl)/sizeof(pPage->aOvfl[0]) );
4593 pPage->aOvfl[j].pCell = pCell;
4594 pPage->aOvfl[j].idx = i;
4595 pPage->nFree = 0;
drh14acc042001-06-10 19:56:58 +00004596 }else{
danielk19776e465eb2007-08-21 13:11:00 +00004597 int rc = sqlite3PagerWrite(pPage->pDbPage);
4598 if( rc!=SQLITE_OK ){
4599 return rc;
4600 }
4601 assert( sqlite3PagerIswriteable(pPage->pDbPage) );
drh43605152004-05-29 21:46:49 +00004602 data = pPage->aData;
4603 hdr = pPage->hdrOffset;
4604 top = get2byte(&data[hdr+5]);
4605 cellOffset = pPage->cellOffset;
4606 end = cellOffset + 2*pPage->nCell + 2;
4607 ins = cellOffset + 2*i;
4608 if( end > top - sz ){
danielk19776e465eb2007-08-21 13:11:00 +00004609 rc = defragmentPage(pPage);
danielk19776b456a22005-03-21 04:04:02 +00004610 if( rc!=SQLITE_OK ) return rc;
drh43605152004-05-29 21:46:49 +00004611 top = get2byte(&data[hdr+5]);
4612 assert( end + sz <= top );
4613 }
4614 idx = allocateSpace(pPage, sz);
4615 assert( idx>0 );
4616 assert( end <= get2byte(&data[hdr+5]) );
4617 pPage->nCell++;
4618 pPage->nFree -= 2;
danielk1977a3ad5e72005-01-07 08:56:44 +00004619 memcpy(&data[idx+nSkip], pCell+nSkip, sz-nSkip);
drh43605152004-05-29 21:46:49 +00004620 for(j=end-2, ptr=&data[j]; j>ins; j-=2, ptr-=2){
4621 ptr[0] = ptr[-2];
4622 ptr[1] = ptr[-1];
drhda200cc2004-05-09 11:51:38 +00004623 }
drh43605152004-05-29 21:46:49 +00004624 put2byte(&data[ins], idx);
4625 put2byte(&data[hdr+3], pPage->nCell);
4626 pPage->idxShift = 1;
danielk1977a19df672004-11-03 11:37:07 +00004627#ifndef SQLITE_OMIT_AUTOVACUUM
4628 if( pPage->pBt->autoVacuum ){
4629 /* The cell may contain a pointer to an overflow page. If so, write
4630 ** the entry for the overflow page into the pointer map.
4631 */
4632 CellInfo info;
drh16a9b832007-05-05 18:39:25 +00004633 sqlite3BtreeParseCellPtr(pPage, pCell, &info);
drh72365832007-03-06 15:53:44 +00004634 assert( (info.nData+(pPage->intKey?0:info.nKey))==info.nPayload );
danielk1977a19df672004-11-03 11:37:07 +00004635 if( (info.nData+(pPage->intKey?0:info.nKey))>info.nLocal ){
4636 Pgno pgnoOvfl = get4byte(&pCell[info.iOverflow]);
danielk19776e465eb2007-08-21 13:11:00 +00004637 rc = ptrmapPut(pPage->pBt, pgnoOvfl, PTRMAP_OVERFLOW1, pPage->pgno);
danielk1977a19df672004-11-03 11:37:07 +00004638 if( rc!=SQLITE_OK ) return rc;
4639 }
4640 }
4641#endif
drh14acc042001-06-10 19:56:58 +00004642 }
danielk1977e80463b2004-11-03 03:01:16 +00004643
danielk1977e80463b2004-11-03 03:01:16 +00004644 return SQLITE_OK;
drh14acc042001-06-10 19:56:58 +00004645}
4646
4647/*
drhfa1a98a2004-05-14 19:08:17 +00004648** Add a list of cells to a page. The page should be initially empty.
4649** The cells are guaranteed to fit on the page.
4650*/
4651static void assemblePage(
4652 MemPage *pPage, /* The page to be assemblied */
4653 int nCell, /* The number of cells to add to this page */
drh43605152004-05-29 21:46:49 +00004654 u8 **apCell, /* Pointers to cell bodies */
drha9121e42008-02-19 14:59:35 +00004655 u16 *aSize /* Sizes of the cells */
drhfa1a98a2004-05-14 19:08:17 +00004656){
4657 int i; /* Loop counter */
4658 int totalSize; /* Total size of all cells */
4659 int hdr; /* Index of page header */
drh43605152004-05-29 21:46:49 +00004660 int cellptr; /* Address of next cell pointer */
4661 int cellbody; /* Address of next cell body */
drhfa1a98a2004-05-14 19:08:17 +00004662 u8 *data; /* Data for the page */
4663
drh43605152004-05-29 21:46:49 +00004664 assert( pPage->nOverflow==0 );
drh1fee73e2007-08-29 04:00:57 +00004665 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
drhfa1a98a2004-05-14 19:08:17 +00004666 totalSize = 0;
4667 for(i=0; i<nCell; i++){
4668 totalSize += aSize[i];
4669 }
drh43605152004-05-29 21:46:49 +00004670 assert( totalSize+2*nCell<=pPage->nFree );
drhfa1a98a2004-05-14 19:08:17 +00004671 assert( pPage->nCell==0 );
drh43605152004-05-29 21:46:49 +00004672 cellptr = pPage->cellOffset;
drhfa1a98a2004-05-14 19:08:17 +00004673 data = pPage->aData;
4674 hdr = pPage->hdrOffset;
drh43605152004-05-29 21:46:49 +00004675 put2byte(&data[hdr+3], nCell);
drh09d0deb2005-08-02 17:13:09 +00004676 if( nCell ){
4677 cellbody = allocateSpace(pPage, totalSize);
4678 assert( cellbody>0 );
4679 assert( pPage->nFree >= 2*nCell );
4680 pPage->nFree -= 2*nCell;
4681 for(i=0; i<nCell; i++){
4682 put2byte(&data[cellptr], cellbody);
4683 memcpy(&data[cellbody], apCell[i], aSize[i]);
4684 cellptr += 2;
4685 cellbody += aSize[i];
4686 }
4687 assert( cellbody==pPage->pBt->usableSize );
drhfa1a98a2004-05-14 19:08:17 +00004688 }
4689 pPage->nCell = nCell;
drhfa1a98a2004-05-14 19:08:17 +00004690}
4691
drh14acc042001-06-10 19:56:58 +00004692/*
drhc3b70572003-01-04 19:44:07 +00004693** The following parameters determine how many adjacent pages get involved
4694** in a balancing operation. NN is the number of neighbors on either side
4695** of the page that participate in the balancing operation. NB is the
4696** total number of pages that participate, including the target page and
4697** NN neighbors on either side.
4698**
4699** The minimum value of NN is 1 (of course). Increasing NN above 1
4700** (to 2 or 3) gives a modest improvement in SELECT and DELETE performance
4701** in exchange for a larger degradation in INSERT and UPDATE performance.
4702** The value of NN appears to give the best results overall.
4703*/
4704#define NN 1 /* Number of neighbors on either side of pPage */
4705#define NB (NN*2+1) /* Total pages involved in the balance */
4706
drh43605152004-05-29 21:46:49 +00004707/* Forward reference */
danielk1977ac245ec2005-01-14 13:50:11 +00004708static int balance(MemPage*, int);
4709
drh615ae552005-01-16 23:21:00 +00004710#ifndef SQLITE_OMIT_QUICKBALANCE
drhf222e712005-01-14 22:55:49 +00004711/*
4712** This version of balance() handles the common special case where
4713** a new entry is being inserted on the extreme right-end of the
4714** tree, in other words, when the new entry will become the largest
4715** entry in the tree.
4716**
4717** Instead of trying balance the 3 right-most leaf pages, just add
4718** a new page to the right-hand side and put the one new entry in
4719** that page. This leaves the right side of the tree somewhat
4720** unbalanced. But odds are that we will be inserting new entries
4721** at the end soon afterwards so the nearly empty page will quickly
4722** fill up. On average.
4723**
4724** pPage is the leaf page which is the right-most page in the tree.
4725** pParent is its parent. pPage must have a single overflow entry
4726** which is also the right-most entry on the page.
4727*/
danielk1977ac245ec2005-01-14 13:50:11 +00004728static int balance_quick(MemPage *pPage, MemPage *pParent){
4729 int rc;
4730 MemPage *pNew;
4731 Pgno pgnoNew;
4732 u8 *pCell;
drha9121e42008-02-19 14:59:35 +00004733 u16 szCell;
danielk1977ac245ec2005-01-14 13:50:11 +00004734 CellInfo info;
danielk1977aef0bf62005-12-30 16:28:01 +00004735 BtShared *pBt = pPage->pBt;
danielk197779a40da2005-01-16 08:00:01 +00004736 int parentIdx = pParent->nCell; /* pParent new divider cell index */
4737 int parentSize; /* Size of new divider cell */
4738 u8 parentCell[64]; /* Space for the new divider cell */
danielk1977ac245ec2005-01-14 13:50:11 +00004739
drh1fee73e2007-08-29 04:00:57 +00004740 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
drhd677b3d2007-08-20 22:48:41 +00004741
danielk1977ac245ec2005-01-14 13:50:11 +00004742 /* Allocate a new page. Insert the overflow cell from pPage
4743 ** into it. Then remove the overflow cell from pPage.
4744 */
drh4f0c5872007-03-26 22:05:01 +00004745 rc = allocateBtreePage(pBt, &pNew, &pgnoNew, 0, 0);
danielk1977ac245ec2005-01-14 13:50:11 +00004746 if( rc!=SQLITE_OK ){
4747 return rc;
4748 }
4749 pCell = pPage->aOvfl[0].pCell;
4750 szCell = cellSizePtr(pPage, pCell);
4751 zeroPage(pNew, pPage->aData[0]);
4752 assemblePage(pNew, 1, &pCell, &szCell);
4753 pPage->nOverflow = 0;
4754
danielk197779a40da2005-01-16 08:00:01 +00004755 /* Set the parent of the newly allocated page to pParent. */
4756 pNew->pParent = pParent;
danielk19773b8a05f2007-03-19 17:44:26 +00004757 sqlite3PagerRef(pParent->pDbPage);
danielk197779a40da2005-01-16 08:00:01 +00004758
danielk1977ac245ec2005-01-14 13:50:11 +00004759 /* pPage is currently the right-child of pParent. Change this
4760 ** so that the right-child is the new page allocated above and
danielk197779a40da2005-01-16 08:00:01 +00004761 ** pPage is the next-to-right child.
danielk1977ac245ec2005-01-14 13:50:11 +00004762 */
danielk1977ac11ee62005-01-15 12:45:51 +00004763 assert( pPage->nCell>0 );
danielk19771cc5ed82007-05-16 17:28:43 +00004764 pCell = findCell(pPage, pPage->nCell-1);
drh16a9b832007-05-05 18:39:25 +00004765 sqlite3BtreeParseCellPtr(pPage, pCell, &info);
drhb026e052007-05-02 01:34:31 +00004766 rc = fillInCell(pParent, parentCell, 0, info.nKey, 0, 0, 0, &parentSize);
danielk1977ac245ec2005-01-14 13:50:11 +00004767 if( rc!=SQLITE_OK ){
danielk197779a40da2005-01-16 08:00:01 +00004768 return rc;
danielk1977ac245ec2005-01-14 13:50:11 +00004769 }
4770 assert( parentSize<64 );
4771 rc = insertCell(pParent, parentIdx, parentCell, parentSize, 0, 4);
4772 if( rc!=SQLITE_OK ){
danielk197779a40da2005-01-16 08:00:01 +00004773 return rc;
danielk1977ac245ec2005-01-14 13:50:11 +00004774 }
4775 put4byte(findOverflowCell(pParent,parentIdx), pPage->pgno);
4776 put4byte(&pParent->aData[pParent->hdrOffset+8], pgnoNew);
4777
danielk197779a40da2005-01-16 08:00:01 +00004778#ifndef SQLITE_OMIT_AUTOVACUUM
4779 /* If this is an auto-vacuum database, update the pointer map
4780 ** with entries for the new page, and any pointer from the
4781 ** cell on the page to an overflow page.
4782 */
danielk1977ac11ee62005-01-15 12:45:51 +00004783 if( pBt->autoVacuum ){
4784 rc = ptrmapPut(pBt, pgnoNew, PTRMAP_BTREE, pParent->pgno);
danielk1977deb403e2007-05-24 09:20:16 +00004785 if( rc==SQLITE_OK ){
4786 rc = ptrmapPutOvfl(pNew, 0);
danielk1977ac11ee62005-01-15 12:45:51 +00004787 }
danielk197779a40da2005-01-16 08:00:01 +00004788 if( rc!=SQLITE_OK ){
danielk1977deb403e2007-05-24 09:20:16 +00004789 releasePage(pNew);
danielk197779a40da2005-01-16 08:00:01 +00004790 return rc;
danielk1977ac11ee62005-01-15 12:45:51 +00004791 }
4792 }
danielk197779a40da2005-01-16 08:00:01 +00004793#endif
danielk1977ac11ee62005-01-15 12:45:51 +00004794
danielk197779a40da2005-01-16 08:00:01 +00004795 /* Release the reference to the new page and balance the parent page,
4796 ** in case the divider cell inserted caused it to become overfull.
4797 */
danielk1977ac245ec2005-01-14 13:50:11 +00004798 releasePage(pNew);
4799 return balance(pParent, 0);
4800}
drh615ae552005-01-16 23:21:00 +00004801#endif /* SQLITE_OMIT_QUICKBALANCE */
drh43605152004-05-29 21:46:49 +00004802
drhc3b70572003-01-04 19:44:07 +00004803/*
drhab01f612004-05-22 02:55:23 +00004804** This routine redistributes Cells on pPage and up to NN*2 siblings
drh8b2f49b2001-06-08 00:21:52 +00004805** of pPage so that all pages have about the same amount of free space.
drh0c6cc4e2004-06-15 02:13:26 +00004806** Usually NN siblings on either side of pPage is used in the balancing,
4807** though more siblings might come from one side if pPage is the first
drhab01f612004-05-22 02:55:23 +00004808** or last child of its parent. If pPage has fewer than 2*NN siblings
drh8b2f49b2001-06-08 00:21:52 +00004809** (something which can only happen if pPage is the root page or a
drh14acc042001-06-10 19:56:58 +00004810** child of root) then all available siblings participate in the balancing.
drh8b2f49b2001-06-08 00:21:52 +00004811**
drh0c6cc4e2004-06-15 02:13:26 +00004812** The number of siblings of pPage might be increased or decreased by one or
4813** two in an effort to keep pages nearly full but not over full. The root page
drhab01f612004-05-22 02:55:23 +00004814** is special and is allowed to be nearly empty. If pPage is
drh8c42ca92001-06-22 19:15:00 +00004815** the root page, then the depth of the tree might be increased
drh8b2f49b2001-06-08 00:21:52 +00004816** or decreased by one, as necessary, to keep the root page from being
drhab01f612004-05-22 02:55:23 +00004817** overfull or completely empty.
drh14acc042001-06-10 19:56:58 +00004818**
drh8b2f49b2001-06-08 00:21:52 +00004819** Note that when this routine is called, some of the Cells on pPage
drh4b70f112004-05-02 21:12:19 +00004820** might not actually be stored in pPage->aData[]. This can happen
drh8b2f49b2001-06-08 00:21:52 +00004821** if the page is overfull. Part of the job of this routine is to
drh4b70f112004-05-02 21:12:19 +00004822** make sure all Cells for pPage once again fit in pPage->aData[].
drh14acc042001-06-10 19:56:58 +00004823**
drh8c42ca92001-06-22 19:15:00 +00004824** In the course of balancing the siblings of pPage, the parent of pPage
4825** might become overfull or underfull. If that happens, then this routine
4826** is called recursively on the parent.
4827**
drh5e00f6c2001-09-13 13:46:56 +00004828** If this routine fails for any reason, it might leave the database
4829** in a corrupted state. So if this routine fails, the database should
4830** be rolled back.
drh8b2f49b2001-06-08 00:21:52 +00004831*/
drh43605152004-05-29 21:46:49 +00004832static int balance_nonroot(MemPage *pPage){
drh8b2f49b2001-06-08 00:21:52 +00004833 MemPage *pParent; /* The parent of pPage */
drh16a9b832007-05-05 18:39:25 +00004834 BtShared *pBt; /* The whole database */
danielk1977634f2982005-03-28 08:44:07 +00004835 int nCell = 0; /* Number of cells in apCell[] */
4836 int nMaxCells = 0; /* Allocated size of apCell, szCell, aFrom. */
drh8b2f49b2001-06-08 00:21:52 +00004837 int nOld; /* Number of pages in apOld[] */
4838 int nNew; /* Number of pages in apNew[] */
drh8b2f49b2001-06-08 00:21:52 +00004839 int nDiv; /* Number of cells in apDiv[] */
drh14acc042001-06-10 19:56:58 +00004840 int i, j, k; /* Loop counters */
drha34b6762004-05-07 13:30:42 +00004841 int idx; /* Index of pPage in pParent->aCell[] */
4842 int nxDiv; /* Next divider slot in pParent->aCell[] */
drh14acc042001-06-10 19:56:58 +00004843 int rc; /* The return code */
drh91025292004-05-03 19:49:32 +00004844 int leafCorrection; /* 4 if pPage is a leaf. 0 if not */
drh8b18dd42004-05-12 19:18:15 +00004845 int leafData; /* True if pPage is a leaf of a LEAFDATA tree */
drh91025292004-05-03 19:49:32 +00004846 int usableSpace; /* Bytes in pPage beyond the header */
4847 int pageFlags; /* Value of pPage->aData[0] */
drh6019e162001-07-02 17:51:45 +00004848 int subtotal; /* Subtotal of bytes in cells on one page */
drhb6f41482004-05-14 01:58:11 +00004849 int iSpace = 0; /* First unused byte of aSpace[] */
drhc3b70572003-01-04 19:44:07 +00004850 MemPage *apOld[NB]; /* pPage and up to two siblings */
4851 Pgno pgnoOld[NB]; /* Page numbers for each page in apOld[] */
drh4b70f112004-05-02 21:12:19 +00004852 MemPage *apCopy[NB]; /* Private copies of apOld[] pages */
drha2fce642004-06-05 00:01:44 +00004853 MemPage *apNew[NB+2]; /* pPage and up to NB siblings after balancing */
4854 Pgno pgnoNew[NB+2]; /* Page numbers for each page in apNew[] */
drh4b70f112004-05-02 21:12:19 +00004855 u8 *apDiv[NB]; /* Divider cells in pParent */
drha2fce642004-06-05 00:01:44 +00004856 int cntNew[NB+2]; /* Index in aCell[] of cell after i-th page */
4857 int szNew[NB+2]; /* Combined size of cells place on i-th page */
danielk197750f059b2005-03-29 02:54:03 +00004858 u8 **apCell = 0; /* All cells begin balanced */
drha9121e42008-02-19 14:59:35 +00004859 u16 *szCell; /* Local size of all cells in apCell[] */
drh2e38c322004-09-03 18:38:44 +00004860 u8 *aCopy[NB]; /* Space for holding data of apCopy[] */
4861 u8 *aSpace; /* Space to hold copies of dividers cells */
danielk19774e17d142005-01-16 09:06:33 +00004862#ifndef SQLITE_OMIT_AUTOVACUUM
danielk1977ac11ee62005-01-15 12:45:51 +00004863 u8 *aFrom = 0;
4864#endif
drh8b2f49b2001-06-08 00:21:52 +00004865
drh1fee73e2007-08-29 04:00:57 +00004866 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
drhd677b3d2007-08-20 22:48:41 +00004867
drh14acc042001-06-10 19:56:58 +00004868 /*
drh43605152004-05-29 21:46:49 +00004869 ** Find the parent page.
drh8b2f49b2001-06-08 00:21:52 +00004870 */
drh3a4c1412004-05-09 20:40:11 +00004871 assert( pPage->isInit );
danielk19776e465eb2007-08-21 13:11:00 +00004872 assert( sqlite3PagerIswriteable(pPage->pDbPage) || pPage->nOverflow==1 );
drh4b70f112004-05-02 21:12:19 +00004873 pBt = pPage->pBt;
drh14acc042001-06-10 19:56:58 +00004874 pParent = pPage->pParent;
drh43605152004-05-29 21:46:49 +00004875 assert( pParent );
danielk19773b8a05f2007-03-19 17:44:26 +00004876 if( SQLITE_OK!=(rc = sqlite3PagerWrite(pParent->pDbPage)) ){
danielk197707cb5602006-01-20 10:55:05 +00004877 return rc;
4878 }
drh43605152004-05-29 21:46:49 +00004879 TRACE(("BALANCE: begin page %d child of %d\n", pPage->pgno, pParent->pgno));
drh2e38c322004-09-03 18:38:44 +00004880
drh615ae552005-01-16 23:21:00 +00004881#ifndef SQLITE_OMIT_QUICKBALANCE
drhf222e712005-01-14 22:55:49 +00004882 /*
4883 ** A special case: If a new entry has just been inserted into a
4884 ** table (that is, a btree with integer keys and all data at the leaves)
drh09d0deb2005-08-02 17:13:09 +00004885 ** and the new entry is the right-most entry in the tree (it has the
drhf222e712005-01-14 22:55:49 +00004886 ** largest key) then use the special balance_quick() routine for
4887 ** balancing. balance_quick() is much faster and results in a tighter
4888 ** packing of data in the common case.
4889 */
danielk1977ac245ec2005-01-14 13:50:11 +00004890 if( pPage->leaf &&
4891 pPage->intKey &&
4892 pPage->leafData &&
4893 pPage->nOverflow==1 &&
4894 pPage->aOvfl[0].idx==pPage->nCell &&
danielk1977ac11ee62005-01-15 12:45:51 +00004895 pPage->pParent->pgno!=1 &&
danielk1977ac245ec2005-01-14 13:50:11 +00004896 get4byte(&pParent->aData[pParent->hdrOffset+8])==pPage->pgno
4897 ){
danielk1977ac11ee62005-01-15 12:45:51 +00004898 /*
4899 ** TODO: Check the siblings to the left of pPage. It may be that
4900 ** they are not full and no new page is required.
4901 */
danielk1977ac245ec2005-01-14 13:50:11 +00004902 return balance_quick(pPage, pParent);
4903 }
4904#endif
4905
danielk19776e465eb2007-08-21 13:11:00 +00004906 if( SQLITE_OK!=(rc = sqlite3PagerWrite(pPage->pDbPage)) ){
4907 return rc;
4908 }
4909
drh2e38c322004-09-03 18:38:44 +00004910 /*
drh4b70f112004-05-02 21:12:19 +00004911 ** Find the cell in the parent page whose left child points back
drh14acc042001-06-10 19:56:58 +00004912 ** to pPage. The "idx" variable is the index of that cell. If pPage
4913 ** is the rightmost child of pParent then set idx to pParent->nCell
drh8b2f49b2001-06-08 00:21:52 +00004914 */
drhbb49aba2003-01-04 18:53:27 +00004915 if( pParent->idxShift ){
drha34b6762004-05-07 13:30:42 +00004916 Pgno pgno;
drh4b70f112004-05-02 21:12:19 +00004917 pgno = pPage->pgno;
danielk19773b8a05f2007-03-19 17:44:26 +00004918 assert( pgno==sqlite3PagerPagenumber(pPage->pDbPage) );
drhbb49aba2003-01-04 18:53:27 +00004919 for(idx=0; idx<pParent->nCell; idx++){
danielk19771cc5ed82007-05-16 17:28:43 +00004920 if( get4byte(findCell(pParent, idx))==pgno ){
drhbb49aba2003-01-04 18:53:27 +00004921 break;
4922 }
drh8b2f49b2001-06-08 00:21:52 +00004923 }
drh4b70f112004-05-02 21:12:19 +00004924 assert( idx<pParent->nCell
drh43605152004-05-29 21:46:49 +00004925 || get4byte(&pParent->aData[pParent->hdrOffset+8])==pgno );
drhbb49aba2003-01-04 18:53:27 +00004926 }else{
4927 idx = pPage->idxParent;
drh8b2f49b2001-06-08 00:21:52 +00004928 }
drh8b2f49b2001-06-08 00:21:52 +00004929
4930 /*
drh14acc042001-06-10 19:56:58 +00004931 ** Initialize variables so that it will be safe to jump
drh5edc3122001-09-13 21:53:09 +00004932 ** directly to balance_cleanup at any moment.
drh8b2f49b2001-06-08 00:21:52 +00004933 */
drh14acc042001-06-10 19:56:58 +00004934 nOld = nNew = 0;
danielk19773b8a05f2007-03-19 17:44:26 +00004935 sqlite3PagerRef(pParent->pDbPage);
drh14acc042001-06-10 19:56:58 +00004936
4937 /*
drh4b70f112004-05-02 21:12:19 +00004938 ** Find sibling pages to pPage and the cells in pParent that divide
drhc3b70572003-01-04 19:44:07 +00004939 ** the siblings. An attempt is made to find NN siblings on either
4940 ** side of pPage. More siblings are taken from one side, however, if
4941 ** pPage there are fewer than NN siblings on the other side. If pParent
4942 ** has NB or fewer children then all children of pParent are taken.
drh14acc042001-06-10 19:56:58 +00004943 */
drhc3b70572003-01-04 19:44:07 +00004944 nxDiv = idx - NN;
4945 if( nxDiv + NB > pParent->nCell ){
4946 nxDiv = pParent->nCell - NB + 1;
drh8b2f49b2001-06-08 00:21:52 +00004947 }
drhc3b70572003-01-04 19:44:07 +00004948 if( nxDiv<0 ){
4949 nxDiv = 0;
4950 }
drh8b2f49b2001-06-08 00:21:52 +00004951 nDiv = 0;
drhc3b70572003-01-04 19:44:07 +00004952 for(i=0, k=nxDiv; i<NB; i++, k++){
drh14acc042001-06-10 19:56:58 +00004953 if( k<pParent->nCell ){
danielk19771cc5ed82007-05-16 17:28:43 +00004954 apDiv[i] = findCell(pParent, k);
drh8b2f49b2001-06-08 00:21:52 +00004955 nDiv++;
drha34b6762004-05-07 13:30:42 +00004956 assert( !pParent->leaf );
drh43605152004-05-29 21:46:49 +00004957 pgnoOld[i] = get4byte(apDiv[i]);
drh14acc042001-06-10 19:56:58 +00004958 }else if( k==pParent->nCell ){
drh43605152004-05-29 21:46:49 +00004959 pgnoOld[i] = get4byte(&pParent->aData[pParent->hdrOffset+8]);
drh14acc042001-06-10 19:56:58 +00004960 }else{
4961 break;
drh8b2f49b2001-06-08 00:21:52 +00004962 }
drhde647132004-05-07 17:57:49 +00004963 rc = getAndInitPage(pBt, pgnoOld[i], &apOld[i], pParent);
drh6019e162001-07-02 17:51:45 +00004964 if( rc ) goto balance_cleanup;
drh428ae8c2003-01-04 16:48:09 +00004965 apOld[i]->idxParent = k;
drh91025292004-05-03 19:49:32 +00004966 apCopy[i] = 0;
4967 assert( i==nOld );
drh14acc042001-06-10 19:56:58 +00004968 nOld++;
danielk1977634f2982005-03-28 08:44:07 +00004969 nMaxCells += 1+apOld[i]->nCell+apOld[i]->nOverflow;
drh8b2f49b2001-06-08 00:21:52 +00004970 }
4971
drha9121e42008-02-19 14:59:35 +00004972 /* Make nMaxCells a multiple of 4 in order to preserve 8-byte
drh8d97f1f2005-05-05 18:14:13 +00004973 ** alignment */
drha9121e42008-02-19 14:59:35 +00004974 nMaxCells = (nMaxCells + 3)&~3;
drh8d97f1f2005-05-05 18:14:13 +00004975
drh8b2f49b2001-06-08 00:21:52 +00004976 /*
danielk1977634f2982005-03-28 08:44:07 +00004977 ** Allocate space for memory structures
4978 */
drh17435752007-08-16 04:30:38 +00004979 apCell = sqlite3_malloc(
drha9121e42008-02-19 14:59:35 +00004980 nMaxCells*sizeof(u8*) /* apCell */
4981 + nMaxCells*sizeof(u16) /* szCell */
4982 + (ROUND8(sizeof(MemPage))+pBt->pageSize)*NB /* aCopy */
4983 + pBt->pageSize*5 /* aSpace */
4984 + (ISAUTOVACUUM ? nMaxCells : 0) /* aFrom */
danielk1977634f2982005-03-28 08:44:07 +00004985 );
4986 if( apCell==0 ){
4987 rc = SQLITE_NOMEM;
4988 goto balance_cleanup;
4989 }
drha9121e42008-02-19 14:59:35 +00004990 szCell = (u16*)&apCell[nMaxCells];
danielk1977634f2982005-03-28 08:44:07 +00004991 aCopy[0] = (u8*)&szCell[nMaxCells];
drhc96d8532005-05-03 12:30:33 +00004992 assert( ((aCopy[0] - (u8*)apCell) & 7)==0 ); /* 8-byte alignment required */
danielk1977634f2982005-03-28 08:44:07 +00004993 for(i=1; i<NB; i++){
drhc96d8532005-05-03 12:30:33 +00004994 aCopy[i] = &aCopy[i-1][pBt->pageSize+ROUND8(sizeof(MemPage))];
4995 assert( ((aCopy[i] - (u8*)apCell) & 7)==0 ); /* 8-byte alignment required */
danielk1977634f2982005-03-28 08:44:07 +00004996 }
drhc96d8532005-05-03 12:30:33 +00004997 aSpace = &aCopy[NB-1][pBt->pageSize+ROUND8(sizeof(MemPage))];
4998 assert( ((aSpace - (u8*)apCell) & 7)==0 ); /* 8-byte alignment required */
danielk1977634f2982005-03-28 08:44:07 +00004999#ifndef SQLITE_OMIT_AUTOVACUUM
5000 if( pBt->autoVacuum ){
drh07d183d2005-05-01 22:52:42 +00005001 aFrom = &aSpace[5*pBt->pageSize];
danielk1977634f2982005-03-28 08:44:07 +00005002 }
5003#endif
5004
5005 /*
drh14acc042001-06-10 19:56:58 +00005006 ** Make copies of the content of pPage and its siblings into aOld[].
5007 ** The rest of this function will use data from the copies rather
5008 ** that the original pages since the original pages will be in the
5009 ** process of being overwritten.
5010 */
5011 for(i=0; i<nOld; i++){
drhbf4bca52007-09-06 22:19:14 +00005012 MemPage *p = apCopy[i] = (MemPage*)aCopy[i];
5013 memcpy(p, apOld[i], sizeof(MemPage));
5014 p->aData = (void*)&p[1];
5015 memcpy(p->aData, apOld[i]->aData, pBt->pageSize);
drh14acc042001-06-10 19:56:58 +00005016 }
5017
5018 /*
5019 ** Load pointers to all cells on sibling pages and the divider cells
5020 ** into the local apCell[] array. Make copies of the divider cells
drhb6f41482004-05-14 01:58:11 +00005021 ** into space obtained form aSpace[] and remove the the divider Cells
5022 ** from pParent.
drh4b70f112004-05-02 21:12:19 +00005023 **
5024 ** If the siblings are on leaf pages, then the child pointers of the
5025 ** divider cells are stripped from the cells before they are copied
drh96f5b762004-05-16 16:24:36 +00005026 ** into aSpace[]. In this way, all cells in apCell[] are without
drh4b70f112004-05-02 21:12:19 +00005027 ** child pointers. If siblings are not leaves, then all cell in
5028 ** apCell[] include child pointers. Either way, all cells in apCell[]
5029 ** are alike.
drh96f5b762004-05-16 16:24:36 +00005030 **
5031 ** leafCorrection: 4 if pPage is a leaf. 0 if pPage is not a leaf.
5032 ** leafData: 1 if pPage holds key+data and pParent holds only keys.
drh8b2f49b2001-06-08 00:21:52 +00005033 */
5034 nCell = 0;
drh4b70f112004-05-02 21:12:19 +00005035 leafCorrection = pPage->leaf*4;
drh8b18dd42004-05-12 19:18:15 +00005036 leafData = pPage->leafData && pPage->leaf;
drh8b2f49b2001-06-08 00:21:52 +00005037 for(i=0; i<nOld; i++){
drh4b70f112004-05-02 21:12:19 +00005038 MemPage *pOld = apCopy[i];
drh43605152004-05-29 21:46:49 +00005039 int limit = pOld->nCell+pOld->nOverflow;
5040 for(j=0; j<limit; j++){
danielk1977634f2982005-03-28 08:44:07 +00005041 assert( nCell<nMaxCells );
drh43605152004-05-29 21:46:49 +00005042 apCell[nCell] = findOverflowCell(pOld, j);
5043 szCell[nCell] = cellSizePtr(pOld, apCell[nCell]);
danielk1977ac11ee62005-01-15 12:45:51 +00005044#ifndef SQLITE_OMIT_AUTOVACUUM
5045 if( pBt->autoVacuum ){
5046 int a;
5047 aFrom[nCell] = i;
5048 for(a=0; a<pOld->nOverflow; a++){
5049 if( pOld->aOvfl[a].pCell==apCell[nCell] ){
5050 aFrom[nCell] = 0xFF;
5051 break;
5052 }
5053 }
5054 }
5055#endif
drh14acc042001-06-10 19:56:58 +00005056 nCell++;
drh8b2f49b2001-06-08 00:21:52 +00005057 }
5058 if( i<nOld-1 ){
drha9121e42008-02-19 14:59:35 +00005059 u16 sz = cellSizePtr(pParent, apDiv[i]);
drh8b18dd42004-05-12 19:18:15 +00005060 if( leafData ){
drh96f5b762004-05-16 16:24:36 +00005061 /* With the LEAFDATA flag, pParent cells hold only INTKEYs that
5062 ** are duplicates of keys on the child pages. We need to remove
5063 ** the divider cells from pParent, but the dividers cells are not
5064 ** added to apCell[] because they are duplicates of child cells.
5065 */
drh8b18dd42004-05-12 19:18:15 +00005066 dropCell(pParent, nxDiv, sz);
drh4b70f112004-05-02 21:12:19 +00005067 }else{
drhb6f41482004-05-14 01:58:11 +00005068 u8 *pTemp;
danielk1977634f2982005-03-28 08:44:07 +00005069 assert( nCell<nMaxCells );
drhb6f41482004-05-14 01:58:11 +00005070 szCell[nCell] = sz;
5071 pTemp = &aSpace[iSpace];
5072 iSpace += sz;
drh07d183d2005-05-01 22:52:42 +00005073 assert( iSpace<=pBt->pageSize*5 );
drhb6f41482004-05-14 01:58:11 +00005074 memcpy(pTemp, apDiv[i], sz);
5075 apCell[nCell] = pTemp+leafCorrection;
danielk1977ac11ee62005-01-15 12:45:51 +00005076#ifndef SQLITE_OMIT_AUTOVACUUM
5077 if( pBt->autoVacuum ){
5078 aFrom[nCell] = 0xFF;
5079 }
5080#endif
drhb6f41482004-05-14 01:58:11 +00005081 dropCell(pParent, nxDiv, sz);
drh8b18dd42004-05-12 19:18:15 +00005082 szCell[nCell] -= leafCorrection;
drh43605152004-05-29 21:46:49 +00005083 assert( get4byte(pTemp)==pgnoOld[i] );
drh8b18dd42004-05-12 19:18:15 +00005084 if( !pOld->leaf ){
5085 assert( leafCorrection==0 );
5086 /* The right pointer of the child page pOld becomes the left
5087 ** pointer of the divider cell */
drh43605152004-05-29 21:46:49 +00005088 memcpy(apCell[nCell], &pOld->aData[pOld->hdrOffset+8], 4);
drh8b18dd42004-05-12 19:18:15 +00005089 }else{
5090 assert( leafCorrection==4 );
danielk197739c96042007-05-12 10:41:47 +00005091 if( szCell[nCell]<4 ){
5092 /* Do not allow any cells smaller than 4 bytes. */
5093 szCell[nCell] = 4;
5094 }
drh8b18dd42004-05-12 19:18:15 +00005095 }
5096 nCell++;
drh4b70f112004-05-02 21:12:19 +00005097 }
drh8b2f49b2001-06-08 00:21:52 +00005098 }
5099 }
5100
5101 /*
drh6019e162001-07-02 17:51:45 +00005102 ** Figure out the number of pages needed to hold all nCell cells.
5103 ** Store this number in "k". Also compute szNew[] which is the total
5104 ** size of all cells on the i-th page and cntNew[] which is the index
drh4b70f112004-05-02 21:12:19 +00005105 ** in apCell[] of the cell that divides page i from page i+1.
drh6019e162001-07-02 17:51:45 +00005106 ** cntNew[k] should equal nCell.
5107 **
drh96f5b762004-05-16 16:24:36 +00005108 ** Values computed by this block:
5109 **
5110 ** k: The total number of sibling pages
5111 ** szNew[i]: Spaced used on the i-th sibling page.
5112 ** cntNew[i]: Index in apCell[] and szCell[] for the first cell to
5113 ** the right of the i-th sibling page.
5114 ** usableSpace: Number of bytes of space available on each sibling.
5115 **
drh8b2f49b2001-06-08 00:21:52 +00005116 */
drh43605152004-05-29 21:46:49 +00005117 usableSpace = pBt->usableSize - 12 + leafCorrection;
drh6019e162001-07-02 17:51:45 +00005118 for(subtotal=k=i=0; i<nCell; i++){
danielk1977634f2982005-03-28 08:44:07 +00005119 assert( i<nMaxCells );
drh43605152004-05-29 21:46:49 +00005120 subtotal += szCell[i] + 2;
drh4b70f112004-05-02 21:12:19 +00005121 if( subtotal > usableSpace ){
drh6019e162001-07-02 17:51:45 +00005122 szNew[k] = subtotal - szCell[i];
5123 cntNew[k] = i;
drh8b18dd42004-05-12 19:18:15 +00005124 if( leafData ){ i--; }
drh6019e162001-07-02 17:51:45 +00005125 subtotal = 0;
5126 k++;
5127 }
5128 }
5129 szNew[k] = subtotal;
5130 cntNew[k] = nCell;
5131 k++;
drh96f5b762004-05-16 16:24:36 +00005132
5133 /*
5134 ** The packing computed by the previous block is biased toward the siblings
5135 ** on the left side. The left siblings are always nearly full, while the
5136 ** right-most sibling might be nearly empty. This block of code attempts
5137 ** to adjust the packing of siblings to get a better balance.
5138 **
5139 ** This adjustment is more than an optimization. The packing above might
5140 ** be so out of balance as to be illegal. For example, the right-most
5141 ** sibling might be completely empty. This adjustment is not optional.
5142 */
drh6019e162001-07-02 17:51:45 +00005143 for(i=k-1; i>0; i--){
drh96f5b762004-05-16 16:24:36 +00005144 int szRight = szNew[i]; /* Size of sibling on the right */
5145 int szLeft = szNew[i-1]; /* Size of sibling on the left */
5146 int r; /* Index of right-most cell in left sibling */
5147 int d; /* Index of first cell to the left of right sibling */
5148
5149 r = cntNew[i-1] - 1;
5150 d = r + 1 - leafData;
danielk1977634f2982005-03-28 08:44:07 +00005151 assert( d<nMaxCells );
5152 assert( r<nMaxCells );
drh43605152004-05-29 21:46:49 +00005153 while( szRight==0 || szRight+szCell[d]+2<=szLeft-(szCell[r]+2) ){
5154 szRight += szCell[d] + 2;
5155 szLeft -= szCell[r] + 2;
drh6019e162001-07-02 17:51:45 +00005156 cntNew[i-1]--;
drh96f5b762004-05-16 16:24:36 +00005157 r = cntNew[i-1] - 1;
5158 d = r + 1 - leafData;
drh6019e162001-07-02 17:51:45 +00005159 }
drh96f5b762004-05-16 16:24:36 +00005160 szNew[i] = szRight;
5161 szNew[i-1] = szLeft;
drh6019e162001-07-02 17:51:45 +00005162 }
drh09d0deb2005-08-02 17:13:09 +00005163
5164 /* Either we found one or more cells (cntnew[0])>0) or we are the
5165 ** a virtual root page. A virtual root page is when the real root
5166 ** page is page 1 and we are the only child of that page.
5167 */
5168 assert( cntNew[0]>0 || (pParent->pgno==1 && pParent->nCell==0) );
drh8b2f49b2001-06-08 00:21:52 +00005169
5170 /*
drh6b308672002-07-08 02:16:37 +00005171 ** Allocate k new pages. Reuse old pages where possible.
drh8b2f49b2001-06-08 00:21:52 +00005172 */
drh4b70f112004-05-02 21:12:19 +00005173 assert( pPage->pgno>1 );
5174 pageFlags = pPage->aData[0];
drh14acc042001-06-10 19:56:58 +00005175 for(i=0; i<k; i++){
drhda200cc2004-05-09 11:51:38 +00005176 MemPage *pNew;
drh6b308672002-07-08 02:16:37 +00005177 if( i<nOld ){
drhda200cc2004-05-09 11:51:38 +00005178 pNew = apNew[i] = apOld[i];
drh6b308672002-07-08 02:16:37 +00005179 pgnoNew[i] = pgnoOld[i];
5180 apOld[i] = 0;
danielk19773b8a05f2007-03-19 17:44:26 +00005181 rc = sqlite3PagerWrite(pNew->pDbPage);
drhf5345442007-04-09 12:45:02 +00005182 nNew++;
danielk197728129562005-01-11 10:25:06 +00005183 if( rc ) goto balance_cleanup;
drh6b308672002-07-08 02:16:37 +00005184 }else{
drh7aa8f852006-03-28 00:24:44 +00005185 assert( i>0 );
drh4f0c5872007-03-26 22:05:01 +00005186 rc = allocateBtreePage(pBt, &pNew, &pgnoNew[i], pgnoNew[i-1], 0);
drh6b308672002-07-08 02:16:37 +00005187 if( rc ) goto balance_cleanup;
drhda200cc2004-05-09 11:51:38 +00005188 apNew[i] = pNew;
drhf5345442007-04-09 12:45:02 +00005189 nNew++;
drh6b308672002-07-08 02:16:37 +00005190 }
drhda200cc2004-05-09 11:51:38 +00005191 zeroPage(pNew, pageFlags);
drh8b2f49b2001-06-08 00:21:52 +00005192 }
5193
danielk1977299b1872004-11-22 10:02:10 +00005194 /* Free any old pages that were not reused as new pages.
5195 */
5196 while( i<nOld ){
5197 rc = freePage(apOld[i]);
5198 if( rc ) goto balance_cleanup;
5199 releasePage(apOld[i]);
5200 apOld[i] = 0;
5201 i++;
5202 }
5203
drh8b2f49b2001-06-08 00:21:52 +00005204 /*
drhf9ffac92002-03-02 19:00:31 +00005205 ** Put the new pages in accending order. This helps to
5206 ** keep entries in the disk file in order so that a scan
5207 ** of the table is a linear scan through the file. That
5208 ** in turn helps the operating system to deliver pages
5209 ** from the disk more rapidly.
5210 **
5211 ** An O(n^2) insertion sort algorithm is used, but since
drhc3b70572003-01-04 19:44:07 +00005212 ** n is never more than NB (a small constant), that should
5213 ** not be a problem.
drhf9ffac92002-03-02 19:00:31 +00005214 **
drhc3b70572003-01-04 19:44:07 +00005215 ** When NB==3, this one optimization makes the database
5216 ** about 25% faster for large insertions and deletions.
drhf9ffac92002-03-02 19:00:31 +00005217 */
5218 for(i=0; i<k-1; i++){
5219 int minV = pgnoNew[i];
5220 int minI = i;
5221 for(j=i+1; j<k; j++){
drh7d02cb72003-06-04 16:24:39 +00005222 if( pgnoNew[j]<(unsigned)minV ){
drhf9ffac92002-03-02 19:00:31 +00005223 minI = j;
5224 minV = pgnoNew[j];
5225 }
5226 }
5227 if( minI>i ){
5228 int t;
5229 MemPage *pT;
5230 t = pgnoNew[i];
5231 pT = apNew[i];
5232 pgnoNew[i] = pgnoNew[minI];
5233 apNew[i] = apNew[minI];
5234 pgnoNew[minI] = t;
5235 apNew[minI] = pT;
5236 }
5237 }
drha2fce642004-06-05 00:01:44 +00005238 TRACE(("BALANCE: old: %d %d %d new: %d(%d) %d(%d) %d(%d) %d(%d) %d(%d)\n",
drh24cd67e2004-05-10 16:18:47 +00005239 pgnoOld[0],
5240 nOld>=2 ? pgnoOld[1] : 0,
5241 nOld>=3 ? pgnoOld[2] : 0,
drh10c0fa62004-05-18 12:50:17 +00005242 pgnoNew[0], szNew[0],
5243 nNew>=2 ? pgnoNew[1] : 0, nNew>=2 ? szNew[1] : 0,
5244 nNew>=3 ? pgnoNew[2] : 0, nNew>=3 ? szNew[2] : 0,
drha2fce642004-06-05 00:01:44 +00005245 nNew>=4 ? pgnoNew[3] : 0, nNew>=4 ? szNew[3] : 0,
5246 nNew>=5 ? pgnoNew[4] : 0, nNew>=5 ? szNew[4] : 0));
drh24cd67e2004-05-10 16:18:47 +00005247
drhf9ffac92002-03-02 19:00:31 +00005248 /*
drh14acc042001-06-10 19:56:58 +00005249 ** Evenly distribute the data in apCell[] across the new pages.
5250 ** Insert divider cells into pParent as necessary.
5251 */
5252 j = 0;
5253 for(i=0; i<nNew; i++){
danielk1977ac11ee62005-01-15 12:45:51 +00005254 /* Assemble the new sibling page. */
drh14acc042001-06-10 19:56:58 +00005255 MemPage *pNew = apNew[i];
drh19642e52005-03-29 13:17:45 +00005256 assert( j<nMaxCells );
drh4b70f112004-05-02 21:12:19 +00005257 assert( pNew->pgno==pgnoNew[i] );
drhfa1a98a2004-05-14 19:08:17 +00005258 assemblePage(pNew, cntNew[i]-j, &apCell[j], &szCell[j]);
drh09d0deb2005-08-02 17:13:09 +00005259 assert( pNew->nCell>0 || (nNew==1 && cntNew[0]==0) );
drh43605152004-05-29 21:46:49 +00005260 assert( pNew->nOverflow==0 );
danielk1977ac11ee62005-01-15 12:45:51 +00005261
5262#ifndef SQLITE_OMIT_AUTOVACUUM
5263 /* If this is an auto-vacuum database, update the pointer map entries
5264 ** that point to the siblings that were rearranged. These can be: left
5265 ** children of cells, the right-child of the page, or overflow pages
5266 ** pointed to by cells.
5267 */
5268 if( pBt->autoVacuum ){
5269 for(k=j; k<cntNew[i]; k++){
danielk1977634f2982005-03-28 08:44:07 +00005270 assert( k<nMaxCells );
danielk1977ac11ee62005-01-15 12:45:51 +00005271 if( aFrom[k]==0xFF || apCopy[aFrom[k]]->pgno!=pNew->pgno ){
danielk197779a40da2005-01-16 08:00:01 +00005272 rc = ptrmapPutOvfl(pNew, k-j);
5273 if( rc!=SQLITE_OK ){
5274 goto balance_cleanup;
danielk1977ac11ee62005-01-15 12:45:51 +00005275 }
5276 }
5277 }
5278 }
5279#endif
5280
5281 j = cntNew[i];
5282
5283 /* If the sibling page assembled above was not the right-most sibling,
5284 ** insert a divider cell into the parent page.
5285 */
drh14acc042001-06-10 19:56:58 +00005286 if( i<nNew-1 && j<nCell ){
drh8b18dd42004-05-12 19:18:15 +00005287 u8 *pCell;
drh24cd67e2004-05-10 16:18:47 +00005288 u8 *pTemp;
drh8b18dd42004-05-12 19:18:15 +00005289 int sz;
danielk1977634f2982005-03-28 08:44:07 +00005290
5291 assert( j<nMaxCells );
drh8b18dd42004-05-12 19:18:15 +00005292 pCell = apCell[j];
5293 sz = szCell[j] + leafCorrection;
drh4b70f112004-05-02 21:12:19 +00005294 if( !pNew->leaf ){
drh43605152004-05-29 21:46:49 +00005295 memcpy(&pNew->aData[8], pCell, 4);
drh24cd67e2004-05-10 16:18:47 +00005296 pTemp = 0;
drh8b18dd42004-05-12 19:18:15 +00005297 }else if( leafData ){
drhfd131da2007-08-07 17:13:03 +00005298 /* If the tree is a leaf-data tree, and the siblings are leaves,
danielk1977ac11ee62005-01-15 12:45:51 +00005299 ** then there is no divider cell in apCell[]. Instead, the divider
5300 ** cell consists of the integer key for the right-most cell of
5301 ** the sibling-page assembled above only.
5302 */
drh6f11bef2004-05-13 01:12:56 +00005303 CellInfo info;
drh8b18dd42004-05-12 19:18:15 +00005304 j--;
drh16a9b832007-05-05 18:39:25 +00005305 sqlite3BtreeParseCellPtr(pNew, apCell[j], &info);
drhb6f41482004-05-14 01:58:11 +00005306 pCell = &aSpace[iSpace];
drhb026e052007-05-02 01:34:31 +00005307 fillInCell(pParent, pCell, 0, info.nKey, 0, 0, 0, &sz);
drhb6f41482004-05-14 01:58:11 +00005308 iSpace += sz;
drh07d183d2005-05-01 22:52:42 +00005309 assert( iSpace<=pBt->pageSize*5 );
drh8b18dd42004-05-12 19:18:15 +00005310 pTemp = 0;
drh4b70f112004-05-02 21:12:19 +00005311 }else{
5312 pCell -= 4;
drhb6f41482004-05-14 01:58:11 +00005313 pTemp = &aSpace[iSpace];
5314 iSpace += sz;
drh07d183d2005-05-01 22:52:42 +00005315 assert( iSpace<=pBt->pageSize*5 );
danielk19774aeff622007-05-12 09:30:47 +00005316 /* Obscure case for non-leaf-data trees: If the cell at pCell was
drh85b623f2007-12-13 21:54:09 +00005317 ** previously stored on a leaf node, and its reported size was 4
danielk19774aeff622007-05-12 09:30:47 +00005318 ** bytes, then it may actually be smaller than this
5319 ** (see sqlite3BtreeParseCellPtr(), 4 bytes is the minimum size of
drh85b623f2007-12-13 21:54:09 +00005320 ** any cell). But it is important to pass the correct size to
danielk19774aeff622007-05-12 09:30:47 +00005321 ** insertCell(), so reparse the cell now.
5322 **
5323 ** Note that this can never happen in an SQLite data file, as all
5324 ** cells are at least 4 bytes. It only happens in b-trees used
5325 ** to evaluate "IN (SELECT ...)" and similar clauses.
5326 */
5327 if( szCell[j]==4 ){
5328 assert(leafCorrection==4);
5329 sz = cellSizePtr(pParent, pCell);
5330 }
drh4b70f112004-05-02 21:12:19 +00005331 }
danielk1977a3ad5e72005-01-07 08:56:44 +00005332 rc = insertCell(pParent, nxDiv, pCell, sz, pTemp, 4);
danielk1977e80463b2004-11-03 03:01:16 +00005333 if( rc!=SQLITE_OK ) goto balance_cleanup;
drh43605152004-05-29 21:46:49 +00005334 put4byte(findOverflowCell(pParent,nxDiv), pNew->pgno);
danielk1977ac11ee62005-01-15 12:45:51 +00005335#ifndef SQLITE_OMIT_AUTOVACUUM
5336 /* If this is an auto-vacuum database, and not a leaf-data tree,
5337 ** then update the pointer map with an entry for the overflow page
5338 ** that the cell just inserted points to (if any).
5339 */
5340 if( pBt->autoVacuum && !leafData ){
danielk197779a40da2005-01-16 08:00:01 +00005341 rc = ptrmapPutOvfl(pParent, nxDiv);
5342 if( rc!=SQLITE_OK ){
5343 goto balance_cleanup;
danielk1977ac11ee62005-01-15 12:45:51 +00005344 }
5345 }
5346#endif
drh14acc042001-06-10 19:56:58 +00005347 j++;
5348 nxDiv++;
5349 }
5350 }
drh6019e162001-07-02 17:51:45 +00005351 assert( j==nCell );
drh7aa8f852006-03-28 00:24:44 +00005352 assert( nOld>0 );
5353 assert( nNew>0 );
drh4b70f112004-05-02 21:12:19 +00005354 if( (pageFlags & PTF_LEAF)==0 ){
drh43605152004-05-29 21:46:49 +00005355 memcpy(&apNew[nNew-1]->aData[8], &apCopy[nOld-1]->aData[8], 4);
drh14acc042001-06-10 19:56:58 +00005356 }
drh43605152004-05-29 21:46:49 +00005357 if( nxDiv==pParent->nCell+pParent->nOverflow ){
drh4b70f112004-05-02 21:12:19 +00005358 /* Right-most sibling is the right-most child of pParent */
drh43605152004-05-29 21:46:49 +00005359 put4byte(&pParent->aData[pParent->hdrOffset+8], pgnoNew[nNew-1]);
drh4b70f112004-05-02 21:12:19 +00005360 }else{
5361 /* Right-most sibling is the left child of the first entry in pParent
5362 ** past the right-most divider entry */
drh43605152004-05-29 21:46:49 +00005363 put4byte(findOverflowCell(pParent, nxDiv), pgnoNew[nNew-1]);
drh14acc042001-06-10 19:56:58 +00005364 }
5365
5366 /*
5367 ** Reparent children of all cells.
drh8b2f49b2001-06-08 00:21:52 +00005368 */
5369 for(i=0; i<nNew; i++){
danielk1977afcdd022004-10-31 16:25:42 +00005370 rc = reparentChildPages(apNew[i]);
5371 if( rc!=SQLITE_OK ) goto balance_cleanup;
drh8b2f49b2001-06-08 00:21:52 +00005372 }
danielk1977afcdd022004-10-31 16:25:42 +00005373 rc = reparentChildPages(pParent);
5374 if( rc!=SQLITE_OK ) goto balance_cleanup;
drh8b2f49b2001-06-08 00:21:52 +00005375
5376 /*
drh3a4c1412004-05-09 20:40:11 +00005377 ** Balance the parent page. Note that the current page (pPage) might
danielk1977ac11ee62005-01-15 12:45:51 +00005378 ** have been added to the freelist so it might no longer be initialized.
drh3a4c1412004-05-09 20:40:11 +00005379 ** But the parent page will always be initialized.
drh8b2f49b2001-06-08 00:21:52 +00005380 */
drhda200cc2004-05-09 11:51:38 +00005381 assert( pParent->isInit );
danielk1977ac245ec2005-01-14 13:50:11 +00005382 rc = balance(pParent, 0);
drhda200cc2004-05-09 11:51:38 +00005383
drh8b2f49b2001-06-08 00:21:52 +00005384 /*
drh14acc042001-06-10 19:56:58 +00005385 ** Cleanup before returning.
drh8b2f49b2001-06-08 00:21:52 +00005386 */
drh14acc042001-06-10 19:56:58 +00005387balance_cleanup:
drh17435752007-08-16 04:30:38 +00005388 sqlite3_free(apCell);
drh8b2f49b2001-06-08 00:21:52 +00005389 for(i=0; i<nOld; i++){
drh91025292004-05-03 19:49:32 +00005390 releasePage(apOld[i]);
drh8b2f49b2001-06-08 00:21:52 +00005391 }
drh14acc042001-06-10 19:56:58 +00005392 for(i=0; i<nNew; i++){
drh91025292004-05-03 19:49:32 +00005393 releasePage(apNew[i]);
drh8b2f49b2001-06-08 00:21:52 +00005394 }
drh91025292004-05-03 19:49:32 +00005395 releasePage(pParent);
drh3a4c1412004-05-09 20:40:11 +00005396 TRACE(("BALANCE: finished with %d: old=%d new=%d cells=%d\n",
5397 pPage->pgno, nOld, nNew, nCell));
drh8b2f49b2001-06-08 00:21:52 +00005398 return rc;
5399}
5400
5401/*
drh43605152004-05-29 21:46:49 +00005402** This routine is called for the root page of a btree when the root
5403** page contains no cells. This is an opportunity to make the tree
5404** shallower by one level.
5405*/
5406static int balance_shallower(MemPage *pPage){
5407 MemPage *pChild; /* The only child page of pPage */
5408 Pgno pgnoChild; /* Page number for pChild */
drh2e38c322004-09-03 18:38:44 +00005409 int rc = SQLITE_OK; /* Return code from subprocedures */
danielk1977aef0bf62005-12-30 16:28:01 +00005410 BtShared *pBt; /* The main BTree structure */
drh2e38c322004-09-03 18:38:44 +00005411 int mxCellPerPage; /* Maximum number of cells per page */
5412 u8 **apCell; /* All cells from pages being balanced */
drha9121e42008-02-19 14:59:35 +00005413 u16 *szCell; /* Local size of all cells */
drh43605152004-05-29 21:46:49 +00005414
5415 assert( pPage->pParent==0 );
5416 assert( pPage->nCell==0 );
drh1fee73e2007-08-29 04:00:57 +00005417 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
drh2e38c322004-09-03 18:38:44 +00005418 pBt = pPage->pBt;
5419 mxCellPerPage = MX_CELL(pBt);
drha9121e42008-02-19 14:59:35 +00005420 apCell = sqlite3_malloc( mxCellPerPage*(sizeof(u8*)+sizeof(u16)) );
drh2e38c322004-09-03 18:38:44 +00005421 if( apCell==0 ) return SQLITE_NOMEM;
drha9121e42008-02-19 14:59:35 +00005422 szCell = (u16*)&apCell[mxCellPerPage];
drh43605152004-05-29 21:46:49 +00005423 if( pPage->leaf ){
5424 /* The table is completely empty */
5425 TRACE(("BALANCE: empty table %d\n", pPage->pgno));
5426 }else{
5427 /* The root page is empty but has one child. Transfer the
5428 ** information from that one child into the root page if it
5429 ** will fit. This reduces the depth of the tree by one.
5430 **
5431 ** If the root page is page 1, it has less space available than
5432 ** its child (due to the 100 byte header that occurs at the beginning
5433 ** of the database fle), so it might not be able to hold all of the
5434 ** information currently contained in the child. If this is the
5435 ** case, then do not do the transfer. Leave page 1 empty except
5436 ** for the right-pointer to the child page. The child page becomes
5437 ** the virtual root of the tree.
5438 */
5439 pgnoChild = get4byte(&pPage->aData[pPage->hdrOffset+8]);
5440 assert( pgnoChild>0 );
danielk1977ad0132d2008-06-07 08:58:22 +00005441 assert( pgnoChild<=pagerPagecount(pPage->pBt->pPager) );
drh16a9b832007-05-05 18:39:25 +00005442 rc = sqlite3BtreeGetPage(pPage->pBt, pgnoChild, &pChild, 0);
drh2e38c322004-09-03 18:38:44 +00005443 if( rc ) goto end_shallow_balance;
drh43605152004-05-29 21:46:49 +00005444 if( pPage->pgno==1 ){
drh16a9b832007-05-05 18:39:25 +00005445 rc = sqlite3BtreeInitPage(pChild, pPage);
drh2e38c322004-09-03 18:38:44 +00005446 if( rc ) goto end_shallow_balance;
drh43605152004-05-29 21:46:49 +00005447 assert( pChild->nOverflow==0 );
5448 if( pChild->nFree>=100 ){
5449 /* The child information will fit on the root page, so do the
5450 ** copy */
5451 int i;
5452 zeroPage(pPage, pChild->aData[0]);
5453 for(i=0; i<pChild->nCell; i++){
danielk19771cc5ed82007-05-16 17:28:43 +00005454 apCell[i] = findCell(pChild,i);
drh43605152004-05-29 21:46:49 +00005455 szCell[i] = cellSizePtr(pChild, apCell[i]);
5456 }
5457 assemblePage(pPage, pChild->nCell, apCell, szCell);
danielk1977ae825582004-11-23 09:06:55 +00005458 /* Copy the right-pointer of the child to the parent. */
5459 put4byte(&pPage->aData[pPage->hdrOffset+8],
5460 get4byte(&pChild->aData[pChild->hdrOffset+8]));
drh43605152004-05-29 21:46:49 +00005461 freePage(pChild);
5462 TRACE(("BALANCE: child %d transfer to page 1\n", pChild->pgno));
5463 }else{
5464 /* The child has more information that will fit on the root.
5465 ** The tree is already balanced. Do nothing. */
5466 TRACE(("BALANCE: child %d will not fit on page 1\n", pChild->pgno));
5467 }
5468 }else{
5469 memcpy(pPage->aData, pChild->aData, pPage->pBt->usableSize);
5470 pPage->isInit = 0;
5471 pPage->pParent = 0;
drh16a9b832007-05-05 18:39:25 +00005472 rc = sqlite3BtreeInitPage(pPage, 0);
drh43605152004-05-29 21:46:49 +00005473 assert( rc==SQLITE_OK );
5474 freePage(pChild);
5475 TRACE(("BALANCE: transfer child %d into root %d\n",
5476 pChild->pgno, pPage->pgno));
5477 }
danielk1977afcdd022004-10-31 16:25:42 +00005478 rc = reparentChildPages(pPage);
danielk1977ac11ee62005-01-15 12:45:51 +00005479 assert( pPage->nOverflow==0 );
5480#ifndef SQLITE_OMIT_AUTOVACUUM
5481 if( pBt->autoVacuum ){
danielk1977aac0a382005-01-16 11:07:06 +00005482 int i;
danielk1977ac11ee62005-01-15 12:45:51 +00005483 for(i=0; i<pPage->nCell; i++){
danielk197779a40da2005-01-16 08:00:01 +00005484 rc = ptrmapPutOvfl(pPage, i);
5485 if( rc!=SQLITE_OK ){
5486 goto end_shallow_balance;
danielk1977ac11ee62005-01-15 12:45:51 +00005487 }
5488 }
5489 }
5490#endif
drh43605152004-05-29 21:46:49 +00005491 releasePage(pChild);
5492 }
drh2e38c322004-09-03 18:38:44 +00005493end_shallow_balance:
drh17435752007-08-16 04:30:38 +00005494 sqlite3_free(apCell);
drh2e38c322004-09-03 18:38:44 +00005495 return rc;
drh43605152004-05-29 21:46:49 +00005496}
5497
5498
5499/*
5500** The root page is overfull
5501**
5502** When this happens, Create a new child page and copy the
5503** contents of the root into the child. Then make the root
5504** page an empty page with rightChild pointing to the new
5505** child. Finally, call balance_internal() on the new child
5506** to cause it to split.
5507*/
5508static int balance_deeper(MemPage *pPage){
5509 int rc; /* Return value from subprocedures */
5510 MemPage *pChild; /* Pointer to a new child page */
5511 Pgno pgnoChild; /* Page number of the new child page */
danielk1977aef0bf62005-12-30 16:28:01 +00005512 BtShared *pBt; /* The BTree */
drh43605152004-05-29 21:46:49 +00005513 int usableSize; /* Total usable size of a page */
5514 u8 *data; /* Content of the parent page */
5515 u8 *cdata; /* Content of the child page */
5516 int hdr; /* Offset to page header in parent */
5517 int brk; /* Offset to content of first cell in parent */
5518
5519 assert( pPage->pParent==0 );
5520 assert( pPage->nOverflow>0 );
5521 pBt = pPage->pBt;
drh1fee73e2007-08-29 04:00:57 +00005522 assert( sqlite3_mutex_held(pBt->mutex) );
drh4f0c5872007-03-26 22:05:01 +00005523 rc = allocateBtreePage(pBt, &pChild, &pgnoChild, pPage->pgno, 0);
drh43605152004-05-29 21:46:49 +00005524 if( rc ) return rc;
danielk19773b8a05f2007-03-19 17:44:26 +00005525 assert( sqlite3PagerIswriteable(pChild->pDbPage) );
drh43605152004-05-29 21:46:49 +00005526 usableSize = pBt->usableSize;
5527 data = pPage->aData;
5528 hdr = pPage->hdrOffset;
5529 brk = get2byte(&data[hdr+5]);
5530 cdata = pChild->aData;
5531 memcpy(cdata, &data[hdr], pPage->cellOffset+2*pPage->nCell-hdr);
5532 memcpy(&cdata[brk], &data[brk], usableSize-brk);
danielk1977c7dc7532004-11-17 10:22:03 +00005533 assert( pChild->isInit==0 );
drh16a9b832007-05-05 18:39:25 +00005534 rc = sqlite3BtreeInitPage(pChild, pPage);
danielk19776b456a22005-03-21 04:04:02 +00005535 if( rc ) goto balancedeeper_out;
drh43605152004-05-29 21:46:49 +00005536 memcpy(pChild->aOvfl, pPage->aOvfl, pPage->nOverflow*sizeof(pPage->aOvfl[0]));
5537 pChild->nOverflow = pPage->nOverflow;
5538 if( pChild->nOverflow ){
5539 pChild->nFree = 0;
5540 }
5541 assert( pChild->nCell==pPage->nCell );
5542 zeroPage(pPage, pChild->aData[0] & ~PTF_LEAF);
5543 put4byte(&pPage->aData[pPage->hdrOffset+8], pgnoChild);
5544 TRACE(("BALANCE: copy root %d into %d\n", pPage->pgno, pChild->pgno));
danielk19774e17d142005-01-16 09:06:33 +00005545#ifndef SQLITE_OMIT_AUTOVACUUM
danielk1977ac11ee62005-01-15 12:45:51 +00005546 if( pBt->autoVacuum ){
5547 int i;
5548 rc = ptrmapPut(pBt, pChild->pgno, PTRMAP_BTREE, pPage->pgno);
danielk19776b456a22005-03-21 04:04:02 +00005549 if( rc ) goto balancedeeper_out;
danielk1977ac11ee62005-01-15 12:45:51 +00005550 for(i=0; i<pChild->nCell; i++){
danielk197779a40da2005-01-16 08:00:01 +00005551 rc = ptrmapPutOvfl(pChild, i);
5552 if( rc!=SQLITE_OK ){
5553 return rc;
danielk1977ac11ee62005-01-15 12:45:51 +00005554 }
5555 }
5556 }
danielk19774e17d142005-01-16 09:06:33 +00005557#endif
drh43605152004-05-29 21:46:49 +00005558 rc = balance_nonroot(pChild);
danielk19776b456a22005-03-21 04:04:02 +00005559
5560balancedeeper_out:
drh43605152004-05-29 21:46:49 +00005561 releasePage(pChild);
5562 return rc;
5563}
5564
5565/*
5566** Decide if the page pPage needs to be balanced. If balancing is
5567** required, call the appropriate balancing routine.
5568*/
danielk1977ac245ec2005-01-14 13:50:11 +00005569static int balance(MemPage *pPage, int insert){
drh43605152004-05-29 21:46:49 +00005570 int rc = SQLITE_OK;
drh1fee73e2007-08-29 04:00:57 +00005571 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
drh43605152004-05-29 21:46:49 +00005572 if( pPage->pParent==0 ){
danielk19776e465eb2007-08-21 13:11:00 +00005573 rc = sqlite3PagerWrite(pPage->pDbPage);
5574 if( rc==SQLITE_OK && pPage->nOverflow>0 ){
drh43605152004-05-29 21:46:49 +00005575 rc = balance_deeper(pPage);
5576 }
danielk1977687566d2004-11-02 12:56:41 +00005577 if( rc==SQLITE_OK && pPage->nCell==0 ){
drh43605152004-05-29 21:46:49 +00005578 rc = balance_shallower(pPage);
5579 }
5580 }else{
danielk1977ac245ec2005-01-14 13:50:11 +00005581 if( pPage->nOverflow>0 ||
5582 (!insert && pPage->nFree>pPage->pBt->usableSize*2/3) ){
drh43605152004-05-29 21:46:49 +00005583 rc = balance_nonroot(pPage);
5584 }
5585 }
5586 return rc;
5587}
5588
5589/*
drh8dcd7ca2004-08-08 19:43:29 +00005590** This routine checks all cursors that point to table pgnoRoot.
drh980b1a72006-08-16 16:42:48 +00005591** If any of those cursors were opened with wrFlag==0 in a different
5592** database connection (a database connection that shares the pager
5593** cache with the current connection) and that other connection
5594** is not in the ReadUncommmitted state, then this routine returns
5595** SQLITE_LOCKED.
danielk1977299b1872004-11-22 10:02:10 +00005596**
5597** In addition to checking for read-locks (where a read-lock
5598** means a cursor opened with wrFlag==0) this routine also moves
drh16a9b832007-05-05 18:39:25 +00005599** all write cursors so that they are pointing to the
drh980b1a72006-08-16 16:42:48 +00005600** first Cell on the root page. This is necessary because an insert
danielk1977299b1872004-11-22 10:02:10 +00005601** or delete might change the number of cells on a page or delete
5602** a page entirely and we do not want to leave any cursors
5603** pointing to non-existant pages or cells.
drhf74b8d92002-09-01 23:20:45 +00005604*/
drh980b1a72006-08-16 16:42:48 +00005605static int checkReadLocks(Btree *pBtree, Pgno pgnoRoot, BtCursor *pExclude){
danielk1977299b1872004-11-22 10:02:10 +00005606 BtCursor *p;
drh980b1a72006-08-16 16:42:48 +00005607 BtShared *pBt = pBtree->pBt;
drhe5fe6902007-12-07 18:55:28 +00005608 sqlite3 *db = pBtree->db;
drh1fee73e2007-08-29 04:00:57 +00005609 assert( sqlite3BtreeHoldsMutex(pBtree) );
danielk1977299b1872004-11-22 10:02:10 +00005610 for(p=pBt->pCursor; p; p=p->pNext){
drh980b1a72006-08-16 16:42:48 +00005611 if( p==pExclude ) continue;
5612 if( p->eState!=CURSOR_VALID ) continue;
5613 if( p->pgnoRoot!=pgnoRoot ) continue;
5614 if( p->wrFlag==0 ){
drhe5fe6902007-12-07 18:55:28 +00005615 sqlite3 *dbOther = p->pBtree->db;
drh980b1a72006-08-16 16:42:48 +00005616 if( dbOther==0 ||
5617 (dbOther!=db && (dbOther->flags & SQLITE_ReadUncommitted)==0) ){
5618 return SQLITE_LOCKED;
5619 }
5620 }else if( p->pPage->pgno!=p->pgnoRoot ){
danielk1977299b1872004-11-22 10:02:10 +00005621 moveToRoot(p);
5622 }
5623 }
drhf74b8d92002-09-01 23:20:45 +00005624 return SQLITE_OK;
5625}
5626
5627/*
danielk197752ae7242008-03-25 14:24:56 +00005628** Make sure pBt->pTmpSpace points to an allocation of
5629** MX_CELL_SIZE(pBt) bytes.
5630*/
5631static void allocateTempSpace(BtShared *pBt){
5632 if( !pBt->pTmpSpace ){
5633 pBt->pTmpSpace = sqlite3_malloc(MX_CELL_SIZE(pBt));
5634 }
5635}
5636
5637/*
drh3b7511c2001-05-26 13:15:44 +00005638** Insert a new record into the BTree. The key is given by (pKey,nKey)
5639** and the data is given by (pData,nData). The cursor is used only to
drh91025292004-05-03 19:49:32 +00005640** define what table the record should be inserted into. The cursor
drh4b70f112004-05-02 21:12:19 +00005641** is left pointing at a random location.
5642**
5643** For an INTKEY table, only the nKey value of the key is used. pKey is
5644** ignored. For a ZERODATA table, the pData and nData are both ignored.
drh3b7511c2001-05-26 13:15:44 +00005645*/
drh3aac2dd2004-04-26 14:10:20 +00005646int sqlite3BtreeInsert(
drh5c4d9702001-08-20 00:33:58 +00005647 BtCursor *pCur, /* Insert data into the table of this cursor */
drh4a1c3802004-05-12 15:15:47 +00005648 const void *pKey, i64 nKey, /* The key of the new record */
drhe4d90812007-03-29 05:51:49 +00005649 const void *pData, int nData, /* The data of the new record */
drhb026e052007-05-02 01:34:31 +00005650 int nZero, /* Number of extra 0 bytes to append to data */
drhe4d90812007-03-29 05:51:49 +00005651 int appendBias /* True if this is likely an append */
drh3b7511c2001-05-26 13:15:44 +00005652){
drh3b7511c2001-05-26 13:15:44 +00005653 int rc;
5654 int loc;
drh14acc042001-06-10 19:56:58 +00005655 int szNew;
drh3b7511c2001-05-26 13:15:44 +00005656 MemPage *pPage;
drhd677b3d2007-08-20 22:48:41 +00005657 Btree *p = pCur->pBtree;
5658 BtShared *pBt = p->pBt;
drha34b6762004-05-07 13:30:42 +00005659 unsigned char *oldCell;
drh2e38c322004-09-03 18:38:44 +00005660 unsigned char *newCell = 0;
drh3b7511c2001-05-26 13:15:44 +00005661
drh1fee73e2007-08-29 04:00:57 +00005662 assert( cursorHoldsMutex(pCur) );
danielk1977aef0bf62005-12-30 16:28:01 +00005663 if( pBt->inTransaction!=TRANS_WRITE ){
drhf74b8d92002-09-01 23:20:45 +00005664 /* Must start a transaction before doing an insert */
drhd677b3d2007-08-20 22:48:41 +00005665 rc = pBt->readOnly ? SQLITE_READONLY : SQLITE_ERROR;
drhd677b3d2007-08-20 22:48:41 +00005666 return rc;
drh8b2f49b2001-06-08 00:21:52 +00005667 }
drhf74b8d92002-09-01 23:20:45 +00005668 assert( !pBt->readOnly );
drhecdc7532001-09-23 02:35:53 +00005669 if( !pCur->wrFlag ){
5670 return SQLITE_PERM; /* Cursor not open for writing */
5671 }
drh980b1a72006-08-16 16:42:48 +00005672 if( checkReadLocks(pCur->pBtree, pCur->pgnoRoot, pCur) ){
drhf74b8d92002-09-01 23:20:45 +00005673 return SQLITE_LOCKED; /* The table pCur points to has a read lock */
5674 }
drhfb982642007-08-30 01:19:59 +00005675 if( pCur->eState==CURSOR_FAULT ){
5676 return pCur->skip;
5677 }
danielk1977da184232006-01-05 11:34:32 +00005678
5679 /* Save the positions of any other cursors open on this table */
drhbf700f32007-03-31 02:36:44 +00005680 clearCursorPosition(pCur);
danielk19772e94d4d2006-01-09 05:36:27 +00005681 if(
danielk19772e94d4d2006-01-09 05:36:27 +00005682 SQLITE_OK!=(rc = saveAllCursors(pBt, pCur->pgnoRoot, pCur)) ||
drhe14006d2008-03-25 17:23:32 +00005683 SQLITE_OK!=(rc = sqlite3BtreeMoveto(pCur, pKey, 0, nKey, appendBias, &loc))
danielk19772e94d4d2006-01-09 05:36:27 +00005684 ){
danielk1977da184232006-01-05 11:34:32 +00005685 return rc;
5686 }
5687
drh14acc042001-06-10 19:56:58 +00005688 pPage = pCur->pPage;
drh4a1c3802004-05-12 15:15:47 +00005689 assert( pPage->intKey || nKey>=0 );
drh8b18dd42004-05-12 19:18:15 +00005690 assert( pPage->leaf || !pPage->leafData );
drh3a4c1412004-05-09 20:40:11 +00005691 TRACE(("INSERT: table=%d nkey=%lld ndata=%d page=%d %s\n",
5692 pCur->pgnoRoot, nKey, nData, pPage->pgno,
5693 loc==0 ? "overwrite" : "new entry"));
drh7aa128d2002-06-21 13:09:16 +00005694 assert( pPage->isInit );
danielk197752ae7242008-03-25 14:24:56 +00005695 allocateTempSpace(pBt);
5696 newCell = pBt->pTmpSpace;
drh2e38c322004-09-03 18:38:44 +00005697 if( newCell==0 ) return SQLITE_NOMEM;
drhb026e052007-05-02 01:34:31 +00005698 rc = fillInCell(pPage, newCell, pKey, nKey, pData, nData, nZero, &szNew);
drh2e38c322004-09-03 18:38:44 +00005699 if( rc ) goto end_insert;
drh43605152004-05-29 21:46:49 +00005700 assert( szNew==cellSizePtr(pPage, newCell) );
drh2e38c322004-09-03 18:38:44 +00005701 assert( szNew<=MX_CELL_SIZE(pBt) );
danielk1977da184232006-01-05 11:34:32 +00005702 if( loc==0 && CURSOR_VALID==pCur->eState ){
drha9121e42008-02-19 14:59:35 +00005703 u16 szOld;
drha34b6762004-05-07 13:30:42 +00005704 assert( pCur->idx>=0 && pCur->idx<pPage->nCell );
danielk19776e465eb2007-08-21 13:11:00 +00005705 rc = sqlite3PagerWrite(pPage->pDbPage);
5706 if( rc ){
5707 goto end_insert;
5708 }
danielk19771cc5ed82007-05-16 17:28:43 +00005709 oldCell = findCell(pPage, pCur->idx);
drh4b70f112004-05-02 21:12:19 +00005710 if( !pPage->leaf ){
drh43605152004-05-29 21:46:49 +00005711 memcpy(newCell, oldCell, 4);
drh4b70f112004-05-02 21:12:19 +00005712 }
drh43605152004-05-29 21:46:49 +00005713 szOld = cellSizePtr(pPage, oldCell);
drh4b70f112004-05-02 21:12:19 +00005714 rc = clearCell(pPage, oldCell);
drh2e38c322004-09-03 18:38:44 +00005715 if( rc ) goto end_insert;
drh4b70f112004-05-02 21:12:19 +00005716 dropCell(pPage, pCur->idx, szOld);
drh7c717f72001-06-24 20:39:41 +00005717 }else if( loc<0 && pPage->nCell>0 ){
drh4b70f112004-05-02 21:12:19 +00005718 assert( pPage->leaf );
drh14acc042001-06-10 19:56:58 +00005719 pCur->idx++;
drh271efa52004-05-30 19:19:05 +00005720 pCur->info.nSize = 0;
drha2c20e42008-03-29 16:01:04 +00005721 pCur->validNKey = 0;
drh14acc042001-06-10 19:56:58 +00005722 }else{
drh4b70f112004-05-02 21:12:19 +00005723 assert( pPage->leaf );
drh3b7511c2001-05-26 13:15:44 +00005724 }
danielk1977a3ad5e72005-01-07 08:56:44 +00005725 rc = insertCell(pPage, pCur->idx, newCell, szNew, 0, 0);
danielk1977e80463b2004-11-03 03:01:16 +00005726 if( rc!=SQLITE_OK ) goto end_insert;
danielk1977ac245ec2005-01-14 13:50:11 +00005727 rc = balance(pPage, 1);
drh23e11ca2004-05-04 17:27:28 +00005728 /* sqlite3BtreePageDump(pCur->pBt, pCur->pgnoRoot, 1); */
drh3fc190c2001-09-14 03:24:23 +00005729 /* fflush(stdout); */
danielk1977299b1872004-11-22 10:02:10 +00005730 if( rc==SQLITE_OK ){
5731 moveToRoot(pCur);
5732 }
drh2e38c322004-09-03 18:38:44 +00005733end_insert:
drh5e2f8b92001-05-28 00:41:15 +00005734 return rc;
5735}
5736
5737/*
drh4b70f112004-05-02 21:12:19 +00005738** Delete the entry that the cursor is pointing to. The cursor
5739** is left pointing at a random location.
drh3b7511c2001-05-26 13:15:44 +00005740*/
drh3aac2dd2004-04-26 14:10:20 +00005741int sqlite3BtreeDelete(BtCursor *pCur){
drh5e2f8b92001-05-28 00:41:15 +00005742 MemPage *pPage = pCur->pPage;
drh4b70f112004-05-02 21:12:19 +00005743 unsigned char *pCell;
drh5e2f8b92001-05-28 00:41:15 +00005744 int rc;
danielk1977cfe9a692004-06-16 12:00:29 +00005745 Pgno pgnoChild = 0;
drhd677b3d2007-08-20 22:48:41 +00005746 Btree *p = pCur->pBtree;
5747 BtShared *pBt = p->pBt;
drh8b2f49b2001-06-08 00:21:52 +00005748
drh1fee73e2007-08-29 04:00:57 +00005749 assert( cursorHoldsMutex(pCur) );
drh7aa128d2002-06-21 13:09:16 +00005750 assert( pPage->isInit );
danielk1977aef0bf62005-12-30 16:28:01 +00005751 if( pBt->inTransaction!=TRANS_WRITE ){
drhf74b8d92002-09-01 23:20:45 +00005752 /* Must start a transaction before doing a delete */
drhd677b3d2007-08-20 22:48:41 +00005753 rc = pBt->readOnly ? SQLITE_READONLY : SQLITE_ERROR;
drhd677b3d2007-08-20 22:48:41 +00005754 return rc;
drh8b2f49b2001-06-08 00:21:52 +00005755 }
drhf74b8d92002-09-01 23:20:45 +00005756 assert( !pBt->readOnly );
drhfb982642007-08-30 01:19:59 +00005757 if( pCur->eState==CURSOR_FAULT ){
5758 return pCur->skip;
5759 }
drhbd03cae2001-06-02 02:40:57 +00005760 if( pCur->idx >= pPage->nCell ){
5761 return SQLITE_ERROR; /* The cursor is not pointing to anything */
5762 }
drhecdc7532001-09-23 02:35:53 +00005763 if( !pCur->wrFlag ){
5764 return SQLITE_PERM; /* Did not open this cursor for writing */
5765 }
drh980b1a72006-08-16 16:42:48 +00005766 if( checkReadLocks(pCur->pBtree, pCur->pgnoRoot, pCur) ){
drhf74b8d92002-09-01 23:20:45 +00005767 return SQLITE_LOCKED; /* The table pCur points to has a read lock */
5768 }
danielk1977da184232006-01-05 11:34:32 +00005769
5770 /* Restore the current cursor position (a no-op if the cursor is not in
5771 ** CURSOR_REQUIRESEEK state) and save the positions of any other cursors
danielk19773b8a05f2007-03-19 17:44:26 +00005772 ** open on the same table. Then call sqlite3PagerWrite() on the page
danielk1977da184232006-01-05 11:34:32 +00005773 ** that the entry will be deleted from.
5774 */
5775 if(
drhbf700f32007-03-31 02:36:44 +00005776 (rc = restoreOrClearCursorPosition(pCur))!=0 ||
drhd1167392006-01-23 13:00:35 +00005777 (rc = saveAllCursors(pBt, pCur->pgnoRoot, pCur))!=0 ||
danielk19773b8a05f2007-03-19 17:44:26 +00005778 (rc = sqlite3PagerWrite(pPage->pDbPage))!=0
danielk1977da184232006-01-05 11:34:32 +00005779 ){
5780 return rc;
5781 }
danielk1977e6efa742004-11-10 11:55:10 +00005782
drh85b623f2007-12-13 21:54:09 +00005783 /* Locate the cell within its page and leave pCell pointing to the
danielk1977e6efa742004-11-10 11:55:10 +00005784 ** data. The clearCell() call frees any overflow pages associated with the
5785 ** cell. The cell itself is still intact.
5786 */
danielk19771cc5ed82007-05-16 17:28:43 +00005787 pCell = findCell(pPage, pCur->idx);
drh4b70f112004-05-02 21:12:19 +00005788 if( !pPage->leaf ){
drh43605152004-05-29 21:46:49 +00005789 pgnoChild = get4byte(pCell);
drh4b70f112004-05-02 21:12:19 +00005790 }
danielk197728129562005-01-11 10:25:06 +00005791 rc = clearCell(pPage, pCell);
drhd677b3d2007-08-20 22:48:41 +00005792 if( rc ){
drhd677b3d2007-08-20 22:48:41 +00005793 return rc;
5794 }
danielk1977e6efa742004-11-10 11:55:10 +00005795
drh4b70f112004-05-02 21:12:19 +00005796 if( !pPage->leaf ){
drh14acc042001-06-10 19:56:58 +00005797 /*
drh5e00f6c2001-09-13 13:46:56 +00005798 ** The entry we are about to delete is not a leaf so if we do not
drh9ca7d3b2001-06-28 11:50:21 +00005799 ** do something we will leave a hole on an internal page.
5800 ** We have to fill the hole by moving in a cell from a leaf. The
5801 ** next Cell after the one to be deleted is guaranteed to exist and
danielk1977299b1872004-11-22 10:02:10 +00005802 ** to be a leaf so we can use it.
drh5e2f8b92001-05-28 00:41:15 +00005803 */
drh14acc042001-06-10 19:56:58 +00005804 BtCursor leafCur;
drh4b70f112004-05-02 21:12:19 +00005805 unsigned char *pNext;
danielk1977299b1872004-11-22 10:02:10 +00005806 int notUsed;
danielk19776b456a22005-03-21 04:04:02 +00005807 unsigned char *tempCell = 0;
drh8b18dd42004-05-12 19:18:15 +00005808 assert( !pPage->leafData );
drh16a9b832007-05-05 18:39:25 +00005809 sqlite3BtreeGetTempCursor(pCur, &leafCur);
danielk1977299b1872004-11-22 10:02:10 +00005810 rc = sqlite3BtreeNext(&leafCur, &notUsed);
danielk19776b456a22005-03-21 04:04:02 +00005811 if( rc==SQLITE_OK ){
danielk19773b8a05f2007-03-19 17:44:26 +00005812 rc = sqlite3PagerWrite(leafCur.pPage->pDbPage);
danielk19776b456a22005-03-21 04:04:02 +00005813 }
5814 if( rc==SQLITE_OK ){
drha9121e42008-02-19 14:59:35 +00005815 u16 szNext;
danielk19776b456a22005-03-21 04:04:02 +00005816 TRACE(("DELETE: table=%d delete internal from %d replace from leaf %d\n",
5817 pCur->pgnoRoot, pPage->pgno, leafCur.pPage->pgno));
5818 dropCell(pPage, pCur->idx, cellSizePtr(pPage, pCell));
danielk19771cc5ed82007-05-16 17:28:43 +00005819 pNext = findCell(leafCur.pPage, leafCur.idx);
danielk19776b456a22005-03-21 04:04:02 +00005820 szNext = cellSizePtr(leafCur.pPage, pNext);
5821 assert( MX_CELL_SIZE(pBt)>=szNext+4 );
danielk197752ae7242008-03-25 14:24:56 +00005822 allocateTempSpace(pBt);
5823 tempCell = pBt->pTmpSpace;
danielk19776b456a22005-03-21 04:04:02 +00005824 if( tempCell==0 ){
5825 rc = SQLITE_NOMEM;
5826 }
danielk19778ea1cfa2008-01-01 06:19:02 +00005827 if( rc==SQLITE_OK ){
5828 rc = insertCell(pPage, pCur->idx, pNext-4, szNext+4, tempCell, 0);
5829 }
5830 if( rc==SQLITE_OK ){
5831 put4byte(findOverflowCell(pPage, pCur->idx), pgnoChild);
5832 rc = balance(pPage, 0);
5833 }
5834 if( rc==SQLITE_OK ){
5835 dropCell(leafCur.pPage, leafCur.idx, szNext);
5836 rc = balance(leafCur.pPage, 0);
5837 }
danielk19776b456a22005-03-21 04:04:02 +00005838 }
drh16a9b832007-05-05 18:39:25 +00005839 sqlite3BtreeReleaseTempCursor(&leafCur);
drh5e2f8b92001-05-28 00:41:15 +00005840 }else{
danielk1977299b1872004-11-22 10:02:10 +00005841 TRACE(("DELETE: table=%d delete from leaf %d\n",
5842 pCur->pgnoRoot, pPage->pgno));
5843 dropCell(pPage, pCur->idx, cellSizePtr(pPage, pCell));
danielk1977ac245ec2005-01-14 13:50:11 +00005844 rc = balance(pPage, 0);
drh5e2f8b92001-05-28 00:41:15 +00005845 }
danielk19776b456a22005-03-21 04:04:02 +00005846 if( rc==SQLITE_OK ){
5847 moveToRoot(pCur);
5848 }
drh5e2f8b92001-05-28 00:41:15 +00005849 return rc;
drh3b7511c2001-05-26 13:15:44 +00005850}
drh8b2f49b2001-06-08 00:21:52 +00005851
5852/*
drhc6b52df2002-01-04 03:09:29 +00005853** Create a new BTree table. Write into *piTable the page
5854** number for the root page of the new table.
5855**
drhab01f612004-05-22 02:55:23 +00005856** The type of type is determined by the flags parameter. Only the
5857** following values of flags are currently in use. Other values for
5858** flags might not work:
5859**
5860** BTREE_INTKEY|BTREE_LEAFDATA Used for SQL tables with rowid keys
5861** BTREE_ZERODATA Used for SQL indices
drh8b2f49b2001-06-08 00:21:52 +00005862*/
drhd677b3d2007-08-20 22:48:41 +00005863static int btreeCreateTable(Btree *p, int *piTable, int flags){
danielk1977aef0bf62005-12-30 16:28:01 +00005864 BtShared *pBt = p->pBt;
drh8b2f49b2001-06-08 00:21:52 +00005865 MemPage *pRoot;
5866 Pgno pgnoRoot;
5867 int rc;
drhd677b3d2007-08-20 22:48:41 +00005868
drh1fee73e2007-08-29 04:00:57 +00005869 assert( sqlite3BtreeHoldsMutex(p) );
danielk1977aef0bf62005-12-30 16:28:01 +00005870 if( pBt->inTransaction!=TRANS_WRITE ){
drhf74b8d92002-09-01 23:20:45 +00005871 /* Must start a transaction first */
drhd677b3d2007-08-20 22:48:41 +00005872 rc = pBt->readOnly ? SQLITE_READONLY : SQLITE_ERROR;
5873 return rc;
drh8b2f49b2001-06-08 00:21:52 +00005874 }
danielk197728129562005-01-11 10:25:06 +00005875 assert( !pBt->readOnly );
danielk1977e6efa742004-11-10 11:55:10 +00005876
danielk1977003ba062004-11-04 02:57:33 +00005877#ifdef SQLITE_OMIT_AUTOVACUUM
drh4f0c5872007-03-26 22:05:01 +00005878 rc = allocateBtreePage(pBt, &pRoot, &pgnoRoot, 1, 0);
drhd677b3d2007-08-20 22:48:41 +00005879 if( rc ){
5880 return rc;
5881 }
danielk1977003ba062004-11-04 02:57:33 +00005882#else
danielk1977687566d2004-11-02 12:56:41 +00005883 if( pBt->autoVacuum ){
danielk1977003ba062004-11-04 02:57:33 +00005884 Pgno pgnoMove; /* Move a page here to make room for the root-page */
5885 MemPage *pPageMove; /* The page to move to. */
5886
danielk197720713f32007-05-03 11:43:33 +00005887 /* Creating a new table may probably require moving an existing database
5888 ** to make room for the new tables root page. In case this page turns
5889 ** out to be an overflow page, delete all overflow page-map caches
5890 ** held by open cursors.
5891 */
danielk197792d4d7a2007-05-04 12:05:56 +00005892 invalidateAllOverflowCache(pBt);
danielk197720713f32007-05-03 11:43:33 +00005893
danielk1977003ba062004-11-04 02:57:33 +00005894 /* Read the value of meta[3] from the database to determine where the
5895 ** root page of the new table should go. meta[3] is the largest root-page
5896 ** created so far, so the new root-page is (meta[3]+1).
5897 */
danielk1977aef0bf62005-12-30 16:28:01 +00005898 rc = sqlite3BtreeGetMeta(p, 4, &pgnoRoot);
drhd677b3d2007-08-20 22:48:41 +00005899 if( rc!=SQLITE_OK ){
5900 return rc;
5901 }
danielk1977003ba062004-11-04 02:57:33 +00005902 pgnoRoot++;
5903
danielk1977599fcba2004-11-08 07:13:13 +00005904 /* The new root-page may not be allocated on a pointer-map page, or the
5905 ** PENDING_BYTE page.
5906 */
drh72190432008-01-31 14:54:43 +00005907 while( pgnoRoot==PTRMAP_PAGENO(pBt, pgnoRoot) ||
danielk1977599fcba2004-11-08 07:13:13 +00005908 pgnoRoot==PENDING_BYTE_PAGE(pBt) ){
danielk1977003ba062004-11-04 02:57:33 +00005909 pgnoRoot++;
5910 }
5911 assert( pgnoRoot>=3 );
5912
5913 /* Allocate a page. The page that currently resides at pgnoRoot will
5914 ** be moved to the allocated page (unless the allocated page happens
5915 ** to reside at pgnoRoot).
5916 */
drh4f0c5872007-03-26 22:05:01 +00005917 rc = allocateBtreePage(pBt, &pPageMove, &pgnoMove, pgnoRoot, 1);
danielk1977003ba062004-11-04 02:57:33 +00005918 if( rc!=SQLITE_OK ){
danielk1977687566d2004-11-02 12:56:41 +00005919 return rc;
5920 }
danielk1977003ba062004-11-04 02:57:33 +00005921
5922 if( pgnoMove!=pgnoRoot ){
danielk1977f35843b2007-04-07 15:03:17 +00005923 /* pgnoRoot is the page that will be used for the root-page of
5924 ** the new table (assuming an error did not occur). But we were
5925 ** allocated pgnoMove. If required (i.e. if it was not allocated
5926 ** by extending the file), the current page at position pgnoMove
5927 ** is already journaled.
5928 */
danielk1977003ba062004-11-04 02:57:33 +00005929 u8 eType;
5930 Pgno iPtrPage;
5931
5932 releasePage(pPageMove);
danielk1977f35843b2007-04-07 15:03:17 +00005933
5934 /* Move the page currently at pgnoRoot to pgnoMove. */
drh16a9b832007-05-05 18:39:25 +00005935 rc = sqlite3BtreeGetPage(pBt, pgnoRoot, &pRoot, 0);
danielk1977003ba062004-11-04 02:57:33 +00005936 if( rc!=SQLITE_OK ){
5937 return rc;
5938 }
5939 rc = ptrmapGet(pBt, pgnoRoot, &eType, &iPtrPage);
drhccae6022005-02-26 17:31:26 +00005940 if( rc!=SQLITE_OK || eType==PTRMAP_ROOTPAGE || eType==PTRMAP_FREEPAGE ){
danielk1977003ba062004-11-04 02:57:33 +00005941 releasePage(pRoot);
5942 return rc;
5943 }
drhccae6022005-02-26 17:31:26 +00005944 assert( eType!=PTRMAP_ROOTPAGE );
5945 assert( eType!=PTRMAP_FREEPAGE );
danielk19773b8a05f2007-03-19 17:44:26 +00005946 rc = sqlite3PagerWrite(pRoot->pDbPage);
danielk19775fd057a2005-03-09 13:09:43 +00005947 if( rc!=SQLITE_OK ){
5948 releasePage(pRoot);
5949 return rc;
5950 }
danielk1977003ba062004-11-04 02:57:33 +00005951 rc = relocatePage(pBt, pRoot, eType, iPtrPage, pgnoMove);
5952 releasePage(pRoot);
danielk1977f35843b2007-04-07 15:03:17 +00005953
5954 /* Obtain the page at pgnoRoot */
danielk1977003ba062004-11-04 02:57:33 +00005955 if( rc!=SQLITE_OK ){
5956 return rc;
5957 }
drh16a9b832007-05-05 18:39:25 +00005958 rc = sqlite3BtreeGetPage(pBt, pgnoRoot, &pRoot, 0);
danielk1977003ba062004-11-04 02:57:33 +00005959 if( rc!=SQLITE_OK ){
5960 return rc;
5961 }
danielk19773b8a05f2007-03-19 17:44:26 +00005962 rc = sqlite3PagerWrite(pRoot->pDbPage);
danielk1977003ba062004-11-04 02:57:33 +00005963 if( rc!=SQLITE_OK ){
5964 releasePage(pRoot);
5965 return rc;
5966 }
5967 }else{
5968 pRoot = pPageMove;
5969 }
5970
danielk197742741be2005-01-08 12:42:39 +00005971 /* Update the pointer-map and meta-data with the new root-page number. */
danielk1977003ba062004-11-04 02:57:33 +00005972 rc = ptrmapPut(pBt, pgnoRoot, PTRMAP_ROOTPAGE, 0);
5973 if( rc ){
5974 releasePage(pRoot);
5975 return rc;
5976 }
danielk1977aef0bf62005-12-30 16:28:01 +00005977 rc = sqlite3BtreeUpdateMeta(p, 4, pgnoRoot);
danielk1977003ba062004-11-04 02:57:33 +00005978 if( rc ){
5979 releasePage(pRoot);
5980 return rc;
5981 }
danielk197742741be2005-01-08 12:42:39 +00005982
danielk1977003ba062004-11-04 02:57:33 +00005983 }else{
drh4f0c5872007-03-26 22:05:01 +00005984 rc = allocateBtreePage(pBt, &pRoot, &pgnoRoot, 1, 0);
danielk1977003ba062004-11-04 02:57:33 +00005985 if( rc ) return rc;
danielk1977687566d2004-11-02 12:56:41 +00005986 }
5987#endif
danielk19773b8a05f2007-03-19 17:44:26 +00005988 assert( sqlite3PagerIswriteable(pRoot->pDbPage) );
drhde647132004-05-07 17:57:49 +00005989 zeroPage(pRoot, flags | PTF_LEAF);
danielk19773b8a05f2007-03-19 17:44:26 +00005990 sqlite3PagerUnref(pRoot->pDbPage);
drh8b2f49b2001-06-08 00:21:52 +00005991 *piTable = (int)pgnoRoot;
5992 return SQLITE_OK;
5993}
drhd677b3d2007-08-20 22:48:41 +00005994int sqlite3BtreeCreateTable(Btree *p, int *piTable, int flags){
5995 int rc;
5996 sqlite3BtreeEnter(p);
drhe5fe6902007-12-07 18:55:28 +00005997 p->pBt->db = p->db;
drhd677b3d2007-08-20 22:48:41 +00005998 rc = btreeCreateTable(p, piTable, flags);
5999 sqlite3BtreeLeave(p);
6000 return rc;
6001}
drh8b2f49b2001-06-08 00:21:52 +00006002
6003/*
6004** Erase the given database page and all its children. Return
6005** the page to the freelist.
6006*/
drh4b70f112004-05-02 21:12:19 +00006007static int clearDatabasePage(
danielk1977aef0bf62005-12-30 16:28:01 +00006008 BtShared *pBt, /* The BTree that contains the table */
drh4b70f112004-05-02 21:12:19 +00006009 Pgno pgno, /* Page number to clear */
6010 MemPage *pParent, /* Parent page. NULL for the root */
6011 int freePageFlag /* Deallocate page if true */
6012){
danielk19776b456a22005-03-21 04:04:02 +00006013 MemPage *pPage = 0;
drh8b2f49b2001-06-08 00:21:52 +00006014 int rc;
drh4b70f112004-05-02 21:12:19 +00006015 unsigned char *pCell;
6016 int i;
drh8b2f49b2001-06-08 00:21:52 +00006017
drh1fee73e2007-08-29 04:00:57 +00006018 assert( sqlite3_mutex_held(pBt->mutex) );
danielk1977ad0132d2008-06-07 08:58:22 +00006019 if( pgno>pagerPagecount(pBt->pPager) ){
drh49285702005-09-17 15:20:26 +00006020 return SQLITE_CORRUPT_BKPT;
danielk1977a1cb1832005-02-12 08:59:55 +00006021 }
6022
drhde647132004-05-07 17:57:49 +00006023 rc = getAndInitPage(pBt, pgno, &pPage, pParent);
danielk19776b456a22005-03-21 04:04:02 +00006024 if( rc ) goto cleardatabasepage_out;
drh4b70f112004-05-02 21:12:19 +00006025 for(i=0; i<pPage->nCell; i++){
danielk19771cc5ed82007-05-16 17:28:43 +00006026 pCell = findCell(pPage, i);
drh4b70f112004-05-02 21:12:19 +00006027 if( !pPage->leaf ){
drh43605152004-05-29 21:46:49 +00006028 rc = clearDatabasePage(pBt, get4byte(pCell), pPage->pParent, 1);
danielk19776b456a22005-03-21 04:04:02 +00006029 if( rc ) goto cleardatabasepage_out;
drh8b2f49b2001-06-08 00:21:52 +00006030 }
drh4b70f112004-05-02 21:12:19 +00006031 rc = clearCell(pPage, pCell);
danielk19776b456a22005-03-21 04:04:02 +00006032 if( rc ) goto cleardatabasepage_out;
drh8b2f49b2001-06-08 00:21:52 +00006033 }
drha34b6762004-05-07 13:30:42 +00006034 if( !pPage->leaf ){
drh43605152004-05-29 21:46:49 +00006035 rc = clearDatabasePage(pBt, get4byte(&pPage->aData[8]), pPage->pParent, 1);
danielk19776b456a22005-03-21 04:04:02 +00006036 if( rc ) goto cleardatabasepage_out;
drh2aa679f2001-06-25 02:11:07 +00006037 }
6038 if( freePageFlag ){
drh4b70f112004-05-02 21:12:19 +00006039 rc = freePage(pPage);
danielk19773b8a05f2007-03-19 17:44:26 +00006040 }else if( (rc = sqlite3PagerWrite(pPage->pDbPage))==0 ){
drh3a4c1412004-05-09 20:40:11 +00006041 zeroPage(pPage, pPage->aData[0] | PTF_LEAF);
drh2aa679f2001-06-25 02:11:07 +00006042 }
danielk19776b456a22005-03-21 04:04:02 +00006043
6044cleardatabasepage_out:
drh4b70f112004-05-02 21:12:19 +00006045 releasePage(pPage);
drh2aa679f2001-06-25 02:11:07 +00006046 return rc;
drh8b2f49b2001-06-08 00:21:52 +00006047}
6048
6049/*
drhab01f612004-05-22 02:55:23 +00006050** Delete all information from a single table in the database. iTable is
6051** the page number of the root of the table. After this routine returns,
6052** the root page is empty, but still exists.
6053**
6054** This routine will fail with SQLITE_LOCKED if there are any open
6055** read cursors on the table. Open write cursors are moved to the
6056** root of the table.
drh8b2f49b2001-06-08 00:21:52 +00006057*/
danielk1977aef0bf62005-12-30 16:28:01 +00006058int sqlite3BtreeClearTable(Btree *p, int iTable){
drh8b2f49b2001-06-08 00:21:52 +00006059 int rc;
danielk1977aef0bf62005-12-30 16:28:01 +00006060 BtShared *pBt = p->pBt;
drhd677b3d2007-08-20 22:48:41 +00006061 sqlite3BtreeEnter(p);
drhe5fe6902007-12-07 18:55:28 +00006062 pBt->db = p->db;
danielk1977aef0bf62005-12-30 16:28:01 +00006063 if( p->inTrans!=TRANS_WRITE ){
drhd677b3d2007-08-20 22:48:41 +00006064 rc = pBt->readOnly ? SQLITE_READONLY : SQLITE_ERROR;
6065 }else if( (rc = checkReadLocks(p, iTable, 0))!=SQLITE_OK ){
6066 /* nothing to do */
6067 }else if( SQLITE_OK!=(rc = saveAllCursors(pBt, iTable, 0)) ){
6068 /* nothing to do */
6069 }else{
6070 rc = clearDatabasePage(pBt, (Pgno)iTable, 0, 0);
drh8b2f49b2001-06-08 00:21:52 +00006071 }
drhd677b3d2007-08-20 22:48:41 +00006072 sqlite3BtreeLeave(p);
6073 return rc;
drh8b2f49b2001-06-08 00:21:52 +00006074}
6075
6076/*
6077** Erase all information in a table and add the root of the table to
6078** the freelist. Except, the root of the principle table (the one on
drhab01f612004-05-22 02:55:23 +00006079** page 1) is never added to the freelist.
6080**
6081** This routine will fail with SQLITE_LOCKED if there are any open
6082** cursors on the table.
drh205f48e2004-11-05 00:43:11 +00006083**
6084** If AUTOVACUUM is enabled and the page at iTable is not the last
6085** root page in the database file, then the last root page
6086** in the database file is moved into the slot formerly occupied by
6087** iTable and that last slot formerly occupied by the last root page
6088** is added to the freelist instead of iTable. In this say, all
6089** root pages are kept at the beginning of the database file, which
6090** is necessary for AUTOVACUUM to work right. *piMoved is set to the
6091** page number that used to be the last root page in the file before
6092** the move. If no page gets moved, *piMoved is set to 0.
6093** The last root page is recorded in meta[3] and the value of
6094** meta[3] is updated by this procedure.
drh8b2f49b2001-06-08 00:21:52 +00006095*/
drhd677b3d2007-08-20 22:48:41 +00006096static int btreeDropTable(Btree *p, int iTable, int *piMoved){
drh8b2f49b2001-06-08 00:21:52 +00006097 int rc;
danielk1977a0bf2652004-11-04 14:30:04 +00006098 MemPage *pPage = 0;
danielk1977aef0bf62005-12-30 16:28:01 +00006099 BtShared *pBt = p->pBt;
danielk1977a0bf2652004-11-04 14:30:04 +00006100
drh1fee73e2007-08-29 04:00:57 +00006101 assert( sqlite3BtreeHoldsMutex(p) );
danielk1977aef0bf62005-12-30 16:28:01 +00006102 if( p->inTrans!=TRANS_WRITE ){
drhf74b8d92002-09-01 23:20:45 +00006103 return pBt->readOnly ? SQLITE_READONLY : SQLITE_ERROR;
drh8b2f49b2001-06-08 00:21:52 +00006104 }
danielk1977a0bf2652004-11-04 14:30:04 +00006105
danielk1977e6efa742004-11-10 11:55:10 +00006106 /* It is illegal to drop a table if any cursors are open on the
6107 ** database. This is because in auto-vacuum mode the backend may
6108 ** need to move another root-page to fill a gap left by the deleted
6109 ** root page. If an open cursor was using this page a problem would
6110 ** occur.
6111 */
6112 if( pBt->pCursor ){
6113 return SQLITE_LOCKED;
drh5df72a52002-06-06 23:16:05 +00006114 }
danielk1977a0bf2652004-11-04 14:30:04 +00006115
drh16a9b832007-05-05 18:39:25 +00006116 rc = sqlite3BtreeGetPage(pBt, (Pgno)iTable, &pPage, 0);
drh2aa679f2001-06-25 02:11:07 +00006117 if( rc ) return rc;
danielk1977aef0bf62005-12-30 16:28:01 +00006118 rc = sqlite3BtreeClearTable(p, iTable);
danielk19776b456a22005-03-21 04:04:02 +00006119 if( rc ){
6120 releasePage(pPage);
6121 return rc;
6122 }
danielk1977a0bf2652004-11-04 14:30:04 +00006123
drh205f48e2004-11-05 00:43:11 +00006124 *piMoved = 0;
danielk1977a0bf2652004-11-04 14:30:04 +00006125
drh4b70f112004-05-02 21:12:19 +00006126 if( iTable>1 ){
danielk1977a0bf2652004-11-04 14:30:04 +00006127#ifdef SQLITE_OMIT_AUTOVACUUM
drha34b6762004-05-07 13:30:42 +00006128 rc = freePage(pPage);
danielk1977a0bf2652004-11-04 14:30:04 +00006129 releasePage(pPage);
6130#else
6131 if( pBt->autoVacuum ){
6132 Pgno maxRootPgno;
danielk1977aef0bf62005-12-30 16:28:01 +00006133 rc = sqlite3BtreeGetMeta(p, 4, &maxRootPgno);
danielk1977a0bf2652004-11-04 14:30:04 +00006134 if( rc!=SQLITE_OK ){
6135 releasePage(pPage);
6136 return rc;
6137 }
6138
6139 if( iTable==maxRootPgno ){
6140 /* If the table being dropped is the table with the largest root-page
6141 ** number in the database, put the root page on the free list.
6142 */
6143 rc = freePage(pPage);
6144 releasePage(pPage);
6145 if( rc!=SQLITE_OK ){
6146 return rc;
6147 }
6148 }else{
6149 /* The table being dropped does not have the largest root-page
6150 ** number in the database. So move the page that does into the
6151 ** gap left by the deleted root-page.
6152 */
6153 MemPage *pMove;
6154 releasePage(pPage);
drh16a9b832007-05-05 18:39:25 +00006155 rc = sqlite3BtreeGetPage(pBt, maxRootPgno, &pMove, 0);
danielk1977a0bf2652004-11-04 14:30:04 +00006156 if( rc!=SQLITE_OK ){
6157 return rc;
6158 }
6159 rc = relocatePage(pBt, pMove, PTRMAP_ROOTPAGE, 0, iTable);
6160 releasePage(pMove);
6161 if( rc!=SQLITE_OK ){
6162 return rc;
6163 }
drh16a9b832007-05-05 18:39:25 +00006164 rc = sqlite3BtreeGetPage(pBt, maxRootPgno, &pMove, 0);
danielk1977a0bf2652004-11-04 14:30:04 +00006165 if( rc!=SQLITE_OK ){
6166 return rc;
6167 }
6168 rc = freePage(pMove);
6169 releasePage(pMove);
6170 if( rc!=SQLITE_OK ){
6171 return rc;
6172 }
6173 *piMoved = maxRootPgno;
6174 }
6175
danielk1977599fcba2004-11-08 07:13:13 +00006176 /* Set the new 'max-root-page' value in the database header. This
6177 ** is the old value less one, less one more if that happens to
6178 ** be a root-page number, less one again if that is the
6179 ** PENDING_BYTE_PAGE.
6180 */
danielk197787a6e732004-11-05 12:58:25 +00006181 maxRootPgno--;
danielk1977599fcba2004-11-08 07:13:13 +00006182 if( maxRootPgno==PENDING_BYTE_PAGE(pBt) ){
6183 maxRootPgno--;
6184 }
danielk1977266664d2006-02-10 08:24:21 +00006185 if( maxRootPgno==PTRMAP_PAGENO(pBt, maxRootPgno) ){
danielk197787a6e732004-11-05 12:58:25 +00006186 maxRootPgno--;
6187 }
danielk1977599fcba2004-11-08 07:13:13 +00006188 assert( maxRootPgno!=PENDING_BYTE_PAGE(pBt) );
6189
danielk1977aef0bf62005-12-30 16:28:01 +00006190 rc = sqlite3BtreeUpdateMeta(p, 4, maxRootPgno);
danielk1977a0bf2652004-11-04 14:30:04 +00006191 }else{
6192 rc = freePage(pPage);
6193 releasePage(pPage);
6194 }
6195#endif
drh2aa679f2001-06-25 02:11:07 +00006196 }else{
danielk1977a0bf2652004-11-04 14:30:04 +00006197 /* If sqlite3BtreeDropTable was called on page 1. */
drha34b6762004-05-07 13:30:42 +00006198 zeroPage(pPage, PTF_INTKEY|PTF_LEAF );
danielk1977a0bf2652004-11-04 14:30:04 +00006199 releasePage(pPage);
drh8b2f49b2001-06-08 00:21:52 +00006200 }
drh8b2f49b2001-06-08 00:21:52 +00006201 return rc;
6202}
drhd677b3d2007-08-20 22:48:41 +00006203int sqlite3BtreeDropTable(Btree *p, int iTable, int *piMoved){
6204 int rc;
6205 sqlite3BtreeEnter(p);
drhe5fe6902007-12-07 18:55:28 +00006206 p->pBt->db = p->db;
drhd677b3d2007-08-20 22:48:41 +00006207 rc = btreeDropTable(p, iTable, piMoved);
6208 sqlite3BtreeLeave(p);
6209 return rc;
6210}
drh8b2f49b2001-06-08 00:21:52 +00006211
drh001bbcb2003-03-19 03:14:00 +00006212
drh8b2f49b2001-06-08 00:21:52 +00006213/*
drh23e11ca2004-05-04 17:27:28 +00006214** Read the meta-information out of a database file. Meta[0]
6215** is the number of free pages currently in the database. Meta[1]
drha3b321d2004-05-11 09:31:31 +00006216** through meta[15] are available for use by higher layers. Meta[0]
6217** is read-only, the others are read/write.
6218**
6219** The schema layer numbers meta values differently. At the schema
6220** layer (and the SetCookie and ReadCookie opcodes) the number of
6221** free pages is not visible. So Cookie[0] is the same as Meta[1].
drh8b2f49b2001-06-08 00:21:52 +00006222*/
danielk1977aef0bf62005-12-30 16:28:01 +00006223int sqlite3BtreeGetMeta(Btree *p, int idx, u32 *pMeta){
danielk19773b8a05f2007-03-19 17:44:26 +00006224 DbPage *pDbPage;
drh8b2f49b2001-06-08 00:21:52 +00006225 int rc;
drh4b70f112004-05-02 21:12:19 +00006226 unsigned char *pP1;
danielk1977aef0bf62005-12-30 16:28:01 +00006227 BtShared *pBt = p->pBt;
drh8b2f49b2001-06-08 00:21:52 +00006228
drhd677b3d2007-08-20 22:48:41 +00006229 sqlite3BtreeEnter(p);
drhe5fe6902007-12-07 18:55:28 +00006230 pBt->db = p->db;
drhd677b3d2007-08-20 22:48:41 +00006231
danielk1977da184232006-01-05 11:34:32 +00006232 /* Reading a meta-data value requires a read-lock on page 1 (and hence
6233 ** the sqlite_master table. We grab this lock regardless of whether or
6234 ** not the SQLITE_ReadUncommitted flag is set (the table rooted at page
6235 ** 1 is treated as a special case by queryTableLock() and lockTable()).
6236 */
6237 rc = queryTableLock(p, 1, READ_LOCK);
6238 if( rc!=SQLITE_OK ){
drhd677b3d2007-08-20 22:48:41 +00006239 sqlite3BtreeLeave(p);
danielk1977da184232006-01-05 11:34:32 +00006240 return rc;
6241 }
6242
drh23e11ca2004-05-04 17:27:28 +00006243 assert( idx>=0 && idx<=15 );
danielk19773b8a05f2007-03-19 17:44:26 +00006244 rc = sqlite3PagerGet(pBt->pPager, 1, &pDbPage);
drhd677b3d2007-08-20 22:48:41 +00006245 if( rc ){
6246 sqlite3BtreeLeave(p);
6247 return rc;
6248 }
danielk19773b8a05f2007-03-19 17:44:26 +00006249 pP1 = (unsigned char *)sqlite3PagerGetData(pDbPage);
drh23e11ca2004-05-04 17:27:28 +00006250 *pMeta = get4byte(&pP1[36 + idx*4]);
danielk19773b8a05f2007-03-19 17:44:26 +00006251 sqlite3PagerUnref(pDbPage);
drhae157872004-08-14 19:20:09 +00006252
danielk1977599fcba2004-11-08 07:13:13 +00006253 /* If autovacuumed is disabled in this build but we are trying to
6254 ** access an autovacuumed database, then make the database readonly.
6255 */
danielk1977003ba062004-11-04 02:57:33 +00006256#ifdef SQLITE_OMIT_AUTOVACUUM
drhae157872004-08-14 19:20:09 +00006257 if( idx==4 && *pMeta>0 ) pBt->readOnly = 1;
danielk1977003ba062004-11-04 02:57:33 +00006258#endif
drhae157872004-08-14 19:20:09 +00006259
danielk1977da184232006-01-05 11:34:32 +00006260 /* Grab the read-lock on page 1. */
6261 rc = lockTable(p, 1, READ_LOCK);
drhd677b3d2007-08-20 22:48:41 +00006262 sqlite3BtreeLeave(p);
danielk1977da184232006-01-05 11:34:32 +00006263 return rc;
drh8b2f49b2001-06-08 00:21:52 +00006264}
6265
6266/*
drh23e11ca2004-05-04 17:27:28 +00006267** Write meta-information back into the database. Meta[0] is
6268** read-only and may not be written.
drh8b2f49b2001-06-08 00:21:52 +00006269*/
danielk1977aef0bf62005-12-30 16:28:01 +00006270int sqlite3BtreeUpdateMeta(Btree *p, int idx, u32 iMeta){
6271 BtShared *pBt = p->pBt;
drh4b70f112004-05-02 21:12:19 +00006272 unsigned char *pP1;
drha34b6762004-05-07 13:30:42 +00006273 int rc;
drh23e11ca2004-05-04 17:27:28 +00006274 assert( idx>=1 && idx<=15 );
drhd677b3d2007-08-20 22:48:41 +00006275 sqlite3BtreeEnter(p);
drhe5fe6902007-12-07 18:55:28 +00006276 pBt->db = p->db;
danielk1977aef0bf62005-12-30 16:28:01 +00006277 if( p->inTrans!=TRANS_WRITE ){
drhd677b3d2007-08-20 22:48:41 +00006278 rc = pBt->readOnly ? SQLITE_READONLY : SQLITE_ERROR;
6279 }else{
6280 assert( pBt->pPage1!=0 );
6281 pP1 = pBt->pPage1->aData;
6282 rc = sqlite3PagerWrite(pBt->pPage1->pDbPage);
6283 if( rc==SQLITE_OK ){
6284 put4byte(&pP1[36 + idx*4], iMeta);
danielk19774152e672007-09-12 17:01:45 +00006285#ifndef SQLITE_OMIT_AUTOVACUUM
drhd677b3d2007-08-20 22:48:41 +00006286 if( idx==7 ){
6287 assert( pBt->autoVacuum || iMeta==0 );
6288 assert( iMeta==0 || iMeta==1 );
6289 pBt->incrVacuum = iMeta;
6290 }
danielk19774152e672007-09-12 17:01:45 +00006291#endif
drhd677b3d2007-08-20 22:48:41 +00006292 }
drh5df72a52002-06-06 23:16:05 +00006293 }
drhd677b3d2007-08-20 22:48:41 +00006294 sqlite3BtreeLeave(p);
6295 return rc;
drh8b2f49b2001-06-08 00:21:52 +00006296}
drh8c42ca92001-06-22 19:15:00 +00006297
drhf328bc82004-05-10 23:29:49 +00006298/*
6299** Return the flag byte at the beginning of the page that the cursor
6300** is currently pointing to.
6301*/
6302int sqlite3BtreeFlags(BtCursor *pCur){
danielk1977da184232006-01-05 11:34:32 +00006303 /* TODO: What about CURSOR_REQUIRESEEK state? Probably need to call
drh777e4c42006-01-13 04:31:58 +00006304 ** restoreOrClearCursorPosition() here.
danielk1977da184232006-01-05 11:34:32 +00006305 */
danielk1977e448dc42008-01-02 11:50:51 +00006306 MemPage *pPage;
6307 restoreOrClearCursorPosition(pCur);
6308 pPage = pCur->pPage;
drh1fee73e2007-08-29 04:00:57 +00006309 assert( cursorHoldsMutex(pCur) );
drhd0679ed2007-08-28 22:24:34 +00006310 assert( pPage->pBt==pCur->pBt );
drhf328bc82004-05-10 23:29:49 +00006311 return pPage ? pPage->aData[pPage->hdrOffset] : 0;
6312}
6313
drhdd793422001-06-28 01:54:48 +00006314
drhdd793422001-06-28 01:54:48 +00006315/*
drh5eddca62001-06-30 21:53:53 +00006316** Return the pager associated with a BTree. This routine is used for
6317** testing and debugging only.
drhdd793422001-06-28 01:54:48 +00006318*/
danielk1977aef0bf62005-12-30 16:28:01 +00006319Pager *sqlite3BtreePager(Btree *p){
6320 return p->pBt->pPager;
drhdd793422001-06-28 01:54:48 +00006321}
drh5eddca62001-06-30 21:53:53 +00006322
drhb7f91642004-10-31 02:22:47 +00006323#ifndef SQLITE_OMIT_INTEGRITY_CHECK
drh5eddca62001-06-30 21:53:53 +00006324/*
6325** Append a message to the error message string.
6326*/
drh2e38c322004-09-03 18:38:44 +00006327static void checkAppendMsg(
6328 IntegrityCk *pCheck,
6329 char *zMsg1,
6330 const char *zFormat,
6331 ...
6332){
6333 va_list ap;
6334 char *zMsg2;
drh1dcdbc02007-01-27 02:24:54 +00006335 if( !pCheck->mxErr ) return;
6336 pCheck->mxErr--;
6337 pCheck->nErr++;
drh2e38c322004-09-03 18:38:44 +00006338 va_start(ap, zFormat);
danielk19771e536952007-08-16 10:09:01 +00006339 zMsg2 = sqlite3VMPrintf(0, zFormat, ap);
drh2e38c322004-09-03 18:38:44 +00006340 va_end(ap);
6341 if( zMsg1==0 ) zMsg1 = "";
drh5eddca62001-06-30 21:53:53 +00006342 if( pCheck->zErrMsg ){
6343 char *zOld = pCheck->zErrMsg;
6344 pCheck->zErrMsg = 0;
danielk19774adee202004-05-08 08:23:19 +00006345 sqlite3SetString(&pCheck->zErrMsg, zOld, "\n", zMsg1, zMsg2, (char*)0);
drh17435752007-08-16 04:30:38 +00006346 sqlite3_free(zOld);
drh5eddca62001-06-30 21:53:53 +00006347 }else{
danielk19774adee202004-05-08 08:23:19 +00006348 sqlite3SetString(&pCheck->zErrMsg, zMsg1, zMsg2, (char*)0);
drh5eddca62001-06-30 21:53:53 +00006349 }
drh17435752007-08-16 04:30:38 +00006350 sqlite3_free(zMsg2);
drh5eddca62001-06-30 21:53:53 +00006351}
drhb7f91642004-10-31 02:22:47 +00006352#endif /* SQLITE_OMIT_INTEGRITY_CHECK */
drh5eddca62001-06-30 21:53:53 +00006353
drhb7f91642004-10-31 02:22:47 +00006354#ifndef SQLITE_OMIT_INTEGRITY_CHECK
drh5eddca62001-06-30 21:53:53 +00006355/*
6356** Add 1 to the reference count for page iPage. If this is the second
6357** reference to the page, add an error message to pCheck->zErrMsg.
6358** Return 1 if there are 2 ore more references to the page and 0 if
6359** if this is the first reference to the page.
6360**
6361** Also check that the page number is in bounds.
6362*/
drhaaab5722002-02-19 13:39:21 +00006363static int checkRef(IntegrityCk *pCheck, int iPage, char *zContext){
drh5eddca62001-06-30 21:53:53 +00006364 if( iPage==0 ) return 1;
drh0de8c112002-07-06 16:32:14 +00006365 if( iPage>pCheck->nPage || iPage<0 ){
drh2e38c322004-09-03 18:38:44 +00006366 checkAppendMsg(pCheck, zContext, "invalid page number %d", iPage);
drh5eddca62001-06-30 21:53:53 +00006367 return 1;
6368 }
6369 if( pCheck->anRef[iPage]==1 ){
drh2e38c322004-09-03 18:38:44 +00006370 checkAppendMsg(pCheck, zContext, "2nd reference to page %d", iPage);
drh5eddca62001-06-30 21:53:53 +00006371 return 1;
6372 }
6373 return (pCheck->anRef[iPage]++)>1;
6374}
6375
danielk1977afcdd022004-10-31 16:25:42 +00006376#ifndef SQLITE_OMIT_AUTOVACUUM
6377/*
6378** Check that the entry in the pointer-map for page iChild maps to
6379** page iParent, pointer type ptrType. If not, append an error message
6380** to pCheck.
6381*/
6382static void checkPtrmap(
6383 IntegrityCk *pCheck, /* Integrity check context */
6384 Pgno iChild, /* Child page number */
6385 u8 eType, /* Expected pointer map type */
6386 Pgno iParent, /* Expected pointer map parent page number */
6387 char *zContext /* Context description (used for error msg) */
6388){
6389 int rc;
6390 u8 ePtrmapType;
6391 Pgno iPtrmapParent;
6392
6393 rc = ptrmapGet(pCheck->pBt, iChild, &ePtrmapType, &iPtrmapParent);
6394 if( rc!=SQLITE_OK ){
6395 checkAppendMsg(pCheck, zContext, "Failed to read ptrmap key=%d", iChild);
6396 return;
6397 }
6398
6399 if( ePtrmapType!=eType || iPtrmapParent!=iParent ){
6400 checkAppendMsg(pCheck, zContext,
6401 "Bad ptr map entry key=%d expected=(%d,%d) got=(%d,%d)",
6402 iChild, eType, iParent, ePtrmapType, iPtrmapParent);
6403 }
6404}
6405#endif
6406
drh5eddca62001-06-30 21:53:53 +00006407/*
6408** Check the integrity of the freelist or of an overflow page list.
6409** Verify that the number of pages on the list is N.
6410*/
drh30e58752002-03-02 20:41:57 +00006411static void checkList(
6412 IntegrityCk *pCheck, /* Integrity checking context */
6413 int isFreeList, /* True for a freelist. False for overflow page list */
6414 int iPage, /* Page number for first page in the list */
6415 int N, /* Expected number of pages in the list */
6416 char *zContext /* Context for error messages */
6417){
6418 int i;
drh3a4c1412004-05-09 20:40:11 +00006419 int expected = N;
6420 int iFirst = iPage;
drh1dcdbc02007-01-27 02:24:54 +00006421 while( N-- > 0 && pCheck->mxErr ){
danielk19773b8a05f2007-03-19 17:44:26 +00006422 DbPage *pOvflPage;
6423 unsigned char *pOvflData;
drh5eddca62001-06-30 21:53:53 +00006424 if( iPage<1 ){
drh2e38c322004-09-03 18:38:44 +00006425 checkAppendMsg(pCheck, zContext,
6426 "%d of %d pages missing from overflow list starting at %d",
drh3a4c1412004-05-09 20:40:11 +00006427 N+1, expected, iFirst);
drh5eddca62001-06-30 21:53:53 +00006428 break;
6429 }
6430 if( checkRef(pCheck, iPage, zContext) ) break;
danielk19773b8a05f2007-03-19 17:44:26 +00006431 if( sqlite3PagerGet(pCheck->pPager, (Pgno)iPage, &pOvflPage) ){
drh2e38c322004-09-03 18:38:44 +00006432 checkAppendMsg(pCheck, zContext, "failed to get page %d", iPage);
drh5eddca62001-06-30 21:53:53 +00006433 break;
6434 }
danielk19773b8a05f2007-03-19 17:44:26 +00006435 pOvflData = (unsigned char *)sqlite3PagerGetData(pOvflPage);
drh30e58752002-03-02 20:41:57 +00006436 if( isFreeList ){
danielk19773b8a05f2007-03-19 17:44:26 +00006437 int n = get4byte(&pOvflData[4]);
danielk1977687566d2004-11-02 12:56:41 +00006438#ifndef SQLITE_OMIT_AUTOVACUUM
6439 if( pCheck->pBt->autoVacuum ){
6440 checkPtrmap(pCheck, iPage, PTRMAP_FREEPAGE, 0, zContext);
6441 }
6442#endif
drh855eb1c2004-08-31 13:45:11 +00006443 if( n>pCheck->pBt->usableSize/4-8 ){
drh2e38c322004-09-03 18:38:44 +00006444 checkAppendMsg(pCheck, zContext,
6445 "freelist leaf count too big on page %d", iPage);
drhee696e22004-08-30 16:52:17 +00006446 N--;
6447 }else{
6448 for(i=0; i<n; i++){
danielk19773b8a05f2007-03-19 17:44:26 +00006449 Pgno iFreePage = get4byte(&pOvflData[8+i*4]);
danielk1977687566d2004-11-02 12:56:41 +00006450#ifndef SQLITE_OMIT_AUTOVACUUM
6451 if( pCheck->pBt->autoVacuum ){
6452 checkPtrmap(pCheck, iFreePage, PTRMAP_FREEPAGE, 0, zContext);
6453 }
6454#endif
6455 checkRef(pCheck, iFreePage, zContext);
drhee696e22004-08-30 16:52:17 +00006456 }
6457 N -= n;
drh30e58752002-03-02 20:41:57 +00006458 }
drh30e58752002-03-02 20:41:57 +00006459 }
danielk1977afcdd022004-10-31 16:25:42 +00006460#ifndef SQLITE_OMIT_AUTOVACUUM
danielk1977687566d2004-11-02 12:56:41 +00006461 else{
6462 /* If this database supports auto-vacuum and iPage is not the last
6463 ** page in this overflow list, check that the pointer-map entry for
6464 ** the following page matches iPage.
6465 */
6466 if( pCheck->pBt->autoVacuum && N>0 ){
danielk19773b8a05f2007-03-19 17:44:26 +00006467 i = get4byte(pOvflData);
danielk1977687566d2004-11-02 12:56:41 +00006468 checkPtrmap(pCheck, i, PTRMAP_OVERFLOW2, iPage, zContext);
6469 }
danielk1977afcdd022004-10-31 16:25:42 +00006470 }
6471#endif
danielk19773b8a05f2007-03-19 17:44:26 +00006472 iPage = get4byte(pOvflData);
6473 sqlite3PagerUnref(pOvflPage);
drh5eddca62001-06-30 21:53:53 +00006474 }
6475}
drhb7f91642004-10-31 02:22:47 +00006476#endif /* SQLITE_OMIT_INTEGRITY_CHECK */
drh5eddca62001-06-30 21:53:53 +00006477
drhb7f91642004-10-31 02:22:47 +00006478#ifndef SQLITE_OMIT_INTEGRITY_CHECK
drh5eddca62001-06-30 21:53:53 +00006479/*
6480** Do various sanity checks on a single page of a tree. Return
6481** the tree depth. Root pages return 0. Parents of root pages
6482** return 1, and so forth.
6483**
6484** These checks are done:
6485**
6486** 1. Make sure that cells and freeblocks do not overlap
6487** but combine to completely cover the page.
drhda200cc2004-05-09 11:51:38 +00006488** NO 2. Make sure cell keys are in order.
6489** NO 3. Make sure no key is less than or equal to zLowerBound.
6490** NO 4. Make sure no key is greater than or equal to zUpperBound.
drh5eddca62001-06-30 21:53:53 +00006491** 5. Check the integrity of overflow pages.
6492** 6. Recursively call checkTreePage on all children.
6493** 7. Verify that the depth of all children is the same.
drh6019e162001-07-02 17:51:45 +00006494** 8. Make sure this page is at least 33% full or else it is
drh5eddca62001-06-30 21:53:53 +00006495** the root of the tree.
6496*/
6497static int checkTreePage(
drhaaab5722002-02-19 13:39:21 +00006498 IntegrityCk *pCheck, /* Context for the sanity check */
drh5eddca62001-06-30 21:53:53 +00006499 int iPage, /* Page number of the page to check */
6500 MemPage *pParent, /* Parent page */
drh74161702006-02-24 02:53:49 +00006501 char *zParentContext /* Parent context */
drh5eddca62001-06-30 21:53:53 +00006502){
6503 MemPage *pPage;
drhda200cc2004-05-09 11:51:38 +00006504 int i, rc, depth, d2, pgno, cnt;
drh43605152004-05-29 21:46:49 +00006505 int hdr, cellStart;
6506 int nCell;
drhda200cc2004-05-09 11:51:38 +00006507 u8 *data;
danielk1977aef0bf62005-12-30 16:28:01 +00006508 BtShared *pBt;
drh4f26bb62005-09-08 14:17:20 +00006509 int usableSize;
drh5eddca62001-06-30 21:53:53 +00006510 char zContext[100];
drh2e38c322004-09-03 18:38:44 +00006511 char *hit;
drh5eddca62001-06-30 21:53:53 +00006512
drh5bb3eb92007-05-04 13:15:55 +00006513 sqlite3_snprintf(sizeof(zContext), zContext, "Page %d: ", iPage);
danielk1977ef73ee92004-11-06 12:26:07 +00006514
drh5eddca62001-06-30 21:53:53 +00006515 /* Check that the page exists
6516 */
drhd9cb6ac2005-10-20 07:28:17 +00006517 pBt = pCheck->pBt;
drhb6f41482004-05-14 01:58:11 +00006518 usableSize = pBt->usableSize;
drh5eddca62001-06-30 21:53:53 +00006519 if( iPage==0 ) return 0;
6520 if( checkRef(pCheck, iPage, zParentContext) ) return 0;
drh16a9b832007-05-05 18:39:25 +00006521 if( (rc = sqlite3BtreeGetPage(pBt, (Pgno)iPage, &pPage, 0))!=0 ){
drh2e38c322004-09-03 18:38:44 +00006522 checkAppendMsg(pCheck, zContext,
6523 "unable to get the page. error code=%d", rc);
drh5eddca62001-06-30 21:53:53 +00006524 return 0;
6525 }
drh16a9b832007-05-05 18:39:25 +00006526 if( (rc = sqlite3BtreeInitPage(pPage, pParent))!=0 ){
6527 checkAppendMsg(pCheck, zContext,
6528 "sqlite3BtreeInitPage() returns error code %d", rc);
drh91025292004-05-03 19:49:32 +00006529 releasePage(pPage);
drh5eddca62001-06-30 21:53:53 +00006530 return 0;
6531 }
6532
6533 /* Check out all the cells.
6534 */
6535 depth = 0;
drh1dcdbc02007-01-27 02:24:54 +00006536 for(i=0; i<pPage->nCell && pCheck->mxErr; i++){
drh6f11bef2004-05-13 01:12:56 +00006537 u8 *pCell;
6538 int sz;
6539 CellInfo info;
drh5eddca62001-06-30 21:53:53 +00006540
6541 /* Check payload overflow pages
6542 */
drh5bb3eb92007-05-04 13:15:55 +00006543 sqlite3_snprintf(sizeof(zContext), zContext,
6544 "On tree page %d cell %d: ", iPage, i);
danielk19771cc5ed82007-05-16 17:28:43 +00006545 pCell = findCell(pPage,i);
drh16a9b832007-05-05 18:39:25 +00006546 sqlite3BtreeParseCellPtr(pPage, pCell, &info);
drh6f11bef2004-05-13 01:12:56 +00006547 sz = info.nData;
6548 if( !pPage->intKey ) sz += info.nKey;
drh72365832007-03-06 15:53:44 +00006549 assert( sz==info.nPayload );
drh6f11bef2004-05-13 01:12:56 +00006550 if( sz>info.nLocal ){
drhb6f41482004-05-14 01:58:11 +00006551 int nPage = (sz - info.nLocal + usableSize - 5)/(usableSize - 4);
danielk1977afcdd022004-10-31 16:25:42 +00006552 Pgno pgnoOvfl = get4byte(&pCell[info.iOverflow]);
6553#ifndef SQLITE_OMIT_AUTOVACUUM
6554 if( pBt->autoVacuum ){
danielk1977687566d2004-11-02 12:56:41 +00006555 checkPtrmap(pCheck, pgnoOvfl, PTRMAP_OVERFLOW1, iPage, zContext);
danielk1977afcdd022004-10-31 16:25:42 +00006556 }
6557#endif
6558 checkList(pCheck, 0, pgnoOvfl, nPage, zContext);
drh5eddca62001-06-30 21:53:53 +00006559 }
6560
6561 /* Check sanity of left child page.
6562 */
drhda200cc2004-05-09 11:51:38 +00006563 if( !pPage->leaf ){
drh43605152004-05-29 21:46:49 +00006564 pgno = get4byte(pCell);
danielk1977afcdd022004-10-31 16:25:42 +00006565#ifndef SQLITE_OMIT_AUTOVACUUM
6566 if( pBt->autoVacuum ){
6567 checkPtrmap(pCheck, pgno, PTRMAP_BTREE, iPage, zContext);
6568 }
6569#endif
drh74161702006-02-24 02:53:49 +00006570 d2 = checkTreePage(pCheck,pgno,pPage,zContext);
drhda200cc2004-05-09 11:51:38 +00006571 if( i>0 && d2!=depth ){
6572 checkAppendMsg(pCheck, zContext, "Child page depth differs");
6573 }
6574 depth = d2;
drh5eddca62001-06-30 21:53:53 +00006575 }
drh5eddca62001-06-30 21:53:53 +00006576 }
drhda200cc2004-05-09 11:51:38 +00006577 if( !pPage->leaf ){
drh43605152004-05-29 21:46:49 +00006578 pgno = get4byte(&pPage->aData[pPage->hdrOffset+8]);
drh5bb3eb92007-05-04 13:15:55 +00006579 sqlite3_snprintf(sizeof(zContext), zContext,
6580 "On page %d at right child: ", iPage);
danielk1977afcdd022004-10-31 16:25:42 +00006581#ifndef SQLITE_OMIT_AUTOVACUUM
6582 if( pBt->autoVacuum ){
danielk1977687566d2004-11-02 12:56:41 +00006583 checkPtrmap(pCheck, pgno, PTRMAP_BTREE, iPage, 0);
danielk1977afcdd022004-10-31 16:25:42 +00006584 }
6585#endif
drh74161702006-02-24 02:53:49 +00006586 checkTreePage(pCheck, pgno, pPage, zContext);
drhda200cc2004-05-09 11:51:38 +00006587 }
drh5eddca62001-06-30 21:53:53 +00006588
6589 /* Check for complete coverage of the page
6590 */
drhda200cc2004-05-09 11:51:38 +00006591 data = pPage->aData;
6592 hdr = pPage->hdrOffset;
drh17435752007-08-16 04:30:38 +00006593 hit = sqlite3MallocZero( usableSize );
drh2e38c322004-09-03 18:38:44 +00006594 if( hit ){
6595 memset(hit, 1, get2byte(&data[hdr+5]));
6596 nCell = get2byte(&data[hdr+3]);
6597 cellStart = hdr + 12 - 4*pPage->leaf;
6598 for(i=0; i<nCell; i++){
6599 int pc = get2byte(&data[cellStart+i*2]);
drha9121e42008-02-19 14:59:35 +00006600 u16 size = cellSizePtr(pPage, &data[pc]);
drh2e38c322004-09-03 18:38:44 +00006601 int j;
danielk19777701e812005-01-10 12:59:51 +00006602 if( (pc+size-1)>=usableSize || pc<0 ){
6603 checkAppendMsg(pCheck, 0,
6604 "Corruption detected in cell %d on page %d",i,iPage,0);
6605 }else{
6606 for(j=pc+size-1; j>=pc; j--) hit[j]++;
6607 }
drh2e38c322004-09-03 18:38:44 +00006608 }
6609 for(cnt=0, i=get2byte(&data[hdr+1]); i>0 && i<usableSize && cnt<10000;
6610 cnt++){
6611 int size = get2byte(&data[i+2]);
6612 int j;
danielk19777701e812005-01-10 12:59:51 +00006613 if( (i+size-1)>=usableSize || i<0 ){
6614 checkAppendMsg(pCheck, 0,
6615 "Corruption detected in cell %d on page %d",i,iPage,0);
6616 }else{
6617 for(j=i+size-1; j>=i; j--) hit[j]++;
6618 }
drh2e38c322004-09-03 18:38:44 +00006619 i = get2byte(&data[i]);
6620 }
6621 for(i=cnt=0; i<usableSize; i++){
6622 if( hit[i]==0 ){
6623 cnt++;
6624 }else if( hit[i]>1 ){
6625 checkAppendMsg(pCheck, 0,
6626 "Multiple uses for byte %d of page %d", i, iPage);
6627 break;
6628 }
6629 }
6630 if( cnt!=data[hdr+7] ){
6631 checkAppendMsg(pCheck, 0,
6632 "Fragmented space is %d byte reported as %d on page %d",
6633 cnt, data[hdr+7], iPage);
drh5eddca62001-06-30 21:53:53 +00006634 }
6635 }
drh17435752007-08-16 04:30:38 +00006636 sqlite3_free(hit);
drh6019e162001-07-02 17:51:45 +00006637
drh4b70f112004-05-02 21:12:19 +00006638 releasePage(pPage);
drhda200cc2004-05-09 11:51:38 +00006639 return depth+1;
drh5eddca62001-06-30 21:53:53 +00006640}
drhb7f91642004-10-31 02:22:47 +00006641#endif /* SQLITE_OMIT_INTEGRITY_CHECK */
drh5eddca62001-06-30 21:53:53 +00006642
drhb7f91642004-10-31 02:22:47 +00006643#ifndef SQLITE_OMIT_INTEGRITY_CHECK
drh5eddca62001-06-30 21:53:53 +00006644/*
6645** This routine does a complete check of the given BTree file. aRoot[] is
6646** an array of pages numbers were each page number is the root page of
6647** a table. nRoot is the number of entries in aRoot.
6648**
6649** If everything checks out, this routine returns NULL. If something is
6650** amiss, an error message is written into memory obtained from malloc()
6651** and a pointer to that error message is returned. The calling function
6652** is responsible for freeing the error message when it is done.
6653*/
drh1dcdbc02007-01-27 02:24:54 +00006654char *sqlite3BtreeIntegrityCheck(
6655 Btree *p, /* The btree to be checked */
6656 int *aRoot, /* An array of root pages numbers for individual trees */
6657 int nRoot, /* Number of entries in aRoot[] */
6658 int mxErr, /* Stop reporting errors after this many */
6659 int *pnErr /* Write number of errors seen to this variable */
6660){
drh5eddca62001-06-30 21:53:53 +00006661 int i;
6662 int nRef;
drhaaab5722002-02-19 13:39:21 +00006663 IntegrityCk sCheck;
danielk1977aef0bf62005-12-30 16:28:01 +00006664 BtShared *pBt = p->pBt;
drh5eddca62001-06-30 21:53:53 +00006665
drhd677b3d2007-08-20 22:48:41 +00006666 sqlite3BtreeEnter(p);
drhe5fe6902007-12-07 18:55:28 +00006667 pBt->db = p->db;
danielk19773b8a05f2007-03-19 17:44:26 +00006668 nRef = sqlite3PagerRefcount(pBt->pPager);
danielk1977aef0bf62005-12-30 16:28:01 +00006669 if( lockBtreeWithRetry(p)!=SQLITE_OK ){
drhd677b3d2007-08-20 22:48:41 +00006670 sqlite3BtreeLeave(p);
drh17435752007-08-16 04:30:38 +00006671 return sqlite3StrDup("Unable to acquire a read lock on the database");
drhefc251d2001-07-01 22:12:01 +00006672 }
drh5eddca62001-06-30 21:53:53 +00006673 sCheck.pBt = pBt;
6674 sCheck.pPager = pBt->pPager;
danielk1977ad0132d2008-06-07 08:58:22 +00006675 sCheck.nPage = pagerPagecount(sCheck.pPager);
drh1dcdbc02007-01-27 02:24:54 +00006676 sCheck.mxErr = mxErr;
6677 sCheck.nErr = 0;
6678 *pnErr = 0;
danielk1977e5321f02007-04-27 07:05:44 +00006679#ifndef SQLITE_OMIT_AUTOVACUUM
6680 if( pBt->nTrunc!=0 ){
6681 sCheck.nPage = pBt->nTrunc;
6682 }
6683#endif
drh0de8c112002-07-06 16:32:14 +00006684 if( sCheck.nPage==0 ){
6685 unlockBtreeIfUnused(pBt);
drhd677b3d2007-08-20 22:48:41 +00006686 sqlite3BtreeLeave(p);
drh0de8c112002-07-06 16:32:14 +00006687 return 0;
6688 }
drh17435752007-08-16 04:30:38 +00006689 sCheck.anRef = sqlite3_malloc( (sCheck.nPage+1)*sizeof(sCheck.anRef[0]) );
danielk1977ac245ec2005-01-14 13:50:11 +00006690 if( !sCheck.anRef ){
6691 unlockBtreeIfUnused(pBt);
drh1dcdbc02007-01-27 02:24:54 +00006692 *pnErr = 1;
drhd677b3d2007-08-20 22:48:41 +00006693 sqlite3BtreeLeave(p);
drhe5fe6902007-12-07 18:55:28 +00006694 return sqlite3MPrintf(p->db, "Unable to malloc %d bytes",
danielk1977ac245ec2005-01-14 13:50:11 +00006695 (sCheck.nPage+1)*sizeof(sCheck.anRef[0]));
6696 }
drhda200cc2004-05-09 11:51:38 +00006697 for(i=0; i<=sCheck.nPage; i++){ sCheck.anRef[i] = 0; }
drh42cac6d2004-11-20 20:31:11 +00006698 i = PENDING_BYTE_PAGE(pBt);
drh1f595712004-06-15 01:40:29 +00006699 if( i<=sCheck.nPage ){
6700 sCheck.anRef[i] = 1;
6701 }
drh5eddca62001-06-30 21:53:53 +00006702 sCheck.zErrMsg = 0;
6703
6704 /* Check the integrity of the freelist
6705 */
drha34b6762004-05-07 13:30:42 +00006706 checkList(&sCheck, 1, get4byte(&pBt->pPage1->aData[32]),
6707 get4byte(&pBt->pPage1->aData[36]), "Main freelist: ");
drh5eddca62001-06-30 21:53:53 +00006708
6709 /* Check all the tables.
6710 */
drh1dcdbc02007-01-27 02:24:54 +00006711 for(i=0; i<nRoot && sCheck.mxErr; i++){
drh4ff6dfa2002-03-03 23:06:00 +00006712 if( aRoot[i]==0 ) continue;
danielk1977687566d2004-11-02 12:56:41 +00006713#ifndef SQLITE_OMIT_AUTOVACUUM
danielk1977687566d2004-11-02 12:56:41 +00006714 if( pBt->autoVacuum && aRoot[i]>1 ){
6715 checkPtrmap(&sCheck, aRoot[i], PTRMAP_ROOTPAGE, 0, 0);
6716 }
6717#endif
drh74161702006-02-24 02:53:49 +00006718 checkTreePage(&sCheck, aRoot[i], 0, "List of tree roots: ");
drh5eddca62001-06-30 21:53:53 +00006719 }
6720
6721 /* Make sure every page in the file is referenced
6722 */
drh1dcdbc02007-01-27 02:24:54 +00006723 for(i=1; i<=sCheck.nPage && sCheck.mxErr; i++){
danielk1977afcdd022004-10-31 16:25:42 +00006724#ifdef SQLITE_OMIT_AUTOVACUUM
drh5eddca62001-06-30 21:53:53 +00006725 if( sCheck.anRef[i]==0 ){
drh2e38c322004-09-03 18:38:44 +00006726 checkAppendMsg(&sCheck, 0, "Page %d is never used", i);
drh5eddca62001-06-30 21:53:53 +00006727 }
danielk1977afcdd022004-10-31 16:25:42 +00006728#else
6729 /* If the database supports auto-vacuum, make sure no tables contain
6730 ** references to pointer-map pages.
6731 */
6732 if( sCheck.anRef[i]==0 &&
danielk1977266664d2006-02-10 08:24:21 +00006733 (PTRMAP_PAGENO(pBt, i)!=i || !pBt->autoVacuum) ){
danielk1977afcdd022004-10-31 16:25:42 +00006734 checkAppendMsg(&sCheck, 0, "Page %d is never used", i);
6735 }
6736 if( sCheck.anRef[i]!=0 &&
danielk1977266664d2006-02-10 08:24:21 +00006737 (PTRMAP_PAGENO(pBt, i)==i && pBt->autoVacuum) ){
danielk1977afcdd022004-10-31 16:25:42 +00006738 checkAppendMsg(&sCheck, 0, "Pointer map page %d is referenced", i);
6739 }
6740#endif
drh5eddca62001-06-30 21:53:53 +00006741 }
6742
6743 /* Make sure this analysis did not leave any unref() pages
6744 */
drh5e00f6c2001-09-13 13:46:56 +00006745 unlockBtreeIfUnused(pBt);
danielk19773b8a05f2007-03-19 17:44:26 +00006746 if( nRef != sqlite3PagerRefcount(pBt->pPager) ){
drh2e38c322004-09-03 18:38:44 +00006747 checkAppendMsg(&sCheck, 0,
drh5eddca62001-06-30 21:53:53 +00006748 "Outstanding page count goes from %d to %d during this analysis",
danielk19773b8a05f2007-03-19 17:44:26 +00006749 nRef, sqlite3PagerRefcount(pBt->pPager)
drh5eddca62001-06-30 21:53:53 +00006750 );
drh5eddca62001-06-30 21:53:53 +00006751 }
6752
6753 /* Clean up and report errors.
6754 */
drhd677b3d2007-08-20 22:48:41 +00006755 sqlite3BtreeLeave(p);
drh17435752007-08-16 04:30:38 +00006756 sqlite3_free(sCheck.anRef);
drh1dcdbc02007-01-27 02:24:54 +00006757 *pnErr = sCheck.nErr;
drh5eddca62001-06-30 21:53:53 +00006758 return sCheck.zErrMsg;
6759}
drhb7f91642004-10-31 02:22:47 +00006760#endif /* SQLITE_OMIT_INTEGRITY_CHECK */
paulb95a8862003-04-01 21:16:41 +00006761
drh73509ee2003-04-06 20:44:45 +00006762/*
6763** Return the full pathname of the underlying database file.
drhd0679ed2007-08-28 22:24:34 +00006764**
6765** The pager filename is invariant as long as the pager is
6766** open so it is safe to access without the BtShared mutex.
drh73509ee2003-04-06 20:44:45 +00006767*/
danielk1977aef0bf62005-12-30 16:28:01 +00006768const char *sqlite3BtreeGetFilename(Btree *p){
6769 assert( p->pBt->pPager!=0 );
danielk19773b8a05f2007-03-19 17:44:26 +00006770 return sqlite3PagerFilename(p->pBt->pPager);
drh73509ee2003-04-06 20:44:45 +00006771}
6772
6773/*
danielk19775865e3d2004-06-14 06:03:57 +00006774** Return the pathname of the directory that contains the database file.
drhd0679ed2007-08-28 22:24:34 +00006775**
6776** The pager directory name is invariant as long as the pager is
6777** open so it is safe to access without the BtShared mutex.
danielk19775865e3d2004-06-14 06:03:57 +00006778*/
danielk1977aef0bf62005-12-30 16:28:01 +00006779const char *sqlite3BtreeGetDirname(Btree *p){
6780 assert( p->pBt->pPager!=0 );
danielk19773b8a05f2007-03-19 17:44:26 +00006781 return sqlite3PagerDirname(p->pBt->pPager);
danielk19775865e3d2004-06-14 06:03:57 +00006782}
6783
6784/*
6785** Return the pathname of the journal file for this database. The return
6786** value of this routine is the same regardless of whether the journal file
6787** has been created or not.
drhd0679ed2007-08-28 22:24:34 +00006788**
6789** The pager journal filename is invariant as long as the pager is
6790** open so it is safe to access without the BtShared mutex.
danielk19775865e3d2004-06-14 06:03:57 +00006791*/
danielk1977aef0bf62005-12-30 16:28:01 +00006792const char *sqlite3BtreeGetJournalname(Btree *p){
6793 assert( p->pBt->pPager!=0 );
danielk19773b8a05f2007-03-19 17:44:26 +00006794 return sqlite3PagerJournalname(p->pBt->pPager);
danielk19775865e3d2004-06-14 06:03:57 +00006795}
6796
drhb7f91642004-10-31 02:22:47 +00006797#ifndef SQLITE_OMIT_VACUUM
danielk19775865e3d2004-06-14 06:03:57 +00006798/*
drhf7c57532003-04-25 13:22:51 +00006799** Copy the complete content of pBtFrom into pBtTo. A transaction
6800** must be active for both files.
6801**
danielk1977f653d782008-03-20 11:04:21 +00006802** The size of file pTo may be reduced by this operation.
6803** If anything goes wrong, the transaction on pTo is rolled back.
6804**
6805** If successful, CommitPhaseOne() may be called on pTo before returning.
6806** The caller should finish committing the transaction on pTo by calling
6807** sqlite3BtreeCommit().
drh73509ee2003-04-06 20:44:45 +00006808*/
drhd677b3d2007-08-20 22:48:41 +00006809static int btreeCopyFile(Btree *pTo, Btree *pFrom){
drhf7c57532003-04-25 13:22:51 +00006810 int rc = SQLITE_OK;
danielk1977f653d782008-03-20 11:04:21 +00006811 Pgno i;
6812
6813 Pgno nFromPage; /* Number of pages in pFrom */
6814 Pgno nToPage; /* Number of pages in pTo */
6815 Pgno nNewPage; /* Number of pages in pTo after the copy */
6816
6817 Pgno iSkip; /* Pending byte page in pTo */
6818 int nToPageSize; /* Page size of pTo in bytes */
6819 int nFromPageSize; /* Page size of pFrom in bytes */
drhf7c57532003-04-25 13:22:51 +00006820
danielk1977aef0bf62005-12-30 16:28:01 +00006821 BtShared *pBtTo = pTo->pBt;
6822 BtShared *pBtFrom = pFrom->pBt;
drhe5fe6902007-12-07 18:55:28 +00006823 pBtTo->db = pTo->db;
6824 pBtFrom->db = pFrom->db;
danielk1977f653d782008-03-20 11:04:21 +00006825
6826 nToPageSize = pBtTo->pageSize;
6827 nFromPageSize = pBtFrom->pageSize;
danielk1977aef0bf62005-12-30 16:28:01 +00006828
6829 if( pTo->inTrans!=TRANS_WRITE || pFrom->inTrans!=TRANS_WRITE ){
danielk1977ee5741e2004-05-31 10:01:34 +00006830 return SQLITE_ERROR;
6831 }
danielk1977f653d782008-03-20 11:04:21 +00006832 if( pBtTo->pCursor ){
6833 return SQLITE_BUSY;
drhf7c57532003-04-25 13:22:51 +00006834 }
drh538f5702007-04-13 02:14:30 +00006835
danielk1977ad0132d2008-06-07 08:58:22 +00006836 nToPage = pagerPagecount(pBtTo->pPager);
6837 nFromPage = pagerPagecount(pBtFrom->pPager);
danielk1977f653d782008-03-20 11:04:21 +00006838 iSkip = PENDING_BYTE_PAGE(pBtTo);
6839
6840 /* Variable nNewPage is the number of pages required to store the
6841 ** contents of pFrom using the current page-size of pTo.
drh538f5702007-04-13 02:14:30 +00006842 */
danielk1977f653d782008-03-20 11:04:21 +00006843 nNewPage = ((i64)nFromPage * (i64)nFromPageSize + (i64)nToPageSize - 1) /
6844 (i64)nToPageSize;
6845
6846 for(i=1; rc==SQLITE_OK && (i<=nToPage || i<=nNewPage); i++){
6847
6848 /* Journal the original page.
6849 **
6850 ** iSkip is the page number of the locking page (PENDING_BYTE_PAGE)
6851 ** in database *pTo (before the copy). This page is never written
6852 ** into the journal file. Unless i==iSkip or the page was not
6853 ** present in pTo before the copy operation, journal page i from pTo.
6854 */
6855 if( i!=iSkip && i<=nToPage ){
danielk19774abd5442008-05-05 15:26:50 +00006856 DbPage *pDbPage = 0;
danielk1977f653d782008-03-20 11:04:21 +00006857 rc = sqlite3PagerGet(pBtTo->pPager, i, &pDbPage);
danielk19774abd5442008-05-05 15:26:50 +00006858 if( rc==SQLITE_OK ){
6859 rc = sqlite3PagerWrite(pDbPage);
danielk1977df2566a2008-05-07 19:11:03 +00006860 if( rc==SQLITE_OK && i>nFromPage ){
6861 /* Yeah. It seems wierd to call DontWrite() right after Write(). But
6862 ** that is because the names of those procedures do not exactly
6863 ** represent what they do. Write() really means "put this page in the
6864 ** rollback journal and mark it as dirty so that it will be written
6865 ** to the database file later." DontWrite() undoes the second part of
6866 ** that and prevents the page from being written to the database. The
6867 ** page is still on the rollback journal, though. And that is the
6868 ** whole point of this block: to put pages on the rollback journal.
6869 */
6870 sqlite3PagerDontWrite(pDbPage);
6871 }
6872 sqlite3PagerUnref(pDbPage);
danielk1977f653d782008-03-20 11:04:21 +00006873 }
danielk1977f653d782008-03-20 11:04:21 +00006874 }
6875
6876 /* Overwrite the data in page i of the target database */
6877 if( rc==SQLITE_OK && i!=iSkip && i<=nNewPage ){
6878
6879 DbPage *pToPage = 0;
6880 sqlite3_int64 iOff;
6881
6882 rc = sqlite3PagerGet(pBtTo->pPager, i, &pToPage);
6883 if( rc==SQLITE_OK ){
6884 rc = sqlite3PagerWrite(pToPage);
6885 }
6886
6887 for(
6888 iOff=(i-1)*nToPageSize;
6889 rc==SQLITE_OK && iOff<i*nToPageSize;
6890 iOff += nFromPageSize
6891 ){
6892 DbPage *pFromPage = 0;
6893 Pgno iFrom = (iOff/nFromPageSize)+1;
6894
6895 if( iFrom==PENDING_BYTE_PAGE(pBtFrom) ){
6896 continue;
6897 }
6898
6899 rc = sqlite3PagerGet(pBtFrom->pPager, iFrom, &pFromPage);
6900 if( rc==SQLITE_OK ){
6901 char *zTo = sqlite3PagerGetData(pToPage);
6902 char *zFrom = sqlite3PagerGetData(pFromPage);
6903 int nCopy;
6904
6905 if( nFromPageSize>=nToPageSize ){
6906 zFrom += ((i-1)*nToPageSize - ((iFrom-1)*nFromPageSize));
6907 nCopy = nToPageSize;
6908 }else{
6909 zTo += (((iFrom-1)*nFromPageSize) - (i-1)*nToPageSize);
6910 nCopy = nFromPageSize;
6911 }
6912
6913 memcpy(zTo, zFrom, nCopy);
6914 sqlite3PagerUnref(pFromPage);
6915 }
6916 }
6917
6918 if( pToPage ) sqlite3PagerUnref(pToPage);
6919 }
drh2e6d11b2003-04-25 15:37:57 +00006920 }
danielk1977f653d782008-03-20 11:04:21 +00006921
6922 /* If things have worked so far, the database file may need to be
6923 ** truncated. The complex part is that it may need to be truncated to
6924 ** a size that is not an integer multiple of nToPageSize - the current
6925 ** page size used by the pager associated with B-Tree pTo.
6926 **
6927 ** For example, say the page-size of pTo is 2048 bytes and the original
6928 ** number of pages is 5 (10 KB file). If pFrom has a page size of 1024
6929 ** bytes and 9 pages, then the file needs to be truncated to 9KB.
6930 */
6931 if( rc==SQLITE_OK ){
6932 if( nFromPageSize!=nToPageSize ){
6933 sqlite3_file *pFile = sqlite3PagerFile(pBtTo->pPager);
6934 i64 iSize = (i64)nFromPageSize * (i64)nFromPage;
6935 i64 iNow = (i64)((nToPage>nNewPage)?nToPage:nNewPage) * (i64)nToPageSize;
6936 i64 iPending = ((i64)PENDING_BYTE_PAGE(pBtTo)-1) *(i64)nToPageSize;
6937
6938 assert( iSize<=iNow );
6939
6940 /* Commit phase one syncs the journal file associated with pTo
6941 ** containing the original data. It does not sync the database file
6942 ** itself. After doing this it is safe to use OsTruncate() and other
6943 ** file APIs on the database file directly.
6944 */
6945 pBtTo->db = pTo->db;
6946 rc = sqlite3PagerCommitPhaseOne(pBtTo->pPager, 0, 0, 1);
6947 if( iSize<iNow && rc==SQLITE_OK ){
6948 rc = sqlite3OsTruncate(pFile, iSize);
6949 }
6950
6951 /* The loop that copied data from database pFrom to pTo did not
6952 ** populate the locking page of database pTo. If the page-size of
6953 ** pFrom is smaller than that of pTo, this means some data will
6954 ** not have been copied.
6955 **
6956 ** This block copies the missing data from database pFrom to pTo
6957 ** using file APIs. This is safe because at this point we know that
6958 ** all of the original data from pTo has been synced into the
6959 ** journal file. At this point it would be safe to do anything at
6960 ** all to the database file except truncate it to zero bytes.
6961 */
6962 if( rc==SQLITE_OK && nFromPageSize<nToPageSize && iSize>iPending){
6963 i64 iOff;
6964 for(
6965 iOff=iPending;
6966 rc==SQLITE_OK && iOff<(iPending+nToPageSize);
6967 iOff += nFromPageSize
6968 ){
6969 DbPage *pFromPage = 0;
6970 Pgno iFrom = (iOff/nFromPageSize)+1;
6971
6972 if( iFrom==PENDING_BYTE_PAGE(pBtFrom) || iFrom>nFromPage ){
6973 continue;
6974 }
6975
6976 rc = sqlite3PagerGet(pBtFrom->pPager, iFrom, &pFromPage);
6977 if( rc==SQLITE_OK ){
6978 char *zFrom = sqlite3PagerGetData(pFromPage);
6979 rc = sqlite3OsWrite(pFile, zFrom, nFromPageSize, iOff);
6980 sqlite3PagerUnref(pFromPage);
6981 }
6982 }
6983 }
6984
6985 /* Sync the database file */
6986 if( rc==SQLITE_OK ){
6987 rc = sqlite3PagerSync(pBtTo->pPager);
6988 }
6989 }else{
6990 rc = sqlite3PagerTruncate(pBtTo->pPager, nNewPage);
6991 }
6992 if( rc==SQLITE_OK ){
6993 pBtTo->pageSizeFixed = 0;
6994 }
drh2e6d11b2003-04-25 15:37:57 +00006995 }
drh538f5702007-04-13 02:14:30 +00006996
drhf7c57532003-04-25 13:22:51 +00006997 if( rc ){
danielk1977aef0bf62005-12-30 16:28:01 +00006998 sqlite3BtreeRollback(pTo);
drhf7c57532003-04-25 13:22:51 +00006999 }
danielk1977f653d782008-03-20 11:04:21 +00007000
drhf7c57532003-04-25 13:22:51 +00007001 return rc;
drh73509ee2003-04-06 20:44:45 +00007002}
drhd677b3d2007-08-20 22:48:41 +00007003int sqlite3BtreeCopyFile(Btree *pTo, Btree *pFrom){
7004 int rc;
7005 sqlite3BtreeEnter(pTo);
7006 sqlite3BtreeEnter(pFrom);
7007 rc = btreeCopyFile(pTo, pFrom);
7008 sqlite3BtreeLeave(pFrom);
7009 sqlite3BtreeLeave(pTo);
7010 return rc;
7011}
7012
drhb7f91642004-10-31 02:22:47 +00007013#endif /* SQLITE_OMIT_VACUUM */
danielk19771d850a72004-05-31 08:26:49 +00007014
7015/*
7016** Return non-zero if a transaction is active.
7017*/
danielk1977aef0bf62005-12-30 16:28:01 +00007018int sqlite3BtreeIsInTrans(Btree *p){
drhe5fe6902007-12-07 18:55:28 +00007019 assert( p==0 || sqlite3_mutex_held(p->db->mutex) );
danielk1977aef0bf62005-12-30 16:28:01 +00007020 return (p && (p->inTrans==TRANS_WRITE));
danielk19771d850a72004-05-31 08:26:49 +00007021}
7022
7023/*
7024** Return non-zero if a statement transaction is active.
7025*/
danielk1977aef0bf62005-12-30 16:28:01 +00007026int sqlite3BtreeIsInStmt(Btree *p){
drh1fee73e2007-08-29 04:00:57 +00007027 assert( sqlite3BtreeHoldsMutex(p) );
danielk1977aef0bf62005-12-30 16:28:01 +00007028 return (p->pBt && p->pBt->inStmt);
danielk19771d850a72004-05-31 08:26:49 +00007029}
danielk197713adf8a2004-06-03 16:08:41 +00007030
7031/*
danielk19772372c2b2006-06-27 16:34:56 +00007032** Return non-zero if a read (or write) transaction is active.
7033*/
7034int sqlite3BtreeIsInReadTrans(Btree *p){
drhe5fe6902007-12-07 18:55:28 +00007035 assert( sqlite3_mutex_held(p->db->mutex) );
danielk19772372c2b2006-06-27 16:34:56 +00007036 return (p && (p->inTrans!=TRANS_NONE));
7037}
7038
7039/*
danielk1977da184232006-01-05 11:34:32 +00007040** This function returns a pointer to a blob of memory associated with
drh85b623f2007-12-13 21:54:09 +00007041** a single shared-btree. The memory is used by client code for its own
danielk1977da184232006-01-05 11:34:32 +00007042** purposes (for example, to store a high-level schema associated with
7043** the shared-btree). The btree layer manages reference counting issues.
7044**
7045** The first time this is called on a shared-btree, nBytes bytes of memory
7046** are allocated, zeroed, and returned to the caller. For each subsequent
7047** call the nBytes parameter is ignored and a pointer to the same blob
7048** of memory returned.
7049**
7050** Just before the shared-btree is closed, the function passed as the
7051** xFree argument when the memory allocation was made is invoked on the
drh17435752007-08-16 04:30:38 +00007052** blob of allocated memory. This function should not call sqlite3_free()
danielk1977da184232006-01-05 11:34:32 +00007053** on the memory, the btree layer does that.
7054*/
7055void *sqlite3BtreeSchema(Btree *p, int nBytes, void(*xFree)(void *)){
7056 BtShared *pBt = p->pBt;
drh27641702007-08-22 02:56:42 +00007057 sqlite3BtreeEnter(p);
danielk1977da184232006-01-05 11:34:32 +00007058 if( !pBt->pSchema ){
drh17435752007-08-16 04:30:38 +00007059 pBt->pSchema = sqlite3MallocZero(nBytes);
danielk1977da184232006-01-05 11:34:32 +00007060 pBt->xFreeSchema = xFree;
7061 }
drh27641702007-08-22 02:56:42 +00007062 sqlite3BtreeLeave(p);
danielk1977da184232006-01-05 11:34:32 +00007063 return pBt->pSchema;
7064}
7065
danielk1977c87d34d2006-01-06 13:00:28 +00007066/*
7067** Return true if another user of the same shared btree as the argument
7068** handle holds an exclusive lock on the sqlite_master table.
7069*/
7070int sqlite3BtreeSchemaLocked(Btree *p){
drh27641702007-08-22 02:56:42 +00007071 int rc;
drhe5fe6902007-12-07 18:55:28 +00007072 assert( sqlite3_mutex_held(p->db->mutex) );
drh27641702007-08-22 02:56:42 +00007073 sqlite3BtreeEnter(p);
7074 rc = (queryTableLock(p, MASTER_ROOT, READ_LOCK)!=SQLITE_OK);
7075 sqlite3BtreeLeave(p);
7076 return rc;
danielk1977c87d34d2006-01-06 13:00:28 +00007077}
7078
drha154dcd2006-03-22 22:10:07 +00007079
7080#ifndef SQLITE_OMIT_SHARED_CACHE
7081/*
7082** Obtain a lock on the table whose root page is iTab. The
7083** lock is a write lock if isWritelock is true or a read lock
7084** if it is false.
7085*/
danielk1977c00da102006-01-07 13:21:04 +00007086int sqlite3BtreeLockTable(Btree *p, int iTab, u8 isWriteLock){
danielk19772e94d4d2006-01-09 05:36:27 +00007087 int rc = SQLITE_OK;
drh6a9ad3d2008-04-02 16:29:30 +00007088 if( p->sharable ){
7089 u8 lockType = READ_LOCK + isWriteLock;
7090 assert( READ_LOCK+1==WRITE_LOCK );
7091 assert( isWriteLock==0 || isWriteLock==1 );
7092 sqlite3BtreeEnter(p);
7093 rc = queryTableLock(p, iTab, lockType);
7094 if( rc==SQLITE_OK ){
7095 rc = lockTable(p, iTab, lockType);
7096 }
7097 sqlite3BtreeLeave(p);
danielk1977c00da102006-01-07 13:21:04 +00007098 }
7099 return rc;
7100}
drha154dcd2006-03-22 22:10:07 +00007101#endif
danielk1977b82e7ed2006-01-11 14:09:31 +00007102
danielk1977b4e9af92007-05-01 17:49:49 +00007103#ifndef SQLITE_OMIT_INCRBLOB
7104/*
7105** Argument pCsr must be a cursor opened for writing on an
7106** INTKEY table currently pointing at a valid table entry.
7107** This function modifies the data stored as part of that entry.
7108** Only the data content may only be modified, it is not possible
7109** to change the length of the data stored.
7110*/
danielk1977dcbb5d32007-05-04 18:36:44 +00007111int sqlite3BtreePutData(BtCursor *pCsr, u32 offset, u32 amt, void *z){
drh1fee73e2007-08-29 04:00:57 +00007112 assert( cursorHoldsMutex(pCsr) );
drhe5fe6902007-12-07 18:55:28 +00007113 assert( sqlite3_mutex_held(pCsr->pBtree->db->mutex) );
danielk1977dcbb5d32007-05-04 18:36:44 +00007114 assert(pCsr->isIncrblobHandle);
drhfb982642007-08-30 01:19:59 +00007115 if( pCsr->eState>=CURSOR_REQUIRESEEK ){
7116 if( pCsr->eState==CURSOR_FAULT ){
7117 return pCsr->skip;
7118 }else{
7119 return SQLITE_ABORT;
7120 }
danielk1977dcbb5d32007-05-04 18:36:44 +00007121 }
7122
danielk1977d04417962007-05-02 13:16:30 +00007123 /* Check some preconditions:
danielk1977dcbb5d32007-05-04 18:36:44 +00007124 ** (a) the cursor is open for writing,
7125 ** (b) there is no read-lock on the table being modified and
7126 ** (c) the cursor points at a valid row of an intKey table.
danielk1977d04417962007-05-02 13:16:30 +00007127 */
danielk1977d04417962007-05-02 13:16:30 +00007128 if( !pCsr->wrFlag ){
danielk1977dcbb5d32007-05-04 18:36:44 +00007129 return SQLITE_READONLY;
danielk1977d04417962007-05-02 13:16:30 +00007130 }
drhd0679ed2007-08-28 22:24:34 +00007131 assert( !pCsr->pBt->readOnly
7132 && pCsr->pBt->inTransaction==TRANS_WRITE );
danielk1977d04417962007-05-02 13:16:30 +00007133 if( checkReadLocks(pCsr->pBtree, pCsr->pgnoRoot, pCsr) ){
7134 return SQLITE_LOCKED; /* The table pCur points to has a read lock */
7135 }
7136 if( pCsr->eState==CURSOR_INVALID || !pCsr->pPage->intKey ){
7137 return SQLITE_ERROR;
danielk1977b4e9af92007-05-01 17:49:49 +00007138 }
7139
danielk19779f8d6402007-05-02 17:48:45 +00007140 return accessPayload(pCsr, offset, amt, (unsigned char *)z, 0, 1);
danielk1977b4e9af92007-05-01 17:49:49 +00007141}
danielk19772dec9702007-05-02 16:48:37 +00007142
7143/*
7144** Set a flag on this cursor to cache the locations of pages from the
danielk1977da107192007-05-04 08:32:13 +00007145** overflow list for the current row. This is used by cursors opened
7146** for incremental blob IO only.
7147**
7148** This function sets a flag only. The actual page location cache
7149** (stored in BtCursor.aOverflow[]) is allocated and used by function
7150** accessPayload() (the worker function for sqlite3BtreeData() and
7151** sqlite3BtreePutData()).
danielk19772dec9702007-05-02 16:48:37 +00007152*/
7153void sqlite3BtreeCacheOverflow(BtCursor *pCur){
drh1fee73e2007-08-29 04:00:57 +00007154 assert( cursorHoldsMutex(pCur) );
drhe5fe6902007-12-07 18:55:28 +00007155 assert( sqlite3_mutex_held(pCur->pBtree->db->mutex) );
danielk1977dcbb5d32007-05-04 18:36:44 +00007156 assert(!pCur->isIncrblobHandle);
danielk19772dec9702007-05-02 16:48:37 +00007157 assert(!pCur->aOverflow);
danielk1977dcbb5d32007-05-04 18:36:44 +00007158 pCur->isIncrblobHandle = 1;
danielk19772dec9702007-05-02 16:48:37 +00007159}
danielk1977b4e9af92007-05-01 17:49:49 +00007160#endif