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 | b39f70b | 2007-05-17 18:28:11 +0000 | [diff] [blame^] | 12 | ** $Id: btree.c,v 1.383 2007/05/17 18:28:11 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 | |
| 27 | /* |
drh | a315289 | 2007-05-05 11:48:52 +0000 | [diff] [blame] | 28 | ** Set this global variable to 1 to enable tracing using the TRACE |
| 29 | ** macro. |
drh | 615ae55 | 2005-01-16 23:21:00 +0000 | [diff] [blame] | 30 | */ |
| 31 | #if SQLITE_TEST |
drh | 0f7eb61 | 2006-08-08 13:51:43 +0000 | [diff] [blame] | 32 | int sqlite3_btree_trace=0; /* True to enable tracing */ |
drh | 615ae55 | 2005-01-16 23:21:00 +0000 | [diff] [blame] | 33 | #endif |
drh | 615ae55 | 2005-01-16 23:21:00 +0000 | [diff] [blame] | 34 | |
| 35 | /* |
drh | 66cbd15 | 2004-09-01 16:12:25 +0000 | [diff] [blame] | 36 | ** Forward declaration |
| 37 | */ |
drh | 980b1a7 | 2006-08-16 16:42:48 +0000 | [diff] [blame] | 38 | static int checkReadLocks(Btree*,Pgno,BtCursor*); |
drh | 66cbd15 | 2004-09-01 16:12:25 +0000 | [diff] [blame] | 39 | |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 40 | |
| 41 | #ifdef SQLITE_OMIT_SHARED_CACHE |
| 42 | /* |
| 43 | ** The functions queryTableLock(), lockTable() and unlockAllTables() |
| 44 | ** manipulate entries in the BtShared.pLock linked list used to store |
| 45 | ** shared-cache table level locks. If the library is compiled with the |
| 46 | ** shared-cache feature disabled, then there is only ever one user |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 47 | ** of each BtShared structure and so this locking is not necessary. |
| 48 | ** So define the lock related functions as no-ops. |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 49 | */ |
| 50 | #define queryTableLock(a,b,c) SQLITE_OK |
| 51 | #define lockTable(a,b,c) SQLITE_OK |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 52 | #define unlockAllTables(a) |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 53 | #else |
| 54 | |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 55 | /* |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 56 | ** Query to see if btree handle p may obtain a lock of type eLock |
| 57 | ** (READ_LOCK or WRITE_LOCK) on the table with root-page iTab. Return |
| 58 | ** SQLITE_OK if the lock may be obtained (by calling lockTable()), or |
danielk1977 | c87d34d | 2006-01-06 13:00:28 +0000 | [diff] [blame] | 59 | ** SQLITE_LOCKED if not. |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 60 | */ |
| 61 | static int queryTableLock(Btree *p, Pgno iTab, u8 eLock){ |
| 62 | BtShared *pBt = p->pBt; |
| 63 | BtLock *pIter; |
| 64 | |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 65 | /* This is a no-op if the shared-cache is not enabled */ |
drh | 6f7adc8 | 2006-01-11 21:41:20 +0000 | [diff] [blame] | 66 | if( 0==sqlite3ThreadDataReadOnly()->useSharedData ){ |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 67 | return SQLITE_OK; |
| 68 | } |
| 69 | |
| 70 | /* This (along with lockTable()) is where the ReadUncommitted flag is |
| 71 | ** dealt with. If the caller is querying for a read-lock and the flag is |
| 72 | ** set, it is unconditionally granted - even if there are write-locks |
| 73 | ** on the table. If a write-lock is requested, the ReadUncommitted flag |
| 74 | ** is not considered. |
| 75 | ** |
| 76 | ** In function lockTable(), if a read-lock is demanded and the |
| 77 | ** ReadUncommitted flag is set, no entry is added to the locks list |
| 78 | ** (BtShared.pLock). |
| 79 | ** |
| 80 | ** To summarize: If the ReadUncommitted flag is set, then read cursors do |
| 81 | ** not create or respect table locks. The locking procedure for a |
| 82 | ** write-cursor does not change. |
| 83 | */ |
| 84 | if( |
| 85 | !p->pSqlite || |
| 86 | 0==(p->pSqlite->flags&SQLITE_ReadUncommitted) || |
| 87 | eLock==WRITE_LOCK || |
drh | 47ded16 | 2006-01-06 01:42:58 +0000 | [diff] [blame] | 88 | iTab==MASTER_ROOT |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 89 | ){ |
| 90 | for(pIter=pBt->pLock; pIter; pIter=pIter->pNext){ |
| 91 | if( pIter->pBtree!=p && pIter->iTable==iTab && |
| 92 | (pIter->eLock!=eLock || eLock!=READ_LOCK) ){ |
danielk1977 | c87d34d | 2006-01-06 13:00:28 +0000 | [diff] [blame] | 93 | return SQLITE_LOCKED; |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 94 | } |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 95 | } |
| 96 | } |
| 97 | return SQLITE_OK; |
| 98 | } |
| 99 | |
| 100 | /* |
| 101 | ** Add a lock on the table with root-page iTable to the shared-btree used |
| 102 | ** by Btree handle p. Parameter eLock must be either READ_LOCK or |
| 103 | ** WRITE_LOCK. |
| 104 | ** |
| 105 | ** SQLITE_OK is returned if the lock is added successfully. SQLITE_BUSY and |
| 106 | ** SQLITE_NOMEM may also be returned. |
| 107 | */ |
| 108 | static int lockTable(Btree *p, Pgno iTable, u8 eLock){ |
| 109 | BtShared *pBt = p->pBt; |
| 110 | BtLock *pLock = 0; |
| 111 | BtLock *pIter; |
| 112 | |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 113 | /* This is a no-op if the shared-cache is not enabled */ |
drh | 6f7adc8 | 2006-01-11 21:41:20 +0000 | [diff] [blame] | 114 | if( 0==sqlite3ThreadDataReadOnly()->useSharedData ){ |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 115 | return SQLITE_OK; |
| 116 | } |
| 117 | |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 118 | assert( SQLITE_OK==queryTableLock(p, iTable, eLock) ); |
| 119 | |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 120 | /* If the read-uncommitted flag is set and a read-lock is requested, |
| 121 | ** return early without adding an entry to the BtShared.pLock list. See |
| 122 | ** comment in function queryTableLock() for more info on handling |
| 123 | ** the ReadUncommitted flag. |
| 124 | */ |
| 125 | if( |
| 126 | (p->pSqlite) && |
| 127 | (p->pSqlite->flags&SQLITE_ReadUncommitted) && |
| 128 | (eLock==READ_LOCK) && |
drh | 47ded16 | 2006-01-06 01:42:58 +0000 | [diff] [blame] | 129 | iTable!=MASTER_ROOT |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 130 | ){ |
| 131 | return SQLITE_OK; |
| 132 | } |
| 133 | |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 134 | /* First search the list for an existing lock on this table. */ |
| 135 | for(pIter=pBt->pLock; pIter; pIter=pIter->pNext){ |
| 136 | if( pIter->iTable==iTable && pIter->pBtree==p ){ |
| 137 | pLock = pIter; |
| 138 | break; |
| 139 | } |
| 140 | } |
| 141 | |
| 142 | /* If the above search did not find a BtLock struct associating Btree p |
| 143 | ** with table iTable, allocate one and link it into the list. |
| 144 | */ |
| 145 | if( !pLock ){ |
| 146 | pLock = (BtLock *)sqliteMalloc(sizeof(BtLock)); |
| 147 | if( !pLock ){ |
| 148 | return SQLITE_NOMEM; |
| 149 | } |
| 150 | pLock->iTable = iTable; |
| 151 | pLock->pBtree = p; |
| 152 | pLock->pNext = pBt->pLock; |
| 153 | pBt->pLock = pLock; |
| 154 | } |
| 155 | |
| 156 | /* Set the BtLock.eLock variable to the maximum of the current lock |
| 157 | ** and the requested lock. This means if a write-lock was already held |
| 158 | ** and a read-lock requested, we don't incorrectly downgrade the lock. |
| 159 | */ |
| 160 | assert( WRITE_LOCK>READ_LOCK ); |
danielk1977 | 5118b91 | 2005-12-30 16:31:53 +0000 | [diff] [blame] | 161 | if( eLock>pLock->eLock ){ |
| 162 | pLock->eLock = eLock; |
| 163 | } |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 164 | |
| 165 | return SQLITE_OK; |
| 166 | } |
| 167 | |
| 168 | /* |
| 169 | ** Release all the table locks (locks obtained via calls to the lockTable() |
| 170 | ** procedure) held by Btree handle p. |
| 171 | */ |
| 172 | static void unlockAllTables(Btree *p){ |
| 173 | BtLock **ppIter = &p->pBt->pLock; |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 174 | |
| 175 | /* If the shared-cache extension is not enabled, there should be no |
| 176 | ** locks in the BtShared.pLock list, making this procedure a no-op. Assert |
| 177 | ** that this is the case. |
| 178 | */ |
drh | 6f7adc8 | 2006-01-11 21:41:20 +0000 | [diff] [blame] | 179 | assert( sqlite3ThreadDataReadOnly()->useSharedData || 0==*ppIter ); |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 180 | |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 181 | while( *ppIter ){ |
| 182 | BtLock *pLock = *ppIter; |
| 183 | if( pLock->pBtree==p ){ |
| 184 | *ppIter = pLock->pNext; |
| 185 | sqliteFree(pLock); |
| 186 | }else{ |
| 187 | ppIter = &pLock->pNext; |
| 188 | } |
| 189 | } |
| 190 | } |
| 191 | #endif /* SQLITE_OMIT_SHARED_CACHE */ |
| 192 | |
drh | 980b1a7 | 2006-08-16 16:42:48 +0000 | [diff] [blame] | 193 | static void releasePage(MemPage *pPage); /* Forward reference */ |
| 194 | |
danielk1977 | 92d4d7a | 2007-05-04 12:05:56 +0000 | [diff] [blame] | 195 | #ifndef SQLITE_OMIT_INCRBLOB |
| 196 | /* |
| 197 | ** Invalidate the overflow page-list cache for cursor pCur, if any. |
| 198 | */ |
| 199 | static void invalidateOverflowCache(BtCursor *pCur){ |
| 200 | sqliteFree(pCur->aOverflow); |
| 201 | pCur->aOverflow = 0; |
| 202 | } |
| 203 | |
| 204 | /* |
| 205 | ** Invalidate the overflow page-list cache for all cursors opened |
| 206 | ** on the shared btree structure pBt. |
| 207 | */ |
| 208 | static void invalidateAllOverflowCache(BtShared *pBt){ |
| 209 | BtCursor *p; |
| 210 | for(p=pBt->pCursor; p; p=p->pNext){ |
| 211 | invalidateOverflowCache(p); |
| 212 | } |
| 213 | } |
| 214 | #else |
| 215 | #define invalidateOverflowCache(x) |
| 216 | #define invalidateAllOverflowCache(x) |
| 217 | #endif |
| 218 | |
drh | 980b1a7 | 2006-08-16 16:42:48 +0000 | [diff] [blame] | 219 | /* |
| 220 | ** Save the current cursor position in the variables BtCursor.nKey |
| 221 | ** and BtCursor.pKey. The cursor's state is set to CURSOR_REQUIRESEEK. |
| 222 | */ |
| 223 | static int saveCursorPosition(BtCursor *pCur){ |
| 224 | int rc; |
| 225 | |
| 226 | assert( CURSOR_VALID==pCur->eState ); |
| 227 | assert( 0==pCur->pKey ); |
| 228 | |
| 229 | rc = sqlite3BtreeKeySize(pCur, &pCur->nKey); |
| 230 | |
| 231 | /* If this is an intKey table, then the above call to BtreeKeySize() |
| 232 | ** stores the integer key in pCur->nKey. In this case this value is |
| 233 | ** all that is required. Otherwise, if pCur is not open on an intKey |
| 234 | ** table, then malloc space for and store the pCur->nKey bytes of key |
| 235 | ** data. |
| 236 | */ |
| 237 | if( rc==SQLITE_OK && 0==pCur->pPage->intKey){ |
| 238 | void *pKey = sqliteMalloc(pCur->nKey); |
| 239 | if( pKey ){ |
| 240 | rc = sqlite3BtreeKey(pCur, 0, pCur->nKey, pKey); |
| 241 | if( rc==SQLITE_OK ){ |
| 242 | pCur->pKey = pKey; |
| 243 | }else{ |
| 244 | sqliteFree(pKey); |
| 245 | } |
| 246 | }else{ |
| 247 | rc = SQLITE_NOMEM; |
| 248 | } |
| 249 | } |
| 250 | assert( !pCur->pPage->intKey || !pCur->pKey ); |
| 251 | |
| 252 | if( rc==SQLITE_OK ){ |
| 253 | releasePage(pCur->pPage); |
| 254 | pCur->pPage = 0; |
| 255 | pCur->eState = CURSOR_REQUIRESEEK; |
| 256 | } |
| 257 | |
danielk1977 | 92d4d7a | 2007-05-04 12:05:56 +0000 | [diff] [blame] | 258 | invalidateOverflowCache(pCur); |
drh | 980b1a7 | 2006-08-16 16:42:48 +0000 | [diff] [blame] | 259 | return rc; |
| 260 | } |
| 261 | |
| 262 | /* |
| 263 | ** Save the positions of all cursors except pExcept open on the table |
| 264 | ** with root-page iRoot. Usually, this is called just before cursor |
| 265 | ** pExcept is used to modify the table (BtreeDelete() or BtreeInsert()). |
| 266 | */ |
| 267 | static int saveAllCursors(BtShared *pBt, Pgno iRoot, BtCursor *pExcept){ |
| 268 | BtCursor *p; |
| 269 | for(p=pBt->pCursor; p; p=p->pNext){ |
| 270 | if( p!=pExcept && (0==iRoot || p->pgnoRoot==iRoot) && |
| 271 | p->eState==CURSOR_VALID ){ |
| 272 | int rc = saveCursorPosition(p); |
| 273 | if( SQLITE_OK!=rc ){ |
| 274 | return rc; |
| 275 | } |
| 276 | } |
| 277 | } |
| 278 | return SQLITE_OK; |
| 279 | } |
| 280 | |
| 281 | /* |
drh | bf700f3 | 2007-03-31 02:36:44 +0000 | [diff] [blame] | 282 | ** Clear the current cursor position. |
| 283 | */ |
| 284 | static void clearCursorPosition(BtCursor *pCur){ |
| 285 | sqliteFree(pCur->pKey); |
| 286 | pCur->pKey = 0; |
| 287 | pCur->eState = CURSOR_INVALID; |
| 288 | } |
| 289 | |
| 290 | /* |
drh | 980b1a7 | 2006-08-16 16:42:48 +0000 | [diff] [blame] | 291 | ** Restore the cursor to the position it was in (or as close to as possible) |
| 292 | ** when saveCursorPosition() was called. Note that this call deletes the |
| 293 | ** saved position info stored by saveCursorPosition(), so there can be |
| 294 | ** at most one effective restoreOrClearCursorPosition() call after each |
| 295 | ** saveCursorPosition(). |
| 296 | ** |
| 297 | ** If the second argument argument - doSeek - is false, then instead of |
| 298 | ** returning the cursor to it's saved position, any saved position is deleted |
| 299 | ** and the cursor state set to CURSOR_INVALID. |
| 300 | */ |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 301 | int sqlite3BtreeRestoreOrClearCursorPosition(BtCursor *pCur){ |
drh | bf700f3 | 2007-03-31 02:36:44 +0000 | [diff] [blame] | 302 | int rc; |
drh | 980b1a7 | 2006-08-16 16:42:48 +0000 | [diff] [blame] | 303 | assert( pCur->eState==CURSOR_REQUIRESEEK ); |
danielk1977 | 32a0d8b | 2007-05-04 19:03:02 +0000 | [diff] [blame] | 304 | #ifndef SQLITE_OMIT_INCRBLOB |
danielk1977 | dcbb5d3 | 2007-05-04 18:36:44 +0000 | [diff] [blame] | 305 | if( pCur->isIncrblobHandle ){ |
| 306 | return SQLITE_ABORT; |
| 307 | } |
danielk1977 | 32a0d8b | 2007-05-04 19:03:02 +0000 | [diff] [blame] | 308 | #endif |
drh | 980b1a7 | 2006-08-16 16:42:48 +0000 | [diff] [blame] | 309 | pCur->eState = CURSOR_INVALID; |
drh | bf700f3 | 2007-03-31 02:36:44 +0000 | [diff] [blame] | 310 | rc = sqlite3BtreeMoveto(pCur, pCur->pKey, pCur->nKey, 0, &pCur->skip); |
drh | 980b1a7 | 2006-08-16 16:42:48 +0000 | [diff] [blame] | 311 | if( rc==SQLITE_OK ){ |
| 312 | sqliteFree(pCur->pKey); |
| 313 | pCur->pKey = 0; |
drh | bf700f3 | 2007-03-31 02:36:44 +0000 | [diff] [blame] | 314 | assert( pCur->eState==CURSOR_VALID || pCur->eState==CURSOR_INVALID ); |
drh | 980b1a7 | 2006-08-16 16:42:48 +0000 | [diff] [blame] | 315 | } |
| 316 | return rc; |
| 317 | } |
| 318 | |
drh | bf700f3 | 2007-03-31 02:36:44 +0000 | [diff] [blame] | 319 | #define restoreOrClearCursorPosition(p) \ |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 320 | (p->eState==CURSOR_REQUIRESEEK ? \ |
| 321 | sqlite3BtreeRestoreOrClearCursorPosition(p) : \ |
| 322 | SQLITE_OK) |
drh | 980b1a7 | 2006-08-16 16:42:48 +0000 | [diff] [blame] | 323 | |
danielk1977 | 599fcba | 2004-11-08 07:13:13 +0000 | [diff] [blame] | 324 | #ifndef SQLITE_OMIT_AUTOVACUUM |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 325 | /* |
drh | a315289 | 2007-05-05 11:48:52 +0000 | [diff] [blame] | 326 | ** Given a page number of a regular database page, return the page |
| 327 | ** number for the pointer-map page that contains the entry for the |
| 328 | ** input page number. |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 329 | */ |
danielk1977 | 266664d | 2006-02-10 08:24:21 +0000 | [diff] [blame] | 330 | static Pgno ptrmapPageno(BtShared *pBt, Pgno pgno){ |
| 331 | int nPagesPerMapPage = (pBt->usableSize/5)+1; |
| 332 | int iPtrMap = (pgno-2)/nPagesPerMapPage; |
| 333 | int ret = (iPtrMap*nPagesPerMapPage) + 2; |
| 334 | if( ret==PENDING_BYTE_PAGE(pBt) ){ |
| 335 | ret++; |
| 336 | } |
| 337 | return ret; |
| 338 | } |
danielk1977 | a19df67 | 2004-11-03 11:37:07 +0000 | [diff] [blame] | 339 | |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 340 | /* |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 341 | ** Write an entry into the pointer map. |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 342 | ** |
| 343 | ** This routine updates the pointer map entry for page number 'key' |
| 344 | ** so that it maps to type 'eType' and parent page number 'pgno'. |
| 345 | ** An error code is returned if something goes wrong, otherwise SQLITE_OK. |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 346 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 347 | static int ptrmapPut(BtShared *pBt, Pgno key, u8 eType, Pgno parent){ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 348 | DbPage *pDbPage; /* The pointer map page */ |
| 349 | u8 *pPtrmap; /* The pointer map data */ |
| 350 | Pgno iPtrmap; /* The pointer map page number */ |
| 351 | int offset; /* Offset in pointer map page */ |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 352 | int rc; |
| 353 | |
danielk1977 | 266664d | 2006-02-10 08:24:21 +0000 | [diff] [blame] | 354 | /* The master-journal page number must never be used as a pointer map page */ |
| 355 | assert( 0==PTRMAP_ISPAGE(pBt, PENDING_BYTE_PAGE(pBt)) ); |
| 356 | |
danielk1977 | ac11ee6 | 2005-01-15 12:45:51 +0000 | [diff] [blame] | 357 | assert( pBt->autoVacuum ); |
danielk1977 | fdb7cdb | 2005-01-17 02:12:18 +0000 | [diff] [blame] | 358 | if( key==0 ){ |
drh | 4928570 | 2005-09-17 15:20:26 +0000 | [diff] [blame] | 359 | return SQLITE_CORRUPT_BKPT; |
danielk1977 | fdb7cdb | 2005-01-17 02:12:18 +0000 | [diff] [blame] | 360 | } |
danielk1977 | 266664d | 2006-02-10 08:24:21 +0000 | [diff] [blame] | 361 | iPtrmap = PTRMAP_PAGENO(pBt, key); |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 362 | rc = sqlite3PagerGet(pBt->pPager, iPtrmap, &pDbPage); |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 363 | if( rc!=SQLITE_OK ){ |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 364 | return rc; |
| 365 | } |
danielk1977 | 266664d | 2006-02-10 08:24:21 +0000 | [diff] [blame] | 366 | offset = PTRMAP_PTROFFSET(pBt, key); |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 367 | pPtrmap = (u8 *)sqlite3PagerGetData(pDbPage); |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 368 | |
drh | 615ae55 | 2005-01-16 23:21:00 +0000 | [diff] [blame] | 369 | if( eType!=pPtrmap[offset] || get4byte(&pPtrmap[offset+1])!=parent ){ |
| 370 | TRACE(("PTRMAP_UPDATE: %d->(%d,%d)\n", key, eType, parent)); |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 371 | rc = sqlite3PagerWrite(pDbPage); |
danielk1977 | 5558a8a | 2005-01-17 07:53:44 +0000 | [diff] [blame] | 372 | if( rc==SQLITE_OK ){ |
| 373 | pPtrmap[offset] = eType; |
| 374 | put4byte(&pPtrmap[offset+1], parent); |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 375 | } |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 376 | } |
| 377 | |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 378 | sqlite3PagerUnref(pDbPage); |
danielk1977 | 5558a8a | 2005-01-17 07:53:44 +0000 | [diff] [blame] | 379 | return rc; |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 380 | } |
| 381 | |
| 382 | /* |
| 383 | ** Read an entry from the pointer map. |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 384 | ** |
| 385 | ** This routine retrieves the pointer map entry for page 'key', writing |
| 386 | ** the type and parent page number to *pEType and *pPgno respectively. |
| 387 | ** An error code is returned if something goes wrong, otherwise SQLITE_OK. |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 388 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 389 | static int ptrmapGet(BtShared *pBt, Pgno key, u8 *pEType, Pgno *pPgno){ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 390 | DbPage *pDbPage; /* The pointer map page */ |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 391 | int iPtrmap; /* Pointer map page index */ |
| 392 | u8 *pPtrmap; /* Pointer map page data */ |
| 393 | int offset; /* Offset of entry in pointer map */ |
| 394 | int rc; |
| 395 | |
danielk1977 | 266664d | 2006-02-10 08:24:21 +0000 | [diff] [blame] | 396 | iPtrmap = PTRMAP_PAGENO(pBt, key); |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 397 | rc = sqlite3PagerGet(pBt->pPager, iPtrmap, &pDbPage); |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 398 | if( rc!=0 ){ |
| 399 | return rc; |
| 400 | } |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 401 | pPtrmap = (u8 *)sqlite3PagerGetData(pDbPage); |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 402 | |
danielk1977 | 266664d | 2006-02-10 08:24:21 +0000 | [diff] [blame] | 403 | offset = PTRMAP_PTROFFSET(pBt, key); |
drh | 43617e9 | 2006-03-06 20:55:46 +0000 | [diff] [blame] | 404 | assert( pEType!=0 ); |
| 405 | *pEType = pPtrmap[offset]; |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 406 | if( pPgno ) *pPgno = get4byte(&pPtrmap[offset+1]); |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 407 | |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 408 | sqlite3PagerUnref(pDbPage); |
drh | 4928570 | 2005-09-17 15:20:26 +0000 | [diff] [blame] | 409 | if( *pEType<1 || *pEType>5 ) return SQLITE_CORRUPT_BKPT; |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 410 | return SQLITE_OK; |
| 411 | } |
| 412 | |
| 413 | #endif /* SQLITE_OMIT_AUTOVACUUM */ |
| 414 | |
drh | 0d316a4 | 2002-08-11 20:10:47 +0000 | [diff] [blame] | 415 | /* |
drh | 271efa5 | 2004-05-30 19:19:05 +0000 | [diff] [blame] | 416 | ** Given a btree page and a cell index (0 means the first cell on |
| 417 | ** the page, 1 means the second cell, and so forth) return a pointer |
| 418 | ** to the cell content. |
| 419 | ** |
| 420 | ** This routine works only for pages that do not contain overflow cells. |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 421 | */ |
danielk1977 | 1cc5ed8 | 2007-05-16 17:28:43 +0000 | [diff] [blame] | 422 | #define findCell(pPage, iCell) \ |
| 423 | ((pPage)->aData + get2byte(&(pPage)->aData[(pPage)->cellOffset+2*(iCell)])) |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 424 | u8 *sqlite3BtreeFindCell(MemPage *pPage, int iCell){ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 425 | u8 *data = pPage->aData; |
| 426 | assert( iCell>=0 ); |
| 427 | assert( iCell<get2byte(&data[pPage->hdrOffset+3]) ); |
danielk1977 | 1cc5ed8 | 2007-05-16 17:28:43 +0000 | [diff] [blame] | 428 | return findCell(pPage, iCell); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 429 | } |
| 430 | |
| 431 | /* |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 432 | ** This a more complex version of sqlite3BtreeFindCell() that works for |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 433 | ** pages that do contain overflow cells. See insert |
| 434 | */ |
| 435 | static u8 *findOverflowCell(MemPage *pPage, int iCell){ |
| 436 | int i; |
| 437 | for(i=pPage->nOverflow-1; i>=0; i--){ |
drh | 6d08b4d | 2004-07-20 12:45:22 +0000 | [diff] [blame] | 438 | int k; |
| 439 | struct _OvflCell *pOvfl; |
| 440 | pOvfl = &pPage->aOvfl[i]; |
| 441 | k = pOvfl->idx; |
| 442 | if( k<=iCell ){ |
| 443 | if( k==iCell ){ |
| 444 | return pOvfl->pCell; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 445 | } |
| 446 | iCell--; |
| 447 | } |
| 448 | } |
danielk1977 | 1cc5ed8 | 2007-05-16 17:28:43 +0000 | [diff] [blame] | 449 | return findCell(pPage, iCell); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 450 | } |
| 451 | |
| 452 | /* |
| 453 | ** Parse a cell content block and fill in the CellInfo structure. There |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 454 | ** are two versions of this function. sqlite3BtreeParseCell() takes a |
| 455 | ** cell index as the second argument and sqlite3BtreeParseCellPtr() |
| 456 | ** takes a pointer to the body of the cell as its second argument. |
danielk1977 | 1cc5ed8 | 2007-05-16 17:28:43 +0000 | [diff] [blame] | 457 | ** |
| 458 | ** Within this file, the parseCell() macro can be called instead of |
| 459 | ** sqlite3BtreeParseCellPtr(). Using some compilers, this will be faster. |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 460 | */ |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 461 | void sqlite3BtreeParseCellPtr( |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 462 | MemPage *pPage, /* Page containing the cell */ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 463 | u8 *pCell, /* Pointer to the cell text. */ |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 464 | CellInfo *pInfo /* Fill in this structure */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 465 | ){ |
drh | 271efa5 | 2004-05-30 19:19:05 +0000 | [diff] [blame] | 466 | int n; /* Number bytes in cell content header */ |
| 467 | u32 nPayload; /* Number of bytes of cell payload */ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 468 | |
| 469 | pInfo->pCell = pCell; |
drh | ab01f61 | 2004-05-22 02:55:23 +0000 | [diff] [blame] | 470 | assert( pPage->leaf==0 || pPage->leaf==1 ); |
drh | 271efa5 | 2004-05-30 19:19:05 +0000 | [diff] [blame] | 471 | n = pPage->childPtrSize; |
| 472 | assert( n==4-4*pPage->leaf ); |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 473 | if( pPage->hasData ){ |
drh | 271efa5 | 2004-05-30 19:19:05 +0000 | [diff] [blame] | 474 | n += getVarint32(&pCell[n], &nPayload); |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 475 | }else{ |
drh | 271efa5 | 2004-05-30 19:19:05 +0000 | [diff] [blame] | 476 | nPayload = 0; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 477 | } |
drh | 271efa5 | 2004-05-30 19:19:05 +0000 | [diff] [blame] | 478 | pInfo->nData = nPayload; |
drh | 504b698 | 2006-01-22 21:52:56 +0000 | [diff] [blame] | 479 | if( pPage->intKey ){ |
| 480 | n += getVarint(&pCell[n], (u64 *)&pInfo->nKey); |
| 481 | }else{ |
| 482 | u32 x; |
| 483 | n += getVarint32(&pCell[n], &x); |
| 484 | pInfo->nKey = x; |
| 485 | nPayload += x; |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 486 | } |
drh | 7236583 | 2007-03-06 15:53:44 +0000 | [diff] [blame] | 487 | pInfo->nPayload = nPayload; |
drh | 504b698 | 2006-01-22 21:52:56 +0000 | [diff] [blame] | 488 | pInfo->nHeader = n; |
drh | 271efa5 | 2004-05-30 19:19:05 +0000 | [diff] [blame] | 489 | if( nPayload<=pPage->maxLocal ){ |
| 490 | /* This is the (easy) common case where the entire payload fits |
| 491 | ** on the local page. No overflow is required. |
| 492 | */ |
| 493 | int nSize; /* Total size of cell content in bytes */ |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 494 | pInfo->nLocal = nPayload; |
| 495 | pInfo->iOverflow = 0; |
drh | 271efa5 | 2004-05-30 19:19:05 +0000 | [diff] [blame] | 496 | nSize = nPayload + n; |
| 497 | if( nSize<4 ){ |
| 498 | nSize = 4; /* Minimum cell size is 4 */ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 499 | } |
drh | 271efa5 | 2004-05-30 19:19:05 +0000 | [diff] [blame] | 500 | pInfo->nSize = nSize; |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 501 | }else{ |
drh | 271efa5 | 2004-05-30 19:19:05 +0000 | [diff] [blame] | 502 | /* If the payload will not fit completely on the local page, we have |
| 503 | ** to decide how much to store locally and how much to spill onto |
| 504 | ** overflow pages. The strategy is to minimize the amount of unused |
| 505 | ** space on overflow pages while keeping the amount of local storage |
| 506 | ** in between minLocal and maxLocal. |
| 507 | ** |
| 508 | ** Warning: changing the way overflow payload is distributed in any |
| 509 | ** way will result in an incompatible file format. |
| 510 | */ |
| 511 | int minLocal; /* Minimum amount of payload held locally */ |
| 512 | int maxLocal; /* Maximum amount of payload held locally */ |
| 513 | int surplus; /* Overflow payload available for local storage */ |
| 514 | |
| 515 | minLocal = pPage->minLocal; |
| 516 | maxLocal = pPage->maxLocal; |
| 517 | surplus = minLocal + (nPayload - minLocal)%(pPage->pBt->usableSize - 4); |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 518 | if( surplus <= maxLocal ){ |
| 519 | pInfo->nLocal = surplus; |
| 520 | }else{ |
| 521 | pInfo->nLocal = minLocal; |
| 522 | } |
| 523 | pInfo->iOverflow = pInfo->nLocal + n; |
| 524 | pInfo->nSize = pInfo->iOverflow + 4; |
| 525 | } |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 526 | } |
danielk1977 | 1cc5ed8 | 2007-05-16 17:28:43 +0000 | [diff] [blame] | 527 | #define parseCell(pPage, iCell, pInfo) \ |
| 528 | sqlite3BtreeParseCellPtr((pPage), findCell((pPage), (iCell)), (pInfo)) |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 529 | void sqlite3BtreeParseCell( |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 530 | MemPage *pPage, /* Page containing the cell */ |
| 531 | int iCell, /* The cell index. First cell is 0 */ |
| 532 | CellInfo *pInfo /* Fill in this structure */ |
| 533 | ){ |
danielk1977 | 1cc5ed8 | 2007-05-16 17:28:43 +0000 | [diff] [blame] | 534 | parseCell(pPage, iCell, pInfo); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 535 | } |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 536 | |
| 537 | /* |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 538 | ** Compute the total number of bytes that a Cell needs in the cell |
| 539 | ** data area of the btree-page. The return number includes the cell |
| 540 | ** data header and the local payload, but not any overflow page or |
| 541 | ** the space used by the cell pointer. |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 542 | */ |
danielk1977 | bc6ada4 | 2004-06-30 08:20:16 +0000 | [diff] [blame] | 543 | #ifndef NDEBUG |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 544 | static int cellSize(MemPage *pPage, int iCell){ |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 545 | CellInfo info; |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 546 | sqlite3BtreeParseCell(pPage, iCell, &info); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 547 | return info.nSize; |
| 548 | } |
danielk1977 | bc6ada4 | 2004-06-30 08:20:16 +0000 | [diff] [blame] | 549 | #endif |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 550 | static int cellSizePtr(MemPage *pPage, u8 *pCell){ |
| 551 | CellInfo info; |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 552 | sqlite3BtreeParseCellPtr(pPage, pCell, &info); |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 553 | return info.nSize; |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 554 | } |
| 555 | |
danielk1977 | 79a40da | 2005-01-16 08:00:01 +0000 | [diff] [blame] | 556 | #ifndef SQLITE_OMIT_AUTOVACUUM |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 557 | /* |
danielk1977 | 2683665 | 2005-01-17 01:33:13 +0000 | [diff] [blame] | 558 | ** If the cell pCell, part of page pPage contains a pointer |
danielk1977 | 79a40da | 2005-01-16 08:00:01 +0000 | [diff] [blame] | 559 | ** to an overflow page, insert an entry into the pointer-map |
| 560 | ** for the overflow page. |
danielk1977 | ac11ee6 | 2005-01-15 12:45:51 +0000 | [diff] [blame] | 561 | */ |
danielk1977 | 2683665 | 2005-01-17 01:33:13 +0000 | [diff] [blame] | 562 | static int ptrmapPutOvflPtr(MemPage *pPage, u8 *pCell){ |
danielk1977 | 79a40da | 2005-01-16 08:00:01 +0000 | [diff] [blame] | 563 | if( pCell ){ |
| 564 | CellInfo info; |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 565 | sqlite3BtreeParseCellPtr(pPage, pCell, &info); |
drh | 7236583 | 2007-03-06 15:53:44 +0000 | [diff] [blame] | 566 | assert( (info.nData+(pPage->intKey?0:info.nKey))==info.nPayload ); |
danielk1977 | 79a40da | 2005-01-16 08:00:01 +0000 | [diff] [blame] | 567 | if( (info.nData+(pPage->intKey?0:info.nKey))>info.nLocal ){ |
| 568 | Pgno ovfl = get4byte(&pCell[info.iOverflow]); |
| 569 | return ptrmapPut(pPage->pBt, ovfl, PTRMAP_OVERFLOW1, pPage->pgno); |
| 570 | } |
danielk1977 | ac11ee6 | 2005-01-15 12:45:51 +0000 | [diff] [blame] | 571 | } |
danielk1977 | 79a40da | 2005-01-16 08:00:01 +0000 | [diff] [blame] | 572 | return SQLITE_OK; |
danielk1977 | ac11ee6 | 2005-01-15 12:45:51 +0000 | [diff] [blame] | 573 | } |
danielk1977 | 2683665 | 2005-01-17 01:33:13 +0000 | [diff] [blame] | 574 | /* |
| 575 | ** If the cell with index iCell on page pPage contains a pointer |
| 576 | ** to an overflow page, insert an entry into the pointer-map |
| 577 | ** for the overflow page. |
| 578 | */ |
| 579 | static int ptrmapPutOvfl(MemPage *pPage, int iCell){ |
| 580 | u8 *pCell; |
| 581 | pCell = findOverflowCell(pPage, iCell); |
| 582 | return ptrmapPutOvflPtr(pPage, pCell); |
| 583 | } |
danielk1977 | 79a40da | 2005-01-16 08:00:01 +0000 | [diff] [blame] | 584 | #endif |
| 585 | |
danielk1977 | ac11ee6 | 2005-01-15 12:45:51 +0000 | [diff] [blame] | 586 | |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 587 | /* |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 588 | ** Defragment the page given. All Cells are moved to the |
drh | 3a4a2d4 | 2005-11-24 14:24:28 +0000 | [diff] [blame] | 589 | ** end of the page and all free space is collected into one |
| 590 | ** big FreeBlk that occurs in between the header and cell |
drh | 31beae9 | 2005-11-24 14:34:36 +0000 | [diff] [blame] | 591 | ** pointer array and the cell content area. |
drh | 365d68f | 2001-05-11 11:02:46 +0000 | [diff] [blame] | 592 | */ |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 593 | static int defragmentPage(MemPage *pPage){ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 594 | int i; /* Loop counter */ |
| 595 | int pc; /* Address of a i-th cell */ |
| 596 | int addr; /* Offset of first byte after cell pointer array */ |
| 597 | int hdr; /* Offset to the page header */ |
| 598 | int size; /* Size of a cell */ |
| 599 | int usableSize; /* Number of usable bytes on a page */ |
| 600 | int cellOffset; /* Offset to the cell pointer array */ |
| 601 | int brk; /* Offset to the cell content area */ |
| 602 | int nCell; /* Number of cells on the page */ |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 603 | unsigned char *data; /* The page data */ |
| 604 | unsigned char *temp; /* Temp area for cell content */ |
drh | 2af926b | 2001-05-15 00:39:25 +0000 | [diff] [blame] | 605 | |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 606 | assert( sqlite3PagerIswriteable(pPage->pDbPage) ); |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 607 | assert( pPage->pBt!=0 ); |
drh | 90f5ecb | 2004-07-22 01:19:35 +0000 | [diff] [blame] | 608 | assert( pPage->pBt->usableSize <= SQLITE_MAX_PAGE_SIZE ); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 609 | assert( pPage->nOverflow==0 ); |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 610 | temp = sqliteMalloc( pPage->pBt->pageSize ); |
| 611 | if( temp==0 ) return SQLITE_NOMEM; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 612 | data = pPage->aData; |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 613 | hdr = pPage->hdrOffset; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 614 | cellOffset = pPage->cellOffset; |
| 615 | nCell = pPage->nCell; |
| 616 | assert( nCell==get2byte(&data[hdr+3]) ); |
| 617 | usableSize = pPage->pBt->usableSize; |
| 618 | brk = get2byte(&data[hdr+5]); |
| 619 | memcpy(&temp[brk], &data[brk], usableSize - brk); |
| 620 | brk = usableSize; |
| 621 | for(i=0; i<nCell; i++){ |
| 622 | u8 *pAddr; /* The i-th cell pointer */ |
| 623 | pAddr = &data[cellOffset + i*2]; |
| 624 | pc = get2byte(pAddr); |
| 625 | assert( pc<pPage->pBt->usableSize ); |
| 626 | size = cellSizePtr(pPage, &temp[pc]); |
| 627 | brk -= size; |
| 628 | memcpy(&data[brk], &temp[pc], size); |
| 629 | put2byte(pAddr, brk); |
drh | 2af926b | 2001-05-15 00:39:25 +0000 | [diff] [blame] | 630 | } |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 631 | assert( brk>=cellOffset+2*nCell ); |
| 632 | put2byte(&data[hdr+5], brk); |
| 633 | data[hdr+1] = 0; |
| 634 | data[hdr+2] = 0; |
| 635 | data[hdr+7] = 0; |
| 636 | addr = cellOffset+2*nCell; |
| 637 | memset(&data[addr], 0, brk-addr); |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 638 | sqliteFree(temp); |
| 639 | return SQLITE_OK; |
drh | 365d68f | 2001-05-11 11:02:46 +0000 | [diff] [blame] | 640 | } |
| 641 | |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 642 | /* |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 643 | ** Allocate nByte bytes of space on a page. |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 644 | ** |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 645 | ** Return the index into pPage->aData[] of the first byte of |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 646 | ** the new allocation. Or return 0 if there is not enough free |
| 647 | ** space on the page to satisfy the allocation request. |
drh | 2af926b | 2001-05-15 00:39:25 +0000 | [diff] [blame] | 648 | ** |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 649 | ** If the page contains nBytes of free space but does not contain |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 650 | ** nBytes of contiguous free space, then this routine automatically |
| 651 | ** calls defragementPage() to consolidate all free space before |
| 652 | ** allocating the new chunk. |
drh | 7e3b0a0 | 2001-04-28 16:52:40 +0000 | [diff] [blame] | 653 | */ |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 654 | static int allocateSpace(MemPage *pPage, int nByte){ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 655 | int addr, pc, hdr; |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 656 | int size; |
drh | 24cd67e | 2004-05-10 16:18:47 +0000 | [diff] [blame] | 657 | int nFrag; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 658 | int top; |
| 659 | int nCell; |
| 660 | int cellOffset; |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 661 | unsigned char *data; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 662 | |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 663 | data = pPage->aData; |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 664 | assert( sqlite3PagerIswriteable(pPage->pDbPage) ); |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 665 | assert( pPage->pBt ); |
| 666 | if( nByte<4 ) nByte = 4; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 667 | if( pPage->nFree<nByte || pPage->nOverflow>0 ) return 0; |
| 668 | pPage->nFree -= nByte; |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 669 | hdr = pPage->hdrOffset; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 670 | |
| 671 | nFrag = data[hdr+7]; |
| 672 | if( nFrag<60 ){ |
| 673 | /* Search the freelist looking for a slot big enough to satisfy the |
| 674 | ** space request. */ |
| 675 | addr = hdr+1; |
| 676 | while( (pc = get2byte(&data[addr]))>0 ){ |
| 677 | size = get2byte(&data[pc+2]); |
| 678 | if( size>=nByte ){ |
| 679 | if( size<nByte+4 ){ |
| 680 | memcpy(&data[addr], &data[pc], 2); |
| 681 | data[hdr+7] = nFrag + size - nByte; |
| 682 | return pc; |
| 683 | }else{ |
| 684 | put2byte(&data[pc+2], size-nByte); |
| 685 | return pc + size - nByte; |
| 686 | } |
| 687 | } |
| 688 | addr = pc; |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 689 | } |
| 690 | } |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 691 | |
| 692 | /* Allocate memory from the gap in between the cell pointer array |
| 693 | ** and the cell content area. |
| 694 | */ |
| 695 | top = get2byte(&data[hdr+5]); |
| 696 | nCell = get2byte(&data[hdr+3]); |
| 697 | cellOffset = pPage->cellOffset; |
| 698 | if( nFrag>=60 || cellOffset + 2*nCell > top - nByte ){ |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 699 | if( defragmentPage(pPage) ) return 0; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 700 | top = get2byte(&data[hdr+5]); |
drh | 2af926b | 2001-05-15 00:39:25 +0000 | [diff] [blame] | 701 | } |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 702 | top -= nByte; |
| 703 | assert( cellOffset + 2*nCell <= top ); |
| 704 | put2byte(&data[hdr+5], top); |
| 705 | return top; |
drh | 7e3b0a0 | 2001-04-28 16:52:40 +0000 | [diff] [blame] | 706 | } |
| 707 | |
| 708 | /* |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 709 | ** Return a section of the pPage->aData to the freelist. |
| 710 | ** The first byte of the new free block is pPage->aDisk[start] |
| 711 | ** and the size of the block is "size" bytes. |
drh | 306dc21 | 2001-05-21 13:45:10 +0000 | [diff] [blame] | 712 | ** |
| 713 | ** Most of the effort here is involved in coalesing adjacent |
| 714 | ** free blocks into a single big free block. |
drh | 7e3b0a0 | 2001-04-28 16:52:40 +0000 | [diff] [blame] | 715 | */ |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 716 | static void freeSpace(MemPage *pPage, int start, int size){ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 717 | int addr, pbegin, hdr; |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 718 | unsigned char *data = pPage->aData; |
drh | 2af926b | 2001-05-15 00:39:25 +0000 | [diff] [blame] | 719 | |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 720 | assert( pPage->pBt!=0 ); |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 721 | assert( sqlite3PagerIswriteable(pPage->pDbPage) ); |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 722 | assert( start>=pPage->hdrOffset+6+(pPage->leaf?0:4) ); |
danielk1977 | bc6ada4 | 2004-06-30 08:20:16 +0000 | [diff] [blame] | 723 | assert( (start + size)<=pPage->pBt->usableSize ); |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 724 | if( size<4 ) size = 4; |
| 725 | |
drh | fcce93f | 2006-02-22 03:08:32 +0000 | [diff] [blame] | 726 | #ifdef SQLITE_SECURE_DELETE |
| 727 | /* Overwrite deleted information with zeros when the SECURE_DELETE |
| 728 | ** option is enabled at compile-time */ |
| 729 | memset(&data[start], 0, size); |
| 730 | #endif |
| 731 | |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 732 | /* Add the space back into the linked list of freeblocks */ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 733 | hdr = pPage->hdrOffset; |
| 734 | addr = hdr + 1; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 735 | while( (pbegin = get2byte(&data[addr]))<start && pbegin>0 ){ |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 736 | assert( pbegin<=pPage->pBt->usableSize-4 ); |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 737 | assert( pbegin>addr ); |
| 738 | addr = pbegin; |
drh | 2af926b | 2001-05-15 00:39:25 +0000 | [diff] [blame] | 739 | } |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 740 | assert( pbegin<=pPage->pBt->usableSize-4 ); |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 741 | assert( pbegin>addr || pbegin==0 ); |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 742 | put2byte(&data[addr], start); |
| 743 | put2byte(&data[start], pbegin); |
| 744 | put2byte(&data[start+2], size); |
drh | 2af926b | 2001-05-15 00:39:25 +0000 | [diff] [blame] | 745 | pPage->nFree += size; |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 746 | |
| 747 | /* Coalesce adjacent free blocks */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 748 | addr = pPage->hdrOffset + 1; |
| 749 | while( (pbegin = get2byte(&data[addr]))>0 ){ |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 750 | int pnext, psize; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 751 | assert( pbegin>addr ); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 752 | assert( pbegin<=pPage->pBt->usableSize-4 ); |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 753 | pnext = get2byte(&data[pbegin]); |
| 754 | psize = get2byte(&data[pbegin+2]); |
| 755 | if( pbegin + psize + 3 >= pnext && pnext>0 ){ |
| 756 | int frag = pnext - (pbegin+psize); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 757 | assert( frag<=data[pPage->hdrOffset+7] ); |
| 758 | data[pPage->hdrOffset+7] -= frag; |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 759 | put2byte(&data[pbegin], get2byte(&data[pnext])); |
| 760 | put2byte(&data[pbegin+2], pnext+get2byte(&data[pnext+2])-pbegin); |
| 761 | }else{ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 762 | addr = pbegin; |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 763 | } |
| 764 | } |
drh | 7e3b0a0 | 2001-04-28 16:52:40 +0000 | [diff] [blame] | 765 | |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 766 | /* If the cell content area begins with a freeblock, remove it. */ |
| 767 | if( data[hdr+1]==data[hdr+5] && data[hdr+2]==data[hdr+6] ){ |
| 768 | int top; |
| 769 | pbegin = get2byte(&data[hdr+1]); |
| 770 | memcpy(&data[hdr+1], &data[pbegin], 2); |
| 771 | top = get2byte(&data[hdr+5]); |
| 772 | put2byte(&data[hdr+5], top + get2byte(&data[pbegin+2])); |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 773 | } |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 774 | } |
| 775 | |
| 776 | /* |
drh | 271efa5 | 2004-05-30 19:19:05 +0000 | [diff] [blame] | 777 | ** Decode the flags byte (the first byte of the header) for a page |
| 778 | ** and initialize fields of the MemPage structure accordingly. |
| 779 | */ |
| 780 | static void decodeFlags(MemPage *pPage, int flagByte){ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 781 | BtShared *pBt; /* A copy of pPage->pBt */ |
drh | 271efa5 | 2004-05-30 19:19:05 +0000 | [diff] [blame] | 782 | |
| 783 | assert( pPage->hdrOffset==(pPage->pgno==1 ? 100 : 0) ); |
| 784 | pPage->intKey = (flagByte & (PTF_INTKEY|PTF_LEAFDATA))!=0; |
| 785 | pPage->zeroData = (flagByte & PTF_ZERODATA)!=0; |
| 786 | pPage->leaf = (flagByte & PTF_LEAF)!=0; |
| 787 | pPage->childPtrSize = 4*(pPage->leaf==0); |
| 788 | pBt = pPage->pBt; |
| 789 | if( flagByte & PTF_LEAFDATA ){ |
| 790 | pPage->leafData = 1; |
| 791 | pPage->maxLocal = pBt->maxLeaf; |
| 792 | pPage->minLocal = pBt->minLeaf; |
| 793 | }else{ |
| 794 | pPage->leafData = 0; |
| 795 | pPage->maxLocal = pBt->maxLocal; |
| 796 | pPage->minLocal = pBt->minLocal; |
| 797 | } |
| 798 | pPage->hasData = !(pPage->zeroData || (!pPage->leaf && pPage->leafData)); |
| 799 | } |
| 800 | |
| 801 | /* |
drh | 7e3b0a0 | 2001-04-28 16:52:40 +0000 | [diff] [blame] | 802 | ** Initialize the auxiliary information for a disk block. |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 803 | ** |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 804 | ** The pParent parameter must be a pointer to the MemPage which |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 805 | ** is the parent of the page being initialized. The root of a |
| 806 | ** BTree has no parent and so for that page, pParent==NULL. |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 807 | ** |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 808 | ** Return SQLITE_OK on success. If we see that the page does |
drh | da47d77 | 2002-12-02 04:25:19 +0000 | [diff] [blame] | 809 | ** not contain a well-formed database page, then return |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 810 | ** SQLITE_CORRUPT. Note that a return of SQLITE_OK does not |
| 811 | ** guarantee that the page is well-formed. It only shows that |
| 812 | ** we failed to detect any corruption. |
drh | 7e3b0a0 | 2001-04-28 16:52:40 +0000 | [diff] [blame] | 813 | */ |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 814 | int sqlite3BtreeInitPage( |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 815 | MemPage *pPage, /* The page to be initialized */ |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 816 | MemPage *pParent /* The parent. Might be NULL */ |
| 817 | ){ |
drh | 271efa5 | 2004-05-30 19:19:05 +0000 | [diff] [blame] | 818 | int pc; /* Address of a freeblock within pPage->aData[] */ |
drh | 271efa5 | 2004-05-30 19:19:05 +0000 | [diff] [blame] | 819 | int hdr; /* Offset to beginning of page header */ |
| 820 | u8 *data; /* Equal to pPage->aData */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 821 | BtShared *pBt; /* The main btree structure */ |
drh | 271efa5 | 2004-05-30 19:19:05 +0000 | [diff] [blame] | 822 | int usableSize; /* Amount of usable space on each page */ |
| 823 | int cellOffset; /* Offset from start of page to first cell pointer */ |
| 824 | int nFree; /* Number of unused bytes on the page */ |
| 825 | int top; /* First byte of the cell content area */ |
drh | 2af926b | 2001-05-15 00:39:25 +0000 | [diff] [blame] | 826 | |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 827 | pBt = pPage->pBt; |
| 828 | assert( pBt!=0 ); |
| 829 | assert( pParent==0 || pParent->pBt==pBt ); |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 830 | assert( pPage->pgno==sqlite3PagerPagenumber(pPage->pDbPage) ); |
drh | 07d183d | 2005-05-01 22:52:42 +0000 | [diff] [blame] | 831 | assert( pPage->aData == &((unsigned char*)pPage)[-pBt->pageSize] ); |
drh | ee696e2 | 2004-08-30 16:52:17 +0000 | [diff] [blame] | 832 | if( pPage->pParent!=pParent && (pPage->pParent!=0 || pPage->isInit) ){ |
| 833 | /* The parent page should never change unless the file is corrupt */ |
drh | 4928570 | 2005-09-17 15:20:26 +0000 | [diff] [blame] | 834 | return SQLITE_CORRUPT_BKPT; |
drh | ee696e2 | 2004-08-30 16:52:17 +0000 | [diff] [blame] | 835 | } |
drh | 10617cd | 2004-05-14 15:27:27 +0000 | [diff] [blame] | 836 | if( pPage->isInit ) return SQLITE_OK; |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 837 | if( pPage->pParent==0 && pParent!=0 ){ |
| 838 | pPage->pParent = pParent; |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 839 | sqlite3PagerRef(pParent->pDbPage); |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 840 | } |
drh | de64713 | 2004-05-07 17:57:49 +0000 | [diff] [blame] | 841 | hdr = pPage->hdrOffset; |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 842 | data = pPage->aData; |
drh | 271efa5 | 2004-05-30 19:19:05 +0000 | [diff] [blame] | 843 | decodeFlags(pPage, data[hdr]); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 844 | pPage->nOverflow = 0; |
drh | c8629a1 | 2004-05-08 20:07:40 +0000 | [diff] [blame] | 845 | pPage->idxShift = 0; |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 846 | usableSize = pBt->usableSize; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 847 | pPage->cellOffset = cellOffset = hdr + 12 - 4*pPage->leaf; |
| 848 | top = get2byte(&data[hdr+5]); |
| 849 | pPage->nCell = get2byte(&data[hdr+3]); |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 850 | if( pPage->nCell>MX_CELL(pBt) ){ |
drh | ee696e2 | 2004-08-30 16:52:17 +0000 | [diff] [blame] | 851 | /* To many cells for a single page. The page must be corrupt */ |
drh | 4928570 | 2005-09-17 15:20:26 +0000 | [diff] [blame] | 852 | return SQLITE_CORRUPT_BKPT; |
drh | ee696e2 | 2004-08-30 16:52:17 +0000 | [diff] [blame] | 853 | } |
| 854 | if( pPage->nCell==0 && pParent!=0 && pParent->pgno!=1 ){ |
| 855 | /* All pages must have at least one cell, except for root pages */ |
drh | 4928570 | 2005-09-17 15:20:26 +0000 | [diff] [blame] | 856 | return SQLITE_CORRUPT_BKPT; |
drh | ee696e2 | 2004-08-30 16:52:17 +0000 | [diff] [blame] | 857 | } |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 858 | |
| 859 | /* Compute the total free space on the page */ |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 860 | pc = get2byte(&data[hdr+1]); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 861 | nFree = data[hdr+7] + top - (cellOffset + 2*pPage->nCell); |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 862 | while( pc>0 ){ |
| 863 | int next, size; |
drh | ee696e2 | 2004-08-30 16:52:17 +0000 | [diff] [blame] | 864 | if( pc>usableSize-4 ){ |
| 865 | /* Free block is off the page */ |
drh | 4928570 | 2005-09-17 15:20:26 +0000 | [diff] [blame] | 866 | return SQLITE_CORRUPT_BKPT; |
drh | ee696e2 | 2004-08-30 16:52:17 +0000 | [diff] [blame] | 867 | } |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 868 | next = get2byte(&data[pc]); |
| 869 | size = get2byte(&data[pc+2]); |
drh | ee696e2 | 2004-08-30 16:52:17 +0000 | [diff] [blame] | 870 | if( next>0 && next<=pc+size+3 ){ |
| 871 | /* Free blocks must be in accending order */ |
drh | 4928570 | 2005-09-17 15:20:26 +0000 | [diff] [blame] | 872 | return SQLITE_CORRUPT_BKPT; |
drh | ee696e2 | 2004-08-30 16:52:17 +0000 | [diff] [blame] | 873 | } |
drh | 3add367 | 2004-05-15 00:29:24 +0000 | [diff] [blame] | 874 | nFree += size; |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 875 | pc = next; |
| 876 | } |
drh | 3add367 | 2004-05-15 00:29:24 +0000 | [diff] [blame] | 877 | pPage->nFree = nFree; |
drh | ee696e2 | 2004-08-30 16:52:17 +0000 | [diff] [blame] | 878 | if( nFree>=usableSize ){ |
| 879 | /* Free space cannot exceed total page size */ |
drh | 4928570 | 2005-09-17 15:20:26 +0000 | [diff] [blame] | 880 | return SQLITE_CORRUPT_BKPT; |
drh | ee696e2 | 2004-08-30 16:52:17 +0000 | [diff] [blame] | 881 | } |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 882 | |
drh | de64713 | 2004-05-07 17:57:49 +0000 | [diff] [blame] | 883 | pPage->isInit = 1; |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 884 | return SQLITE_OK; |
drh | 7e3b0a0 | 2001-04-28 16:52:40 +0000 | [diff] [blame] | 885 | } |
| 886 | |
| 887 | /* |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 888 | ** Set up a raw page so that it looks like a database page holding |
| 889 | ** no entries. |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 890 | */ |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 891 | static void zeroPage(MemPage *pPage, int flags){ |
| 892 | unsigned char *data = pPage->aData; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 893 | BtShared *pBt = pPage->pBt; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 894 | int hdr = pPage->hdrOffset; |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 895 | int first; |
| 896 | |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 897 | assert( sqlite3PagerPagenumber(pPage->pDbPage)==pPage->pgno ); |
drh | 07d183d | 2005-05-01 22:52:42 +0000 | [diff] [blame] | 898 | assert( &data[pBt->pageSize] == (unsigned char*)pPage ); |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 899 | assert( sqlite3PagerIswriteable(pPage->pDbPage) ); |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 900 | memset(&data[hdr], 0, pBt->usableSize - hdr); |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 901 | data[hdr] = flags; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 902 | first = hdr + 8 + 4*((flags&PTF_LEAF)==0); |
| 903 | memset(&data[hdr+1], 0, 4); |
| 904 | data[hdr+7] = 0; |
| 905 | put2byte(&data[hdr+5], pBt->usableSize); |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 906 | pPage->nFree = pBt->usableSize - first; |
drh | 271efa5 | 2004-05-30 19:19:05 +0000 | [diff] [blame] | 907 | decodeFlags(pPage, flags); |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 908 | pPage->hdrOffset = hdr; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 909 | pPage->cellOffset = first; |
| 910 | pPage->nOverflow = 0; |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 911 | pPage->idxShift = 0; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 912 | pPage->nCell = 0; |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 913 | pPage->isInit = 1; |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 914 | } |
| 915 | |
| 916 | /* |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 917 | ** Get a page from the pager. Initialize the MemPage.pBt and |
| 918 | ** MemPage.aData elements if needed. |
drh | 538f570 | 2007-04-13 02:14:30 +0000 | [diff] [blame] | 919 | ** |
| 920 | ** If the noContent flag is set, it means that we do not care about |
| 921 | ** the content of the page at this time. So do not go to the disk |
| 922 | ** to fetch the content. Just fill in the content with zeros for now. |
| 923 | ** If in the future we call sqlite3PagerWrite() on this page, that |
| 924 | ** means we have started to be concerned about content and the disk |
| 925 | ** read should occur at that point. |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 926 | */ |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 927 | int sqlite3BtreeGetPage( |
| 928 | BtShared *pBt, /* The btree */ |
| 929 | Pgno pgno, /* Number of the page to fetch */ |
| 930 | MemPage **ppPage, /* Return the page in this parameter */ |
| 931 | int noContent /* Do not load page content if true */ |
| 932 | ){ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 933 | int rc; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 934 | MemPage *pPage; |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 935 | DbPage *pDbPage; |
| 936 | |
drh | 538f570 | 2007-04-13 02:14:30 +0000 | [diff] [blame] | 937 | rc = sqlite3PagerAcquire(pBt->pPager, pgno, (DbPage**)&pDbPage, noContent); |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 938 | if( rc ) return rc; |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 939 | pPage = (MemPage *)sqlite3PagerGetExtra(pDbPage); |
| 940 | pPage->aData = sqlite3PagerGetData(pDbPage); |
| 941 | pPage->pDbPage = pDbPage; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 942 | pPage->pBt = pBt; |
| 943 | pPage->pgno = pgno; |
drh | de64713 | 2004-05-07 17:57:49 +0000 | [diff] [blame] | 944 | pPage->hdrOffset = pPage->pgno==1 ? 100 : 0; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 945 | *ppPage = pPage; |
| 946 | return SQLITE_OK; |
| 947 | } |
| 948 | |
| 949 | /* |
drh | de64713 | 2004-05-07 17:57:49 +0000 | [diff] [blame] | 950 | ** Get a page from the pager and initialize it. This routine |
| 951 | ** is just a convenience wrapper around separate calls to |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 952 | ** sqlite3BtreeGetPage() and sqlite3BtreeInitPage(). |
drh | de64713 | 2004-05-07 17:57:49 +0000 | [diff] [blame] | 953 | */ |
| 954 | static int getAndInitPage( |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 955 | BtShared *pBt, /* The database file */ |
drh | de64713 | 2004-05-07 17:57:49 +0000 | [diff] [blame] | 956 | Pgno pgno, /* Number of the page to get */ |
| 957 | MemPage **ppPage, /* Write the page pointer here */ |
| 958 | MemPage *pParent /* Parent of the page */ |
| 959 | ){ |
| 960 | int rc; |
drh | ee696e2 | 2004-08-30 16:52:17 +0000 | [diff] [blame] | 961 | if( pgno==0 ){ |
drh | 4928570 | 2005-09-17 15:20:26 +0000 | [diff] [blame] | 962 | return SQLITE_CORRUPT_BKPT; |
drh | ee696e2 | 2004-08-30 16:52:17 +0000 | [diff] [blame] | 963 | } |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 964 | rc = sqlite3BtreeGetPage(pBt, pgno, ppPage, 0); |
drh | 10617cd | 2004-05-14 15:27:27 +0000 | [diff] [blame] | 965 | if( rc==SQLITE_OK && (*ppPage)->isInit==0 ){ |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 966 | rc = sqlite3BtreeInitPage(*ppPage, pParent); |
drh | de64713 | 2004-05-07 17:57:49 +0000 | [diff] [blame] | 967 | } |
| 968 | return rc; |
| 969 | } |
| 970 | |
| 971 | /* |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 972 | ** Release a MemPage. This should be called once for each prior |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 973 | ** call to sqlite3BtreeGetPage. |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 974 | */ |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 975 | static void releasePage(MemPage *pPage){ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 976 | if( pPage ){ |
| 977 | assert( pPage->aData ); |
| 978 | assert( pPage->pBt ); |
drh | 07d183d | 2005-05-01 22:52:42 +0000 | [diff] [blame] | 979 | assert( &pPage->aData[pPage->pBt->pageSize]==(unsigned char*)pPage ); |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 980 | sqlite3PagerUnref(pPage->pDbPage); |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 981 | } |
| 982 | } |
| 983 | |
| 984 | /* |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 985 | ** This routine is called when the reference count for a page |
| 986 | ** reaches zero. We need to unref the pParent pointer when that |
| 987 | ** happens. |
| 988 | */ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 989 | static void pageDestructor(DbPage *pData, int pageSize){ |
drh | 07d183d | 2005-05-01 22:52:42 +0000 | [diff] [blame] | 990 | MemPage *pPage; |
| 991 | assert( (pageSize & 7)==0 ); |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 992 | pPage = (MemPage *)sqlite3PagerGetExtra(pData); |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 993 | if( pPage->pParent ){ |
| 994 | MemPage *pParent = pPage->pParent; |
| 995 | pPage->pParent = 0; |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 996 | releasePage(pParent); |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 997 | } |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 998 | pPage->isInit = 0; |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 999 | } |
| 1000 | |
| 1001 | /* |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 1002 | ** During a rollback, when the pager reloads information into the cache |
| 1003 | ** so that the cache is restored to its original state at the start of |
| 1004 | ** the transaction, for each page restored this routine is called. |
| 1005 | ** |
| 1006 | ** This routine needs to reset the extra data section at the end of the |
| 1007 | ** page to agree with the restored data. |
| 1008 | */ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 1009 | static void pageReinit(DbPage *pData, int pageSize){ |
drh | 07d183d | 2005-05-01 22:52:42 +0000 | [diff] [blame] | 1010 | MemPage *pPage; |
| 1011 | assert( (pageSize & 7)==0 ); |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 1012 | pPage = (MemPage *)sqlite3PagerGetExtra(pData); |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 1013 | if( pPage->isInit ){ |
| 1014 | pPage->isInit = 0; |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 1015 | sqlite3BtreeInitPage(pPage, pPage->pParent); |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 1016 | } |
| 1017 | } |
| 1018 | |
| 1019 | /* |
drh | ad3e010 | 2004-09-03 23:32:18 +0000 | [diff] [blame] | 1020 | ** Open a database file. |
| 1021 | ** |
drh | 382c024 | 2001-10-06 16:33:02 +0000 | [diff] [blame] | 1022 | ** zFilename is the name of the database file. If zFilename is NULL |
drh | 1bee3d7 | 2001-10-15 00:44:35 +0000 | [diff] [blame] | 1023 | ** a new database with a random name is created. This randomly named |
drh | 23e11ca | 2004-05-04 17:27:28 +0000 | [diff] [blame] | 1024 | ** database file will be deleted when sqlite3BtreeClose() is called. |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 1025 | */ |
drh | 23e11ca | 2004-05-04 17:27:28 +0000 | [diff] [blame] | 1026 | int sqlite3BtreeOpen( |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1027 | const char *zFilename, /* Name of the file containing the BTree database */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1028 | sqlite3 *pSqlite, /* Associated database handle */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1029 | Btree **ppBtree, /* Pointer to new Btree object written here */ |
drh | 90f5ecb | 2004-07-22 01:19:35 +0000 | [diff] [blame] | 1030 | int flags /* Options */ |
drh | 6019e16 | 2001-07-02 17:51:45 +0000 | [diff] [blame] | 1031 | ){ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1032 | BtShared *pBt; /* Shared part of btree structure */ |
| 1033 | Btree *p; /* Handle to return */ |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 1034 | int rc = SQLITE_OK; |
drh | 90f5ecb | 2004-07-22 01:19:35 +0000 | [diff] [blame] | 1035 | int nReserve; |
| 1036 | unsigned char zDbHeader[100]; |
drh | 6f7adc8 | 2006-01-11 21:41:20 +0000 | [diff] [blame] | 1037 | #if !defined(SQLITE_OMIT_SHARED_CACHE) && !defined(SQLITE_OMIT_DISKIO) |
| 1038 | const ThreadData *pTsdro; |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 1039 | #endif |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1040 | |
| 1041 | /* Set the variable isMemdb to true for an in-memory database, or |
| 1042 | ** false for a file-based database. This symbol is only required if |
| 1043 | ** either of the shared-data or autovacuum features are compiled |
| 1044 | ** into the library. |
| 1045 | */ |
| 1046 | #if !defined(SQLITE_OMIT_SHARED_CACHE) || !defined(SQLITE_OMIT_AUTOVACUUM) |
| 1047 | #ifdef SQLITE_OMIT_MEMORYDB |
drh | 980b1a7 | 2006-08-16 16:42:48 +0000 | [diff] [blame] | 1048 | const int isMemdb = 0; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1049 | #else |
drh | 980b1a7 | 2006-08-16 16:42:48 +0000 | [diff] [blame] | 1050 | const int isMemdb = zFilename && !strcmp(zFilename, ":memory:"); |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1051 | #endif |
| 1052 | #endif |
| 1053 | |
| 1054 | p = sqliteMalloc(sizeof(Btree)); |
| 1055 | if( !p ){ |
| 1056 | return SQLITE_NOMEM; |
| 1057 | } |
| 1058 | p->inTrans = TRANS_NONE; |
| 1059 | p->pSqlite = pSqlite; |
| 1060 | |
| 1061 | /* Try to find an existing Btree structure opened on zFilename. */ |
drh | 198bf39 | 2006-01-06 21:52:49 +0000 | [diff] [blame] | 1062 | #if !defined(SQLITE_OMIT_SHARED_CACHE) && !defined(SQLITE_OMIT_DISKIO) |
drh | 6f7adc8 | 2006-01-11 21:41:20 +0000 | [diff] [blame] | 1063 | pTsdro = sqlite3ThreadDataReadOnly(); |
| 1064 | if( pTsdro->useSharedData && zFilename && !isMemdb ){ |
drh | 66560ad | 2006-01-06 14:32:19 +0000 | [diff] [blame] | 1065 | char *zFullPathname = sqlite3OsFullPathname(zFilename); |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1066 | if( !zFullPathname ){ |
| 1067 | sqliteFree(p); |
| 1068 | return SQLITE_NOMEM; |
| 1069 | } |
drh | 6f7adc8 | 2006-01-11 21:41:20 +0000 | [diff] [blame] | 1070 | for(pBt=pTsdro->pBtree; pBt; pBt=pBt->pNext){ |
danielk1977 | b82e7ed | 2006-01-11 14:09:31 +0000 | [diff] [blame] | 1071 | assert( pBt->nRef>0 ); |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 1072 | if( 0==strcmp(zFullPathname, sqlite3PagerFilename(pBt->pPager)) ){ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1073 | p->pBt = pBt; |
| 1074 | *ppBtree = p; |
| 1075 | pBt->nRef++; |
| 1076 | sqliteFree(zFullPathname); |
| 1077 | return SQLITE_OK; |
| 1078 | } |
| 1079 | } |
| 1080 | sqliteFree(zFullPathname); |
| 1081 | } |
| 1082 | #endif |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 1083 | |
drh | d62d3d0 | 2003-01-24 12:14:20 +0000 | [diff] [blame] | 1084 | /* |
| 1085 | ** The following asserts make sure that structures used by the btree are |
| 1086 | ** the right size. This is to guard against size changes that result |
| 1087 | ** when compiling on a different architecture. |
| 1088 | */ |
drh | 9b8f447 | 2006-04-04 01:54:55 +0000 | [diff] [blame] | 1089 | assert( sizeof(i64)==8 || sizeof(i64)==4 ); |
| 1090 | assert( sizeof(u64)==8 || sizeof(u64)==4 ); |
drh | d62d3d0 | 2003-01-24 12:14:20 +0000 | [diff] [blame] | 1091 | assert( sizeof(u32)==4 ); |
| 1092 | assert( sizeof(u16)==2 ); |
| 1093 | assert( sizeof(Pgno)==4 ); |
drh | d62d3d0 | 2003-01-24 12:14:20 +0000 | [diff] [blame] | 1094 | |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 1095 | pBt = sqliteMalloc( sizeof(*pBt) ); |
| 1096 | if( pBt==0 ){ |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 1097 | rc = SQLITE_NOMEM; |
| 1098 | goto btree_open_out; |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 1099 | } |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 1100 | rc = sqlite3PagerOpen(&pBt->pPager, zFilename, EXTRA_SIZE, flags); |
drh | 551b773 | 2006-11-06 21:20:25 +0000 | [diff] [blame] | 1101 | if( rc==SQLITE_OK ){ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 1102 | rc = sqlite3PagerReadFileheader(pBt->pPager,sizeof(zDbHeader),zDbHeader); |
drh | 551b773 | 2006-11-06 21:20:25 +0000 | [diff] [blame] | 1103 | } |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 1104 | if( rc!=SQLITE_OK ){ |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 1105 | goto btree_open_out; |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 1106 | } |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1107 | p->pBt = pBt; |
| 1108 | |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 1109 | sqlite3PagerSetDestructor(pBt->pPager, pageDestructor); |
| 1110 | sqlite3PagerSetReiniter(pBt->pPager, pageReinit); |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 1111 | pBt->pCursor = 0; |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 1112 | pBt->pPage1 = 0; |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 1113 | pBt->readOnly = sqlite3PagerIsreadonly(pBt->pPager); |
drh | 90f5ecb | 2004-07-22 01:19:35 +0000 | [diff] [blame] | 1114 | pBt->pageSize = get2byte(&zDbHeader[16]); |
drh | 07d183d | 2005-05-01 22:52:42 +0000 | [diff] [blame] | 1115 | if( pBt->pageSize<512 || pBt->pageSize>SQLITE_MAX_PAGE_SIZE |
| 1116 | || ((pBt->pageSize-1)&pBt->pageSize)!=0 ){ |
drh | 90f5ecb | 2004-07-22 01:19:35 +0000 | [diff] [blame] | 1117 | pBt->pageSize = SQLITE_DEFAULT_PAGE_SIZE; |
| 1118 | pBt->maxEmbedFrac = 64; /* 25% */ |
| 1119 | pBt->minEmbedFrac = 32; /* 12.5% */ |
| 1120 | pBt->minLeafFrac = 32; /* 12.5% */ |
drh | eee46cf | 2004-11-06 00:02:48 +0000 | [diff] [blame] | 1121 | #ifndef SQLITE_OMIT_AUTOVACUUM |
danielk1977 | 03aded4 | 2004-11-22 05:26:27 +0000 | [diff] [blame] | 1122 | /* If the magic name ":memory:" will create an in-memory database, then |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 1123 | ** leave the autoVacuum mode at 0 (do not auto-vacuum), even if |
| 1124 | ** SQLITE_DEFAULT_AUTOVACUUM is true. On the other hand, if |
| 1125 | ** SQLITE_OMIT_MEMORYDB has been defined, then ":memory:" is just a |
| 1126 | ** regular file-name. In this case the auto-vacuum applies as per normal. |
danielk1977 | 03aded4 | 2004-11-22 05:26:27 +0000 | [diff] [blame] | 1127 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1128 | if( zFilename && !isMemdb ){ |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 1129 | pBt->autoVacuum = (SQLITE_DEFAULT_AUTOVACUUM ? 1 : 0); |
| 1130 | pBt->incrVacuum = (SQLITE_DEFAULT_AUTOVACUUM==2 ? 1 : 0); |
danielk1977 | 951af80 | 2004-11-05 15:45:09 +0000 | [diff] [blame] | 1131 | } |
drh | eee46cf | 2004-11-06 00:02:48 +0000 | [diff] [blame] | 1132 | #endif |
drh | 90f5ecb | 2004-07-22 01:19:35 +0000 | [diff] [blame] | 1133 | nReserve = 0; |
| 1134 | }else{ |
| 1135 | nReserve = zDbHeader[20]; |
| 1136 | pBt->maxEmbedFrac = zDbHeader[21]; |
| 1137 | pBt->minEmbedFrac = zDbHeader[22]; |
| 1138 | pBt->minLeafFrac = zDbHeader[23]; |
| 1139 | pBt->pageSizeFixed = 1; |
danielk1977 | 951af80 | 2004-11-05 15:45:09 +0000 | [diff] [blame] | 1140 | #ifndef SQLITE_OMIT_AUTOVACUUM |
| 1141 | pBt->autoVacuum = (get4byte(&zDbHeader[36 + 4*4])?1:0); |
| 1142 | #endif |
drh | 90f5ecb | 2004-07-22 01:19:35 +0000 | [diff] [blame] | 1143 | } |
| 1144 | pBt->usableSize = pBt->pageSize - nReserve; |
drh | 07d183d | 2005-05-01 22:52:42 +0000 | [diff] [blame] | 1145 | assert( (pBt->pageSize & 7)==0 ); /* 8-byte alignment of pageSize */ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 1146 | sqlite3PagerSetPagesize(pBt->pPager, pBt->pageSize); |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1147 | |
drh | cfed7bc | 2006-03-13 14:28:05 +0000 | [diff] [blame] | 1148 | #if !defined(SQLITE_OMIT_SHARED_CACHE) && !defined(SQLITE_OMIT_DISKIO) |
danielk1977 | 54f0198 | 2006-01-18 15:25:17 +0000 | [diff] [blame] | 1149 | /* Add the new btree to the linked list starting at ThreadData.pBtree. |
| 1150 | ** There is no chance that a malloc() may fail inside of the |
| 1151 | ** sqlite3ThreadData() call, as the ThreadData structure must have already |
| 1152 | ** been allocated for pTsdro->useSharedData to be non-zero. |
| 1153 | */ |
drh | 6f7adc8 | 2006-01-11 21:41:20 +0000 | [diff] [blame] | 1154 | if( pTsdro->useSharedData && zFilename && !isMemdb ){ |
| 1155 | pBt->pNext = pTsdro->pBtree; |
| 1156 | sqlite3ThreadData()->pBtree = pBt; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1157 | } |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1158 | #endif |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 1159 | pBt->nRef = 1; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1160 | *ppBtree = p; |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 1161 | |
| 1162 | btree_open_out: |
| 1163 | if( rc!=SQLITE_OK ){ |
| 1164 | if( pBt && pBt->pPager ){ |
| 1165 | sqlite3PagerClose(pBt->pPager); |
| 1166 | } |
| 1167 | sqliteFree(pBt); |
| 1168 | sqliteFree(p); |
| 1169 | *ppBtree = 0; |
| 1170 | } |
| 1171 | return rc; |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 1172 | } |
| 1173 | |
| 1174 | /* |
| 1175 | ** Close an open database and invalidate all cursors. |
| 1176 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1177 | int sqlite3BtreeClose(Btree *p){ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1178 | BtShared *pBt = p->pBt; |
| 1179 | BtCursor *pCur; |
| 1180 | |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 1181 | #ifndef SQLITE_OMIT_SHARED_CACHE |
drh | 6f7adc8 | 2006-01-11 21:41:20 +0000 | [diff] [blame] | 1182 | ThreadData *pTsd; |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 1183 | #endif |
| 1184 | |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1185 | /* Close all cursors opened via this handle. */ |
| 1186 | pCur = pBt->pCursor; |
| 1187 | while( pCur ){ |
| 1188 | BtCursor *pTmp = pCur; |
| 1189 | pCur = pCur->pNext; |
| 1190 | if( pTmp->pBtree==p ){ |
| 1191 | sqlite3BtreeCloseCursor(pTmp); |
| 1192 | } |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 1193 | } |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1194 | |
danielk1977 | 8d34dfd | 2006-01-24 16:37:57 +0000 | [diff] [blame] | 1195 | /* Rollback any active transaction and free the handle structure. |
| 1196 | ** The call to sqlite3BtreeRollback() drops any table-locks held by |
| 1197 | ** this handle. |
| 1198 | */ |
danielk1977 | b597f74 | 2006-01-15 11:39:18 +0000 | [diff] [blame] | 1199 | sqlite3BtreeRollback(p); |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1200 | sqliteFree(p); |
| 1201 | |
| 1202 | #ifndef SQLITE_OMIT_SHARED_CACHE |
| 1203 | /* If there are still other outstanding references to the shared-btree |
| 1204 | ** structure, return now. The remainder of this procedure cleans |
| 1205 | ** up the shared-btree. |
| 1206 | */ |
| 1207 | assert( pBt->nRef>0 ); |
| 1208 | pBt->nRef--; |
| 1209 | if( pBt->nRef ){ |
| 1210 | return SQLITE_OK; |
| 1211 | } |
| 1212 | |
danielk1977 | 54f0198 | 2006-01-18 15:25:17 +0000 | [diff] [blame] | 1213 | /* Remove the shared-btree from the thread wide list. Call |
| 1214 | ** ThreadDataReadOnly() and then cast away the const property of the |
| 1215 | ** pointer to avoid allocating thread data if it is not really required. |
| 1216 | */ |
| 1217 | pTsd = (ThreadData *)sqlite3ThreadDataReadOnly(); |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1218 | if( pTsd->pBtree==pBt ){ |
danielk1977 | 54f0198 | 2006-01-18 15:25:17 +0000 | [diff] [blame] | 1219 | assert( pTsd==sqlite3ThreadData() ); |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1220 | pTsd->pBtree = pBt->pNext; |
| 1221 | }else{ |
| 1222 | BtShared *pPrev; |
drh | 7416170 | 2006-02-24 02:53:49 +0000 | [diff] [blame] | 1223 | for(pPrev=pTsd->pBtree; pPrev && pPrev->pNext!=pBt; pPrev=pPrev->pNext){} |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1224 | if( pPrev ){ |
danielk1977 | 54f0198 | 2006-01-18 15:25:17 +0000 | [diff] [blame] | 1225 | assert( pTsd==sqlite3ThreadData() ); |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1226 | pPrev->pNext = pBt->pNext; |
| 1227 | } |
| 1228 | } |
| 1229 | #endif |
| 1230 | |
| 1231 | /* Close the pager and free the shared-btree structure */ |
| 1232 | assert( !pBt->pCursor ); |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 1233 | sqlite3PagerClose(pBt->pPager); |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 1234 | if( pBt->xFreeSchema && pBt->pSchema ){ |
| 1235 | pBt->xFreeSchema(pBt->pSchema); |
| 1236 | } |
danielk1977 | de0fe3e | 2006-01-06 06:33:12 +0000 | [diff] [blame] | 1237 | sqliteFree(pBt->pSchema); |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 1238 | sqliteFree(pBt); |
| 1239 | return SQLITE_OK; |
| 1240 | } |
| 1241 | |
| 1242 | /* |
drh | 90f5ecb | 2004-07-22 01:19:35 +0000 | [diff] [blame] | 1243 | ** Change the busy handler callback function. |
| 1244 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1245 | int sqlite3BtreeSetBusyHandler(Btree *p, BusyHandler *pHandler){ |
| 1246 | BtShared *pBt = p->pBt; |
drh | b8ef32c | 2005-03-14 02:01:49 +0000 | [diff] [blame] | 1247 | pBt->pBusyHandler = pHandler; |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 1248 | sqlite3PagerSetBusyhandler(pBt->pPager, pHandler); |
drh | 90f5ecb | 2004-07-22 01:19:35 +0000 | [diff] [blame] | 1249 | return SQLITE_OK; |
| 1250 | } |
| 1251 | |
| 1252 | /* |
drh | da47d77 | 2002-12-02 04:25:19 +0000 | [diff] [blame] | 1253 | ** Change the limit on the number of pages allowed in the cache. |
drh | cd61c28 | 2002-03-06 22:01:34 +0000 | [diff] [blame] | 1254 | ** |
| 1255 | ** The maximum number of cache pages is set to the absolute |
| 1256 | ** value of mxPage. If mxPage is negative, the pager will |
| 1257 | ** operate asynchronously - it will not stop to do fsync()s |
| 1258 | ** to insure data is written to the disk surface before |
| 1259 | ** continuing. Transactions still work if synchronous is off, |
| 1260 | ** and the database cannot be corrupted if this program |
| 1261 | ** crashes. But if the operating system crashes or there is |
| 1262 | ** an abrupt power failure when synchronous is off, the database |
| 1263 | ** could be left in an inconsistent and unrecoverable state. |
| 1264 | ** Synchronous is on by default so database corruption is not |
| 1265 | ** normally a worry. |
drh | f57b14a | 2001-09-14 18:54:08 +0000 | [diff] [blame] | 1266 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1267 | int sqlite3BtreeSetCacheSize(Btree *p, int mxPage){ |
| 1268 | BtShared *pBt = p->pBt; |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 1269 | sqlite3PagerSetCachesize(pBt->pPager, mxPage); |
drh | f57b14a | 2001-09-14 18:54:08 +0000 | [diff] [blame] | 1270 | return SQLITE_OK; |
| 1271 | } |
| 1272 | |
| 1273 | /* |
drh | 973b6e3 | 2003-02-12 14:09:42 +0000 | [diff] [blame] | 1274 | ** Change the way data is synced to disk in order to increase or decrease |
| 1275 | ** how well the database resists damage due to OS crashes and power |
| 1276 | ** failures. Level 1 is the same as asynchronous (no syncs() occur and |
| 1277 | ** there is a high probability of damage) Level 2 is the default. There |
| 1278 | ** is a very low but non-zero probability of damage. Level 3 reduces the |
| 1279 | ** probability of damage to near zero but with a write performance reduction. |
| 1280 | */ |
danielk1977 | 93758c8 | 2005-01-21 08:13:14 +0000 | [diff] [blame] | 1281 | #ifndef SQLITE_OMIT_PAGER_PRAGMAS |
drh | ac530b1 | 2006-02-11 01:25:50 +0000 | [diff] [blame] | 1282 | int sqlite3BtreeSetSafetyLevel(Btree *p, int level, int fullSync){ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1283 | BtShared *pBt = p->pBt; |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 1284 | sqlite3PagerSetSafetyLevel(pBt->pPager, level, fullSync); |
drh | 973b6e3 | 2003-02-12 14:09:42 +0000 | [diff] [blame] | 1285 | return SQLITE_OK; |
| 1286 | } |
danielk1977 | 93758c8 | 2005-01-21 08:13:14 +0000 | [diff] [blame] | 1287 | #endif |
drh | 973b6e3 | 2003-02-12 14:09:42 +0000 | [diff] [blame] | 1288 | |
drh | 2c8997b | 2005-08-27 16:36:48 +0000 | [diff] [blame] | 1289 | /* |
| 1290 | ** Return TRUE if the given btree is set to safety level 1. In other |
| 1291 | ** words, return TRUE if no sync() occurs on the disk files. |
| 1292 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1293 | int sqlite3BtreeSyncDisabled(Btree *p){ |
| 1294 | BtShared *pBt = p->pBt; |
drh | 2c8997b | 2005-08-27 16:36:48 +0000 | [diff] [blame] | 1295 | assert( pBt && pBt->pPager ); |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 1296 | return sqlite3PagerNosync(pBt->pPager); |
drh | 2c8997b | 2005-08-27 16:36:48 +0000 | [diff] [blame] | 1297 | } |
| 1298 | |
danielk1977 | 576ec6b | 2005-01-21 11:55:25 +0000 | [diff] [blame] | 1299 | #if !defined(SQLITE_OMIT_PAGER_PRAGMAS) || !defined(SQLITE_OMIT_VACUUM) |
drh | 973b6e3 | 2003-02-12 14:09:42 +0000 | [diff] [blame] | 1300 | /* |
drh | 90f5ecb | 2004-07-22 01:19:35 +0000 | [diff] [blame] | 1301 | ** Change the default pages size and the number of reserved bytes per page. |
drh | 06f5021 | 2004-11-02 14:24:33 +0000 | [diff] [blame] | 1302 | ** |
| 1303 | ** The page size must be a power of 2 between 512 and 65536. If the page |
| 1304 | ** size supplied does not meet this constraint then the page size is not |
| 1305 | ** changed. |
| 1306 | ** |
| 1307 | ** Page sizes are constrained to be a power of two so that the region |
| 1308 | ** of the database file used for locking (beginning at PENDING_BYTE, |
| 1309 | ** the first byte past the 1GB boundary, 0x40000000) needs to occur |
| 1310 | ** at the beginning of a page. |
danielk1977 | 2812956 | 2005-01-11 10:25:06 +0000 | [diff] [blame] | 1311 | ** |
| 1312 | ** If parameter nReserve is less than zero, then the number of reserved |
| 1313 | ** bytes per page is left unchanged. |
drh | 90f5ecb | 2004-07-22 01:19:35 +0000 | [diff] [blame] | 1314 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1315 | int sqlite3BtreeSetPageSize(Btree *p, int pageSize, int nReserve){ |
| 1316 | BtShared *pBt = p->pBt; |
drh | 90f5ecb | 2004-07-22 01:19:35 +0000 | [diff] [blame] | 1317 | if( pBt->pageSizeFixed ){ |
| 1318 | return SQLITE_READONLY; |
| 1319 | } |
| 1320 | if( nReserve<0 ){ |
| 1321 | nReserve = pBt->pageSize - pBt->usableSize; |
| 1322 | } |
drh | 06f5021 | 2004-11-02 14:24:33 +0000 | [diff] [blame] | 1323 | if( pageSize>=512 && pageSize<=SQLITE_MAX_PAGE_SIZE && |
| 1324 | ((pageSize-1)&pageSize)==0 ){ |
drh | 07d183d | 2005-05-01 22:52:42 +0000 | [diff] [blame] | 1325 | assert( (pageSize & 7)==0 ); |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1326 | assert( !pBt->pPage1 && !pBt->pCursor ); |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 1327 | pBt->pageSize = sqlite3PagerSetPagesize(pBt->pPager, pageSize); |
drh | 90f5ecb | 2004-07-22 01:19:35 +0000 | [diff] [blame] | 1328 | } |
| 1329 | pBt->usableSize = pBt->pageSize - nReserve; |
| 1330 | return SQLITE_OK; |
| 1331 | } |
| 1332 | |
| 1333 | /* |
| 1334 | ** Return the currently defined page size |
| 1335 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1336 | int sqlite3BtreeGetPageSize(Btree *p){ |
| 1337 | return p->pBt->pageSize; |
drh | 90f5ecb | 2004-07-22 01:19:35 +0000 | [diff] [blame] | 1338 | } |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1339 | int sqlite3BtreeGetReserve(Btree *p){ |
| 1340 | return p->pBt->pageSize - p->pBt->usableSize; |
drh | 2011d5f | 2004-07-22 02:40:37 +0000 | [diff] [blame] | 1341 | } |
drh | f8e632b | 2007-05-08 14:51:36 +0000 | [diff] [blame] | 1342 | |
| 1343 | /* |
| 1344 | ** Set the maximum page count for a database if mxPage is positive. |
| 1345 | ** No changes are made if mxPage is 0 or negative. |
| 1346 | ** Regardless of the value of mxPage, return the maximum page count. |
| 1347 | */ |
| 1348 | int sqlite3BtreeMaxPageCount(Btree *p, int mxPage){ |
| 1349 | return sqlite3PagerMaxPageCount(p->pBt->pPager, mxPage); |
| 1350 | } |
danielk1977 | 576ec6b | 2005-01-21 11:55:25 +0000 | [diff] [blame] | 1351 | #endif /* !defined(SQLITE_OMIT_PAGER_PRAGMAS) || !defined(SQLITE_OMIT_VACUUM) */ |
drh | 90f5ecb | 2004-07-22 01:19:35 +0000 | [diff] [blame] | 1352 | |
| 1353 | /* |
danielk1977 | 951af80 | 2004-11-05 15:45:09 +0000 | [diff] [blame] | 1354 | ** Change the 'auto-vacuum' property of the database. If the 'autoVacuum' |
| 1355 | ** parameter is non-zero, then auto-vacuum mode is enabled. If zero, it |
| 1356 | ** is disabled. The default value for the auto-vacuum property is |
| 1357 | ** determined by the SQLITE_DEFAULT_AUTOVACUUM macro. |
| 1358 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1359 | int sqlite3BtreeSetAutoVacuum(Btree *p, int autoVacuum){ |
danielk1977 | 951af80 | 2004-11-05 15:45:09 +0000 | [diff] [blame] | 1360 | #ifdef SQLITE_OMIT_AUTOVACUUM |
drh | eee46cf | 2004-11-06 00:02:48 +0000 | [diff] [blame] | 1361 | return SQLITE_READONLY; |
danielk1977 | 951af80 | 2004-11-05 15:45:09 +0000 | [diff] [blame] | 1362 | #else |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 1363 | BtShared *pBt = p->pBt; |
| 1364 | int av = (autoVacuum?1:0); |
| 1365 | int iv = (autoVacuum==BTREE_AUTOVACUUM_INCR?1:0); |
| 1366 | if( pBt->pageSizeFixed && av!=pBt->autoVacuum ){ |
danielk1977 | 951af80 | 2004-11-05 15:45:09 +0000 | [diff] [blame] | 1367 | return SQLITE_READONLY; |
| 1368 | } |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 1369 | pBt->autoVacuum = av; |
| 1370 | pBt->incrVacuum = iv; |
danielk1977 | 951af80 | 2004-11-05 15:45:09 +0000 | [diff] [blame] | 1371 | return SQLITE_OK; |
| 1372 | #endif |
| 1373 | } |
| 1374 | |
| 1375 | /* |
| 1376 | ** Return the value of the 'auto-vacuum' property. If auto-vacuum is |
| 1377 | ** enabled 1 is returned. Otherwise 0. |
| 1378 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1379 | int sqlite3BtreeGetAutoVacuum(Btree *p){ |
danielk1977 | 951af80 | 2004-11-05 15:45:09 +0000 | [diff] [blame] | 1380 | #ifdef SQLITE_OMIT_AUTOVACUUM |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 1381 | return BTREE_AUTOVACUUM_NONE; |
danielk1977 | 951af80 | 2004-11-05 15:45:09 +0000 | [diff] [blame] | 1382 | #else |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 1383 | return ( |
| 1384 | (!p->pBt->autoVacuum)?BTREE_AUTOVACUUM_NONE: |
| 1385 | (!p->pBt->incrVacuum)?BTREE_AUTOVACUUM_FULL: |
| 1386 | BTREE_AUTOVACUUM_INCR |
| 1387 | ); |
danielk1977 | 951af80 | 2004-11-05 15:45:09 +0000 | [diff] [blame] | 1388 | #endif |
| 1389 | } |
| 1390 | |
| 1391 | |
| 1392 | /* |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 1393 | ** Get a reference to pPage1 of the database file. This will |
drh | 306dc21 | 2001-05-21 13:45:10 +0000 | [diff] [blame] | 1394 | ** also acquire a readlock on that file. |
| 1395 | ** |
| 1396 | ** SQLITE_OK is returned on success. If the file is not a |
| 1397 | ** well-formed database file, then SQLITE_CORRUPT is returned. |
| 1398 | ** SQLITE_BUSY is returned if the database is locked. SQLITE_NOMEM |
drh | 4f0ee68 | 2007-03-30 20:43:40 +0000 | [diff] [blame] | 1399 | ** is returned if we run out of memory. |
drh | 306dc21 | 2001-05-21 13:45:10 +0000 | [diff] [blame] | 1400 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1401 | static int lockBtree(BtShared *pBt){ |
drh | 07d183d | 2005-05-01 22:52:42 +0000 | [diff] [blame] | 1402 | int rc, pageSize; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1403 | MemPage *pPage1; |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 1404 | if( pBt->pPage1 ) return SQLITE_OK; |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 1405 | rc = sqlite3BtreeGetPage(pBt, 1, &pPage1, 0); |
drh | 306dc21 | 2001-05-21 13:45:10 +0000 | [diff] [blame] | 1406 | if( rc!=SQLITE_OK ) return rc; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1407 | |
drh | 306dc21 | 2001-05-21 13:45:10 +0000 | [diff] [blame] | 1408 | |
| 1409 | /* Do some checking to help insure the file we opened really is |
| 1410 | ** a valid database file. |
| 1411 | */ |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 1412 | rc = SQLITE_NOTADB; |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 1413 | if( sqlite3PagerPagecount(pBt->pPager)>0 ){ |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 1414 | u8 *page1 = pPage1->aData; |
| 1415 | if( memcmp(page1, zMagicHeader, 16)!=0 ){ |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 1416 | goto page1_init_failed; |
drh | 306dc21 | 2001-05-21 13:45:10 +0000 | [diff] [blame] | 1417 | } |
drh | 309169a | 2007-04-24 17:27:51 +0000 | [diff] [blame] | 1418 | if( page1[18]>1 ){ |
| 1419 | pBt->readOnly = 1; |
| 1420 | } |
| 1421 | if( page1[19]>1 ){ |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 1422 | goto page1_init_failed; |
| 1423 | } |
drh | 07d183d | 2005-05-01 22:52:42 +0000 | [diff] [blame] | 1424 | pageSize = get2byte(&page1[16]); |
drh | 1592659 | 2007-04-06 15:02:13 +0000 | [diff] [blame] | 1425 | if( ((pageSize-1)&pageSize)!=0 || pageSize<512 ){ |
drh | 07d183d | 2005-05-01 22:52:42 +0000 | [diff] [blame] | 1426 | goto page1_init_failed; |
| 1427 | } |
| 1428 | assert( (pageSize & 7)==0 ); |
| 1429 | pBt->pageSize = pageSize; |
| 1430 | pBt->usableSize = pageSize - page1[20]; |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 1431 | if( pBt->usableSize<500 ){ |
| 1432 | goto page1_init_failed; |
| 1433 | } |
| 1434 | pBt->maxEmbedFrac = page1[21]; |
| 1435 | pBt->minEmbedFrac = page1[22]; |
| 1436 | pBt->minLeafFrac = page1[23]; |
drh | 057cd3a | 2005-02-15 16:23:02 +0000 | [diff] [blame] | 1437 | #ifndef SQLITE_OMIT_AUTOVACUUM |
| 1438 | pBt->autoVacuum = (get4byte(&page1[36 + 4*4])?1:0); |
| 1439 | #endif |
drh | 306dc21 | 2001-05-21 13:45:10 +0000 | [diff] [blame] | 1440 | } |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 1441 | |
| 1442 | /* maxLocal is the maximum amount of payload to store locally for |
| 1443 | ** a cell. Make sure it is small enough so that at least minFanout |
| 1444 | ** cells can will fit on one page. We assume a 10-byte page header. |
| 1445 | ** Besides the payload, the cell must store: |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 1446 | ** 2-byte pointer to the cell |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 1447 | ** 4-byte child pointer |
| 1448 | ** 9-byte nKey value |
| 1449 | ** 4-byte nData value |
| 1450 | ** 4-byte overflow page pointer |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 1451 | ** So a cell consists of a 2-byte poiner, a header which is as much as |
| 1452 | ** 17 bytes long, 0 to N bytes of payload, and an optional 4 byte overflow |
| 1453 | ** page pointer. |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 1454 | */ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 1455 | pBt->maxLocal = (pBt->usableSize-12)*pBt->maxEmbedFrac/255 - 23; |
| 1456 | pBt->minLocal = (pBt->usableSize-12)*pBt->minEmbedFrac/255 - 23; |
| 1457 | pBt->maxLeaf = pBt->usableSize - 35; |
| 1458 | pBt->minLeaf = (pBt->usableSize-12)*pBt->minLeafFrac/255 - 23; |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 1459 | if( pBt->minLocal>pBt->maxLocal || pBt->maxLocal<0 ){ |
| 1460 | goto page1_init_failed; |
| 1461 | } |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 1462 | assert( pBt->maxLeaf + 23 <= MX_CELL_SIZE(pBt) ); |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1463 | pBt->pPage1 = pPage1; |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 1464 | return SQLITE_OK; |
drh | 306dc21 | 2001-05-21 13:45:10 +0000 | [diff] [blame] | 1465 | |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 1466 | page1_init_failed: |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1467 | releasePage(pPage1); |
| 1468 | pBt->pPage1 = 0; |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 1469 | return rc; |
drh | 306dc21 | 2001-05-21 13:45:10 +0000 | [diff] [blame] | 1470 | } |
| 1471 | |
| 1472 | /* |
drh | b8ef32c | 2005-03-14 02:01:49 +0000 | [diff] [blame] | 1473 | ** This routine works like lockBtree() except that it also invokes the |
| 1474 | ** busy callback if there is lock contention. |
| 1475 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1476 | static int lockBtreeWithRetry(Btree *pRef){ |
drh | b8ef32c | 2005-03-14 02:01:49 +0000 | [diff] [blame] | 1477 | int rc = SQLITE_OK; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1478 | if( pRef->inTrans==TRANS_NONE ){ |
| 1479 | u8 inTransaction = pRef->pBt->inTransaction; |
| 1480 | btreeIntegrity(pRef); |
| 1481 | rc = sqlite3BtreeBeginTrans(pRef, 0); |
| 1482 | pRef->pBt->inTransaction = inTransaction; |
| 1483 | pRef->inTrans = TRANS_NONE; |
| 1484 | if( rc==SQLITE_OK ){ |
| 1485 | pRef->pBt->nTransaction--; |
| 1486 | } |
| 1487 | btreeIntegrity(pRef); |
drh | b8ef32c | 2005-03-14 02:01:49 +0000 | [diff] [blame] | 1488 | } |
| 1489 | return rc; |
| 1490 | } |
| 1491 | |
| 1492 | |
| 1493 | /* |
drh | b8ca307 | 2001-12-05 00:21:20 +0000 | [diff] [blame] | 1494 | ** If there are no outstanding cursors and we are not in the middle |
| 1495 | ** of a transaction but there is a read lock on the database, then |
| 1496 | ** this routine unrefs the first page of the database file which |
| 1497 | ** has the effect of releasing the read lock. |
| 1498 | ** |
| 1499 | ** If there are any outstanding cursors, this routine is a no-op. |
| 1500 | ** |
| 1501 | ** If there is a transaction in progress, this routine is a no-op. |
| 1502 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1503 | static void unlockBtreeIfUnused(BtShared *pBt){ |
| 1504 | if( pBt->inTransaction==TRANS_NONE && pBt->pCursor==0 && pBt->pPage1!=0 ){ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 1505 | if( sqlite3PagerRefcount(pBt->pPager)>=1 ){ |
drh | 24c9a2e | 2007-01-05 02:00:47 +0000 | [diff] [blame] | 1506 | if( pBt->pPage1->aData==0 ){ |
| 1507 | MemPage *pPage = pBt->pPage1; |
| 1508 | pPage->aData = &((u8*)pPage)[-pBt->pageSize]; |
| 1509 | pPage->pBt = pBt; |
| 1510 | pPage->pgno = 1; |
| 1511 | } |
| 1512 | releasePage(pBt->pPage1); |
drh | 51c6d96 | 2004-06-06 00:42:25 +0000 | [diff] [blame] | 1513 | } |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1514 | pBt->pPage1 = 0; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1515 | pBt->inStmt = 0; |
drh | b8ca307 | 2001-12-05 00:21:20 +0000 | [diff] [blame] | 1516 | } |
| 1517 | } |
| 1518 | |
| 1519 | /* |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 1520 | ** Create a new database by initializing the first page of the |
drh | 8c42ca9 | 2001-06-22 19:15:00 +0000 | [diff] [blame] | 1521 | ** file. |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 1522 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1523 | static int newDatabase(BtShared *pBt){ |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 1524 | MemPage *pP1; |
| 1525 | unsigned char *data; |
drh | 8c42ca9 | 2001-06-22 19:15:00 +0000 | [diff] [blame] | 1526 | int rc; |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 1527 | if( sqlite3PagerPagecount(pBt->pPager)>0 ) return SQLITE_OK; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1528 | pP1 = pBt->pPage1; |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 1529 | assert( pP1!=0 ); |
| 1530 | data = pP1->aData; |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 1531 | rc = sqlite3PagerWrite(pP1->pDbPage); |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 1532 | if( rc ) return rc; |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 1533 | memcpy(data, zMagicHeader, sizeof(zMagicHeader)); |
| 1534 | assert( sizeof(zMagicHeader)==16 ); |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 1535 | put2byte(&data[16], pBt->pageSize); |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 1536 | data[18] = 1; |
| 1537 | data[19] = 1; |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 1538 | data[20] = pBt->pageSize - pBt->usableSize; |
| 1539 | data[21] = pBt->maxEmbedFrac; |
| 1540 | data[22] = pBt->minEmbedFrac; |
| 1541 | data[23] = pBt->minLeafFrac; |
| 1542 | memset(&data[24], 0, 100-24); |
drh | e6c4381 | 2004-05-14 12:17:46 +0000 | [diff] [blame] | 1543 | zeroPage(pP1, PTF_INTKEY|PTF_LEAF|PTF_LEAFDATA ); |
drh | f2a611c | 2004-09-05 00:33:43 +0000 | [diff] [blame] | 1544 | pBt->pageSizeFixed = 1; |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 1545 | #ifndef SQLITE_OMIT_AUTOVACUUM |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 1546 | assert( pBt->autoVacuum==1 || pBt->autoVacuum==0 ); |
| 1547 | put4byte(&data[36 + 4*4], pBt->autoVacuum); |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 1548 | #endif |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 1549 | return SQLITE_OK; |
| 1550 | } |
| 1551 | |
| 1552 | /* |
danielk1977 | ee5741e | 2004-05-31 10:01:34 +0000 | [diff] [blame] | 1553 | ** Attempt to start a new transaction. A write-transaction |
drh | 684917c | 2004-10-05 02:41:42 +0000 | [diff] [blame] | 1554 | ** is started if the second argument is nonzero, otherwise a read- |
| 1555 | ** transaction. If the second argument is 2 or more and exclusive |
| 1556 | ** transaction is started, meaning that no other process is allowed |
| 1557 | ** to access the database. A preexisting transaction may not be |
drh | b8ef32c | 2005-03-14 02:01:49 +0000 | [diff] [blame] | 1558 | ** upgraded to exclusive by calling this routine a second time - the |
drh | 684917c | 2004-10-05 02:41:42 +0000 | [diff] [blame] | 1559 | ** exclusivity flag only works for a new transaction. |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 1560 | ** |
danielk1977 | ee5741e | 2004-05-31 10:01:34 +0000 | [diff] [blame] | 1561 | ** A write-transaction must be started before attempting any |
| 1562 | ** changes to the database. None of the following routines |
| 1563 | ** will work unless a transaction is started first: |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 1564 | ** |
drh | 23e11ca | 2004-05-04 17:27:28 +0000 | [diff] [blame] | 1565 | ** sqlite3BtreeCreateTable() |
| 1566 | ** sqlite3BtreeCreateIndex() |
| 1567 | ** sqlite3BtreeClearTable() |
| 1568 | ** sqlite3BtreeDropTable() |
| 1569 | ** sqlite3BtreeInsert() |
| 1570 | ** sqlite3BtreeDelete() |
| 1571 | ** sqlite3BtreeUpdateMeta() |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 1572 | ** |
drh | b8ef32c | 2005-03-14 02:01:49 +0000 | [diff] [blame] | 1573 | ** If an initial attempt to acquire the lock fails because of lock contention |
| 1574 | ** and the database was previously unlocked, then invoke the busy handler |
| 1575 | ** if there is one. But if there was previously a read-lock, do not |
| 1576 | ** invoke the busy handler - just return SQLITE_BUSY. SQLITE_BUSY is |
| 1577 | ** returned when there is already a read-lock in order to avoid a deadlock. |
| 1578 | ** |
| 1579 | ** Suppose there are two processes A and B. A has a read lock and B has |
| 1580 | ** a reserved lock. B tries to promote to exclusive but is blocked because |
| 1581 | ** of A's read lock. A tries to promote to reserved but is blocked by B. |
| 1582 | ** One or the other of the two processes must give way or there can be |
| 1583 | ** no progress. By returning SQLITE_BUSY and not invoking the busy callback |
| 1584 | ** when A already has a read lock, we encourage A to give up and let B |
| 1585 | ** proceed. |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 1586 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1587 | int sqlite3BtreeBeginTrans(Btree *p, int wrflag){ |
| 1588 | BtShared *pBt = p->pBt; |
danielk1977 | ee5741e | 2004-05-31 10:01:34 +0000 | [diff] [blame] | 1589 | int rc = SQLITE_OK; |
| 1590 | |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1591 | btreeIntegrity(p); |
| 1592 | |
danielk1977 | ee5741e | 2004-05-31 10:01:34 +0000 | [diff] [blame] | 1593 | /* If the btree is already in a write-transaction, or it |
| 1594 | ** is already in a read-transaction and a read-transaction |
| 1595 | ** is requested, this is a no-op. |
| 1596 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1597 | if( p->inTrans==TRANS_WRITE || (p->inTrans==TRANS_READ && !wrflag) ){ |
danielk1977 | ee5741e | 2004-05-31 10:01:34 +0000 | [diff] [blame] | 1598 | return SQLITE_OK; |
| 1599 | } |
drh | b8ef32c | 2005-03-14 02:01:49 +0000 | [diff] [blame] | 1600 | |
| 1601 | /* Write transactions are not possible on a read-only database */ |
danielk1977 | ee5741e | 2004-05-31 10:01:34 +0000 | [diff] [blame] | 1602 | if( pBt->readOnly && wrflag ){ |
| 1603 | return SQLITE_READONLY; |
| 1604 | } |
| 1605 | |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1606 | /* If another database handle has already opened a write transaction |
| 1607 | ** on this shared-btree structure and a second write transaction is |
| 1608 | ** requested, return SQLITE_BUSY. |
| 1609 | */ |
| 1610 | if( pBt->inTransaction==TRANS_WRITE && wrflag ){ |
| 1611 | return SQLITE_BUSY; |
| 1612 | } |
| 1613 | |
drh | b8ef32c | 2005-03-14 02:01:49 +0000 | [diff] [blame] | 1614 | do { |
| 1615 | if( pBt->pPage1==0 ){ |
| 1616 | rc = lockBtree(pBt); |
drh | 8c42ca9 | 2001-06-22 19:15:00 +0000 | [diff] [blame] | 1617 | } |
drh | 309169a | 2007-04-24 17:27:51 +0000 | [diff] [blame] | 1618 | |
drh | b8ef32c | 2005-03-14 02:01:49 +0000 | [diff] [blame] | 1619 | if( rc==SQLITE_OK && wrflag ){ |
drh | 309169a | 2007-04-24 17:27:51 +0000 | [diff] [blame] | 1620 | if( pBt->readOnly ){ |
| 1621 | rc = SQLITE_READONLY; |
| 1622 | }else{ |
| 1623 | rc = sqlite3PagerBegin(pBt->pPage1->pDbPage, wrflag>1); |
| 1624 | if( rc==SQLITE_OK ){ |
| 1625 | rc = newDatabase(pBt); |
| 1626 | } |
drh | b8ef32c | 2005-03-14 02:01:49 +0000 | [diff] [blame] | 1627 | } |
| 1628 | } |
| 1629 | |
| 1630 | if( rc==SQLITE_OK ){ |
drh | b8ef32c | 2005-03-14 02:01:49 +0000 | [diff] [blame] | 1631 | if( wrflag ) pBt->inStmt = 0; |
| 1632 | }else{ |
| 1633 | unlockBtreeIfUnused(pBt); |
| 1634 | } |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1635 | }while( rc==SQLITE_BUSY && pBt->inTransaction==TRANS_NONE && |
drh | a4afb65 | 2005-07-09 02:16:02 +0000 | [diff] [blame] | 1636 | sqlite3InvokeBusyHandler(pBt->pBusyHandler) ); |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1637 | |
| 1638 | if( rc==SQLITE_OK ){ |
| 1639 | if( p->inTrans==TRANS_NONE ){ |
| 1640 | pBt->nTransaction++; |
| 1641 | } |
| 1642 | p->inTrans = (wrflag?TRANS_WRITE:TRANS_READ); |
| 1643 | if( p->inTrans>pBt->inTransaction ){ |
| 1644 | pBt->inTransaction = p->inTrans; |
| 1645 | } |
| 1646 | } |
| 1647 | |
| 1648 | btreeIntegrity(p); |
drh | b8ca307 | 2001-12-05 00:21:20 +0000 | [diff] [blame] | 1649 | return rc; |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 1650 | } |
| 1651 | |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 1652 | #ifndef SQLITE_OMIT_AUTOVACUUM |
| 1653 | |
| 1654 | /* |
| 1655 | ** Set the pointer-map entries for all children of page pPage. Also, if |
| 1656 | ** pPage contains cells that point to overflow pages, set the pointer |
| 1657 | ** map entries for the overflow pages as well. |
| 1658 | */ |
| 1659 | static int setChildPtrmaps(MemPage *pPage){ |
| 1660 | int i; /* Counter variable */ |
| 1661 | int nCell; /* Number of cells in page pPage */ |
| 1662 | int rc = SQLITE_OK; /* Return code */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1663 | BtShared *pBt = pPage->pBt; |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 1664 | int isInitOrig = pPage->isInit; |
| 1665 | Pgno pgno = pPage->pgno; |
| 1666 | |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 1667 | sqlite3BtreeInitPage(pPage, 0); |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 1668 | nCell = pPage->nCell; |
| 1669 | |
| 1670 | for(i=0; i<nCell; i++){ |
danielk1977 | 1cc5ed8 | 2007-05-16 17:28:43 +0000 | [diff] [blame] | 1671 | u8 *pCell = findCell(pPage, i); |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 1672 | |
danielk1977 | 2683665 | 2005-01-17 01:33:13 +0000 | [diff] [blame] | 1673 | rc = ptrmapPutOvflPtr(pPage, pCell); |
| 1674 | if( rc!=SQLITE_OK ){ |
| 1675 | goto set_child_ptrmaps_out; |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 1676 | } |
danielk1977 | 2683665 | 2005-01-17 01:33:13 +0000 | [diff] [blame] | 1677 | |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 1678 | if( !pPage->leaf ){ |
| 1679 | Pgno childPgno = get4byte(pCell); |
| 1680 | rc = ptrmapPut(pBt, childPgno, PTRMAP_BTREE, pgno); |
| 1681 | if( rc!=SQLITE_OK ) goto set_child_ptrmaps_out; |
| 1682 | } |
| 1683 | } |
| 1684 | |
| 1685 | if( !pPage->leaf ){ |
| 1686 | Pgno childPgno = get4byte(&pPage->aData[pPage->hdrOffset+8]); |
| 1687 | rc = ptrmapPut(pBt, childPgno, PTRMAP_BTREE, pgno); |
| 1688 | } |
| 1689 | |
| 1690 | set_child_ptrmaps_out: |
| 1691 | pPage->isInit = isInitOrig; |
| 1692 | return rc; |
| 1693 | } |
| 1694 | |
| 1695 | /* |
| 1696 | ** Somewhere on pPage, which is guarenteed to be a btree page, not an overflow |
| 1697 | ** page, is a pointer to page iFrom. Modify this pointer so that it points to |
| 1698 | ** iTo. Parameter eType describes the type of pointer to be modified, as |
| 1699 | ** follows: |
| 1700 | ** |
| 1701 | ** PTRMAP_BTREE: pPage is a btree-page. The pointer points at a child |
| 1702 | ** page of pPage. |
| 1703 | ** |
| 1704 | ** PTRMAP_OVERFLOW1: pPage is a btree-page. The pointer points at an overflow |
| 1705 | ** page pointed to by one of the cells on pPage. |
| 1706 | ** |
| 1707 | ** PTRMAP_OVERFLOW2: pPage is an overflow-page. The pointer points at the next |
| 1708 | ** overflow page in the list. |
| 1709 | */ |
danielk1977 | fdb7cdb | 2005-01-17 02:12:18 +0000 | [diff] [blame] | 1710 | static int modifyPagePointer(MemPage *pPage, Pgno iFrom, Pgno iTo, u8 eType){ |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 1711 | if( eType==PTRMAP_OVERFLOW2 ){ |
danielk1977 | f78fc08 | 2004-11-02 14:40:32 +0000 | [diff] [blame] | 1712 | /* 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] | 1713 | if( get4byte(pPage->aData)!=iFrom ){ |
drh | 4928570 | 2005-09-17 15:20:26 +0000 | [diff] [blame] | 1714 | return SQLITE_CORRUPT_BKPT; |
danielk1977 | fdb7cdb | 2005-01-17 02:12:18 +0000 | [diff] [blame] | 1715 | } |
danielk1977 | f78fc08 | 2004-11-02 14:40:32 +0000 | [diff] [blame] | 1716 | put4byte(pPage->aData, iTo); |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 1717 | }else{ |
| 1718 | int isInitOrig = pPage->isInit; |
| 1719 | int i; |
| 1720 | int nCell; |
| 1721 | |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 1722 | sqlite3BtreeInitPage(pPage, 0); |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 1723 | nCell = pPage->nCell; |
| 1724 | |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 1725 | for(i=0; i<nCell; i++){ |
danielk1977 | 1cc5ed8 | 2007-05-16 17:28:43 +0000 | [diff] [blame] | 1726 | u8 *pCell = findCell(pPage, i); |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 1727 | if( eType==PTRMAP_OVERFLOW1 ){ |
| 1728 | CellInfo info; |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 1729 | sqlite3BtreeParseCellPtr(pPage, pCell, &info); |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 1730 | if( info.iOverflow ){ |
| 1731 | if( iFrom==get4byte(&pCell[info.iOverflow]) ){ |
| 1732 | put4byte(&pCell[info.iOverflow], iTo); |
| 1733 | break; |
| 1734 | } |
| 1735 | } |
| 1736 | }else{ |
| 1737 | if( get4byte(pCell)==iFrom ){ |
| 1738 | put4byte(pCell, iTo); |
| 1739 | break; |
| 1740 | } |
| 1741 | } |
| 1742 | } |
| 1743 | |
| 1744 | if( i==nCell ){ |
danielk1977 | fdb7cdb | 2005-01-17 02:12:18 +0000 | [diff] [blame] | 1745 | if( eType!=PTRMAP_BTREE || |
| 1746 | get4byte(&pPage->aData[pPage->hdrOffset+8])!=iFrom ){ |
drh | 4928570 | 2005-09-17 15:20:26 +0000 | [diff] [blame] | 1747 | return SQLITE_CORRUPT_BKPT; |
danielk1977 | fdb7cdb | 2005-01-17 02:12:18 +0000 | [diff] [blame] | 1748 | } |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 1749 | put4byte(&pPage->aData[pPage->hdrOffset+8], iTo); |
| 1750 | } |
| 1751 | |
| 1752 | pPage->isInit = isInitOrig; |
| 1753 | } |
danielk1977 | fdb7cdb | 2005-01-17 02:12:18 +0000 | [diff] [blame] | 1754 | return SQLITE_OK; |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 1755 | } |
| 1756 | |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 1757 | |
danielk1977 | 7701e81 | 2005-01-10 12:59:51 +0000 | [diff] [blame] | 1758 | /* |
| 1759 | ** Move the open database page pDbPage to location iFreePage in the |
| 1760 | ** database. The pDbPage reference remains valid. |
| 1761 | */ |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 1762 | static int relocatePage( |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1763 | BtShared *pBt, /* Btree */ |
danielk1977 | 7701e81 | 2005-01-10 12:59:51 +0000 | [diff] [blame] | 1764 | MemPage *pDbPage, /* Open page to move */ |
| 1765 | u8 eType, /* Pointer map 'type' entry for pDbPage */ |
| 1766 | Pgno iPtrPage, /* Pointer map 'page-no' entry for pDbPage */ |
| 1767 | Pgno iFreePage /* The location to move pDbPage to */ |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 1768 | ){ |
| 1769 | MemPage *pPtrPage; /* The page that contains a pointer to pDbPage */ |
| 1770 | Pgno iDbPage = pDbPage->pgno; |
| 1771 | Pager *pPager = pBt->pPager; |
| 1772 | int rc; |
| 1773 | |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 1774 | assert( eType==PTRMAP_OVERFLOW2 || eType==PTRMAP_OVERFLOW1 || |
| 1775 | eType==PTRMAP_BTREE || eType==PTRMAP_ROOTPAGE ); |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 1776 | |
| 1777 | /* Move page iDbPage from it's current location to page number iFreePage */ |
| 1778 | TRACE(("AUTOVACUUM: Moving %d to free page %d (ptr page %d type %d)\n", |
| 1779 | iDbPage, iFreePage, iPtrPage, eType)); |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 1780 | rc = sqlite3PagerMovepage(pPager, pDbPage->pDbPage, iFreePage); |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 1781 | if( rc!=SQLITE_OK ){ |
| 1782 | return rc; |
| 1783 | } |
| 1784 | pDbPage->pgno = iFreePage; |
| 1785 | |
| 1786 | /* If pDbPage was a btree-page, then it may have child pages and/or cells |
| 1787 | ** that point to overflow pages. The pointer map entries for all these |
| 1788 | ** pages need to be changed. |
| 1789 | ** |
| 1790 | ** If pDbPage is an overflow page, then the first 4 bytes may store a |
| 1791 | ** pointer to a subsequent overflow page. If this is the case, then |
| 1792 | ** the pointer map needs to be updated for the subsequent overflow page. |
| 1793 | */ |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 1794 | if( eType==PTRMAP_BTREE || eType==PTRMAP_ROOTPAGE ){ |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 1795 | rc = setChildPtrmaps(pDbPage); |
| 1796 | if( rc!=SQLITE_OK ){ |
| 1797 | return rc; |
| 1798 | } |
| 1799 | }else{ |
| 1800 | Pgno nextOvfl = get4byte(pDbPage->aData); |
| 1801 | if( nextOvfl!=0 ){ |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 1802 | rc = ptrmapPut(pBt, nextOvfl, PTRMAP_OVERFLOW2, iFreePage); |
| 1803 | if( rc!=SQLITE_OK ){ |
| 1804 | return rc; |
| 1805 | } |
| 1806 | } |
| 1807 | } |
| 1808 | |
| 1809 | /* Fix the database pointer on page iPtrPage that pointed at iDbPage so |
| 1810 | ** that it points at iFreePage. Also fix the pointer map entry for |
| 1811 | ** iPtrPage. |
| 1812 | */ |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 1813 | if( eType!=PTRMAP_ROOTPAGE ){ |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 1814 | rc = sqlite3BtreeGetPage(pBt, iPtrPage, &pPtrPage, 0); |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 1815 | if( rc!=SQLITE_OK ){ |
| 1816 | return rc; |
| 1817 | } |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 1818 | rc = sqlite3PagerWrite(pPtrPage->pDbPage); |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 1819 | if( rc!=SQLITE_OK ){ |
| 1820 | releasePage(pPtrPage); |
| 1821 | return rc; |
| 1822 | } |
danielk1977 | fdb7cdb | 2005-01-17 02:12:18 +0000 | [diff] [blame] | 1823 | rc = modifyPagePointer(pPtrPage, iDbPage, iFreePage, eType); |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 1824 | releasePage(pPtrPage); |
danielk1977 | fdb7cdb | 2005-01-17 02:12:18 +0000 | [diff] [blame] | 1825 | if( rc==SQLITE_OK ){ |
| 1826 | rc = ptrmapPut(pBt, iFreePage, eType, iPtrPage); |
| 1827 | } |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 1828 | } |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 1829 | return rc; |
| 1830 | } |
| 1831 | |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 1832 | /* Forward declaration required by incrVacuumStep(). */ |
drh | 4f0c587 | 2007-03-26 22:05:01 +0000 | [diff] [blame] | 1833 | static int allocateBtreePage(BtShared *, MemPage **, Pgno *, Pgno, u8); |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 1834 | |
| 1835 | /* |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 1836 | ** Perform a single step of an incremental-vacuum. If successful, |
| 1837 | ** return SQLITE_OK. If there is no work to do (and therefore no |
| 1838 | ** point in calling this function again), return SQLITE_DONE. |
| 1839 | ** |
| 1840 | ** More specificly, this function attempts to re-organize the |
| 1841 | ** database so that the last page of the file currently in use |
| 1842 | ** is no longer in use. |
| 1843 | ** |
| 1844 | ** If the nFin parameter is non-zero, the implementation assumes |
| 1845 | ** that the caller will keep calling incrVacuumStep() until |
| 1846 | ** it returns SQLITE_DONE or an error, and that nFin is the |
| 1847 | ** number of pages the database file will contain after this |
| 1848 | ** process is complete. |
| 1849 | */ |
| 1850 | static int incrVacuumStep(BtShared *pBt, Pgno nFin){ |
| 1851 | Pgno iLastPg; /* Last page in the database */ |
| 1852 | Pgno nFreeList; /* Number of pages still on the free-list */ |
| 1853 | |
| 1854 | iLastPg = pBt->nTrunc; |
| 1855 | if( iLastPg==0 ){ |
| 1856 | iLastPg = sqlite3PagerPagecount(pBt->pPager); |
| 1857 | } |
| 1858 | |
| 1859 | if( !PTRMAP_ISPAGE(pBt, iLastPg) && iLastPg!=PENDING_BYTE_PAGE(pBt) ){ |
| 1860 | int rc; |
| 1861 | u8 eType; |
| 1862 | Pgno iPtrPage; |
| 1863 | |
| 1864 | nFreeList = get4byte(&pBt->pPage1->aData[36]); |
| 1865 | if( nFreeList==0 || nFin==iLastPg ){ |
| 1866 | return SQLITE_DONE; |
| 1867 | } |
| 1868 | |
| 1869 | rc = ptrmapGet(pBt, iLastPg, &eType, &iPtrPage); |
| 1870 | if( rc!=SQLITE_OK ){ |
| 1871 | return rc; |
| 1872 | } |
| 1873 | if( eType==PTRMAP_ROOTPAGE ){ |
| 1874 | return SQLITE_CORRUPT_BKPT; |
| 1875 | } |
| 1876 | |
| 1877 | if( eType==PTRMAP_FREEPAGE ){ |
| 1878 | if( nFin==0 ){ |
| 1879 | /* Remove the page from the files free-list. This is not required |
| 1880 | ** if nFin is non-zero. In this case, the free-list will be |
| 1881 | ** truncated to zero after this function returns, so it doesn't |
| 1882 | ** matter if it still contains some garbage entries. |
| 1883 | */ |
| 1884 | Pgno iFreePg; |
| 1885 | MemPage *pFreePg; |
| 1886 | rc = allocateBtreePage(pBt, &pFreePg, &iFreePg, iLastPg, 1); |
| 1887 | if( rc!=SQLITE_OK ){ |
| 1888 | return rc; |
| 1889 | } |
| 1890 | assert( iFreePg==iLastPg ); |
| 1891 | releasePage(pFreePg); |
| 1892 | } |
| 1893 | } else { |
| 1894 | Pgno iFreePg; /* Index of free page to move pLastPg to */ |
| 1895 | MemPage *pLastPg; |
| 1896 | |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 1897 | rc = sqlite3BtreeGetPage(pBt, iLastPg, &pLastPg, 0); |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 1898 | if( rc!=SQLITE_OK ){ |
| 1899 | return rc; |
| 1900 | } |
| 1901 | |
danielk1977 | b4626a3 | 2007-04-28 15:47:43 +0000 | [diff] [blame] | 1902 | /* If nFin is zero, this loop runs exactly once and page pLastPg |
| 1903 | ** is swapped with the first free page pulled off the free list. |
| 1904 | ** |
| 1905 | ** On the other hand, if nFin is greater than zero, then keep |
| 1906 | ** looping until a free-page located within the first nFin pages |
| 1907 | ** of the file is found. |
| 1908 | */ |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 1909 | do { |
| 1910 | MemPage *pFreePg; |
| 1911 | rc = allocateBtreePage(pBt, &pFreePg, &iFreePg, 0, 0); |
| 1912 | if( rc!=SQLITE_OK ){ |
| 1913 | releasePage(pLastPg); |
| 1914 | return rc; |
| 1915 | } |
| 1916 | releasePage(pFreePg); |
| 1917 | }while( nFin!=0 && iFreePg>nFin ); |
| 1918 | assert( iFreePg<iLastPg ); |
danielk1977 | b4626a3 | 2007-04-28 15:47:43 +0000 | [diff] [blame] | 1919 | |
| 1920 | rc = sqlite3PagerWrite(pLastPg->pDbPage); |
| 1921 | if( rc!=SQLITE_OK ){ |
| 1922 | return rc; |
| 1923 | } |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 1924 | rc = relocatePage(pBt, pLastPg, eType, iPtrPage, iFreePg); |
| 1925 | releasePage(pLastPg); |
| 1926 | if( rc!=SQLITE_OK ){ |
| 1927 | return rc; |
| 1928 | } |
| 1929 | } |
| 1930 | } |
| 1931 | |
| 1932 | pBt->nTrunc = iLastPg - 1; |
| 1933 | while( pBt->nTrunc==PENDING_BYTE_PAGE(pBt)||PTRMAP_ISPAGE(pBt, pBt->nTrunc) ){ |
| 1934 | pBt->nTrunc--; |
| 1935 | } |
| 1936 | return SQLITE_OK; |
| 1937 | } |
| 1938 | |
| 1939 | /* |
| 1940 | ** A write-transaction must be opened before calling this function. |
| 1941 | ** It performs a single unit of work towards an incremental vacuum. |
| 1942 | ** |
| 1943 | ** If the incremental vacuum is finished after this function has run, |
| 1944 | ** SQLITE_DONE is returned. If it is not finished, but no error occured, |
| 1945 | ** SQLITE_OK is returned. Otherwise an SQLite error code. |
| 1946 | */ |
| 1947 | int sqlite3BtreeIncrVacuum(Btree *p){ |
| 1948 | BtShared *pBt = p->pBt; |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 1949 | assert( pBt->inTransaction==TRANS_WRITE && p->inTrans==TRANS_WRITE ); |
| 1950 | if( !pBt->autoVacuum ){ |
| 1951 | return SQLITE_DONE; |
| 1952 | } |
danielk1977 | 92d4d7a | 2007-05-04 12:05:56 +0000 | [diff] [blame] | 1953 | invalidateAllOverflowCache(pBt); |
danielk1977 | 2dec970 | 2007-05-02 16:48:37 +0000 | [diff] [blame] | 1954 | return incrVacuumStep(pBt, 0); |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 1955 | } |
| 1956 | |
| 1957 | /* |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 1958 | ** This routine is called prior to sqlite3PagerCommit when a transaction |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 1959 | ** is commited for an auto-vacuum database. |
danielk1977 | 2416872 | 2007-04-02 05:07:47 +0000 | [diff] [blame] | 1960 | ** |
| 1961 | ** If SQLITE_OK is returned, then *pnTrunc is set to the number of pages |
| 1962 | ** the database file should be truncated to during the commit process. |
| 1963 | ** i.e. the database has been reorganized so that only the first *pnTrunc |
| 1964 | ** pages are in use. |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 1965 | */ |
danielk1977 | 2416872 | 2007-04-02 05:07:47 +0000 | [diff] [blame] | 1966 | static int autoVacuumCommit(BtShared *pBt, Pgno *pnTrunc){ |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 1967 | int rc = SQLITE_OK; |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 1968 | Pager *pPager = pBt->pPager; |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 1969 | #ifndef NDEBUG |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 1970 | int nRef = sqlite3PagerRefcount(pPager); |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 1971 | #endif |
| 1972 | |
danielk1977 | 92d4d7a | 2007-05-04 12:05:56 +0000 | [diff] [blame] | 1973 | invalidateAllOverflowCache(pBt); |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 1974 | assert(pBt->autoVacuum); |
| 1975 | if( !pBt->incrVacuum ){ |
| 1976 | Pgno nFin = 0; |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 1977 | |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 1978 | if( pBt->nTrunc==0 ){ |
| 1979 | Pgno nFree; |
| 1980 | Pgno nPtrmap; |
| 1981 | const int pgsz = pBt->pageSize; |
| 1982 | Pgno nOrig = sqlite3PagerPagecount(pBt->pPager); |
danielk1977 | e5321f0 | 2007-04-27 07:05:44 +0000 | [diff] [blame] | 1983 | |
| 1984 | if( PTRMAP_ISPAGE(pBt, nOrig) ){ |
| 1985 | return SQLITE_CORRUPT_BKPT; |
| 1986 | } |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 1987 | if( nOrig==PENDING_BYTE_PAGE(pBt) ){ |
| 1988 | nOrig--; |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 1989 | } |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 1990 | nFree = get4byte(&pBt->pPage1->aData[36]); |
| 1991 | nPtrmap = (nFree-nOrig+PTRMAP_PAGENO(pBt, nOrig)+pgsz/5)/(pgsz/5); |
| 1992 | nFin = nOrig - nFree - nPtrmap; |
| 1993 | if( nOrig>PENDING_BYTE_PAGE(pBt) && nFin<=PENDING_BYTE_PAGE(pBt) ){ |
| 1994 | nFin--; |
danielk1977 | ac11ee6 | 2005-01-15 12:45:51 +0000 | [diff] [blame] | 1995 | } |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 1996 | while( PTRMAP_ISPAGE(pBt, nFin) || nFin==PENDING_BYTE_PAGE(pBt) ){ |
| 1997 | nFin--; |
| 1998 | } |
| 1999 | } |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 2000 | |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 2001 | while( rc==SQLITE_OK ){ |
| 2002 | rc = incrVacuumStep(pBt, nFin); |
| 2003 | } |
| 2004 | if( rc==SQLITE_DONE ){ |
| 2005 | assert(nFin==0 || pBt->nTrunc==0 || nFin<=pBt->nTrunc); |
| 2006 | rc = SQLITE_OK; |
| 2007 | if( pBt->nTrunc ){ |
| 2008 | sqlite3PagerWrite(pBt->pPage1->pDbPage); |
| 2009 | put4byte(&pBt->pPage1->aData[32], 0); |
| 2010 | put4byte(&pBt->pPage1->aData[36], 0); |
| 2011 | pBt->nTrunc = nFin; |
| 2012 | } |
| 2013 | } |
| 2014 | if( rc!=SQLITE_OK ){ |
| 2015 | sqlite3PagerRollback(pPager); |
| 2016 | } |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 2017 | } |
| 2018 | |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 2019 | if( rc==SQLITE_OK ){ |
| 2020 | *pnTrunc = pBt->nTrunc; |
| 2021 | pBt->nTrunc = 0; |
| 2022 | } |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 2023 | assert( nRef==sqlite3PagerRefcount(pPager) ); |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 2024 | return rc; |
| 2025 | } |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 2026 | |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 2027 | #endif |
| 2028 | |
| 2029 | /* |
drh | 80e35f4 | 2007-03-30 14:06:34 +0000 | [diff] [blame] | 2030 | ** This routine does the first phase of a two-phase commit. This routine |
| 2031 | ** causes a rollback journal to be created (if it does not already exist) |
| 2032 | ** and populated with enough information so that if a power loss occurs |
| 2033 | ** the database can be restored to its original state by playing back |
| 2034 | ** the journal. Then the contents of the journal are flushed out to |
| 2035 | ** the disk. After the journal is safely on oxide, the changes to the |
| 2036 | ** database are written into the database file and flushed to oxide. |
| 2037 | ** At the end of this call, the rollback journal still exists on the |
| 2038 | ** disk and we are still holding all locks, so the transaction has not |
| 2039 | ** committed. See sqlite3BtreeCommit() for the second phase of the |
| 2040 | ** commit process. |
| 2041 | ** |
| 2042 | ** This call is a no-op if no write-transaction is currently active on pBt. |
| 2043 | ** |
| 2044 | ** Otherwise, sync the database file for the btree pBt. zMaster points to |
| 2045 | ** the name of a master journal file that should be written into the |
| 2046 | ** individual journal file, or is NULL, indicating no master journal file |
| 2047 | ** (single database transaction). |
| 2048 | ** |
| 2049 | ** When this is called, the master journal should already have been |
| 2050 | ** created, populated with this journal pointer and synced to disk. |
| 2051 | ** |
| 2052 | ** Once this is routine has returned, the only thing required to commit |
| 2053 | ** the write-transaction for this database file is to delete the journal. |
| 2054 | */ |
| 2055 | int sqlite3BtreeCommitPhaseOne(Btree *p, const char *zMaster){ |
| 2056 | int rc = SQLITE_OK; |
| 2057 | if( p->inTrans==TRANS_WRITE ){ |
| 2058 | BtShared *pBt = p->pBt; |
| 2059 | Pgno nTrunc = 0; |
| 2060 | #ifndef SQLITE_OMIT_AUTOVACUUM |
| 2061 | if( pBt->autoVacuum ){ |
| 2062 | rc = autoVacuumCommit(pBt, &nTrunc); |
| 2063 | if( rc!=SQLITE_OK ){ |
| 2064 | return rc; |
| 2065 | } |
| 2066 | } |
| 2067 | #endif |
| 2068 | rc = sqlite3PagerCommitPhaseOne(pBt->pPager, zMaster, nTrunc); |
| 2069 | } |
| 2070 | return rc; |
| 2071 | } |
| 2072 | |
| 2073 | /* |
drh | 2aa679f | 2001-06-25 02:11:07 +0000 | [diff] [blame] | 2074 | ** Commit the transaction currently in progress. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2075 | ** |
drh | 6e34599 | 2007-03-30 11:12:08 +0000 | [diff] [blame] | 2076 | ** This routine implements the second phase of a 2-phase commit. The |
| 2077 | ** sqlite3BtreeSync() routine does the first phase and should be invoked |
| 2078 | ** prior to calling this routine. The sqlite3BtreeSync() routine did |
| 2079 | ** all the work of writing information out to disk and flushing the |
| 2080 | ** contents so that they are written onto the disk platter. All this |
| 2081 | ** routine has to do is delete or truncate the rollback journal |
| 2082 | ** (which causes the transaction to commit) and drop locks. |
| 2083 | ** |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2084 | ** This will release the write lock on the database file. If there |
| 2085 | ** are no active cursors, it also releases the read lock. |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 2086 | */ |
drh | 80e35f4 | 2007-03-30 14:06:34 +0000 | [diff] [blame] | 2087 | int sqlite3BtreeCommitPhaseTwo(Btree *p){ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2088 | BtShared *pBt = p->pBt; |
| 2089 | |
| 2090 | btreeIntegrity(p); |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2091 | |
| 2092 | /* If the handle has a write-transaction open, commit the shared-btrees |
| 2093 | ** transaction and set the shared state to TRANS_READ. |
| 2094 | */ |
| 2095 | if( p->inTrans==TRANS_WRITE ){ |
danielk1977 | 7f7bc66 | 2006-01-23 13:47:47 +0000 | [diff] [blame] | 2096 | int rc; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2097 | assert( pBt->inTransaction==TRANS_WRITE ); |
| 2098 | assert( pBt->nTransaction>0 ); |
drh | 80e35f4 | 2007-03-30 14:06:34 +0000 | [diff] [blame] | 2099 | rc = sqlite3PagerCommitPhaseTwo(pBt->pPager); |
danielk1977 | 7f7bc66 | 2006-01-23 13:47:47 +0000 | [diff] [blame] | 2100 | if( rc!=SQLITE_OK ){ |
| 2101 | return rc; |
| 2102 | } |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2103 | pBt->inTransaction = TRANS_READ; |
| 2104 | pBt->inStmt = 0; |
danielk1977 | ee5741e | 2004-05-31 10:01:34 +0000 | [diff] [blame] | 2105 | } |
danielk1977 | 7f7bc66 | 2006-01-23 13:47:47 +0000 | [diff] [blame] | 2106 | unlockAllTables(p); |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2107 | |
| 2108 | /* If the handle has any kind of transaction open, decrement the transaction |
| 2109 | ** count of the shared btree. If the transaction count reaches 0, set |
| 2110 | ** the shared state to TRANS_NONE. The unlockBtreeIfUnused() call below |
| 2111 | ** will unlock the pager. |
| 2112 | */ |
| 2113 | if( p->inTrans!=TRANS_NONE ){ |
| 2114 | pBt->nTransaction--; |
| 2115 | if( 0==pBt->nTransaction ){ |
| 2116 | pBt->inTransaction = TRANS_NONE; |
| 2117 | } |
| 2118 | } |
| 2119 | |
| 2120 | /* Set the handles current transaction state to TRANS_NONE and unlock |
| 2121 | ** the pager if this call closed the only read or write transaction. |
| 2122 | */ |
| 2123 | p->inTrans = TRANS_NONE; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2124 | unlockBtreeIfUnused(pBt); |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2125 | |
| 2126 | btreeIntegrity(p); |
danielk1977 | 7f7bc66 | 2006-01-23 13:47:47 +0000 | [diff] [blame] | 2127 | return SQLITE_OK; |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 2128 | } |
| 2129 | |
drh | 80e35f4 | 2007-03-30 14:06:34 +0000 | [diff] [blame] | 2130 | /* |
| 2131 | ** Do both phases of a commit. |
| 2132 | */ |
| 2133 | int sqlite3BtreeCommit(Btree *p){ |
| 2134 | int rc; |
| 2135 | rc = sqlite3BtreeCommitPhaseOne(p, 0); |
| 2136 | if( rc==SQLITE_OK ){ |
| 2137 | rc = sqlite3BtreeCommitPhaseTwo(p); |
| 2138 | } |
| 2139 | return rc; |
| 2140 | } |
| 2141 | |
danielk1977 | fbcd585 | 2004-06-15 02:44:18 +0000 | [diff] [blame] | 2142 | #ifndef NDEBUG |
| 2143 | /* |
| 2144 | ** Return the number of write-cursors open on this handle. This is for use |
| 2145 | ** in assert() expressions, so it is only compiled if NDEBUG is not |
| 2146 | ** defined. |
| 2147 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2148 | static int countWriteCursors(BtShared *pBt){ |
danielk1977 | fbcd585 | 2004-06-15 02:44:18 +0000 | [diff] [blame] | 2149 | BtCursor *pCur; |
| 2150 | int r = 0; |
| 2151 | for(pCur=pBt->pCursor; pCur; pCur=pCur->pNext){ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2152 | if( pCur->wrFlag ) r++; |
danielk1977 | fbcd585 | 2004-06-15 02:44:18 +0000 | [diff] [blame] | 2153 | } |
| 2154 | return r; |
| 2155 | } |
| 2156 | #endif |
| 2157 | |
drh | c39e000 | 2004-05-07 23:50:57 +0000 | [diff] [blame] | 2158 | /* |
drh | ecdc753 | 2001-09-23 02:35:53 +0000 | [diff] [blame] | 2159 | ** Rollback the transaction in progress. All cursors will be |
| 2160 | ** invalided by this operation. Any attempt to use a cursor |
| 2161 | ** that was open at the beginning of this operation will result |
| 2162 | ** in an error. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2163 | ** |
| 2164 | ** This will release the write lock on the database file. If there |
| 2165 | ** are no active cursors, it also releases the read lock. |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 2166 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2167 | int sqlite3BtreeRollback(Btree *p){ |
danielk1977 | 8d34dfd | 2006-01-24 16:37:57 +0000 | [diff] [blame] | 2168 | int rc; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2169 | BtShared *pBt = p->pBt; |
drh | 24cd67e | 2004-05-10 16:18:47 +0000 | [diff] [blame] | 2170 | MemPage *pPage1; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2171 | |
danielk1977 | 2b8c13e | 2006-01-24 14:21:24 +0000 | [diff] [blame] | 2172 | rc = saveAllCursors(pBt, 0, 0); |
danielk1977 | 8d34dfd | 2006-01-24 16:37:57 +0000 | [diff] [blame] | 2173 | #ifndef SQLITE_OMIT_SHARED_CACHE |
danielk1977 | 2b8c13e | 2006-01-24 14:21:24 +0000 | [diff] [blame] | 2174 | if( rc!=SQLITE_OK ){ |
danielk1977 | 8d34dfd | 2006-01-24 16:37:57 +0000 | [diff] [blame] | 2175 | /* This is a horrible situation. An IO or malloc() error occured whilst |
| 2176 | ** trying to save cursor positions. If this is an automatic rollback (as |
| 2177 | ** the result of a constraint, malloc() failure or IO error) then |
| 2178 | ** the cache may be internally inconsistent (not contain valid trees) so |
| 2179 | ** we cannot simply return the error to the caller. Instead, abort |
| 2180 | ** all queries that may be using any of the cursors that failed to save. |
| 2181 | */ |
| 2182 | while( pBt->pCursor ){ |
| 2183 | sqlite3 *db = pBt->pCursor->pBtree->pSqlite; |
| 2184 | if( db ){ |
| 2185 | sqlite3AbortOtherActiveVdbes(db, 0); |
| 2186 | } |
| 2187 | } |
danielk1977 | 2b8c13e | 2006-01-24 14:21:24 +0000 | [diff] [blame] | 2188 | } |
danielk1977 | 8d34dfd | 2006-01-24 16:37:57 +0000 | [diff] [blame] | 2189 | #endif |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2190 | btreeIntegrity(p); |
| 2191 | unlockAllTables(p); |
| 2192 | |
| 2193 | if( p->inTrans==TRANS_WRITE ){ |
danielk1977 | 8d34dfd | 2006-01-24 16:37:57 +0000 | [diff] [blame] | 2194 | int rc2; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2195 | |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 2196 | #ifndef SQLITE_OMIT_AUTOVACUUM |
| 2197 | pBt->nTrunc = 0; |
| 2198 | #endif |
| 2199 | |
danielk1977 | 8d34dfd | 2006-01-24 16:37:57 +0000 | [diff] [blame] | 2200 | assert( TRANS_WRITE==pBt->inTransaction ); |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 2201 | rc2 = sqlite3PagerRollback(pBt->pPager); |
danielk1977 | 8d34dfd | 2006-01-24 16:37:57 +0000 | [diff] [blame] | 2202 | if( rc2!=SQLITE_OK ){ |
| 2203 | rc = rc2; |
| 2204 | } |
| 2205 | |
drh | 24cd67e | 2004-05-10 16:18:47 +0000 | [diff] [blame] | 2206 | /* The rollback may have destroyed the pPage1->aData value. So |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 2207 | ** call sqlite3BtreeGetPage() on page 1 again to make |
| 2208 | ** sure pPage1->aData is set correctly. */ |
| 2209 | if( sqlite3BtreeGetPage(pBt, 1, &pPage1, 0)==SQLITE_OK ){ |
drh | 24cd67e | 2004-05-10 16:18:47 +0000 | [diff] [blame] | 2210 | releasePage(pPage1); |
| 2211 | } |
danielk1977 | fbcd585 | 2004-06-15 02:44:18 +0000 | [diff] [blame] | 2212 | assert( countWriteCursors(pBt)==0 ); |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2213 | pBt->inTransaction = TRANS_READ; |
drh | 24cd67e | 2004-05-10 16:18:47 +0000 | [diff] [blame] | 2214 | } |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2215 | |
| 2216 | if( p->inTrans!=TRANS_NONE ){ |
| 2217 | assert( pBt->nTransaction>0 ); |
| 2218 | pBt->nTransaction--; |
| 2219 | if( 0==pBt->nTransaction ){ |
| 2220 | pBt->inTransaction = TRANS_NONE; |
| 2221 | } |
| 2222 | } |
| 2223 | |
| 2224 | p->inTrans = TRANS_NONE; |
danielk1977 | ee5741e | 2004-05-31 10:01:34 +0000 | [diff] [blame] | 2225 | pBt->inStmt = 0; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2226 | unlockBtreeIfUnused(pBt); |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2227 | |
| 2228 | btreeIntegrity(p); |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 2229 | return rc; |
| 2230 | } |
| 2231 | |
| 2232 | /* |
drh | ab01f61 | 2004-05-22 02:55:23 +0000 | [diff] [blame] | 2233 | ** Start a statement subtransaction. The subtransaction can |
| 2234 | ** can be rolled back independently of the main transaction. |
| 2235 | ** You must start a transaction before starting a subtransaction. |
| 2236 | ** The subtransaction is ended automatically if the main transaction |
drh | 663fc63 | 2002-02-02 18:49:19 +0000 | [diff] [blame] | 2237 | ** commits or rolls back. |
| 2238 | ** |
drh | ab01f61 | 2004-05-22 02:55:23 +0000 | [diff] [blame] | 2239 | ** Only one subtransaction may be active at a time. It is an error to try |
| 2240 | ** to start a new subtransaction if another subtransaction is already active. |
| 2241 | ** |
| 2242 | ** Statement subtransactions are used around individual SQL statements |
| 2243 | ** that are contained within a BEGIN...COMMIT block. If a constraint |
| 2244 | ** error occurs within the statement, the effect of that one statement |
| 2245 | ** can be rolled back without having to rollback the entire transaction. |
drh | 663fc63 | 2002-02-02 18:49:19 +0000 | [diff] [blame] | 2246 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2247 | int sqlite3BtreeBeginStmt(Btree *p){ |
drh | 663fc63 | 2002-02-02 18:49:19 +0000 | [diff] [blame] | 2248 | int rc; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2249 | BtShared *pBt = p->pBt; |
| 2250 | if( (p->inTrans!=TRANS_WRITE) || pBt->inStmt ){ |
drh | f74b8d9 | 2002-09-01 23:20:45 +0000 | [diff] [blame] | 2251 | return pBt->readOnly ? SQLITE_READONLY : SQLITE_ERROR; |
drh | 0d65dc0 | 2002-02-03 00:56:09 +0000 | [diff] [blame] | 2252 | } |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2253 | assert( pBt->inTransaction==TRANS_WRITE ); |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 2254 | rc = pBt->readOnly ? SQLITE_OK : sqlite3PagerStmtBegin(pBt->pPager); |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2255 | pBt->inStmt = 1; |
drh | 663fc63 | 2002-02-02 18:49:19 +0000 | [diff] [blame] | 2256 | return rc; |
| 2257 | } |
| 2258 | |
| 2259 | |
| 2260 | /* |
drh | ab01f61 | 2004-05-22 02:55:23 +0000 | [diff] [blame] | 2261 | ** Commit the statment subtransaction currently in progress. If no |
| 2262 | ** subtransaction is active, this is a no-op. |
drh | 663fc63 | 2002-02-02 18:49:19 +0000 | [diff] [blame] | 2263 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2264 | int sqlite3BtreeCommitStmt(Btree *p){ |
drh | 663fc63 | 2002-02-02 18:49:19 +0000 | [diff] [blame] | 2265 | int rc; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2266 | BtShared *pBt = p->pBt; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2267 | if( pBt->inStmt && !pBt->readOnly ){ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 2268 | rc = sqlite3PagerStmtCommit(pBt->pPager); |
drh | 663fc63 | 2002-02-02 18:49:19 +0000 | [diff] [blame] | 2269 | }else{ |
| 2270 | rc = SQLITE_OK; |
| 2271 | } |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2272 | pBt->inStmt = 0; |
drh | 663fc63 | 2002-02-02 18:49:19 +0000 | [diff] [blame] | 2273 | return rc; |
| 2274 | } |
| 2275 | |
| 2276 | /* |
drh | ab01f61 | 2004-05-22 02:55:23 +0000 | [diff] [blame] | 2277 | ** Rollback the active statement subtransaction. If no subtransaction |
| 2278 | ** is active this routine is a no-op. |
drh | 663fc63 | 2002-02-02 18:49:19 +0000 | [diff] [blame] | 2279 | ** |
drh | ab01f61 | 2004-05-22 02:55:23 +0000 | [diff] [blame] | 2280 | ** All cursors will be invalidated by this operation. Any attempt |
drh | 663fc63 | 2002-02-02 18:49:19 +0000 | [diff] [blame] | 2281 | ** to use a cursor that was open at the beginning of this operation |
| 2282 | ** will result in an error. |
| 2283 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2284 | int sqlite3BtreeRollbackStmt(Btree *p){ |
danielk1977 | 97a227c | 2006-01-20 16:32:04 +0000 | [diff] [blame] | 2285 | int rc = SQLITE_OK; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2286 | BtShared *pBt = p->pBt; |
danielk1977 | 97a227c | 2006-01-20 16:32:04 +0000 | [diff] [blame] | 2287 | sqlite3MallocDisallow(); |
| 2288 | if( pBt->inStmt && !pBt->readOnly ){ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 2289 | rc = sqlite3PagerStmtRollback(pBt->pPager); |
danielk1977 | 97a227c | 2006-01-20 16:32:04 +0000 | [diff] [blame] | 2290 | assert( countWriteCursors(pBt)==0 ); |
| 2291 | pBt->inStmt = 0; |
| 2292 | } |
| 2293 | sqlite3MallocAllow(); |
drh | 663fc63 | 2002-02-02 18:49:19 +0000 | [diff] [blame] | 2294 | return rc; |
| 2295 | } |
| 2296 | |
| 2297 | /* |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2298 | ** Default key comparison function to be used if no comparison function |
| 2299 | ** is specified on the sqlite3BtreeCursor() call. |
| 2300 | */ |
| 2301 | static int dfltCompare( |
| 2302 | void *NotUsed, /* User data is not used */ |
| 2303 | int n1, const void *p1, /* First key to compare */ |
| 2304 | int n2, const void *p2 /* Second key to compare */ |
| 2305 | ){ |
| 2306 | int c; |
| 2307 | c = memcmp(p1, p2, n1<n2 ? n1 : n2); |
| 2308 | if( c==0 ){ |
| 2309 | c = n1 - n2; |
| 2310 | } |
| 2311 | return c; |
| 2312 | } |
| 2313 | |
| 2314 | /* |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 2315 | ** Create a new cursor for the BTree whose root is on the page |
| 2316 | ** iTable. The act of acquiring a cursor gets a read lock on |
| 2317 | ** the database file. |
drh | 1bee3d7 | 2001-10-15 00:44:35 +0000 | [diff] [blame] | 2318 | ** |
| 2319 | ** If wrFlag==0, then the cursor can only be used for reading. |
drh | f74b8d9 | 2002-09-01 23:20:45 +0000 | [diff] [blame] | 2320 | ** If wrFlag==1, then the cursor can be used for reading or for |
| 2321 | ** writing if other conditions for writing are also met. These |
| 2322 | ** are the conditions that must be met in order for writing to |
| 2323 | ** be allowed: |
drh | 6446c4d | 2001-12-15 14:22:18 +0000 | [diff] [blame] | 2324 | ** |
drh | f74b8d9 | 2002-09-01 23:20:45 +0000 | [diff] [blame] | 2325 | ** 1: The cursor must have been opened with wrFlag==1 |
| 2326 | ** |
drh | fe5d71d | 2007-03-19 11:54:10 +0000 | [diff] [blame] | 2327 | ** 2: Other database connections that share the same pager cache |
| 2328 | ** but which are not in the READ_UNCOMMITTED state may not have |
| 2329 | ** cursors open with wrFlag==0 on the same table. Otherwise |
| 2330 | ** the changes made by this write cursor would be visible to |
| 2331 | ** the read cursors in the other database connection. |
drh | f74b8d9 | 2002-09-01 23:20:45 +0000 | [diff] [blame] | 2332 | ** |
| 2333 | ** 3: The database must be writable (not on read-only media) |
| 2334 | ** |
| 2335 | ** 4: There must be an active transaction. |
| 2336 | ** |
drh | 6446c4d | 2001-12-15 14:22:18 +0000 | [diff] [blame] | 2337 | ** No checking is done to make sure that page iTable really is the |
| 2338 | ** root page of a b-tree. If it is not, then the cursor acquired |
| 2339 | ** will not work correctly. |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2340 | ** |
| 2341 | ** The comparison function must be logically the same for every cursor |
| 2342 | ** on a particular table. Changing the comparison function will result |
| 2343 | ** in incorrect operations. If the comparison function is NULL, a |
| 2344 | ** default comparison function is used. The comparison function is |
| 2345 | ** always ignored for INTKEY tables. |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 2346 | */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2347 | int sqlite3BtreeCursor( |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2348 | Btree *p, /* The btree */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2349 | int iTable, /* Root page of table to open */ |
| 2350 | int wrFlag, /* 1 to write. 0 read-only */ |
| 2351 | int (*xCmp)(void*,int,const void*,int,const void*), /* Key Comparison func */ |
| 2352 | void *pArg, /* First arg to xCompare() */ |
| 2353 | BtCursor **ppCur /* Write new cursor here */ |
| 2354 | ){ |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 2355 | int rc; |
drh | 8dcd7ca | 2004-08-08 19:43:29 +0000 | [diff] [blame] | 2356 | BtCursor *pCur; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2357 | BtShared *pBt = p->pBt; |
drh | ecdc753 | 2001-09-23 02:35:53 +0000 | [diff] [blame] | 2358 | |
drh | 8dcd7ca | 2004-08-08 19:43:29 +0000 | [diff] [blame] | 2359 | *ppCur = 0; |
| 2360 | if( wrFlag ){ |
drh | 8dcd7ca | 2004-08-08 19:43:29 +0000 | [diff] [blame] | 2361 | if( pBt->readOnly ){ |
| 2362 | return SQLITE_READONLY; |
| 2363 | } |
drh | 980b1a7 | 2006-08-16 16:42:48 +0000 | [diff] [blame] | 2364 | if( checkReadLocks(p, iTable, 0) ){ |
drh | 8dcd7ca | 2004-08-08 19:43:29 +0000 | [diff] [blame] | 2365 | return SQLITE_LOCKED; |
| 2366 | } |
drh | a0c9a11 | 2004-03-10 13:42:37 +0000 | [diff] [blame] | 2367 | } |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2368 | |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 2369 | if( pBt->pPage1==0 ){ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2370 | rc = lockBtreeWithRetry(p); |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 2371 | if( rc!=SQLITE_OK ){ |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 2372 | return rc; |
| 2373 | } |
drh | 1831f18 | 2007-04-24 17:35:59 +0000 | [diff] [blame] | 2374 | if( pBt->readOnly && wrFlag ){ |
| 2375 | return SQLITE_READONLY; |
| 2376 | } |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 2377 | } |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 2378 | pCur = sqliteMalloc( sizeof(*pCur) ); |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 2379 | if( pCur==0 ){ |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 2380 | rc = SQLITE_NOMEM; |
| 2381 | goto create_cursor_exception; |
| 2382 | } |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 2383 | pCur->pgnoRoot = (Pgno)iTable; |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 2384 | if( iTable==1 && sqlite3PagerPagecount(pBt->pPager)==0 ){ |
drh | 24cd67e | 2004-05-10 16:18:47 +0000 | [diff] [blame] | 2385 | rc = SQLITE_EMPTY; |
| 2386 | goto create_cursor_exception; |
| 2387 | } |
drh | de64713 | 2004-05-07 17:57:49 +0000 | [diff] [blame] | 2388 | rc = getAndInitPage(pBt, pCur->pgnoRoot, &pCur->pPage, 0); |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 2389 | if( rc!=SQLITE_OK ){ |
| 2390 | goto create_cursor_exception; |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 2391 | } |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2392 | |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2393 | /* Now that no other errors can occur, finish filling in the BtCursor |
| 2394 | ** variables, link the cursor into the BtShared list and set *ppCur (the |
| 2395 | ** output argument to this function). |
| 2396 | */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2397 | pCur->xCompare = xCmp ? xCmp : dfltCompare; |
| 2398 | pCur->pArg = pArg; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2399 | pCur->pBtree = p; |
drh | ecdc753 | 2001-09-23 02:35:53 +0000 | [diff] [blame] | 2400 | pCur->wrFlag = wrFlag; |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 2401 | pCur->pNext = pBt->pCursor; |
| 2402 | if( pCur->pNext ){ |
| 2403 | pCur->pNext->pPrev = pCur; |
| 2404 | } |
| 2405 | pBt->pCursor = pCur; |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 2406 | pCur->eState = CURSOR_INVALID; |
drh | 2af926b | 2001-05-15 00:39:25 +0000 | [diff] [blame] | 2407 | *ppCur = pCur; |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 2408 | |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2409 | return SQLITE_OK; |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 2410 | create_cursor_exception: |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 2411 | if( pCur ){ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2412 | releasePage(pCur->pPage); |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 2413 | sqliteFree(pCur); |
| 2414 | } |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2415 | unlockBtreeIfUnused(pBt); |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 2416 | return rc; |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 2417 | } |
| 2418 | |
| 2419 | /* |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2420 | ** Close a cursor. The read lock on the database file is released |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 2421 | ** when the last cursor is closed. |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 2422 | */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2423 | int sqlite3BtreeCloseCursor(BtCursor *pCur){ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2424 | BtShared *pBt = pCur->pBtree->pBt; |
drh | bf700f3 | 2007-03-31 02:36:44 +0000 | [diff] [blame] | 2425 | clearCursorPosition(pCur); |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 2426 | if( pCur->pPrev ){ |
| 2427 | pCur->pPrev->pNext = pCur->pNext; |
| 2428 | }else{ |
| 2429 | pBt->pCursor = pCur->pNext; |
| 2430 | } |
| 2431 | if( pCur->pNext ){ |
| 2432 | pCur->pNext->pPrev = pCur->pPrev; |
| 2433 | } |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2434 | releasePage(pCur->pPage); |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2435 | unlockBtreeIfUnused(pBt); |
danielk1977 | 92d4d7a | 2007-05-04 12:05:56 +0000 | [diff] [blame] | 2436 | invalidateOverflowCache(pCur); |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 2437 | sqliteFree(pCur); |
drh | 8c42ca9 | 2001-06-22 19:15:00 +0000 | [diff] [blame] | 2438 | return SQLITE_OK; |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 2439 | } |
| 2440 | |
drh | 7e3b0a0 | 2001-04-28 16:52:40 +0000 | [diff] [blame] | 2441 | /* |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 2442 | ** Make a temporary cursor by filling in the fields of pTempCur. |
| 2443 | ** The temporary cursor is not on the cursor list for the Btree. |
| 2444 | */ |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 2445 | void sqlite3BtreeGetTempCursor(BtCursor *pCur, BtCursor *pTempCur){ |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 2446 | memcpy(pTempCur, pCur, sizeof(*pCur)); |
| 2447 | pTempCur->pNext = 0; |
| 2448 | pTempCur->pPrev = 0; |
drh | ecdc753 | 2001-09-23 02:35:53 +0000 | [diff] [blame] | 2449 | if( pTempCur->pPage ){ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 2450 | sqlite3PagerRef(pTempCur->pPage->pDbPage); |
drh | ecdc753 | 2001-09-23 02:35:53 +0000 | [diff] [blame] | 2451 | } |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 2452 | } |
| 2453 | |
| 2454 | /* |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 2455 | ** Delete a temporary cursor such as was made by the CreateTemporaryCursor() |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 2456 | ** function above. |
| 2457 | */ |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 2458 | void sqlite3BtreeReleaseTempCursor(BtCursor *pCur){ |
drh | ecdc753 | 2001-09-23 02:35:53 +0000 | [diff] [blame] | 2459 | if( pCur->pPage ){ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 2460 | sqlite3PagerUnref(pCur->pPage->pDbPage); |
drh | ecdc753 | 2001-09-23 02:35:53 +0000 | [diff] [blame] | 2461 | } |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 2462 | } |
| 2463 | |
| 2464 | /* |
danielk1977 | 1cc5ed8 | 2007-05-16 17:28:43 +0000 | [diff] [blame] | 2465 | ** The GET_CELL_INFO() macro. Takes one argument, a pointer to a valid |
| 2466 | ** btree cursor (type BtCursor*). This macro makes sure the BtCursor.info |
| 2467 | ** field of the given cursor is valid. If it is not already valid, call |
| 2468 | ** sqlite3BtreeParseCell() to fill it in. |
drh | ab01f61 | 2004-05-22 02:55:23 +0000 | [diff] [blame] | 2469 | ** |
| 2470 | ** BtCursor.info is a cache of the information in the current cell. |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 2471 | ** Using this cache reduces the number of calls to sqlite3BtreeParseCell(). |
drh | 9188b38 | 2004-05-14 21:12:22 +0000 | [diff] [blame] | 2472 | */ |
drh | 9188b38 | 2004-05-14 21:12:22 +0000 | [diff] [blame] | 2473 | #ifndef NDEBUG |
danielk1977 | 1cc5ed8 | 2007-05-16 17:28:43 +0000 | [diff] [blame] | 2474 | static void assertCellInfo(BtCursor *pCur){ |
drh | 9188b38 | 2004-05-14 21:12:22 +0000 | [diff] [blame] | 2475 | CellInfo info; |
drh | 51c6d96 | 2004-06-06 00:42:25 +0000 | [diff] [blame] | 2476 | memset(&info, 0, sizeof(info)); |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 2477 | sqlite3BtreeParseCell(pCur->pPage, pCur->idx, &info); |
drh | 9188b38 | 2004-05-14 21:12:22 +0000 | [diff] [blame] | 2478 | assert( memcmp(&info, &pCur->info, sizeof(info))==0 ); |
drh | 9188b38 | 2004-05-14 21:12:22 +0000 | [diff] [blame] | 2479 | } |
danielk1977 | 1cc5ed8 | 2007-05-16 17:28:43 +0000 | [diff] [blame] | 2480 | #else |
| 2481 | #define assertCellInfo(x) |
| 2482 | #endif |
| 2483 | |
| 2484 | #define GET_CELL_INFO(pCur) \ |
| 2485 | if( pCur->info.nSize==0 ) \ |
| 2486 | sqlite3BtreeParseCell(pCur->pPage, pCur->idx, &pCur->info); \ |
| 2487 | else \ |
| 2488 | assertCellInfo(pCur); |
| 2489 | |
drh | 9188b38 | 2004-05-14 21:12:22 +0000 | [diff] [blame] | 2490 | |
| 2491 | /* |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2492 | ** Set *pSize to the size of the buffer needed to hold the value of |
| 2493 | ** the key for the current entry. If the cursor is not pointing |
| 2494 | ** to a valid entry, *pSize is set to 0. |
| 2495 | ** |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 2496 | ** For a table with the INTKEY flag set, this routine returns the key |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2497 | ** itself, not the number of bytes in the key. |
drh | 7e3b0a0 | 2001-04-28 16:52:40 +0000 | [diff] [blame] | 2498 | */ |
drh | 4a1c380 | 2004-05-12 15:15:47 +0000 | [diff] [blame] | 2499 | int sqlite3BtreeKeySize(BtCursor *pCur, i64 *pSize){ |
drh | bf700f3 | 2007-03-31 02:36:44 +0000 | [diff] [blame] | 2500 | int rc = restoreOrClearCursorPosition(pCur); |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 2501 | if( rc==SQLITE_OK ){ |
| 2502 | assert( pCur->eState==CURSOR_INVALID || pCur->eState==CURSOR_VALID ); |
| 2503 | if( pCur->eState==CURSOR_INVALID ){ |
| 2504 | *pSize = 0; |
| 2505 | }else{ |
danielk1977 | 1cc5ed8 | 2007-05-16 17:28:43 +0000 | [diff] [blame] | 2506 | GET_CELL_INFO(pCur); |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 2507 | *pSize = pCur->info.nKey; |
| 2508 | } |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 2509 | } |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 2510 | return rc; |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 2511 | } |
drh | 2af926b | 2001-05-15 00:39:25 +0000 | [diff] [blame] | 2512 | |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 2513 | /* |
drh | 0e1c19e | 2004-05-11 00:58:56 +0000 | [diff] [blame] | 2514 | ** Set *pSize to the number of bytes of data in the entry the |
| 2515 | ** cursor currently points to. Always return SQLITE_OK. |
| 2516 | ** Failure is not possible. If the cursor is not currently |
| 2517 | ** pointing to an entry (which can happen, for example, if |
| 2518 | ** the database is empty) then *pSize is set to 0. |
| 2519 | */ |
| 2520 | int sqlite3BtreeDataSize(BtCursor *pCur, u32 *pSize){ |
drh | bf700f3 | 2007-03-31 02:36:44 +0000 | [diff] [blame] | 2521 | int rc = restoreOrClearCursorPosition(pCur); |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 2522 | if( rc==SQLITE_OK ){ |
| 2523 | assert( pCur->eState==CURSOR_INVALID || pCur->eState==CURSOR_VALID ); |
| 2524 | if( pCur->eState==CURSOR_INVALID ){ |
| 2525 | /* Not pointing at a valid entry - set *pSize to 0. */ |
| 2526 | *pSize = 0; |
| 2527 | }else{ |
danielk1977 | 1cc5ed8 | 2007-05-16 17:28:43 +0000 | [diff] [blame] | 2528 | GET_CELL_INFO(pCur); |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 2529 | *pSize = pCur->info.nData; |
| 2530 | } |
drh | 0e1c19e | 2004-05-11 00:58:56 +0000 | [diff] [blame] | 2531 | } |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 2532 | return rc; |
drh | 0e1c19e | 2004-05-11 00:58:56 +0000 | [diff] [blame] | 2533 | } |
| 2534 | |
| 2535 | /* |
danielk1977 | d0441796 | 2007-05-02 13:16:30 +0000 | [diff] [blame] | 2536 | ** Given the page number of an overflow page in the database (parameter |
| 2537 | ** ovfl), this function finds the page number of the next page in the |
| 2538 | ** linked list of overflow pages. If possible, it uses the auto-vacuum |
| 2539 | ** pointer-map data instead of reading the content of page ovfl to do so. |
| 2540 | ** |
| 2541 | ** If an error occurs an SQLite error code is returned. Otherwise: |
| 2542 | ** |
| 2543 | ** Unless pPgnoNext is NULL, the page number of the next overflow |
| 2544 | ** page in the linked list is written to *pPgnoNext. If page ovfl |
| 2545 | ** is the last page in it's linked list, *pPgnoNext is set to zero. |
| 2546 | ** |
| 2547 | ** If ppPage is not NULL, *ppPage is set to the MemPage* handle |
| 2548 | ** for page ovfl. The underlying pager page may have been requested |
| 2549 | ** with the noContent flag set, so the page data accessable via |
| 2550 | ** this handle may not be trusted. |
| 2551 | */ |
| 2552 | static int getOverflowPage( |
| 2553 | BtShared *pBt, |
| 2554 | Pgno ovfl, /* Overflow page */ |
| 2555 | MemPage **ppPage, /* OUT: MemPage handle */ |
| 2556 | Pgno *pPgnoNext /* OUT: Next overflow page number */ |
| 2557 | ){ |
| 2558 | Pgno next = 0; |
| 2559 | int rc; |
| 2560 | |
| 2561 | /* One of these must not be NULL. Otherwise, why call this function? */ |
| 2562 | assert(ppPage || pPgnoNext); |
| 2563 | |
| 2564 | /* If pPgnoNext is NULL, then this function is being called to obtain |
| 2565 | ** a MemPage* reference only. No page-data is required in this case. |
| 2566 | */ |
| 2567 | if( !pPgnoNext ){ |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 2568 | return sqlite3BtreeGetPage(pBt, ovfl, ppPage, 1); |
danielk1977 | d0441796 | 2007-05-02 13:16:30 +0000 | [diff] [blame] | 2569 | } |
| 2570 | |
| 2571 | #ifndef SQLITE_OMIT_AUTOVACUUM |
| 2572 | /* Try to find the next page in the overflow list using the |
| 2573 | ** autovacuum pointer-map pages. Guess that the next page in |
| 2574 | ** the overflow list is page number (ovfl+1). If that guess turns |
| 2575 | ** out to be wrong, fall back to loading the data of page |
| 2576 | ** number ovfl to determine the next page number. |
| 2577 | */ |
| 2578 | if( pBt->autoVacuum ){ |
| 2579 | Pgno pgno; |
| 2580 | Pgno iGuess = ovfl+1; |
| 2581 | u8 eType; |
| 2582 | |
| 2583 | while( PTRMAP_ISPAGE(pBt, iGuess) || iGuess==PENDING_BYTE_PAGE(pBt) ){ |
| 2584 | iGuess++; |
| 2585 | } |
| 2586 | |
danielk1977 | 20713f3 | 2007-05-03 11:43:33 +0000 | [diff] [blame] | 2587 | if( iGuess<=sqlite3PagerPagecount(pBt->pPager) ){ |
danielk1977 | d0441796 | 2007-05-02 13:16:30 +0000 | [diff] [blame] | 2588 | rc = ptrmapGet(pBt, iGuess, &eType, &pgno); |
| 2589 | if( rc!=SQLITE_OK ){ |
| 2590 | return rc; |
| 2591 | } |
| 2592 | if( eType==PTRMAP_OVERFLOW2 && pgno==ovfl ){ |
| 2593 | next = iGuess; |
| 2594 | } |
| 2595 | } |
| 2596 | } |
| 2597 | #endif |
| 2598 | |
| 2599 | if( next==0 || ppPage ){ |
| 2600 | MemPage *pPage = 0; |
| 2601 | |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 2602 | rc = sqlite3BtreeGetPage(pBt, ovfl, &pPage, next!=0); |
danielk1977 | d0441796 | 2007-05-02 13:16:30 +0000 | [diff] [blame] | 2603 | assert(rc==SQLITE_OK || pPage==0); |
| 2604 | if( next==0 && rc==SQLITE_OK ){ |
| 2605 | next = get4byte(pPage->aData); |
| 2606 | } |
| 2607 | |
| 2608 | if( ppPage ){ |
| 2609 | *ppPage = pPage; |
| 2610 | }else{ |
| 2611 | releasePage(pPage); |
| 2612 | } |
| 2613 | } |
| 2614 | *pPgnoNext = next; |
| 2615 | |
| 2616 | return rc; |
| 2617 | } |
| 2618 | |
danielk1977 | da10719 | 2007-05-04 08:32:13 +0000 | [diff] [blame] | 2619 | /* |
| 2620 | ** Copy data from a buffer to a page, or from a page to a buffer. |
| 2621 | ** |
| 2622 | ** pPayload is a pointer to data stored on database page pDbPage. |
| 2623 | ** If argument eOp is false, then nByte bytes of data are copied |
| 2624 | ** from pPayload to the buffer pointed at by pBuf. If eOp is true, |
| 2625 | ** then sqlite3PagerWrite() is called on pDbPage and nByte bytes |
| 2626 | ** of data are copied from the buffer pBuf to pPayload. |
| 2627 | ** |
| 2628 | ** SQLITE_OK is returned on success, otherwise an error code. |
| 2629 | */ |
| 2630 | static int copyPayload( |
| 2631 | void *pPayload, /* Pointer to page data */ |
| 2632 | void *pBuf, /* Pointer to buffer */ |
| 2633 | int nByte, /* Number of bytes to copy */ |
| 2634 | int eOp, /* 0 -> copy from page, 1 -> copy to page */ |
| 2635 | DbPage *pDbPage /* Page containing pPayload */ |
| 2636 | ){ |
| 2637 | if( eOp ){ |
| 2638 | /* Copy data from buffer to page (a write operation) */ |
| 2639 | int rc = sqlite3PagerWrite(pDbPage); |
| 2640 | if( rc!=SQLITE_OK ){ |
| 2641 | return rc; |
| 2642 | } |
| 2643 | memcpy(pPayload, pBuf, nByte); |
| 2644 | }else{ |
| 2645 | /* Copy data from page to buffer (a read operation) */ |
| 2646 | memcpy(pBuf, pPayload, nByte); |
| 2647 | } |
| 2648 | return SQLITE_OK; |
| 2649 | } |
danielk1977 | d0441796 | 2007-05-02 13:16:30 +0000 | [diff] [blame] | 2650 | |
| 2651 | /* |
danielk1977 | 9f8d640 | 2007-05-02 17:48:45 +0000 | [diff] [blame] | 2652 | ** This function is used to read or overwrite payload information |
| 2653 | ** for the entry that the pCur cursor is pointing to. If the eOp |
| 2654 | ** parameter is 0, this is a read operation (data copied into |
| 2655 | ** buffer pBuf). If it is non-zero, a write (data copied from |
| 2656 | ** buffer pBuf). |
| 2657 | ** |
| 2658 | ** A total of "amt" bytes are read or written beginning at "offset". |
| 2659 | ** Data is read to or from the buffer pBuf. |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 2660 | ** |
| 2661 | ** This routine does not make a distinction between key and data. |
danielk1977 | 9f8d640 | 2007-05-02 17:48:45 +0000 | [diff] [blame] | 2662 | ** It just reads or writes bytes from the payload area. Data might |
| 2663 | ** appear on the main page or be scattered out on multiple overflow |
| 2664 | ** pages. |
danielk1977 | da10719 | 2007-05-04 08:32:13 +0000 | [diff] [blame] | 2665 | ** |
danielk1977 | dcbb5d3 | 2007-05-04 18:36:44 +0000 | [diff] [blame] | 2666 | ** If the BtCursor.isIncrblobHandle flag is set, and the current |
danielk1977 | da10719 | 2007-05-04 08:32:13 +0000 | [diff] [blame] | 2667 | ** cursor entry uses one or more overflow pages, this function |
| 2668 | ** allocates space for and lazily popluates the overflow page-list |
| 2669 | ** cache array (BtCursor.aOverflow). Subsequent calls use this |
| 2670 | ** cache to make seeking to the supplied offset more efficient. |
| 2671 | ** |
| 2672 | ** Once an overflow page-list cache has been allocated, it may be |
| 2673 | ** invalidated if some other cursor writes to the same table, or if |
| 2674 | ** the cursor is moved to a different row. Additionally, in auto-vacuum |
| 2675 | ** mode, the following events may invalidate an overflow page-list cache. |
| 2676 | ** |
| 2677 | ** * An incremental vacuum, |
| 2678 | ** * A commit in auto_vacuum="full" mode, |
| 2679 | ** * Creating a table (may require moving an overflow page). |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 2680 | */ |
danielk1977 | 9f8d640 | 2007-05-02 17:48:45 +0000 | [diff] [blame] | 2681 | static int accessPayload( |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2682 | BtCursor *pCur, /* Cursor pointing to entry to read from */ |
| 2683 | int offset, /* Begin reading this far into payload */ |
| 2684 | int amt, /* Read this many bytes */ |
| 2685 | unsigned char *pBuf, /* Write the bytes into this buffer */ |
danielk1977 | 9f8d640 | 2007-05-02 17:48:45 +0000 | [diff] [blame] | 2686 | int skipKey, /* offset begins at data if this is true */ |
| 2687 | int eOp /* zero to read. non-zero to write. */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2688 | ){ |
| 2689 | unsigned char *aPayload; |
danielk1977 | da10719 | 2007-05-04 08:32:13 +0000 | [diff] [blame] | 2690 | int rc = SQLITE_OK; |
drh | fa1a98a | 2004-05-14 19:08:17 +0000 | [diff] [blame] | 2691 | u32 nKey; |
danielk1977 | 2dec970 | 2007-05-02 16:48:37 +0000 | [diff] [blame] | 2692 | int iIdx = 0; |
danielk1977 | da10719 | 2007-05-04 08:32:13 +0000 | [diff] [blame] | 2693 | MemPage *pPage = pCur->pPage; /* Btree page of current cursor entry */ |
| 2694 | BtShared *pBt = pCur->pBtree->pBt; /* Btree this cursor belongs to */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2695 | |
danielk1977 | da10719 | 2007-05-04 08:32:13 +0000 | [diff] [blame] | 2696 | assert( pPage ); |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 2697 | assert( pCur->eState==CURSOR_VALID ); |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2698 | assert( pCur->idx>=0 && pCur->idx<pPage->nCell ); |
danielk1977 | da10719 | 2007-05-04 08:32:13 +0000 | [diff] [blame] | 2699 | assert( offset>=0 ); |
| 2700 | |
danielk1977 | 1cc5ed8 | 2007-05-16 17:28:43 +0000 | [diff] [blame] | 2701 | GET_CELL_INFO(pCur); |
drh | 366fda6 | 2006-01-13 02:35:09 +0000 | [diff] [blame] | 2702 | aPayload = pCur->info.pCell + pCur->info.nHeader; |
danielk1977 | da10719 | 2007-05-04 08:32:13 +0000 | [diff] [blame] | 2703 | nKey = (pPage->intKey ? 0 : pCur->info.nKey); |
| 2704 | |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2705 | if( skipKey ){ |
drh | fa1a98a | 2004-05-14 19:08:17 +0000 | [diff] [blame] | 2706 | offset += nKey; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2707 | } |
drh | fa1a98a | 2004-05-14 19:08:17 +0000 | [diff] [blame] | 2708 | if( offset+amt > nKey+pCur->info.nData ){ |
danielk1977 | da10719 | 2007-05-04 08:32:13 +0000 | [diff] [blame] | 2709 | /* 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] | 2710 | return SQLITE_ERROR; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2711 | } |
danielk1977 | da10719 | 2007-05-04 08:32:13 +0000 | [diff] [blame] | 2712 | |
| 2713 | /* Check if data must be read/written to/from the btree page itself. */ |
drh | fa1a98a | 2004-05-14 19:08:17 +0000 | [diff] [blame] | 2714 | if( offset<pCur->info.nLocal ){ |
drh | 2af926b | 2001-05-15 00:39:25 +0000 | [diff] [blame] | 2715 | int a = amt; |
drh | fa1a98a | 2004-05-14 19:08:17 +0000 | [diff] [blame] | 2716 | if( a+offset>pCur->info.nLocal ){ |
| 2717 | a = pCur->info.nLocal - offset; |
drh | 2af926b | 2001-05-15 00:39:25 +0000 | [diff] [blame] | 2718 | } |
danielk1977 | da10719 | 2007-05-04 08:32:13 +0000 | [diff] [blame] | 2719 | rc = copyPayload(&aPayload[offset], pBuf, a, eOp, pPage->pDbPage); |
drh | 2aa679f | 2001-06-25 02:11:07 +0000 | [diff] [blame] | 2720 | offset = 0; |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 2721 | pBuf += a; |
drh | 2af926b | 2001-05-15 00:39:25 +0000 | [diff] [blame] | 2722 | amt -= a; |
drh | dd79342 | 2001-06-28 01:54:48 +0000 | [diff] [blame] | 2723 | }else{ |
drh | fa1a98a | 2004-05-14 19:08:17 +0000 | [diff] [blame] | 2724 | offset -= pCur->info.nLocal; |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 2725 | } |
danielk1977 | da10719 | 2007-05-04 08:32:13 +0000 | [diff] [blame] | 2726 | |
| 2727 | if( rc==SQLITE_OK && amt>0 ){ |
| 2728 | const int ovflSize = pBt->usableSize - 4; /* Bytes content per ovfl page */ |
| 2729 | Pgno nextPage; |
| 2730 | |
drh | fa1a98a | 2004-05-14 19:08:17 +0000 | [diff] [blame] | 2731 | nextPage = get4byte(&aPayload[pCur->info.nLocal]); |
danielk1977 | da10719 | 2007-05-04 08:32:13 +0000 | [diff] [blame] | 2732 | |
danielk1977 | 2dec970 | 2007-05-02 16:48:37 +0000 | [diff] [blame] | 2733 | #ifndef SQLITE_OMIT_INCRBLOB |
danielk1977 | dcbb5d3 | 2007-05-04 18:36:44 +0000 | [diff] [blame] | 2734 | /* If the isIncrblobHandle flag is set and the BtCursor.aOverflow[] |
danielk1977 | da10719 | 2007-05-04 08:32:13 +0000 | [diff] [blame] | 2735 | ** has not been allocated, allocate it now. The array is sized at |
| 2736 | ** one entry for each overflow page in the overflow chain. The |
| 2737 | ** page number of the first overflow page is stored in aOverflow[0], |
| 2738 | ** etc. A value of 0 in the aOverflow[] array means "not yet known" |
| 2739 | ** (the cache is lazily populated). |
| 2740 | */ |
danielk1977 | dcbb5d3 | 2007-05-04 18:36:44 +0000 | [diff] [blame] | 2741 | if( pCur->isIncrblobHandle && !pCur->aOverflow ){ |
danielk1977 | 2dec970 | 2007-05-02 16:48:37 +0000 | [diff] [blame] | 2742 | int nOvfl = (pCur->info.nPayload-pCur->info.nLocal+ovflSize-1)/ovflSize; |
| 2743 | pCur->aOverflow = (Pgno *)sqliteMalloc(sizeof(Pgno)*nOvfl); |
| 2744 | if( nOvfl && !pCur->aOverflow ){ |
danielk1977 | da10719 | 2007-05-04 08:32:13 +0000 | [diff] [blame] | 2745 | rc = SQLITE_NOMEM; |
danielk1977 | 2dec970 | 2007-05-02 16:48:37 +0000 | [diff] [blame] | 2746 | } |
| 2747 | } |
danielk1977 | da10719 | 2007-05-04 08:32:13 +0000 | [diff] [blame] | 2748 | |
| 2749 | /* If the overflow page-list cache has been allocated and the |
| 2750 | ** entry for the first required overflow page is valid, skip |
| 2751 | ** directly to it. |
| 2752 | */ |
danielk1977 | 2dec970 | 2007-05-02 16:48:37 +0000 | [diff] [blame] | 2753 | if( pCur->aOverflow && pCur->aOverflow[offset/ovflSize] ){ |
| 2754 | iIdx = (offset/ovflSize); |
| 2755 | nextPage = pCur->aOverflow[iIdx]; |
| 2756 | offset = (offset%ovflSize); |
| 2757 | } |
| 2758 | #endif |
danielk1977 | da10719 | 2007-05-04 08:32:13 +0000 | [diff] [blame] | 2759 | |
| 2760 | for( ; rc==SQLITE_OK && amt>0 && nextPage; iIdx++){ |
| 2761 | |
| 2762 | #ifndef SQLITE_OMIT_INCRBLOB |
| 2763 | /* If required, populate the overflow page-list cache. */ |
| 2764 | if( pCur->aOverflow ){ |
| 2765 | assert(!pCur->aOverflow[iIdx] || pCur->aOverflow[iIdx]==nextPage); |
| 2766 | pCur->aOverflow[iIdx] = nextPage; |
| 2767 | } |
| 2768 | #endif |
| 2769 | |
danielk1977 | d0441796 | 2007-05-02 13:16:30 +0000 | [diff] [blame] | 2770 | if( offset>=ovflSize ){ |
| 2771 | /* The only reason to read this page is to obtain the page |
danielk1977 | da10719 | 2007-05-04 08:32:13 +0000 | [diff] [blame] | 2772 | ** number for the next page in the overflow chain. The page |
| 2773 | ** data is not required. So first try to lookup the overflow |
| 2774 | ** page-list cache, if any, then fall back to the getOverflowPage() |
| 2775 | ** function. |
danielk1977 | d0441796 | 2007-05-02 13:16:30 +0000 | [diff] [blame] | 2776 | */ |
danielk1977 | 2dec970 | 2007-05-02 16:48:37 +0000 | [diff] [blame] | 2777 | #ifndef SQLITE_OMIT_INCRBLOB |
danielk1977 | da10719 | 2007-05-04 08:32:13 +0000 | [diff] [blame] | 2778 | if( pCur->aOverflow && pCur->aOverflow[iIdx+1] ){ |
| 2779 | nextPage = pCur->aOverflow[iIdx+1]; |
| 2780 | } else |
danielk1977 | 2dec970 | 2007-05-02 16:48:37 +0000 | [diff] [blame] | 2781 | #endif |
danielk1977 | da10719 | 2007-05-04 08:32:13 +0000 | [diff] [blame] | 2782 | rc = getOverflowPage(pBt, nextPage, 0, &nextPage); |
| 2783 | assert(rc==SQLITE_OK || nextPage==0); |
| 2784 | offset -= ovflSize; |
danielk1977 | d0441796 | 2007-05-02 13:16:30 +0000 | [diff] [blame] | 2785 | }else{ |
danielk1977 | 9f8d640 | 2007-05-02 17:48:45 +0000 | [diff] [blame] | 2786 | /* Need to read this page properly. It contains some of the |
| 2787 | ** range of data that is being read (eOp==0) or written (eOp!=0). |
danielk1977 | d0441796 | 2007-05-02 13:16:30 +0000 | [diff] [blame] | 2788 | */ |
| 2789 | DbPage *pDbPage; |
danielk1977 | cfe9a69 | 2004-06-16 12:00:29 +0000 | [diff] [blame] | 2790 | int a = amt; |
danielk1977 | d0441796 | 2007-05-02 13:16:30 +0000 | [diff] [blame] | 2791 | rc = sqlite3PagerGet(pBt->pPager, nextPage, &pDbPage); |
danielk1977 | da10719 | 2007-05-04 08:32:13 +0000 | [diff] [blame] | 2792 | if( rc==SQLITE_OK ){ |
| 2793 | aPayload = sqlite3PagerGetData(pDbPage); |
| 2794 | nextPage = get4byte(aPayload); |
| 2795 | if( a + offset > ovflSize ){ |
| 2796 | a = ovflSize - offset; |
danielk1977 | 9f8d640 | 2007-05-02 17:48:45 +0000 | [diff] [blame] | 2797 | } |
danielk1977 | da10719 | 2007-05-04 08:32:13 +0000 | [diff] [blame] | 2798 | rc = copyPayload(&aPayload[offset+4], pBuf, a, eOp, pDbPage); |
| 2799 | sqlite3PagerUnref(pDbPage); |
| 2800 | offset = 0; |
| 2801 | amt -= a; |
| 2802 | pBuf += a; |
danielk1977 | 9f8d640 | 2007-05-02 17:48:45 +0000 | [diff] [blame] | 2803 | } |
danielk1977 | cfe9a69 | 2004-06-16 12:00:29 +0000 | [diff] [blame] | 2804 | } |
drh | 2af926b | 2001-05-15 00:39:25 +0000 | [diff] [blame] | 2805 | } |
drh | 2af926b | 2001-05-15 00:39:25 +0000 | [diff] [blame] | 2806 | } |
danielk1977 | cfe9a69 | 2004-06-16 12:00:29 +0000 | [diff] [blame] | 2807 | |
danielk1977 | da10719 | 2007-05-04 08:32:13 +0000 | [diff] [blame] | 2808 | if( rc==SQLITE_OK && amt>0 ){ |
drh | 4928570 | 2005-09-17 15:20:26 +0000 | [diff] [blame] | 2809 | return SQLITE_CORRUPT_BKPT; |
drh | a7fcb05 | 2001-12-14 15:09:55 +0000 | [diff] [blame] | 2810 | } |
danielk1977 | da10719 | 2007-05-04 08:32:13 +0000 | [diff] [blame] | 2811 | return rc; |
drh | 2af926b | 2001-05-15 00:39:25 +0000 | [diff] [blame] | 2812 | } |
| 2813 | |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 2814 | /* |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2815 | ** Read part of the key associated with cursor pCur. Exactly |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 2816 | ** "amt" bytes will be transfered into pBuf[]. The transfer |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2817 | ** begins at "offset". |
drh | 8c1238a | 2003-01-02 14:43:55 +0000 | [diff] [blame] | 2818 | ** |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2819 | ** Return SQLITE_OK on success or an error code if anything goes |
| 2820 | ** wrong. An error is returned if "offset+amt" is larger than |
| 2821 | ** the available payload. |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 2822 | */ |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 2823 | int sqlite3BtreeKey(BtCursor *pCur, u32 offset, u32 amt, void *pBuf){ |
drh | bf700f3 | 2007-03-31 02:36:44 +0000 | [diff] [blame] | 2824 | int rc = restoreOrClearCursorPosition(pCur); |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 2825 | if( rc==SQLITE_OK ){ |
| 2826 | assert( pCur->eState==CURSOR_VALID ); |
| 2827 | assert( pCur->pPage!=0 ); |
| 2828 | if( pCur->pPage->intKey ){ |
| 2829 | return SQLITE_CORRUPT_BKPT; |
| 2830 | } |
| 2831 | assert( pCur->pPage->intKey==0 ); |
| 2832 | assert( pCur->idx>=0 && pCur->idx<pCur->pPage->nCell ); |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 2833 | rc = accessPayload(pCur, offset, amt, (unsigned char*)pBuf, 0, 0); |
drh | 6575a22 | 2005-03-10 17:06:34 +0000 | [diff] [blame] | 2834 | } |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 2835 | return rc; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2836 | } |
| 2837 | |
| 2838 | /* |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2839 | ** Read part of the data associated with cursor pCur. Exactly |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 2840 | ** "amt" bytes will be transfered into pBuf[]. The transfer |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2841 | ** begins at "offset". |
| 2842 | ** |
| 2843 | ** Return SQLITE_OK on success or an error code if anything goes |
| 2844 | ** wrong. An error is returned if "offset+amt" is larger than |
| 2845 | ** the available payload. |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 2846 | */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2847 | int sqlite3BtreeData(BtCursor *pCur, u32 offset, u32 amt, void *pBuf){ |
drh | bf700f3 | 2007-03-31 02:36:44 +0000 | [diff] [blame] | 2848 | int rc = restoreOrClearCursorPosition(pCur); |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 2849 | if( rc==SQLITE_OK ){ |
| 2850 | assert( pCur->eState==CURSOR_VALID ); |
| 2851 | assert( pCur->pPage!=0 ); |
| 2852 | assert( pCur->idx>=0 && pCur->idx<pCur->pPage->nCell ); |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 2853 | rc = accessPayload(pCur, offset, amt, pBuf, 1, 0); |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 2854 | } |
| 2855 | return rc; |
drh | 2af926b | 2001-05-15 00:39:25 +0000 | [diff] [blame] | 2856 | } |
| 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 | ** Return a pointer to payload information from the entry that the |
| 2860 | ** pCur cursor is pointing to. The pointer is to the beginning of |
| 2861 | ** 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] | 2862 | ** skipKey==1. The number of bytes of available key/data is written |
| 2863 | ** into *pAmt. If *pAmt==0, then the value returned will not be |
| 2864 | ** a valid pointer. |
drh | 0e1c19e | 2004-05-11 00:58:56 +0000 | [diff] [blame] | 2865 | ** |
| 2866 | ** This routine is an optimization. It is common for the entire key |
| 2867 | ** and data to fit on the local page and for there to be no overflow |
| 2868 | ** pages. When that is so, this routine can be used to access the |
| 2869 | ** 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] | 2870 | ** onto overflow pages, then accessPayload() must be used to reassembly |
drh | 0e1c19e | 2004-05-11 00:58:56 +0000 | [diff] [blame] | 2871 | ** the key/data and copy it into a preallocated buffer. |
| 2872 | ** |
| 2873 | ** The pointer returned by this routine looks directly into the cached |
| 2874 | ** page of the database. The data might change or move the next time |
| 2875 | ** any btree routine is called. |
| 2876 | */ |
| 2877 | static const unsigned char *fetchPayload( |
| 2878 | BtCursor *pCur, /* Cursor pointing to entry to read from */ |
drh | e51c44f | 2004-05-30 20:46:09 +0000 | [diff] [blame] | 2879 | int *pAmt, /* Write the number of available bytes here */ |
drh | 0e1c19e | 2004-05-11 00:58:56 +0000 | [diff] [blame] | 2880 | int skipKey /* read beginning at data if this is true */ |
| 2881 | ){ |
| 2882 | unsigned char *aPayload; |
| 2883 | MemPage *pPage; |
drh | fa1a98a | 2004-05-14 19:08:17 +0000 | [diff] [blame] | 2884 | u32 nKey; |
| 2885 | int nLocal; |
drh | 0e1c19e | 2004-05-11 00:58:56 +0000 | [diff] [blame] | 2886 | |
| 2887 | assert( pCur!=0 && pCur->pPage!=0 ); |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 2888 | assert( pCur->eState==CURSOR_VALID ); |
drh | 0e1c19e | 2004-05-11 00:58:56 +0000 | [diff] [blame] | 2889 | pPage = pCur->pPage; |
drh | 0e1c19e | 2004-05-11 00:58:56 +0000 | [diff] [blame] | 2890 | assert( pCur->idx>=0 && pCur->idx<pPage->nCell ); |
danielk1977 | 1cc5ed8 | 2007-05-16 17:28:43 +0000 | [diff] [blame] | 2891 | GET_CELL_INFO(pCur); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 2892 | aPayload = pCur->info.pCell; |
drh | fa1a98a | 2004-05-14 19:08:17 +0000 | [diff] [blame] | 2893 | aPayload += pCur->info.nHeader; |
drh | 0e1c19e | 2004-05-11 00:58:56 +0000 | [diff] [blame] | 2894 | if( pPage->intKey ){ |
drh | fa1a98a | 2004-05-14 19:08:17 +0000 | [diff] [blame] | 2895 | nKey = 0; |
| 2896 | }else{ |
| 2897 | nKey = pCur->info.nKey; |
drh | 0e1c19e | 2004-05-11 00:58:56 +0000 | [diff] [blame] | 2898 | } |
drh | 0e1c19e | 2004-05-11 00:58:56 +0000 | [diff] [blame] | 2899 | if( skipKey ){ |
drh | fa1a98a | 2004-05-14 19:08:17 +0000 | [diff] [blame] | 2900 | aPayload += nKey; |
| 2901 | nLocal = pCur->info.nLocal - nKey; |
drh | 0e1c19e | 2004-05-11 00:58:56 +0000 | [diff] [blame] | 2902 | }else{ |
drh | fa1a98a | 2004-05-14 19:08:17 +0000 | [diff] [blame] | 2903 | nLocal = pCur->info.nLocal; |
drh | e51c44f | 2004-05-30 20:46:09 +0000 | [diff] [blame] | 2904 | if( nLocal>nKey ){ |
| 2905 | nLocal = nKey; |
| 2906 | } |
drh | 0e1c19e | 2004-05-11 00:58:56 +0000 | [diff] [blame] | 2907 | } |
drh | e51c44f | 2004-05-30 20:46:09 +0000 | [diff] [blame] | 2908 | *pAmt = nLocal; |
drh | 0e1c19e | 2004-05-11 00:58:56 +0000 | [diff] [blame] | 2909 | return aPayload; |
| 2910 | } |
| 2911 | |
| 2912 | |
| 2913 | /* |
drh | e51c44f | 2004-05-30 20:46:09 +0000 | [diff] [blame] | 2914 | ** For the entry that cursor pCur is point to, return as |
| 2915 | ** many bytes of the key or data as are available on the local |
| 2916 | ** b-tree page. Write the number of available bytes into *pAmt. |
drh | 0e1c19e | 2004-05-11 00:58:56 +0000 | [diff] [blame] | 2917 | ** |
| 2918 | ** The pointer returned is ephemeral. The key/data may move |
| 2919 | ** or be destroyed on the next call to any Btree routine. |
| 2920 | ** |
| 2921 | ** These routines is used to get quick access to key and data |
| 2922 | ** in the common case where no overflow pages are used. |
drh | 0e1c19e | 2004-05-11 00:58:56 +0000 | [diff] [blame] | 2923 | */ |
drh | e51c44f | 2004-05-30 20:46:09 +0000 | [diff] [blame] | 2924 | const void *sqlite3BtreeKeyFetch(BtCursor *pCur, int *pAmt){ |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 2925 | if( pCur->eState==CURSOR_VALID ){ |
| 2926 | return (const void*)fetchPayload(pCur, pAmt, 0); |
| 2927 | } |
| 2928 | return 0; |
drh | 0e1c19e | 2004-05-11 00:58:56 +0000 | [diff] [blame] | 2929 | } |
drh | e51c44f | 2004-05-30 20:46:09 +0000 | [diff] [blame] | 2930 | const void *sqlite3BtreeDataFetch(BtCursor *pCur, int *pAmt){ |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 2931 | if( pCur->eState==CURSOR_VALID ){ |
| 2932 | return (const void*)fetchPayload(pCur, pAmt, 1); |
| 2933 | } |
| 2934 | return 0; |
drh | 0e1c19e | 2004-05-11 00:58:56 +0000 | [diff] [blame] | 2935 | } |
| 2936 | |
| 2937 | |
| 2938 | /* |
drh | 8178a75 | 2003-01-05 21:41:40 +0000 | [diff] [blame] | 2939 | ** 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] | 2940 | ** page number of the child page to move to. |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 2941 | */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2942 | static int moveToChild(BtCursor *pCur, u32 newPgno){ |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 2943 | int rc; |
| 2944 | MemPage *pNewPage; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2945 | MemPage *pOldPage; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2946 | BtShared *pBt = pCur->pBtree->pBt; |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 2947 | |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 2948 | assert( pCur->eState==CURSOR_VALID ); |
drh | de64713 | 2004-05-07 17:57:49 +0000 | [diff] [blame] | 2949 | rc = getAndInitPage(pBt, newPgno, &pNewPage, pCur->pPage); |
drh | 6019e16 | 2001-07-02 17:51:45 +0000 | [diff] [blame] | 2950 | if( rc ) return rc; |
drh | 428ae8c | 2003-01-04 16:48:09 +0000 | [diff] [blame] | 2951 | pNewPage->idxParent = pCur->idx; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2952 | pOldPage = pCur->pPage; |
| 2953 | pOldPage->idxShift = 0; |
| 2954 | releasePage(pOldPage); |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 2955 | pCur->pPage = pNewPage; |
| 2956 | pCur->idx = 0; |
drh | 271efa5 | 2004-05-30 19:19:05 +0000 | [diff] [blame] | 2957 | pCur->info.nSize = 0; |
drh | 4be295b | 2003-12-16 03:44:47 +0000 | [diff] [blame] | 2958 | if( pNewPage->nCell<1 ){ |
drh | 4928570 | 2005-09-17 15:20:26 +0000 | [diff] [blame] | 2959 | return SQLITE_CORRUPT_BKPT; |
drh | 4be295b | 2003-12-16 03:44:47 +0000 | [diff] [blame] | 2960 | } |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 2961 | return SQLITE_OK; |
| 2962 | } |
| 2963 | |
| 2964 | /* |
drh | 8856d6a | 2004-04-29 14:42:46 +0000 | [diff] [blame] | 2965 | ** Return true if the page is the virtual root of its table. |
| 2966 | ** |
| 2967 | ** The virtual root page is the root page for most tables. But |
| 2968 | ** for the table rooted on page 1, sometime the real root page |
| 2969 | ** is empty except for the right-pointer. In such cases the |
| 2970 | ** virtual root page is the page that the right-pointer of page |
| 2971 | ** 1 is pointing to. |
| 2972 | */ |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 2973 | int sqlite3BtreeIsRootPage(MemPage *pPage){ |
drh | 8856d6a | 2004-04-29 14:42:46 +0000 | [diff] [blame] | 2974 | MemPage *pParent = pPage->pParent; |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 2975 | if( pParent==0 ) return 1; |
| 2976 | if( pParent->pgno>1 ) return 0; |
| 2977 | if( get2byte(&pParent->aData[pParent->hdrOffset+3])==0 ) return 1; |
drh | 8856d6a | 2004-04-29 14:42:46 +0000 | [diff] [blame] | 2978 | return 0; |
| 2979 | } |
| 2980 | |
| 2981 | /* |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 2982 | ** Move the cursor up to the parent page. |
| 2983 | ** |
| 2984 | ** pCur->idx is set to the cell index that contains the pointer |
| 2985 | ** to the page we are coming from. If we are coming from the |
| 2986 | ** right-most child page then pCur->idx is set to one more than |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 2987 | ** the largest cell index. |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 2988 | */ |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 2989 | void sqlite3BtreeMoveToParent(BtCursor *pCur){ |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 2990 | MemPage *pParent; |
drh | 8178a75 | 2003-01-05 21:41:40 +0000 | [diff] [blame] | 2991 | MemPage *pPage; |
drh | 428ae8c | 2003-01-04 16:48:09 +0000 | [diff] [blame] | 2992 | int idxParent; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2993 | |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 2994 | assert( pCur->eState==CURSOR_VALID ); |
drh | 8178a75 | 2003-01-05 21:41:40 +0000 | [diff] [blame] | 2995 | pPage = pCur->pPage; |
| 2996 | assert( pPage!=0 ); |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 2997 | assert( !sqlite3BtreeIsRootPage(pPage) ); |
drh | 8178a75 | 2003-01-05 21:41:40 +0000 | [diff] [blame] | 2998 | pParent = pPage->pParent; |
| 2999 | assert( pParent!=0 ); |
| 3000 | idxParent = pPage->idxParent; |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 3001 | sqlite3PagerRef(pParent->pDbPage); |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3002 | releasePage(pPage); |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 3003 | pCur->pPage = pParent; |
drh | 271efa5 | 2004-05-30 19:19:05 +0000 | [diff] [blame] | 3004 | pCur->info.nSize = 0; |
drh | 428ae8c | 2003-01-04 16:48:09 +0000 | [diff] [blame] | 3005 | assert( pParent->idxShift==0 ); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 3006 | pCur->idx = idxParent; |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 3007 | } |
| 3008 | |
| 3009 | /* |
| 3010 | ** Move the cursor to the root page |
| 3011 | */ |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 3012 | static int moveToRoot(BtCursor *pCur){ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3013 | MemPage *pRoot; |
drh | 777e4c4 | 2006-01-13 04:31:58 +0000 | [diff] [blame] | 3014 | int rc = SQLITE_OK; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 3015 | BtShared *pBt = pCur->pBtree->pBt; |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 3016 | |
drh | bf700f3 | 2007-03-31 02:36:44 +0000 | [diff] [blame] | 3017 | if( pCur->eState==CURSOR_REQUIRESEEK ){ |
| 3018 | clearCursorPosition(pCur); |
| 3019 | } |
drh | 777e4c4 | 2006-01-13 04:31:58 +0000 | [diff] [blame] | 3020 | pRoot = pCur->pPage; |
danielk1977 | 97a227c | 2006-01-20 16:32:04 +0000 | [diff] [blame] | 3021 | if( pRoot && pRoot->pgno==pCur->pgnoRoot ){ |
drh | 777e4c4 | 2006-01-13 04:31:58 +0000 | [diff] [blame] | 3022 | assert( pRoot->isInit ); |
| 3023 | }else{ |
| 3024 | if( |
| 3025 | SQLITE_OK!=(rc = getAndInitPage(pBt, pCur->pgnoRoot, &pRoot, 0)) |
| 3026 | ){ |
| 3027 | pCur->eState = CURSOR_INVALID; |
| 3028 | return rc; |
| 3029 | } |
| 3030 | releasePage(pCur->pPage); |
drh | 777e4c4 | 2006-01-13 04:31:58 +0000 | [diff] [blame] | 3031 | pCur->pPage = pRoot; |
drh | c39e000 | 2004-05-07 23:50:57 +0000 | [diff] [blame] | 3032 | } |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 3033 | pCur->idx = 0; |
drh | 271efa5 | 2004-05-30 19:19:05 +0000 | [diff] [blame] | 3034 | pCur->info.nSize = 0; |
drh | 8856d6a | 2004-04-29 14:42:46 +0000 | [diff] [blame] | 3035 | if( pRoot->nCell==0 && !pRoot->leaf ){ |
| 3036 | Pgno subpage; |
| 3037 | assert( pRoot->pgno==1 ); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 3038 | subpage = get4byte(&pRoot->aData[pRoot->hdrOffset+8]); |
drh | 8856d6a | 2004-04-29 14:42:46 +0000 | [diff] [blame] | 3039 | assert( subpage>0 ); |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 3040 | pCur->eState = CURSOR_VALID; |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 3041 | rc = moveToChild(pCur, subpage); |
drh | 8856d6a | 2004-04-29 14:42:46 +0000 | [diff] [blame] | 3042 | } |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 3043 | pCur->eState = ((pCur->pPage->nCell>0)?CURSOR_VALID:CURSOR_INVALID); |
drh | 8856d6a | 2004-04-29 14:42:46 +0000 | [diff] [blame] | 3044 | return rc; |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 3045 | } |
drh | 2af926b | 2001-05-15 00:39:25 +0000 | [diff] [blame] | 3046 | |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 3047 | /* |
| 3048 | ** Move the cursor down to the left-most leaf entry beneath the |
| 3049 | ** entry to which it is currently pointing. |
drh | 777e4c4 | 2006-01-13 04:31:58 +0000 | [diff] [blame] | 3050 | ** |
| 3051 | ** The left-most leaf is the one with the smallest key - the first |
| 3052 | ** in ascending order. |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 3053 | */ |
| 3054 | static int moveToLeftmost(BtCursor *pCur){ |
| 3055 | Pgno pgno; |
| 3056 | int rc; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3057 | MemPage *pPage; |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 3058 | |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 3059 | assert( pCur->eState==CURSOR_VALID ); |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3060 | while( !(pPage = pCur->pPage)->leaf ){ |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 3061 | assert( pCur->idx>=0 && pCur->idx<pPage->nCell ); |
danielk1977 | 1cc5ed8 | 2007-05-16 17:28:43 +0000 | [diff] [blame] | 3062 | pgno = get4byte(findCell(pPage, pCur->idx)); |
drh | 8178a75 | 2003-01-05 21:41:40 +0000 | [diff] [blame] | 3063 | rc = moveToChild(pCur, pgno); |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 3064 | if( rc ) return rc; |
| 3065 | } |
| 3066 | return SQLITE_OK; |
| 3067 | } |
| 3068 | |
drh | 2dcc9aa | 2002-12-04 13:40:25 +0000 | [diff] [blame] | 3069 | /* |
| 3070 | ** Move the cursor down to the right-most leaf entry beneath the |
| 3071 | ** page to which it is currently pointing. Notice the difference |
| 3072 | ** between moveToLeftmost() and moveToRightmost(). moveToLeftmost() |
| 3073 | ** finds the left-most entry beneath the *entry* whereas moveToRightmost() |
| 3074 | ** finds the right-most entry beneath the *page*. |
drh | 777e4c4 | 2006-01-13 04:31:58 +0000 | [diff] [blame] | 3075 | ** |
| 3076 | ** The right-most entry is the one with the largest key - the last |
| 3077 | ** key in ascending order. |
drh | 2dcc9aa | 2002-12-04 13:40:25 +0000 | [diff] [blame] | 3078 | */ |
| 3079 | static int moveToRightmost(BtCursor *pCur){ |
| 3080 | Pgno pgno; |
| 3081 | int rc; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3082 | MemPage *pPage; |
drh | 2dcc9aa | 2002-12-04 13:40:25 +0000 | [diff] [blame] | 3083 | |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 3084 | assert( pCur->eState==CURSOR_VALID ); |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3085 | while( !(pPage = pCur->pPage)->leaf ){ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 3086 | pgno = get4byte(&pPage->aData[pPage->hdrOffset+8]); |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3087 | pCur->idx = pPage->nCell; |
drh | 8178a75 | 2003-01-05 21:41:40 +0000 | [diff] [blame] | 3088 | rc = moveToChild(pCur, pgno); |
drh | 2dcc9aa | 2002-12-04 13:40:25 +0000 | [diff] [blame] | 3089 | if( rc ) return rc; |
| 3090 | } |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3091 | pCur->idx = pPage->nCell - 1; |
drh | 271efa5 | 2004-05-30 19:19:05 +0000 | [diff] [blame] | 3092 | pCur->info.nSize = 0; |
drh | 2dcc9aa | 2002-12-04 13:40:25 +0000 | [diff] [blame] | 3093 | return SQLITE_OK; |
| 3094 | } |
| 3095 | |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3096 | /* Move the cursor to the first entry in the table. Return SQLITE_OK |
| 3097 | ** on success. Set *pRes to 0 if the cursor actually points to something |
drh | 77c679c | 2002-02-19 22:43:58 +0000 | [diff] [blame] | 3098 | ** or set *pRes to 1 if the table is empty. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3099 | */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3100 | int sqlite3BtreeFirst(BtCursor *pCur, int *pRes){ |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3101 | int rc; |
| 3102 | rc = moveToRoot(pCur); |
| 3103 | if( rc ) return rc; |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 3104 | if( pCur->eState==CURSOR_INVALID ){ |
drh | c39e000 | 2004-05-07 23:50:57 +0000 | [diff] [blame] | 3105 | assert( pCur->pPage->nCell==0 ); |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3106 | *pRes = 1; |
| 3107 | return SQLITE_OK; |
| 3108 | } |
drh | c39e000 | 2004-05-07 23:50:57 +0000 | [diff] [blame] | 3109 | assert( pCur->pPage->nCell>0 ); |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3110 | *pRes = 0; |
| 3111 | rc = moveToLeftmost(pCur); |
| 3112 | return rc; |
| 3113 | } |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 3114 | |
drh | 9562b55 | 2002-02-19 15:00:07 +0000 | [diff] [blame] | 3115 | /* Move the cursor to the last entry in the table. Return SQLITE_OK |
| 3116 | ** on success. Set *pRes to 0 if the cursor actually points to something |
drh | 77c679c | 2002-02-19 22:43:58 +0000 | [diff] [blame] | 3117 | ** or set *pRes to 1 if the table is empty. |
drh | 9562b55 | 2002-02-19 15:00:07 +0000 | [diff] [blame] | 3118 | */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3119 | int sqlite3BtreeLast(BtCursor *pCur, int *pRes){ |
drh | 9562b55 | 2002-02-19 15:00:07 +0000 | [diff] [blame] | 3120 | int rc; |
drh | 9562b55 | 2002-02-19 15:00:07 +0000 | [diff] [blame] | 3121 | rc = moveToRoot(pCur); |
| 3122 | if( rc ) return rc; |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 3123 | if( CURSOR_INVALID==pCur->eState ){ |
drh | c39e000 | 2004-05-07 23:50:57 +0000 | [diff] [blame] | 3124 | assert( pCur->pPage->nCell==0 ); |
drh | 9562b55 | 2002-02-19 15:00:07 +0000 | [diff] [blame] | 3125 | *pRes = 1; |
| 3126 | return SQLITE_OK; |
| 3127 | } |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 3128 | assert( pCur->eState==CURSOR_VALID ); |
drh | 9562b55 | 2002-02-19 15:00:07 +0000 | [diff] [blame] | 3129 | *pRes = 0; |
drh | 2dcc9aa | 2002-12-04 13:40:25 +0000 | [diff] [blame] | 3130 | rc = moveToRightmost(pCur); |
drh | 9562b55 | 2002-02-19 15:00:07 +0000 | [diff] [blame] | 3131 | return rc; |
| 3132 | } |
| 3133 | |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3134 | /* Move the cursor so that it points to an entry near pKey/nKey. |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 3135 | ** Return a success code. |
| 3136 | ** |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3137 | ** For INTKEY tables, only the nKey parameter is used. pKey is |
| 3138 | ** ignored. For other tables, nKey is the number of bytes of data |
drh | 0b2f316 | 2005-12-21 18:36:45 +0000 | [diff] [blame] | 3139 | ** in pKey. The comparison function specified when the cursor was |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3140 | ** created is used to compare keys. |
| 3141 | ** |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 3142 | ** If an exact match is not found, then the cursor is always |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 3143 | ** left pointing at a leaf page which would hold the entry if it |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 3144 | ** were present. The cursor might point to an entry that comes |
| 3145 | ** before or after the key. |
| 3146 | ** |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 3147 | ** The result of comparing the key with the entry to which the |
drh | ab01f61 | 2004-05-22 02:55:23 +0000 | [diff] [blame] | 3148 | ** cursor is written to *pRes if pRes!=NULL. The meaning of |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 3149 | ** this value is as follows: |
| 3150 | ** |
| 3151 | ** *pRes<0 The cursor is left pointing at an entry that |
drh | 1a844c3 | 2002-12-04 22:29:28 +0000 | [diff] [blame] | 3152 | ** is smaller than pKey or if the table is empty |
| 3153 | ** and the cursor is therefore left point to nothing. |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 3154 | ** |
| 3155 | ** *pRes==0 The cursor is left pointing at an entry that |
| 3156 | ** exactly matches pKey. |
| 3157 | ** |
| 3158 | ** *pRes>0 The cursor is left pointing at an entry that |
drh | 7c717f7 | 2001-06-24 20:39:41 +0000 | [diff] [blame] | 3159 | ** is larger than pKey. |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 3160 | */ |
drh | e4d9081 | 2007-03-29 05:51:49 +0000 | [diff] [blame] | 3161 | int sqlite3BtreeMoveto( |
| 3162 | BtCursor *pCur, /* The cursor to be moved */ |
| 3163 | const void *pKey, /* The key content for indices. Not used by tables */ |
| 3164 | i64 nKey, /* Size of pKey. Or the key for tables */ |
| 3165 | int biasRight, /* If true, bias the search to the high end */ |
| 3166 | int *pRes /* Search result flag */ |
| 3167 | ){ |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 3168 | int rc; |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 3169 | rc = moveToRoot(pCur); |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 3170 | if( rc ) return rc; |
drh | c39e000 | 2004-05-07 23:50:57 +0000 | [diff] [blame] | 3171 | assert( pCur->pPage ); |
| 3172 | assert( pCur->pPage->isInit ); |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 3173 | if( pCur->eState==CURSOR_INVALID ){ |
drh | f328bc8 | 2004-05-10 23:29:49 +0000 | [diff] [blame] | 3174 | *pRes = -1; |
drh | c39e000 | 2004-05-07 23:50:57 +0000 | [diff] [blame] | 3175 | assert( pCur->pPage->nCell==0 ); |
| 3176 | return SQLITE_OK; |
| 3177 | } |
drh | 1468438 | 2006-11-30 13:05:29 +0000 | [diff] [blame] | 3178 | for(;;){ |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 3179 | int lwr, upr; |
| 3180 | Pgno chldPg; |
| 3181 | MemPage *pPage = pCur->pPage; |
drh | 1a844c3 | 2002-12-04 22:29:28 +0000 | [diff] [blame] | 3182 | int c = -1; /* pRes return if table is empty must be -1 */ |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 3183 | lwr = 0; |
| 3184 | upr = pPage->nCell-1; |
drh | 4eec4c1 | 2005-01-21 00:22:37 +0000 | [diff] [blame] | 3185 | if( !pPage->intKey && pKey==0 ){ |
drh | 4928570 | 2005-09-17 15:20:26 +0000 | [diff] [blame] | 3186 | return SQLITE_CORRUPT_BKPT; |
drh | 4eec4c1 | 2005-01-21 00:22:37 +0000 | [diff] [blame] | 3187 | } |
drh | e4d9081 | 2007-03-29 05:51:49 +0000 | [diff] [blame] | 3188 | if( biasRight ){ |
| 3189 | pCur->idx = upr; |
| 3190 | }else{ |
| 3191 | pCur->idx = (upr+lwr)/2; |
| 3192 | } |
drh | f1d68b3 | 2007-03-29 04:43:26 +0000 | [diff] [blame] | 3193 | if( lwr<=upr ) for(;;){ |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 3194 | void *pCellKey; |
drh | 4a1c380 | 2004-05-12 15:15:47 +0000 | [diff] [blame] | 3195 | i64 nCellKey; |
drh | 366fda6 | 2006-01-13 02:35:09 +0000 | [diff] [blame] | 3196 | pCur->info.nSize = 0; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3197 | if( pPage->intKey ){ |
drh | 777e4c4 | 2006-01-13 04:31:58 +0000 | [diff] [blame] | 3198 | u8 *pCell; |
danielk1977 | 1cc5ed8 | 2007-05-16 17:28:43 +0000 | [diff] [blame] | 3199 | pCell = findCell(pPage, pCur->idx) + pPage->childPtrSize; |
drh | d172f86 | 2006-01-12 15:01:15 +0000 | [diff] [blame] | 3200 | if( pPage->hasData ){ |
danielk1977 | bab45c6 | 2006-01-16 15:14:27 +0000 | [diff] [blame] | 3201 | u32 dummy; |
drh | d172f86 | 2006-01-12 15:01:15 +0000 | [diff] [blame] | 3202 | pCell += getVarint32(pCell, &dummy); |
| 3203 | } |
danielk1977 | bab45c6 | 2006-01-16 15:14:27 +0000 | [diff] [blame] | 3204 | getVarint(pCell, (u64 *)&nCellKey); |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3205 | if( nCellKey<nKey ){ |
| 3206 | c = -1; |
| 3207 | }else if( nCellKey>nKey ){ |
| 3208 | c = +1; |
| 3209 | }else{ |
| 3210 | c = 0; |
| 3211 | } |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3212 | }else{ |
drh | e51c44f | 2004-05-30 20:46:09 +0000 | [diff] [blame] | 3213 | int available; |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 3214 | pCellKey = (void *)fetchPayload(pCur, &available, 0); |
drh | 366fda6 | 2006-01-13 02:35:09 +0000 | [diff] [blame] | 3215 | nCellKey = pCur->info.nKey; |
drh | e51c44f | 2004-05-30 20:46:09 +0000 | [diff] [blame] | 3216 | if( available>=nCellKey ){ |
| 3217 | c = pCur->xCompare(pCur->pArg, nCellKey, pCellKey, nKey, pKey); |
| 3218 | }else{ |
| 3219 | pCellKey = sqliteMallocRaw( nCellKey ); |
| 3220 | if( pCellKey==0 ) return SQLITE_NOMEM; |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 3221 | rc = sqlite3BtreeKey(pCur, 0, nCellKey, (void *)pCellKey); |
drh | e51c44f | 2004-05-30 20:46:09 +0000 | [diff] [blame] | 3222 | c = pCur->xCompare(pCur->pArg, nCellKey, pCellKey, nKey, pKey); |
| 3223 | sqliteFree(pCellKey); |
| 3224 | if( rc ) return rc; |
| 3225 | } |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3226 | } |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 3227 | if( c==0 ){ |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 3228 | if( pPage->leafData && !pPage->leaf ){ |
drh | fc70e6f | 2004-05-12 21:11:27 +0000 | [diff] [blame] | 3229 | lwr = pCur->idx; |
| 3230 | upr = lwr - 1; |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 3231 | break; |
| 3232 | }else{ |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 3233 | if( pRes ) *pRes = 0; |
| 3234 | return SQLITE_OK; |
| 3235 | } |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 3236 | } |
| 3237 | if( c<0 ){ |
| 3238 | lwr = pCur->idx+1; |
| 3239 | }else{ |
| 3240 | upr = pCur->idx-1; |
| 3241 | } |
drh | f1d68b3 | 2007-03-29 04:43:26 +0000 | [diff] [blame] | 3242 | if( lwr>upr ){ |
| 3243 | break; |
| 3244 | } |
| 3245 | pCur->idx = (lwr+upr)/2; |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 3246 | } |
| 3247 | assert( lwr==upr+1 ); |
drh | 7aa128d | 2002-06-21 13:09:16 +0000 | [diff] [blame] | 3248 | assert( pPage->isInit ); |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3249 | if( pPage->leaf ){ |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 3250 | chldPg = 0; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3251 | }else if( lwr>=pPage->nCell ){ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 3252 | chldPg = get4byte(&pPage->aData[pPage->hdrOffset+8]); |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 3253 | }else{ |
danielk1977 | 1cc5ed8 | 2007-05-16 17:28:43 +0000 | [diff] [blame] | 3254 | chldPg = get4byte(findCell(pPage, lwr)); |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 3255 | } |
| 3256 | if( chldPg==0 ){ |
drh | c39e000 | 2004-05-07 23:50:57 +0000 | [diff] [blame] | 3257 | assert( pCur->idx>=0 && pCur->idx<pCur->pPage->nCell ); |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 3258 | if( pRes ) *pRes = c; |
| 3259 | return SQLITE_OK; |
| 3260 | } |
drh | 428ae8c | 2003-01-04 16:48:09 +0000 | [diff] [blame] | 3261 | pCur->idx = lwr; |
drh | 271efa5 | 2004-05-30 19:19:05 +0000 | [diff] [blame] | 3262 | pCur->info.nSize = 0; |
drh | 8178a75 | 2003-01-05 21:41:40 +0000 | [diff] [blame] | 3263 | rc = moveToChild(pCur, chldPg); |
drh | c39e000 | 2004-05-07 23:50:57 +0000 | [diff] [blame] | 3264 | if( rc ){ |
| 3265 | return rc; |
| 3266 | } |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 3267 | } |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 3268 | /* NOT REACHED */ |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 3269 | } |
| 3270 | |
| 3271 | /* |
drh | c39e000 | 2004-05-07 23:50:57 +0000 | [diff] [blame] | 3272 | ** Return TRUE if the cursor is not pointing at an entry of the table. |
| 3273 | ** |
| 3274 | ** TRUE will be returned after a call to sqlite3BtreeNext() moves |
| 3275 | ** past the last entry in the table or sqlite3BtreePrev() moves past |
| 3276 | ** the first entry. TRUE is also returned if the table is empty. |
| 3277 | */ |
| 3278 | int sqlite3BtreeEof(BtCursor *pCur){ |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 3279 | /* TODO: What if the cursor is in CURSOR_REQUIRESEEK but all table entries |
| 3280 | ** have been deleted? This API will need to change to return an error code |
| 3281 | ** as well as the boolean result value. |
| 3282 | */ |
| 3283 | return (CURSOR_VALID!=pCur->eState); |
drh | c39e000 | 2004-05-07 23:50:57 +0000 | [diff] [blame] | 3284 | } |
| 3285 | |
| 3286 | /* |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 3287 | ** Advance the cursor to the next entry in the database. If |
drh | 8c1238a | 2003-01-02 14:43:55 +0000 | [diff] [blame] | 3288 | ** successful then set *pRes=0. If the cursor |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 3289 | ** was already pointing to the last entry in the database before |
drh | 8c1238a | 2003-01-02 14:43:55 +0000 | [diff] [blame] | 3290 | ** this routine was called, then set *pRes=1. |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 3291 | */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3292 | int sqlite3BtreeNext(BtCursor *pCur, int *pRes){ |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 3293 | int rc; |
danielk1977 | 97a227c | 2006-01-20 16:32:04 +0000 | [diff] [blame] | 3294 | MemPage *pPage; |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 3295 | |
drh | bf700f3 | 2007-03-31 02:36:44 +0000 | [diff] [blame] | 3296 | rc = restoreOrClearCursorPosition(pCur); |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 3297 | if( rc!=SQLITE_OK ){ |
| 3298 | return rc; |
| 3299 | } |
drh | 8c4d3a6 | 2007-04-06 01:03:32 +0000 | [diff] [blame] | 3300 | assert( pRes!=0 ); |
| 3301 | pPage = pCur->pPage; |
| 3302 | if( CURSOR_INVALID==pCur->eState ){ |
| 3303 | *pRes = 1; |
| 3304 | return SQLITE_OK; |
| 3305 | } |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 3306 | if( pCur->skip>0 ){ |
| 3307 | pCur->skip = 0; |
| 3308 | *pRes = 0; |
| 3309 | return SQLITE_OK; |
| 3310 | } |
| 3311 | pCur->skip = 0; |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 3312 | |
drh | 8178a75 | 2003-01-05 21:41:40 +0000 | [diff] [blame] | 3313 | assert( pPage->isInit ); |
drh | 8178a75 | 2003-01-05 21:41:40 +0000 | [diff] [blame] | 3314 | assert( pCur->idx<pPage->nCell ); |
danielk1977 | 6a43f9b | 2004-11-16 04:57:24 +0000 | [diff] [blame] | 3315 | |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 3316 | pCur->idx++; |
drh | 271efa5 | 2004-05-30 19:19:05 +0000 | [diff] [blame] | 3317 | pCur->info.nSize = 0; |
drh | 8178a75 | 2003-01-05 21:41:40 +0000 | [diff] [blame] | 3318 | if( pCur->idx>=pPage->nCell ){ |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 3319 | if( !pPage->leaf ){ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 3320 | rc = moveToChild(pCur, get4byte(&pPage->aData[pPage->hdrOffset+8])); |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 3321 | if( rc ) return rc; |
| 3322 | rc = moveToLeftmost(pCur); |
drh | 8c1238a | 2003-01-02 14:43:55 +0000 | [diff] [blame] | 3323 | *pRes = 0; |
| 3324 | return rc; |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 3325 | } |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 3326 | do{ |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 3327 | if( sqlite3BtreeIsRootPage(pPage) ){ |
drh | 8c1238a | 2003-01-02 14:43:55 +0000 | [diff] [blame] | 3328 | *pRes = 1; |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 3329 | pCur->eState = CURSOR_INVALID; |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 3330 | return SQLITE_OK; |
| 3331 | } |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 3332 | sqlite3BtreeMoveToParent(pCur); |
drh | 8178a75 | 2003-01-05 21:41:40 +0000 | [diff] [blame] | 3333 | pPage = pCur->pPage; |
| 3334 | }while( pCur->idx>=pPage->nCell ); |
drh | 8c1238a | 2003-01-02 14:43:55 +0000 | [diff] [blame] | 3335 | *pRes = 0; |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 3336 | if( pPage->leafData ){ |
| 3337 | rc = sqlite3BtreeNext(pCur, pRes); |
| 3338 | }else{ |
| 3339 | rc = SQLITE_OK; |
| 3340 | } |
| 3341 | return rc; |
drh | 8178a75 | 2003-01-05 21:41:40 +0000 | [diff] [blame] | 3342 | } |
| 3343 | *pRes = 0; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3344 | if( pPage->leaf ){ |
drh | 8178a75 | 2003-01-05 21:41:40 +0000 | [diff] [blame] | 3345 | return SQLITE_OK; |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 3346 | } |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 3347 | rc = moveToLeftmost(pCur); |
drh | 8c1238a | 2003-01-02 14:43:55 +0000 | [diff] [blame] | 3348 | return rc; |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 3349 | } |
| 3350 | |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 3351 | /* |
drh | 2dcc9aa | 2002-12-04 13:40:25 +0000 | [diff] [blame] | 3352 | ** 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] | 3353 | ** successful then set *pRes=0. If the cursor |
drh | 2dcc9aa | 2002-12-04 13:40:25 +0000 | [diff] [blame] | 3354 | ** was already pointing to the first entry in the database before |
drh | 8178a75 | 2003-01-05 21:41:40 +0000 | [diff] [blame] | 3355 | ** this routine was called, then set *pRes=1. |
drh | 2dcc9aa | 2002-12-04 13:40:25 +0000 | [diff] [blame] | 3356 | */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3357 | int sqlite3BtreePrevious(BtCursor *pCur, int *pRes){ |
drh | 2dcc9aa | 2002-12-04 13:40:25 +0000 | [diff] [blame] | 3358 | int rc; |
| 3359 | Pgno pgno; |
drh | 8178a75 | 2003-01-05 21:41:40 +0000 | [diff] [blame] | 3360 | MemPage *pPage; |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 3361 | |
drh | bf700f3 | 2007-03-31 02:36:44 +0000 | [diff] [blame] | 3362 | rc = restoreOrClearCursorPosition(pCur); |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 3363 | if( rc!=SQLITE_OK ){ |
| 3364 | return rc; |
| 3365 | } |
drh | 8c4d3a6 | 2007-04-06 01:03:32 +0000 | [diff] [blame] | 3366 | if( CURSOR_INVALID==pCur->eState ){ |
| 3367 | *pRes = 1; |
| 3368 | return SQLITE_OK; |
| 3369 | } |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 3370 | if( pCur->skip<0 ){ |
| 3371 | pCur->skip = 0; |
| 3372 | *pRes = 0; |
| 3373 | return SQLITE_OK; |
| 3374 | } |
| 3375 | pCur->skip = 0; |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 3376 | |
drh | 8178a75 | 2003-01-05 21:41:40 +0000 | [diff] [blame] | 3377 | pPage = pCur->pPage; |
drh | 8178a75 | 2003-01-05 21:41:40 +0000 | [diff] [blame] | 3378 | assert( pPage->isInit ); |
drh | 2dcc9aa | 2002-12-04 13:40:25 +0000 | [diff] [blame] | 3379 | assert( pCur->idx>=0 ); |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 3380 | if( !pPage->leaf ){ |
danielk1977 | 1cc5ed8 | 2007-05-16 17:28:43 +0000 | [diff] [blame] | 3381 | pgno = get4byte( findCell(pPage, pCur->idx) ); |
drh | 8178a75 | 2003-01-05 21:41:40 +0000 | [diff] [blame] | 3382 | rc = moveToChild(pCur, pgno); |
drh | 2dcc9aa | 2002-12-04 13:40:25 +0000 | [diff] [blame] | 3383 | if( rc ) return rc; |
| 3384 | rc = moveToRightmost(pCur); |
| 3385 | }else{ |
| 3386 | while( pCur->idx==0 ){ |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 3387 | if( sqlite3BtreeIsRootPage(pPage) ){ |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 3388 | pCur->eState = CURSOR_INVALID; |
drh | c39e000 | 2004-05-07 23:50:57 +0000 | [diff] [blame] | 3389 | *pRes = 1; |
drh | 2dcc9aa | 2002-12-04 13:40:25 +0000 | [diff] [blame] | 3390 | return SQLITE_OK; |
| 3391 | } |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 3392 | sqlite3BtreeMoveToParent(pCur); |
drh | 8178a75 | 2003-01-05 21:41:40 +0000 | [diff] [blame] | 3393 | pPage = pCur->pPage; |
drh | 2dcc9aa | 2002-12-04 13:40:25 +0000 | [diff] [blame] | 3394 | } |
| 3395 | pCur->idx--; |
drh | 271efa5 | 2004-05-30 19:19:05 +0000 | [diff] [blame] | 3396 | pCur->info.nSize = 0; |
drh | 8237d45 | 2004-11-22 19:07:09 +0000 | [diff] [blame] | 3397 | if( pPage->leafData && !pPage->leaf ){ |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 3398 | rc = sqlite3BtreePrevious(pCur, pRes); |
| 3399 | }else{ |
| 3400 | rc = SQLITE_OK; |
| 3401 | } |
drh | 2dcc9aa | 2002-12-04 13:40:25 +0000 | [diff] [blame] | 3402 | } |
drh | 8178a75 | 2003-01-05 21:41:40 +0000 | [diff] [blame] | 3403 | *pRes = 0; |
drh | 2dcc9aa | 2002-12-04 13:40:25 +0000 | [diff] [blame] | 3404 | return rc; |
| 3405 | } |
| 3406 | |
| 3407 | /* |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 3408 | ** Allocate a new page from the database file. |
| 3409 | ** |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 3410 | ** The new page is marked as dirty. (In other words, sqlite3PagerWrite() |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 3411 | ** has already been called on the new page.) The new page has also |
| 3412 | ** been referenced and the calling routine is responsible for calling |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 3413 | ** sqlite3PagerUnref() on the new page when it is done. |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 3414 | ** |
| 3415 | ** SQLITE_OK is returned on success. Any other return value indicates |
| 3416 | ** an error. *ppPage and *pPgno are undefined in the event of an error. |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 3417 | ** Do not invoke sqlite3PagerUnref() on *ppPage if an error is returned. |
drh | bea00b9 | 2002-07-08 10:59:50 +0000 | [diff] [blame] | 3418 | ** |
drh | 199e3cf | 2002-07-18 11:01:47 +0000 | [diff] [blame] | 3419 | ** If the "nearby" parameter is not 0, then a (feeble) effort is made to |
| 3420 | ** 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] | 3421 | ** attempt to keep related pages close to each other in the database file, |
| 3422 | ** which in turn can make database access faster. |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 3423 | ** |
| 3424 | ** If the "exact" parameter is not 0, and the page-number nearby exists |
| 3425 | ** anywhere on the free-list, then it is guarenteed to be returned. This |
| 3426 | ** is only used by auto-vacuum databases when allocating a new table. |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 3427 | */ |
drh | 4f0c587 | 2007-03-26 22:05:01 +0000 | [diff] [blame] | 3428 | static int allocateBtreePage( |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 3429 | BtShared *pBt, |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 3430 | MemPage **ppPage, |
| 3431 | Pgno *pPgno, |
| 3432 | Pgno nearby, |
| 3433 | u8 exact |
| 3434 | ){ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3435 | MemPage *pPage1; |
drh | 8c42ca9 | 2001-06-22 19:15:00 +0000 | [diff] [blame] | 3436 | int rc; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3437 | int n; /* Number of pages on the freelist */ |
| 3438 | int k; /* Number of leaves on the trunk of the freelist */ |
drh | d3627af | 2006-12-18 18:34:51 +0000 | [diff] [blame] | 3439 | MemPage *pTrunk = 0; |
| 3440 | MemPage *pPrevTrunk = 0; |
drh | 30e5875 | 2002-03-02 20:41:57 +0000 | [diff] [blame] | 3441 | |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3442 | pPage1 = pBt->pPage1; |
| 3443 | n = get4byte(&pPage1->aData[36]); |
| 3444 | if( n>0 ){ |
drh | 9102529 | 2004-05-03 19:49:32 +0000 | [diff] [blame] | 3445 | /* There are pages on the freelist. Reuse one of those pages. */ |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 3446 | Pgno iTrunk; |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 3447 | u8 searchList = 0; /* If the free-list must be searched for 'nearby' */ |
| 3448 | |
| 3449 | /* If the 'exact' parameter was true and a query of the pointer-map |
| 3450 | ** shows that the page 'nearby' is somewhere on the free-list, then |
| 3451 | ** the entire-list will be searched for that page. |
| 3452 | */ |
| 3453 | #ifndef SQLITE_OMIT_AUTOVACUUM |
| 3454 | if( exact ){ |
| 3455 | u8 eType; |
| 3456 | assert( nearby>0 ); |
| 3457 | assert( pBt->autoVacuum ); |
| 3458 | rc = ptrmapGet(pBt, nearby, &eType, 0); |
| 3459 | if( rc ) return rc; |
| 3460 | if( eType==PTRMAP_FREEPAGE ){ |
| 3461 | searchList = 1; |
| 3462 | } |
| 3463 | *pPgno = nearby; |
| 3464 | } |
| 3465 | #endif |
| 3466 | |
| 3467 | /* Decrement the free-list count by 1. Set iTrunk to the index of the |
| 3468 | ** first free-list trunk page. iPrevTrunk is initially 1. |
| 3469 | */ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 3470 | rc = sqlite3PagerWrite(pPage1->pDbPage); |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 3471 | if( rc ) return rc; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3472 | put4byte(&pPage1->aData[36], n-1); |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 3473 | |
| 3474 | /* The code within this loop is run only once if the 'searchList' variable |
| 3475 | ** is not true. Otherwise, it runs once for each trunk-page on the |
| 3476 | ** free-list until the page 'nearby' is located. |
| 3477 | */ |
| 3478 | do { |
| 3479 | pPrevTrunk = pTrunk; |
| 3480 | if( pPrevTrunk ){ |
| 3481 | iTrunk = get4byte(&pPrevTrunk->aData[0]); |
drh | bea00b9 | 2002-07-08 10:59:50 +0000 | [diff] [blame] | 3482 | }else{ |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 3483 | iTrunk = get4byte(&pPage1->aData[32]); |
drh | bea00b9 | 2002-07-08 10:59:50 +0000 | [diff] [blame] | 3484 | } |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 3485 | rc = sqlite3BtreeGetPage(pBt, iTrunk, &pTrunk, 0); |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 3486 | if( rc ){ |
drh | d3627af | 2006-12-18 18:34:51 +0000 | [diff] [blame] | 3487 | pTrunk = 0; |
| 3488 | goto end_allocate_page; |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 3489 | } |
| 3490 | |
| 3491 | k = get4byte(&pTrunk->aData[4]); |
| 3492 | if( k==0 && !searchList ){ |
| 3493 | /* The trunk has no leaves and the list is not being searched. |
| 3494 | ** So extract the trunk page itself and use it as the newly |
| 3495 | ** allocated page */ |
| 3496 | assert( pPrevTrunk==0 ); |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 3497 | rc = sqlite3PagerWrite(pTrunk->pDbPage); |
drh | d3627af | 2006-12-18 18:34:51 +0000 | [diff] [blame] | 3498 | if( rc ){ |
| 3499 | goto end_allocate_page; |
| 3500 | } |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 3501 | *pPgno = iTrunk; |
| 3502 | memcpy(&pPage1->aData[32], &pTrunk->aData[0], 4); |
| 3503 | *ppPage = pTrunk; |
| 3504 | pTrunk = 0; |
| 3505 | TRACE(("ALLOCATE: %d trunk - %d free pages left\n", *pPgno, n-1)); |
| 3506 | }else if( k>pBt->usableSize/4 - 8 ){ |
| 3507 | /* Value of k is out of range. Database corruption */ |
drh | d3627af | 2006-12-18 18:34:51 +0000 | [diff] [blame] | 3508 | rc = SQLITE_CORRUPT_BKPT; |
| 3509 | goto end_allocate_page; |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 3510 | #ifndef SQLITE_OMIT_AUTOVACUUM |
| 3511 | }else if( searchList && nearby==iTrunk ){ |
| 3512 | /* The list is being searched and this trunk page is the page |
| 3513 | ** to allocate, regardless of whether it has leaves. |
| 3514 | */ |
| 3515 | assert( *pPgno==iTrunk ); |
| 3516 | *ppPage = pTrunk; |
| 3517 | searchList = 0; |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 3518 | rc = sqlite3PagerWrite(pTrunk->pDbPage); |
drh | d3627af | 2006-12-18 18:34:51 +0000 | [diff] [blame] | 3519 | if( rc ){ |
| 3520 | goto end_allocate_page; |
| 3521 | } |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 3522 | if( k==0 ){ |
| 3523 | if( !pPrevTrunk ){ |
| 3524 | memcpy(&pPage1->aData[32], &pTrunk->aData[0], 4); |
| 3525 | }else{ |
| 3526 | memcpy(&pPrevTrunk->aData[0], &pTrunk->aData[0], 4); |
| 3527 | } |
| 3528 | }else{ |
| 3529 | /* The trunk page is required by the caller but it contains |
| 3530 | ** pointers to free-list leaves. The first leaf becomes a trunk |
| 3531 | ** page in this case. |
| 3532 | */ |
| 3533 | MemPage *pNewTrunk; |
| 3534 | Pgno iNewTrunk = get4byte(&pTrunk->aData[8]); |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 3535 | rc = sqlite3BtreeGetPage(pBt, iNewTrunk, &pNewTrunk, 0); |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 3536 | if( rc!=SQLITE_OK ){ |
drh | d3627af | 2006-12-18 18:34:51 +0000 | [diff] [blame] | 3537 | goto end_allocate_page; |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 3538 | } |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 3539 | rc = sqlite3PagerWrite(pNewTrunk->pDbPage); |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 3540 | if( rc!=SQLITE_OK ){ |
| 3541 | releasePage(pNewTrunk); |
drh | d3627af | 2006-12-18 18:34:51 +0000 | [diff] [blame] | 3542 | goto end_allocate_page; |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 3543 | } |
| 3544 | memcpy(&pNewTrunk->aData[0], &pTrunk->aData[0], 4); |
| 3545 | put4byte(&pNewTrunk->aData[4], k-1); |
| 3546 | memcpy(&pNewTrunk->aData[8], &pTrunk->aData[12], (k-1)*4); |
drh | d3627af | 2006-12-18 18:34:51 +0000 | [diff] [blame] | 3547 | releasePage(pNewTrunk); |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 3548 | if( !pPrevTrunk ){ |
| 3549 | put4byte(&pPage1->aData[32], iNewTrunk); |
| 3550 | }else{ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 3551 | rc = sqlite3PagerWrite(pPrevTrunk->pDbPage); |
drh | d3627af | 2006-12-18 18:34:51 +0000 | [diff] [blame] | 3552 | if( rc ){ |
| 3553 | goto end_allocate_page; |
| 3554 | } |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 3555 | put4byte(&pPrevTrunk->aData[0], iNewTrunk); |
| 3556 | } |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 3557 | } |
| 3558 | pTrunk = 0; |
| 3559 | TRACE(("ALLOCATE: %d trunk - %d free pages left\n", *pPgno, n-1)); |
| 3560 | #endif |
| 3561 | }else{ |
| 3562 | /* Extract a leaf from the trunk */ |
| 3563 | int closest; |
| 3564 | Pgno iPage; |
| 3565 | unsigned char *aData = pTrunk->aData; |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 3566 | rc = sqlite3PagerWrite(pTrunk->pDbPage); |
drh | d3627af | 2006-12-18 18:34:51 +0000 | [diff] [blame] | 3567 | if( rc ){ |
| 3568 | goto end_allocate_page; |
| 3569 | } |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 3570 | if( nearby>0 ){ |
| 3571 | int i, dist; |
| 3572 | closest = 0; |
| 3573 | dist = get4byte(&aData[8]) - nearby; |
| 3574 | if( dist<0 ) dist = -dist; |
| 3575 | for(i=1; i<k; i++){ |
| 3576 | int d2 = get4byte(&aData[8+i*4]) - nearby; |
| 3577 | if( d2<0 ) d2 = -d2; |
| 3578 | if( d2<dist ){ |
| 3579 | closest = i; |
| 3580 | dist = d2; |
| 3581 | } |
| 3582 | } |
| 3583 | }else{ |
| 3584 | closest = 0; |
| 3585 | } |
| 3586 | |
| 3587 | iPage = get4byte(&aData[8+closest*4]); |
| 3588 | if( !searchList || iPage==nearby ){ |
| 3589 | *pPgno = iPage; |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 3590 | if( *pPgno>sqlite3PagerPagecount(pBt->pPager) ){ |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 3591 | /* Free page off the end of the file */ |
drh | 4928570 | 2005-09-17 15:20:26 +0000 | [diff] [blame] | 3592 | return SQLITE_CORRUPT_BKPT; |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 3593 | } |
| 3594 | TRACE(("ALLOCATE: %d was leaf %d of %d on trunk %d" |
| 3595 | ": %d more free pages\n", |
| 3596 | *pPgno, closest+1, k, pTrunk->pgno, n-1)); |
| 3597 | if( closest<k-1 ){ |
| 3598 | memcpy(&aData[8+closest*4], &aData[4+k*4], 4); |
| 3599 | } |
| 3600 | put4byte(&aData[4], k-1); |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 3601 | rc = sqlite3BtreeGetPage(pBt, *pPgno, ppPage, 1); |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 3602 | if( rc==SQLITE_OK ){ |
drh | 538f570 | 2007-04-13 02:14:30 +0000 | [diff] [blame] | 3603 | sqlite3PagerDontRollback((*ppPage)->pDbPage); |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 3604 | rc = sqlite3PagerWrite((*ppPage)->pDbPage); |
danielk1977 | aac0a38 | 2005-01-16 11:07:06 +0000 | [diff] [blame] | 3605 | if( rc!=SQLITE_OK ){ |
| 3606 | releasePage(*ppPage); |
| 3607 | } |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 3608 | } |
| 3609 | searchList = 0; |
| 3610 | } |
drh | ee696e2 | 2004-08-30 16:52:17 +0000 | [diff] [blame] | 3611 | } |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 3612 | releasePage(pPrevTrunk); |
drh | d3627af | 2006-12-18 18:34:51 +0000 | [diff] [blame] | 3613 | pPrevTrunk = 0; |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 3614 | }while( searchList ); |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 3615 | }else{ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3616 | /* There are no pages on the freelist, so create a new page at the |
| 3617 | ** end of the file */ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 3618 | *pPgno = sqlite3PagerPagecount(pBt->pPager) + 1; |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 3619 | |
| 3620 | #ifndef SQLITE_OMIT_AUTOVACUUM |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 3621 | if( pBt->nTrunc ){ |
| 3622 | /* An incr-vacuum has already run within this transaction. So the |
| 3623 | ** page to allocate is not from the physical end of the file, but |
| 3624 | ** at pBt->nTrunc. |
| 3625 | */ |
| 3626 | *pPgno = pBt->nTrunc+1; |
| 3627 | if( *pPgno==PENDING_BYTE_PAGE(pBt) ){ |
| 3628 | (*pPgno)++; |
| 3629 | } |
| 3630 | } |
danielk1977 | 266664d | 2006-02-10 08:24:21 +0000 | [diff] [blame] | 3631 | if( pBt->autoVacuum && PTRMAP_ISPAGE(pBt, *pPgno) ){ |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 3632 | /* If *pPgno refers to a pointer-map page, allocate two new pages |
| 3633 | ** at the end of the file instead of one. The first allocated page |
| 3634 | ** becomes a new pointer-map page, the second is used by the caller. |
| 3635 | */ |
| 3636 | TRACE(("ALLOCATE: %d from end of file (pointer-map page)\n", *pPgno)); |
danielk1977 | 599fcba | 2004-11-08 07:13:13 +0000 | [diff] [blame] | 3637 | assert( *pPgno!=PENDING_BYTE_PAGE(pBt) ); |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 3638 | (*pPgno)++; |
| 3639 | } |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 3640 | if( pBt->nTrunc ){ |
| 3641 | pBt->nTrunc = *pPgno; |
| 3642 | } |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 3643 | #endif |
| 3644 | |
danielk1977 | 599fcba | 2004-11-08 07:13:13 +0000 | [diff] [blame] | 3645 | assert( *pPgno!=PENDING_BYTE_PAGE(pBt) ); |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 3646 | rc = sqlite3BtreeGetPage(pBt, *pPgno, ppPage, 0); |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 3647 | if( rc ) return rc; |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 3648 | rc = sqlite3PagerWrite((*ppPage)->pDbPage); |
danielk1977 | aac0a38 | 2005-01-16 11:07:06 +0000 | [diff] [blame] | 3649 | if( rc!=SQLITE_OK ){ |
| 3650 | releasePage(*ppPage); |
| 3651 | } |
drh | 3a4c141 | 2004-05-09 20:40:11 +0000 | [diff] [blame] | 3652 | TRACE(("ALLOCATE: %d from end of file\n", *pPgno)); |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 3653 | } |
danielk1977 | 599fcba | 2004-11-08 07:13:13 +0000 | [diff] [blame] | 3654 | |
| 3655 | assert( *pPgno!=PENDING_BYTE_PAGE(pBt) ); |
drh | d3627af | 2006-12-18 18:34:51 +0000 | [diff] [blame] | 3656 | |
| 3657 | end_allocate_page: |
| 3658 | releasePage(pTrunk); |
| 3659 | releasePage(pPrevTrunk); |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 3660 | return rc; |
| 3661 | } |
| 3662 | |
| 3663 | /* |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3664 | ** Add a page of the database file to the freelist. |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 3665 | ** |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 3666 | ** sqlite3PagerUnref() is NOT called for pPage. |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 3667 | */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3668 | static int freePage(MemPage *pPage){ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 3669 | BtShared *pBt = pPage->pBt; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3670 | MemPage *pPage1 = pBt->pPage1; |
| 3671 | int rc, n, k; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 3672 | |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3673 | /* Prepare the page for freeing */ |
| 3674 | assert( pPage->pgno>1 ); |
| 3675 | pPage->isInit = 0; |
| 3676 | releasePage(pPage->pParent); |
| 3677 | pPage->pParent = 0; |
| 3678 | |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 3679 | /* Increment the free page count on pPage1 */ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 3680 | rc = sqlite3PagerWrite(pPage1->pDbPage); |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3681 | if( rc ) return rc; |
| 3682 | n = get4byte(&pPage1->aData[36]); |
| 3683 | put4byte(&pPage1->aData[36], n+1); |
| 3684 | |
drh | fcce93f | 2006-02-22 03:08:32 +0000 | [diff] [blame] | 3685 | #ifdef SQLITE_SECURE_DELETE |
| 3686 | /* If the SQLITE_SECURE_DELETE compile-time option is enabled, then |
| 3687 | ** always fully overwrite deleted information with zeros. |
| 3688 | */ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 3689 | rc = sqlite3PagerWrite(pPage->pDbPage); |
drh | fcce93f | 2006-02-22 03:08:32 +0000 | [diff] [blame] | 3690 | if( rc ) return rc; |
| 3691 | memset(pPage->aData, 0, pPage->pBt->pageSize); |
| 3692 | #endif |
| 3693 | |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 3694 | #ifndef SQLITE_OMIT_AUTOVACUUM |
| 3695 | /* If the database supports auto-vacuum, write an entry in the pointer-map |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 3696 | ** to indicate that the page is free. |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 3697 | */ |
| 3698 | if( pBt->autoVacuum ){ |
| 3699 | rc = ptrmapPut(pBt, pPage->pgno, PTRMAP_FREEPAGE, 0); |
danielk1977 | a64a035 | 2004-11-05 01:45:13 +0000 | [diff] [blame] | 3700 | if( rc ) return rc; |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 3701 | } |
| 3702 | #endif |
| 3703 | |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3704 | if( n==0 ){ |
| 3705 | /* This is the first free page */ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 3706 | rc = sqlite3PagerWrite(pPage->pDbPage); |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 3707 | if( rc ) return rc; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3708 | memset(pPage->aData, 0, 8); |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 3709 | put4byte(&pPage1->aData[32], pPage->pgno); |
drh | 3a4c141 | 2004-05-09 20:40:11 +0000 | [diff] [blame] | 3710 | TRACE(("FREE-PAGE: %d first\n", pPage->pgno)); |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3711 | }else{ |
| 3712 | /* Other free pages already exist. Retrive the first trunk page |
| 3713 | ** of the freelist and find out how many leaves it has. */ |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 3714 | MemPage *pTrunk; |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 3715 | rc = sqlite3BtreeGetPage(pBt, get4byte(&pPage1->aData[32]), &pTrunk, 0); |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 3716 | if( rc ) return rc; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3717 | k = get4byte(&pTrunk->aData[4]); |
drh | ee696e2 | 2004-08-30 16:52:17 +0000 | [diff] [blame] | 3718 | if( k>=pBt->usableSize/4 - 8 ){ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3719 | /* The trunk is full. Turn the page being freed into a new |
| 3720 | ** trunk page with no leaves. */ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 3721 | rc = sqlite3PagerWrite(pPage->pDbPage); |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3722 | if( rc ) return rc; |
| 3723 | put4byte(pPage->aData, pTrunk->pgno); |
| 3724 | put4byte(&pPage->aData[4], 0); |
| 3725 | put4byte(&pPage1->aData[32], pPage->pgno); |
drh | 3a4c141 | 2004-05-09 20:40:11 +0000 | [diff] [blame] | 3726 | TRACE(("FREE-PAGE: %d new trunk page replacing %d\n", |
| 3727 | pPage->pgno, pTrunk->pgno)); |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3728 | }else{ |
| 3729 | /* Add the newly freed page as a leaf on the current trunk */ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 3730 | rc = sqlite3PagerWrite(pTrunk->pDbPage); |
drh | f534544 | 2007-04-09 12:45:02 +0000 | [diff] [blame] | 3731 | if( rc==SQLITE_OK ){ |
| 3732 | put4byte(&pTrunk->aData[4], k+1); |
| 3733 | put4byte(&pTrunk->aData[8+k*4], pPage->pgno); |
drh | fcce93f | 2006-02-22 03:08:32 +0000 | [diff] [blame] | 3734 | #ifndef SQLITE_SECURE_DELETE |
drh | 538f570 | 2007-04-13 02:14:30 +0000 | [diff] [blame] | 3735 | sqlite3PagerDontWrite(pPage->pDbPage); |
drh | fcce93f | 2006-02-22 03:08:32 +0000 | [diff] [blame] | 3736 | #endif |
drh | f534544 | 2007-04-09 12:45:02 +0000 | [diff] [blame] | 3737 | } |
drh | 3a4c141 | 2004-05-09 20:40:11 +0000 | [diff] [blame] | 3738 | 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] | 3739 | } |
| 3740 | releasePage(pTrunk); |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 3741 | } |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 3742 | return rc; |
| 3743 | } |
| 3744 | |
| 3745 | /* |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3746 | ** Free any overflow pages associated with the given Cell. |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 3747 | */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3748 | static int clearCell(MemPage *pPage, unsigned char *pCell){ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 3749 | BtShared *pBt = pPage->pBt; |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 3750 | CellInfo info; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3751 | Pgno ovflPgno; |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 3752 | int rc; |
drh | 9444081 | 2007-03-06 11:42:19 +0000 | [diff] [blame] | 3753 | int nOvfl; |
| 3754 | int ovflPageSize; |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 3755 | |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 3756 | sqlite3BtreeParseCellPtr(pPage, pCell, &info); |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 3757 | if( info.iOverflow==0 ){ |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 3758 | return SQLITE_OK; /* No overflow pages. Return without doing anything */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3759 | } |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 3760 | ovflPgno = get4byte(&pCell[info.iOverflow]); |
drh | 9444081 | 2007-03-06 11:42:19 +0000 | [diff] [blame] | 3761 | ovflPageSize = pBt->usableSize - 4; |
drh | 7236583 | 2007-03-06 15:53:44 +0000 | [diff] [blame] | 3762 | nOvfl = (info.nPayload - info.nLocal + ovflPageSize - 1)/ovflPageSize; |
| 3763 | assert( ovflPgno==0 || nOvfl>0 ); |
| 3764 | while( nOvfl-- ){ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3765 | MemPage *pOvfl; |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 3766 | if( ovflPgno==0 || ovflPgno>sqlite3PagerPagecount(pBt->pPager) ){ |
drh | 4928570 | 2005-09-17 15:20:26 +0000 | [diff] [blame] | 3767 | return SQLITE_CORRUPT_BKPT; |
danielk1977 | a1cb183 | 2005-02-12 08:59:55 +0000 | [diff] [blame] | 3768 | } |
danielk1977 | 8c0a959 | 2007-04-30 16:55:00 +0000 | [diff] [blame] | 3769 | |
| 3770 | rc = getOverflowPage(pBt, ovflPgno, &pOvfl, (nOvfl==0)?0:&ovflPgno); |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 3771 | if( rc ) return rc; |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 3772 | rc = freePage(pOvfl); |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 3773 | sqlite3PagerUnref(pOvfl->pDbPage); |
danielk1977 | 6b456a2 | 2005-03-21 04:04:02 +0000 | [diff] [blame] | 3774 | if( rc ) return rc; |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 3775 | } |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 3776 | return SQLITE_OK; |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 3777 | } |
| 3778 | |
| 3779 | /* |
drh | 9102529 | 2004-05-03 19:49:32 +0000 | [diff] [blame] | 3780 | ** Create the byte sequence used to represent a cell on page pPage |
| 3781 | ** and write that byte sequence into pCell[]. Overflow pages are |
| 3782 | ** allocated and filled in as necessary. The calling procedure |
| 3783 | ** is responsible for making sure sufficient space has been allocated |
| 3784 | ** for pCell[]. |
| 3785 | ** |
| 3786 | ** Note that pCell does not necessary need to point to the pPage->aData |
| 3787 | ** area. pCell might point to some temporary storage. The cell will |
| 3788 | ** be constructed in this temporary area then copied into pPage->aData |
| 3789 | ** later. |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 3790 | */ |
| 3791 | static int fillInCell( |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3792 | MemPage *pPage, /* The page that contains the cell */ |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 3793 | unsigned char *pCell, /* Complete text of the cell */ |
drh | 4a1c380 | 2004-05-12 15:15:47 +0000 | [diff] [blame] | 3794 | const void *pKey, i64 nKey, /* The key */ |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 3795 | const void *pData,int nData, /* The data */ |
drh | b026e05 | 2007-05-02 01:34:31 +0000 | [diff] [blame] | 3796 | int nZero, /* Extra zero bytes to append to pData */ |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 3797 | int *pnSize /* Write cell size here */ |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 3798 | ){ |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 3799 | int nPayload; |
drh | 8c6fa9b | 2004-05-26 00:01:53 +0000 | [diff] [blame] | 3800 | const u8 *pSrc; |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 3801 | int nSrc, n, rc; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3802 | int spaceLeft; |
| 3803 | MemPage *pOvfl = 0; |
drh | 9b17127 | 2004-05-08 02:03:22 +0000 | [diff] [blame] | 3804 | MemPage *pToRelease = 0; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3805 | unsigned char *pPrior; |
| 3806 | unsigned char *pPayload; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 3807 | BtShared *pBt = pPage->pBt; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3808 | Pgno pgnoOvfl = 0; |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 3809 | int nHeader; |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 3810 | CellInfo info; |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 3811 | |
drh | 9102529 | 2004-05-03 19:49:32 +0000 | [diff] [blame] | 3812 | /* Fill in the header. */ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 3813 | nHeader = 0; |
drh | 9102529 | 2004-05-03 19:49:32 +0000 | [diff] [blame] | 3814 | if( !pPage->leaf ){ |
| 3815 | nHeader += 4; |
| 3816 | } |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 3817 | if( pPage->hasData ){ |
drh | b026e05 | 2007-05-02 01:34:31 +0000 | [diff] [blame] | 3818 | nHeader += putVarint(&pCell[nHeader], nData+nZero); |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 3819 | }else{ |
drh | b026e05 | 2007-05-02 01:34:31 +0000 | [diff] [blame] | 3820 | nData = nZero = 0; |
drh | 9102529 | 2004-05-03 19:49:32 +0000 | [diff] [blame] | 3821 | } |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 3822 | nHeader += putVarint(&pCell[nHeader], *(u64*)&nKey); |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 3823 | sqlite3BtreeParseCellPtr(pPage, pCell, &info); |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 3824 | assert( info.nHeader==nHeader ); |
| 3825 | assert( info.nKey==nKey ); |
drh | b026e05 | 2007-05-02 01:34:31 +0000 | [diff] [blame] | 3826 | assert( info.nData==nData+nZero ); |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 3827 | |
| 3828 | /* Fill in the payload */ |
drh | b026e05 | 2007-05-02 01:34:31 +0000 | [diff] [blame] | 3829 | nPayload = nData + nZero; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3830 | if( pPage->intKey ){ |
| 3831 | pSrc = pData; |
| 3832 | nSrc = nData; |
drh | 9102529 | 2004-05-03 19:49:32 +0000 | [diff] [blame] | 3833 | nData = 0; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3834 | }else{ |
| 3835 | nPayload += nKey; |
| 3836 | pSrc = pKey; |
| 3837 | nSrc = nKey; |
| 3838 | } |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 3839 | *pnSize = info.nSize; |
| 3840 | spaceLeft = info.nLocal; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3841 | pPayload = &pCell[nHeader]; |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 3842 | pPrior = &pCell[info.iOverflow]; |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 3843 | |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 3844 | while( nPayload>0 ){ |
| 3845 | if( spaceLeft==0 ){ |
danielk1977 | b39f70b | 2007-05-17 18:28:11 +0000 | [diff] [blame^] | 3846 | int isExact = 0; |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 3847 | #ifndef SQLITE_OMIT_AUTOVACUUM |
| 3848 | Pgno pgnoPtrmap = pgnoOvfl; /* Overflow page pointer-map entry page */ |
danielk1977 | b39f70b | 2007-05-17 18:28:11 +0000 | [diff] [blame^] | 3849 | if( pBt->autoVacuum ){ |
| 3850 | do{ |
| 3851 | pgnoOvfl++; |
| 3852 | } while( |
| 3853 | PTRMAP_ISPAGE(pBt, pgnoOvfl) || pgnoOvfl==PENDING_BYTE_PAGE(pBt) |
| 3854 | ); |
| 3855 | if( pgnoOvfl>1 ){ |
| 3856 | /* isExact = 1; */ |
| 3857 | } |
| 3858 | } |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 3859 | #endif |
danielk1977 | b39f70b | 2007-05-17 18:28:11 +0000 | [diff] [blame^] | 3860 | rc = allocateBtreePage(pBt, &pOvfl, &pgnoOvfl, pgnoOvfl, isExact); |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 3861 | #ifndef SQLITE_OMIT_AUTOVACUUM |
danielk1977 | a19df67 | 2004-11-03 11:37:07 +0000 | [diff] [blame] | 3862 | /* If the database supports auto-vacuum, and the second or subsequent |
| 3863 | ** overflow page is being allocated, add an entry to the pointer-map |
| 3864 | ** for that page now. The entry for the first overflow page will be |
| 3865 | ** added later, by the insertCell() routine. |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 3866 | */ |
danielk1977 | a19df67 | 2004-11-03 11:37:07 +0000 | [diff] [blame] | 3867 | if( pBt->autoVacuum && pgnoPtrmap!=0 && rc==SQLITE_OK ){ |
| 3868 | rc = ptrmapPut(pBt, pgnoOvfl, PTRMAP_OVERFLOW2, pgnoPtrmap); |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 3869 | } |
| 3870 | #endif |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 3871 | if( rc ){ |
drh | 9b17127 | 2004-05-08 02:03:22 +0000 | [diff] [blame] | 3872 | releasePage(pToRelease); |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 3873 | return rc; |
| 3874 | } |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3875 | put4byte(pPrior, pgnoOvfl); |
drh | 9b17127 | 2004-05-08 02:03:22 +0000 | [diff] [blame] | 3876 | releasePage(pToRelease); |
| 3877 | pToRelease = pOvfl; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3878 | pPrior = pOvfl->aData; |
| 3879 | put4byte(pPrior, 0); |
| 3880 | pPayload = &pOvfl->aData[4]; |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 3881 | spaceLeft = pBt->usableSize - 4; |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 3882 | } |
| 3883 | n = nPayload; |
| 3884 | if( n>spaceLeft ) n = spaceLeft; |
drh | b026e05 | 2007-05-02 01:34:31 +0000 | [diff] [blame] | 3885 | if( nSrc>0 ){ |
| 3886 | if( n>nSrc ) n = nSrc; |
| 3887 | assert( pSrc ); |
| 3888 | memcpy(pPayload, pSrc, n); |
| 3889 | }else{ |
| 3890 | memset(pPayload, 0, n); |
| 3891 | } |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 3892 | nPayload -= n; |
drh | de64713 | 2004-05-07 17:57:49 +0000 | [diff] [blame] | 3893 | pPayload += n; |
drh | 9b17127 | 2004-05-08 02:03:22 +0000 | [diff] [blame] | 3894 | pSrc += n; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3895 | nSrc -= n; |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 3896 | spaceLeft -= n; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3897 | if( nSrc==0 ){ |
| 3898 | nSrc = nData; |
| 3899 | pSrc = pData; |
| 3900 | } |
drh | dd79342 | 2001-06-28 01:54:48 +0000 | [diff] [blame] | 3901 | } |
drh | 9b17127 | 2004-05-08 02:03:22 +0000 | [diff] [blame] | 3902 | releasePage(pToRelease); |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 3903 | return SQLITE_OK; |
| 3904 | } |
| 3905 | |
| 3906 | /* |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 3907 | ** Change the MemPage.pParent pointer on the page whose number is |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 3908 | ** given in the second argument so that MemPage.pParent holds the |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 3909 | ** pointer in the third argument. |
| 3910 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 3911 | static int reparentPage(BtShared *pBt, Pgno pgno, MemPage *pNewParent, int idx){ |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 3912 | MemPage *pThis; |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 3913 | DbPage *pDbPage; |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 3914 | |
drh | 43617e9 | 2006-03-06 20:55:46 +0000 | [diff] [blame] | 3915 | assert( pNewParent!=0 ); |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 3916 | if( pgno==0 ) return SQLITE_OK; |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 3917 | assert( pBt->pPager!=0 ); |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 3918 | pDbPage = sqlite3PagerLookup(pBt->pPager, pgno); |
| 3919 | if( pDbPage ){ |
| 3920 | pThis = (MemPage *)sqlite3PagerGetExtra(pDbPage); |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 3921 | if( pThis->isInit ){ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 3922 | assert( pThis->aData==(sqlite3PagerGetData(pDbPage)) ); |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 3923 | if( pThis->pParent!=pNewParent ){ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 3924 | if( pThis->pParent ) sqlite3PagerUnref(pThis->pParent->pDbPage); |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 3925 | pThis->pParent = pNewParent; |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 3926 | sqlite3PagerRef(pNewParent->pDbPage); |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 3927 | } |
| 3928 | pThis->idxParent = idx; |
drh | dd79342 | 2001-06-28 01:54:48 +0000 | [diff] [blame] | 3929 | } |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 3930 | sqlite3PagerUnref(pDbPage); |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 3931 | } |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 3932 | |
| 3933 | #ifndef SQLITE_OMIT_AUTOVACUUM |
| 3934 | if( pBt->autoVacuum ){ |
| 3935 | return ptrmapPut(pBt, pgno, PTRMAP_BTREE, pNewParent->pgno); |
| 3936 | } |
| 3937 | #endif |
| 3938 | return SQLITE_OK; |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 3939 | } |
| 3940 | |
danielk1977 | ac11ee6 | 2005-01-15 12:45:51 +0000 | [diff] [blame] | 3941 | |
| 3942 | |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 3943 | /* |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 3944 | ** Change the pParent pointer of all children of pPage to point back |
| 3945 | ** to pPage. |
| 3946 | ** |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 3947 | ** In other words, for every child of pPage, invoke reparentPage() |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3948 | ** to make sure that each child knows that pPage is its parent. |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 3949 | ** |
| 3950 | ** This routine gets called after you memcpy() one page into |
| 3951 | ** another. |
| 3952 | */ |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 3953 | static int reparentChildPages(MemPage *pPage){ |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 3954 | int i; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 3955 | BtShared *pBt = pPage->pBt; |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 3956 | int rc = SQLITE_OK; |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 3957 | |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 3958 | if( pPage->leaf ) return SQLITE_OK; |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 3959 | |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 3960 | for(i=0; i<pPage->nCell; i++){ |
danielk1977 | 1cc5ed8 | 2007-05-16 17:28:43 +0000 | [diff] [blame] | 3961 | u8 *pCell = findCell(pPage, i); |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 3962 | if( !pPage->leaf ){ |
| 3963 | rc = reparentPage(pBt, get4byte(pCell), pPage, i); |
| 3964 | if( rc!=SQLITE_OK ) return rc; |
| 3965 | } |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 3966 | } |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 3967 | if( !pPage->leaf ){ |
| 3968 | rc = reparentPage(pBt, get4byte(&pPage->aData[pPage->hdrOffset+8]), |
| 3969 | pPage, i); |
| 3970 | pPage->idxShift = 0; |
| 3971 | } |
| 3972 | return rc; |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 3973 | } |
| 3974 | |
| 3975 | /* |
| 3976 | ** Remove the i-th cell from pPage. This routine effects pPage only. |
| 3977 | ** The cell content is not freed or deallocated. It is assumed that |
| 3978 | ** the cell content has been copied someplace else. This routine just |
| 3979 | ** removes the reference to the cell from pPage. |
| 3980 | ** |
| 3981 | ** "sz" must be the number of bytes in the cell. |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 3982 | */ |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 3983 | static void dropCell(MemPage *pPage, int idx, int sz){ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 3984 | int i; /* Loop counter */ |
| 3985 | int pc; /* Offset to cell content of cell being deleted */ |
| 3986 | u8 *data; /* pPage->aData */ |
| 3987 | u8 *ptr; /* Used to move bytes around within data[] */ |
| 3988 | |
drh | 8c42ca9 | 2001-06-22 19:15:00 +0000 | [diff] [blame] | 3989 | assert( idx>=0 && idx<pPage->nCell ); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 3990 | assert( sz==cellSize(pPage, idx) ); |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 3991 | assert( sqlite3PagerIswriteable(pPage->pDbPage) ); |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 3992 | data = pPage->aData; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 3993 | ptr = &data[pPage->cellOffset + 2*idx]; |
| 3994 | pc = get2byte(ptr); |
| 3995 | assert( pc>10 && pc+sz<=pPage->pBt->usableSize ); |
drh | de64713 | 2004-05-07 17:57:49 +0000 | [diff] [blame] | 3996 | freeSpace(pPage, pc, sz); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 3997 | for(i=idx+1; i<pPage->nCell; i++, ptr+=2){ |
| 3998 | ptr[0] = ptr[2]; |
| 3999 | ptr[1] = ptr[3]; |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 4000 | } |
| 4001 | pPage->nCell--; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4002 | put2byte(&data[pPage->hdrOffset+3], pPage->nCell); |
| 4003 | pPage->nFree += 2; |
drh | 428ae8c | 2003-01-04 16:48:09 +0000 | [diff] [blame] | 4004 | pPage->idxShift = 1; |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 4005 | } |
| 4006 | |
| 4007 | /* |
| 4008 | ** Insert a new cell on pPage at cell index "i". pCell points to the |
| 4009 | ** content of the cell. |
| 4010 | ** |
| 4011 | ** 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] | 4012 | ** will not fit, then make a copy of the cell content into pTemp if |
| 4013 | ** pTemp is not null. Regardless of pTemp, allocate a new entry |
| 4014 | ** in pPage->aOvfl[] and make it point to the cell content (either |
| 4015 | ** in pTemp or the original pCell) and also record its index. |
| 4016 | ** Allocating a new entry in pPage->aCell[] implies that |
| 4017 | ** pPage->nOverflow is incremented. |
danielk1977 | a3ad5e7 | 2005-01-07 08:56:44 +0000 | [diff] [blame] | 4018 | ** |
| 4019 | ** If nSkip is non-zero, then do not copy the first nSkip bytes of the |
| 4020 | ** cell. The caller will overwrite them after this function returns. If |
drh | 4b238df | 2005-01-08 15:43:18 +0000 | [diff] [blame] | 4021 | ** 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] | 4022 | ** (but pCell+nSkip is always valid). |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 4023 | */ |
danielk1977 | e80463b | 2004-11-03 03:01:16 +0000 | [diff] [blame] | 4024 | static int insertCell( |
drh | 24cd67e | 2004-05-10 16:18:47 +0000 | [diff] [blame] | 4025 | MemPage *pPage, /* Page into which we are copying */ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4026 | int i, /* New cell becomes the i-th cell of the page */ |
| 4027 | u8 *pCell, /* Content of the new cell */ |
| 4028 | int sz, /* Bytes of content in pCell */ |
danielk1977 | a3ad5e7 | 2005-01-07 08:56:44 +0000 | [diff] [blame] | 4029 | u8 *pTemp, /* Temp storage space for pCell, if needed */ |
| 4030 | u8 nSkip /* Do not write the first nSkip bytes of the cell */ |
drh | 24cd67e | 2004-05-10 16:18:47 +0000 | [diff] [blame] | 4031 | ){ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4032 | int idx; /* Where to write new cell content in data[] */ |
| 4033 | int j; /* Loop counter */ |
| 4034 | int top; /* First byte of content for any cell in data[] */ |
| 4035 | int end; /* First byte past the last cell pointer in data[] */ |
| 4036 | int ins; /* Index in data[] where new cell pointer is inserted */ |
| 4037 | int hdr; /* Offset into data[] of the page header */ |
| 4038 | int cellOffset; /* Address of first cell pointer in data[] */ |
| 4039 | u8 *data; /* The content of the whole page */ |
| 4040 | u8 *ptr; /* Used for moving information around in data[] */ |
| 4041 | |
| 4042 | assert( i>=0 && i<=pPage->nCell+pPage->nOverflow ); |
| 4043 | assert( sz==cellSizePtr(pPage, pCell) ); |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 4044 | assert( sqlite3PagerIswriteable(pPage->pDbPage) ); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4045 | if( pPage->nOverflow || sz+2>pPage->nFree ){ |
drh | 24cd67e | 2004-05-10 16:18:47 +0000 | [diff] [blame] | 4046 | if( pTemp ){ |
danielk1977 | a3ad5e7 | 2005-01-07 08:56:44 +0000 | [diff] [blame] | 4047 | memcpy(pTemp+nSkip, pCell+nSkip, sz-nSkip); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4048 | pCell = pTemp; |
drh | 24cd67e | 2004-05-10 16:18:47 +0000 | [diff] [blame] | 4049 | } |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4050 | j = pPage->nOverflow++; |
| 4051 | assert( j<sizeof(pPage->aOvfl)/sizeof(pPage->aOvfl[0]) ); |
| 4052 | pPage->aOvfl[j].pCell = pCell; |
| 4053 | pPage->aOvfl[j].idx = i; |
| 4054 | pPage->nFree = 0; |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 4055 | }else{ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4056 | data = pPage->aData; |
| 4057 | hdr = pPage->hdrOffset; |
| 4058 | top = get2byte(&data[hdr+5]); |
| 4059 | cellOffset = pPage->cellOffset; |
| 4060 | end = cellOffset + 2*pPage->nCell + 2; |
| 4061 | ins = cellOffset + 2*i; |
| 4062 | if( end > top - sz ){ |
danielk1977 | 6b456a2 | 2005-03-21 04:04:02 +0000 | [diff] [blame] | 4063 | int rc = defragmentPage(pPage); |
| 4064 | if( rc!=SQLITE_OK ) return rc; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4065 | top = get2byte(&data[hdr+5]); |
| 4066 | assert( end + sz <= top ); |
| 4067 | } |
| 4068 | idx = allocateSpace(pPage, sz); |
| 4069 | assert( idx>0 ); |
| 4070 | assert( end <= get2byte(&data[hdr+5]) ); |
| 4071 | pPage->nCell++; |
| 4072 | pPage->nFree -= 2; |
danielk1977 | a3ad5e7 | 2005-01-07 08:56:44 +0000 | [diff] [blame] | 4073 | memcpy(&data[idx+nSkip], pCell+nSkip, sz-nSkip); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4074 | for(j=end-2, ptr=&data[j]; j>ins; j-=2, ptr-=2){ |
| 4075 | ptr[0] = ptr[-2]; |
| 4076 | ptr[1] = ptr[-1]; |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 4077 | } |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4078 | put2byte(&data[ins], idx); |
| 4079 | put2byte(&data[hdr+3], pPage->nCell); |
| 4080 | pPage->idxShift = 1; |
danielk1977 | a19df67 | 2004-11-03 11:37:07 +0000 | [diff] [blame] | 4081 | #ifndef SQLITE_OMIT_AUTOVACUUM |
| 4082 | if( pPage->pBt->autoVacuum ){ |
| 4083 | /* The cell may contain a pointer to an overflow page. If so, write |
| 4084 | ** the entry for the overflow page into the pointer map. |
| 4085 | */ |
| 4086 | CellInfo info; |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 4087 | sqlite3BtreeParseCellPtr(pPage, pCell, &info); |
drh | 7236583 | 2007-03-06 15:53:44 +0000 | [diff] [blame] | 4088 | assert( (info.nData+(pPage->intKey?0:info.nKey))==info.nPayload ); |
danielk1977 | a19df67 | 2004-11-03 11:37:07 +0000 | [diff] [blame] | 4089 | if( (info.nData+(pPage->intKey?0:info.nKey))>info.nLocal ){ |
| 4090 | Pgno pgnoOvfl = get4byte(&pCell[info.iOverflow]); |
| 4091 | int rc = ptrmapPut(pPage->pBt, pgnoOvfl, PTRMAP_OVERFLOW1, pPage->pgno); |
| 4092 | if( rc!=SQLITE_OK ) return rc; |
| 4093 | } |
| 4094 | } |
| 4095 | #endif |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 4096 | } |
danielk1977 | e80463b | 2004-11-03 03:01:16 +0000 | [diff] [blame] | 4097 | |
danielk1977 | e80463b | 2004-11-03 03:01:16 +0000 | [diff] [blame] | 4098 | return SQLITE_OK; |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 4099 | } |
| 4100 | |
| 4101 | /* |
drh | fa1a98a | 2004-05-14 19:08:17 +0000 | [diff] [blame] | 4102 | ** Add a list of cells to a page. The page should be initially empty. |
| 4103 | ** The cells are guaranteed to fit on the page. |
| 4104 | */ |
| 4105 | static void assemblePage( |
| 4106 | MemPage *pPage, /* The page to be assemblied */ |
| 4107 | int nCell, /* The number of cells to add to this page */ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4108 | u8 **apCell, /* Pointers to cell bodies */ |
drh | fa1a98a | 2004-05-14 19:08:17 +0000 | [diff] [blame] | 4109 | int *aSize /* Sizes of the cells */ |
| 4110 | ){ |
| 4111 | int i; /* Loop counter */ |
| 4112 | int totalSize; /* Total size of all cells */ |
| 4113 | int hdr; /* Index of page header */ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4114 | int cellptr; /* Address of next cell pointer */ |
| 4115 | int cellbody; /* Address of next cell body */ |
drh | fa1a98a | 2004-05-14 19:08:17 +0000 | [diff] [blame] | 4116 | u8 *data; /* Data for the page */ |
| 4117 | |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4118 | assert( pPage->nOverflow==0 ); |
drh | fa1a98a | 2004-05-14 19:08:17 +0000 | [diff] [blame] | 4119 | totalSize = 0; |
| 4120 | for(i=0; i<nCell; i++){ |
| 4121 | totalSize += aSize[i]; |
| 4122 | } |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4123 | assert( totalSize+2*nCell<=pPage->nFree ); |
drh | fa1a98a | 2004-05-14 19:08:17 +0000 | [diff] [blame] | 4124 | assert( pPage->nCell==0 ); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4125 | cellptr = pPage->cellOffset; |
drh | fa1a98a | 2004-05-14 19:08:17 +0000 | [diff] [blame] | 4126 | data = pPage->aData; |
| 4127 | hdr = pPage->hdrOffset; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4128 | put2byte(&data[hdr+3], nCell); |
drh | 09d0deb | 2005-08-02 17:13:09 +0000 | [diff] [blame] | 4129 | if( nCell ){ |
| 4130 | cellbody = allocateSpace(pPage, totalSize); |
| 4131 | assert( cellbody>0 ); |
| 4132 | assert( pPage->nFree >= 2*nCell ); |
| 4133 | pPage->nFree -= 2*nCell; |
| 4134 | for(i=0; i<nCell; i++){ |
| 4135 | put2byte(&data[cellptr], cellbody); |
| 4136 | memcpy(&data[cellbody], apCell[i], aSize[i]); |
| 4137 | cellptr += 2; |
| 4138 | cellbody += aSize[i]; |
| 4139 | } |
| 4140 | assert( cellbody==pPage->pBt->usableSize ); |
drh | fa1a98a | 2004-05-14 19:08:17 +0000 | [diff] [blame] | 4141 | } |
| 4142 | pPage->nCell = nCell; |
drh | fa1a98a | 2004-05-14 19:08:17 +0000 | [diff] [blame] | 4143 | } |
| 4144 | |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 4145 | /* |
drh | c3b7057 | 2003-01-04 19:44:07 +0000 | [diff] [blame] | 4146 | ** The following parameters determine how many adjacent pages get involved |
| 4147 | ** in a balancing operation. NN is the number of neighbors on either side |
| 4148 | ** of the page that participate in the balancing operation. NB is the |
| 4149 | ** total number of pages that participate, including the target page and |
| 4150 | ** NN neighbors on either side. |
| 4151 | ** |
| 4152 | ** The minimum value of NN is 1 (of course). Increasing NN above 1 |
| 4153 | ** (to 2 or 3) gives a modest improvement in SELECT and DELETE performance |
| 4154 | ** in exchange for a larger degradation in INSERT and UPDATE performance. |
| 4155 | ** The value of NN appears to give the best results overall. |
| 4156 | */ |
| 4157 | #define NN 1 /* Number of neighbors on either side of pPage */ |
| 4158 | #define NB (NN*2+1) /* Total pages involved in the balance */ |
| 4159 | |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4160 | /* Forward reference */ |
danielk1977 | ac245ec | 2005-01-14 13:50:11 +0000 | [diff] [blame] | 4161 | static int balance(MemPage*, int); |
| 4162 | |
drh | 615ae55 | 2005-01-16 23:21:00 +0000 | [diff] [blame] | 4163 | #ifndef SQLITE_OMIT_QUICKBALANCE |
drh | f222e71 | 2005-01-14 22:55:49 +0000 | [diff] [blame] | 4164 | /* |
| 4165 | ** This version of balance() handles the common special case where |
| 4166 | ** a new entry is being inserted on the extreme right-end of the |
| 4167 | ** tree, in other words, when the new entry will become the largest |
| 4168 | ** entry in the tree. |
| 4169 | ** |
| 4170 | ** Instead of trying balance the 3 right-most leaf pages, just add |
| 4171 | ** a new page to the right-hand side and put the one new entry in |
| 4172 | ** that page. This leaves the right side of the tree somewhat |
| 4173 | ** unbalanced. But odds are that we will be inserting new entries |
| 4174 | ** at the end soon afterwards so the nearly empty page will quickly |
| 4175 | ** fill up. On average. |
| 4176 | ** |
| 4177 | ** pPage is the leaf page which is the right-most page in the tree. |
| 4178 | ** pParent is its parent. pPage must have a single overflow entry |
| 4179 | ** which is also the right-most entry on the page. |
| 4180 | */ |
danielk1977 | ac245ec | 2005-01-14 13:50:11 +0000 | [diff] [blame] | 4181 | static int balance_quick(MemPage *pPage, MemPage *pParent){ |
| 4182 | int rc; |
| 4183 | MemPage *pNew; |
| 4184 | Pgno pgnoNew; |
| 4185 | u8 *pCell; |
| 4186 | int szCell; |
| 4187 | CellInfo info; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 4188 | BtShared *pBt = pPage->pBt; |
danielk1977 | 79a40da | 2005-01-16 08:00:01 +0000 | [diff] [blame] | 4189 | int parentIdx = pParent->nCell; /* pParent new divider cell index */ |
| 4190 | int parentSize; /* Size of new divider cell */ |
| 4191 | u8 parentCell[64]; /* Space for the new divider cell */ |
danielk1977 | ac245ec | 2005-01-14 13:50:11 +0000 | [diff] [blame] | 4192 | |
| 4193 | /* Allocate a new page. Insert the overflow cell from pPage |
| 4194 | ** into it. Then remove the overflow cell from pPage. |
| 4195 | */ |
drh | 4f0c587 | 2007-03-26 22:05:01 +0000 | [diff] [blame] | 4196 | rc = allocateBtreePage(pBt, &pNew, &pgnoNew, 0, 0); |
danielk1977 | ac245ec | 2005-01-14 13:50:11 +0000 | [diff] [blame] | 4197 | if( rc!=SQLITE_OK ){ |
| 4198 | return rc; |
| 4199 | } |
| 4200 | pCell = pPage->aOvfl[0].pCell; |
| 4201 | szCell = cellSizePtr(pPage, pCell); |
| 4202 | zeroPage(pNew, pPage->aData[0]); |
| 4203 | assemblePage(pNew, 1, &pCell, &szCell); |
| 4204 | pPage->nOverflow = 0; |
| 4205 | |
danielk1977 | 79a40da | 2005-01-16 08:00:01 +0000 | [diff] [blame] | 4206 | /* Set the parent of the newly allocated page to pParent. */ |
| 4207 | pNew->pParent = pParent; |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 4208 | sqlite3PagerRef(pParent->pDbPage); |
danielk1977 | 79a40da | 2005-01-16 08:00:01 +0000 | [diff] [blame] | 4209 | |
danielk1977 | ac245ec | 2005-01-14 13:50:11 +0000 | [diff] [blame] | 4210 | /* pPage is currently the right-child of pParent. Change this |
| 4211 | ** so that the right-child is the new page allocated above and |
danielk1977 | 79a40da | 2005-01-16 08:00:01 +0000 | [diff] [blame] | 4212 | ** pPage is the next-to-right child. |
danielk1977 | ac245ec | 2005-01-14 13:50:11 +0000 | [diff] [blame] | 4213 | */ |
danielk1977 | ac11ee6 | 2005-01-15 12:45:51 +0000 | [diff] [blame] | 4214 | assert( pPage->nCell>0 ); |
danielk1977 | 1cc5ed8 | 2007-05-16 17:28:43 +0000 | [diff] [blame] | 4215 | pCell = findCell(pPage, pPage->nCell-1); |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 4216 | sqlite3BtreeParseCellPtr(pPage, pCell, &info); |
drh | b026e05 | 2007-05-02 01:34:31 +0000 | [diff] [blame] | 4217 | rc = fillInCell(pParent, parentCell, 0, info.nKey, 0, 0, 0, &parentSize); |
danielk1977 | ac245ec | 2005-01-14 13:50:11 +0000 | [diff] [blame] | 4218 | if( rc!=SQLITE_OK ){ |
danielk1977 | 79a40da | 2005-01-16 08:00:01 +0000 | [diff] [blame] | 4219 | return rc; |
danielk1977 | ac245ec | 2005-01-14 13:50:11 +0000 | [diff] [blame] | 4220 | } |
| 4221 | assert( parentSize<64 ); |
| 4222 | rc = insertCell(pParent, parentIdx, parentCell, parentSize, 0, 4); |
| 4223 | if( rc!=SQLITE_OK ){ |
danielk1977 | 79a40da | 2005-01-16 08:00:01 +0000 | [diff] [blame] | 4224 | return rc; |
danielk1977 | ac245ec | 2005-01-14 13:50:11 +0000 | [diff] [blame] | 4225 | } |
| 4226 | put4byte(findOverflowCell(pParent,parentIdx), pPage->pgno); |
| 4227 | put4byte(&pParent->aData[pParent->hdrOffset+8], pgnoNew); |
| 4228 | |
danielk1977 | 79a40da | 2005-01-16 08:00:01 +0000 | [diff] [blame] | 4229 | #ifndef SQLITE_OMIT_AUTOVACUUM |
| 4230 | /* If this is an auto-vacuum database, update the pointer map |
| 4231 | ** with entries for the new page, and any pointer from the |
| 4232 | ** cell on the page to an overflow page. |
| 4233 | */ |
danielk1977 | ac11ee6 | 2005-01-15 12:45:51 +0000 | [diff] [blame] | 4234 | if( pBt->autoVacuum ){ |
| 4235 | rc = ptrmapPut(pBt, pgnoNew, PTRMAP_BTREE, pParent->pgno); |
| 4236 | if( rc!=SQLITE_OK ){ |
| 4237 | return rc; |
| 4238 | } |
danielk1977 | 79a40da | 2005-01-16 08:00:01 +0000 | [diff] [blame] | 4239 | rc = ptrmapPutOvfl(pNew, 0); |
| 4240 | if( rc!=SQLITE_OK ){ |
| 4241 | return rc; |
danielk1977 | ac11ee6 | 2005-01-15 12:45:51 +0000 | [diff] [blame] | 4242 | } |
| 4243 | } |
danielk1977 | 79a40da | 2005-01-16 08:00:01 +0000 | [diff] [blame] | 4244 | #endif |
danielk1977 | ac11ee6 | 2005-01-15 12:45:51 +0000 | [diff] [blame] | 4245 | |
danielk1977 | 79a40da | 2005-01-16 08:00:01 +0000 | [diff] [blame] | 4246 | /* Release the reference to the new page and balance the parent page, |
| 4247 | ** in case the divider cell inserted caused it to become overfull. |
| 4248 | */ |
danielk1977 | ac245ec | 2005-01-14 13:50:11 +0000 | [diff] [blame] | 4249 | releasePage(pNew); |
| 4250 | return balance(pParent, 0); |
| 4251 | } |
drh | 615ae55 | 2005-01-16 23:21:00 +0000 | [diff] [blame] | 4252 | #endif /* SQLITE_OMIT_QUICKBALANCE */ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4253 | |
drh | c3b7057 | 2003-01-04 19:44:07 +0000 | [diff] [blame] | 4254 | /* |
drh | ab01f61 | 2004-05-22 02:55:23 +0000 | [diff] [blame] | 4255 | ** This routine redistributes Cells on pPage and up to NN*2 siblings |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 4256 | ** 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] | 4257 | ** Usually NN siblings on either side of pPage is used in the balancing, |
| 4258 | ** though more siblings might come from one side if pPage is the first |
drh | ab01f61 | 2004-05-22 02:55:23 +0000 | [diff] [blame] | 4259 | ** 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] | 4260 | ** (something which can only happen if pPage is the root page or a |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 4261 | ** child of root) then all available siblings participate in the balancing. |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 4262 | ** |
drh | 0c6cc4e | 2004-06-15 02:13:26 +0000 | [diff] [blame] | 4263 | ** The number of siblings of pPage might be increased or decreased by one or |
| 4264 | ** 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] | 4265 | ** is special and is allowed to be nearly empty. If pPage is |
drh | 8c42ca9 | 2001-06-22 19:15:00 +0000 | [diff] [blame] | 4266 | ** the root page, then the depth of the tree might be increased |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 4267 | ** or decreased by one, as necessary, to keep the root page from being |
drh | ab01f61 | 2004-05-22 02:55:23 +0000 | [diff] [blame] | 4268 | ** overfull or completely empty. |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 4269 | ** |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 4270 | ** Note that when this routine is called, some of the Cells on pPage |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 4271 | ** might not actually be stored in pPage->aData[]. This can happen |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 4272 | ** 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] | 4273 | ** make sure all Cells for pPage once again fit in pPage->aData[]. |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 4274 | ** |
drh | 8c42ca9 | 2001-06-22 19:15:00 +0000 | [diff] [blame] | 4275 | ** In the course of balancing the siblings of pPage, the parent of pPage |
| 4276 | ** might become overfull or underfull. If that happens, then this routine |
| 4277 | ** is called recursively on the parent. |
| 4278 | ** |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4279 | ** If this routine fails for any reason, it might leave the database |
| 4280 | ** in a corrupted state. So if this routine fails, the database should |
| 4281 | ** be rolled back. |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 4282 | */ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4283 | static int balance_nonroot(MemPage *pPage){ |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 4284 | MemPage *pParent; /* The parent of pPage */ |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 4285 | BtShared *pBt; /* The whole database */ |
danielk1977 | 634f298 | 2005-03-28 08:44:07 +0000 | [diff] [blame] | 4286 | int nCell = 0; /* Number of cells in apCell[] */ |
| 4287 | int nMaxCells = 0; /* Allocated size of apCell, szCell, aFrom. */ |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 4288 | int nOld; /* Number of pages in apOld[] */ |
| 4289 | int nNew; /* Number of pages in apNew[] */ |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 4290 | int nDiv; /* Number of cells in apDiv[] */ |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 4291 | int i, j, k; /* Loop counters */ |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 4292 | int idx; /* Index of pPage in pParent->aCell[] */ |
| 4293 | int nxDiv; /* Next divider slot in pParent->aCell[] */ |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 4294 | int rc; /* The return code */ |
drh | 9102529 | 2004-05-03 19:49:32 +0000 | [diff] [blame] | 4295 | int leafCorrection; /* 4 if pPage is a leaf. 0 if not */ |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 4296 | int leafData; /* True if pPage is a leaf of a LEAFDATA tree */ |
drh | 9102529 | 2004-05-03 19:49:32 +0000 | [diff] [blame] | 4297 | int usableSpace; /* Bytes in pPage beyond the header */ |
| 4298 | int pageFlags; /* Value of pPage->aData[0] */ |
drh | 6019e16 | 2001-07-02 17:51:45 +0000 | [diff] [blame] | 4299 | int subtotal; /* Subtotal of bytes in cells on one page */ |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 4300 | int iSpace = 0; /* First unused byte of aSpace[] */ |
drh | c3b7057 | 2003-01-04 19:44:07 +0000 | [diff] [blame] | 4301 | MemPage *apOld[NB]; /* pPage and up to two siblings */ |
| 4302 | Pgno pgnoOld[NB]; /* Page numbers for each page in apOld[] */ |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 4303 | MemPage *apCopy[NB]; /* Private copies of apOld[] pages */ |
drh | a2fce64 | 2004-06-05 00:01:44 +0000 | [diff] [blame] | 4304 | MemPage *apNew[NB+2]; /* pPage and up to NB siblings after balancing */ |
| 4305 | Pgno pgnoNew[NB+2]; /* Page numbers for each page in apNew[] */ |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 4306 | u8 *apDiv[NB]; /* Divider cells in pParent */ |
drh | a2fce64 | 2004-06-05 00:01:44 +0000 | [diff] [blame] | 4307 | int cntNew[NB+2]; /* Index in aCell[] of cell after i-th page */ |
| 4308 | int szNew[NB+2]; /* Combined size of cells place on i-th page */ |
danielk1977 | 50f059b | 2005-03-29 02:54:03 +0000 | [diff] [blame] | 4309 | u8 **apCell = 0; /* All cells begin balanced */ |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 4310 | int *szCell; /* Local size of all cells in apCell[] */ |
| 4311 | u8 *aCopy[NB]; /* Space for holding data of apCopy[] */ |
| 4312 | u8 *aSpace; /* Space to hold copies of dividers cells */ |
danielk1977 | 4e17d14 | 2005-01-16 09:06:33 +0000 | [diff] [blame] | 4313 | #ifndef SQLITE_OMIT_AUTOVACUUM |
danielk1977 | ac11ee6 | 2005-01-15 12:45:51 +0000 | [diff] [blame] | 4314 | u8 *aFrom = 0; |
| 4315 | #endif |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 4316 | |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 4317 | /* |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4318 | ** Find the parent page. |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 4319 | */ |
drh | 3a4c141 | 2004-05-09 20:40:11 +0000 | [diff] [blame] | 4320 | assert( pPage->isInit ); |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 4321 | assert( sqlite3PagerIswriteable(pPage->pDbPage) ); |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 4322 | pBt = pPage->pBt; |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 4323 | pParent = pPage->pParent; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4324 | assert( pParent ); |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 4325 | if( SQLITE_OK!=(rc = sqlite3PagerWrite(pParent->pDbPage)) ){ |
danielk1977 | 07cb560 | 2006-01-20 10:55:05 +0000 | [diff] [blame] | 4326 | return rc; |
| 4327 | } |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4328 | TRACE(("BALANCE: begin page %d child of %d\n", pPage->pgno, pParent->pgno)); |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 4329 | |
drh | 615ae55 | 2005-01-16 23:21:00 +0000 | [diff] [blame] | 4330 | #ifndef SQLITE_OMIT_QUICKBALANCE |
drh | f222e71 | 2005-01-14 22:55:49 +0000 | [diff] [blame] | 4331 | /* |
| 4332 | ** A special case: If a new entry has just been inserted into a |
| 4333 | ** 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] | 4334 | ** 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] | 4335 | ** largest key) then use the special balance_quick() routine for |
| 4336 | ** balancing. balance_quick() is much faster and results in a tighter |
| 4337 | ** packing of data in the common case. |
| 4338 | */ |
danielk1977 | ac245ec | 2005-01-14 13:50:11 +0000 | [diff] [blame] | 4339 | if( pPage->leaf && |
| 4340 | pPage->intKey && |
| 4341 | pPage->leafData && |
| 4342 | pPage->nOverflow==1 && |
| 4343 | pPage->aOvfl[0].idx==pPage->nCell && |
danielk1977 | ac11ee6 | 2005-01-15 12:45:51 +0000 | [diff] [blame] | 4344 | pPage->pParent->pgno!=1 && |
danielk1977 | ac245ec | 2005-01-14 13:50:11 +0000 | [diff] [blame] | 4345 | get4byte(&pParent->aData[pParent->hdrOffset+8])==pPage->pgno |
| 4346 | ){ |
danielk1977 | ac11ee6 | 2005-01-15 12:45:51 +0000 | [diff] [blame] | 4347 | /* |
| 4348 | ** TODO: Check the siblings to the left of pPage. It may be that |
| 4349 | ** they are not full and no new page is required. |
| 4350 | */ |
danielk1977 | ac245ec | 2005-01-14 13:50:11 +0000 | [diff] [blame] | 4351 | return balance_quick(pPage, pParent); |
| 4352 | } |
| 4353 | #endif |
| 4354 | |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 4355 | /* |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 4356 | ** Find the cell in the parent page whose left child points back |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 4357 | ** to pPage. The "idx" variable is the index of that cell. If pPage |
| 4358 | ** is the rightmost child of pParent then set idx to pParent->nCell |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 4359 | */ |
drh | bb49aba | 2003-01-04 18:53:27 +0000 | [diff] [blame] | 4360 | if( pParent->idxShift ){ |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 4361 | Pgno pgno; |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 4362 | pgno = pPage->pgno; |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 4363 | assert( pgno==sqlite3PagerPagenumber(pPage->pDbPage) ); |
drh | bb49aba | 2003-01-04 18:53:27 +0000 | [diff] [blame] | 4364 | for(idx=0; idx<pParent->nCell; idx++){ |
danielk1977 | 1cc5ed8 | 2007-05-16 17:28:43 +0000 | [diff] [blame] | 4365 | if( get4byte(findCell(pParent, idx))==pgno ){ |
drh | bb49aba | 2003-01-04 18:53:27 +0000 | [diff] [blame] | 4366 | break; |
| 4367 | } |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 4368 | } |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 4369 | assert( idx<pParent->nCell |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4370 | || get4byte(&pParent->aData[pParent->hdrOffset+8])==pgno ); |
drh | bb49aba | 2003-01-04 18:53:27 +0000 | [diff] [blame] | 4371 | }else{ |
| 4372 | idx = pPage->idxParent; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 4373 | } |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 4374 | |
| 4375 | /* |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 4376 | ** Initialize variables so that it will be safe to jump |
drh | 5edc312 | 2001-09-13 21:53:09 +0000 | [diff] [blame] | 4377 | ** directly to balance_cleanup at any moment. |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 4378 | */ |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 4379 | nOld = nNew = 0; |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 4380 | sqlite3PagerRef(pParent->pDbPage); |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 4381 | |
| 4382 | /* |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 4383 | ** Find sibling pages to pPage and the cells in pParent that divide |
drh | c3b7057 | 2003-01-04 19:44:07 +0000 | [diff] [blame] | 4384 | ** the siblings. An attempt is made to find NN siblings on either |
| 4385 | ** side of pPage. More siblings are taken from one side, however, if |
| 4386 | ** pPage there are fewer than NN siblings on the other side. If pParent |
| 4387 | ** has NB or fewer children then all children of pParent are taken. |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 4388 | */ |
drh | c3b7057 | 2003-01-04 19:44:07 +0000 | [diff] [blame] | 4389 | nxDiv = idx - NN; |
| 4390 | if( nxDiv + NB > pParent->nCell ){ |
| 4391 | nxDiv = pParent->nCell - NB + 1; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 4392 | } |
drh | c3b7057 | 2003-01-04 19:44:07 +0000 | [diff] [blame] | 4393 | if( nxDiv<0 ){ |
| 4394 | nxDiv = 0; |
| 4395 | } |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 4396 | nDiv = 0; |
drh | c3b7057 | 2003-01-04 19:44:07 +0000 | [diff] [blame] | 4397 | for(i=0, k=nxDiv; i<NB; i++, k++){ |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 4398 | if( k<pParent->nCell ){ |
danielk1977 | 1cc5ed8 | 2007-05-16 17:28:43 +0000 | [diff] [blame] | 4399 | apDiv[i] = findCell(pParent, k); |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 4400 | nDiv++; |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 4401 | assert( !pParent->leaf ); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4402 | pgnoOld[i] = get4byte(apDiv[i]); |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 4403 | }else if( k==pParent->nCell ){ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4404 | pgnoOld[i] = get4byte(&pParent->aData[pParent->hdrOffset+8]); |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 4405 | }else{ |
| 4406 | break; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 4407 | } |
drh | de64713 | 2004-05-07 17:57:49 +0000 | [diff] [blame] | 4408 | rc = getAndInitPage(pBt, pgnoOld[i], &apOld[i], pParent); |
drh | 6019e16 | 2001-07-02 17:51:45 +0000 | [diff] [blame] | 4409 | if( rc ) goto balance_cleanup; |
drh | 428ae8c | 2003-01-04 16:48:09 +0000 | [diff] [blame] | 4410 | apOld[i]->idxParent = k; |
drh | 9102529 | 2004-05-03 19:49:32 +0000 | [diff] [blame] | 4411 | apCopy[i] = 0; |
| 4412 | assert( i==nOld ); |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 4413 | nOld++; |
danielk1977 | 634f298 | 2005-03-28 08:44:07 +0000 | [diff] [blame] | 4414 | nMaxCells += 1+apOld[i]->nCell+apOld[i]->nOverflow; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 4415 | } |
| 4416 | |
drh | 8d97f1f | 2005-05-05 18:14:13 +0000 | [diff] [blame] | 4417 | /* Make nMaxCells a multiple of 2 in order to preserve 8-byte |
| 4418 | ** alignment */ |
| 4419 | nMaxCells = (nMaxCells + 1)&~1; |
| 4420 | |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 4421 | /* |
danielk1977 | 634f298 | 2005-03-28 08:44:07 +0000 | [diff] [blame] | 4422 | ** Allocate space for memory structures |
| 4423 | */ |
| 4424 | apCell = sqliteMallocRaw( |
| 4425 | nMaxCells*sizeof(u8*) /* apCell */ |
| 4426 | + nMaxCells*sizeof(int) /* szCell */ |
drh | c96d853 | 2005-05-03 12:30:33 +0000 | [diff] [blame] | 4427 | + ROUND8(sizeof(MemPage))*NB /* aCopy */ |
drh | 07d183d | 2005-05-01 22:52:42 +0000 | [diff] [blame] | 4428 | + pBt->pageSize*(5+NB) /* aSpace */ |
drh | c96d853 | 2005-05-03 12:30:33 +0000 | [diff] [blame] | 4429 | + (ISAUTOVACUUM ? nMaxCells : 0) /* aFrom */ |
danielk1977 | 634f298 | 2005-03-28 08:44:07 +0000 | [diff] [blame] | 4430 | ); |
| 4431 | if( apCell==0 ){ |
| 4432 | rc = SQLITE_NOMEM; |
| 4433 | goto balance_cleanup; |
| 4434 | } |
| 4435 | szCell = (int*)&apCell[nMaxCells]; |
| 4436 | aCopy[0] = (u8*)&szCell[nMaxCells]; |
drh | c96d853 | 2005-05-03 12:30:33 +0000 | [diff] [blame] | 4437 | assert( ((aCopy[0] - (u8*)apCell) & 7)==0 ); /* 8-byte alignment required */ |
danielk1977 | 634f298 | 2005-03-28 08:44:07 +0000 | [diff] [blame] | 4438 | for(i=1; i<NB; i++){ |
drh | c96d853 | 2005-05-03 12:30:33 +0000 | [diff] [blame] | 4439 | aCopy[i] = &aCopy[i-1][pBt->pageSize+ROUND8(sizeof(MemPage))]; |
| 4440 | assert( ((aCopy[i] - (u8*)apCell) & 7)==0 ); /* 8-byte alignment required */ |
danielk1977 | 634f298 | 2005-03-28 08:44:07 +0000 | [diff] [blame] | 4441 | } |
drh | c96d853 | 2005-05-03 12:30:33 +0000 | [diff] [blame] | 4442 | aSpace = &aCopy[NB-1][pBt->pageSize+ROUND8(sizeof(MemPage))]; |
| 4443 | assert( ((aSpace - (u8*)apCell) & 7)==0 ); /* 8-byte alignment required */ |
danielk1977 | 634f298 | 2005-03-28 08:44:07 +0000 | [diff] [blame] | 4444 | #ifndef SQLITE_OMIT_AUTOVACUUM |
| 4445 | if( pBt->autoVacuum ){ |
drh | 07d183d | 2005-05-01 22:52:42 +0000 | [diff] [blame] | 4446 | aFrom = &aSpace[5*pBt->pageSize]; |
danielk1977 | 634f298 | 2005-03-28 08:44:07 +0000 | [diff] [blame] | 4447 | } |
| 4448 | #endif |
| 4449 | |
| 4450 | /* |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 4451 | ** Make copies of the content of pPage and its siblings into aOld[]. |
| 4452 | ** The rest of this function will use data from the copies rather |
| 4453 | ** that the original pages since the original pages will be in the |
| 4454 | ** process of being overwritten. |
| 4455 | */ |
| 4456 | for(i=0; i<nOld; i++){ |
drh | 07d183d | 2005-05-01 22:52:42 +0000 | [diff] [blame] | 4457 | MemPage *p = apCopy[i] = (MemPage*)&aCopy[i][pBt->pageSize]; |
drh | 07d183d | 2005-05-01 22:52:42 +0000 | [diff] [blame] | 4458 | p->aData = &((u8*)p)[-pBt->pageSize]; |
| 4459 | memcpy(p->aData, apOld[i]->aData, pBt->pageSize + sizeof(MemPage)); |
| 4460 | /* The memcpy() above changes the value of p->aData so we have to |
| 4461 | ** set it again. */ |
drh | 07d183d | 2005-05-01 22:52:42 +0000 | [diff] [blame] | 4462 | p->aData = &((u8*)p)[-pBt->pageSize]; |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 4463 | } |
| 4464 | |
| 4465 | /* |
| 4466 | ** Load pointers to all cells on sibling pages and the divider cells |
| 4467 | ** into the local apCell[] array. Make copies of the divider cells |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 4468 | ** into space obtained form aSpace[] and remove the the divider Cells |
| 4469 | ** from pParent. |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 4470 | ** |
| 4471 | ** If the siblings are on leaf pages, then the child pointers of the |
| 4472 | ** divider cells are stripped from the cells before they are copied |
drh | 96f5b76 | 2004-05-16 16:24:36 +0000 | [diff] [blame] | 4473 | ** into aSpace[]. In this way, all cells in apCell[] are without |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 4474 | ** child pointers. If siblings are not leaves, then all cell in |
| 4475 | ** apCell[] include child pointers. Either way, all cells in apCell[] |
| 4476 | ** are alike. |
drh | 96f5b76 | 2004-05-16 16:24:36 +0000 | [diff] [blame] | 4477 | ** |
| 4478 | ** leafCorrection: 4 if pPage is a leaf. 0 if pPage is not a leaf. |
| 4479 | ** leafData: 1 if pPage holds key+data and pParent holds only keys. |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 4480 | */ |
| 4481 | nCell = 0; |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 4482 | leafCorrection = pPage->leaf*4; |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 4483 | leafData = pPage->leafData && pPage->leaf; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 4484 | for(i=0; i<nOld; i++){ |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 4485 | MemPage *pOld = apCopy[i]; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4486 | int limit = pOld->nCell+pOld->nOverflow; |
| 4487 | for(j=0; j<limit; j++){ |
danielk1977 | 634f298 | 2005-03-28 08:44:07 +0000 | [diff] [blame] | 4488 | assert( nCell<nMaxCells ); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4489 | apCell[nCell] = findOverflowCell(pOld, j); |
| 4490 | szCell[nCell] = cellSizePtr(pOld, apCell[nCell]); |
danielk1977 | ac11ee6 | 2005-01-15 12:45:51 +0000 | [diff] [blame] | 4491 | #ifndef SQLITE_OMIT_AUTOVACUUM |
| 4492 | if( pBt->autoVacuum ){ |
| 4493 | int a; |
| 4494 | aFrom[nCell] = i; |
| 4495 | for(a=0; a<pOld->nOverflow; a++){ |
| 4496 | if( pOld->aOvfl[a].pCell==apCell[nCell] ){ |
| 4497 | aFrom[nCell] = 0xFF; |
| 4498 | break; |
| 4499 | } |
| 4500 | } |
| 4501 | } |
| 4502 | #endif |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 4503 | nCell++; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 4504 | } |
| 4505 | if( i<nOld-1 ){ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4506 | int sz = cellSizePtr(pParent, apDiv[i]); |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 4507 | if( leafData ){ |
drh | 96f5b76 | 2004-05-16 16:24:36 +0000 | [diff] [blame] | 4508 | /* With the LEAFDATA flag, pParent cells hold only INTKEYs that |
| 4509 | ** are duplicates of keys on the child pages. We need to remove |
| 4510 | ** the divider cells from pParent, but the dividers cells are not |
| 4511 | ** added to apCell[] because they are duplicates of child cells. |
| 4512 | */ |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 4513 | dropCell(pParent, nxDiv, sz); |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 4514 | }else{ |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 4515 | u8 *pTemp; |
danielk1977 | 634f298 | 2005-03-28 08:44:07 +0000 | [diff] [blame] | 4516 | assert( nCell<nMaxCells ); |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 4517 | szCell[nCell] = sz; |
| 4518 | pTemp = &aSpace[iSpace]; |
| 4519 | iSpace += sz; |
drh | 07d183d | 2005-05-01 22:52:42 +0000 | [diff] [blame] | 4520 | assert( iSpace<=pBt->pageSize*5 ); |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 4521 | memcpy(pTemp, apDiv[i], sz); |
| 4522 | apCell[nCell] = pTemp+leafCorrection; |
danielk1977 | ac11ee6 | 2005-01-15 12:45:51 +0000 | [diff] [blame] | 4523 | #ifndef SQLITE_OMIT_AUTOVACUUM |
| 4524 | if( pBt->autoVacuum ){ |
| 4525 | aFrom[nCell] = 0xFF; |
| 4526 | } |
| 4527 | #endif |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 4528 | dropCell(pParent, nxDiv, sz); |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 4529 | szCell[nCell] -= leafCorrection; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4530 | assert( get4byte(pTemp)==pgnoOld[i] ); |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 4531 | if( !pOld->leaf ){ |
| 4532 | assert( leafCorrection==0 ); |
| 4533 | /* The right pointer of the child page pOld becomes the left |
| 4534 | ** pointer of the divider cell */ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4535 | memcpy(apCell[nCell], &pOld->aData[pOld->hdrOffset+8], 4); |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 4536 | }else{ |
| 4537 | assert( leafCorrection==4 ); |
danielk1977 | 39c9604 | 2007-05-12 10:41:47 +0000 | [diff] [blame] | 4538 | if( szCell[nCell]<4 ){ |
| 4539 | /* Do not allow any cells smaller than 4 bytes. */ |
| 4540 | szCell[nCell] = 4; |
| 4541 | } |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 4542 | } |
| 4543 | nCell++; |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 4544 | } |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 4545 | } |
| 4546 | } |
| 4547 | |
| 4548 | /* |
drh | 6019e16 | 2001-07-02 17:51:45 +0000 | [diff] [blame] | 4549 | ** Figure out the number of pages needed to hold all nCell cells. |
| 4550 | ** Store this number in "k". Also compute szNew[] which is the total |
| 4551 | ** 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] | 4552 | ** in apCell[] of the cell that divides page i from page i+1. |
drh | 6019e16 | 2001-07-02 17:51:45 +0000 | [diff] [blame] | 4553 | ** cntNew[k] should equal nCell. |
| 4554 | ** |
drh | 96f5b76 | 2004-05-16 16:24:36 +0000 | [diff] [blame] | 4555 | ** Values computed by this block: |
| 4556 | ** |
| 4557 | ** k: The total number of sibling pages |
| 4558 | ** szNew[i]: Spaced used on the i-th sibling page. |
| 4559 | ** cntNew[i]: Index in apCell[] and szCell[] for the first cell to |
| 4560 | ** the right of the i-th sibling page. |
| 4561 | ** usableSpace: Number of bytes of space available on each sibling. |
| 4562 | ** |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 4563 | */ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4564 | usableSpace = pBt->usableSize - 12 + leafCorrection; |
drh | 6019e16 | 2001-07-02 17:51:45 +0000 | [diff] [blame] | 4565 | for(subtotal=k=i=0; i<nCell; i++){ |
danielk1977 | 634f298 | 2005-03-28 08:44:07 +0000 | [diff] [blame] | 4566 | assert( i<nMaxCells ); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4567 | subtotal += szCell[i] + 2; |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 4568 | if( subtotal > usableSpace ){ |
drh | 6019e16 | 2001-07-02 17:51:45 +0000 | [diff] [blame] | 4569 | szNew[k] = subtotal - szCell[i]; |
| 4570 | cntNew[k] = i; |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 4571 | if( leafData ){ i--; } |
drh | 6019e16 | 2001-07-02 17:51:45 +0000 | [diff] [blame] | 4572 | subtotal = 0; |
| 4573 | k++; |
| 4574 | } |
| 4575 | } |
| 4576 | szNew[k] = subtotal; |
| 4577 | cntNew[k] = nCell; |
| 4578 | k++; |
drh | 96f5b76 | 2004-05-16 16:24:36 +0000 | [diff] [blame] | 4579 | |
| 4580 | /* |
| 4581 | ** The packing computed by the previous block is biased toward the siblings |
| 4582 | ** on the left side. The left siblings are always nearly full, while the |
| 4583 | ** right-most sibling might be nearly empty. This block of code attempts |
| 4584 | ** to adjust the packing of siblings to get a better balance. |
| 4585 | ** |
| 4586 | ** This adjustment is more than an optimization. The packing above might |
| 4587 | ** be so out of balance as to be illegal. For example, the right-most |
| 4588 | ** sibling might be completely empty. This adjustment is not optional. |
| 4589 | */ |
drh | 6019e16 | 2001-07-02 17:51:45 +0000 | [diff] [blame] | 4590 | for(i=k-1; i>0; i--){ |
drh | 96f5b76 | 2004-05-16 16:24:36 +0000 | [diff] [blame] | 4591 | int szRight = szNew[i]; /* Size of sibling on the right */ |
| 4592 | int szLeft = szNew[i-1]; /* Size of sibling on the left */ |
| 4593 | int r; /* Index of right-most cell in left sibling */ |
| 4594 | int d; /* Index of first cell to the left of right sibling */ |
| 4595 | |
| 4596 | r = cntNew[i-1] - 1; |
| 4597 | d = r + 1 - leafData; |
danielk1977 | 634f298 | 2005-03-28 08:44:07 +0000 | [diff] [blame] | 4598 | assert( d<nMaxCells ); |
| 4599 | assert( r<nMaxCells ); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4600 | while( szRight==0 || szRight+szCell[d]+2<=szLeft-(szCell[r]+2) ){ |
| 4601 | szRight += szCell[d] + 2; |
| 4602 | szLeft -= szCell[r] + 2; |
drh | 6019e16 | 2001-07-02 17:51:45 +0000 | [diff] [blame] | 4603 | cntNew[i-1]--; |
drh | 96f5b76 | 2004-05-16 16:24:36 +0000 | [diff] [blame] | 4604 | r = cntNew[i-1] - 1; |
| 4605 | d = r + 1 - leafData; |
drh | 6019e16 | 2001-07-02 17:51:45 +0000 | [diff] [blame] | 4606 | } |
drh | 96f5b76 | 2004-05-16 16:24:36 +0000 | [diff] [blame] | 4607 | szNew[i] = szRight; |
| 4608 | szNew[i-1] = szLeft; |
drh | 6019e16 | 2001-07-02 17:51:45 +0000 | [diff] [blame] | 4609 | } |
drh | 09d0deb | 2005-08-02 17:13:09 +0000 | [diff] [blame] | 4610 | |
| 4611 | /* Either we found one or more cells (cntnew[0])>0) or we are the |
| 4612 | ** a virtual root page. A virtual root page is when the real root |
| 4613 | ** page is page 1 and we are the only child of that page. |
| 4614 | */ |
| 4615 | assert( cntNew[0]>0 || (pParent->pgno==1 && pParent->nCell==0) ); |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 4616 | |
| 4617 | /* |
drh | 6b30867 | 2002-07-08 02:16:37 +0000 | [diff] [blame] | 4618 | ** Allocate k new pages. Reuse old pages where possible. |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 4619 | */ |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 4620 | assert( pPage->pgno>1 ); |
| 4621 | pageFlags = pPage->aData[0]; |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 4622 | for(i=0; i<k; i++){ |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 4623 | MemPage *pNew; |
drh | 6b30867 | 2002-07-08 02:16:37 +0000 | [diff] [blame] | 4624 | if( i<nOld ){ |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 4625 | pNew = apNew[i] = apOld[i]; |
drh | 6b30867 | 2002-07-08 02:16:37 +0000 | [diff] [blame] | 4626 | pgnoNew[i] = pgnoOld[i]; |
| 4627 | apOld[i] = 0; |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 4628 | rc = sqlite3PagerWrite(pNew->pDbPage); |
drh | f534544 | 2007-04-09 12:45:02 +0000 | [diff] [blame] | 4629 | nNew++; |
danielk1977 | 2812956 | 2005-01-11 10:25:06 +0000 | [diff] [blame] | 4630 | if( rc ) goto balance_cleanup; |
drh | 6b30867 | 2002-07-08 02:16:37 +0000 | [diff] [blame] | 4631 | }else{ |
drh | 7aa8f85 | 2006-03-28 00:24:44 +0000 | [diff] [blame] | 4632 | assert( i>0 ); |
drh | 4f0c587 | 2007-03-26 22:05:01 +0000 | [diff] [blame] | 4633 | rc = allocateBtreePage(pBt, &pNew, &pgnoNew[i], pgnoNew[i-1], 0); |
drh | 6b30867 | 2002-07-08 02:16:37 +0000 | [diff] [blame] | 4634 | if( rc ) goto balance_cleanup; |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 4635 | apNew[i] = pNew; |
drh | f534544 | 2007-04-09 12:45:02 +0000 | [diff] [blame] | 4636 | nNew++; |
drh | 6b30867 | 2002-07-08 02:16:37 +0000 | [diff] [blame] | 4637 | } |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 4638 | zeroPage(pNew, pageFlags); |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 4639 | } |
| 4640 | |
danielk1977 | 299b187 | 2004-11-22 10:02:10 +0000 | [diff] [blame] | 4641 | /* Free any old pages that were not reused as new pages. |
| 4642 | */ |
| 4643 | while( i<nOld ){ |
| 4644 | rc = freePage(apOld[i]); |
| 4645 | if( rc ) goto balance_cleanup; |
| 4646 | releasePage(apOld[i]); |
| 4647 | apOld[i] = 0; |
| 4648 | i++; |
| 4649 | } |
| 4650 | |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 4651 | /* |
drh | f9ffac9 | 2002-03-02 19:00:31 +0000 | [diff] [blame] | 4652 | ** Put the new pages in accending order. This helps to |
| 4653 | ** keep entries in the disk file in order so that a scan |
| 4654 | ** of the table is a linear scan through the file. That |
| 4655 | ** in turn helps the operating system to deliver pages |
| 4656 | ** from the disk more rapidly. |
| 4657 | ** |
| 4658 | ** An O(n^2) insertion sort algorithm is used, but since |
drh | c3b7057 | 2003-01-04 19:44:07 +0000 | [diff] [blame] | 4659 | ** n is never more than NB (a small constant), that should |
| 4660 | ** not be a problem. |
drh | f9ffac9 | 2002-03-02 19:00:31 +0000 | [diff] [blame] | 4661 | ** |
drh | c3b7057 | 2003-01-04 19:44:07 +0000 | [diff] [blame] | 4662 | ** When NB==3, this one optimization makes the database |
| 4663 | ** about 25% faster for large insertions and deletions. |
drh | f9ffac9 | 2002-03-02 19:00:31 +0000 | [diff] [blame] | 4664 | */ |
| 4665 | for(i=0; i<k-1; i++){ |
| 4666 | int minV = pgnoNew[i]; |
| 4667 | int minI = i; |
| 4668 | for(j=i+1; j<k; j++){ |
drh | 7d02cb7 | 2003-06-04 16:24:39 +0000 | [diff] [blame] | 4669 | if( pgnoNew[j]<(unsigned)minV ){ |
drh | f9ffac9 | 2002-03-02 19:00:31 +0000 | [diff] [blame] | 4670 | minI = j; |
| 4671 | minV = pgnoNew[j]; |
| 4672 | } |
| 4673 | } |
| 4674 | if( minI>i ){ |
| 4675 | int t; |
| 4676 | MemPage *pT; |
| 4677 | t = pgnoNew[i]; |
| 4678 | pT = apNew[i]; |
| 4679 | pgnoNew[i] = pgnoNew[minI]; |
| 4680 | apNew[i] = apNew[minI]; |
| 4681 | pgnoNew[minI] = t; |
| 4682 | apNew[minI] = pT; |
| 4683 | } |
| 4684 | } |
drh | a2fce64 | 2004-06-05 00:01:44 +0000 | [diff] [blame] | 4685 | 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] | 4686 | pgnoOld[0], |
| 4687 | nOld>=2 ? pgnoOld[1] : 0, |
| 4688 | nOld>=3 ? pgnoOld[2] : 0, |
drh | 10c0fa6 | 2004-05-18 12:50:17 +0000 | [diff] [blame] | 4689 | pgnoNew[0], szNew[0], |
| 4690 | nNew>=2 ? pgnoNew[1] : 0, nNew>=2 ? szNew[1] : 0, |
| 4691 | nNew>=3 ? pgnoNew[2] : 0, nNew>=3 ? szNew[2] : 0, |
drh | a2fce64 | 2004-06-05 00:01:44 +0000 | [diff] [blame] | 4692 | nNew>=4 ? pgnoNew[3] : 0, nNew>=4 ? szNew[3] : 0, |
| 4693 | nNew>=5 ? pgnoNew[4] : 0, nNew>=5 ? szNew[4] : 0)); |
drh | 24cd67e | 2004-05-10 16:18:47 +0000 | [diff] [blame] | 4694 | |
drh | f9ffac9 | 2002-03-02 19:00:31 +0000 | [diff] [blame] | 4695 | /* |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 4696 | ** Evenly distribute the data in apCell[] across the new pages. |
| 4697 | ** Insert divider cells into pParent as necessary. |
| 4698 | */ |
| 4699 | j = 0; |
| 4700 | for(i=0; i<nNew; i++){ |
danielk1977 | ac11ee6 | 2005-01-15 12:45:51 +0000 | [diff] [blame] | 4701 | /* Assemble the new sibling page. */ |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 4702 | MemPage *pNew = apNew[i]; |
drh | 19642e5 | 2005-03-29 13:17:45 +0000 | [diff] [blame] | 4703 | assert( j<nMaxCells ); |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 4704 | assert( pNew->pgno==pgnoNew[i] ); |
drh | fa1a98a | 2004-05-14 19:08:17 +0000 | [diff] [blame] | 4705 | assemblePage(pNew, cntNew[i]-j, &apCell[j], &szCell[j]); |
drh | 09d0deb | 2005-08-02 17:13:09 +0000 | [diff] [blame] | 4706 | assert( pNew->nCell>0 || (nNew==1 && cntNew[0]==0) ); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4707 | assert( pNew->nOverflow==0 ); |
danielk1977 | ac11ee6 | 2005-01-15 12:45:51 +0000 | [diff] [blame] | 4708 | |
| 4709 | #ifndef SQLITE_OMIT_AUTOVACUUM |
| 4710 | /* If this is an auto-vacuum database, update the pointer map entries |
| 4711 | ** that point to the siblings that were rearranged. These can be: left |
| 4712 | ** children of cells, the right-child of the page, or overflow pages |
| 4713 | ** pointed to by cells. |
| 4714 | */ |
| 4715 | if( pBt->autoVacuum ){ |
| 4716 | for(k=j; k<cntNew[i]; k++){ |
danielk1977 | 634f298 | 2005-03-28 08:44:07 +0000 | [diff] [blame] | 4717 | assert( k<nMaxCells ); |
danielk1977 | ac11ee6 | 2005-01-15 12:45:51 +0000 | [diff] [blame] | 4718 | if( aFrom[k]==0xFF || apCopy[aFrom[k]]->pgno!=pNew->pgno ){ |
danielk1977 | 79a40da | 2005-01-16 08:00:01 +0000 | [diff] [blame] | 4719 | rc = ptrmapPutOvfl(pNew, k-j); |
| 4720 | if( rc!=SQLITE_OK ){ |
| 4721 | goto balance_cleanup; |
danielk1977 | ac11ee6 | 2005-01-15 12:45:51 +0000 | [diff] [blame] | 4722 | } |
| 4723 | } |
| 4724 | } |
| 4725 | } |
| 4726 | #endif |
| 4727 | |
| 4728 | j = cntNew[i]; |
| 4729 | |
| 4730 | /* If the sibling page assembled above was not the right-most sibling, |
| 4731 | ** insert a divider cell into the parent page. |
| 4732 | */ |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 4733 | if( i<nNew-1 && j<nCell ){ |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 4734 | u8 *pCell; |
drh | 24cd67e | 2004-05-10 16:18:47 +0000 | [diff] [blame] | 4735 | u8 *pTemp; |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 4736 | int sz; |
danielk1977 | 634f298 | 2005-03-28 08:44:07 +0000 | [diff] [blame] | 4737 | |
| 4738 | assert( j<nMaxCells ); |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 4739 | pCell = apCell[j]; |
| 4740 | sz = szCell[j] + leafCorrection; |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 4741 | if( !pNew->leaf ){ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4742 | memcpy(&pNew->aData[8], pCell, 4); |
drh | 24cd67e | 2004-05-10 16:18:47 +0000 | [diff] [blame] | 4743 | pTemp = 0; |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 4744 | }else if( leafData ){ |
danielk1977 | ac11ee6 | 2005-01-15 12:45:51 +0000 | [diff] [blame] | 4745 | /* If the tree is a leaf-data tree, and the siblings are leaves, |
| 4746 | ** then there is no divider cell in apCell[]. Instead, the divider |
| 4747 | ** cell consists of the integer key for the right-most cell of |
| 4748 | ** the sibling-page assembled above only. |
| 4749 | */ |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 4750 | CellInfo info; |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 4751 | j--; |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 4752 | sqlite3BtreeParseCellPtr(pNew, apCell[j], &info); |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 4753 | pCell = &aSpace[iSpace]; |
drh | b026e05 | 2007-05-02 01:34:31 +0000 | [diff] [blame] | 4754 | fillInCell(pParent, pCell, 0, info.nKey, 0, 0, 0, &sz); |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 4755 | iSpace += sz; |
drh | 07d183d | 2005-05-01 22:52:42 +0000 | [diff] [blame] | 4756 | assert( iSpace<=pBt->pageSize*5 ); |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 4757 | pTemp = 0; |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 4758 | }else{ |
| 4759 | pCell -= 4; |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 4760 | pTemp = &aSpace[iSpace]; |
| 4761 | iSpace += sz; |
drh | 07d183d | 2005-05-01 22:52:42 +0000 | [diff] [blame] | 4762 | assert( iSpace<=pBt->pageSize*5 ); |
danielk1977 | 4aeff62 | 2007-05-12 09:30:47 +0000 | [diff] [blame] | 4763 | /* Obscure case for non-leaf-data trees: If the cell at pCell was |
| 4764 | ** previously stored on a leaf node, and it's reported size was 4 |
| 4765 | ** bytes, then it may actually be smaller than this |
| 4766 | ** (see sqlite3BtreeParseCellPtr(), 4 bytes is the minimum size of |
| 4767 | ** any cell). But it's important to pass the correct size to |
| 4768 | ** insertCell(), so reparse the cell now. |
| 4769 | ** |
| 4770 | ** Note that this can never happen in an SQLite data file, as all |
| 4771 | ** cells are at least 4 bytes. It only happens in b-trees used |
| 4772 | ** to evaluate "IN (SELECT ...)" and similar clauses. |
| 4773 | */ |
| 4774 | if( szCell[j]==4 ){ |
| 4775 | assert(leafCorrection==4); |
| 4776 | sz = cellSizePtr(pParent, pCell); |
| 4777 | } |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 4778 | } |
danielk1977 | a3ad5e7 | 2005-01-07 08:56:44 +0000 | [diff] [blame] | 4779 | rc = insertCell(pParent, nxDiv, pCell, sz, pTemp, 4); |
danielk1977 | e80463b | 2004-11-03 03:01:16 +0000 | [diff] [blame] | 4780 | if( rc!=SQLITE_OK ) goto balance_cleanup; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4781 | put4byte(findOverflowCell(pParent,nxDiv), pNew->pgno); |
danielk1977 | ac11ee6 | 2005-01-15 12:45:51 +0000 | [diff] [blame] | 4782 | #ifndef SQLITE_OMIT_AUTOVACUUM |
| 4783 | /* If this is an auto-vacuum database, and not a leaf-data tree, |
| 4784 | ** then update the pointer map with an entry for the overflow page |
| 4785 | ** that the cell just inserted points to (if any). |
| 4786 | */ |
| 4787 | if( pBt->autoVacuum && !leafData ){ |
danielk1977 | 79a40da | 2005-01-16 08:00:01 +0000 | [diff] [blame] | 4788 | rc = ptrmapPutOvfl(pParent, nxDiv); |
| 4789 | if( rc!=SQLITE_OK ){ |
| 4790 | goto balance_cleanup; |
danielk1977 | ac11ee6 | 2005-01-15 12:45:51 +0000 | [diff] [blame] | 4791 | } |
| 4792 | } |
| 4793 | #endif |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 4794 | j++; |
| 4795 | nxDiv++; |
| 4796 | } |
| 4797 | } |
drh | 6019e16 | 2001-07-02 17:51:45 +0000 | [diff] [blame] | 4798 | assert( j==nCell ); |
drh | 7aa8f85 | 2006-03-28 00:24:44 +0000 | [diff] [blame] | 4799 | assert( nOld>0 ); |
| 4800 | assert( nNew>0 ); |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 4801 | if( (pageFlags & PTF_LEAF)==0 ){ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4802 | memcpy(&apNew[nNew-1]->aData[8], &apCopy[nOld-1]->aData[8], 4); |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 4803 | } |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4804 | if( nxDiv==pParent->nCell+pParent->nOverflow ){ |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 4805 | /* Right-most sibling is the right-most child of pParent */ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4806 | put4byte(&pParent->aData[pParent->hdrOffset+8], pgnoNew[nNew-1]); |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 4807 | }else{ |
| 4808 | /* Right-most sibling is the left child of the first entry in pParent |
| 4809 | ** past the right-most divider entry */ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4810 | put4byte(findOverflowCell(pParent, nxDiv), pgnoNew[nNew-1]); |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 4811 | } |
| 4812 | |
| 4813 | /* |
| 4814 | ** Reparent children of all cells. |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 4815 | */ |
| 4816 | for(i=0; i<nNew; i++){ |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 4817 | rc = reparentChildPages(apNew[i]); |
| 4818 | if( rc!=SQLITE_OK ) goto balance_cleanup; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 4819 | } |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 4820 | rc = reparentChildPages(pParent); |
| 4821 | if( rc!=SQLITE_OK ) goto balance_cleanup; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 4822 | |
| 4823 | /* |
drh | 3a4c141 | 2004-05-09 20:40:11 +0000 | [diff] [blame] | 4824 | ** Balance the parent page. Note that the current page (pPage) might |
danielk1977 | ac11ee6 | 2005-01-15 12:45:51 +0000 | [diff] [blame] | 4825 | ** have been added to the freelist so it might no longer be initialized. |
drh | 3a4c141 | 2004-05-09 20:40:11 +0000 | [diff] [blame] | 4826 | ** But the parent page will always be initialized. |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 4827 | */ |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 4828 | assert( pParent->isInit ); |
danielk1977 | ac245ec | 2005-01-14 13:50:11 +0000 | [diff] [blame] | 4829 | rc = balance(pParent, 0); |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 4830 | |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 4831 | /* |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 4832 | ** Cleanup before returning. |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 4833 | */ |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 4834 | balance_cleanup: |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 4835 | sqliteFree(apCell); |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 4836 | for(i=0; i<nOld; i++){ |
drh | 9102529 | 2004-05-03 19:49:32 +0000 | [diff] [blame] | 4837 | releasePage(apOld[i]); |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 4838 | } |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 4839 | for(i=0; i<nNew; i++){ |
drh | 9102529 | 2004-05-03 19:49:32 +0000 | [diff] [blame] | 4840 | releasePage(apNew[i]); |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 4841 | } |
drh | 9102529 | 2004-05-03 19:49:32 +0000 | [diff] [blame] | 4842 | releasePage(pParent); |
drh | 3a4c141 | 2004-05-09 20:40:11 +0000 | [diff] [blame] | 4843 | TRACE(("BALANCE: finished with %d: old=%d new=%d cells=%d\n", |
| 4844 | pPage->pgno, nOld, nNew, nCell)); |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 4845 | return rc; |
| 4846 | } |
| 4847 | |
| 4848 | /* |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4849 | ** This routine is called for the root page of a btree when the root |
| 4850 | ** page contains no cells. This is an opportunity to make the tree |
| 4851 | ** shallower by one level. |
| 4852 | */ |
| 4853 | static int balance_shallower(MemPage *pPage){ |
| 4854 | MemPage *pChild; /* The only child page of pPage */ |
| 4855 | Pgno pgnoChild; /* Page number for pChild */ |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 4856 | int rc = SQLITE_OK; /* Return code from subprocedures */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 4857 | BtShared *pBt; /* The main BTree structure */ |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 4858 | int mxCellPerPage; /* Maximum number of cells per page */ |
| 4859 | u8 **apCell; /* All cells from pages being balanced */ |
| 4860 | int *szCell; /* Local size of all cells */ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4861 | |
| 4862 | assert( pPage->pParent==0 ); |
| 4863 | assert( pPage->nCell==0 ); |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 4864 | pBt = pPage->pBt; |
| 4865 | mxCellPerPage = MX_CELL(pBt); |
| 4866 | apCell = sqliteMallocRaw( mxCellPerPage*(sizeof(u8*)+sizeof(int)) ); |
| 4867 | if( apCell==0 ) return SQLITE_NOMEM; |
| 4868 | szCell = (int*)&apCell[mxCellPerPage]; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4869 | if( pPage->leaf ){ |
| 4870 | /* The table is completely empty */ |
| 4871 | TRACE(("BALANCE: empty table %d\n", pPage->pgno)); |
| 4872 | }else{ |
| 4873 | /* The root page is empty but has one child. Transfer the |
| 4874 | ** information from that one child into the root page if it |
| 4875 | ** will fit. This reduces the depth of the tree by one. |
| 4876 | ** |
| 4877 | ** If the root page is page 1, it has less space available than |
| 4878 | ** its child (due to the 100 byte header that occurs at the beginning |
| 4879 | ** of the database fle), so it might not be able to hold all of the |
| 4880 | ** information currently contained in the child. If this is the |
| 4881 | ** case, then do not do the transfer. Leave page 1 empty except |
| 4882 | ** for the right-pointer to the child page. The child page becomes |
| 4883 | ** the virtual root of the tree. |
| 4884 | */ |
| 4885 | pgnoChild = get4byte(&pPage->aData[pPage->hdrOffset+8]); |
| 4886 | assert( pgnoChild>0 ); |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 4887 | assert( pgnoChild<=sqlite3PagerPagecount(pPage->pBt->pPager) ); |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 4888 | rc = sqlite3BtreeGetPage(pPage->pBt, pgnoChild, &pChild, 0); |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 4889 | if( rc ) goto end_shallow_balance; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4890 | if( pPage->pgno==1 ){ |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 4891 | rc = sqlite3BtreeInitPage(pChild, pPage); |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 4892 | if( rc ) goto end_shallow_balance; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4893 | assert( pChild->nOverflow==0 ); |
| 4894 | if( pChild->nFree>=100 ){ |
| 4895 | /* The child information will fit on the root page, so do the |
| 4896 | ** copy */ |
| 4897 | int i; |
| 4898 | zeroPage(pPage, pChild->aData[0]); |
| 4899 | for(i=0; i<pChild->nCell; i++){ |
danielk1977 | 1cc5ed8 | 2007-05-16 17:28:43 +0000 | [diff] [blame] | 4900 | apCell[i] = findCell(pChild,i); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4901 | szCell[i] = cellSizePtr(pChild, apCell[i]); |
| 4902 | } |
| 4903 | assemblePage(pPage, pChild->nCell, apCell, szCell); |
danielk1977 | ae82558 | 2004-11-23 09:06:55 +0000 | [diff] [blame] | 4904 | /* Copy the right-pointer of the child to the parent. */ |
| 4905 | put4byte(&pPage->aData[pPage->hdrOffset+8], |
| 4906 | get4byte(&pChild->aData[pChild->hdrOffset+8])); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4907 | freePage(pChild); |
| 4908 | TRACE(("BALANCE: child %d transfer to page 1\n", pChild->pgno)); |
| 4909 | }else{ |
| 4910 | /* The child has more information that will fit on the root. |
| 4911 | ** The tree is already balanced. Do nothing. */ |
| 4912 | TRACE(("BALANCE: child %d will not fit on page 1\n", pChild->pgno)); |
| 4913 | } |
| 4914 | }else{ |
| 4915 | memcpy(pPage->aData, pChild->aData, pPage->pBt->usableSize); |
| 4916 | pPage->isInit = 0; |
| 4917 | pPage->pParent = 0; |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 4918 | rc = sqlite3BtreeInitPage(pPage, 0); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4919 | assert( rc==SQLITE_OK ); |
| 4920 | freePage(pChild); |
| 4921 | TRACE(("BALANCE: transfer child %d into root %d\n", |
| 4922 | pChild->pgno, pPage->pgno)); |
| 4923 | } |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 4924 | rc = reparentChildPages(pPage); |
danielk1977 | ac11ee6 | 2005-01-15 12:45:51 +0000 | [diff] [blame] | 4925 | assert( pPage->nOverflow==0 ); |
| 4926 | #ifndef SQLITE_OMIT_AUTOVACUUM |
| 4927 | if( pBt->autoVacuum ){ |
danielk1977 | aac0a38 | 2005-01-16 11:07:06 +0000 | [diff] [blame] | 4928 | int i; |
danielk1977 | ac11ee6 | 2005-01-15 12:45:51 +0000 | [diff] [blame] | 4929 | for(i=0; i<pPage->nCell; i++){ |
danielk1977 | 79a40da | 2005-01-16 08:00:01 +0000 | [diff] [blame] | 4930 | rc = ptrmapPutOvfl(pPage, i); |
| 4931 | if( rc!=SQLITE_OK ){ |
| 4932 | goto end_shallow_balance; |
danielk1977 | ac11ee6 | 2005-01-15 12:45:51 +0000 | [diff] [blame] | 4933 | } |
| 4934 | } |
| 4935 | } |
| 4936 | #endif |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 4937 | if( rc!=SQLITE_OK ) goto end_shallow_balance; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4938 | releasePage(pChild); |
| 4939 | } |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 4940 | end_shallow_balance: |
| 4941 | sqliteFree(apCell); |
| 4942 | return rc; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4943 | } |
| 4944 | |
| 4945 | |
| 4946 | /* |
| 4947 | ** The root page is overfull |
| 4948 | ** |
| 4949 | ** When this happens, Create a new child page and copy the |
| 4950 | ** contents of the root into the child. Then make the root |
| 4951 | ** page an empty page with rightChild pointing to the new |
| 4952 | ** child. Finally, call balance_internal() on the new child |
| 4953 | ** to cause it to split. |
| 4954 | */ |
| 4955 | static int balance_deeper(MemPage *pPage){ |
| 4956 | int rc; /* Return value from subprocedures */ |
| 4957 | MemPage *pChild; /* Pointer to a new child page */ |
| 4958 | Pgno pgnoChild; /* Page number of the new child page */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 4959 | BtShared *pBt; /* The BTree */ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4960 | int usableSize; /* Total usable size of a page */ |
| 4961 | u8 *data; /* Content of the parent page */ |
| 4962 | u8 *cdata; /* Content of the child page */ |
| 4963 | int hdr; /* Offset to page header in parent */ |
| 4964 | int brk; /* Offset to content of first cell in parent */ |
| 4965 | |
| 4966 | assert( pPage->pParent==0 ); |
| 4967 | assert( pPage->nOverflow>0 ); |
| 4968 | pBt = pPage->pBt; |
drh | 4f0c587 | 2007-03-26 22:05:01 +0000 | [diff] [blame] | 4969 | rc = allocateBtreePage(pBt, &pChild, &pgnoChild, pPage->pgno, 0); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4970 | if( rc ) return rc; |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 4971 | assert( sqlite3PagerIswriteable(pChild->pDbPage) ); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4972 | usableSize = pBt->usableSize; |
| 4973 | data = pPage->aData; |
| 4974 | hdr = pPage->hdrOffset; |
| 4975 | brk = get2byte(&data[hdr+5]); |
| 4976 | cdata = pChild->aData; |
| 4977 | memcpy(cdata, &data[hdr], pPage->cellOffset+2*pPage->nCell-hdr); |
| 4978 | memcpy(&cdata[brk], &data[brk], usableSize-brk); |
danielk1977 | c7dc753 | 2004-11-17 10:22:03 +0000 | [diff] [blame] | 4979 | assert( pChild->isInit==0 ); |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 4980 | rc = sqlite3BtreeInitPage(pChild, pPage); |
danielk1977 | 6b456a2 | 2005-03-21 04:04:02 +0000 | [diff] [blame] | 4981 | if( rc ) goto balancedeeper_out; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4982 | memcpy(pChild->aOvfl, pPage->aOvfl, pPage->nOverflow*sizeof(pPage->aOvfl[0])); |
| 4983 | pChild->nOverflow = pPage->nOverflow; |
| 4984 | if( pChild->nOverflow ){ |
| 4985 | pChild->nFree = 0; |
| 4986 | } |
| 4987 | assert( pChild->nCell==pPage->nCell ); |
| 4988 | zeroPage(pPage, pChild->aData[0] & ~PTF_LEAF); |
| 4989 | put4byte(&pPage->aData[pPage->hdrOffset+8], pgnoChild); |
| 4990 | TRACE(("BALANCE: copy root %d into %d\n", pPage->pgno, pChild->pgno)); |
danielk1977 | 4e17d14 | 2005-01-16 09:06:33 +0000 | [diff] [blame] | 4991 | #ifndef SQLITE_OMIT_AUTOVACUUM |
danielk1977 | ac11ee6 | 2005-01-15 12:45:51 +0000 | [diff] [blame] | 4992 | if( pBt->autoVacuum ){ |
| 4993 | int i; |
| 4994 | rc = ptrmapPut(pBt, pChild->pgno, PTRMAP_BTREE, pPage->pgno); |
danielk1977 | 6b456a2 | 2005-03-21 04:04:02 +0000 | [diff] [blame] | 4995 | if( rc ) goto balancedeeper_out; |
danielk1977 | ac11ee6 | 2005-01-15 12:45:51 +0000 | [diff] [blame] | 4996 | for(i=0; i<pChild->nCell; i++){ |
danielk1977 | 79a40da | 2005-01-16 08:00:01 +0000 | [diff] [blame] | 4997 | rc = ptrmapPutOvfl(pChild, i); |
| 4998 | if( rc!=SQLITE_OK ){ |
| 4999 | return rc; |
danielk1977 | ac11ee6 | 2005-01-15 12:45:51 +0000 | [diff] [blame] | 5000 | } |
| 5001 | } |
| 5002 | } |
danielk1977 | 4e17d14 | 2005-01-16 09:06:33 +0000 | [diff] [blame] | 5003 | #endif |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5004 | rc = balance_nonroot(pChild); |
danielk1977 | 6b456a2 | 2005-03-21 04:04:02 +0000 | [diff] [blame] | 5005 | |
| 5006 | balancedeeper_out: |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5007 | releasePage(pChild); |
| 5008 | return rc; |
| 5009 | } |
| 5010 | |
| 5011 | /* |
| 5012 | ** Decide if the page pPage needs to be balanced. If balancing is |
| 5013 | ** required, call the appropriate balancing routine. |
| 5014 | */ |
danielk1977 | ac245ec | 2005-01-14 13:50:11 +0000 | [diff] [blame] | 5015 | static int balance(MemPage *pPage, int insert){ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5016 | int rc = SQLITE_OK; |
| 5017 | if( pPage->pParent==0 ){ |
| 5018 | if( pPage->nOverflow>0 ){ |
| 5019 | rc = balance_deeper(pPage); |
| 5020 | } |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 5021 | if( rc==SQLITE_OK && pPage->nCell==0 ){ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5022 | rc = balance_shallower(pPage); |
| 5023 | } |
| 5024 | }else{ |
danielk1977 | ac245ec | 2005-01-14 13:50:11 +0000 | [diff] [blame] | 5025 | if( pPage->nOverflow>0 || |
| 5026 | (!insert && pPage->nFree>pPage->pBt->usableSize*2/3) ){ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5027 | rc = balance_nonroot(pPage); |
| 5028 | } |
| 5029 | } |
| 5030 | return rc; |
| 5031 | } |
| 5032 | |
| 5033 | /* |
drh | 8dcd7ca | 2004-08-08 19:43:29 +0000 | [diff] [blame] | 5034 | ** This routine checks all cursors that point to table pgnoRoot. |
drh | 980b1a7 | 2006-08-16 16:42:48 +0000 | [diff] [blame] | 5035 | ** If any of those cursors were opened with wrFlag==0 in a different |
| 5036 | ** database connection (a database connection that shares the pager |
| 5037 | ** cache with the current connection) and that other connection |
| 5038 | ** is not in the ReadUncommmitted state, then this routine returns |
| 5039 | ** SQLITE_LOCKED. |
danielk1977 | 299b187 | 2004-11-22 10:02:10 +0000 | [diff] [blame] | 5040 | ** |
| 5041 | ** In addition to checking for read-locks (where a read-lock |
| 5042 | ** means a cursor opened with wrFlag==0) this routine also moves |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 5043 | ** all write cursors so that they are pointing to the |
drh | 980b1a7 | 2006-08-16 16:42:48 +0000 | [diff] [blame] | 5044 | ** first Cell on the root page. This is necessary because an insert |
danielk1977 | 299b187 | 2004-11-22 10:02:10 +0000 | [diff] [blame] | 5045 | ** or delete might change the number of cells on a page or delete |
| 5046 | ** a page entirely and we do not want to leave any cursors |
| 5047 | ** pointing to non-existant pages or cells. |
drh | f74b8d9 | 2002-09-01 23:20:45 +0000 | [diff] [blame] | 5048 | */ |
drh | 980b1a7 | 2006-08-16 16:42:48 +0000 | [diff] [blame] | 5049 | static int checkReadLocks(Btree *pBtree, Pgno pgnoRoot, BtCursor *pExclude){ |
danielk1977 | 299b187 | 2004-11-22 10:02:10 +0000 | [diff] [blame] | 5050 | BtCursor *p; |
drh | 980b1a7 | 2006-08-16 16:42:48 +0000 | [diff] [blame] | 5051 | BtShared *pBt = pBtree->pBt; |
| 5052 | sqlite3 *db = pBtree->pSqlite; |
danielk1977 | 299b187 | 2004-11-22 10:02:10 +0000 | [diff] [blame] | 5053 | for(p=pBt->pCursor; p; p=p->pNext){ |
drh | 980b1a7 | 2006-08-16 16:42:48 +0000 | [diff] [blame] | 5054 | if( p==pExclude ) continue; |
| 5055 | if( p->eState!=CURSOR_VALID ) continue; |
| 5056 | if( p->pgnoRoot!=pgnoRoot ) continue; |
| 5057 | if( p->wrFlag==0 ){ |
| 5058 | sqlite3 *dbOther = p->pBtree->pSqlite; |
| 5059 | if( dbOther==0 || |
| 5060 | (dbOther!=db && (dbOther->flags & SQLITE_ReadUncommitted)==0) ){ |
| 5061 | return SQLITE_LOCKED; |
| 5062 | } |
| 5063 | }else if( p->pPage->pgno!=p->pgnoRoot ){ |
danielk1977 | 299b187 | 2004-11-22 10:02:10 +0000 | [diff] [blame] | 5064 | moveToRoot(p); |
| 5065 | } |
| 5066 | } |
drh | f74b8d9 | 2002-09-01 23:20:45 +0000 | [diff] [blame] | 5067 | return SQLITE_OK; |
| 5068 | } |
| 5069 | |
| 5070 | /* |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 5071 | ** Insert a new record into the BTree. The key is given by (pKey,nKey) |
| 5072 | ** 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] | 5073 | ** define what table the record should be inserted into. The cursor |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 5074 | ** is left pointing at a random location. |
| 5075 | ** |
| 5076 | ** For an INTKEY table, only the nKey value of the key is used. pKey is |
| 5077 | ** ignored. For a ZERODATA table, the pData and nData are both ignored. |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 5078 | */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 5079 | int sqlite3BtreeInsert( |
drh | 5c4d970 | 2001-08-20 00:33:58 +0000 | [diff] [blame] | 5080 | BtCursor *pCur, /* Insert data into the table of this cursor */ |
drh | 4a1c380 | 2004-05-12 15:15:47 +0000 | [diff] [blame] | 5081 | const void *pKey, i64 nKey, /* The key of the new record */ |
drh | e4d9081 | 2007-03-29 05:51:49 +0000 | [diff] [blame] | 5082 | const void *pData, int nData, /* The data of the new record */ |
drh | b026e05 | 2007-05-02 01:34:31 +0000 | [diff] [blame] | 5083 | int nZero, /* Number of extra 0 bytes to append to data */ |
drh | e4d9081 | 2007-03-29 05:51:49 +0000 | [diff] [blame] | 5084 | int appendBias /* True if this is likely an append */ |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 5085 | ){ |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 5086 | int rc; |
| 5087 | int loc; |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 5088 | int szNew; |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 5089 | MemPage *pPage; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 5090 | BtShared *pBt = pCur->pBtree->pBt; |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 5091 | unsigned char *oldCell; |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 5092 | unsigned char *newCell = 0; |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 5093 | |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 5094 | if( pBt->inTransaction!=TRANS_WRITE ){ |
drh | f74b8d9 | 2002-09-01 23:20:45 +0000 | [diff] [blame] | 5095 | /* Must start a transaction before doing an insert */ |
| 5096 | return pBt->readOnly ? SQLITE_READONLY : SQLITE_ERROR; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 5097 | } |
drh | f74b8d9 | 2002-09-01 23:20:45 +0000 | [diff] [blame] | 5098 | assert( !pBt->readOnly ); |
drh | ecdc753 | 2001-09-23 02:35:53 +0000 | [diff] [blame] | 5099 | if( !pCur->wrFlag ){ |
| 5100 | return SQLITE_PERM; /* Cursor not open for writing */ |
| 5101 | } |
drh | 980b1a7 | 2006-08-16 16:42:48 +0000 | [diff] [blame] | 5102 | if( checkReadLocks(pCur->pBtree, pCur->pgnoRoot, pCur) ){ |
drh | f74b8d9 | 2002-09-01 23:20:45 +0000 | [diff] [blame] | 5103 | return SQLITE_LOCKED; /* The table pCur points to has a read lock */ |
| 5104 | } |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 5105 | |
| 5106 | /* Save the positions of any other cursors open on this table */ |
drh | bf700f3 | 2007-03-31 02:36:44 +0000 | [diff] [blame] | 5107 | clearCursorPosition(pCur); |
danielk1977 | 2e94d4d | 2006-01-09 05:36:27 +0000 | [diff] [blame] | 5108 | if( |
danielk1977 | 2e94d4d | 2006-01-09 05:36:27 +0000 | [diff] [blame] | 5109 | SQLITE_OK!=(rc = saveAllCursors(pBt, pCur->pgnoRoot, pCur)) || |
drh | e4d9081 | 2007-03-29 05:51:49 +0000 | [diff] [blame] | 5110 | SQLITE_OK!=(rc = sqlite3BtreeMoveto(pCur, pKey, nKey, appendBias, &loc)) |
danielk1977 | 2e94d4d | 2006-01-09 05:36:27 +0000 | [diff] [blame] | 5111 | ){ |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 5112 | return rc; |
| 5113 | } |
| 5114 | |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 5115 | pPage = pCur->pPage; |
drh | 4a1c380 | 2004-05-12 15:15:47 +0000 | [diff] [blame] | 5116 | assert( pPage->intKey || nKey>=0 ); |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 5117 | assert( pPage->leaf || !pPage->leafData ); |
drh | 3a4c141 | 2004-05-09 20:40:11 +0000 | [diff] [blame] | 5118 | TRACE(("INSERT: table=%d nkey=%lld ndata=%d page=%d %s\n", |
| 5119 | pCur->pgnoRoot, nKey, nData, pPage->pgno, |
| 5120 | loc==0 ? "overwrite" : "new entry")); |
drh | 7aa128d | 2002-06-21 13:09:16 +0000 | [diff] [blame] | 5121 | assert( pPage->isInit ); |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 5122 | rc = sqlite3PagerWrite(pPage->pDbPage); |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 5123 | if( rc ) return rc; |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 5124 | newCell = sqliteMallocRaw( MX_CELL_SIZE(pBt) ); |
| 5125 | if( newCell==0 ) return SQLITE_NOMEM; |
drh | b026e05 | 2007-05-02 01:34:31 +0000 | [diff] [blame] | 5126 | rc = fillInCell(pPage, newCell, pKey, nKey, pData, nData, nZero, &szNew); |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 5127 | if( rc ) goto end_insert; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5128 | assert( szNew==cellSizePtr(pPage, newCell) ); |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 5129 | assert( szNew<=MX_CELL_SIZE(pBt) ); |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 5130 | if( loc==0 && CURSOR_VALID==pCur->eState ){ |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 5131 | int szOld; |
| 5132 | assert( pCur->idx>=0 && pCur->idx<pPage->nCell ); |
danielk1977 | 1cc5ed8 | 2007-05-16 17:28:43 +0000 | [diff] [blame] | 5133 | oldCell = findCell(pPage, pCur->idx); |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 5134 | if( !pPage->leaf ){ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5135 | memcpy(newCell, oldCell, 4); |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 5136 | } |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5137 | szOld = cellSizePtr(pPage, oldCell); |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 5138 | rc = clearCell(pPage, oldCell); |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 5139 | if( rc ) goto end_insert; |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 5140 | dropCell(pPage, pCur->idx, szOld); |
drh | 7c717f7 | 2001-06-24 20:39:41 +0000 | [diff] [blame] | 5141 | }else if( loc<0 && pPage->nCell>0 ){ |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 5142 | assert( pPage->leaf ); |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 5143 | pCur->idx++; |
drh | 271efa5 | 2004-05-30 19:19:05 +0000 | [diff] [blame] | 5144 | pCur->info.nSize = 0; |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 5145 | }else{ |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 5146 | assert( pPage->leaf ); |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 5147 | } |
danielk1977 | a3ad5e7 | 2005-01-07 08:56:44 +0000 | [diff] [blame] | 5148 | rc = insertCell(pPage, pCur->idx, newCell, szNew, 0, 0); |
danielk1977 | e80463b | 2004-11-03 03:01:16 +0000 | [diff] [blame] | 5149 | if( rc!=SQLITE_OK ) goto end_insert; |
danielk1977 | ac245ec | 2005-01-14 13:50:11 +0000 | [diff] [blame] | 5150 | rc = balance(pPage, 1); |
drh | 23e11ca | 2004-05-04 17:27:28 +0000 | [diff] [blame] | 5151 | /* sqlite3BtreePageDump(pCur->pBt, pCur->pgnoRoot, 1); */ |
drh | 3fc190c | 2001-09-14 03:24:23 +0000 | [diff] [blame] | 5152 | /* fflush(stdout); */ |
danielk1977 | 299b187 | 2004-11-22 10:02:10 +0000 | [diff] [blame] | 5153 | if( rc==SQLITE_OK ){ |
| 5154 | moveToRoot(pCur); |
| 5155 | } |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 5156 | end_insert: |
| 5157 | sqliteFree(newCell); |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 5158 | return rc; |
| 5159 | } |
| 5160 | |
| 5161 | /* |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 5162 | ** Delete the entry that the cursor is pointing to. The cursor |
| 5163 | ** is left pointing at a random location. |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 5164 | */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 5165 | int sqlite3BtreeDelete(BtCursor *pCur){ |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 5166 | MemPage *pPage = pCur->pPage; |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 5167 | unsigned char *pCell; |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 5168 | int rc; |
danielk1977 | cfe9a69 | 2004-06-16 12:00:29 +0000 | [diff] [blame] | 5169 | Pgno pgnoChild = 0; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 5170 | BtShared *pBt = pCur->pBtree->pBt; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 5171 | |
drh | 7aa128d | 2002-06-21 13:09:16 +0000 | [diff] [blame] | 5172 | assert( pPage->isInit ); |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 5173 | if( pBt->inTransaction!=TRANS_WRITE ){ |
drh | f74b8d9 | 2002-09-01 23:20:45 +0000 | [diff] [blame] | 5174 | /* Must start a transaction before doing a delete */ |
| 5175 | return pBt->readOnly ? SQLITE_READONLY : SQLITE_ERROR; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 5176 | } |
drh | f74b8d9 | 2002-09-01 23:20:45 +0000 | [diff] [blame] | 5177 | assert( !pBt->readOnly ); |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 5178 | if( pCur->idx >= pPage->nCell ){ |
| 5179 | return SQLITE_ERROR; /* The cursor is not pointing to anything */ |
| 5180 | } |
drh | ecdc753 | 2001-09-23 02:35:53 +0000 | [diff] [blame] | 5181 | if( !pCur->wrFlag ){ |
| 5182 | return SQLITE_PERM; /* Did not open this cursor for writing */ |
| 5183 | } |
drh | 980b1a7 | 2006-08-16 16:42:48 +0000 | [diff] [blame] | 5184 | if( checkReadLocks(pCur->pBtree, pCur->pgnoRoot, pCur) ){ |
drh | f74b8d9 | 2002-09-01 23:20:45 +0000 | [diff] [blame] | 5185 | return SQLITE_LOCKED; /* The table pCur points to has a read lock */ |
| 5186 | } |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 5187 | |
| 5188 | /* Restore the current cursor position (a no-op if the cursor is not in |
| 5189 | ** CURSOR_REQUIRESEEK state) and save the positions of any other cursors |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 5190 | ** open on the same table. Then call sqlite3PagerWrite() on the page |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 5191 | ** that the entry will be deleted from. |
| 5192 | */ |
| 5193 | if( |
drh | bf700f3 | 2007-03-31 02:36:44 +0000 | [diff] [blame] | 5194 | (rc = restoreOrClearCursorPosition(pCur))!=0 || |
drh | d116739 | 2006-01-23 13:00:35 +0000 | [diff] [blame] | 5195 | (rc = saveAllCursors(pBt, pCur->pgnoRoot, pCur))!=0 || |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 5196 | (rc = sqlite3PagerWrite(pPage->pDbPage))!=0 |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 5197 | ){ |
| 5198 | return rc; |
| 5199 | } |
danielk1977 | e6efa74 | 2004-11-10 11:55:10 +0000 | [diff] [blame] | 5200 | |
| 5201 | /* Locate the cell within it's page and leave pCell pointing to the |
| 5202 | ** data. The clearCell() call frees any overflow pages associated with the |
| 5203 | ** cell. The cell itself is still intact. |
| 5204 | */ |
danielk1977 | 1cc5ed8 | 2007-05-16 17:28:43 +0000 | [diff] [blame] | 5205 | pCell = findCell(pPage, pCur->idx); |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 5206 | if( !pPage->leaf ){ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5207 | pgnoChild = get4byte(pCell); |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 5208 | } |
danielk1977 | 2812956 | 2005-01-11 10:25:06 +0000 | [diff] [blame] | 5209 | rc = clearCell(pPage, pCell); |
| 5210 | if( rc ) return rc; |
danielk1977 | e6efa74 | 2004-11-10 11:55:10 +0000 | [diff] [blame] | 5211 | |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 5212 | if( !pPage->leaf ){ |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 5213 | /* |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 5214 | ** 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] | 5215 | ** do something we will leave a hole on an internal page. |
| 5216 | ** We have to fill the hole by moving in a cell from a leaf. The |
| 5217 | ** next Cell after the one to be deleted is guaranteed to exist and |
danielk1977 | 299b187 | 2004-11-22 10:02:10 +0000 | [diff] [blame] | 5218 | ** to be a leaf so we can use it. |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 5219 | */ |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 5220 | BtCursor leafCur; |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 5221 | unsigned char *pNext; |
drh | 02afc86 | 2006-01-20 18:10:57 +0000 | [diff] [blame] | 5222 | int szNext; /* The compiler warning is wrong: szNext is always |
| 5223 | ** initialized before use. Adding an extra initialization |
| 5224 | ** to silence the compiler slows down the code. */ |
danielk1977 | 299b187 | 2004-11-22 10:02:10 +0000 | [diff] [blame] | 5225 | int notUsed; |
danielk1977 | 6b456a2 | 2005-03-21 04:04:02 +0000 | [diff] [blame] | 5226 | unsigned char *tempCell = 0; |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 5227 | assert( !pPage->leafData ); |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 5228 | sqlite3BtreeGetTempCursor(pCur, &leafCur); |
danielk1977 | 299b187 | 2004-11-22 10:02:10 +0000 | [diff] [blame] | 5229 | rc = sqlite3BtreeNext(&leafCur, ¬Used); |
danielk1977 | 6b456a2 | 2005-03-21 04:04:02 +0000 | [diff] [blame] | 5230 | if( rc==SQLITE_OK ){ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 5231 | rc = sqlite3PagerWrite(leafCur.pPage->pDbPage); |
danielk1977 | 6b456a2 | 2005-03-21 04:04:02 +0000 | [diff] [blame] | 5232 | } |
| 5233 | if( rc==SQLITE_OK ){ |
| 5234 | TRACE(("DELETE: table=%d delete internal from %d replace from leaf %d\n", |
| 5235 | pCur->pgnoRoot, pPage->pgno, leafCur.pPage->pgno)); |
| 5236 | dropCell(pPage, pCur->idx, cellSizePtr(pPage, pCell)); |
danielk1977 | 1cc5ed8 | 2007-05-16 17:28:43 +0000 | [diff] [blame] | 5237 | pNext = findCell(leafCur.pPage, leafCur.idx); |
danielk1977 | 6b456a2 | 2005-03-21 04:04:02 +0000 | [diff] [blame] | 5238 | szNext = cellSizePtr(leafCur.pPage, pNext); |
| 5239 | assert( MX_CELL_SIZE(pBt)>=szNext+4 ); |
| 5240 | tempCell = sqliteMallocRaw( MX_CELL_SIZE(pBt) ); |
| 5241 | if( tempCell==0 ){ |
| 5242 | rc = SQLITE_NOMEM; |
| 5243 | } |
| 5244 | } |
| 5245 | if( rc==SQLITE_OK ){ |
| 5246 | rc = insertCell(pPage, pCur->idx, pNext-4, szNext+4, tempCell, 0); |
| 5247 | } |
| 5248 | if( rc==SQLITE_OK ){ |
| 5249 | put4byte(findOverflowCell(pPage, pCur->idx), pgnoChild); |
| 5250 | rc = balance(pPage, 0); |
| 5251 | } |
| 5252 | if( rc==SQLITE_OK ){ |
| 5253 | dropCell(leafCur.pPage, leafCur.idx, szNext); |
| 5254 | rc = balance(leafCur.pPage, 0); |
| 5255 | } |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 5256 | sqliteFree(tempCell); |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 5257 | sqlite3BtreeReleaseTempCursor(&leafCur); |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 5258 | }else{ |
danielk1977 | 299b187 | 2004-11-22 10:02:10 +0000 | [diff] [blame] | 5259 | TRACE(("DELETE: table=%d delete from leaf %d\n", |
| 5260 | pCur->pgnoRoot, pPage->pgno)); |
| 5261 | dropCell(pPage, pCur->idx, cellSizePtr(pPage, pCell)); |
danielk1977 | ac245ec | 2005-01-14 13:50:11 +0000 | [diff] [blame] | 5262 | rc = balance(pPage, 0); |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 5263 | } |
danielk1977 | 6b456a2 | 2005-03-21 04:04:02 +0000 | [diff] [blame] | 5264 | if( rc==SQLITE_OK ){ |
| 5265 | moveToRoot(pCur); |
| 5266 | } |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 5267 | return rc; |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 5268 | } |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 5269 | |
| 5270 | /* |
drh | c6b52df | 2002-01-04 03:09:29 +0000 | [diff] [blame] | 5271 | ** Create a new BTree table. Write into *piTable the page |
| 5272 | ** number for the root page of the new table. |
| 5273 | ** |
drh | ab01f61 | 2004-05-22 02:55:23 +0000 | [diff] [blame] | 5274 | ** The type of type is determined by the flags parameter. Only the |
| 5275 | ** following values of flags are currently in use. Other values for |
| 5276 | ** flags might not work: |
| 5277 | ** |
| 5278 | ** BTREE_INTKEY|BTREE_LEAFDATA Used for SQL tables with rowid keys |
| 5279 | ** BTREE_ZERODATA Used for SQL indices |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 5280 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 5281 | int sqlite3BtreeCreateTable(Btree *p, int *piTable, int flags){ |
| 5282 | BtShared *pBt = p->pBt; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 5283 | MemPage *pRoot; |
| 5284 | Pgno pgnoRoot; |
| 5285 | int rc; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 5286 | if( pBt->inTransaction!=TRANS_WRITE ){ |
drh | f74b8d9 | 2002-09-01 23:20:45 +0000 | [diff] [blame] | 5287 | /* Must start a transaction first */ |
| 5288 | return pBt->readOnly ? SQLITE_READONLY : SQLITE_ERROR; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 5289 | } |
danielk1977 | 2812956 | 2005-01-11 10:25:06 +0000 | [diff] [blame] | 5290 | assert( !pBt->readOnly ); |
danielk1977 | e6efa74 | 2004-11-10 11:55:10 +0000 | [diff] [blame] | 5291 | |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 5292 | #ifdef SQLITE_OMIT_AUTOVACUUM |
drh | 4f0c587 | 2007-03-26 22:05:01 +0000 | [diff] [blame] | 5293 | rc = allocateBtreePage(pBt, &pRoot, &pgnoRoot, 1, 0); |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 5294 | if( rc ) return rc; |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 5295 | #else |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 5296 | if( pBt->autoVacuum ){ |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 5297 | Pgno pgnoMove; /* Move a page here to make room for the root-page */ |
| 5298 | MemPage *pPageMove; /* The page to move to. */ |
| 5299 | |
danielk1977 | 20713f3 | 2007-05-03 11:43:33 +0000 | [diff] [blame] | 5300 | /* Creating a new table may probably require moving an existing database |
| 5301 | ** to make room for the new tables root page. In case this page turns |
| 5302 | ** out to be an overflow page, delete all overflow page-map caches |
| 5303 | ** held by open cursors. |
| 5304 | */ |
danielk1977 | 92d4d7a | 2007-05-04 12:05:56 +0000 | [diff] [blame] | 5305 | invalidateAllOverflowCache(pBt); |
danielk1977 | 20713f3 | 2007-05-03 11:43:33 +0000 | [diff] [blame] | 5306 | |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 5307 | /* Read the value of meta[3] from the database to determine where the |
| 5308 | ** root page of the new table should go. meta[3] is the largest root-page |
| 5309 | ** created so far, so the new root-page is (meta[3]+1). |
| 5310 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 5311 | rc = sqlite3BtreeGetMeta(p, 4, &pgnoRoot); |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 5312 | if( rc!=SQLITE_OK ) return rc; |
| 5313 | pgnoRoot++; |
| 5314 | |
danielk1977 | 599fcba | 2004-11-08 07:13:13 +0000 | [diff] [blame] | 5315 | /* The new root-page may not be allocated on a pointer-map page, or the |
| 5316 | ** PENDING_BYTE page. |
| 5317 | */ |
danielk1977 | 266664d | 2006-02-10 08:24:21 +0000 | [diff] [blame] | 5318 | if( pgnoRoot==PTRMAP_PAGENO(pBt, pgnoRoot) || |
danielk1977 | 599fcba | 2004-11-08 07:13:13 +0000 | [diff] [blame] | 5319 | pgnoRoot==PENDING_BYTE_PAGE(pBt) ){ |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 5320 | pgnoRoot++; |
| 5321 | } |
| 5322 | assert( pgnoRoot>=3 ); |
| 5323 | |
| 5324 | /* Allocate a page. The page that currently resides at pgnoRoot will |
| 5325 | ** be moved to the allocated page (unless the allocated page happens |
| 5326 | ** to reside at pgnoRoot). |
| 5327 | */ |
drh | 4f0c587 | 2007-03-26 22:05:01 +0000 | [diff] [blame] | 5328 | rc = allocateBtreePage(pBt, &pPageMove, &pgnoMove, pgnoRoot, 1); |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 5329 | if( rc!=SQLITE_OK ){ |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 5330 | return rc; |
| 5331 | } |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 5332 | |
| 5333 | if( pgnoMove!=pgnoRoot ){ |
danielk1977 | f35843b | 2007-04-07 15:03:17 +0000 | [diff] [blame] | 5334 | /* pgnoRoot is the page that will be used for the root-page of |
| 5335 | ** the new table (assuming an error did not occur). But we were |
| 5336 | ** allocated pgnoMove. If required (i.e. if it was not allocated |
| 5337 | ** by extending the file), the current page at position pgnoMove |
| 5338 | ** is already journaled. |
| 5339 | */ |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 5340 | u8 eType; |
| 5341 | Pgno iPtrPage; |
| 5342 | |
| 5343 | releasePage(pPageMove); |
danielk1977 | f35843b | 2007-04-07 15:03:17 +0000 | [diff] [blame] | 5344 | |
| 5345 | /* Move the page currently at pgnoRoot to pgnoMove. */ |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 5346 | rc = sqlite3BtreeGetPage(pBt, pgnoRoot, &pRoot, 0); |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 5347 | if( rc!=SQLITE_OK ){ |
| 5348 | return rc; |
| 5349 | } |
| 5350 | rc = ptrmapGet(pBt, pgnoRoot, &eType, &iPtrPage); |
drh | ccae602 | 2005-02-26 17:31:26 +0000 | [diff] [blame] | 5351 | if( rc!=SQLITE_OK || eType==PTRMAP_ROOTPAGE || eType==PTRMAP_FREEPAGE ){ |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 5352 | releasePage(pRoot); |
| 5353 | return rc; |
| 5354 | } |
drh | ccae602 | 2005-02-26 17:31:26 +0000 | [diff] [blame] | 5355 | assert( eType!=PTRMAP_ROOTPAGE ); |
| 5356 | assert( eType!=PTRMAP_FREEPAGE ); |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 5357 | rc = sqlite3PagerWrite(pRoot->pDbPage); |
danielk1977 | 5fd057a | 2005-03-09 13:09:43 +0000 | [diff] [blame] | 5358 | if( rc!=SQLITE_OK ){ |
| 5359 | releasePage(pRoot); |
| 5360 | return rc; |
| 5361 | } |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 5362 | rc = relocatePage(pBt, pRoot, eType, iPtrPage, pgnoMove); |
| 5363 | releasePage(pRoot); |
danielk1977 | f35843b | 2007-04-07 15:03:17 +0000 | [diff] [blame] | 5364 | |
| 5365 | /* Obtain the page at pgnoRoot */ |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 5366 | if( rc!=SQLITE_OK ){ |
| 5367 | return rc; |
| 5368 | } |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 5369 | rc = sqlite3BtreeGetPage(pBt, pgnoRoot, &pRoot, 0); |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 5370 | if( rc!=SQLITE_OK ){ |
| 5371 | return rc; |
| 5372 | } |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 5373 | rc = sqlite3PagerWrite(pRoot->pDbPage); |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 5374 | if( rc!=SQLITE_OK ){ |
| 5375 | releasePage(pRoot); |
| 5376 | return rc; |
| 5377 | } |
| 5378 | }else{ |
| 5379 | pRoot = pPageMove; |
| 5380 | } |
| 5381 | |
danielk1977 | 42741be | 2005-01-08 12:42:39 +0000 | [diff] [blame] | 5382 | /* Update the pointer-map and meta-data with the new root-page number. */ |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 5383 | rc = ptrmapPut(pBt, pgnoRoot, PTRMAP_ROOTPAGE, 0); |
| 5384 | if( rc ){ |
| 5385 | releasePage(pRoot); |
| 5386 | return rc; |
| 5387 | } |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 5388 | rc = sqlite3BtreeUpdateMeta(p, 4, pgnoRoot); |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 5389 | if( rc ){ |
| 5390 | releasePage(pRoot); |
| 5391 | return rc; |
| 5392 | } |
danielk1977 | 42741be | 2005-01-08 12:42:39 +0000 | [diff] [blame] | 5393 | |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 5394 | }else{ |
drh | 4f0c587 | 2007-03-26 22:05:01 +0000 | [diff] [blame] | 5395 | rc = allocateBtreePage(pBt, &pRoot, &pgnoRoot, 1, 0); |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 5396 | if( rc ) return rc; |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 5397 | } |
| 5398 | #endif |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 5399 | assert( sqlite3PagerIswriteable(pRoot->pDbPage) ); |
drh | de64713 | 2004-05-07 17:57:49 +0000 | [diff] [blame] | 5400 | zeroPage(pRoot, flags | PTF_LEAF); |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 5401 | sqlite3PagerUnref(pRoot->pDbPage); |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 5402 | *piTable = (int)pgnoRoot; |
| 5403 | return SQLITE_OK; |
| 5404 | } |
| 5405 | |
| 5406 | /* |
| 5407 | ** Erase the given database page and all its children. Return |
| 5408 | ** the page to the freelist. |
| 5409 | */ |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 5410 | static int clearDatabasePage( |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 5411 | BtShared *pBt, /* The BTree that contains the table */ |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 5412 | Pgno pgno, /* Page number to clear */ |
| 5413 | MemPage *pParent, /* Parent page. NULL for the root */ |
| 5414 | int freePageFlag /* Deallocate page if true */ |
| 5415 | ){ |
danielk1977 | 6b456a2 | 2005-03-21 04:04:02 +0000 | [diff] [blame] | 5416 | MemPage *pPage = 0; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 5417 | int rc; |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 5418 | unsigned char *pCell; |
| 5419 | int i; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 5420 | |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 5421 | if( pgno>sqlite3PagerPagecount(pBt->pPager) ){ |
drh | 4928570 | 2005-09-17 15:20:26 +0000 | [diff] [blame] | 5422 | return SQLITE_CORRUPT_BKPT; |
danielk1977 | a1cb183 | 2005-02-12 08:59:55 +0000 | [diff] [blame] | 5423 | } |
| 5424 | |
drh | de64713 | 2004-05-07 17:57:49 +0000 | [diff] [blame] | 5425 | rc = getAndInitPage(pBt, pgno, &pPage, pParent); |
danielk1977 | 6b456a2 | 2005-03-21 04:04:02 +0000 | [diff] [blame] | 5426 | if( rc ) goto cleardatabasepage_out; |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 5427 | for(i=0; i<pPage->nCell; i++){ |
danielk1977 | 1cc5ed8 | 2007-05-16 17:28:43 +0000 | [diff] [blame] | 5428 | pCell = findCell(pPage, i); |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 5429 | if( !pPage->leaf ){ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5430 | rc = clearDatabasePage(pBt, get4byte(pCell), pPage->pParent, 1); |
danielk1977 | 6b456a2 | 2005-03-21 04:04:02 +0000 | [diff] [blame] | 5431 | if( rc ) goto cleardatabasepage_out; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 5432 | } |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 5433 | rc = clearCell(pPage, pCell); |
danielk1977 | 6b456a2 | 2005-03-21 04:04:02 +0000 | [diff] [blame] | 5434 | if( rc ) goto cleardatabasepage_out; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 5435 | } |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 5436 | if( !pPage->leaf ){ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5437 | rc = clearDatabasePage(pBt, get4byte(&pPage->aData[8]), pPage->pParent, 1); |
danielk1977 | 6b456a2 | 2005-03-21 04:04:02 +0000 | [diff] [blame] | 5438 | if( rc ) goto cleardatabasepage_out; |
drh | 2aa679f | 2001-06-25 02:11:07 +0000 | [diff] [blame] | 5439 | } |
| 5440 | if( freePageFlag ){ |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 5441 | rc = freePage(pPage); |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 5442 | }else if( (rc = sqlite3PagerWrite(pPage->pDbPage))==0 ){ |
drh | 3a4c141 | 2004-05-09 20:40:11 +0000 | [diff] [blame] | 5443 | zeroPage(pPage, pPage->aData[0] | PTF_LEAF); |
drh | 2aa679f | 2001-06-25 02:11:07 +0000 | [diff] [blame] | 5444 | } |
danielk1977 | 6b456a2 | 2005-03-21 04:04:02 +0000 | [diff] [blame] | 5445 | |
| 5446 | cleardatabasepage_out: |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 5447 | releasePage(pPage); |
drh | 2aa679f | 2001-06-25 02:11:07 +0000 | [diff] [blame] | 5448 | return rc; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 5449 | } |
| 5450 | |
| 5451 | /* |
drh | ab01f61 | 2004-05-22 02:55:23 +0000 | [diff] [blame] | 5452 | ** Delete all information from a single table in the database. iTable is |
| 5453 | ** the page number of the root of the table. After this routine returns, |
| 5454 | ** the root page is empty, but still exists. |
| 5455 | ** |
| 5456 | ** This routine will fail with SQLITE_LOCKED if there are any open |
| 5457 | ** read cursors on the table. Open write cursors are moved to the |
| 5458 | ** root of the table. |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 5459 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 5460 | int sqlite3BtreeClearTable(Btree *p, int iTable){ |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 5461 | int rc; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 5462 | BtShared *pBt = p->pBt; |
| 5463 | if( p->inTrans!=TRANS_WRITE ){ |
drh | f74b8d9 | 2002-09-01 23:20:45 +0000 | [diff] [blame] | 5464 | return pBt->readOnly ? SQLITE_READONLY : SQLITE_ERROR; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 5465 | } |
drh | 980b1a7 | 2006-08-16 16:42:48 +0000 | [diff] [blame] | 5466 | rc = checkReadLocks(p, iTable, 0); |
| 5467 | if( rc ){ |
| 5468 | return rc; |
drh | ecdc753 | 2001-09-23 02:35:53 +0000 | [diff] [blame] | 5469 | } |
danielk1977 | ed42931 | 2006-01-19 08:43:31 +0000 | [diff] [blame] | 5470 | |
| 5471 | /* Save the position of all cursors open on this table */ |
| 5472 | if( SQLITE_OK!=(rc = saveAllCursors(pBt, iTable, 0)) ){ |
| 5473 | return rc; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 5474 | } |
danielk1977 | ed42931 | 2006-01-19 08:43:31 +0000 | [diff] [blame] | 5475 | |
| 5476 | return clearDatabasePage(pBt, (Pgno)iTable, 0, 0); |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 5477 | } |
| 5478 | |
| 5479 | /* |
| 5480 | ** Erase all information in a table and add the root of the table to |
| 5481 | ** the freelist. Except, the root of the principle table (the one on |
drh | ab01f61 | 2004-05-22 02:55:23 +0000 | [diff] [blame] | 5482 | ** page 1) is never added to the freelist. |
| 5483 | ** |
| 5484 | ** This routine will fail with SQLITE_LOCKED if there are any open |
| 5485 | ** cursors on the table. |
drh | 205f48e | 2004-11-05 00:43:11 +0000 | [diff] [blame] | 5486 | ** |
| 5487 | ** If AUTOVACUUM is enabled and the page at iTable is not the last |
| 5488 | ** root page in the database file, then the last root page |
| 5489 | ** in the database file is moved into the slot formerly occupied by |
| 5490 | ** iTable and that last slot formerly occupied by the last root page |
| 5491 | ** is added to the freelist instead of iTable. In this say, all |
| 5492 | ** root pages are kept at the beginning of the database file, which |
| 5493 | ** is necessary for AUTOVACUUM to work right. *piMoved is set to the |
| 5494 | ** page number that used to be the last root page in the file before |
| 5495 | ** the move. If no page gets moved, *piMoved is set to 0. |
| 5496 | ** The last root page is recorded in meta[3] and the value of |
| 5497 | ** meta[3] is updated by this procedure. |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 5498 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 5499 | int sqlite3BtreeDropTable(Btree *p, int iTable, int *piMoved){ |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 5500 | int rc; |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 5501 | MemPage *pPage = 0; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 5502 | BtShared *pBt = p->pBt; |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 5503 | |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 5504 | if( p->inTrans!=TRANS_WRITE ){ |
drh | f74b8d9 | 2002-09-01 23:20:45 +0000 | [diff] [blame] | 5505 | return pBt->readOnly ? SQLITE_READONLY : SQLITE_ERROR; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 5506 | } |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 5507 | |
danielk1977 | e6efa74 | 2004-11-10 11:55:10 +0000 | [diff] [blame] | 5508 | /* It is illegal to drop a table if any cursors are open on the |
| 5509 | ** database. This is because in auto-vacuum mode the backend may |
| 5510 | ** need to move another root-page to fill a gap left by the deleted |
| 5511 | ** root page. If an open cursor was using this page a problem would |
| 5512 | ** occur. |
| 5513 | */ |
| 5514 | if( pBt->pCursor ){ |
| 5515 | return SQLITE_LOCKED; |
drh | 5df72a5 | 2002-06-06 23:16:05 +0000 | [diff] [blame] | 5516 | } |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 5517 | |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 5518 | rc = sqlite3BtreeGetPage(pBt, (Pgno)iTable, &pPage, 0); |
drh | 2aa679f | 2001-06-25 02:11:07 +0000 | [diff] [blame] | 5519 | if( rc ) return rc; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 5520 | rc = sqlite3BtreeClearTable(p, iTable); |
danielk1977 | 6b456a2 | 2005-03-21 04:04:02 +0000 | [diff] [blame] | 5521 | if( rc ){ |
| 5522 | releasePage(pPage); |
| 5523 | return rc; |
| 5524 | } |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 5525 | |
drh | 205f48e | 2004-11-05 00:43:11 +0000 | [diff] [blame] | 5526 | *piMoved = 0; |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 5527 | |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 5528 | if( iTable>1 ){ |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 5529 | #ifdef SQLITE_OMIT_AUTOVACUUM |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 5530 | rc = freePage(pPage); |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 5531 | releasePage(pPage); |
| 5532 | #else |
| 5533 | if( pBt->autoVacuum ){ |
| 5534 | Pgno maxRootPgno; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 5535 | rc = sqlite3BtreeGetMeta(p, 4, &maxRootPgno); |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 5536 | if( rc!=SQLITE_OK ){ |
| 5537 | releasePage(pPage); |
| 5538 | return rc; |
| 5539 | } |
| 5540 | |
| 5541 | if( iTable==maxRootPgno ){ |
| 5542 | /* If the table being dropped is the table with the largest root-page |
| 5543 | ** number in the database, put the root page on the free list. |
| 5544 | */ |
| 5545 | rc = freePage(pPage); |
| 5546 | releasePage(pPage); |
| 5547 | if( rc!=SQLITE_OK ){ |
| 5548 | return rc; |
| 5549 | } |
| 5550 | }else{ |
| 5551 | /* The table being dropped does not have the largest root-page |
| 5552 | ** number in the database. So move the page that does into the |
| 5553 | ** gap left by the deleted root-page. |
| 5554 | */ |
| 5555 | MemPage *pMove; |
| 5556 | releasePage(pPage); |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 5557 | rc = sqlite3BtreeGetPage(pBt, maxRootPgno, &pMove, 0); |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 5558 | if( rc!=SQLITE_OK ){ |
| 5559 | return rc; |
| 5560 | } |
| 5561 | rc = relocatePage(pBt, pMove, PTRMAP_ROOTPAGE, 0, iTable); |
| 5562 | releasePage(pMove); |
| 5563 | if( rc!=SQLITE_OK ){ |
| 5564 | return rc; |
| 5565 | } |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 5566 | rc = sqlite3BtreeGetPage(pBt, maxRootPgno, &pMove, 0); |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 5567 | if( rc!=SQLITE_OK ){ |
| 5568 | return rc; |
| 5569 | } |
| 5570 | rc = freePage(pMove); |
| 5571 | releasePage(pMove); |
| 5572 | if( rc!=SQLITE_OK ){ |
| 5573 | return rc; |
| 5574 | } |
| 5575 | *piMoved = maxRootPgno; |
| 5576 | } |
| 5577 | |
danielk1977 | 599fcba | 2004-11-08 07:13:13 +0000 | [diff] [blame] | 5578 | /* Set the new 'max-root-page' value in the database header. This |
| 5579 | ** is the old value less one, less one more if that happens to |
| 5580 | ** be a root-page number, less one again if that is the |
| 5581 | ** PENDING_BYTE_PAGE. |
| 5582 | */ |
danielk1977 | 87a6e73 | 2004-11-05 12:58:25 +0000 | [diff] [blame] | 5583 | maxRootPgno--; |
danielk1977 | 599fcba | 2004-11-08 07:13:13 +0000 | [diff] [blame] | 5584 | if( maxRootPgno==PENDING_BYTE_PAGE(pBt) ){ |
| 5585 | maxRootPgno--; |
| 5586 | } |
danielk1977 | 266664d | 2006-02-10 08:24:21 +0000 | [diff] [blame] | 5587 | if( maxRootPgno==PTRMAP_PAGENO(pBt, maxRootPgno) ){ |
danielk1977 | 87a6e73 | 2004-11-05 12:58:25 +0000 | [diff] [blame] | 5588 | maxRootPgno--; |
| 5589 | } |
danielk1977 | 599fcba | 2004-11-08 07:13:13 +0000 | [diff] [blame] | 5590 | assert( maxRootPgno!=PENDING_BYTE_PAGE(pBt) ); |
| 5591 | |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 5592 | rc = sqlite3BtreeUpdateMeta(p, 4, maxRootPgno); |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 5593 | }else{ |
| 5594 | rc = freePage(pPage); |
| 5595 | releasePage(pPage); |
| 5596 | } |
| 5597 | #endif |
drh | 2aa679f | 2001-06-25 02:11:07 +0000 | [diff] [blame] | 5598 | }else{ |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 5599 | /* If sqlite3BtreeDropTable was called on page 1. */ |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 5600 | zeroPage(pPage, PTF_INTKEY|PTF_LEAF ); |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 5601 | releasePage(pPage); |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 5602 | } |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 5603 | return rc; |
| 5604 | } |
| 5605 | |
drh | 001bbcb | 2003-03-19 03:14:00 +0000 | [diff] [blame] | 5606 | |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 5607 | /* |
drh | 23e11ca | 2004-05-04 17:27:28 +0000 | [diff] [blame] | 5608 | ** Read the meta-information out of a database file. Meta[0] |
| 5609 | ** is the number of free pages currently in the database. Meta[1] |
drh | a3b321d | 2004-05-11 09:31:31 +0000 | [diff] [blame] | 5610 | ** through meta[15] are available for use by higher layers. Meta[0] |
| 5611 | ** is read-only, the others are read/write. |
| 5612 | ** |
| 5613 | ** The schema layer numbers meta values differently. At the schema |
| 5614 | ** layer (and the SetCookie and ReadCookie opcodes) the number of |
| 5615 | ** 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] | 5616 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 5617 | int sqlite3BtreeGetMeta(Btree *p, int idx, u32 *pMeta){ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 5618 | DbPage *pDbPage; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 5619 | int rc; |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 5620 | unsigned char *pP1; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 5621 | BtShared *pBt = p->pBt; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 5622 | |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 5623 | /* Reading a meta-data value requires a read-lock on page 1 (and hence |
| 5624 | ** the sqlite_master table. We grab this lock regardless of whether or |
| 5625 | ** not the SQLITE_ReadUncommitted flag is set (the table rooted at page |
| 5626 | ** 1 is treated as a special case by queryTableLock() and lockTable()). |
| 5627 | */ |
| 5628 | rc = queryTableLock(p, 1, READ_LOCK); |
| 5629 | if( rc!=SQLITE_OK ){ |
| 5630 | return rc; |
| 5631 | } |
| 5632 | |
drh | 23e11ca | 2004-05-04 17:27:28 +0000 | [diff] [blame] | 5633 | assert( idx>=0 && idx<=15 ); |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 5634 | rc = sqlite3PagerGet(pBt->pPager, 1, &pDbPage); |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 5635 | if( rc ) return rc; |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 5636 | pP1 = (unsigned char *)sqlite3PagerGetData(pDbPage); |
drh | 23e11ca | 2004-05-04 17:27:28 +0000 | [diff] [blame] | 5637 | *pMeta = get4byte(&pP1[36 + idx*4]); |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 5638 | sqlite3PagerUnref(pDbPage); |
drh | ae15787 | 2004-08-14 19:20:09 +0000 | [diff] [blame] | 5639 | |
danielk1977 | 599fcba | 2004-11-08 07:13:13 +0000 | [diff] [blame] | 5640 | /* If autovacuumed is disabled in this build but we are trying to |
| 5641 | ** access an autovacuumed database, then make the database readonly. |
| 5642 | */ |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 5643 | #ifdef SQLITE_OMIT_AUTOVACUUM |
drh | ae15787 | 2004-08-14 19:20:09 +0000 | [diff] [blame] | 5644 | if( idx==4 && *pMeta>0 ) pBt->readOnly = 1; |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 5645 | #endif |
drh | ae15787 | 2004-08-14 19:20:09 +0000 | [diff] [blame] | 5646 | |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 5647 | /* Grab the read-lock on page 1. */ |
| 5648 | rc = lockTable(p, 1, READ_LOCK); |
| 5649 | return rc; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 5650 | } |
| 5651 | |
| 5652 | /* |
drh | 23e11ca | 2004-05-04 17:27:28 +0000 | [diff] [blame] | 5653 | ** Write meta-information back into the database. Meta[0] is |
| 5654 | ** read-only and may not be written. |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 5655 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 5656 | int sqlite3BtreeUpdateMeta(Btree *p, int idx, u32 iMeta){ |
| 5657 | BtShared *pBt = p->pBt; |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 5658 | unsigned char *pP1; |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 5659 | int rc; |
drh | 23e11ca | 2004-05-04 17:27:28 +0000 | [diff] [blame] | 5660 | assert( idx>=1 && idx<=15 ); |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 5661 | if( p->inTrans!=TRANS_WRITE ){ |
drh | f74b8d9 | 2002-09-01 23:20:45 +0000 | [diff] [blame] | 5662 | return pBt->readOnly ? SQLITE_READONLY : SQLITE_ERROR; |
drh | 5df72a5 | 2002-06-06 23:16:05 +0000 | [diff] [blame] | 5663 | } |
drh | de64713 | 2004-05-07 17:57:49 +0000 | [diff] [blame] | 5664 | assert( pBt->pPage1!=0 ); |
| 5665 | pP1 = pBt->pPage1->aData; |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 5666 | rc = sqlite3PagerWrite(pBt->pPage1->pDbPage); |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 5667 | if( rc ) return rc; |
drh | 23e11ca | 2004-05-04 17:27:28 +0000 | [diff] [blame] | 5668 | put4byte(&pP1[36 + idx*4], iMeta); |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 5669 | return SQLITE_OK; |
| 5670 | } |
drh | 8c42ca9 | 2001-06-22 19:15:00 +0000 | [diff] [blame] | 5671 | |
drh | f328bc8 | 2004-05-10 23:29:49 +0000 | [diff] [blame] | 5672 | /* |
| 5673 | ** Return the flag byte at the beginning of the page that the cursor |
| 5674 | ** is currently pointing to. |
| 5675 | */ |
| 5676 | int sqlite3BtreeFlags(BtCursor *pCur){ |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 5677 | /* TODO: What about CURSOR_REQUIRESEEK state? Probably need to call |
drh | 777e4c4 | 2006-01-13 04:31:58 +0000 | [diff] [blame] | 5678 | ** restoreOrClearCursorPosition() here. |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 5679 | */ |
drh | f328bc8 | 2004-05-10 23:29:49 +0000 | [diff] [blame] | 5680 | MemPage *pPage = pCur->pPage; |
| 5681 | return pPage ? pPage->aData[pPage->hdrOffset] : 0; |
| 5682 | } |
| 5683 | |
drh | dd79342 | 2001-06-28 01:54:48 +0000 | [diff] [blame] | 5684 | |
drh | dd79342 | 2001-06-28 01:54:48 +0000 | [diff] [blame] | 5685 | /* |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 5686 | ** Return the pager associated with a BTree. This routine is used for |
| 5687 | ** testing and debugging only. |
drh | dd79342 | 2001-06-28 01:54:48 +0000 | [diff] [blame] | 5688 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 5689 | Pager *sqlite3BtreePager(Btree *p){ |
| 5690 | return p->pBt->pPager; |
drh | dd79342 | 2001-06-28 01:54:48 +0000 | [diff] [blame] | 5691 | } |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 5692 | |
drh | b7f9164 | 2004-10-31 02:22:47 +0000 | [diff] [blame] | 5693 | #ifndef SQLITE_OMIT_INTEGRITY_CHECK |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 5694 | /* |
| 5695 | ** Append a message to the error message string. |
| 5696 | */ |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 5697 | static void checkAppendMsg( |
| 5698 | IntegrityCk *pCheck, |
| 5699 | char *zMsg1, |
| 5700 | const char *zFormat, |
| 5701 | ... |
| 5702 | ){ |
| 5703 | va_list ap; |
| 5704 | char *zMsg2; |
drh | 1dcdbc0 | 2007-01-27 02:24:54 +0000 | [diff] [blame] | 5705 | if( !pCheck->mxErr ) return; |
| 5706 | pCheck->mxErr--; |
| 5707 | pCheck->nErr++; |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 5708 | va_start(ap, zFormat); |
| 5709 | zMsg2 = sqlite3VMPrintf(zFormat, ap); |
| 5710 | va_end(ap); |
| 5711 | if( zMsg1==0 ) zMsg1 = ""; |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 5712 | if( pCheck->zErrMsg ){ |
| 5713 | char *zOld = pCheck->zErrMsg; |
| 5714 | pCheck->zErrMsg = 0; |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 5715 | sqlite3SetString(&pCheck->zErrMsg, zOld, "\n", zMsg1, zMsg2, (char*)0); |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 5716 | sqliteFree(zOld); |
| 5717 | }else{ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 5718 | sqlite3SetString(&pCheck->zErrMsg, zMsg1, zMsg2, (char*)0); |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 5719 | } |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 5720 | sqliteFree(zMsg2); |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 5721 | } |
drh | b7f9164 | 2004-10-31 02:22:47 +0000 | [diff] [blame] | 5722 | #endif /* SQLITE_OMIT_INTEGRITY_CHECK */ |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 5723 | |
drh | b7f9164 | 2004-10-31 02:22:47 +0000 | [diff] [blame] | 5724 | #ifndef SQLITE_OMIT_INTEGRITY_CHECK |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 5725 | /* |
| 5726 | ** Add 1 to the reference count for page iPage. If this is the second |
| 5727 | ** reference to the page, add an error message to pCheck->zErrMsg. |
| 5728 | ** Return 1 if there are 2 ore more references to the page and 0 if |
| 5729 | ** if this is the first reference to the page. |
| 5730 | ** |
| 5731 | ** Also check that the page number is in bounds. |
| 5732 | */ |
drh | aaab572 | 2002-02-19 13:39:21 +0000 | [diff] [blame] | 5733 | static int checkRef(IntegrityCk *pCheck, int iPage, char *zContext){ |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 5734 | if( iPage==0 ) return 1; |
drh | 0de8c11 | 2002-07-06 16:32:14 +0000 | [diff] [blame] | 5735 | if( iPage>pCheck->nPage || iPage<0 ){ |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 5736 | checkAppendMsg(pCheck, zContext, "invalid page number %d", iPage); |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 5737 | return 1; |
| 5738 | } |
| 5739 | if( pCheck->anRef[iPage]==1 ){ |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 5740 | checkAppendMsg(pCheck, zContext, "2nd reference to page %d", iPage); |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 5741 | return 1; |
| 5742 | } |
| 5743 | return (pCheck->anRef[iPage]++)>1; |
| 5744 | } |
| 5745 | |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 5746 | #ifndef SQLITE_OMIT_AUTOVACUUM |
| 5747 | /* |
| 5748 | ** Check that the entry in the pointer-map for page iChild maps to |
| 5749 | ** page iParent, pointer type ptrType. If not, append an error message |
| 5750 | ** to pCheck. |
| 5751 | */ |
| 5752 | static void checkPtrmap( |
| 5753 | IntegrityCk *pCheck, /* Integrity check context */ |
| 5754 | Pgno iChild, /* Child page number */ |
| 5755 | u8 eType, /* Expected pointer map type */ |
| 5756 | Pgno iParent, /* Expected pointer map parent page number */ |
| 5757 | char *zContext /* Context description (used for error msg) */ |
| 5758 | ){ |
| 5759 | int rc; |
| 5760 | u8 ePtrmapType; |
| 5761 | Pgno iPtrmapParent; |
| 5762 | |
| 5763 | rc = ptrmapGet(pCheck->pBt, iChild, &ePtrmapType, &iPtrmapParent); |
| 5764 | if( rc!=SQLITE_OK ){ |
| 5765 | checkAppendMsg(pCheck, zContext, "Failed to read ptrmap key=%d", iChild); |
| 5766 | return; |
| 5767 | } |
| 5768 | |
| 5769 | if( ePtrmapType!=eType || iPtrmapParent!=iParent ){ |
| 5770 | checkAppendMsg(pCheck, zContext, |
| 5771 | "Bad ptr map entry key=%d expected=(%d,%d) got=(%d,%d)", |
| 5772 | iChild, eType, iParent, ePtrmapType, iPtrmapParent); |
| 5773 | } |
| 5774 | } |
| 5775 | #endif |
| 5776 | |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 5777 | /* |
| 5778 | ** Check the integrity of the freelist or of an overflow page list. |
| 5779 | ** Verify that the number of pages on the list is N. |
| 5780 | */ |
drh | 30e5875 | 2002-03-02 20:41:57 +0000 | [diff] [blame] | 5781 | static void checkList( |
| 5782 | IntegrityCk *pCheck, /* Integrity checking context */ |
| 5783 | int isFreeList, /* True for a freelist. False for overflow page list */ |
| 5784 | int iPage, /* Page number for first page in the list */ |
| 5785 | int N, /* Expected number of pages in the list */ |
| 5786 | char *zContext /* Context for error messages */ |
| 5787 | ){ |
| 5788 | int i; |
drh | 3a4c141 | 2004-05-09 20:40:11 +0000 | [diff] [blame] | 5789 | int expected = N; |
| 5790 | int iFirst = iPage; |
drh | 1dcdbc0 | 2007-01-27 02:24:54 +0000 | [diff] [blame] | 5791 | while( N-- > 0 && pCheck->mxErr ){ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 5792 | DbPage *pOvflPage; |
| 5793 | unsigned char *pOvflData; |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 5794 | if( iPage<1 ){ |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 5795 | checkAppendMsg(pCheck, zContext, |
| 5796 | "%d of %d pages missing from overflow list starting at %d", |
drh | 3a4c141 | 2004-05-09 20:40:11 +0000 | [diff] [blame] | 5797 | N+1, expected, iFirst); |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 5798 | break; |
| 5799 | } |
| 5800 | if( checkRef(pCheck, iPage, zContext) ) break; |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 5801 | if( sqlite3PagerGet(pCheck->pPager, (Pgno)iPage, &pOvflPage) ){ |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 5802 | checkAppendMsg(pCheck, zContext, "failed to get page %d", iPage); |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 5803 | break; |
| 5804 | } |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 5805 | pOvflData = (unsigned char *)sqlite3PagerGetData(pOvflPage); |
drh | 30e5875 | 2002-03-02 20:41:57 +0000 | [diff] [blame] | 5806 | if( isFreeList ){ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 5807 | int n = get4byte(&pOvflData[4]); |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 5808 | #ifndef SQLITE_OMIT_AUTOVACUUM |
| 5809 | if( pCheck->pBt->autoVacuum ){ |
| 5810 | checkPtrmap(pCheck, iPage, PTRMAP_FREEPAGE, 0, zContext); |
| 5811 | } |
| 5812 | #endif |
drh | 855eb1c | 2004-08-31 13:45:11 +0000 | [diff] [blame] | 5813 | if( n>pCheck->pBt->usableSize/4-8 ){ |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 5814 | checkAppendMsg(pCheck, zContext, |
| 5815 | "freelist leaf count too big on page %d", iPage); |
drh | ee696e2 | 2004-08-30 16:52:17 +0000 | [diff] [blame] | 5816 | N--; |
| 5817 | }else{ |
| 5818 | for(i=0; i<n; i++){ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 5819 | Pgno iFreePage = get4byte(&pOvflData[8+i*4]); |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 5820 | #ifndef SQLITE_OMIT_AUTOVACUUM |
| 5821 | if( pCheck->pBt->autoVacuum ){ |
| 5822 | checkPtrmap(pCheck, iFreePage, PTRMAP_FREEPAGE, 0, zContext); |
| 5823 | } |
| 5824 | #endif |
| 5825 | checkRef(pCheck, iFreePage, zContext); |
drh | ee696e2 | 2004-08-30 16:52:17 +0000 | [diff] [blame] | 5826 | } |
| 5827 | N -= n; |
drh | 30e5875 | 2002-03-02 20:41:57 +0000 | [diff] [blame] | 5828 | } |
drh | 30e5875 | 2002-03-02 20:41:57 +0000 | [diff] [blame] | 5829 | } |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 5830 | #ifndef SQLITE_OMIT_AUTOVACUUM |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 5831 | else{ |
| 5832 | /* If this database supports auto-vacuum and iPage is not the last |
| 5833 | ** page in this overflow list, check that the pointer-map entry for |
| 5834 | ** the following page matches iPage. |
| 5835 | */ |
| 5836 | if( pCheck->pBt->autoVacuum && N>0 ){ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 5837 | i = get4byte(pOvflData); |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 5838 | checkPtrmap(pCheck, i, PTRMAP_OVERFLOW2, iPage, zContext); |
| 5839 | } |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 5840 | } |
| 5841 | #endif |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 5842 | iPage = get4byte(pOvflData); |
| 5843 | sqlite3PagerUnref(pOvflPage); |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 5844 | } |
| 5845 | } |
drh | b7f9164 | 2004-10-31 02:22:47 +0000 | [diff] [blame] | 5846 | #endif /* SQLITE_OMIT_INTEGRITY_CHECK */ |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 5847 | |
drh | b7f9164 | 2004-10-31 02:22:47 +0000 | [diff] [blame] | 5848 | #ifndef SQLITE_OMIT_INTEGRITY_CHECK |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 5849 | /* |
| 5850 | ** Do various sanity checks on a single page of a tree. Return |
| 5851 | ** the tree depth. Root pages return 0. Parents of root pages |
| 5852 | ** return 1, and so forth. |
| 5853 | ** |
| 5854 | ** These checks are done: |
| 5855 | ** |
| 5856 | ** 1. Make sure that cells and freeblocks do not overlap |
| 5857 | ** but combine to completely cover the page. |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 5858 | ** NO 2. Make sure cell keys are in order. |
| 5859 | ** NO 3. Make sure no key is less than or equal to zLowerBound. |
| 5860 | ** NO 4. Make sure no key is greater than or equal to zUpperBound. |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 5861 | ** 5. Check the integrity of overflow pages. |
| 5862 | ** 6. Recursively call checkTreePage on all children. |
| 5863 | ** 7. Verify that the depth of all children is the same. |
drh | 6019e16 | 2001-07-02 17:51:45 +0000 | [diff] [blame] | 5864 | ** 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] | 5865 | ** the root of the tree. |
| 5866 | */ |
| 5867 | static int checkTreePage( |
drh | aaab572 | 2002-02-19 13:39:21 +0000 | [diff] [blame] | 5868 | IntegrityCk *pCheck, /* Context for the sanity check */ |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 5869 | int iPage, /* Page number of the page to check */ |
| 5870 | MemPage *pParent, /* Parent page */ |
drh | 7416170 | 2006-02-24 02:53:49 +0000 | [diff] [blame] | 5871 | char *zParentContext /* Parent context */ |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 5872 | ){ |
| 5873 | MemPage *pPage; |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 5874 | int i, rc, depth, d2, pgno, cnt; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5875 | int hdr, cellStart; |
| 5876 | int nCell; |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 5877 | u8 *data; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 5878 | BtShared *pBt; |
drh | 4f26bb6 | 2005-09-08 14:17:20 +0000 | [diff] [blame] | 5879 | int usableSize; |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 5880 | char zContext[100]; |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 5881 | char *hit; |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 5882 | |
drh | 5bb3eb9 | 2007-05-04 13:15:55 +0000 | [diff] [blame] | 5883 | sqlite3_snprintf(sizeof(zContext), zContext, "Page %d: ", iPage); |
danielk1977 | ef73ee9 | 2004-11-06 12:26:07 +0000 | [diff] [blame] | 5884 | |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 5885 | /* Check that the page exists |
| 5886 | */ |
drh | d9cb6ac | 2005-10-20 07:28:17 +0000 | [diff] [blame] | 5887 | pBt = pCheck->pBt; |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 5888 | usableSize = pBt->usableSize; |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 5889 | if( iPage==0 ) return 0; |
| 5890 | if( checkRef(pCheck, iPage, zParentContext) ) return 0; |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 5891 | if( (rc = sqlite3BtreeGetPage(pBt, (Pgno)iPage, &pPage, 0))!=0 ){ |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 5892 | checkAppendMsg(pCheck, zContext, |
| 5893 | "unable to get the page. error code=%d", rc); |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 5894 | return 0; |
| 5895 | } |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 5896 | if( (rc = sqlite3BtreeInitPage(pPage, pParent))!=0 ){ |
| 5897 | checkAppendMsg(pCheck, zContext, |
| 5898 | "sqlite3BtreeInitPage() returns error code %d", rc); |
drh | 9102529 | 2004-05-03 19:49:32 +0000 | [diff] [blame] | 5899 | releasePage(pPage); |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 5900 | return 0; |
| 5901 | } |
| 5902 | |
| 5903 | /* Check out all the cells. |
| 5904 | */ |
| 5905 | depth = 0; |
drh | 1dcdbc0 | 2007-01-27 02:24:54 +0000 | [diff] [blame] | 5906 | for(i=0; i<pPage->nCell && pCheck->mxErr; i++){ |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 5907 | u8 *pCell; |
| 5908 | int sz; |
| 5909 | CellInfo info; |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 5910 | |
| 5911 | /* Check payload overflow pages |
| 5912 | */ |
drh | 5bb3eb9 | 2007-05-04 13:15:55 +0000 | [diff] [blame] | 5913 | sqlite3_snprintf(sizeof(zContext), zContext, |
| 5914 | "On tree page %d cell %d: ", iPage, i); |
danielk1977 | 1cc5ed8 | 2007-05-16 17:28:43 +0000 | [diff] [blame] | 5915 | pCell = findCell(pPage,i); |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 5916 | sqlite3BtreeParseCellPtr(pPage, pCell, &info); |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 5917 | sz = info.nData; |
| 5918 | if( !pPage->intKey ) sz += info.nKey; |
drh | 7236583 | 2007-03-06 15:53:44 +0000 | [diff] [blame] | 5919 | assert( sz==info.nPayload ); |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 5920 | if( sz>info.nLocal ){ |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 5921 | int nPage = (sz - info.nLocal + usableSize - 5)/(usableSize - 4); |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 5922 | Pgno pgnoOvfl = get4byte(&pCell[info.iOverflow]); |
| 5923 | #ifndef SQLITE_OMIT_AUTOVACUUM |
| 5924 | if( pBt->autoVacuum ){ |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 5925 | checkPtrmap(pCheck, pgnoOvfl, PTRMAP_OVERFLOW1, iPage, zContext); |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 5926 | } |
| 5927 | #endif |
| 5928 | checkList(pCheck, 0, pgnoOvfl, nPage, zContext); |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 5929 | } |
| 5930 | |
| 5931 | /* Check sanity of left child page. |
| 5932 | */ |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 5933 | if( !pPage->leaf ){ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5934 | pgno = get4byte(pCell); |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 5935 | #ifndef SQLITE_OMIT_AUTOVACUUM |
| 5936 | if( pBt->autoVacuum ){ |
| 5937 | checkPtrmap(pCheck, pgno, PTRMAP_BTREE, iPage, zContext); |
| 5938 | } |
| 5939 | #endif |
drh | 7416170 | 2006-02-24 02:53:49 +0000 | [diff] [blame] | 5940 | d2 = checkTreePage(pCheck,pgno,pPage,zContext); |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 5941 | if( i>0 && d2!=depth ){ |
| 5942 | checkAppendMsg(pCheck, zContext, "Child page depth differs"); |
| 5943 | } |
| 5944 | depth = d2; |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 5945 | } |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 5946 | } |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 5947 | if( !pPage->leaf ){ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5948 | pgno = get4byte(&pPage->aData[pPage->hdrOffset+8]); |
drh | 5bb3eb9 | 2007-05-04 13:15:55 +0000 | [diff] [blame] | 5949 | sqlite3_snprintf(sizeof(zContext), zContext, |
| 5950 | "On page %d at right child: ", iPage); |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 5951 | #ifndef SQLITE_OMIT_AUTOVACUUM |
| 5952 | if( pBt->autoVacuum ){ |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 5953 | checkPtrmap(pCheck, pgno, PTRMAP_BTREE, iPage, 0); |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 5954 | } |
| 5955 | #endif |
drh | 7416170 | 2006-02-24 02:53:49 +0000 | [diff] [blame] | 5956 | checkTreePage(pCheck, pgno, pPage, zContext); |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 5957 | } |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 5958 | |
| 5959 | /* Check for complete coverage of the page |
| 5960 | */ |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 5961 | data = pPage->aData; |
| 5962 | hdr = pPage->hdrOffset; |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 5963 | hit = sqliteMalloc( usableSize ); |
| 5964 | if( hit ){ |
| 5965 | memset(hit, 1, get2byte(&data[hdr+5])); |
| 5966 | nCell = get2byte(&data[hdr+3]); |
| 5967 | cellStart = hdr + 12 - 4*pPage->leaf; |
| 5968 | for(i=0; i<nCell; i++){ |
| 5969 | int pc = get2byte(&data[cellStart+i*2]); |
| 5970 | int size = cellSizePtr(pPage, &data[pc]); |
| 5971 | int j; |
danielk1977 | 7701e81 | 2005-01-10 12:59:51 +0000 | [diff] [blame] | 5972 | if( (pc+size-1)>=usableSize || pc<0 ){ |
| 5973 | checkAppendMsg(pCheck, 0, |
| 5974 | "Corruption detected in cell %d on page %d",i,iPage,0); |
| 5975 | }else{ |
| 5976 | for(j=pc+size-1; j>=pc; j--) hit[j]++; |
| 5977 | } |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 5978 | } |
| 5979 | for(cnt=0, i=get2byte(&data[hdr+1]); i>0 && i<usableSize && cnt<10000; |
| 5980 | cnt++){ |
| 5981 | int size = get2byte(&data[i+2]); |
| 5982 | int j; |
danielk1977 | 7701e81 | 2005-01-10 12:59:51 +0000 | [diff] [blame] | 5983 | if( (i+size-1)>=usableSize || i<0 ){ |
| 5984 | checkAppendMsg(pCheck, 0, |
| 5985 | "Corruption detected in cell %d on page %d",i,iPage,0); |
| 5986 | }else{ |
| 5987 | for(j=i+size-1; j>=i; j--) hit[j]++; |
| 5988 | } |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 5989 | i = get2byte(&data[i]); |
| 5990 | } |
| 5991 | for(i=cnt=0; i<usableSize; i++){ |
| 5992 | if( hit[i]==0 ){ |
| 5993 | cnt++; |
| 5994 | }else if( hit[i]>1 ){ |
| 5995 | checkAppendMsg(pCheck, 0, |
| 5996 | "Multiple uses for byte %d of page %d", i, iPage); |
| 5997 | break; |
| 5998 | } |
| 5999 | } |
| 6000 | if( cnt!=data[hdr+7] ){ |
| 6001 | checkAppendMsg(pCheck, 0, |
| 6002 | "Fragmented space is %d byte reported as %d on page %d", |
| 6003 | cnt, data[hdr+7], iPage); |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6004 | } |
| 6005 | } |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 6006 | sqliteFree(hit); |
drh | 6019e16 | 2001-07-02 17:51:45 +0000 | [diff] [blame] | 6007 | |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 6008 | releasePage(pPage); |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 6009 | return depth+1; |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6010 | } |
drh | b7f9164 | 2004-10-31 02:22:47 +0000 | [diff] [blame] | 6011 | #endif /* SQLITE_OMIT_INTEGRITY_CHECK */ |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6012 | |
drh | b7f9164 | 2004-10-31 02:22:47 +0000 | [diff] [blame] | 6013 | #ifndef SQLITE_OMIT_INTEGRITY_CHECK |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6014 | /* |
| 6015 | ** This routine does a complete check of the given BTree file. aRoot[] is |
| 6016 | ** an array of pages numbers were each page number is the root page of |
| 6017 | ** a table. nRoot is the number of entries in aRoot. |
| 6018 | ** |
| 6019 | ** If everything checks out, this routine returns NULL. If something is |
| 6020 | ** amiss, an error message is written into memory obtained from malloc() |
| 6021 | ** and a pointer to that error message is returned. The calling function |
| 6022 | ** is responsible for freeing the error message when it is done. |
| 6023 | */ |
drh | 1dcdbc0 | 2007-01-27 02:24:54 +0000 | [diff] [blame] | 6024 | char *sqlite3BtreeIntegrityCheck( |
| 6025 | Btree *p, /* The btree to be checked */ |
| 6026 | int *aRoot, /* An array of root pages numbers for individual trees */ |
| 6027 | int nRoot, /* Number of entries in aRoot[] */ |
| 6028 | int mxErr, /* Stop reporting errors after this many */ |
| 6029 | int *pnErr /* Write number of errors seen to this variable */ |
| 6030 | ){ |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6031 | int i; |
| 6032 | int nRef; |
drh | aaab572 | 2002-02-19 13:39:21 +0000 | [diff] [blame] | 6033 | IntegrityCk sCheck; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 6034 | BtShared *pBt = p->pBt; |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6035 | |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 6036 | nRef = sqlite3PagerRefcount(pBt->pPager); |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 6037 | if( lockBtreeWithRetry(p)!=SQLITE_OK ){ |
drh | efc251d | 2001-07-01 22:12:01 +0000 | [diff] [blame] | 6038 | return sqliteStrDup("Unable to acquire a read lock on the database"); |
| 6039 | } |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6040 | sCheck.pBt = pBt; |
| 6041 | sCheck.pPager = pBt->pPager; |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 6042 | sCheck.nPage = sqlite3PagerPagecount(sCheck.pPager); |
drh | 1dcdbc0 | 2007-01-27 02:24:54 +0000 | [diff] [blame] | 6043 | sCheck.mxErr = mxErr; |
| 6044 | sCheck.nErr = 0; |
| 6045 | *pnErr = 0; |
danielk1977 | e5321f0 | 2007-04-27 07:05:44 +0000 | [diff] [blame] | 6046 | #ifndef SQLITE_OMIT_AUTOVACUUM |
| 6047 | if( pBt->nTrunc!=0 ){ |
| 6048 | sCheck.nPage = pBt->nTrunc; |
| 6049 | } |
| 6050 | #endif |
drh | 0de8c11 | 2002-07-06 16:32:14 +0000 | [diff] [blame] | 6051 | if( sCheck.nPage==0 ){ |
| 6052 | unlockBtreeIfUnused(pBt); |
| 6053 | return 0; |
| 6054 | } |
drh | 8c1238a | 2003-01-02 14:43:55 +0000 | [diff] [blame] | 6055 | sCheck.anRef = sqliteMallocRaw( (sCheck.nPage+1)*sizeof(sCheck.anRef[0]) ); |
danielk1977 | ac245ec | 2005-01-14 13:50:11 +0000 | [diff] [blame] | 6056 | if( !sCheck.anRef ){ |
| 6057 | unlockBtreeIfUnused(pBt); |
drh | 1dcdbc0 | 2007-01-27 02:24:54 +0000 | [diff] [blame] | 6058 | *pnErr = 1; |
danielk1977 | ac245ec | 2005-01-14 13:50:11 +0000 | [diff] [blame] | 6059 | return sqlite3MPrintf("Unable to malloc %d bytes", |
| 6060 | (sCheck.nPage+1)*sizeof(sCheck.anRef[0])); |
| 6061 | } |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 6062 | for(i=0; i<=sCheck.nPage; i++){ sCheck.anRef[i] = 0; } |
drh | 42cac6d | 2004-11-20 20:31:11 +0000 | [diff] [blame] | 6063 | i = PENDING_BYTE_PAGE(pBt); |
drh | 1f59571 | 2004-06-15 01:40:29 +0000 | [diff] [blame] | 6064 | if( i<=sCheck.nPage ){ |
| 6065 | sCheck.anRef[i] = 1; |
| 6066 | } |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6067 | sCheck.zErrMsg = 0; |
| 6068 | |
| 6069 | /* Check the integrity of the freelist |
| 6070 | */ |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 6071 | checkList(&sCheck, 1, get4byte(&pBt->pPage1->aData[32]), |
| 6072 | get4byte(&pBt->pPage1->aData[36]), "Main freelist: "); |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6073 | |
| 6074 | /* Check all the tables. |
| 6075 | */ |
drh | 1dcdbc0 | 2007-01-27 02:24:54 +0000 | [diff] [blame] | 6076 | for(i=0; i<nRoot && sCheck.mxErr; i++){ |
drh | 4ff6dfa | 2002-03-03 23:06:00 +0000 | [diff] [blame] | 6077 | if( aRoot[i]==0 ) continue; |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 6078 | #ifndef SQLITE_OMIT_AUTOVACUUM |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 6079 | if( pBt->autoVacuum && aRoot[i]>1 ){ |
| 6080 | checkPtrmap(&sCheck, aRoot[i], PTRMAP_ROOTPAGE, 0, 0); |
| 6081 | } |
| 6082 | #endif |
drh | 7416170 | 2006-02-24 02:53:49 +0000 | [diff] [blame] | 6083 | checkTreePage(&sCheck, aRoot[i], 0, "List of tree roots: "); |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6084 | } |
| 6085 | |
| 6086 | /* Make sure every page in the file is referenced |
| 6087 | */ |
drh | 1dcdbc0 | 2007-01-27 02:24:54 +0000 | [diff] [blame] | 6088 | for(i=1; i<=sCheck.nPage && sCheck.mxErr; i++){ |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 6089 | #ifdef SQLITE_OMIT_AUTOVACUUM |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6090 | if( sCheck.anRef[i]==0 ){ |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 6091 | checkAppendMsg(&sCheck, 0, "Page %d is never used", i); |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6092 | } |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 6093 | #else |
| 6094 | /* If the database supports auto-vacuum, make sure no tables contain |
| 6095 | ** references to pointer-map pages. |
| 6096 | */ |
| 6097 | if( sCheck.anRef[i]==0 && |
danielk1977 | 266664d | 2006-02-10 08:24:21 +0000 | [diff] [blame] | 6098 | (PTRMAP_PAGENO(pBt, i)!=i || !pBt->autoVacuum) ){ |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 6099 | checkAppendMsg(&sCheck, 0, "Page %d is never used", i); |
| 6100 | } |
| 6101 | if( sCheck.anRef[i]!=0 && |
danielk1977 | 266664d | 2006-02-10 08:24:21 +0000 | [diff] [blame] | 6102 | (PTRMAP_PAGENO(pBt, i)==i && pBt->autoVacuum) ){ |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 6103 | checkAppendMsg(&sCheck, 0, "Pointer map page %d is referenced", i); |
| 6104 | } |
| 6105 | #endif |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6106 | } |
| 6107 | |
| 6108 | /* Make sure this analysis did not leave any unref() pages |
| 6109 | */ |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 6110 | unlockBtreeIfUnused(pBt); |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 6111 | if( nRef != sqlite3PagerRefcount(pBt->pPager) ){ |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 6112 | checkAppendMsg(&sCheck, 0, |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6113 | "Outstanding page count goes from %d to %d during this analysis", |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 6114 | nRef, sqlite3PagerRefcount(pBt->pPager) |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6115 | ); |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6116 | } |
| 6117 | |
| 6118 | /* Clean up and report errors. |
| 6119 | */ |
| 6120 | sqliteFree(sCheck.anRef); |
drh | 1dcdbc0 | 2007-01-27 02:24:54 +0000 | [diff] [blame] | 6121 | *pnErr = sCheck.nErr; |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6122 | return sCheck.zErrMsg; |
| 6123 | } |
drh | b7f9164 | 2004-10-31 02:22:47 +0000 | [diff] [blame] | 6124 | #endif /* SQLITE_OMIT_INTEGRITY_CHECK */ |
paul | b95a886 | 2003-04-01 21:16:41 +0000 | [diff] [blame] | 6125 | |
drh | 73509ee | 2003-04-06 20:44:45 +0000 | [diff] [blame] | 6126 | /* |
| 6127 | ** Return the full pathname of the underlying database file. |
| 6128 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 6129 | const char *sqlite3BtreeGetFilename(Btree *p){ |
| 6130 | assert( p->pBt->pPager!=0 ); |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 6131 | return sqlite3PagerFilename(p->pBt->pPager); |
drh | 73509ee | 2003-04-06 20:44:45 +0000 | [diff] [blame] | 6132 | } |
| 6133 | |
| 6134 | /* |
danielk1977 | 5865e3d | 2004-06-14 06:03:57 +0000 | [diff] [blame] | 6135 | ** Return the pathname of the directory that contains the database file. |
| 6136 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 6137 | const char *sqlite3BtreeGetDirname(Btree *p){ |
| 6138 | assert( p->pBt->pPager!=0 ); |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 6139 | return sqlite3PagerDirname(p->pBt->pPager); |
danielk1977 | 5865e3d | 2004-06-14 06:03:57 +0000 | [diff] [blame] | 6140 | } |
| 6141 | |
| 6142 | /* |
| 6143 | ** Return the pathname of the journal file for this database. The return |
| 6144 | ** value of this routine is the same regardless of whether the journal file |
| 6145 | ** has been created or not. |
| 6146 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 6147 | const char *sqlite3BtreeGetJournalname(Btree *p){ |
| 6148 | assert( p->pBt->pPager!=0 ); |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 6149 | return sqlite3PagerJournalname(p->pBt->pPager); |
danielk1977 | 5865e3d | 2004-06-14 06:03:57 +0000 | [diff] [blame] | 6150 | } |
| 6151 | |
drh | b7f9164 | 2004-10-31 02:22:47 +0000 | [diff] [blame] | 6152 | #ifndef SQLITE_OMIT_VACUUM |
danielk1977 | 5865e3d | 2004-06-14 06:03:57 +0000 | [diff] [blame] | 6153 | /* |
drh | f7c5753 | 2003-04-25 13:22:51 +0000 | [diff] [blame] | 6154 | ** Copy the complete content of pBtFrom into pBtTo. A transaction |
| 6155 | ** must be active for both files. |
| 6156 | ** |
| 6157 | ** The size of file pBtFrom may be reduced by this operation. |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 6158 | ** If anything goes wrong, the transaction on pBtFrom is rolled back. |
drh | 73509ee | 2003-04-06 20:44:45 +0000 | [diff] [blame] | 6159 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 6160 | int sqlite3BtreeCopyFile(Btree *pTo, Btree *pFrom){ |
drh | f7c5753 | 2003-04-25 13:22:51 +0000 | [diff] [blame] | 6161 | int rc = SQLITE_OK; |
drh | 50f2f43 | 2005-09-16 11:32:18 +0000 | [diff] [blame] | 6162 | Pgno i, nPage, nToPage, iSkip; |
drh | f7c5753 | 2003-04-25 13:22:51 +0000 | [diff] [blame] | 6163 | |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 6164 | BtShared *pBtTo = pTo->pBt; |
| 6165 | BtShared *pBtFrom = pFrom->pBt; |
| 6166 | |
| 6167 | if( pTo->inTrans!=TRANS_WRITE || pFrom->inTrans!=TRANS_WRITE ){ |
danielk1977 | ee5741e | 2004-05-31 10:01:34 +0000 | [diff] [blame] | 6168 | return SQLITE_ERROR; |
| 6169 | } |
drh | f7c5753 | 2003-04-25 13:22:51 +0000 | [diff] [blame] | 6170 | if( pBtTo->pCursor ) return SQLITE_BUSY; |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 6171 | nToPage = sqlite3PagerPagecount(pBtTo->pPager); |
| 6172 | nPage = sqlite3PagerPagecount(pBtFrom->pPager); |
drh | 50f2f43 | 2005-09-16 11:32:18 +0000 | [diff] [blame] | 6173 | iSkip = PENDING_BYTE_PAGE(pBtTo); |
danielk1977 | 369f27e | 2004-06-15 11:40:04 +0000 | [diff] [blame] | 6174 | for(i=1; rc==SQLITE_OK && i<=nPage; i++){ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 6175 | DbPage *pDbPage; |
drh | 50f2f43 | 2005-09-16 11:32:18 +0000 | [diff] [blame] | 6176 | if( i==iSkip ) continue; |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 6177 | rc = sqlite3PagerGet(pBtFrom->pPager, i, &pDbPage); |
drh | f7c5753 | 2003-04-25 13:22:51 +0000 | [diff] [blame] | 6178 | if( rc ) break; |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 6179 | rc = sqlite3PagerOverwrite(pBtTo->pPager, i, sqlite3PagerGetData(pDbPage)); |
| 6180 | sqlite3PagerUnref(pDbPage); |
drh | f7c5753 | 2003-04-25 13:22:51 +0000 | [diff] [blame] | 6181 | } |
drh | 538f570 | 2007-04-13 02:14:30 +0000 | [diff] [blame] | 6182 | |
| 6183 | /* If the file is shrinking, journal the pages that are being truncated |
| 6184 | ** so that they can be rolled back if the commit fails. |
| 6185 | */ |
drh | 2e6d11b | 2003-04-25 15:37:57 +0000 | [diff] [blame] | 6186 | for(i=nPage+1; rc==SQLITE_OK && i<=nToPage; i++){ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 6187 | DbPage *pDbPage; |
drh | 4928570 | 2005-09-17 15:20:26 +0000 | [diff] [blame] | 6188 | if( i==iSkip ) continue; |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 6189 | rc = sqlite3PagerGet(pBtTo->pPager, i, &pDbPage); |
drh | 2e6d11b | 2003-04-25 15:37:57 +0000 | [diff] [blame] | 6190 | if( rc ) break; |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 6191 | rc = sqlite3PagerWrite(pDbPage); |
drh | 538f570 | 2007-04-13 02:14:30 +0000 | [diff] [blame] | 6192 | sqlite3PagerDontWrite(pDbPage); |
| 6193 | /* Yeah. It seems wierd to call DontWrite() right after Write(). But |
| 6194 | ** that is because the names of those procedures do not exactly |
| 6195 | ** represent what they do. Write() really means "put this page in the |
| 6196 | ** rollback journal and mark it as dirty so that it will be written |
| 6197 | ** to the database file later." DontWrite() undoes the second part of |
| 6198 | ** that and prevents the page from being written to the database. The |
| 6199 | ** page is still on the rollback journal, though. And that is the whole |
| 6200 | ** point of this loop: to put pages on the rollback journal. */ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 6201 | sqlite3PagerUnref(pDbPage); |
drh | 2e6d11b | 2003-04-25 15:37:57 +0000 | [diff] [blame] | 6202 | } |
| 6203 | if( !rc && nPage<nToPage ){ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 6204 | rc = sqlite3PagerTruncate(pBtTo->pPager, nPage); |
drh | 2e6d11b | 2003-04-25 15:37:57 +0000 | [diff] [blame] | 6205 | } |
drh | 538f570 | 2007-04-13 02:14:30 +0000 | [diff] [blame] | 6206 | |
drh | f7c5753 | 2003-04-25 13:22:51 +0000 | [diff] [blame] | 6207 | if( rc ){ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 6208 | sqlite3BtreeRollback(pTo); |
drh | f7c5753 | 2003-04-25 13:22:51 +0000 | [diff] [blame] | 6209 | } |
| 6210 | return rc; |
drh | 73509ee | 2003-04-06 20:44:45 +0000 | [diff] [blame] | 6211 | } |
drh | b7f9164 | 2004-10-31 02:22:47 +0000 | [diff] [blame] | 6212 | #endif /* SQLITE_OMIT_VACUUM */ |
danielk1977 | 1d850a7 | 2004-05-31 08:26:49 +0000 | [diff] [blame] | 6213 | |
| 6214 | /* |
| 6215 | ** Return non-zero if a transaction is active. |
| 6216 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 6217 | int sqlite3BtreeIsInTrans(Btree *p){ |
| 6218 | return (p && (p->inTrans==TRANS_WRITE)); |
danielk1977 | 1d850a7 | 2004-05-31 08:26:49 +0000 | [diff] [blame] | 6219 | } |
| 6220 | |
| 6221 | /* |
| 6222 | ** Return non-zero if a statement transaction is active. |
| 6223 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 6224 | int sqlite3BtreeIsInStmt(Btree *p){ |
| 6225 | return (p->pBt && p->pBt->inStmt); |
danielk1977 | 1d850a7 | 2004-05-31 08:26:49 +0000 | [diff] [blame] | 6226 | } |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 6227 | |
| 6228 | /* |
danielk1977 | 2372c2b | 2006-06-27 16:34:56 +0000 | [diff] [blame] | 6229 | ** Return non-zero if a read (or write) transaction is active. |
| 6230 | */ |
| 6231 | int sqlite3BtreeIsInReadTrans(Btree *p){ |
| 6232 | return (p && (p->inTrans!=TRANS_NONE)); |
| 6233 | } |
| 6234 | |
| 6235 | /* |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 6236 | ** This function returns a pointer to a blob of memory associated with |
| 6237 | ** a single shared-btree. The memory is used by client code for it's own |
| 6238 | ** purposes (for example, to store a high-level schema associated with |
| 6239 | ** the shared-btree). The btree layer manages reference counting issues. |
| 6240 | ** |
| 6241 | ** The first time this is called on a shared-btree, nBytes bytes of memory |
| 6242 | ** are allocated, zeroed, and returned to the caller. For each subsequent |
| 6243 | ** call the nBytes parameter is ignored and a pointer to the same blob |
| 6244 | ** of memory returned. |
| 6245 | ** |
| 6246 | ** Just before the shared-btree is closed, the function passed as the |
| 6247 | ** xFree argument when the memory allocation was made is invoked on the |
| 6248 | ** blob of allocated memory. This function should not call sqliteFree() |
| 6249 | ** on the memory, the btree layer does that. |
| 6250 | */ |
| 6251 | void *sqlite3BtreeSchema(Btree *p, int nBytes, void(*xFree)(void *)){ |
| 6252 | BtShared *pBt = p->pBt; |
| 6253 | if( !pBt->pSchema ){ |
| 6254 | pBt->pSchema = sqliteMalloc(nBytes); |
| 6255 | pBt->xFreeSchema = xFree; |
| 6256 | } |
| 6257 | return pBt->pSchema; |
| 6258 | } |
| 6259 | |
danielk1977 | c87d34d | 2006-01-06 13:00:28 +0000 | [diff] [blame] | 6260 | /* |
| 6261 | ** Return true if another user of the same shared btree as the argument |
| 6262 | ** handle holds an exclusive lock on the sqlite_master table. |
| 6263 | */ |
| 6264 | int sqlite3BtreeSchemaLocked(Btree *p){ |
| 6265 | return (queryTableLock(p, MASTER_ROOT, READ_LOCK)!=SQLITE_OK); |
| 6266 | } |
| 6267 | |
drh | a154dcd | 2006-03-22 22:10:07 +0000 | [diff] [blame] | 6268 | |
| 6269 | #ifndef SQLITE_OMIT_SHARED_CACHE |
| 6270 | /* |
| 6271 | ** Obtain a lock on the table whose root page is iTab. The |
| 6272 | ** lock is a write lock if isWritelock is true or a read lock |
| 6273 | ** if it is false. |
| 6274 | */ |
danielk1977 | c00da10 | 2006-01-07 13:21:04 +0000 | [diff] [blame] | 6275 | int sqlite3BtreeLockTable(Btree *p, int iTab, u8 isWriteLock){ |
danielk1977 | 2e94d4d | 2006-01-09 05:36:27 +0000 | [diff] [blame] | 6276 | int rc = SQLITE_OK; |
danielk1977 | c00da10 | 2006-01-07 13:21:04 +0000 | [diff] [blame] | 6277 | u8 lockType = (isWriteLock?WRITE_LOCK:READ_LOCK); |
danielk1977 | 2e94d4d | 2006-01-09 05:36:27 +0000 | [diff] [blame] | 6278 | rc = queryTableLock(p, iTab, lockType); |
danielk1977 | c00da10 | 2006-01-07 13:21:04 +0000 | [diff] [blame] | 6279 | if( rc==SQLITE_OK ){ |
| 6280 | rc = lockTable(p, iTab, lockType); |
| 6281 | } |
| 6282 | return rc; |
| 6283 | } |
drh | a154dcd | 2006-03-22 22:10:07 +0000 | [diff] [blame] | 6284 | #endif |
danielk1977 | b82e7ed | 2006-01-11 14:09:31 +0000 | [diff] [blame] | 6285 | |
danielk1977 | b4e9af9 | 2007-05-01 17:49:49 +0000 | [diff] [blame] | 6286 | #ifndef SQLITE_OMIT_INCRBLOB |
| 6287 | /* |
| 6288 | ** Argument pCsr must be a cursor opened for writing on an |
| 6289 | ** INTKEY table currently pointing at a valid table entry. |
| 6290 | ** This function modifies the data stored as part of that entry. |
| 6291 | ** Only the data content may only be modified, it is not possible |
| 6292 | ** to change the length of the data stored. |
| 6293 | */ |
danielk1977 | dcbb5d3 | 2007-05-04 18:36:44 +0000 | [diff] [blame] | 6294 | int sqlite3BtreePutData(BtCursor *pCsr, u32 offset, u32 amt, void *z){ |
danielk1977 | d0441796 | 2007-05-02 13:16:30 +0000 | [diff] [blame] | 6295 | |
danielk1977 | dcbb5d3 | 2007-05-04 18:36:44 +0000 | [diff] [blame] | 6296 | assert(pCsr->isIncrblobHandle); |
| 6297 | if( pCsr->eState==CURSOR_REQUIRESEEK ){ |
| 6298 | return SQLITE_ABORT; |
| 6299 | } |
| 6300 | |
danielk1977 | d0441796 | 2007-05-02 13:16:30 +0000 | [diff] [blame] | 6301 | /* Check some preconditions: |
danielk1977 | dcbb5d3 | 2007-05-04 18:36:44 +0000 | [diff] [blame] | 6302 | ** (a) the cursor is open for writing, |
| 6303 | ** (b) there is no read-lock on the table being modified and |
| 6304 | ** (c) the cursor points at a valid row of an intKey table. |
danielk1977 | d0441796 | 2007-05-02 13:16:30 +0000 | [diff] [blame] | 6305 | */ |
danielk1977 | d0441796 | 2007-05-02 13:16:30 +0000 | [diff] [blame] | 6306 | if( !pCsr->wrFlag ){ |
danielk1977 | dcbb5d3 | 2007-05-04 18:36:44 +0000 | [diff] [blame] | 6307 | return SQLITE_READONLY; |
danielk1977 | d0441796 | 2007-05-02 13:16:30 +0000 | [diff] [blame] | 6308 | } |
drh | 87cc3b3 | 2007-05-08 21:45:27 +0000 | [diff] [blame] | 6309 | assert( !pCsr->pBtree->pBt->readOnly |
| 6310 | && pCsr->pBtree->pBt->inTransaction==TRANS_WRITE ); |
danielk1977 | d0441796 | 2007-05-02 13:16:30 +0000 | [diff] [blame] | 6311 | if( checkReadLocks(pCsr->pBtree, pCsr->pgnoRoot, pCsr) ){ |
| 6312 | return SQLITE_LOCKED; /* The table pCur points to has a read lock */ |
| 6313 | } |
| 6314 | if( pCsr->eState==CURSOR_INVALID || !pCsr->pPage->intKey ){ |
| 6315 | return SQLITE_ERROR; |
danielk1977 | b4e9af9 | 2007-05-01 17:49:49 +0000 | [diff] [blame] | 6316 | } |
| 6317 | |
danielk1977 | 9f8d640 | 2007-05-02 17:48:45 +0000 | [diff] [blame] | 6318 | return accessPayload(pCsr, offset, amt, (unsigned char *)z, 0, 1); |
danielk1977 | b4e9af9 | 2007-05-01 17:49:49 +0000 | [diff] [blame] | 6319 | } |
danielk1977 | 2dec970 | 2007-05-02 16:48:37 +0000 | [diff] [blame] | 6320 | |
| 6321 | /* |
| 6322 | ** 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] | 6323 | ** overflow list for the current row. This is used by cursors opened |
| 6324 | ** for incremental blob IO only. |
| 6325 | ** |
| 6326 | ** This function sets a flag only. The actual page location cache |
| 6327 | ** (stored in BtCursor.aOverflow[]) is allocated and used by function |
| 6328 | ** accessPayload() (the worker function for sqlite3BtreeData() and |
| 6329 | ** sqlite3BtreePutData()). |
danielk1977 | 2dec970 | 2007-05-02 16:48:37 +0000 | [diff] [blame] | 6330 | */ |
| 6331 | void sqlite3BtreeCacheOverflow(BtCursor *pCur){ |
danielk1977 | dcbb5d3 | 2007-05-04 18:36:44 +0000 | [diff] [blame] | 6332 | assert(!pCur->isIncrblobHandle); |
danielk1977 | 2dec970 | 2007-05-02 16:48:37 +0000 | [diff] [blame] | 6333 | assert(!pCur->aOverflow); |
danielk1977 | dcbb5d3 | 2007-05-04 18:36:44 +0000 | [diff] [blame] | 6334 | pCur->isIncrblobHandle = 1; |
danielk1977 | 2dec970 | 2007-05-02 16:48:37 +0000 | [diff] [blame] | 6335 | } |
danielk1977 | b4e9af9 | 2007-05-01 17:49:49 +0000 | [diff] [blame] | 6336 | #endif |