drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 1 | /* |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 2 | ** 2004 April 6 |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 3 | ** |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 4 | ** The author disclaims copyright to this source code. In place of |
| 5 | ** a legal notice, here is a blessing: |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 6 | ** |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 7 | ** 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. |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 10 | ** |
| 11 | ************************************************************************* |
danielk1977 | 4152e67 | 2007-09-12 17:01:45 +0000 | [diff] [blame^] | 12 | ** $Id: btree.c,v 1.426 2007/09/12 17:01:45 danielk1977 Exp $ |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 13 | ** |
| 14 | ** This file implements a external (disk-based) database using BTrees. |
drh | a315289 | 2007-05-05 11:48:52 +0000 | [diff] [blame] | 15 | ** See the header comment on "btreeInt.h" for additional information. |
| 16 | ** Including a description of file format and an overview of operation. |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 17 | */ |
drh | a315289 | 2007-05-05 11:48:52 +0000 | [diff] [blame] | 18 | #include "btreeInt.h" |
paul | b95a886 | 2003-04-01 21:16:41 +0000 | [diff] [blame] | 19 | |
drh | 8c42ca9 | 2001-06-22 19:15:00 +0000 | [diff] [blame] | 20 | /* |
drh | a315289 | 2007-05-05 11:48:52 +0000 | [diff] [blame] | 21 | ** The header string that appears at the beginning of every |
| 22 | ** SQLite database. |
drh | 556b2a2 | 2005-06-14 16:04:05 +0000 | [diff] [blame] | 23 | */ |
drh | 556b2a2 | 2005-06-14 16:04:05 +0000 | [diff] [blame] | 24 | static const char zMagicHeader[] = SQLITE_FILE_HEADER; |
drh | 08ed44e | 2001-04-29 23:32:55 +0000 | [diff] [blame] | 25 | |
drh | 8c42ca9 | 2001-06-22 19:15:00 +0000 | [diff] [blame] | 26 | /* |
drh | a315289 | 2007-05-05 11:48:52 +0000 | [diff] [blame] | 27 | ** Set this global variable to 1 to enable tracing using the TRACE |
| 28 | ** macro. |
drh | 615ae55 | 2005-01-16 23:21:00 +0000 | [diff] [blame] | 29 | */ |
| 30 | #if SQLITE_TEST |
drh | 0f7eb61 | 2006-08-08 13:51:43 +0000 | [diff] [blame] | 31 | int sqlite3_btree_trace=0; /* True to enable tracing */ |
drh | 615ae55 | 2005-01-16 23:21:00 +0000 | [diff] [blame] | 32 | #endif |
drh | 615ae55 | 2005-01-16 23:21:00 +0000 | [diff] [blame] | 33 | |
drh | 86f8c19 | 2007-08-22 00:39:19 +0000 | [diff] [blame] | 34 | |
| 35 | |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 36 | #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 |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 40 | ** in shared cache. The variables have file scope during normal builds, |
drh | 86f8c19 | 2007-08-22 00:39:19 +0000 | [diff] [blame] | 41 | ** but the test harness needs to access these variables so we make them |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 42 | ** global for test builds. |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 43 | */ |
| 44 | #ifdef SQLITE_TEST |
| 45 | BtShared *sqlite3SharedCacheList = 0; |
| 46 | int sqlite3SharedCacheEnabled = 0; |
| 47 | #else |
| 48 | static BtShared *sqlite3SharedCacheList = 0; |
| 49 | static int sqlite3SharedCacheEnabled = 0; |
| 50 | #endif |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 51 | #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 | */ |
| 61 | int sqlite3_enable_shared_cache(int enable){ |
| 62 | sqlite3SharedCacheEnabled = enable; |
| 63 | return SQLITE_OK; |
| 64 | } |
| 65 | #endif |
| 66 | |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 67 | |
drh | 615ae55 | 2005-01-16 23:21:00 +0000 | [diff] [blame] | 68 | /* |
drh | 66cbd15 | 2004-09-01 16:12:25 +0000 | [diff] [blame] | 69 | ** Forward declaration |
| 70 | */ |
drh | 980b1a7 | 2006-08-16 16:42:48 +0000 | [diff] [blame] | 71 | static int checkReadLocks(Btree*,Pgno,BtCursor*); |
drh | 66cbd15 | 2004-09-01 16:12:25 +0000 | [diff] [blame] | 72 | |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 73 | |
| 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 |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 80 | ** of each BtShared structure and so this locking is not necessary. |
| 81 | ** So define the lock related functions as no-ops. |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 82 | */ |
| 83 | #define queryTableLock(a,b,c) SQLITE_OK |
| 84 | #define lockTable(a,b,c) SQLITE_OK |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 85 | #define unlockAllTables(a) |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 86 | #endif |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 87 | |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 88 | #ifndef SQLITE_OMIT_SHARED_CACHE |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 89 | /* |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 90 | ** 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 |
danielk1977 | c87d34d | 2006-01-06 13:00:28 +0000 | [diff] [blame] | 93 | ** SQLITE_LOCKED if not. |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 94 | */ |
| 95 | static int queryTableLock(Btree *p, Pgno iTab, u8 eLock){ |
| 96 | BtShared *pBt = p->pBt; |
| 97 | BtLock *pIter; |
| 98 | |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 99 | assert( sqlite3BtreeHoldsMutex(p) ); |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 100 | |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 101 | /* This is a no-op if the shared-cache is not enabled */ |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 102 | if( !p->sharable ){ |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 103 | return SQLITE_OK; |
| 104 | } |
| 105 | |
| 106 | /* This (along with lockTable()) is where the ReadUncommitted flag is |
| 107 | ** dealt with. If the caller is querying for a read-lock and the flag is |
| 108 | ** set, it is unconditionally granted - even if there are write-locks |
| 109 | ** on the table. If a write-lock is requested, the ReadUncommitted flag |
| 110 | ** is not considered. |
| 111 | ** |
| 112 | ** In function lockTable(), if a read-lock is demanded and the |
| 113 | ** ReadUncommitted flag is set, no entry is added to the locks list |
| 114 | ** (BtShared.pLock). |
| 115 | ** |
| 116 | ** To summarize: If the ReadUncommitted flag is set, then read cursors do |
| 117 | ** not create or respect table locks. The locking procedure for a |
| 118 | ** write-cursor does not change. |
| 119 | */ |
| 120 | if( |
| 121 | !p->pSqlite || |
| 122 | 0==(p->pSqlite->flags&SQLITE_ReadUncommitted) || |
| 123 | eLock==WRITE_LOCK || |
drh | 47ded16 | 2006-01-06 01:42:58 +0000 | [diff] [blame] | 124 | iTab==MASTER_ROOT |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 125 | ){ |
| 126 | for(pIter=pBt->pLock; pIter; pIter=pIter->pNext){ |
| 127 | if( pIter->pBtree!=p && pIter->iTable==iTab && |
| 128 | (pIter->eLock!=eLock || eLock!=READ_LOCK) ){ |
danielk1977 | c87d34d | 2006-01-06 13:00:28 +0000 | [diff] [blame] | 129 | return SQLITE_LOCKED; |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 130 | } |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 131 | } |
| 132 | } |
| 133 | return SQLITE_OK; |
| 134 | } |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 135 | #endif /* !SQLITE_OMIT_SHARED_CACHE */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 136 | |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 137 | #ifndef SQLITE_OMIT_SHARED_CACHE |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 138 | /* |
| 139 | ** Add a lock on the table with root-page iTable to the shared-btree used |
| 140 | ** by Btree handle p. Parameter eLock must be either READ_LOCK or |
| 141 | ** WRITE_LOCK. |
| 142 | ** |
| 143 | ** SQLITE_OK is returned if the lock is added successfully. SQLITE_BUSY and |
| 144 | ** SQLITE_NOMEM may also be returned. |
| 145 | */ |
| 146 | static int lockTable(Btree *p, Pgno iTable, u8 eLock){ |
| 147 | BtShared *pBt = p->pBt; |
| 148 | BtLock *pLock = 0; |
| 149 | BtLock *pIter; |
| 150 | |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 151 | assert( sqlite3BtreeHoldsMutex(p) ); |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 152 | |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 153 | /* This is a no-op if the shared-cache is not enabled */ |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 154 | if( !p->sharable ){ |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 155 | return SQLITE_OK; |
| 156 | } |
| 157 | |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 158 | assert( SQLITE_OK==queryTableLock(p, iTable, eLock) ); |
| 159 | |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 160 | /* If the read-uncommitted flag is set and a read-lock is requested, |
| 161 | ** return early without adding an entry to the BtShared.pLock list. See |
| 162 | ** comment in function queryTableLock() for more info on handling |
| 163 | ** the ReadUncommitted flag. |
| 164 | */ |
| 165 | if( |
| 166 | (p->pSqlite) && |
| 167 | (p->pSqlite->flags&SQLITE_ReadUncommitted) && |
| 168 | (eLock==READ_LOCK) && |
drh | 47ded16 | 2006-01-06 01:42:58 +0000 | [diff] [blame] | 169 | iTable!=MASTER_ROOT |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 170 | ){ |
| 171 | return SQLITE_OK; |
| 172 | } |
| 173 | |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 174 | /* First search the list for an existing lock on this table. */ |
| 175 | for(pIter=pBt->pLock; pIter; pIter=pIter->pNext){ |
| 176 | if( pIter->iTable==iTable && pIter->pBtree==p ){ |
| 177 | pLock = pIter; |
| 178 | break; |
| 179 | } |
| 180 | } |
| 181 | |
| 182 | /* If the above search did not find a BtLock struct associating Btree p |
| 183 | ** with table iTable, allocate one and link it into the list. |
| 184 | */ |
| 185 | if( !pLock ){ |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 186 | pLock = (BtLock *)sqlite3MallocZero(sizeof(BtLock)); |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 187 | if( !pLock ){ |
| 188 | return SQLITE_NOMEM; |
| 189 | } |
| 190 | pLock->iTable = iTable; |
| 191 | pLock->pBtree = p; |
| 192 | pLock->pNext = pBt->pLock; |
| 193 | pBt->pLock = pLock; |
| 194 | } |
| 195 | |
| 196 | /* Set the BtLock.eLock variable to the maximum of the current lock |
| 197 | ** and the requested lock. This means if a write-lock was already held |
| 198 | ** and a read-lock requested, we don't incorrectly downgrade the lock. |
| 199 | */ |
| 200 | assert( WRITE_LOCK>READ_LOCK ); |
danielk1977 | 5118b91 | 2005-12-30 16:31:53 +0000 | [diff] [blame] | 201 | if( eLock>pLock->eLock ){ |
| 202 | pLock->eLock = eLock; |
| 203 | } |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 204 | |
| 205 | return SQLITE_OK; |
| 206 | } |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 207 | #endif /* !SQLITE_OMIT_SHARED_CACHE */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 208 | |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 209 | #ifndef SQLITE_OMIT_SHARED_CACHE |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 210 | /* |
| 211 | ** Release all the table locks (locks obtained via calls to the lockTable() |
| 212 | ** procedure) held by Btree handle p. |
| 213 | */ |
| 214 | static void unlockAllTables(Btree *p){ |
| 215 | BtLock **ppIter = &p->pBt->pLock; |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 216 | |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 217 | assert( sqlite3BtreeHoldsMutex(p) ); |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 218 | assert( p->sharable || 0==*ppIter ); |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 219 | |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 220 | while( *ppIter ){ |
| 221 | BtLock *pLock = *ppIter; |
| 222 | if( pLock->pBtree==p ){ |
| 223 | *ppIter = pLock->pNext; |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 224 | sqlite3_free(pLock); |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 225 | }else{ |
| 226 | ppIter = &pLock->pNext; |
| 227 | } |
| 228 | } |
| 229 | } |
| 230 | #endif /* SQLITE_OMIT_SHARED_CACHE */ |
| 231 | |
drh | 980b1a7 | 2006-08-16 16:42:48 +0000 | [diff] [blame] | 232 | static void releasePage(MemPage *pPage); /* Forward reference */ |
| 233 | |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 234 | /* |
| 235 | ** Verify that the cursor holds a mutex on the BtShared |
| 236 | */ |
| 237 | #ifndef NDEBUG |
| 238 | static int cursorHoldsMutex(BtCursor *p){ |
drh | ff0587c | 2007-08-29 17:43:19 +0000 | [diff] [blame] | 239 | return sqlite3_mutex_held(p->pBt->mutex); |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 240 | } |
| 241 | #endif |
| 242 | |
| 243 | |
danielk1977 | 92d4d7a | 2007-05-04 12:05:56 +0000 | [diff] [blame] | 244 | #ifndef SQLITE_OMIT_INCRBLOB |
| 245 | /* |
| 246 | ** Invalidate the overflow page-list cache for cursor pCur, if any. |
| 247 | */ |
| 248 | static void invalidateOverflowCache(BtCursor *pCur){ |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 249 | assert( cursorHoldsMutex(pCur) ); |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 250 | sqlite3_free(pCur->aOverflow); |
danielk1977 | 92d4d7a | 2007-05-04 12:05:56 +0000 | [diff] [blame] | 251 | pCur->aOverflow = 0; |
| 252 | } |
| 253 | |
| 254 | /* |
| 255 | ** Invalidate the overflow page-list cache for all cursors opened |
| 256 | ** on the shared btree structure pBt. |
| 257 | */ |
| 258 | static void invalidateAllOverflowCache(BtShared *pBt){ |
| 259 | BtCursor *p; |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 260 | assert( sqlite3_mutex_held(pBt->mutex) ); |
danielk1977 | 92d4d7a | 2007-05-04 12:05:56 +0000 | [diff] [blame] | 261 | for(p=pBt->pCursor; p; p=p->pNext){ |
| 262 | invalidateOverflowCache(p); |
| 263 | } |
| 264 | } |
| 265 | #else |
| 266 | #define invalidateOverflowCache(x) |
| 267 | #define invalidateAllOverflowCache(x) |
| 268 | #endif |
| 269 | |
drh | 980b1a7 | 2006-08-16 16:42:48 +0000 | [diff] [blame] | 270 | /* |
| 271 | ** Save the current cursor position in the variables BtCursor.nKey |
| 272 | ** and BtCursor.pKey. The cursor's state is set to CURSOR_REQUIRESEEK. |
| 273 | */ |
| 274 | static int saveCursorPosition(BtCursor *pCur){ |
| 275 | int rc; |
| 276 | |
| 277 | assert( CURSOR_VALID==pCur->eState ); |
| 278 | assert( 0==pCur->pKey ); |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 279 | assert( cursorHoldsMutex(pCur) ); |
drh | 980b1a7 | 2006-08-16 16:42:48 +0000 | [diff] [blame] | 280 | |
| 281 | rc = sqlite3BtreeKeySize(pCur, &pCur->nKey); |
| 282 | |
| 283 | /* If this is an intKey table, then the above call to BtreeKeySize() |
| 284 | ** stores the integer key in pCur->nKey. In this case this value is |
| 285 | ** all that is required. Otherwise, if pCur is not open on an intKey |
| 286 | ** table, then malloc space for and store the pCur->nKey bytes of key |
| 287 | ** data. |
| 288 | */ |
| 289 | if( rc==SQLITE_OK && 0==pCur->pPage->intKey){ |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 290 | void *pKey = sqlite3_malloc(pCur->nKey); |
drh | 980b1a7 | 2006-08-16 16:42:48 +0000 | [diff] [blame] | 291 | if( pKey ){ |
| 292 | rc = sqlite3BtreeKey(pCur, 0, pCur->nKey, pKey); |
| 293 | if( rc==SQLITE_OK ){ |
| 294 | pCur->pKey = pKey; |
| 295 | }else{ |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 296 | sqlite3_free(pKey); |
drh | 980b1a7 | 2006-08-16 16:42:48 +0000 | [diff] [blame] | 297 | } |
| 298 | }else{ |
| 299 | rc = SQLITE_NOMEM; |
| 300 | } |
| 301 | } |
| 302 | assert( !pCur->pPage->intKey || !pCur->pKey ); |
| 303 | |
| 304 | if( rc==SQLITE_OK ){ |
| 305 | releasePage(pCur->pPage); |
| 306 | pCur->pPage = 0; |
| 307 | pCur->eState = CURSOR_REQUIRESEEK; |
| 308 | } |
| 309 | |
danielk1977 | 92d4d7a | 2007-05-04 12:05:56 +0000 | [diff] [blame] | 310 | invalidateOverflowCache(pCur); |
drh | 980b1a7 | 2006-08-16 16:42:48 +0000 | [diff] [blame] | 311 | return rc; |
| 312 | } |
| 313 | |
| 314 | /* |
| 315 | ** Save the positions of all cursors except pExcept open on the table |
| 316 | ** with root-page iRoot. Usually, this is called just before cursor |
| 317 | ** pExcept is used to modify the table (BtreeDelete() or BtreeInsert()). |
| 318 | */ |
| 319 | static int saveAllCursors(BtShared *pBt, Pgno iRoot, BtCursor *pExcept){ |
| 320 | BtCursor *p; |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 321 | assert( sqlite3_mutex_held(pBt->mutex) ); |
drh | d0679ed | 2007-08-28 22:24:34 +0000 | [diff] [blame] | 322 | assert( pExcept==0 || pExcept->pBt==pBt ); |
drh | 980b1a7 | 2006-08-16 16:42:48 +0000 | [diff] [blame] | 323 | for(p=pBt->pCursor; p; p=p->pNext){ |
| 324 | if( p!=pExcept && (0==iRoot || p->pgnoRoot==iRoot) && |
| 325 | p->eState==CURSOR_VALID ){ |
| 326 | int rc = saveCursorPosition(p); |
| 327 | if( SQLITE_OK!=rc ){ |
| 328 | return rc; |
| 329 | } |
| 330 | } |
| 331 | } |
| 332 | return SQLITE_OK; |
| 333 | } |
| 334 | |
| 335 | /* |
drh | bf700f3 | 2007-03-31 02:36:44 +0000 | [diff] [blame] | 336 | ** Clear the current cursor position. |
| 337 | */ |
| 338 | static void clearCursorPosition(BtCursor *pCur){ |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 339 | assert( cursorHoldsMutex(pCur) ); |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 340 | sqlite3_free(pCur->pKey); |
drh | bf700f3 | 2007-03-31 02:36:44 +0000 | [diff] [blame] | 341 | pCur->pKey = 0; |
| 342 | pCur->eState = CURSOR_INVALID; |
| 343 | } |
| 344 | |
| 345 | /* |
drh | 980b1a7 | 2006-08-16 16:42:48 +0000 | [diff] [blame] | 346 | ** Restore the cursor to the position it was in (or as close to as possible) |
| 347 | ** when saveCursorPosition() was called. Note that this call deletes the |
| 348 | ** saved position info stored by saveCursorPosition(), so there can be |
| 349 | ** at most one effective restoreOrClearCursorPosition() call after each |
| 350 | ** saveCursorPosition(). |
| 351 | ** |
| 352 | ** If the second argument argument - doSeek - is false, then instead of |
| 353 | ** returning the cursor to it's saved position, any saved position is deleted |
| 354 | ** and the cursor state set to CURSOR_INVALID. |
| 355 | */ |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 356 | int sqlite3BtreeRestoreOrClearCursorPosition(BtCursor *pCur){ |
drh | bf700f3 | 2007-03-31 02:36:44 +0000 | [diff] [blame] | 357 | int rc; |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 358 | assert( cursorHoldsMutex(pCur) ); |
drh | fb98264 | 2007-08-30 01:19:59 +0000 | [diff] [blame] | 359 | assert( pCur->eState>=CURSOR_REQUIRESEEK ); |
| 360 | if( pCur->eState==CURSOR_FAULT ){ |
| 361 | return pCur->skip; |
| 362 | } |
danielk1977 | 32a0d8b | 2007-05-04 19:03:02 +0000 | [diff] [blame] | 363 | #ifndef SQLITE_OMIT_INCRBLOB |
danielk1977 | dcbb5d3 | 2007-05-04 18:36:44 +0000 | [diff] [blame] | 364 | if( pCur->isIncrblobHandle ){ |
| 365 | return SQLITE_ABORT; |
| 366 | } |
danielk1977 | 32a0d8b | 2007-05-04 19:03:02 +0000 | [diff] [blame] | 367 | #endif |
drh | 980b1a7 | 2006-08-16 16:42:48 +0000 | [diff] [blame] | 368 | pCur->eState = CURSOR_INVALID; |
drh | bf700f3 | 2007-03-31 02:36:44 +0000 | [diff] [blame] | 369 | rc = sqlite3BtreeMoveto(pCur, pCur->pKey, pCur->nKey, 0, &pCur->skip); |
drh | 980b1a7 | 2006-08-16 16:42:48 +0000 | [diff] [blame] | 370 | if( rc==SQLITE_OK ){ |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 371 | sqlite3_free(pCur->pKey); |
drh | 980b1a7 | 2006-08-16 16:42:48 +0000 | [diff] [blame] | 372 | pCur->pKey = 0; |
drh | bf700f3 | 2007-03-31 02:36:44 +0000 | [diff] [blame] | 373 | assert( pCur->eState==CURSOR_VALID || pCur->eState==CURSOR_INVALID ); |
drh | 980b1a7 | 2006-08-16 16:42:48 +0000 | [diff] [blame] | 374 | } |
| 375 | return rc; |
| 376 | } |
| 377 | |
drh | bf700f3 | 2007-03-31 02:36:44 +0000 | [diff] [blame] | 378 | #define restoreOrClearCursorPosition(p) \ |
drh | fb98264 | 2007-08-30 01:19:59 +0000 | [diff] [blame] | 379 | (p->eState>=CURSOR_REQUIRESEEK ? \ |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 380 | sqlite3BtreeRestoreOrClearCursorPosition(p) : \ |
| 381 | SQLITE_OK) |
drh | 980b1a7 | 2006-08-16 16:42:48 +0000 | [diff] [blame] | 382 | |
danielk1977 | 599fcba | 2004-11-08 07:13:13 +0000 | [diff] [blame] | 383 | #ifndef SQLITE_OMIT_AUTOVACUUM |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 384 | /* |
drh | a315289 | 2007-05-05 11:48:52 +0000 | [diff] [blame] | 385 | ** Given a page number of a regular database page, return the page |
| 386 | ** number for the pointer-map page that contains the entry for the |
| 387 | ** input page number. |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 388 | */ |
danielk1977 | 266664d | 2006-02-10 08:24:21 +0000 | [diff] [blame] | 389 | static Pgno ptrmapPageno(BtShared *pBt, Pgno pgno){ |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 390 | int nPagesPerMapPage, iPtrMap, ret; |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 391 | assert( sqlite3_mutex_held(pBt->mutex) ); |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 392 | nPagesPerMapPage = (pBt->usableSize/5)+1; |
| 393 | iPtrMap = (pgno-2)/nPagesPerMapPage; |
| 394 | ret = (iPtrMap*nPagesPerMapPage) + 2; |
danielk1977 | 266664d | 2006-02-10 08:24:21 +0000 | [diff] [blame] | 395 | if( ret==PENDING_BYTE_PAGE(pBt) ){ |
| 396 | ret++; |
| 397 | } |
| 398 | return ret; |
| 399 | } |
danielk1977 | a19df67 | 2004-11-03 11:37:07 +0000 | [diff] [blame] | 400 | |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 401 | /* |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 402 | ** Write an entry into the pointer map. |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 403 | ** |
| 404 | ** This routine updates the pointer map entry for page number 'key' |
| 405 | ** so that it maps to type 'eType' and parent page number 'pgno'. |
| 406 | ** An error code is returned if something goes wrong, otherwise SQLITE_OK. |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 407 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 408 | static int ptrmapPut(BtShared *pBt, Pgno key, u8 eType, Pgno parent){ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 409 | DbPage *pDbPage; /* The pointer map page */ |
| 410 | u8 *pPtrmap; /* The pointer map data */ |
| 411 | Pgno iPtrmap; /* The pointer map page number */ |
| 412 | int offset; /* Offset in pointer map page */ |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 413 | int rc; |
| 414 | |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 415 | assert( sqlite3_mutex_held(pBt->mutex) ); |
danielk1977 | 266664d | 2006-02-10 08:24:21 +0000 | [diff] [blame] | 416 | /* The master-journal page number must never be used as a pointer map page */ |
| 417 | assert( 0==PTRMAP_ISPAGE(pBt, PENDING_BYTE_PAGE(pBt)) ); |
| 418 | |
danielk1977 | ac11ee6 | 2005-01-15 12:45:51 +0000 | [diff] [blame] | 419 | assert( pBt->autoVacuum ); |
danielk1977 | fdb7cdb | 2005-01-17 02:12:18 +0000 | [diff] [blame] | 420 | if( key==0 ){ |
drh | 4928570 | 2005-09-17 15:20:26 +0000 | [diff] [blame] | 421 | return SQLITE_CORRUPT_BKPT; |
danielk1977 | fdb7cdb | 2005-01-17 02:12:18 +0000 | [diff] [blame] | 422 | } |
danielk1977 | 266664d | 2006-02-10 08:24:21 +0000 | [diff] [blame] | 423 | iPtrmap = PTRMAP_PAGENO(pBt, key); |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 424 | rc = sqlite3PagerGet(pBt->pPager, iPtrmap, &pDbPage); |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 425 | if( rc!=SQLITE_OK ){ |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 426 | return rc; |
| 427 | } |
danielk1977 | 266664d | 2006-02-10 08:24:21 +0000 | [diff] [blame] | 428 | offset = PTRMAP_PTROFFSET(pBt, key); |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 429 | pPtrmap = (u8 *)sqlite3PagerGetData(pDbPage); |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 430 | |
drh | 615ae55 | 2005-01-16 23:21:00 +0000 | [diff] [blame] | 431 | if( eType!=pPtrmap[offset] || get4byte(&pPtrmap[offset+1])!=parent ){ |
| 432 | TRACE(("PTRMAP_UPDATE: %d->(%d,%d)\n", key, eType, parent)); |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 433 | rc = sqlite3PagerWrite(pDbPage); |
danielk1977 | 5558a8a | 2005-01-17 07:53:44 +0000 | [diff] [blame] | 434 | if( rc==SQLITE_OK ){ |
| 435 | pPtrmap[offset] = eType; |
| 436 | put4byte(&pPtrmap[offset+1], parent); |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 437 | } |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 438 | } |
| 439 | |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 440 | sqlite3PagerUnref(pDbPage); |
danielk1977 | 5558a8a | 2005-01-17 07:53:44 +0000 | [diff] [blame] | 441 | return rc; |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 442 | } |
| 443 | |
| 444 | /* |
| 445 | ** Read an entry from the pointer map. |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 446 | ** |
| 447 | ** This routine retrieves the pointer map entry for page 'key', writing |
| 448 | ** the type and parent page number to *pEType and *pPgno respectively. |
| 449 | ** An error code is returned if something goes wrong, otherwise SQLITE_OK. |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 450 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 451 | static int ptrmapGet(BtShared *pBt, Pgno key, u8 *pEType, Pgno *pPgno){ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 452 | DbPage *pDbPage; /* The pointer map page */ |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 453 | int iPtrmap; /* Pointer map page index */ |
| 454 | u8 *pPtrmap; /* Pointer map page data */ |
| 455 | int offset; /* Offset of entry in pointer map */ |
| 456 | int rc; |
| 457 | |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 458 | assert( sqlite3_mutex_held(pBt->mutex) ); |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 459 | |
danielk1977 | 266664d | 2006-02-10 08:24:21 +0000 | [diff] [blame] | 460 | iPtrmap = PTRMAP_PAGENO(pBt, key); |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 461 | rc = sqlite3PagerGet(pBt->pPager, iPtrmap, &pDbPage); |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 462 | if( rc!=0 ){ |
| 463 | return rc; |
| 464 | } |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 465 | pPtrmap = (u8 *)sqlite3PagerGetData(pDbPage); |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 466 | |
danielk1977 | 266664d | 2006-02-10 08:24:21 +0000 | [diff] [blame] | 467 | offset = PTRMAP_PTROFFSET(pBt, key); |
drh | 43617e9 | 2006-03-06 20:55:46 +0000 | [diff] [blame] | 468 | assert( pEType!=0 ); |
| 469 | *pEType = pPtrmap[offset]; |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 470 | if( pPgno ) *pPgno = get4byte(&pPtrmap[offset+1]); |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 471 | |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 472 | sqlite3PagerUnref(pDbPage); |
drh | 4928570 | 2005-09-17 15:20:26 +0000 | [diff] [blame] | 473 | if( *pEType<1 || *pEType>5 ) return SQLITE_CORRUPT_BKPT; |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 474 | return SQLITE_OK; |
| 475 | } |
| 476 | |
| 477 | #endif /* SQLITE_OMIT_AUTOVACUUM */ |
| 478 | |
drh | 0d316a4 | 2002-08-11 20:10:47 +0000 | [diff] [blame] | 479 | /* |
drh | 271efa5 | 2004-05-30 19:19:05 +0000 | [diff] [blame] | 480 | ** Given a btree page and a cell index (0 means the first cell on |
| 481 | ** the page, 1 means the second cell, and so forth) return a pointer |
| 482 | ** to the cell content. |
| 483 | ** |
| 484 | ** This routine works only for pages that do not contain overflow cells. |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 485 | */ |
danielk1977 | 1cc5ed8 | 2007-05-16 17:28:43 +0000 | [diff] [blame] | 486 | #define findCell(pPage, iCell) \ |
| 487 | ((pPage)->aData + get2byte(&(pPage)->aData[(pPage)->cellOffset+2*(iCell)])) |
drh | e6e4d6b | 2007-08-05 23:52:05 +0000 | [diff] [blame] | 488 | #ifdef SQLITE_TEST |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 489 | u8 *sqlite3BtreeFindCell(MemPage *pPage, int iCell){ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 490 | assert( iCell>=0 ); |
drh | 029f3f8 | 2007-06-20 15:14:10 +0000 | [diff] [blame] | 491 | assert( iCell<get2byte(&pPage->aData[pPage->hdrOffset+3]) ); |
danielk1977 | 1cc5ed8 | 2007-05-16 17:28:43 +0000 | [diff] [blame] | 492 | return findCell(pPage, iCell); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 493 | } |
drh | e6e4d6b | 2007-08-05 23:52:05 +0000 | [diff] [blame] | 494 | #endif |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 495 | |
| 496 | /* |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 497 | ** This a more complex version of sqlite3BtreeFindCell() that works for |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 498 | ** pages that do contain overflow cells. See insert |
| 499 | */ |
| 500 | static u8 *findOverflowCell(MemPage *pPage, int iCell){ |
| 501 | int i; |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 502 | assert( sqlite3_mutex_held(pPage->pBt->mutex) ); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 503 | for(i=pPage->nOverflow-1; i>=0; i--){ |
drh | 6d08b4d | 2004-07-20 12:45:22 +0000 | [diff] [blame] | 504 | int k; |
| 505 | struct _OvflCell *pOvfl; |
| 506 | pOvfl = &pPage->aOvfl[i]; |
| 507 | k = pOvfl->idx; |
| 508 | if( k<=iCell ){ |
| 509 | if( k==iCell ){ |
| 510 | return pOvfl->pCell; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 511 | } |
| 512 | iCell--; |
| 513 | } |
| 514 | } |
danielk1977 | 1cc5ed8 | 2007-05-16 17:28:43 +0000 | [diff] [blame] | 515 | return findCell(pPage, iCell); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 516 | } |
| 517 | |
| 518 | /* |
| 519 | ** Parse a cell content block and fill in the CellInfo structure. There |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 520 | ** are two versions of this function. sqlite3BtreeParseCell() takes a |
| 521 | ** cell index as the second argument and sqlite3BtreeParseCellPtr() |
| 522 | ** takes a pointer to the body of the cell as its second argument. |
danielk1977 | 1cc5ed8 | 2007-05-16 17:28:43 +0000 | [diff] [blame] | 523 | ** |
| 524 | ** Within this file, the parseCell() macro can be called instead of |
| 525 | ** sqlite3BtreeParseCellPtr(). Using some compilers, this will be faster. |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 526 | */ |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 527 | void sqlite3BtreeParseCellPtr( |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 528 | MemPage *pPage, /* Page containing the cell */ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 529 | u8 *pCell, /* Pointer to the cell text. */ |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 530 | CellInfo *pInfo /* Fill in this structure */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 531 | ){ |
drh | 271efa5 | 2004-05-30 19:19:05 +0000 | [diff] [blame] | 532 | int n; /* Number bytes in cell content header */ |
| 533 | u32 nPayload; /* Number of bytes of cell payload */ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 534 | |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 535 | assert( sqlite3_mutex_held(pPage->pBt->mutex) ); |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 536 | |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 537 | pInfo->pCell = pCell; |
drh | ab01f61 | 2004-05-22 02:55:23 +0000 | [diff] [blame] | 538 | assert( pPage->leaf==0 || pPage->leaf==1 ); |
drh | 271efa5 | 2004-05-30 19:19:05 +0000 | [diff] [blame] | 539 | n = pPage->childPtrSize; |
| 540 | assert( n==4-4*pPage->leaf ); |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 541 | if( pPage->hasData ){ |
drh | 271efa5 | 2004-05-30 19:19:05 +0000 | [diff] [blame] | 542 | n += getVarint32(&pCell[n], &nPayload); |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 543 | }else{ |
drh | 271efa5 | 2004-05-30 19:19:05 +0000 | [diff] [blame] | 544 | nPayload = 0; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 545 | } |
drh | 271efa5 | 2004-05-30 19:19:05 +0000 | [diff] [blame] | 546 | pInfo->nData = nPayload; |
drh | 504b698 | 2006-01-22 21:52:56 +0000 | [diff] [blame] | 547 | if( pPage->intKey ){ |
| 548 | n += getVarint(&pCell[n], (u64 *)&pInfo->nKey); |
| 549 | }else{ |
| 550 | u32 x; |
| 551 | n += getVarint32(&pCell[n], &x); |
| 552 | pInfo->nKey = x; |
| 553 | nPayload += x; |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 554 | } |
drh | 7236583 | 2007-03-06 15:53:44 +0000 | [diff] [blame] | 555 | pInfo->nPayload = nPayload; |
drh | 504b698 | 2006-01-22 21:52:56 +0000 | [diff] [blame] | 556 | pInfo->nHeader = n; |
drh | 271efa5 | 2004-05-30 19:19:05 +0000 | [diff] [blame] | 557 | if( nPayload<=pPage->maxLocal ){ |
| 558 | /* This is the (easy) common case where the entire payload fits |
| 559 | ** on the local page. No overflow is required. |
| 560 | */ |
| 561 | int nSize; /* Total size of cell content in bytes */ |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 562 | pInfo->nLocal = nPayload; |
| 563 | pInfo->iOverflow = 0; |
drh | 271efa5 | 2004-05-30 19:19:05 +0000 | [diff] [blame] | 564 | nSize = nPayload + n; |
| 565 | if( nSize<4 ){ |
| 566 | nSize = 4; /* Minimum cell size is 4 */ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 567 | } |
drh | 271efa5 | 2004-05-30 19:19:05 +0000 | [diff] [blame] | 568 | pInfo->nSize = nSize; |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 569 | }else{ |
drh | 271efa5 | 2004-05-30 19:19:05 +0000 | [diff] [blame] | 570 | /* If the payload will not fit completely on the local page, we have |
| 571 | ** to decide how much to store locally and how much to spill onto |
| 572 | ** overflow pages. The strategy is to minimize the amount of unused |
| 573 | ** space on overflow pages while keeping the amount of local storage |
| 574 | ** in between minLocal and maxLocal. |
| 575 | ** |
| 576 | ** Warning: changing the way overflow payload is distributed in any |
| 577 | ** way will result in an incompatible file format. |
| 578 | */ |
| 579 | int minLocal; /* Minimum amount of payload held locally */ |
| 580 | int maxLocal; /* Maximum amount of payload held locally */ |
| 581 | int surplus; /* Overflow payload available for local storage */ |
| 582 | |
| 583 | minLocal = pPage->minLocal; |
| 584 | maxLocal = pPage->maxLocal; |
| 585 | surplus = minLocal + (nPayload - minLocal)%(pPage->pBt->usableSize - 4); |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 586 | if( surplus <= maxLocal ){ |
| 587 | pInfo->nLocal = surplus; |
| 588 | }else{ |
| 589 | pInfo->nLocal = minLocal; |
| 590 | } |
| 591 | pInfo->iOverflow = pInfo->nLocal + n; |
| 592 | pInfo->nSize = pInfo->iOverflow + 4; |
| 593 | } |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 594 | } |
danielk1977 | 1cc5ed8 | 2007-05-16 17:28:43 +0000 | [diff] [blame] | 595 | #define parseCell(pPage, iCell, pInfo) \ |
| 596 | sqlite3BtreeParseCellPtr((pPage), findCell((pPage), (iCell)), (pInfo)) |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 597 | void sqlite3BtreeParseCell( |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 598 | MemPage *pPage, /* Page containing the cell */ |
| 599 | int iCell, /* The cell index. First cell is 0 */ |
| 600 | CellInfo *pInfo /* Fill in this structure */ |
| 601 | ){ |
danielk1977 | 1cc5ed8 | 2007-05-16 17:28:43 +0000 | [diff] [blame] | 602 | parseCell(pPage, iCell, pInfo); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 603 | } |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 604 | |
| 605 | /* |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 606 | ** Compute the total number of bytes that a Cell needs in the cell |
| 607 | ** data area of the btree-page. The return number includes the cell |
| 608 | ** data header and the local payload, but not any overflow page or |
| 609 | ** the space used by the cell pointer. |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 610 | */ |
danielk1977 | bc6ada4 | 2004-06-30 08:20:16 +0000 | [diff] [blame] | 611 | #ifndef NDEBUG |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 612 | static int cellSize(MemPage *pPage, int iCell){ |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 613 | CellInfo info; |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 614 | sqlite3BtreeParseCell(pPage, iCell, &info); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 615 | return info.nSize; |
| 616 | } |
danielk1977 | bc6ada4 | 2004-06-30 08:20:16 +0000 | [diff] [blame] | 617 | #endif |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 618 | static int cellSizePtr(MemPage *pPage, u8 *pCell){ |
| 619 | CellInfo info; |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 620 | sqlite3BtreeParseCellPtr(pPage, pCell, &info); |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 621 | return info.nSize; |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 622 | } |
| 623 | |
danielk1977 | 79a40da | 2005-01-16 08:00:01 +0000 | [diff] [blame] | 624 | #ifndef SQLITE_OMIT_AUTOVACUUM |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 625 | /* |
danielk1977 | 2683665 | 2005-01-17 01:33:13 +0000 | [diff] [blame] | 626 | ** If the cell pCell, part of page pPage contains a pointer |
danielk1977 | 79a40da | 2005-01-16 08:00:01 +0000 | [diff] [blame] | 627 | ** to an overflow page, insert an entry into the pointer-map |
| 628 | ** for the overflow page. |
danielk1977 | ac11ee6 | 2005-01-15 12:45:51 +0000 | [diff] [blame] | 629 | */ |
danielk1977 | 2683665 | 2005-01-17 01:33:13 +0000 | [diff] [blame] | 630 | static int ptrmapPutOvflPtr(MemPage *pPage, u8 *pCell){ |
danielk1977 | 79a40da | 2005-01-16 08:00:01 +0000 | [diff] [blame] | 631 | if( pCell ){ |
| 632 | CellInfo info; |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 633 | sqlite3BtreeParseCellPtr(pPage, pCell, &info); |
drh | 7236583 | 2007-03-06 15:53:44 +0000 | [diff] [blame] | 634 | assert( (info.nData+(pPage->intKey?0:info.nKey))==info.nPayload ); |
danielk1977 | 79a40da | 2005-01-16 08:00:01 +0000 | [diff] [blame] | 635 | if( (info.nData+(pPage->intKey?0:info.nKey))>info.nLocal ){ |
| 636 | Pgno ovfl = get4byte(&pCell[info.iOverflow]); |
| 637 | return ptrmapPut(pPage->pBt, ovfl, PTRMAP_OVERFLOW1, pPage->pgno); |
| 638 | } |
danielk1977 | ac11ee6 | 2005-01-15 12:45:51 +0000 | [diff] [blame] | 639 | } |
danielk1977 | 79a40da | 2005-01-16 08:00:01 +0000 | [diff] [blame] | 640 | return SQLITE_OK; |
danielk1977 | ac11ee6 | 2005-01-15 12:45:51 +0000 | [diff] [blame] | 641 | } |
danielk1977 | 2683665 | 2005-01-17 01:33:13 +0000 | [diff] [blame] | 642 | /* |
| 643 | ** If the cell with index iCell on page pPage contains a pointer |
| 644 | ** to an overflow page, insert an entry into the pointer-map |
| 645 | ** for the overflow page. |
| 646 | */ |
| 647 | static int ptrmapPutOvfl(MemPage *pPage, int iCell){ |
| 648 | u8 *pCell; |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 649 | assert( sqlite3_mutex_held(pPage->pBt->mutex) ); |
danielk1977 | 2683665 | 2005-01-17 01:33:13 +0000 | [diff] [blame] | 650 | pCell = findOverflowCell(pPage, iCell); |
| 651 | return ptrmapPutOvflPtr(pPage, pCell); |
| 652 | } |
danielk1977 | 79a40da | 2005-01-16 08:00:01 +0000 | [diff] [blame] | 653 | #endif |
| 654 | |
danielk1977 | ac11ee6 | 2005-01-15 12:45:51 +0000 | [diff] [blame] | 655 | |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 656 | /* |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 657 | ** Defragment the page given. All Cells are moved to the |
drh | 3a4a2d4 | 2005-11-24 14:24:28 +0000 | [diff] [blame] | 658 | ** end of the page and all free space is collected into one |
| 659 | ** big FreeBlk that occurs in between the header and cell |
drh | 31beae9 | 2005-11-24 14:34:36 +0000 | [diff] [blame] | 660 | ** pointer array and the cell content area. |
drh | 365d68f | 2001-05-11 11:02:46 +0000 | [diff] [blame] | 661 | */ |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 662 | static int defragmentPage(MemPage *pPage){ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 663 | int i; /* Loop counter */ |
| 664 | int pc; /* Address of a i-th cell */ |
| 665 | int addr; /* Offset of first byte after cell pointer array */ |
| 666 | int hdr; /* Offset to the page header */ |
| 667 | int size; /* Size of a cell */ |
| 668 | int usableSize; /* Number of usable bytes on a page */ |
| 669 | int cellOffset; /* Offset to the cell pointer array */ |
| 670 | int brk; /* Offset to the cell content area */ |
| 671 | int nCell; /* Number of cells on the page */ |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 672 | unsigned char *data; /* The page data */ |
| 673 | unsigned char *temp; /* Temp area for cell content */ |
drh | 2af926b | 2001-05-15 00:39:25 +0000 | [diff] [blame] | 674 | |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 675 | assert( sqlite3PagerIswriteable(pPage->pDbPage) ); |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 676 | assert( pPage->pBt!=0 ); |
drh | 90f5ecb | 2004-07-22 01:19:35 +0000 | [diff] [blame] | 677 | assert( pPage->pBt->usableSize <= SQLITE_MAX_PAGE_SIZE ); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 678 | assert( pPage->nOverflow==0 ); |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 679 | assert( sqlite3_mutex_held(pPage->pBt->mutex) ); |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 680 | temp = sqlite3_malloc( pPage->pBt->pageSize ); |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 681 | if( temp==0 ) return SQLITE_NOMEM; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 682 | data = pPage->aData; |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 683 | hdr = pPage->hdrOffset; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 684 | cellOffset = pPage->cellOffset; |
| 685 | nCell = pPage->nCell; |
| 686 | assert( nCell==get2byte(&data[hdr+3]) ); |
| 687 | usableSize = pPage->pBt->usableSize; |
| 688 | brk = get2byte(&data[hdr+5]); |
| 689 | memcpy(&temp[brk], &data[brk], usableSize - brk); |
| 690 | brk = usableSize; |
| 691 | for(i=0; i<nCell; i++){ |
| 692 | u8 *pAddr; /* The i-th cell pointer */ |
| 693 | pAddr = &data[cellOffset + i*2]; |
| 694 | pc = get2byte(pAddr); |
| 695 | assert( pc<pPage->pBt->usableSize ); |
| 696 | size = cellSizePtr(pPage, &temp[pc]); |
| 697 | brk -= size; |
| 698 | memcpy(&data[brk], &temp[pc], size); |
| 699 | put2byte(pAddr, brk); |
drh | 2af926b | 2001-05-15 00:39:25 +0000 | [diff] [blame] | 700 | } |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 701 | assert( brk>=cellOffset+2*nCell ); |
| 702 | put2byte(&data[hdr+5], brk); |
| 703 | data[hdr+1] = 0; |
| 704 | data[hdr+2] = 0; |
| 705 | data[hdr+7] = 0; |
| 706 | addr = cellOffset+2*nCell; |
| 707 | memset(&data[addr], 0, brk-addr); |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 708 | sqlite3_free(temp); |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 709 | return SQLITE_OK; |
drh | 365d68f | 2001-05-11 11:02:46 +0000 | [diff] [blame] | 710 | } |
| 711 | |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 712 | /* |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 713 | ** Allocate nByte bytes of space on a page. |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 714 | ** |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 715 | ** Return the index into pPage->aData[] of the first byte of |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 716 | ** the new allocation. Or return 0 if there is not enough free |
| 717 | ** space on the page to satisfy the allocation request. |
drh | 2af926b | 2001-05-15 00:39:25 +0000 | [diff] [blame] | 718 | ** |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 719 | ** If the page contains nBytes of free space but does not contain |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 720 | ** nBytes of contiguous free space, then this routine automatically |
| 721 | ** calls defragementPage() to consolidate all free space before |
| 722 | ** allocating the new chunk. |
drh | 7e3b0a0 | 2001-04-28 16:52:40 +0000 | [diff] [blame] | 723 | */ |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 724 | static int allocateSpace(MemPage *pPage, int nByte){ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 725 | int addr, pc, hdr; |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 726 | int size; |
drh | 24cd67e | 2004-05-10 16:18:47 +0000 | [diff] [blame] | 727 | int nFrag; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 728 | int top; |
| 729 | int nCell; |
| 730 | int cellOffset; |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 731 | unsigned char *data; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 732 | |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 733 | data = pPage->aData; |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 734 | assert( sqlite3PagerIswriteable(pPage->pDbPage) ); |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 735 | assert( pPage->pBt ); |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 736 | assert( sqlite3_mutex_held(pPage->pBt->mutex) ); |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 737 | if( nByte<4 ) nByte = 4; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 738 | if( pPage->nFree<nByte || pPage->nOverflow>0 ) return 0; |
| 739 | pPage->nFree -= nByte; |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 740 | hdr = pPage->hdrOffset; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 741 | |
| 742 | nFrag = data[hdr+7]; |
| 743 | if( nFrag<60 ){ |
| 744 | /* Search the freelist looking for a slot big enough to satisfy the |
| 745 | ** space request. */ |
| 746 | addr = hdr+1; |
| 747 | while( (pc = get2byte(&data[addr]))>0 ){ |
| 748 | size = get2byte(&data[pc+2]); |
| 749 | if( size>=nByte ){ |
| 750 | if( size<nByte+4 ){ |
| 751 | memcpy(&data[addr], &data[pc], 2); |
| 752 | data[hdr+7] = nFrag + size - nByte; |
| 753 | return pc; |
| 754 | }else{ |
| 755 | put2byte(&data[pc+2], size-nByte); |
| 756 | return pc + size - nByte; |
| 757 | } |
| 758 | } |
| 759 | addr = pc; |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 760 | } |
| 761 | } |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 762 | |
| 763 | /* Allocate memory from the gap in between the cell pointer array |
| 764 | ** and the cell content area. |
| 765 | */ |
| 766 | top = get2byte(&data[hdr+5]); |
| 767 | nCell = get2byte(&data[hdr+3]); |
| 768 | cellOffset = pPage->cellOffset; |
| 769 | if( nFrag>=60 || cellOffset + 2*nCell > top - nByte ){ |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 770 | if( defragmentPage(pPage) ) return 0; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 771 | top = get2byte(&data[hdr+5]); |
drh | 2af926b | 2001-05-15 00:39:25 +0000 | [diff] [blame] | 772 | } |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 773 | top -= nByte; |
| 774 | assert( cellOffset + 2*nCell <= top ); |
| 775 | put2byte(&data[hdr+5], top); |
| 776 | return top; |
drh | 7e3b0a0 | 2001-04-28 16:52:40 +0000 | [diff] [blame] | 777 | } |
| 778 | |
| 779 | /* |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 780 | ** Return a section of the pPage->aData to the freelist. |
| 781 | ** The first byte of the new free block is pPage->aDisk[start] |
| 782 | ** and the size of the block is "size" bytes. |
drh | 306dc21 | 2001-05-21 13:45:10 +0000 | [diff] [blame] | 783 | ** |
| 784 | ** Most of the effort here is involved in coalesing adjacent |
| 785 | ** free blocks into a single big free block. |
drh | 7e3b0a0 | 2001-04-28 16:52:40 +0000 | [diff] [blame] | 786 | */ |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 787 | static void freeSpace(MemPage *pPage, int start, int size){ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 788 | int addr, pbegin, hdr; |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 789 | unsigned char *data = pPage->aData; |
drh | 2af926b | 2001-05-15 00:39:25 +0000 | [diff] [blame] | 790 | |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 791 | assert( pPage->pBt!=0 ); |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 792 | assert( sqlite3PagerIswriteable(pPage->pDbPage) ); |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 793 | assert( start>=pPage->hdrOffset+6+(pPage->leaf?0:4) ); |
danielk1977 | bc6ada4 | 2004-06-30 08:20:16 +0000 | [diff] [blame] | 794 | assert( (start + size)<=pPage->pBt->usableSize ); |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 795 | assert( sqlite3_mutex_held(pPage->pBt->mutex) ); |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 796 | if( size<4 ) size = 4; |
| 797 | |
drh | fcce93f | 2006-02-22 03:08:32 +0000 | [diff] [blame] | 798 | #ifdef SQLITE_SECURE_DELETE |
| 799 | /* Overwrite deleted information with zeros when the SECURE_DELETE |
| 800 | ** option is enabled at compile-time */ |
| 801 | memset(&data[start], 0, size); |
| 802 | #endif |
| 803 | |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 804 | /* Add the space back into the linked list of freeblocks */ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 805 | hdr = pPage->hdrOffset; |
| 806 | addr = hdr + 1; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 807 | while( (pbegin = get2byte(&data[addr]))<start && pbegin>0 ){ |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 808 | assert( pbegin<=pPage->pBt->usableSize-4 ); |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 809 | assert( pbegin>addr ); |
| 810 | addr = pbegin; |
drh | 2af926b | 2001-05-15 00:39:25 +0000 | [diff] [blame] | 811 | } |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 812 | assert( pbegin<=pPage->pBt->usableSize-4 ); |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 813 | assert( pbegin>addr || pbegin==0 ); |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 814 | put2byte(&data[addr], start); |
| 815 | put2byte(&data[start], pbegin); |
| 816 | put2byte(&data[start+2], size); |
drh | 2af926b | 2001-05-15 00:39:25 +0000 | [diff] [blame] | 817 | pPage->nFree += size; |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 818 | |
| 819 | /* Coalesce adjacent free blocks */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 820 | addr = pPage->hdrOffset + 1; |
| 821 | while( (pbegin = get2byte(&data[addr]))>0 ){ |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 822 | int pnext, psize; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 823 | assert( pbegin>addr ); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 824 | assert( pbegin<=pPage->pBt->usableSize-4 ); |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 825 | pnext = get2byte(&data[pbegin]); |
| 826 | psize = get2byte(&data[pbegin+2]); |
| 827 | if( pbegin + psize + 3 >= pnext && pnext>0 ){ |
| 828 | int frag = pnext - (pbegin+psize); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 829 | assert( frag<=data[pPage->hdrOffset+7] ); |
| 830 | data[pPage->hdrOffset+7] -= frag; |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 831 | put2byte(&data[pbegin], get2byte(&data[pnext])); |
| 832 | put2byte(&data[pbegin+2], pnext+get2byte(&data[pnext+2])-pbegin); |
| 833 | }else{ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 834 | addr = pbegin; |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 835 | } |
| 836 | } |
drh | 7e3b0a0 | 2001-04-28 16:52:40 +0000 | [diff] [blame] | 837 | |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 838 | /* If the cell content area begins with a freeblock, remove it. */ |
| 839 | if( data[hdr+1]==data[hdr+5] && data[hdr+2]==data[hdr+6] ){ |
| 840 | int top; |
| 841 | pbegin = get2byte(&data[hdr+1]); |
| 842 | memcpy(&data[hdr+1], &data[pbegin], 2); |
| 843 | top = get2byte(&data[hdr+5]); |
| 844 | put2byte(&data[hdr+5], top + get2byte(&data[pbegin+2])); |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 845 | } |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 846 | } |
| 847 | |
| 848 | /* |
drh | 271efa5 | 2004-05-30 19:19:05 +0000 | [diff] [blame] | 849 | ** Decode the flags byte (the first byte of the header) for a page |
| 850 | ** and initialize fields of the MemPage structure accordingly. |
| 851 | */ |
| 852 | static void decodeFlags(MemPage *pPage, int flagByte){ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 853 | BtShared *pBt; /* A copy of pPage->pBt */ |
drh | 271efa5 | 2004-05-30 19:19:05 +0000 | [diff] [blame] | 854 | |
| 855 | assert( pPage->hdrOffset==(pPage->pgno==1 ? 100 : 0) ); |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 856 | assert( sqlite3_mutex_held(pPage->pBt->mutex) ); |
drh | 271efa5 | 2004-05-30 19:19:05 +0000 | [diff] [blame] | 857 | pPage->intKey = (flagByte & (PTF_INTKEY|PTF_LEAFDATA))!=0; |
| 858 | pPage->zeroData = (flagByte & PTF_ZERODATA)!=0; |
| 859 | pPage->leaf = (flagByte & PTF_LEAF)!=0; |
| 860 | pPage->childPtrSize = 4*(pPage->leaf==0); |
| 861 | pBt = pPage->pBt; |
| 862 | if( flagByte & PTF_LEAFDATA ){ |
| 863 | pPage->leafData = 1; |
| 864 | pPage->maxLocal = pBt->maxLeaf; |
| 865 | pPage->minLocal = pBt->minLeaf; |
| 866 | }else{ |
| 867 | pPage->leafData = 0; |
| 868 | pPage->maxLocal = pBt->maxLocal; |
| 869 | pPage->minLocal = pBt->minLocal; |
| 870 | } |
| 871 | pPage->hasData = !(pPage->zeroData || (!pPage->leaf && pPage->leafData)); |
| 872 | } |
| 873 | |
| 874 | /* |
drh | 7e3b0a0 | 2001-04-28 16:52:40 +0000 | [diff] [blame] | 875 | ** Initialize the auxiliary information for a disk block. |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 876 | ** |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 877 | ** The pParent parameter must be a pointer to the MemPage which |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 878 | ** is the parent of the page being initialized. The root of a |
| 879 | ** BTree has no parent and so for that page, pParent==NULL. |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 880 | ** |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 881 | ** Return SQLITE_OK on success. If we see that the page does |
drh | da47d77 | 2002-12-02 04:25:19 +0000 | [diff] [blame] | 882 | ** not contain a well-formed database page, then return |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 883 | ** SQLITE_CORRUPT. Note that a return of SQLITE_OK does not |
| 884 | ** guarantee that the page is well-formed. It only shows that |
| 885 | ** we failed to detect any corruption. |
drh | 7e3b0a0 | 2001-04-28 16:52:40 +0000 | [diff] [blame] | 886 | */ |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 887 | int sqlite3BtreeInitPage( |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 888 | MemPage *pPage, /* The page to be initialized */ |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 889 | MemPage *pParent /* The parent. Might be NULL */ |
| 890 | ){ |
drh | 271efa5 | 2004-05-30 19:19:05 +0000 | [diff] [blame] | 891 | int pc; /* Address of a freeblock within pPage->aData[] */ |
drh | 271efa5 | 2004-05-30 19:19:05 +0000 | [diff] [blame] | 892 | int hdr; /* Offset to beginning of page header */ |
| 893 | u8 *data; /* Equal to pPage->aData */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 894 | BtShared *pBt; /* The main btree structure */ |
drh | 271efa5 | 2004-05-30 19:19:05 +0000 | [diff] [blame] | 895 | int usableSize; /* Amount of usable space on each page */ |
| 896 | int cellOffset; /* Offset from start of page to first cell pointer */ |
| 897 | int nFree; /* Number of unused bytes on the page */ |
| 898 | int top; /* First byte of the cell content area */ |
drh | 2af926b | 2001-05-15 00:39:25 +0000 | [diff] [blame] | 899 | |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 900 | pBt = pPage->pBt; |
| 901 | assert( pBt!=0 ); |
| 902 | assert( pParent==0 || pParent->pBt==pBt ); |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 903 | assert( sqlite3_mutex_held(pBt->mutex) ); |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 904 | assert( pPage->pgno==sqlite3PagerPagenumber(pPage->pDbPage) ); |
drh | bf4bca5 | 2007-09-06 22:19:14 +0000 | [diff] [blame] | 905 | assert( pPage == sqlite3PagerGetExtra(pPage->pDbPage) ); |
| 906 | assert( pPage->aData == sqlite3PagerGetData(pPage->pDbPage) ); |
drh | ee696e2 | 2004-08-30 16:52:17 +0000 | [diff] [blame] | 907 | if( pPage->pParent!=pParent && (pPage->pParent!=0 || pPage->isInit) ){ |
| 908 | /* The parent page should never change unless the file is corrupt */ |
drh | 4928570 | 2005-09-17 15:20:26 +0000 | [diff] [blame] | 909 | return SQLITE_CORRUPT_BKPT; |
drh | ee696e2 | 2004-08-30 16:52:17 +0000 | [diff] [blame] | 910 | } |
drh | 10617cd | 2004-05-14 15:27:27 +0000 | [diff] [blame] | 911 | if( pPage->isInit ) return SQLITE_OK; |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 912 | if( pPage->pParent==0 && pParent!=0 ){ |
| 913 | pPage->pParent = pParent; |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 914 | sqlite3PagerRef(pParent->pDbPage); |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 915 | } |
drh | de64713 | 2004-05-07 17:57:49 +0000 | [diff] [blame] | 916 | hdr = pPage->hdrOffset; |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 917 | data = pPage->aData; |
drh | 271efa5 | 2004-05-30 19:19:05 +0000 | [diff] [blame] | 918 | decodeFlags(pPage, data[hdr]); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 919 | pPage->nOverflow = 0; |
drh | c8629a1 | 2004-05-08 20:07:40 +0000 | [diff] [blame] | 920 | pPage->idxShift = 0; |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 921 | usableSize = pBt->usableSize; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 922 | pPage->cellOffset = cellOffset = hdr + 12 - 4*pPage->leaf; |
| 923 | top = get2byte(&data[hdr+5]); |
| 924 | pPage->nCell = get2byte(&data[hdr+3]); |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 925 | if( pPage->nCell>MX_CELL(pBt) ){ |
drh | ee696e2 | 2004-08-30 16:52:17 +0000 | [diff] [blame] | 926 | /* To many cells for a single page. The page must be corrupt */ |
drh | 4928570 | 2005-09-17 15:20:26 +0000 | [diff] [blame] | 927 | return SQLITE_CORRUPT_BKPT; |
drh | ee696e2 | 2004-08-30 16:52:17 +0000 | [diff] [blame] | 928 | } |
| 929 | if( pPage->nCell==0 && pParent!=0 && pParent->pgno!=1 ){ |
| 930 | /* All pages must have at least one cell, except for root pages */ |
drh | 4928570 | 2005-09-17 15:20:26 +0000 | [diff] [blame] | 931 | return SQLITE_CORRUPT_BKPT; |
drh | ee696e2 | 2004-08-30 16:52:17 +0000 | [diff] [blame] | 932 | } |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 933 | |
| 934 | /* Compute the total free space on the page */ |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 935 | pc = get2byte(&data[hdr+1]); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 936 | nFree = data[hdr+7] + top - (cellOffset + 2*pPage->nCell); |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 937 | while( pc>0 ){ |
| 938 | int next, size; |
drh | ee696e2 | 2004-08-30 16:52:17 +0000 | [diff] [blame] | 939 | if( pc>usableSize-4 ){ |
| 940 | /* Free block is off the page */ |
drh | 4928570 | 2005-09-17 15:20:26 +0000 | [diff] [blame] | 941 | return SQLITE_CORRUPT_BKPT; |
drh | ee696e2 | 2004-08-30 16:52:17 +0000 | [diff] [blame] | 942 | } |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 943 | next = get2byte(&data[pc]); |
| 944 | size = get2byte(&data[pc+2]); |
drh | ee696e2 | 2004-08-30 16:52:17 +0000 | [diff] [blame] | 945 | if( next>0 && next<=pc+size+3 ){ |
| 946 | /* Free blocks must be in accending order */ |
drh | 4928570 | 2005-09-17 15:20:26 +0000 | [diff] [blame] | 947 | return SQLITE_CORRUPT_BKPT; |
drh | ee696e2 | 2004-08-30 16:52:17 +0000 | [diff] [blame] | 948 | } |
drh | 3add367 | 2004-05-15 00:29:24 +0000 | [diff] [blame] | 949 | nFree += size; |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 950 | pc = next; |
| 951 | } |
drh | 3add367 | 2004-05-15 00:29:24 +0000 | [diff] [blame] | 952 | pPage->nFree = nFree; |
drh | ee696e2 | 2004-08-30 16:52:17 +0000 | [diff] [blame] | 953 | if( nFree>=usableSize ){ |
| 954 | /* Free space cannot exceed total page size */ |
drh | 4928570 | 2005-09-17 15:20:26 +0000 | [diff] [blame] | 955 | return SQLITE_CORRUPT_BKPT; |
drh | ee696e2 | 2004-08-30 16:52:17 +0000 | [diff] [blame] | 956 | } |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 957 | |
drh | de64713 | 2004-05-07 17:57:49 +0000 | [diff] [blame] | 958 | pPage->isInit = 1; |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 959 | return SQLITE_OK; |
drh | 7e3b0a0 | 2001-04-28 16:52:40 +0000 | [diff] [blame] | 960 | } |
| 961 | |
| 962 | /* |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 963 | ** Set up a raw page so that it looks like a database page holding |
| 964 | ** no entries. |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 965 | */ |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 966 | static void zeroPage(MemPage *pPage, int flags){ |
| 967 | unsigned char *data = pPage->aData; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 968 | BtShared *pBt = pPage->pBt; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 969 | int hdr = pPage->hdrOffset; |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 970 | int first; |
| 971 | |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 972 | assert( sqlite3PagerPagenumber(pPage->pDbPage)==pPage->pgno ); |
drh | bf4bca5 | 2007-09-06 22:19:14 +0000 | [diff] [blame] | 973 | assert( sqlite3PagerGetExtra(pPage->pDbPage) == (void*)pPage ); |
| 974 | assert( sqlite3PagerGetData(pPage->pDbPage) == data ); |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 975 | assert( sqlite3PagerIswriteable(pPage->pDbPage) ); |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 976 | assert( sqlite3_mutex_held(pBt->mutex) ); |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 977 | memset(&data[hdr], 0, pBt->usableSize - hdr); |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 978 | data[hdr] = flags; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 979 | first = hdr + 8 + 4*((flags&PTF_LEAF)==0); |
| 980 | memset(&data[hdr+1], 0, 4); |
| 981 | data[hdr+7] = 0; |
| 982 | put2byte(&data[hdr+5], pBt->usableSize); |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 983 | pPage->nFree = pBt->usableSize - first; |
drh | 271efa5 | 2004-05-30 19:19:05 +0000 | [diff] [blame] | 984 | decodeFlags(pPage, flags); |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 985 | pPage->hdrOffset = hdr; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 986 | pPage->cellOffset = first; |
| 987 | pPage->nOverflow = 0; |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 988 | pPage->idxShift = 0; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 989 | pPage->nCell = 0; |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 990 | pPage->isInit = 1; |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 991 | } |
| 992 | |
| 993 | /* |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 994 | ** Get a page from the pager. Initialize the MemPage.pBt and |
| 995 | ** MemPage.aData elements if needed. |
drh | 538f570 | 2007-04-13 02:14:30 +0000 | [diff] [blame] | 996 | ** |
| 997 | ** If the noContent flag is set, it means that we do not care about |
| 998 | ** the content of the page at this time. So do not go to the disk |
| 999 | ** to fetch the content. Just fill in the content with zeros for now. |
| 1000 | ** If in the future we call sqlite3PagerWrite() on this page, that |
| 1001 | ** means we have started to be concerned about content and the disk |
| 1002 | ** read should occur at that point. |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1003 | */ |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 1004 | int sqlite3BtreeGetPage( |
| 1005 | BtShared *pBt, /* The btree */ |
| 1006 | Pgno pgno, /* Number of the page to fetch */ |
| 1007 | MemPage **ppPage, /* Return the page in this parameter */ |
| 1008 | int noContent /* Do not load page content if true */ |
| 1009 | ){ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1010 | int rc; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1011 | MemPage *pPage; |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 1012 | DbPage *pDbPage; |
| 1013 | |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 1014 | assert( sqlite3_mutex_held(pBt->mutex) ); |
drh | 538f570 | 2007-04-13 02:14:30 +0000 | [diff] [blame] | 1015 | rc = sqlite3PagerAcquire(pBt->pPager, pgno, (DbPage**)&pDbPage, noContent); |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1016 | if( rc ) return rc; |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 1017 | pPage = (MemPage *)sqlite3PagerGetExtra(pDbPage); |
| 1018 | pPage->aData = sqlite3PagerGetData(pDbPage); |
| 1019 | pPage->pDbPage = pDbPage; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1020 | pPage->pBt = pBt; |
| 1021 | pPage->pgno = pgno; |
drh | de64713 | 2004-05-07 17:57:49 +0000 | [diff] [blame] | 1022 | pPage->hdrOffset = pPage->pgno==1 ? 100 : 0; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1023 | *ppPage = pPage; |
| 1024 | return SQLITE_OK; |
| 1025 | } |
| 1026 | |
| 1027 | /* |
drh | de64713 | 2004-05-07 17:57:49 +0000 | [diff] [blame] | 1028 | ** Get a page from the pager and initialize it. This routine |
| 1029 | ** is just a convenience wrapper around separate calls to |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 1030 | ** sqlite3BtreeGetPage() and sqlite3BtreeInitPage(). |
drh | de64713 | 2004-05-07 17:57:49 +0000 | [diff] [blame] | 1031 | */ |
| 1032 | static int getAndInitPage( |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1033 | BtShared *pBt, /* The database file */ |
drh | de64713 | 2004-05-07 17:57:49 +0000 | [diff] [blame] | 1034 | Pgno pgno, /* Number of the page to get */ |
| 1035 | MemPage **ppPage, /* Write the page pointer here */ |
| 1036 | MemPage *pParent /* Parent of the page */ |
| 1037 | ){ |
| 1038 | int rc; |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 1039 | assert( sqlite3_mutex_held(pBt->mutex) ); |
drh | ee696e2 | 2004-08-30 16:52:17 +0000 | [diff] [blame] | 1040 | if( pgno==0 ){ |
drh | 4928570 | 2005-09-17 15:20:26 +0000 | [diff] [blame] | 1041 | return SQLITE_CORRUPT_BKPT; |
drh | ee696e2 | 2004-08-30 16:52:17 +0000 | [diff] [blame] | 1042 | } |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 1043 | rc = sqlite3BtreeGetPage(pBt, pgno, ppPage, 0); |
drh | 10617cd | 2004-05-14 15:27:27 +0000 | [diff] [blame] | 1044 | if( rc==SQLITE_OK && (*ppPage)->isInit==0 ){ |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 1045 | rc = sqlite3BtreeInitPage(*ppPage, pParent); |
drh | de64713 | 2004-05-07 17:57:49 +0000 | [diff] [blame] | 1046 | } |
| 1047 | return rc; |
| 1048 | } |
| 1049 | |
| 1050 | /* |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1051 | ** Release a MemPage. This should be called once for each prior |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 1052 | ** call to sqlite3BtreeGetPage. |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1053 | */ |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 1054 | static void releasePage(MemPage *pPage){ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1055 | if( pPage ){ |
| 1056 | assert( pPage->aData ); |
| 1057 | assert( pPage->pBt ); |
drh | bf4bca5 | 2007-09-06 22:19:14 +0000 | [diff] [blame] | 1058 | assert( sqlite3PagerGetExtra(pPage->pDbPage) == (void*)pPage ); |
| 1059 | assert( sqlite3PagerGetData(pPage->pDbPage)==pPage->aData ); |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 1060 | assert( sqlite3_mutex_held(pPage->pBt->mutex) ); |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 1061 | sqlite3PagerUnref(pPage->pDbPage); |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1062 | } |
| 1063 | } |
| 1064 | |
| 1065 | /* |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 1066 | ** This routine is called when the reference count for a page |
| 1067 | ** reaches zero. We need to unref the pParent pointer when that |
| 1068 | ** happens. |
| 1069 | */ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 1070 | static void pageDestructor(DbPage *pData, int pageSize){ |
drh | 07d183d | 2005-05-01 22:52:42 +0000 | [diff] [blame] | 1071 | MemPage *pPage; |
| 1072 | assert( (pageSize & 7)==0 ); |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 1073 | pPage = (MemPage *)sqlite3PagerGetExtra(pData); |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 1074 | assert( pPage->isInit==0 || sqlite3_mutex_held(pPage->pBt->mutex) ); |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 1075 | if( pPage->pParent ){ |
| 1076 | MemPage *pParent = pPage->pParent; |
drh | d0679ed | 2007-08-28 22:24:34 +0000 | [diff] [blame] | 1077 | assert( pParent->pBt==pPage->pBt ); |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 1078 | pPage->pParent = 0; |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 1079 | releasePage(pParent); |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 1080 | } |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1081 | pPage->isInit = 0; |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 1082 | } |
| 1083 | |
| 1084 | /* |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 1085 | ** During a rollback, when the pager reloads information into the cache |
| 1086 | ** so that the cache is restored to its original state at the start of |
| 1087 | ** the transaction, for each page restored this routine is called. |
| 1088 | ** |
| 1089 | ** This routine needs to reset the extra data section at the end of the |
| 1090 | ** page to agree with the restored data. |
| 1091 | */ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 1092 | static void pageReinit(DbPage *pData, int pageSize){ |
drh | 07d183d | 2005-05-01 22:52:42 +0000 | [diff] [blame] | 1093 | MemPage *pPage; |
| 1094 | assert( (pageSize & 7)==0 ); |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 1095 | pPage = (MemPage *)sqlite3PagerGetExtra(pData); |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 1096 | if( pPage->isInit ){ |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 1097 | assert( sqlite3_mutex_held(pPage->pBt->mutex) ); |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 1098 | pPage->isInit = 0; |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 1099 | sqlite3BtreeInitPage(pPage, pPage->pParent); |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 1100 | } |
| 1101 | } |
| 1102 | |
| 1103 | /* |
drh | ad3e010 | 2004-09-03 23:32:18 +0000 | [diff] [blame] | 1104 | ** Open a database file. |
| 1105 | ** |
drh | 382c024 | 2001-10-06 16:33:02 +0000 | [diff] [blame] | 1106 | ** zFilename is the name of the database file. If zFilename is NULL |
drh | 1bee3d7 | 2001-10-15 00:44:35 +0000 | [diff] [blame] | 1107 | ** a new database with a random name is created. This randomly named |
drh | 23e11ca | 2004-05-04 17:27:28 +0000 | [diff] [blame] | 1108 | ** database file will be deleted when sqlite3BtreeClose() is called. |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 1109 | ** If zFilename is ":memory:" then an in-memory database is created |
| 1110 | ** that is automatically destroyed when it is closed. |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 1111 | */ |
drh | 23e11ca | 2004-05-04 17:27:28 +0000 | [diff] [blame] | 1112 | int sqlite3BtreeOpen( |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1113 | const char *zFilename, /* Name of the file containing the BTree database */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1114 | sqlite3 *pSqlite, /* Associated database handle */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1115 | Btree **ppBtree, /* Pointer to new Btree object written here */ |
drh | 33f4e02 | 2007-09-03 15:19:34 +0000 | [diff] [blame] | 1116 | int flags, /* Options */ |
| 1117 | int vfsFlags /* Flags passed through to sqlite3_vfs.xOpen() */ |
drh | 6019e16 | 2001-07-02 17:51:45 +0000 | [diff] [blame] | 1118 | ){ |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 1119 | sqlite3_vfs *pVfs; /* The VFS to use for this btree */ |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 1120 | BtShared *pBt = 0; /* Shared part of btree structure */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1121 | Btree *p; /* Handle to return */ |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 1122 | int rc = SQLITE_OK; |
drh | 90f5ecb | 2004-07-22 01:19:35 +0000 | [diff] [blame] | 1123 | int nReserve; |
| 1124 | unsigned char zDbHeader[100]; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1125 | |
| 1126 | /* Set the variable isMemdb to true for an in-memory database, or |
| 1127 | ** false for a file-based database. This symbol is only required if |
| 1128 | ** either of the shared-data or autovacuum features are compiled |
| 1129 | ** into the library. |
| 1130 | */ |
| 1131 | #if !defined(SQLITE_OMIT_SHARED_CACHE) || !defined(SQLITE_OMIT_AUTOVACUUM) |
| 1132 | #ifdef SQLITE_OMIT_MEMORYDB |
drh | 980b1a7 | 2006-08-16 16:42:48 +0000 | [diff] [blame] | 1133 | const int isMemdb = 0; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1134 | #else |
drh | 980b1a7 | 2006-08-16 16:42:48 +0000 | [diff] [blame] | 1135 | const int isMemdb = zFilename && !strcmp(zFilename, ":memory:"); |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1136 | #endif |
| 1137 | #endif |
| 1138 | |
drh | d0679ed | 2007-08-28 22:24:34 +0000 | [diff] [blame] | 1139 | assert( pSqlite!=0 ); |
| 1140 | assert( sqlite3_mutex_held(pSqlite->mutex) ); |
drh | 153c62c | 2007-08-24 03:51:33 +0000 | [diff] [blame] | 1141 | |
drh | d0679ed | 2007-08-28 22:24:34 +0000 | [diff] [blame] | 1142 | pVfs = pSqlite->pVfs; |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 1143 | p = sqlite3MallocZero(sizeof(Btree)); |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1144 | if( !p ){ |
| 1145 | return SQLITE_NOMEM; |
| 1146 | } |
| 1147 | p->inTrans = TRANS_NONE; |
| 1148 | p->pSqlite = pSqlite; |
| 1149 | |
drh | 198bf39 | 2006-01-06 21:52:49 +0000 | [diff] [blame] | 1150 | #if !defined(SQLITE_OMIT_SHARED_CACHE) && !defined(SQLITE_OMIT_DISKIO) |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 1151 | /* |
| 1152 | ** If this Btree is a candidate for shared cache, try to find an |
| 1153 | ** existing BtShared object that we can share with |
| 1154 | */ |
| 1155 | if( (flags & BTREE_PRIVATE)==0 |
| 1156 | && isMemdb==0 |
drh | d0679ed | 2007-08-28 22:24:34 +0000 | [diff] [blame] | 1157 | && (pSqlite->flags & SQLITE_Vtab)==0 |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 1158 | && zFilename && zFilename[0] |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 1159 | ){ |
drh | ff0587c | 2007-08-29 17:43:19 +0000 | [diff] [blame] | 1160 | if( sqlite3SharedCacheEnabled ){ |
| 1161 | char *zFullPathname = (char *)sqlite3_malloc(pVfs->mxPathname); |
| 1162 | sqlite3_mutex *mutexShared; |
| 1163 | p->sharable = 1; |
| 1164 | if( pSqlite ){ |
| 1165 | pSqlite->flags |= SQLITE_SharedCache; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1166 | } |
drh | ff0587c | 2007-08-29 17:43:19 +0000 | [diff] [blame] | 1167 | if( !zFullPathname ){ |
| 1168 | sqlite3_free(p); |
| 1169 | return SQLITE_NOMEM; |
| 1170 | } |
| 1171 | sqlite3OsFullPathname(pVfs, zFilename, zFullPathname); |
| 1172 | mutexShared = sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_MASTER); |
| 1173 | sqlite3_mutex_enter(mutexShared); |
| 1174 | for(pBt=sqlite3SharedCacheList; pBt; pBt=pBt->pNext){ |
| 1175 | assert( pBt->nRef>0 ); |
| 1176 | if( 0==strcmp(zFullPathname, sqlite3PagerFilename(pBt->pPager)) |
| 1177 | && sqlite3PagerVfs(pBt->pPager)==pVfs ){ |
| 1178 | p->pBt = pBt; |
| 1179 | pBt->nRef++; |
| 1180 | break; |
| 1181 | } |
| 1182 | } |
| 1183 | sqlite3_mutex_leave(mutexShared); |
| 1184 | sqlite3_free(zFullPathname); |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1185 | } |
drh | ff0587c | 2007-08-29 17:43:19 +0000 | [diff] [blame] | 1186 | #ifdef SQLITE_DEBUG |
| 1187 | else{ |
| 1188 | /* In debug mode, we mark all persistent databases as sharable |
| 1189 | ** even when they are not. This exercises the locking code and |
| 1190 | ** gives more opportunity for asserts(sqlite3_mutex_held()) |
| 1191 | ** statements to find locking problems. |
| 1192 | */ |
| 1193 | p->sharable = 1; |
| 1194 | } |
| 1195 | #endif |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1196 | } |
| 1197 | #endif |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 1198 | if( pBt==0 ){ |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 1199 | /* |
| 1200 | ** The following asserts make sure that structures used by the btree are |
| 1201 | ** the right size. This is to guard against size changes that result |
| 1202 | ** when compiling on a different architecture. |
danielk1977 | 03aded4 | 2004-11-22 05:26:27 +0000 | [diff] [blame] | 1203 | */ |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 1204 | assert( sizeof(i64)==8 || sizeof(i64)==4 ); |
| 1205 | assert( sizeof(u64)==8 || sizeof(u64)==4 ); |
| 1206 | assert( sizeof(u32)==4 ); |
| 1207 | assert( sizeof(u16)==2 ); |
| 1208 | assert( sizeof(Pgno)==4 ); |
| 1209 | |
| 1210 | pBt = sqlite3MallocZero( sizeof(*pBt) ); |
| 1211 | if( pBt==0 ){ |
| 1212 | rc = SQLITE_NOMEM; |
| 1213 | goto btree_open_out; |
| 1214 | } |
drh | 33f4e02 | 2007-09-03 15:19:34 +0000 | [diff] [blame] | 1215 | rc = sqlite3PagerOpen(pVfs, &pBt->pPager, zFilename, |
| 1216 | EXTRA_SIZE, flags, vfsFlags); |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 1217 | if( rc==SQLITE_OK ){ |
| 1218 | rc = sqlite3PagerReadFileheader(pBt->pPager,sizeof(zDbHeader),zDbHeader); |
| 1219 | } |
| 1220 | if( rc!=SQLITE_OK ){ |
| 1221 | goto btree_open_out; |
| 1222 | } |
| 1223 | p->pBt = pBt; |
| 1224 | |
| 1225 | sqlite3PagerSetDestructor(pBt->pPager, pageDestructor); |
| 1226 | sqlite3PagerSetReiniter(pBt->pPager, pageReinit); |
| 1227 | pBt->pCursor = 0; |
| 1228 | pBt->pPage1 = 0; |
| 1229 | pBt->readOnly = sqlite3PagerIsreadonly(pBt->pPager); |
| 1230 | pBt->pageSize = get2byte(&zDbHeader[16]); |
| 1231 | if( pBt->pageSize<512 || pBt->pageSize>SQLITE_MAX_PAGE_SIZE |
| 1232 | || ((pBt->pageSize-1)&pBt->pageSize)!=0 ){ |
danielk1977 | a1644fd | 2007-08-29 12:31:25 +0000 | [diff] [blame] | 1233 | pBt->pageSize = 0; |
| 1234 | sqlite3PagerSetPagesize(pBt->pPager, &pBt->pageSize); |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 1235 | pBt->maxEmbedFrac = 64; /* 25% */ |
| 1236 | pBt->minEmbedFrac = 32; /* 12.5% */ |
| 1237 | pBt->minLeafFrac = 32; /* 12.5% */ |
| 1238 | #ifndef SQLITE_OMIT_AUTOVACUUM |
| 1239 | /* If the magic name ":memory:" will create an in-memory database, then |
| 1240 | ** leave the autoVacuum mode at 0 (do not auto-vacuum), even if |
| 1241 | ** SQLITE_DEFAULT_AUTOVACUUM is true. On the other hand, if |
| 1242 | ** SQLITE_OMIT_MEMORYDB has been defined, then ":memory:" is just a |
| 1243 | ** regular file-name. In this case the auto-vacuum applies as per normal. |
| 1244 | */ |
| 1245 | if( zFilename && !isMemdb ){ |
| 1246 | pBt->autoVacuum = (SQLITE_DEFAULT_AUTOVACUUM ? 1 : 0); |
| 1247 | pBt->incrVacuum = (SQLITE_DEFAULT_AUTOVACUUM==2 ? 1 : 0); |
| 1248 | } |
| 1249 | #endif |
| 1250 | nReserve = 0; |
| 1251 | }else{ |
| 1252 | nReserve = zDbHeader[20]; |
| 1253 | pBt->maxEmbedFrac = zDbHeader[21]; |
| 1254 | pBt->minEmbedFrac = zDbHeader[22]; |
| 1255 | pBt->minLeafFrac = zDbHeader[23]; |
| 1256 | pBt->pageSizeFixed = 1; |
| 1257 | #ifndef SQLITE_OMIT_AUTOVACUUM |
| 1258 | pBt->autoVacuum = (get4byte(&zDbHeader[36 + 4*4])?1:0); |
| 1259 | pBt->incrVacuum = (get4byte(&zDbHeader[36 + 7*4])?1:0); |
| 1260 | #endif |
| 1261 | } |
| 1262 | pBt->usableSize = pBt->pageSize - nReserve; |
| 1263 | assert( (pBt->pageSize & 7)==0 ); /* 8-byte alignment of pageSize */ |
danielk1977 | a1644fd | 2007-08-29 12:31:25 +0000 | [diff] [blame] | 1264 | sqlite3PagerSetPagesize(pBt->pPager, &pBt->pageSize); |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 1265 | |
| 1266 | #if !defined(SQLITE_OMIT_SHARED_CACHE) && !defined(SQLITE_OMIT_DISKIO) |
| 1267 | /* Add the new BtShared object to the linked list sharable BtShareds. |
| 1268 | */ |
| 1269 | if( p->sharable ){ |
| 1270 | sqlite3_mutex *mutexShared; |
| 1271 | pBt->nRef = 1; |
| 1272 | mutexShared = sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_MASTER); |
drh | 3285db2 | 2007-09-03 22:00:39 +0000 | [diff] [blame] | 1273 | if( SQLITE_THREADSAFE ){ |
| 1274 | pBt->mutex = sqlite3_mutex_alloc(SQLITE_MUTEX_FAST); |
| 1275 | if( pBt->mutex==0 ){ |
| 1276 | rc = SQLITE_NOMEM; |
| 1277 | pSqlite->mallocFailed = 0; |
| 1278 | goto btree_open_out; |
| 1279 | } |
drh | ff0587c | 2007-08-29 17:43:19 +0000 | [diff] [blame] | 1280 | } |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 1281 | sqlite3_mutex_enter(mutexShared); |
| 1282 | pBt->pNext = sqlite3SharedCacheList; |
| 1283 | sqlite3SharedCacheList = pBt; |
| 1284 | sqlite3_mutex_leave(mutexShared); |
danielk1977 | 951af80 | 2004-11-05 15:45:09 +0000 | [diff] [blame] | 1285 | } |
drh | eee46cf | 2004-11-06 00:02:48 +0000 | [diff] [blame] | 1286 | #endif |
drh | 90f5ecb | 2004-07-22 01:19:35 +0000 | [diff] [blame] | 1287 | } |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1288 | |
drh | cfed7bc | 2006-03-13 14:28:05 +0000 | [diff] [blame] | 1289 | #if !defined(SQLITE_OMIT_SHARED_CACHE) && !defined(SQLITE_OMIT_DISKIO) |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 1290 | /* If the new Btree uses a sharable pBtShared, then link the new |
| 1291 | ** Btree into the list of all sharable Btrees for the same connection. |
drh | abddb0c | 2007-08-20 13:14:28 +0000 | [diff] [blame] | 1292 | ** The list is kept in ascending order by pBt address. |
danielk1977 | 54f0198 | 2006-01-18 15:25:17 +0000 | [diff] [blame] | 1293 | */ |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 1294 | if( p->sharable ){ |
| 1295 | int i; |
| 1296 | Btree *pSib; |
| 1297 | for(i=0; i<pSqlite->nDb; i++){ |
| 1298 | if( (pSib = pSqlite->aDb[i].pBt)!=0 && pSib->sharable ){ |
| 1299 | while( pSib->pPrev ){ pSib = pSib->pPrev; } |
| 1300 | if( p->pBt<pSib->pBt ){ |
| 1301 | p->pNext = pSib; |
| 1302 | p->pPrev = 0; |
| 1303 | pSib->pPrev = p; |
| 1304 | }else{ |
drh | abddb0c | 2007-08-20 13:14:28 +0000 | [diff] [blame] | 1305 | while( pSib->pNext && pSib->pNext->pBt<p->pBt ){ |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 1306 | pSib = pSib->pNext; |
| 1307 | } |
| 1308 | p->pNext = pSib->pNext; |
| 1309 | p->pPrev = pSib; |
| 1310 | if( p->pNext ){ |
| 1311 | p->pNext->pPrev = p; |
| 1312 | } |
| 1313 | pSib->pNext = p; |
| 1314 | } |
| 1315 | break; |
| 1316 | } |
| 1317 | } |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1318 | } |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1319 | #endif |
| 1320 | *ppBtree = p; |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 1321 | |
| 1322 | btree_open_out: |
| 1323 | if( rc!=SQLITE_OK ){ |
| 1324 | if( pBt && pBt->pPager ){ |
| 1325 | sqlite3PagerClose(pBt->pPager); |
| 1326 | } |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 1327 | sqlite3_free(pBt); |
| 1328 | sqlite3_free(p); |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 1329 | *ppBtree = 0; |
| 1330 | } |
| 1331 | return rc; |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 1332 | } |
| 1333 | |
| 1334 | /* |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 1335 | ** Decrement the BtShared.nRef counter. When it reaches zero, |
| 1336 | ** remove the BtShared structure from the sharing list. Return |
| 1337 | ** true if the BtShared.nRef counter reaches zero and return |
| 1338 | ** false if it is still positive. |
| 1339 | */ |
| 1340 | static int removeFromSharingList(BtShared *pBt){ |
| 1341 | #ifndef SQLITE_OMIT_SHARED_CACHE |
| 1342 | sqlite3_mutex *pMaster; |
| 1343 | BtShared *pList; |
| 1344 | int removed = 0; |
| 1345 | |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 1346 | assert( sqlite3_mutex_notheld(pBt->mutex) ); |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 1347 | pMaster = sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_MASTER); |
| 1348 | sqlite3_mutex_enter(pMaster); |
| 1349 | pBt->nRef--; |
| 1350 | if( pBt->nRef<=0 ){ |
| 1351 | if( sqlite3SharedCacheList==pBt ){ |
| 1352 | sqlite3SharedCacheList = pBt->pNext; |
| 1353 | }else{ |
| 1354 | pList = sqlite3SharedCacheList; |
| 1355 | while( pList && pList->pNext!=pBt ){ |
| 1356 | pList=pList->pNext; |
| 1357 | } |
| 1358 | if( pList ){ |
| 1359 | pList->pNext = pBt->pNext; |
| 1360 | } |
| 1361 | } |
drh | 3285db2 | 2007-09-03 22:00:39 +0000 | [diff] [blame] | 1362 | if( SQLITE_THREADSAFE ){ |
| 1363 | sqlite3_mutex_free(pBt->mutex); |
| 1364 | } |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 1365 | removed = 1; |
| 1366 | } |
| 1367 | sqlite3_mutex_leave(pMaster); |
| 1368 | return removed; |
| 1369 | #else |
| 1370 | return 1; |
| 1371 | #endif |
| 1372 | } |
| 1373 | |
| 1374 | /* |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 1375 | ** Close an open database and invalidate all cursors. |
| 1376 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1377 | int sqlite3BtreeClose(Btree *p){ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1378 | BtShared *pBt = p->pBt; |
| 1379 | BtCursor *pCur; |
| 1380 | |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1381 | /* Close all cursors opened via this handle. */ |
drh | d0679ed | 2007-08-28 22:24:34 +0000 | [diff] [blame] | 1382 | assert( sqlite3_mutex_held(p->pSqlite->mutex) ); |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 1383 | sqlite3BtreeEnter(p); |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1384 | pCur = pBt->pCursor; |
| 1385 | while( pCur ){ |
| 1386 | BtCursor *pTmp = pCur; |
| 1387 | pCur = pCur->pNext; |
| 1388 | if( pTmp->pBtree==p ){ |
| 1389 | sqlite3BtreeCloseCursor(pTmp); |
| 1390 | } |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 1391 | } |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1392 | |
danielk1977 | 8d34dfd | 2006-01-24 16:37:57 +0000 | [diff] [blame] | 1393 | /* Rollback any active transaction and free the handle structure. |
| 1394 | ** The call to sqlite3BtreeRollback() drops any table-locks held by |
| 1395 | ** this handle. |
| 1396 | */ |
danielk1977 | b597f74 | 2006-01-15 11:39:18 +0000 | [diff] [blame] | 1397 | sqlite3BtreeRollback(p); |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 1398 | sqlite3BtreeLeave(p); |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1399 | |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1400 | /* If there are still other outstanding references to the shared-btree |
| 1401 | ** structure, return now. The remainder of this procedure cleans |
| 1402 | ** up the shared-btree. |
| 1403 | */ |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 1404 | assert( p->wantToLock==0 && p->locked==0 ); |
| 1405 | if( !p->sharable || removeFromSharingList(pBt) ){ |
| 1406 | /* The pBt is no longer on the sharing list, so we can access |
| 1407 | ** it without having to hold the mutex. |
| 1408 | ** |
| 1409 | ** Clean out and delete the BtShared object. |
| 1410 | */ |
| 1411 | assert( !pBt->pCursor ); |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 1412 | sqlite3PagerClose(pBt->pPager); |
| 1413 | if( pBt->xFreeSchema && pBt->pSchema ){ |
| 1414 | pBt->xFreeSchema(pBt->pSchema); |
| 1415 | } |
| 1416 | sqlite3_free(pBt->pSchema); |
| 1417 | sqlite3_free(pBt); |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1418 | } |
| 1419 | |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 1420 | #ifndef SQLITE_OMIT_SHARED_CACHE |
drh | cab5ed7 | 2007-08-22 11:41:18 +0000 | [diff] [blame] | 1421 | assert( p->wantToLock==0 ); |
| 1422 | assert( p->locked==0 ); |
| 1423 | if( p->pPrev ) p->pPrev->pNext = p->pNext; |
| 1424 | if( p->pNext ) p->pNext->pPrev = p->pPrev; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1425 | #endif |
| 1426 | |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 1427 | sqlite3_free(p); |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 1428 | return SQLITE_OK; |
| 1429 | } |
| 1430 | |
| 1431 | /* |
drh | 90f5ecb | 2004-07-22 01:19:35 +0000 | [diff] [blame] | 1432 | ** Change the busy handler callback function. |
| 1433 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1434 | int sqlite3BtreeSetBusyHandler(Btree *p, BusyHandler *pHandler){ |
| 1435 | BtShared *pBt = p->pBt; |
drh | d0679ed | 2007-08-28 22:24:34 +0000 | [diff] [blame] | 1436 | assert( sqlite3_mutex_held(p->pSqlite->mutex) ); |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 1437 | sqlite3BtreeEnter(p); |
drh | b8ef32c | 2005-03-14 02:01:49 +0000 | [diff] [blame] | 1438 | pBt->pBusyHandler = pHandler; |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 1439 | sqlite3PagerSetBusyhandler(pBt->pPager, pHandler); |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 1440 | sqlite3BtreeLeave(p); |
drh | 90f5ecb | 2004-07-22 01:19:35 +0000 | [diff] [blame] | 1441 | return SQLITE_OK; |
| 1442 | } |
| 1443 | |
| 1444 | /* |
drh | da47d77 | 2002-12-02 04:25:19 +0000 | [diff] [blame] | 1445 | ** Change the limit on the number of pages allowed in the cache. |
drh | cd61c28 | 2002-03-06 22:01:34 +0000 | [diff] [blame] | 1446 | ** |
| 1447 | ** The maximum number of cache pages is set to the absolute |
| 1448 | ** value of mxPage. If mxPage is negative, the pager will |
| 1449 | ** operate asynchronously - it will not stop to do fsync()s |
| 1450 | ** to insure data is written to the disk surface before |
| 1451 | ** continuing. Transactions still work if synchronous is off, |
| 1452 | ** and the database cannot be corrupted if this program |
| 1453 | ** crashes. But if the operating system crashes or there is |
| 1454 | ** an abrupt power failure when synchronous is off, the database |
| 1455 | ** could be left in an inconsistent and unrecoverable state. |
| 1456 | ** Synchronous is on by default so database corruption is not |
| 1457 | ** normally a worry. |
drh | f57b14a | 2001-09-14 18:54:08 +0000 | [diff] [blame] | 1458 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1459 | int sqlite3BtreeSetCacheSize(Btree *p, int mxPage){ |
| 1460 | BtShared *pBt = p->pBt; |
drh | d0679ed | 2007-08-28 22:24:34 +0000 | [diff] [blame] | 1461 | assert( sqlite3_mutex_held(p->pSqlite->mutex) ); |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 1462 | sqlite3BtreeEnter(p); |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 1463 | sqlite3PagerSetCachesize(pBt->pPager, mxPage); |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 1464 | sqlite3BtreeLeave(p); |
drh | f57b14a | 2001-09-14 18:54:08 +0000 | [diff] [blame] | 1465 | return SQLITE_OK; |
| 1466 | } |
| 1467 | |
| 1468 | /* |
drh | 973b6e3 | 2003-02-12 14:09:42 +0000 | [diff] [blame] | 1469 | ** Change the way data is synced to disk in order to increase or decrease |
| 1470 | ** how well the database resists damage due to OS crashes and power |
| 1471 | ** failures. Level 1 is the same as asynchronous (no syncs() occur and |
| 1472 | ** there is a high probability of damage) Level 2 is the default. There |
| 1473 | ** is a very low but non-zero probability of damage. Level 3 reduces the |
| 1474 | ** probability of damage to near zero but with a write performance reduction. |
| 1475 | */ |
danielk1977 | 93758c8 | 2005-01-21 08:13:14 +0000 | [diff] [blame] | 1476 | #ifndef SQLITE_OMIT_PAGER_PRAGMAS |
drh | ac530b1 | 2006-02-11 01:25:50 +0000 | [diff] [blame] | 1477 | int sqlite3BtreeSetSafetyLevel(Btree *p, int level, int fullSync){ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1478 | BtShared *pBt = p->pBt; |
drh | d0679ed | 2007-08-28 22:24:34 +0000 | [diff] [blame] | 1479 | assert( sqlite3_mutex_held(p->pSqlite->mutex) ); |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 1480 | sqlite3BtreeEnter(p); |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 1481 | sqlite3PagerSetSafetyLevel(pBt->pPager, level, fullSync); |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 1482 | sqlite3BtreeLeave(p); |
drh | 973b6e3 | 2003-02-12 14:09:42 +0000 | [diff] [blame] | 1483 | return SQLITE_OK; |
| 1484 | } |
danielk1977 | 93758c8 | 2005-01-21 08:13:14 +0000 | [diff] [blame] | 1485 | #endif |
drh | 973b6e3 | 2003-02-12 14:09:42 +0000 | [diff] [blame] | 1486 | |
drh | 2c8997b | 2005-08-27 16:36:48 +0000 | [diff] [blame] | 1487 | /* |
| 1488 | ** Return TRUE if the given btree is set to safety level 1. In other |
| 1489 | ** words, return TRUE if no sync() occurs on the disk files. |
| 1490 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1491 | int sqlite3BtreeSyncDisabled(Btree *p){ |
| 1492 | BtShared *pBt = p->pBt; |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 1493 | int rc; |
drh | d0679ed | 2007-08-28 22:24:34 +0000 | [diff] [blame] | 1494 | assert( sqlite3_mutex_held(p->pSqlite->mutex) ); |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 1495 | sqlite3BtreeEnter(p); |
drh | d0679ed | 2007-08-28 22:24:34 +0000 | [diff] [blame] | 1496 | assert( pBt && pBt->pPager ); |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 1497 | rc = sqlite3PagerNosync(pBt->pPager); |
| 1498 | sqlite3BtreeLeave(p); |
| 1499 | return rc; |
drh | 2c8997b | 2005-08-27 16:36:48 +0000 | [diff] [blame] | 1500 | } |
| 1501 | |
danielk1977 | 576ec6b | 2005-01-21 11:55:25 +0000 | [diff] [blame] | 1502 | #if !defined(SQLITE_OMIT_PAGER_PRAGMAS) || !defined(SQLITE_OMIT_VACUUM) |
drh | 973b6e3 | 2003-02-12 14:09:42 +0000 | [diff] [blame] | 1503 | /* |
drh | 90f5ecb | 2004-07-22 01:19:35 +0000 | [diff] [blame] | 1504 | ** Change the default pages size and the number of reserved bytes per page. |
drh | 06f5021 | 2004-11-02 14:24:33 +0000 | [diff] [blame] | 1505 | ** |
| 1506 | ** The page size must be a power of 2 between 512 and 65536. If the page |
| 1507 | ** size supplied does not meet this constraint then the page size is not |
| 1508 | ** changed. |
| 1509 | ** |
| 1510 | ** Page sizes are constrained to be a power of two so that the region |
| 1511 | ** of the database file used for locking (beginning at PENDING_BYTE, |
| 1512 | ** the first byte past the 1GB boundary, 0x40000000) needs to occur |
| 1513 | ** at the beginning of a page. |
danielk1977 | 2812956 | 2005-01-11 10:25:06 +0000 | [diff] [blame] | 1514 | ** |
| 1515 | ** If parameter nReserve is less than zero, then the number of reserved |
| 1516 | ** bytes per page is left unchanged. |
drh | 90f5ecb | 2004-07-22 01:19:35 +0000 | [diff] [blame] | 1517 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1518 | int sqlite3BtreeSetPageSize(Btree *p, int pageSize, int nReserve){ |
danielk1977 | a1644fd | 2007-08-29 12:31:25 +0000 | [diff] [blame] | 1519 | int rc = SQLITE_OK; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1520 | BtShared *pBt = p->pBt; |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 1521 | sqlite3BtreeEnter(p); |
drh | 90f5ecb | 2004-07-22 01:19:35 +0000 | [diff] [blame] | 1522 | if( pBt->pageSizeFixed ){ |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 1523 | sqlite3BtreeLeave(p); |
drh | 90f5ecb | 2004-07-22 01:19:35 +0000 | [diff] [blame] | 1524 | return SQLITE_READONLY; |
| 1525 | } |
| 1526 | if( nReserve<0 ){ |
| 1527 | nReserve = pBt->pageSize - pBt->usableSize; |
| 1528 | } |
drh | 06f5021 | 2004-11-02 14:24:33 +0000 | [diff] [blame] | 1529 | if( pageSize>=512 && pageSize<=SQLITE_MAX_PAGE_SIZE && |
| 1530 | ((pageSize-1)&pageSize)==0 ){ |
drh | 07d183d | 2005-05-01 22:52:42 +0000 | [diff] [blame] | 1531 | assert( (pageSize & 7)==0 ); |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1532 | assert( !pBt->pPage1 && !pBt->pCursor ); |
danielk1977 | a1644fd | 2007-08-29 12:31:25 +0000 | [diff] [blame] | 1533 | pBt->pageSize = pageSize; |
| 1534 | rc = sqlite3PagerSetPagesize(pBt->pPager, &pBt->pageSize); |
drh | 90f5ecb | 2004-07-22 01:19:35 +0000 | [diff] [blame] | 1535 | } |
| 1536 | pBt->usableSize = pBt->pageSize - nReserve; |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 1537 | sqlite3BtreeLeave(p); |
danielk1977 | a1644fd | 2007-08-29 12:31:25 +0000 | [diff] [blame] | 1538 | return rc; |
drh | 90f5ecb | 2004-07-22 01:19:35 +0000 | [diff] [blame] | 1539 | } |
| 1540 | |
| 1541 | /* |
| 1542 | ** Return the currently defined page size |
| 1543 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1544 | int sqlite3BtreeGetPageSize(Btree *p){ |
| 1545 | return p->pBt->pageSize; |
drh | 90f5ecb | 2004-07-22 01:19:35 +0000 | [diff] [blame] | 1546 | } |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1547 | int sqlite3BtreeGetReserve(Btree *p){ |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 1548 | int n; |
| 1549 | sqlite3BtreeEnter(p); |
| 1550 | n = p->pBt->pageSize - p->pBt->usableSize; |
| 1551 | sqlite3BtreeLeave(p); |
| 1552 | return n; |
drh | 2011d5f | 2004-07-22 02:40:37 +0000 | [diff] [blame] | 1553 | } |
drh | f8e632b | 2007-05-08 14:51:36 +0000 | [diff] [blame] | 1554 | |
| 1555 | /* |
| 1556 | ** Set the maximum page count for a database if mxPage is positive. |
| 1557 | ** No changes are made if mxPage is 0 or negative. |
| 1558 | ** Regardless of the value of mxPage, return the maximum page count. |
| 1559 | */ |
| 1560 | int sqlite3BtreeMaxPageCount(Btree *p, int mxPage){ |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 1561 | int n; |
| 1562 | sqlite3BtreeEnter(p); |
| 1563 | n = sqlite3PagerMaxPageCount(p->pBt->pPager, mxPage); |
| 1564 | sqlite3BtreeLeave(p); |
| 1565 | return n; |
drh | f8e632b | 2007-05-08 14:51:36 +0000 | [diff] [blame] | 1566 | } |
danielk1977 | 576ec6b | 2005-01-21 11:55:25 +0000 | [diff] [blame] | 1567 | #endif /* !defined(SQLITE_OMIT_PAGER_PRAGMAS) || !defined(SQLITE_OMIT_VACUUM) */ |
drh | 90f5ecb | 2004-07-22 01:19:35 +0000 | [diff] [blame] | 1568 | |
| 1569 | /* |
danielk1977 | 951af80 | 2004-11-05 15:45:09 +0000 | [diff] [blame] | 1570 | ** Change the 'auto-vacuum' property of the database. If the 'autoVacuum' |
| 1571 | ** parameter is non-zero, then auto-vacuum mode is enabled. If zero, it |
| 1572 | ** is disabled. The default value for the auto-vacuum property is |
| 1573 | ** determined by the SQLITE_DEFAULT_AUTOVACUUM macro. |
| 1574 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1575 | int sqlite3BtreeSetAutoVacuum(Btree *p, int autoVacuum){ |
danielk1977 | 951af80 | 2004-11-05 15:45:09 +0000 | [diff] [blame] | 1576 | #ifdef SQLITE_OMIT_AUTOVACUUM |
drh | eee46cf | 2004-11-06 00:02:48 +0000 | [diff] [blame] | 1577 | return SQLITE_READONLY; |
danielk1977 | 951af80 | 2004-11-05 15:45:09 +0000 | [diff] [blame] | 1578 | #else |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 1579 | BtShared *pBt = p->pBt; |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 1580 | int rc = SQLITE_OK; |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 1581 | int av = (autoVacuum?1:0); |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 1582 | |
| 1583 | sqlite3BtreeEnter(p); |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 1584 | if( pBt->pageSizeFixed && av!=pBt->autoVacuum ){ |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 1585 | rc = SQLITE_READONLY; |
| 1586 | }else{ |
| 1587 | pBt->autoVacuum = av; |
danielk1977 | 951af80 | 2004-11-05 15:45:09 +0000 | [diff] [blame] | 1588 | } |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 1589 | sqlite3BtreeLeave(p); |
| 1590 | return rc; |
danielk1977 | 951af80 | 2004-11-05 15:45:09 +0000 | [diff] [blame] | 1591 | #endif |
| 1592 | } |
| 1593 | |
| 1594 | /* |
| 1595 | ** Return the value of the 'auto-vacuum' property. If auto-vacuum is |
| 1596 | ** enabled 1 is returned. Otherwise 0. |
| 1597 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1598 | int sqlite3BtreeGetAutoVacuum(Btree *p){ |
danielk1977 | 951af80 | 2004-11-05 15:45:09 +0000 | [diff] [blame] | 1599 | #ifdef SQLITE_OMIT_AUTOVACUUM |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 1600 | return BTREE_AUTOVACUUM_NONE; |
danielk1977 | 951af80 | 2004-11-05 15:45:09 +0000 | [diff] [blame] | 1601 | #else |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 1602 | int rc; |
| 1603 | sqlite3BtreeEnter(p); |
| 1604 | rc = ( |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 1605 | (!p->pBt->autoVacuum)?BTREE_AUTOVACUUM_NONE: |
| 1606 | (!p->pBt->incrVacuum)?BTREE_AUTOVACUUM_FULL: |
| 1607 | BTREE_AUTOVACUUM_INCR |
| 1608 | ); |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 1609 | sqlite3BtreeLeave(p); |
| 1610 | return rc; |
danielk1977 | 951af80 | 2004-11-05 15:45:09 +0000 | [diff] [blame] | 1611 | #endif |
| 1612 | } |
| 1613 | |
| 1614 | |
| 1615 | /* |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 1616 | ** Get a reference to pPage1 of the database file. This will |
drh | 306dc21 | 2001-05-21 13:45:10 +0000 | [diff] [blame] | 1617 | ** also acquire a readlock on that file. |
| 1618 | ** |
| 1619 | ** SQLITE_OK is returned on success. If the file is not a |
| 1620 | ** well-formed database file, then SQLITE_CORRUPT is returned. |
| 1621 | ** SQLITE_BUSY is returned if the database is locked. SQLITE_NOMEM |
drh | 4f0ee68 | 2007-03-30 20:43:40 +0000 | [diff] [blame] | 1622 | ** is returned if we run out of memory. |
drh | 306dc21 | 2001-05-21 13:45:10 +0000 | [diff] [blame] | 1623 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1624 | static int lockBtree(BtShared *pBt){ |
drh | 07d183d | 2005-05-01 22:52:42 +0000 | [diff] [blame] | 1625 | int rc, pageSize; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1626 | MemPage *pPage1; |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 1627 | |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 1628 | assert( sqlite3_mutex_held(pBt->mutex) ); |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 1629 | if( pBt->pPage1 ) return SQLITE_OK; |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 1630 | rc = sqlite3BtreeGetPage(pBt, 1, &pPage1, 0); |
drh | 306dc21 | 2001-05-21 13:45:10 +0000 | [diff] [blame] | 1631 | if( rc!=SQLITE_OK ) return rc; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1632 | |
drh | 306dc21 | 2001-05-21 13:45:10 +0000 | [diff] [blame] | 1633 | |
| 1634 | /* Do some checking to help insure the file we opened really is |
| 1635 | ** a valid database file. |
| 1636 | */ |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 1637 | rc = SQLITE_NOTADB; |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 1638 | if( sqlite3PagerPagecount(pBt->pPager)>0 ){ |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 1639 | u8 *page1 = pPage1->aData; |
| 1640 | if( memcmp(page1, zMagicHeader, 16)!=0 ){ |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 1641 | goto page1_init_failed; |
drh | 306dc21 | 2001-05-21 13:45:10 +0000 | [diff] [blame] | 1642 | } |
drh | 309169a | 2007-04-24 17:27:51 +0000 | [diff] [blame] | 1643 | if( page1[18]>1 ){ |
| 1644 | pBt->readOnly = 1; |
| 1645 | } |
| 1646 | if( page1[19]>1 ){ |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 1647 | goto page1_init_failed; |
| 1648 | } |
drh | 07d183d | 2005-05-01 22:52:42 +0000 | [diff] [blame] | 1649 | pageSize = get2byte(&page1[16]); |
drh | 7dc385e | 2007-09-06 23:39:36 +0000 | [diff] [blame] | 1650 | if( ((pageSize-1)&pageSize)!=0 || pageSize<512 || |
| 1651 | (SQLITE_MAX_PAGE_SIZE<32768 && pageSize>SQLITE_MAX_PAGE_SIZE) |
| 1652 | ){ |
drh | 07d183d | 2005-05-01 22:52:42 +0000 | [diff] [blame] | 1653 | goto page1_init_failed; |
| 1654 | } |
| 1655 | assert( (pageSize & 7)==0 ); |
| 1656 | pBt->pageSize = pageSize; |
| 1657 | pBt->usableSize = pageSize - page1[20]; |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 1658 | if( pBt->usableSize<500 ){ |
| 1659 | goto page1_init_failed; |
| 1660 | } |
| 1661 | pBt->maxEmbedFrac = page1[21]; |
| 1662 | pBt->minEmbedFrac = page1[22]; |
| 1663 | pBt->minLeafFrac = page1[23]; |
drh | 057cd3a | 2005-02-15 16:23:02 +0000 | [diff] [blame] | 1664 | #ifndef SQLITE_OMIT_AUTOVACUUM |
| 1665 | pBt->autoVacuum = (get4byte(&page1[36 + 4*4])?1:0); |
danielk1977 | 27b1f95 | 2007-06-25 08:16:58 +0000 | [diff] [blame] | 1666 | pBt->incrVacuum = (get4byte(&page1[36 + 7*4])?1:0); |
drh | 057cd3a | 2005-02-15 16:23:02 +0000 | [diff] [blame] | 1667 | #endif |
drh | 306dc21 | 2001-05-21 13:45:10 +0000 | [diff] [blame] | 1668 | } |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 1669 | |
| 1670 | /* maxLocal is the maximum amount of payload to store locally for |
| 1671 | ** a cell. Make sure it is small enough so that at least minFanout |
| 1672 | ** cells can will fit on one page. We assume a 10-byte page header. |
| 1673 | ** Besides the payload, the cell must store: |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 1674 | ** 2-byte pointer to the cell |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 1675 | ** 4-byte child pointer |
| 1676 | ** 9-byte nKey value |
| 1677 | ** 4-byte nData value |
| 1678 | ** 4-byte overflow page pointer |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 1679 | ** So a cell consists of a 2-byte poiner, a header which is as much as |
| 1680 | ** 17 bytes long, 0 to N bytes of payload, and an optional 4 byte overflow |
| 1681 | ** page pointer. |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 1682 | */ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 1683 | pBt->maxLocal = (pBt->usableSize-12)*pBt->maxEmbedFrac/255 - 23; |
| 1684 | pBt->minLocal = (pBt->usableSize-12)*pBt->minEmbedFrac/255 - 23; |
| 1685 | pBt->maxLeaf = pBt->usableSize - 35; |
| 1686 | pBt->minLeaf = (pBt->usableSize-12)*pBt->minLeafFrac/255 - 23; |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 1687 | if( pBt->minLocal>pBt->maxLocal || pBt->maxLocal<0 ){ |
| 1688 | goto page1_init_failed; |
| 1689 | } |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 1690 | assert( pBt->maxLeaf + 23 <= MX_CELL_SIZE(pBt) ); |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1691 | pBt->pPage1 = pPage1; |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 1692 | return SQLITE_OK; |
drh | 306dc21 | 2001-05-21 13:45:10 +0000 | [diff] [blame] | 1693 | |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 1694 | page1_init_failed: |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1695 | releasePage(pPage1); |
| 1696 | pBt->pPage1 = 0; |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 1697 | return rc; |
drh | 306dc21 | 2001-05-21 13:45:10 +0000 | [diff] [blame] | 1698 | } |
| 1699 | |
| 1700 | /* |
drh | b8ef32c | 2005-03-14 02:01:49 +0000 | [diff] [blame] | 1701 | ** This routine works like lockBtree() except that it also invokes the |
| 1702 | ** busy callback if there is lock contention. |
| 1703 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1704 | static int lockBtreeWithRetry(Btree *pRef){ |
drh | b8ef32c | 2005-03-14 02:01:49 +0000 | [diff] [blame] | 1705 | int rc = SQLITE_OK; |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 1706 | |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 1707 | assert( sqlite3BtreeHoldsMutex(pRef) ); |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1708 | if( pRef->inTrans==TRANS_NONE ){ |
| 1709 | u8 inTransaction = pRef->pBt->inTransaction; |
| 1710 | btreeIntegrity(pRef); |
| 1711 | rc = sqlite3BtreeBeginTrans(pRef, 0); |
| 1712 | pRef->pBt->inTransaction = inTransaction; |
| 1713 | pRef->inTrans = TRANS_NONE; |
| 1714 | if( rc==SQLITE_OK ){ |
| 1715 | pRef->pBt->nTransaction--; |
| 1716 | } |
| 1717 | btreeIntegrity(pRef); |
drh | b8ef32c | 2005-03-14 02:01:49 +0000 | [diff] [blame] | 1718 | } |
| 1719 | return rc; |
| 1720 | } |
| 1721 | |
| 1722 | |
| 1723 | /* |
drh | b8ca307 | 2001-12-05 00:21:20 +0000 | [diff] [blame] | 1724 | ** If there are no outstanding cursors and we are not in the middle |
| 1725 | ** of a transaction but there is a read lock on the database, then |
| 1726 | ** this routine unrefs the first page of the database file which |
| 1727 | ** has the effect of releasing the read lock. |
| 1728 | ** |
| 1729 | ** If there are any outstanding cursors, this routine is a no-op. |
| 1730 | ** |
| 1731 | ** If there is a transaction in progress, this routine is a no-op. |
| 1732 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1733 | static void unlockBtreeIfUnused(BtShared *pBt){ |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 1734 | assert( sqlite3_mutex_held(pBt->mutex) ); |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1735 | if( pBt->inTransaction==TRANS_NONE && pBt->pCursor==0 && pBt->pPage1!=0 ){ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 1736 | if( sqlite3PagerRefcount(pBt->pPager)>=1 ){ |
drh | 24c9a2e | 2007-01-05 02:00:47 +0000 | [diff] [blame] | 1737 | if( pBt->pPage1->aData==0 ){ |
| 1738 | MemPage *pPage = pBt->pPage1; |
drh | bf4bca5 | 2007-09-06 22:19:14 +0000 | [diff] [blame] | 1739 | pPage->aData = sqlite3PagerGetData(pPage->pDbPage); |
drh | 24c9a2e | 2007-01-05 02:00:47 +0000 | [diff] [blame] | 1740 | pPage->pBt = pBt; |
| 1741 | pPage->pgno = 1; |
| 1742 | } |
| 1743 | releasePage(pBt->pPage1); |
drh | 51c6d96 | 2004-06-06 00:42:25 +0000 | [diff] [blame] | 1744 | } |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1745 | pBt->pPage1 = 0; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1746 | pBt->inStmt = 0; |
drh | b8ca307 | 2001-12-05 00:21:20 +0000 | [diff] [blame] | 1747 | } |
| 1748 | } |
| 1749 | |
| 1750 | /* |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 1751 | ** Create a new database by initializing the first page of the |
drh | 8c42ca9 | 2001-06-22 19:15:00 +0000 | [diff] [blame] | 1752 | ** file. |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 1753 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1754 | static int newDatabase(BtShared *pBt){ |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 1755 | MemPage *pP1; |
| 1756 | unsigned char *data; |
drh | 8c42ca9 | 2001-06-22 19:15:00 +0000 | [diff] [blame] | 1757 | int rc; |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 1758 | |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 1759 | assert( sqlite3_mutex_held(pBt->mutex) ); |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 1760 | if( sqlite3PagerPagecount(pBt->pPager)>0 ) return SQLITE_OK; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1761 | pP1 = pBt->pPage1; |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 1762 | assert( pP1!=0 ); |
| 1763 | data = pP1->aData; |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 1764 | rc = sqlite3PagerWrite(pP1->pDbPage); |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 1765 | if( rc ) return rc; |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 1766 | memcpy(data, zMagicHeader, sizeof(zMagicHeader)); |
| 1767 | assert( sizeof(zMagicHeader)==16 ); |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 1768 | put2byte(&data[16], pBt->pageSize); |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 1769 | data[18] = 1; |
| 1770 | data[19] = 1; |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 1771 | data[20] = pBt->pageSize - pBt->usableSize; |
| 1772 | data[21] = pBt->maxEmbedFrac; |
| 1773 | data[22] = pBt->minEmbedFrac; |
| 1774 | data[23] = pBt->minLeafFrac; |
| 1775 | memset(&data[24], 0, 100-24); |
drh | e6c4381 | 2004-05-14 12:17:46 +0000 | [diff] [blame] | 1776 | zeroPage(pP1, PTF_INTKEY|PTF_LEAF|PTF_LEAFDATA ); |
drh | f2a611c | 2004-09-05 00:33:43 +0000 | [diff] [blame] | 1777 | pBt->pageSizeFixed = 1; |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 1778 | #ifndef SQLITE_OMIT_AUTOVACUUM |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 1779 | assert( pBt->autoVacuum==1 || pBt->autoVacuum==0 ); |
danielk1977 | 418899a | 2007-06-24 10:14:00 +0000 | [diff] [blame] | 1780 | assert( pBt->incrVacuum==1 || pBt->incrVacuum==0 ); |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 1781 | put4byte(&data[36 + 4*4], pBt->autoVacuum); |
danielk1977 | 418899a | 2007-06-24 10:14:00 +0000 | [diff] [blame] | 1782 | put4byte(&data[36 + 7*4], pBt->incrVacuum); |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 1783 | #endif |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 1784 | return SQLITE_OK; |
| 1785 | } |
| 1786 | |
| 1787 | /* |
danielk1977 | ee5741e | 2004-05-31 10:01:34 +0000 | [diff] [blame] | 1788 | ** Attempt to start a new transaction. A write-transaction |
drh | 684917c | 2004-10-05 02:41:42 +0000 | [diff] [blame] | 1789 | ** is started if the second argument is nonzero, otherwise a read- |
| 1790 | ** transaction. If the second argument is 2 or more and exclusive |
| 1791 | ** transaction is started, meaning that no other process is allowed |
| 1792 | ** to access the database. A preexisting transaction may not be |
drh | b8ef32c | 2005-03-14 02:01:49 +0000 | [diff] [blame] | 1793 | ** upgraded to exclusive by calling this routine a second time - the |
drh | 684917c | 2004-10-05 02:41:42 +0000 | [diff] [blame] | 1794 | ** exclusivity flag only works for a new transaction. |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 1795 | ** |
danielk1977 | ee5741e | 2004-05-31 10:01:34 +0000 | [diff] [blame] | 1796 | ** A write-transaction must be started before attempting any |
| 1797 | ** changes to the database. None of the following routines |
| 1798 | ** will work unless a transaction is started first: |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 1799 | ** |
drh | 23e11ca | 2004-05-04 17:27:28 +0000 | [diff] [blame] | 1800 | ** sqlite3BtreeCreateTable() |
| 1801 | ** sqlite3BtreeCreateIndex() |
| 1802 | ** sqlite3BtreeClearTable() |
| 1803 | ** sqlite3BtreeDropTable() |
| 1804 | ** sqlite3BtreeInsert() |
| 1805 | ** sqlite3BtreeDelete() |
| 1806 | ** sqlite3BtreeUpdateMeta() |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 1807 | ** |
drh | b8ef32c | 2005-03-14 02:01:49 +0000 | [diff] [blame] | 1808 | ** If an initial attempt to acquire the lock fails because of lock contention |
| 1809 | ** and the database was previously unlocked, then invoke the busy handler |
| 1810 | ** if there is one. But if there was previously a read-lock, do not |
| 1811 | ** invoke the busy handler - just return SQLITE_BUSY. SQLITE_BUSY is |
| 1812 | ** returned when there is already a read-lock in order to avoid a deadlock. |
| 1813 | ** |
| 1814 | ** Suppose there are two processes A and B. A has a read lock and B has |
| 1815 | ** a reserved lock. B tries to promote to exclusive but is blocked because |
| 1816 | ** of A's read lock. A tries to promote to reserved but is blocked by B. |
| 1817 | ** One or the other of the two processes must give way or there can be |
| 1818 | ** no progress. By returning SQLITE_BUSY and not invoking the busy callback |
| 1819 | ** when A already has a read lock, we encourage A to give up and let B |
| 1820 | ** proceed. |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 1821 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1822 | int sqlite3BtreeBeginTrans(Btree *p, int wrflag){ |
| 1823 | BtShared *pBt = p->pBt; |
danielk1977 | ee5741e | 2004-05-31 10:01:34 +0000 | [diff] [blame] | 1824 | int rc = SQLITE_OK; |
| 1825 | |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 1826 | sqlite3BtreeEnter(p); |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1827 | btreeIntegrity(p); |
| 1828 | |
danielk1977 | ee5741e | 2004-05-31 10:01:34 +0000 | [diff] [blame] | 1829 | /* If the btree is already in a write-transaction, or it |
| 1830 | ** is already in a read-transaction and a read-transaction |
| 1831 | ** is requested, this is a no-op. |
| 1832 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1833 | if( p->inTrans==TRANS_WRITE || (p->inTrans==TRANS_READ && !wrflag) ){ |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 1834 | goto trans_begun; |
danielk1977 | ee5741e | 2004-05-31 10:01:34 +0000 | [diff] [blame] | 1835 | } |
drh | b8ef32c | 2005-03-14 02:01:49 +0000 | [diff] [blame] | 1836 | |
| 1837 | /* Write transactions are not possible on a read-only database */ |
danielk1977 | ee5741e | 2004-05-31 10:01:34 +0000 | [diff] [blame] | 1838 | if( pBt->readOnly && wrflag ){ |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 1839 | rc = SQLITE_READONLY; |
| 1840 | goto trans_begun; |
danielk1977 | ee5741e | 2004-05-31 10:01:34 +0000 | [diff] [blame] | 1841 | } |
| 1842 | |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1843 | /* If another database handle has already opened a write transaction |
| 1844 | ** on this shared-btree structure and a second write transaction is |
| 1845 | ** requested, return SQLITE_BUSY. |
| 1846 | */ |
| 1847 | if( pBt->inTransaction==TRANS_WRITE && wrflag ){ |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 1848 | rc = SQLITE_BUSY; |
| 1849 | goto trans_begun; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1850 | } |
| 1851 | |
drh | b8ef32c | 2005-03-14 02:01:49 +0000 | [diff] [blame] | 1852 | do { |
| 1853 | if( pBt->pPage1==0 ){ |
| 1854 | rc = lockBtree(pBt); |
drh | 8c42ca9 | 2001-06-22 19:15:00 +0000 | [diff] [blame] | 1855 | } |
drh | 309169a | 2007-04-24 17:27:51 +0000 | [diff] [blame] | 1856 | |
drh | b8ef32c | 2005-03-14 02:01:49 +0000 | [diff] [blame] | 1857 | if( rc==SQLITE_OK && wrflag ){ |
drh | 309169a | 2007-04-24 17:27:51 +0000 | [diff] [blame] | 1858 | if( pBt->readOnly ){ |
| 1859 | rc = SQLITE_READONLY; |
| 1860 | }else{ |
| 1861 | rc = sqlite3PagerBegin(pBt->pPage1->pDbPage, wrflag>1); |
| 1862 | if( rc==SQLITE_OK ){ |
| 1863 | rc = newDatabase(pBt); |
| 1864 | } |
drh | b8ef32c | 2005-03-14 02:01:49 +0000 | [diff] [blame] | 1865 | } |
| 1866 | } |
| 1867 | |
| 1868 | if( rc==SQLITE_OK ){ |
drh | b8ef32c | 2005-03-14 02:01:49 +0000 | [diff] [blame] | 1869 | if( wrflag ) pBt->inStmt = 0; |
| 1870 | }else{ |
| 1871 | unlockBtreeIfUnused(pBt); |
| 1872 | } |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1873 | }while( rc==SQLITE_BUSY && pBt->inTransaction==TRANS_NONE && |
drh | a4afb65 | 2005-07-09 02:16:02 +0000 | [diff] [blame] | 1874 | sqlite3InvokeBusyHandler(pBt->pBusyHandler) ); |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1875 | |
| 1876 | if( rc==SQLITE_OK ){ |
| 1877 | if( p->inTrans==TRANS_NONE ){ |
| 1878 | pBt->nTransaction++; |
| 1879 | } |
| 1880 | p->inTrans = (wrflag?TRANS_WRITE:TRANS_READ); |
| 1881 | if( p->inTrans>pBt->inTransaction ){ |
| 1882 | pBt->inTransaction = p->inTrans; |
| 1883 | } |
| 1884 | } |
| 1885 | |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 1886 | |
| 1887 | trans_begun: |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1888 | btreeIntegrity(p); |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 1889 | sqlite3BtreeLeave(p); |
drh | b8ca307 | 2001-12-05 00:21:20 +0000 | [diff] [blame] | 1890 | return rc; |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 1891 | } |
| 1892 | |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 1893 | #ifndef SQLITE_OMIT_AUTOVACUUM |
| 1894 | |
| 1895 | /* |
| 1896 | ** Set the pointer-map entries for all children of page pPage. Also, if |
| 1897 | ** pPage contains cells that point to overflow pages, set the pointer |
| 1898 | ** map entries for the overflow pages as well. |
| 1899 | */ |
| 1900 | static int setChildPtrmaps(MemPage *pPage){ |
| 1901 | int i; /* Counter variable */ |
| 1902 | int nCell; /* Number of cells in page pPage */ |
danielk1977 | 2df71c7 | 2007-05-24 07:22:42 +0000 | [diff] [blame] | 1903 | int rc; /* Return code */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1904 | BtShared *pBt = pPage->pBt; |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 1905 | int isInitOrig = pPage->isInit; |
| 1906 | Pgno pgno = pPage->pgno; |
| 1907 | |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 1908 | assert( sqlite3_mutex_held(pPage->pBt->mutex) ); |
danielk1977 | 2df71c7 | 2007-05-24 07:22:42 +0000 | [diff] [blame] | 1909 | rc = sqlite3BtreeInitPage(pPage, pPage->pParent); |
| 1910 | if( rc!=SQLITE_OK ){ |
| 1911 | goto set_child_ptrmaps_out; |
| 1912 | } |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 1913 | nCell = pPage->nCell; |
| 1914 | |
| 1915 | for(i=0; i<nCell; i++){ |
danielk1977 | 1cc5ed8 | 2007-05-16 17:28:43 +0000 | [diff] [blame] | 1916 | u8 *pCell = findCell(pPage, i); |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 1917 | |
danielk1977 | 2683665 | 2005-01-17 01:33:13 +0000 | [diff] [blame] | 1918 | rc = ptrmapPutOvflPtr(pPage, pCell); |
| 1919 | if( rc!=SQLITE_OK ){ |
| 1920 | goto set_child_ptrmaps_out; |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 1921 | } |
danielk1977 | 2683665 | 2005-01-17 01:33:13 +0000 | [diff] [blame] | 1922 | |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 1923 | if( !pPage->leaf ){ |
| 1924 | Pgno childPgno = get4byte(pCell); |
| 1925 | rc = ptrmapPut(pBt, childPgno, PTRMAP_BTREE, pgno); |
| 1926 | if( rc!=SQLITE_OK ) goto set_child_ptrmaps_out; |
| 1927 | } |
| 1928 | } |
| 1929 | |
| 1930 | if( !pPage->leaf ){ |
| 1931 | Pgno childPgno = get4byte(&pPage->aData[pPage->hdrOffset+8]); |
| 1932 | rc = ptrmapPut(pBt, childPgno, PTRMAP_BTREE, pgno); |
| 1933 | } |
| 1934 | |
| 1935 | set_child_ptrmaps_out: |
| 1936 | pPage->isInit = isInitOrig; |
| 1937 | return rc; |
| 1938 | } |
| 1939 | |
| 1940 | /* |
| 1941 | ** Somewhere on pPage, which is guarenteed to be a btree page, not an overflow |
| 1942 | ** page, is a pointer to page iFrom. Modify this pointer so that it points to |
| 1943 | ** iTo. Parameter eType describes the type of pointer to be modified, as |
| 1944 | ** follows: |
| 1945 | ** |
| 1946 | ** PTRMAP_BTREE: pPage is a btree-page. The pointer points at a child |
| 1947 | ** page of pPage. |
| 1948 | ** |
| 1949 | ** PTRMAP_OVERFLOW1: pPage is a btree-page. The pointer points at an overflow |
| 1950 | ** page pointed to by one of the cells on pPage. |
| 1951 | ** |
| 1952 | ** PTRMAP_OVERFLOW2: pPage is an overflow-page. The pointer points at the next |
| 1953 | ** overflow page in the list. |
| 1954 | */ |
danielk1977 | fdb7cdb | 2005-01-17 02:12:18 +0000 | [diff] [blame] | 1955 | static int modifyPagePointer(MemPage *pPage, Pgno iFrom, Pgno iTo, u8 eType){ |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 1956 | assert( sqlite3_mutex_held(pPage->pBt->mutex) ); |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 1957 | if( eType==PTRMAP_OVERFLOW2 ){ |
danielk1977 | f78fc08 | 2004-11-02 14:40:32 +0000 | [diff] [blame] | 1958 | /* The pointer is always the first 4 bytes of the page in this case. */ |
danielk1977 | fdb7cdb | 2005-01-17 02:12:18 +0000 | [diff] [blame] | 1959 | if( get4byte(pPage->aData)!=iFrom ){ |
drh | 4928570 | 2005-09-17 15:20:26 +0000 | [diff] [blame] | 1960 | return SQLITE_CORRUPT_BKPT; |
danielk1977 | fdb7cdb | 2005-01-17 02:12:18 +0000 | [diff] [blame] | 1961 | } |
danielk1977 | f78fc08 | 2004-11-02 14:40:32 +0000 | [diff] [blame] | 1962 | put4byte(pPage->aData, iTo); |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 1963 | }else{ |
| 1964 | int isInitOrig = pPage->isInit; |
| 1965 | int i; |
| 1966 | int nCell; |
| 1967 | |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 1968 | sqlite3BtreeInitPage(pPage, 0); |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 1969 | nCell = pPage->nCell; |
| 1970 | |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 1971 | for(i=0; i<nCell; i++){ |
danielk1977 | 1cc5ed8 | 2007-05-16 17:28:43 +0000 | [diff] [blame] | 1972 | u8 *pCell = findCell(pPage, i); |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 1973 | if( eType==PTRMAP_OVERFLOW1 ){ |
| 1974 | CellInfo info; |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 1975 | sqlite3BtreeParseCellPtr(pPage, pCell, &info); |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 1976 | if( info.iOverflow ){ |
| 1977 | if( iFrom==get4byte(&pCell[info.iOverflow]) ){ |
| 1978 | put4byte(&pCell[info.iOverflow], iTo); |
| 1979 | break; |
| 1980 | } |
| 1981 | } |
| 1982 | }else{ |
| 1983 | if( get4byte(pCell)==iFrom ){ |
| 1984 | put4byte(pCell, iTo); |
| 1985 | break; |
| 1986 | } |
| 1987 | } |
| 1988 | } |
| 1989 | |
| 1990 | if( i==nCell ){ |
danielk1977 | fdb7cdb | 2005-01-17 02:12:18 +0000 | [diff] [blame] | 1991 | if( eType!=PTRMAP_BTREE || |
| 1992 | get4byte(&pPage->aData[pPage->hdrOffset+8])!=iFrom ){ |
drh | 4928570 | 2005-09-17 15:20:26 +0000 | [diff] [blame] | 1993 | return SQLITE_CORRUPT_BKPT; |
danielk1977 | fdb7cdb | 2005-01-17 02:12:18 +0000 | [diff] [blame] | 1994 | } |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 1995 | put4byte(&pPage->aData[pPage->hdrOffset+8], iTo); |
| 1996 | } |
| 1997 | |
| 1998 | pPage->isInit = isInitOrig; |
| 1999 | } |
danielk1977 | fdb7cdb | 2005-01-17 02:12:18 +0000 | [diff] [blame] | 2000 | return SQLITE_OK; |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 2001 | } |
| 2002 | |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 2003 | |
danielk1977 | 7701e81 | 2005-01-10 12:59:51 +0000 | [diff] [blame] | 2004 | /* |
| 2005 | ** Move the open database page pDbPage to location iFreePage in the |
| 2006 | ** database. The pDbPage reference remains valid. |
| 2007 | */ |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 2008 | static int relocatePage( |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2009 | BtShared *pBt, /* Btree */ |
danielk1977 | 7701e81 | 2005-01-10 12:59:51 +0000 | [diff] [blame] | 2010 | MemPage *pDbPage, /* Open page to move */ |
| 2011 | u8 eType, /* Pointer map 'type' entry for pDbPage */ |
| 2012 | Pgno iPtrPage, /* Pointer map 'page-no' entry for pDbPage */ |
| 2013 | Pgno iFreePage /* The location to move pDbPage to */ |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 2014 | ){ |
| 2015 | MemPage *pPtrPage; /* The page that contains a pointer to pDbPage */ |
| 2016 | Pgno iDbPage = pDbPage->pgno; |
| 2017 | Pager *pPager = pBt->pPager; |
| 2018 | int rc; |
| 2019 | |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 2020 | assert( eType==PTRMAP_OVERFLOW2 || eType==PTRMAP_OVERFLOW1 || |
| 2021 | eType==PTRMAP_BTREE || eType==PTRMAP_ROOTPAGE ); |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 2022 | assert( sqlite3_mutex_held(pBt->mutex) ); |
drh | d0679ed | 2007-08-28 22:24:34 +0000 | [diff] [blame] | 2023 | assert( pDbPage->pBt==pBt ); |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 2024 | |
| 2025 | /* Move page iDbPage from it's current location to page number iFreePage */ |
| 2026 | TRACE(("AUTOVACUUM: Moving %d to free page %d (ptr page %d type %d)\n", |
| 2027 | iDbPage, iFreePage, iPtrPage, eType)); |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 2028 | rc = sqlite3PagerMovepage(pPager, pDbPage->pDbPage, iFreePage); |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 2029 | if( rc!=SQLITE_OK ){ |
| 2030 | return rc; |
| 2031 | } |
| 2032 | pDbPage->pgno = iFreePage; |
| 2033 | |
| 2034 | /* If pDbPage was a btree-page, then it may have child pages and/or cells |
| 2035 | ** that point to overflow pages. The pointer map entries for all these |
| 2036 | ** pages need to be changed. |
| 2037 | ** |
| 2038 | ** If pDbPage is an overflow page, then the first 4 bytes may store a |
| 2039 | ** pointer to a subsequent overflow page. If this is the case, then |
| 2040 | ** the pointer map needs to be updated for the subsequent overflow page. |
| 2041 | */ |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 2042 | if( eType==PTRMAP_BTREE || eType==PTRMAP_ROOTPAGE ){ |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 2043 | rc = setChildPtrmaps(pDbPage); |
| 2044 | if( rc!=SQLITE_OK ){ |
| 2045 | return rc; |
| 2046 | } |
| 2047 | }else{ |
| 2048 | Pgno nextOvfl = get4byte(pDbPage->aData); |
| 2049 | if( nextOvfl!=0 ){ |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 2050 | rc = ptrmapPut(pBt, nextOvfl, PTRMAP_OVERFLOW2, iFreePage); |
| 2051 | if( rc!=SQLITE_OK ){ |
| 2052 | return rc; |
| 2053 | } |
| 2054 | } |
| 2055 | } |
| 2056 | |
| 2057 | /* Fix the database pointer on page iPtrPage that pointed at iDbPage so |
| 2058 | ** that it points at iFreePage. Also fix the pointer map entry for |
| 2059 | ** iPtrPage. |
| 2060 | */ |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 2061 | if( eType!=PTRMAP_ROOTPAGE ){ |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 2062 | rc = sqlite3BtreeGetPage(pBt, iPtrPage, &pPtrPage, 0); |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 2063 | if( rc!=SQLITE_OK ){ |
| 2064 | return rc; |
| 2065 | } |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 2066 | rc = sqlite3PagerWrite(pPtrPage->pDbPage); |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 2067 | if( rc!=SQLITE_OK ){ |
| 2068 | releasePage(pPtrPage); |
| 2069 | return rc; |
| 2070 | } |
danielk1977 | fdb7cdb | 2005-01-17 02:12:18 +0000 | [diff] [blame] | 2071 | rc = modifyPagePointer(pPtrPage, iDbPage, iFreePage, eType); |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 2072 | releasePage(pPtrPage); |
danielk1977 | fdb7cdb | 2005-01-17 02:12:18 +0000 | [diff] [blame] | 2073 | if( rc==SQLITE_OK ){ |
| 2074 | rc = ptrmapPut(pBt, iFreePage, eType, iPtrPage); |
| 2075 | } |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 2076 | } |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 2077 | return rc; |
| 2078 | } |
| 2079 | |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 2080 | /* Forward declaration required by incrVacuumStep(). */ |
drh | 4f0c587 | 2007-03-26 22:05:01 +0000 | [diff] [blame] | 2081 | static int allocateBtreePage(BtShared *, MemPage **, Pgno *, Pgno, u8); |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 2082 | |
| 2083 | /* |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 2084 | ** Perform a single step of an incremental-vacuum. If successful, |
| 2085 | ** return SQLITE_OK. If there is no work to do (and therefore no |
| 2086 | ** point in calling this function again), return SQLITE_DONE. |
| 2087 | ** |
| 2088 | ** More specificly, this function attempts to re-organize the |
| 2089 | ** database so that the last page of the file currently in use |
| 2090 | ** is no longer in use. |
| 2091 | ** |
| 2092 | ** If the nFin parameter is non-zero, the implementation assumes |
| 2093 | ** that the caller will keep calling incrVacuumStep() until |
| 2094 | ** it returns SQLITE_DONE or an error, and that nFin is the |
| 2095 | ** number of pages the database file will contain after this |
| 2096 | ** process is complete. |
| 2097 | */ |
| 2098 | static int incrVacuumStep(BtShared *pBt, Pgno nFin){ |
| 2099 | Pgno iLastPg; /* Last page in the database */ |
| 2100 | Pgno nFreeList; /* Number of pages still on the free-list */ |
| 2101 | |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 2102 | assert( sqlite3_mutex_held(pBt->mutex) ); |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 2103 | iLastPg = pBt->nTrunc; |
| 2104 | if( iLastPg==0 ){ |
| 2105 | iLastPg = sqlite3PagerPagecount(pBt->pPager); |
| 2106 | } |
| 2107 | |
| 2108 | if( !PTRMAP_ISPAGE(pBt, iLastPg) && iLastPg!=PENDING_BYTE_PAGE(pBt) ){ |
| 2109 | int rc; |
| 2110 | u8 eType; |
| 2111 | Pgno iPtrPage; |
| 2112 | |
| 2113 | nFreeList = get4byte(&pBt->pPage1->aData[36]); |
| 2114 | if( nFreeList==0 || nFin==iLastPg ){ |
| 2115 | return SQLITE_DONE; |
| 2116 | } |
| 2117 | |
| 2118 | rc = ptrmapGet(pBt, iLastPg, &eType, &iPtrPage); |
| 2119 | if( rc!=SQLITE_OK ){ |
| 2120 | return rc; |
| 2121 | } |
| 2122 | if( eType==PTRMAP_ROOTPAGE ){ |
| 2123 | return SQLITE_CORRUPT_BKPT; |
| 2124 | } |
| 2125 | |
| 2126 | if( eType==PTRMAP_FREEPAGE ){ |
| 2127 | if( nFin==0 ){ |
| 2128 | /* Remove the page from the files free-list. This is not required |
danielk1977 | 4ef2449 | 2007-05-23 09:52:41 +0000 | [diff] [blame] | 2129 | ** if nFin is non-zero. In that case, the free-list will be |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 2130 | ** truncated to zero after this function returns, so it doesn't |
| 2131 | ** matter if it still contains some garbage entries. |
| 2132 | */ |
| 2133 | Pgno iFreePg; |
| 2134 | MemPage *pFreePg; |
| 2135 | rc = allocateBtreePage(pBt, &pFreePg, &iFreePg, iLastPg, 1); |
| 2136 | if( rc!=SQLITE_OK ){ |
| 2137 | return rc; |
| 2138 | } |
| 2139 | assert( iFreePg==iLastPg ); |
| 2140 | releasePage(pFreePg); |
| 2141 | } |
| 2142 | } else { |
| 2143 | Pgno iFreePg; /* Index of free page to move pLastPg to */ |
| 2144 | MemPage *pLastPg; |
| 2145 | |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 2146 | rc = sqlite3BtreeGetPage(pBt, iLastPg, &pLastPg, 0); |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 2147 | if( rc!=SQLITE_OK ){ |
| 2148 | return rc; |
| 2149 | } |
| 2150 | |
danielk1977 | b4626a3 | 2007-04-28 15:47:43 +0000 | [diff] [blame] | 2151 | /* If nFin is zero, this loop runs exactly once and page pLastPg |
| 2152 | ** is swapped with the first free page pulled off the free list. |
| 2153 | ** |
| 2154 | ** On the other hand, if nFin is greater than zero, then keep |
| 2155 | ** looping until a free-page located within the first nFin pages |
| 2156 | ** of the file is found. |
| 2157 | */ |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 2158 | do { |
| 2159 | MemPage *pFreePg; |
| 2160 | rc = allocateBtreePage(pBt, &pFreePg, &iFreePg, 0, 0); |
| 2161 | if( rc!=SQLITE_OK ){ |
| 2162 | releasePage(pLastPg); |
| 2163 | return rc; |
| 2164 | } |
| 2165 | releasePage(pFreePg); |
| 2166 | }while( nFin!=0 && iFreePg>nFin ); |
| 2167 | assert( iFreePg<iLastPg ); |
danielk1977 | b4626a3 | 2007-04-28 15:47:43 +0000 | [diff] [blame] | 2168 | |
| 2169 | rc = sqlite3PagerWrite(pLastPg->pDbPage); |
| 2170 | if( rc!=SQLITE_OK ){ |
| 2171 | return rc; |
| 2172 | } |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 2173 | rc = relocatePage(pBt, pLastPg, eType, iPtrPage, iFreePg); |
| 2174 | releasePage(pLastPg); |
| 2175 | if( rc!=SQLITE_OK ){ |
| 2176 | return rc; |
| 2177 | } |
| 2178 | } |
| 2179 | } |
| 2180 | |
| 2181 | pBt->nTrunc = iLastPg - 1; |
| 2182 | while( pBt->nTrunc==PENDING_BYTE_PAGE(pBt)||PTRMAP_ISPAGE(pBt, pBt->nTrunc) ){ |
| 2183 | pBt->nTrunc--; |
| 2184 | } |
| 2185 | return SQLITE_OK; |
| 2186 | } |
| 2187 | |
| 2188 | /* |
| 2189 | ** A write-transaction must be opened before calling this function. |
| 2190 | ** It performs a single unit of work towards an incremental vacuum. |
| 2191 | ** |
| 2192 | ** If the incremental vacuum is finished after this function has run, |
| 2193 | ** SQLITE_DONE is returned. If it is not finished, but no error occured, |
| 2194 | ** SQLITE_OK is returned. Otherwise an SQLite error code. |
| 2195 | */ |
| 2196 | int sqlite3BtreeIncrVacuum(Btree *p){ |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 2197 | int rc; |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 2198 | BtShared *pBt = p->pBt; |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 2199 | |
| 2200 | sqlite3BtreeEnter(p); |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 2201 | assert( pBt->inTransaction==TRANS_WRITE && p->inTrans==TRANS_WRITE ); |
| 2202 | if( !pBt->autoVacuum ){ |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 2203 | rc = SQLITE_DONE; |
| 2204 | }else{ |
| 2205 | invalidateAllOverflowCache(pBt); |
| 2206 | rc = incrVacuumStep(pBt, 0); |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 2207 | } |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 2208 | sqlite3BtreeLeave(p); |
| 2209 | return rc; |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 2210 | } |
| 2211 | |
| 2212 | /* |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 2213 | ** This routine is called prior to sqlite3PagerCommit when a transaction |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 2214 | ** is commited for an auto-vacuum database. |
danielk1977 | 2416872 | 2007-04-02 05:07:47 +0000 | [diff] [blame] | 2215 | ** |
| 2216 | ** If SQLITE_OK is returned, then *pnTrunc is set to the number of pages |
| 2217 | ** the database file should be truncated to during the commit process. |
| 2218 | ** i.e. the database has been reorganized so that only the first *pnTrunc |
| 2219 | ** pages are in use. |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 2220 | */ |
danielk1977 | 2416872 | 2007-04-02 05:07:47 +0000 | [diff] [blame] | 2221 | static int autoVacuumCommit(BtShared *pBt, Pgno *pnTrunc){ |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 2222 | int rc = SQLITE_OK; |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 2223 | Pager *pPager = pBt->pPager; |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 2224 | #ifndef NDEBUG |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 2225 | int nRef = sqlite3PagerRefcount(pPager); |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 2226 | #endif |
| 2227 | |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 2228 | assert( sqlite3_mutex_held(pBt->mutex) ); |
danielk1977 | 92d4d7a | 2007-05-04 12:05:56 +0000 | [diff] [blame] | 2229 | invalidateAllOverflowCache(pBt); |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 2230 | assert(pBt->autoVacuum); |
| 2231 | if( !pBt->incrVacuum ){ |
| 2232 | Pgno nFin = 0; |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 2233 | |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 2234 | if( pBt->nTrunc==0 ){ |
| 2235 | Pgno nFree; |
| 2236 | Pgno nPtrmap; |
| 2237 | const int pgsz = pBt->pageSize; |
| 2238 | Pgno nOrig = sqlite3PagerPagecount(pBt->pPager); |
danielk1977 | e5321f0 | 2007-04-27 07:05:44 +0000 | [diff] [blame] | 2239 | |
| 2240 | if( PTRMAP_ISPAGE(pBt, nOrig) ){ |
| 2241 | return SQLITE_CORRUPT_BKPT; |
| 2242 | } |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 2243 | if( nOrig==PENDING_BYTE_PAGE(pBt) ){ |
| 2244 | nOrig--; |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 2245 | } |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 2246 | nFree = get4byte(&pBt->pPage1->aData[36]); |
| 2247 | nPtrmap = (nFree-nOrig+PTRMAP_PAGENO(pBt, nOrig)+pgsz/5)/(pgsz/5); |
| 2248 | nFin = nOrig - nFree - nPtrmap; |
| 2249 | if( nOrig>PENDING_BYTE_PAGE(pBt) && nFin<=PENDING_BYTE_PAGE(pBt) ){ |
| 2250 | nFin--; |
danielk1977 | ac11ee6 | 2005-01-15 12:45:51 +0000 | [diff] [blame] | 2251 | } |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 2252 | while( PTRMAP_ISPAGE(pBt, nFin) || nFin==PENDING_BYTE_PAGE(pBt) ){ |
| 2253 | nFin--; |
| 2254 | } |
| 2255 | } |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 2256 | |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 2257 | while( rc==SQLITE_OK ){ |
| 2258 | rc = incrVacuumStep(pBt, nFin); |
| 2259 | } |
| 2260 | if( rc==SQLITE_DONE ){ |
| 2261 | assert(nFin==0 || pBt->nTrunc==0 || nFin<=pBt->nTrunc); |
| 2262 | rc = SQLITE_OK; |
| 2263 | if( pBt->nTrunc ){ |
drh | 67f80b6 | 2007-07-23 19:26:17 +0000 | [diff] [blame] | 2264 | rc = sqlite3PagerWrite(pBt->pPage1->pDbPage); |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 2265 | put4byte(&pBt->pPage1->aData[32], 0); |
| 2266 | put4byte(&pBt->pPage1->aData[36], 0); |
| 2267 | pBt->nTrunc = nFin; |
| 2268 | } |
| 2269 | } |
| 2270 | if( rc!=SQLITE_OK ){ |
| 2271 | sqlite3PagerRollback(pPager); |
| 2272 | } |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 2273 | } |
| 2274 | |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 2275 | if( rc==SQLITE_OK ){ |
| 2276 | *pnTrunc = pBt->nTrunc; |
| 2277 | pBt->nTrunc = 0; |
| 2278 | } |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 2279 | assert( nRef==sqlite3PagerRefcount(pPager) ); |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 2280 | return rc; |
| 2281 | } |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 2282 | |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 2283 | #endif |
| 2284 | |
| 2285 | /* |
drh | 80e35f4 | 2007-03-30 14:06:34 +0000 | [diff] [blame] | 2286 | ** This routine does the first phase of a two-phase commit. This routine |
| 2287 | ** causes a rollback journal to be created (if it does not already exist) |
| 2288 | ** and populated with enough information so that if a power loss occurs |
| 2289 | ** the database can be restored to its original state by playing back |
| 2290 | ** the journal. Then the contents of the journal are flushed out to |
| 2291 | ** the disk. After the journal is safely on oxide, the changes to the |
| 2292 | ** database are written into the database file and flushed to oxide. |
| 2293 | ** At the end of this call, the rollback journal still exists on the |
| 2294 | ** disk and we are still holding all locks, so the transaction has not |
| 2295 | ** committed. See sqlite3BtreeCommit() for the second phase of the |
| 2296 | ** commit process. |
| 2297 | ** |
| 2298 | ** This call is a no-op if no write-transaction is currently active on pBt. |
| 2299 | ** |
| 2300 | ** Otherwise, sync the database file for the btree pBt. zMaster points to |
| 2301 | ** the name of a master journal file that should be written into the |
| 2302 | ** individual journal file, or is NULL, indicating no master journal file |
| 2303 | ** (single database transaction). |
| 2304 | ** |
| 2305 | ** When this is called, the master journal should already have been |
| 2306 | ** created, populated with this journal pointer and synced to disk. |
| 2307 | ** |
| 2308 | ** Once this is routine has returned, the only thing required to commit |
| 2309 | ** the write-transaction for this database file is to delete the journal. |
| 2310 | */ |
| 2311 | int sqlite3BtreeCommitPhaseOne(Btree *p, const char *zMaster){ |
| 2312 | int rc = SQLITE_OK; |
| 2313 | if( p->inTrans==TRANS_WRITE ){ |
| 2314 | BtShared *pBt = p->pBt; |
| 2315 | Pgno nTrunc = 0; |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 2316 | sqlite3BtreeEnter(p); |
drh | 80e35f4 | 2007-03-30 14:06:34 +0000 | [diff] [blame] | 2317 | #ifndef SQLITE_OMIT_AUTOVACUUM |
| 2318 | if( pBt->autoVacuum ){ |
| 2319 | rc = autoVacuumCommit(pBt, &nTrunc); |
| 2320 | if( rc!=SQLITE_OK ){ |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 2321 | sqlite3BtreeLeave(p); |
drh | 80e35f4 | 2007-03-30 14:06:34 +0000 | [diff] [blame] | 2322 | return rc; |
| 2323 | } |
| 2324 | } |
| 2325 | #endif |
| 2326 | rc = sqlite3PagerCommitPhaseOne(pBt->pPager, zMaster, nTrunc); |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 2327 | sqlite3BtreeLeave(p); |
drh | 80e35f4 | 2007-03-30 14:06:34 +0000 | [diff] [blame] | 2328 | } |
| 2329 | return rc; |
| 2330 | } |
| 2331 | |
| 2332 | /* |
drh | 2aa679f | 2001-06-25 02:11:07 +0000 | [diff] [blame] | 2333 | ** Commit the transaction currently in progress. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2334 | ** |
drh | 6e34599 | 2007-03-30 11:12:08 +0000 | [diff] [blame] | 2335 | ** This routine implements the second phase of a 2-phase commit. The |
| 2336 | ** sqlite3BtreeSync() routine does the first phase and should be invoked |
| 2337 | ** prior to calling this routine. The sqlite3BtreeSync() routine did |
| 2338 | ** all the work of writing information out to disk and flushing the |
| 2339 | ** contents so that they are written onto the disk platter. All this |
| 2340 | ** routine has to do is delete or truncate the rollback journal |
| 2341 | ** (which causes the transaction to commit) and drop locks. |
| 2342 | ** |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2343 | ** This will release the write lock on the database file. If there |
| 2344 | ** are no active cursors, it also releases the read lock. |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 2345 | */ |
drh | 80e35f4 | 2007-03-30 14:06:34 +0000 | [diff] [blame] | 2346 | int sqlite3BtreeCommitPhaseTwo(Btree *p){ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2347 | BtShared *pBt = p->pBt; |
| 2348 | |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 2349 | sqlite3BtreeEnter(p); |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2350 | btreeIntegrity(p); |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2351 | |
| 2352 | /* If the handle has a write-transaction open, commit the shared-btrees |
| 2353 | ** transaction and set the shared state to TRANS_READ. |
| 2354 | */ |
| 2355 | if( p->inTrans==TRANS_WRITE ){ |
danielk1977 | 7f7bc66 | 2006-01-23 13:47:47 +0000 | [diff] [blame] | 2356 | int rc; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2357 | assert( pBt->inTransaction==TRANS_WRITE ); |
| 2358 | assert( pBt->nTransaction>0 ); |
drh | 80e35f4 | 2007-03-30 14:06:34 +0000 | [diff] [blame] | 2359 | rc = sqlite3PagerCommitPhaseTwo(pBt->pPager); |
danielk1977 | 7f7bc66 | 2006-01-23 13:47:47 +0000 | [diff] [blame] | 2360 | if( rc!=SQLITE_OK ){ |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 2361 | sqlite3BtreeLeave(p); |
danielk1977 | 7f7bc66 | 2006-01-23 13:47:47 +0000 | [diff] [blame] | 2362 | return rc; |
| 2363 | } |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2364 | pBt->inTransaction = TRANS_READ; |
| 2365 | pBt->inStmt = 0; |
danielk1977 | ee5741e | 2004-05-31 10:01:34 +0000 | [diff] [blame] | 2366 | } |
danielk1977 | 7f7bc66 | 2006-01-23 13:47:47 +0000 | [diff] [blame] | 2367 | unlockAllTables(p); |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2368 | |
| 2369 | /* If the handle has any kind of transaction open, decrement the transaction |
| 2370 | ** count of the shared btree. If the transaction count reaches 0, set |
| 2371 | ** the shared state to TRANS_NONE. The unlockBtreeIfUnused() call below |
| 2372 | ** will unlock the pager. |
| 2373 | */ |
| 2374 | if( p->inTrans!=TRANS_NONE ){ |
| 2375 | pBt->nTransaction--; |
| 2376 | if( 0==pBt->nTransaction ){ |
| 2377 | pBt->inTransaction = TRANS_NONE; |
| 2378 | } |
| 2379 | } |
| 2380 | |
| 2381 | /* Set the handles current transaction state to TRANS_NONE and unlock |
| 2382 | ** the pager if this call closed the only read or write transaction. |
| 2383 | */ |
| 2384 | p->inTrans = TRANS_NONE; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2385 | unlockBtreeIfUnused(pBt); |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2386 | |
| 2387 | btreeIntegrity(p); |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 2388 | sqlite3BtreeLeave(p); |
danielk1977 | 7f7bc66 | 2006-01-23 13:47:47 +0000 | [diff] [blame] | 2389 | return SQLITE_OK; |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 2390 | } |
| 2391 | |
drh | 80e35f4 | 2007-03-30 14:06:34 +0000 | [diff] [blame] | 2392 | /* |
| 2393 | ** Do both phases of a commit. |
| 2394 | */ |
| 2395 | int sqlite3BtreeCommit(Btree *p){ |
| 2396 | int rc; |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 2397 | sqlite3BtreeEnter(p); |
drh | 80e35f4 | 2007-03-30 14:06:34 +0000 | [diff] [blame] | 2398 | rc = sqlite3BtreeCommitPhaseOne(p, 0); |
| 2399 | if( rc==SQLITE_OK ){ |
| 2400 | rc = sqlite3BtreeCommitPhaseTwo(p); |
| 2401 | } |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 2402 | sqlite3BtreeLeave(p); |
drh | 80e35f4 | 2007-03-30 14:06:34 +0000 | [diff] [blame] | 2403 | return rc; |
| 2404 | } |
| 2405 | |
danielk1977 | fbcd585 | 2004-06-15 02:44:18 +0000 | [diff] [blame] | 2406 | #ifndef NDEBUG |
| 2407 | /* |
| 2408 | ** Return the number of write-cursors open on this handle. This is for use |
| 2409 | ** in assert() expressions, so it is only compiled if NDEBUG is not |
| 2410 | ** defined. |
drh | fb98264 | 2007-08-30 01:19:59 +0000 | [diff] [blame] | 2411 | ** |
| 2412 | ** For the purposes of this routine, a write-cursor is any cursor that |
| 2413 | ** is capable of writing to the databse. That means the cursor was |
| 2414 | ** originally opened for writing and the cursor has not be disabled |
| 2415 | ** by having its state changed to CURSOR_FAULT. |
danielk1977 | fbcd585 | 2004-06-15 02:44:18 +0000 | [diff] [blame] | 2416 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2417 | static int countWriteCursors(BtShared *pBt){ |
danielk1977 | fbcd585 | 2004-06-15 02:44:18 +0000 | [diff] [blame] | 2418 | BtCursor *pCur; |
| 2419 | int r = 0; |
| 2420 | for(pCur=pBt->pCursor; pCur; pCur=pCur->pNext){ |
drh | fb98264 | 2007-08-30 01:19:59 +0000 | [diff] [blame] | 2421 | if( pCur->wrFlag && pCur->eState!=CURSOR_FAULT ) r++; |
danielk1977 | fbcd585 | 2004-06-15 02:44:18 +0000 | [diff] [blame] | 2422 | } |
| 2423 | return r; |
| 2424 | } |
| 2425 | #endif |
| 2426 | |
drh | c39e000 | 2004-05-07 23:50:57 +0000 | [diff] [blame] | 2427 | /* |
drh | fb98264 | 2007-08-30 01:19:59 +0000 | [diff] [blame] | 2428 | ** This routine sets the state to CURSOR_FAULT and the error |
| 2429 | ** code to errCode for every cursor on BtShared that pBtree |
| 2430 | ** references. |
| 2431 | ** |
| 2432 | ** Every cursor is tripped, including cursors that belong |
| 2433 | ** to other database connections that happen to be sharing |
| 2434 | ** the cache with pBtree. |
| 2435 | ** |
| 2436 | ** This routine gets called when a rollback occurs. |
| 2437 | ** All cursors using the same cache must be tripped |
| 2438 | ** to prevent them from trying to use the btree after |
| 2439 | ** the rollback. The rollback may have deleted tables |
| 2440 | ** or moved root pages, so it is not sufficient to |
| 2441 | ** save the state of the cursor. The cursor must be |
| 2442 | ** invalidated. |
| 2443 | */ |
| 2444 | void sqlite3BtreeTripAllCursors(Btree *pBtree, int errCode){ |
| 2445 | BtCursor *p; |
| 2446 | sqlite3BtreeEnter(pBtree); |
| 2447 | for(p=pBtree->pBt->pCursor; p; p=p->pNext){ |
| 2448 | clearCursorPosition(p); |
| 2449 | p->eState = CURSOR_FAULT; |
| 2450 | p->skip = errCode; |
| 2451 | } |
| 2452 | sqlite3BtreeLeave(pBtree); |
| 2453 | } |
| 2454 | |
| 2455 | /* |
drh | ecdc753 | 2001-09-23 02:35:53 +0000 | [diff] [blame] | 2456 | ** Rollback the transaction in progress. All cursors will be |
| 2457 | ** invalided by this operation. Any attempt to use a cursor |
| 2458 | ** that was open at the beginning of this operation will result |
| 2459 | ** in an error. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2460 | ** |
| 2461 | ** This will release the write lock on the database file. If there |
| 2462 | ** are no active cursors, it also releases the read lock. |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 2463 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2464 | int sqlite3BtreeRollback(Btree *p){ |
danielk1977 | 8d34dfd | 2006-01-24 16:37:57 +0000 | [diff] [blame] | 2465 | int rc; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2466 | BtShared *pBt = p->pBt; |
drh | 24cd67e | 2004-05-10 16:18:47 +0000 | [diff] [blame] | 2467 | MemPage *pPage1; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2468 | |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 2469 | sqlite3BtreeEnter(p); |
danielk1977 | 2b8c13e | 2006-01-24 14:21:24 +0000 | [diff] [blame] | 2470 | rc = saveAllCursors(pBt, 0, 0); |
danielk1977 | 8d34dfd | 2006-01-24 16:37:57 +0000 | [diff] [blame] | 2471 | #ifndef SQLITE_OMIT_SHARED_CACHE |
danielk1977 | 2b8c13e | 2006-01-24 14:21:24 +0000 | [diff] [blame] | 2472 | if( rc!=SQLITE_OK ){ |
danielk1977 | 8d34dfd | 2006-01-24 16:37:57 +0000 | [diff] [blame] | 2473 | /* This is a horrible situation. An IO or malloc() error occured whilst |
| 2474 | ** trying to save cursor positions. If this is an automatic rollback (as |
| 2475 | ** the result of a constraint, malloc() failure or IO error) then |
| 2476 | ** the cache may be internally inconsistent (not contain valid trees) so |
| 2477 | ** we cannot simply return the error to the caller. Instead, abort |
| 2478 | ** all queries that may be using any of the cursors that failed to save. |
| 2479 | */ |
drh | fb98264 | 2007-08-30 01:19:59 +0000 | [diff] [blame] | 2480 | sqlite3BtreeTripAllCursors(p, rc); |
danielk1977 | 2b8c13e | 2006-01-24 14:21:24 +0000 | [diff] [blame] | 2481 | } |
danielk1977 | 8d34dfd | 2006-01-24 16:37:57 +0000 | [diff] [blame] | 2482 | #endif |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2483 | btreeIntegrity(p); |
| 2484 | unlockAllTables(p); |
| 2485 | |
| 2486 | if( p->inTrans==TRANS_WRITE ){ |
danielk1977 | 8d34dfd | 2006-01-24 16:37:57 +0000 | [diff] [blame] | 2487 | int rc2; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2488 | |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 2489 | #ifndef SQLITE_OMIT_AUTOVACUUM |
| 2490 | pBt->nTrunc = 0; |
| 2491 | #endif |
| 2492 | |
danielk1977 | 8d34dfd | 2006-01-24 16:37:57 +0000 | [diff] [blame] | 2493 | assert( TRANS_WRITE==pBt->inTransaction ); |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 2494 | rc2 = sqlite3PagerRollback(pBt->pPager); |
danielk1977 | 8d34dfd | 2006-01-24 16:37:57 +0000 | [diff] [blame] | 2495 | if( rc2!=SQLITE_OK ){ |
| 2496 | rc = rc2; |
| 2497 | } |
| 2498 | |
drh | 24cd67e | 2004-05-10 16:18:47 +0000 | [diff] [blame] | 2499 | /* The rollback may have destroyed the pPage1->aData value. So |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 2500 | ** call sqlite3BtreeGetPage() on page 1 again to make |
| 2501 | ** sure pPage1->aData is set correctly. */ |
| 2502 | if( sqlite3BtreeGetPage(pBt, 1, &pPage1, 0)==SQLITE_OK ){ |
drh | 24cd67e | 2004-05-10 16:18:47 +0000 | [diff] [blame] | 2503 | releasePage(pPage1); |
| 2504 | } |
danielk1977 | fbcd585 | 2004-06-15 02:44:18 +0000 | [diff] [blame] | 2505 | assert( countWriteCursors(pBt)==0 ); |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2506 | pBt->inTransaction = TRANS_READ; |
drh | 24cd67e | 2004-05-10 16:18:47 +0000 | [diff] [blame] | 2507 | } |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2508 | |
| 2509 | if( p->inTrans!=TRANS_NONE ){ |
| 2510 | assert( pBt->nTransaction>0 ); |
| 2511 | pBt->nTransaction--; |
| 2512 | if( 0==pBt->nTransaction ){ |
| 2513 | pBt->inTransaction = TRANS_NONE; |
| 2514 | } |
| 2515 | } |
| 2516 | |
| 2517 | p->inTrans = TRANS_NONE; |
danielk1977 | ee5741e | 2004-05-31 10:01:34 +0000 | [diff] [blame] | 2518 | pBt->inStmt = 0; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2519 | unlockBtreeIfUnused(pBt); |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2520 | |
| 2521 | btreeIntegrity(p); |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 2522 | sqlite3BtreeLeave(p); |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 2523 | return rc; |
| 2524 | } |
| 2525 | |
| 2526 | /* |
drh | ab01f61 | 2004-05-22 02:55:23 +0000 | [diff] [blame] | 2527 | ** Start a statement subtransaction. The subtransaction can |
| 2528 | ** can be rolled back independently of the main transaction. |
| 2529 | ** You must start a transaction before starting a subtransaction. |
| 2530 | ** The subtransaction is ended automatically if the main transaction |
drh | 663fc63 | 2002-02-02 18:49:19 +0000 | [diff] [blame] | 2531 | ** commits or rolls back. |
| 2532 | ** |
drh | ab01f61 | 2004-05-22 02:55:23 +0000 | [diff] [blame] | 2533 | ** Only one subtransaction may be active at a time. It is an error to try |
| 2534 | ** to start a new subtransaction if another subtransaction is already active. |
| 2535 | ** |
| 2536 | ** Statement subtransactions are used around individual SQL statements |
| 2537 | ** that are contained within a BEGIN...COMMIT block. If a constraint |
| 2538 | ** error occurs within the statement, the effect of that one statement |
| 2539 | ** can be rolled back without having to rollback the entire transaction. |
drh | 663fc63 | 2002-02-02 18:49:19 +0000 | [diff] [blame] | 2540 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2541 | int sqlite3BtreeBeginStmt(Btree *p){ |
drh | 663fc63 | 2002-02-02 18:49:19 +0000 | [diff] [blame] | 2542 | int rc; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2543 | BtShared *pBt = p->pBt; |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 2544 | sqlite3BtreeEnter(p); |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2545 | if( (p->inTrans!=TRANS_WRITE) || pBt->inStmt ){ |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 2546 | rc = pBt->readOnly ? SQLITE_READONLY : SQLITE_ERROR; |
| 2547 | }else{ |
| 2548 | assert( pBt->inTransaction==TRANS_WRITE ); |
| 2549 | rc = pBt->readOnly ? SQLITE_OK : sqlite3PagerStmtBegin(pBt->pPager); |
| 2550 | pBt->inStmt = 1; |
drh | 0d65dc0 | 2002-02-03 00:56:09 +0000 | [diff] [blame] | 2551 | } |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 2552 | sqlite3BtreeLeave(p); |
drh | 663fc63 | 2002-02-02 18:49:19 +0000 | [diff] [blame] | 2553 | return rc; |
| 2554 | } |
| 2555 | |
| 2556 | |
| 2557 | /* |
drh | ab01f61 | 2004-05-22 02:55:23 +0000 | [diff] [blame] | 2558 | ** Commit the statment subtransaction currently in progress. If no |
| 2559 | ** subtransaction is active, this is a no-op. |
drh | 663fc63 | 2002-02-02 18:49:19 +0000 | [diff] [blame] | 2560 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2561 | int sqlite3BtreeCommitStmt(Btree *p){ |
drh | 663fc63 | 2002-02-02 18:49:19 +0000 | [diff] [blame] | 2562 | int rc; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2563 | BtShared *pBt = p->pBt; |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 2564 | sqlite3BtreeEnter(p); |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2565 | if( pBt->inStmt && !pBt->readOnly ){ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 2566 | rc = sqlite3PagerStmtCommit(pBt->pPager); |
drh | 663fc63 | 2002-02-02 18:49:19 +0000 | [diff] [blame] | 2567 | }else{ |
| 2568 | rc = SQLITE_OK; |
| 2569 | } |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2570 | pBt->inStmt = 0; |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 2571 | sqlite3BtreeLeave(p); |
drh | 663fc63 | 2002-02-02 18:49:19 +0000 | [diff] [blame] | 2572 | return rc; |
| 2573 | } |
| 2574 | |
| 2575 | /* |
drh | ab01f61 | 2004-05-22 02:55:23 +0000 | [diff] [blame] | 2576 | ** Rollback the active statement subtransaction. If no subtransaction |
| 2577 | ** is active this routine is a no-op. |
drh | 663fc63 | 2002-02-02 18:49:19 +0000 | [diff] [blame] | 2578 | ** |
drh | ab01f61 | 2004-05-22 02:55:23 +0000 | [diff] [blame] | 2579 | ** All cursors will be invalidated by this operation. Any attempt |
drh | 663fc63 | 2002-02-02 18:49:19 +0000 | [diff] [blame] | 2580 | ** to use a cursor that was open at the beginning of this operation |
| 2581 | ** will result in an error. |
| 2582 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2583 | int sqlite3BtreeRollbackStmt(Btree *p){ |
danielk1977 | 97a227c | 2006-01-20 16:32:04 +0000 | [diff] [blame] | 2584 | int rc = SQLITE_OK; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2585 | BtShared *pBt = p->pBt; |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 2586 | sqlite3BtreeEnter(p); |
danielk1977 | 97a227c | 2006-01-20 16:32:04 +0000 | [diff] [blame] | 2587 | sqlite3MallocDisallow(); |
| 2588 | if( pBt->inStmt && !pBt->readOnly ){ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 2589 | rc = sqlite3PagerStmtRollback(pBt->pPager); |
danielk1977 | 97a227c | 2006-01-20 16:32:04 +0000 | [diff] [blame] | 2590 | assert( countWriteCursors(pBt)==0 ); |
| 2591 | pBt->inStmt = 0; |
| 2592 | } |
| 2593 | sqlite3MallocAllow(); |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 2594 | sqlite3BtreeLeave(p); |
drh | 663fc63 | 2002-02-02 18:49:19 +0000 | [diff] [blame] | 2595 | return rc; |
| 2596 | } |
| 2597 | |
| 2598 | /* |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2599 | ** Default key comparison function to be used if no comparison function |
| 2600 | ** is specified on the sqlite3BtreeCursor() call. |
| 2601 | */ |
| 2602 | static int dfltCompare( |
| 2603 | void *NotUsed, /* User data is not used */ |
| 2604 | int n1, const void *p1, /* First key to compare */ |
| 2605 | int n2, const void *p2 /* Second key to compare */ |
| 2606 | ){ |
| 2607 | int c; |
| 2608 | c = memcmp(p1, p2, n1<n2 ? n1 : n2); |
| 2609 | if( c==0 ){ |
| 2610 | c = n1 - n2; |
| 2611 | } |
| 2612 | return c; |
| 2613 | } |
| 2614 | |
| 2615 | /* |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 2616 | ** Create a new cursor for the BTree whose root is on the page |
| 2617 | ** iTable. The act of acquiring a cursor gets a read lock on |
| 2618 | ** the database file. |
drh | 1bee3d7 | 2001-10-15 00:44:35 +0000 | [diff] [blame] | 2619 | ** |
| 2620 | ** If wrFlag==0, then the cursor can only be used for reading. |
drh | f74b8d9 | 2002-09-01 23:20:45 +0000 | [diff] [blame] | 2621 | ** If wrFlag==1, then the cursor can be used for reading or for |
| 2622 | ** writing if other conditions for writing are also met. These |
| 2623 | ** are the conditions that must be met in order for writing to |
| 2624 | ** be allowed: |
drh | 6446c4d | 2001-12-15 14:22:18 +0000 | [diff] [blame] | 2625 | ** |
drh | f74b8d9 | 2002-09-01 23:20:45 +0000 | [diff] [blame] | 2626 | ** 1: The cursor must have been opened with wrFlag==1 |
| 2627 | ** |
drh | fe5d71d | 2007-03-19 11:54:10 +0000 | [diff] [blame] | 2628 | ** 2: Other database connections that share the same pager cache |
| 2629 | ** but which are not in the READ_UNCOMMITTED state may not have |
| 2630 | ** cursors open with wrFlag==0 on the same table. Otherwise |
| 2631 | ** the changes made by this write cursor would be visible to |
| 2632 | ** the read cursors in the other database connection. |
drh | f74b8d9 | 2002-09-01 23:20:45 +0000 | [diff] [blame] | 2633 | ** |
| 2634 | ** 3: The database must be writable (not on read-only media) |
| 2635 | ** |
| 2636 | ** 4: There must be an active transaction. |
| 2637 | ** |
drh | 6446c4d | 2001-12-15 14:22:18 +0000 | [diff] [blame] | 2638 | ** No checking is done to make sure that page iTable really is the |
| 2639 | ** root page of a b-tree. If it is not, then the cursor acquired |
| 2640 | ** will not work correctly. |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2641 | ** |
| 2642 | ** The comparison function must be logically the same for every cursor |
| 2643 | ** on a particular table. Changing the comparison function will result |
| 2644 | ** in incorrect operations. If the comparison function is NULL, a |
| 2645 | ** default comparison function is used. The comparison function is |
| 2646 | ** always ignored for INTKEY tables. |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 2647 | */ |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 2648 | static int btreeCursor( |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2649 | Btree *p, /* The btree */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2650 | int iTable, /* Root page of table to open */ |
| 2651 | int wrFlag, /* 1 to write. 0 read-only */ |
| 2652 | int (*xCmp)(void*,int,const void*,int,const void*), /* Key Comparison func */ |
| 2653 | void *pArg, /* First arg to xCompare() */ |
| 2654 | BtCursor **ppCur /* Write new cursor here */ |
| 2655 | ){ |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 2656 | int rc; |
drh | 8dcd7ca | 2004-08-08 19:43:29 +0000 | [diff] [blame] | 2657 | BtCursor *pCur; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2658 | BtShared *pBt = p->pBt; |
drh | ecdc753 | 2001-09-23 02:35:53 +0000 | [diff] [blame] | 2659 | |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 2660 | assert( sqlite3BtreeHoldsMutex(p) ); |
drh | 8dcd7ca | 2004-08-08 19:43:29 +0000 | [diff] [blame] | 2661 | *ppCur = 0; |
| 2662 | if( wrFlag ){ |
drh | 8dcd7ca | 2004-08-08 19:43:29 +0000 | [diff] [blame] | 2663 | if( pBt->readOnly ){ |
| 2664 | return SQLITE_READONLY; |
| 2665 | } |
drh | 980b1a7 | 2006-08-16 16:42:48 +0000 | [diff] [blame] | 2666 | if( checkReadLocks(p, iTable, 0) ){ |
drh | 8dcd7ca | 2004-08-08 19:43:29 +0000 | [diff] [blame] | 2667 | return SQLITE_LOCKED; |
| 2668 | } |
drh | a0c9a11 | 2004-03-10 13:42:37 +0000 | [diff] [blame] | 2669 | } |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2670 | |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 2671 | if( pBt->pPage1==0 ){ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2672 | rc = lockBtreeWithRetry(p); |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 2673 | if( rc!=SQLITE_OK ){ |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 2674 | return rc; |
| 2675 | } |
drh | 1831f18 | 2007-04-24 17:35:59 +0000 | [diff] [blame] | 2676 | if( pBt->readOnly && wrFlag ){ |
| 2677 | return SQLITE_READONLY; |
| 2678 | } |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 2679 | } |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 2680 | pCur = sqlite3MallocZero( sizeof(*pCur) ); |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 2681 | if( pCur==0 ){ |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 2682 | rc = SQLITE_NOMEM; |
| 2683 | goto create_cursor_exception; |
| 2684 | } |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 2685 | pCur->pgnoRoot = (Pgno)iTable; |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 2686 | if( iTable==1 && sqlite3PagerPagecount(pBt->pPager)==0 ){ |
drh | 24cd67e | 2004-05-10 16:18:47 +0000 | [diff] [blame] | 2687 | rc = SQLITE_EMPTY; |
| 2688 | goto create_cursor_exception; |
| 2689 | } |
drh | de64713 | 2004-05-07 17:57:49 +0000 | [diff] [blame] | 2690 | rc = getAndInitPage(pBt, pCur->pgnoRoot, &pCur->pPage, 0); |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 2691 | if( rc!=SQLITE_OK ){ |
| 2692 | goto create_cursor_exception; |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 2693 | } |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2694 | |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2695 | /* Now that no other errors can occur, finish filling in the BtCursor |
| 2696 | ** variables, link the cursor into the BtShared list and set *ppCur (the |
| 2697 | ** output argument to this function). |
| 2698 | */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2699 | pCur->xCompare = xCmp ? xCmp : dfltCompare; |
| 2700 | pCur->pArg = pArg; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2701 | pCur->pBtree = p; |
drh | d0679ed | 2007-08-28 22:24:34 +0000 | [diff] [blame] | 2702 | pCur->pBt = pBt; |
drh | ecdc753 | 2001-09-23 02:35:53 +0000 | [diff] [blame] | 2703 | pCur->wrFlag = wrFlag; |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 2704 | pCur->pNext = pBt->pCursor; |
| 2705 | if( pCur->pNext ){ |
| 2706 | pCur->pNext->pPrev = pCur; |
| 2707 | } |
| 2708 | pBt->pCursor = pCur; |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 2709 | pCur->eState = CURSOR_INVALID; |
drh | 2af926b | 2001-05-15 00:39:25 +0000 | [diff] [blame] | 2710 | *ppCur = pCur; |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 2711 | |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2712 | return SQLITE_OK; |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 2713 | |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 2714 | create_cursor_exception: |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 2715 | if( pCur ){ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2716 | releasePage(pCur->pPage); |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 2717 | sqlite3_free(pCur); |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 2718 | } |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2719 | unlockBtreeIfUnused(pBt); |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 2720 | return rc; |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 2721 | } |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 2722 | int sqlite3BtreeCursor( |
| 2723 | Btree *p, /* The btree */ |
| 2724 | int iTable, /* Root page of table to open */ |
| 2725 | int wrFlag, /* 1 to write. 0 read-only */ |
| 2726 | int (*xCmp)(void*,int,const void*,int,const void*), /* Key Comparison func */ |
| 2727 | void *pArg, /* First arg to xCompare() */ |
| 2728 | BtCursor **ppCur /* Write new cursor here */ |
| 2729 | ){ |
| 2730 | int rc; |
| 2731 | sqlite3BtreeEnter(p); |
| 2732 | rc = btreeCursor(p, iTable, wrFlag, xCmp, pArg, ppCur); |
| 2733 | sqlite3BtreeLeave(p); |
| 2734 | return rc; |
| 2735 | } |
| 2736 | |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 2737 | |
| 2738 | /* |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2739 | ** Close a cursor. The read lock on the database file is released |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 2740 | ** when the last cursor is closed. |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 2741 | */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2742 | int sqlite3BtreeCloseCursor(BtCursor *pCur){ |
drh | d0679ed | 2007-08-28 22:24:34 +0000 | [diff] [blame] | 2743 | BtShared *pBt = pCur->pBt; |
drh | ff0587c | 2007-08-29 17:43:19 +0000 | [diff] [blame] | 2744 | Btree *pBtree = pCur->pBtree; |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 2745 | |
drh | ff0587c | 2007-08-29 17:43:19 +0000 | [diff] [blame] | 2746 | sqlite3BtreeEnter(pBtree); |
drh | bf700f3 | 2007-03-31 02:36:44 +0000 | [diff] [blame] | 2747 | clearCursorPosition(pCur); |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 2748 | if( pCur->pPrev ){ |
| 2749 | pCur->pPrev->pNext = pCur->pNext; |
| 2750 | }else{ |
| 2751 | pBt->pCursor = pCur->pNext; |
| 2752 | } |
| 2753 | if( pCur->pNext ){ |
| 2754 | pCur->pNext->pPrev = pCur->pPrev; |
| 2755 | } |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2756 | releasePage(pCur->pPage); |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2757 | unlockBtreeIfUnused(pBt); |
danielk1977 | 92d4d7a | 2007-05-04 12:05:56 +0000 | [diff] [blame] | 2758 | invalidateOverflowCache(pCur); |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 2759 | sqlite3_free(pCur); |
drh | ff0587c | 2007-08-29 17:43:19 +0000 | [diff] [blame] | 2760 | sqlite3BtreeLeave(pBtree); |
drh | 8c42ca9 | 2001-06-22 19:15:00 +0000 | [diff] [blame] | 2761 | return SQLITE_OK; |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 2762 | } |
| 2763 | |
drh | 7e3b0a0 | 2001-04-28 16:52:40 +0000 | [diff] [blame] | 2764 | /* |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 2765 | ** Make a temporary cursor by filling in the fields of pTempCur. |
| 2766 | ** The temporary cursor is not on the cursor list for the Btree. |
| 2767 | */ |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 2768 | void sqlite3BtreeGetTempCursor(BtCursor *pCur, BtCursor *pTempCur){ |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 2769 | assert( cursorHoldsMutex(pCur) ); |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 2770 | memcpy(pTempCur, pCur, sizeof(*pCur)); |
| 2771 | pTempCur->pNext = 0; |
| 2772 | pTempCur->pPrev = 0; |
drh | ecdc753 | 2001-09-23 02:35:53 +0000 | [diff] [blame] | 2773 | if( pTempCur->pPage ){ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 2774 | sqlite3PagerRef(pTempCur->pPage->pDbPage); |
drh | ecdc753 | 2001-09-23 02:35:53 +0000 | [diff] [blame] | 2775 | } |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 2776 | } |
| 2777 | |
| 2778 | /* |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 2779 | ** Delete a temporary cursor such as was made by the CreateTemporaryCursor() |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 2780 | ** function above. |
| 2781 | */ |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 2782 | void sqlite3BtreeReleaseTempCursor(BtCursor *pCur){ |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 2783 | assert( cursorHoldsMutex(pCur) ); |
drh | ecdc753 | 2001-09-23 02:35:53 +0000 | [diff] [blame] | 2784 | if( pCur->pPage ){ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 2785 | sqlite3PagerUnref(pCur->pPage->pDbPage); |
drh | ecdc753 | 2001-09-23 02:35:53 +0000 | [diff] [blame] | 2786 | } |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 2787 | } |
| 2788 | |
| 2789 | /* |
drh | 8605761 | 2007-06-26 01:04:48 +0000 | [diff] [blame] | 2790 | ** Make sure the BtCursor* given in the argument has a valid |
| 2791 | ** BtCursor.info structure. If it is not already valid, call |
danielk1977 | 1cc5ed8 | 2007-05-16 17:28:43 +0000 | [diff] [blame] | 2792 | ** sqlite3BtreeParseCell() to fill it in. |
drh | ab01f61 | 2004-05-22 02:55:23 +0000 | [diff] [blame] | 2793 | ** |
| 2794 | ** BtCursor.info is a cache of the information in the current cell. |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 2795 | ** Using this cache reduces the number of calls to sqlite3BtreeParseCell(). |
drh | 8605761 | 2007-06-26 01:04:48 +0000 | [diff] [blame] | 2796 | ** |
| 2797 | ** 2007-06-25: There is a bug in some versions of MSVC that cause the |
| 2798 | ** compiler to crash when getCellInfo() is implemented as a macro. |
| 2799 | ** But there is a measureable speed advantage to using the macro on gcc |
| 2800 | ** (when less compiler optimizations like -Os or -O0 are used and the |
| 2801 | ** compiler is not doing agressive inlining.) So we use a real function |
| 2802 | ** for MSVC and a macro for everything else. Ticket #2457. |
drh | 9188b38 | 2004-05-14 21:12:22 +0000 | [diff] [blame] | 2803 | */ |
drh | 9188b38 | 2004-05-14 21:12:22 +0000 | [diff] [blame] | 2804 | #ifndef NDEBUG |
danielk1977 | 1cc5ed8 | 2007-05-16 17:28:43 +0000 | [diff] [blame] | 2805 | static void assertCellInfo(BtCursor *pCur){ |
drh | 9188b38 | 2004-05-14 21:12:22 +0000 | [diff] [blame] | 2806 | CellInfo info; |
drh | 51c6d96 | 2004-06-06 00:42:25 +0000 | [diff] [blame] | 2807 | memset(&info, 0, sizeof(info)); |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 2808 | sqlite3BtreeParseCell(pCur->pPage, pCur->idx, &info); |
drh | 9188b38 | 2004-05-14 21:12:22 +0000 | [diff] [blame] | 2809 | assert( memcmp(&info, &pCur->info, sizeof(info))==0 ); |
drh | 9188b38 | 2004-05-14 21:12:22 +0000 | [diff] [blame] | 2810 | } |
danielk1977 | 1cc5ed8 | 2007-05-16 17:28:43 +0000 | [diff] [blame] | 2811 | #else |
| 2812 | #define assertCellInfo(x) |
| 2813 | #endif |
drh | 8605761 | 2007-06-26 01:04:48 +0000 | [diff] [blame] | 2814 | #ifdef _MSC_VER |
| 2815 | /* Use a real function in MSVC to work around bugs in that compiler. */ |
| 2816 | static void getCellInfo(BtCursor *pCur){ |
| 2817 | if( pCur->info.nSize==0 ){ |
| 2818 | sqlite3BtreeParseCell(pCur->pPage, pCur->idx, &pCur->info); |
| 2819 | }else{ |
| 2820 | assertCellInfo(pCur); |
| 2821 | } |
| 2822 | } |
| 2823 | #else /* if not _MSC_VER */ |
| 2824 | /* Use a macro in all other compilers so that the function is inlined */ |
| 2825 | #define getCellInfo(pCur) \ |
| 2826 | if( pCur->info.nSize==0 ){ \ |
danielk1977 | 1cc5ed8 | 2007-05-16 17:28:43 +0000 | [diff] [blame] | 2827 | sqlite3BtreeParseCell(pCur->pPage, pCur->idx, &pCur->info); \ |
drh | 8605761 | 2007-06-26 01:04:48 +0000 | [diff] [blame] | 2828 | }else{ \ |
| 2829 | assertCellInfo(pCur); \ |
| 2830 | } |
| 2831 | #endif /* _MSC_VER */ |
drh | 9188b38 | 2004-05-14 21:12:22 +0000 | [diff] [blame] | 2832 | |
| 2833 | /* |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2834 | ** Set *pSize to the size of the buffer needed to hold the value of |
| 2835 | ** the key for the current entry. If the cursor is not pointing |
| 2836 | ** to a valid entry, *pSize is set to 0. |
| 2837 | ** |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 2838 | ** For a table with the INTKEY flag set, this routine returns the key |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2839 | ** itself, not the number of bytes in the key. |
drh | 7e3b0a0 | 2001-04-28 16:52:40 +0000 | [diff] [blame] | 2840 | */ |
drh | 4a1c380 | 2004-05-12 15:15:47 +0000 | [diff] [blame] | 2841 | int sqlite3BtreeKeySize(BtCursor *pCur, i64 *pSize){ |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 2842 | int rc; |
| 2843 | |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 2844 | assert( cursorHoldsMutex(pCur) ); |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 2845 | rc = restoreOrClearCursorPosition(pCur); |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 2846 | if( rc==SQLITE_OK ){ |
| 2847 | assert( pCur->eState==CURSOR_INVALID || pCur->eState==CURSOR_VALID ); |
| 2848 | if( pCur->eState==CURSOR_INVALID ){ |
| 2849 | *pSize = 0; |
| 2850 | }else{ |
drh | 8605761 | 2007-06-26 01:04:48 +0000 | [diff] [blame] | 2851 | getCellInfo(pCur); |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 2852 | *pSize = pCur->info.nKey; |
| 2853 | } |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 2854 | } |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 2855 | return rc; |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 2856 | } |
drh | 2af926b | 2001-05-15 00:39:25 +0000 | [diff] [blame] | 2857 | |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 2858 | /* |
drh | 0e1c19e | 2004-05-11 00:58:56 +0000 | [diff] [blame] | 2859 | ** Set *pSize to the number of bytes of data in the entry the |
| 2860 | ** cursor currently points to. Always return SQLITE_OK. |
| 2861 | ** Failure is not possible. If the cursor is not currently |
| 2862 | ** pointing to an entry (which can happen, for example, if |
| 2863 | ** the database is empty) then *pSize is set to 0. |
| 2864 | */ |
| 2865 | int sqlite3BtreeDataSize(BtCursor *pCur, u32 *pSize){ |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 2866 | int rc; |
| 2867 | |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 2868 | assert( cursorHoldsMutex(pCur) ); |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 2869 | rc = restoreOrClearCursorPosition(pCur); |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 2870 | if( rc==SQLITE_OK ){ |
| 2871 | assert( pCur->eState==CURSOR_INVALID || pCur->eState==CURSOR_VALID ); |
| 2872 | if( pCur->eState==CURSOR_INVALID ){ |
| 2873 | /* Not pointing at a valid entry - set *pSize to 0. */ |
| 2874 | *pSize = 0; |
| 2875 | }else{ |
drh | 8605761 | 2007-06-26 01:04:48 +0000 | [diff] [blame] | 2876 | getCellInfo(pCur); |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 2877 | *pSize = pCur->info.nData; |
| 2878 | } |
drh | 0e1c19e | 2004-05-11 00:58:56 +0000 | [diff] [blame] | 2879 | } |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 2880 | return rc; |
drh | 0e1c19e | 2004-05-11 00:58:56 +0000 | [diff] [blame] | 2881 | } |
| 2882 | |
| 2883 | /* |
danielk1977 | d0441796 | 2007-05-02 13:16:30 +0000 | [diff] [blame] | 2884 | ** Given the page number of an overflow page in the database (parameter |
| 2885 | ** ovfl), this function finds the page number of the next page in the |
| 2886 | ** linked list of overflow pages. If possible, it uses the auto-vacuum |
| 2887 | ** pointer-map data instead of reading the content of page ovfl to do so. |
| 2888 | ** |
| 2889 | ** If an error occurs an SQLite error code is returned. Otherwise: |
| 2890 | ** |
| 2891 | ** Unless pPgnoNext is NULL, the page number of the next overflow |
| 2892 | ** page in the linked list is written to *pPgnoNext. If page ovfl |
| 2893 | ** is the last page in it's linked list, *pPgnoNext is set to zero. |
| 2894 | ** |
| 2895 | ** If ppPage is not NULL, *ppPage is set to the MemPage* handle |
| 2896 | ** for page ovfl. The underlying pager page may have been requested |
| 2897 | ** with the noContent flag set, so the page data accessable via |
| 2898 | ** this handle may not be trusted. |
| 2899 | */ |
| 2900 | static int getOverflowPage( |
| 2901 | BtShared *pBt, |
| 2902 | Pgno ovfl, /* Overflow page */ |
| 2903 | MemPage **ppPage, /* OUT: MemPage handle */ |
| 2904 | Pgno *pPgnoNext /* OUT: Next overflow page number */ |
| 2905 | ){ |
| 2906 | Pgno next = 0; |
| 2907 | int rc; |
| 2908 | |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 2909 | assert( sqlite3_mutex_held(pBt->mutex) ); |
danielk1977 | d0441796 | 2007-05-02 13:16:30 +0000 | [diff] [blame] | 2910 | /* One of these must not be NULL. Otherwise, why call this function? */ |
| 2911 | assert(ppPage || pPgnoNext); |
| 2912 | |
| 2913 | /* If pPgnoNext is NULL, then this function is being called to obtain |
| 2914 | ** a MemPage* reference only. No page-data is required in this case. |
| 2915 | */ |
| 2916 | if( !pPgnoNext ){ |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 2917 | return sqlite3BtreeGetPage(pBt, ovfl, ppPage, 1); |
danielk1977 | d0441796 | 2007-05-02 13:16:30 +0000 | [diff] [blame] | 2918 | } |
| 2919 | |
| 2920 | #ifndef SQLITE_OMIT_AUTOVACUUM |
| 2921 | /* Try to find the next page in the overflow list using the |
| 2922 | ** autovacuum pointer-map pages. Guess that the next page in |
| 2923 | ** the overflow list is page number (ovfl+1). If that guess turns |
| 2924 | ** out to be wrong, fall back to loading the data of page |
| 2925 | ** number ovfl to determine the next page number. |
| 2926 | */ |
| 2927 | if( pBt->autoVacuum ){ |
| 2928 | Pgno pgno; |
| 2929 | Pgno iGuess = ovfl+1; |
| 2930 | u8 eType; |
| 2931 | |
| 2932 | while( PTRMAP_ISPAGE(pBt, iGuess) || iGuess==PENDING_BYTE_PAGE(pBt) ){ |
| 2933 | iGuess++; |
| 2934 | } |
| 2935 | |
danielk1977 | 20713f3 | 2007-05-03 11:43:33 +0000 | [diff] [blame] | 2936 | if( iGuess<=sqlite3PagerPagecount(pBt->pPager) ){ |
danielk1977 | d0441796 | 2007-05-02 13:16:30 +0000 | [diff] [blame] | 2937 | rc = ptrmapGet(pBt, iGuess, &eType, &pgno); |
| 2938 | if( rc!=SQLITE_OK ){ |
| 2939 | return rc; |
| 2940 | } |
| 2941 | if( eType==PTRMAP_OVERFLOW2 && pgno==ovfl ){ |
| 2942 | next = iGuess; |
| 2943 | } |
| 2944 | } |
| 2945 | } |
| 2946 | #endif |
| 2947 | |
| 2948 | if( next==0 || ppPage ){ |
| 2949 | MemPage *pPage = 0; |
| 2950 | |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 2951 | rc = sqlite3BtreeGetPage(pBt, ovfl, &pPage, next!=0); |
danielk1977 | d0441796 | 2007-05-02 13:16:30 +0000 | [diff] [blame] | 2952 | assert(rc==SQLITE_OK || pPage==0); |
| 2953 | if( next==0 && rc==SQLITE_OK ){ |
| 2954 | next = get4byte(pPage->aData); |
| 2955 | } |
| 2956 | |
| 2957 | if( ppPage ){ |
| 2958 | *ppPage = pPage; |
| 2959 | }else{ |
| 2960 | releasePage(pPage); |
| 2961 | } |
| 2962 | } |
| 2963 | *pPgnoNext = next; |
| 2964 | |
| 2965 | return rc; |
| 2966 | } |
| 2967 | |
danielk1977 | da10719 | 2007-05-04 08:32:13 +0000 | [diff] [blame] | 2968 | /* |
| 2969 | ** Copy data from a buffer to a page, or from a page to a buffer. |
| 2970 | ** |
| 2971 | ** pPayload is a pointer to data stored on database page pDbPage. |
| 2972 | ** If argument eOp is false, then nByte bytes of data are copied |
| 2973 | ** from pPayload to the buffer pointed at by pBuf. If eOp is true, |
| 2974 | ** then sqlite3PagerWrite() is called on pDbPage and nByte bytes |
| 2975 | ** of data are copied from the buffer pBuf to pPayload. |
| 2976 | ** |
| 2977 | ** SQLITE_OK is returned on success, otherwise an error code. |
| 2978 | */ |
| 2979 | static int copyPayload( |
| 2980 | void *pPayload, /* Pointer to page data */ |
| 2981 | void *pBuf, /* Pointer to buffer */ |
| 2982 | int nByte, /* Number of bytes to copy */ |
| 2983 | int eOp, /* 0 -> copy from page, 1 -> copy to page */ |
| 2984 | DbPage *pDbPage /* Page containing pPayload */ |
| 2985 | ){ |
| 2986 | if( eOp ){ |
| 2987 | /* Copy data from buffer to page (a write operation) */ |
| 2988 | int rc = sqlite3PagerWrite(pDbPage); |
| 2989 | if( rc!=SQLITE_OK ){ |
| 2990 | return rc; |
| 2991 | } |
| 2992 | memcpy(pPayload, pBuf, nByte); |
| 2993 | }else{ |
| 2994 | /* Copy data from page to buffer (a read operation) */ |
| 2995 | memcpy(pBuf, pPayload, nByte); |
| 2996 | } |
| 2997 | return SQLITE_OK; |
| 2998 | } |
danielk1977 | d0441796 | 2007-05-02 13:16:30 +0000 | [diff] [blame] | 2999 | |
| 3000 | /* |
danielk1977 | 9f8d640 | 2007-05-02 17:48:45 +0000 | [diff] [blame] | 3001 | ** This function is used to read or overwrite payload information |
| 3002 | ** for the entry that the pCur cursor is pointing to. If the eOp |
| 3003 | ** parameter is 0, this is a read operation (data copied into |
| 3004 | ** buffer pBuf). If it is non-zero, a write (data copied from |
| 3005 | ** buffer pBuf). |
| 3006 | ** |
| 3007 | ** A total of "amt" bytes are read or written beginning at "offset". |
| 3008 | ** Data is read to or from the buffer pBuf. |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 3009 | ** |
| 3010 | ** This routine does not make a distinction between key and data. |
danielk1977 | 9f8d640 | 2007-05-02 17:48:45 +0000 | [diff] [blame] | 3011 | ** It just reads or writes bytes from the payload area. Data might |
| 3012 | ** appear on the main page or be scattered out on multiple overflow |
| 3013 | ** pages. |
danielk1977 | da10719 | 2007-05-04 08:32:13 +0000 | [diff] [blame] | 3014 | ** |
danielk1977 | dcbb5d3 | 2007-05-04 18:36:44 +0000 | [diff] [blame] | 3015 | ** If the BtCursor.isIncrblobHandle flag is set, and the current |
danielk1977 | da10719 | 2007-05-04 08:32:13 +0000 | [diff] [blame] | 3016 | ** cursor entry uses one or more overflow pages, this function |
| 3017 | ** allocates space for and lazily popluates the overflow page-list |
| 3018 | ** cache array (BtCursor.aOverflow). Subsequent calls use this |
| 3019 | ** cache to make seeking to the supplied offset more efficient. |
| 3020 | ** |
| 3021 | ** Once an overflow page-list cache has been allocated, it may be |
| 3022 | ** invalidated if some other cursor writes to the same table, or if |
| 3023 | ** the cursor is moved to a different row. Additionally, in auto-vacuum |
| 3024 | ** mode, the following events may invalidate an overflow page-list cache. |
| 3025 | ** |
| 3026 | ** * An incremental vacuum, |
| 3027 | ** * A commit in auto_vacuum="full" mode, |
| 3028 | ** * Creating a table (may require moving an overflow page). |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 3029 | */ |
danielk1977 | 9f8d640 | 2007-05-02 17:48:45 +0000 | [diff] [blame] | 3030 | static int accessPayload( |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3031 | BtCursor *pCur, /* Cursor pointing to entry to read from */ |
| 3032 | int offset, /* Begin reading this far into payload */ |
| 3033 | int amt, /* Read this many bytes */ |
| 3034 | unsigned char *pBuf, /* Write the bytes into this buffer */ |
danielk1977 | 9f8d640 | 2007-05-02 17:48:45 +0000 | [diff] [blame] | 3035 | int skipKey, /* offset begins at data if this is true */ |
| 3036 | int eOp /* zero to read. non-zero to write. */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3037 | ){ |
| 3038 | unsigned char *aPayload; |
danielk1977 | da10719 | 2007-05-04 08:32:13 +0000 | [diff] [blame] | 3039 | int rc = SQLITE_OK; |
drh | fa1a98a | 2004-05-14 19:08:17 +0000 | [diff] [blame] | 3040 | u32 nKey; |
danielk1977 | 2dec970 | 2007-05-02 16:48:37 +0000 | [diff] [blame] | 3041 | int iIdx = 0; |
drh | d0679ed | 2007-08-28 22:24:34 +0000 | [diff] [blame] | 3042 | MemPage *pPage = pCur->pPage; /* Btree page of current cursor entry */ |
| 3043 | BtShared *pBt = pCur->pBt; /* Btree this cursor belongs to */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3044 | |
danielk1977 | da10719 | 2007-05-04 08:32:13 +0000 | [diff] [blame] | 3045 | assert( pPage ); |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 3046 | assert( pCur->eState==CURSOR_VALID ); |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3047 | assert( pCur->idx>=0 && pCur->idx<pPage->nCell ); |
danielk1977 | da10719 | 2007-05-04 08:32:13 +0000 | [diff] [blame] | 3048 | assert( offset>=0 ); |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 3049 | assert( cursorHoldsMutex(pCur) ); |
danielk1977 | da10719 | 2007-05-04 08:32:13 +0000 | [diff] [blame] | 3050 | |
drh | 8605761 | 2007-06-26 01:04:48 +0000 | [diff] [blame] | 3051 | getCellInfo(pCur); |
drh | 366fda6 | 2006-01-13 02:35:09 +0000 | [diff] [blame] | 3052 | aPayload = pCur->info.pCell + pCur->info.nHeader; |
danielk1977 | da10719 | 2007-05-04 08:32:13 +0000 | [diff] [blame] | 3053 | nKey = (pPage->intKey ? 0 : pCur->info.nKey); |
| 3054 | |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3055 | if( skipKey ){ |
drh | fa1a98a | 2004-05-14 19:08:17 +0000 | [diff] [blame] | 3056 | offset += nKey; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3057 | } |
drh | fa1a98a | 2004-05-14 19:08:17 +0000 | [diff] [blame] | 3058 | if( offset+amt > nKey+pCur->info.nData ){ |
danielk1977 | da10719 | 2007-05-04 08:32:13 +0000 | [diff] [blame] | 3059 | /* Trying to read or write past the end of the data is an error */ |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 3060 | return SQLITE_ERROR; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3061 | } |
danielk1977 | da10719 | 2007-05-04 08:32:13 +0000 | [diff] [blame] | 3062 | |
| 3063 | /* Check if data must be read/written to/from the btree page itself. */ |
drh | fa1a98a | 2004-05-14 19:08:17 +0000 | [diff] [blame] | 3064 | if( offset<pCur->info.nLocal ){ |
drh | 2af926b | 2001-05-15 00:39:25 +0000 | [diff] [blame] | 3065 | int a = amt; |
drh | fa1a98a | 2004-05-14 19:08:17 +0000 | [diff] [blame] | 3066 | if( a+offset>pCur->info.nLocal ){ |
| 3067 | a = pCur->info.nLocal - offset; |
drh | 2af926b | 2001-05-15 00:39:25 +0000 | [diff] [blame] | 3068 | } |
danielk1977 | da10719 | 2007-05-04 08:32:13 +0000 | [diff] [blame] | 3069 | rc = copyPayload(&aPayload[offset], pBuf, a, eOp, pPage->pDbPage); |
drh | 2aa679f | 2001-06-25 02:11:07 +0000 | [diff] [blame] | 3070 | offset = 0; |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 3071 | pBuf += a; |
drh | 2af926b | 2001-05-15 00:39:25 +0000 | [diff] [blame] | 3072 | amt -= a; |
drh | dd79342 | 2001-06-28 01:54:48 +0000 | [diff] [blame] | 3073 | }else{ |
drh | fa1a98a | 2004-05-14 19:08:17 +0000 | [diff] [blame] | 3074 | offset -= pCur->info.nLocal; |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 3075 | } |
danielk1977 | da10719 | 2007-05-04 08:32:13 +0000 | [diff] [blame] | 3076 | |
| 3077 | if( rc==SQLITE_OK && amt>0 ){ |
| 3078 | const int ovflSize = pBt->usableSize - 4; /* Bytes content per ovfl page */ |
| 3079 | Pgno nextPage; |
| 3080 | |
drh | fa1a98a | 2004-05-14 19:08:17 +0000 | [diff] [blame] | 3081 | nextPage = get4byte(&aPayload[pCur->info.nLocal]); |
danielk1977 | da10719 | 2007-05-04 08:32:13 +0000 | [diff] [blame] | 3082 | |
danielk1977 | 2dec970 | 2007-05-02 16:48:37 +0000 | [diff] [blame] | 3083 | #ifndef SQLITE_OMIT_INCRBLOB |
danielk1977 | dcbb5d3 | 2007-05-04 18:36:44 +0000 | [diff] [blame] | 3084 | /* If the isIncrblobHandle flag is set and the BtCursor.aOverflow[] |
danielk1977 | da10719 | 2007-05-04 08:32:13 +0000 | [diff] [blame] | 3085 | ** has not been allocated, allocate it now. The array is sized at |
| 3086 | ** one entry for each overflow page in the overflow chain. The |
| 3087 | ** page number of the first overflow page is stored in aOverflow[0], |
| 3088 | ** etc. A value of 0 in the aOverflow[] array means "not yet known" |
| 3089 | ** (the cache is lazily populated). |
| 3090 | */ |
danielk1977 | dcbb5d3 | 2007-05-04 18:36:44 +0000 | [diff] [blame] | 3091 | if( pCur->isIncrblobHandle && !pCur->aOverflow ){ |
danielk1977 | 2dec970 | 2007-05-02 16:48:37 +0000 | [diff] [blame] | 3092 | int nOvfl = (pCur->info.nPayload-pCur->info.nLocal+ovflSize-1)/ovflSize; |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 3093 | pCur->aOverflow = (Pgno *)sqlite3MallocZero(sizeof(Pgno)*nOvfl); |
danielk1977 | 2dec970 | 2007-05-02 16:48:37 +0000 | [diff] [blame] | 3094 | if( nOvfl && !pCur->aOverflow ){ |
danielk1977 | da10719 | 2007-05-04 08:32:13 +0000 | [diff] [blame] | 3095 | rc = SQLITE_NOMEM; |
danielk1977 | 2dec970 | 2007-05-02 16:48:37 +0000 | [diff] [blame] | 3096 | } |
| 3097 | } |
danielk1977 | da10719 | 2007-05-04 08:32:13 +0000 | [diff] [blame] | 3098 | |
| 3099 | /* If the overflow page-list cache has been allocated and the |
| 3100 | ** entry for the first required overflow page is valid, skip |
| 3101 | ** directly to it. |
| 3102 | */ |
danielk1977 | 2dec970 | 2007-05-02 16:48:37 +0000 | [diff] [blame] | 3103 | if( pCur->aOverflow && pCur->aOverflow[offset/ovflSize] ){ |
| 3104 | iIdx = (offset/ovflSize); |
| 3105 | nextPage = pCur->aOverflow[iIdx]; |
| 3106 | offset = (offset%ovflSize); |
| 3107 | } |
| 3108 | #endif |
danielk1977 | da10719 | 2007-05-04 08:32:13 +0000 | [diff] [blame] | 3109 | |
| 3110 | for( ; rc==SQLITE_OK && amt>0 && nextPage; iIdx++){ |
| 3111 | |
| 3112 | #ifndef SQLITE_OMIT_INCRBLOB |
| 3113 | /* If required, populate the overflow page-list cache. */ |
| 3114 | if( pCur->aOverflow ){ |
| 3115 | assert(!pCur->aOverflow[iIdx] || pCur->aOverflow[iIdx]==nextPage); |
| 3116 | pCur->aOverflow[iIdx] = nextPage; |
| 3117 | } |
| 3118 | #endif |
| 3119 | |
danielk1977 | d0441796 | 2007-05-02 13:16:30 +0000 | [diff] [blame] | 3120 | if( offset>=ovflSize ){ |
| 3121 | /* The only reason to read this page is to obtain the page |
danielk1977 | da10719 | 2007-05-04 08:32:13 +0000 | [diff] [blame] | 3122 | ** number for the next page in the overflow chain. The page |
drh | fd131da | 2007-08-07 17:13:03 +0000 | [diff] [blame] | 3123 | ** data is not required. So first try to lookup the overflow |
| 3124 | ** page-list cache, if any, then fall back to the getOverflowPage() |
danielk1977 | da10719 | 2007-05-04 08:32:13 +0000 | [diff] [blame] | 3125 | ** function. |
danielk1977 | d0441796 | 2007-05-02 13:16:30 +0000 | [diff] [blame] | 3126 | */ |
danielk1977 | 2dec970 | 2007-05-02 16:48:37 +0000 | [diff] [blame] | 3127 | #ifndef SQLITE_OMIT_INCRBLOB |
danielk1977 | da10719 | 2007-05-04 08:32:13 +0000 | [diff] [blame] | 3128 | if( pCur->aOverflow && pCur->aOverflow[iIdx+1] ){ |
| 3129 | nextPage = pCur->aOverflow[iIdx+1]; |
| 3130 | } else |
danielk1977 | 2dec970 | 2007-05-02 16:48:37 +0000 | [diff] [blame] | 3131 | #endif |
danielk1977 | da10719 | 2007-05-04 08:32:13 +0000 | [diff] [blame] | 3132 | rc = getOverflowPage(pBt, nextPage, 0, &nextPage); |
danielk1977 | da10719 | 2007-05-04 08:32:13 +0000 | [diff] [blame] | 3133 | offset -= ovflSize; |
danielk1977 | d0441796 | 2007-05-02 13:16:30 +0000 | [diff] [blame] | 3134 | }else{ |
danielk1977 | 9f8d640 | 2007-05-02 17:48:45 +0000 | [diff] [blame] | 3135 | /* Need to read this page properly. It contains some of the |
| 3136 | ** range of data that is being read (eOp==0) or written (eOp!=0). |
danielk1977 | d0441796 | 2007-05-02 13:16:30 +0000 | [diff] [blame] | 3137 | */ |
| 3138 | DbPage *pDbPage; |
danielk1977 | cfe9a69 | 2004-06-16 12:00:29 +0000 | [diff] [blame] | 3139 | int a = amt; |
danielk1977 | d0441796 | 2007-05-02 13:16:30 +0000 | [diff] [blame] | 3140 | rc = sqlite3PagerGet(pBt->pPager, nextPage, &pDbPage); |
danielk1977 | da10719 | 2007-05-04 08:32:13 +0000 | [diff] [blame] | 3141 | if( rc==SQLITE_OK ){ |
| 3142 | aPayload = sqlite3PagerGetData(pDbPage); |
| 3143 | nextPage = get4byte(aPayload); |
| 3144 | if( a + offset > ovflSize ){ |
| 3145 | a = ovflSize - offset; |
danielk1977 | 9f8d640 | 2007-05-02 17:48:45 +0000 | [diff] [blame] | 3146 | } |
danielk1977 | da10719 | 2007-05-04 08:32:13 +0000 | [diff] [blame] | 3147 | rc = copyPayload(&aPayload[offset+4], pBuf, a, eOp, pDbPage); |
| 3148 | sqlite3PagerUnref(pDbPage); |
| 3149 | offset = 0; |
| 3150 | amt -= a; |
| 3151 | pBuf += a; |
danielk1977 | 9f8d640 | 2007-05-02 17:48:45 +0000 | [diff] [blame] | 3152 | } |
danielk1977 | cfe9a69 | 2004-06-16 12:00:29 +0000 | [diff] [blame] | 3153 | } |
drh | 2af926b | 2001-05-15 00:39:25 +0000 | [diff] [blame] | 3154 | } |
drh | 2af926b | 2001-05-15 00:39:25 +0000 | [diff] [blame] | 3155 | } |
danielk1977 | cfe9a69 | 2004-06-16 12:00:29 +0000 | [diff] [blame] | 3156 | |
danielk1977 | da10719 | 2007-05-04 08:32:13 +0000 | [diff] [blame] | 3157 | if( rc==SQLITE_OK && amt>0 ){ |
drh | 4928570 | 2005-09-17 15:20:26 +0000 | [diff] [blame] | 3158 | return SQLITE_CORRUPT_BKPT; |
drh | a7fcb05 | 2001-12-14 15:09:55 +0000 | [diff] [blame] | 3159 | } |
danielk1977 | da10719 | 2007-05-04 08:32:13 +0000 | [diff] [blame] | 3160 | return rc; |
drh | 2af926b | 2001-05-15 00:39:25 +0000 | [diff] [blame] | 3161 | } |
| 3162 | |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 3163 | /* |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3164 | ** Read part of the key associated with cursor pCur. Exactly |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 3165 | ** "amt" bytes will be transfered into pBuf[]. The transfer |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3166 | ** begins at "offset". |
drh | 8c1238a | 2003-01-02 14:43:55 +0000 | [diff] [blame] | 3167 | ** |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3168 | ** Return SQLITE_OK on success or an error code if anything goes |
| 3169 | ** wrong. An error is returned if "offset+amt" is larger than |
| 3170 | ** the available payload. |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 3171 | */ |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 3172 | int sqlite3BtreeKey(BtCursor *pCur, u32 offset, u32 amt, void *pBuf){ |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 3173 | int rc; |
| 3174 | |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 3175 | assert( cursorHoldsMutex(pCur) ); |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 3176 | rc = restoreOrClearCursorPosition(pCur); |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 3177 | if( rc==SQLITE_OK ){ |
| 3178 | assert( pCur->eState==CURSOR_VALID ); |
| 3179 | assert( pCur->pPage!=0 ); |
| 3180 | if( pCur->pPage->intKey ){ |
| 3181 | return SQLITE_CORRUPT_BKPT; |
| 3182 | } |
| 3183 | assert( pCur->pPage->intKey==0 ); |
| 3184 | assert( pCur->idx>=0 && pCur->idx<pCur->pPage->nCell ); |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 3185 | rc = accessPayload(pCur, offset, amt, (unsigned char*)pBuf, 0, 0); |
drh | 6575a22 | 2005-03-10 17:06:34 +0000 | [diff] [blame] | 3186 | } |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 3187 | return rc; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3188 | } |
| 3189 | |
| 3190 | /* |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3191 | ** Read part of the data associated with cursor pCur. Exactly |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 3192 | ** "amt" bytes will be transfered into pBuf[]. The transfer |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3193 | ** begins at "offset". |
| 3194 | ** |
| 3195 | ** Return SQLITE_OK on success or an error code if anything goes |
| 3196 | ** wrong. An error is returned if "offset+amt" is larger than |
| 3197 | ** the available payload. |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 3198 | */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3199 | int sqlite3BtreeData(BtCursor *pCur, u32 offset, u32 amt, void *pBuf){ |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 3200 | int rc; |
| 3201 | |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 3202 | assert( cursorHoldsMutex(pCur) ); |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 3203 | rc = restoreOrClearCursorPosition(pCur); |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 3204 | if( rc==SQLITE_OK ){ |
| 3205 | assert( pCur->eState==CURSOR_VALID ); |
| 3206 | assert( pCur->pPage!=0 ); |
| 3207 | assert( pCur->idx>=0 && pCur->idx<pCur->pPage->nCell ); |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 3208 | rc = accessPayload(pCur, offset, amt, pBuf, 1, 0); |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 3209 | } |
| 3210 | return rc; |
drh | 2af926b | 2001-05-15 00:39:25 +0000 | [diff] [blame] | 3211 | } |
| 3212 | |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 3213 | /* |
drh | 0e1c19e | 2004-05-11 00:58:56 +0000 | [diff] [blame] | 3214 | ** Return a pointer to payload information from the entry that the |
| 3215 | ** pCur cursor is pointing to. The pointer is to the beginning of |
| 3216 | ** the key if skipKey==0 and it points to the beginning of data if |
drh | e51c44f | 2004-05-30 20:46:09 +0000 | [diff] [blame] | 3217 | ** skipKey==1. The number of bytes of available key/data is written |
| 3218 | ** into *pAmt. If *pAmt==0, then the value returned will not be |
| 3219 | ** a valid pointer. |
drh | 0e1c19e | 2004-05-11 00:58:56 +0000 | [diff] [blame] | 3220 | ** |
| 3221 | ** This routine is an optimization. It is common for the entire key |
| 3222 | ** and data to fit on the local page and for there to be no overflow |
| 3223 | ** pages. When that is so, this routine can be used to access the |
| 3224 | ** key and data without making a copy. If the key and/or data spills |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 3225 | ** onto overflow pages, then accessPayload() must be used to reassembly |
drh | 0e1c19e | 2004-05-11 00:58:56 +0000 | [diff] [blame] | 3226 | ** the key/data and copy it into a preallocated buffer. |
| 3227 | ** |
| 3228 | ** The pointer returned by this routine looks directly into the cached |
| 3229 | ** page of the database. The data might change or move the next time |
| 3230 | ** any btree routine is called. |
| 3231 | */ |
| 3232 | static const unsigned char *fetchPayload( |
| 3233 | BtCursor *pCur, /* Cursor pointing to entry to read from */ |
drh | e51c44f | 2004-05-30 20:46:09 +0000 | [diff] [blame] | 3234 | int *pAmt, /* Write the number of available bytes here */ |
drh | 0e1c19e | 2004-05-11 00:58:56 +0000 | [diff] [blame] | 3235 | int skipKey /* read beginning at data if this is true */ |
| 3236 | ){ |
| 3237 | unsigned char *aPayload; |
| 3238 | MemPage *pPage; |
drh | fa1a98a | 2004-05-14 19:08:17 +0000 | [diff] [blame] | 3239 | u32 nKey; |
| 3240 | int nLocal; |
drh | 0e1c19e | 2004-05-11 00:58:56 +0000 | [diff] [blame] | 3241 | |
| 3242 | assert( pCur!=0 && pCur->pPage!=0 ); |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 3243 | assert( pCur->eState==CURSOR_VALID ); |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 3244 | assert( cursorHoldsMutex(pCur) ); |
drh | 0e1c19e | 2004-05-11 00:58:56 +0000 | [diff] [blame] | 3245 | pPage = pCur->pPage; |
drh | 0e1c19e | 2004-05-11 00:58:56 +0000 | [diff] [blame] | 3246 | assert( pCur->idx>=0 && pCur->idx<pPage->nCell ); |
drh | 8605761 | 2007-06-26 01:04:48 +0000 | [diff] [blame] | 3247 | getCellInfo(pCur); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 3248 | aPayload = pCur->info.pCell; |
drh | fa1a98a | 2004-05-14 19:08:17 +0000 | [diff] [blame] | 3249 | aPayload += pCur->info.nHeader; |
drh | 0e1c19e | 2004-05-11 00:58:56 +0000 | [diff] [blame] | 3250 | if( pPage->intKey ){ |
drh | fa1a98a | 2004-05-14 19:08:17 +0000 | [diff] [blame] | 3251 | nKey = 0; |
| 3252 | }else{ |
| 3253 | nKey = pCur->info.nKey; |
drh | 0e1c19e | 2004-05-11 00:58:56 +0000 | [diff] [blame] | 3254 | } |
drh | 0e1c19e | 2004-05-11 00:58:56 +0000 | [diff] [blame] | 3255 | if( skipKey ){ |
drh | fa1a98a | 2004-05-14 19:08:17 +0000 | [diff] [blame] | 3256 | aPayload += nKey; |
| 3257 | nLocal = pCur->info.nLocal - nKey; |
drh | 0e1c19e | 2004-05-11 00:58:56 +0000 | [diff] [blame] | 3258 | }else{ |
drh | fa1a98a | 2004-05-14 19:08:17 +0000 | [diff] [blame] | 3259 | nLocal = pCur->info.nLocal; |
drh | e51c44f | 2004-05-30 20:46:09 +0000 | [diff] [blame] | 3260 | if( nLocal>nKey ){ |
| 3261 | nLocal = nKey; |
| 3262 | } |
drh | 0e1c19e | 2004-05-11 00:58:56 +0000 | [diff] [blame] | 3263 | } |
drh | e51c44f | 2004-05-30 20:46:09 +0000 | [diff] [blame] | 3264 | *pAmt = nLocal; |
drh | 0e1c19e | 2004-05-11 00:58:56 +0000 | [diff] [blame] | 3265 | return aPayload; |
| 3266 | } |
| 3267 | |
| 3268 | |
| 3269 | /* |
drh | e51c44f | 2004-05-30 20:46:09 +0000 | [diff] [blame] | 3270 | ** For the entry that cursor pCur is point to, return as |
| 3271 | ** many bytes of the key or data as are available on the local |
| 3272 | ** b-tree page. Write the number of available bytes into *pAmt. |
drh | 0e1c19e | 2004-05-11 00:58:56 +0000 | [diff] [blame] | 3273 | ** |
| 3274 | ** The pointer returned is ephemeral. The key/data may move |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 3275 | ** or be destroyed on the next call to any Btree routine, |
| 3276 | ** including calls from other threads against the same cache. |
| 3277 | ** Hence, a mutex on the BtShared should be held prior to calling |
| 3278 | ** this routine. |
drh | 0e1c19e | 2004-05-11 00:58:56 +0000 | [diff] [blame] | 3279 | ** |
| 3280 | ** These routines is used to get quick access to key and data |
| 3281 | ** in the common case where no overflow pages are used. |
drh | 0e1c19e | 2004-05-11 00:58:56 +0000 | [diff] [blame] | 3282 | */ |
drh | e51c44f | 2004-05-30 20:46:09 +0000 | [diff] [blame] | 3283 | const void *sqlite3BtreeKeyFetch(BtCursor *pCur, int *pAmt){ |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 3284 | assert( cursorHoldsMutex(pCur) ); |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 3285 | if( pCur->eState==CURSOR_VALID ){ |
| 3286 | return (const void*)fetchPayload(pCur, pAmt, 0); |
| 3287 | } |
| 3288 | return 0; |
drh | 0e1c19e | 2004-05-11 00:58:56 +0000 | [diff] [blame] | 3289 | } |
drh | e51c44f | 2004-05-30 20:46:09 +0000 | [diff] [blame] | 3290 | const void *sqlite3BtreeDataFetch(BtCursor *pCur, int *pAmt){ |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 3291 | assert( cursorHoldsMutex(pCur) ); |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 3292 | if( pCur->eState==CURSOR_VALID ){ |
| 3293 | return (const void*)fetchPayload(pCur, pAmt, 1); |
| 3294 | } |
| 3295 | return 0; |
drh | 0e1c19e | 2004-05-11 00:58:56 +0000 | [diff] [blame] | 3296 | } |
| 3297 | |
| 3298 | |
| 3299 | /* |
drh | 8178a75 | 2003-01-05 21:41:40 +0000 | [diff] [blame] | 3300 | ** Move the cursor down to a new child page. The newPgno argument is the |
drh | ab01f61 | 2004-05-22 02:55:23 +0000 | [diff] [blame] | 3301 | ** page number of the child page to move to. |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 3302 | */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3303 | static int moveToChild(BtCursor *pCur, u32 newPgno){ |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 3304 | int rc; |
| 3305 | MemPage *pNewPage; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3306 | MemPage *pOldPage; |
drh | d0679ed | 2007-08-28 22:24:34 +0000 | [diff] [blame] | 3307 | BtShared *pBt = pCur->pBt; |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 3308 | |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 3309 | assert( cursorHoldsMutex(pCur) ); |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 3310 | assert( pCur->eState==CURSOR_VALID ); |
drh | de64713 | 2004-05-07 17:57:49 +0000 | [diff] [blame] | 3311 | rc = getAndInitPage(pBt, newPgno, &pNewPage, pCur->pPage); |
drh | 6019e16 | 2001-07-02 17:51:45 +0000 | [diff] [blame] | 3312 | if( rc ) return rc; |
drh | 428ae8c | 2003-01-04 16:48:09 +0000 | [diff] [blame] | 3313 | pNewPage->idxParent = pCur->idx; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3314 | pOldPage = pCur->pPage; |
| 3315 | pOldPage->idxShift = 0; |
| 3316 | releasePage(pOldPage); |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 3317 | pCur->pPage = pNewPage; |
| 3318 | pCur->idx = 0; |
drh | 271efa5 | 2004-05-30 19:19:05 +0000 | [diff] [blame] | 3319 | pCur->info.nSize = 0; |
drh | 4be295b | 2003-12-16 03:44:47 +0000 | [diff] [blame] | 3320 | if( pNewPage->nCell<1 ){ |
drh | 4928570 | 2005-09-17 15:20:26 +0000 | [diff] [blame] | 3321 | return SQLITE_CORRUPT_BKPT; |
drh | 4be295b | 2003-12-16 03:44:47 +0000 | [diff] [blame] | 3322 | } |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 3323 | return SQLITE_OK; |
| 3324 | } |
| 3325 | |
| 3326 | /* |
drh | 8856d6a | 2004-04-29 14:42:46 +0000 | [diff] [blame] | 3327 | ** Return true if the page is the virtual root of its table. |
| 3328 | ** |
| 3329 | ** The virtual root page is the root page for most tables. But |
| 3330 | ** for the table rooted on page 1, sometime the real root page |
| 3331 | ** is empty except for the right-pointer. In such cases the |
| 3332 | ** virtual root page is the page that the right-pointer of page |
| 3333 | ** 1 is pointing to. |
| 3334 | */ |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 3335 | int sqlite3BtreeIsRootPage(MemPage *pPage){ |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 3336 | MemPage *pParent; |
| 3337 | |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 3338 | assert( sqlite3_mutex_held(pPage->pBt->mutex) ); |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 3339 | pParent = pPage->pParent; |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 3340 | if( pParent==0 ) return 1; |
| 3341 | if( pParent->pgno>1 ) return 0; |
| 3342 | if( get2byte(&pParent->aData[pParent->hdrOffset+3])==0 ) return 1; |
drh | 8856d6a | 2004-04-29 14:42:46 +0000 | [diff] [blame] | 3343 | return 0; |
| 3344 | } |
| 3345 | |
| 3346 | /* |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 3347 | ** Move the cursor up to the parent page. |
| 3348 | ** |
| 3349 | ** pCur->idx is set to the cell index that contains the pointer |
| 3350 | ** to the page we are coming from. If we are coming from the |
| 3351 | ** right-most child page then pCur->idx is set to one more than |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 3352 | ** the largest cell index. |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 3353 | */ |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 3354 | void sqlite3BtreeMoveToParent(BtCursor *pCur){ |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 3355 | MemPage *pParent; |
drh | 8178a75 | 2003-01-05 21:41:40 +0000 | [diff] [blame] | 3356 | MemPage *pPage; |
drh | 428ae8c | 2003-01-04 16:48:09 +0000 | [diff] [blame] | 3357 | int idxParent; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3358 | |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 3359 | assert( cursorHoldsMutex(pCur) ); |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 3360 | assert( pCur->eState==CURSOR_VALID ); |
drh | 8178a75 | 2003-01-05 21:41:40 +0000 | [diff] [blame] | 3361 | pPage = pCur->pPage; |
| 3362 | assert( pPage!=0 ); |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 3363 | assert( !sqlite3BtreeIsRootPage(pPage) ); |
drh | 8178a75 | 2003-01-05 21:41:40 +0000 | [diff] [blame] | 3364 | pParent = pPage->pParent; |
| 3365 | assert( pParent!=0 ); |
| 3366 | idxParent = pPage->idxParent; |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 3367 | sqlite3PagerRef(pParent->pDbPage); |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3368 | releasePage(pPage); |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 3369 | pCur->pPage = pParent; |
drh | 271efa5 | 2004-05-30 19:19:05 +0000 | [diff] [blame] | 3370 | pCur->info.nSize = 0; |
drh | 428ae8c | 2003-01-04 16:48:09 +0000 | [diff] [blame] | 3371 | assert( pParent->idxShift==0 ); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 3372 | pCur->idx = idxParent; |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 3373 | } |
| 3374 | |
| 3375 | /* |
| 3376 | ** Move the cursor to the root page |
| 3377 | */ |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 3378 | static int moveToRoot(BtCursor *pCur){ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3379 | MemPage *pRoot; |
drh | 777e4c4 | 2006-01-13 04:31:58 +0000 | [diff] [blame] | 3380 | int rc = SQLITE_OK; |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 3381 | Btree *p = pCur->pBtree; |
| 3382 | BtShared *pBt = p->pBt; |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 3383 | |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 3384 | assert( cursorHoldsMutex(pCur) ); |
drh | fb98264 | 2007-08-30 01:19:59 +0000 | [diff] [blame] | 3385 | assert( CURSOR_INVALID < CURSOR_REQUIRESEEK ); |
| 3386 | assert( CURSOR_VALID < CURSOR_REQUIRESEEK ); |
| 3387 | assert( CURSOR_FAULT > CURSOR_REQUIRESEEK ); |
| 3388 | if( pCur->eState>=CURSOR_REQUIRESEEK ){ |
| 3389 | if( pCur->eState==CURSOR_FAULT ){ |
| 3390 | return pCur->skip; |
| 3391 | } |
drh | bf700f3 | 2007-03-31 02:36:44 +0000 | [diff] [blame] | 3392 | clearCursorPosition(pCur); |
| 3393 | } |
drh | 777e4c4 | 2006-01-13 04:31:58 +0000 | [diff] [blame] | 3394 | pRoot = pCur->pPage; |
danielk1977 | 97a227c | 2006-01-20 16:32:04 +0000 | [diff] [blame] | 3395 | if( pRoot && pRoot->pgno==pCur->pgnoRoot ){ |
drh | 777e4c4 | 2006-01-13 04:31:58 +0000 | [diff] [blame] | 3396 | assert( pRoot->isInit ); |
| 3397 | }else{ |
| 3398 | if( |
| 3399 | SQLITE_OK!=(rc = getAndInitPage(pBt, pCur->pgnoRoot, &pRoot, 0)) |
| 3400 | ){ |
| 3401 | pCur->eState = CURSOR_INVALID; |
| 3402 | return rc; |
| 3403 | } |
| 3404 | releasePage(pCur->pPage); |
drh | 777e4c4 | 2006-01-13 04:31:58 +0000 | [diff] [blame] | 3405 | pCur->pPage = pRoot; |
drh | c39e000 | 2004-05-07 23:50:57 +0000 | [diff] [blame] | 3406 | } |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 3407 | pCur->idx = 0; |
drh | 271efa5 | 2004-05-30 19:19:05 +0000 | [diff] [blame] | 3408 | pCur->info.nSize = 0; |
drh | 8856d6a | 2004-04-29 14:42:46 +0000 | [diff] [blame] | 3409 | if( pRoot->nCell==0 && !pRoot->leaf ){ |
| 3410 | Pgno subpage; |
| 3411 | assert( pRoot->pgno==1 ); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 3412 | subpage = get4byte(&pRoot->aData[pRoot->hdrOffset+8]); |
drh | 8856d6a | 2004-04-29 14:42:46 +0000 | [diff] [blame] | 3413 | assert( subpage>0 ); |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 3414 | pCur->eState = CURSOR_VALID; |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 3415 | rc = moveToChild(pCur, subpage); |
drh | 8856d6a | 2004-04-29 14:42:46 +0000 | [diff] [blame] | 3416 | } |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 3417 | pCur->eState = ((pCur->pPage->nCell>0)?CURSOR_VALID:CURSOR_INVALID); |
drh | 8856d6a | 2004-04-29 14:42:46 +0000 | [diff] [blame] | 3418 | return rc; |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 3419 | } |
drh | 2af926b | 2001-05-15 00:39:25 +0000 | [diff] [blame] | 3420 | |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 3421 | /* |
| 3422 | ** Move the cursor down to the left-most leaf entry beneath the |
| 3423 | ** entry to which it is currently pointing. |
drh | 777e4c4 | 2006-01-13 04:31:58 +0000 | [diff] [blame] | 3424 | ** |
| 3425 | ** The left-most leaf is the one with the smallest key - the first |
| 3426 | ** in ascending order. |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 3427 | */ |
| 3428 | static int moveToLeftmost(BtCursor *pCur){ |
| 3429 | Pgno pgno; |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 3430 | int rc = SQLITE_OK; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3431 | MemPage *pPage; |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 3432 | |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 3433 | assert( cursorHoldsMutex(pCur) ); |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 3434 | assert( pCur->eState==CURSOR_VALID ); |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 3435 | while( rc==SQLITE_OK && !(pPage = pCur->pPage)->leaf ){ |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 3436 | assert( pCur->idx>=0 && pCur->idx<pPage->nCell ); |
danielk1977 | 1cc5ed8 | 2007-05-16 17:28:43 +0000 | [diff] [blame] | 3437 | pgno = get4byte(findCell(pPage, pCur->idx)); |
drh | 8178a75 | 2003-01-05 21:41:40 +0000 | [diff] [blame] | 3438 | rc = moveToChild(pCur, pgno); |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 3439 | } |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 3440 | return rc; |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 3441 | } |
| 3442 | |
drh | 2dcc9aa | 2002-12-04 13:40:25 +0000 | [diff] [blame] | 3443 | /* |
| 3444 | ** Move the cursor down to the right-most leaf entry beneath the |
| 3445 | ** page to which it is currently pointing. Notice the difference |
| 3446 | ** between moveToLeftmost() and moveToRightmost(). moveToLeftmost() |
| 3447 | ** finds the left-most entry beneath the *entry* whereas moveToRightmost() |
| 3448 | ** finds the right-most entry beneath the *page*. |
drh | 777e4c4 | 2006-01-13 04:31:58 +0000 | [diff] [blame] | 3449 | ** |
| 3450 | ** The right-most entry is the one with the largest key - the last |
| 3451 | ** key in ascending order. |
drh | 2dcc9aa | 2002-12-04 13:40:25 +0000 | [diff] [blame] | 3452 | */ |
| 3453 | static int moveToRightmost(BtCursor *pCur){ |
| 3454 | Pgno pgno; |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 3455 | int rc = SQLITE_OK; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3456 | MemPage *pPage; |
drh | 2dcc9aa | 2002-12-04 13:40:25 +0000 | [diff] [blame] | 3457 | |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 3458 | assert( cursorHoldsMutex(pCur) ); |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 3459 | assert( pCur->eState==CURSOR_VALID ); |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 3460 | while( rc==SQLITE_OK && !(pPage = pCur->pPage)->leaf ){ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 3461 | pgno = get4byte(&pPage->aData[pPage->hdrOffset+8]); |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3462 | pCur->idx = pPage->nCell; |
drh | 8178a75 | 2003-01-05 21:41:40 +0000 | [diff] [blame] | 3463 | rc = moveToChild(pCur, pgno); |
drh | 2dcc9aa | 2002-12-04 13:40:25 +0000 | [diff] [blame] | 3464 | } |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 3465 | if( rc==SQLITE_OK ){ |
| 3466 | pCur->idx = pPage->nCell - 1; |
| 3467 | pCur->info.nSize = 0; |
| 3468 | } |
drh | 2dcc9aa | 2002-12-04 13:40:25 +0000 | [diff] [blame] | 3469 | return SQLITE_OK; |
| 3470 | } |
| 3471 | |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3472 | /* Move the cursor to the first entry in the table. Return SQLITE_OK |
| 3473 | ** on success. Set *pRes to 0 if the cursor actually points to something |
drh | 77c679c | 2002-02-19 22:43:58 +0000 | [diff] [blame] | 3474 | ** or set *pRes to 1 if the table is empty. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3475 | */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3476 | int sqlite3BtreeFirst(BtCursor *pCur, int *pRes){ |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3477 | int rc; |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 3478 | |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 3479 | assert( cursorHoldsMutex(pCur) ); |
| 3480 | assert( sqlite3_mutex_held(pCur->pBtree->pSqlite->mutex) ); |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3481 | rc = moveToRoot(pCur); |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 3482 | if( rc==SQLITE_OK ){ |
| 3483 | if( pCur->eState==CURSOR_INVALID ){ |
| 3484 | assert( pCur->pPage->nCell==0 ); |
| 3485 | *pRes = 1; |
| 3486 | rc = SQLITE_OK; |
| 3487 | }else{ |
| 3488 | assert( pCur->pPage->nCell>0 ); |
| 3489 | *pRes = 0; |
| 3490 | rc = moveToLeftmost(pCur); |
| 3491 | } |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3492 | } |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3493 | return rc; |
| 3494 | } |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 3495 | |
drh | 9562b55 | 2002-02-19 15:00:07 +0000 | [diff] [blame] | 3496 | /* Move the cursor to the last entry in the table. Return SQLITE_OK |
| 3497 | ** on success. Set *pRes to 0 if the cursor actually points to something |
drh | 77c679c | 2002-02-19 22:43:58 +0000 | [diff] [blame] | 3498 | ** or set *pRes to 1 if the table is empty. |
drh | 9562b55 | 2002-02-19 15:00:07 +0000 | [diff] [blame] | 3499 | */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3500 | int sqlite3BtreeLast(BtCursor *pCur, int *pRes){ |
drh | 9562b55 | 2002-02-19 15:00:07 +0000 | [diff] [blame] | 3501 | int rc; |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 3502 | |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 3503 | assert( cursorHoldsMutex(pCur) ); |
| 3504 | assert( sqlite3_mutex_held(pCur->pBtree->pSqlite->mutex) ); |
drh | 9562b55 | 2002-02-19 15:00:07 +0000 | [diff] [blame] | 3505 | rc = moveToRoot(pCur); |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 3506 | if( rc==SQLITE_OK ){ |
| 3507 | if( CURSOR_INVALID==pCur->eState ){ |
| 3508 | assert( pCur->pPage->nCell==0 ); |
| 3509 | *pRes = 1; |
| 3510 | }else{ |
| 3511 | assert( pCur->eState==CURSOR_VALID ); |
| 3512 | *pRes = 0; |
| 3513 | rc = moveToRightmost(pCur); |
| 3514 | } |
drh | 9562b55 | 2002-02-19 15:00:07 +0000 | [diff] [blame] | 3515 | } |
drh | 9562b55 | 2002-02-19 15:00:07 +0000 | [diff] [blame] | 3516 | return rc; |
| 3517 | } |
| 3518 | |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3519 | /* Move the cursor so that it points to an entry near pKey/nKey. |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 3520 | ** Return a success code. |
| 3521 | ** |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3522 | ** For INTKEY tables, only the nKey parameter is used. pKey is |
| 3523 | ** ignored. For other tables, nKey is the number of bytes of data |
drh | 0b2f316 | 2005-12-21 18:36:45 +0000 | [diff] [blame] | 3524 | ** in pKey. The comparison function specified when the cursor was |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3525 | ** created is used to compare keys. |
| 3526 | ** |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 3527 | ** If an exact match is not found, then the cursor is always |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 3528 | ** left pointing at a leaf page which would hold the entry if it |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 3529 | ** were present. The cursor might point to an entry that comes |
| 3530 | ** before or after the key. |
| 3531 | ** |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 3532 | ** The result of comparing the key with the entry to which the |
drh | ab01f61 | 2004-05-22 02:55:23 +0000 | [diff] [blame] | 3533 | ** cursor is written to *pRes if pRes!=NULL. The meaning of |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 3534 | ** this value is as follows: |
| 3535 | ** |
| 3536 | ** *pRes<0 The cursor is left pointing at an entry that |
drh | 1a844c3 | 2002-12-04 22:29:28 +0000 | [diff] [blame] | 3537 | ** is smaller than pKey or if the table is empty |
| 3538 | ** and the cursor is therefore left point to nothing. |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 3539 | ** |
| 3540 | ** *pRes==0 The cursor is left pointing at an entry that |
| 3541 | ** exactly matches pKey. |
| 3542 | ** |
| 3543 | ** *pRes>0 The cursor is left pointing at an entry that |
drh | 7c717f7 | 2001-06-24 20:39:41 +0000 | [diff] [blame] | 3544 | ** is larger than pKey. |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 3545 | ** |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 3546 | */ |
drh | e4d9081 | 2007-03-29 05:51:49 +0000 | [diff] [blame] | 3547 | int sqlite3BtreeMoveto( |
| 3548 | BtCursor *pCur, /* The cursor to be moved */ |
| 3549 | const void *pKey, /* The key content for indices. Not used by tables */ |
| 3550 | i64 nKey, /* Size of pKey. Or the key for tables */ |
| 3551 | int biasRight, /* If true, bias the search to the high end */ |
| 3552 | int *pRes /* Search result flag */ |
| 3553 | ){ |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 3554 | int rc; |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 3555 | |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 3556 | assert( cursorHoldsMutex(pCur) ); |
| 3557 | assert( sqlite3_mutex_held(pCur->pBtree->pSqlite->mutex) ); |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 3558 | rc = moveToRoot(pCur); |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 3559 | if( rc ){ |
| 3560 | return rc; |
| 3561 | } |
drh | c39e000 | 2004-05-07 23:50:57 +0000 | [diff] [blame] | 3562 | assert( pCur->pPage ); |
| 3563 | assert( pCur->pPage->isInit ); |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 3564 | if( pCur->eState==CURSOR_INVALID ){ |
drh | f328bc8 | 2004-05-10 23:29:49 +0000 | [diff] [blame] | 3565 | *pRes = -1; |
drh | c39e000 | 2004-05-07 23:50:57 +0000 | [diff] [blame] | 3566 | assert( pCur->pPage->nCell==0 ); |
| 3567 | return SQLITE_OK; |
| 3568 | } |
drh | 1468438 | 2006-11-30 13:05:29 +0000 | [diff] [blame] | 3569 | for(;;){ |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 3570 | int lwr, upr; |
| 3571 | Pgno chldPg; |
| 3572 | MemPage *pPage = pCur->pPage; |
drh | 1a844c3 | 2002-12-04 22:29:28 +0000 | [diff] [blame] | 3573 | int c = -1; /* pRes return if table is empty must be -1 */ |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 3574 | lwr = 0; |
| 3575 | upr = pPage->nCell-1; |
drh | 4eec4c1 | 2005-01-21 00:22:37 +0000 | [diff] [blame] | 3576 | if( !pPage->intKey && pKey==0 ){ |
drh | 4928570 | 2005-09-17 15:20:26 +0000 | [diff] [blame] | 3577 | return SQLITE_CORRUPT_BKPT; |
drh | 4eec4c1 | 2005-01-21 00:22:37 +0000 | [diff] [blame] | 3578 | } |
drh | e4d9081 | 2007-03-29 05:51:49 +0000 | [diff] [blame] | 3579 | if( biasRight ){ |
| 3580 | pCur->idx = upr; |
| 3581 | }else{ |
| 3582 | pCur->idx = (upr+lwr)/2; |
| 3583 | } |
drh | f1d68b3 | 2007-03-29 04:43:26 +0000 | [diff] [blame] | 3584 | if( lwr<=upr ) for(;;){ |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 3585 | void *pCellKey; |
drh | 4a1c380 | 2004-05-12 15:15:47 +0000 | [diff] [blame] | 3586 | i64 nCellKey; |
drh | 366fda6 | 2006-01-13 02:35:09 +0000 | [diff] [blame] | 3587 | pCur->info.nSize = 0; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3588 | if( pPage->intKey ){ |
drh | 777e4c4 | 2006-01-13 04:31:58 +0000 | [diff] [blame] | 3589 | u8 *pCell; |
danielk1977 | 1cc5ed8 | 2007-05-16 17:28:43 +0000 | [diff] [blame] | 3590 | pCell = findCell(pPage, pCur->idx) + pPage->childPtrSize; |
drh | d172f86 | 2006-01-12 15:01:15 +0000 | [diff] [blame] | 3591 | if( pPage->hasData ){ |
danielk1977 | bab45c6 | 2006-01-16 15:14:27 +0000 | [diff] [blame] | 3592 | u32 dummy; |
drh | d172f86 | 2006-01-12 15:01:15 +0000 | [diff] [blame] | 3593 | pCell += getVarint32(pCell, &dummy); |
| 3594 | } |
danielk1977 | bab45c6 | 2006-01-16 15:14:27 +0000 | [diff] [blame] | 3595 | getVarint(pCell, (u64 *)&nCellKey); |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3596 | if( nCellKey<nKey ){ |
| 3597 | c = -1; |
| 3598 | }else if( nCellKey>nKey ){ |
| 3599 | c = +1; |
| 3600 | }else{ |
| 3601 | c = 0; |
| 3602 | } |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3603 | }else{ |
drh | e51c44f | 2004-05-30 20:46:09 +0000 | [diff] [blame] | 3604 | int available; |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 3605 | pCellKey = (void *)fetchPayload(pCur, &available, 0); |
drh | 366fda6 | 2006-01-13 02:35:09 +0000 | [diff] [blame] | 3606 | nCellKey = pCur->info.nKey; |
drh | e51c44f | 2004-05-30 20:46:09 +0000 | [diff] [blame] | 3607 | if( available>=nCellKey ){ |
| 3608 | c = pCur->xCompare(pCur->pArg, nCellKey, pCellKey, nKey, pKey); |
| 3609 | }else{ |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 3610 | pCellKey = sqlite3_malloc( nCellKey ); |
drh | e51c44f | 2004-05-30 20:46:09 +0000 | [diff] [blame] | 3611 | if( pCellKey==0 ) return SQLITE_NOMEM; |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 3612 | rc = sqlite3BtreeKey(pCur, 0, nCellKey, (void *)pCellKey); |
drh | e51c44f | 2004-05-30 20:46:09 +0000 | [diff] [blame] | 3613 | c = pCur->xCompare(pCur->pArg, nCellKey, pCellKey, nKey, pKey); |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 3614 | sqlite3_free(pCellKey); |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 3615 | if( rc ){ |
| 3616 | return rc; |
| 3617 | } |
drh | e51c44f | 2004-05-30 20:46:09 +0000 | [diff] [blame] | 3618 | } |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3619 | } |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 3620 | if( c==0 ){ |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 3621 | if( pPage->leafData && !pPage->leaf ){ |
drh | fc70e6f | 2004-05-12 21:11:27 +0000 | [diff] [blame] | 3622 | lwr = pCur->idx; |
| 3623 | upr = lwr - 1; |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 3624 | break; |
| 3625 | }else{ |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 3626 | if( pRes ) *pRes = 0; |
| 3627 | return SQLITE_OK; |
| 3628 | } |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 3629 | } |
| 3630 | if( c<0 ){ |
| 3631 | lwr = pCur->idx+1; |
| 3632 | }else{ |
| 3633 | upr = pCur->idx-1; |
| 3634 | } |
drh | f1d68b3 | 2007-03-29 04:43:26 +0000 | [diff] [blame] | 3635 | if( lwr>upr ){ |
| 3636 | break; |
| 3637 | } |
| 3638 | pCur->idx = (lwr+upr)/2; |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 3639 | } |
| 3640 | assert( lwr==upr+1 ); |
drh | 7aa128d | 2002-06-21 13:09:16 +0000 | [diff] [blame] | 3641 | assert( pPage->isInit ); |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3642 | if( pPage->leaf ){ |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 3643 | chldPg = 0; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3644 | }else if( lwr>=pPage->nCell ){ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 3645 | chldPg = get4byte(&pPage->aData[pPage->hdrOffset+8]); |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 3646 | }else{ |
danielk1977 | 1cc5ed8 | 2007-05-16 17:28:43 +0000 | [diff] [blame] | 3647 | chldPg = get4byte(findCell(pPage, lwr)); |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 3648 | } |
| 3649 | if( chldPg==0 ){ |
drh | c39e000 | 2004-05-07 23:50:57 +0000 | [diff] [blame] | 3650 | assert( pCur->idx>=0 && pCur->idx<pCur->pPage->nCell ); |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 3651 | if( pRes ) *pRes = c; |
| 3652 | return SQLITE_OK; |
| 3653 | } |
drh | 428ae8c | 2003-01-04 16:48:09 +0000 | [diff] [blame] | 3654 | pCur->idx = lwr; |
drh | 271efa5 | 2004-05-30 19:19:05 +0000 | [diff] [blame] | 3655 | pCur->info.nSize = 0; |
drh | 8178a75 | 2003-01-05 21:41:40 +0000 | [diff] [blame] | 3656 | rc = moveToChild(pCur, chldPg); |
drh | c39e000 | 2004-05-07 23:50:57 +0000 | [diff] [blame] | 3657 | if( rc ){ |
| 3658 | return rc; |
| 3659 | } |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 3660 | } |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 3661 | /* NOT REACHED */ |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 3662 | } |
| 3663 | |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 3664 | |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 3665 | /* |
drh | c39e000 | 2004-05-07 23:50:57 +0000 | [diff] [blame] | 3666 | ** Return TRUE if the cursor is not pointing at an entry of the table. |
| 3667 | ** |
| 3668 | ** TRUE will be returned after a call to sqlite3BtreeNext() moves |
| 3669 | ** past the last entry in the table or sqlite3BtreePrev() moves past |
| 3670 | ** the first entry. TRUE is also returned if the table is empty. |
| 3671 | */ |
| 3672 | int sqlite3BtreeEof(BtCursor *pCur){ |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 3673 | /* TODO: What if the cursor is in CURSOR_REQUIRESEEK but all table entries |
| 3674 | ** have been deleted? This API will need to change to return an error code |
| 3675 | ** as well as the boolean result value. |
| 3676 | */ |
| 3677 | return (CURSOR_VALID!=pCur->eState); |
drh | c39e000 | 2004-05-07 23:50:57 +0000 | [diff] [blame] | 3678 | } |
| 3679 | |
| 3680 | /* |
drh | b21c8cd | 2007-08-21 19:33:56 +0000 | [diff] [blame] | 3681 | ** Return the database connection handle for a cursor. |
| 3682 | */ |
| 3683 | sqlite3 *sqlite3BtreeCursorDb(const BtCursor *pCur){ |
drh | d0679ed | 2007-08-28 22:24:34 +0000 | [diff] [blame] | 3684 | assert( sqlite3_mutex_held(pCur->pBtree->pSqlite->mutex) ); |
drh | b21c8cd | 2007-08-21 19:33:56 +0000 | [diff] [blame] | 3685 | return pCur->pBtree->pSqlite; |
| 3686 | } |
| 3687 | |
| 3688 | /* |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 3689 | ** Advance the cursor to the next entry in the database. If |
drh | 8c1238a | 2003-01-02 14:43:55 +0000 | [diff] [blame] | 3690 | ** successful then set *pRes=0. If the cursor |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 3691 | ** was already pointing to the last entry in the database before |
drh | 8c1238a | 2003-01-02 14:43:55 +0000 | [diff] [blame] | 3692 | ** this routine was called, then set *pRes=1. |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 3693 | */ |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 3694 | static int btreeNext(BtCursor *pCur, int *pRes){ |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 3695 | int rc; |
danielk1977 | 97a227c | 2006-01-20 16:32:04 +0000 | [diff] [blame] | 3696 | MemPage *pPage; |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 3697 | |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 3698 | assert( cursorHoldsMutex(pCur) ); |
drh | bf700f3 | 2007-03-31 02:36:44 +0000 | [diff] [blame] | 3699 | rc = restoreOrClearCursorPosition(pCur); |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 3700 | if( rc!=SQLITE_OK ){ |
| 3701 | return rc; |
| 3702 | } |
drh | 8c4d3a6 | 2007-04-06 01:03:32 +0000 | [diff] [blame] | 3703 | assert( pRes!=0 ); |
| 3704 | pPage = pCur->pPage; |
| 3705 | if( CURSOR_INVALID==pCur->eState ){ |
| 3706 | *pRes = 1; |
| 3707 | return SQLITE_OK; |
| 3708 | } |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 3709 | if( pCur->skip>0 ){ |
| 3710 | pCur->skip = 0; |
| 3711 | *pRes = 0; |
| 3712 | return SQLITE_OK; |
| 3713 | } |
| 3714 | pCur->skip = 0; |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 3715 | |
drh | 8178a75 | 2003-01-05 21:41:40 +0000 | [diff] [blame] | 3716 | assert( pPage->isInit ); |
drh | 8178a75 | 2003-01-05 21:41:40 +0000 | [diff] [blame] | 3717 | assert( pCur->idx<pPage->nCell ); |
danielk1977 | 6a43f9b | 2004-11-16 04:57:24 +0000 | [diff] [blame] | 3718 | |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 3719 | pCur->idx++; |
drh | 271efa5 | 2004-05-30 19:19:05 +0000 | [diff] [blame] | 3720 | pCur->info.nSize = 0; |
drh | 8178a75 | 2003-01-05 21:41:40 +0000 | [diff] [blame] | 3721 | if( pCur->idx>=pPage->nCell ){ |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 3722 | if( !pPage->leaf ){ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 3723 | rc = moveToChild(pCur, get4byte(&pPage->aData[pPage->hdrOffset+8])); |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 3724 | if( rc ) return rc; |
| 3725 | rc = moveToLeftmost(pCur); |
drh | 8c1238a | 2003-01-02 14:43:55 +0000 | [diff] [blame] | 3726 | *pRes = 0; |
| 3727 | return rc; |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 3728 | } |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 3729 | do{ |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 3730 | if( sqlite3BtreeIsRootPage(pPage) ){ |
drh | 8c1238a | 2003-01-02 14:43:55 +0000 | [diff] [blame] | 3731 | *pRes = 1; |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 3732 | pCur->eState = CURSOR_INVALID; |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 3733 | return SQLITE_OK; |
| 3734 | } |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 3735 | sqlite3BtreeMoveToParent(pCur); |
drh | 8178a75 | 2003-01-05 21:41:40 +0000 | [diff] [blame] | 3736 | pPage = pCur->pPage; |
| 3737 | }while( pCur->idx>=pPage->nCell ); |
drh | 8c1238a | 2003-01-02 14:43:55 +0000 | [diff] [blame] | 3738 | *pRes = 0; |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 3739 | if( pPage->leafData ){ |
| 3740 | rc = sqlite3BtreeNext(pCur, pRes); |
| 3741 | }else{ |
| 3742 | rc = SQLITE_OK; |
| 3743 | } |
| 3744 | return rc; |
drh | 8178a75 | 2003-01-05 21:41:40 +0000 | [diff] [blame] | 3745 | } |
| 3746 | *pRes = 0; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3747 | if( pPage->leaf ){ |
drh | 8178a75 | 2003-01-05 21:41:40 +0000 | [diff] [blame] | 3748 | return SQLITE_OK; |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 3749 | } |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 3750 | rc = moveToLeftmost(pCur); |
drh | 8c1238a | 2003-01-02 14:43:55 +0000 | [diff] [blame] | 3751 | return rc; |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 3752 | } |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 3753 | int sqlite3BtreeNext(BtCursor *pCur, int *pRes){ |
| 3754 | int rc; |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 3755 | assert( cursorHoldsMutex(pCur) ); |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 3756 | rc = btreeNext(pCur, pRes); |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 3757 | return rc; |
| 3758 | } |
| 3759 | |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 3760 | |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 3761 | /* |
drh | 2dcc9aa | 2002-12-04 13:40:25 +0000 | [diff] [blame] | 3762 | ** Step the cursor to the back to the previous entry in the database. If |
drh | 8178a75 | 2003-01-05 21:41:40 +0000 | [diff] [blame] | 3763 | ** successful then set *pRes=0. If the cursor |
drh | 2dcc9aa | 2002-12-04 13:40:25 +0000 | [diff] [blame] | 3764 | ** was already pointing to the first entry in the database before |
drh | 8178a75 | 2003-01-05 21:41:40 +0000 | [diff] [blame] | 3765 | ** this routine was called, then set *pRes=1. |
drh | 2dcc9aa | 2002-12-04 13:40:25 +0000 | [diff] [blame] | 3766 | */ |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 3767 | static int btreePrevious(BtCursor *pCur, int *pRes){ |
drh | 2dcc9aa | 2002-12-04 13:40:25 +0000 | [diff] [blame] | 3768 | int rc; |
| 3769 | Pgno pgno; |
drh | 8178a75 | 2003-01-05 21:41:40 +0000 | [diff] [blame] | 3770 | MemPage *pPage; |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 3771 | |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 3772 | assert( cursorHoldsMutex(pCur) ); |
drh | bf700f3 | 2007-03-31 02:36:44 +0000 | [diff] [blame] | 3773 | rc = restoreOrClearCursorPosition(pCur); |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 3774 | if( rc!=SQLITE_OK ){ |
| 3775 | return rc; |
| 3776 | } |
drh | 8c4d3a6 | 2007-04-06 01:03:32 +0000 | [diff] [blame] | 3777 | if( CURSOR_INVALID==pCur->eState ){ |
| 3778 | *pRes = 1; |
| 3779 | return SQLITE_OK; |
| 3780 | } |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 3781 | if( pCur->skip<0 ){ |
| 3782 | pCur->skip = 0; |
| 3783 | *pRes = 0; |
| 3784 | return SQLITE_OK; |
| 3785 | } |
| 3786 | pCur->skip = 0; |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 3787 | |
drh | 8178a75 | 2003-01-05 21:41:40 +0000 | [diff] [blame] | 3788 | pPage = pCur->pPage; |
drh | 8178a75 | 2003-01-05 21:41:40 +0000 | [diff] [blame] | 3789 | assert( pPage->isInit ); |
drh | 2dcc9aa | 2002-12-04 13:40:25 +0000 | [diff] [blame] | 3790 | assert( pCur->idx>=0 ); |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 3791 | if( !pPage->leaf ){ |
danielk1977 | 1cc5ed8 | 2007-05-16 17:28:43 +0000 | [diff] [blame] | 3792 | pgno = get4byte( findCell(pPage, pCur->idx) ); |
drh | 8178a75 | 2003-01-05 21:41:40 +0000 | [diff] [blame] | 3793 | rc = moveToChild(pCur, pgno); |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 3794 | if( rc ){ |
| 3795 | return rc; |
| 3796 | } |
drh | 2dcc9aa | 2002-12-04 13:40:25 +0000 | [diff] [blame] | 3797 | rc = moveToRightmost(pCur); |
| 3798 | }else{ |
| 3799 | while( pCur->idx==0 ){ |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 3800 | if( sqlite3BtreeIsRootPage(pPage) ){ |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 3801 | pCur->eState = CURSOR_INVALID; |
drh | c39e000 | 2004-05-07 23:50:57 +0000 | [diff] [blame] | 3802 | *pRes = 1; |
drh | 2dcc9aa | 2002-12-04 13:40:25 +0000 | [diff] [blame] | 3803 | return SQLITE_OK; |
| 3804 | } |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 3805 | sqlite3BtreeMoveToParent(pCur); |
drh | 8178a75 | 2003-01-05 21:41:40 +0000 | [diff] [blame] | 3806 | pPage = pCur->pPage; |
drh | 2dcc9aa | 2002-12-04 13:40:25 +0000 | [diff] [blame] | 3807 | } |
| 3808 | pCur->idx--; |
drh | 271efa5 | 2004-05-30 19:19:05 +0000 | [diff] [blame] | 3809 | pCur->info.nSize = 0; |
drh | 8237d45 | 2004-11-22 19:07:09 +0000 | [diff] [blame] | 3810 | if( pPage->leafData && !pPage->leaf ){ |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 3811 | rc = sqlite3BtreePrevious(pCur, pRes); |
| 3812 | }else{ |
| 3813 | rc = SQLITE_OK; |
| 3814 | } |
drh | 2dcc9aa | 2002-12-04 13:40:25 +0000 | [diff] [blame] | 3815 | } |
drh | 8178a75 | 2003-01-05 21:41:40 +0000 | [diff] [blame] | 3816 | *pRes = 0; |
drh | 2dcc9aa | 2002-12-04 13:40:25 +0000 | [diff] [blame] | 3817 | return rc; |
| 3818 | } |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 3819 | int sqlite3BtreePrevious(BtCursor *pCur, int *pRes){ |
| 3820 | int rc; |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 3821 | assert( cursorHoldsMutex(pCur) ); |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 3822 | rc = btreePrevious(pCur, pRes); |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 3823 | return rc; |
| 3824 | } |
drh | 2dcc9aa | 2002-12-04 13:40:25 +0000 | [diff] [blame] | 3825 | |
| 3826 | /* |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 3827 | ** Allocate a new page from the database file. |
| 3828 | ** |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 3829 | ** The new page is marked as dirty. (In other words, sqlite3PagerWrite() |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 3830 | ** has already been called on the new page.) The new page has also |
| 3831 | ** been referenced and the calling routine is responsible for calling |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 3832 | ** sqlite3PagerUnref() on the new page when it is done. |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 3833 | ** |
| 3834 | ** SQLITE_OK is returned on success. Any other return value indicates |
| 3835 | ** an error. *ppPage and *pPgno are undefined in the event of an error. |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 3836 | ** Do not invoke sqlite3PagerUnref() on *ppPage if an error is returned. |
drh | bea00b9 | 2002-07-08 10:59:50 +0000 | [diff] [blame] | 3837 | ** |
drh | 199e3cf | 2002-07-18 11:01:47 +0000 | [diff] [blame] | 3838 | ** If the "nearby" parameter is not 0, then a (feeble) effort is made to |
| 3839 | ** locate a page close to the page number "nearby". This can be used in an |
drh | bea00b9 | 2002-07-08 10:59:50 +0000 | [diff] [blame] | 3840 | ** attempt to keep related pages close to each other in the database file, |
| 3841 | ** which in turn can make database access faster. |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 3842 | ** |
| 3843 | ** If the "exact" parameter is not 0, and the page-number nearby exists |
| 3844 | ** anywhere on the free-list, then it is guarenteed to be returned. This |
| 3845 | ** is only used by auto-vacuum databases when allocating a new table. |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 3846 | */ |
drh | 4f0c587 | 2007-03-26 22:05:01 +0000 | [diff] [blame] | 3847 | static int allocateBtreePage( |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 3848 | BtShared *pBt, |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 3849 | MemPage **ppPage, |
| 3850 | Pgno *pPgno, |
| 3851 | Pgno nearby, |
| 3852 | u8 exact |
| 3853 | ){ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3854 | MemPage *pPage1; |
drh | 8c42ca9 | 2001-06-22 19:15:00 +0000 | [diff] [blame] | 3855 | int rc; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3856 | int n; /* Number of pages on the freelist */ |
| 3857 | int k; /* Number of leaves on the trunk of the freelist */ |
drh | d3627af | 2006-12-18 18:34:51 +0000 | [diff] [blame] | 3858 | MemPage *pTrunk = 0; |
| 3859 | MemPage *pPrevTrunk = 0; |
drh | 30e5875 | 2002-03-02 20:41:57 +0000 | [diff] [blame] | 3860 | |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 3861 | assert( sqlite3_mutex_held(pBt->mutex) ); |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3862 | pPage1 = pBt->pPage1; |
| 3863 | n = get4byte(&pPage1->aData[36]); |
| 3864 | if( n>0 ){ |
drh | 9102529 | 2004-05-03 19:49:32 +0000 | [diff] [blame] | 3865 | /* There are pages on the freelist. Reuse one of those pages. */ |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 3866 | Pgno iTrunk; |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 3867 | u8 searchList = 0; /* If the free-list must be searched for 'nearby' */ |
| 3868 | |
| 3869 | /* If the 'exact' parameter was true and a query of the pointer-map |
| 3870 | ** shows that the page 'nearby' is somewhere on the free-list, then |
| 3871 | ** the entire-list will be searched for that page. |
| 3872 | */ |
| 3873 | #ifndef SQLITE_OMIT_AUTOVACUUM |
danielk1977 | 4ef2449 | 2007-05-23 09:52:41 +0000 | [diff] [blame] | 3874 | if( exact && nearby<=sqlite3PagerPagecount(pBt->pPager) ){ |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 3875 | u8 eType; |
| 3876 | assert( nearby>0 ); |
| 3877 | assert( pBt->autoVacuum ); |
| 3878 | rc = ptrmapGet(pBt, nearby, &eType, 0); |
| 3879 | if( rc ) return rc; |
| 3880 | if( eType==PTRMAP_FREEPAGE ){ |
| 3881 | searchList = 1; |
| 3882 | } |
| 3883 | *pPgno = nearby; |
| 3884 | } |
| 3885 | #endif |
| 3886 | |
| 3887 | /* Decrement the free-list count by 1. Set iTrunk to the index of the |
| 3888 | ** first free-list trunk page. iPrevTrunk is initially 1. |
| 3889 | */ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 3890 | rc = sqlite3PagerWrite(pPage1->pDbPage); |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 3891 | if( rc ) return rc; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3892 | put4byte(&pPage1->aData[36], n-1); |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 3893 | |
| 3894 | /* The code within this loop is run only once if the 'searchList' variable |
| 3895 | ** is not true. Otherwise, it runs once for each trunk-page on the |
| 3896 | ** free-list until the page 'nearby' is located. |
| 3897 | */ |
| 3898 | do { |
| 3899 | pPrevTrunk = pTrunk; |
| 3900 | if( pPrevTrunk ){ |
| 3901 | iTrunk = get4byte(&pPrevTrunk->aData[0]); |
drh | bea00b9 | 2002-07-08 10:59:50 +0000 | [diff] [blame] | 3902 | }else{ |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 3903 | iTrunk = get4byte(&pPage1->aData[32]); |
drh | bea00b9 | 2002-07-08 10:59:50 +0000 | [diff] [blame] | 3904 | } |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 3905 | rc = sqlite3BtreeGetPage(pBt, iTrunk, &pTrunk, 0); |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 3906 | if( rc ){ |
drh | d3627af | 2006-12-18 18:34:51 +0000 | [diff] [blame] | 3907 | pTrunk = 0; |
| 3908 | goto end_allocate_page; |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 3909 | } |
| 3910 | |
| 3911 | k = get4byte(&pTrunk->aData[4]); |
| 3912 | if( k==0 && !searchList ){ |
| 3913 | /* The trunk has no leaves and the list is not being searched. |
| 3914 | ** So extract the trunk page itself and use it as the newly |
| 3915 | ** allocated page */ |
| 3916 | assert( pPrevTrunk==0 ); |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 3917 | rc = sqlite3PagerWrite(pTrunk->pDbPage); |
drh | d3627af | 2006-12-18 18:34:51 +0000 | [diff] [blame] | 3918 | if( rc ){ |
| 3919 | goto end_allocate_page; |
| 3920 | } |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 3921 | *pPgno = iTrunk; |
| 3922 | memcpy(&pPage1->aData[32], &pTrunk->aData[0], 4); |
| 3923 | *ppPage = pTrunk; |
| 3924 | pTrunk = 0; |
| 3925 | TRACE(("ALLOCATE: %d trunk - %d free pages left\n", *pPgno, n-1)); |
| 3926 | }else if( k>pBt->usableSize/4 - 8 ){ |
| 3927 | /* Value of k is out of range. Database corruption */ |
drh | d3627af | 2006-12-18 18:34:51 +0000 | [diff] [blame] | 3928 | rc = SQLITE_CORRUPT_BKPT; |
| 3929 | goto end_allocate_page; |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 3930 | #ifndef SQLITE_OMIT_AUTOVACUUM |
| 3931 | }else if( searchList && nearby==iTrunk ){ |
| 3932 | /* The list is being searched and this trunk page is the page |
| 3933 | ** to allocate, regardless of whether it has leaves. |
| 3934 | */ |
| 3935 | assert( *pPgno==iTrunk ); |
| 3936 | *ppPage = pTrunk; |
| 3937 | searchList = 0; |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 3938 | rc = sqlite3PagerWrite(pTrunk->pDbPage); |
drh | d3627af | 2006-12-18 18:34:51 +0000 | [diff] [blame] | 3939 | if( rc ){ |
| 3940 | goto end_allocate_page; |
| 3941 | } |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 3942 | if( k==0 ){ |
| 3943 | if( !pPrevTrunk ){ |
| 3944 | memcpy(&pPage1->aData[32], &pTrunk->aData[0], 4); |
| 3945 | }else{ |
| 3946 | memcpy(&pPrevTrunk->aData[0], &pTrunk->aData[0], 4); |
| 3947 | } |
| 3948 | }else{ |
| 3949 | /* The trunk page is required by the caller but it contains |
| 3950 | ** pointers to free-list leaves. The first leaf becomes a trunk |
| 3951 | ** page in this case. |
| 3952 | */ |
| 3953 | MemPage *pNewTrunk; |
| 3954 | Pgno iNewTrunk = get4byte(&pTrunk->aData[8]); |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 3955 | rc = sqlite3BtreeGetPage(pBt, iNewTrunk, &pNewTrunk, 0); |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 3956 | if( rc!=SQLITE_OK ){ |
drh | d3627af | 2006-12-18 18:34:51 +0000 | [diff] [blame] | 3957 | goto end_allocate_page; |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 3958 | } |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 3959 | rc = sqlite3PagerWrite(pNewTrunk->pDbPage); |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 3960 | if( rc!=SQLITE_OK ){ |
| 3961 | releasePage(pNewTrunk); |
drh | d3627af | 2006-12-18 18:34:51 +0000 | [diff] [blame] | 3962 | goto end_allocate_page; |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 3963 | } |
| 3964 | memcpy(&pNewTrunk->aData[0], &pTrunk->aData[0], 4); |
| 3965 | put4byte(&pNewTrunk->aData[4], k-1); |
| 3966 | memcpy(&pNewTrunk->aData[8], &pTrunk->aData[12], (k-1)*4); |
drh | d3627af | 2006-12-18 18:34:51 +0000 | [diff] [blame] | 3967 | releasePage(pNewTrunk); |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 3968 | if( !pPrevTrunk ){ |
| 3969 | put4byte(&pPage1->aData[32], iNewTrunk); |
| 3970 | }else{ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 3971 | rc = sqlite3PagerWrite(pPrevTrunk->pDbPage); |
drh | d3627af | 2006-12-18 18:34:51 +0000 | [diff] [blame] | 3972 | if( rc ){ |
| 3973 | goto end_allocate_page; |
| 3974 | } |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 3975 | put4byte(&pPrevTrunk->aData[0], iNewTrunk); |
| 3976 | } |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 3977 | } |
| 3978 | pTrunk = 0; |
| 3979 | TRACE(("ALLOCATE: %d trunk - %d free pages left\n", *pPgno, n-1)); |
| 3980 | #endif |
| 3981 | }else{ |
| 3982 | /* Extract a leaf from the trunk */ |
| 3983 | int closest; |
| 3984 | Pgno iPage; |
| 3985 | unsigned char *aData = pTrunk->aData; |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 3986 | rc = sqlite3PagerWrite(pTrunk->pDbPage); |
drh | d3627af | 2006-12-18 18:34:51 +0000 | [diff] [blame] | 3987 | if( rc ){ |
| 3988 | goto end_allocate_page; |
| 3989 | } |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 3990 | if( nearby>0 ){ |
| 3991 | int i, dist; |
| 3992 | closest = 0; |
| 3993 | dist = get4byte(&aData[8]) - nearby; |
| 3994 | if( dist<0 ) dist = -dist; |
| 3995 | for(i=1; i<k; i++){ |
| 3996 | int d2 = get4byte(&aData[8+i*4]) - nearby; |
| 3997 | if( d2<0 ) d2 = -d2; |
| 3998 | if( d2<dist ){ |
| 3999 | closest = i; |
| 4000 | dist = d2; |
| 4001 | } |
| 4002 | } |
| 4003 | }else{ |
| 4004 | closest = 0; |
| 4005 | } |
| 4006 | |
| 4007 | iPage = get4byte(&aData[8+closest*4]); |
| 4008 | if( !searchList || iPage==nearby ){ |
| 4009 | *pPgno = iPage; |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 4010 | if( *pPgno>sqlite3PagerPagecount(pBt->pPager) ){ |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 4011 | /* Free page off the end of the file */ |
drh | 4928570 | 2005-09-17 15:20:26 +0000 | [diff] [blame] | 4012 | return SQLITE_CORRUPT_BKPT; |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 4013 | } |
| 4014 | TRACE(("ALLOCATE: %d was leaf %d of %d on trunk %d" |
| 4015 | ": %d more free pages\n", |
| 4016 | *pPgno, closest+1, k, pTrunk->pgno, n-1)); |
| 4017 | if( closest<k-1 ){ |
| 4018 | memcpy(&aData[8+closest*4], &aData[4+k*4], 4); |
| 4019 | } |
| 4020 | put4byte(&aData[4], k-1); |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 4021 | rc = sqlite3BtreeGetPage(pBt, *pPgno, ppPage, 1); |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 4022 | if( rc==SQLITE_OK ){ |
drh | 538f570 | 2007-04-13 02:14:30 +0000 | [diff] [blame] | 4023 | sqlite3PagerDontRollback((*ppPage)->pDbPage); |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 4024 | rc = sqlite3PagerWrite((*ppPage)->pDbPage); |
danielk1977 | aac0a38 | 2005-01-16 11:07:06 +0000 | [diff] [blame] | 4025 | if( rc!=SQLITE_OK ){ |
| 4026 | releasePage(*ppPage); |
| 4027 | } |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 4028 | } |
| 4029 | searchList = 0; |
| 4030 | } |
drh | ee696e2 | 2004-08-30 16:52:17 +0000 | [diff] [blame] | 4031 | } |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 4032 | releasePage(pPrevTrunk); |
drh | d3627af | 2006-12-18 18:34:51 +0000 | [diff] [blame] | 4033 | pPrevTrunk = 0; |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 4034 | }while( searchList ); |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 4035 | }else{ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 4036 | /* There are no pages on the freelist, so create a new page at the |
| 4037 | ** end of the file */ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 4038 | *pPgno = sqlite3PagerPagecount(pBt->pPager) + 1; |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 4039 | |
| 4040 | #ifndef SQLITE_OMIT_AUTOVACUUM |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 4041 | if( pBt->nTrunc ){ |
| 4042 | /* An incr-vacuum has already run within this transaction. So the |
| 4043 | ** page to allocate is not from the physical end of the file, but |
| 4044 | ** at pBt->nTrunc. |
| 4045 | */ |
| 4046 | *pPgno = pBt->nTrunc+1; |
| 4047 | if( *pPgno==PENDING_BYTE_PAGE(pBt) ){ |
| 4048 | (*pPgno)++; |
| 4049 | } |
| 4050 | } |
danielk1977 | 266664d | 2006-02-10 08:24:21 +0000 | [diff] [blame] | 4051 | if( pBt->autoVacuum && PTRMAP_ISPAGE(pBt, *pPgno) ){ |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 4052 | /* If *pPgno refers to a pointer-map page, allocate two new pages |
| 4053 | ** at the end of the file instead of one. The first allocated page |
| 4054 | ** becomes a new pointer-map page, the second is used by the caller. |
| 4055 | */ |
| 4056 | TRACE(("ALLOCATE: %d from end of file (pointer-map page)\n", *pPgno)); |
danielk1977 | 599fcba | 2004-11-08 07:13:13 +0000 | [diff] [blame] | 4057 | assert( *pPgno!=PENDING_BYTE_PAGE(pBt) ); |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 4058 | (*pPgno)++; |
| 4059 | } |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 4060 | if( pBt->nTrunc ){ |
| 4061 | pBt->nTrunc = *pPgno; |
| 4062 | } |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 4063 | #endif |
| 4064 | |
danielk1977 | 599fcba | 2004-11-08 07:13:13 +0000 | [diff] [blame] | 4065 | assert( *pPgno!=PENDING_BYTE_PAGE(pBt) ); |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 4066 | rc = sqlite3BtreeGetPage(pBt, *pPgno, ppPage, 0); |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 4067 | if( rc ) return rc; |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 4068 | rc = sqlite3PagerWrite((*ppPage)->pDbPage); |
danielk1977 | aac0a38 | 2005-01-16 11:07:06 +0000 | [diff] [blame] | 4069 | if( rc!=SQLITE_OK ){ |
| 4070 | releasePage(*ppPage); |
| 4071 | } |
drh | 3a4c141 | 2004-05-09 20:40:11 +0000 | [diff] [blame] | 4072 | TRACE(("ALLOCATE: %d from end of file\n", *pPgno)); |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 4073 | } |
danielk1977 | 599fcba | 2004-11-08 07:13:13 +0000 | [diff] [blame] | 4074 | |
| 4075 | assert( *pPgno!=PENDING_BYTE_PAGE(pBt) ); |
drh | d3627af | 2006-12-18 18:34:51 +0000 | [diff] [blame] | 4076 | |
| 4077 | end_allocate_page: |
| 4078 | releasePage(pTrunk); |
| 4079 | releasePage(pPrevTrunk); |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 4080 | return rc; |
| 4081 | } |
| 4082 | |
| 4083 | /* |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 4084 | ** Add a page of the database file to the freelist. |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 4085 | ** |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 4086 | ** sqlite3PagerUnref() is NOT called for pPage. |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 4087 | */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 4088 | static int freePage(MemPage *pPage){ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 4089 | BtShared *pBt = pPage->pBt; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 4090 | MemPage *pPage1 = pBt->pPage1; |
| 4091 | int rc, n, k; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 4092 | |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 4093 | /* Prepare the page for freeing */ |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 4094 | assert( sqlite3_mutex_held(pPage->pBt->mutex) ); |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 4095 | assert( pPage->pgno>1 ); |
| 4096 | pPage->isInit = 0; |
| 4097 | releasePage(pPage->pParent); |
| 4098 | pPage->pParent = 0; |
| 4099 | |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 4100 | /* Increment the free page count on pPage1 */ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 4101 | rc = sqlite3PagerWrite(pPage1->pDbPage); |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 4102 | if( rc ) return rc; |
| 4103 | n = get4byte(&pPage1->aData[36]); |
| 4104 | put4byte(&pPage1->aData[36], n+1); |
| 4105 | |
drh | fcce93f | 2006-02-22 03:08:32 +0000 | [diff] [blame] | 4106 | #ifdef SQLITE_SECURE_DELETE |
| 4107 | /* If the SQLITE_SECURE_DELETE compile-time option is enabled, then |
| 4108 | ** always fully overwrite deleted information with zeros. |
| 4109 | */ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 4110 | rc = sqlite3PagerWrite(pPage->pDbPage); |
drh | fcce93f | 2006-02-22 03:08:32 +0000 | [diff] [blame] | 4111 | if( rc ) return rc; |
| 4112 | memset(pPage->aData, 0, pPage->pBt->pageSize); |
| 4113 | #endif |
| 4114 | |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 4115 | #ifndef SQLITE_OMIT_AUTOVACUUM |
| 4116 | /* If the database supports auto-vacuum, write an entry in the pointer-map |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 4117 | ** to indicate that the page is free. |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 4118 | */ |
| 4119 | if( pBt->autoVacuum ){ |
| 4120 | rc = ptrmapPut(pBt, pPage->pgno, PTRMAP_FREEPAGE, 0); |
danielk1977 | a64a035 | 2004-11-05 01:45:13 +0000 | [diff] [blame] | 4121 | if( rc ) return rc; |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 4122 | } |
| 4123 | #endif |
| 4124 | |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 4125 | if( n==0 ){ |
| 4126 | /* This is the first free page */ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 4127 | rc = sqlite3PagerWrite(pPage->pDbPage); |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 4128 | if( rc ) return rc; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 4129 | memset(pPage->aData, 0, 8); |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 4130 | put4byte(&pPage1->aData[32], pPage->pgno); |
drh | 3a4c141 | 2004-05-09 20:40:11 +0000 | [diff] [blame] | 4131 | TRACE(("FREE-PAGE: %d first\n", pPage->pgno)); |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 4132 | }else{ |
| 4133 | /* Other free pages already exist. Retrive the first trunk page |
| 4134 | ** of the freelist and find out how many leaves it has. */ |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 4135 | MemPage *pTrunk; |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 4136 | rc = sqlite3BtreeGetPage(pBt, get4byte(&pPage1->aData[32]), &pTrunk, 0); |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 4137 | if( rc ) return rc; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 4138 | k = get4byte(&pTrunk->aData[4]); |
drh | ee696e2 | 2004-08-30 16:52:17 +0000 | [diff] [blame] | 4139 | if( k>=pBt->usableSize/4 - 8 ){ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 4140 | /* The trunk is full. Turn the page being freed into a new |
| 4141 | ** trunk page with no leaves. */ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 4142 | rc = sqlite3PagerWrite(pPage->pDbPage); |
drh | b9ee493 | 2007-09-07 14:32:06 +0000 | [diff] [blame] | 4143 | if( rc==SQLITE_OK ){ |
| 4144 | put4byte(pPage->aData, pTrunk->pgno); |
| 4145 | put4byte(&pPage->aData[4], 0); |
| 4146 | put4byte(&pPage1->aData[32], pPage->pgno); |
| 4147 | TRACE(("FREE-PAGE: %d new trunk page replacing %d\n", |
| 4148 | pPage->pgno, pTrunk->pgno)); |
| 4149 | } |
| 4150 | }else if( k<0 ){ |
| 4151 | rc = SQLITE_CORRUPT; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 4152 | }else{ |
| 4153 | /* Add the newly freed page as a leaf on the current trunk */ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 4154 | rc = sqlite3PagerWrite(pTrunk->pDbPage); |
drh | f534544 | 2007-04-09 12:45:02 +0000 | [diff] [blame] | 4155 | if( rc==SQLITE_OK ){ |
| 4156 | put4byte(&pTrunk->aData[4], k+1); |
| 4157 | put4byte(&pTrunk->aData[8+k*4], pPage->pgno); |
drh | fcce93f | 2006-02-22 03:08:32 +0000 | [diff] [blame] | 4158 | #ifndef SQLITE_SECURE_DELETE |
drh | 538f570 | 2007-04-13 02:14:30 +0000 | [diff] [blame] | 4159 | sqlite3PagerDontWrite(pPage->pDbPage); |
drh | fcce93f | 2006-02-22 03:08:32 +0000 | [diff] [blame] | 4160 | #endif |
drh | f534544 | 2007-04-09 12:45:02 +0000 | [diff] [blame] | 4161 | } |
drh | 3a4c141 | 2004-05-09 20:40:11 +0000 | [diff] [blame] | 4162 | TRACE(("FREE-PAGE: %d leaf on trunk page %d\n",pPage->pgno,pTrunk->pgno)); |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 4163 | } |
| 4164 | releasePage(pTrunk); |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 4165 | } |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 4166 | return rc; |
| 4167 | } |
| 4168 | |
| 4169 | /* |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 4170 | ** Free any overflow pages associated with the given Cell. |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 4171 | */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 4172 | static int clearCell(MemPage *pPage, unsigned char *pCell){ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 4173 | BtShared *pBt = pPage->pBt; |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 4174 | CellInfo info; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 4175 | Pgno ovflPgno; |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 4176 | int rc; |
drh | 9444081 | 2007-03-06 11:42:19 +0000 | [diff] [blame] | 4177 | int nOvfl; |
| 4178 | int ovflPageSize; |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 4179 | |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 4180 | assert( sqlite3_mutex_held(pPage->pBt->mutex) ); |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 4181 | sqlite3BtreeParseCellPtr(pPage, pCell, &info); |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 4182 | if( info.iOverflow==0 ){ |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 4183 | return SQLITE_OK; /* No overflow pages. Return without doing anything */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 4184 | } |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 4185 | ovflPgno = get4byte(&pCell[info.iOverflow]); |
drh | 9444081 | 2007-03-06 11:42:19 +0000 | [diff] [blame] | 4186 | ovflPageSize = pBt->usableSize - 4; |
drh | 7236583 | 2007-03-06 15:53:44 +0000 | [diff] [blame] | 4187 | nOvfl = (info.nPayload - info.nLocal + ovflPageSize - 1)/ovflPageSize; |
| 4188 | assert( ovflPgno==0 || nOvfl>0 ); |
| 4189 | while( nOvfl-- ){ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 4190 | MemPage *pOvfl; |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 4191 | if( ovflPgno==0 || ovflPgno>sqlite3PagerPagecount(pBt->pPager) ){ |
drh | 4928570 | 2005-09-17 15:20:26 +0000 | [diff] [blame] | 4192 | return SQLITE_CORRUPT_BKPT; |
danielk1977 | a1cb183 | 2005-02-12 08:59:55 +0000 | [diff] [blame] | 4193 | } |
danielk1977 | 8c0a959 | 2007-04-30 16:55:00 +0000 | [diff] [blame] | 4194 | |
| 4195 | rc = getOverflowPage(pBt, ovflPgno, &pOvfl, (nOvfl==0)?0:&ovflPgno); |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 4196 | if( rc ) return rc; |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 4197 | rc = freePage(pOvfl); |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 4198 | sqlite3PagerUnref(pOvfl->pDbPage); |
danielk1977 | 6b456a2 | 2005-03-21 04:04:02 +0000 | [diff] [blame] | 4199 | if( rc ) return rc; |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 4200 | } |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 4201 | return SQLITE_OK; |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 4202 | } |
| 4203 | |
| 4204 | /* |
drh | 9102529 | 2004-05-03 19:49:32 +0000 | [diff] [blame] | 4205 | ** Create the byte sequence used to represent a cell on page pPage |
| 4206 | ** and write that byte sequence into pCell[]. Overflow pages are |
| 4207 | ** allocated and filled in as necessary. The calling procedure |
| 4208 | ** is responsible for making sure sufficient space has been allocated |
| 4209 | ** for pCell[]. |
| 4210 | ** |
| 4211 | ** Note that pCell does not necessary need to point to the pPage->aData |
| 4212 | ** area. pCell might point to some temporary storage. The cell will |
| 4213 | ** be constructed in this temporary area then copied into pPage->aData |
| 4214 | ** later. |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 4215 | */ |
| 4216 | static int fillInCell( |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 4217 | MemPage *pPage, /* The page that contains the cell */ |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 4218 | unsigned char *pCell, /* Complete text of the cell */ |
drh | 4a1c380 | 2004-05-12 15:15:47 +0000 | [diff] [blame] | 4219 | const void *pKey, i64 nKey, /* The key */ |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 4220 | const void *pData,int nData, /* The data */ |
drh | b026e05 | 2007-05-02 01:34:31 +0000 | [diff] [blame] | 4221 | int nZero, /* Extra zero bytes to append to pData */ |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 4222 | int *pnSize /* Write cell size here */ |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 4223 | ){ |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 4224 | int nPayload; |
drh | 8c6fa9b | 2004-05-26 00:01:53 +0000 | [diff] [blame] | 4225 | const u8 *pSrc; |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 4226 | int nSrc, n, rc; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 4227 | int spaceLeft; |
| 4228 | MemPage *pOvfl = 0; |
drh | 9b17127 | 2004-05-08 02:03:22 +0000 | [diff] [blame] | 4229 | MemPage *pToRelease = 0; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 4230 | unsigned char *pPrior; |
| 4231 | unsigned char *pPayload; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 4232 | BtShared *pBt = pPage->pBt; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 4233 | Pgno pgnoOvfl = 0; |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 4234 | int nHeader; |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 4235 | CellInfo info; |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 4236 | |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 4237 | assert( sqlite3_mutex_held(pPage->pBt->mutex) ); |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 4238 | |
drh | 9102529 | 2004-05-03 19:49:32 +0000 | [diff] [blame] | 4239 | /* Fill in the header. */ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4240 | nHeader = 0; |
drh | 9102529 | 2004-05-03 19:49:32 +0000 | [diff] [blame] | 4241 | if( !pPage->leaf ){ |
| 4242 | nHeader += 4; |
| 4243 | } |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 4244 | if( pPage->hasData ){ |
drh | b026e05 | 2007-05-02 01:34:31 +0000 | [diff] [blame] | 4245 | nHeader += putVarint(&pCell[nHeader], nData+nZero); |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 4246 | }else{ |
drh | b026e05 | 2007-05-02 01:34:31 +0000 | [diff] [blame] | 4247 | nData = nZero = 0; |
drh | 9102529 | 2004-05-03 19:49:32 +0000 | [diff] [blame] | 4248 | } |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 4249 | nHeader += putVarint(&pCell[nHeader], *(u64*)&nKey); |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 4250 | sqlite3BtreeParseCellPtr(pPage, pCell, &info); |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 4251 | assert( info.nHeader==nHeader ); |
| 4252 | assert( info.nKey==nKey ); |
drh | b026e05 | 2007-05-02 01:34:31 +0000 | [diff] [blame] | 4253 | assert( info.nData==nData+nZero ); |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 4254 | |
| 4255 | /* Fill in the payload */ |
drh | b026e05 | 2007-05-02 01:34:31 +0000 | [diff] [blame] | 4256 | nPayload = nData + nZero; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 4257 | if( pPage->intKey ){ |
| 4258 | pSrc = pData; |
| 4259 | nSrc = nData; |
drh | 9102529 | 2004-05-03 19:49:32 +0000 | [diff] [blame] | 4260 | nData = 0; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 4261 | }else{ |
| 4262 | nPayload += nKey; |
| 4263 | pSrc = pKey; |
| 4264 | nSrc = nKey; |
| 4265 | } |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 4266 | *pnSize = info.nSize; |
| 4267 | spaceLeft = info.nLocal; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 4268 | pPayload = &pCell[nHeader]; |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 4269 | pPrior = &pCell[info.iOverflow]; |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 4270 | |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 4271 | while( nPayload>0 ){ |
| 4272 | if( spaceLeft==0 ){ |
danielk1977 | b39f70b | 2007-05-17 18:28:11 +0000 | [diff] [blame] | 4273 | int isExact = 0; |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 4274 | #ifndef SQLITE_OMIT_AUTOVACUUM |
| 4275 | Pgno pgnoPtrmap = pgnoOvfl; /* Overflow page pointer-map entry page */ |
danielk1977 | b39f70b | 2007-05-17 18:28:11 +0000 | [diff] [blame] | 4276 | if( pBt->autoVacuum ){ |
| 4277 | do{ |
| 4278 | pgnoOvfl++; |
| 4279 | } while( |
| 4280 | PTRMAP_ISPAGE(pBt, pgnoOvfl) || pgnoOvfl==PENDING_BYTE_PAGE(pBt) |
| 4281 | ); |
danielk1977 | 89a4be8 | 2007-05-23 13:34:32 +0000 | [diff] [blame] | 4282 | if( pgnoOvfl>1 ){ |
danielk1977 | b39f70b | 2007-05-17 18:28:11 +0000 | [diff] [blame] | 4283 | /* isExact = 1; */ |
| 4284 | } |
| 4285 | } |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 4286 | #endif |
danielk1977 | b39f70b | 2007-05-17 18:28:11 +0000 | [diff] [blame] | 4287 | rc = allocateBtreePage(pBt, &pOvfl, &pgnoOvfl, pgnoOvfl, isExact); |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 4288 | #ifndef SQLITE_OMIT_AUTOVACUUM |
danielk1977 | a19df67 | 2004-11-03 11:37:07 +0000 | [diff] [blame] | 4289 | /* If the database supports auto-vacuum, and the second or subsequent |
| 4290 | ** overflow page is being allocated, add an entry to the pointer-map |
danielk1977 | 4ef2449 | 2007-05-23 09:52:41 +0000 | [diff] [blame] | 4291 | ** for that page now. |
| 4292 | ** |
| 4293 | ** If this is the first overflow page, then write a partial entry |
| 4294 | ** to the pointer-map. If we write nothing to this pointer-map slot, |
| 4295 | ** then the optimistic overflow chain processing in clearCell() |
| 4296 | ** may misinterpret the uninitialised values and delete the |
| 4297 | ** wrong pages from the database. |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 4298 | */ |
danielk1977 | 4ef2449 | 2007-05-23 09:52:41 +0000 | [diff] [blame] | 4299 | if( pBt->autoVacuum && rc==SQLITE_OK ){ |
| 4300 | u8 eType = (pgnoPtrmap?PTRMAP_OVERFLOW2:PTRMAP_OVERFLOW1); |
| 4301 | rc = ptrmapPut(pBt, pgnoOvfl, eType, pgnoPtrmap); |
danielk1977 | 89a4be8 | 2007-05-23 13:34:32 +0000 | [diff] [blame] | 4302 | if( rc ){ |
| 4303 | releasePage(pOvfl); |
| 4304 | } |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 4305 | } |
| 4306 | #endif |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 4307 | if( rc ){ |
drh | 9b17127 | 2004-05-08 02:03:22 +0000 | [diff] [blame] | 4308 | releasePage(pToRelease); |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 4309 | return rc; |
| 4310 | } |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 4311 | put4byte(pPrior, pgnoOvfl); |
drh | 9b17127 | 2004-05-08 02:03:22 +0000 | [diff] [blame] | 4312 | releasePage(pToRelease); |
| 4313 | pToRelease = pOvfl; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 4314 | pPrior = pOvfl->aData; |
| 4315 | put4byte(pPrior, 0); |
| 4316 | pPayload = &pOvfl->aData[4]; |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 4317 | spaceLeft = pBt->usableSize - 4; |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 4318 | } |
| 4319 | n = nPayload; |
| 4320 | if( n>spaceLeft ) n = spaceLeft; |
drh | b026e05 | 2007-05-02 01:34:31 +0000 | [diff] [blame] | 4321 | if( nSrc>0 ){ |
| 4322 | if( n>nSrc ) n = nSrc; |
| 4323 | assert( pSrc ); |
| 4324 | memcpy(pPayload, pSrc, n); |
| 4325 | }else{ |
| 4326 | memset(pPayload, 0, n); |
| 4327 | } |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 4328 | nPayload -= n; |
drh | de64713 | 2004-05-07 17:57:49 +0000 | [diff] [blame] | 4329 | pPayload += n; |
drh | 9b17127 | 2004-05-08 02:03:22 +0000 | [diff] [blame] | 4330 | pSrc += n; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 4331 | nSrc -= n; |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 4332 | spaceLeft -= n; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 4333 | if( nSrc==0 ){ |
| 4334 | nSrc = nData; |
| 4335 | pSrc = pData; |
| 4336 | } |
drh | dd79342 | 2001-06-28 01:54:48 +0000 | [diff] [blame] | 4337 | } |
drh | 9b17127 | 2004-05-08 02:03:22 +0000 | [diff] [blame] | 4338 | releasePage(pToRelease); |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 4339 | return SQLITE_OK; |
| 4340 | } |
| 4341 | |
| 4342 | /* |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 4343 | ** Change the MemPage.pParent pointer on the page whose number is |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 4344 | ** given in the second argument so that MemPage.pParent holds the |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 4345 | ** pointer in the third argument. |
| 4346 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 4347 | static int reparentPage(BtShared *pBt, Pgno pgno, MemPage *pNewParent, int idx){ |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 4348 | MemPage *pThis; |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 4349 | DbPage *pDbPage; |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 4350 | |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 4351 | assert( sqlite3_mutex_held(pBt->mutex) ); |
drh | 43617e9 | 2006-03-06 20:55:46 +0000 | [diff] [blame] | 4352 | assert( pNewParent!=0 ); |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 4353 | if( pgno==0 ) return SQLITE_OK; |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 4354 | assert( pBt->pPager!=0 ); |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 4355 | pDbPage = sqlite3PagerLookup(pBt->pPager, pgno); |
| 4356 | if( pDbPage ){ |
| 4357 | pThis = (MemPage *)sqlite3PagerGetExtra(pDbPage); |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 4358 | if( pThis->isInit ){ |
drh | bf4bca5 | 2007-09-06 22:19:14 +0000 | [diff] [blame] | 4359 | assert( pThis->aData==sqlite3PagerGetData(pDbPage) ); |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 4360 | if( pThis->pParent!=pNewParent ){ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 4361 | if( pThis->pParent ) sqlite3PagerUnref(pThis->pParent->pDbPage); |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 4362 | pThis->pParent = pNewParent; |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 4363 | sqlite3PagerRef(pNewParent->pDbPage); |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 4364 | } |
| 4365 | pThis->idxParent = idx; |
drh | dd79342 | 2001-06-28 01:54:48 +0000 | [diff] [blame] | 4366 | } |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 4367 | sqlite3PagerUnref(pDbPage); |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 4368 | } |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 4369 | |
| 4370 | #ifndef SQLITE_OMIT_AUTOVACUUM |
| 4371 | if( pBt->autoVacuum ){ |
| 4372 | return ptrmapPut(pBt, pgno, PTRMAP_BTREE, pNewParent->pgno); |
| 4373 | } |
| 4374 | #endif |
| 4375 | return SQLITE_OK; |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 4376 | } |
| 4377 | |
danielk1977 | ac11ee6 | 2005-01-15 12:45:51 +0000 | [diff] [blame] | 4378 | |
| 4379 | |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 4380 | /* |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 4381 | ** Change the pParent pointer of all children of pPage to point back |
| 4382 | ** to pPage. |
| 4383 | ** |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 4384 | ** In other words, for every child of pPage, invoke reparentPage() |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4385 | ** to make sure that each child knows that pPage is its parent. |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 4386 | ** |
| 4387 | ** This routine gets called after you memcpy() one page into |
| 4388 | ** another. |
| 4389 | */ |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 4390 | static int reparentChildPages(MemPage *pPage){ |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 4391 | int i; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 4392 | BtShared *pBt = pPage->pBt; |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 4393 | int rc = SQLITE_OK; |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 4394 | |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 4395 | assert( sqlite3_mutex_held(pPage->pBt->mutex) ); |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 4396 | if( pPage->leaf ) return SQLITE_OK; |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 4397 | |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 4398 | for(i=0; i<pPage->nCell; i++){ |
danielk1977 | 1cc5ed8 | 2007-05-16 17:28:43 +0000 | [diff] [blame] | 4399 | u8 *pCell = findCell(pPage, i); |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 4400 | if( !pPage->leaf ){ |
| 4401 | rc = reparentPage(pBt, get4byte(pCell), pPage, i); |
| 4402 | if( rc!=SQLITE_OK ) return rc; |
| 4403 | } |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 4404 | } |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 4405 | if( !pPage->leaf ){ |
| 4406 | rc = reparentPage(pBt, get4byte(&pPage->aData[pPage->hdrOffset+8]), |
| 4407 | pPage, i); |
| 4408 | pPage->idxShift = 0; |
| 4409 | } |
| 4410 | return rc; |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 4411 | } |
| 4412 | |
| 4413 | /* |
| 4414 | ** Remove the i-th cell from pPage. This routine effects pPage only. |
| 4415 | ** The cell content is not freed or deallocated. It is assumed that |
| 4416 | ** the cell content has been copied someplace else. This routine just |
| 4417 | ** removes the reference to the cell from pPage. |
| 4418 | ** |
| 4419 | ** "sz" must be the number of bytes in the cell. |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 4420 | */ |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 4421 | static void dropCell(MemPage *pPage, int idx, int sz){ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4422 | int i; /* Loop counter */ |
| 4423 | int pc; /* Offset to cell content of cell being deleted */ |
| 4424 | u8 *data; /* pPage->aData */ |
| 4425 | u8 *ptr; /* Used to move bytes around within data[] */ |
| 4426 | |
drh | 8c42ca9 | 2001-06-22 19:15:00 +0000 | [diff] [blame] | 4427 | assert( idx>=0 && idx<pPage->nCell ); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4428 | assert( sz==cellSize(pPage, idx) ); |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 4429 | assert( sqlite3PagerIswriteable(pPage->pDbPage) ); |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 4430 | assert( sqlite3_mutex_held(pPage->pBt->mutex) ); |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 4431 | data = pPage->aData; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4432 | ptr = &data[pPage->cellOffset + 2*idx]; |
| 4433 | pc = get2byte(ptr); |
| 4434 | assert( pc>10 && pc+sz<=pPage->pBt->usableSize ); |
drh | de64713 | 2004-05-07 17:57:49 +0000 | [diff] [blame] | 4435 | freeSpace(pPage, pc, sz); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4436 | for(i=idx+1; i<pPage->nCell; i++, ptr+=2){ |
| 4437 | ptr[0] = ptr[2]; |
| 4438 | ptr[1] = ptr[3]; |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 4439 | } |
| 4440 | pPage->nCell--; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4441 | put2byte(&data[pPage->hdrOffset+3], pPage->nCell); |
| 4442 | pPage->nFree += 2; |
drh | 428ae8c | 2003-01-04 16:48:09 +0000 | [diff] [blame] | 4443 | pPage->idxShift = 1; |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 4444 | } |
| 4445 | |
| 4446 | /* |
| 4447 | ** Insert a new cell on pPage at cell index "i". pCell points to the |
| 4448 | ** content of the cell. |
| 4449 | ** |
| 4450 | ** If the cell content will fit on the page, then put it there. If it |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4451 | ** will not fit, then make a copy of the cell content into pTemp if |
| 4452 | ** pTemp is not null. Regardless of pTemp, allocate a new entry |
| 4453 | ** in pPage->aOvfl[] and make it point to the cell content (either |
| 4454 | ** in pTemp or the original pCell) and also record its index. |
| 4455 | ** Allocating a new entry in pPage->aCell[] implies that |
| 4456 | ** pPage->nOverflow is incremented. |
danielk1977 | a3ad5e7 | 2005-01-07 08:56:44 +0000 | [diff] [blame] | 4457 | ** |
| 4458 | ** If nSkip is non-zero, then do not copy the first nSkip bytes of the |
| 4459 | ** cell. The caller will overwrite them after this function returns. If |
drh | 4b238df | 2005-01-08 15:43:18 +0000 | [diff] [blame] | 4460 | ** nSkip is non-zero, then pCell may not point to an invalid memory location |
danielk1977 | a3ad5e7 | 2005-01-07 08:56:44 +0000 | [diff] [blame] | 4461 | ** (but pCell+nSkip is always valid). |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 4462 | */ |
danielk1977 | e80463b | 2004-11-03 03:01:16 +0000 | [diff] [blame] | 4463 | static int insertCell( |
drh | 24cd67e | 2004-05-10 16:18:47 +0000 | [diff] [blame] | 4464 | MemPage *pPage, /* Page into which we are copying */ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4465 | int i, /* New cell becomes the i-th cell of the page */ |
| 4466 | u8 *pCell, /* Content of the new cell */ |
| 4467 | int sz, /* Bytes of content in pCell */ |
danielk1977 | a3ad5e7 | 2005-01-07 08:56:44 +0000 | [diff] [blame] | 4468 | u8 *pTemp, /* Temp storage space for pCell, if needed */ |
| 4469 | u8 nSkip /* Do not write the first nSkip bytes of the cell */ |
drh | 24cd67e | 2004-05-10 16:18:47 +0000 | [diff] [blame] | 4470 | ){ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4471 | int idx; /* Where to write new cell content in data[] */ |
| 4472 | int j; /* Loop counter */ |
| 4473 | int top; /* First byte of content for any cell in data[] */ |
| 4474 | int end; /* First byte past the last cell pointer in data[] */ |
| 4475 | int ins; /* Index in data[] where new cell pointer is inserted */ |
| 4476 | int hdr; /* Offset into data[] of the page header */ |
| 4477 | int cellOffset; /* Address of first cell pointer in data[] */ |
| 4478 | u8 *data; /* The content of the whole page */ |
| 4479 | u8 *ptr; /* Used for moving information around in data[] */ |
| 4480 | |
| 4481 | assert( i>=0 && i<=pPage->nCell+pPage->nOverflow ); |
| 4482 | assert( sz==cellSizePtr(pPage, pCell) ); |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 4483 | assert( sqlite3_mutex_held(pPage->pBt->mutex) ); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4484 | if( pPage->nOverflow || sz+2>pPage->nFree ){ |
drh | 24cd67e | 2004-05-10 16:18:47 +0000 | [diff] [blame] | 4485 | if( pTemp ){ |
danielk1977 | a3ad5e7 | 2005-01-07 08:56:44 +0000 | [diff] [blame] | 4486 | memcpy(pTemp+nSkip, pCell+nSkip, sz-nSkip); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4487 | pCell = pTemp; |
drh | 24cd67e | 2004-05-10 16:18:47 +0000 | [diff] [blame] | 4488 | } |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4489 | j = pPage->nOverflow++; |
| 4490 | assert( j<sizeof(pPage->aOvfl)/sizeof(pPage->aOvfl[0]) ); |
| 4491 | pPage->aOvfl[j].pCell = pCell; |
| 4492 | pPage->aOvfl[j].idx = i; |
| 4493 | pPage->nFree = 0; |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 4494 | }else{ |
danielk1977 | 6e465eb | 2007-08-21 13:11:00 +0000 | [diff] [blame] | 4495 | int rc = sqlite3PagerWrite(pPage->pDbPage); |
| 4496 | if( rc!=SQLITE_OK ){ |
| 4497 | return rc; |
| 4498 | } |
| 4499 | assert( sqlite3PagerIswriteable(pPage->pDbPage) ); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4500 | data = pPage->aData; |
| 4501 | hdr = pPage->hdrOffset; |
| 4502 | top = get2byte(&data[hdr+5]); |
| 4503 | cellOffset = pPage->cellOffset; |
| 4504 | end = cellOffset + 2*pPage->nCell + 2; |
| 4505 | ins = cellOffset + 2*i; |
| 4506 | if( end > top - sz ){ |
danielk1977 | 6e465eb | 2007-08-21 13:11:00 +0000 | [diff] [blame] | 4507 | rc = defragmentPage(pPage); |
danielk1977 | 6b456a2 | 2005-03-21 04:04:02 +0000 | [diff] [blame] | 4508 | if( rc!=SQLITE_OK ) return rc; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4509 | top = get2byte(&data[hdr+5]); |
| 4510 | assert( end + sz <= top ); |
| 4511 | } |
| 4512 | idx = allocateSpace(pPage, sz); |
| 4513 | assert( idx>0 ); |
| 4514 | assert( end <= get2byte(&data[hdr+5]) ); |
| 4515 | pPage->nCell++; |
| 4516 | pPage->nFree -= 2; |
danielk1977 | a3ad5e7 | 2005-01-07 08:56:44 +0000 | [diff] [blame] | 4517 | memcpy(&data[idx+nSkip], pCell+nSkip, sz-nSkip); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4518 | for(j=end-2, ptr=&data[j]; j>ins; j-=2, ptr-=2){ |
| 4519 | ptr[0] = ptr[-2]; |
| 4520 | ptr[1] = ptr[-1]; |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 4521 | } |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4522 | put2byte(&data[ins], idx); |
| 4523 | put2byte(&data[hdr+3], pPage->nCell); |
| 4524 | pPage->idxShift = 1; |
danielk1977 | a19df67 | 2004-11-03 11:37:07 +0000 | [diff] [blame] | 4525 | #ifndef SQLITE_OMIT_AUTOVACUUM |
| 4526 | if( pPage->pBt->autoVacuum ){ |
| 4527 | /* The cell may contain a pointer to an overflow page. If so, write |
| 4528 | ** the entry for the overflow page into the pointer map. |
| 4529 | */ |
| 4530 | CellInfo info; |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 4531 | sqlite3BtreeParseCellPtr(pPage, pCell, &info); |
drh | 7236583 | 2007-03-06 15:53:44 +0000 | [diff] [blame] | 4532 | assert( (info.nData+(pPage->intKey?0:info.nKey))==info.nPayload ); |
danielk1977 | a19df67 | 2004-11-03 11:37:07 +0000 | [diff] [blame] | 4533 | if( (info.nData+(pPage->intKey?0:info.nKey))>info.nLocal ){ |
| 4534 | Pgno pgnoOvfl = get4byte(&pCell[info.iOverflow]); |
danielk1977 | 6e465eb | 2007-08-21 13:11:00 +0000 | [diff] [blame] | 4535 | rc = ptrmapPut(pPage->pBt, pgnoOvfl, PTRMAP_OVERFLOW1, pPage->pgno); |
danielk1977 | a19df67 | 2004-11-03 11:37:07 +0000 | [diff] [blame] | 4536 | if( rc!=SQLITE_OK ) return rc; |
| 4537 | } |
| 4538 | } |
| 4539 | #endif |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 4540 | } |
danielk1977 | e80463b | 2004-11-03 03:01:16 +0000 | [diff] [blame] | 4541 | |
danielk1977 | e80463b | 2004-11-03 03:01:16 +0000 | [diff] [blame] | 4542 | return SQLITE_OK; |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 4543 | } |
| 4544 | |
| 4545 | /* |
drh | fa1a98a | 2004-05-14 19:08:17 +0000 | [diff] [blame] | 4546 | ** Add a list of cells to a page. The page should be initially empty. |
| 4547 | ** The cells are guaranteed to fit on the page. |
| 4548 | */ |
| 4549 | static void assemblePage( |
| 4550 | MemPage *pPage, /* The page to be assemblied */ |
| 4551 | int nCell, /* The number of cells to add to this page */ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4552 | u8 **apCell, /* Pointers to cell bodies */ |
drh | fa1a98a | 2004-05-14 19:08:17 +0000 | [diff] [blame] | 4553 | int *aSize /* Sizes of the cells */ |
| 4554 | ){ |
| 4555 | int i; /* Loop counter */ |
| 4556 | int totalSize; /* Total size of all cells */ |
| 4557 | int hdr; /* Index of page header */ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4558 | int cellptr; /* Address of next cell pointer */ |
| 4559 | int cellbody; /* Address of next cell body */ |
drh | fa1a98a | 2004-05-14 19:08:17 +0000 | [diff] [blame] | 4560 | u8 *data; /* Data for the page */ |
| 4561 | |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4562 | assert( pPage->nOverflow==0 ); |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 4563 | assert( sqlite3_mutex_held(pPage->pBt->mutex) ); |
drh | fa1a98a | 2004-05-14 19:08:17 +0000 | [diff] [blame] | 4564 | totalSize = 0; |
| 4565 | for(i=0; i<nCell; i++){ |
| 4566 | totalSize += aSize[i]; |
| 4567 | } |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4568 | assert( totalSize+2*nCell<=pPage->nFree ); |
drh | fa1a98a | 2004-05-14 19:08:17 +0000 | [diff] [blame] | 4569 | assert( pPage->nCell==0 ); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4570 | cellptr = pPage->cellOffset; |
drh | fa1a98a | 2004-05-14 19:08:17 +0000 | [diff] [blame] | 4571 | data = pPage->aData; |
| 4572 | hdr = pPage->hdrOffset; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4573 | put2byte(&data[hdr+3], nCell); |
drh | 09d0deb | 2005-08-02 17:13:09 +0000 | [diff] [blame] | 4574 | if( nCell ){ |
| 4575 | cellbody = allocateSpace(pPage, totalSize); |
| 4576 | assert( cellbody>0 ); |
| 4577 | assert( pPage->nFree >= 2*nCell ); |
| 4578 | pPage->nFree -= 2*nCell; |
| 4579 | for(i=0; i<nCell; i++){ |
| 4580 | put2byte(&data[cellptr], cellbody); |
| 4581 | memcpy(&data[cellbody], apCell[i], aSize[i]); |
| 4582 | cellptr += 2; |
| 4583 | cellbody += aSize[i]; |
| 4584 | } |
| 4585 | assert( cellbody==pPage->pBt->usableSize ); |
drh | fa1a98a | 2004-05-14 19:08:17 +0000 | [diff] [blame] | 4586 | } |
| 4587 | pPage->nCell = nCell; |
drh | fa1a98a | 2004-05-14 19:08:17 +0000 | [diff] [blame] | 4588 | } |
| 4589 | |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 4590 | /* |
drh | c3b7057 | 2003-01-04 19:44:07 +0000 | [diff] [blame] | 4591 | ** The following parameters determine how many adjacent pages get involved |
| 4592 | ** in a balancing operation. NN is the number of neighbors on either side |
| 4593 | ** of the page that participate in the balancing operation. NB is the |
| 4594 | ** total number of pages that participate, including the target page and |
| 4595 | ** NN neighbors on either side. |
| 4596 | ** |
| 4597 | ** The minimum value of NN is 1 (of course). Increasing NN above 1 |
| 4598 | ** (to 2 or 3) gives a modest improvement in SELECT and DELETE performance |
| 4599 | ** in exchange for a larger degradation in INSERT and UPDATE performance. |
| 4600 | ** The value of NN appears to give the best results overall. |
| 4601 | */ |
| 4602 | #define NN 1 /* Number of neighbors on either side of pPage */ |
| 4603 | #define NB (NN*2+1) /* Total pages involved in the balance */ |
| 4604 | |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4605 | /* Forward reference */ |
danielk1977 | ac245ec | 2005-01-14 13:50:11 +0000 | [diff] [blame] | 4606 | static int balance(MemPage*, int); |
| 4607 | |
drh | 615ae55 | 2005-01-16 23:21:00 +0000 | [diff] [blame] | 4608 | #ifndef SQLITE_OMIT_QUICKBALANCE |
drh | f222e71 | 2005-01-14 22:55:49 +0000 | [diff] [blame] | 4609 | /* |
| 4610 | ** This version of balance() handles the common special case where |
| 4611 | ** a new entry is being inserted on the extreme right-end of the |
| 4612 | ** tree, in other words, when the new entry will become the largest |
| 4613 | ** entry in the tree. |
| 4614 | ** |
| 4615 | ** Instead of trying balance the 3 right-most leaf pages, just add |
| 4616 | ** a new page to the right-hand side and put the one new entry in |
| 4617 | ** that page. This leaves the right side of the tree somewhat |
| 4618 | ** unbalanced. But odds are that we will be inserting new entries |
| 4619 | ** at the end soon afterwards so the nearly empty page will quickly |
| 4620 | ** fill up. On average. |
| 4621 | ** |
| 4622 | ** pPage is the leaf page which is the right-most page in the tree. |
| 4623 | ** pParent is its parent. pPage must have a single overflow entry |
| 4624 | ** which is also the right-most entry on the page. |
| 4625 | */ |
danielk1977 | ac245ec | 2005-01-14 13:50:11 +0000 | [diff] [blame] | 4626 | static int balance_quick(MemPage *pPage, MemPage *pParent){ |
| 4627 | int rc; |
| 4628 | MemPage *pNew; |
| 4629 | Pgno pgnoNew; |
| 4630 | u8 *pCell; |
| 4631 | int szCell; |
| 4632 | CellInfo info; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 4633 | BtShared *pBt = pPage->pBt; |
danielk1977 | 79a40da | 2005-01-16 08:00:01 +0000 | [diff] [blame] | 4634 | int parentIdx = pParent->nCell; /* pParent new divider cell index */ |
| 4635 | int parentSize; /* Size of new divider cell */ |
| 4636 | u8 parentCell[64]; /* Space for the new divider cell */ |
danielk1977 | ac245ec | 2005-01-14 13:50:11 +0000 | [diff] [blame] | 4637 | |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 4638 | assert( sqlite3_mutex_held(pPage->pBt->mutex) ); |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 4639 | |
danielk1977 | ac245ec | 2005-01-14 13:50:11 +0000 | [diff] [blame] | 4640 | /* Allocate a new page. Insert the overflow cell from pPage |
| 4641 | ** into it. Then remove the overflow cell from pPage. |
| 4642 | */ |
drh | 4f0c587 | 2007-03-26 22:05:01 +0000 | [diff] [blame] | 4643 | rc = allocateBtreePage(pBt, &pNew, &pgnoNew, 0, 0); |
danielk1977 | ac245ec | 2005-01-14 13:50:11 +0000 | [diff] [blame] | 4644 | if( rc!=SQLITE_OK ){ |
| 4645 | return rc; |
| 4646 | } |
| 4647 | pCell = pPage->aOvfl[0].pCell; |
| 4648 | szCell = cellSizePtr(pPage, pCell); |
| 4649 | zeroPage(pNew, pPage->aData[0]); |
| 4650 | assemblePage(pNew, 1, &pCell, &szCell); |
| 4651 | pPage->nOverflow = 0; |
| 4652 | |
danielk1977 | 79a40da | 2005-01-16 08:00:01 +0000 | [diff] [blame] | 4653 | /* Set the parent of the newly allocated page to pParent. */ |
| 4654 | pNew->pParent = pParent; |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 4655 | sqlite3PagerRef(pParent->pDbPage); |
danielk1977 | 79a40da | 2005-01-16 08:00:01 +0000 | [diff] [blame] | 4656 | |
danielk1977 | ac245ec | 2005-01-14 13:50:11 +0000 | [diff] [blame] | 4657 | /* pPage is currently the right-child of pParent. Change this |
| 4658 | ** so that the right-child is the new page allocated above and |
danielk1977 | 79a40da | 2005-01-16 08:00:01 +0000 | [diff] [blame] | 4659 | ** pPage is the next-to-right child. |
danielk1977 | ac245ec | 2005-01-14 13:50:11 +0000 | [diff] [blame] | 4660 | */ |
danielk1977 | ac11ee6 | 2005-01-15 12:45:51 +0000 | [diff] [blame] | 4661 | assert( pPage->nCell>0 ); |
danielk1977 | 1cc5ed8 | 2007-05-16 17:28:43 +0000 | [diff] [blame] | 4662 | pCell = findCell(pPage, pPage->nCell-1); |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 4663 | sqlite3BtreeParseCellPtr(pPage, pCell, &info); |
drh | b026e05 | 2007-05-02 01:34:31 +0000 | [diff] [blame] | 4664 | rc = fillInCell(pParent, parentCell, 0, info.nKey, 0, 0, 0, &parentSize); |
danielk1977 | ac245ec | 2005-01-14 13:50:11 +0000 | [diff] [blame] | 4665 | if( rc!=SQLITE_OK ){ |
danielk1977 | 79a40da | 2005-01-16 08:00:01 +0000 | [diff] [blame] | 4666 | return rc; |
danielk1977 | ac245ec | 2005-01-14 13:50:11 +0000 | [diff] [blame] | 4667 | } |
| 4668 | assert( parentSize<64 ); |
| 4669 | rc = insertCell(pParent, parentIdx, parentCell, parentSize, 0, 4); |
| 4670 | if( rc!=SQLITE_OK ){ |
danielk1977 | 79a40da | 2005-01-16 08:00:01 +0000 | [diff] [blame] | 4671 | return rc; |
danielk1977 | ac245ec | 2005-01-14 13:50:11 +0000 | [diff] [blame] | 4672 | } |
| 4673 | put4byte(findOverflowCell(pParent,parentIdx), pPage->pgno); |
| 4674 | put4byte(&pParent->aData[pParent->hdrOffset+8], pgnoNew); |
| 4675 | |
danielk1977 | 79a40da | 2005-01-16 08:00:01 +0000 | [diff] [blame] | 4676 | #ifndef SQLITE_OMIT_AUTOVACUUM |
| 4677 | /* If this is an auto-vacuum database, update the pointer map |
| 4678 | ** with entries for the new page, and any pointer from the |
| 4679 | ** cell on the page to an overflow page. |
| 4680 | */ |
danielk1977 | ac11ee6 | 2005-01-15 12:45:51 +0000 | [diff] [blame] | 4681 | if( pBt->autoVacuum ){ |
| 4682 | rc = ptrmapPut(pBt, pgnoNew, PTRMAP_BTREE, pParent->pgno); |
danielk1977 | deb403e | 2007-05-24 09:20:16 +0000 | [diff] [blame] | 4683 | if( rc==SQLITE_OK ){ |
| 4684 | rc = ptrmapPutOvfl(pNew, 0); |
danielk1977 | ac11ee6 | 2005-01-15 12:45:51 +0000 | [diff] [blame] | 4685 | } |
danielk1977 | 79a40da | 2005-01-16 08:00:01 +0000 | [diff] [blame] | 4686 | if( rc!=SQLITE_OK ){ |
danielk1977 | deb403e | 2007-05-24 09:20:16 +0000 | [diff] [blame] | 4687 | releasePage(pNew); |
danielk1977 | 79a40da | 2005-01-16 08:00:01 +0000 | [diff] [blame] | 4688 | return rc; |
danielk1977 | ac11ee6 | 2005-01-15 12:45:51 +0000 | [diff] [blame] | 4689 | } |
| 4690 | } |
danielk1977 | 79a40da | 2005-01-16 08:00:01 +0000 | [diff] [blame] | 4691 | #endif |
danielk1977 | ac11ee6 | 2005-01-15 12:45:51 +0000 | [diff] [blame] | 4692 | |
danielk1977 | 79a40da | 2005-01-16 08:00:01 +0000 | [diff] [blame] | 4693 | /* Release the reference to the new page and balance the parent page, |
| 4694 | ** in case the divider cell inserted caused it to become overfull. |
| 4695 | */ |
danielk1977 | ac245ec | 2005-01-14 13:50:11 +0000 | [diff] [blame] | 4696 | releasePage(pNew); |
| 4697 | return balance(pParent, 0); |
| 4698 | } |
drh | 615ae55 | 2005-01-16 23:21:00 +0000 | [diff] [blame] | 4699 | #endif /* SQLITE_OMIT_QUICKBALANCE */ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4700 | |
drh | c3b7057 | 2003-01-04 19:44:07 +0000 | [diff] [blame] | 4701 | /* |
drh | ab01f61 | 2004-05-22 02:55:23 +0000 | [diff] [blame] | 4702 | ** This routine redistributes Cells on pPage and up to NN*2 siblings |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 4703 | ** of pPage so that all pages have about the same amount of free space. |
drh | 0c6cc4e | 2004-06-15 02:13:26 +0000 | [diff] [blame] | 4704 | ** Usually NN siblings on either side of pPage is used in the balancing, |
| 4705 | ** though more siblings might come from one side if pPage is the first |
drh | ab01f61 | 2004-05-22 02:55:23 +0000 | [diff] [blame] | 4706 | ** or last child of its parent. If pPage has fewer than 2*NN siblings |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 4707 | ** (something which can only happen if pPage is the root page or a |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 4708 | ** child of root) then all available siblings participate in the balancing. |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 4709 | ** |
drh | 0c6cc4e | 2004-06-15 02:13:26 +0000 | [diff] [blame] | 4710 | ** The number of siblings of pPage might be increased or decreased by one or |
| 4711 | ** two in an effort to keep pages nearly full but not over full. The root page |
drh | ab01f61 | 2004-05-22 02:55:23 +0000 | [diff] [blame] | 4712 | ** is special and is allowed to be nearly empty. If pPage is |
drh | 8c42ca9 | 2001-06-22 19:15:00 +0000 | [diff] [blame] | 4713 | ** the root page, then the depth of the tree might be increased |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 4714 | ** or decreased by one, as necessary, to keep the root page from being |
drh | ab01f61 | 2004-05-22 02:55:23 +0000 | [diff] [blame] | 4715 | ** overfull or completely empty. |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 4716 | ** |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 4717 | ** Note that when this routine is called, some of the Cells on pPage |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 4718 | ** might not actually be stored in pPage->aData[]. This can happen |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 4719 | ** if the page is overfull. Part of the job of this routine is to |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 4720 | ** make sure all Cells for pPage once again fit in pPage->aData[]. |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 4721 | ** |
drh | 8c42ca9 | 2001-06-22 19:15:00 +0000 | [diff] [blame] | 4722 | ** In the course of balancing the siblings of pPage, the parent of pPage |
| 4723 | ** might become overfull or underfull. If that happens, then this routine |
| 4724 | ** is called recursively on the parent. |
| 4725 | ** |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4726 | ** If this routine fails for any reason, it might leave the database |
| 4727 | ** in a corrupted state. So if this routine fails, the database should |
| 4728 | ** be rolled back. |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 4729 | */ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4730 | static int balance_nonroot(MemPage *pPage){ |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 4731 | MemPage *pParent; /* The parent of pPage */ |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 4732 | BtShared *pBt; /* The whole database */ |
danielk1977 | 634f298 | 2005-03-28 08:44:07 +0000 | [diff] [blame] | 4733 | int nCell = 0; /* Number of cells in apCell[] */ |
| 4734 | int nMaxCells = 0; /* Allocated size of apCell, szCell, aFrom. */ |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 4735 | int nOld; /* Number of pages in apOld[] */ |
| 4736 | int nNew; /* Number of pages in apNew[] */ |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 4737 | int nDiv; /* Number of cells in apDiv[] */ |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 4738 | int i, j, k; /* Loop counters */ |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 4739 | int idx; /* Index of pPage in pParent->aCell[] */ |
| 4740 | int nxDiv; /* Next divider slot in pParent->aCell[] */ |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 4741 | int rc; /* The return code */ |
drh | 9102529 | 2004-05-03 19:49:32 +0000 | [diff] [blame] | 4742 | int leafCorrection; /* 4 if pPage is a leaf. 0 if not */ |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 4743 | int leafData; /* True if pPage is a leaf of a LEAFDATA tree */ |
drh | 9102529 | 2004-05-03 19:49:32 +0000 | [diff] [blame] | 4744 | int usableSpace; /* Bytes in pPage beyond the header */ |
| 4745 | int pageFlags; /* Value of pPage->aData[0] */ |
drh | 6019e16 | 2001-07-02 17:51:45 +0000 | [diff] [blame] | 4746 | int subtotal; /* Subtotal of bytes in cells on one page */ |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 4747 | int iSpace = 0; /* First unused byte of aSpace[] */ |
drh | c3b7057 | 2003-01-04 19:44:07 +0000 | [diff] [blame] | 4748 | MemPage *apOld[NB]; /* pPage and up to two siblings */ |
| 4749 | Pgno pgnoOld[NB]; /* Page numbers for each page in apOld[] */ |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 4750 | MemPage *apCopy[NB]; /* Private copies of apOld[] pages */ |
drh | a2fce64 | 2004-06-05 00:01:44 +0000 | [diff] [blame] | 4751 | MemPage *apNew[NB+2]; /* pPage and up to NB siblings after balancing */ |
| 4752 | Pgno pgnoNew[NB+2]; /* Page numbers for each page in apNew[] */ |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 4753 | u8 *apDiv[NB]; /* Divider cells in pParent */ |
drh | a2fce64 | 2004-06-05 00:01:44 +0000 | [diff] [blame] | 4754 | int cntNew[NB+2]; /* Index in aCell[] of cell after i-th page */ |
| 4755 | int szNew[NB+2]; /* Combined size of cells place on i-th page */ |
danielk1977 | 50f059b | 2005-03-29 02:54:03 +0000 | [diff] [blame] | 4756 | u8 **apCell = 0; /* All cells begin balanced */ |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 4757 | int *szCell; /* Local size of all cells in apCell[] */ |
| 4758 | u8 *aCopy[NB]; /* Space for holding data of apCopy[] */ |
| 4759 | u8 *aSpace; /* Space to hold copies of dividers cells */ |
danielk1977 | 4e17d14 | 2005-01-16 09:06:33 +0000 | [diff] [blame] | 4760 | #ifndef SQLITE_OMIT_AUTOVACUUM |
danielk1977 | ac11ee6 | 2005-01-15 12:45:51 +0000 | [diff] [blame] | 4761 | u8 *aFrom = 0; |
| 4762 | #endif |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 4763 | |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 4764 | assert( sqlite3_mutex_held(pPage->pBt->mutex) ); |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 4765 | |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 4766 | /* |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4767 | ** Find the parent page. |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 4768 | */ |
drh | 3a4c141 | 2004-05-09 20:40:11 +0000 | [diff] [blame] | 4769 | assert( pPage->isInit ); |
danielk1977 | 6e465eb | 2007-08-21 13:11:00 +0000 | [diff] [blame] | 4770 | assert( sqlite3PagerIswriteable(pPage->pDbPage) || pPage->nOverflow==1 ); |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 4771 | pBt = pPage->pBt; |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 4772 | pParent = pPage->pParent; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4773 | assert( pParent ); |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 4774 | if( SQLITE_OK!=(rc = sqlite3PagerWrite(pParent->pDbPage)) ){ |
danielk1977 | 07cb560 | 2006-01-20 10:55:05 +0000 | [diff] [blame] | 4775 | return rc; |
| 4776 | } |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4777 | TRACE(("BALANCE: begin page %d child of %d\n", pPage->pgno, pParent->pgno)); |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 4778 | |
drh | 615ae55 | 2005-01-16 23:21:00 +0000 | [diff] [blame] | 4779 | #ifndef SQLITE_OMIT_QUICKBALANCE |
drh | f222e71 | 2005-01-14 22:55:49 +0000 | [diff] [blame] | 4780 | /* |
| 4781 | ** A special case: If a new entry has just been inserted into a |
| 4782 | ** table (that is, a btree with integer keys and all data at the leaves) |
drh | 09d0deb | 2005-08-02 17:13:09 +0000 | [diff] [blame] | 4783 | ** and the new entry is the right-most entry in the tree (it has the |
drh | f222e71 | 2005-01-14 22:55:49 +0000 | [diff] [blame] | 4784 | ** largest key) then use the special balance_quick() routine for |
| 4785 | ** balancing. balance_quick() is much faster and results in a tighter |
| 4786 | ** packing of data in the common case. |
| 4787 | */ |
danielk1977 | ac245ec | 2005-01-14 13:50:11 +0000 | [diff] [blame] | 4788 | if( pPage->leaf && |
| 4789 | pPage->intKey && |
| 4790 | pPage->leafData && |
| 4791 | pPage->nOverflow==1 && |
| 4792 | pPage->aOvfl[0].idx==pPage->nCell && |
danielk1977 | ac11ee6 | 2005-01-15 12:45:51 +0000 | [diff] [blame] | 4793 | pPage->pParent->pgno!=1 && |
danielk1977 | ac245ec | 2005-01-14 13:50:11 +0000 | [diff] [blame] | 4794 | get4byte(&pParent->aData[pParent->hdrOffset+8])==pPage->pgno |
| 4795 | ){ |
danielk1977 | ac11ee6 | 2005-01-15 12:45:51 +0000 | [diff] [blame] | 4796 | /* |
| 4797 | ** TODO: Check the siblings to the left of pPage. It may be that |
| 4798 | ** they are not full and no new page is required. |
| 4799 | */ |
danielk1977 | ac245ec | 2005-01-14 13:50:11 +0000 | [diff] [blame] | 4800 | return balance_quick(pPage, pParent); |
| 4801 | } |
| 4802 | #endif |
| 4803 | |
danielk1977 | 6e465eb | 2007-08-21 13:11:00 +0000 | [diff] [blame] | 4804 | if( SQLITE_OK!=(rc = sqlite3PagerWrite(pPage->pDbPage)) ){ |
| 4805 | return rc; |
| 4806 | } |
| 4807 | |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 4808 | /* |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 4809 | ** Find the cell in the parent page whose left child points back |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 4810 | ** to pPage. The "idx" variable is the index of that cell. If pPage |
| 4811 | ** is the rightmost child of pParent then set idx to pParent->nCell |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 4812 | */ |
drh | bb49aba | 2003-01-04 18:53:27 +0000 | [diff] [blame] | 4813 | if( pParent->idxShift ){ |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 4814 | Pgno pgno; |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 4815 | pgno = pPage->pgno; |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 4816 | assert( pgno==sqlite3PagerPagenumber(pPage->pDbPage) ); |
drh | bb49aba | 2003-01-04 18:53:27 +0000 | [diff] [blame] | 4817 | for(idx=0; idx<pParent->nCell; idx++){ |
danielk1977 | 1cc5ed8 | 2007-05-16 17:28:43 +0000 | [diff] [blame] | 4818 | if( get4byte(findCell(pParent, idx))==pgno ){ |
drh | bb49aba | 2003-01-04 18:53:27 +0000 | [diff] [blame] | 4819 | break; |
| 4820 | } |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 4821 | } |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 4822 | assert( idx<pParent->nCell |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4823 | || get4byte(&pParent->aData[pParent->hdrOffset+8])==pgno ); |
drh | bb49aba | 2003-01-04 18:53:27 +0000 | [diff] [blame] | 4824 | }else{ |
| 4825 | idx = pPage->idxParent; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 4826 | } |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 4827 | |
| 4828 | /* |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 4829 | ** Initialize variables so that it will be safe to jump |
drh | 5edc312 | 2001-09-13 21:53:09 +0000 | [diff] [blame] | 4830 | ** directly to balance_cleanup at any moment. |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 4831 | */ |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 4832 | nOld = nNew = 0; |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 4833 | sqlite3PagerRef(pParent->pDbPage); |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 4834 | |
| 4835 | /* |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 4836 | ** Find sibling pages to pPage and the cells in pParent that divide |
drh | c3b7057 | 2003-01-04 19:44:07 +0000 | [diff] [blame] | 4837 | ** the siblings. An attempt is made to find NN siblings on either |
| 4838 | ** side of pPage. More siblings are taken from one side, however, if |
| 4839 | ** pPage there are fewer than NN siblings on the other side. If pParent |
| 4840 | ** has NB or fewer children then all children of pParent are taken. |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 4841 | */ |
drh | c3b7057 | 2003-01-04 19:44:07 +0000 | [diff] [blame] | 4842 | nxDiv = idx - NN; |
| 4843 | if( nxDiv + NB > pParent->nCell ){ |
| 4844 | nxDiv = pParent->nCell - NB + 1; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 4845 | } |
drh | c3b7057 | 2003-01-04 19:44:07 +0000 | [diff] [blame] | 4846 | if( nxDiv<0 ){ |
| 4847 | nxDiv = 0; |
| 4848 | } |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 4849 | nDiv = 0; |
drh | c3b7057 | 2003-01-04 19:44:07 +0000 | [diff] [blame] | 4850 | for(i=0, k=nxDiv; i<NB; i++, k++){ |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 4851 | if( k<pParent->nCell ){ |
danielk1977 | 1cc5ed8 | 2007-05-16 17:28:43 +0000 | [diff] [blame] | 4852 | apDiv[i] = findCell(pParent, k); |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 4853 | nDiv++; |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 4854 | assert( !pParent->leaf ); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4855 | pgnoOld[i] = get4byte(apDiv[i]); |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 4856 | }else if( k==pParent->nCell ){ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4857 | pgnoOld[i] = get4byte(&pParent->aData[pParent->hdrOffset+8]); |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 4858 | }else{ |
| 4859 | break; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 4860 | } |
drh | de64713 | 2004-05-07 17:57:49 +0000 | [diff] [blame] | 4861 | rc = getAndInitPage(pBt, pgnoOld[i], &apOld[i], pParent); |
drh | 6019e16 | 2001-07-02 17:51:45 +0000 | [diff] [blame] | 4862 | if( rc ) goto balance_cleanup; |
drh | 428ae8c | 2003-01-04 16:48:09 +0000 | [diff] [blame] | 4863 | apOld[i]->idxParent = k; |
drh | 9102529 | 2004-05-03 19:49:32 +0000 | [diff] [blame] | 4864 | apCopy[i] = 0; |
| 4865 | assert( i==nOld ); |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 4866 | nOld++; |
danielk1977 | 634f298 | 2005-03-28 08:44:07 +0000 | [diff] [blame] | 4867 | nMaxCells += 1+apOld[i]->nCell+apOld[i]->nOverflow; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 4868 | } |
| 4869 | |
drh | 8d97f1f | 2005-05-05 18:14:13 +0000 | [diff] [blame] | 4870 | /* Make nMaxCells a multiple of 2 in order to preserve 8-byte |
| 4871 | ** alignment */ |
| 4872 | nMaxCells = (nMaxCells + 1)&~1; |
| 4873 | |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 4874 | /* |
danielk1977 | 634f298 | 2005-03-28 08:44:07 +0000 | [diff] [blame] | 4875 | ** Allocate space for memory structures |
| 4876 | */ |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 4877 | apCell = sqlite3_malloc( |
danielk1977 | 634f298 | 2005-03-28 08:44:07 +0000 | [diff] [blame] | 4878 | nMaxCells*sizeof(u8*) /* apCell */ |
| 4879 | + nMaxCells*sizeof(int) /* szCell */ |
drh | c96d853 | 2005-05-03 12:30:33 +0000 | [diff] [blame] | 4880 | + ROUND8(sizeof(MemPage))*NB /* aCopy */ |
drh | 07d183d | 2005-05-01 22:52:42 +0000 | [diff] [blame] | 4881 | + pBt->pageSize*(5+NB) /* aSpace */ |
drh | c96d853 | 2005-05-03 12:30:33 +0000 | [diff] [blame] | 4882 | + (ISAUTOVACUUM ? nMaxCells : 0) /* aFrom */ |
danielk1977 | 634f298 | 2005-03-28 08:44:07 +0000 | [diff] [blame] | 4883 | ); |
| 4884 | if( apCell==0 ){ |
| 4885 | rc = SQLITE_NOMEM; |
| 4886 | goto balance_cleanup; |
| 4887 | } |
| 4888 | szCell = (int*)&apCell[nMaxCells]; |
| 4889 | aCopy[0] = (u8*)&szCell[nMaxCells]; |
drh | c96d853 | 2005-05-03 12:30:33 +0000 | [diff] [blame] | 4890 | assert( ((aCopy[0] - (u8*)apCell) & 7)==0 ); /* 8-byte alignment required */ |
danielk1977 | 634f298 | 2005-03-28 08:44:07 +0000 | [diff] [blame] | 4891 | for(i=1; i<NB; i++){ |
drh | c96d853 | 2005-05-03 12:30:33 +0000 | [diff] [blame] | 4892 | aCopy[i] = &aCopy[i-1][pBt->pageSize+ROUND8(sizeof(MemPage))]; |
| 4893 | assert( ((aCopy[i] - (u8*)apCell) & 7)==0 ); /* 8-byte alignment required */ |
danielk1977 | 634f298 | 2005-03-28 08:44:07 +0000 | [diff] [blame] | 4894 | } |
drh | c96d853 | 2005-05-03 12:30:33 +0000 | [diff] [blame] | 4895 | aSpace = &aCopy[NB-1][pBt->pageSize+ROUND8(sizeof(MemPage))]; |
| 4896 | assert( ((aSpace - (u8*)apCell) & 7)==0 ); /* 8-byte alignment required */ |
danielk1977 | 634f298 | 2005-03-28 08:44:07 +0000 | [diff] [blame] | 4897 | #ifndef SQLITE_OMIT_AUTOVACUUM |
| 4898 | if( pBt->autoVacuum ){ |
drh | 07d183d | 2005-05-01 22:52:42 +0000 | [diff] [blame] | 4899 | aFrom = &aSpace[5*pBt->pageSize]; |
danielk1977 | 634f298 | 2005-03-28 08:44:07 +0000 | [diff] [blame] | 4900 | } |
| 4901 | #endif |
| 4902 | |
| 4903 | /* |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 4904 | ** Make copies of the content of pPage and its siblings into aOld[]. |
| 4905 | ** The rest of this function will use data from the copies rather |
| 4906 | ** that the original pages since the original pages will be in the |
| 4907 | ** process of being overwritten. |
| 4908 | */ |
| 4909 | for(i=0; i<nOld; i++){ |
drh | bf4bca5 | 2007-09-06 22:19:14 +0000 | [diff] [blame] | 4910 | MemPage *p = apCopy[i] = (MemPage*)aCopy[i]; |
| 4911 | memcpy(p, apOld[i], sizeof(MemPage)); |
| 4912 | p->aData = (void*)&p[1]; |
| 4913 | memcpy(p->aData, apOld[i]->aData, pBt->pageSize); |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 4914 | } |
| 4915 | |
| 4916 | /* |
| 4917 | ** Load pointers to all cells on sibling pages and the divider cells |
| 4918 | ** into the local apCell[] array. Make copies of the divider cells |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 4919 | ** into space obtained form aSpace[] and remove the the divider Cells |
| 4920 | ** from pParent. |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 4921 | ** |
| 4922 | ** If the siblings are on leaf pages, then the child pointers of the |
| 4923 | ** divider cells are stripped from the cells before they are copied |
drh | 96f5b76 | 2004-05-16 16:24:36 +0000 | [diff] [blame] | 4924 | ** into aSpace[]. In this way, all cells in apCell[] are without |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 4925 | ** child pointers. If siblings are not leaves, then all cell in |
| 4926 | ** apCell[] include child pointers. Either way, all cells in apCell[] |
| 4927 | ** are alike. |
drh | 96f5b76 | 2004-05-16 16:24:36 +0000 | [diff] [blame] | 4928 | ** |
| 4929 | ** leafCorrection: 4 if pPage is a leaf. 0 if pPage is not a leaf. |
| 4930 | ** leafData: 1 if pPage holds key+data and pParent holds only keys. |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 4931 | */ |
| 4932 | nCell = 0; |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 4933 | leafCorrection = pPage->leaf*4; |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 4934 | leafData = pPage->leafData && pPage->leaf; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 4935 | for(i=0; i<nOld; i++){ |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 4936 | MemPage *pOld = apCopy[i]; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4937 | int limit = pOld->nCell+pOld->nOverflow; |
| 4938 | for(j=0; j<limit; j++){ |
danielk1977 | 634f298 | 2005-03-28 08:44:07 +0000 | [diff] [blame] | 4939 | assert( nCell<nMaxCells ); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4940 | apCell[nCell] = findOverflowCell(pOld, j); |
| 4941 | szCell[nCell] = cellSizePtr(pOld, apCell[nCell]); |
danielk1977 | ac11ee6 | 2005-01-15 12:45:51 +0000 | [diff] [blame] | 4942 | #ifndef SQLITE_OMIT_AUTOVACUUM |
| 4943 | if( pBt->autoVacuum ){ |
| 4944 | int a; |
| 4945 | aFrom[nCell] = i; |
| 4946 | for(a=0; a<pOld->nOverflow; a++){ |
| 4947 | if( pOld->aOvfl[a].pCell==apCell[nCell] ){ |
| 4948 | aFrom[nCell] = 0xFF; |
| 4949 | break; |
| 4950 | } |
| 4951 | } |
| 4952 | } |
| 4953 | #endif |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 4954 | nCell++; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 4955 | } |
| 4956 | if( i<nOld-1 ){ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4957 | int sz = cellSizePtr(pParent, apDiv[i]); |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 4958 | if( leafData ){ |
drh | 96f5b76 | 2004-05-16 16:24:36 +0000 | [diff] [blame] | 4959 | /* With the LEAFDATA flag, pParent cells hold only INTKEYs that |
| 4960 | ** are duplicates of keys on the child pages. We need to remove |
| 4961 | ** the divider cells from pParent, but the dividers cells are not |
| 4962 | ** added to apCell[] because they are duplicates of child cells. |
| 4963 | */ |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 4964 | dropCell(pParent, nxDiv, sz); |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 4965 | }else{ |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 4966 | u8 *pTemp; |
danielk1977 | 634f298 | 2005-03-28 08:44:07 +0000 | [diff] [blame] | 4967 | assert( nCell<nMaxCells ); |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 4968 | szCell[nCell] = sz; |
| 4969 | pTemp = &aSpace[iSpace]; |
| 4970 | iSpace += sz; |
drh | 07d183d | 2005-05-01 22:52:42 +0000 | [diff] [blame] | 4971 | assert( iSpace<=pBt->pageSize*5 ); |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 4972 | memcpy(pTemp, apDiv[i], sz); |
| 4973 | apCell[nCell] = pTemp+leafCorrection; |
danielk1977 | ac11ee6 | 2005-01-15 12:45:51 +0000 | [diff] [blame] | 4974 | #ifndef SQLITE_OMIT_AUTOVACUUM |
| 4975 | if( pBt->autoVacuum ){ |
| 4976 | aFrom[nCell] = 0xFF; |
| 4977 | } |
| 4978 | #endif |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 4979 | dropCell(pParent, nxDiv, sz); |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 4980 | szCell[nCell] -= leafCorrection; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4981 | assert( get4byte(pTemp)==pgnoOld[i] ); |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 4982 | if( !pOld->leaf ){ |
| 4983 | assert( leafCorrection==0 ); |
| 4984 | /* The right pointer of the child page pOld becomes the left |
| 4985 | ** pointer of the divider cell */ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4986 | memcpy(apCell[nCell], &pOld->aData[pOld->hdrOffset+8], 4); |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 4987 | }else{ |
| 4988 | assert( leafCorrection==4 ); |
danielk1977 | 39c9604 | 2007-05-12 10:41:47 +0000 | [diff] [blame] | 4989 | if( szCell[nCell]<4 ){ |
| 4990 | /* Do not allow any cells smaller than 4 bytes. */ |
| 4991 | szCell[nCell] = 4; |
| 4992 | } |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 4993 | } |
| 4994 | nCell++; |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 4995 | } |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 4996 | } |
| 4997 | } |
| 4998 | |
| 4999 | /* |
drh | 6019e16 | 2001-07-02 17:51:45 +0000 | [diff] [blame] | 5000 | ** Figure out the number of pages needed to hold all nCell cells. |
| 5001 | ** Store this number in "k". Also compute szNew[] which is the total |
| 5002 | ** size of all cells on the i-th page and cntNew[] which is the index |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 5003 | ** in apCell[] of the cell that divides page i from page i+1. |
drh | 6019e16 | 2001-07-02 17:51:45 +0000 | [diff] [blame] | 5004 | ** cntNew[k] should equal nCell. |
| 5005 | ** |
drh | 96f5b76 | 2004-05-16 16:24:36 +0000 | [diff] [blame] | 5006 | ** Values computed by this block: |
| 5007 | ** |
| 5008 | ** k: The total number of sibling pages |
| 5009 | ** szNew[i]: Spaced used on the i-th sibling page. |
| 5010 | ** cntNew[i]: Index in apCell[] and szCell[] for the first cell to |
| 5011 | ** the right of the i-th sibling page. |
| 5012 | ** usableSpace: Number of bytes of space available on each sibling. |
| 5013 | ** |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 5014 | */ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5015 | usableSpace = pBt->usableSize - 12 + leafCorrection; |
drh | 6019e16 | 2001-07-02 17:51:45 +0000 | [diff] [blame] | 5016 | for(subtotal=k=i=0; i<nCell; i++){ |
danielk1977 | 634f298 | 2005-03-28 08:44:07 +0000 | [diff] [blame] | 5017 | assert( i<nMaxCells ); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5018 | subtotal += szCell[i] + 2; |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 5019 | if( subtotal > usableSpace ){ |
drh | 6019e16 | 2001-07-02 17:51:45 +0000 | [diff] [blame] | 5020 | szNew[k] = subtotal - szCell[i]; |
| 5021 | cntNew[k] = i; |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 5022 | if( leafData ){ i--; } |
drh | 6019e16 | 2001-07-02 17:51:45 +0000 | [diff] [blame] | 5023 | subtotal = 0; |
| 5024 | k++; |
| 5025 | } |
| 5026 | } |
| 5027 | szNew[k] = subtotal; |
| 5028 | cntNew[k] = nCell; |
| 5029 | k++; |
drh | 96f5b76 | 2004-05-16 16:24:36 +0000 | [diff] [blame] | 5030 | |
| 5031 | /* |
| 5032 | ** The packing computed by the previous block is biased toward the siblings |
| 5033 | ** on the left side. The left siblings are always nearly full, while the |
| 5034 | ** right-most sibling might be nearly empty. This block of code attempts |
| 5035 | ** to adjust the packing of siblings to get a better balance. |
| 5036 | ** |
| 5037 | ** This adjustment is more than an optimization. The packing above might |
| 5038 | ** be so out of balance as to be illegal. For example, the right-most |
| 5039 | ** sibling might be completely empty. This adjustment is not optional. |
| 5040 | */ |
drh | 6019e16 | 2001-07-02 17:51:45 +0000 | [diff] [blame] | 5041 | for(i=k-1; i>0; i--){ |
drh | 96f5b76 | 2004-05-16 16:24:36 +0000 | [diff] [blame] | 5042 | int szRight = szNew[i]; /* Size of sibling on the right */ |
| 5043 | int szLeft = szNew[i-1]; /* Size of sibling on the left */ |
| 5044 | int r; /* Index of right-most cell in left sibling */ |
| 5045 | int d; /* Index of first cell to the left of right sibling */ |
| 5046 | |
| 5047 | r = cntNew[i-1] - 1; |
| 5048 | d = r + 1 - leafData; |
danielk1977 | 634f298 | 2005-03-28 08:44:07 +0000 | [diff] [blame] | 5049 | assert( d<nMaxCells ); |
| 5050 | assert( r<nMaxCells ); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5051 | while( szRight==0 || szRight+szCell[d]+2<=szLeft-(szCell[r]+2) ){ |
| 5052 | szRight += szCell[d] + 2; |
| 5053 | szLeft -= szCell[r] + 2; |
drh | 6019e16 | 2001-07-02 17:51:45 +0000 | [diff] [blame] | 5054 | cntNew[i-1]--; |
drh | 96f5b76 | 2004-05-16 16:24:36 +0000 | [diff] [blame] | 5055 | r = cntNew[i-1] - 1; |
| 5056 | d = r + 1 - leafData; |
drh | 6019e16 | 2001-07-02 17:51:45 +0000 | [diff] [blame] | 5057 | } |
drh | 96f5b76 | 2004-05-16 16:24:36 +0000 | [diff] [blame] | 5058 | szNew[i] = szRight; |
| 5059 | szNew[i-1] = szLeft; |
drh | 6019e16 | 2001-07-02 17:51:45 +0000 | [diff] [blame] | 5060 | } |
drh | 09d0deb | 2005-08-02 17:13:09 +0000 | [diff] [blame] | 5061 | |
| 5062 | /* Either we found one or more cells (cntnew[0])>0) or we are the |
| 5063 | ** a virtual root page. A virtual root page is when the real root |
| 5064 | ** page is page 1 and we are the only child of that page. |
| 5065 | */ |
| 5066 | assert( cntNew[0]>0 || (pParent->pgno==1 && pParent->nCell==0) ); |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 5067 | |
| 5068 | /* |
drh | 6b30867 | 2002-07-08 02:16:37 +0000 | [diff] [blame] | 5069 | ** Allocate k new pages. Reuse old pages where possible. |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 5070 | */ |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 5071 | assert( pPage->pgno>1 ); |
| 5072 | pageFlags = pPage->aData[0]; |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 5073 | for(i=0; i<k; i++){ |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 5074 | MemPage *pNew; |
drh | 6b30867 | 2002-07-08 02:16:37 +0000 | [diff] [blame] | 5075 | if( i<nOld ){ |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 5076 | pNew = apNew[i] = apOld[i]; |
drh | 6b30867 | 2002-07-08 02:16:37 +0000 | [diff] [blame] | 5077 | pgnoNew[i] = pgnoOld[i]; |
| 5078 | apOld[i] = 0; |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 5079 | rc = sqlite3PagerWrite(pNew->pDbPage); |
drh | f534544 | 2007-04-09 12:45:02 +0000 | [diff] [blame] | 5080 | nNew++; |
danielk1977 | 2812956 | 2005-01-11 10:25:06 +0000 | [diff] [blame] | 5081 | if( rc ) goto balance_cleanup; |
drh | 6b30867 | 2002-07-08 02:16:37 +0000 | [diff] [blame] | 5082 | }else{ |
drh | 7aa8f85 | 2006-03-28 00:24:44 +0000 | [diff] [blame] | 5083 | assert( i>0 ); |
drh | 4f0c587 | 2007-03-26 22:05:01 +0000 | [diff] [blame] | 5084 | rc = allocateBtreePage(pBt, &pNew, &pgnoNew[i], pgnoNew[i-1], 0); |
drh | 6b30867 | 2002-07-08 02:16:37 +0000 | [diff] [blame] | 5085 | if( rc ) goto balance_cleanup; |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 5086 | apNew[i] = pNew; |
drh | f534544 | 2007-04-09 12:45:02 +0000 | [diff] [blame] | 5087 | nNew++; |
drh | 6b30867 | 2002-07-08 02:16:37 +0000 | [diff] [blame] | 5088 | } |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 5089 | zeroPage(pNew, pageFlags); |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 5090 | } |
| 5091 | |
danielk1977 | 299b187 | 2004-11-22 10:02:10 +0000 | [diff] [blame] | 5092 | /* Free any old pages that were not reused as new pages. |
| 5093 | */ |
| 5094 | while( i<nOld ){ |
| 5095 | rc = freePage(apOld[i]); |
| 5096 | if( rc ) goto balance_cleanup; |
| 5097 | releasePage(apOld[i]); |
| 5098 | apOld[i] = 0; |
| 5099 | i++; |
| 5100 | } |
| 5101 | |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 5102 | /* |
drh | f9ffac9 | 2002-03-02 19:00:31 +0000 | [diff] [blame] | 5103 | ** Put the new pages in accending order. This helps to |
| 5104 | ** keep entries in the disk file in order so that a scan |
| 5105 | ** of the table is a linear scan through the file. That |
| 5106 | ** in turn helps the operating system to deliver pages |
| 5107 | ** from the disk more rapidly. |
| 5108 | ** |
| 5109 | ** An O(n^2) insertion sort algorithm is used, but since |
drh | c3b7057 | 2003-01-04 19:44:07 +0000 | [diff] [blame] | 5110 | ** n is never more than NB (a small constant), that should |
| 5111 | ** not be a problem. |
drh | f9ffac9 | 2002-03-02 19:00:31 +0000 | [diff] [blame] | 5112 | ** |
drh | c3b7057 | 2003-01-04 19:44:07 +0000 | [diff] [blame] | 5113 | ** When NB==3, this one optimization makes the database |
| 5114 | ** about 25% faster for large insertions and deletions. |
drh | f9ffac9 | 2002-03-02 19:00:31 +0000 | [diff] [blame] | 5115 | */ |
| 5116 | for(i=0; i<k-1; i++){ |
| 5117 | int minV = pgnoNew[i]; |
| 5118 | int minI = i; |
| 5119 | for(j=i+1; j<k; j++){ |
drh | 7d02cb7 | 2003-06-04 16:24:39 +0000 | [diff] [blame] | 5120 | if( pgnoNew[j]<(unsigned)minV ){ |
drh | f9ffac9 | 2002-03-02 19:00:31 +0000 | [diff] [blame] | 5121 | minI = j; |
| 5122 | minV = pgnoNew[j]; |
| 5123 | } |
| 5124 | } |
| 5125 | if( minI>i ){ |
| 5126 | int t; |
| 5127 | MemPage *pT; |
| 5128 | t = pgnoNew[i]; |
| 5129 | pT = apNew[i]; |
| 5130 | pgnoNew[i] = pgnoNew[minI]; |
| 5131 | apNew[i] = apNew[minI]; |
| 5132 | pgnoNew[minI] = t; |
| 5133 | apNew[minI] = pT; |
| 5134 | } |
| 5135 | } |
drh | a2fce64 | 2004-06-05 00:01:44 +0000 | [diff] [blame] | 5136 | TRACE(("BALANCE: old: %d %d %d new: %d(%d) %d(%d) %d(%d) %d(%d) %d(%d)\n", |
drh | 24cd67e | 2004-05-10 16:18:47 +0000 | [diff] [blame] | 5137 | pgnoOld[0], |
| 5138 | nOld>=2 ? pgnoOld[1] : 0, |
| 5139 | nOld>=3 ? pgnoOld[2] : 0, |
drh | 10c0fa6 | 2004-05-18 12:50:17 +0000 | [diff] [blame] | 5140 | pgnoNew[0], szNew[0], |
| 5141 | nNew>=2 ? pgnoNew[1] : 0, nNew>=2 ? szNew[1] : 0, |
| 5142 | nNew>=3 ? pgnoNew[2] : 0, nNew>=3 ? szNew[2] : 0, |
drh | a2fce64 | 2004-06-05 00:01:44 +0000 | [diff] [blame] | 5143 | nNew>=4 ? pgnoNew[3] : 0, nNew>=4 ? szNew[3] : 0, |
| 5144 | nNew>=5 ? pgnoNew[4] : 0, nNew>=5 ? szNew[4] : 0)); |
drh | 24cd67e | 2004-05-10 16:18:47 +0000 | [diff] [blame] | 5145 | |
drh | f9ffac9 | 2002-03-02 19:00:31 +0000 | [diff] [blame] | 5146 | /* |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 5147 | ** Evenly distribute the data in apCell[] across the new pages. |
| 5148 | ** Insert divider cells into pParent as necessary. |
| 5149 | */ |
| 5150 | j = 0; |
| 5151 | for(i=0; i<nNew; i++){ |
danielk1977 | ac11ee6 | 2005-01-15 12:45:51 +0000 | [diff] [blame] | 5152 | /* Assemble the new sibling page. */ |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 5153 | MemPage *pNew = apNew[i]; |
drh | 19642e5 | 2005-03-29 13:17:45 +0000 | [diff] [blame] | 5154 | assert( j<nMaxCells ); |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 5155 | assert( pNew->pgno==pgnoNew[i] ); |
drh | fa1a98a | 2004-05-14 19:08:17 +0000 | [diff] [blame] | 5156 | assemblePage(pNew, cntNew[i]-j, &apCell[j], &szCell[j]); |
drh | 09d0deb | 2005-08-02 17:13:09 +0000 | [diff] [blame] | 5157 | assert( pNew->nCell>0 || (nNew==1 && cntNew[0]==0) ); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5158 | assert( pNew->nOverflow==0 ); |
danielk1977 | ac11ee6 | 2005-01-15 12:45:51 +0000 | [diff] [blame] | 5159 | |
| 5160 | #ifndef SQLITE_OMIT_AUTOVACUUM |
| 5161 | /* If this is an auto-vacuum database, update the pointer map entries |
| 5162 | ** that point to the siblings that were rearranged. These can be: left |
| 5163 | ** children of cells, the right-child of the page, or overflow pages |
| 5164 | ** pointed to by cells. |
| 5165 | */ |
| 5166 | if( pBt->autoVacuum ){ |
| 5167 | for(k=j; k<cntNew[i]; k++){ |
danielk1977 | 634f298 | 2005-03-28 08:44:07 +0000 | [diff] [blame] | 5168 | assert( k<nMaxCells ); |
danielk1977 | ac11ee6 | 2005-01-15 12:45:51 +0000 | [diff] [blame] | 5169 | if( aFrom[k]==0xFF || apCopy[aFrom[k]]->pgno!=pNew->pgno ){ |
danielk1977 | 79a40da | 2005-01-16 08:00:01 +0000 | [diff] [blame] | 5170 | rc = ptrmapPutOvfl(pNew, k-j); |
| 5171 | if( rc!=SQLITE_OK ){ |
| 5172 | goto balance_cleanup; |
danielk1977 | ac11ee6 | 2005-01-15 12:45:51 +0000 | [diff] [blame] | 5173 | } |
| 5174 | } |
| 5175 | } |
| 5176 | } |
| 5177 | #endif |
| 5178 | |
| 5179 | j = cntNew[i]; |
| 5180 | |
| 5181 | /* If the sibling page assembled above was not the right-most sibling, |
| 5182 | ** insert a divider cell into the parent page. |
| 5183 | */ |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 5184 | if( i<nNew-1 && j<nCell ){ |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 5185 | u8 *pCell; |
drh | 24cd67e | 2004-05-10 16:18:47 +0000 | [diff] [blame] | 5186 | u8 *pTemp; |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 5187 | int sz; |
danielk1977 | 634f298 | 2005-03-28 08:44:07 +0000 | [diff] [blame] | 5188 | |
| 5189 | assert( j<nMaxCells ); |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 5190 | pCell = apCell[j]; |
| 5191 | sz = szCell[j] + leafCorrection; |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 5192 | if( !pNew->leaf ){ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5193 | memcpy(&pNew->aData[8], pCell, 4); |
drh | 24cd67e | 2004-05-10 16:18:47 +0000 | [diff] [blame] | 5194 | pTemp = 0; |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 5195 | }else if( leafData ){ |
drh | fd131da | 2007-08-07 17:13:03 +0000 | [diff] [blame] | 5196 | /* If the tree is a leaf-data tree, and the siblings are leaves, |
danielk1977 | ac11ee6 | 2005-01-15 12:45:51 +0000 | [diff] [blame] | 5197 | ** then there is no divider cell in apCell[]. Instead, the divider |
| 5198 | ** cell consists of the integer key for the right-most cell of |
| 5199 | ** the sibling-page assembled above only. |
| 5200 | */ |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 5201 | CellInfo info; |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 5202 | j--; |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 5203 | sqlite3BtreeParseCellPtr(pNew, apCell[j], &info); |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 5204 | pCell = &aSpace[iSpace]; |
drh | b026e05 | 2007-05-02 01:34:31 +0000 | [diff] [blame] | 5205 | fillInCell(pParent, pCell, 0, info.nKey, 0, 0, 0, &sz); |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 5206 | iSpace += sz; |
drh | 07d183d | 2005-05-01 22:52:42 +0000 | [diff] [blame] | 5207 | assert( iSpace<=pBt->pageSize*5 ); |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 5208 | pTemp = 0; |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 5209 | }else{ |
| 5210 | pCell -= 4; |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 5211 | pTemp = &aSpace[iSpace]; |
| 5212 | iSpace += sz; |
drh | 07d183d | 2005-05-01 22:52:42 +0000 | [diff] [blame] | 5213 | assert( iSpace<=pBt->pageSize*5 ); |
danielk1977 | 4aeff62 | 2007-05-12 09:30:47 +0000 | [diff] [blame] | 5214 | /* Obscure case for non-leaf-data trees: If the cell at pCell was |
| 5215 | ** previously stored on a leaf node, and it's reported size was 4 |
| 5216 | ** bytes, then it may actually be smaller than this |
| 5217 | ** (see sqlite3BtreeParseCellPtr(), 4 bytes is the minimum size of |
| 5218 | ** any cell). But it's important to pass the correct size to |
| 5219 | ** insertCell(), so reparse the cell now. |
| 5220 | ** |
| 5221 | ** Note that this can never happen in an SQLite data file, as all |
| 5222 | ** cells are at least 4 bytes. It only happens in b-trees used |
| 5223 | ** to evaluate "IN (SELECT ...)" and similar clauses. |
| 5224 | */ |
| 5225 | if( szCell[j]==4 ){ |
| 5226 | assert(leafCorrection==4); |
| 5227 | sz = cellSizePtr(pParent, pCell); |
| 5228 | } |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 5229 | } |
danielk1977 | a3ad5e7 | 2005-01-07 08:56:44 +0000 | [diff] [blame] | 5230 | rc = insertCell(pParent, nxDiv, pCell, sz, pTemp, 4); |
danielk1977 | e80463b | 2004-11-03 03:01:16 +0000 | [diff] [blame] | 5231 | if( rc!=SQLITE_OK ) goto balance_cleanup; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5232 | put4byte(findOverflowCell(pParent,nxDiv), pNew->pgno); |
danielk1977 | ac11ee6 | 2005-01-15 12:45:51 +0000 | [diff] [blame] | 5233 | #ifndef SQLITE_OMIT_AUTOVACUUM |
| 5234 | /* If this is an auto-vacuum database, and not a leaf-data tree, |
| 5235 | ** then update the pointer map with an entry for the overflow page |
| 5236 | ** that the cell just inserted points to (if any). |
| 5237 | */ |
| 5238 | if( pBt->autoVacuum && !leafData ){ |
danielk1977 | 79a40da | 2005-01-16 08:00:01 +0000 | [diff] [blame] | 5239 | rc = ptrmapPutOvfl(pParent, nxDiv); |
| 5240 | if( rc!=SQLITE_OK ){ |
| 5241 | goto balance_cleanup; |
danielk1977 | ac11ee6 | 2005-01-15 12:45:51 +0000 | [diff] [blame] | 5242 | } |
| 5243 | } |
| 5244 | #endif |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 5245 | j++; |
| 5246 | nxDiv++; |
| 5247 | } |
| 5248 | } |
drh | 6019e16 | 2001-07-02 17:51:45 +0000 | [diff] [blame] | 5249 | assert( j==nCell ); |
drh | 7aa8f85 | 2006-03-28 00:24:44 +0000 | [diff] [blame] | 5250 | assert( nOld>0 ); |
| 5251 | assert( nNew>0 ); |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 5252 | if( (pageFlags & PTF_LEAF)==0 ){ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5253 | memcpy(&apNew[nNew-1]->aData[8], &apCopy[nOld-1]->aData[8], 4); |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 5254 | } |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5255 | if( nxDiv==pParent->nCell+pParent->nOverflow ){ |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 5256 | /* Right-most sibling is the right-most child of pParent */ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5257 | put4byte(&pParent->aData[pParent->hdrOffset+8], pgnoNew[nNew-1]); |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 5258 | }else{ |
| 5259 | /* Right-most sibling is the left child of the first entry in pParent |
| 5260 | ** past the right-most divider entry */ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5261 | put4byte(findOverflowCell(pParent, nxDiv), pgnoNew[nNew-1]); |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 5262 | } |
| 5263 | |
| 5264 | /* |
| 5265 | ** Reparent children of all cells. |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 5266 | */ |
| 5267 | for(i=0; i<nNew; i++){ |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 5268 | rc = reparentChildPages(apNew[i]); |
| 5269 | if( rc!=SQLITE_OK ) goto balance_cleanup; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 5270 | } |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 5271 | rc = reparentChildPages(pParent); |
| 5272 | if( rc!=SQLITE_OK ) goto balance_cleanup; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 5273 | |
| 5274 | /* |
drh | 3a4c141 | 2004-05-09 20:40:11 +0000 | [diff] [blame] | 5275 | ** Balance the parent page. Note that the current page (pPage) might |
danielk1977 | ac11ee6 | 2005-01-15 12:45:51 +0000 | [diff] [blame] | 5276 | ** have been added to the freelist so it might no longer be initialized. |
drh | 3a4c141 | 2004-05-09 20:40:11 +0000 | [diff] [blame] | 5277 | ** But the parent page will always be initialized. |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 5278 | */ |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 5279 | assert( pParent->isInit ); |
danielk1977 | ac245ec | 2005-01-14 13:50:11 +0000 | [diff] [blame] | 5280 | rc = balance(pParent, 0); |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 5281 | |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 5282 | /* |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 5283 | ** Cleanup before returning. |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 5284 | */ |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 5285 | balance_cleanup: |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 5286 | sqlite3_free(apCell); |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 5287 | for(i=0; i<nOld; i++){ |
drh | 9102529 | 2004-05-03 19:49:32 +0000 | [diff] [blame] | 5288 | releasePage(apOld[i]); |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 5289 | } |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 5290 | for(i=0; i<nNew; i++){ |
drh | 9102529 | 2004-05-03 19:49:32 +0000 | [diff] [blame] | 5291 | releasePage(apNew[i]); |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 5292 | } |
drh | 9102529 | 2004-05-03 19:49:32 +0000 | [diff] [blame] | 5293 | releasePage(pParent); |
drh | 3a4c141 | 2004-05-09 20:40:11 +0000 | [diff] [blame] | 5294 | TRACE(("BALANCE: finished with %d: old=%d new=%d cells=%d\n", |
| 5295 | pPage->pgno, nOld, nNew, nCell)); |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 5296 | return rc; |
| 5297 | } |
| 5298 | |
| 5299 | /* |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5300 | ** This routine is called for the root page of a btree when the root |
| 5301 | ** page contains no cells. This is an opportunity to make the tree |
| 5302 | ** shallower by one level. |
| 5303 | */ |
| 5304 | static int balance_shallower(MemPage *pPage){ |
| 5305 | MemPage *pChild; /* The only child page of pPage */ |
| 5306 | Pgno pgnoChild; /* Page number for pChild */ |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 5307 | int rc = SQLITE_OK; /* Return code from subprocedures */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 5308 | BtShared *pBt; /* The main BTree structure */ |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 5309 | int mxCellPerPage; /* Maximum number of cells per page */ |
| 5310 | u8 **apCell; /* All cells from pages being balanced */ |
| 5311 | int *szCell; /* Local size of all cells */ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5312 | |
| 5313 | assert( pPage->pParent==0 ); |
| 5314 | assert( pPage->nCell==0 ); |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 5315 | assert( sqlite3_mutex_held(pPage->pBt->mutex) ); |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 5316 | pBt = pPage->pBt; |
| 5317 | mxCellPerPage = MX_CELL(pBt); |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 5318 | apCell = sqlite3_malloc( mxCellPerPage*(sizeof(u8*)+sizeof(int)) ); |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 5319 | if( apCell==0 ) return SQLITE_NOMEM; |
| 5320 | szCell = (int*)&apCell[mxCellPerPage]; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5321 | if( pPage->leaf ){ |
| 5322 | /* The table is completely empty */ |
| 5323 | TRACE(("BALANCE: empty table %d\n", pPage->pgno)); |
| 5324 | }else{ |
| 5325 | /* The root page is empty but has one child. Transfer the |
| 5326 | ** information from that one child into the root page if it |
| 5327 | ** will fit. This reduces the depth of the tree by one. |
| 5328 | ** |
| 5329 | ** If the root page is page 1, it has less space available than |
| 5330 | ** its child (due to the 100 byte header that occurs at the beginning |
| 5331 | ** of the database fle), so it might not be able to hold all of the |
| 5332 | ** information currently contained in the child. If this is the |
| 5333 | ** case, then do not do the transfer. Leave page 1 empty except |
| 5334 | ** for the right-pointer to the child page. The child page becomes |
| 5335 | ** the virtual root of the tree. |
| 5336 | */ |
| 5337 | pgnoChild = get4byte(&pPage->aData[pPage->hdrOffset+8]); |
| 5338 | assert( pgnoChild>0 ); |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 5339 | assert( pgnoChild<=sqlite3PagerPagecount(pPage->pBt->pPager) ); |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 5340 | rc = sqlite3BtreeGetPage(pPage->pBt, pgnoChild, &pChild, 0); |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 5341 | if( rc ) goto end_shallow_balance; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5342 | if( pPage->pgno==1 ){ |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 5343 | rc = sqlite3BtreeInitPage(pChild, pPage); |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 5344 | if( rc ) goto end_shallow_balance; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5345 | assert( pChild->nOverflow==0 ); |
| 5346 | if( pChild->nFree>=100 ){ |
| 5347 | /* The child information will fit on the root page, so do the |
| 5348 | ** copy */ |
| 5349 | int i; |
| 5350 | zeroPage(pPage, pChild->aData[0]); |
| 5351 | for(i=0; i<pChild->nCell; i++){ |
danielk1977 | 1cc5ed8 | 2007-05-16 17:28:43 +0000 | [diff] [blame] | 5352 | apCell[i] = findCell(pChild,i); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5353 | szCell[i] = cellSizePtr(pChild, apCell[i]); |
| 5354 | } |
| 5355 | assemblePage(pPage, pChild->nCell, apCell, szCell); |
danielk1977 | ae82558 | 2004-11-23 09:06:55 +0000 | [diff] [blame] | 5356 | /* Copy the right-pointer of the child to the parent. */ |
| 5357 | put4byte(&pPage->aData[pPage->hdrOffset+8], |
| 5358 | get4byte(&pChild->aData[pChild->hdrOffset+8])); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5359 | freePage(pChild); |
| 5360 | TRACE(("BALANCE: child %d transfer to page 1\n", pChild->pgno)); |
| 5361 | }else{ |
| 5362 | /* The child has more information that will fit on the root. |
| 5363 | ** The tree is already balanced. Do nothing. */ |
| 5364 | TRACE(("BALANCE: child %d will not fit on page 1\n", pChild->pgno)); |
| 5365 | } |
| 5366 | }else{ |
| 5367 | memcpy(pPage->aData, pChild->aData, pPage->pBt->usableSize); |
| 5368 | pPage->isInit = 0; |
| 5369 | pPage->pParent = 0; |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 5370 | rc = sqlite3BtreeInitPage(pPage, 0); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5371 | assert( rc==SQLITE_OK ); |
| 5372 | freePage(pChild); |
| 5373 | TRACE(("BALANCE: transfer child %d into root %d\n", |
| 5374 | pChild->pgno, pPage->pgno)); |
| 5375 | } |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 5376 | rc = reparentChildPages(pPage); |
danielk1977 | ac11ee6 | 2005-01-15 12:45:51 +0000 | [diff] [blame] | 5377 | assert( pPage->nOverflow==0 ); |
| 5378 | #ifndef SQLITE_OMIT_AUTOVACUUM |
| 5379 | if( pBt->autoVacuum ){ |
danielk1977 | aac0a38 | 2005-01-16 11:07:06 +0000 | [diff] [blame] | 5380 | int i; |
danielk1977 | ac11ee6 | 2005-01-15 12:45:51 +0000 | [diff] [blame] | 5381 | for(i=0; i<pPage->nCell; i++){ |
danielk1977 | 79a40da | 2005-01-16 08:00:01 +0000 | [diff] [blame] | 5382 | rc = ptrmapPutOvfl(pPage, i); |
| 5383 | if( rc!=SQLITE_OK ){ |
| 5384 | goto end_shallow_balance; |
danielk1977 | ac11ee6 | 2005-01-15 12:45:51 +0000 | [diff] [blame] | 5385 | } |
| 5386 | } |
| 5387 | } |
| 5388 | #endif |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5389 | releasePage(pChild); |
| 5390 | } |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 5391 | end_shallow_balance: |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 5392 | sqlite3_free(apCell); |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 5393 | return rc; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5394 | } |
| 5395 | |
| 5396 | |
| 5397 | /* |
| 5398 | ** The root page is overfull |
| 5399 | ** |
| 5400 | ** When this happens, Create a new child page and copy the |
| 5401 | ** contents of the root into the child. Then make the root |
| 5402 | ** page an empty page with rightChild pointing to the new |
| 5403 | ** child. Finally, call balance_internal() on the new child |
| 5404 | ** to cause it to split. |
| 5405 | */ |
| 5406 | static int balance_deeper(MemPage *pPage){ |
| 5407 | int rc; /* Return value from subprocedures */ |
| 5408 | MemPage *pChild; /* Pointer to a new child page */ |
| 5409 | Pgno pgnoChild; /* Page number of the new child page */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 5410 | BtShared *pBt; /* The BTree */ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5411 | int usableSize; /* Total usable size of a page */ |
| 5412 | u8 *data; /* Content of the parent page */ |
| 5413 | u8 *cdata; /* Content of the child page */ |
| 5414 | int hdr; /* Offset to page header in parent */ |
| 5415 | int brk; /* Offset to content of first cell in parent */ |
| 5416 | |
| 5417 | assert( pPage->pParent==0 ); |
| 5418 | assert( pPage->nOverflow>0 ); |
| 5419 | pBt = pPage->pBt; |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 5420 | assert( sqlite3_mutex_held(pBt->mutex) ); |
drh | 4f0c587 | 2007-03-26 22:05:01 +0000 | [diff] [blame] | 5421 | rc = allocateBtreePage(pBt, &pChild, &pgnoChild, pPage->pgno, 0); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5422 | if( rc ) return rc; |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 5423 | assert( sqlite3PagerIswriteable(pChild->pDbPage) ); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5424 | usableSize = pBt->usableSize; |
| 5425 | data = pPage->aData; |
| 5426 | hdr = pPage->hdrOffset; |
| 5427 | brk = get2byte(&data[hdr+5]); |
| 5428 | cdata = pChild->aData; |
| 5429 | memcpy(cdata, &data[hdr], pPage->cellOffset+2*pPage->nCell-hdr); |
| 5430 | memcpy(&cdata[brk], &data[brk], usableSize-brk); |
danielk1977 | c7dc753 | 2004-11-17 10:22:03 +0000 | [diff] [blame] | 5431 | assert( pChild->isInit==0 ); |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 5432 | rc = sqlite3BtreeInitPage(pChild, pPage); |
danielk1977 | 6b456a2 | 2005-03-21 04:04:02 +0000 | [diff] [blame] | 5433 | if( rc ) goto balancedeeper_out; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5434 | memcpy(pChild->aOvfl, pPage->aOvfl, pPage->nOverflow*sizeof(pPage->aOvfl[0])); |
| 5435 | pChild->nOverflow = pPage->nOverflow; |
| 5436 | if( pChild->nOverflow ){ |
| 5437 | pChild->nFree = 0; |
| 5438 | } |
| 5439 | assert( pChild->nCell==pPage->nCell ); |
| 5440 | zeroPage(pPage, pChild->aData[0] & ~PTF_LEAF); |
| 5441 | put4byte(&pPage->aData[pPage->hdrOffset+8], pgnoChild); |
| 5442 | TRACE(("BALANCE: copy root %d into %d\n", pPage->pgno, pChild->pgno)); |
danielk1977 | 4e17d14 | 2005-01-16 09:06:33 +0000 | [diff] [blame] | 5443 | #ifndef SQLITE_OMIT_AUTOVACUUM |
danielk1977 | ac11ee6 | 2005-01-15 12:45:51 +0000 | [diff] [blame] | 5444 | if( pBt->autoVacuum ){ |
| 5445 | int i; |
| 5446 | rc = ptrmapPut(pBt, pChild->pgno, PTRMAP_BTREE, pPage->pgno); |
danielk1977 | 6b456a2 | 2005-03-21 04:04:02 +0000 | [diff] [blame] | 5447 | if( rc ) goto balancedeeper_out; |
danielk1977 | ac11ee6 | 2005-01-15 12:45:51 +0000 | [diff] [blame] | 5448 | for(i=0; i<pChild->nCell; i++){ |
danielk1977 | 79a40da | 2005-01-16 08:00:01 +0000 | [diff] [blame] | 5449 | rc = ptrmapPutOvfl(pChild, i); |
| 5450 | if( rc!=SQLITE_OK ){ |
| 5451 | return rc; |
danielk1977 | ac11ee6 | 2005-01-15 12:45:51 +0000 | [diff] [blame] | 5452 | } |
| 5453 | } |
| 5454 | } |
danielk1977 | 4e17d14 | 2005-01-16 09:06:33 +0000 | [diff] [blame] | 5455 | #endif |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5456 | rc = balance_nonroot(pChild); |
danielk1977 | 6b456a2 | 2005-03-21 04:04:02 +0000 | [diff] [blame] | 5457 | |
| 5458 | balancedeeper_out: |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5459 | releasePage(pChild); |
| 5460 | return rc; |
| 5461 | } |
| 5462 | |
| 5463 | /* |
| 5464 | ** Decide if the page pPage needs to be balanced. If balancing is |
| 5465 | ** required, call the appropriate balancing routine. |
| 5466 | */ |
danielk1977 | ac245ec | 2005-01-14 13:50:11 +0000 | [diff] [blame] | 5467 | static int balance(MemPage *pPage, int insert){ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5468 | int rc = SQLITE_OK; |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 5469 | assert( sqlite3_mutex_held(pPage->pBt->mutex) ); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5470 | if( pPage->pParent==0 ){ |
danielk1977 | 6e465eb | 2007-08-21 13:11:00 +0000 | [diff] [blame] | 5471 | rc = sqlite3PagerWrite(pPage->pDbPage); |
| 5472 | if( rc==SQLITE_OK && pPage->nOverflow>0 ){ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5473 | rc = balance_deeper(pPage); |
| 5474 | } |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 5475 | if( rc==SQLITE_OK && pPage->nCell==0 ){ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5476 | rc = balance_shallower(pPage); |
| 5477 | } |
| 5478 | }else{ |
danielk1977 | ac245ec | 2005-01-14 13:50:11 +0000 | [diff] [blame] | 5479 | if( pPage->nOverflow>0 || |
| 5480 | (!insert && pPage->nFree>pPage->pBt->usableSize*2/3) ){ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5481 | rc = balance_nonroot(pPage); |
| 5482 | } |
| 5483 | } |
| 5484 | return rc; |
| 5485 | } |
| 5486 | |
| 5487 | /* |
drh | 8dcd7ca | 2004-08-08 19:43:29 +0000 | [diff] [blame] | 5488 | ** This routine checks all cursors that point to table pgnoRoot. |
drh | 980b1a7 | 2006-08-16 16:42:48 +0000 | [diff] [blame] | 5489 | ** If any of those cursors were opened with wrFlag==0 in a different |
| 5490 | ** database connection (a database connection that shares the pager |
| 5491 | ** cache with the current connection) and that other connection |
| 5492 | ** is not in the ReadUncommmitted state, then this routine returns |
| 5493 | ** SQLITE_LOCKED. |
danielk1977 | 299b187 | 2004-11-22 10:02:10 +0000 | [diff] [blame] | 5494 | ** |
| 5495 | ** In addition to checking for read-locks (where a read-lock |
| 5496 | ** means a cursor opened with wrFlag==0) this routine also moves |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 5497 | ** all write cursors so that they are pointing to the |
drh | 980b1a7 | 2006-08-16 16:42:48 +0000 | [diff] [blame] | 5498 | ** first Cell on the root page. This is necessary because an insert |
danielk1977 | 299b187 | 2004-11-22 10:02:10 +0000 | [diff] [blame] | 5499 | ** or delete might change the number of cells on a page or delete |
| 5500 | ** a page entirely and we do not want to leave any cursors |
| 5501 | ** pointing to non-existant pages or cells. |
drh | f74b8d9 | 2002-09-01 23:20:45 +0000 | [diff] [blame] | 5502 | */ |
drh | 980b1a7 | 2006-08-16 16:42:48 +0000 | [diff] [blame] | 5503 | static int checkReadLocks(Btree *pBtree, Pgno pgnoRoot, BtCursor *pExclude){ |
danielk1977 | 299b187 | 2004-11-22 10:02:10 +0000 | [diff] [blame] | 5504 | BtCursor *p; |
drh | 980b1a7 | 2006-08-16 16:42:48 +0000 | [diff] [blame] | 5505 | BtShared *pBt = pBtree->pBt; |
| 5506 | sqlite3 *db = pBtree->pSqlite; |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 5507 | assert( sqlite3BtreeHoldsMutex(pBtree) ); |
danielk1977 | 299b187 | 2004-11-22 10:02:10 +0000 | [diff] [blame] | 5508 | for(p=pBt->pCursor; p; p=p->pNext){ |
drh | 980b1a7 | 2006-08-16 16:42:48 +0000 | [diff] [blame] | 5509 | if( p==pExclude ) continue; |
| 5510 | if( p->eState!=CURSOR_VALID ) continue; |
| 5511 | if( p->pgnoRoot!=pgnoRoot ) continue; |
| 5512 | if( p->wrFlag==0 ){ |
| 5513 | sqlite3 *dbOther = p->pBtree->pSqlite; |
| 5514 | if( dbOther==0 || |
| 5515 | (dbOther!=db && (dbOther->flags & SQLITE_ReadUncommitted)==0) ){ |
| 5516 | return SQLITE_LOCKED; |
| 5517 | } |
| 5518 | }else if( p->pPage->pgno!=p->pgnoRoot ){ |
danielk1977 | 299b187 | 2004-11-22 10:02:10 +0000 | [diff] [blame] | 5519 | moveToRoot(p); |
| 5520 | } |
| 5521 | } |
drh | f74b8d9 | 2002-09-01 23:20:45 +0000 | [diff] [blame] | 5522 | return SQLITE_OK; |
| 5523 | } |
| 5524 | |
| 5525 | /* |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 5526 | ** Insert a new record into the BTree. The key is given by (pKey,nKey) |
| 5527 | ** and the data is given by (pData,nData). The cursor is used only to |
drh | 9102529 | 2004-05-03 19:49:32 +0000 | [diff] [blame] | 5528 | ** define what table the record should be inserted into. The cursor |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 5529 | ** is left pointing at a random location. |
| 5530 | ** |
| 5531 | ** For an INTKEY table, only the nKey value of the key is used. pKey is |
| 5532 | ** ignored. For a ZERODATA table, the pData and nData are both ignored. |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 5533 | */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 5534 | int sqlite3BtreeInsert( |
drh | 5c4d970 | 2001-08-20 00:33:58 +0000 | [diff] [blame] | 5535 | BtCursor *pCur, /* Insert data into the table of this cursor */ |
drh | 4a1c380 | 2004-05-12 15:15:47 +0000 | [diff] [blame] | 5536 | const void *pKey, i64 nKey, /* The key of the new record */ |
drh | e4d9081 | 2007-03-29 05:51:49 +0000 | [diff] [blame] | 5537 | const void *pData, int nData, /* The data of the new record */ |
drh | b026e05 | 2007-05-02 01:34:31 +0000 | [diff] [blame] | 5538 | int nZero, /* Number of extra 0 bytes to append to data */ |
drh | e4d9081 | 2007-03-29 05:51:49 +0000 | [diff] [blame] | 5539 | int appendBias /* True if this is likely an append */ |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 5540 | ){ |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 5541 | int rc; |
| 5542 | int loc; |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 5543 | int szNew; |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 5544 | MemPage *pPage; |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 5545 | Btree *p = pCur->pBtree; |
| 5546 | BtShared *pBt = p->pBt; |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 5547 | unsigned char *oldCell; |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 5548 | unsigned char *newCell = 0; |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 5549 | |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 5550 | assert( cursorHoldsMutex(pCur) ); |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 5551 | if( pBt->inTransaction!=TRANS_WRITE ){ |
drh | f74b8d9 | 2002-09-01 23:20:45 +0000 | [diff] [blame] | 5552 | /* Must start a transaction before doing an insert */ |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 5553 | rc = pBt->readOnly ? SQLITE_READONLY : SQLITE_ERROR; |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 5554 | return rc; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 5555 | } |
drh | f74b8d9 | 2002-09-01 23:20:45 +0000 | [diff] [blame] | 5556 | assert( !pBt->readOnly ); |
drh | ecdc753 | 2001-09-23 02:35:53 +0000 | [diff] [blame] | 5557 | if( !pCur->wrFlag ){ |
| 5558 | return SQLITE_PERM; /* Cursor not open for writing */ |
| 5559 | } |
drh | 980b1a7 | 2006-08-16 16:42:48 +0000 | [diff] [blame] | 5560 | if( checkReadLocks(pCur->pBtree, pCur->pgnoRoot, pCur) ){ |
drh | f74b8d9 | 2002-09-01 23:20:45 +0000 | [diff] [blame] | 5561 | return SQLITE_LOCKED; /* The table pCur points to has a read lock */ |
| 5562 | } |
drh | fb98264 | 2007-08-30 01:19:59 +0000 | [diff] [blame] | 5563 | if( pCur->eState==CURSOR_FAULT ){ |
| 5564 | return pCur->skip; |
| 5565 | } |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 5566 | |
| 5567 | /* Save the positions of any other cursors open on this table */ |
drh | bf700f3 | 2007-03-31 02:36:44 +0000 | [diff] [blame] | 5568 | clearCursorPosition(pCur); |
danielk1977 | 2e94d4d | 2006-01-09 05:36:27 +0000 | [diff] [blame] | 5569 | if( |
danielk1977 | 2e94d4d | 2006-01-09 05:36:27 +0000 | [diff] [blame] | 5570 | SQLITE_OK!=(rc = saveAllCursors(pBt, pCur->pgnoRoot, pCur)) || |
drh | e4d9081 | 2007-03-29 05:51:49 +0000 | [diff] [blame] | 5571 | SQLITE_OK!=(rc = sqlite3BtreeMoveto(pCur, pKey, nKey, appendBias, &loc)) |
danielk1977 | 2e94d4d | 2006-01-09 05:36:27 +0000 | [diff] [blame] | 5572 | ){ |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 5573 | return rc; |
| 5574 | } |
| 5575 | |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 5576 | pPage = pCur->pPage; |
drh | 4a1c380 | 2004-05-12 15:15:47 +0000 | [diff] [blame] | 5577 | assert( pPage->intKey || nKey>=0 ); |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 5578 | assert( pPage->leaf || !pPage->leafData ); |
drh | 3a4c141 | 2004-05-09 20:40:11 +0000 | [diff] [blame] | 5579 | TRACE(("INSERT: table=%d nkey=%lld ndata=%d page=%d %s\n", |
| 5580 | pCur->pgnoRoot, nKey, nData, pPage->pgno, |
| 5581 | loc==0 ? "overwrite" : "new entry")); |
drh | 7aa128d | 2002-06-21 13:09:16 +0000 | [diff] [blame] | 5582 | assert( pPage->isInit ); |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 5583 | newCell = sqlite3_malloc( MX_CELL_SIZE(pBt) ); |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 5584 | if( newCell==0 ) return SQLITE_NOMEM; |
drh | b026e05 | 2007-05-02 01:34:31 +0000 | [diff] [blame] | 5585 | rc = fillInCell(pPage, newCell, pKey, nKey, pData, nData, nZero, &szNew); |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 5586 | if( rc ) goto end_insert; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5587 | assert( szNew==cellSizePtr(pPage, newCell) ); |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 5588 | assert( szNew<=MX_CELL_SIZE(pBt) ); |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 5589 | if( loc==0 && CURSOR_VALID==pCur->eState ){ |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 5590 | int szOld; |
| 5591 | assert( pCur->idx>=0 && pCur->idx<pPage->nCell ); |
danielk1977 | 6e465eb | 2007-08-21 13:11:00 +0000 | [diff] [blame] | 5592 | rc = sqlite3PagerWrite(pPage->pDbPage); |
| 5593 | if( rc ){ |
| 5594 | goto end_insert; |
| 5595 | } |
danielk1977 | 1cc5ed8 | 2007-05-16 17:28:43 +0000 | [diff] [blame] | 5596 | oldCell = findCell(pPage, pCur->idx); |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 5597 | if( !pPage->leaf ){ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5598 | memcpy(newCell, oldCell, 4); |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 5599 | } |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5600 | szOld = cellSizePtr(pPage, oldCell); |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 5601 | rc = clearCell(pPage, oldCell); |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 5602 | if( rc ) goto end_insert; |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 5603 | dropCell(pPage, pCur->idx, szOld); |
drh | 7c717f7 | 2001-06-24 20:39:41 +0000 | [diff] [blame] | 5604 | }else if( loc<0 && pPage->nCell>0 ){ |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 5605 | assert( pPage->leaf ); |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 5606 | pCur->idx++; |
drh | 271efa5 | 2004-05-30 19:19:05 +0000 | [diff] [blame] | 5607 | pCur->info.nSize = 0; |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 5608 | }else{ |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 5609 | assert( pPage->leaf ); |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 5610 | } |
danielk1977 | a3ad5e7 | 2005-01-07 08:56:44 +0000 | [diff] [blame] | 5611 | rc = insertCell(pPage, pCur->idx, newCell, szNew, 0, 0); |
danielk1977 | e80463b | 2004-11-03 03:01:16 +0000 | [diff] [blame] | 5612 | if( rc!=SQLITE_OK ) goto end_insert; |
danielk1977 | ac245ec | 2005-01-14 13:50:11 +0000 | [diff] [blame] | 5613 | rc = balance(pPage, 1); |
drh | 23e11ca | 2004-05-04 17:27:28 +0000 | [diff] [blame] | 5614 | /* sqlite3BtreePageDump(pCur->pBt, pCur->pgnoRoot, 1); */ |
drh | 3fc190c | 2001-09-14 03:24:23 +0000 | [diff] [blame] | 5615 | /* fflush(stdout); */ |
danielk1977 | 299b187 | 2004-11-22 10:02:10 +0000 | [diff] [blame] | 5616 | if( rc==SQLITE_OK ){ |
| 5617 | moveToRoot(pCur); |
| 5618 | } |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 5619 | end_insert: |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 5620 | sqlite3_free(newCell); |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 5621 | return rc; |
| 5622 | } |
| 5623 | |
| 5624 | /* |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 5625 | ** Delete the entry that the cursor is pointing to. The cursor |
| 5626 | ** is left pointing at a random location. |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 5627 | */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 5628 | int sqlite3BtreeDelete(BtCursor *pCur){ |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 5629 | MemPage *pPage = pCur->pPage; |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 5630 | unsigned char *pCell; |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 5631 | int rc; |
danielk1977 | cfe9a69 | 2004-06-16 12:00:29 +0000 | [diff] [blame] | 5632 | Pgno pgnoChild = 0; |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 5633 | Btree *p = pCur->pBtree; |
| 5634 | BtShared *pBt = p->pBt; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 5635 | |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 5636 | assert( cursorHoldsMutex(pCur) ); |
drh | 7aa128d | 2002-06-21 13:09:16 +0000 | [diff] [blame] | 5637 | assert( pPage->isInit ); |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 5638 | if( pBt->inTransaction!=TRANS_WRITE ){ |
drh | f74b8d9 | 2002-09-01 23:20:45 +0000 | [diff] [blame] | 5639 | /* Must start a transaction before doing a delete */ |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 5640 | rc = pBt->readOnly ? SQLITE_READONLY : SQLITE_ERROR; |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 5641 | return rc; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 5642 | } |
drh | f74b8d9 | 2002-09-01 23:20:45 +0000 | [diff] [blame] | 5643 | assert( !pBt->readOnly ); |
drh | fb98264 | 2007-08-30 01:19:59 +0000 | [diff] [blame] | 5644 | if( pCur->eState==CURSOR_FAULT ){ |
| 5645 | return pCur->skip; |
| 5646 | } |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 5647 | if( pCur->idx >= pPage->nCell ){ |
| 5648 | return SQLITE_ERROR; /* The cursor is not pointing to anything */ |
| 5649 | } |
drh | ecdc753 | 2001-09-23 02:35:53 +0000 | [diff] [blame] | 5650 | if( !pCur->wrFlag ){ |
| 5651 | return SQLITE_PERM; /* Did not open this cursor for writing */ |
| 5652 | } |
drh | 980b1a7 | 2006-08-16 16:42:48 +0000 | [diff] [blame] | 5653 | if( checkReadLocks(pCur->pBtree, pCur->pgnoRoot, pCur) ){ |
drh | f74b8d9 | 2002-09-01 23:20:45 +0000 | [diff] [blame] | 5654 | return SQLITE_LOCKED; /* The table pCur points to has a read lock */ |
| 5655 | } |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 5656 | |
| 5657 | /* Restore the current cursor position (a no-op if the cursor is not in |
| 5658 | ** CURSOR_REQUIRESEEK state) and save the positions of any other cursors |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 5659 | ** open on the same table. Then call sqlite3PagerWrite() on the page |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 5660 | ** that the entry will be deleted from. |
| 5661 | */ |
| 5662 | if( |
drh | bf700f3 | 2007-03-31 02:36:44 +0000 | [diff] [blame] | 5663 | (rc = restoreOrClearCursorPosition(pCur))!=0 || |
drh | d116739 | 2006-01-23 13:00:35 +0000 | [diff] [blame] | 5664 | (rc = saveAllCursors(pBt, pCur->pgnoRoot, pCur))!=0 || |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 5665 | (rc = sqlite3PagerWrite(pPage->pDbPage))!=0 |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 5666 | ){ |
| 5667 | return rc; |
| 5668 | } |
danielk1977 | e6efa74 | 2004-11-10 11:55:10 +0000 | [diff] [blame] | 5669 | |
| 5670 | /* Locate the cell within it's page and leave pCell pointing to the |
| 5671 | ** data. The clearCell() call frees any overflow pages associated with the |
| 5672 | ** cell. The cell itself is still intact. |
| 5673 | */ |
danielk1977 | 1cc5ed8 | 2007-05-16 17:28:43 +0000 | [diff] [blame] | 5674 | pCell = findCell(pPage, pCur->idx); |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 5675 | if( !pPage->leaf ){ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5676 | pgnoChild = get4byte(pCell); |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 5677 | } |
danielk1977 | 2812956 | 2005-01-11 10:25:06 +0000 | [diff] [blame] | 5678 | rc = clearCell(pPage, pCell); |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 5679 | if( rc ){ |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 5680 | return rc; |
| 5681 | } |
danielk1977 | e6efa74 | 2004-11-10 11:55:10 +0000 | [diff] [blame] | 5682 | |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 5683 | if( !pPage->leaf ){ |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 5684 | /* |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 5685 | ** The entry we are about to delete is not a leaf so if we do not |
drh | 9ca7d3b | 2001-06-28 11:50:21 +0000 | [diff] [blame] | 5686 | ** do something we will leave a hole on an internal page. |
| 5687 | ** We have to fill the hole by moving in a cell from a leaf. The |
| 5688 | ** next Cell after the one to be deleted is guaranteed to exist and |
danielk1977 | 299b187 | 2004-11-22 10:02:10 +0000 | [diff] [blame] | 5689 | ** to be a leaf so we can use it. |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 5690 | */ |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 5691 | BtCursor leafCur; |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 5692 | unsigned char *pNext; |
drh | 02afc86 | 2006-01-20 18:10:57 +0000 | [diff] [blame] | 5693 | int szNext; /* The compiler warning is wrong: szNext is always |
| 5694 | ** initialized before use. Adding an extra initialization |
| 5695 | ** to silence the compiler slows down the code. */ |
danielk1977 | 299b187 | 2004-11-22 10:02:10 +0000 | [diff] [blame] | 5696 | int notUsed; |
danielk1977 | 6b456a2 | 2005-03-21 04:04:02 +0000 | [diff] [blame] | 5697 | unsigned char *tempCell = 0; |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 5698 | assert( !pPage->leafData ); |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 5699 | sqlite3BtreeGetTempCursor(pCur, &leafCur); |
danielk1977 | 299b187 | 2004-11-22 10:02:10 +0000 | [diff] [blame] | 5700 | rc = sqlite3BtreeNext(&leafCur, ¬Used); |
danielk1977 | 6b456a2 | 2005-03-21 04:04:02 +0000 | [diff] [blame] | 5701 | if( rc==SQLITE_OK ){ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 5702 | rc = sqlite3PagerWrite(leafCur.pPage->pDbPage); |
danielk1977 | 6b456a2 | 2005-03-21 04:04:02 +0000 | [diff] [blame] | 5703 | } |
| 5704 | if( rc==SQLITE_OK ){ |
| 5705 | TRACE(("DELETE: table=%d delete internal from %d replace from leaf %d\n", |
| 5706 | pCur->pgnoRoot, pPage->pgno, leafCur.pPage->pgno)); |
| 5707 | dropCell(pPage, pCur->idx, cellSizePtr(pPage, pCell)); |
danielk1977 | 1cc5ed8 | 2007-05-16 17:28:43 +0000 | [diff] [blame] | 5708 | pNext = findCell(leafCur.pPage, leafCur.idx); |
danielk1977 | 6b456a2 | 2005-03-21 04:04:02 +0000 | [diff] [blame] | 5709 | szNext = cellSizePtr(leafCur.pPage, pNext); |
| 5710 | assert( MX_CELL_SIZE(pBt)>=szNext+4 ); |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 5711 | tempCell = sqlite3_malloc( MX_CELL_SIZE(pBt) ); |
danielk1977 | 6b456a2 | 2005-03-21 04:04:02 +0000 | [diff] [blame] | 5712 | if( tempCell==0 ){ |
| 5713 | rc = SQLITE_NOMEM; |
| 5714 | } |
| 5715 | } |
| 5716 | if( rc==SQLITE_OK ){ |
| 5717 | rc = insertCell(pPage, pCur->idx, pNext-4, szNext+4, tempCell, 0); |
| 5718 | } |
| 5719 | if( rc==SQLITE_OK ){ |
| 5720 | put4byte(findOverflowCell(pPage, pCur->idx), pgnoChild); |
| 5721 | rc = balance(pPage, 0); |
| 5722 | } |
| 5723 | if( rc==SQLITE_OK ){ |
| 5724 | dropCell(leafCur.pPage, leafCur.idx, szNext); |
| 5725 | rc = balance(leafCur.pPage, 0); |
| 5726 | } |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 5727 | sqlite3_free(tempCell); |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 5728 | sqlite3BtreeReleaseTempCursor(&leafCur); |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 5729 | }else{ |
danielk1977 | 299b187 | 2004-11-22 10:02:10 +0000 | [diff] [blame] | 5730 | TRACE(("DELETE: table=%d delete from leaf %d\n", |
| 5731 | pCur->pgnoRoot, pPage->pgno)); |
| 5732 | dropCell(pPage, pCur->idx, cellSizePtr(pPage, pCell)); |
danielk1977 | ac245ec | 2005-01-14 13:50:11 +0000 | [diff] [blame] | 5733 | rc = balance(pPage, 0); |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 5734 | } |
danielk1977 | 6b456a2 | 2005-03-21 04:04:02 +0000 | [diff] [blame] | 5735 | if( rc==SQLITE_OK ){ |
| 5736 | moveToRoot(pCur); |
| 5737 | } |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 5738 | return rc; |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 5739 | } |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 5740 | |
| 5741 | /* |
drh | c6b52df | 2002-01-04 03:09:29 +0000 | [diff] [blame] | 5742 | ** Create a new BTree table. Write into *piTable the page |
| 5743 | ** number for the root page of the new table. |
| 5744 | ** |
drh | ab01f61 | 2004-05-22 02:55:23 +0000 | [diff] [blame] | 5745 | ** The type of type is determined by the flags parameter. Only the |
| 5746 | ** following values of flags are currently in use. Other values for |
| 5747 | ** flags might not work: |
| 5748 | ** |
| 5749 | ** BTREE_INTKEY|BTREE_LEAFDATA Used for SQL tables with rowid keys |
| 5750 | ** BTREE_ZERODATA Used for SQL indices |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 5751 | */ |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 5752 | static int btreeCreateTable(Btree *p, int *piTable, int flags){ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 5753 | BtShared *pBt = p->pBt; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 5754 | MemPage *pRoot; |
| 5755 | Pgno pgnoRoot; |
| 5756 | int rc; |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 5757 | |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 5758 | assert( sqlite3BtreeHoldsMutex(p) ); |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 5759 | if( pBt->inTransaction!=TRANS_WRITE ){ |
drh | f74b8d9 | 2002-09-01 23:20:45 +0000 | [diff] [blame] | 5760 | /* Must start a transaction first */ |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 5761 | rc = pBt->readOnly ? SQLITE_READONLY : SQLITE_ERROR; |
| 5762 | return rc; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 5763 | } |
danielk1977 | 2812956 | 2005-01-11 10:25:06 +0000 | [diff] [blame] | 5764 | assert( !pBt->readOnly ); |
danielk1977 | e6efa74 | 2004-11-10 11:55:10 +0000 | [diff] [blame] | 5765 | |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 5766 | #ifdef SQLITE_OMIT_AUTOVACUUM |
drh | 4f0c587 | 2007-03-26 22:05:01 +0000 | [diff] [blame] | 5767 | rc = allocateBtreePage(pBt, &pRoot, &pgnoRoot, 1, 0); |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 5768 | if( rc ){ |
| 5769 | return rc; |
| 5770 | } |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 5771 | #else |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 5772 | if( pBt->autoVacuum ){ |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 5773 | Pgno pgnoMove; /* Move a page here to make room for the root-page */ |
| 5774 | MemPage *pPageMove; /* The page to move to. */ |
| 5775 | |
danielk1977 | 20713f3 | 2007-05-03 11:43:33 +0000 | [diff] [blame] | 5776 | /* Creating a new table may probably require moving an existing database |
| 5777 | ** to make room for the new tables root page. In case this page turns |
| 5778 | ** out to be an overflow page, delete all overflow page-map caches |
| 5779 | ** held by open cursors. |
| 5780 | */ |
danielk1977 | 92d4d7a | 2007-05-04 12:05:56 +0000 | [diff] [blame] | 5781 | invalidateAllOverflowCache(pBt); |
danielk1977 | 20713f3 | 2007-05-03 11:43:33 +0000 | [diff] [blame] | 5782 | |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 5783 | /* Read the value of meta[3] from the database to determine where the |
| 5784 | ** root page of the new table should go. meta[3] is the largest root-page |
| 5785 | ** created so far, so the new root-page is (meta[3]+1). |
| 5786 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 5787 | rc = sqlite3BtreeGetMeta(p, 4, &pgnoRoot); |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 5788 | if( rc!=SQLITE_OK ){ |
| 5789 | return rc; |
| 5790 | } |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 5791 | pgnoRoot++; |
| 5792 | |
danielk1977 | 599fcba | 2004-11-08 07:13:13 +0000 | [diff] [blame] | 5793 | /* The new root-page may not be allocated on a pointer-map page, or the |
| 5794 | ** PENDING_BYTE page. |
| 5795 | */ |
danielk1977 | 266664d | 2006-02-10 08:24:21 +0000 | [diff] [blame] | 5796 | if( pgnoRoot==PTRMAP_PAGENO(pBt, pgnoRoot) || |
danielk1977 | 599fcba | 2004-11-08 07:13:13 +0000 | [diff] [blame] | 5797 | pgnoRoot==PENDING_BYTE_PAGE(pBt) ){ |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 5798 | pgnoRoot++; |
| 5799 | } |
| 5800 | assert( pgnoRoot>=3 ); |
| 5801 | |
| 5802 | /* Allocate a page. The page that currently resides at pgnoRoot will |
| 5803 | ** be moved to the allocated page (unless the allocated page happens |
| 5804 | ** to reside at pgnoRoot). |
| 5805 | */ |
drh | 4f0c587 | 2007-03-26 22:05:01 +0000 | [diff] [blame] | 5806 | rc = allocateBtreePage(pBt, &pPageMove, &pgnoMove, pgnoRoot, 1); |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 5807 | if( rc!=SQLITE_OK ){ |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 5808 | return rc; |
| 5809 | } |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 5810 | |
| 5811 | if( pgnoMove!=pgnoRoot ){ |
danielk1977 | f35843b | 2007-04-07 15:03:17 +0000 | [diff] [blame] | 5812 | /* pgnoRoot is the page that will be used for the root-page of |
| 5813 | ** the new table (assuming an error did not occur). But we were |
| 5814 | ** allocated pgnoMove. If required (i.e. if it was not allocated |
| 5815 | ** by extending the file), the current page at position pgnoMove |
| 5816 | ** is already journaled. |
| 5817 | */ |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 5818 | u8 eType; |
| 5819 | Pgno iPtrPage; |
| 5820 | |
| 5821 | releasePage(pPageMove); |
danielk1977 | f35843b | 2007-04-07 15:03:17 +0000 | [diff] [blame] | 5822 | |
| 5823 | /* Move the page currently at pgnoRoot to pgnoMove. */ |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 5824 | rc = sqlite3BtreeGetPage(pBt, pgnoRoot, &pRoot, 0); |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 5825 | if( rc!=SQLITE_OK ){ |
| 5826 | return rc; |
| 5827 | } |
| 5828 | rc = ptrmapGet(pBt, pgnoRoot, &eType, &iPtrPage); |
drh | ccae602 | 2005-02-26 17:31:26 +0000 | [diff] [blame] | 5829 | if( rc!=SQLITE_OK || eType==PTRMAP_ROOTPAGE || eType==PTRMAP_FREEPAGE ){ |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 5830 | releasePage(pRoot); |
| 5831 | return rc; |
| 5832 | } |
drh | ccae602 | 2005-02-26 17:31:26 +0000 | [diff] [blame] | 5833 | assert( eType!=PTRMAP_ROOTPAGE ); |
| 5834 | assert( eType!=PTRMAP_FREEPAGE ); |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 5835 | rc = sqlite3PagerWrite(pRoot->pDbPage); |
danielk1977 | 5fd057a | 2005-03-09 13:09:43 +0000 | [diff] [blame] | 5836 | if( rc!=SQLITE_OK ){ |
| 5837 | releasePage(pRoot); |
| 5838 | return rc; |
| 5839 | } |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 5840 | rc = relocatePage(pBt, pRoot, eType, iPtrPage, pgnoMove); |
| 5841 | releasePage(pRoot); |
danielk1977 | f35843b | 2007-04-07 15:03:17 +0000 | [diff] [blame] | 5842 | |
| 5843 | /* Obtain the page at pgnoRoot */ |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 5844 | if( rc!=SQLITE_OK ){ |
| 5845 | return rc; |
| 5846 | } |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 5847 | rc = sqlite3BtreeGetPage(pBt, pgnoRoot, &pRoot, 0); |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 5848 | if( rc!=SQLITE_OK ){ |
| 5849 | return rc; |
| 5850 | } |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 5851 | rc = sqlite3PagerWrite(pRoot->pDbPage); |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 5852 | if( rc!=SQLITE_OK ){ |
| 5853 | releasePage(pRoot); |
| 5854 | return rc; |
| 5855 | } |
| 5856 | }else{ |
| 5857 | pRoot = pPageMove; |
| 5858 | } |
| 5859 | |
danielk1977 | 42741be | 2005-01-08 12:42:39 +0000 | [diff] [blame] | 5860 | /* Update the pointer-map and meta-data with the new root-page number. */ |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 5861 | rc = ptrmapPut(pBt, pgnoRoot, PTRMAP_ROOTPAGE, 0); |
| 5862 | if( rc ){ |
| 5863 | releasePage(pRoot); |
| 5864 | return rc; |
| 5865 | } |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 5866 | rc = sqlite3BtreeUpdateMeta(p, 4, pgnoRoot); |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 5867 | if( rc ){ |
| 5868 | releasePage(pRoot); |
| 5869 | return rc; |
| 5870 | } |
danielk1977 | 42741be | 2005-01-08 12:42:39 +0000 | [diff] [blame] | 5871 | |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 5872 | }else{ |
drh | 4f0c587 | 2007-03-26 22:05:01 +0000 | [diff] [blame] | 5873 | rc = allocateBtreePage(pBt, &pRoot, &pgnoRoot, 1, 0); |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 5874 | if( rc ) return rc; |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 5875 | } |
| 5876 | #endif |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 5877 | assert( sqlite3PagerIswriteable(pRoot->pDbPage) ); |
drh | de64713 | 2004-05-07 17:57:49 +0000 | [diff] [blame] | 5878 | zeroPage(pRoot, flags | PTF_LEAF); |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 5879 | sqlite3PagerUnref(pRoot->pDbPage); |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 5880 | *piTable = (int)pgnoRoot; |
| 5881 | return SQLITE_OK; |
| 5882 | } |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 5883 | int sqlite3BtreeCreateTable(Btree *p, int *piTable, int flags){ |
| 5884 | int rc; |
| 5885 | sqlite3BtreeEnter(p); |
| 5886 | rc = btreeCreateTable(p, piTable, flags); |
| 5887 | sqlite3BtreeLeave(p); |
| 5888 | return rc; |
| 5889 | } |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 5890 | |
| 5891 | /* |
| 5892 | ** Erase the given database page and all its children. Return |
| 5893 | ** the page to the freelist. |
| 5894 | */ |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 5895 | static int clearDatabasePage( |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 5896 | BtShared *pBt, /* The BTree that contains the table */ |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 5897 | Pgno pgno, /* Page number to clear */ |
| 5898 | MemPage *pParent, /* Parent page. NULL for the root */ |
| 5899 | int freePageFlag /* Deallocate page if true */ |
| 5900 | ){ |
danielk1977 | 6b456a2 | 2005-03-21 04:04:02 +0000 | [diff] [blame] | 5901 | MemPage *pPage = 0; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 5902 | int rc; |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 5903 | unsigned char *pCell; |
| 5904 | int i; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 5905 | |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 5906 | assert( sqlite3_mutex_held(pBt->mutex) ); |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 5907 | if( pgno>sqlite3PagerPagecount(pBt->pPager) ){ |
drh | 4928570 | 2005-09-17 15:20:26 +0000 | [diff] [blame] | 5908 | return SQLITE_CORRUPT_BKPT; |
danielk1977 | a1cb183 | 2005-02-12 08:59:55 +0000 | [diff] [blame] | 5909 | } |
| 5910 | |
drh | de64713 | 2004-05-07 17:57:49 +0000 | [diff] [blame] | 5911 | rc = getAndInitPage(pBt, pgno, &pPage, pParent); |
danielk1977 | 6b456a2 | 2005-03-21 04:04:02 +0000 | [diff] [blame] | 5912 | if( rc ) goto cleardatabasepage_out; |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 5913 | for(i=0; i<pPage->nCell; i++){ |
danielk1977 | 1cc5ed8 | 2007-05-16 17:28:43 +0000 | [diff] [blame] | 5914 | pCell = findCell(pPage, i); |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 5915 | if( !pPage->leaf ){ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5916 | rc = clearDatabasePage(pBt, get4byte(pCell), pPage->pParent, 1); |
danielk1977 | 6b456a2 | 2005-03-21 04:04:02 +0000 | [diff] [blame] | 5917 | if( rc ) goto cleardatabasepage_out; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 5918 | } |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 5919 | rc = clearCell(pPage, pCell); |
danielk1977 | 6b456a2 | 2005-03-21 04:04:02 +0000 | [diff] [blame] | 5920 | if( rc ) goto cleardatabasepage_out; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 5921 | } |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 5922 | if( !pPage->leaf ){ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5923 | rc = clearDatabasePage(pBt, get4byte(&pPage->aData[8]), pPage->pParent, 1); |
danielk1977 | 6b456a2 | 2005-03-21 04:04:02 +0000 | [diff] [blame] | 5924 | if( rc ) goto cleardatabasepage_out; |
drh | 2aa679f | 2001-06-25 02:11:07 +0000 | [diff] [blame] | 5925 | } |
| 5926 | if( freePageFlag ){ |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 5927 | rc = freePage(pPage); |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 5928 | }else if( (rc = sqlite3PagerWrite(pPage->pDbPage))==0 ){ |
drh | 3a4c141 | 2004-05-09 20:40:11 +0000 | [diff] [blame] | 5929 | zeroPage(pPage, pPage->aData[0] | PTF_LEAF); |
drh | 2aa679f | 2001-06-25 02:11:07 +0000 | [diff] [blame] | 5930 | } |
danielk1977 | 6b456a2 | 2005-03-21 04:04:02 +0000 | [diff] [blame] | 5931 | |
| 5932 | cleardatabasepage_out: |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 5933 | releasePage(pPage); |
drh | 2aa679f | 2001-06-25 02:11:07 +0000 | [diff] [blame] | 5934 | return rc; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 5935 | } |
| 5936 | |
| 5937 | /* |
drh | ab01f61 | 2004-05-22 02:55:23 +0000 | [diff] [blame] | 5938 | ** Delete all information from a single table in the database. iTable is |
| 5939 | ** the page number of the root of the table. After this routine returns, |
| 5940 | ** the root page is empty, but still exists. |
| 5941 | ** |
| 5942 | ** This routine will fail with SQLITE_LOCKED if there are any open |
| 5943 | ** read cursors on the table. Open write cursors are moved to the |
| 5944 | ** root of the table. |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 5945 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 5946 | int sqlite3BtreeClearTable(Btree *p, int iTable){ |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 5947 | int rc; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 5948 | BtShared *pBt = p->pBt; |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 5949 | sqlite3BtreeEnter(p); |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 5950 | if( p->inTrans!=TRANS_WRITE ){ |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 5951 | rc = pBt->readOnly ? SQLITE_READONLY : SQLITE_ERROR; |
| 5952 | }else if( (rc = checkReadLocks(p, iTable, 0))!=SQLITE_OK ){ |
| 5953 | /* nothing to do */ |
| 5954 | }else if( SQLITE_OK!=(rc = saveAllCursors(pBt, iTable, 0)) ){ |
| 5955 | /* nothing to do */ |
| 5956 | }else{ |
| 5957 | rc = clearDatabasePage(pBt, (Pgno)iTable, 0, 0); |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 5958 | } |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 5959 | sqlite3BtreeLeave(p); |
| 5960 | return rc; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 5961 | } |
| 5962 | |
| 5963 | /* |
| 5964 | ** Erase all information in a table and add the root of the table to |
| 5965 | ** the freelist. Except, the root of the principle table (the one on |
drh | ab01f61 | 2004-05-22 02:55:23 +0000 | [diff] [blame] | 5966 | ** page 1) is never added to the freelist. |
| 5967 | ** |
| 5968 | ** This routine will fail with SQLITE_LOCKED if there are any open |
| 5969 | ** cursors on the table. |
drh | 205f48e | 2004-11-05 00:43:11 +0000 | [diff] [blame] | 5970 | ** |
| 5971 | ** If AUTOVACUUM is enabled and the page at iTable is not the last |
| 5972 | ** root page in the database file, then the last root page |
| 5973 | ** in the database file is moved into the slot formerly occupied by |
| 5974 | ** iTable and that last slot formerly occupied by the last root page |
| 5975 | ** is added to the freelist instead of iTable. In this say, all |
| 5976 | ** root pages are kept at the beginning of the database file, which |
| 5977 | ** is necessary for AUTOVACUUM to work right. *piMoved is set to the |
| 5978 | ** page number that used to be the last root page in the file before |
| 5979 | ** the move. If no page gets moved, *piMoved is set to 0. |
| 5980 | ** The last root page is recorded in meta[3] and the value of |
| 5981 | ** meta[3] is updated by this procedure. |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 5982 | */ |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 5983 | static int btreeDropTable(Btree *p, int iTable, int *piMoved){ |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 5984 | int rc; |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 5985 | MemPage *pPage = 0; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 5986 | BtShared *pBt = p->pBt; |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 5987 | |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 5988 | assert( sqlite3BtreeHoldsMutex(p) ); |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 5989 | if( p->inTrans!=TRANS_WRITE ){ |
drh | f74b8d9 | 2002-09-01 23:20:45 +0000 | [diff] [blame] | 5990 | return pBt->readOnly ? SQLITE_READONLY : SQLITE_ERROR; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 5991 | } |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 5992 | |
danielk1977 | e6efa74 | 2004-11-10 11:55:10 +0000 | [diff] [blame] | 5993 | /* It is illegal to drop a table if any cursors are open on the |
| 5994 | ** database. This is because in auto-vacuum mode the backend may |
| 5995 | ** need to move another root-page to fill a gap left by the deleted |
| 5996 | ** root page. If an open cursor was using this page a problem would |
| 5997 | ** occur. |
| 5998 | */ |
| 5999 | if( pBt->pCursor ){ |
| 6000 | return SQLITE_LOCKED; |
drh | 5df72a5 | 2002-06-06 23:16:05 +0000 | [diff] [blame] | 6001 | } |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 6002 | |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 6003 | rc = sqlite3BtreeGetPage(pBt, (Pgno)iTable, &pPage, 0); |
drh | 2aa679f | 2001-06-25 02:11:07 +0000 | [diff] [blame] | 6004 | if( rc ) return rc; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 6005 | rc = sqlite3BtreeClearTable(p, iTable); |
danielk1977 | 6b456a2 | 2005-03-21 04:04:02 +0000 | [diff] [blame] | 6006 | if( rc ){ |
| 6007 | releasePage(pPage); |
| 6008 | return rc; |
| 6009 | } |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 6010 | |
drh | 205f48e | 2004-11-05 00:43:11 +0000 | [diff] [blame] | 6011 | *piMoved = 0; |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 6012 | |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 6013 | if( iTable>1 ){ |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 6014 | #ifdef SQLITE_OMIT_AUTOVACUUM |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 6015 | rc = freePage(pPage); |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 6016 | releasePage(pPage); |
| 6017 | #else |
| 6018 | if( pBt->autoVacuum ){ |
| 6019 | Pgno maxRootPgno; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 6020 | rc = sqlite3BtreeGetMeta(p, 4, &maxRootPgno); |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 6021 | if( rc!=SQLITE_OK ){ |
| 6022 | releasePage(pPage); |
| 6023 | return rc; |
| 6024 | } |
| 6025 | |
| 6026 | if( iTable==maxRootPgno ){ |
| 6027 | /* If the table being dropped is the table with the largest root-page |
| 6028 | ** number in the database, put the root page on the free list. |
| 6029 | */ |
| 6030 | rc = freePage(pPage); |
| 6031 | releasePage(pPage); |
| 6032 | if( rc!=SQLITE_OK ){ |
| 6033 | return rc; |
| 6034 | } |
| 6035 | }else{ |
| 6036 | /* The table being dropped does not have the largest root-page |
| 6037 | ** number in the database. So move the page that does into the |
| 6038 | ** gap left by the deleted root-page. |
| 6039 | */ |
| 6040 | MemPage *pMove; |
| 6041 | releasePage(pPage); |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 6042 | rc = sqlite3BtreeGetPage(pBt, maxRootPgno, &pMove, 0); |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 6043 | if( rc!=SQLITE_OK ){ |
| 6044 | return rc; |
| 6045 | } |
| 6046 | rc = relocatePage(pBt, pMove, PTRMAP_ROOTPAGE, 0, iTable); |
| 6047 | releasePage(pMove); |
| 6048 | if( rc!=SQLITE_OK ){ |
| 6049 | return rc; |
| 6050 | } |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 6051 | rc = sqlite3BtreeGetPage(pBt, maxRootPgno, &pMove, 0); |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 6052 | if( rc!=SQLITE_OK ){ |
| 6053 | return rc; |
| 6054 | } |
| 6055 | rc = freePage(pMove); |
| 6056 | releasePage(pMove); |
| 6057 | if( rc!=SQLITE_OK ){ |
| 6058 | return rc; |
| 6059 | } |
| 6060 | *piMoved = maxRootPgno; |
| 6061 | } |
| 6062 | |
danielk1977 | 599fcba | 2004-11-08 07:13:13 +0000 | [diff] [blame] | 6063 | /* Set the new 'max-root-page' value in the database header. This |
| 6064 | ** is the old value less one, less one more if that happens to |
| 6065 | ** be a root-page number, less one again if that is the |
| 6066 | ** PENDING_BYTE_PAGE. |
| 6067 | */ |
danielk1977 | 87a6e73 | 2004-11-05 12:58:25 +0000 | [diff] [blame] | 6068 | maxRootPgno--; |
danielk1977 | 599fcba | 2004-11-08 07:13:13 +0000 | [diff] [blame] | 6069 | if( maxRootPgno==PENDING_BYTE_PAGE(pBt) ){ |
| 6070 | maxRootPgno--; |
| 6071 | } |
danielk1977 | 266664d | 2006-02-10 08:24:21 +0000 | [diff] [blame] | 6072 | if( maxRootPgno==PTRMAP_PAGENO(pBt, maxRootPgno) ){ |
danielk1977 | 87a6e73 | 2004-11-05 12:58:25 +0000 | [diff] [blame] | 6073 | maxRootPgno--; |
| 6074 | } |
danielk1977 | 599fcba | 2004-11-08 07:13:13 +0000 | [diff] [blame] | 6075 | assert( maxRootPgno!=PENDING_BYTE_PAGE(pBt) ); |
| 6076 | |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 6077 | rc = sqlite3BtreeUpdateMeta(p, 4, maxRootPgno); |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 6078 | }else{ |
| 6079 | rc = freePage(pPage); |
| 6080 | releasePage(pPage); |
| 6081 | } |
| 6082 | #endif |
drh | 2aa679f | 2001-06-25 02:11:07 +0000 | [diff] [blame] | 6083 | }else{ |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 6084 | /* If sqlite3BtreeDropTable was called on page 1. */ |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 6085 | zeroPage(pPage, PTF_INTKEY|PTF_LEAF ); |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 6086 | releasePage(pPage); |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 6087 | } |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 6088 | return rc; |
| 6089 | } |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 6090 | int sqlite3BtreeDropTable(Btree *p, int iTable, int *piMoved){ |
| 6091 | int rc; |
| 6092 | sqlite3BtreeEnter(p); |
| 6093 | rc = btreeDropTable(p, iTable, piMoved); |
| 6094 | sqlite3BtreeLeave(p); |
| 6095 | return rc; |
| 6096 | } |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 6097 | |
drh | 001bbcb | 2003-03-19 03:14:00 +0000 | [diff] [blame] | 6098 | |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 6099 | /* |
drh | 23e11ca | 2004-05-04 17:27:28 +0000 | [diff] [blame] | 6100 | ** Read the meta-information out of a database file. Meta[0] |
| 6101 | ** is the number of free pages currently in the database. Meta[1] |
drh | a3b321d | 2004-05-11 09:31:31 +0000 | [diff] [blame] | 6102 | ** through meta[15] are available for use by higher layers. Meta[0] |
| 6103 | ** is read-only, the others are read/write. |
| 6104 | ** |
| 6105 | ** The schema layer numbers meta values differently. At the schema |
| 6106 | ** layer (and the SetCookie and ReadCookie opcodes) the number of |
| 6107 | ** free pages is not visible. So Cookie[0] is the same as Meta[1]. |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 6108 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 6109 | int sqlite3BtreeGetMeta(Btree *p, int idx, u32 *pMeta){ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 6110 | DbPage *pDbPage; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 6111 | int rc; |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 6112 | unsigned char *pP1; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 6113 | BtShared *pBt = p->pBt; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 6114 | |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 6115 | sqlite3BtreeEnter(p); |
| 6116 | |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 6117 | /* Reading a meta-data value requires a read-lock on page 1 (and hence |
| 6118 | ** the sqlite_master table. We grab this lock regardless of whether or |
| 6119 | ** not the SQLITE_ReadUncommitted flag is set (the table rooted at page |
| 6120 | ** 1 is treated as a special case by queryTableLock() and lockTable()). |
| 6121 | */ |
| 6122 | rc = queryTableLock(p, 1, READ_LOCK); |
| 6123 | if( rc!=SQLITE_OK ){ |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 6124 | sqlite3BtreeLeave(p); |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 6125 | return rc; |
| 6126 | } |
| 6127 | |
drh | 23e11ca | 2004-05-04 17:27:28 +0000 | [diff] [blame] | 6128 | assert( idx>=0 && idx<=15 ); |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 6129 | rc = sqlite3PagerGet(pBt->pPager, 1, &pDbPage); |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 6130 | if( rc ){ |
| 6131 | sqlite3BtreeLeave(p); |
| 6132 | return rc; |
| 6133 | } |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 6134 | pP1 = (unsigned char *)sqlite3PagerGetData(pDbPage); |
drh | 23e11ca | 2004-05-04 17:27:28 +0000 | [diff] [blame] | 6135 | *pMeta = get4byte(&pP1[36 + idx*4]); |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 6136 | sqlite3PagerUnref(pDbPage); |
drh | ae15787 | 2004-08-14 19:20:09 +0000 | [diff] [blame] | 6137 | |
danielk1977 | 599fcba | 2004-11-08 07:13:13 +0000 | [diff] [blame] | 6138 | /* If autovacuumed is disabled in this build but we are trying to |
| 6139 | ** access an autovacuumed database, then make the database readonly. |
| 6140 | */ |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 6141 | #ifdef SQLITE_OMIT_AUTOVACUUM |
drh | ae15787 | 2004-08-14 19:20:09 +0000 | [diff] [blame] | 6142 | if( idx==4 && *pMeta>0 ) pBt->readOnly = 1; |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 6143 | #endif |
drh | ae15787 | 2004-08-14 19:20:09 +0000 | [diff] [blame] | 6144 | |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 6145 | /* Grab the read-lock on page 1. */ |
| 6146 | rc = lockTable(p, 1, READ_LOCK); |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 6147 | sqlite3BtreeLeave(p); |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 6148 | return rc; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 6149 | } |
| 6150 | |
| 6151 | /* |
drh | 23e11ca | 2004-05-04 17:27:28 +0000 | [diff] [blame] | 6152 | ** Write meta-information back into the database. Meta[0] is |
| 6153 | ** read-only and may not be written. |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 6154 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 6155 | int sqlite3BtreeUpdateMeta(Btree *p, int idx, u32 iMeta){ |
| 6156 | BtShared *pBt = p->pBt; |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 6157 | unsigned char *pP1; |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 6158 | int rc; |
drh | 23e11ca | 2004-05-04 17:27:28 +0000 | [diff] [blame] | 6159 | assert( idx>=1 && idx<=15 ); |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 6160 | sqlite3BtreeEnter(p); |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 6161 | if( p->inTrans!=TRANS_WRITE ){ |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 6162 | rc = pBt->readOnly ? SQLITE_READONLY : SQLITE_ERROR; |
| 6163 | }else{ |
| 6164 | assert( pBt->pPage1!=0 ); |
| 6165 | pP1 = pBt->pPage1->aData; |
| 6166 | rc = sqlite3PagerWrite(pBt->pPage1->pDbPage); |
| 6167 | if( rc==SQLITE_OK ){ |
| 6168 | put4byte(&pP1[36 + idx*4], iMeta); |
danielk1977 | 4152e67 | 2007-09-12 17:01:45 +0000 | [diff] [blame^] | 6169 | #ifndef SQLITE_OMIT_AUTOVACUUM |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 6170 | if( idx==7 ){ |
| 6171 | assert( pBt->autoVacuum || iMeta==0 ); |
| 6172 | assert( iMeta==0 || iMeta==1 ); |
| 6173 | pBt->incrVacuum = iMeta; |
| 6174 | } |
danielk1977 | 4152e67 | 2007-09-12 17:01:45 +0000 | [diff] [blame^] | 6175 | #endif |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 6176 | } |
drh | 5df72a5 | 2002-06-06 23:16:05 +0000 | [diff] [blame] | 6177 | } |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 6178 | sqlite3BtreeLeave(p); |
| 6179 | return rc; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 6180 | } |
drh | 8c42ca9 | 2001-06-22 19:15:00 +0000 | [diff] [blame] | 6181 | |
drh | f328bc8 | 2004-05-10 23:29:49 +0000 | [diff] [blame] | 6182 | /* |
| 6183 | ** Return the flag byte at the beginning of the page that the cursor |
| 6184 | ** is currently pointing to. |
| 6185 | */ |
| 6186 | int sqlite3BtreeFlags(BtCursor *pCur){ |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 6187 | /* TODO: What about CURSOR_REQUIRESEEK state? Probably need to call |
drh | 777e4c4 | 2006-01-13 04:31:58 +0000 | [diff] [blame] | 6188 | ** restoreOrClearCursorPosition() here. |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 6189 | */ |
drh | f328bc8 | 2004-05-10 23:29:49 +0000 | [diff] [blame] | 6190 | MemPage *pPage = pCur->pPage; |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 6191 | assert( cursorHoldsMutex(pCur) ); |
drh | d0679ed | 2007-08-28 22:24:34 +0000 | [diff] [blame] | 6192 | assert( pPage->pBt==pCur->pBt ); |
drh | f328bc8 | 2004-05-10 23:29:49 +0000 | [diff] [blame] | 6193 | return pPage ? pPage->aData[pPage->hdrOffset] : 0; |
| 6194 | } |
| 6195 | |
drh | dd79342 | 2001-06-28 01:54:48 +0000 | [diff] [blame] | 6196 | |
drh | dd79342 | 2001-06-28 01:54:48 +0000 | [diff] [blame] | 6197 | /* |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6198 | ** Return the pager associated with a BTree. This routine is used for |
| 6199 | ** testing and debugging only. |
drh | dd79342 | 2001-06-28 01:54:48 +0000 | [diff] [blame] | 6200 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 6201 | Pager *sqlite3BtreePager(Btree *p){ |
| 6202 | return p->pBt->pPager; |
drh | dd79342 | 2001-06-28 01:54:48 +0000 | [diff] [blame] | 6203 | } |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6204 | |
drh | b7f9164 | 2004-10-31 02:22:47 +0000 | [diff] [blame] | 6205 | #ifndef SQLITE_OMIT_INTEGRITY_CHECK |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6206 | /* |
| 6207 | ** Append a message to the error message string. |
| 6208 | */ |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 6209 | static void checkAppendMsg( |
| 6210 | IntegrityCk *pCheck, |
| 6211 | char *zMsg1, |
| 6212 | const char *zFormat, |
| 6213 | ... |
| 6214 | ){ |
| 6215 | va_list ap; |
| 6216 | char *zMsg2; |
drh | 1dcdbc0 | 2007-01-27 02:24:54 +0000 | [diff] [blame] | 6217 | if( !pCheck->mxErr ) return; |
| 6218 | pCheck->mxErr--; |
| 6219 | pCheck->nErr++; |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 6220 | va_start(ap, zFormat); |
danielk1977 | 1e53695 | 2007-08-16 10:09:01 +0000 | [diff] [blame] | 6221 | zMsg2 = sqlite3VMPrintf(0, zFormat, ap); |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 6222 | va_end(ap); |
| 6223 | if( zMsg1==0 ) zMsg1 = ""; |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6224 | if( pCheck->zErrMsg ){ |
| 6225 | char *zOld = pCheck->zErrMsg; |
| 6226 | pCheck->zErrMsg = 0; |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 6227 | sqlite3SetString(&pCheck->zErrMsg, zOld, "\n", zMsg1, zMsg2, (char*)0); |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 6228 | sqlite3_free(zOld); |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6229 | }else{ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 6230 | sqlite3SetString(&pCheck->zErrMsg, zMsg1, zMsg2, (char*)0); |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6231 | } |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 6232 | sqlite3_free(zMsg2); |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6233 | } |
drh | b7f9164 | 2004-10-31 02:22:47 +0000 | [diff] [blame] | 6234 | #endif /* SQLITE_OMIT_INTEGRITY_CHECK */ |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6235 | |
drh | b7f9164 | 2004-10-31 02:22:47 +0000 | [diff] [blame] | 6236 | #ifndef SQLITE_OMIT_INTEGRITY_CHECK |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6237 | /* |
| 6238 | ** Add 1 to the reference count for page iPage. If this is the second |
| 6239 | ** reference to the page, add an error message to pCheck->zErrMsg. |
| 6240 | ** Return 1 if there are 2 ore more references to the page and 0 if |
| 6241 | ** if this is the first reference to the page. |
| 6242 | ** |
| 6243 | ** Also check that the page number is in bounds. |
| 6244 | */ |
drh | aaab572 | 2002-02-19 13:39:21 +0000 | [diff] [blame] | 6245 | static int checkRef(IntegrityCk *pCheck, int iPage, char *zContext){ |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6246 | if( iPage==0 ) return 1; |
drh | 0de8c11 | 2002-07-06 16:32:14 +0000 | [diff] [blame] | 6247 | if( iPage>pCheck->nPage || iPage<0 ){ |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 6248 | checkAppendMsg(pCheck, zContext, "invalid page number %d", iPage); |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6249 | return 1; |
| 6250 | } |
| 6251 | if( pCheck->anRef[iPage]==1 ){ |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 6252 | checkAppendMsg(pCheck, zContext, "2nd reference to page %d", iPage); |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6253 | return 1; |
| 6254 | } |
| 6255 | return (pCheck->anRef[iPage]++)>1; |
| 6256 | } |
| 6257 | |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 6258 | #ifndef SQLITE_OMIT_AUTOVACUUM |
| 6259 | /* |
| 6260 | ** Check that the entry in the pointer-map for page iChild maps to |
| 6261 | ** page iParent, pointer type ptrType. If not, append an error message |
| 6262 | ** to pCheck. |
| 6263 | */ |
| 6264 | static void checkPtrmap( |
| 6265 | IntegrityCk *pCheck, /* Integrity check context */ |
| 6266 | Pgno iChild, /* Child page number */ |
| 6267 | u8 eType, /* Expected pointer map type */ |
| 6268 | Pgno iParent, /* Expected pointer map parent page number */ |
| 6269 | char *zContext /* Context description (used for error msg) */ |
| 6270 | ){ |
| 6271 | int rc; |
| 6272 | u8 ePtrmapType; |
| 6273 | Pgno iPtrmapParent; |
| 6274 | |
| 6275 | rc = ptrmapGet(pCheck->pBt, iChild, &ePtrmapType, &iPtrmapParent); |
| 6276 | if( rc!=SQLITE_OK ){ |
| 6277 | checkAppendMsg(pCheck, zContext, "Failed to read ptrmap key=%d", iChild); |
| 6278 | return; |
| 6279 | } |
| 6280 | |
| 6281 | if( ePtrmapType!=eType || iPtrmapParent!=iParent ){ |
| 6282 | checkAppendMsg(pCheck, zContext, |
| 6283 | "Bad ptr map entry key=%d expected=(%d,%d) got=(%d,%d)", |
| 6284 | iChild, eType, iParent, ePtrmapType, iPtrmapParent); |
| 6285 | } |
| 6286 | } |
| 6287 | #endif |
| 6288 | |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6289 | /* |
| 6290 | ** Check the integrity of the freelist or of an overflow page list. |
| 6291 | ** Verify that the number of pages on the list is N. |
| 6292 | */ |
drh | 30e5875 | 2002-03-02 20:41:57 +0000 | [diff] [blame] | 6293 | static void checkList( |
| 6294 | IntegrityCk *pCheck, /* Integrity checking context */ |
| 6295 | int isFreeList, /* True for a freelist. False for overflow page list */ |
| 6296 | int iPage, /* Page number for first page in the list */ |
| 6297 | int N, /* Expected number of pages in the list */ |
| 6298 | char *zContext /* Context for error messages */ |
| 6299 | ){ |
| 6300 | int i; |
drh | 3a4c141 | 2004-05-09 20:40:11 +0000 | [diff] [blame] | 6301 | int expected = N; |
| 6302 | int iFirst = iPage; |
drh | 1dcdbc0 | 2007-01-27 02:24:54 +0000 | [diff] [blame] | 6303 | while( N-- > 0 && pCheck->mxErr ){ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 6304 | DbPage *pOvflPage; |
| 6305 | unsigned char *pOvflData; |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6306 | if( iPage<1 ){ |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 6307 | checkAppendMsg(pCheck, zContext, |
| 6308 | "%d of %d pages missing from overflow list starting at %d", |
drh | 3a4c141 | 2004-05-09 20:40:11 +0000 | [diff] [blame] | 6309 | N+1, expected, iFirst); |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6310 | break; |
| 6311 | } |
| 6312 | if( checkRef(pCheck, iPage, zContext) ) break; |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 6313 | if( sqlite3PagerGet(pCheck->pPager, (Pgno)iPage, &pOvflPage) ){ |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 6314 | checkAppendMsg(pCheck, zContext, "failed to get page %d", iPage); |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6315 | break; |
| 6316 | } |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 6317 | pOvflData = (unsigned char *)sqlite3PagerGetData(pOvflPage); |
drh | 30e5875 | 2002-03-02 20:41:57 +0000 | [diff] [blame] | 6318 | if( isFreeList ){ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 6319 | int n = get4byte(&pOvflData[4]); |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 6320 | #ifndef SQLITE_OMIT_AUTOVACUUM |
| 6321 | if( pCheck->pBt->autoVacuum ){ |
| 6322 | checkPtrmap(pCheck, iPage, PTRMAP_FREEPAGE, 0, zContext); |
| 6323 | } |
| 6324 | #endif |
drh | 855eb1c | 2004-08-31 13:45:11 +0000 | [diff] [blame] | 6325 | if( n>pCheck->pBt->usableSize/4-8 ){ |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 6326 | checkAppendMsg(pCheck, zContext, |
| 6327 | "freelist leaf count too big on page %d", iPage); |
drh | ee696e2 | 2004-08-30 16:52:17 +0000 | [diff] [blame] | 6328 | N--; |
| 6329 | }else{ |
| 6330 | for(i=0; i<n; i++){ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 6331 | Pgno iFreePage = get4byte(&pOvflData[8+i*4]); |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 6332 | #ifndef SQLITE_OMIT_AUTOVACUUM |
| 6333 | if( pCheck->pBt->autoVacuum ){ |
| 6334 | checkPtrmap(pCheck, iFreePage, PTRMAP_FREEPAGE, 0, zContext); |
| 6335 | } |
| 6336 | #endif |
| 6337 | checkRef(pCheck, iFreePage, zContext); |
drh | ee696e2 | 2004-08-30 16:52:17 +0000 | [diff] [blame] | 6338 | } |
| 6339 | N -= n; |
drh | 30e5875 | 2002-03-02 20:41:57 +0000 | [diff] [blame] | 6340 | } |
drh | 30e5875 | 2002-03-02 20:41:57 +0000 | [diff] [blame] | 6341 | } |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 6342 | #ifndef SQLITE_OMIT_AUTOVACUUM |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 6343 | else{ |
| 6344 | /* If this database supports auto-vacuum and iPage is not the last |
| 6345 | ** page in this overflow list, check that the pointer-map entry for |
| 6346 | ** the following page matches iPage. |
| 6347 | */ |
| 6348 | if( pCheck->pBt->autoVacuum && N>0 ){ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 6349 | i = get4byte(pOvflData); |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 6350 | checkPtrmap(pCheck, i, PTRMAP_OVERFLOW2, iPage, zContext); |
| 6351 | } |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 6352 | } |
| 6353 | #endif |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 6354 | iPage = get4byte(pOvflData); |
| 6355 | sqlite3PagerUnref(pOvflPage); |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6356 | } |
| 6357 | } |
drh | b7f9164 | 2004-10-31 02:22:47 +0000 | [diff] [blame] | 6358 | #endif /* SQLITE_OMIT_INTEGRITY_CHECK */ |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6359 | |
drh | b7f9164 | 2004-10-31 02:22:47 +0000 | [diff] [blame] | 6360 | #ifndef SQLITE_OMIT_INTEGRITY_CHECK |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6361 | /* |
| 6362 | ** Do various sanity checks on a single page of a tree. Return |
| 6363 | ** the tree depth. Root pages return 0. Parents of root pages |
| 6364 | ** return 1, and so forth. |
| 6365 | ** |
| 6366 | ** These checks are done: |
| 6367 | ** |
| 6368 | ** 1. Make sure that cells and freeblocks do not overlap |
| 6369 | ** but combine to completely cover the page. |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 6370 | ** NO 2. Make sure cell keys are in order. |
| 6371 | ** NO 3. Make sure no key is less than or equal to zLowerBound. |
| 6372 | ** NO 4. Make sure no key is greater than or equal to zUpperBound. |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6373 | ** 5. Check the integrity of overflow pages. |
| 6374 | ** 6. Recursively call checkTreePage on all children. |
| 6375 | ** 7. Verify that the depth of all children is the same. |
drh | 6019e16 | 2001-07-02 17:51:45 +0000 | [diff] [blame] | 6376 | ** 8. Make sure this page is at least 33% full or else it is |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6377 | ** the root of the tree. |
| 6378 | */ |
| 6379 | static int checkTreePage( |
drh | aaab572 | 2002-02-19 13:39:21 +0000 | [diff] [blame] | 6380 | IntegrityCk *pCheck, /* Context for the sanity check */ |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6381 | int iPage, /* Page number of the page to check */ |
| 6382 | MemPage *pParent, /* Parent page */ |
drh | 7416170 | 2006-02-24 02:53:49 +0000 | [diff] [blame] | 6383 | char *zParentContext /* Parent context */ |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6384 | ){ |
| 6385 | MemPage *pPage; |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 6386 | int i, rc, depth, d2, pgno, cnt; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 6387 | int hdr, cellStart; |
| 6388 | int nCell; |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 6389 | u8 *data; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 6390 | BtShared *pBt; |
drh | 4f26bb6 | 2005-09-08 14:17:20 +0000 | [diff] [blame] | 6391 | int usableSize; |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6392 | char zContext[100]; |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 6393 | char *hit; |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6394 | |
drh | 5bb3eb9 | 2007-05-04 13:15:55 +0000 | [diff] [blame] | 6395 | sqlite3_snprintf(sizeof(zContext), zContext, "Page %d: ", iPage); |
danielk1977 | ef73ee9 | 2004-11-06 12:26:07 +0000 | [diff] [blame] | 6396 | |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6397 | /* Check that the page exists |
| 6398 | */ |
drh | d9cb6ac | 2005-10-20 07:28:17 +0000 | [diff] [blame] | 6399 | pBt = pCheck->pBt; |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 6400 | usableSize = pBt->usableSize; |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6401 | if( iPage==0 ) return 0; |
| 6402 | if( checkRef(pCheck, iPage, zParentContext) ) return 0; |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 6403 | if( (rc = sqlite3BtreeGetPage(pBt, (Pgno)iPage, &pPage, 0))!=0 ){ |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 6404 | checkAppendMsg(pCheck, zContext, |
| 6405 | "unable to get the page. error code=%d", rc); |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6406 | return 0; |
| 6407 | } |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 6408 | if( (rc = sqlite3BtreeInitPage(pPage, pParent))!=0 ){ |
| 6409 | checkAppendMsg(pCheck, zContext, |
| 6410 | "sqlite3BtreeInitPage() returns error code %d", rc); |
drh | 9102529 | 2004-05-03 19:49:32 +0000 | [diff] [blame] | 6411 | releasePage(pPage); |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6412 | return 0; |
| 6413 | } |
| 6414 | |
| 6415 | /* Check out all the cells. |
| 6416 | */ |
| 6417 | depth = 0; |
drh | 1dcdbc0 | 2007-01-27 02:24:54 +0000 | [diff] [blame] | 6418 | for(i=0; i<pPage->nCell && pCheck->mxErr; i++){ |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 6419 | u8 *pCell; |
| 6420 | int sz; |
| 6421 | CellInfo info; |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6422 | |
| 6423 | /* Check payload overflow pages |
| 6424 | */ |
drh | 5bb3eb9 | 2007-05-04 13:15:55 +0000 | [diff] [blame] | 6425 | sqlite3_snprintf(sizeof(zContext), zContext, |
| 6426 | "On tree page %d cell %d: ", iPage, i); |
danielk1977 | 1cc5ed8 | 2007-05-16 17:28:43 +0000 | [diff] [blame] | 6427 | pCell = findCell(pPage,i); |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 6428 | sqlite3BtreeParseCellPtr(pPage, pCell, &info); |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 6429 | sz = info.nData; |
| 6430 | if( !pPage->intKey ) sz += info.nKey; |
drh | 7236583 | 2007-03-06 15:53:44 +0000 | [diff] [blame] | 6431 | assert( sz==info.nPayload ); |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 6432 | if( sz>info.nLocal ){ |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 6433 | int nPage = (sz - info.nLocal + usableSize - 5)/(usableSize - 4); |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 6434 | Pgno pgnoOvfl = get4byte(&pCell[info.iOverflow]); |
| 6435 | #ifndef SQLITE_OMIT_AUTOVACUUM |
| 6436 | if( pBt->autoVacuum ){ |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 6437 | checkPtrmap(pCheck, pgnoOvfl, PTRMAP_OVERFLOW1, iPage, zContext); |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 6438 | } |
| 6439 | #endif |
| 6440 | checkList(pCheck, 0, pgnoOvfl, nPage, zContext); |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6441 | } |
| 6442 | |
| 6443 | /* Check sanity of left child page. |
| 6444 | */ |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 6445 | if( !pPage->leaf ){ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 6446 | pgno = get4byte(pCell); |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 6447 | #ifndef SQLITE_OMIT_AUTOVACUUM |
| 6448 | if( pBt->autoVacuum ){ |
| 6449 | checkPtrmap(pCheck, pgno, PTRMAP_BTREE, iPage, zContext); |
| 6450 | } |
| 6451 | #endif |
drh | 7416170 | 2006-02-24 02:53:49 +0000 | [diff] [blame] | 6452 | d2 = checkTreePage(pCheck,pgno,pPage,zContext); |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 6453 | if( i>0 && d2!=depth ){ |
| 6454 | checkAppendMsg(pCheck, zContext, "Child page depth differs"); |
| 6455 | } |
| 6456 | depth = d2; |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6457 | } |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6458 | } |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 6459 | if( !pPage->leaf ){ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 6460 | pgno = get4byte(&pPage->aData[pPage->hdrOffset+8]); |
drh | 5bb3eb9 | 2007-05-04 13:15:55 +0000 | [diff] [blame] | 6461 | sqlite3_snprintf(sizeof(zContext), zContext, |
| 6462 | "On page %d at right child: ", iPage); |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 6463 | #ifndef SQLITE_OMIT_AUTOVACUUM |
| 6464 | if( pBt->autoVacuum ){ |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 6465 | checkPtrmap(pCheck, pgno, PTRMAP_BTREE, iPage, 0); |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 6466 | } |
| 6467 | #endif |
drh | 7416170 | 2006-02-24 02:53:49 +0000 | [diff] [blame] | 6468 | checkTreePage(pCheck, pgno, pPage, zContext); |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 6469 | } |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6470 | |
| 6471 | /* Check for complete coverage of the page |
| 6472 | */ |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 6473 | data = pPage->aData; |
| 6474 | hdr = pPage->hdrOffset; |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 6475 | hit = sqlite3MallocZero( usableSize ); |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 6476 | if( hit ){ |
| 6477 | memset(hit, 1, get2byte(&data[hdr+5])); |
| 6478 | nCell = get2byte(&data[hdr+3]); |
| 6479 | cellStart = hdr + 12 - 4*pPage->leaf; |
| 6480 | for(i=0; i<nCell; i++){ |
| 6481 | int pc = get2byte(&data[cellStart+i*2]); |
| 6482 | int size = cellSizePtr(pPage, &data[pc]); |
| 6483 | int j; |
danielk1977 | 7701e81 | 2005-01-10 12:59:51 +0000 | [diff] [blame] | 6484 | if( (pc+size-1)>=usableSize || pc<0 ){ |
| 6485 | checkAppendMsg(pCheck, 0, |
| 6486 | "Corruption detected in cell %d on page %d",i,iPage,0); |
| 6487 | }else{ |
| 6488 | for(j=pc+size-1; j>=pc; j--) hit[j]++; |
| 6489 | } |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 6490 | } |
| 6491 | for(cnt=0, i=get2byte(&data[hdr+1]); i>0 && i<usableSize && cnt<10000; |
| 6492 | cnt++){ |
| 6493 | int size = get2byte(&data[i+2]); |
| 6494 | int j; |
danielk1977 | 7701e81 | 2005-01-10 12:59:51 +0000 | [diff] [blame] | 6495 | if( (i+size-1)>=usableSize || i<0 ){ |
| 6496 | checkAppendMsg(pCheck, 0, |
| 6497 | "Corruption detected in cell %d on page %d",i,iPage,0); |
| 6498 | }else{ |
| 6499 | for(j=i+size-1; j>=i; j--) hit[j]++; |
| 6500 | } |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 6501 | i = get2byte(&data[i]); |
| 6502 | } |
| 6503 | for(i=cnt=0; i<usableSize; i++){ |
| 6504 | if( hit[i]==0 ){ |
| 6505 | cnt++; |
| 6506 | }else if( hit[i]>1 ){ |
| 6507 | checkAppendMsg(pCheck, 0, |
| 6508 | "Multiple uses for byte %d of page %d", i, iPage); |
| 6509 | break; |
| 6510 | } |
| 6511 | } |
| 6512 | if( cnt!=data[hdr+7] ){ |
| 6513 | checkAppendMsg(pCheck, 0, |
| 6514 | "Fragmented space is %d byte reported as %d on page %d", |
| 6515 | cnt, data[hdr+7], iPage); |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6516 | } |
| 6517 | } |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 6518 | sqlite3_free(hit); |
drh | 6019e16 | 2001-07-02 17:51:45 +0000 | [diff] [blame] | 6519 | |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 6520 | releasePage(pPage); |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 6521 | return depth+1; |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6522 | } |
drh | b7f9164 | 2004-10-31 02:22:47 +0000 | [diff] [blame] | 6523 | #endif /* SQLITE_OMIT_INTEGRITY_CHECK */ |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6524 | |
drh | b7f9164 | 2004-10-31 02:22:47 +0000 | [diff] [blame] | 6525 | #ifndef SQLITE_OMIT_INTEGRITY_CHECK |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6526 | /* |
| 6527 | ** This routine does a complete check of the given BTree file. aRoot[] is |
| 6528 | ** an array of pages numbers were each page number is the root page of |
| 6529 | ** a table. nRoot is the number of entries in aRoot. |
| 6530 | ** |
| 6531 | ** If everything checks out, this routine returns NULL. If something is |
| 6532 | ** amiss, an error message is written into memory obtained from malloc() |
| 6533 | ** and a pointer to that error message is returned. The calling function |
| 6534 | ** is responsible for freeing the error message when it is done. |
| 6535 | */ |
drh | 1dcdbc0 | 2007-01-27 02:24:54 +0000 | [diff] [blame] | 6536 | char *sqlite3BtreeIntegrityCheck( |
| 6537 | Btree *p, /* The btree to be checked */ |
| 6538 | int *aRoot, /* An array of root pages numbers for individual trees */ |
| 6539 | int nRoot, /* Number of entries in aRoot[] */ |
| 6540 | int mxErr, /* Stop reporting errors after this many */ |
| 6541 | int *pnErr /* Write number of errors seen to this variable */ |
| 6542 | ){ |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6543 | int i; |
| 6544 | int nRef; |
drh | aaab572 | 2002-02-19 13:39:21 +0000 | [diff] [blame] | 6545 | IntegrityCk sCheck; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 6546 | BtShared *pBt = p->pBt; |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6547 | |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 6548 | sqlite3BtreeEnter(p); |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 6549 | nRef = sqlite3PagerRefcount(pBt->pPager); |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 6550 | if( lockBtreeWithRetry(p)!=SQLITE_OK ){ |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 6551 | sqlite3BtreeLeave(p); |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 6552 | return sqlite3StrDup("Unable to acquire a read lock on the database"); |
drh | efc251d | 2001-07-01 22:12:01 +0000 | [diff] [blame] | 6553 | } |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6554 | sCheck.pBt = pBt; |
| 6555 | sCheck.pPager = pBt->pPager; |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 6556 | sCheck.nPage = sqlite3PagerPagecount(sCheck.pPager); |
drh | 1dcdbc0 | 2007-01-27 02:24:54 +0000 | [diff] [blame] | 6557 | sCheck.mxErr = mxErr; |
| 6558 | sCheck.nErr = 0; |
| 6559 | *pnErr = 0; |
danielk1977 | e5321f0 | 2007-04-27 07:05:44 +0000 | [diff] [blame] | 6560 | #ifndef SQLITE_OMIT_AUTOVACUUM |
| 6561 | if( pBt->nTrunc!=0 ){ |
| 6562 | sCheck.nPage = pBt->nTrunc; |
| 6563 | } |
| 6564 | #endif |
drh | 0de8c11 | 2002-07-06 16:32:14 +0000 | [diff] [blame] | 6565 | if( sCheck.nPage==0 ){ |
| 6566 | unlockBtreeIfUnused(pBt); |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 6567 | sqlite3BtreeLeave(p); |
drh | 0de8c11 | 2002-07-06 16:32:14 +0000 | [diff] [blame] | 6568 | return 0; |
| 6569 | } |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 6570 | sCheck.anRef = sqlite3_malloc( (sCheck.nPage+1)*sizeof(sCheck.anRef[0]) ); |
danielk1977 | ac245ec | 2005-01-14 13:50:11 +0000 | [diff] [blame] | 6571 | if( !sCheck.anRef ){ |
| 6572 | unlockBtreeIfUnused(pBt); |
drh | 1dcdbc0 | 2007-01-27 02:24:54 +0000 | [diff] [blame] | 6573 | *pnErr = 1; |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 6574 | sqlite3BtreeLeave(p); |
danielk1977 | 1e53695 | 2007-08-16 10:09:01 +0000 | [diff] [blame] | 6575 | return sqlite3MPrintf(p->pSqlite, "Unable to malloc %d bytes", |
danielk1977 | ac245ec | 2005-01-14 13:50:11 +0000 | [diff] [blame] | 6576 | (sCheck.nPage+1)*sizeof(sCheck.anRef[0])); |
| 6577 | } |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 6578 | for(i=0; i<=sCheck.nPage; i++){ sCheck.anRef[i] = 0; } |
drh | 42cac6d | 2004-11-20 20:31:11 +0000 | [diff] [blame] | 6579 | i = PENDING_BYTE_PAGE(pBt); |
drh | 1f59571 | 2004-06-15 01:40:29 +0000 | [diff] [blame] | 6580 | if( i<=sCheck.nPage ){ |
| 6581 | sCheck.anRef[i] = 1; |
| 6582 | } |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6583 | sCheck.zErrMsg = 0; |
| 6584 | |
| 6585 | /* Check the integrity of the freelist |
| 6586 | */ |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 6587 | checkList(&sCheck, 1, get4byte(&pBt->pPage1->aData[32]), |
| 6588 | get4byte(&pBt->pPage1->aData[36]), "Main freelist: "); |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6589 | |
| 6590 | /* Check all the tables. |
| 6591 | */ |
drh | 1dcdbc0 | 2007-01-27 02:24:54 +0000 | [diff] [blame] | 6592 | for(i=0; i<nRoot && sCheck.mxErr; i++){ |
drh | 4ff6dfa | 2002-03-03 23:06:00 +0000 | [diff] [blame] | 6593 | if( aRoot[i]==0 ) continue; |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 6594 | #ifndef SQLITE_OMIT_AUTOVACUUM |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 6595 | if( pBt->autoVacuum && aRoot[i]>1 ){ |
| 6596 | checkPtrmap(&sCheck, aRoot[i], PTRMAP_ROOTPAGE, 0, 0); |
| 6597 | } |
| 6598 | #endif |
drh | 7416170 | 2006-02-24 02:53:49 +0000 | [diff] [blame] | 6599 | checkTreePage(&sCheck, aRoot[i], 0, "List of tree roots: "); |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6600 | } |
| 6601 | |
| 6602 | /* Make sure every page in the file is referenced |
| 6603 | */ |
drh | 1dcdbc0 | 2007-01-27 02:24:54 +0000 | [diff] [blame] | 6604 | for(i=1; i<=sCheck.nPage && sCheck.mxErr; i++){ |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 6605 | #ifdef SQLITE_OMIT_AUTOVACUUM |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6606 | if( sCheck.anRef[i]==0 ){ |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 6607 | checkAppendMsg(&sCheck, 0, "Page %d is never used", i); |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6608 | } |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 6609 | #else |
| 6610 | /* If the database supports auto-vacuum, make sure no tables contain |
| 6611 | ** references to pointer-map pages. |
| 6612 | */ |
| 6613 | if( sCheck.anRef[i]==0 && |
danielk1977 | 266664d | 2006-02-10 08:24:21 +0000 | [diff] [blame] | 6614 | (PTRMAP_PAGENO(pBt, i)!=i || !pBt->autoVacuum) ){ |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 6615 | checkAppendMsg(&sCheck, 0, "Page %d is never used", i); |
| 6616 | } |
| 6617 | if( sCheck.anRef[i]!=0 && |
danielk1977 | 266664d | 2006-02-10 08:24:21 +0000 | [diff] [blame] | 6618 | (PTRMAP_PAGENO(pBt, i)==i && pBt->autoVacuum) ){ |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 6619 | checkAppendMsg(&sCheck, 0, "Pointer map page %d is referenced", i); |
| 6620 | } |
| 6621 | #endif |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6622 | } |
| 6623 | |
| 6624 | /* Make sure this analysis did not leave any unref() pages |
| 6625 | */ |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 6626 | unlockBtreeIfUnused(pBt); |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 6627 | if( nRef != sqlite3PagerRefcount(pBt->pPager) ){ |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 6628 | checkAppendMsg(&sCheck, 0, |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6629 | "Outstanding page count goes from %d to %d during this analysis", |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 6630 | nRef, sqlite3PagerRefcount(pBt->pPager) |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6631 | ); |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6632 | } |
| 6633 | |
| 6634 | /* Clean up and report errors. |
| 6635 | */ |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 6636 | sqlite3BtreeLeave(p); |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 6637 | sqlite3_free(sCheck.anRef); |
drh | 1dcdbc0 | 2007-01-27 02:24:54 +0000 | [diff] [blame] | 6638 | *pnErr = sCheck.nErr; |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6639 | return sCheck.zErrMsg; |
| 6640 | } |
drh | b7f9164 | 2004-10-31 02:22:47 +0000 | [diff] [blame] | 6641 | #endif /* SQLITE_OMIT_INTEGRITY_CHECK */ |
paul | b95a886 | 2003-04-01 21:16:41 +0000 | [diff] [blame] | 6642 | |
drh | 73509ee | 2003-04-06 20:44:45 +0000 | [diff] [blame] | 6643 | /* |
| 6644 | ** Return the full pathname of the underlying database file. |
drh | d0679ed | 2007-08-28 22:24:34 +0000 | [diff] [blame] | 6645 | ** |
| 6646 | ** The pager filename is invariant as long as the pager is |
| 6647 | ** open so it is safe to access without the BtShared mutex. |
drh | 73509ee | 2003-04-06 20:44:45 +0000 | [diff] [blame] | 6648 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 6649 | const char *sqlite3BtreeGetFilename(Btree *p){ |
| 6650 | assert( p->pBt->pPager!=0 ); |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 6651 | return sqlite3PagerFilename(p->pBt->pPager); |
drh | 73509ee | 2003-04-06 20:44:45 +0000 | [diff] [blame] | 6652 | } |
| 6653 | |
| 6654 | /* |
danielk1977 | 5865e3d | 2004-06-14 06:03:57 +0000 | [diff] [blame] | 6655 | ** Return the pathname of the directory that contains the database file. |
drh | d0679ed | 2007-08-28 22:24:34 +0000 | [diff] [blame] | 6656 | ** |
| 6657 | ** The pager directory name is invariant as long as the pager is |
| 6658 | ** open so it is safe to access without the BtShared mutex. |
danielk1977 | 5865e3d | 2004-06-14 06:03:57 +0000 | [diff] [blame] | 6659 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 6660 | const char *sqlite3BtreeGetDirname(Btree *p){ |
| 6661 | assert( p->pBt->pPager!=0 ); |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 6662 | return sqlite3PagerDirname(p->pBt->pPager); |
danielk1977 | 5865e3d | 2004-06-14 06:03:57 +0000 | [diff] [blame] | 6663 | } |
| 6664 | |
| 6665 | /* |
| 6666 | ** Return the pathname of the journal file for this database. The return |
| 6667 | ** value of this routine is the same regardless of whether the journal file |
| 6668 | ** has been created or not. |
drh | d0679ed | 2007-08-28 22:24:34 +0000 | [diff] [blame] | 6669 | ** |
| 6670 | ** The pager journal filename is invariant as long as the pager is |
| 6671 | ** open so it is safe to access without the BtShared mutex. |
danielk1977 | 5865e3d | 2004-06-14 06:03:57 +0000 | [diff] [blame] | 6672 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 6673 | const char *sqlite3BtreeGetJournalname(Btree *p){ |
| 6674 | assert( p->pBt->pPager!=0 ); |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 6675 | return sqlite3PagerJournalname(p->pBt->pPager); |
danielk1977 | 5865e3d | 2004-06-14 06:03:57 +0000 | [diff] [blame] | 6676 | } |
| 6677 | |
drh | b7f9164 | 2004-10-31 02:22:47 +0000 | [diff] [blame] | 6678 | #ifndef SQLITE_OMIT_VACUUM |
danielk1977 | 5865e3d | 2004-06-14 06:03:57 +0000 | [diff] [blame] | 6679 | /* |
drh | f7c5753 | 2003-04-25 13:22:51 +0000 | [diff] [blame] | 6680 | ** Copy the complete content of pBtFrom into pBtTo. A transaction |
| 6681 | ** must be active for both files. |
| 6682 | ** |
| 6683 | ** The size of file pBtFrom may be reduced by this operation. |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 6684 | ** If anything goes wrong, the transaction on pBtFrom is rolled back. |
drh | 73509ee | 2003-04-06 20:44:45 +0000 | [diff] [blame] | 6685 | */ |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 6686 | static int btreeCopyFile(Btree *pTo, Btree *pFrom){ |
drh | f7c5753 | 2003-04-25 13:22:51 +0000 | [diff] [blame] | 6687 | int rc = SQLITE_OK; |
drh | 50f2f43 | 2005-09-16 11:32:18 +0000 | [diff] [blame] | 6688 | Pgno i, nPage, nToPage, iSkip; |
drh | f7c5753 | 2003-04-25 13:22:51 +0000 | [diff] [blame] | 6689 | |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 6690 | BtShared *pBtTo = pTo->pBt; |
| 6691 | BtShared *pBtFrom = pFrom->pBt; |
| 6692 | |
| 6693 | if( pTo->inTrans!=TRANS_WRITE || pFrom->inTrans!=TRANS_WRITE ){ |
danielk1977 | ee5741e | 2004-05-31 10:01:34 +0000 | [diff] [blame] | 6694 | return SQLITE_ERROR; |
| 6695 | } |
drh | f7c5753 | 2003-04-25 13:22:51 +0000 | [diff] [blame] | 6696 | if( pBtTo->pCursor ) return SQLITE_BUSY; |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 6697 | nToPage = sqlite3PagerPagecount(pBtTo->pPager); |
| 6698 | nPage = sqlite3PagerPagecount(pBtFrom->pPager); |
drh | 50f2f43 | 2005-09-16 11:32:18 +0000 | [diff] [blame] | 6699 | iSkip = PENDING_BYTE_PAGE(pBtTo); |
danielk1977 | 369f27e | 2004-06-15 11:40:04 +0000 | [diff] [blame] | 6700 | for(i=1; rc==SQLITE_OK && i<=nPage; i++){ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 6701 | DbPage *pDbPage; |
drh | 50f2f43 | 2005-09-16 11:32:18 +0000 | [diff] [blame] | 6702 | if( i==iSkip ) continue; |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 6703 | rc = sqlite3PagerGet(pBtFrom->pPager, i, &pDbPage); |
drh | f7c5753 | 2003-04-25 13:22:51 +0000 | [diff] [blame] | 6704 | if( rc ) break; |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 6705 | rc = sqlite3PagerOverwrite(pBtTo->pPager, i, sqlite3PagerGetData(pDbPage)); |
| 6706 | sqlite3PagerUnref(pDbPage); |
drh | f7c5753 | 2003-04-25 13:22:51 +0000 | [diff] [blame] | 6707 | } |
drh | 538f570 | 2007-04-13 02:14:30 +0000 | [diff] [blame] | 6708 | |
| 6709 | /* If the file is shrinking, journal the pages that are being truncated |
| 6710 | ** so that they can be rolled back if the commit fails. |
| 6711 | */ |
drh | 2e6d11b | 2003-04-25 15:37:57 +0000 | [diff] [blame] | 6712 | for(i=nPage+1; rc==SQLITE_OK && i<=nToPage; i++){ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 6713 | DbPage *pDbPage; |
drh | 4928570 | 2005-09-17 15:20:26 +0000 | [diff] [blame] | 6714 | if( i==iSkip ) continue; |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 6715 | rc = sqlite3PagerGet(pBtTo->pPager, i, &pDbPage); |
drh | 2e6d11b | 2003-04-25 15:37:57 +0000 | [diff] [blame] | 6716 | if( rc ) break; |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 6717 | rc = sqlite3PagerWrite(pDbPage); |
drh | 538f570 | 2007-04-13 02:14:30 +0000 | [diff] [blame] | 6718 | sqlite3PagerDontWrite(pDbPage); |
| 6719 | /* Yeah. It seems wierd to call DontWrite() right after Write(). But |
| 6720 | ** that is because the names of those procedures do not exactly |
| 6721 | ** represent what they do. Write() really means "put this page in the |
| 6722 | ** rollback journal and mark it as dirty so that it will be written |
| 6723 | ** to the database file later." DontWrite() undoes the second part of |
| 6724 | ** that and prevents the page from being written to the database. The |
| 6725 | ** page is still on the rollback journal, though. And that is the whole |
| 6726 | ** point of this loop: to put pages on the rollback journal. */ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 6727 | sqlite3PagerUnref(pDbPage); |
drh | 2e6d11b | 2003-04-25 15:37:57 +0000 | [diff] [blame] | 6728 | } |
| 6729 | if( !rc && nPage<nToPage ){ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 6730 | rc = sqlite3PagerTruncate(pBtTo->pPager, nPage); |
drh | 2e6d11b | 2003-04-25 15:37:57 +0000 | [diff] [blame] | 6731 | } |
drh | 538f570 | 2007-04-13 02:14:30 +0000 | [diff] [blame] | 6732 | |
drh | f7c5753 | 2003-04-25 13:22:51 +0000 | [diff] [blame] | 6733 | if( rc ){ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 6734 | sqlite3BtreeRollback(pTo); |
drh | f7c5753 | 2003-04-25 13:22:51 +0000 | [diff] [blame] | 6735 | } |
| 6736 | return rc; |
drh | 73509ee | 2003-04-06 20:44:45 +0000 | [diff] [blame] | 6737 | } |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 6738 | int sqlite3BtreeCopyFile(Btree *pTo, Btree *pFrom){ |
| 6739 | int rc; |
| 6740 | sqlite3BtreeEnter(pTo); |
| 6741 | sqlite3BtreeEnter(pFrom); |
| 6742 | rc = btreeCopyFile(pTo, pFrom); |
| 6743 | sqlite3BtreeLeave(pFrom); |
| 6744 | sqlite3BtreeLeave(pTo); |
| 6745 | return rc; |
| 6746 | } |
| 6747 | |
drh | b7f9164 | 2004-10-31 02:22:47 +0000 | [diff] [blame] | 6748 | #endif /* SQLITE_OMIT_VACUUM */ |
danielk1977 | 1d850a7 | 2004-05-31 08:26:49 +0000 | [diff] [blame] | 6749 | |
| 6750 | /* |
| 6751 | ** Return non-zero if a transaction is active. |
| 6752 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 6753 | int sqlite3BtreeIsInTrans(Btree *p){ |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 6754 | assert( p==0 || sqlite3_mutex_held(p->pSqlite->mutex) ); |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 6755 | return (p && (p->inTrans==TRANS_WRITE)); |
danielk1977 | 1d850a7 | 2004-05-31 08:26:49 +0000 | [diff] [blame] | 6756 | } |
| 6757 | |
| 6758 | /* |
| 6759 | ** Return non-zero if a statement transaction is active. |
| 6760 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 6761 | int sqlite3BtreeIsInStmt(Btree *p){ |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 6762 | assert( sqlite3BtreeHoldsMutex(p) ); |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 6763 | return (p->pBt && p->pBt->inStmt); |
danielk1977 | 1d850a7 | 2004-05-31 08:26:49 +0000 | [diff] [blame] | 6764 | } |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 6765 | |
| 6766 | /* |
danielk1977 | 2372c2b | 2006-06-27 16:34:56 +0000 | [diff] [blame] | 6767 | ** Return non-zero if a read (or write) transaction is active. |
| 6768 | */ |
| 6769 | int sqlite3BtreeIsInReadTrans(Btree *p){ |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 6770 | assert( sqlite3_mutex_held(p->pSqlite->mutex) ); |
danielk1977 | 2372c2b | 2006-06-27 16:34:56 +0000 | [diff] [blame] | 6771 | return (p && (p->inTrans!=TRANS_NONE)); |
| 6772 | } |
| 6773 | |
| 6774 | /* |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 6775 | ** This function returns a pointer to a blob of memory associated with |
| 6776 | ** a single shared-btree. The memory is used by client code for it's own |
| 6777 | ** purposes (for example, to store a high-level schema associated with |
| 6778 | ** the shared-btree). The btree layer manages reference counting issues. |
| 6779 | ** |
| 6780 | ** The first time this is called on a shared-btree, nBytes bytes of memory |
| 6781 | ** are allocated, zeroed, and returned to the caller. For each subsequent |
| 6782 | ** call the nBytes parameter is ignored and a pointer to the same blob |
| 6783 | ** of memory returned. |
| 6784 | ** |
| 6785 | ** Just before the shared-btree is closed, the function passed as the |
| 6786 | ** xFree argument when the memory allocation was made is invoked on the |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 6787 | ** blob of allocated memory. This function should not call sqlite3_free() |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 6788 | ** on the memory, the btree layer does that. |
| 6789 | */ |
| 6790 | void *sqlite3BtreeSchema(Btree *p, int nBytes, void(*xFree)(void *)){ |
| 6791 | BtShared *pBt = p->pBt; |
drh | 2764170 | 2007-08-22 02:56:42 +0000 | [diff] [blame] | 6792 | sqlite3BtreeEnter(p); |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 6793 | if( !pBt->pSchema ){ |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 6794 | pBt->pSchema = sqlite3MallocZero(nBytes); |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 6795 | pBt->xFreeSchema = xFree; |
| 6796 | } |
drh | 2764170 | 2007-08-22 02:56:42 +0000 | [diff] [blame] | 6797 | sqlite3BtreeLeave(p); |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 6798 | return pBt->pSchema; |
| 6799 | } |
| 6800 | |
danielk1977 | c87d34d | 2006-01-06 13:00:28 +0000 | [diff] [blame] | 6801 | /* |
| 6802 | ** Return true if another user of the same shared btree as the argument |
| 6803 | ** handle holds an exclusive lock on the sqlite_master table. |
| 6804 | */ |
| 6805 | int sqlite3BtreeSchemaLocked(Btree *p){ |
drh | 2764170 | 2007-08-22 02:56:42 +0000 | [diff] [blame] | 6806 | int rc; |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 6807 | assert( sqlite3_mutex_held(p->pSqlite->mutex) ); |
drh | 2764170 | 2007-08-22 02:56:42 +0000 | [diff] [blame] | 6808 | sqlite3BtreeEnter(p); |
| 6809 | rc = (queryTableLock(p, MASTER_ROOT, READ_LOCK)!=SQLITE_OK); |
| 6810 | sqlite3BtreeLeave(p); |
| 6811 | return rc; |
danielk1977 | c87d34d | 2006-01-06 13:00:28 +0000 | [diff] [blame] | 6812 | } |
| 6813 | |
drh | a154dcd | 2006-03-22 22:10:07 +0000 | [diff] [blame] | 6814 | |
| 6815 | #ifndef SQLITE_OMIT_SHARED_CACHE |
| 6816 | /* |
| 6817 | ** Obtain a lock on the table whose root page is iTab. The |
| 6818 | ** lock is a write lock if isWritelock is true or a read lock |
| 6819 | ** if it is false. |
| 6820 | */ |
danielk1977 | c00da10 | 2006-01-07 13:21:04 +0000 | [diff] [blame] | 6821 | int sqlite3BtreeLockTable(Btree *p, int iTab, u8 isWriteLock){ |
danielk1977 | 2e94d4d | 2006-01-09 05:36:27 +0000 | [diff] [blame] | 6822 | int rc = SQLITE_OK; |
danielk1977 | c00da10 | 2006-01-07 13:21:04 +0000 | [diff] [blame] | 6823 | u8 lockType = (isWriteLock?WRITE_LOCK:READ_LOCK); |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 6824 | sqlite3BtreeEnter(p); |
danielk1977 | 2e94d4d | 2006-01-09 05:36:27 +0000 | [diff] [blame] | 6825 | rc = queryTableLock(p, iTab, lockType); |
danielk1977 | c00da10 | 2006-01-07 13:21:04 +0000 | [diff] [blame] | 6826 | if( rc==SQLITE_OK ){ |
| 6827 | rc = lockTable(p, iTab, lockType); |
| 6828 | } |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 6829 | sqlite3BtreeLeave(p); |
danielk1977 | c00da10 | 2006-01-07 13:21:04 +0000 | [diff] [blame] | 6830 | return rc; |
| 6831 | } |
drh | a154dcd | 2006-03-22 22:10:07 +0000 | [diff] [blame] | 6832 | #endif |
danielk1977 | b82e7ed | 2006-01-11 14:09:31 +0000 | [diff] [blame] | 6833 | |
danielk1977 | b4e9af9 | 2007-05-01 17:49:49 +0000 | [diff] [blame] | 6834 | #ifndef SQLITE_OMIT_INCRBLOB |
| 6835 | /* |
| 6836 | ** Argument pCsr must be a cursor opened for writing on an |
| 6837 | ** INTKEY table currently pointing at a valid table entry. |
| 6838 | ** This function modifies the data stored as part of that entry. |
| 6839 | ** Only the data content may only be modified, it is not possible |
| 6840 | ** to change the length of the data stored. |
| 6841 | */ |
danielk1977 | dcbb5d3 | 2007-05-04 18:36:44 +0000 | [diff] [blame] | 6842 | int sqlite3BtreePutData(BtCursor *pCsr, u32 offset, u32 amt, void *z){ |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 6843 | assert( cursorHoldsMutex(pCsr) ); |
| 6844 | assert( sqlite3_mutex_held(pCsr->pBtree->pSqlite->mutex) ); |
danielk1977 | dcbb5d3 | 2007-05-04 18:36:44 +0000 | [diff] [blame] | 6845 | assert(pCsr->isIncrblobHandle); |
drh | fb98264 | 2007-08-30 01:19:59 +0000 | [diff] [blame] | 6846 | if( pCsr->eState>=CURSOR_REQUIRESEEK ){ |
| 6847 | if( pCsr->eState==CURSOR_FAULT ){ |
| 6848 | return pCsr->skip; |
| 6849 | }else{ |
| 6850 | return SQLITE_ABORT; |
| 6851 | } |
danielk1977 | dcbb5d3 | 2007-05-04 18:36:44 +0000 | [diff] [blame] | 6852 | } |
| 6853 | |
danielk1977 | d0441796 | 2007-05-02 13:16:30 +0000 | [diff] [blame] | 6854 | /* Check some preconditions: |
danielk1977 | dcbb5d3 | 2007-05-04 18:36:44 +0000 | [diff] [blame] | 6855 | ** (a) the cursor is open for writing, |
| 6856 | ** (b) there is no read-lock on the table being modified and |
| 6857 | ** (c) the cursor points at a valid row of an intKey table. |
danielk1977 | d0441796 | 2007-05-02 13:16:30 +0000 | [diff] [blame] | 6858 | */ |
danielk1977 | d0441796 | 2007-05-02 13:16:30 +0000 | [diff] [blame] | 6859 | if( !pCsr->wrFlag ){ |
danielk1977 | dcbb5d3 | 2007-05-04 18:36:44 +0000 | [diff] [blame] | 6860 | return SQLITE_READONLY; |
danielk1977 | d0441796 | 2007-05-02 13:16:30 +0000 | [diff] [blame] | 6861 | } |
drh | d0679ed | 2007-08-28 22:24:34 +0000 | [diff] [blame] | 6862 | assert( !pCsr->pBt->readOnly |
| 6863 | && pCsr->pBt->inTransaction==TRANS_WRITE ); |
danielk1977 | d0441796 | 2007-05-02 13:16:30 +0000 | [diff] [blame] | 6864 | if( checkReadLocks(pCsr->pBtree, pCsr->pgnoRoot, pCsr) ){ |
| 6865 | return SQLITE_LOCKED; /* The table pCur points to has a read lock */ |
| 6866 | } |
| 6867 | if( pCsr->eState==CURSOR_INVALID || !pCsr->pPage->intKey ){ |
| 6868 | return SQLITE_ERROR; |
danielk1977 | b4e9af9 | 2007-05-01 17:49:49 +0000 | [diff] [blame] | 6869 | } |
| 6870 | |
danielk1977 | 9f8d640 | 2007-05-02 17:48:45 +0000 | [diff] [blame] | 6871 | return accessPayload(pCsr, offset, amt, (unsigned char *)z, 0, 1); |
danielk1977 | b4e9af9 | 2007-05-01 17:49:49 +0000 | [diff] [blame] | 6872 | } |
danielk1977 | 2dec970 | 2007-05-02 16:48:37 +0000 | [diff] [blame] | 6873 | |
| 6874 | /* |
| 6875 | ** Set a flag on this cursor to cache the locations of pages from the |
danielk1977 | da10719 | 2007-05-04 08:32:13 +0000 | [diff] [blame] | 6876 | ** overflow list for the current row. This is used by cursors opened |
| 6877 | ** for incremental blob IO only. |
| 6878 | ** |
| 6879 | ** This function sets a flag only. The actual page location cache |
| 6880 | ** (stored in BtCursor.aOverflow[]) is allocated and used by function |
| 6881 | ** accessPayload() (the worker function for sqlite3BtreeData() and |
| 6882 | ** sqlite3BtreePutData()). |
danielk1977 | 2dec970 | 2007-05-02 16:48:37 +0000 | [diff] [blame] | 6883 | */ |
| 6884 | void sqlite3BtreeCacheOverflow(BtCursor *pCur){ |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 6885 | assert( cursorHoldsMutex(pCur) ); |
| 6886 | assert( sqlite3_mutex_held(pCur->pBtree->pSqlite->mutex) ); |
danielk1977 | dcbb5d3 | 2007-05-04 18:36:44 +0000 | [diff] [blame] | 6887 | assert(!pCur->isIncrblobHandle); |
danielk1977 | 2dec970 | 2007-05-02 16:48:37 +0000 | [diff] [blame] | 6888 | assert(!pCur->aOverflow); |
danielk1977 | dcbb5d3 | 2007-05-04 18:36:44 +0000 | [diff] [blame] | 6889 | pCur->isIncrblobHandle = 1; |
danielk1977 | 2dec970 | 2007-05-02 16:48:37 +0000 | [diff] [blame] | 6890 | } |
danielk1977 | b4e9af9 | 2007-05-01 17:49:49 +0000 | [diff] [blame] | 6891 | #endif |