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 | ************************************************************************* |
drh | fd131da | 2007-08-07 17:13:03 +0000 | [diff] [blame^] | 12 | ** $Id: btree.c,v 1.395 2007/08/07 17:13:04 drh 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 | e6e4d6b | 2007-08-05 23:52:05 +0000 | [diff] [blame] | 424 | #ifdef SQLITE_TEST |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 425 | u8 *sqlite3BtreeFindCell(MemPage *pPage, int iCell){ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 426 | assert( iCell>=0 ); |
drh | 029f3f8 | 2007-06-20 15:14:10 +0000 | [diff] [blame] | 427 | assert( iCell<get2byte(&pPage->aData[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 | } |
drh | e6e4d6b | 2007-08-05 23:52:05 +0000 | [diff] [blame] | 430 | #endif |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 431 | |
| 432 | /* |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 433 | ** This a more complex version of sqlite3BtreeFindCell() that works for |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 434 | ** pages that do contain overflow cells. See insert |
| 435 | */ |
| 436 | static u8 *findOverflowCell(MemPage *pPage, int iCell){ |
| 437 | int i; |
| 438 | for(i=pPage->nOverflow-1; i>=0; i--){ |
drh | 6d08b4d | 2004-07-20 12:45:22 +0000 | [diff] [blame] | 439 | int k; |
| 440 | struct _OvflCell *pOvfl; |
| 441 | pOvfl = &pPage->aOvfl[i]; |
| 442 | k = pOvfl->idx; |
| 443 | if( k<=iCell ){ |
| 444 | if( k==iCell ){ |
| 445 | return pOvfl->pCell; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 446 | } |
| 447 | iCell--; |
| 448 | } |
| 449 | } |
danielk1977 | 1cc5ed8 | 2007-05-16 17:28:43 +0000 | [diff] [blame] | 450 | return findCell(pPage, iCell); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 451 | } |
| 452 | |
| 453 | /* |
| 454 | ** Parse a cell content block and fill in the CellInfo structure. There |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 455 | ** are two versions of this function. sqlite3BtreeParseCell() takes a |
| 456 | ** cell index as the second argument and sqlite3BtreeParseCellPtr() |
| 457 | ** takes a pointer to the body of the cell as its second argument. |
danielk1977 | 1cc5ed8 | 2007-05-16 17:28:43 +0000 | [diff] [blame] | 458 | ** |
| 459 | ** Within this file, the parseCell() macro can be called instead of |
| 460 | ** sqlite3BtreeParseCellPtr(). Using some compilers, this will be faster. |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 461 | */ |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 462 | void sqlite3BtreeParseCellPtr( |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 463 | MemPage *pPage, /* Page containing the cell */ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 464 | u8 *pCell, /* Pointer to the cell text. */ |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 465 | CellInfo *pInfo /* Fill in this structure */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 466 | ){ |
drh | 271efa5 | 2004-05-30 19:19:05 +0000 | [diff] [blame] | 467 | int n; /* Number bytes in cell content header */ |
| 468 | u32 nPayload; /* Number of bytes of cell payload */ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 469 | |
| 470 | pInfo->pCell = pCell; |
drh | ab01f61 | 2004-05-22 02:55:23 +0000 | [diff] [blame] | 471 | assert( pPage->leaf==0 || pPage->leaf==1 ); |
drh | 271efa5 | 2004-05-30 19:19:05 +0000 | [diff] [blame] | 472 | n = pPage->childPtrSize; |
| 473 | assert( n==4-4*pPage->leaf ); |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 474 | if( pPage->hasData ){ |
drh | 271efa5 | 2004-05-30 19:19:05 +0000 | [diff] [blame] | 475 | n += getVarint32(&pCell[n], &nPayload); |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 476 | }else{ |
drh | 271efa5 | 2004-05-30 19:19:05 +0000 | [diff] [blame] | 477 | nPayload = 0; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 478 | } |
drh | 271efa5 | 2004-05-30 19:19:05 +0000 | [diff] [blame] | 479 | pInfo->nData = nPayload; |
drh | 504b698 | 2006-01-22 21:52:56 +0000 | [diff] [blame] | 480 | if( pPage->intKey ){ |
| 481 | n += getVarint(&pCell[n], (u64 *)&pInfo->nKey); |
| 482 | }else{ |
| 483 | u32 x; |
| 484 | n += getVarint32(&pCell[n], &x); |
| 485 | pInfo->nKey = x; |
| 486 | nPayload += x; |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 487 | } |
drh | 7236583 | 2007-03-06 15:53:44 +0000 | [diff] [blame] | 488 | pInfo->nPayload = nPayload; |
drh | 504b698 | 2006-01-22 21:52:56 +0000 | [diff] [blame] | 489 | pInfo->nHeader = n; |
drh | 271efa5 | 2004-05-30 19:19:05 +0000 | [diff] [blame] | 490 | if( nPayload<=pPage->maxLocal ){ |
| 491 | /* This is the (easy) common case where the entire payload fits |
| 492 | ** on the local page. No overflow is required. |
| 493 | */ |
| 494 | int nSize; /* Total size of cell content in bytes */ |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 495 | pInfo->nLocal = nPayload; |
| 496 | pInfo->iOverflow = 0; |
drh | 271efa5 | 2004-05-30 19:19:05 +0000 | [diff] [blame] | 497 | nSize = nPayload + n; |
| 498 | if( nSize<4 ){ |
| 499 | nSize = 4; /* Minimum cell size is 4 */ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 500 | } |
drh | 271efa5 | 2004-05-30 19:19:05 +0000 | [diff] [blame] | 501 | pInfo->nSize = nSize; |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 502 | }else{ |
drh | 271efa5 | 2004-05-30 19:19:05 +0000 | [diff] [blame] | 503 | /* If the payload will not fit completely on the local page, we have |
| 504 | ** to decide how much to store locally and how much to spill onto |
| 505 | ** overflow pages. The strategy is to minimize the amount of unused |
| 506 | ** space on overflow pages while keeping the amount of local storage |
| 507 | ** in between minLocal and maxLocal. |
| 508 | ** |
| 509 | ** Warning: changing the way overflow payload is distributed in any |
| 510 | ** way will result in an incompatible file format. |
| 511 | */ |
| 512 | int minLocal; /* Minimum amount of payload held locally */ |
| 513 | int maxLocal; /* Maximum amount of payload held locally */ |
| 514 | int surplus; /* Overflow payload available for local storage */ |
| 515 | |
| 516 | minLocal = pPage->minLocal; |
| 517 | maxLocal = pPage->maxLocal; |
| 518 | surplus = minLocal + (nPayload - minLocal)%(pPage->pBt->usableSize - 4); |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 519 | if( surplus <= maxLocal ){ |
| 520 | pInfo->nLocal = surplus; |
| 521 | }else{ |
| 522 | pInfo->nLocal = minLocal; |
| 523 | } |
| 524 | pInfo->iOverflow = pInfo->nLocal + n; |
| 525 | pInfo->nSize = pInfo->iOverflow + 4; |
| 526 | } |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 527 | } |
danielk1977 | 1cc5ed8 | 2007-05-16 17:28:43 +0000 | [diff] [blame] | 528 | #define parseCell(pPage, iCell, pInfo) \ |
| 529 | sqlite3BtreeParseCellPtr((pPage), findCell((pPage), (iCell)), (pInfo)) |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 530 | void sqlite3BtreeParseCell( |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 531 | MemPage *pPage, /* Page containing the cell */ |
| 532 | int iCell, /* The cell index. First cell is 0 */ |
| 533 | CellInfo *pInfo /* Fill in this structure */ |
| 534 | ){ |
danielk1977 | 1cc5ed8 | 2007-05-16 17:28:43 +0000 | [diff] [blame] | 535 | parseCell(pPage, iCell, pInfo); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 536 | } |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 537 | |
| 538 | /* |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 539 | ** Compute the total number of bytes that a Cell needs in the cell |
| 540 | ** data area of the btree-page. The return number includes the cell |
| 541 | ** data header and the local payload, but not any overflow page or |
| 542 | ** the space used by the cell pointer. |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 543 | */ |
danielk1977 | bc6ada4 | 2004-06-30 08:20:16 +0000 | [diff] [blame] | 544 | #ifndef NDEBUG |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 545 | static int cellSize(MemPage *pPage, int iCell){ |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 546 | CellInfo info; |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 547 | sqlite3BtreeParseCell(pPage, iCell, &info); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 548 | return info.nSize; |
| 549 | } |
danielk1977 | bc6ada4 | 2004-06-30 08:20:16 +0000 | [diff] [blame] | 550 | #endif |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 551 | static int cellSizePtr(MemPage *pPage, u8 *pCell){ |
| 552 | CellInfo info; |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 553 | sqlite3BtreeParseCellPtr(pPage, pCell, &info); |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 554 | return info.nSize; |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 555 | } |
| 556 | |
danielk1977 | 79a40da | 2005-01-16 08:00:01 +0000 | [diff] [blame] | 557 | #ifndef SQLITE_OMIT_AUTOVACUUM |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 558 | /* |
danielk1977 | 2683665 | 2005-01-17 01:33:13 +0000 | [diff] [blame] | 559 | ** If the cell pCell, part of page pPage contains a pointer |
danielk1977 | 79a40da | 2005-01-16 08:00:01 +0000 | [diff] [blame] | 560 | ** to an overflow page, insert an entry into the pointer-map |
| 561 | ** for the overflow page. |
danielk1977 | ac11ee6 | 2005-01-15 12:45:51 +0000 | [diff] [blame] | 562 | */ |
danielk1977 | 2683665 | 2005-01-17 01:33:13 +0000 | [diff] [blame] | 563 | static int ptrmapPutOvflPtr(MemPage *pPage, u8 *pCell){ |
danielk1977 | 79a40da | 2005-01-16 08:00:01 +0000 | [diff] [blame] | 564 | if( pCell ){ |
| 565 | CellInfo info; |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 566 | sqlite3BtreeParseCellPtr(pPage, pCell, &info); |
drh | 7236583 | 2007-03-06 15:53:44 +0000 | [diff] [blame] | 567 | assert( (info.nData+(pPage->intKey?0:info.nKey))==info.nPayload ); |
danielk1977 | 79a40da | 2005-01-16 08:00:01 +0000 | [diff] [blame] | 568 | if( (info.nData+(pPage->intKey?0:info.nKey))>info.nLocal ){ |
| 569 | Pgno ovfl = get4byte(&pCell[info.iOverflow]); |
| 570 | return ptrmapPut(pPage->pBt, ovfl, PTRMAP_OVERFLOW1, pPage->pgno); |
| 571 | } |
danielk1977 | ac11ee6 | 2005-01-15 12:45:51 +0000 | [diff] [blame] | 572 | } |
danielk1977 | 79a40da | 2005-01-16 08:00:01 +0000 | [diff] [blame] | 573 | return SQLITE_OK; |
danielk1977 | ac11ee6 | 2005-01-15 12:45:51 +0000 | [diff] [blame] | 574 | } |
danielk1977 | 2683665 | 2005-01-17 01:33:13 +0000 | [diff] [blame] | 575 | /* |
| 576 | ** If the cell with index iCell on page pPage contains a pointer |
| 577 | ** to an overflow page, insert an entry into the pointer-map |
| 578 | ** for the overflow page. |
| 579 | */ |
| 580 | static int ptrmapPutOvfl(MemPage *pPage, int iCell){ |
| 581 | u8 *pCell; |
| 582 | pCell = findOverflowCell(pPage, iCell); |
| 583 | return ptrmapPutOvflPtr(pPage, pCell); |
| 584 | } |
danielk1977 | 79a40da | 2005-01-16 08:00:01 +0000 | [diff] [blame] | 585 | #endif |
| 586 | |
danielk1977 | ac11ee6 | 2005-01-15 12:45:51 +0000 | [diff] [blame] | 587 | |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 588 | /* |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 589 | ** Defragment the page given. All Cells are moved to the |
drh | 3a4a2d4 | 2005-11-24 14:24:28 +0000 | [diff] [blame] | 590 | ** end of the page and all free space is collected into one |
| 591 | ** big FreeBlk that occurs in between the header and cell |
drh | 31beae9 | 2005-11-24 14:34:36 +0000 | [diff] [blame] | 592 | ** pointer array and the cell content area. |
drh | 365d68f | 2001-05-11 11:02:46 +0000 | [diff] [blame] | 593 | */ |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 594 | static int defragmentPage(MemPage *pPage){ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 595 | int i; /* Loop counter */ |
| 596 | int pc; /* Address of a i-th cell */ |
| 597 | int addr; /* Offset of first byte after cell pointer array */ |
| 598 | int hdr; /* Offset to the page header */ |
| 599 | int size; /* Size of a cell */ |
| 600 | int usableSize; /* Number of usable bytes on a page */ |
| 601 | int cellOffset; /* Offset to the cell pointer array */ |
| 602 | int brk; /* Offset to the cell content area */ |
| 603 | int nCell; /* Number of cells on the page */ |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 604 | unsigned char *data; /* The page data */ |
| 605 | unsigned char *temp; /* Temp area for cell content */ |
drh | 2af926b | 2001-05-15 00:39:25 +0000 | [diff] [blame] | 606 | |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 607 | assert( sqlite3PagerIswriteable(pPage->pDbPage) ); |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 608 | assert( pPage->pBt!=0 ); |
drh | 90f5ecb | 2004-07-22 01:19:35 +0000 | [diff] [blame] | 609 | assert( pPage->pBt->usableSize <= SQLITE_MAX_PAGE_SIZE ); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 610 | assert( pPage->nOverflow==0 ); |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 611 | temp = sqliteMalloc( pPage->pBt->pageSize ); |
| 612 | if( temp==0 ) return SQLITE_NOMEM; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 613 | data = pPage->aData; |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 614 | hdr = pPage->hdrOffset; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 615 | cellOffset = pPage->cellOffset; |
| 616 | nCell = pPage->nCell; |
| 617 | assert( nCell==get2byte(&data[hdr+3]) ); |
| 618 | usableSize = pPage->pBt->usableSize; |
| 619 | brk = get2byte(&data[hdr+5]); |
| 620 | memcpy(&temp[brk], &data[brk], usableSize - brk); |
| 621 | brk = usableSize; |
| 622 | for(i=0; i<nCell; i++){ |
| 623 | u8 *pAddr; /* The i-th cell pointer */ |
| 624 | pAddr = &data[cellOffset + i*2]; |
| 625 | pc = get2byte(pAddr); |
| 626 | assert( pc<pPage->pBt->usableSize ); |
| 627 | size = cellSizePtr(pPage, &temp[pc]); |
| 628 | brk -= size; |
| 629 | memcpy(&data[brk], &temp[pc], size); |
| 630 | put2byte(pAddr, brk); |
drh | 2af926b | 2001-05-15 00:39:25 +0000 | [diff] [blame] | 631 | } |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 632 | assert( brk>=cellOffset+2*nCell ); |
| 633 | put2byte(&data[hdr+5], brk); |
| 634 | data[hdr+1] = 0; |
| 635 | data[hdr+2] = 0; |
| 636 | data[hdr+7] = 0; |
| 637 | addr = cellOffset+2*nCell; |
| 638 | memset(&data[addr], 0, brk-addr); |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 639 | sqliteFree(temp); |
| 640 | return SQLITE_OK; |
drh | 365d68f | 2001-05-11 11:02:46 +0000 | [diff] [blame] | 641 | } |
| 642 | |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 643 | /* |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 644 | ** Allocate nByte bytes of space on a page. |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 645 | ** |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 646 | ** Return the index into pPage->aData[] of the first byte of |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 647 | ** the new allocation. Or return 0 if there is not enough free |
| 648 | ** space on the page to satisfy the allocation request. |
drh | 2af926b | 2001-05-15 00:39:25 +0000 | [diff] [blame] | 649 | ** |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 650 | ** If the page contains nBytes of free space but does not contain |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 651 | ** nBytes of contiguous free space, then this routine automatically |
| 652 | ** calls defragementPage() to consolidate all free space before |
| 653 | ** allocating the new chunk. |
drh | 7e3b0a0 | 2001-04-28 16:52:40 +0000 | [diff] [blame] | 654 | */ |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 655 | static int allocateSpace(MemPage *pPage, int nByte){ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 656 | int addr, pc, hdr; |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 657 | int size; |
drh | 24cd67e | 2004-05-10 16:18:47 +0000 | [diff] [blame] | 658 | int nFrag; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 659 | int top; |
| 660 | int nCell; |
| 661 | int cellOffset; |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 662 | unsigned char *data; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 663 | |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 664 | data = pPage->aData; |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 665 | assert( sqlite3PagerIswriteable(pPage->pDbPage) ); |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 666 | assert( pPage->pBt ); |
| 667 | if( nByte<4 ) nByte = 4; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 668 | if( pPage->nFree<nByte || pPage->nOverflow>0 ) return 0; |
| 669 | pPage->nFree -= nByte; |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 670 | hdr = pPage->hdrOffset; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 671 | |
| 672 | nFrag = data[hdr+7]; |
| 673 | if( nFrag<60 ){ |
| 674 | /* Search the freelist looking for a slot big enough to satisfy the |
| 675 | ** space request. */ |
| 676 | addr = hdr+1; |
| 677 | while( (pc = get2byte(&data[addr]))>0 ){ |
| 678 | size = get2byte(&data[pc+2]); |
| 679 | if( size>=nByte ){ |
| 680 | if( size<nByte+4 ){ |
| 681 | memcpy(&data[addr], &data[pc], 2); |
| 682 | data[hdr+7] = nFrag + size - nByte; |
| 683 | return pc; |
| 684 | }else{ |
| 685 | put2byte(&data[pc+2], size-nByte); |
| 686 | return pc + size - nByte; |
| 687 | } |
| 688 | } |
| 689 | addr = pc; |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 690 | } |
| 691 | } |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 692 | |
| 693 | /* Allocate memory from the gap in between the cell pointer array |
| 694 | ** and the cell content area. |
| 695 | */ |
| 696 | top = get2byte(&data[hdr+5]); |
| 697 | nCell = get2byte(&data[hdr+3]); |
| 698 | cellOffset = pPage->cellOffset; |
| 699 | if( nFrag>=60 || cellOffset + 2*nCell > top - nByte ){ |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 700 | if( defragmentPage(pPage) ) return 0; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 701 | top = get2byte(&data[hdr+5]); |
drh | 2af926b | 2001-05-15 00:39:25 +0000 | [diff] [blame] | 702 | } |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 703 | top -= nByte; |
| 704 | assert( cellOffset + 2*nCell <= top ); |
| 705 | put2byte(&data[hdr+5], top); |
| 706 | return top; |
drh | 7e3b0a0 | 2001-04-28 16:52:40 +0000 | [diff] [blame] | 707 | } |
| 708 | |
| 709 | /* |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 710 | ** Return a section of the pPage->aData to the freelist. |
| 711 | ** The first byte of the new free block is pPage->aDisk[start] |
| 712 | ** and the size of the block is "size" bytes. |
drh | 306dc21 | 2001-05-21 13:45:10 +0000 | [diff] [blame] | 713 | ** |
| 714 | ** Most of the effort here is involved in coalesing adjacent |
| 715 | ** free blocks into a single big free block. |
drh | 7e3b0a0 | 2001-04-28 16:52:40 +0000 | [diff] [blame] | 716 | */ |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 717 | static void freeSpace(MemPage *pPage, int start, int size){ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 718 | int addr, pbegin, hdr; |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 719 | unsigned char *data = pPage->aData; |
drh | 2af926b | 2001-05-15 00:39:25 +0000 | [diff] [blame] | 720 | |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 721 | assert( pPage->pBt!=0 ); |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 722 | assert( sqlite3PagerIswriteable(pPage->pDbPage) ); |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 723 | assert( start>=pPage->hdrOffset+6+(pPage->leaf?0:4) ); |
danielk1977 | bc6ada4 | 2004-06-30 08:20:16 +0000 | [diff] [blame] | 724 | assert( (start + size)<=pPage->pBt->usableSize ); |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 725 | if( size<4 ) size = 4; |
| 726 | |
drh | fcce93f | 2006-02-22 03:08:32 +0000 | [diff] [blame] | 727 | #ifdef SQLITE_SECURE_DELETE |
| 728 | /* Overwrite deleted information with zeros when the SECURE_DELETE |
| 729 | ** option is enabled at compile-time */ |
| 730 | memset(&data[start], 0, size); |
| 731 | #endif |
| 732 | |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 733 | /* Add the space back into the linked list of freeblocks */ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 734 | hdr = pPage->hdrOffset; |
| 735 | addr = hdr + 1; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 736 | while( (pbegin = get2byte(&data[addr]))<start && pbegin>0 ){ |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 737 | assert( pbegin<=pPage->pBt->usableSize-4 ); |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 738 | assert( pbegin>addr ); |
| 739 | addr = pbegin; |
drh | 2af926b | 2001-05-15 00:39:25 +0000 | [diff] [blame] | 740 | } |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 741 | assert( pbegin<=pPage->pBt->usableSize-4 ); |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 742 | assert( pbegin>addr || pbegin==0 ); |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 743 | put2byte(&data[addr], start); |
| 744 | put2byte(&data[start], pbegin); |
| 745 | put2byte(&data[start+2], size); |
drh | 2af926b | 2001-05-15 00:39:25 +0000 | [diff] [blame] | 746 | pPage->nFree += size; |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 747 | |
| 748 | /* Coalesce adjacent free blocks */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 749 | addr = pPage->hdrOffset + 1; |
| 750 | while( (pbegin = get2byte(&data[addr]))>0 ){ |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 751 | int pnext, psize; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 752 | assert( pbegin>addr ); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 753 | assert( pbegin<=pPage->pBt->usableSize-4 ); |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 754 | pnext = get2byte(&data[pbegin]); |
| 755 | psize = get2byte(&data[pbegin+2]); |
| 756 | if( pbegin + psize + 3 >= pnext && pnext>0 ){ |
| 757 | int frag = pnext - (pbegin+psize); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 758 | assert( frag<=data[pPage->hdrOffset+7] ); |
| 759 | data[pPage->hdrOffset+7] -= frag; |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 760 | put2byte(&data[pbegin], get2byte(&data[pnext])); |
| 761 | put2byte(&data[pbegin+2], pnext+get2byte(&data[pnext+2])-pbegin); |
| 762 | }else{ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 763 | addr = pbegin; |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 764 | } |
| 765 | } |
drh | 7e3b0a0 | 2001-04-28 16:52:40 +0000 | [diff] [blame] | 766 | |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 767 | /* If the cell content area begins with a freeblock, remove it. */ |
| 768 | if( data[hdr+1]==data[hdr+5] && data[hdr+2]==data[hdr+6] ){ |
| 769 | int top; |
| 770 | pbegin = get2byte(&data[hdr+1]); |
| 771 | memcpy(&data[hdr+1], &data[pbegin], 2); |
| 772 | top = get2byte(&data[hdr+5]); |
| 773 | put2byte(&data[hdr+5], top + get2byte(&data[pbegin+2])); |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 774 | } |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 775 | } |
| 776 | |
| 777 | /* |
drh | 271efa5 | 2004-05-30 19:19:05 +0000 | [diff] [blame] | 778 | ** Decode the flags byte (the first byte of the header) for a page |
| 779 | ** and initialize fields of the MemPage structure accordingly. |
| 780 | */ |
| 781 | static void decodeFlags(MemPage *pPage, int flagByte){ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 782 | BtShared *pBt; /* A copy of pPage->pBt */ |
drh | 271efa5 | 2004-05-30 19:19:05 +0000 | [diff] [blame] | 783 | |
| 784 | assert( pPage->hdrOffset==(pPage->pgno==1 ? 100 : 0) ); |
| 785 | pPage->intKey = (flagByte & (PTF_INTKEY|PTF_LEAFDATA))!=0; |
| 786 | pPage->zeroData = (flagByte & PTF_ZERODATA)!=0; |
| 787 | pPage->leaf = (flagByte & PTF_LEAF)!=0; |
| 788 | pPage->childPtrSize = 4*(pPage->leaf==0); |
| 789 | pBt = pPage->pBt; |
| 790 | if( flagByte & PTF_LEAFDATA ){ |
| 791 | pPage->leafData = 1; |
| 792 | pPage->maxLocal = pBt->maxLeaf; |
| 793 | pPage->minLocal = pBt->minLeaf; |
| 794 | }else{ |
| 795 | pPage->leafData = 0; |
| 796 | pPage->maxLocal = pBt->maxLocal; |
| 797 | pPage->minLocal = pBt->minLocal; |
| 798 | } |
| 799 | pPage->hasData = !(pPage->zeroData || (!pPage->leaf && pPage->leafData)); |
| 800 | } |
| 801 | |
| 802 | /* |
drh | 7e3b0a0 | 2001-04-28 16:52:40 +0000 | [diff] [blame] | 803 | ** Initialize the auxiliary information for a disk block. |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 804 | ** |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 805 | ** The pParent parameter must be a pointer to the MemPage which |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 806 | ** is the parent of the page being initialized. The root of a |
| 807 | ** BTree has no parent and so for that page, pParent==NULL. |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 808 | ** |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 809 | ** Return SQLITE_OK on success. If we see that the page does |
drh | da47d77 | 2002-12-02 04:25:19 +0000 | [diff] [blame] | 810 | ** not contain a well-formed database page, then return |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 811 | ** SQLITE_CORRUPT. Note that a return of SQLITE_OK does not |
| 812 | ** guarantee that the page is well-formed. It only shows that |
| 813 | ** we failed to detect any corruption. |
drh | 7e3b0a0 | 2001-04-28 16:52:40 +0000 | [diff] [blame] | 814 | */ |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 815 | int sqlite3BtreeInitPage( |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 816 | MemPage *pPage, /* The page to be initialized */ |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 817 | MemPage *pParent /* The parent. Might be NULL */ |
| 818 | ){ |
drh | 271efa5 | 2004-05-30 19:19:05 +0000 | [diff] [blame] | 819 | int pc; /* Address of a freeblock within pPage->aData[] */ |
drh | 271efa5 | 2004-05-30 19:19:05 +0000 | [diff] [blame] | 820 | int hdr; /* Offset to beginning of page header */ |
| 821 | u8 *data; /* Equal to pPage->aData */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 822 | BtShared *pBt; /* The main btree structure */ |
drh | 271efa5 | 2004-05-30 19:19:05 +0000 | [diff] [blame] | 823 | int usableSize; /* Amount of usable space on each page */ |
| 824 | int cellOffset; /* Offset from start of page to first cell pointer */ |
| 825 | int nFree; /* Number of unused bytes on the page */ |
| 826 | int top; /* First byte of the cell content area */ |
drh | 2af926b | 2001-05-15 00:39:25 +0000 | [diff] [blame] | 827 | |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 828 | pBt = pPage->pBt; |
| 829 | assert( pBt!=0 ); |
| 830 | assert( pParent==0 || pParent->pBt==pBt ); |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 831 | assert( pPage->pgno==sqlite3PagerPagenumber(pPage->pDbPage) ); |
drh | 07d183d | 2005-05-01 22:52:42 +0000 | [diff] [blame] | 832 | assert( pPage->aData == &((unsigned char*)pPage)[-pBt->pageSize] ); |
drh | ee696e2 | 2004-08-30 16:52:17 +0000 | [diff] [blame] | 833 | if( pPage->pParent!=pParent && (pPage->pParent!=0 || pPage->isInit) ){ |
| 834 | /* The parent page should never change unless the file is corrupt */ |
drh | 4928570 | 2005-09-17 15:20:26 +0000 | [diff] [blame] | 835 | return SQLITE_CORRUPT_BKPT; |
drh | ee696e2 | 2004-08-30 16:52:17 +0000 | [diff] [blame] | 836 | } |
drh | 10617cd | 2004-05-14 15:27:27 +0000 | [diff] [blame] | 837 | if( pPage->isInit ) return SQLITE_OK; |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 838 | if( pPage->pParent==0 && pParent!=0 ){ |
| 839 | pPage->pParent = pParent; |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 840 | sqlite3PagerRef(pParent->pDbPage); |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 841 | } |
drh | de64713 | 2004-05-07 17:57:49 +0000 | [diff] [blame] | 842 | hdr = pPage->hdrOffset; |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 843 | data = pPage->aData; |
drh | 271efa5 | 2004-05-30 19:19:05 +0000 | [diff] [blame] | 844 | decodeFlags(pPage, data[hdr]); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 845 | pPage->nOverflow = 0; |
drh | c8629a1 | 2004-05-08 20:07:40 +0000 | [diff] [blame] | 846 | pPage->idxShift = 0; |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 847 | usableSize = pBt->usableSize; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 848 | pPage->cellOffset = cellOffset = hdr + 12 - 4*pPage->leaf; |
| 849 | top = get2byte(&data[hdr+5]); |
| 850 | pPage->nCell = get2byte(&data[hdr+3]); |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 851 | if( pPage->nCell>MX_CELL(pBt) ){ |
drh | ee696e2 | 2004-08-30 16:52:17 +0000 | [diff] [blame] | 852 | /* To many cells for a single page. The page must be corrupt */ |
drh | 4928570 | 2005-09-17 15:20:26 +0000 | [diff] [blame] | 853 | return SQLITE_CORRUPT_BKPT; |
drh | ee696e2 | 2004-08-30 16:52:17 +0000 | [diff] [blame] | 854 | } |
| 855 | if( pPage->nCell==0 && pParent!=0 && pParent->pgno!=1 ){ |
| 856 | /* All pages must have at least one cell, except for root pages */ |
drh | 4928570 | 2005-09-17 15:20:26 +0000 | [diff] [blame] | 857 | return SQLITE_CORRUPT_BKPT; |
drh | ee696e2 | 2004-08-30 16:52:17 +0000 | [diff] [blame] | 858 | } |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 859 | |
| 860 | /* Compute the total free space on the page */ |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 861 | pc = get2byte(&data[hdr+1]); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 862 | nFree = data[hdr+7] + top - (cellOffset + 2*pPage->nCell); |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 863 | while( pc>0 ){ |
| 864 | int next, size; |
drh | ee696e2 | 2004-08-30 16:52:17 +0000 | [diff] [blame] | 865 | if( pc>usableSize-4 ){ |
| 866 | /* Free block is off the page */ |
drh | 4928570 | 2005-09-17 15:20:26 +0000 | [diff] [blame] | 867 | return SQLITE_CORRUPT_BKPT; |
drh | ee696e2 | 2004-08-30 16:52:17 +0000 | [diff] [blame] | 868 | } |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 869 | next = get2byte(&data[pc]); |
| 870 | size = get2byte(&data[pc+2]); |
drh | ee696e2 | 2004-08-30 16:52:17 +0000 | [diff] [blame] | 871 | if( next>0 && next<=pc+size+3 ){ |
| 872 | /* Free blocks must be in accending order */ |
drh | 4928570 | 2005-09-17 15:20:26 +0000 | [diff] [blame] | 873 | return SQLITE_CORRUPT_BKPT; |
drh | ee696e2 | 2004-08-30 16:52:17 +0000 | [diff] [blame] | 874 | } |
drh | 3add367 | 2004-05-15 00:29:24 +0000 | [diff] [blame] | 875 | nFree += size; |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 876 | pc = next; |
| 877 | } |
drh | 3add367 | 2004-05-15 00:29:24 +0000 | [diff] [blame] | 878 | pPage->nFree = nFree; |
drh | ee696e2 | 2004-08-30 16:52:17 +0000 | [diff] [blame] | 879 | if( nFree>=usableSize ){ |
| 880 | /* Free space cannot exceed total page size */ |
drh | 4928570 | 2005-09-17 15:20:26 +0000 | [diff] [blame] | 881 | return SQLITE_CORRUPT_BKPT; |
drh | ee696e2 | 2004-08-30 16:52:17 +0000 | [diff] [blame] | 882 | } |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 883 | |
drh | de64713 | 2004-05-07 17:57:49 +0000 | [diff] [blame] | 884 | pPage->isInit = 1; |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 885 | return SQLITE_OK; |
drh | 7e3b0a0 | 2001-04-28 16:52:40 +0000 | [diff] [blame] | 886 | } |
| 887 | |
| 888 | /* |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 889 | ** Set up a raw page so that it looks like a database page holding |
| 890 | ** no entries. |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 891 | */ |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 892 | static void zeroPage(MemPage *pPage, int flags){ |
| 893 | unsigned char *data = pPage->aData; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 894 | BtShared *pBt = pPage->pBt; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 895 | int hdr = pPage->hdrOffset; |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 896 | int first; |
| 897 | |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 898 | assert( sqlite3PagerPagenumber(pPage->pDbPage)==pPage->pgno ); |
drh | 07d183d | 2005-05-01 22:52:42 +0000 | [diff] [blame] | 899 | assert( &data[pBt->pageSize] == (unsigned char*)pPage ); |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 900 | assert( sqlite3PagerIswriteable(pPage->pDbPage) ); |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 901 | memset(&data[hdr], 0, pBt->usableSize - hdr); |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 902 | data[hdr] = flags; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 903 | first = hdr + 8 + 4*((flags&PTF_LEAF)==0); |
| 904 | memset(&data[hdr+1], 0, 4); |
| 905 | data[hdr+7] = 0; |
| 906 | put2byte(&data[hdr+5], pBt->usableSize); |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 907 | pPage->nFree = pBt->usableSize - first; |
drh | 271efa5 | 2004-05-30 19:19:05 +0000 | [diff] [blame] | 908 | decodeFlags(pPage, flags); |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 909 | pPage->hdrOffset = hdr; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 910 | pPage->cellOffset = first; |
| 911 | pPage->nOverflow = 0; |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 912 | pPage->idxShift = 0; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 913 | pPage->nCell = 0; |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 914 | pPage->isInit = 1; |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 915 | } |
| 916 | |
| 917 | /* |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 918 | ** Get a page from the pager. Initialize the MemPage.pBt and |
| 919 | ** MemPage.aData elements if needed. |
drh | 538f570 | 2007-04-13 02:14:30 +0000 | [diff] [blame] | 920 | ** |
| 921 | ** If the noContent flag is set, it means that we do not care about |
| 922 | ** the content of the page at this time. So do not go to the disk |
| 923 | ** to fetch the content. Just fill in the content with zeros for now. |
| 924 | ** If in the future we call sqlite3PagerWrite() on this page, that |
| 925 | ** means we have started to be concerned about content and the disk |
| 926 | ** read should occur at that point. |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 927 | */ |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 928 | int sqlite3BtreeGetPage( |
| 929 | BtShared *pBt, /* The btree */ |
| 930 | Pgno pgno, /* Number of the page to fetch */ |
| 931 | MemPage **ppPage, /* Return the page in this parameter */ |
| 932 | int noContent /* Do not load page content if true */ |
| 933 | ){ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 934 | int rc; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 935 | MemPage *pPage; |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 936 | DbPage *pDbPage; |
| 937 | |
drh | 538f570 | 2007-04-13 02:14:30 +0000 | [diff] [blame] | 938 | rc = sqlite3PagerAcquire(pBt->pPager, pgno, (DbPage**)&pDbPage, noContent); |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 939 | if( rc ) return rc; |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 940 | pPage = (MemPage *)sqlite3PagerGetExtra(pDbPage); |
| 941 | pPage->aData = sqlite3PagerGetData(pDbPage); |
| 942 | pPage->pDbPage = pDbPage; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 943 | pPage->pBt = pBt; |
| 944 | pPage->pgno = pgno; |
drh | de64713 | 2004-05-07 17:57:49 +0000 | [diff] [blame] | 945 | pPage->hdrOffset = pPage->pgno==1 ? 100 : 0; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 946 | *ppPage = pPage; |
| 947 | return SQLITE_OK; |
| 948 | } |
| 949 | |
| 950 | /* |
drh | de64713 | 2004-05-07 17:57:49 +0000 | [diff] [blame] | 951 | ** Get a page from the pager and initialize it. This routine |
| 952 | ** is just a convenience wrapper around separate calls to |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 953 | ** sqlite3BtreeGetPage() and sqlite3BtreeInitPage(). |
drh | de64713 | 2004-05-07 17:57:49 +0000 | [diff] [blame] | 954 | */ |
| 955 | static int getAndInitPage( |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 956 | BtShared *pBt, /* The database file */ |
drh | de64713 | 2004-05-07 17:57:49 +0000 | [diff] [blame] | 957 | Pgno pgno, /* Number of the page to get */ |
| 958 | MemPage **ppPage, /* Write the page pointer here */ |
| 959 | MemPage *pParent /* Parent of the page */ |
| 960 | ){ |
| 961 | int rc; |
drh | ee696e2 | 2004-08-30 16:52:17 +0000 | [diff] [blame] | 962 | if( pgno==0 ){ |
drh | 4928570 | 2005-09-17 15:20:26 +0000 | [diff] [blame] | 963 | return SQLITE_CORRUPT_BKPT; |
drh | ee696e2 | 2004-08-30 16:52:17 +0000 | [diff] [blame] | 964 | } |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 965 | rc = sqlite3BtreeGetPage(pBt, pgno, ppPage, 0); |
drh | 10617cd | 2004-05-14 15:27:27 +0000 | [diff] [blame] | 966 | if( rc==SQLITE_OK && (*ppPage)->isInit==0 ){ |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 967 | rc = sqlite3BtreeInitPage(*ppPage, pParent); |
drh | de64713 | 2004-05-07 17:57:49 +0000 | [diff] [blame] | 968 | } |
| 969 | return rc; |
| 970 | } |
| 971 | |
| 972 | /* |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 973 | ** Release a MemPage. This should be called once for each prior |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 974 | ** call to sqlite3BtreeGetPage. |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 975 | */ |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 976 | static void releasePage(MemPage *pPage){ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 977 | if( pPage ){ |
| 978 | assert( pPage->aData ); |
| 979 | assert( pPage->pBt ); |
drh | 07d183d | 2005-05-01 22:52:42 +0000 | [diff] [blame] | 980 | assert( &pPage->aData[pPage->pBt->pageSize]==(unsigned char*)pPage ); |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 981 | sqlite3PagerUnref(pPage->pDbPage); |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 982 | } |
| 983 | } |
| 984 | |
| 985 | /* |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 986 | ** This routine is called when the reference count for a page |
| 987 | ** reaches zero. We need to unref the pParent pointer when that |
| 988 | ** happens. |
| 989 | */ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 990 | static void pageDestructor(DbPage *pData, int pageSize){ |
drh | 07d183d | 2005-05-01 22:52:42 +0000 | [diff] [blame] | 991 | MemPage *pPage; |
| 992 | assert( (pageSize & 7)==0 ); |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 993 | pPage = (MemPage *)sqlite3PagerGetExtra(pData); |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 994 | if( pPage->pParent ){ |
| 995 | MemPage *pParent = pPage->pParent; |
| 996 | pPage->pParent = 0; |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 997 | releasePage(pParent); |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 998 | } |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 999 | pPage->isInit = 0; |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 1000 | } |
| 1001 | |
| 1002 | /* |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 1003 | ** During a rollback, when the pager reloads information into the cache |
| 1004 | ** so that the cache is restored to its original state at the start of |
| 1005 | ** the transaction, for each page restored this routine is called. |
| 1006 | ** |
| 1007 | ** This routine needs to reset the extra data section at the end of the |
| 1008 | ** page to agree with the restored data. |
| 1009 | */ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 1010 | static void pageReinit(DbPage *pData, int pageSize){ |
drh | 07d183d | 2005-05-01 22:52:42 +0000 | [diff] [blame] | 1011 | MemPage *pPage; |
| 1012 | assert( (pageSize & 7)==0 ); |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 1013 | pPage = (MemPage *)sqlite3PagerGetExtra(pData); |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 1014 | if( pPage->isInit ){ |
| 1015 | pPage->isInit = 0; |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 1016 | sqlite3BtreeInitPage(pPage, pPage->pParent); |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 1017 | } |
| 1018 | } |
| 1019 | |
| 1020 | /* |
drh | ad3e010 | 2004-09-03 23:32:18 +0000 | [diff] [blame] | 1021 | ** Open a database file. |
| 1022 | ** |
drh | 382c024 | 2001-10-06 16:33:02 +0000 | [diff] [blame] | 1023 | ** zFilename is the name of the database file. If zFilename is NULL |
drh | 1bee3d7 | 2001-10-15 00:44:35 +0000 | [diff] [blame] | 1024 | ** a new database with a random name is created. This randomly named |
drh | 23e11ca | 2004-05-04 17:27:28 +0000 | [diff] [blame] | 1025 | ** database file will be deleted when sqlite3BtreeClose() is called. |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 1026 | */ |
drh | 23e11ca | 2004-05-04 17:27:28 +0000 | [diff] [blame] | 1027 | int sqlite3BtreeOpen( |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1028 | const char *zFilename, /* Name of the file containing the BTree database */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1029 | sqlite3 *pSqlite, /* Associated database handle */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1030 | Btree **ppBtree, /* Pointer to new Btree object written here */ |
drh | 90f5ecb | 2004-07-22 01:19:35 +0000 | [diff] [blame] | 1031 | int flags /* Options */ |
drh | 6019e16 | 2001-07-02 17:51:45 +0000 | [diff] [blame] | 1032 | ){ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1033 | BtShared *pBt; /* Shared part of btree structure */ |
| 1034 | Btree *p; /* Handle to return */ |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 1035 | int rc = SQLITE_OK; |
drh | 90f5ecb | 2004-07-22 01:19:35 +0000 | [diff] [blame] | 1036 | int nReserve; |
| 1037 | unsigned char zDbHeader[100]; |
drh | 6f7adc8 | 2006-01-11 21:41:20 +0000 | [diff] [blame] | 1038 | #if !defined(SQLITE_OMIT_SHARED_CACHE) && !defined(SQLITE_OMIT_DISKIO) |
| 1039 | const ThreadData *pTsdro; |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 1040 | #endif |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1041 | |
| 1042 | /* Set the variable isMemdb to true for an in-memory database, or |
| 1043 | ** false for a file-based database. This symbol is only required if |
| 1044 | ** either of the shared-data or autovacuum features are compiled |
| 1045 | ** into the library. |
| 1046 | */ |
| 1047 | #if !defined(SQLITE_OMIT_SHARED_CACHE) || !defined(SQLITE_OMIT_AUTOVACUUM) |
| 1048 | #ifdef SQLITE_OMIT_MEMORYDB |
drh | 980b1a7 | 2006-08-16 16:42:48 +0000 | [diff] [blame] | 1049 | const int isMemdb = 0; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1050 | #else |
drh | 980b1a7 | 2006-08-16 16:42:48 +0000 | [diff] [blame] | 1051 | const int isMemdb = zFilename && !strcmp(zFilename, ":memory:"); |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1052 | #endif |
| 1053 | #endif |
| 1054 | |
| 1055 | p = sqliteMalloc(sizeof(Btree)); |
| 1056 | if( !p ){ |
| 1057 | return SQLITE_NOMEM; |
| 1058 | } |
| 1059 | p->inTrans = TRANS_NONE; |
| 1060 | p->pSqlite = pSqlite; |
| 1061 | |
| 1062 | /* Try to find an existing Btree structure opened on zFilename. */ |
drh | 198bf39 | 2006-01-06 21:52:49 +0000 | [diff] [blame] | 1063 | #if !defined(SQLITE_OMIT_SHARED_CACHE) && !defined(SQLITE_OMIT_DISKIO) |
drh | 6f7adc8 | 2006-01-11 21:41:20 +0000 | [diff] [blame] | 1064 | pTsdro = sqlite3ThreadDataReadOnly(); |
| 1065 | if( pTsdro->useSharedData && zFilename && !isMemdb ){ |
drh | 66560ad | 2006-01-06 14:32:19 +0000 | [diff] [blame] | 1066 | char *zFullPathname = sqlite3OsFullPathname(zFilename); |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1067 | if( !zFullPathname ){ |
| 1068 | sqliteFree(p); |
| 1069 | return SQLITE_NOMEM; |
| 1070 | } |
drh | 6f7adc8 | 2006-01-11 21:41:20 +0000 | [diff] [blame] | 1071 | for(pBt=pTsdro->pBtree; pBt; pBt=pBt->pNext){ |
danielk1977 | b82e7ed | 2006-01-11 14:09:31 +0000 | [diff] [blame] | 1072 | assert( pBt->nRef>0 ); |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 1073 | if( 0==strcmp(zFullPathname, sqlite3PagerFilename(pBt->pPager)) ){ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1074 | p->pBt = pBt; |
| 1075 | *ppBtree = p; |
| 1076 | pBt->nRef++; |
| 1077 | sqliteFree(zFullPathname); |
| 1078 | return SQLITE_OK; |
| 1079 | } |
| 1080 | } |
| 1081 | sqliteFree(zFullPathname); |
| 1082 | } |
| 1083 | #endif |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 1084 | |
drh | d62d3d0 | 2003-01-24 12:14:20 +0000 | [diff] [blame] | 1085 | /* |
| 1086 | ** The following asserts make sure that structures used by the btree are |
| 1087 | ** the right size. This is to guard against size changes that result |
| 1088 | ** when compiling on a different architecture. |
| 1089 | */ |
drh | 9b8f447 | 2006-04-04 01:54:55 +0000 | [diff] [blame] | 1090 | assert( sizeof(i64)==8 || sizeof(i64)==4 ); |
| 1091 | assert( sizeof(u64)==8 || sizeof(u64)==4 ); |
drh | d62d3d0 | 2003-01-24 12:14:20 +0000 | [diff] [blame] | 1092 | assert( sizeof(u32)==4 ); |
| 1093 | assert( sizeof(u16)==2 ); |
| 1094 | assert( sizeof(Pgno)==4 ); |
drh | d62d3d0 | 2003-01-24 12:14:20 +0000 | [diff] [blame] | 1095 | |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 1096 | pBt = sqliteMalloc( sizeof(*pBt) ); |
| 1097 | if( pBt==0 ){ |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 1098 | rc = SQLITE_NOMEM; |
| 1099 | goto btree_open_out; |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 1100 | } |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 1101 | rc = sqlite3PagerOpen(&pBt->pPager, zFilename, EXTRA_SIZE, flags); |
drh | 551b773 | 2006-11-06 21:20:25 +0000 | [diff] [blame] | 1102 | if( rc==SQLITE_OK ){ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 1103 | rc = sqlite3PagerReadFileheader(pBt->pPager,sizeof(zDbHeader),zDbHeader); |
drh | 551b773 | 2006-11-06 21:20:25 +0000 | [diff] [blame] | 1104 | } |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 1105 | if( rc!=SQLITE_OK ){ |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 1106 | goto btree_open_out; |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 1107 | } |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1108 | p->pBt = pBt; |
| 1109 | |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 1110 | sqlite3PagerSetDestructor(pBt->pPager, pageDestructor); |
| 1111 | sqlite3PagerSetReiniter(pBt->pPager, pageReinit); |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 1112 | pBt->pCursor = 0; |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 1113 | pBt->pPage1 = 0; |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 1114 | pBt->readOnly = sqlite3PagerIsreadonly(pBt->pPager); |
drh | 90f5ecb | 2004-07-22 01:19:35 +0000 | [diff] [blame] | 1115 | pBt->pageSize = get2byte(&zDbHeader[16]); |
drh | 07d183d | 2005-05-01 22:52:42 +0000 | [diff] [blame] | 1116 | if( pBt->pageSize<512 || pBt->pageSize>SQLITE_MAX_PAGE_SIZE |
| 1117 | || ((pBt->pageSize-1)&pBt->pageSize)!=0 ){ |
drh | 90f5ecb | 2004-07-22 01:19:35 +0000 | [diff] [blame] | 1118 | pBt->pageSize = SQLITE_DEFAULT_PAGE_SIZE; |
| 1119 | pBt->maxEmbedFrac = 64; /* 25% */ |
| 1120 | pBt->minEmbedFrac = 32; /* 12.5% */ |
| 1121 | pBt->minLeafFrac = 32; /* 12.5% */ |
drh | eee46cf | 2004-11-06 00:02:48 +0000 | [diff] [blame] | 1122 | #ifndef SQLITE_OMIT_AUTOVACUUM |
danielk1977 | 03aded4 | 2004-11-22 05:26:27 +0000 | [diff] [blame] | 1123 | /* If the magic name ":memory:" will create an in-memory database, then |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 1124 | ** leave the autoVacuum mode at 0 (do not auto-vacuum), even if |
| 1125 | ** SQLITE_DEFAULT_AUTOVACUUM is true. On the other hand, if |
| 1126 | ** SQLITE_OMIT_MEMORYDB has been defined, then ":memory:" is just a |
| 1127 | ** regular file-name. In this case the auto-vacuum applies as per normal. |
danielk1977 | 03aded4 | 2004-11-22 05:26:27 +0000 | [diff] [blame] | 1128 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1129 | if( zFilename && !isMemdb ){ |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 1130 | pBt->autoVacuum = (SQLITE_DEFAULT_AUTOVACUUM ? 1 : 0); |
| 1131 | pBt->incrVacuum = (SQLITE_DEFAULT_AUTOVACUUM==2 ? 1 : 0); |
danielk1977 | 951af80 | 2004-11-05 15:45:09 +0000 | [diff] [blame] | 1132 | } |
drh | eee46cf | 2004-11-06 00:02:48 +0000 | [diff] [blame] | 1133 | #endif |
drh | 90f5ecb | 2004-07-22 01:19:35 +0000 | [diff] [blame] | 1134 | nReserve = 0; |
| 1135 | }else{ |
| 1136 | nReserve = zDbHeader[20]; |
| 1137 | pBt->maxEmbedFrac = zDbHeader[21]; |
| 1138 | pBt->minEmbedFrac = zDbHeader[22]; |
| 1139 | pBt->minLeafFrac = zDbHeader[23]; |
| 1140 | pBt->pageSizeFixed = 1; |
danielk1977 | 951af80 | 2004-11-05 15:45:09 +0000 | [diff] [blame] | 1141 | #ifndef SQLITE_OMIT_AUTOVACUUM |
| 1142 | pBt->autoVacuum = (get4byte(&zDbHeader[36 + 4*4])?1:0); |
danielk1977 | 418899a | 2007-06-24 10:14:00 +0000 | [diff] [blame] | 1143 | pBt->incrVacuum = (get4byte(&zDbHeader[36 + 7*4])?1:0); |
danielk1977 | 951af80 | 2004-11-05 15:45:09 +0000 | [diff] [blame] | 1144 | #endif |
drh | 90f5ecb | 2004-07-22 01:19:35 +0000 | [diff] [blame] | 1145 | } |
| 1146 | pBt->usableSize = pBt->pageSize - nReserve; |
drh | 07d183d | 2005-05-01 22:52:42 +0000 | [diff] [blame] | 1147 | assert( (pBt->pageSize & 7)==0 ); /* 8-byte alignment of pageSize */ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 1148 | sqlite3PagerSetPagesize(pBt->pPager, pBt->pageSize); |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1149 | |
drh | cfed7bc | 2006-03-13 14:28:05 +0000 | [diff] [blame] | 1150 | #if !defined(SQLITE_OMIT_SHARED_CACHE) && !defined(SQLITE_OMIT_DISKIO) |
danielk1977 | 54f0198 | 2006-01-18 15:25:17 +0000 | [diff] [blame] | 1151 | /* Add the new btree to the linked list starting at ThreadData.pBtree. |
| 1152 | ** There is no chance that a malloc() may fail inside of the |
| 1153 | ** sqlite3ThreadData() call, as the ThreadData structure must have already |
| 1154 | ** been allocated for pTsdro->useSharedData to be non-zero. |
| 1155 | */ |
drh | 6f7adc8 | 2006-01-11 21:41:20 +0000 | [diff] [blame] | 1156 | if( pTsdro->useSharedData && zFilename && !isMemdb ){ |
| 1157 | pBt->pNext = pTsdro->pBtree; |
| 1158 | sqlite3ThreadData()->pBtree = pBt; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1159 | } |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1160 | #endif |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 1161 | pBt->nRef = 1; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1162 | *ppBtree = p; |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 1163 | |
| 1164 | btree_open_out: |
| 1165 | if( rc!=SQLITE_OK ){ |
| 1166 | if( pBt && pBt->pPager ){ |
| 1167 | sqlite3PagerClose(pBt->pPager); |
| 1168 | } |
| 1169 | sqliteFree(pBt); |
| 1170 | sqliteFree(p); |
| 1171 | *ppBtree = 0; |
| 1172 | } |
| 1173 | return rc; |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 1174 | } |
| 1175 | |
| 1176 | /* |
| 1177 | ** Close an open database and invalidate all cursors. |
| 1178 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1179 | int sqlite3BtreeClose(Btree *p){ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1180 | BtShared *pBt = p->pBt; |
| 1181 | BtCursor *pCur; |
| 1182 | |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 1183 | #ifndef SQLITE_OMIT_SHARED_CACHE |
drh | 6f7adc8 | 2006-01-11 21:41:20 +0000 | [diff] [blame] | 1184 | ThreadData *pTsd; |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 1185 | #endif |
| 1186 | |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1187 | /* Close all cursors opened via this handle. */ |
| 1188 | pCur = pBt->pCursor; |
| 1189 | while( pCur ){ |
| 1190 | BtCursor *pTmp = pCur; |
| 1191 | pCur = pCur->pNext; |
| 1192 | if( pTmp->pBtree==p ){ |
| 1193 | sqlite3BtreeCloseCursor(pTmp); |
| 1194 | } |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 1195 | } |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1196 | |
danielk1977 | 8d34dfd | 2006-01-24 16:37:57 +0000 | [diff] [blame] | 1197 | /* Rollback any active transaction and free the handle structure. |
| 1198 | ** The call to sqlite3BtreeRollback() drops any table-locks held by |
| 1199 | ** this handle. |
| 1200 | */ |
danielk1977 | b597f74 | 2006-01-15 11:39:18 +0000 | [diff] [blame] | 1201 | sqlite3BtreeRollback(p); |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1202 | sqliteFree(p); |
| 1203 | |
| 1204 | #ifndef SQLITE_OMIT_SHARED_CACHE |
| 1205 | /* If there are still other outstanding references to the shared-btree |
| 1206 | ** structure, return now. The remainder of this procedure cleans |
| 1207 | ** up the shared-btree. |
| 1208 | */ |
| 1209 | assert( pBt->nRef>0 ); |
| 1210 | pBt->nRef--; |
| 1211 | if( pBt->nRef ){ |
| 1212 | return SQLITE_OK; |
| 1213 | } |
| 1214 | |
danielk1977 | 54f0198 | 2006-01-18 15:25:17 +0000 | [diff] [blame] | 1215 | /* Remove the shared-btree from the thread wide list. Call |
| 1216 | ** ThreadDataReadOnly() and then cast away the const property of the |
| 1217 | ** pointer to avoid allocating thread data if it is not really required. |
| 1218 | */ |
| 1219 | pTsd = (ThreadData *)sqlite3ThreadDataReadOnly(); |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1220 | if( pTsd->pBtree==pBt ){ |
danielk1977 | 54f0198 | 2006-01-18 15:25:17 +0000 | [diff] [blame] | 1221 | assert( pTsd==sqlite3ThreadData() ); |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1222 | pTsd->pBtree = pBt->pNext; |
| 1223 | }else{ |
| 1224 | BtShared *pPrev; |
drh | 7416170 | 2006-02-24 02:53:49 +0000 | [diff] [blame] | 1225 | for(pPrev=pTsd->pBtree; pPrev && pPrev->pNext!=pBt; pPrev=pPrev->pNext){} |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1226 | if( pPrev ){ |
danielk1977 | 54f0198 | 2006-01-18 15:25:17 +0000 | [diff] [blame] | 1227 | assert( pTsd==sqlite3ThreadData() ); |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1228 | pPrev->pNext = pBt->pNext; |
| 1229 | } |
| 1230 | } |
| 1231 | #endif |
| 1232 | |
| 1233 | /* Close the pager and free the shared-btree structure */ |
| 1234 | assert( !pBt->pCursor ); |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 1235 | sqlite3PagerClose(pBt->pPager); |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 1236 | if( pBt->xFreeSchema && pBt->pSchema ){ |
| 1237 | pBt->xFreeSchema(pBt->pSchema); |
| 1238 | } |
danielk1977 | de0fe3e | 2006-01-06 06:33:12 +0000 | [diff] [blame] | 1239 | sqliteFree(pBt->pSchema); |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 1240 | sqliteFree(pBt); |
| 1241 | return SQLITE_OK; |
| 1242 | } |
| 1243 | |
| 1244 | /* |
drh | 90f5ecb | 2004-07-22 01:19:35 +0000 | [diff] [blame] | 1245 | ** Change the busy handler callback function. |
| 1246 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1247 | int sqlite3BtreeSetBusyHandler(Btree *p, BusyHandler *pHandler){ |
| 1248 | BtShared *pBt = p->pBt; |
drh | b8ef32c | 2005-03-14 02:01:49 +0000 | [diff] [blame] | 1249 | pBt->pBusyHandler = pHandler; |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 1250 | sqlite3PagerSetBusyhandler(pBt->pPager, pHandler); |
drh | 90f5ecb | 2004-07-22 01:19:35 +0000 | [diff] [blame] | 1251 | return SQLITE_OK; |
| 1252 | } |
| 1253 | |
| 1254 | /* |
drh | da47d77 | 2002-12-02 04:25:19 +0000 | [diff] [blame] | 1255 | ** Change the limit on the number of pages allowed in the cache. |
drh | cd61c28 | 2002-03-06 22:01:34 +0000 | [diff] [blame] | 1256 | ** |
| 1257 | ** The maximum number of cache pages is set to the absolute |
| 1258 | ** value of mxPage. If mxPage is negative, the pager will |
| 1259 | ** operate asynchronously - it will not stop to do fsync()s |
| 1260 | ** to insure data is written to the disk surface before |
| 1261 | ** continuing. Transactions still work if synchronous is off, |
| 1262 | ** and the database cannot be corrupted if this program |
| 1263 | ** crashes. But if the operating system crashes or there is |
| 1264 | ** an abrupt power failure when synchronous is off, the database |
| 1265 | ** could be left in an inconsistent and unrecoverable state. |
| 1266 | ** Synchronous is on by default so database corruption is not |
| 1267 | ** normally a worry. |
drh | f57b14a | 2001-09-14 18:54:08 +0000 | [diff] [blame] | 1268 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1269 | int sqlite3BtreeSetCacheSize(Btree *p, int mxPage){ |
| 1270 | BtShared *pBt = p->pBt; |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 1271 | sqlite3PagerSetCachesize(pBt->pPager, mxPage); |
drh | f57b14a | 2001-09-14 18:54:08 +0000 | [diff] [blame] | 1272 | return SQLITE_OK; |
| 1273 | } |
| 1274 | |
| 1275 | /* |
drh | 973b6e3 | 2003-02-12 14:09:42 +0000 | [diff] [blame] | 1276 | ** Change the way data is synced to disk in order to increase or decrease |
| 1277 | ** how well the database resists damage due to OS crashes and power |
| 1278 | ** failures. Level 1 is the same as asynchronous (no syncs() occur and |
| 1279 | ** there is a high probability of damage) Level 2 is the default. There |
| 1280 | ** is a very low but non-zero probability of damage. Level 3 reduces the |
| 1281 | ** probability of damage to near zero but with a write performance reduction. |
| 1282 | */ |
danielk1977 | 93758c8 | 2005-01-21 08:13:14 +0000 | [diff] [blame] | 1283 | #ifndef SQLITE_OMIT_PAGER_PRAGMAS |
drh | ac530b1 | 2006-02-11 01:25:50 +0000 | [diff] [blame] | 1284 | int sqlite3BtreeSetSafetyLevel(Btree *p, int level, int fullSync){ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1285 | BtShared *pBt = p->pBt; |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 1286 | sqlite3PagerSetSafetyLevel(pBt->pPager, level, fullSync); |
drh | 973b6e3 | 2003-02-12 14:09:42 +0000 | [diff] [blame] | 1287 | return SQLITE_OK; |
| 1288 | } |
danielk1977 | 93758c8 | 2005-01-21 08:13:14 +0000 | [diff] [blame] | 1289 | #endif |
drh | 973b6e3 | 2003-02-12 14:09:42 +0000 | [diff] [blame] | 1290 | |
drh | 2c8997b | 2005-08-27 16:36:48 +0000 | [diff] [blame] | 1291 | /* |
| 1292 | ** Return TRUE if the given btree is set to safety level 1. In other |
| 1293 | ** words, return TRUE if no sync() occurs on the disk files. |
| 1294 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1295 | int sqlite3BtreeSyncDisabled(Btree *p){ |
| 1296 | BtShared *pBt = p->pBt; |
drh | 2c8997b | 2005-08-27 16:36:48 +0000 | [diff] [blame] | 1297 | assert( pBt && pBt->pPager ); |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 1298 | return sqlite3PagerNosync(pBt->pPager); |
drh | 2c8997b | 2005-08-27 16:36:48 +0000 | [diff] [blame] | 1299 | } |
| 1300 | |
danielk1977 | 576ec6b | 2005-01-21 11:55:25 +0000 | [diff] [blame] | 1301 | #if !defined(SQLITE_OMIT_PAGER_PRAGMAS) || !defined(SQLITE_OMIT_VACUUM) |
drh | 973b6e3 | 2003-02-12 14:09:42 +0000 | [diff] [blame] | 1302 | /* |
drh | 90f5ecb | 2004-07-22 01:19:35 +0000 | [diff] [blame] | 1303 | ** Change the default pages size and the number of reserved bytes per page. |
drh | 06f5021 | 2004-11-02 14:24:33 +0000 | [diff] [blame] | 1304 | ** |
| 1305 | ** The page size must be a power of 2 between 512 and 65536. If the page |
| 1306 | ** size supplied does not meet this constraint then the page size is not |
| 1307 | ** changed. |
| 1308 | ** |
| 1309 | ** Page sizes are constrained to be a power of two so that the region |
| 1310 | ** of the database file used for locking (beginning at PENDING_BYTE, |
| 1311 | ** the first byte past the 1GB boundary, 0x40000000) needs to occur |
| 1312 | ** at the beginning of a page. |
danielk1977 | 2812956 | 2005-01-11 10:25:06 +0000 | [diff] [blame] | 1313 | ** |
| 1314 | ** If parameter nReserve is less than zero, then the number of reserved |
| 1315 | ** bytes per page is left unchanged. |
drh | 90f5ecb | 2004-07-22 01:19:35 +0000 | [diff] [blame] | 1316 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1317 | int sqlite3BtreeSetPageSize(Btree *p, int pageSize, int nReserve){ |
| 1318 | BtShared *pBt = p->pBt; |
drh | 90f5ecb | 2004-07-22 01:19:35 +0000 | [diff] [blame] | 1319 | if( pBt->pageSizeFixed ){ |
| 1320 | return SQLITE_READONLY; |
| 1321 | } |
| 1322 | if( nReserve<0 ){ |
| 1323 | nReserve = pBt->pageSize - pBt->usableSize; |
| 1324 | } |
drh | 06f5021 | 2004-11-02 14:24:33 +0000 | [diff] [blame] | 1325 | if( pageSize>=512 && pageSize<=SQLITE_MAX_PAGE_SIZE && |
| 1326 | ((pageSize-1)&pageSize)==0 ){ |
drh | 07d183d | 2005-05-01 22:52:42 +0000 | [diff] [blame] | 1327 | assert( (pageSize & 7)==0 ); |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1328 | assert( !pBt->pPage1 && !pBt->pCursor ); |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 1329 | pBt->pageSize = sqlite3PagerSetPagesize(pBt->pPager, pageSize); |
drh | 90f5ecb | 2004-07-22 01:19:35 +0000 | [diff] [blame] | 1330 | } |
| 1331 | pBt->usableSize = pBt->pageSize - nReserve; |
| 1332 | return SQLITE_OK; |
| 1333 | } |
| 1334 | |
| 1335 | /* |
| 1336 | ** Return the currently defined page size |
| 1337 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1338 | int sqlite3BtreeGetPageSize(Btree *p){ |
| 1339 | return p->pBt->pageSize; |
drh | 90f5ecb | 2004-07-22 01:19:35 +0000 | [diff] [blame] | 1340 | } |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1341 | int sqlite3BtreeGetReserve(Btree *p){ |
| 1342 | return p->pBt->pageSize - p->pBt->usableSize; |
drh | 2011d5f | 2004-07-22 02:40:37 +0000 | [diff] [blame] | 1343 | } |
drh | f8e632b | 2007-05-08 14:51:36 +0000 | [diff] [blame] | 1344 | |
| 1345 | /* |
| 1346 | ** Set the maximum page count for a database if mxPage is positive. |
| 1347 | ** No changes are made if mxPage is 0 or negative. |
| 1348 | ** Regardless of the value of mxPage, return the maximum page count. |
| 1349 | */ |
| 1350 | int sqlite3BtreeMaxPageCount(Btree *p, int mxPage){ |
| 1351 | return sqlite3PagerMaxPageCount(p->pBt->pPager, mxPage); |
| 1352 | } |
danielk1977 | 576ec6b | 2005-01-21 11:55:25 +0000 | [diff] [blame] | 1353 | #endif /* !defined(SQLITE_OMIT_PAGER_PRAGMAS) || !defined(SQLITE_OMIT_VACUUM) */ |
drh | 90f5ecb | 2004-07-22 01:19:35 +0000 | [diff] [blame] | 1354 | |
| 1355 | /* |
danielk1977 | 951af80 | 2004-11-05 15:45:09 +0000 | [diff] [blame] | 1356 | ** Change the 'auto-vacuum' property of the database. If the 'autoVacuum' |
| 1357 | ** parameter is non-zero, then auto-vacuum mode is enabled. If zero, it |
| 1358 | ** is disabled. The default value for the auto-vacuum property is |
| 1359 | ** determined by the SQLITE_DEFAULT_AUTOVACUUM macro. |
| 1360 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1361 | int sqlite3BtreeSetAutoVacuum(Btree *p, int autoVacuum){ |
danielk1977 | 951af80 | 2004-11-05 15:45:09 +0000 | [diff] [blame] | 1362 | #ifdef SQLITE_OMIT_AUTOVACUUM |
drh | eee46cf | 2004-11-06 00:02:48 +0000 | [diff] [blame] | 1363 | return SQLITE_READONLY; |
danielk1977 | 951af80 | 2004-11-05 15:45:09 +0000 | [diff] [blame] | 1364 | #else |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 1365 | BtShared *pBt = p->pBt; |
| 1366 | int av = (autoVacuum?1:0); |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 1367 | if( pBt->pageSizeFixed && av!=pBt->autoVacuum ){ |
danielk1977 | 951af80 | 2004-11-05 15:45:09 +0000 | [diff] [blame] | 1368 | return SQLITE_READONLY; |
| 1369 | } |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 1370 | pBt->autoVacuum = av; |
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); |
danielk1977 | 27b1f95 | 2007-06-25 08:16:58 +0000 | [diff] [blame] | 1439 | pBt->incrVacuum = (get4byte(&page1[36 + 7*4])?1:0); |
drh | 057cd3a | 2005-02-15 16:23:02 +0000 | [diff] [blame] | 1440 | #endif |
drh | 306dc21 | 2001-05-21 13:45:10 +0000 | [diff] [blame] | 1441 | } |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 1442 | |
| 1443 | /* maxLocal is the maximum amount of payload to store locally for |
| 1444 | ** a cell. Make sure it is small enough so that at least minFanout |
| 1445 | ** cells can will fit on one page. We assume a 10-byte page header. |
| 1446 | ** Besides the payload, the cell must store: |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 1447 | ** 2-byte pointer to the cell |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 1448 | ** 4-byte child pointer |
| 1449 | ** 9-byte nKey value |
| 1450 | ** 4-byte nData value |
| 1451 | ** 4-byte overflow page pointer |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 1452 | ** So a cell consists of a 2-byte poiner, a header which is as much as |
| 1453 | ** 17 bytes long, 0 to N bytes of payload, and an optional 4 byte overflow |
| 1454 | ** page pointer. |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 1455 | */ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 1456 | pBt->maxLocal = (pBt->usableSize-12)*pBt->maxEmbedFrac/255 - 23; |
| 1457 | pBt->minLocal = (pBt->usableSize-12)*pBt->minEmbedFrac/255 - 23; |
| 1458 | pBt->maxLeaf = pBt->usableSize - 35; |
| 1459 | pBt->minLeaf = (pBt->usableSize-12)*pBt->minLeafFrac/255 - 23; |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 1460 | if( pBt->minLocal>pBt->maxLocal || pBt->maxLocal<0 ){ |
| 1461 | goto page1_init_failed; |
| 1462 | } |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 1463 | assert( pBt->maxLeaf + 23 <= MX_CELL_SIZE(pBt) ); |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1464 | pBt->pPage1 = pPage1; |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 1465 | return SQLITE_OK; |
drh | 306dc21 | 2001-05-21 13:45:10 +0000 | [diff] [blame] | 1466 | |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 1467 | page1_init_failed: |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1468 | releasePage(pPage1); |
| 1469 | pBt->pPage1 = 0; |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 1470 | return rc; |
drh | 306dc21 | 2001-05-21 13:45:10 +0000 | [diff] [blame] | 1471 | } |
| 1472 | |
| 1473 | /* |
drh | b8ef32c | 2005-03-14 02:01:49 +0000 | [diff] [blame] | 1474 | ** This routine works like lockBtree() except that it also invokes the |
| 1475 | ** busy callback if there is lock contention. |
| 1476 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1477 | static int lockBtreeWithRetry(Btree *pRef){ |
drh | b8ef32c | 2005-03-14 02:01:49 +0000 | [diff] [blame] | 1478 | int rc = SQLITE_OK; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1479 | if( pRef->inTrans==TRANS_NONE ){ |
| 1480 | u8 inTransaction = pRef->pBt->inTransaction; |
| 1481 | btreeIntegrity(pRef); |
| 1482 | rc = sqlite3BtreeBeginTrans(pRef, 0); |
| 1483 | pRef->pBt->inTransaction = inTransaction; |
| 1484 | pRef->inTrans = TRANS_NONE; |
| 1485 | if( rc==SQLITE_OK ){ |
| 1486 | pRef->pBt->nTransaction--; |
| 1487 | } |
| 1488 | btreeIntegrity(pRef); |
drh | b8ef32c | 2005-03-14 02:01:49 +0000 | [diff] [blame] | 1489 | } |
| 1490 | return rc; |
| 1491 | } |
| 1492 | |
| 1493 | |
| 1494 | /* |
drh | b8ca307 | 2001-12-05 00:21:20 +0000 | [diff] [blame] | 1495 | ** If there are no outstanding cursors and we are not in the middle |
| 1496 | ** of a transaction but there is a read lock on the database, then |
| 1497 | ** this routine unrefs the first page of the database file which |
| 1498 | ** has the effect of releasing the read lock. |
| 1499 | ** |
| 1500 | ** If there are any outstanding cursors, this routine is a no-op. |
| 1501 | ** |
| 1502 | ** If there is a transaction in progress, this routine is a no-op. |
| 1503 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1504 | static void unlockBtreeIfUnused(BtShared *pBt){ |
| 1505 | if( pBt->inTransaction==TRANS_NONE && pBt->pCursor==0 && pBt->pPage1!=0 ){ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 1506 | if( sqlite3PagerRefcount(pBt->pPager)>=1 ){ |
drh | 24c9a2e | 2007-01-05 02:00:47 +0000 | [diff] [blame] | 1507 | if( pBt->pPage1->aData==0 ){ |
| 1508 | MemPage *pPage = pBt->pPage1; |
| 1509 | pPage->aData = &((u8*)pPage)[-pBt->pageSize]; |
| 1510 | pPage->pBt = pBt; |
| 1511 | pPage->pgno = 1; |
| 1512 | } |
| 1513 | releasePage(pBt->pPage1); |
drh | 51c6d96 | 2004-06-06 00:42:25 +0000 | [diff] [blame] | 1514 | } |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1515 | pBt->pPage1 = 0; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1516 | pBt->inStmt = 0; |
drh | b8ca307 | 2001-12-05 00:21:20 +0000 | [diff] [blame] | 1517 | } |
| 1518 | } |
| 1519 | |
| 1520 | /* |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 1521 | ** Create a new database by initializing the first page of the |
drh | 8c42ca9 | 2001-06-22 19:15:00 +0000 | [diff] [blame] | 1522 | ** file. |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 1523 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1524 | static int newDatabase(BtShared *pBt){ |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 1525 | MemPage *pP1; |
| 1526 | unsigned char *data; |
drh | 8c42ca9 | 2001-06-22 19:15:00 +0000 | [diff] [blame] | 1527 | int rc; |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 1528 | if( sqlite3PagerPagecount(pBt->pPager)>0 ) return SQLITE_OK; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1529 | pP1 = pBt->pPage1; |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 1530 | assert( pP1!=0 ); |
| 1531 | data = pP1->aData; |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 1532 | rc = sqlite3PagerWrite(pP1->pDbPage); |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 1533 | if( rc ) return rc; |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 1534 | memcpy(data, zMagicHeader, sizeof(zMagicHeader)); |
| 1535 | assert( sizeof(zMagicHeader)==16 ); |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 1536 | put2byte(&data[16], pBt->pageSize); |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 1537 | data[18] = 1; |
| 1538 | data[19] = 1; |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 1539 | data[20] = pBt->pageSize - pBt->usableSize; |
| 1540 | data[21] = pBt->maxEmbedFrac; |
| 1541 | data[22] = pBt->minEmbedFrac; |
| 1542 | data[23] = pBt->minLeafFrac; |
| 1543 | memset(&data[24], 0, 100-24); |
drh | e6c4381 | 2004-05-14 12:17:46 +0000 | [diff] [blame] | 1544 | zeroPage(pP1, PTF_INTKEY|PTF_LEAF|PTF_LEAFDATA ); |
drh | f2a611c | 2004-09-05 00:33:43 +0000 | [diff] [blame] | 1545 | pBt->pageSizeFixed = 1; |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 1546 | #ifndef SQLITE_OMIT_AUTOVACUUM |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 1547 | assert( pBt->autoVacuum==1 || pBt->autoVacuum==0 ); |
danielk1977 | 418899a | 2007-06-24 10:14:00 +0000 | [diff] [blame] | 1548 | assert( pBt->incrVacuum==1 || pBt->incrVacuum==0 ); |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 1549 | put4byte(&data[36 + 4*4], pBt->autoVacuum); |
danielk1977 | 418899a | 2007-06-24 10:14:00 +0000 | [diff] [blame] | 1550 | put4byte(&data[36 + 7*4], pBt->incrVacuum); |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 1551 | #endif |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 1552 | return SQLITE_OK; |
| 1553 | } |
| 1554 | |
| 1555 | /* |
danielk1977 | ee5741e | 2004-05-31 10:01:34 +0000 | [diff] [blame] | 1556 | ** Attempt to start a new transaction. A write-transaction |
drh | 684917c | 2004-10-05 02:41:42 +0000 | [diff] [blame] | 1557 | ** is started if the second argument is nonzero, otherwise a read- |
| 1558 | ** transaction. If the second argument is 2 or more and exclusive |
| 1559 | ** transaction is started, meaning that no other process is allowed |
| 1560 | ** to access the database. A preexisting transaction may not be |
drh | b8ef32c | 2005-03-14 02:01:49 +0000 | [diff] [blame] | 1561 | ** upgraded to exclusive by calling this routine a second time - the |
drh | 684917c | 2004-10-05 02:41:42 +0000 | [diff] [blame] | 1562 | ** exclusivity flag only works for a new transaction. |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 1563 | ** |
danielk1977 | ee5741e | 2004-05-31 10:01:34 +0000 | [diff] [blame] | 1564 | ** A write-transaction must be started before attempting any |
| 1565 | ** changes to the database. None of the following routines |
| 1566 | ** will work unless a transaction is started first: |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 1567 | ** |
drh | 23e11ca | 2004-05-04 17:27:28 +0000 | [diff] [blame] | 1568 | ** sqlite3BtreeCreateTable() |
| 1569 | ** sqlite3BtreeCreateIndex() |
| 1570 | ** sqlite3BtreeClearTable() |
| 1571 | ** sqlite3BtreeDropTable() |
| 1572 | ** sqlite3BtreeInsert() |
| 1573 | ** sqlite3BtreeDelete() |
| 1574 | ** sqlite3BtreeUpdateMeta() |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 1575 | ** |
drh | b8ef32c | 2005-03-14 02:01:49 +0000 | [diff] [blame] | 1576 | ** If an initial attempt to acquire the lock fails because of lock contention |
| 1577 | ** and the database was previously unlocked, then invoke the busy handler |
| 1578 | ** if there is one. But if there was previously a read-lock, do not |
| 1579 | ** invoke the busy handler - just return SQLITE_BUSY. SQLITE_BUSY is |
| 1580 | ** returned when there is already a read-lock in order to avoid a deadlock. |
| 1581 | ** |
| 1582 | ** Suppose there are two processes A and B. A has a read lock and B has |
| 1583 | ** a reserved lock. B tries to promote to exclusive but is blocked because |
| 1584 | ** of A's read lock. A tries to promote to reserved but is blocked by B. |
| 1585 | ** One or the other of the two processes must give way or there can be |
| 1586 | ** no progress. By returning SQLITE_BUSY and not invoking the busy callback |
| 1587 | ** when A already has a read lock, we encourage A to give up and let B |
| 1588 | ** proceed. |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 1589 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1590 | int sqlite3BtreeBeginTrans(Btree *p, int wrflag){ |
| 1591 | BtShared *pBt = p->pBt; |
danielk1977 | ee5741e | 2004-05-31 10:01:34 +0000 | [diff] [blame] | 1592 | int rc = SQLITE_OK; |
| 1593 | |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1594 | btreeIntegrity(p); |
| 1595 | |
danielk1977 | ee5741e | 2004-05-31 10:01:34 +0000 | [diff] [blame] | 1596 | /* If the btree is already in a write-transaction, or it |
| 1597 | ** is already in a read-transaction and a read-transaction |
| 1598 | ** is requested, this is a no-op. |
| 1599 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1600 | if( p->inTrans==TRANS_WRITE || (p->inTrans==TRANS_READ && !wrflag) ){ |
danielk1977 | ee5741e | 2004-05-31 10:01:34 +0000 | [diff] [blame] | 1601 | return SQLITE_OK; |
| 1602 | } |
drh | b8ef32c | 2005-03-14 02:01:49 +0000 | [diff] [blame] | 1603 | |
| 1604 | /* Write transactions are not possible on a read-only database */ |
danielk1977 | ee5741e | 2004-05-31 10:01:34 +0000 | [diff] [blame] | 1605 | if( pBt->readOnly && wrflag ){ |
| 1606 | return SQLITE_READONLY; |
| 1607 | } |
| 1608 | |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1609 | /* If another database handle has already opened a write transaction |
| 1610 | ** on this shared-btree structure and a second write transaction is |
| 1611 | ** requested, return SQLITE_BUSY. |
| 1612 | */ |
| 1613 | if( pBt->inTransaction==TRANS_WRITE && wrflag ){ |
| 1614 | return SQLITE_BUSY; |
| 1615 | } |
| 1616 | |
drh | b8ef32c | 2005-03-14 02:01:49 +0000 | [diff] [blame] | 1617 | do { |
| 1618 | if( pBt->pPage1==0 ){ |
| 1619 | rc = lockBtree(pBt); |
drh | 8c42ca9 | 2001-06-22 19:15:00 +0000 | [diff] [blame] | 1620 | } |
drh | 309169a | 2007-04-24 17:27:51 +0000 | [diff] [blame] | 1621 | |
drh | b8ef32c | 2005-03-14 02:01:49 +0000 | [diff] [blame] | 1622 | if( rc==SQLITE_OK && wrflag ){ |
drh | 309169a | 2007-04-24 17:27:51 +0000 | [diff] [blame] | 1623 | if( pBt->readOnly ){ |
| 1624 | rc = SQLITE_READONLY; |
| 1625 | }else{ |
| 1626 | rc = sqlite3PagerBegin(pBt->pPage1->pDbPage, wrflag>1); |
| 1627 | if( rc==SQLITE_OK ){ |
| 1628 | rc = newDatabase(pBt); |
| 1629 | } |
drh | b8ef32c | 2005-03-14 02:01:49 +0000 | [diff] [blame] | 1630 | } |
| 1631 | } |
| 1632 | |
| 1633 | if( rc==SQLITE_OK ){ |
drh | b8ef32c | 2005-03-14 02:01:49 +0000 | [diff] [blame] | 1634 | if( wrflag ) pBt->inStmt = 0; |
| 1635 | }else{ |
| 1636 | unlockBtreeIfUnused(pBt); |
| 1637 | } |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1638 | }while( rc==SQLITE_BUSY && pBt->inTransaction==TRANS_NONE && |
drh | a4afb65 | 2005-07-09 02:16:02 +0000 | [diff] [blame] | 1639 | sqlite3InvokeBusyHandler(pBt->pBusyHandler) ); |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1640 | |
| 1641 | if( rc==SQLITE_OK ){ |
| 1642 | if( p->inTrans==TRANS_NONE ){ |
| 1643 | pBt->nTransaction++; |
| 1644 | } |
| 1645 | p->inTrans = (wrflag?TRANS_WRITE:TRANS_READ); |
| 1646 | if( p->inTrans>pBt->inTransaction ){ |
| 1647 | pBt->inTransaction = p->inTrans; |
| 1648 | } |
| 1649 | } |
| 1650 | |
| 1651 | btreeIntegrity(p); |
drh | b8ca307 | 2001-12-05 00:21:20 +0000 | [diff] [blame] | 1652 | return rc; |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 1653 | } |
| 1654 | |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 1655 | #ifndef SQLITE_OMIT_AUTOVACUUM |
| 1656 | |
| 1657 | /* |
| 1658 | ** Set the pointer-map entries for all children of page pPage. Also, if |
| 1659 | ** pPage contains cells that point to overflow pages, set the pointer |
| 1660 | ** map entries for the overflow pages as well. |
| 1661 | */ |
| 1662 | static int setChildPtrmaps(MemPage *pPage){ |
| 1663 | int i; /* Counter variable */ |
| 1664 | int nCell; /* Number of cells in page pPage */ |
danielk1977 | 2df71c7 | 2007-05-24 07:22:42 +0000 | [diff] [blame] | 1665 | int rc; /* Return code */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1666 | BtShared *pBt = pPage->pBt; |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 1667 | int isInitOrig = pPage->isInit; |
| 1668 | Pgno pgno = pPage->pgno; |
| 1669 | |
danielk1977 | 2df71c7 | 2007-05-24 07:22:42 +0000 | [diff] [blame] | 1670 | rc = sqlite3BtreeInitPage(pPage, pPage->pParent); |
| 1671 | if( rc!=SQLITE_OK ){ |
| 1672 | goto set_child_ptrmaps_out; |
| 1673 | } |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 1674 | nCell = pPage->nCell; |
| 1675 | |
| 1676 | for(i=0; i<nCell; i++){ |
danielk1977 | 1cc5ed8 | 2007-05-16 17:28:43 +0000 | [diff] [blame] | 1677 | u8 *pCell = findCell(pPage, i); |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 1678 | |
danielk1977 | 2683665 | 2005-01-17 01:33:13 +0000 | [diff] [blame] | 1679 | rc = ptrmapPutOvflPtr(pPage, pCell); |
| 1680 | if( rc!=SQLITE_OK ){ |
| 1681 | goto set_child_ptrmaps_out; |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 1682 | } |
danielk1977 | 2683665 | 2005-01-17 01:33:13 +0000 | [diff] [blame] | 1683 | |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 1684 | if( !pPage->leaf ){ |
| 1685 | Pgno childPgno = get4byte(pCell); |
| 1686 | rc = ptrmapPut(pBt, childPgno, PTRMAP_BTREE, pgno); |
| 1687 | if( rc!=SQLITE_OK ) goto set_child_ptrmaps_out; |
| 1688 | } |
| 1689 | } |
| 1690 | |
| 1691 | if( !pPage->leaf ){ |
| 1692 | Pgno childPgno = get4byte(&pPage->aData[pPage->hdrOffset+8]); |
| 1693 | rc = ptrmapPut(pBt, childPgno, PTRMAP_BTREE, pgno); |
| 1694 | } |
| 1695 | |
| 1696 | set_child_ptrmaps_out: |
| 1697 | pPage->isInit = isInitOrig; |
| 1698 | return rc; |
| 1699 | } |
| 1700 | |
| 1701 | /* |
| 1702 | ** Somewhere on pPage, which is guarenteed to be a btree page, not an overflow |
| 1703 | ** page, is a pointer to page iFrom. Modify this pointer so that it points to |
| 1704 | ** iTo. Parameter eType describes the type of pointer to be modified, as |
| 1705 | ** follows: |
| 1706 | ** |
| 1707 | ** PTRMAP_BTREE: pPage is a btree-page. The pointer points at a child |
| 1708 | ** page of pPage. |
| 1709 | ** |
| 1710 | ** PTRMAP_OVERFLOW1: pPage is a btree-page. The pointer points at an overflow |
| 1711 | ** page pointed to by one of the cells on pPage. |
| 1712 | ** |
| 1713 | ** PTRMAP_OVERFLOW2: pPage is an overflow-page. The pointer points at the next |
| 1714 | ** overflow page in the list. |
| 1715 | */ |
danielk1977 | fdb7cdb | 2005-01-17 02:12:18 +0000 | [diff] [blame] | 1716 | static int modifyPagePointer(MemPage *pPage, Pgno iFrom, Pgno iTo, u8 eType){ |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 1717 | if( eType==PTRMAP_OVERFLOW2 ){ |
danielk1977 | f78fc08 | 2004-11-02 14:40:32 +0000 | [diff] [blame] | 1718 | /* 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] | 1719 | if( get4byte(pPage->aData)!=iFrom ){ |
drh | 4928570 | 2005-09-17 15:20:26 +0000 | [diff] [blame] | 1720 | return SQLITE_CORRUPT_BKPT; |
danielk1977 | fdb7cdb | 2005-01-17 02:12:18 +0000 | [diff] [blame] | 1721 | } |
danielk1977 | f78fc08 | 2004-11-02 14:40:32 +0000 | [diff] [blame] | 1722 | put4byte(pPage->aData, iTo); |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 1723 | }else{ |
| 1724 | int isInitOrig = pPage->isInit; |
| 1725 | int i; |
| 1726 | int nCell; |
| 1727 | |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 1728 | sqlite3BtreeInitPage(pPage, 0); |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 1729 | nCell = pPage->nCell; |
| 1730 | |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 1731 | for(i=0; i<nCell; i++){ |
danielk1977 | 1cc5ed8 | 2007-05-16 17:28:43 +0000 | [diff] [blame] | 1732 | u8 *pCell = findCell(pPage, i); |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 1733 | if( eType==PTRMAP_OVERFLOW1 ){ |
| 1734 | CellInfo info; |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 1735 | sqlite3BtreeParseCellPtr(pPage, pCell, &info); |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 1736 | if( info.iOverflow ){ |
| 1737 | if( iFrom==get4byte(&pCell[info.iOverflow]) ){ |
| 1738 | put4byte(&pCell[info.iOverflow], iTo); |
| 1739 | break; |
| 1740 | } |
| 1741 | } |
| 1742 | }else{ |
| 1743 | if( get4byte(pCell)==iFrom ){ |
| 1744 | put4byte(pCell, iTo); |
| 1745 | break; |
| 1746 | } |
| 1747 | } |
| 1748 | } |
| 1749 | |
| 1750 | if( i==nCell ){ |
danielk1977 | fdb7cdb | 2005-01-17 02:12:18 +0000 | [diff] [blame] | 1751 | if( eType!=PTRMAP_BTREE || |
| 1752 | get4byte(&pPage->aData[pPage->hdrOffset+8])!=iFrom ){ |
drh | 4928570 | 2005-09-17 15:20:26 +0000 | [diff] [blame] | 1753 | return SQLITE_CORRUPT_BKPT; |
danielk1977 | fdb7cdb | 2005-01-17 02:12:18 +0000 | [diff] [blame] | 1754 | } |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 1755 | put4byte(&pPage->aData[pPage->hdrOffset+8], iTo); |
| 1756 | } |
| 1757 | |
| 1758 | pPage->isInit = isInitOrig; |
| 1759 | } |
danielk1977 | fdb7cdb | 2005-01-17 02:12:18 +0000 | [diff] [blame] | 1760 | return SQLITE_OK; |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 1761 | } |
| 1762 | |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 1763 | |
danielk1977 | 7701e81 | 2005-01-10 12:59:51 +0000 | [diff] [blame] | 1764 | /* |
| 1765 | ** Move the open database page pDbPage to location iFreePage in the |
| 1766 | ** database. The pDbPage reference remains valid. |
| 1767 | */ |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 1768 | static int relocatePage( |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1769 | BtShared *pBt, /* Btree */ |
danielk1977 | 7701e81 | 2005-01-10 12:59:51 +0000 | [diff] [blame] | 1770 | MemPage *pDbPage, /* Open page to move */ |
| 1771 | u8 eType, /* Pointer map 'type' entry for pDbPage */ |
| 1772 | Pgno iPtrPage, /* Pointer map 'page-no' entry for pDbPage */ |
| 1773 | Pgno iFreePage /* The location to move pDbPage to */ |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 1774 | ){ |
| 1775 | MemPage *pPtrPage; /* The page that contains a pointer to pDbPage */ |
| 1776 | Pgno iDbPage = pDbPage->pgno; |
| 1777 | Pager *pPager = pBt->pPager; |
| 1778 | int rc; |
| 1779 | |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 1780 | assert( eType==PTRMAP_OVERFLOW2 || eType==PTRMAP_OVERFLOW1 || |
| 1781 | eType==PTRMAP_BTREE || eType==PTRMAP_ROOTPAGE ); |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 1782 | |
| 1783 | /* Move page iDbPage from it's current location to page number iFreePage */ |
| 1784 | TRACE(("AUTOVACUUM: Moving %d to free page %d (ptr page %d type %d)\n", |
| 1785 | iDbPage, iFreePage, iPtrPage, eType)); |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 1786 | rc = sqlite3PagerMovepage(pPager, pDbPage->pDbPage, iFreePage); |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 1787 | if( rc!=SQLITE_OK ){ |
| 1788 | return rc; |
| 1789 | } |
| 1790 | pDbPage->pgno = iFreePage; |
| 1791 | |
| 1792 | /* If pDbPage was a btree-page, then it may have child pages and/or cells |
| 1793 | ** that point to overflow pages. The pointer map entries for all these |
| 1794 | ** pages need to be changed. |
| 1795 | ** |
| 1796 | ** If pDbPage is an overflow page, then the first 4 bytes may store a |
| 1797 | ** pointer to a subsequent overflow page. If this is the case, then |
| 1798 | ** the pointer map needs to be updated for the subsequent overflow page. |
| 1799 | */ |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 1800 | if( eType==PTRMAP_BTREE || eType==PTRMAP_ROOTPAGE ){ |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 1801 | rc = setChildPtrmaps(pDbPage); |
| 1802 | if( rc!=SQLITE_OK ){ |
| 1803 | return rc; |
| 1804 | } |
| 1805 | }else{ |
| 1806 | Pgno nextOvfl = get4byte(pDbPage->aData); |
| 1807 | if( nextOvfl!=0 ){ |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 1808 | rc = ptrmapPut(pBt, nextOvfl, PTRMAP_OVERFLOW2, iFreePage); |
| 1809 | if( rc!=SQLITE_OK ){ |
| 1810 | return rc; |
| 1811 | } |
| 1812 | } |
| 1813 | } |
| 1814 | |
| 1815 | /* Fix the database pointer on page iPtrPage that pointed at iDbPage so |
| 1816 | ** that it points at iFreePage. Also fix the pointer map entry for |
| 1817 | ** iPtrPage. |
| 1818 | */ |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 1819 | if( eType!=PTRMAP_ROOTPAGE ){ |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 1820 | rc = sqlite3BtreeGetPage(pBt, iPtrPage, &pPtrPage, 0); |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 1821 | if( rc!=SQLITE_OK ){ |
| 1822 | return rc; |
| 1823 | } |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 1824 | rc = sqlite3PagerWrite(pPtrPage->pDbPage); |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 1825 | if( rc!=SQLITE_OK ){ |
| 1826 | releasePage(pPtrPage); |
| 1827 | return rc; |
| 1828 | } |
danielk1977 | fdb7cdb | 2005-01-17 02:12:18 +0000 | [diff] [blame] | 1829 | rc = modifyPagePointer(pPtrPage, iDbPage, iFreePage, eType); |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 1830 | releasePage(pPtrPage); |
danielk1977 | fdb7cdb | 2005-01-17 02:12:18 +0000 | [diff] [blame] | 1831 | if( rc==SQLITE_OK ){ |
| 1832 | rc = ptrmapPut(pBt, iFreePage, eType, iPtrPage); |
| 1833 | } |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 1834 | } |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 1835 | return rc; |
| 1836 | } |
| 1837 | |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 1838 | /* Forward declaration required by incrVacuumStep(). */ |
drh | 4f0c587 | 2007-03-26 22:05:01 +0000 | [diff] [blame] | 1839 | static int allocateBtreePage(BtShared *, MemPage **, Pgno *, Pgno, u8); |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 1840 | |
| 1841 | /* |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 1842 | ** Perform a single step of an incremental-vacuum. If successful, |
| 1843 | ** return SQLITE_OK. If there is no work to do (and therefore no |
| 1844 | ** point in calling this function again), return SQLITE_DONE. |
| 1845 | ** |
| 1846 | ** More specificly, this function attempts to re-organize the |
| 1847 | ** database so that the last page of the file currently in use |
| 1848 | ** is no longer in use. |
| 1849 | ** |
| 1850 | ** If the nFin parameter is non-zero, the implementation assumes |
| 1851 | ** that the caller will keep calling incrVacuumStep() until |
| 1852 | ** it returns SQLITE_DONE or an error, and that nFin is the |
| 1853 | ** number of pages the database file will contain after this |
| 1854 | ** process is complete. |
| 1855 | */ |
| 1856 | static int incrVacuumStep(BtShared *pBt, Pgno nFin){ |
| 1857 | Pgno iLastPg; /* Last page in the database */ |
| 1858 | Pgno nFreeList; /* Number of pages still on the free-list */ |
| 1859 | |
| 1860 | iLastPg = pBt->nTrunc; |
| 1861 | if( iLastPg==0 ){ |
| 1862 | iLastPg = sqlite3PagerPagecount(pBt->pPager); |
| 1863 | } |
| 1864 | |
| 1865 | if( !PTRMAP_ISPAGE(pBt, iLastPg) && iLastPg!=PENDING_BYTE_PAGE(pBt) ){ |
| 1866 | int rc; |
| 1867 | u8 eType; |
| 1868 | Pgno iPtrPage; |
| 1869 | |
| 1870 | nFreeList = get4byte(&pBt->pPage1->aData[36]); |
| 1871 | if( nFreeList==0 || nFin==iLastPg ){ |
| 1872 | return SQLITE_DONE; |
| 1873 | } |
| 1874 | |
| 1875 | rc = ptrmapGet(pBt, iLastPg, &eType, &iPtrPage); |
| 1876 | if( rc!=SQLITE_OK ){ |
| 1877 | return rc; |
| 1878 | } |
| 1879 | if( eType==PTRMAP_ROOTPAGE ){ |
| 1880 | return SQLITE_CORRUPT_BKPT; |
| 1881 | } |
| 1882 | |
| 1883 | if( eType==PTRMAP_FREEPAGE ){ |
| 1884 | if( nFin==0 ){ |
| 1885 | /* Remove the page from the files free-list. This is not required |
danielk1977 | 4ef2449 | 2007-05-23 09:52:41 +0000 | [diff] [blame] | 1886 | ** if nFin is non-zero. In that case, the free-list will be |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 1887 | ** truncated to zero after this function returns, so it doesn't |
| 1888 | ** matter if it still contains some garbage entries. |
| 1889 | */ |
| 1890 | Pgno iFreePg; |
| 1891 | MemPage *pFreePg; |
| 1892 | rc = allocateBtreePage(pBt, &pFreePg, &iFreePg, iLastPg, 1); |
| 1893 | if( rc!=SQLITE_OK ){ |
| 1894 | return rc; |
| 1895 | } |
| 1896 | assert( iFreePg==iLastPg ); |
| 1897 | releasePage(pFreePg); |
| 1898 | } |
| 1899 | } else { |
| 1900 | Pgno iFreePg; /* Index of free page to move pLastPg to */ |
| 1901 | MemPage *pLastPg; |
| 1902 | |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 1903 | rc = sqlite3BtreeGetPage(pBt, iLastPg, &pLastPg, 0); |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 1904 | if( rc!=SQLITE_OK ){ |
| 1905 | return rc; |
| 1906 | } |
| 1907 | |
danielk1977 | b4626a3 | 2007-04-28 15:47:43 +0000 | [diff] [blame] | 1908 | /* If nFin is zero, this loop runs exactly once and page pLastPg |
| 1909 | ** is swapped with the first free page pulled off the free list. |
| 1910 | ** |
| 1911 | ** On the other hand, if nFin is greater than zero, then keep |
| 1912 | ** looping until a free-page located within the first nFin pages |
| 1913 | ** of the file is found. |
| 1914 | */ |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 1915 | do { |
| 1916 | MemPage *pFreePg; |
| 1917 | rc = allocateBtreePage(pBt, &pFreePg, &iFreePg, 0, 0); |
| 1918 | if( rc!=SQLITE_OK ){ |
| 1919 | releasePage(pLastPg); |
| 1920 | return rc; |
| 1921 | } |
| 1922 | releasePage(pFreePg); |
| 1923 | }while( nFin!=0 && iFreePg>nFin ); |
| 1924 | assert( iFreePg<iLastPg ); |
danielk1977 | b4626a3 | 2007-04-28 15:47:43 +0000 | [diff] [blame] | 1925 | |
| 1926 | rc = sqlite3PagerWrite(pLastPg->pDbPage); |
| 1927 | if( rc!=SQLITE_OK ){ |
| 1928 | return rc; |
| 1929 | } |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 1930 | rc = relocatePage(pBt, pLastPg, eType, iPtrPage, iFreePg); |
| 1931 | releasePage(pLastPg); |
| 1932 | if( rc!=SQLITE_OK ){ |
| 1933 | return rc; |
| 1934 | } |
| 1935 | } |
| 1936 | } |
| 1937 | |
| 1938 | pBt->nTrunc = iLastPg - 1; |
| 1939 | while( pBt->nTrunc==PENDING_BYTE_PAGE(pBt)||PTRMAP_ISPAGE(pBt, pBt->nTrunc) ){ |
| 1940 | pBt->nTrunc--; |
| 1941 | } |
| 1942 | return SQLITE_OK; |
| 1943 | } |
| 1944 | |
| 1945 | /* |
| 1946 | ** A write-transaction must be opened before calling this function. |
| 1947 | ** It performs a single unit of work towards an incremental vacuum. |
| 1948 | ** |
| 1949 | ** If the incremental vacuum is finished after this function has run, |
| 1950 | ** SQLITE_DONE is returned. If it is not finished, but no error occured, |
| 1951 | ** SQLITE_OK is returned. Otherwise an SQLite error code. |
| 1952 | */ |
| 1953 | int sqlite3BtreeIncrVacuum(Btree *p){ |
| 1954 | BtShared *pBt = p->pBt; |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 1955 | assert( pBt->inTransaction==TRANS_WRITE && p->inTrans==TRANS_WRITE ); |
| 1956 | if( !pBt->autoVacuum ){ |
| 1957 | return SQLITE_DONE; |
| 1958 | } |
danielk1977 | 92d4d7a | 2007-05-04 12:05:56 +0000 | [diff] [blame] | 1959 | invalidateAllOverflowCache(pBt); |
danielk1977 | 2dec970 | 2007-05-02 16:48:37 +0000 | [diff] [blame] | 1960 | return incrVacuumStep(pBt, 0); |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 1961 | } |
| 1962 | |
| 1963 | /* |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 1964 | ** This routine is called prior to sqlite3PagerCommit when a transaction |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 1965 | ** is commited for an auto-vacuum database. |
danielk1977 | 2416872 | 2007-04-02 05:07:47 +0000 | [diff] [blame] | 1966 | ** |
| 1967 | ** If SQLITE_OK is returned, then *pnTrunc is set to the number of pages |
| 1968 | ** the database file should be truncated to during the commit process. |
| 1969 | ** i.e. the database has been reorganized so that only the first *pnTrunc |
| 1970 | ** pages are in use. |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 1971 | */ |
danielk1977 | 2416872 | 2007-04-02 05:07:47 +0000 | [diff] [blame] | 1972 | static int autoVacuumCommit(BtShared *pBt, Pgno *pnTrunc){ |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 1973 | int rc = SQLITE_OK; |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 1974 | Pager *pPager = pBt->pPager; |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 1975 | #ifndef NDEBUG |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 1976 | int nRef = sqlite3PagerRefcount(pPager); |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 1977 | #endif |
| 1978 | |
danielk1977 | 92d4d7a | 2007-05-04 12:05:56 +0000 | [diff] [blame] | 1979 | invalidateAllOverflowCache(pBt); |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 1980 | assert(pBt->autoVacuum); |
| 1981 | if( !pBt->incrVacuum ){ |
| 1982 | Pgno nFin = 0; |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 1983 | |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 1984 | if( pBt->nTrunc==0 ){ |
| 1985 | Pgno nFree; |
| 1986 | Pgno nPtrmap; |
| 1987 | const int pgsz = pBt->pageSize; |
| 1988 | Pgno nOrig = sqlite3PagerPagecount(pBt->pPager); |
danielk1977 | e5321f0 | 2007-04-27 07:05:44 +0000 | [diff] [blame] | 1989 | |
| 1990 | if( PTRMAP_ISPAGE(pBt, nOrig) ){ |
| 1991 | return SQLITE_CORRUPT_BKPT; |
| 1992 | } |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 1993 | if( nOrig==PENDING_BYTE_PAGE(pBt) ){ |
| 1994 | nOrig--; |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 1995 | } |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 1996 | nFree = get4byte(&pBt->pPage1->aData[36]); |
| 1997 | nPtrmap = (nFree-nOrig+PTRMAP_PAGENO(pBt, nOrig)+pgsz/5)/(pgsz/5); |
| 1998 | nFin = nOrig - nFree - nPtrmap; |
| 1999 | if( nOrig>PENDING_BYTE_PAGE(pBt) && nFin<=PENDING_BYTE_PAGE(pBt) ){ |
| 2000 | nFin--; |
danielk1977 | ac11ee6 | 2005-01-15 12:45:51 +0000 | [diff] [blame] | 2001 | } |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 2002 | while( PTRMAP_ISPAGE(pBt, nFin) || nFin==PENDING_BYTE_PAGE(pBt) ){ |
| 2003 | nFin--; |
| 2004 | } |
| 2005 | } |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 2006 | |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 2007 | while( rc==SQLITE_OK ){ |
| 2008 | rc = incrVacuumStep(pBt, nFin); |
| 2009 | } |
| 2010 | if( rc==SQLITE_DONE ){ |
| 2011 | assert(nFin==0 || pBt->nTrunc==0 || nFin<=pBt->nTrunc); |
| 2012 | rc = SQLITE_OK; |
| 2013 | if( pBt->nTrunc ){ |
drh | 67f80b6 | 2007-07-23 19:26:17 +0000 | [diff] [blame] | 2014 | rc = sqlite3PagerWrite(pBt->pPage1->pDbPage); |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 2015 | put4byte(&pBt->pPage1->aData[32], 0); |
| 2016 | put4byte(&pBt->pPage1->aData[36], 0); |
| 2017 | pBt->nTrunc = nFin; |
| 2018 | } |
| 2019 | } |
| 2020 | if( rc!=SQLITE_OK ){ |
| 2021 | sqlite3PagerRollback(pPager); |
| 2022 | } |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 2023 | } |
| 2024 | |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 2025 | if( rc==SQLITE_OK ){ |
| 2026 | *pnTrunc = pBt->nTrunc; |
| 2027 | pBt->nTrunc = 0; |
| 2028 | } |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 2029 | assert( nRef==sqlite3PagerRefcount(pPager) ); |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 2030 | return rc; |
| 2031 | } |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 2032 | |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 2033 | #endif |
| 2034 | |
| 2035 | /* |
drh | 80e35f4 | 2007-03-30 14:06:34 +0000 | [diff] [blame] | 2036 | ** This routine does the first phase of a two-phase commit. This routine |
| 2037 | ** causes a rollback journal to be created (if it does not already exist) |
| 2038 | ** and populated with enough information so that if a power loss occurs |
| 2039 | ** the database can be restored to its original state by playing back |
| 2040 | ** the journal. Then the contents of the journal are flushed out to |
| 2041 | ** the disk. After the journal is safely on oxide, the changes to the |
| 2042 | ** database are written into the database file and flushed to oxide. |
| 2043 | ** At the end of this call, the rollback journal still exists on the |
| 2044 | ** disk and we are still holding all locks, so the transaction has not |
| 2045 | ** committed. See sqlite3BtreeCommit() for the second phase of the |
| 2046 | ** commit process. |
| 2047 | ** |
| 2048 | ** This call is a no-op if no write-transaction is currently active on pBt. |
| 2049 | ** |
| 2050 | ** Otherwise, sync the database file for the btree pBt. zMaster points to |
| 2051 | ** the name of a master journal file that should be written into the |
| 2052 | ** individual journal file, or is NULL, indicating no master journal file |
| 2053 | ** (single database transaction). |
| 2054 | ** |
| 2055 | ** When this is called, the master journal should already have been |
| 2056 | ** created, populated with this journal pointer and synced to disk. |
| 2057 | ** |
| 2058 | ** Once this is routine has returned, the only thing required to commit |
| 2059 | ** the write-transaction for this database file is to delete the journal. |
| 2060 | */ |
| 2061 | int sqlite3BtreeCommitPhaseOne(Btree *p, const char *zMaster){ |
| 2062 | int rc = SQLITE_OK; |
| 2063 | if( p->inTrans==TRANS_WRITE ){ |
| 2064 | BtShared *pBt = p->pBt; |
| 2065 | Pgno nTrunc = 0; |
| 2066 | #ifndef SQLITE_OMIT_AUTOVACUUM |
| 2067 | if( pBt->autoVacuum ){ |
| 2068 | rc = autoVacuumCommit(pBt, &nTrunc); |
| 2069 | if( rc!=SQLITE_OK ){ |
| 2070 | return rc; |
| 2071 | } |
| 2072 | } |
| 2073 | #endif |
| 2074 | rc = sqlite3PagerCommitPhaseOne(pBt->pPager, zMaster, nTrunc); |
| 2075 | } |
| 2076 | return rc; |
| 2077 | } |
| 2078 | |
| 2079 | /* |
drh | 2aa679f | 2001-06-25 02:11:07 +0000 | [diff] [blame] | 2080 | ** Commit the transaction currently in progress. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2081 | ** |
drh | 6e34599 | 2007-03-30 11:12:08 +0000 | [diff] [blame] | 2082 | ** This routine implements the second phase of a 2-phase commit. The |
| 2083 | ** sqlite3BtreeSync() routine does the first phase and should be invoked |
| 2084 | ** prior to calling this routine. The sqlite3BtreeSync() routine did |
| 2085 | ** all the work of writing information out to disk and flushing the |
| 2086 | ** contents so that they are written onto the disk platter. All this |
| 2087 | ** routine has to do is delete or truncate the rollback journal |
| 2088 | ** (which causes the transaction to commit) and drop locks. |
| 2089 | ** |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2090 | ** This will release the write lock on the database file. If there |
| 2091 | ** are no active cursors, it also releases the read lock. |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 2092 | */ |
drh | 80e35f4 | 2007-03-30 14:06:34 +0000 | [diff] [blame] | 2093 | int sqlite3BtreeCommitPhaseTwo(Btree *p){ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2094 | BtShared *pBt = p->pBt; |
| 2095 | |
| 2096 | btreeIntegrity(p); |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2097 | |
| 2098 | /* If the handle has a write-transaction open, commit the shared-btrees |
| 2099 | ** transaction and set the shared state to TRANS_READ. |
| 2100 | */ |
| 2101 | if( p->inTrans==TRANS_WRITE ){ |
danielk1977 | 7f7bc66 | 2006-01-23 13:47:47 +0000 | [diff] [blame] | 2102 | int rc; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2103 | assert( pBt->inTransaction==TRANS_WRITE ); |
| 2104 | assert( pBt->nTransaction>0 ); |
drh | 80e35f4 | 2007-03-30 14:06:34 +0000 | [diff] [blame] | 2105 | rc = sqlite3PagerCommitPhaseTwo(pBt->pPager); |
danielk1977 | 7f7bc66 | 2006-01-23 13:47:47 +0000 | [diff] [blame] | 2106 | if( rc!=SQLITE_OK ){ |
| 2107 | return rc; |
| 2108 | } |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2109 | pBt->inTransaction = TRANS_READ; |
| 2110 | pBt->inStmt = 0; |
danielk1977 | ee5741e | 2004-05-31 10:01:34 +0000 | [diff] [blame] | 2111 | } |
danielk1977 | 7f7bc66 | 2006-01-23 13:47:47 +0000 | [diff] [blame] | 2112 | unlockAllTables(p); |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2113 | |
| 2114 | /* If the handle has any kind of transaction open, decrement the transaction |
| 2115 | ** count of the shared btree. If the transaction count reaches 0, set |
| 2116 | ** the shared state to TRANS_NONE. The unlockBtreeIfUnused() call below |
| 2117 | ** will unlock the pager. |
| 2118 | */ |
| 2119 | if( p->inTrans!=TRANS_NONE ){ |
| 2120 | pBt->nTransaction--; |
| 2121 | if( 0==pBt->nTransaction ){ |
| 2122 | pBt->inTransaction = TRANS_NONE; |
| 2123 | } |
| 2124 | } |
| 2125 | |
| 2126 | /* Set the handles current transaction state to TRANS_NONE and unlock |
| 2127 | ** the pager if this call closed the only read or write transaction. |
| 2128 | */ |
| 2129 | p->inTrans = TRANS_NONE; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2130 | unlockBtreeIfUnused(pBt); |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2131 | |
| 2132 | btreeIntegrity(p); |
danielk1977 | 7f7bc66 | 2006-01-23 13:47:47 +0000 | [diff] [blame] | 2133 | return SQLITE_OK; |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 2134 | } |
| 2135 | |
drh | 80e35f4 | 2007-03-30 14:06:34 +0000 | [diff] [blame] | 2136 | /* |
| 2137 | ** Do both phases of a commit. |
| 2138 | */ |
| 2139 | int sqlite3BtreeCommit(Btree *p){ |
| 2140 | int rc; |
| 2141 | rc = sqlite3BtreeCommitPhaseOne(p, 0); |
| 2142 | if( rc==SQLITE_OK ){ |
| 2143 | rc = sqlite3BtreeCommitPhaseTwo(p); |
| 2144 | } |
| 2145 | return rc; |
| 2146 | } |
| 2147 | |
danielk1977 | fbcd585 | 2004-06-15 02:44:18 +0000 | [diff] [blame] | 2148 | #ifndef NDEBUG |
| 2149 | /* |
| 2150 | ** Return the number of write-cursors open on this handle. This is for use |
| 2151 | ** in assert() expressions, so it is only compiled if NDEBUG is not |
| 2152 | ** defined. |
| 2153 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2154 | static int countWriteCursors(BtShared *pBt){ |
danielk1977 | fbcd585 | 2004-06-15 02:44:18 +0000 | [diff] [blame] | 2155 | BtCursor *pCur; |
| 2156 | int r = 0; |
| 2157 | for(pCur=pBt->pCursor; pCur; pCur=pCur->pNext){ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2158 | if( pCur->wrFlag ) r++; |
danielk1977 | fbcd585 | 2004-06-15 02:44:18 +0000 | [diff] [blame] | 2159 | } |
| 2160 | return r; |
| 2161 | } |
| 2162 | #endif |
| 2163 | |
drh | c39e000 | 2004-05-07 23:50:57 +0000 | [diff] [blame] | 2164 | /* |
drh | ecdc753 | 2001-09-23 02:35:53 +0000 | [diff] [blame] | 2165 | ** Rollback the transaction in progress. All cursors will be |
| 2166 | ** invalided by this operation. Any attempt to use a cursor |
| 2167 | ** that was open at the beginning of this operation will result |
| 2168 | ** in an error. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2169 | ** |
| 2170 | ** This will release the write lock on the database file. If there |
| 2171 | ** are no active cursors, it also releases the read lock. |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 2172 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2173 | int sqlite3BtreeRollback(Btree *p){ |
danielk1977 | 8d34dfd | 2006-01-24 16:37:57 +0000 | [diff] [blame] | 2174 | int rc; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2175 | BtShared *pBt = p->pBt; |
drh | 24cd67e | 2004-05-10 16:18:47 +0000 | [diff] [blame] | 2176 | MemPage *pPage1; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2177 | |
danielk1977 | 2b8c13e | 2006-01-24 14:21:24 +0000 | [diff] [blame] | 2178 | rc = saveAllCursors(pBt, 0, 0); |
danielk1977 | 8d34dfd | 2006-01-24 16:37:57 +0000 | [diff] [blame] | 2179 | #ifndef SQLITE_OMIT_SHARED_CACHE |
danielk1977 | 2b8c13e | 2006-01-24 14:21:24 +0000 | [diff] [blame] | 2180 | if( rc!=SQLITE_OK ){ |
danielk1977 | 8d34dfd | 2006-01-24 16:37:57 +0000 | [diff] [blame] | 2181 | /* This is a horrible situation. An IO or malloc() error occured whilst |
| 2182 | ** trying to save cursor positions. If this is an automatic rollback (as |
| 2183 | ** the result of a constraint, malloc() failure or IO error) then |
| 2184 | ** the cache may be internally inconsistent (not contain valid trees) so |
| 2185 | ** we cannot simply return the error to the caller. Instead, abort |
| 2186 | ** all queries that may be using any of the cursors that failed to save. |
| 2187 | */ |
| 2188 | while( pBt->pCursor ){ |
| 2189 | sqlite3 *db = pBt->pCursor->pBtree->pSqlite; |
| 2190 | if( db ){ |
| 2191 | sqlite3AbortOtherActiveVdbes(db, 0); |
| 2192 | } |
| 2193 | } |
danielk1977 | 2b8c13e | 2006-01-24 14:21:24 +0000 | [diff] [blame] | 2194 | } |
danielk1977 | 8d34dfd | 2006-01-24 16:37:57 +0000 | [diff] [blame] | 2195 | #endif |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2196 | btreeIntegrity(p); |
| 2197 | unlockAllTables(p); |
| 2198 | |
| 2199 | if( p->inTrans==TRANS_WRITE ){ |
danielk1977 | 8d34dfd | 2006-01-24 16:37:57 +0000 | [diff] [blame] | 2200 | int rc2; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2201 | |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 2202 | #ifndef SQLITE_OMIT_AUTOVACUUM |
| 2203 | pBt->nTrunc = 0; |
| 2204 | #endif |
| 2205 | |
danielk1977 | 8d34dfd | 2006-01-24 16:37:57 +0000 | [diff] [blame] | 2206 | assert( TRANS_WRITE==pBt->inTransaction ); |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 2207 | rc2 = sqlite3PagerRollback(pBt->pPager); |
danielk1977 | 8d34dfd | 2006-01-24 16:37:57 +0000 | [diff] [blame] | 2208 | if( rc2!=SQLITE_OK ){ |
| 2209 | rc = rc2; |
| 2210 | } |
| 2211 | |
drh | 24cd67e | 2004-05-10 16:18:47 +0000 | [diff] [blame] | 2212 | /* The rollback may have destroyed the pPage1->aData value. So |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 2213 | ** call sqlite3BtreeGetPage() on page 1 again to make |
| 2214 | ** sure pPage1->aData is set correctly. */ |
| 2215 | if( sqlite3BtreeGetPage(pBt, 1, &pPage1, 0)==SQLITE_OK ){ |
drh | 24cd67e | 2004-05-10 16:18:47 +0000 | [diff] [blame] | 2216 | releasePage(pPage1); |
| 2217 | } |
danielk1977 | fbcd585 | 2004-06-15 02:44:18 +0000 | [diff] [blame] | 2218 | assert( countWriteCursors(pBt)==0 ); |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2219 | pBt->inTransaction = TRANS_READ; |
drh | 24cd67e | 2004-05-10 16:18:47 +0000 | [diff] [blame] | 2220 | } |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2221 | |
| 2222 | if( p->inTrans!=TRANS_NONE ){ |
| 2223 | assert( pBt->nTransaction>0 ); |
| 2224 | pBt->nTransaction--; |
| 2225 | if( 0==pBt->nTransaction ){ |
| 2226 | pBt->inTransaction = TRANS_NONE; |
| 2227 | } |
| 2228 | } |
| 2229 | |
| 2230 | p->inTrans = TRANS_NONE; |
danielk1977 | ee5741e | 2004-05-31 10:01:34 +0000 | [diff] [blame] | 2231 | pBt->inStmt = 0; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2232 | unlockBtreeIfUnused(pBt); |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2233 | |
| 2234 | btreeIntegrity(p); |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 2235 | return rc; |
| 2236 | } |
| 2237 | |
| 2238 | /* |
drh | ab01f61 | 2004-05-22 02:55:23 +0000 | [diff] [blame] | 2239 | ** Start a statement subtransaction. The subtransaction can |
| 2240 | ** can be rolled back independently of the main transaction. |
| 2241 | ** You must start a transaction before starting a subtransaction. |
| 2242 | ** The subtransaction is ended automatically if the main transaction |
drh | 663fc63 | 2002-02-02 18:49:19 +0000 | [diff] [blame] | 2243 | ** commits or rolls back. |
| 2244 | ** |
drh | ab01f61 | 2004-05-22 02:55:23 +0000 | [diff] [blame] | 2245 | ** Only one subtransaction may be active at a time. It is an error to try |
| 2246 | ** to start a new subtransaction if another subtransaction is already active. |
| 2247 | ** |
| 2248 | ** Statement subtransactions are used around individual SQL statements |
| 2249 | ** that are contained within a BEGIN...COMMIT block. If a constraint |
| 2250 | ** error occurs within the statement, the effect of that one statement |
| 2251 | ** can be rolled back without having to rollback the entire transaction. |
drh | 663fc63 | 2002-02-02 18:49:19 +0000 | [diff] [blame] | 2252 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2253 | int sqlite3BtreeBeginStmt(Btree *p){ |
drh | 663fc63 | 2002-02-02 18:49:19 +0000 | [diff] [blame] | 2254 | int rc; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2255 | BtShared *pBt = p->pBt; |
| 2256 | if( (p->inTrans!=TRANS_WRITE) || pBt->inStmt ){ |
drh | f74b8d9 | 2002-09-01 23:20:45 +0000 | [diff] [blame] | 2257 | return pBt->readOnly ? SQLITE_READONLY : SQLITE_ERROR; |
drh | 0d65dc0 | 2002-02-03 00:56:09 +0000 | [diff] [blame] | 2258 | } |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2259 | assert( pBt->inTransaction==TRANS_WRITE ); |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 2260 | rc = pBt->readOnly ? SQLITE_OK : sqlite3PagerStmtBegin(pBt->pPager); |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2261 | pBt->inStmt = 1; |
drh | 663fc63 | 2002-02-02 18:49:19 +0000 | [diff] [blame] | 2262 | return rc; |
| 2263 | } |
| 2264 | |
| 2265 | |
| 2266 | /* |
drh | ab01f61 | 2004-05-22 02:55:23 +0000 | [diff] [blame] | 2267 | ** Commit the statment subtransaction currently in progress. If no |
| 2268 | ** subtransaction is active, this is a no-op. |
drh | 663fc63 | 2002-02-02 18:49:19 +0000 | [diff] [blame] | 2269 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2270 | int sqlite3BtreeCommitStmt(Btree *p){ |
drh | 663fc63 | 2002-02-02 18:49:19 +0000 | [diff] [blame] | 2271 | int rc; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2272 | BtShared *pBt = p->pBt; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2273 | if( pBt->inStmt && !pBt->readOnly ){ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 2274 | rc = sqlite3PagerStmtCommit(pBt->pPager); |
drh | 663fc63 | 2002-02-02 18:49:19 +0000 | [diff] [blame] | 2275 | }else{ |
| 2276 | rc = SQLITE_OK; |
| 2277 | } |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2278 | pBt->inStmt = 0; |
drh | 663fc63 | 2002-02-02 18:49:19 +0000 | [diff] [blame] | 2279 | return rc; |
| 2280 | } |
| 2281 | |
| 2282 | /* |
drh | ab01f61 | 2004-05-22 02:55:23 +0000 | [diff] [blame] | 2283 | ** Rollback the active statement subtransaction. If no subtransaction |
| 2284 | ** is active this routine is a no-op. |
drh | 663fc63 | 2002-02-02 18:49:19 +0000 | [diff] [blame] | 2285 | ** |
drh | ab01f61 | 2004-05-22 02:55:23 +0000 | [diff] [blame] | 2286 | ** All cursors will be invalidated by this operation. Any attempt |
drh | 663fc63 | 2002-02-02 18:49:19 +0000 | [diff] [blame] | 2287 | ** to use a cursor that was open at the beginning of this operation |
| 2288 | ** will result in an error. |
| 2289 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2290 | int sqlite3BtreeRollbackStmt(Btree *p){ |
danielk1977 | 97a227c | 2006-01-20 16:32:04 +0000 | [diff] [blame] | 2291 | int rc = SQLITE_OK; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2292 | BtShared *pBt = p->pBt; |
danielk1977 | 97a227c | 2006-01-20 16:32:04 +0000 | [diff] [blame] | 2293 | sqlite3MallocDisallow(); |
| 2294 | if( pBt->inStmt && !pBt->readOnly ){ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 2295 | rc = sqlite3PagerStmtRollback(pBt->pPager); |
danielk1977 | 97a227c | 2006-01-20 16:32:04 +0000 | [diff] [blame] | 2296 | assert( countWriteCursors(pBt)==0 ); |
| 2297 | pBt->inStmt = 0; |
| 2298 | } |
| 2299 | sqlite3MallocAllow(); |
drh | 663fc63 | 2002-02-02 18:49:19 +0000 | [diff] [blame] | 2300 | return rc; |
| 2301 | } |
| 2302 | |
| 2303 | /* |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2304 | ** Default key comparison function to be used if no comparison function |
| 2305 | ** is specified on the sqlite3BtreeCursor() call. |
| 2306 | */ |
| 2307 | static int dfltCompare( |
| 2308 | void *NotUsed, /* User data is not used */ |
| 2309 | int n1, const void *p1, /* First key to compare */ |
| 2310 | int n2, const void *p2 /* Second key to compare */ |
| 2311 | ){ |
| 2312 | int c; |
| 2313 | c = memcmp(p1, p2, n1<n2 ? n1 : n2); |
| 2314 | if( c==0 ){ |
| 2315 | c = n1 - n2; |
| 2316 | } |
| 2317 | return c; |
| 2318 | } |
| 2319 | |
| 2320 | /* |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 2321 | ** Create a new cursor for the BTree whose root is on the page |
| 2322 | ** iTable. The act of acquiring a cursor gets a read lock on |
| 2323 | ** the database file. |
drh | 1bee3d7 | 2001-10-15 00:44:35 +0000 | [diff] [blame] | 2324 | ** |
| 2325 | ** If wrFlag==0, then the cursor can only be used for reading. |
drh | f74b8d9 | 2002-09-01 23:20:45 +0000 | [diff] [blame] | 2326 | ** If wrFlag==1, then the cursor can be used for reading or for |
| 2327 | ** writing if other conditions for writing are also met. These |
| 2328 | ** are the conditions that must be met in order for writing to |
| 2329 | ** be allowed: |
drh | 6446c4d | 2001-12-15 14:22:18 +0000 | [diff] [blame] | 2330 | ** |
drh | f74b8d9 | 2002-09-01 23:20:45 +0000 | [diff] [blame] | 2331 | ** 1: The cursor must have been opened with wrFlag==1 |
| 2332 | ** |
drh | fe5d71d | 2007-03-19 11:54:10 +0000 | [diff] [blame] | 2333 | ** 2: Other database connections that share the same pager cache |
| 2334 | ** but which are not in the READ_UNCOMMITTED state may not have |
| 2335 | ** cursors open with wrFlag==0 on the same table. Otherwise |
| 2336 | ** the changes made by this write cursor would be visible to |
| 2337 | ** the read cursors in the other database connection. |
drh | f74b8d9 | 2002-09-01 23:20:45 +0000 | [diff] [blame] | 2338 | ** |
| 2339 | ** 3: The database must be writable (not on read-only media) |
| 2340 | ** |
| 2341 | ** 4: There must be an active transaction. |
| 2342 | ** |
drh | 6446c4d | 2001-12-15 14:22:18 +0000 | [diff] [blame] | 2343 | ** No checking is done to make sure that page iTable really is the |
| 2344 | ** root page of a b-tree. If it is not, then the cursor acquired |
| 2345 | ** will not work correctly. |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2346 | ** |
| 2347 | ** The comparison function must be logically the same for every cursor |
| 2348 | ** on a particular table. Changing the comparison function will result |
| 2349 | ** in incorrect operations. If the comparison function is NULL, a |
| 2350 | ** default comparison function is used. The comparison function is |
| 2351 | ** always ignored for INTKEY tables. |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 2352 | */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2353 | int sqlite3BtreeCursor( |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2354 | Btree *p, /* The btree */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2355 | int iTable, /* Root page of table to open */ |
| 2356 | int wrFlag, /* 1 to write. 0 read-only */ |
| 2357 | int (*xCmp)(void*,int,const void*,int,const void*), /* Key Comparison func */ |
| 2358 | void *pArg, /* First arg to xCompare() */ |
| 2359 | BtCursor **ppCur /* Write new cursor here */ |
| 2360 | ){ |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 2361 | int rc; |
drh | 8dcd7ca | 2004-08-08 19:43:29 +0000 | [diff] [blame] | 2362 | BtCursor *pCur; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2363 | BtShared *pBt = p->pBt; |
drh | ecdc753 | 2001-09-23 02:35:53 +0000 | [diff] [blame] | 2364 | |
drh | 8dcd7ca | 2004-08-08 19:43:29 +0000 | [diff] [blame] | 2365 | *ppCur = 0; |
| 2366 | if( wrFlag ){ |
drh | 8dcd7ca | 2004-08-08 19:43:29 +0000 | [diff] [blame] | 2367 | if( pBt->readOnly ){ |
| 2368 | return SQLITE_READONLY; |
| 2369 | } |
drh | 980b1a7 | 2006-08-16 16:42:48 +0000 | [diff] [blame] | 2370 | if( checkReadLocks(p, iTable, 0) ){ |
drh | 8dcd7ca | 2004-08-08 19:43:29 +0000 | [diff] [blame] | 2371 | return SQLITE_LOCKED; |
| 2372 | } |
drh | a0c9a11 | 2004-03-10 13:42:37 +0000 | [diff] [blame] | 2373 | } |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2374 | |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 2375 | if( pBt->pPage1==0 ){ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2376 | rc = lockBtreeWithRetry(p); |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 2377 | if( rc!=SQLITE_OK ){ |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 2378 | return rc; |
| 2379 | } |
drh | 1831f18 | 2007-04-24 17:35:59 +0000 | [diff] [blame] | 2380 | if( pBt->readOnly && wrFlag ){ |
| 2381 | return SQLITE_READONLY; |
| 2382 | } |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 2383 | } |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 2384 | pCur = sqliteMalloc( sizeof(*pCur) ); |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 2385 | if( pCur==0 ){ |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 2386 | rc = SQLITE_NOMEM; |
| 2387 | goto create_cursor_exception; |
| 2388 | } |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 2389 | pCur->pgnoRoot = (Pgno)iTable; |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 2390 | if( iTable==1 && sqlite3PagerPagecount(pBt->pPager)==0 ){ |
drh | 24cd67e | 2004-05-10 16:18:47 +0000 | [diff] [blame] | 2391 | rc = SQLITE_EMPTY; |
| 2392 | goto create_cursor_exception; |
| 2393 | } |
drh | de64713 | 2004-05-07 17:57:49 +0000 | [diff] [blame] | 2394 | rc = getAndInitPage(pBt, pCur->pgnoRoot, &pCur->pPage, 0); |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 2395 | if( rc!=SQLITE_OK ){ |
| 2396 | goto create_cursor_exception; |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 2397 | } |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2398 | |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2399 | /* Now that no other errors can occur, finish filling in the BtCursor |
| 2400 | ** variables, link the cursor into the BtShared list and set *ppCur (the |
| 2401 | ** output argument to this function). |
| 2402 | */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2403 | pCur->xCompare = xCmp ? xCmp : dfltCompare; |
| 2404 | pCur->pArg = pArg; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2405 | pCur->pBtree = p; |
drh | ecdc753 | 2001-09-23 02:35:53 +0000 | [diff] [blame] | 2406 | pCur->wrFlag = wrFlag; |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 2407 | pCur->pNext = pBt->pCursor; |
| 2408 | if( pCur->pNext ){ |
| 2409 | pCur->pNext->pPrev = pCur; |
| 2410 | } |
| 2411 | pBt->pCursor = pCur; |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 2412 | pCur->eState = CURSOR_INVALID; |
drh | 2af926b | 2001-05-15 00:39:25 +0000 | [diff] [blame] | 2413 | *ppCur = pCur; |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 2414 | |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2415 | return SQLITE_OK; |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 2416 | create_cursor_exception: |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 2417 | if( pCur ){ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2418 | releasePage(pCur->pPage); |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 2419 | sqliteFree(pCur); |
| 2420 | } |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2421 | unlockBtreeIfUnused(pBt); |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 2422 | return rc; |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 2423 | } |
| 2424 | |
| 2425 | /* |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2426 | ** Close a cursor. The read lock on the database file is released |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 2427 | ** when the last cursor is closed. |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 2428 | */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2429 | int sqlite3BtreeCloseCursor(BtCursor *pCur){ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2430 | BtShared *pBt = pCur->pBtree->pBt; |
drh | bf700f3 | 2007-03-31 02:36:44 +0000 | [diff] [blame] | 2431 | clearCursorPosition(pCur); |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 2432 | if( pCur->pPrev ){ |
| 2433 | pCur->pPrev->pNext = pCur->pNext; |
| 2434 | }else{ |
| 2435 | pBt->pCursor = pCur->pNext; |
| 2436 | } |
| 2437 | if( pCur->pNext ){ |
| 2438 | pCur->pNext->pPrev = pCur->pPrev; |
| 2439 | } |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2440 | releasePage(pCur->pPage); |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2441 | unlockBtreeIfUnused(pBt); |
danielk1977 | 92d4d7a | 2007-05-04 12:05:56 +0000 | [diff] [blame] | 2442 | invalidateOverflowCache(pCur); |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 2443 | sqliteFree(pCur); |
drh | 8c42ca9 | 2001-06-22 19:15:00 +0000 | [diff] [blame] | 2444 | return SQLITE_OK; |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 2445 | } |
| 2446 | |
drh | 7e3b0a0 | 2001-04-28 16:52:40 +0000 | [diff] [blame] | 2447 | /* |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 2448 | ** Make a temporary cursor by filling in the fields of pTempCur. |
| 2449 | ** The temporary cursor is not on the cursor list for the Btree. |
| 2450 | */ |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 2451 | void sqlite3BtreeGetTempCursor(BtCursor *pCur, BtCursor *pTempCur){ |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 2452 | memcpy(pTempCur, pCur, sizeof(*pCur)); |
| 2453 | pTempCur->pNext = 0; |
| 2454 | pTempCur->pPrev = 0; |
drh | ecdc753 | 2001-09-23 02:35:53 +0000 | [diff] [blame] | 2455 | if( pTempCur->pPage ){ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 2456 | sqlite3PagerRef(pTempCur->pPage->pDbPage); |
drh | ecdc753 | 2001-09-23 02:35:53 +0000 | [diff] [blame] | 2457 | } |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 2458 | } |
| 2459 | |
| 2460 | /* |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 2461 | ** Delete a temporary cursor such as was made by the CreateTemporaryCursor() |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 2462 | ** function above. |
| 2463 | */ |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 2464 | void sqlite3BtreeReleaseTempCursor(BtCursor *pCur){ |
drh | ecdc753 | 2001-09-23 02:35:53 +0000 | [diff] [blame] | 2465 | if( pCur->pPage ){ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 2466 | sqlite3PagerUnref(pCur->pPage->pDbPage); |
drh | ecdc753 | 2001-09-23 02:35:53 +0000 | [diff] [blame] | 2467 | } |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 2468 | } |
| 2469 | |
| 2470 | /* |
drh | 8605761 | 2007-06-26 01:04:48 +0000 | [diff] [blame] | 2471 | ** Make sure the BtCursor* given in the argument has a valid |
| 2472 | ** BtCursor.info structure. If it is not already valid, call |
danielk1977 | 1cc5ed8 | 2007-05-16 17:28:43 +0000 | [diff] [blame] | 2473 | ** sqlite3BtreeParseCell() to fill it in. |
drh | ab01f61 | 2004-05-22 02:55:23 +0000 | [diff] [blame] | 2474 | ** |
| 2475 | ** BtCursor.info is a cache of the information in the current cell. |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 2476 | ** Using this cache reduces the number of calls to sqlite3BtreeParseCell(). |
drh | 8605761 | 2007-06-26 01:04:48 +0000 | [diff] [blame] | 2477 | ** |
| 2478 | ** 2007-06-25: There is a bug in some versions of MSVC that cause the |
| 2479 | ** compiler to crash when getCellInfo() is implemented as a macro. |
| 2480 | ** But there is a measureable speed advantage to using the macro on gcc |
| 2481 | ** (when less compiler optimizations like -Os or -O0 are used and the |
| 2482 | ** compiler is not doing agressive inlining.) So we use a real function |
| 2483 | ** for MSVC and a macro for everything else. Ticket #2457. |
drh | 9188b38 | 2004-05-14 21:12:22 +0000 | [diff] [blame] | 2484 | */ |
drh | 9188b38 | 2004-05-14 21:12:22 +0000 | [diff] [blame] | 2485 | #ifndef NDEBUG |
danielk1977 | 1cc5ed8 | 2007-05-16 17:28:43 +0000 | [diff] [blame] | 2486 | static void assertCellInfo(BtCursor *pCur){ |
drh | 9188b38 | 2004-05-14 21:12:22 +0000 | [diff] [blame] | 2487 | CellInfo info; |
drh | 51c6d96 | 2004-06-06 00:42:25 +0000 | [diff] [blame] | 2488 | memset(&info, 0, sizeof(info)); |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 2489 | sqlite3BtreeParseCell(pCur->pPage, pCur->idx, &info); |
drh | 9188b38 | 2004-05-14 21:12:22 +0000 | [diff] [blame] | 2490 | assert( memcmp(&info, &pCur->info, sizeof(info))==0 ); |
drh | 9188b38 | 2004-05-14 21:12:22 +0000 | [diff] [blame] | 2491 | } |
danielk1977 | 1cc5ed8 | 2007-05-16 17:28:43 +0000 | [diff] [blame] | 2492 | #else |
| 2493 | #define assertCellInfo(x) |
| 2494 | #endif |
drh | 8605761 | 2007-06-26 01:04:48 +0000 | [diff] [blame] | 2495 | #ifdef _MSC_VER |
| 2496 | /* Use a real function in MSVC to work around bugs in that compiler. */ |
| 2497 | static void getCellInfo(BtCursor *pCur){ |
| 2498 | if( pCur->info.nSize==0 ){ |
| 2499 | sqlite3BtreeParseCell(pCur->pPage, pCur->idx, &pCur->info); |
| 2500 | }else{ |
| 2501 | assertCellInfo(pCur); |
| 2502 | } |
| 2503 | } |
| 2504 | #else /* if not _MSC_VER */ |
| 2505 | /* Use a macro in all other compilers so that the function is inlined */ |
| 2506 | #define getCellInfo(pCur) \ |
| 2507 | if( pCur->info.nSize==0 ){ \ |
danielk1977 | 1cc5ed8 | 2007-05-16 17:28:43 +0000 | [diff] [blame] | 2508 | sqlite3BtreeParseCell(pCur->pPage, pCur->idx, &pCur->info); \ |
drh | 8605761 | 2007-06-26 01:04:48 +0000 | [diff] [blame] | 2509 | }else{ \ |
| 2510 | assertCellInfo(pCur); \ |
| 2511 | } |
| 2512 | #endif /* _MSC_VER */ |
drh | 9188b38 | 2004-05-14 21:12:22 +0000 | [diff] [blame] | 2513 | |
| 2514 | /* |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2515 | ** Set *pSize to the size of the buffer needed to hold the value of |
| 2516 | ** the key for the current entry. If the cursor is not pointing |
| 2517 | ** to a valid entry, *pSize is set to 0. |
| 2518 | ** |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 2519 | ** For a table with the INTKEY flag set, this routine returns the key |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2520 | ** itself, not the number of bytes in the key. |
drh | 7e3b0a0 | 2001-04-28 16:52:40 +0000 | [diff] [blame] | 2521 | */ |
drh | 4a1c380 | 2004-05-12 15:15:47 +0000 | [diff] [blame] | 2522 | int sqlite3BtreeKeySize(BtCursor *pCur, i64 *pSize){ |
drh | bf700f3 | 2007-03-31 02:36:44 +0000 | [diff] [blame] | 2523 | int rc = restoreOrClearCursorPosition(pCur); |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 2524 | if( rc==SQLITE_OK ){ |
| 2525 | assert( pCur->eState==CURSOR_INVALID || pCur->eState==CURSOR_VALID ); |
| 2526 | if( pCur->eState==CURSOR_INVALID ){ |
| 2527 | *pSize = 0; |
| 2528 | }else{ |
drh | 8605761 | 2007-06-26 01:04:48 +0000 | [diff] [blame] | 2529 | getCellInfo(pCur); |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 2530 | *pSize = pCur->info.nKey; |
| 2531 | } |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 2532 | } |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 2533 | return rc; |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 2534 | } |
drh | 2af926b | 2001-05-15 00:39:25 +0000 | [diff] [blame] | 2535 | |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 2536 | /* |
drh | 0e1c19e | 2004-05-11 00:58:56 +0000 | [diff] [blame] | 2537 | ** Set *pSize to the number of bytes of data in the entry the |
| 2538 | ** cursor currently points to. Always return SQLITE_OK. |
| 2539 | ** Failure is not possible. If the cursor is not currently |
| 2540 | ** pointing to an entry (which can happen, for example, if |
| 2541 | ** the database is empty) then *pSize is set to 0. |
| 2542 | */ |
| 2543 | int sqlite3BtreeDataSize(BtCursor *pCur, u32 *pSize){ |
drh | bf700f3 | 2007-03-31 02:36:44 +0000 | [diff] [blame] | 2544 | int rc = restoreOrClearCursorPosition(pCur); |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 2545 | if( rc==SQLITE_OK ){ |
| 2546 | assert( pCur->eState==CURSOR_INVALID || pCur->eState==CURSOR_VALID ); |
| 2547 | if( pCur->eState==CURSOR_INVALID ){ |
| 2548 | /* Not pointing at a valid entry - set *pSize to 0. */ |
| 2549 | *pSize = 0; |
| 2550 | }else{ |
drh | 8605761 | 2007-06-26 01:04:48 +0000 | [diff] [blame] | 2551 | getCellInfo(pCur); |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 2552 | *pSize = pCur->info.nData; |
| 2553 | } |
drh | 0e1c19e | 2004-05-11 00:58:56 +0000 | [diff] [blame] | 2554 | } |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 2555 | return rc; |
drh | 0e1c19e | 2004-05-11 00:58:56 +0000 | [diff] [blame] | 2556 | } |
| 2557 | |
| 2558 | /* |
danielk1977 | d0441796 | 2007-05-02 13:16:30 +0000 | [diff] [blame] | 2559 | ** Given the page number of an overflow page in the database (parameter |
| 2560 | ** ovfl), this function finds the page number of the next page in the |
| 2561 | ** linked list of overflow pages. If possible, it uses the auto-vacuum |
| 2562 | ** pointer-map data instead of reading the content of page ovfl to do so. |
| 2563 | ** |
| 2564 | ** If an error occurs an SQLite error code is returned. Otherwise: |
| 2565 | ** |
| 2566 | ** Unless pPgnoNext is NULL, the page number of the next overflow |
| 2567 | ** page in the linked list is written to *pPgnoNext. If page ovfl |
| 2568 | ** is the last page in it's linked list, *pPgnoNext is set to zero. |
| 2569 | ** |
| 2570 | ** If ppPage is not NULL, *ppPage is set to the MemPage* handle |
| 2571 | ** for page ovfl. The underlying pager page may have been requested |
| 2572 | ** with the noContent flag set, so the page data accessable via |
| 2573 | ** this handle may not be trusted. |
| 2574 | */ |
| 2575 | static int getOverflowPage( |
| 2576 | BtShared *pBt, |
| 2577 | Pgno ovfl, /* Overflow page */ |
| 2578 | MemPage **ppPage, /* OUT: MemPage handle */ |
| 2579 | Pgno *pPgnoNext /* OUT: Next overflow page number */ |
| 2580 | ){ |
| 2581 | Pgno next = 0; |
| 2582 | int rc; |
| 2583 | |
| 2584 | /* One of these must not be NULL. Otherwise, why call this function? */ |
| 2585 | assert(ppPage || pPgnoNext); |
| 2586 | |
| 2587 | /* If pPgnoNext is NULL, then this function is being called to obtain |
| 2588 | ** a MemPage* reference only. No page-data is required in this case. |
| 2589 | */ |
| 2590 | if( !pPgnoNext ){ |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 2591 | return sqlite3BtreeGetPage(pBt, ovfl, ppPage, 1); |
danielk1977 | d0441796 | 2007-05-02 13:16:30 +0000 | [diff] [blame] | 2592 | } |
| 2593 | |
| 2594 | #ifndef SQLITE_OMIT_AUTOVACUUM |
| 2595 | /* Try to find the next page in the overflow list using the |
| 2596 | ** autovacuum pointer-map pages. Guess that the next page in |
| 2597 | ** the overflow list is page number (ovfl+1). If that guess turns |
| 2598 | ** out to be wrong, fall back to loading the data of page |
| 2599 | ** number ovfl to determine the next page number. |
| 2600 | */ |
| 2601 | if( pBt->autoVacuum ){ |
| 2602 | Pgno pgno; |
| 2603 | Pgno iGuess = ovfl+1; |
| 2604 | u8 eType; |
| 2605 | |
| 2606 | while( PTRMAP_ISPAGE(pBt, iGuess) || iGuess==PENDING_BYTE_PAGE(pBt) ){ |
| 2607 | iGuess++; |
| 2608 | } |
| 2609 | |
danielk1977 | 20713f3 | 2007-05-03 11:43:33 +0000 | [diff] [blame] | 2610 | if( iGuess<=sqlite3PagerPagecount(pBt->pPager) ){ |
danielk1977 | d0441796 | 2007-05-02 13:16:30 +0000 | [diff] [blame] | 2611 | rc = ptrmapGet(pBt, iGuess, &eType, &pgno); |
| 2612 | if( rc!=SQLITE_OK ){ |
| 2613 | return rc; |
| 2614 | } |
| 2615 | if( eType==PTRMAP_OVERFLOW2 && pgno==ovfl ){ |
| 2616 | next = iGuess; |
| 2617 | } |
| 2618 | } |
| 2619 | } |
| 2620 | #endif |
| 2621 | |
| 2622 | if( next==0 || ppPage ){ |
| 2623 | MemPage *pPage = 0; |
| 2624 | |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 2625 | rc = sqlite3BtreeGetPage(pBt, ovfl, &pPage, next!=0); |
danielk1977 | d0441796 | 2007-05-02 13:16:30 +0000 | [diff] [blame] | 2626 | assert(rc==SQLITE_OK || pPage==0); |
| 2627 | if( next==0 && rc==SQLITE_OK ){ |
| 2628 | next = get4byte(pPage->aData); |
| 2629 | } |
| 2630 | |
| 2631 | if( ppPage ){ |
| 2632 | *ppPage = pPage; |
| 2633 | }else{ |
| 2634 | releasePage(pPage); |
| 2635 | } |
| 2636 | } |
| 2637 | *pPgnoNext = next; |
| 2638 | |
| 2639 | return rc; |
| 2640 | } |
| 2641 | |
danielk1977 | da10719 | 2007-05-04 08:32:13 +0000 | [diff] [blame] | 2642 | /* |
| 2643 | ** Copy data from a buffer to a page, or from a page to a buffer. |
| 2644 | ** |
| 2645 | ** pPayload is a pointer to data stored on database page pDbPage. |
| 2646 | ** If argument eOp is false, then nByte bytes of data are copied |
| 2647 | ** from pPayload to the buffer pointed at by pBuf. If eOp is true, |
| 2648 | ** then sqlite3PagerWrite() is called on pDbPage and nByte bytes |
| 2649 | ** of data are copied from the buffer pBuf to pPayload. |
| 2650 | ** |
| 2651 | ** SQLITE_OK is returned on success, otherwise an error code. |
| 2652 | */ |
| 2653 | static int copyPayload( |
| 2654 | void *pPayload, /* Pointer to page data */ |
| 2655 | void *pBuf, /* Pointer to buffer */ |
| 2656 | int nByte, /* Number of bytes to copy */ |
| 2657 | int eOp, /* 0 -> copy from page, 1 -> copy to page */ |
| 2658 | DbPage *pDbPage /* Page containing pPayload */ |
| 2659 | ){ |
| 2660 | if( eOp ){ |
| 2661 | /* Copy data from buffer to page (a write operation) */ |
| 2662 | int rc = sqlite3PagerWrite(pDbPage); |
| 2663 | if( rc!=SQLITE_OK ){ |
| 2664 | return rc; |
| 2665 | } |
| 2666 | memcpy(pPayload, pBuf, nByte); |
| 2667 | }else{ |
| 2668 | /* Copy data from page to buffer (a read operation) */ |
| 2669 | memcpy(pBuf, pPayload, nByte); |
| 2670 | } |
| 2671 | return SQLITE_OK; |
| 2672 | } |
danielk1977 | d0441796 | 2007-05-02 13:16:30 +0000 | [diff] [blame] | 2673 | |
| 2674 | /* |
danielk1977 | 9f8d640 | 2007-05-02 17:48:45 +0000 | [diff] [blame] | 2675 | ** This function is used to read or overwrite payload information |
| 2676 | ** for the entry that the pCur cursor is pointing to. If the eOp |
| 2677 | ** parameter is 0, this is a read operation (data copied into |
| 2678 | ** buffer pBuf). If it is non-zero, a write (data copied from |
| 2679 | ** buffer pBuf). |
| 2680 | ** |
| 2681 | ** A total of "amt" bytes are read or written beginning at "offset". |
| 2682 | ** Data is read to or from the buffer pBuf. |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 2683 | ** |
| 2684 | ** This routine does not make a distinction between key and data. |
danielk1977 | 9f8d640 | 2007-05-02 17:48:45 +0000 | [diff] [blame] | 2685 | ** It just reads or writes bytes from the payload area. Data might |
| 2686 | ** appear on the main page or be scattered out on multiple overflow |
| 2687 | ** pages. |
danielk1977 | da10719 | 2007-05-04 08:32:13 +0000 | [diff] [blame] | 2688 | ** |
danielk1977 | dcbb5d3 | 2007-05-04 18:36:44 +0000 | [diff] [blame] | 2689 | ** If the BtCursor.isIncrblobHandle flag is set, and the current |
danielk1977 | da10719 | 2007-05-04 08:32:13 +0000 | [diff] [blame] | 2690 | ** cursor entry uses one or more overflow pages, this function |
| 2691 | ** allocates space for and lazily popluates the overflow page-list |
| 2692 | ** cache array (BtCursor.aOverflow). Subsequent calls use this |
| 2693 | ** cache to make seeking to the supplied offset more efficient. |
| 2694 | ** |
| 2695 | ** Once an overflow page-list cache has been allocated, it may be |
| 2696 | ** invalidated if some other cursor writes to the same table, or if |
| 2697 | ** the cursor is moved to a different row. Additionally, in auto-vacuum |
| 2698 | ** mode, the following events may invalidate an overflow page-list cache. |
| 2699 | ** |
| 2700 | ** * An incremental vacuum, |
| 2701 | ** * A commit in auto_vacuum="full" mode, |
| 2702 | ** * Creating a table (may require moving an overflow page). |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 2703 | */ |
danielk1977 | 9f8d640 | 2007-05-02 17:48:45 +0000 | [diff] [blame] | 2704 | static int accessPayload( |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2705 | BtCursor *pCur, /* Cursor pointing to entry to read from */ |
| 2706 | int offset, /* Begin reading this far into payload */ |
| 2707 | int amt, /* Read this many bytes */ |
| 2708 | unsigned char *pBuf, /* Write the bytes into this buffer */ |
danielk1977 | 9f8d640 | 2007-05-02 17:48:45 +0000 | [diff] [blame] | 2709 | int skipKey, /* offset begins at data if this is true */ |
| 2710 | int eOp /* zero to read. non-zero to write. */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2711 | ){ |
| 2712 | unsigned char *aPayload; |
danielk1977 | da10719 | 2007-05-04 08:32:13 +0000 | [diff] [blame] | 2713 | int rc = SQLITE_OK; |
drh | fa1a98a | 2004-05-14 19:08:17 +0000 | [diff] [blame] | 2714 | u32 nKey; |
danielk1977 | 2dec970 | 2007-05-02 16:48:37 +0000 | [diff] [blame] | 2715 | int iIdx = 0; |
danielk1977 | da10719 | 2007-05-04 08:32:13 +0000 | [diff] [blame] | 2716 | MemPage *pPage = pCur->pPage; /* Btree page of current cursor entry */ |
| 2717 | BtShared *pBt = pCur->pBtree->pBt; /* Btree this cursor belongs to */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2718 | |
danielk1977 | da10719 | 2007-05-04 08:32:13 +0000 | [diff] [blame] | 2719 | assert( pPage ); |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 2720 | assert( pCur->eState==CURSOR_VALID ); |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2721 | assert( pCur->idx>=0 && pCur->idx<pPage->nCell ); |
danielk1977 | da10719 | 2007-05-04 08:32:13 +0000 | [diff] [blame] | 2722 | assert( offset>=0 ); |
| 2723 | |
drh | 8605761 | 2007-06-26 01:04:48 +0000 | [diff] [blame] | 2724 | getCellInfo(pCur); |
drh | 366fda6 | 2006-01-13 02:35:09 +0000 | [diff] [blame] | 2725 | aPayload = pCur->info.pCell + pCur->info.nHeader; |
danielk1977 | da10719 | 2007-05-04 08:32:13 +0000 | [diff] [blame] | 2726 | nKey = (pPage->intKey ? 0 : pCur->info.nKey); |
| 2727 | |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2728 | if( skipKey ){ |
drh | fa1a98a | 2004-05-14 19:08:17 +0000 | [diff] [blame] | 2729 | offset += nKey; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2730 | } |
drh | fa1a98a | 2004-05-14 19:08:17 +0000 | [diff] [blame] | 2731 | if( offset+amt > nKey+pCur->info.nData ){ |
danielk1977 | da10719 | 2007-05-04 08:32:13 +0000 | [diff] [blame] | 2732 | /* 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] | 2733 | return SQLITE_ERROR; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2734 | } |
danielk1977 | da10719 | 2007-05-04 08:32:13 +0000 | [diff] [blame] | 2735 | |
| 2736 | /* Check if data must be read/written to/from the btree page itself. */ |
drh | fa1a98a | 2004-05-14 19:08:17 +0000 | [diff] [blame] | 2737 | if( offset<pCur->info.nLocal ){ |
drh | 2af926b | 2001-05-15 00:39:25 +0000 | [diff] [blame] | 2738 | int a = amt; |
drh | fa1a98a | 2004-05-14 19:08:17 +0000 | [diff] [blame] | 2739 | if( a+offset>pCur->info.nLocal ){ |
| 2740 | a = pCur->info.nLocal - offset; |
drh | 2af926b | 2001-05-15 00:39:25 +0000 | [diff] [blame] | 2741 | } |
danielk1977 | da10719 | 2007-05-04 08:32:13 +0000 | [diff] [blame] | 2742 | rc = copyPayload(&aPayload[offset], pBuf, a, eOp, pPage->pDbPage); |
drh | 2aa679f | 2001-06-25 02:11:07 +0000 | [diff] [blame] | 2743 | offset = 0; |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 2744 | pBuf += a; |
drh | 2af926b | 2001-05-15 00:39:25 +0000 | [diff] [blame] | 2745 | amt -= a; |
drh | dd79342 | 2001-06-28 01:54:48 +0000 | [diff] [blame] | 2746 | }else{ |
drh | fa1a98a | 2004-05-14 19:08:17 +0000 | [diff] [blame] | 2747 | offset -= pCur->info.nLocal; |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 2748 | } |
danielk1977 | da10719 | 2007-05-04 08:32:13 +0000 | [diff] [blame] | 2749 | |
| 2750 | if( rc==SQLITE_OK && amt>0 ){ |
| 2751 | const int ovflSize = pBt->usableSize - 4; /* Bytes content per ovfl page */ |
| 2752 | Pgno nextPage; |
| 2753 | |
drh | fa1a98a | 2004-05-14 19:08:17 +0000 | [diff] [blame] | 2754 | nextPage = get4byte(&aPayload[pCur->info.nLocal]); |
danielk1977 | da10719 | 2007-05-04 08:32:13 +0000 | [diff] [blame] | 2755 | |
danielk1977 | 2dec970 | 2007-05-02 16:48:37 +0000 | [diff] [blame] | 2756 | #ifndef SQLITE_OMIT_INCRBLOB |
danielk1977 | dcbb5d3 | 2007-05-04 18:36:44 +0000 | [diff] [blame] | 2757 | /* If the isIncrblobHandle flag is set and the BtCursor.aOverflow[] |
danielk1977 | da10719 | 2007-05-04 08:32:13 +0000 | [diff] [blame] | 2758 | ** has not been allocated, allocate it now. The array is sized at |
| 2759 | ** one entry for each overflow page in the overflow chain. The |
| 2760 | ** page number of the first overflow page is stored in aOverflow[0], |
| 2761 | ** etc. A value of 0 in the aOverflow[] array means "not yet known" |
| 2762 | ** (the cache is lazily populated). |
| 2763 | */ |
danielk1977 | dcbb5d3 | 2007-05-04 18:36:44 +0000 | [diff] [blame] | 2764 | if( pCur->isIncrblobHandle && !pCur->aOverflow ){ |
danielk1977 | 2dec970 | 2007-05-02 16:48:37 +0000 | [diff] [blame] | 2765 | int nOvfl = (pCur->info.nPayload-pCur->info.nLocal+ovflSize-1)/ovflSize; |
| 2766 | pCur->aOverflow = (Pgno *)sqliteMalloc(sizeof(Pgno)*nOvfl); |
| 2767 | if( nOvfl && !pCur->aOverflow ){ |
danielk1977 | da10719 | 2007-05-04 08:32:13 +0000 | [diff] [blame] | 2768 | rc = SQLITE_NOMEM; |
danielk1977 | 2dec970 | 2007-05-02 16:48:37 +0000 | [diff] [blame] | 2769 | } |
| 2770 | } |
danielk1977 | da10719 | 2007-05-04 08:32:13 +0000 | [diff] [blame] | 2771 | |
| 2772 | /* If the overflow page-list cache has been allocated and the |
| 2773 | ** entry for the first required overflow page is valid, skip |
| 2774 | ** directly to it. |
| 2775 | */ |
danielk1977 | 2dec970 | 2007-05-02 16:48:37 +0000 | [diff] [blame] | 2776 | if( pCur->aOverflow && pCur->aOverflow[offset/ovflSize] ){ |
| 2777 | iIdx = (offset/ovflSize); |
| 2778 | nextPage = pCur->aOverflow[iIdx]; |
| 2779 | offset = (offset%ovflSize); |
| 2780 | } |
| 2781 | #endif |
danielk1977 | da10719 | 2007-05-04 08:32:13 +0000 | [diff] [blame] | 2782 | |
| 2783 | for( ; rc==SQLITE_OK && amt>0 && nextPage; iIdx++){ |
| 2784 | |
| 2785 | #ifndef SQLITE_OMIT_INCRBLOB |
| 2786 | /* If required, populate the overflow page-list cache. */ |
| 2787 | if( pCur->aOverflow ){ |
| 2788 | assert(!pCur->aOverflow[iIdx] || pCur->aOverflow[iIdx]==nextPage); |
| 2789 | pCur->aOverflow[iIdx] = nextPage; |
| 2790 | } |
| 2791 | #endif |
| 2792 | |
danielk1977 | d0441796 | 2007-05-02 13:16:30 +0000 | [diff] [blame] | 2793 | if( offset>=ovflSize ){ |
| 2794 | /* The only reason to read this page is to obtain the page |
danielk1977 | da10719 | 2007-05-04 08:32:13 +0000 | [diff] [blame] | 2795 | ** number for the next page in the overflow chain. The page |
drh | fd131da | 2007-08-07 17:13:03 +0000 | [diff] [blame^] | 2796 | ** data is not required. So first try to lookup the overflow |
| 2797 | ** page-list cache, if any, then fall back to the getOverflowPage() |
danielk1977 | da10719 | 2007-05-04 08:32:13 +0000 | [diff] [blame] | 2798 | ** function. |
danielk1977 | d0441796 | 2007-05-02 13:16:30 +0000 | [diff] [blame] | 2799 | */ |
danielk1977 | 2dec970 | 2007-05-02 16:48:37 +0000 | [diff] [blame] | 2800 | #ifndef SQLITE_OMIT_INCRBLOB |
danielk1977 | da10719 | 2007-05-04 08:32:13 +0000 | [diff] [blame] | 2801 | if( pCur->aOverflow && pCur->aOverflow[iIdx+1] ){ |
| 2802 | nextPage = pCur->aOverflow[iIdx+1]; |
| 2803 | } else |
danielk1977 | 2dec970 | 2007-05-02 16:48:37 +0000 | [diff] [blame] | 2804 | #endif |
danielk1977 | da10719 | 2007-05-04 08:32:13 +0000 | [diff] [blame] | 2805 | rc = getOverflowPage(pBt, nextPage, 0, &nextPage); |
danielk1977 | da10719 | 2007-05-04 08:32:13 +0000 | [diff] [blame] | 2806 | offset -= ovflSize; |
danielk1977 | d0441796 | 2007-05-02 13:16:30 +0000 | [diff] [blame] | 2807 | }else{ |
danielk1977 | 9f8d640 | 2007-05-02 17:48:45 +0000 | [diff] [blame] | 2808 | /* Need to read this page properly. It contains some of the |
| 2809 | ** range of data that is being read (eOp==0) or written (eOp!=0). |
danielk1977 | d0441796 | 2007-05-02 13:16:30 +0000 | [diff] [blame] | 2810 | */ |
| 2811 | DbPage *pDbPage; |
danielk1977 | cfe9a69 | 2004-06-16 12:00:29 +0000 | [diff] [blame] | 2812 | int a = amt; |
danielk1977 | d0441796 | 2007-05-02 13:16:30 +0000 | [diff] [blame] | 2813 | rc = sqlite3PagerGet(pBt->pPager, nextPage, &pDbPage); |
danielk1977 | da10719 | 2007-05-04 08:32:13 +0000 | [diff] [blame] | 2814 | if( rc==SQLITE_OK ){ |
| 2815 | aPayload = sqlite3PagerGetData(pDbPage); |
| 2816 | nextPage = get4byte(aPayload); |
| 2817 | if( a + offset > ovflSize ){ |
| 2818 | a = ovflSize - offset; |
danielk1977 | 9f8d640 | 2007-05-02 17:48:45 +0000 | [diff] [blame] | 2819 | } |
danielk1977 | da10719 | 2007-05-04 08:32:13 +0000 | [diff] [blame] | 2820 | rc = copyPayload(&aPayload[offset+4], pBuf, a, eOp, pDbPage); |
| 2821 | sqlite3PagerUnref(pDbPage); |
| 2822 | offset = 0; |
| 2823 | amt -= a; |
| 2824 | pBuf += a; |
danielk1977 | 9f8d640 | 2007-05-02 17:48:45 +0000 | [diff] [blame] | 2825 | } |
danielk1977 | cfe9a69 | 2004-06-16 12:00:29 +0000 | [diff] [blame] | 2826 | } |
drh | 2af926b | 2001-05-15 00:39:25 +0000 | [diff] [blame] | 2827 | } |
drh | 2af926b | 2001-05-15 00:39:25 +0000 | [diff] [blame] | 2828 | } |
danielk1977 | cfe9a69 | 2004-06-16 12:00:29 +0000 | [diff] [blame] | 2829 | |
danielk1977 | da10719 | 2007-05-04 08:32:13 +0000 | [diff] [blame] | 2830 | if( rc==SQLITE_OK && amt>0 ){ |
drh | 4928570 | 2005-09-17 15:20:26 +0000 | [diff] [blame] | 2831 | return SQLITE_CORRUPT_BKPT; |
drh | a7fcb05 | 2001-12-14 15:09:55 +0000 | [diff] [blame] | 2832 | } |
danielk1977 | da10719 | 2007-05-04 08:32:13 +0000 | [diff] [blame] | 2833 | return rc; |
drh | 2af926b | 2001-05-15 00:39:25 +0000 | [diff] [blame] | 2834 | } |
| 2835 | |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 2836 | /* |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2837 | ** Read part of the key associated with cursor pCur. Exactly |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 2838 | ** "amt" bytes will be transfered into pBuf[]. The transfer |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2839 | ** begins at "offset". |
drh | 8c1238a | 2003-01-02 14:43:55 +0000 | [diff] [blame] | 2840 | ** |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2841 | ** Return SQLITE_OK on success or an error code if anything goes |
| 2842 | ** wrong. An error is returned if "offset+amt" is larger than |
| 2843 | ** the available payload. |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 2844 | */ |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 2845 | int sqlite3BtreeKey(BtCursor *pCur, u32 offset, u32 amt, void *pBuf){ |
drh | bf700f3 | 2007-03-31 02:36:44 +0000 | [diff] [blame] | 2846 | int rc = restoreOrClearCursorPosition(pCur); |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 2847 | if( rc==SQLITE_OK ){ |
| 2848 | assert( pCur->eState==CURSOR_VALID ); |
| 2849 | assert( pCur->pPage!=0 ); |
| 2850 | if( pCur->pPage->intKey ){ |
| 2851 | return SQLITE_CORRUPT_BKPT; |
| 2852 | } |
| 2853 | assert( pCur->pPage->intKey==0 ); |
| 2854 | assert( pCur->idx>=0 && pCur->idx<pCur->pPage->nCell ); |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 2855 | rc = accessPayload(pCur, offset, amt, (unsigned char*)pBuf, 0, 0); |
drh | 6575a22 | 2005-03-10 17:06:34 +0000 | [diff] [blame] | 2856 | } |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 2857 | return rc; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2858 | } |
| 2859 | |
| 2860 | /* |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2861 | ** Read part of the data associated with cursor pCur. Exactly |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 2862 | ** "amt" bytes will be transfered into pBuf[]. The transfer |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2863 | ** begins at "offset". |
| 2864 | ** |
| 2865 | ** Return SQLITE_OK on success or an error code if anything goes |
| 2866 | ** wrong. An error is returned if "offset+amt" is larger than |
| 2867 | ** the available payload. |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 2868 | */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2869 | int sqlite3BtreeData(BtCursor *pCur, u32 offset, u32 amt, void *pBuf){ |
drh | bf700f3 | 2007-03-31 02:36:44 +0000 | [diff] [blame] | 2870 | int rc = restoreOrClearCursorPosition(pCur); |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 2871 | if( rc==SQLITE_OK ){ |
| 2872 | assert( pCur->eState==CURSOR_VALID ); |
| 2873 | assert( pCur->pPage!=0 ); |
| 2874 | assert( pCur->idx>=0 && pCur->idx<pCur->pPage->nCell ); |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 2875 | rc = accessPayload(pCur, offset, amt, pBuf, 1, 0); |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 2876 | } |
| 2877 | return rc; |
drh | 2af926b | 2001-05-15 00:39:25 +0000 | [diff] [blame] | 2878 | } |
| 2879 | |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 2880 | /* |
drh | 0e1c19e | 2004-05-11 00:58:56 +0000 | [diff] [blame] | 2881 | ** Return a pointer to payload information from the entry that the |
| 2882 | ** pCur cursor is pointing to. The pointer is to the beginning of |
| 2883 | ** 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] | 2884 | ** skipKey==1. The number of bytes of available key/data is written |
| 2885 | ** into *pAmt. If *pAmt==0, then the value returned will not be |
| 2886 | ** a valid pointer. |
drh | 0e1c19e | 2004-05-11 00:58:56 +0000 | [diff] [blame] | 2887 | ** |
| 2888 | ** This routine is an optimization. It is common for the entire key |
| 2889 | ** and data to fit on the local page and for there to be no overflow |
| 2890 | ** pages. When that is so, this routine can be used to access the |
| 2891 | ** 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] | 2892 | ** onto overflow pages, then accessPayload() must be used to reassembly |
drh | 0e1c19e | 2004-05-11 00:58:56 +0000 | [diff] [blame] | 2893 | ** the key/data and copy it into a preallocated buffer. |
| 2894 | ** |
| 2895 | ** The pointer returned by this routine looks directly into the cached |
| 2896 | ** page of the database. The data might change or move the next time |
| 2897 | ** any btree routine is called. |
| 2898 | */ |
| 2899 | static const unsigned char *fetchPayload( |
| 2900 | BtCursor *pCur, /* Cursor pointing to entry to read from */ |
drh | e51c44f | 2004-05-30 20:46:09 +0000 | [diff] [blame] | 2901 | int *pAmt, /* Write the number of available bytes here */ |
drh | 0e1c19e | 2004-05-11 00:58:56 +0000 | [diff] [blame] | 2902 | int skipKey /* read beginning at data if this is true */ |
| 2903 | ){ |
| 2904 | unsigned char *aPayload; |
| 2905 | MemPage *pPage; |
drh | fa1a98a | 2004-05-14 19:08:17 +0000 | [diff] [blame] | 2906 | u32 nKey; |
| 2907 | int nLocal; |
drh | 0e1c19e | 2004-05-11 00:58:56 +0000 | [diff] [blame] | 2908 | |
| 2909 | assert( pCur!=0 && pCur->pPage!=0 ); |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 2910 | assert( pCur->eState==CURSOR_VALID ); |
drh | 0e1c19e | 2004-05-11 00:58:56 +0000 | [diff] [blame] | 2911 | pPage = pCur->pPage; |
drh | 0e1c19e | 2004-05-11 00:58:56 +0000 | [diff] [blame] | 2912 | assert( pCur->idx>=0 && pCur->idx<pPage->nCell ); |
drh | 8605761 | 2007-06-26 01:04:48 +0000 | [diff] [blame] | 2913 | getCellInfo(pCur); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 2914 | aPayload = pCur->info.pCell; |
drh | fa1a98a | 2004-05-14 19:08:17 +0000 | [diff] [blame] | 2915 | aPayload += pCur->info.nHeader; |
drh | 0e1c19e | 2004-05-11 00:58:56 +0000 | [diff] [blame] | 2916 | if( pPage->intKey ){ |
drh | fa1a98a | 2004-05-14 19:08:17 +0000 | [diff] [blame] | 2917 | nKey = 0; |
| 2918 | }else{ |
| 2919 | nKey = pCur->info.nKey; |
drh | 0e1c19e | 2004-05-11 00:58:56 +0000 | [diff] [blame] | 2920 | } |
drh | 0e1c19e | 2004-05-11 00:58:56 +0000 | [diff] [blame] | 2921 | if( skipKey ){ |
drh | fa1a98a | 2004-05-14 19:08:17 +0000 | [diff] [blame] | 2922 | aPayload += nKey; |
| 2923 | nLocal = pCur->info.nLocal - nKey; |
drh | 0e1c19e | 2004-05-11 00:58:56 +0000 | [diff] [blame] | 2924 | }else{ |
drh | fa1a98a | 2004-05-14 19:08:17 +0000 | [diff] [blame] | 2925 | nLocal = pCur->info.nLocal; |
drh | e51c44f | 2004-05-30 20:46:09 +0000 | [diff] [blame] | 2926 | if( nLocal>nKey ){ |
| 2927 | nLocal = nKey; |
| 2928 | } |
drh | 0e1c19e | 2004-05-11 00:58:56 +0000 | [diff] [blame] | 2929 | } |
drh | e51c44f | 2004-05-30 20:46:09 +0000 | [diff] [blame] | 2930 | *pAmt = nLocal; |
drh | 0e1c19e | 2004-05-11 00:58:56 +0000 | [diff] [blame] | 2931 | return aPayload; |
| 2932 | } |
| 2933 | |
| 2934 | |
| 2935 | /* |
drh | e51c44f | 2004-05-30 20:46:09 +0000 | [diff] [blame] | 2936 | ** For the entry that cursor pCur is point to, return as |
| 2937 | ** many bytes of the key or data as are available on the local |
| 2938 | ** b-tree page. Write the number of available bytes into *pAmt. |
drh | 0e1c19e | 2004-05-11 00:58:56 +0000 | [diff] [blame] | 2939 | ** |
| 2940 | ** The pointer returned is ephemeral. The key/data may move |
| 2941 | ** or be destroyed on the next call to any Btree routine. |
| 2942 | ** |
| 2943 | ** These routines is used to get quick access to key and data |
| 2944 | ** in the common case where no overflow pages are used. |
drh | 0e1c19e | 2004-05-11 00:58:56 +0000 | [diff] [blame] | 2945 | */ |
drh | e51c44f | 2004-05-30 20:46:09 +0000 | [diff] [blame] | 2946 | const void *sqlite3BtreeKeyFetch(BtCursor *pCur, int *pAmt){ |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 2947 | if( pCur->eState==CURSOR_VALID ){ |
| 2948 | return (const void*)fetchPayload(pCur, pAmt, 0); |
| 2949 | } |
| 2950 | return 0; |
drh | 0e1c19e | 2004-05-11 00:58:56 +0000 | [diff] [blame] | 2951 | } |
drh | e51c44f | 2004-05-30 20:46:09 +0000 | [diff] [blame] | 2952 | const void *sqlite3BtreeDataFetch(BtCursor *pCur, int *pAmt){ |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 2953 | if( pCur->eState==CURSOR_VALID ){ |
| 2954 | return (const void*)fetchPayload(pCur, pAmt, 1); |
| 2955 | } |
| 2956 | return 0; |
drh | 0e1c19e | 2004-05-11 00:58:56 +0000 | [diff] [blame] | 2957 | } |
| 2958 | |
| 2959 | |
| 2960 | /* |
drh | 8178a75 | 2003-01-05 21:41:40 +0000 | [diff] [blame] | 2961 | ** 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] | 2962 | ** page number of the child page to move to. |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 2963 | */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2964 | static int moveToChild(BtCursor *pCur, u32 newPgno){ |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 2965 | int rc; |
| 2966 | MemPage *pNewPage; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2967 | MemPage *pOldPage; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2968 | BtShared *pBt = pCur->pBtree->pBt; |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 2969 | |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 2970 | assert( pCur->eState==CURSOR_VALID ); |
drh | de64713 | 2004-05-07 17:57:49 +0000 | [diff] [blame] | 2971 | rc = getAndInitPage(pBt, newPgno, &pNewPage, pCur->pPage); |
drh | 6019e16 | 2001-07-02 17:51:45 +0000 | [diff] [blame] | 2972 | if( rc ) return rc; |
drh | 428ae8c | 2003-01-04 16:48:09 +0000 | [diff] [blame] | 2973 | pNewPage->idxParent = pCur->idx; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2974 | pOldPage = pCur->pPage; |
| 2975 | pOldPage->idxShift = 0; |
| 2976 | releasePage(pOldPage); |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 2977 | pCur->pPage = pNewPage; |
| 2978 | pCur->idx = 0; |
drh | 271efa5 | 2004-05-30 19:19:05 +0000 | [diff] [blame] | 2979 | pCur->info.nSize = 0; |
drh | 4be295b | 2003-12-16 03:44:47 +0000 | [diff] [blame] | 2980 | if( pNewPage->nCell<1 ){ |
drh | 4928570 | 2005-09-17 15:20:26 +0000 | [diff] [blame] | 2981 | return SQLITE_CORRUPT_BKPT; |
drh | 4be295b | 2003-12-16 03:44:47 +0000 | [diff] [blame] | 2982 | } |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 2983 | return SQLITE_OK; |
| 2984 | } |
| 2985 | |
| 2986 | /* |
drh | 8856d6a | 2004-04-29 14:42:46 +0000 | [diff] [blame] | 2987 | ** Return true if the page is the virtual root of its table. |
| 2988 | ** |
| 2989 | ** The virtual root page is the root page for most tables. But |
| 2990 | ** for the table rooted on page 1, sometime the real root page |
| 2991 | ** is empty except for the right-pointer. In such cases the |
| 2992 | ** virtual root page is the page that the right-pointer of page |
| 2993 | ** 1 is pointing to. |
| 2994 | */ |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 2995 | int sqlite3BtreeIsRootPage(MemPage *pPage){ |
drh | 8856d6a | 2004-04-29 14:42:46 +0000 | [diff] [blame] | 2996 | MemPage *pParent = pPage->pParent; |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 2997 | if( pParent==0 ) return 1; |
| 2998 | if( pParent->pgno>1 ) return 0; |
| 2999 | if( get2byte(&pParent->aData[pParent->hdrOffset+3])==0 ) return 1; |
drh | 8856d6a | 2004-04-29 14:42:46 +0000 | [diff] [blame] | 3000 | return 0; |
| 3001 | } |
| 3002 | |
| 3003 | /* |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 3004 | ** Move the cursor up to the parent page. |
| 3005 | ** |
| 3006 | ** pCur->idx is set to the cell index that contains the pointer |
| 3007 | ** to the page we are coming from. If we are coming from the |
| 3008 | ** right-most child page then pCur->idx is set to one more than |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 3009 | ** the largest cell index. |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 3010 | */ |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 3011 | void sqlite3BtreeMoveToParent(BtCursor *pCur){ |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 3012 | MemPage *pParent; |
drh | 8178a75 | 2003-01-05 21:41:40 +0000 | [diff] [blame] | 3013 | MemPage *pPage; |
drh | 428ae8c | 2003-01-04 16:48:09 +0000 | [diff] [blame] | 3014 | int idxParent; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3015 | |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 3016 | assert( pCur->eState==CURSOR_VALID ); |
drh | 8178a75 | 2003-01-05 21:41:40 +0000 | [diff] [blame] | 3017 | pPage = pCur->pPage; |
| 3018 | assert( pPage!=0 ); |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 3019 | assert( !sqlite3BtreeIsRootPage(pPage) ); |
drh | 8178a75 | 2003-01-05 21:41:40 +0000 | [diff] [blame] | 3020 | pParent = pPage->pParent; |
| 3021 | assert( pParent!=0 ); |
| 3022 | idxParent = pPage->idxParent; |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 3023 | sqlite3PagerRef(pParent->pDbPage); |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3024 | releasePage(pPage); |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 3025 | pCur->pPage = pParent; |
drh | 271efa5 | 2004-05-30 19:19:05 +0000 | [diff] [blame] | 3026 | pCur->info.nSize = 0; |
drh | 428ae8c | 2003-01-04 16:48:09 +0000 | [diff] [blame] | 3027 | assert( pParent->idxShift==0 ); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 3028 | pCur->idx = idxParent; |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 3029 | } |
| 3030 | |
| 3031 | /* |
| 3032 | ** Move the cursor to the root page |
| 3033 | */ |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 3034 | static int moveToRoot(BtCursor *pCur){ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3035 | MemPage *pRoot; |
drh | 777e4c4 | 2006-01-13 04:31:58 +0000 | [diff] [blame] | 3036 | int rc = SQLITE_OK; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 3037 | BtShared *pBt = pCur->pBtree->pBt; |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 3038 | |
drh | bf700f3 | 2007-03-31 02:36:44 +0000 | [diff] [blame] | 3039 | if( pCur->eState==CURSOR_REQUIRESEEK ){ |
| 3040 | clearCursorPosition(pCur); |
| 3041 | } |
drh | 777e4c4 | 2006-01-13 04:31:58 +0000 | [diff] [blame] | 3042 | pRoot = pCur->pPage; |
danielk1977 | 97a227c | 2006-01-20 16:32:04 +0000 | [diff] [blame] | 3043 | if( pRoot && pRoot->pgno==pCur->pgnoRoot ){ |
drh | 777e4c4 | 2006-01-13 04:31:58 +0000 | [diff] [blame] | 3044 | assert( pRoot->isInit ); |
| 3045 | }else{ |
| 3046 | if( |
| 3047 | SQLITE_OK!=(rc = getAndInitPage(pBt, pCur->pgnoRoot, &pRoot, 0)) |
| 3048 | ){ |
| 3049 | pCur->eState = CURSOR_INVALID; |
| 3050 | return rc; |
| 3051 | } |
| 3052 | releasePage(pCur->pPage); |
drh | 777e4c4 | 2006-01-13 04:31:58 +0000 | [diff] [blame] | 3053 | pCur->pPage = pRoot; |
drh | c39e000 | 2004-05-07 23:50:57 +0000 | [diff] [blame] | 3054 | } |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 3055 | pCur->idx = 0; |
drh | 271efa5 | 2004-05-30 19:19:05 +0000 | [diff] [blame] | 3056 | pCur->info.nSize = 0; |
drh | 8856d6a | 2004-04-29 14:42:46 +0000 | [diff] [blame] | 3057 | if( pRoot->nCell==0 && !pRoot->leaf ){ |
| 3058 | Pgno subpage; |
| 3059 | assert( pRoot->pgno==1 ); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 3060 | subpage = get4byte(&pRoot->aData[pRoot->hdrOffset+8]); |
drh | 8856d6a | 2004-04-29 14:42:46 +0000 | [diff] [blame] | 3061 | assert( subpage>0 ); |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 3062 | pCur->eState = CURSOR_VALID; |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 3063 | rc = moveToChild(pCur, subpage); |
drh | 8856d6a | 2004-04-29 14:42:46 +0000 | [diff] [blame] | 3064 | } |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 3065 | pCur->eState = ((pCur->pPage->nCell>0)?CURSOR_VALID:CURSOR_INVALID); |
drh | 8856d6a | 2004-04-29 14:42:46 +0000 | [diff] [blame] | 3066 | return rc; |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 3067 | } |
drh | 2af926b | 2001-05-15 00:39:25 +0000 | [diff] [blame] | 3068 | |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 3069 | /* |
| 3070 | ** Move the cursor down to the left-most leaf entry beneath the |
| 3071 | ** entry to which it is currently pointing. |
drh | 777e4c4 | 2006-01-13 04:31:58 +0000 | [diff] [blame] | 3072 | ** |
| 3073 | ** The left-most leaf is the one with the smallest key - the first |
| 3074 | ** in ascending order. |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 3075 | */ |
| 3076 | static int moveToLeftmost(BtCursor *pCur){ |
| 3077 | Pgno pgno; |
| 3078 | int rc; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3079 | MemPage *pPage; |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 3080 | |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 3081 | assert( pCur->eState==CURSOR_VALID ); |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3082 | while( !(pPage = pCur->pPage)->leaf ){ |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 3083 | assert( pCur->idx>=0 && pCur->idx<pPage->nCell ); |
danielk1977 | 1cc5ed8 | 2007-05-16 17:28:43 +0000 | [diff] [blame] | 3084 | pgno = get4byte(findCell(pPage, pCur->idx)); |
drh | 8178a75 | 2003-01-05 21:41:40 +0000 | [diff] [blame] | 3085 | rc = moveToChild(pCur, pgno); |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 3086 | if( rc ) return rc; |
| 3087 | } |
| 3088 | return SQLITE_OK; |
| 3089 | } |
| 3090 | |
drh | 2dcc9aa | 2002-12-04 13:40:25 +0000 | [diff] [blame] | 3091 | /* |
| 3092 | ** Move the cursor down to the right-most leaf entry beneath the |
| 3093 | ** page to which it is currently pointing. Notice the difference |
| 3094 | ** between moveToLeftmost() and moveToRightmost(). moveToLeftmost() |
| 3095 | ** finds the left-most entry beneath the *entry* whereas moveToRightmost() |
| 3096 | ** finds the right-most entry beneath the *page*. |
drh | 777e4c4 | 2006-01-13 04:31:58 +0000 | [diff] [blame] | 3097 | ** |
| 3098 | ** The right-most entry is the one with the largest key - the last |
| 3099 | ** key in ascending order. |
drh | 2dcc9aa | 2002-12-04 13:40:25 +0000 | [diff] [blame] | 3100 | */ |
| 3101 | static int moveToRightmost(BtCursor *pCur){ |
| 3102 | Pgno pgno; |
| 3103 | int rc; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3104 | MemPage *pPage; |
drh | 2dcc9aa | 2002-12-04 13:40:25 +0000 | [diff] [blame] | 3105 | |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 3106 | assert( pCur->eState==CURSOR_VALID ); |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3107 | while( !(pPage = pCur->pPage)->leaf ){ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 3108 | pgno = get4byte(&pPage->aData[pPage->hdrOffset+8]); |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3109 | pCur->idx = pPage->nCell; |
drh | 8178a75 | 2003-01-05 21:41:40 +0000 | [diff] [blame] | 3110 | rc = moveToChild(pCur, pgno); |
drh | 2dcc9aa | 2002-12-04 13:40:25 +0000 | [diff] [blame] | 3111 | if( rc ) return rc; |
| 3112 | } |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3113 | pCur->idx = pPage->nCell - 1; |
drh | 271efa5 | 2004-05-30 19:19:05 +0000 | [diff] [blame] | 3114 | pCur->info.nSize = 0; |
drh | 2dcc9aa | 2002-12-04 13:40:25 +0000 | [diff] [blame] | 3115 | return SQLITE_OK; |
| 3116 | } |
| 3117 | |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3118 | /* Move the cursor to the first entry in the table. Return SQLITE_OK |
| 3119 | ** on success. Set *pRes to 0 if the cursor actually points to something |
drh | 77c679c | 2002-02-19 22:43:58 +0000 | [diff] [blame] | 3120 | ** or set *pRes to 1 if the table is empty. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3121 | */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3122 | int sqlite3BtreeFirst(BtCursor *pCur, int *pRes){ |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3123 | int rc; |
| 3124 | rc = moveToRoot(pCur); |
| 3125 | if( rc ) return rc; |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 3126 | if( pCur->eState==CURSOR_INVALID ){ |
drh | c39e000 | 2004-05-07 23:50:57 +0000 | [diff] [blame] | 3127 | assert( pCur->pPage->nCell==0 ); |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3128 | *pRes = 1; |
| 3129 | return SQLITE_OK; |
| 3130 | } |
drh | c39e000 | 2004-05-07 23:50:57 +0000 | [diff] [blame] | 3131 | assert( pCur->pPage->nCell>0 ); |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3132 | *pRes = 0; |
| 3133 | rc = moveToLeftmost(pCur); |
| 3134 | return rc; |
| 3135 | } |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 3136 | |
drh | 9562b55 | 2002-02-19 15:00:07 +0000 | [diff] [blame] | 3137 | /* Move the cursor to the last entry in the table. Return SQLITE_OK |
| 3138 | ** on success. Set *pRes to 0 if the cursor actually points to something |
drh | 77c679c | 2002-02-19 22:43:58 +0000 | [diff] [blame] | 3139 | ** or set *pRes to 1 if the table is empty. |
drh | 9562b55 | 2002-02-19 15:00:07 +0000 | [diff] [blame] | 3140 | */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3141 | int sqlite3BtreeLast(BtCursor *pCur, int *pRes){ |
drh | 9562b55 | 2002-02-19 15:00:07 +0000 | [diff] [blame] | 3142 | int rc; |
drh | 9562b55 | 2002-02-19 15:00:07 +0000 | [diff] [blame] | 3143 | rc = moveToRoot(pCur); |
| 3144 | if( rc ) return rc; |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 3145 | if( CURSOR_INVALID==pCur->eState ){ |
drh | c39e000 | 2004-05-07 23:50:57 +0000 | [diff] [blame] | 3146 | assert( pCur->pPage->nCell==0 ); |
drh | 9562b55 | 2002-02-19 15:00:07 +0000 | [diff] [blame] | 3147 | *pRes = 1; |
| 3148 | return SQLITE_OK; |
| 3149 | } |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 3150 | assert( pCur->eState==CURSOR_VALID ); |
drh | 9562b55 | 2002-02-19 15:00:07 +0000 | [diff] [blame] | 3151 | *pRes = 0; |
drh | 2dcc9aa | 2002-12-04 13:40:25 +0000 | [diff] [blame] | 3152 | rc = moveToRightmost(pCur); |
drh | 9562b55 | 2002-02-19 15:00:07 +0000 | [diff] [blame] | 3153 | return rc; |
| 3154 | } |
| 3155 | |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3156 | /* Move the cursor so that it points to an entry near pKey/nKey. |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 3157 | ** Return a success code. |
| 3158 | ** |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3159 | ** For INTKEY tables, only the nKey parameter is used. pKey is |
| 3160 | ** ignored. For other tables, nKey is the number of bytes of data |
drh | 0b2f316 | 2005-12-21 18:36:45 +0000 | [diff] [blame] | 3161 | ** in pKey. The comparison function specified when the cursor was |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3162 | ** created is used to compare keys. |
| 3163 | ** |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 3164 | ** If an exact match is not found, then the cursor is always |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 3165 | ** left pointing at a leaf page which would hold the entry if it |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 3166 | ** were present. The cursor might point to an entry that comes |
| 3167 | ** before or after the key. |
| 3168 | ** |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 3169 | ** The result of comparing the key with the entry to which the |
drh | ab01f61 | 2004-05-22 02:55:23 +0000 | [diff] [blame] | 3170 | ** cursor is written to *pRes if pRes!=NULL. The meaning of |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 3171 | ** this value is as follows: |
| 3172 | ** |
| 3173 | ** *pRes<0 The cursor is left pointing at an entry that |
drh | 1a844c3 | 2002-12-04 22:29:28 +0000 | [diff] [blame] | 3174 | ** is smaller than pKey or if the table is empty |
| 3175 | ** and the cursor is therefore left point to nothing. |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 3176 | ** |
| 3177 | ** *pRes==0 The cursor is left pointing at an entry that |
| 3178 | ** exactly matches pKey. |
| 3179 | ** |
| 3180 | ** *pRes>0 The cursor is left pointing at an entry that |
drh | 7c717f7 | 2001-06-24 20:39:41 +0000 | [diff] [blame] | 3181 | ** is larger than pKey. |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 3182 | */ |
drh | e4d9081 | 2007-03-29 05:51:49 +0000 | [diff] [blame] | 3183 | int sqlite3BtreeMoveto( |
| 3184 | BtCursor *pCur, /* The cursor to be moved */ |
| 3185 | const void *pKey, /* The key content for indices. Not used by tables */ |
| 3186 | i64 nKey, /* Size of pKey. Or the key for tables */ |
| 3187 | int biasRight, /* If true, bias the search to the high end */ |
| 3188 | int *pRes /* Search result flag */ |
| 3189 | ){ |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 3190 | int rc; |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 3191 | rc = moveToRoot(pCur); |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 3192 | if( rc ) return rc; |
drh | c39e000 | 2004-05-07 23:50:57 +0000 | [diff] [blame] | 3193 | assert( pCur->pPage ); |
| 3194 | assert( pCur->pPage->isInit ); |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 3195 | if( pCur->eState==CURSOR_INVALID ){ |
drh | f328bc8 | 2004-05-10 23:29:49 +0000 | [diff] [blame] | 3196 | *pRes = -1; |
drh | c39e000 | 2004-05-07 23:50:57 +0000 | [diff] [blame] | 3197 | assert( pCur->pPage->nCell==0 ); |
| 3198 | return SQLITE_OK; |
| 3199 | } |
drh | 1468438 | 2006-11-30 13:05:29 +0000 | [diff] [blame] | 3200 | for(;;){ |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 3201 | int lwr, upr; |
| 3202 | Pgno chldPg; |
| 3203 | MemPage *pPage = pCur->pPage; |
drh | 1a844c3 | 2002-12-04 22:29:28 +0000 | [diff] [blame] | 3204 | int c = -1; /* pRes return if table is empty must be -1 */ |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 3205 | lwr = 0; |
| 3206 | upr = pPage->nCell-1; |
drh | 4eec4c1 | 2005-01-21 00:22:37 +0000 | [diff] [blame] | 3207 | if( !pPage->intKey && pKey==0 ){ |
drh | 4928570 | 2005-09-17 15:20:26 +0000 | [diff] [blame] | 3208 | return SQLITE_CORRUPT_BKPT; |
drh | 4eec4c1 | 2005-01-21 00:22:37 +0000 | [diff] [blame] | 3209 | } |
drh | e4d9081 | 2007-03-29 05:51:49 +0000 | [diff] [blame] | 3210 | if( biasRight ){ |
| 3211 | pCur->idx = upr; |
| 3212 | }else{ |
| 3213 | pCur->idx = (upr+lwr)/2; |
| 3214 | } |
drh | f1d68b3 | 2007-03-29 04:43:26 +0000 | [diff] [blame] | 3215 | if( lwr<=upr ) for(;;){ |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 3216 | void *pCellKey; |
drh | 4a1c380 | 2004-05-12 15:15:47 +0000 | [diff] [blame] | 3217 | i64 nCellKey; |
drh | 366fda6 | 2006-01-13 02:35:09 +0000 | [diff] [blame] | 3218 | pCur->info.nSize = 0; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3219 | if( pPage->intKey ){ |
drh | 777e4c4 | 2006-01-13 04:31:58 +0000 | [diff] [blame] | 3220 | u8 *pCell; |
danielk1977 | 1cc5ed8 | 2007-05-16 17:28:43 +0000 | [diff] [blame] | 3221 | pCell = findCell(pPage, pCur->idx) + pPage->childPtrSize; |
drh | d172f86 | 2006-01-12 15:01:15 +0000 | [diff] [blame] | 3222 | if( pPage->hasData ){ |
danielk1977 | bab45c6 | 2006-01-16 15:14:27 +0000 | [diff] [blame] | 3223 | u32 dummy; |
drh | d172f86 | 2006-01-12 15:01:15 +0000 | [diff] [blame] | 3224 | pCell += getVarint32(pCell, &dummy); |
| 3225 | } |
danielk1977 | bab45c6 | 2006-01-16 15:14:27 +0000 | [diff] [blame] | 3226 | getVarint(pCell, (u64 *)&nCellKey); |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3227 | if( nCellKey<nKey ){ |
| 3228 | c = -1; |
| 3229 | }else if( nCellKey>nKey ){ |
| 3230 | c = +1; |
| 3231 | }else{ |
| 3232 | c = 0; |
| 3233 | } |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3234 | }else{ |
drh | e51c44f | 2004-05-30 20:46:09 +0000 | [diff] [blame] | 3235 | int available; |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 3236 | pCellKey = (void *)fetchPayload(pCur, &available, 0); |
drh | 366fda6 | 2006-01-13 02:35:09 +0000 | [diff] [blame] | 3237 | nCellKey = pCur->info.nKey; |
drh | e51c44f | 2004-05-30 20:46:09 +0000 | [diff] [blame] | 3238 | if( available>=nCellKey ){ |
| 3239 | c = pCur->xCompare(pCur->pArg, nCellKey, pCellKey, nKey, pKey); |
| 3240 | }else{ |
| 3241 | pCellKey = sqliteMallocRaw( nCellKey ); |
| 3242 | if( pCellKey==0 ) return SQLITE_NOMEM; |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 3243 | rc = sqlite3BtreeKey(pCur, 0, nCellKey, (void *)pCellKey); |
drh | e51c44f | 2004-05-30 20:46:09 +0000 | [diff] [blame] | 3244 | c = pCur->xCompare(pCur->pArg, nCellKey, pCellKey, nKey, pKey); |
| 3245 | sqliteFree(pCellKey); |
| 3246 | if( rc ) return rc; |
| 3247 | } |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3248 | } |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 3249 | if( c==0 ){ |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 3250 | if( pPage->leafData && !pPage->leaf ){ |
drh | fc70e6f | 2004-05-12 21:11:27 +0000 | [diff] [blame] | 3251 | lwr = pCur->idx; |
| 3252 | upr = lwr - 1; |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 3253 | break; |
| 3254 | }else{ |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 3255 | if( pRes ) *pRes = 0; |
| 3256 | return SQLITE_OK; |
| 3257 | } |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 3258 | } |
| 3259 | if( c<0 ){ |
| 3260 | lwr = pCur->idx+1; |
| 3261 | }else{ |
| 3262 | upr = pCur->idx-1; |
| 3263 | } |
drh | f1d68b3 | 2007-03-29 04:43:26 +0000 | [diff] [blame] | 3264 | if( lwr>upr ){ |
| 3265 | break; |
| 3266 | } |
| 3267 | pCur->idx = (lwr+upr)/2; |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 3268 | } |
| 3269 | assert( lwr==upr+1 ); |
drh | 7aa128d | 2002-06-21 13:09:16 +0000 | [diff] [blame] | 3270 | assert( pPage->isInit ); |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3271 | if( pPage->leaf ){ |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 3272 | chldPg = 0; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3273 | }else if( lwr>=pPage->nCell ){ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 3274 | chldPg = get4byte(&pPage->aData[pPage->hdrOffset+8]); |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 3275 | }else{ |
danielk1977 | 1cc5ed8 | 2007-05-16 17:28:43 +0000 | [diff] [blame] | 3276 | chldPg = get4byte(findCell(pPage, lwr)); |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 3277 | } |
| 3278 | if( chldPg==0 ){ |
drh | c39e000 | 2004-05-07 23:50:57 +0000 | [diff] [blame] | 3279 | assert( pCur->idx>=0 && pCur->idx<pCur->pPage->nCell ); |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 3280 | if( pRes ) *pRes = c; |
| 3281 | return SQLITE_OK; |
| 3282 | } |
drh | 428ae8c | 2003-01-04 16:48:09 +0000 | [diff] [blame] | 3283 | pCur->idx = lwr; |
drh | 271efa5 | 2004-05-30 19:19:05 +0000 | [diff] [blame] | 3284 | pCur->info.nSize = 0; |
drh | 8178a75 | 2003-01-05 21:41:40 +0000 | [diff] [blame] | 3285 | rc = moveToChild(pCur, chldPg); |
drh | c39e000 | 2004-05-07 23:50:57 +0000 | [diff] [blame] | 3286 | if( rc ){ |
| 3287 | return rc; |
| 3288 | } |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 3289 | } |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 3290 | /* NOT REACHED */ |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 3291 | } |
| 3292 | |
| 3293 | /* |
drh | c39e000 | 2004-05-07 23:50:57 +0000 | [diff] [blame] | 3294 | ** Return TRUE if the cursor is not pointing at an entry of the table. |
| 3295 | ** |
| 3296 | ** TRUE will be returned after a call to sqlite3BtreeNext() moves |
| 3297 | ** past the last entry in the table or sqlite3BtreePrev() moves past |
| 3298 | ** the first entry. TRUE is also returned if the table is empty. |
| 3299 | */ |
| 3300 | int sqlite3BtreeEof(BtCursor *pCur){ |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 3301 | /* TODO: What if the cursor is in CURSOR_REQUIRESEEK but all table entries |
| 3302 | ** have been deleted? This API will need to change to return an error code |
| 3303 | ** as well as the boolean result value. |
| 3304 | */ |
| 3305 | return (CURSOR_VALID!=pCur->eState); |
drh | c39e000 | 2004-05-07 23:50:57 +0000 | [diff] [blame] | 3306 | } |
| 3307 | |
| 3308 | /* |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 3309 | ** Advance the cursor to the next entry in the database. If |
drh | 8c1238a | 2003-01-02 14:43:55 +0000 | [diff] [blame] | 3310 | ** successful then set *pRes=0. If the cursor |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 3311 | ** was already pointing to the last entry in the database before |
drh | 8c1238a | 2003-01-02 14:43:55 +0000 | [diff] [blame] | 3312 | ** this routine was called, then set *pRes=1. |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 3313 | */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3314 | int sqlite3BtreeNext(BtCursor *pCur, int *pRes){ |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 3315 | int rc; |
danielk1977 | 97a227c | 2006-01-20 16:32:04 +0000 | [diff] [blame] | 3316 | MemPage *pPage; |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 3317 | |
drh | bf700f3 | 2007-03-31 02:36:44 +0000 | [diff] [blame] | 3318 | rc = restoreOrClearCursorPosition(pCur); |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 3319 | if( rc!=SQLITE_OK ){ |
| 3320 | return rc; |
| 3321 | } |
drh | 8c4d3a6 | 2007-04-06 01:03:32 +0000 | [diff] [blame] | 3322 | assert( pRes!=0 ); |
| 3323 | pPage = pCur->pPage; |
| 3324 | if( CURSOR_INVALID==pCur->eState ){ |
| 3325 | *pRes = 1; |
| 3326 | return SQLITE_OK; |
| 3327 | } |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 3328 | if( pCur->skip>0 ){ |
| 3329 | pCur->skip = 0; |
| 3330 | *pRes = 0; |
| 3331 | return SQLITE_OK; |
| 3332 | } |
| 3333 | pCur->skip = 0; |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 3334 | |
drh | 8178a75 | 2003-01-05 21:41:40 +0000 | [diff] [blame] | 3335 | assert( pPage->isInit ); |
drh | 8178a75 | 2003-01-05 21:41:40 +0000 | [diff] [blame] | 3336 | assert( pCur->idx<pPage->nCell ); |
danielk1977 | 6a43f9b | 2004-11-16 04:57:24 +0000 | [diff] [blame] | 3337 | |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 3338 | pCur->idx++; |
drh | 271efa5 | 2004-05-30 19:19:05 +0000 | [diff] [blame] | 3339 | pCur->info.nSize = 0; |
drh | 8178a75 | 2003-01-05 21:41:40 +0000 | [diff] [blame] | 3340 | if( pCur->idx>=pPage->nCell ){ |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 3341 | if( !pPage->leaf ){ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 3342 | rc = moveToChild(pCur, get4byte(&pPage->aData[pPage->hdrOffset+8])); |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 3343 | if( rc ) return rc; |
| 3344 | rc = moveToLeftmost(pCur); |
drh | 8c1238a | 2003-01-02 14:43:55 +0000 | [diff] [blame] | 3345 | *pRes = 0; |
| 3346 | return rc; |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 3347 | } |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 3348 | do{ |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 3349 | if( sqlite3BtreeIsRootPage(pPage) ){ |
drh | 8c1238a | 2003-01-02 14:43:55 +0000 | [diff] [blame] | 3350 | *pRes = 1; |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 3351 | pCur->eState = CURSOR_INVALID; |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 3352 | return SQLITE_OK; |
| 3353 | } |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 3354 | sqlite3BtreeMoveToParent(pCur); |
drh | 8178a75 | 2003-01-05 21:41:40 +0000 | [diff] [blame] | 3355 | pPage = pCur->pPage; |
| 3356 | }while( pCur->idx>=pPage->nCell ); |
drh | 8c1238a | 2003-01-02 14:43:55 +0000 | [diff] [blame] | 3357 | *pRes = 0; |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 3358 | if( pPage->leafData ){ |
| 3359 | rc = sqlite3BtreeNext(pCur, pRes); |
| 3360 | }else{ |
| 3361 | rc = SQLITE_OK; |
| 3362 | } |
| 3363 | return rc; |
drh | 8178a75 | 2003-01-05 21:41:40 +0000 | [diff] [blame] | 3364 | } |
| 3365 | *pRes = 0; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3366 | if( pPage->leaf ){ |
drh | 8178a75 | 2003-01-05 21:41:40 +0000 | [diff] [blame] | 3367 | return SQLITE_OK; |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 3368 | } |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 3369 | rc = moveToLeftmost(pCur); |
drh | 8c1238a | 2003-01-02 14:43:55 +0000 | [diff] [blame] | 3370 | return rc; |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 3371 | } |
| 3372 | |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 3373 | /* |
drh | 2dcc9aa | 2002-12-04 13:40:25 +0000 | [diff] [blame] | 3374 | ** 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] | 3375 | ** successful then set *pRes=0. If the cursor |
drh | 2dcc9aa | 2002-12-04 13:40:25 +0000 | [diff] [blame] | 3376 | ** was already pointing to the first entry in the database before |
drh | 8178a75 | 2003-01-05 21:41:40 +0000 | [diff] [blame] | 3377 | ** this routine was called, then set *pRes=1. |
drh | 2dcc9aa | 2002-12-04 13:40:25 +0000 | [diff] [blame] | 3378 | */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3379 | int sqlite3BtreePrevious(BtCursor *pCur, int *pRes){ |
drh | 2dcc9aa | 2002-12-04 13:40:25 +0000 | [diff] [blame] | 3380 | int rc; |
| 3381 | Pgno pgno; |
drh | 8178a75 | 2003-01-05 21:41:40 +0000 | [diff] [blame] | 3382 | MemPage *pPage; |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 3383 | |
drh | bf700f3 | 2007-03-31 02:36:44 +0000 | [diff] [blame] | 3384 | rc = restoreOrClearCursorPosition(pCur); |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 3385 | if( rc!=SQLITE_OK ){ |
| 3386 | return rc; |
| 3387 | } |
drh | 8c4d3a6 | 2007-04-06 01:03:32 +0000 | [diff] [blame] | 3388 | if( CURSOR_INVALID==pCur->eState ){ |
| 3389 | *pRes = 1; |
| 3390 | return SQLITE_OK; |
| 3391 | } |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 3392 | if( pCur->skip<0 ){ |
| 3393 | pCur->skip = 0; |
| 3394 | *pRes = 0; |
| 3395 | return SQLITE_OK; |
| 3396 | } |
| 3397 | pCur->skip = 0; |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 3398 | |
drh | 8178a75 | 2003-01-05 21:41:40 +0000 | [diff] [blame] | 3399 | pPage = pCur->pPage; |
drh | 8178a75 | 2003-01-05 21:41:40 +0000 | [diff] [blame] | 3400 | assert( pPage->isInit ); |
drh | 2dcc9aa | 2002-12-04 13:40:25 +0000 | [diff] [blame] | 3401 | assert( pCur->idx>=0 ); |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 3402 | if( !pPage->leaf ){ |
danielk1977 | 1cc5ed8 | 2007-05-16 17:28:43 +0000 | [diff] [blame] | 3403 | pgno = get4byte( findCell(pPage, pCur->idx) ); |
drh | 8178a75 | 2003-01-05 21:41:40 +0000 | [diff] [blame] | 3404 | rc = moveToChild(pCur, pgno); |
drh | 2dcc9aa | 2002-12-04 13:40:25 +0000 | [diff] [blame] | 3405 | if( rc ) return rc; |
| 3406 | rc = moveToRightmost(pCur); |
| 3407 | }else{ |
| 3408 | while( pCur->idx==0 ){ |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 3409 | if( sqlite3BtreeIsRootPage(pPage) ){ |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 3410 | pCur->eState = CURSOR_INVALID; |
drh | c39e000 | 2004-05-07 23:50:57 +0000 | [diff] [blame] | 3411 | *pRes = 1; |
drh | 2dcc9aa | 2002-12-04 13:40:25 +0000 | [diff] [blame] | 3412 | return SQLITE_OK; |
| 3413 | } |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 3414 | sqlite3BtreeMoveToParent(pCur); |
drh | 8178a75 | 2003-01-05 21:41:40 +0000 | [diff] [blame] | 3415 | pPage = pCur->pPage; |
drh | 2dcc9aa | 2002-12-04 13:40:25 +0000 | [diff] [blame] | 3416 | } |
| 3417 | pCur->idx--; |
drh | 271efa5 | 2004-05-30 19:19:05 +0000 | [diff] [blame] | 3418 | pCur->info.nSize = 0; |
drh | 8237d45 | 2004-11-22 19:07:09 +0000 | [diff] [blame] | 3419 | if( pPage->leafData && !pPage->leaf ){ |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 3420 | rc = sqlite3BtreePrevious(pCur, pRes); |
| 3421 | }else{ |
| 3422 | rc = SQLITE_OK; |
| 3423 | } |
drh | 2dcc9aa | 2002-12-04 13:40:25 +0000 | [diff] [blame] | 3424 | } |
drh | 8178a75 | 2003-01-05 21:41:40 +0000 | [diff] [blame] | 3425 | *pRes = 0; |
drh | 2dcc9aa | 2002-12-04 13:40:25 +0000 | [diff] [blame] | 3426 | return rc; |
| 3427 | } |
| 3428 | |
| 3429 | /* |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 3430 | ** Allocate a new page from the database file. |
| 3431 | ** |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 3432 | ** The new page is marked as dirty. (In other words, sqlite3PagerWrite() |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 3433 | ** has already been called on the new page.) The new page has also |
| 3434 | ** been referenced and the calling routine is responsible for calling |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 3435 | ** sqlite3PagerUnref() on the new page when it is done. |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 3436 | ** |
| 3437 | ** SQLITE_OK is returned on success. Any other return value indicates |
| 3438 | ** an error. *ppPage and *pPgno are undefined in the event of an error. |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 3439 | ** Do not invoke sqlite3PagerUnref() on *ppPage if an error is returned. |
drh | bea00b9 | 2002-07-08 10:59:50 +0000 | [diff] [blame] | 3440 | ** |
drh | 199e3cf | 2002-07-18 11:01:47 +0000 | [diff] [blame] | 3441 | ** If the "nearby" parameter is not 0, then a (feeble) effort is made to |
| 3442 | ** 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] | 3443 | ** attempt to keep related pages close to each other in the database file, |
| 3444 | ** which in turn can make database access faster. |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 3445 | ** |
| 3446 | ** If the "exact" parameter is not 0, and the page-number nearby exists |
| 3447 | ** anywhere on the free-list, then it is guarenteed to be returned. This |
| 3448 | ** is only used by auto-vacuum databases when allocating a new table. |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 3449 | */ |
drh | 4f0c587 | 2007-03-26 22:05:01 +0000 | [diff] [blame] | 3450 | static int allocateBtreePage( |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 3451 | BtShared *pBt, |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 3452 | MemPage **ppPage, |
| 3453 | Pgno *pPgno, |
| 3454 | Pgno nearby, |
| 3455 | u8 exact |
| 3456 | ){ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3457 | MemPage *pPage1; |
drh | 8c42ca9 | 2001-06-22 19:15:00 +0000 | [diff] [blame] | 3458 | int rc; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3459 | int n; /* Number of pages on the freelist */ |
| 3460 | int k; /* Number of leaves on the trunk of the freelist */ |
drh | d3627af | 2006-12-18 18:34:51 +0000 | [diff] [blame] | 3461 | MemPage *pTrunk = 0; |
| 3462 | MemPage *pPrevTrunk = 0; |
drh | 30e5875 | 2002-03-02 20:41:57 +0000 | [diff] [blame] | 3463 | |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3464 | pPage1 = pBt->pPage1; |
| 3465 | n = get4byte(&pPage1->aData[36]); |
| 3466 | if( n>0 ){ |
drh | 9102529 | 2004-05-03 19:49:32 +0000 | [diff] [blame] | 3467 | /* There are pages on the freelist. Reuse one of those pages. */ |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 3468 | Pgno iTrunk; |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 3469 | u8 searchList = 0; /* If the free-list must be searched for 'nearby' */ |
| 3470 | |
| 3471 | /* If the 'exact' parameter was true and a query of the pointer-map |
| 3472 | ** shows that the page 'nearby' is somewhere on the free-list, then |
| 3473 | ** the entire-list will be searched for that page. |
| 3474 | */ |
| 3475 | #ifndef SQLITE_OMIT_AUTOVACUUM |
danielk1977 | 4ef2449 | 2007-05-23 09:52:41 +0000 | [diff] [blame] | 3476 | if( exact && nearby<=sqlite3PagerPagecount(pBt->pPager) ){ |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 3477 | u8 eType; |
| 3478 | assert( nearby>0 ); |
| 3479 | assert( pBt->autoVacuum ); |
| 3480 | rc = ptrmapGet(pBt, nearby, &eType, 0); |
| 3481 | if( rc ) return rc; |
| 3482 | if( eType==PTRMAP_FREEPAGE ){ |
| 3483 | searchList = 1; |
| 3484 | } |
| 3485 | *pPgno = nearby; |
| 3486 | } |
| 3487 | #endif |
| 3488 | |
| 3489 | /* Decrement the free-list count by 1. Set iTrunk to the index of the |
| 3490 | ** first free-list trunk page. iPrevTrunk is initially 1. |
| 3491 | */ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 3492 | rc = sqlite3PagerWrite(pPage1->pDbPage); |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 3493 | if( rc ) return rc; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3494 | put4byte(&pPage1->aData[36], n-1); |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 3495 | |
| 3496 | /* The code within this loop is run only once if the 'searchList' variable |
| 3497 | ** is not true. Otherwise, it runs once for each trunk-page on the |
| 3498 | ** free-list until the page 'nearby' is located. |
| 3499 | */ |
| 3500 | do { |
| 3501 | pPrevTrunk = pTrunk; |
| 3502 | if( pPrevTrunk ){ |
| 3503 | iTrunk = get4byte(&pPrevTrunk->aData[0]); |
drh | bea00b9 | 2002-07-08 10:59:50 +0000 | [diff] [blame] | 3504 | }else{ |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 3505 | iTrunk = get4byte(&pPage1->aData[32]); |
drh | bea00b9 | 2002-07-08 10:59:50 +0000 | [diff] [blame] | 3506 | } |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 3507 | rc = sqlite3BtreeGetPage(pBt, iTrunk, &pTrunk, 0); |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 3508 | if( rc ){ |
drh | d3627af | 2006-12-18 18:34:51 +0000 | [diff] [blame] | 3509 | pTrunk = 0; |
| 3510 | goto end_allocate_page; |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 3511 | } |
| 3512 | |
| 3513 | k = get4byte(&pTrunk->aData[4]); |
| 3514 | if( k==0 && !searchList ){ |
| 3515 | /* The trunk has no leaves and the list is not being searched. |
| 3516 | ** So extract the trunk page itself and use it as the newly |
| 3517 | ** allocated page */ |
| 3518 | assert( pPrevTrunk==0 ); |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 3519 | rc = sqlite3PagerWrite(pTrunk->pDbPage); |
drh | d3627af | 2006-12-18 18:34:51 +0000 | [diff] [blame] | 3520 | if( rc ){ |
| 3521 | goto end_allocate_page; |
| 3522 | } |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 3523 | *pPgno = iTrunk; |
| 3524 | memcpy(&pPage1->aData[32], &pTrunk->aData[0], 4); |
| 3525 | *ppPage = pTrunk; |
| 3526 | pTrunk = 0; |
| 3527 | TRACE(("ALLOCATE: %d trunk - %d free pages left\n", *pPgno, n-1)); |
| 3528 | }else if( k>pBt->usableSize/4 - 8 ){ |
| 3529 | /* Value of k is out of range. Database corruption */ |
drh | d3627af | 2006-12-18 18:34:51 +0000 | [diff] [blame] | 3530 | rc = SQLITE_CORRUPT_BKPT; |
| 3531 | goto end_allocate_page; |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 3532 | #ifndef SQLITE_OMIT_AUTOVACUUM |
| 3533 | }else if( searchList && nearby==iTrunk ){ |
| 3534 | /* The list is being searched and this trunk page is the page |
| 3535 | ** to allocate, regardless of whether it has leaves. |
| 3536 | */ |
| 3537 | assert( *pPgno==iTrunk ); |
| 3538 | *ppPage = pTrunk; |
| 3539 | searchList = 0; |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 3540 | rc = sqlite3PagerWrite(pTrunk->pDbPage); |
drh | d3627af | 2006-12-18 18:34:51 +0000 | [diff] [blame] | 3541 | if( rc ){ |
| 3542 | goto end_allocate_page; |
| 3543 | } |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 3544 | if( k==0 ){ |
| 3545 | if( !pPrevTrunk ){ |
| 3546 | memcpy(&pPage1->aData[32], &pTrunk->aData[0], 4); |
| 3547 | }else{ |
| 3548 | memcpy(&pPrevTrunk->aData[0], &pTrunk->aData[0], 4); |
| 3549 | } |
| 3550 | }else{ |
| 3551 | /* The trunk page is required by the caller but it contains |
| 3552 | ** pointers to free-list leaves. The first leaf becomes a trunk |
| 3553 | ** page in this case. |
| 3554 | */ |
| 3555 | MemPage *pNewTrunk; |
| 3556 | Pgno iNewTrunk = get4byte(&pTrunk->aData[8]); |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 3557 | rc = sqlite3BtreeGetPage(pBt, iNewTrunk, &pNewTrunk, 0); |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 3558 | if( rc!=SQLITE_OK ){ |
drh | d3627af | 2006-12-18 18:34:51 +0000 | [diff] [blame] | 3559 | goto end_allocate_page; |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 3560 | } |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 3561 | rc = sqlite3PagerWrite(pNewTrunk->pDbPage); |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 3562 | if( rc!=SQLITE_OK ){ |
| 3563 | releasePage(pNewTrunk); |
drh | d3627af | 2006-12-18 18:34:51 +0000 | [diff] [blame] | 3564 | goto end_allocate_page; |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 3565 | } |
| 3566 | memcpy(&pNewTrunk->aData[0], &pTrunk->aData[0], 4); |
| 3567 | put4byte(&pNewTrunk->aData[4], k-1); |
| 3568 | memcpy(&pNewTrunk->aData[8], &pTrunk->aData[12], (k-1)*4); |
drh | d3627af | 2006-12-18 18:34:51 +0000 | [diff] [blame] | 3569 | releasePage(pNewTrunk); |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 3570 | if( !pPrevTrunk ){ |
| 3571 | put4byte(&pPage1->aData[32], iNewTrunk); |
| 3572 | }else{ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 3573 | rc = sqlite3PagerWrite(pPrevTrunk->pDbPage); |
drh | d3627af | 2006-12-18 18:34:51 +0000 | [diff] [blame] | 3574 | if( rc ){ |
| 3575 | goto end_allocate_page; |
| 3576 | } |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 3577 | put4byte(&pPrevTrunk->aData[0], iNewTrunk); |
| 3578 | } |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 3579 | } |
| 3580 | pTrunk = 0; |
| 3581 | TRACE(("ALLOCATE: %d trunk - %d free pages left\n", *pPgno, n-1)); |
| 3582 | #endif |
| 3583 | }else{ |
| 3584 | /* Extract a leaf from the trunk */ |
| 3585 | int closest; |
| 3586 | Pgno iPage; |
| 3587 | unsigned char *aData = pTrunk->aData; |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 3588 | rc = sqlite3PagerWrite(pTrunk->pDbPage); |
drh | d3627af | 2006-12-18 18:34:51 +0000 | [diff] [blame] | 3589 | if( rc ){ |
| 3590 | goto end_allocate_page; |
| 3591 | } |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 3592 | if( nearby>0 ){ |
| 3593 | int i, dist; |
| 3594 | closest = 0; |
| 3595 | dist = get4byte(&aData[8]) - nearby; |
| 3596 | if( dist<0 ) dist = -dist; |
| 3597 | for(i=1; i<k; i++){ |
| 3598 | int d2 = get4byte(&aData[8+i*4]) - nearby; |
| 3599 | if( d2<0 ) d2 = -d2; |
| 3600 | if( d2<dist ){ |
| 3601 | closest = i; |
| 3602 | dist = d2; |
| 3603 | } |
| 3604 | } |
| 3605 | }else{ |
| 3606 | closest = 0; |
| 3607 | } |
| 3608 | |
| 3609 | iPage = get4byte(&aData[8+closest*4]); |
| 3610 | if( !searchList || iPage==nearby ){ |
| 3611 | *pPgno = iPage; |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 3612 | if( *pPgno>sqlite3PagerPagecount(pBt->pPager) ){ |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 3613 | /* Free page off the end of the file */ |
drh | 4928570 | 2005-09-17 15:20:26 +0000 | [diff] [blame] | 3614 | return SQLITE_CORRUPT_BKPT; |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 3615 | } |
| 3616 | TRACE(("ALLOCATE: %d was leaf %d of %d on trunk %d" |
| 3617 | ": %d more free pages\n", |
| 3618 | *pPgno, closest+1, k, pTrunk->pgno, n-1)); |
| 3619 | if( closest<k-1 ){ |
| 3620 | memcpy(&aData[8+closest*4], &aData[4+k*4], 4); |
| 3621 | } |
| 3622 | put4byte(&aData[4], k-1); |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 3623 | rc = sqlite3BtreeGetPage(pBt, *pPgno, ppPage, 1); |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 3624 | if( rc==SQLITE_OK ){ |
drh | 538f570 | 2007-04-13 02:14:30 +0000 | [diff] [blame] | 3625 | sqlite3PagerDontRollback((*ppPage)->pDbPage); |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 3626 | rc = sqlite3PagerWrite((*ppPage)->pDbPage); |
danielk1977 | aac0a38 | 2005-01-16 11:07:06 +0000 | [diff] [blame] | 3627 | if( rc!=SQLITE_OK ){ |
| 3628 | releasePage(*ppPage); |
| 3629 | } |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 3630 | } |
| 3631 | searchList = 0; |
| 3632 | } |
drh | ee696e2 | 2004-08-30 16:52:17 +0000 | [diff] [blame] | 3633 | } |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 3634 | releasePage(pPrevTrunk); |
drh | d3627af | 2006-12-18 18:34:51 +0000 | [diff] [blame] | 3635 | pPrevTrunk = 0; |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 3636 | }while( searchList ); |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 3637 | }else{ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3638 | /* There are no pages on the freelist, so create a new page at the |
| 3639 | ** end of the file */ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 3640 | *pPgno = sqlite3PagerPagecount(pBt->pPager) + 1; |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 3641 | |
| 3642 | #ifndef SQLITE_OMIT_AUTOVACUUM |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 3643 | if( pBt->nTrunc ){ |
| 3644 | /* An incr-vacuum has already run within this transaction. So the |
| 3645 | ** page to allocate is not from the physical end of the file, but |
| 3646 | ** at pBt->nTrunc. |
| 3647 | */ |
| 3648 | *pPgno = pBt->nTrunc+1; |
| 3649 | if( *pPgno==PENDING_BYTE_PAGE(pBt) ){ |
| 3650 | (*pPgno)++; |
| 3651 | } |
| 3652 | } |
danielk1977 | 266664d | 2006-02-10 08:24:21 +0000 | [diff] [blame] | 3653 | if( pBt->autoVacuum && PTRMAP_ISPAGE(pBt, *pPgno) ){ |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 3654 | /* If *pPgno refers to a pointer-map page, allocate two new pages |
| 3655 | ** at the end of the file instead of one. The first allocated page |
| 3656 | ** becomes a new pointer-map page, the second is used by the caller. |
| 3657 | */ |
| 3658 | TRACE(("ALLOCATE: %d from end of file (pointer-map page)\n", *pPgno)); |
danielk1977 | 599fcba | 2004-11-08 07:13:13 +0000 | [diff] [blame] | 3659 | assert( *pPgno!=PENDING_BYTE_PAGE(pBt) ); |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 3660 | (*pPgno)++; |
| 3661 | } |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 3662 | if( pBt->nTrunc ){ |
| 3663 | pBt->nTrunc = *pPgno; |
| 3664 | } |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 3665 | #endif |
| 3666 | |
danielk1977 | 599fcba | 2004-11-08 07:13:13 +0000 | [diff] [blame] | 3667 | assert( *pPgno!=PENDING_BYTE_PAGE(pBt) ); |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 3668 | rc = sqlite3BtreeGetPage(pBt, *pPgno, ppPage, 0); |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 3669 | if( rc ) return rc; |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 3670 | rc = sqlite3PagerWrite((*ppPage)->pDbPage); |
danielk1977 | aac0a38 | 2005-01-16 11:07:06 +0000 | [diff] [blame] | 3671 | if( rc!=SQLITE_OK ){ |
| 3672 | releasePage(*ppPage); |
| 3673 | } |
drh | 3a4c141 | 2004-05-09 20:40:11 +0000 | [diff] [blame] | 3674 | TRACE(("ALLOCATE: %d from end of file\n", *pPgno)); |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 3675 | } |
danielk1977 | 599fcba | 2004-11-08 07:13:13 +0000 | [diff] [blame] | 3676 | |
| 3677 | assert( *pPgno!=PENDING_BYTE_PAGE(pBt) ); |
drh | d3627af | 2006-12-18 18:34:51 +0000 | [diff] [blame] | 3678 | |
| 3679 | end_allocate_page: |
| 3680 | releasePage(pTrunk); |
| 3681 | releasePage(pPrevTrunk); |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 3682 | return rc; |
| 3683 | } |
| 3684 | |
| 3685 | /* |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3686 | ** Add a page of the database file to the freelist. |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 3687 | ** |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 3688 | ** sqlite3PagerUnref() is NOT called for pPage. |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 3689 | */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3690 | static int freePage(MemPage *pPage){ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 3691 | BtShared *pBt = pPage->pBt; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3692 | MemPage *pPage1 = pBt->pPage1; |
| 3693 | int rc, n, k; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 3694 | |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3695 | /* Prepare the page for freeing */ |
| 3696 | assert( pPage->pgno>1 ); |
| 3697 | pPage->isInit = 0; |
| 3698 | releasePage(pPage->pParent); |
| 3699 | pPage->pParent = 0; |
| 3700 | |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 3701 | /* Increment the free page count on pPage1 */ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 3702 | rc = sqlite3PagerWrite(pPage1->pDbPage); |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3703 | if( rc ) return rc; |
| 3704 | n = get4byte(&pPage1->aData[36]); |
| 3705 | put4byte(&pPage1->aData[36], n+1); |
| 3706 | |
drh | fcce93f | 2006-02-22 03:08:32 +0000 | [diff] [blame] | 3707 | #ifdef SQLITE_SECURE_DELETE |
| 3708 | /* If the SQLITE_SECURE_DELETE compile-time option is enabled, then |
| 3709 | ** always fully overwrite deleted information with zeros. |
| 3710 | */ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 3711 | rc = sqlite3PagerWrite(pPage->pDbPage); |
drh | fcce93f | 2006-02-22 03:08:32 +0000 | [diff] [blame] | 3712 | if( rc ) return rc; |
| 3713 | memset(pPage->aData, 0, pPage->pBt->pageSize); |
| 3714 | #endif |
| 3715 | |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 3716 | #ifndef SQLITE_OMIT_AUTOVACUUM |
| 3717 | /* If the database supports auto-vacuum, write an entry in the pointer-map |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 3718 | ** to indicate that the page is free. |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 3719 | */ |
| 3720 | if( pBt->autoVacuum ){ |
| 3721 | rc = ptrmapPut(pBt, pPage->pgno, PTRMAP_FREEPAGE, 0); |
danielk1977 | a64a035 | 2004-11-05 01:45:13 +0000 | [diff] [blame] | 3722 | if( rc ) return rc; |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 3723 | } |
| 3724 | #endif |
| 3725 | |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3726 | if( n==0 ){ |
| 3727 | /* This is the first free page */ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 3728 | rc = sqlite3PagerWrite(pPage->pDbPage); |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 3729 | if( rc ) return rc; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3730 | memset(pPage->aData, 0, 8); |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 3731 | put4byte(&pPage1->aData[32], pPage->pgno); |
drh | 3a4c141 | 2004-05-09 20:40:11 +0000 | [diff] [blame] | 3732 | TRACE(("FREE-PAGE: %d first\n", pPage->pgno)); |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3733 | }else{ |
| 3734 | /* Other free pages already exist. Retrive the first trunk page |
| 3735 | ** of the freelist and find out how many leaves it has. */ |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 3736 | MemPage *pTrunk; |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 3737 | rc = sqlite3BtreeGetPage(pBt, get4byte(&pPage1->aData[32]), &pTrunk, 0); |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 3738 | if( rc ) return rc; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3739 | k = get4byte(&pTrunk->aData[4]); |
drh | ee696e2 | 2004-08-30 16:52:17 +0000 | [diff] [blame] | 3740 | if( k>=pBt->usableSize/4 - 8 ){ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3741 | /* The trunk is full. Turn the page being freed into a new |
| 3742 | ** trunk page with no leaves. */ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 3743 | rc = sqlite3PagerWrite(pPage->pDbPage); |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3744 | if( rc ) return rc; |
| 3745 | put4byte(pPage->aData, pTrunk->pgno); |
| 3746 | put4byte(&pPage->aData[4], 0); |
| 3747 | put4byte(&pPage1->aData[32], pPage->pgno); |
drh | 3a4c141 | 2004-05-09 20:40:11 +0000 | [diff] [blame] | 3748 | TRACE(("FREE-PAGE: %d new trunk page replacing %d\n", |
| 3749 | pPage->pgno, pTrunk->pgno)); |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3750 | }else{ |
| 3751 | /* Add the newly freed page as a leaf on the current trunk */ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 3752 | rc = sqlite3PagerWrite(pTrunk->pDbPage); |
drh | f534544 | 2007-04-09 12:45:02 +0000 | [diff] [blame] | 3753 | if( rc==SQLITE_OK ){ |
| 3754 | put4byte(&pTrunk->aData[4], k+1); |
| 3755 | put4byte(&pTrunk->aData[8+k*4], pPage->pgno); |
drh | fcce93f | 2006-02-22 03:08:32 +0000 | [diff] [blame] | 3756 | #ifndef SQLITE_SECURE_DELETE |
drh | 538f570 | 2007-04-13 02:14:30 +0000 | [diff] [blame] | 3757 | sqlite3PagerDontWrite(pPage->pDbPage); |
drh | fcce93f | 2006-02-22 03:08:32 +0000 | [diff] [blame] | 3758 | #endif |
drh | f534544 | 2007-04-09 12:45:02 +0000 | [diff] [blame] | 3759 | } |
drh | 3a4c141 | 2004-05-09 20:40:11 +0000 | [diff] [blame] | 3760 | 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] | 3761 | } |
| 3762 | releasePage(pTrunk); |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 3763 | } |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 3764 | return rc; |
| 3765 | } |
| 3766 | |
| 3767 | /* |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3768 | ** Free any overflow pages associated with the given Cell. |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 3769 | */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3770 | static int clearCell(MemPage *pPage, unsigned char *pCell){ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 3771 | BtShared *pBt = pPage->pBt; |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 3772 | CellInfo info; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3773 | Pgno ovflPgno; |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 3774 | int rc; |
drh | 9444081 | 2007-03-06 11:42:19 +0000 | [diff] [blame] | 3775 | int nOvfl; |
| 3776 | int ovflPageSize; |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 3777 | |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 3778 | sqlite3BtreeParseCellPtr(pPage, pCell, &info); |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 3779 | if( info.iOverflow==0 ){ |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 3780 | return SQLITE_OK; /* No overflow pages. Return without doing anything */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3781 | } |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 3782 | ovflPgno = get4byte(&pCell[info.iOverflow]); |
drh | 9444081 | 2007-03-06 11:42:19 +0000 | [diff] [blame] | 3783 | ovflPageSize = pBt->usableSize - 4; |
drh | 7236583 | 2007-03-06 15:53:44 +0000 | [diff] [blame] | 3784 | nOvfl = (info.nPayload - info.nLocal + ovflPageSize - 1)/ovflPageSize; |
| 3785 | assert( ovflPgno==0 || nOvfl>0 ); |
| 3786 | while( nOvfl-- ){ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3787 | MemPage *pOvfl; |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 3788 | if( ovflPgno==0 || ovflPgno>sqlite3PagerPagecount(pBt->pPager) ){ |
drh | 4928570 | 2005-09-17 15:20:26 +0000 | [diff] [blame] | 3789 | return SQLITE_CORRUPT_BKPT; |
danielk1977 | a1cb183 | 2005-02-12 08:59:55 +0000 | [diff] [blame] | 3790 | } |
danielk1977 | 8c0a959 | 2007-04-30 16:55:00 +0000 | [diff] [blame] | 3791 | |
| 3792 | rc = getOverflowPage(pBt, ovflPgno, &pOvfl, (nOvfl==0)?0:&ovflPgno); |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 3793 | if( rc ) return rc; |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 3794 | rc = freePage(pOvfl); |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 3795 | sqlite3PagerUnref(pOvfl->pDbPage); |
danielk1977 | 6b456a2 | 2005-03-21 04:04:02 +0000 | [diff] [blame] | 3796 | if( rc ) return rc; |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 3797 | } |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 3798 | return SQLITE_OK; |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 3799 | } |
| 3800 | |
| 3801 | /* |
drh | 9102529 | 2004-05-03 19:49:32 +0000 | [diff] [blame] | 3802 | ** Create the byte sequence used to represent a cell on page pPage |
| 3803 | ** and write that byte sequence into pCell[]. Overflow pages are |
| 3804 | ** allocated and filled in as necessary. The calling procedure |
| 3805 | ** is responsible for making sure sufficient space has been allocated |
| 3806 | ** for pCell[]. |
| 3807 | ** |
| 3808 | ** Note that pCell does not necessary need to point to the pPage->aData |
| 3809 | ** area. pCell might point to some temporary storage. The cell will |
| 3810 | ** be constructed in this temporary area then copied into pPage->aData |
| 3811 | ** later. |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 3812 | */ |
| 3813 | static int fillInCell( |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3814 | MemPage *pPage, /* The page that contains the cell */ |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 3815 | unsigned char *pCell, /* Complete text of the cell */ |
drh | 4a1c380 | 2004-05-12 15:15:47 +0000 | [diff] [blame] | 3816 | const void *pKey, i64 nKey, /* The key */ |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 3817 | const void *pData,int nData, /* The data */ |
drh | b026e05 | 2007-05-02 01:34:31 +0000 | [diff] [blame] | 3818 | int nZero, /* Extra zero bytes to append to pData */ |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 3819 | int *pnSize /* Write cell size here */ |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 3820 | ){ |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 3821 | int nPayload; |
drh | 8c6fa9b | 2004-05-26 00:01:53 +0000 | [diff] [blame] | 3822 | const u8 *pSrc; |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 3823 | int nSrc, n, rc; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3824 | int spaceLeft; |
| 3825 | MemPage *pOvfl = 0; |
drh | 9b17127 | 2004-05-08 02:03:22 +0000 | [diff] [blame] | 3826 | MemPage *pToRelease = 0; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3827 | unsigned char *pPrior; |
| 3828 | unsigned char *pPayload; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 3829 | BtShared *pBt = pPage->pBt; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3830 | Pgno pgnoOvfl = 0; |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 3831 | int nHeader; |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 3832 | CellInfo info; |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 3833 | |
drh | 9102529 | 2004-05-03 19:49:32 +0000 | [diff] [blame] | 3834 | /* Fill in the header. */ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 3835 | nHeader = 0; |
drh | 9102529 | 2004-05-03 19:49:32 +0000 | [diff] [blame] | 3836 | if( !pPage->leaf ){ |
| 3837 | nHeader += 4; |
| 3838 | } |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 3839 | if( pPage->hasData ){ |
drh | b026e05 | 2007-05-02 01:34:31 +0000 | [diff] [blame] | 3840 | nHeader += putVarint(&pCell[nHeader], nData+nZero); |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 3841 | }else{ |
drh | b026e05 | 2007-05-02 01:34:31 +0000 | [diff] [blame] | 3842 | nData = nZero = 0; |
drh | 9102529 | 2004-05-03 19:49:32 +0000 | [diff] [blame] | 3843 | } |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 3844 | nHeader += putVarint(&pCell[nHeader], *(u64*)&nKey); |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 3845 | sqlite3BtreeParseCellPtr(pPage, pCell, &info); |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 3846 | assert( info.nHeader==nHeader ); |
| 3847 | assert( info.nKey==nKey ); |
drh | b026e05 | 2007-05-02 01:34:31 +0000 | [diff] [blame] | 3848 | assert( info.nData==nData+nZero ); |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 3849 | |
| 3850 | /* Fill in the payload */ |
drh | b026e05 | 2007-05-02 01:34:31 +0000 | [diff] [blame] | 3851 | nPayload = nData + nZero; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3852 | if( pPage->intKey ){ |
| 3853 | pSrc = pData; |
| 3854 | nSrc = nData; |
drh | 9102529 | 2004-05-03 19:49:32 +0000 | [diff] [blame] | 3855 | nData = 0; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3856 | }else{ |
| 3857 | nPayload += nKey; |
| 3858 | pSrc = pKey; |
| 3859 | nSrc = nKey; |
| 3860 | } |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 3861 | *pnSize = info.nSize; |
| 3862 | spaceLeft = info.nLocal; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3863 | pPayload = &pCell[nHeader]; |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 3864 | pPrior = &pCell[info.iOverflow]; |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 3865 | |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 3866 | while( nPayload>0 ){ |
| 3867 | if( spaceLeft==0 ){ |
danielk1977 | b39f70b | 2007-05-17 18:28:11 +0000 | [diff] [blame] | 3868 | int isExact = 0; |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 3869 | #ifndef SQLITE_OMIT_AUTOVACUUM |
| 3870 | Pgno pgnoPtrmap = pgnoOvfl; /* Overflow page pointer-map entry page */ |
danielk1977 | b39f70b | 2007-05-17 18:28:11 +0000 | [diff] [blame] | 3871 | if( pBt->autoVacuum ){ |
| 3872 | do{ |
| 3873 | pgnoOvfl++; |
| 3874 | } while( |
| 3875 | PTRMAP_ISPAGE(pBt, pgnoOvfl) || pgnoOvfl==PENDING_BYTE_PAGE(pBt) |
| 3876 | ); |
danielk1977 | 89a4be8 | 2007-05-23 13:34:32 +0000 | [diff] [blame] | 3877 | if( pgnoOvfl>1 ){ |
danielk1977 | b39f70b | 2007-05-17 18:28:11 +0000 | [diff] [blame] | 3878 | /* isExact = 1; */ |
| 3879 | } |
| 3880 | } |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 3881 | #endif |
danielk1977 | b39f70b | 2007-05-17 18:28:11 +0000 | [diff] [blame] | 3882 | rc = allocateBtreePage(pBt, &pOvfl, &pgnoOvfl, pgnoOvfl, isExact); |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 3883 | #ifndef SQLITE_OMIT_AUTOVACUUM |
danielk1977 | a19df67 | 2004-11-03 11:37:07 +0000 | [diff] [blame] | 3884 | /* If the database supports auto-vacuum, and the second or subsequent |
| 3885 | ** overflow page is being allocated, add an entry to the pointer-map |
danielk1977 | 4ef2449 | 2007-05-23 09:52:41 +0000 | [diff] [blame] | 3886 | ** for that page now. |
| 3887 | ** |
| 3888 | ** If this is the first overflow page, then write a partial entry |
| 3889 | ** to the pointer-map. If we write nothing to this pointer-map slot, |
| 3890 | ** then the optimistic overflow chain processing in clearCell() |
| 3891 | ** may misinterpret the uninitialised values and delete the |
| 3892 | ** wrong pages from the database. |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 3893 | */ |
danielk1977 | 4ef2449 | 2007-05-23 09:52:41 +0000 | [diff] [blame] | 3894 | if( pBt->autoVacuum && rc==SQLITE_OK ){ |
| 3895 | u8 eType = (pgnoPtrmap?PTRMAP_OVERFLOW2:PTRMAP_OVERFLOW1); |
| 3896 | rc = ptrmapPut(pBt, pgnoOvfl, eType, pgnoPtrmap); |
danielk1977 | 89a4be8 | 2007-05-23 13:34:32 +0000 | [diff] [blame] | 3897 | if( rc ){ |
| 3898 | releasePage(pOvfl); |
| 3899 | } |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 3900 | } |
| 3901 | #endif |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 3902 | if( rc ){ |
drh | 9b17127 | 2004-05-08 02:03:22 +0000 | [diff] [blame] | 3903 | releasePage(pToRelease); |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 3904 | return rc; |
| 3905 | } |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3906 | put4byte(pPrior, pgnoOvfl); |
drh | 9b17127 | 2004-05-08 02:03:22 +0000 | [diff] [blame] | 3907 | releasePage(pToRelease); |
| 3908 | pToRelease = pOvfl; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3909 | pPrior = pOvfl->aData; |
| 3910 | put4byte(pPrior, 0); |
| 3911 | pPayload = &pOvfl->aData[4]; |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 3912 | spaceLeft = pBt->usableSize - 4; |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 3913 | } |
| 3914 | n = nPayload; |
| 3915 | if( n>spaceLeft ) n = spaceLeft; |
drh | b026e05 | 2007-05-02 01:34:31 +0000 | [diff] [blame] | 3916 | if( nSrc>0 ){ |
| 3917 | if( n>nSrc ) n = nSrc; |
| 3918 | assert( pSrc ); |
| 3919 | memcpy(pPayload, pSrc, n); |
| 3920 | }else{ |
| 3921 | memset(pPayload, 0, n); |
| 3922 | } |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 3923 | nPayload -= n; |
drh | de64713 | 2004-05-07 17:57:49 +0000 | [diff] [blame] | 3924 | pPayload += n; |
drh | 9b17127 | 2004-05-08 02:03:22 +0000 | [diff] [blame] | 3925 | pSrc += n; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3926 | nSrc -= n; |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 3927 | spaceLeft -= n; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3928 | if( nSrc==0 ){ |
| 3929 | nSrc = nData; |
| 3930 | pSrc = pData; |
| 3931 | } |
drh | dd79342 | 2001-06-28 01:54:48 +0000 | [diff] [blame] | 3932 | } |
drh | 9b17127 | 2004-05-08 02:03:22 +0000 | [diff] [blame] | 3933 | releasePage(pToRelease); |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 3934 | return SQLITE_OK; |
| 3935 | } |
| 3936 | |
| 3937 | /* |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 3938 | ** Change the MemPage.pParent pointer on the page whose number is |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 3939 | ** given in the second argument so that MemPage.pParent holds the |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 3940 | ** pointer in the third argument. |
| 3941 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 3942 | static int reparentPage(BtShared *pBt, Pgno pgno, MemPage *pNewParent, int idx){ |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 3943 | MemPage *pThis; |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 3944 | DbPage *pDbPage; |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 3945 | |
drh | 43617e9 | 2006-03-06 20:55:46 +0000 | [diff] [blame] | 3946 | assert( pNewParent!=0 ); |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 3947 | if( pgno==0 ) return SQLITE_OK; |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 3948 | assert( pBt->pPager!=0 ); |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 3949 | pDbPage = sqlite3PagerLookup(pBt->pPager, pgno); |
| 3950 | if( pDbPage ){ |
| 3951 | pThis = (MemPage *)sqlite3PagerGetExtra(pDbPage); |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 3952 | if( pThis->isInit ){ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 3953 | assert( pThis->aData==(sqlite3PagerGetData(pDbPage)) ); |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 3954 | if( pThis->pParent!=pNewParent ){ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 3955 | if( pThis->pParent ) sqlite3PagerUnref(pThis->pParent->pDbPage); |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 3956 | pThis->pParent = pNewParent; |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 3957 | sqlite3PagerRef(pNewParent->pDbPage); |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 3958 | } |
| 3959 | pThis->idxParent = idx; |
drh | dd79342 | 2001-06-28 01:54:48 +0000 | [diff] [blame] | 3960 | } |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 3961 | sqlite3PagerUnref(pDbPage); |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 3962 | } |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 3963 | |
| 3964 | #ifndef SQLITE_OMIT_AUTOVACUUM |
| 3965 | if( pBt->autoVacuum ){ |
| 3966 | return ptrmapPut(pBt, pgno, PTRMAP_BTREE, pNewParent->pgno); |
| 3967 | } |
| 3968 | #endif |
| 3969 | return SQLITE_OK; |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 3970 | } |
| 3971 | |
danielk1977 | ac11ee6 | 2005-01-15 12:45:51 +0000 | [diff] [blame] | 3972 | |
| 3973 | |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 3974 | /* |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 3975 | ** Change the pParent pointer of all children of pPage to point back |
| 3976 | ** to pPage. |
| 3977 | ** |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 3978 | ** In other words, for every child of pPage, invoke reparentPage() |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3979 | ** to make sure that each child knows that pPage is its parent. |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 3980 | ** |
| 3981 | ** This routine gets called after you memcpy() one page into |
| 3982 | ** another. |
| 3983 | */ |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 3984 | static int reparentChildPages(MemPage *pPage){ |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 3985 | int i; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 3986 | BtShared *pBt = pPage->pBt; |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 3987 | int rc = SQLITE_OK; |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 3988 | |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 3989 | if( pPage->leaf ) return SQLITE_OK; |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 3990 | |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 3991 | for(i=0; i<pPage->nCell; i++){ |
danielk1977 | 1cc5ed8 | 2007-05-16 17:28:43 +0000 | [diff] [blame] | 3992 | u8 *pCell = findCell(pPage, i); |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 3993 | if( !pPage->leaf ){ |
| 3994 | rc = reparentPage(pBt, get4byte(pCell), pPage, i); |
| 3995 | if( rc!=SQLITE_OK ) return rc; |
| 3996 | } |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 3997 | } |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 3998 | if( !pPage->leaf ){ |
| 3999 | rc = reparentPage(pBt, get4byte(&pPage->aData[pPage->hdrOffset+8]), |
| 4000 | pPage, i); |
| 4001 | pPage->idxShift = 0; |
| 4002 | } |
| 4003 | return rc; |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 4004 | } |
| 4005 | |
| 4006 | /* |
| 4007 | ** Remove the i-th cell from pPage. This routine effects pPage only. |
| 4008 | ** The cell content is not freed or deallocated. It is assumed that |
| 4009 | ** the cell content has been copied someplace else. This routine just |
| 4010 | ** removes the reference to the cell from pPage. |
| 4011 | ** |
| 4012 | ** "sz" must be the number of bytes in the cell. |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 4013 | */ |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 4014 | static void dropCell(MemPage *pPage, int idx, int sz){ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4015 | int i; /* Loop counter */ |
| 4016 | int pc; /* Offset to cell content of cell being deleted */ |
| 4017 | u8 *data; /* pPage->aData */ |
| 4018 | u8 *ptr; /* Used to move bytes around within data[] */ |
| 4019 | |
drh | 8c42ca9 | 2001-06-22 19:15:00 +0000 | [diff] [blame] | 4020 | assert( idx>=0 && idx<pPage->nCell ); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4021 | assert( sz==cellSize(pPage, idx) ); |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 4022 | assert( sqlite3PagerIswriteable(pPage->pDbPage) ); |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 4023 | data = pPage->aData; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4024 | ptr = &data[pPage->cellOffset + 2*idx]; |
| 4025 | pc = get2byte(ptr); |
| 4026 | assert( pc>10 && pc+sz<=pPage->pBt->usableSize ); |
drh | de64713 | 2004-05-07 17:57:49 +0000 | [diff] [blame] | 4027 | freeSpace(pPage, pc, sz); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4028 | for(i=idx+1; i<pPage->nCell; i++, ptr+=2){ |
| 4029 | ptr[0] = ptr[2]; |
| 4030 | ptr[1] = ptr[3]; |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 4031 | } |
| 4032 | pPage->nCell--; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4033 | put2byte(&data[pPage->hdrOffset+3], pPage->nCell); |
| 4034 | pPage->nFree += 2; |
drh | 428ae8c | 2003-01-04 16:48:09 +0000 | [diff] [blame] | 4035 | pPage->idxShift = 1; |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 4036 | } |
| 4037 | |
| 4038 | /* |
| 4039 | ** Insert a new cell on pPage at cell index "i". pCell points to the |
| 4040 | ** content of the cell. |
| 4041 | ** |
| 4042 | ** 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] | 4043 | ** will not fit, then make a copy of the cell content into pTemp if |
| 4044 | ** pTemp is not null. Regardless of pTemp, allocate a new entry |
| 4045 | ** in pPage->aOvfl[] and make it point to the cell content (either |
| 4046 | ** in pTemp or the original pCell) and also record its index. |
| 4047 | ** Allocating a new entry in pPage->aCell[] implies that |
| 4048 | ** pPage->nOverflow is incremented. |
danielk1977 | a3ad5e7 | 2005-01-07 08:56:44 +0000 | [diff] [blame] | 4049 | ** |
| 4050 | ** If nSkip is non-zero, then do not copy the first nSkip bytes of the |
| 4051 | ** cell. The caller will overwrite them after this function returns. If |
drh | 4b238df | 2005-01-08 15:43:18 +0000 | [diff] [blame] | 4052 | ** 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] | 4053 | ** (but pCell+nSkip is always valid). |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 4054 | */ |
danielk1977 | e80463b | 2004-11-03 03:01:16 +0000 | [diff] [blame] | 4055 | static int insertCell( |
drh | 24cd67e | 2004-05-10 16:18:47 +0000 | [diff] [blame] | 4056 | MemPage *pPage, /* Page into which we are copying */ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4057 | int i, /* New cell becomes the i-th cell of the page */ |
| 4058 | u8 *pCell, /* Content of the new cell */ |
| 4059 | int sz, /* Bytes of content in pCell */ |
danielk1977 | a3ad5e7 | 2005-01-07 08:56:44 +0000 | [diff] [blame] | 4060 | u8 *pTemp, /* Temp storage space for pCell, if needed */ |
| 4061 | u8 nSkip /* Do not write the first nSkip bytes of the cell */ |
drh | 24cd67e | 2004-05-10 16:18:47 +0000 | [diff] [blame] | 4062 | ){ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4063 | int idx; /* Where to write new cell content in data[] */ |
| 4064 | int j; /* Loop counter */ |
| 4065 | int top; /* First byte of content for any cell in data[] */ |
| 4066 | int end; /* First byte past the last cell pointer in data[] */ |
| 4067 | int ins; /* Index in data[] where new cell pointer is inserted */ |
| 4068 | int hdr; /* Offset into data[] of the page header */ |
| 4069 | int cellOffset; /* Address of first cell pointer in data[] */ |
| 4070 | u8 *data; /* The content of the whole page */ |
| 4071 | u8 *ptr; /* Used for moving information around in data[] */ |
| 4072 | |
| 4073 | assert( i>=0 && i<=pPage->nCell+pPage->nOverflow ); |
| 4074 | assert( sz==cellSizePtr(pPage, pCell) ); |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 4075 | assert( sqlite3PagerIswriteable(pPage->pDbPage) ); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4076 | if( pPage->nOverflow || sz+2>pPage->nFree ){ |
drh | 24cd67e | 2004-05-10 16:18:47 +0000 | [diff] [blame] | 4077 | if( pTemp ){ |
danielk1977 | a3ad5e7 | 2005-01-07 08:56:44 +0000 | [diff] [blame] | 4078 | memcpy(pTemp+nSkip, pCell+nSkip, sz-nSkip); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4079 | pCell = pTemp; |
drh | 24cd67e | 2004-05-10 16:18:47 +0000 | [diff] [blame] | 4080 | } |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4081 | j = pPage->nOverflow++; |
| 4082 | assert( j<sizeof(pPage->aOvfl)/sizeof(pPage->aOvfl[0]) ); |
| 4083 | pPage->aOvfl[j].pCell = pCell; |
| 4084 | pPage->aOvfl[j].idx = i; |
| 4085 | pPage->nFree = 0; |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 4086 | }else{ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4087 | data = pPage->aData; |
| 4088 | hdr = pPage->hdrOffset; |
| 4089 | top = get2byte(&data[hdr+5]); |
| 4090 | cellOffset = pPage->cellOffset; |
| 4091 | end = cellOffset + 2*pPage->nCell + 2; |
| 4092 | ins = cellOffset + 2*i; |
| 4093 | if( end > top - sz ){ |
danielk1977 | 6b456a2 | 2005-03-21 04:04:02 +0000 | [diff] [blame] | 4094 | int rc = defragmentPage(pPage); |
| 4095 | if( rc!=SQLITE_OK ) return rc; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4096 | top = get2byte(&data[hdr+5]); |
| 4097 | assert( end + sz <= top ); |
| 4098 | } |
| 4099 | idx = allocateSpace(pPage, sz); |
| 4100 | assert( idx>0 ); |
| 4101 | assert( end <= get2byte(&data[hdr+5]) ); |
| 4102 | pPage->nCell++; |
| 4103 | pPage->nFree -= 2; |
danielk1977 | a3ad5e7 | 2005-01-07 08:56:44 +0000 | [diff] [blame] | 4104 | memcpy(&data[idx+nSkip], pCell+nSkip, sz-nSkip); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4105 | for(j=end-2, ptr=&data[j]; j>ins; j-=2, ptr-=2){ |
| 4106 | ptr[0] = ptr[-2]; |
| 4107 | ptr[1] = ptr[-1]; |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 4108 | } |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4109 | put2byte(&data[ins], idx); |
| 4110 | put2byte(&data[hdr+3], pPage->nCell); |
| 4111 | pPage->idxShift = 1; |
danielk1977 | a19df67 | 2004-11-03 11:37:07 +0000 | [diff] [blame] | 4112 | #ifndef SQLITE_OMIT_AUTOVACUUM |
| 4113 | if( pPage->pBt->autoVacuum ){ |
| 4114 | /* The cell may contain a pointer to an overflow page. If so, write |
| 4115 | ** the entry for the overflow page into the pointer map. |
| 4116 | */ |
| 4117 | CellInfo info; |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 4118 | sqlite3BtreeParseCellPtr(pPage, pCell, &info); |
drh | 7236583 | 2007-03-06 15:53:44 +0000 | [diff] [blame] | 4119 | assert( (info.nData+(pPage->intKey?0:info.nKey))==info.nPayload ); |
danielk1977 | a19df67 | 2004-11-03 11:37:07 +0000 | [diff] [blame] | 4120 | if( (info.nData+(pPage->intKey?0:info.nKey))>info.nLocal ){ |
| 4121 | Pgno pgnoOvfl = get4byte(&pCell[info.iOverflow]); |
| 4122 | int rc = ptrmapPut(pPage->pBt, pgnoOvfl, PTRMAP_OVERFLOW1, pPage->pgno); |
| 4123 | if( rc!=SQLITE_OK ) return rc; |
| 4124 | } |
| 4125 | } |
| 4126 | #endif |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 4127 | } |
danielk1977 | e80463b | 2004-11-03 03:01:16 +0000 | [diff] [blame] | 4128 | |
danielk1977 | e80463b | 2004-11-03 03:01:16 +0000 | [diff] [blame] | 4129 | return SQLITE_OK; |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 4130 | } |
| 4131 | |
| 4132 | /* |
drh | fa1a98a | 2004-05-14 19:08:17 +0000 | [diff] [blame] | 4133 | ** Add a list of cells to a page. The page should be initially empty. |
| 4134 | ** The cells are guaranteed to fit on the page. |
| 4135 | */ |
| 4136 | static void assemblePage( |
| 4137 | MemPage *pPage, /* The page to be assemblied */ |
| 4138 | int nCell, /* The number of cells to add to this page */ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4139 | u8 **apCell, /* Pointers to cell bodies */ |
drh | fa1a98a | 2004-05-14 19:08:17 +0000 | [diff] [blame] | 4140 | int *aSize /* Sizes of the cells */ |
| 4141 | ){ |
| 4142 | int i; /* Loop counter */ |
| 4143 | int totalSize; /* Total size of all cells */ |
| 4144 | int hdr; /* Index of page header */ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4145 | int cellptr; /* Address of next cell pointer */ |
| 4146 | int cellbody; /* Address of next cell body */ |
drh | fa1a98a | 2004-05-14 19:08:17 +0000 | [diff] [blame] | 4147 | u8 *data; /* Data for the page */ |
| 4148 | |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4149 | assert( pPage->nOverflow==0 ); |
drh | fa1a98a | 2004-05-14 19:08:17 +0000 | [diff] [blame] | 4150 | totalSize = 0; |
| 4151 | for(i=0; i<nCell; i++){ |
| 4152 | totalSize += aSize[i]; |
| 4153 | } |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4154 | assert( totalSize+2*nCell<=pPage->nFree ); |
drh | fa1a98a | 2004-05-14 19:08:17 +0000 | [diff] [blame] | 4155 | assert( pPage->nCell==0 ); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4156 | cellptr = pPage->cellOffset; |
drh | fa1a98a | 2004-05-14 19:08:17 +0000 | [diff] [blame] | 4157 | data = pPage->aData; |
| 4158 | hdr = pPage->hdrOffset; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4159 | put2byte(&data[hdr+3], nCell); |
drh | 09d0deb | 2005-08-02 17:13:09 +0000 | [diff] [blame] | 4160 | if( nCell ){ |
| 4161 | cellbody = allocateSpace(pPage, totalSize); |
| 4162 | assert( cellbody>0 ); |
| 4163 | assert( pPage->nFree >= 2*nCell ); |
| 4164 | pPage->nFree -= 2*nCell; |
| 4165 | for(i=0; i<nCell; i++){ |
| 4166 | put2byte(&data[cellptr], cellbody); |
| 4167 | memcpy(&data[cellbody], apCell[i], aSize[i]); |
| 4168 | cellptr += 2; |
| 4169 | cellbody += aSize[i]; |
| 4170 | } |
| 4171 | assert( cellbody==pPage->pBt->usableSize ); |
drh | fa1a98a | 2004-05-14 19:08:17 +0000 | [diff] [blame] | 4172 | } |
| 4173 | pPage->nCell = nCell; |
drh | fa1a98a | 2004-05-14 19:08:17 +0000 | [diff] [blame] | 4174 | } |
| 4175 | |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 4176 | /* |
drh | c3b7057 | 2003-01-04 19:44:07 +0000 | [diff] [blame] | 4177 | ** The following parameters determine how many adjacent pages get involved |
| 4178 | ** in a balancing operation. NN is the number of neighbors on either side |
| 4179 | ** of the page that participate in the balancing operation. NB is the |
| 4180 | ** total number of pages that participate, including the target page and |
| 4181 | ** NN neighbors on either side. |
| 4182 | ** |
| 4183 | ** The minimum value of NN is 1 (of course). Increasing NN above 1 |
| 4184 | ** (to 2 or 3) gives a modest improvement in SELECT and DELETE performance |
| 4185 | ** in exchange for a larger degradation in INSERT and UPDATE performance. |
| 4186 | ** The value of NN appears to give the best results overall. |
| 4187 | */ |
| 4188 | #define NN 1 /* Number of neighbors on either side of pPage */ |
| 4189 | #define NB (NN*2+1) /* Total pages involved in the balance */ |
| 4190 | |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4191 | /* Forward reference */ |
danielk1977 | ac245ec | 2005-01-14 13:50:11 +0000 | [diff] [blame] | 4192 | static int balance(MemPage*, int); |
| 4193 | |
drh | 615ae55 | 2005-01-16 23:21:00 +0000 | [diff] [blame] | 4194 | #ifndef SQLITE_OMIT_QUICKBALANCE |
drh | f222e71 | 2005-01-14 22:55:49 +0000 | [diff] [blame] | 4195 | /* |
| 4196 | ** This version of balance() handles the common special case where |
| 4197 | ** a new entry is being inserted on the extreme right-end of the |
| 4198 | ** tree, in other words, when the new entry will become the largest |
| 4199 | ** entry in the tree. |
| 4200 | ** |
| 4201 | ** Instead of trying balance the 3 right-most leaf pages, just add |
| 4202 | ** a new page to the right-hand side and put the one new entry in |
| 4203 | ** that page. This leaves the right side of the tree somewhat |
| 4204 | ** unbalanced. But odds are that we will be inserting new entries |
| 4205 | ** at the end soon afterwards so the nearly empty page will quickly |
| 4206 | ** fill up. On average. |
| 4207 | ** |
| 4208 | ** pPage is the leaf page which is the right-most page in the tree. |
| 4209 | ** pParent is its parent. pPage must have a single overflow entry |
| 4210 | ** which is also the right-most entry on the page. |
| 4211 | */ |
danielk1977 | ac245ec | 2005-01-14 13:50:11 +0000 | [diff] [blame] | 4212 | static int balance_quick(MemPage *pPage, MemPage *pParent){ |
| 4213 | int rc; |
| 4214 | MemPage *pNew; |
| 4215 | Pgno pgnoNew; |
| 4216 | u8 *pCell; |
| 4217 | int szCell; |
| 4218 | CellInfo info; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 4219 | BtShared *pBt = pPage->pBt; |
danielk1977 | 79a40da | 2005-01-16 08:00:01 +0000 | [diff] [blame] | 4220 | int parentIdx = pParent->nCell; /* pParent new divider cell index */ |
| 4221 | int parentSize; /* Size of new divider cell */ |
| 4222 | u8 parentCell[64]; /* Space for the new divider cell */ |
danielk1977 | ac245ec | 2005-01-14 13:50:11 +0000 | [diff] [blame] | 4223 | |
| 4224 | /* Allocate a new page. Insert the overflow cell from pPage |
| 4225 | ** into it. Then remove the overflow cell from pPage. |
| 4226 | */ |
drh | 4f0c587 | 2007-03-26 22:05:01 +0000 | [diff] [blame] | 4227 | rc = allocateBtreePage(pBt, &pNew, &pgnoNew, 0, 0); |
danielk1977 | ac245ec | 2005-01-14 13:50:11 +0000 | [diff] [blame] | 4228 | if( rc!=SQLITE_OK ){ |
| 4229 | return rc; |
| 4230 | } |
| 4231 | pCell = pPage->aOvfl[0].pCell; |
| 4232 | szCell = cellSizePtr(pPage, pCell); |
| 4233 | zeroPage(pNew, pPage->aData[0]); |
| 4234 | assemblePage(pNew, 1, &pCell, &szCell); |
| 4235 | pPage->nOverflow = 0; |
| 4236 | |
danielk1977 | 79a40da | 2005-01-16 08:00:01 +0000 | [diff] [blame] | 4237 | /* Set the parent of the newly allocated page to pParent. */ |
| 4238 | pNew->pParent = pParent; |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 4239 | sqlite3PagerRef(pParent->pDbPage); |
danielk1977 | 79a40da | 2005-01-16 08:00:01 +0000 | [diff] [blame] | 4240 | |
danielk1977 | ac245ec | 2005-01-14 13:50:11 +0000 | [diff] [blame] | 4241 | /* pPage is currently the right-child of pParent. Change this |
| 4242 | ** so that the right-child is the new page allocated above and |
danielk1977 | 79a40da | 2005-01-16 08:00:01 +0000 | [diff] [blame] | 4243 | ** pPage is the next-to-right child. |
danielk1977 | ac245ec | 2005-01-14 13:50:11 +0000 | [diff] [blame] | 4244 | */ |
danielk1977 | ac11ee6 | 2005-01-15 12:45:51 +0000 | [diff] [blame] | 4245 | assert( pPage->nCell>0 ); |
danielk1977 | 1cc5ed8 | 2007-05-16 17:28:43 +0000 | [diff] [blame] | 4246 | pCell = findCell(pPage, pPage->nCell-1); |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 4247 | sqlite3BtreeParseCellPtr(pPage, pCell, &info); |
drh | b026e05 | 2007-05-02 01:34:31 +0000 | [diff] [blame] | 4248 | rc = fillInCell(pParent, parentCell, 0, info.nKey, 0, 0, 0, &parentSize); |
danielk1977 | ac245ec | 2005-01-14 13:50:11 +0000 | [diff] [blame] | 4249 | if( rc!=SQLITE_OK ){ |
danielk1977 | 79a40da | 2005-01-16 08:00:01 +0000 | [diff] [blame] | 4250 | return rc; |
danielk1977 | ac245ec | 2005-01-14 13:50:11 +0000 | [diff] [blame] | 4251 | } |
| 4252 | assert( parentSize<64 ); |
| 4253 | rc = insertCell(pParent, parentIdx, parentCell, parentSize, 0, 4); |
| 4254 | if( rc!=SQLITE_OK ){ |
danielk1977 | 79a40da | 2005-01-16 08:00:01 +0000 | [diff] [blame] | 4255 | return rc; |
danielk1977 | ac245ec | 2005-01-14 13:50:11 +0000 | [diff] [blame] | 4256 | } |
| 4257 | put4byte(findOverflowCell(pParent,parentIdx), pPage->pgno); |
| 4258 | put4byte(&pParent->aData[pParent->hdrOffset+8], pgnoNew); |
| 4259 | |
danielk1977 | 79a40da | 2005-01-16 08:00:01 +0000 | [diff] [blame] | 4260 | #ifndef SQLITE_OMIT_AUTOVACUUM |
| 4261 | /* If this is an auto-vacuum database, update the pointer map |
| 4262 | ** with entries for the new page, and any pointer from the |
| 4263 | ** cell on the page to an overflow page. |
| 4264 | */ |
danielk1977 | ac11ee6 | 2005-01-15 12:45:51 +0000 | [diff] [blame] | 4265 | if( pBt->autoVacuum ){ |
| 4266 | rc = ptrmapPut(pBt, pgnoNew, PTRMAP_BTREE, pParent->pgno); |
danielk1977 | deb403e | 2007-05-24 09:20:16 +0000 | [diff] [blame] | 4267 | if( rc==SQLITE_OK ){ |
| 4268 | rc = ptrmapPutOvfl(pNew, 0); |
danielk1977 | ac11ee6 | 2005-01-15 12:45:51 +0000 | [diff] [blame] | 4269 | } |
danielk1977 | 79a40da | 2005-01-16 08:00:01 +0000 | [diff] [blame] | 4270 | if( rc!=SQLITE_OK ){ |
danielk1977 | deb403e | 2007-05-24 09:20:16 +0000 | [diff] [blame] | 4271 | releasePage(pNew); |
danielk1977 | 79a40da | 2005-01-16 08:00:01 +0000 | [diff] [blame] | 4272 | return rc; |
danielk1977 | ac11ee6 | 2005-01-15 12:45:51 +0000 | [diff] [blame] | 4273 | } |
| 4274 | } |
danielk1977 | 79a40da | 2005-01-16 08:00:01 +0000 | [diff] [blame] | 4275 | #endif |
danielk1977 | ac11ee6 | 2005-01-15 12:45:51 +0000 | [diff] [blame] | 4276 | |
danielk1977 | 79a40da | 2005-01-16 08:00:01 +0000 | [diff] [blame] | 4277 | /* Release the reference to the new page and balance the parent page, |
| 4278 | ** in case the divider cell inserted caused it to become overfull. |
| 4279 | */ |
danielk1977 | ac245ec | 2005-01-14 13:50:11 +0000 | [diff] [blame] | 4280 | releasePage(pNew); |
| 4281 | return balance(pParent, 0); |
| 4282 | } |
drh | 615ae55 | 2005-01-16 23:21:00 +0000 | [diff] [blame] | 4283 | #endif /* SQLITE_OMIT_QUICKBALANCE */ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4284 | |
drh | c3b7057 | 2003-01-04 19:44:07 +0000 | [diff] [blame] | 4285 | /* |
drh | ab01f61 | 2004-05-22 02:55:23 +0000 | [diff] [blame] | 4286 | ** This routine redistributes Cells on pPage and up to NN*2 siblings |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 4287 | ** 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] | 4288 | ** Usually NN siblings on either side of pPage is used in the balancing, |
| 4289 | ** though more siblings might come from one side if pPage is the first |
drh | ab01f61 | 2004-05-22 02:55:23 +0000 | [diff] [blame] | 4290 | ** 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] | 4291 | ** (something which can only happen if pPage is the root page or a |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 4292 | ** child of root) then all available siblings participate in the balancing. |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 4293 | ** |
drh | 0c6cc4e | 2004-06-15 02:13:26 +0000 | [diff] [blame] | 4294 | ** The number of siblings of pPage might be increased or decreased by one or |
| 4295 | ** 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] | 4296 | ** is special and is allowed to be nearly empty. If pPage is |
drh | 8c42ca9 | 2001-06-22 19:15:00 +0000 | [diff] [blame] | 4297 | ** the root page, then the depth of the tree might be increased |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 4298 | ** or decreased by one, as necessary, to keep the root page from being |
drh | ab01f61 | 2004-05-22 02:55:23 +0000 | [diff] [blame] | 4299 | ** overfull or completely empty. |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 4300 | ** |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 4301 | ** Note that when this routine is called, some of the Cells on pPage |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 4302 | ** might not actually be stored in pPage->aData[]. This can happen |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 4303 | ** 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] | 4304 | ** make sure all Cells for pPage once again fit in pPage->aData[]. |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 4305 | ** |
drh | 8c42ca9 | 2001-06-22 19:15:00 +0000 | [diff] [blame] | 4306 | ** In the course of balancing the siblings of pPage, the parent of pPage |
| 4307 | ** might become overfull or underfull. If that happens, then this routine |
| 4308 | ** is called recursively on the parent. |
| 4309 | ** |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4310 | ** If this routine fails for any reason, it might leave the database |
| 4311 | ** in a corrupted state. So if this routine fails, the database should |
| 4312 | ** be rolled back. |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 4313 | */ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4314 | static int balance_nonroot(MemPage *pPage){ |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 4315 | MemPage *pParent; /* The parent of pPage */ |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 4316 | BtShared *pBt; /* The whole database */ |
danielk1977 | 634f298 | 2005-03-28 08:44:07 +0000 | [diff] [blame] | 4317 | int nCell = 0; /* Number of cells in apCell[] */ |
| 4318 | int nMaxCells = 0; /* Allocated size of apCell, szCell, aFrom. */ |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 4319 | int nOld; /* Number of pages in apOld[] */ |
| 4320 | int nNew; /* Number of pages in apNew[] */ |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 4321 | int nDiv; /* Number of cells in apDiv[] */ |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 4322 | int i, j, k; /* Loop counters */ |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 4323 | int idx; /* Index of pPage in pParent->aCell[] */ |
| 4324 | int nxDiv; /* Next divider slot in pParent->aCell[] */ |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 4325 | int rc; /* The return code */ |
drh | 9102529 | 2004-05-03 19:49:32 +0000 | [diff] [blame] | 4326 | int leafCorrection; /* 4 if pPage is a leaf. 0 if not */ |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 4327 | int leafData; /* True if pPage is a leaf of a LEAFDATA tree */ |
drh | 9102529 | 2004-05-03 19:49:32 +0000 | [diff] [blame] | 4328 | int usableSpace; /* Bytes in pPage beyond the header */ |
| 4329 | int pageFlags; /* Value of pPage->aData[0] */ |
drh | 6019e16 | 2001-07-02 17:51:45 +0000 | [diff] [blame] | 4330 | int subtotal; /* Subtotal of bytes in cells on one page */ |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 4331 | int iSpace = 0; /* First unused byte of aSpace[] */ |
drh | c3b7057 | 2003-01-04 19:44:07 +0000 | [diff] [blame] | 4332 | MemPage *apOld[NB]; /* pPage and up to two siblings */ |
| 4333 | Pgno pgnoOld[NB]; /* Page numbers for each page in apOld[] */ |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 4334 | MemPage *apCopy[NB]; /* Private copies of apOld[] pages */ |
drh | a2fce64 | 2004-06-05 00:01:44 +0000 | [diff] [blame] | 4335 | MemPage *apNew[NB+2]; /* pPage and up to NB siblings after balancing */ |
| 4336 | Pgno pgnoNew[NB+2]; /* Page numbers for each page in apNew[] */ |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 4337 | u8 *apDiv[NB]; /* Divider cells in pParent */ |
drh | a2fce64 | 2004-06-05 00:01:44 +0000 | [diff] [blame] | 4338 | int cntNew[NB+2]; /* Index in aCell[] of cell after i-th page */ |
| 4339 | int szNew[NB+2]; /* Combined size of cells place on i-th page */ |
danielk1977 | 50f059b | 2005-03-29 02:54:03 +0000 | [diff] [blame] | 4340 | u8 **apCell = 0; /* All cells begin balanced */ |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 4341 | int *szCell; /* Local size of all cells in apCell[] */ |
| 4342 | u8 *aCopy[NB]; /* Space for holding data of apCopy[] */ |
| 4343 | u8 *aSpace; /* Space to hold copies of dividers cells */ |
danielk1977 | 4e17d14 | 2005-01-16 09:06:33 +0000 | [diff] [blame] | 4344 | #ifndef SQLITE_OMIT_AUTOVACUUM |
danielk1977 | ac11ee6 | 2005-01-15 12:45:51 +0000 | [diff] [blame] | 4345 | u8 *aFrom = 0; |
| 4346 | #endif |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 4347 | |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 4348 | /* |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4349 | ** Find the parent page. |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 4350 | */ |
drh | 3a4c141 | 2004-05-09 20:40:11 +0000 | [diff] [blame] | 4351 | assert( pPage->isInit ); |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 4352 | assert( sqlite3PagerIswriteable(pPage->pDbPage) ); |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 4353 | pBt = pPage->pBt; |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 4354 | pParent = pPage->pParent; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4355 | assert( pParent ); |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 4356 | if( SQLITE_OK!=(rc = sqlite3PagerWrite(pParent->pDbPage)) ){ |
danielk1977 | 07cb560 | 2006-01-20 10:55:05 +0000 | [diff] [blame] | 4357 | return rc; |
| 4358 | } |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4359 | TRACE(("BALANCE: begin page %d child of %d\n", pPage->pgno, pParent->pgno)); |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 4360 | |
drh | 615ae55 | 2005-01-16 23:21:00 +0000 | [diff] [blame] | 4361 | #ifndef SQLITE_OMIT_QUICKBALANCE |
drh | f222e71 | 2005-01-14 22:55:49 +0000 | [diff] [blame] | 4362 | /* |
| 4363 | ** A special case: If a new entry has just been inserted into a |
| 4364 | ** 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] | 4365 | ** 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] | 4366 | ** largest key) then use the special balance_quick() routine for |
| 4367 | ** balancing. balance_quick() is much faster and results in a tighter |
| 4368 | ** packing of data in the common case. |
| 4369 | */ |
danielk1977 | ac245ec | 2005-01-14 13:50:11 +0000 | [diff] [blame] | 4370 | if( pPage->leaf && |
| 4371 | pPage->intKey && |
| 4372 | pPage->leafData && |
| 4373 | pPage->nOverflow==1 && |
| 4374 | pPage->aOvfl[0].idx==pPage->nCell && |
danielk1977 | ac11ee6 | 2005-01-15 12:45:51 +0000 | [diff] [blame] | 4375 | pPage->pParent->pgno!=1 && |
danielk1977 | ac245ec | 2005-01-14 13:50:11 +0000 | [diff] [blame] | 4376 | get4byte(&pParent->aData[pParent->hdrOffset+8])==pPage->pgno |
| 4377 | ){ |
danielk1977 | ac11ee6 | 2005-01-15 12:45:51 +0000 | [diff] [blame] | 4378 | /* |
| 4379 | ** TODO: Check the siblings to the left of pPage. It may be that |
| 4380 | ** they are not full and no new page is required. |
| 4381 | */ |
danielk1977 | ac245ec | 2005-01-14 13:50:11 +0000 | [diff] [blame] | 4382 | return balance_quick(pPage, pParent); |
| 4383 | } |
| 4384 | #endif |
| 4385 | |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 4386 | /* |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 4387 | ** Find the cell in the parent page whose left child points back |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 4388 | ** to pPage. The "idx" variable is the index of that cell. If pPage |
| 4389 | ** is the rightmost child of pParent then set idx to pParent->nCell |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 4390 | */ |
drh | bb49aba | 2003-01-04 18:53:27 +0000 | [diff] [blame] | 4391 | if( pParent->idxShift ){ |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 4392 | Pgno pgno; |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 4393 | pgno = pPage->pgno; |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 4394 | assert( pgno==sqlite3PagerPagenumber(pPage->pDbPage) ); |
drh | bb49aba | 2003-01-04 18:53:27 +0000 | [diff] [blame] | 4395 | for(idx=0; idx<pParent->nCell; idx++){ |
danielk1977 | 1cc5ed8 | 2007-05-16 17:28:43 +0000 | [diff] [blame] | 4396 | if( get4byte(findCell(pParent, idx))==pgno ){ |
drh | bb49aba | 2003-01-04 18:53:27 +0000 | [diff] [blame] | 4397 | break; |
| 4398 | } |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 4399 | } |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 4400 | assert( idx<pParent->nCell |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4401 | || get4byte(&pParent->aData[pParent->hdrOffset+8])==pgno ); |
drh | bb49aba | 2003-01-04 18:53:27 +0000 | [diff] [blame] | 4402 | }else{ |
| 4403 | idx = pPage->idxParent; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 4404 | } |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 4405 | |
| 4406 | /* |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 4407 | ** Initialize variables so that it will be safe to jump |
drh | 5edc312 | 2001-09-13 21:53:09 +0000 | [diff] [blame] | 4408 | ** directly to balance_cleanup at any moment. |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 4409 | */ |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 4410 | nOld = nNew = 0; |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 4411 | sqlite3PagerRef(pParent->pDbPage); |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 4412 | |
| 4413 | /* |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 4414 | ** Find sibling pages to pPage and the cells in pParent that divide |
drh | c3b7057 | 2003-01-04 19:44:07 +0000 | [diff] [blame] | 4415 | ** the siblings. An attempt is made to find NN siblings on either |
| 4416 | ** side of pPage. More siblings are taken from one side, however, if |
| 4417 | ** pPage there are fewer than NN siblings on the other side. If pParent |
| 4418 | ** has NB or fewer children then all children of pParent are taken. |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 4419 | */ |
drh | c3b7057 | 2003-01-04 19:44:07 +0000 | [diff] [blame] | 4420 | nxDiv = idx - NN; |
| 4421 | if( nxDiv + NB > pParent->nCell ){ |
| 4422 | nxDiv = pParent->nCell - NB + 1; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 4423 | } |
drh | c3b7057 | 2003-01-04 19:44:07 +0000 | [diff] [blame] | 4424 | if( nxDiv<0 ){ |
| 4425 | nxDiv = 0; |
| 4426 | } |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 4427 | nDiv = 0; |
drh | c3b7057 | 2003-01-04 19:44:07 +0000 | [diff] [blame] | 4428 | for(i=0, k=nxDiv; i<NB; i++, k++){ |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 4429 | if( k<pParent->nCell ){ |
danielk1977 | 1cc5ed8 | 2007-05-16 17:28:43 +0000 | [diff] [blame] | 4430 | apDiv[i] = findCell(pParent, k); |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 4431 | nDiv++; |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 4432 | assert( !pParent->leaf ); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4433 | pgnoOld[i] = get4byte(apDiv[i]); |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 4434 | }else if( k==pParent->nCell ){ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4435 | pgnoOld[i] = get4byte(&pParent->aData[pParent->hdrOffset+8]); |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 4436 | }else{ |
| 4437 | break; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 4438 | } |
drh | de64713 | 2004-05-07 17:57:49 +0000 | [diff] [blame] | 4439 | rc = getAndInitPage(pBt, pgnoOld[i], &apOld[i], pParent); |
drh | 6019e16 | 2001-07-02 17:51:45 +0000 | [diff] [blame] | 4440 | if( rc ) goto balance_cleanup; |
drh | 428ae8c | 2003-01-04 16:48:09 +0000 | [diff] [blame] | 4441 | apOld[i]->idxParent = k; |
drh | 9102529 | 2004-05-03 19:49:32 +0000 | [diff] [blame] | 4442 | apCopy[i] = 0; |
| 4443 | assert( i==nOld ); |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 4444 | nOld++; |
danielk1977 | 634f298 | 2005-03-28 08:44:07 +0000 | [diff] [blame] | 4445 | nMaxCells += 1+apOld[i]->nCell+apOld[i]->nOverflow; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 4446 | } |
| 4447 | |
drh | 8d97f1f | 2005-05-05 18:14:13 +0000 | [diff] [blame] | 4448 | /* Make nMaxCells a multiple of 2 in order to preserve 8-byte |
| 4449 | ** alignment */ |
| 4450 | nMaxCells = (nMaxCells + 1)&~1; |
| 4451 | |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 4452 | /* |
danielk1977 | 634f298 | 2005-03-28 08:44:07 +0000 | [diff] [blame] | 4453 | ** Allocate space for memory structures |
| 4454 | */ |
| 4455 | apCell = sqliteMallocRaw( |
| 4456 | nMaxCells*sizeof(u8*) /* apCell */ |
| 4457 | + nMaxCells*sizeof(int) /* szCell */ |
drh | c96d853 | 2005-05-03 12:30:33 +0000 | [diff] [blame] | 4458 | + ROUND8(sizeof(MemPage))*NB /* aCopy */ |
drh | 07d183d | 2005-05-01 22:52:42 +0000 | [diff] [blame] | 4459 | + pBt->pageSize*(5+NB) /* aSpace */ |
drh | c96d853 | 2005-05-03 12:30:33 +0000 | [diff] [blame] | 4460 | + (ISAUTOVACUUM ? nMaxCells : 0) /* aFrom */ |
danielk1977 | 634f298 | 2005-03-28 08:44:07 +0000 | [diff] [blame] | 4461 | ); |
| 4462 | if( apCell==0 ){ |
| 4463 | rc = SQLITE_NOMEM; |
| 4464 | goto balance_cleanup; |
| 4465 | } |
| 4466 | szCell = (int*)&apCell[nMaxCells]; |
| 4467 | aCopy[0] = (u8*)&szCell[nMaxCells]; |
drh | c96d853 | 2005-05-03 12:30:33 +0000 | [diff] [blame] | 4468 | assert( ((aCopy[0] - (u8*)apCell) & 7)==0 ); /* 8-byte alignment required */ |
danielk1977 | 634f298 | 2005-03-28 08:44:07 +0000 | [diff] [blame] | 4469 | for(i=1; i<NB; i++){ |
drh | c96d853 | 2005-05-03 12:30:33 +0000 | [diff] [blame] | 4470 | aCopy[i] = &aCopy[i-1][pBt->pageSize+ROUND8(sizeof(MemPage))]; |
| 4471 | assert( ((aCopy[i] - (u8*)apCell) & 7)==0 ); /* 8-byte alignment required */ |
danielk1977 | 634f298 | 2005-03-28 08:44:07 +0000 | [diff] [blame] | 4472 | } |
drh | c96d853 | 2005-05-03 12:30:33 +0000 | [diff] [blame] | 4473 | aSpace = &aCopy[NB-1][pBt->pageSize+ROUND8(sizeof(MemPage))]; |
| 4474 | assert( ((aSpace - (u8*)apCell) & 7)==0 ); /* 8-byte alignment required */ |
danielk1977 | 634f298 | 2005-03-28 08:44:07 +0000 | [diff] [blame] | 4475 | #ifndef SQLITE_OMIT_AUTOVACUUM |
| 4476 | if( pBt->autoVacuum ){ |
drh | 07d183d | 2005-05-01 22:52:42 +0000 | [diff] [blame] | 4477 | aFrom = &aSpace[5*pBt->pageSize]; |
danielk1977 | 634f298 | 2005-03-28 08:44:07 +0000 | [diff] [blame] | 4478 | } |
| 4479 | #endif |
| 4480 | |
| 4481 | /* |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 4482 | ** Make copies of the content of pPage and its siblings into aOld[]. |
| 4483 | ** The rest of this function will use data from the copies rather |
| 4484 | ** that the original pages since the original pages will be in the |
| 4485 | ** process of being overwritten. |
| 4486 | */ |
| 4487 | for(i=0; i<nOld; i++){ |
drh | 07d183d | 2005-05-01 22:52:42 +0000 | [diff] [blame] | 4488 | MemPage *p = apCopy[i] = (MemPage*)&aCopy[i][pBt->pageSize]; |
drh | 07d183d | 2005-05-01 22:52:42 +0000 | [diff] [blame] | 4489 | p->aData = &((u8*)p)[-pBt->pageSize]; |
| 4490 | memcpy(p->aData, apOld[i]->aData, pBt->pageSize + sizeof(MemPage)); |
| 4491 | /* The memcpy() above changes the value of p->aData so we have to |
| 4492 | ** set it again. */ |
drh | 07d183d | 2005-05-01 22:52:42 +0000 | [diff] [blame] | 4493 | p->aData = &((u8*)p)[-pBt->pageSize]; |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 4494 | } |
| 4495 | |
| 4496 | /* |
| 4497 | ** Load pointers to all cells on sibling pages and the divider cells |
| 4498 | ** into the local apCell[] array. Make copies of the divider cells |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 4499 | ** into space obtained form aSpace[] and remove the the divider Cells |
| 4500 | ** from pParent. |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 4501 | ** |
| 4502 | ** If the siblings are on leaf pages, then the child pointers of the |
| 4503 | ** divider cells are stripped from the cells before they are copied |
drh | 96f5b76 | 2004-05-16 16:24:36 +0000 | [diff] [blame] | 4504 | ** into aSpace[]. In this way, all cells in apCell[] are without |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 4505 | ** child pointers. If siblings are not leaves, then all cell in |
| 4506 | ** apCell[] include child pointers. Either way, all cells in apCell[] |
| 4507 | ** are alike. |
drh | 96f5b76 | 2004-05-16 16:24:36 +0000 | [diff] [blame] | 4508 | ** |
| 4509 | ** leafCorrection: 4 if pPage is a leaf. 0 if pPage is not a leaf. |
| 4510 | ** leafData: 1 if pPage holds key+data and pParent holds only keys. |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 4511 | */ |
| 4512 | nCell = 0; |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 4513 | leafCorrection = pPage->leaf*4; |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 4514 | leafData = pPage->leafData && pPage->leaf; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 4515 | for(i=0; i<nOld; i++){ |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 4516 | MemPage *pOld = apCopy[i]; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4517 | int limit = pOld->nCell+pOld->nOverflow; |
| 4518 | for(j=0; j<limit; j++){ |
danielk1977 | 634f298 | 2005-03-28 08:44:07 +0000 | [diff] [blame] | 4519 | assert( nCell<nMaxCells ); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4520 | apCell[nCell] = findOverflowCell(pOld, j); |
| 4521 | szCell[nCell] = cellSizePtr(pOld, apCell[nCell]); |
danielk1977 | ac11ee6 | 2005-01-15 12:45:51 +0000 | [diff] [blame] | 4522 | #ifndef SQLITE_OMIT_AUTOVACUUM |
| 4523 | if( pBt->autoVacuum ){ |
| 4524 | int a; |
| 4525 | aFrom[nCell] = i; |
| 4526 | for(a=0; a<pOld->nOverflow; a++){ |
| 4527 | if( pOld->aOvfl[a].pCell==apCell[nCell] ){ |
| 4528 | aFrom[nCell] = 0xFF; |
| 4529 | break; |
| 4530 | } |
| 4531 | } |
| 4532 | } |
| 4533 | #endif |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 4534 | nCell++; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 4535 | } |
| 4536 | if( i<nOld-1 ){ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4537 | int sz = cellSizePtr(pParent, apDiv[i]); |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 4538 | if( leafData ){ |
drh | 96f5b76 | 2004-05-16 16:24:36 +0000 | [diff] [blame] | 4539 | /* With the LEAFDATA flag, pParent cells hold only INTKEYs that |
| 4540 | ** are duplicates of keys on the child pages. We need to remove |
| 4541 | ** the divider cells from pParent, but the dividers cells are not |
| 4542 | ** added to apCell[] because they are duplicates of child cells. |
| 4543 | */ |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 4544 | dropCell(pParent, nxDiv, sz); |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 4545 | }else{ |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 4546 | u8 *pTemp; |
danielk1977 | 634f298 | 2005-03-28 08:44:07 +0000 | [diff] [blame] | 4547 | assert( nCell<nMaxCells ); |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 4548 | szCell[nCell] = sz; |
| 4549 | pTemp = &aSpace[iSpace]; |
| 4550 | iSpace += sz; |
drh | 07d183d | 2005-05-01 22:52:42 +0000 | [diff] [blame] | 4551 | assert( iSpace<=pBt->pageSize*5 ); |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 4552 | memcpy(pTemp, apDiv[i], sz); |
| 4553 | apCell[nCell] = pTemp+leafCorrection; |
danielk1977 | ac11ee6 | 2005-01-15 12:45:51 +0000 | [diff] [blame] | 4554 | #ifndef SQLITE_OMIT_AUTOVACUUM |
| 4555 | if( pBt->autoVacuum ){ |
| 4556 | aFrom[nCell] = 0xFF; |
| 4557 | } |
| 4558 | #endif |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 4559 | dropCell(pParent, nxDiv, sz); |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 4560 | szCell[nCell] -= leafCorrection; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4561 | assert( get4byte(pTemp)==pgnoOld[i] ); |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 4562 | if( !pOld->leaf ){ |
| 4563 | assert( leafCorrection==0 ); |
| 4564 | /* The right pointer of the child page pOld becomes the left |
| 4565 | ** pointer of the divider cell */ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4566 | memcpy(apCell[nCell], &pOld->aData[pOld->hdrOffset+8], 4); |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 4567 | }else{ |
| 4568 | assert( leafCorrection==4 ); |
danielk1977 | 39c9604 | 2007-05-12 10:41:47 +0000 | [diff] [blame] | 4569 | if( szCell[nCell]<4 ){ |
| 4570 | /* Do not allow any cells smaller than 4 bytes. */ |
| 4571 | szCell[nCell] = 4; |
| 4572 | } |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 4573 | } |
| 4574 | nCell++; |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 4575 | } |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 4576 | } |
| 4577 | } |
| 4578 | |
| 4579 | /* |
drh | 6019e16 | 2001-07-02 17:51:45 +0000 | [diff] [blame] | 4580 | ** Figure out the number of pages needed to hold all nCell cells. |
| 4581 | ** Store this number in "k". Also compute szNew[] which is the total |
| 4582 | ** 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] | 4583 | ** in apCell[] of the cell that divides page i from page i+1. |
drh | 6019e16 | 2001-07-02 17:51:45 +0000 | [diff] [blame] | 4584 | ** cntNew[k] should equal nCell. |
| 4585 | ** |
drh | 96f5b76 | 2004-05-16 16:24:36 +0000 | [diff] [blame] | 4586 | ** Values computed by this block: |
| 4587 | ** |
| 4588 | ** k: The total number of sibling pages |
| 4589 | ** szNew[i]: Spaced used on the i-th sibling page. |
| 4590 | ** cntNew[i]: Index in apCell[] and szCell[] for the first cell to |
| 4591 | ** the right of the i-th sibling page. |
| 4592 | ** usableSpace: Number of bytes of space available on each sibling. |
| 4593 | ** |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 4594 | */ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4595 | usableSpace = pBt->usableSize - 12 + leafCorrection; |
drh | 6019e16 | 2001-07-02 17:51:45 +0000 | [diff] [blame] | 4596 | for(subtotal=k=i=0; i<nCell; i++){ |
danielk1977 | 634f298 | 2005-03-28 08:44:07 +0000 | [diff] [blame] | 4597 | assert( i<nMaxCells ); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4598 | subtotal += szCell[i] + 2; |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 4599 | if( subtotal > usableSpace ){ |
drh | 6019e16 | 2001-07-02 17:51:45 +0000 | [diff] [blame] | 4600 | szNew[k] = subtotal - szCell[i]; |
| 4601 | cntNew[k] = i; |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 4602 | if( leafData ){ i--; } |
drh | 6019e16 | 2001-07-02 17:51:45 +0000 | [diff] [blame] | 4603 | subtotal = 0; |
| 4604 | k++; |
| 4605 | } |
| 4606 | } |
| 4607 | szNew[k] = subtotal; |
| 4608 | cntNew[k] = nCell; |
| 4609 | k++; |
drh | 96f5b76 | 2004-05-16 16:24:36 +0000 | [diff] [blame] | 4610 | |
| 4611 | /* |
| 4612 | ** The packing computed by the previous block is biased toward the siblings |
| 4613 | ** on the left side. The left siblings are always nearly full, while the |
| 4614 | ** right-most sibling might be nearly empty. This block of code attempts |
| 4615 | ** to adjust the packing of siblings to get a better balance. |
| 4616 | ** |
| 4617 | ** This adjustment is more than an optimization. The packing above might |
| 4618 | ** be so out of balance as to be illegal. For example, the right-most |
| 4619 | ** sibling might be completely empty. This adjustment is not optional. |
| 4620 | */ |
drh | 6019e16 | 2001-07-02 17:51:45 +0000 | [diff] [blame] | 4621 | for(i=k-1; i>0; i--){ |
drh | 96f5b76 | 2004-05-16 16:24:36 +0000 | [diff] [blame] | 4622 | int szRight = szNew[i]; /* Size of sibling on the right */ |
| 4623 | int szLeft = szNew[i-1]; /* Size of sibling on the left */ |
| 4624 | int r; /* Index of right-most cell in left sibling */ |
| 4625 | int d; /* Index of first cell to the left of right sibling */ |
| 4626 | |
| 4627 | r = cntNew[i-1] - 1; |
| 4628 | d = r + 1 - leafData; |
danielk1977 | 634f298 | 2005-03-28 08:44:07 +0000 | [diff] [blame] | 4629 | assert( d<nMaxCells ); |
| 4630 | assert( r<nMaxCells ); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4631 | while( szRight==0 || szRight+szCell[d]+2<=szLeft-(szCell[r]+2) ){ |
| 4632 | szRight += szCell[d] + 2; |
| 4633 | szLeft -= szCell[r] + 2; |
drh | 6019e16 | 2001-07-02 17:51:45 +0000 | [diff] [blame] | 4634 | cntNew[i-1]--; |
drh | 96f5b76 | 2004-05-16 16:24:36 +0000 | [diff] [blame] | 4635 | r = cntNew[i-1] - 1; |
| 4636 | d = r + 1 - leafData; |
drh | 6019e16 | 2001-07-02 17:51:45 +0000 | [diff] [blame] | 4637 | } |
drh | 96f5b76 | 2004-05-16 16:24:36 +0000 | [diff] [blame] | 4638 | szNew[i] = szRight; |
| 4639 | szNew[i-1] = szLeft; |
drh | 6019e16 | 2001-07-02 17:51:45 +0000 | [diff] [blame] | 4640 | } |
drh | 09d0deb | 2005-08-02 17:13:09 +0000 | [diff] [blame] | 4641 | |
| 4642 | /* Either we found one or more cells (cntnew[0])>0) or we are the |
| 4643 | ** a virtual root page. A virtual root page is when the real root |
| 4644 | ** page is page 1 and we are the only child of that page. |
| 4645 | */ |
| 4646 | assert( cntNew[0]>0 || (pParent->pgno==1 && pParent->nCell==0) ); |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 4647 | |
| 4648 | /* |
drh | 6b30867 | 2002-07-08 02:16:37 +0000 | [diff] [blame] | 4649 | ** Allocate k new pages. Reuse old pages where possible. |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 4650 | */ |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 4651 | assert( pPage->pgno>1 ); |
| 4652 | pageFlags = pPage->aData[0]; |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 4653 | for(i=0; i<k; i++){ |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 4654 | MemPage *pNew; |
drh | 6b30867 | 2002-07-08 02:16:37 +0000 | [diff] [blame] | 4655 | if( i<nOld ){ |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 4656 | pNew = apNew[i] = apOld[i]; |
drh | 6b30867 | 2002-07-08 02:16:37 +0000 | [diff] [blame] | 4657 | pgnoNew[i] = pgnoOld[i]; |
| 4658 | apOld[i] = 0; |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 4659 | rc = sqlite3PagerWrite(pNew->pDbPage); |
drh | f534544 | 2007-04-09 12:45:02 +0000 | [diff] [blame] | 4660 | nNew++; |
danielk1977 | 2812956 | 2005-01-11 10:25:06 +0000 | [diff] [blame] | 4661 | if( rc ) goto balance_cleanup; |
drh | 6b30867 | 2002-07-08 02:16:37 +0000 | [diff] [blame] | 4662 | }else{ |
drh | 7aa8f85 | 2006-03-28 00:24:44 +0000 | [diff] [blame] | 4663 | assert( i>0 ); |
drh | 4f0c587 | 2007-03-26 22:05:01 +0000 | [diff] [blame] | 4664 | rc = allocateBtreePage(pBt, &pNew, &pgnoNew[i], pgnoNew[i-1], 0); |
drh | 6b30867 | 2002-07-08 02:16:37 +0000 | [diff] [blame] | 4665 | if( rc ) goto balance_cleanup; |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 4666 | apNew[i] = pNew; |
drh | f534544 | 2007-04-09 12:45:02 +0000 | [diff] [blame] | 4667 | nNew++; |
drh | 6b30867 | 2002-07-08 02:16:37 +0000 | [diff] [blame] | 4668 | } |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 4669 | zeroPage(pNew, pageFlags); |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 4670 | } |
| 4671 | |
danielk1977 | 299b187 | 2004-11-22 10:02:10 +0000 | [diff] [blame] | 4672 | /* Free any old pages that were not reused as new pages. |
| 4673 | */ |
| 4674 | while( i<nOld ){ |
| 4675 | rc = freePage(apOld[i]); |
| 4676 | if( rc ) goto balance_cleanup; |
| 4677 | releasePage(apOld[i]); |
| 4678 | apOld[i] = 0; |
| 4679 | i++; |
| 4680 | } |
| 4681 | |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 4682 | /* |
drh | f9ffac9 | 2002-03-02 19:00:31 +0000 | [diff] [blame] | 4683 | ** Put the new pages in accending order. This helps to |
| 4684 | ** keep entries in the disk file in order so that a scan |
| 4685 | ** of the table is a linear scan through the file. That |
| 4686 | ** in turn helps the operating system to deliver pages |
| 4687 | ** from the disk more rapidly. |
| 4688 | ** |
| 4689 | ** An O(n^2) insertion sort algorithm is used, but since |
drh | c3b7057 | 2003-01-04 19:44:07 +0000 | [diff] [blame] | 4690 | ** n is never more than NB (a small constant), that should |
| 4691 | ** not be a problem. |
drh | f9ffac9 | 2002-03-02 19:00:31 +0000 | [diff] [blame] | 4692 | ** |
drh | c3b7057 | 2003-01-04 19:44:07 +0000 | [diff] [blame] | 4693 | ** When NB==3, this one optimization makes the database |
| 4694 | ** about 25% faster for large insertions and deletions. |
drh | f9ffac9 | 2002-03-02 19:00:31 +0000 | [diff] [blame] | 4695 | */ |
| 4696 | for(i=0; i<k-1; i++){ |
| 4697 | int minV = pgnoNew[i]; |
| 4698 | int minI = i; |
| 4699 | for(j=i+1; j<k; j++){ |
drh | 7d02cb7 | 2003-06-04 16:24:39 +0000 | [diff] [blame] | 4700 | if( pgnoNew[j]<(unsigned)minV ){ |
drh | f9ffac9 | 2002-03-02 19:00:31 +0000 | [diff] [blame] | 4701 | minI = j; |
| 4702 | minV = pgnoNew[j]; |
| 4703 | } |
| 4704 | } |
| 4705 | if( minI>i ){ |
| 4706 | int t; |
| 4707 | MemPage *pT; |
| 4708 | t = pgnoNew[i]; |
| 4709 | pT = apNew[i]; |
| 4710 | pgnoNew[i] = pgnoNew[minI]; |
| 4711 | apNew[i] = apNew[minI]; |
| 4712 | pgnoNew[minI] = t; |
| 4713 | apNew[minI] = pT; |
| 4714 | } |
| 4715 | } |
drh | a2fce64 | 2004-06-05 00:01:44 +0000 | [diff] [blame] | 4716 | 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] | 4717 | pgnoOld[0], |
| 4718 | nOld>=2 ? pgnoOld[1] : 0, |
| 4719 | nOld>=3 ? pgnoOld[2] : 0, |
drh | 10c0fa6 | 2004-05-18 12:50:17 +0000 | [diff] [blame] | 4720 | pgnoNew[0], szNew[0], |
| 4721 | nNew>=2 ? pgnoNew[1] : 0, nNew>=2 ? szNew[1] : 0, |
| 4722 | nNew>=3 ? pgnoNew[2] : 0, nNew>=3 ? szNew[2] : 0, |
drh | a2fce64 | 2004-06-05 00:01:44 +0000 | [diff] [blame] | 4723 | nNew>=4 ? pgnoNew[3] : 0, nNew>=4 ? szNew[3] : 0, |
| 4724 | nNew>=5 ? pgnoNew[4] : 0, nNew>=5 ? szNew[4] : 0)); |
drh | 24cd67e | 2004-05-10 16:18:47 +0000 | [diff] [blame] | 4725 | |
drh | f9ffac9 | 2002-03-02 19:00:31 +0000 | [diff] [blame] | 4726 | /* |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 4727 | ** Evenly distribute the data in apCell[] across the new pages. |
| 4728 | ** Insert divider cells into pParent as necessary. |
| 4729 | */ |
| 4730 | j = 0; |
| 4731 | for(i=0; i<nNew; i++){ |
danielk1977 | ac11ee6 | 2005-01-15 12:45:51 +0000 | [diff] [blame] | 4732 | /* Assemble the new sibling page. */ |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 4733 | MemPage *pNew = apNew[i]; |
drh | 19642e5 | 2005-03-29 13:17:45 +0000 | [diff] [blame] | 4734 | assert( j<nMaxCells ); |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 4735 | assert( pNew->pgno==pgnoNew[i] ); |
drh | fa1a98a | 2004-05-14 19:08:17 +0000 | [diff] [blame] | 4736 | assemblePage(pNew, cntNew[i]-j, &apCell[j], &szCell[j]); |
drh | 09d0deb | 2005-08-02 17:13:09 +0000 | [diff] [blame] | 4737 | assert( pNew->nCell>0 || (nNew==1 && cntNew[0]==0) ); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4738 | assert( pNew->nOverflow==0 ); |
danielk1977 | ac11ee6 | 2005-01-15 12:45:51 +0000 | [diff] [blame] | 4739 | |
| 4740 | #ifndef SQLITE_OMIT_AUTOVACUUM |
| 4741 | /* If this is an auto-vacuum database, update the pointer map entries |
| 4742 | ** that point to the siblings that were rearranged. These can be: left |
| 4743 | ** children of cells, the right-child of the page, or overflow pages |
| 4744 | ** pointed to by cells. |
| 4745 | */ |
| 4746 | if( pBt->autoVacuum ){ |
| 4747 | for(k=j; k<cntNew[i]; k++){ |
danielk1977 | 634f298 | 2005-03-28 08:44:07 +0000 | [diff] [blame] | 4748 | assert( k<nMaxCells ); |
danielk1977 | ac11ee6 | 2005-01-15 12:45:51 +0000 | [diff] [blame] | 4749 | if( aFrom[k]==0xFF || apCopy[aFrom[k]]->pgno!=pNew->pgno ){ |
danielk1977 | 79a40da | 2005-01-16 08:00:01 +0000 | [diff] [blame] | 4750 | rc = ptrmapPutOvfl(pNew, k-j); |
| 4751 | if( rc!=SQLITE_OK ){ |
| 4752 | goto balance_cleanup; |
danielk1977 | ac11ee6 | 2005-01-15 12:45:51 +0000 | [diff] [blame] | 4753 | } |
| 4754 | } |
| 4755 | } |
| 4756 | } |
| 4757 | #endif |
| 4758 | |
| 4759 | j = cntNew[i]; |
| 4760 | |
| 4761 | /* If the sibling page assembled above was not the right-most sibling, |
| 4762 | ** insert a divider cell into the parent page. |
| 4763 | */ |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 4764 | if( i<nNew-1 && j<nCell ){ |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 4765 | u8 *pCell; |
drh | 24cd67e | 2004-05-10 16:18:47 +0000 | [diff] [blame] | 4766 | u8 *pTemp; |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 4767 | int sz; |
danielk1977 | 634f298 | 2005-03-28 08:44:07 +0000 | [diff] [blame] | 4768 | |
| 4769 | assert( j<nMaxCells ); |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 4770 | pCell = apCell[j]; |
| 4771 | sz = szCell[j] + leafCorrection; |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 4772 | if( !pNew->leaf ){ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4773 | memcpy(&pNew->aData[8], pCell, 4); |
drh | 24cd67e | 2004-05-10 16:18:47 +0000 | [diff] [blame] | 4774 | pTemp = 0; |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 4775 | }else if( leafData ){ |
drh | fd131da | 2007-08-07 17:13:03 +0000 | [diff] [blame^] | 4776 | /* If the tree is a leaf-data tree, and the siblings are leaves, |
danielk1977 | ac11ee6 | 2005-01-15 12:45:51 +0000 | [diff] [blame] | 4777 | ** then there is no divider cell in apCell[]. Instead, the divider |
| 4778 | ** cell consists of the integer key for the right-most cell of |
| 4779 | ** the sibling-page assembled above only. |
| 4780 | */ |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 4781 | CellInfo info; |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 4782 | j--; |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 4783 | sqlite3BtreeParseCellPtr(pNew, apCell[j], &info); |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 4784 | pCell = &aSpace[iSpace]; |
drh | b026e05 | 2007-05-02 01:34:31 +0000 | [diff] [blame] | 4785 | fillInCell(pParent, pCell, 0, info.nKey, 0, 0, 0, &sz); |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 4786 | iSpace += sz; |
drh | 07d183d | 2005-05-01 22:52:42 +0000 | [diff] [blame] | 4787 | assert( iSpace<=pBt->pageSize*5 ); |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 4788 | pTemp = 0; |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 4789 | }else{ |
| 4790 | pCell -= 4; |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 4791 | pTemp = &aSpace[iSpace]; |
| 4792 | iSpace += sz; |
drh | 07d183d | 2005-05-01 22:52:42 +0000 | [diff] [blame] | 4793 | assert( iSpace<=pBt->pageSize*5 ); |
danielk1977 | 4aeff62 | 2007-05-12 09:30:47 +0000 | [diff] [blame] | 4794 | /* Obscure case for non-leaf-data trees: If the cell at pCell was |
| 4795 | ** previously stored on a leaf node, and it's reported size was 4 |
| 4796 | ** bytes, then it may actually be smaller than this |
| 4797 | ** (see sqlite3BtreeParseCellPtr(), 4 bytes is the minimum size of |
| 4798 | ** any cell). But it's important to pass the correct size to |
| 4799 | ** insertCell(), so reparse the cell now. |
| 4800 | ** |
| 4801 | ** Note that this can never happen in an SQLite data file, as all |
| 4802 | ** cells are at least 4 bytes. It only happens in b-trees used |
| 4803 | ** to evaluate "IN (SELECT ...)" and similar clauses. |
| 4804 | */ |
| 4805 | if( szCell[j]==4 ){ |
| 4806 | assert(leafCorrection==4); |
| 4807 | sz = cellSizePtr(pParent, pCell); |
| 4808 | } |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 4809 | } |
danielk1977 | a3ad5e7 | 2005-01-07 08:56:44 +0000 | [diff] [blame] | 4810 | rc = insertCell(pParent, nxDiv, pCell, sz, pTemp, 4); |
danielk1977 | e80463b | 2004-11-03 03:01:16 +0000 | [diff] [blame] | 4811 | if( rc!=SQLITE_OK ) goto balance_cleanup; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4812 | put4byte(findOverflowCell(pParent,nxDiv), pNew->pgno); |
danielk1977 | ac11ee6 | 2005-01-15 12:45:51 +0000 | [diff] [blame] | 4813 | #ifndef SQLITE_OMIT_AUTOVACUUM |
| 4814 | /* If this is an auto-vacuum database, and not a leaf-data tree, |
| 4815 | ** then update the pointer map with an entry for the overflow page |
| 4816 | ** that the cell just inserted points to (if any). |
| 4817 | */ |
| 4818 | if( pBt->autoVacuum && !leafData ){ |
danielk1977 | 79a40da | 2005-01-16 08:00:01 +0000 | [diff] [blame] | 4819 | rc = ptrmapPutOvfl(pParent, nxDiv); |
| 4820 | if( rc!=SQLITE_OK ){ |
| 4821 | goto balance_cleanup; |
danielk1977 | ac11ee6 | 2005-01-15 12:45:51 +0000 | [diff] [blame] | 4822 | } |
| 4823 | } |
| 4824 | #endif |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 4825 | j++; |
| 4826 | nxDiv++; |
| 4827 | } |
| 4828 | } |
drh | 6019e16 | 2001-07-02 17:51:45 +0000 | [diff] [blame] | 4829 | assert( j==nCell ); |
drh | 7aa8f85 | 2006-03-28 00:24:44 +0000 | [diff] [blame] | 4830 | assert( nOld>0 ); |
| 4831 | assert( nNew>0 ); |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 4832 | if( (pageFlags & PTF_LEAF)==0 ){ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4833 | memcpy(&apNew[nNew-1]->aData[8], &apCopy[nOld-1]->aData[8], 4); |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 4834 | } |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4835 | if( nxDiv==pParent->nCell+pParent->nOverflow ){ |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 4836 | /* Right-most sibling is the right-most child of pParent */ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4837 | put4byte(&pParent->aData[pParent->hdrOffset+8], pgnoNew[nNew-1]); |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 4838 | }else{ |
| 4839 | /* Right-most sibling is the left child of the first entry in pParent |
| 4840 | ** past the right-most divider entry */ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4841 | put4byte(findOverflowCell(pParent, nxDiv), pgnoNew[nNew-1]); |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 4842 | } |
| 4843 | |
| 4844 | /* |
| 4845 | ** Reparent children of all cells. |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 4846 | */ |
| 4847 | for(i=0; i<nNew; i++){ |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 4848 | rc = reparentChildPages(apNew[i]); |
| 4849 | if( rc!=SQLITE_OK ) goto balance_cleanup; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 4850 | } |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 4851 | rc = reparentChildPages(pParent); |
| 4852 | if( rc!=SQLITE_OK ) goto balance_cleanup; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 4853 | |
| 4854 | /* |
drh | 3a4c141 | 2004-05-09 20:40:11 +0000 | [diff] [blame] | 4855 | ** Balance the parent page. Note that the current page (pPage) might |
danielk1977 | ac11ee6 | 2005-01-15 12:45:51 +0000 | [diff] [blame] | 4856 | ** have been added to the freelist so it might no longer be initialized. |
drh | 3a4c141 | 2004-05-09 20:40:11 +0000 | [diff] [blame] | 4857 | ** But the parent page will always be initialized. |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 4858 | */ |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 4859 | assert( pParent->isInit ); |
danielk1977 | ac245ec | 2005-01-14 13:50:11 +0000 | [diff] [blame] | 4860 | rc = balance(pParent, 0); |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 4861 | |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 4862 | /* |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 4863 | ** Cleanup before returning. |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 4864 | */ |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 4865 | balance_cleanup: |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 4866 | sqliteFree(apCell); |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 4867 | for(i=0; i<nOld; i++){ |
drh | 9102529 | 2004-05-03 19:49:32 +0000 | [diff] [blame] | 4868 | releasePage(apOld[i]); |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 4869 | } |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 4870 | for(i=0; i<nNew; i++){ |
drh | 9102529 | 2004-05-03 19:49:32 +0000 | [diff] [blame] | 4871 | releasePage(apNew[i]); |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 4872 | } |
drh | 9102529 | 2004-05-03 19:49:32 +0000 | [diff] [blame] | 4873 | releasePage(pParent); |
drh | 3a4c141 | 2004-05-09 20:40:11 +0000 | [diff] [blame] | 4874 | TRACE(("BALANCE: finished with %d: old=%d new=%d cells=%d\n", |
| 4875 | pPage->pgno, nOld, nNew, nCell)); |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 4876 | return rc; |
| 4877 | } |
| 4878 | |
| 4879 | /* |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4880 | ** This routine is called for the root page of a btree when the root |
| 4881 | ** page contains no cells. This is an opportunity to make the tree |
| 4882 | ** shallower by one level. |
| 4883 | */ |
| 4884 | static int balance_shallower(MemPage *pPage){ |
| 4885 | MemPage *pChild; /* The only child page of pPage */ |
| 4886 | Pgno pgnoChild; /* Page number for pChild */ |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 4887 | int rc = SQLITE_OK; /* Return code from subprocedures */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 4888 | BtShared *pBt; /* The main BTree structure */ |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 4889 | int mxCellPerPage; /* Maximum number of cells per page */ |
| 4890 | u8 **apCell; /* All cells from pages being balanced */ |
| 4891 | int *szCell; /* Local size of all cells */ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4892 | |
| 4893 | assert( pPage->pParent==0 ); |
| 4894 | assert( pPage->nCell==0 ); |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 4895 | pBt = pPage->pBt; |
| 4896 | mxCellPerPage = MX_CELL(pBt); |
| 4897 | apCell = sqliteMallocRaw( mxCellPerPage*(sizeof(u8*)+sizeof(int)) ); |
| 4898 | if( apCell==0 ) return SQLITE_NOMEM; |
| 4899 | szCell = (int*)&apCell[mxCellPerPage]; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4900 | if( pPage->leaf ){ |
| 4901 | /* The table is completely empty */ |
| 4902 | TRACE(("BALANCE: empty table %d\n", pPage->pgno)); |
| 4903 | }else{ |
| 4904 | /* The root page is empty but has one child. Transfer the |
| 4905 | ** information from that one child into the root page if it |
| 4906 | ** will fit. This reduces the depth of the tree by one. |
| 4907 | ** |
| 4908 | ** If the root page is page 1, it has less space available than |
| 4909 | ** its child (due to the 100 byte header that occurs at the beginning |
| 4910 | ** of the database fle), so it might not be able to hold all of the |
| 4911 | ** information currently contained in the child. If this is the |
| 4912 | ** case, then do not do the transfer. Leave page 1 empty except |
| 4913 | ** for the right-pointer to the child page. The child page becomes |
| 4914 | ** the virtual root of the tree. |
| 4915 | */ |
| 4916 | pgnoChild = get4byte(&pPage->aData[pPage->hdrOffset+8]); |
| 4917 | assert( pgnoChild>0 ); |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 4918 | assert( pgnoChild<=sqlite3PagerPagecount(pPage->pBt->pPager) ); |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 4919 | rc = sqlite3BtreeGetPage(pPage->pBt, pgnoChild, &pChild, 0); |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 4920 | if( rc ) goto end_shallow_balance; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4921 | if( pPage->pgno==1 ){ |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 4922 | rc = sqlite3BtreeInitPage(pChild, pPage); |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 4923 | if( rc ) goto end_shallow_balance; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4924 | assert( pChild->nOverflow==0 ); |
| 4925 | if( pChild->nFree>=100 ){ |
| 4926 | /* The child information will fit on the root page, so do the |
| 4927 | ** copy */ |
| 4928 | int i; |
| 4929 | zeroPage(pPage, pChild->aData[0]); |
| 4930 | for(i=0; i<pChild->nCell; i++){ |
danielk1977 | 1cc5ed8 | 2007-05-16 17:28:43 +0000 | [diff] [blame] | 4931 | apCell[i] = findCell(pChild,i); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4932 | szCell[i] = cellSizePtr(pChild, apCell[i]); |
| 4933 | } |
| 4934 | assemblePage(pPage, pChild->nCell, apCell, szCell); |
danielk1977 | ae82558 | 2004-11-23 09:06:55 +0000 | [diff] [blame] | 4935 | /* Copy the right-pointer of the child to the parent. */ |
| 4936 | put4byte(&pPage->aData[pPage->hdrOffset+8], |
| 4937 | get4byte(&pChild->aData[pChild->hdrOffset+8])); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4938 | freePage(pChild); |
| 4939 | TRACE(("BALANCE: child %d transfer to page 1\n", pChild->pgno)); |
| 4940 | }else{ |
| 4941 | /* The child has more information that will fit on the root. |
| 4942 | ** The tree is already balanced. Do nothing. */ |
| 4943 | TRACE(("BALANCE: child %d will not fit on page 1\n", pChild->pgno)); |
| 4944 | } |
| 4945 | }else{ |
| 4946 | memcpy(pPage->aData, pChild->aData, pPage->pBt->usableSize); |
| 4947 | pPage->isInit = 0; |
| 4948 | pPage->pParent = 0; |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 4949 | rc = sqlite3BtreeInitPage(pPage, 0); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4950 | assert( rc==SQLITE_OK ); |
| 4951 | freePage(pChild); |
| 4952 | TRACE(("BALANCE: transfer child %d into root %d\n", |
| 4953 | pChild->pgno, pPage->pgno)); |
| 4954 | } |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 4955 | rc = reparentChildPages(pPage); |
danielk1977 | ac11ee6 | 2005-01-15 12:45:51 +0000 | [diff] [blame] | 4956 | assert( pPage->nOverflow==0 ); |
| 4957 | #ifndef SQLITE_OMIT_AUTOVACUUM |
| 4958 | if( pBt->autoVacuum ){ |
danielk1977 | aac0a38 | 2005-01-16 11:07:06 +0000 | [diff] [blame] | 4959 | int i; |
danielk1977 | ac11ee6 | 2005-01-15 12:45:51 +0000 | [diff] [blame] | 4960 | for(i=0; i<pPage->nCell; i++){ |
danielk1977 | 79a40da | 2005-01-16 08:00:01 +0000 | [diff] [blame] | 4961 | rc = ptrmapPutOvfl(pPage, i); |
| 4962 | if( rc!=SQLITE_OK ){ |
| 4963 | goto end_shallow_balance; |
danielk1977 | ac11ee6 | 2005-01-15 12:45:51 +0000 | [diff] [blame] | 4964 | } |
| 4965 | } |
| 4966 | } |
| 4967 | #endif |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 4968 | if( rc!=SQLITE_OK ) goto end_shallow_balance; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4969 | releasePage(pChild); |
| 4970 | } |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 4971 | end_shallow_balance: |
| 4972 | sqliteFree(apCell); |
| 4973 | return rc; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4974 | } |
| 4975 | |
| 4976 | |
| 4977 | /* |
| 4978 | ** The root page is overfull |
| 4979 | ** |
| 4980 | ** When this happens, Create a new child page and copy the |
| 4981 | ** contents of the root into the child. Then make the root |
| 4982 | ** page an empty page with rightChild pointing to the new |
| 4983 | ** child. Finally, call balance_internal() on the new child |
| 4984 | ** to cause it to split. |
| 4985 | */ |
| 4986 | static int balance_deeper(MemPage *pPage){ |
| 4987 | int rc; /* Return value from subprocedures */ |
| 4988 | MemPage *pChild; /* Pointer to a new child page */ |
| 4989 | Pgno pgnoChild; /* Page number of the new child page */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 4990 | BtShared *pBt; /* The BTree */ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4991 | int usableSize; /* Total usable size of a page */ |
| 4992 | u8 *data; /* Content of the parent page */ |
| 4993 | u8 *cdata; /* Content of the child page */ |
| 4994 | int hdr; /* Offset to page header in parent */ |
| 4995 | int brk; /* Offset to content of first cell in parent */ |
| 4996 | |
| 4997 | assert( pPage->pParent==0 ); |
| 4998 | assert( pPage->nOverflow>0 ); |
| 4999 | pBt = pPage->pBt; |
drh | 4f0c587 | 2007-03-26 22:05:01 +0000 | [diff] [blame] | 5000 | rc = allocateBtreePage(pBt, &pChild, &pgnoChild, pPage->pgno, 0); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5001 | if( rc ) return rc; |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 5002 | assert( sqlite3PagerIswriteable(pChild->pDbPage) ); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5003 | usableSize = pBt->usableSize; |
| 5004 | data = pPage->aData; |
| 5005 | hdr = pPage->hdrOffset; |
| 5006 | brk = get2byte(&data[hdr+5]); |
| 5007 | cdata = pChild->aData; |
| 5008 | memcpy(cdata, &data[hdr], pPage->cellOffset+2*pPage->nCell-hdr); |
| 5009 | memcpy(&cdata[brk], &data[brk], usableSize-brk); |
danielk1977 | c7dc753 | 2004-11-17 10:22:03 +0000 | [diff] [blame] | 5010 | assert( pChild->isInit==0 ); |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 5011 | rc = sqlite3BtreeInitPage(pChild, pPage); |
danielk1977 | 6b456a2 | 2005-03-21 04:04:02 +0000 | [diff] [blame] | 5012 | if( rc ) goto balancedeeper_out; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5013 | memcpy(pChild->aOvfl, pPage->aOvfl, pPage->nOverflow*sizeof(pPage->aOvfl[0])); |
| 5014 | pChild->nOverflow = pPage->nOverflow; |
| 5015 | if( pChild->nOverflow ){ |
| 5016 | pChild->nFree = 0; |
| 5017 | } |
| 5018 | assert( pChild->nCell==pPage->nCell ); |
| 5019 | zeroPage(pPage, pChild->aData[0] & ~PTF_LEAF); |
| 5020 | put4byte(&pPage->aData[pPage->hdrOffset+8], pgnoChild); |
| 5021 | TRACE(("BALANCE: copy root %d into %d\n", pPage->pgno, pChild->pgno)); |
danielk1977 | 4e17d14 | 2005-01-16 09:06:33 +0000 | [diff] [blame] | 5022 | #ifndef SQLITE_OMIT_AUTOVACUUM |
danielk1977 | ac11ee6 | 2005-01-15 12:45:51 +0000 | [diff] [blame] | 5023 | if( pBt->autoVacuum ){ |
| 5024 | int i; |
| 5025 | rc = ptrmapPut(pBt, pChild->pgno, PTRMAP_BTREE, pPage->pgno); |
danielk1977 | 6b456a2 | 2005-03-21 04:04:02 +0000 | [diff] [blame] | 5026 | if( rc ) goto balancedeeper_out; |
danielk1977 | ac11ee6 | 2005-01-15 12:45:51 +0000 | [diff] [blame] | 5027 | for(i=0; i<pChild->nCell; i++){ |
danielk1977 | 79a40da | 2005-01-16 08:00:01 +0000 | [diff] [blame] | 5028 | rc = ptrmapPutOvfl(pChild, i); |
| 5029 | if( rc!=SQLITE_OK ){ |
| 5030 | return rc; |
danielk1977 | ac11ee6 | 2005-01-15 12:45:51 +0000 | [diff] [blame] | 5031 | } |
| 5032 | } |
| 5033 | } |
danielk1977 | 4e17d14 | 2005-01-16 09:06:33 +0000 | [diff] [blame] | 5034 | #endif |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5035 | rc = balance_nonroot(pChild); |
danielk1977 | 6b456a2 | 2005-03-21 04:04:02 +0000 | [diff] [blame] | 5036 | |
| 5037 | balancedeeper_out: |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5038 | releasePage(pChild); |
| 5039 | return rc; |
| 5040 | } |
| 5041 | |
| 5042 | /* |
| 5043 | ** Decide if the page pPage needs to be balanced. If balancing is |
| 5044 | ** required, call the appropriate balancing routine. |
| 5045 | */ |
danielk1977 | ac245ec | 2005-01-14 13:50:11 +0000 | [diff] [blame] | 5046 | static int balance(MemPage *pPage, int insert){ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5047 | int rc = SQLITE_OK; |
| 5048 | if( pPage->pParent==0 ){ |
| 5049 | if( pPage->nOverflow>0 ){ |
| 5050 | rc = balance_deeper(pPage); |
| 5051 | } |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 5052 | if( rc==SQLITE_OK && pPage->nCell==0 ){ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5053 | rc = balance_shallower(pPage); |
| 5054 | } |
| 5055 | }else{ |
danielk1977 | ac245ec | 2005-01-14 13:50:11 +0000 | [diff] [blame] | 5056 | if( pPage->nOverflow>0 || |
| 5057 | (!insert && pPage->nFree>pPage->pBt->usableSize*2/3) ){ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5058 | rc = balance_nonroot(pPage); |
| 5059 | } |
| 5060 | } |
| 5061 | return rc; |
| 5062 | } |
| 5063 | |
| 5064 | /* |
drh | 8dcd7ca | 2004-08-08 19:43:29 +0000 | [diff] [blame] | 5065 | ** This routine checks all cursors that point to table pgnoRoot. |
drh | 980b1a7 | 2006-08-16 16:42:48 +0000 | [diff] [blame] | 5066 | ** If any of those cursors were opened with wrFlag==0 in a different |
| 5067 | ** database connection (a database connection that shares the pager |
| 5068 | ** cache with the current connection) and that other connection |
| 5069 | ** is not in the ReadUncommmitted state, then this routine returns |
| 5070 | ** SQLITE_LOCKED. |
danielk1977 | 299b187 | 2004-11-22 10:02:10 +0000 | [diff] [blame] | 5071 | ** |
| 5072 | ** In addition to checking for read-locks (where a read-lock |
| 5073 | ** means a cursor opened with wrFlag==0) this routine also moves |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 5074 | ** all write cursors so that they are pointing to the |
drh | 980b1a7 | 2006-08-16 16:42:48 +0000 | [diff] [blame] | 5075 | ** first Cell on the root page. This is necessary because an insert |
danielk1977 | 299b187 | 2004-11-22 10:02:10 +0000 | [diff] [blame] | 5076 | ** or delete might change the number of cells on a page or delete |
| 5077 | ** a page entirely and we do not want to leave any cursors |
| 5078 | ** pointing to non-existant pages or cells. |
drh | f74b8d9 | 2002-09-01 23:20:45 +0000 | [diff] [blame] | 5079 | */ |
drh | 980b1a7 | 2006-08-16 16:42:48 +0000 | [diff] [blame] | 5080 | static int checkReadLocks(Btree *pBtree, Pgno pgnoRoot, BtCursor *pExclude){ |
danielk1977 | 299b187 | 2004-11-22 10:02:10 +0000 | [diff] [blame] | 5081 | BtCursor *p; |
drh | 980b1a7 | 2006-08-16 16:42:48 +0000 | [diff] [blame] | 5082 | BtShared *pBt = pBtree->pBt; |
| 5083 | sqlite3 *db = pBtree->pSqlite; |
danielk1977 | 299b187 | 2004-11-22 10:02:10 +0000 | [diff] [blame] | 5084 | for(p=pBt->pCursor; p; p=p->pNext){ |
drh | 980b1a7 | 2006-08-16 16:42:48 +0000 | [diff] [blame] | 5085 | if( p==pExclude ) continue; |
| 5086 | if( p->eState!=CURSOR_VALID ) continue; |
| 5087 | if( p->pgnoRoot!=pgnoRoot ) continue; |
| 5088 | if( p->wrFlag==0 ){ |
| 5089 | sqlite3 *dbOther = p->pBtree->pSqlite; |
| 5090 | if( dbOther==0 || |
| 5091 | (dbOther!=db && (dbOther->flags & SQLITE_ReadUncommitted)==0) ){ |
| 5092 | return SQLITE_LOCKED; |
| 5093 | } |
| 5094 | }else if( p->pPage->pgno!=p->pgnoRoot ){ |
danielk1977 | 299b187 | 2004-11-22 10:02:10 +0000 | [diff] [blame] | 5095 | moveToRoot(p); |
| 5096 | } |
| 5097 | } |
drh | f74b8d9 | 2002-09-01 23:20:45 +0000 | [diff] [blame] | 5098 | return SQLITE_OK; |
| 5099 | } |
| 5100 | |
| 5101 | /* |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 5102 | ** Insert a new record into the BTree. The key is given by (pKey,nKey) |
| 5103 | ** 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] | 5104 | ** define what table the record should be inserted into. The cursor |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 5105 | ** is left pointing at a random location. |
| 5106 | ** |
| 5107 | ** For an INTKEY table, only the nKey value of the key is used. pKey is |
| 5108 | ** ignored. For a ZERODATA table, the pData and nData are both ignored. |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 5109 | */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 5110 | int sqlite3BtreeInsert( |
drh | 5c4d970 | 2001-08-20 00:33:58 +0000 | [diff] [blame] | 5111 | BtCursor *pCur, /* Insert data into the table of this cursor */ |
drh | 4a1c380 | 2004-05-12 15:15:47 +0000 | [diff] [blame] | 5112 | const void *pKey, i64 nKey, /* The key of the new record */ |
drh | e4d9081 | 2007-03-29 05:51:49 +0000 | [diff] [blame] | 5113 | const void *pData, int nData, /* The data of the new record */ |
drh | b026e05 | 2007-05-02 01:34:31 +0000 | [diff] [blame] | 5114 | int nZero, /* Number of extra 0 bytes to append to data */ |
drh | e4d9081 | 2007-03-29 05:51:49 +0000 | [diff] [blame] | 5115 | int appendBias /* True if this is likely an append */ |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 5116 | ){ |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 5117 | int rc; |
| 5118 | int loc; |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 5119 | int szNew; |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 5120 | MemPage *pPage; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 5121 | BtShared *pBt = pCur->pBtree->pBt; |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 5122 | unsigned char *oldCell; |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 5123 | unsigned char *newCell = 0; |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 5124 | |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 5125 | if( pBt->inTransaction!=TRANS_WRITE ){ |
drh | f74b8d9 | 2002-09-01 23:20:45 +0000 | [diff] [blame] | 5126 | /* Must start a transaction before doing an insert */ |
| 5127 | return pBt->readOnly ? SQLITE_READONLY : SQLITE_ERROR; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 5128 | } |
drh | f74b8d9 | 2002-09-01 23:20:45 +0000 | [diff] [blame] | 5129 | assert( !pBt->readOnly ); |
drh | ecdc753 | 2001-09-23 02:35:53 +0000 | [diff] [blame] | 5130 | if( !pCur->wrFlag ){ |
| 5131 | return SQLITE_PERM; /* Cursor not open for writing */ |
| 5132 | } |
drh | 980b1a7 | 2006-08-16 16:42:48 +0000 | [diff] [blame] | 5133 | if( checkReadLocks(pCur->pBtree, pCur->pgnoRoot, pCur) ){ |
drh | f74b8d9 | 2002-09-01 23:20:45 +0000 | [diff] [blame] | 5134 | return SQLITE_LOCKED; /* The table pCur points to has a read lock */ |
| 5135 | } |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 5136 | |
| 5137 | /* Save the positions of any other cursors open on this table */ |
drh | bf700f3 | 2007-03-31 02:36:44 +0000 | [diff] [blame] | 5138 | clearCursorPosition(pCur); |
danielk1977 | 2e94d4d | 2006-01-09 05:36:27 +0000 | [diff] [blame] | 5139 | if( |
danielk1977 | 2e94d4d | 2006-01-09 05:36:27 +0000 | [diff] [blame] | 5140 | SQLITE_OK!=(rc = saveAllCursors(pBt, pCur->pgnoRoot, pCur)) || |
drh | e4d9081 | 2007-03-29 05:51:49 +0000 | [diff] [blame] | 5141 | SQLITE_OK!=(rc = sqlite3BtreeMoveto(pCur, pKey, nKey, appendBias, &loc)) |
danielk1977 | 2e94d4d | 2006-01-09 05:36:27 +0000 | [diff] [blame] | 5142 | ){ |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 5143 | return rc; |
| 5144 | } |
| 5145 | |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 5146 | pPage = pCur->pPage; |
drh | 4a1c380 | 2004-05-12 15:15:47 +0000 | [diff] [blame] | 5147 | assert( pPage->intKey || nKey>=0 ); |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 5148 | assert( pPage->leaf || !pPage->leafData ); |
drh | 3a4c141 | 2004-05-09 20:40:11 +0000 | [diff] [blame] | 5149 | TRACE(("INSERT: table=%d nkey=%lld ndata=%d page=%d %s\n", |
| 5150 | pCur->pgnoRoot, nKey, nData, pPage->pgno, |
| 5151 | loc==0 ? "overwrite" : "new entry")); |
drh | 7aa128d | 2002-06-21 13:09:16 +0000 | [diff] [blame] | 5152 | assert( pPage->isInit ); |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 5153 | rc = sqlite3PagerWrite(pPage->pDbPage); |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 5154 | if( rc ) return rc; |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 5155 | newCell = sqliteMallocRaw( MX_CELL_SIZE(pBt) ); |
| 5156 | if( newCell==0 ) return SQLITE_NOMEM; |
drh | b026e05 | 2007-05-02 01:34:31 +0000 | [diff] [blame] | 5157 | rc = fillInCell(pPage, newCell, pKey, nKey, pData, nData, nZero, &szNew); |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 5158 | if( rc ) goto end_insert; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5159 | assert( szNew==cellSizePtr(pPage, newCell) ); |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 5160 | assert( szNew<=MX_CELL_SIZE(pBt) ); |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 5161 | if( loc==0 && CURSOR_VALID==pCur->eState ){ |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 5162 | int szOld; |
| 5163 | assert( pCur->idx>=0 && pCur->idx<pPage->nCell ); |
danielk1977 | 1cc5ed8 | 2007-05-16 17:28:43 +0000 | [diff] [blame] | 5164 | oldCell = findCell(pPage, pCur->idx); |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 5165 | if( !pPage->leaf ){ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5166 | memcpy(newCell, oldCell, 4); |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 5167 | } |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5168 | szOld = cellSizePtr(pPage, oldCell); |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 5169 | rc = clearCell(pPage, oldCell); |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 5170 | if( rc ) goto end_insert; |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 5171 | dropCell(pPage, pCur->idx, szOld); |
drh | 7c717f7 | 2001-06-24 20:39:41 +0000 | [diff] [blame] | 5172 | }else if( loc<0 && pPage->nCell>0 ){ |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 5173 | assert( pPage->leaf ); |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 5174 | pCur->idx++; |
drh | 271efa5 | 2004-05-30 19:19:05 +0000 | [diff] [blame] | 5175 | pCur->info.nSize = 0; |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 5176 | }else{ |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 5177 | assert( pPage->leaf ); |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 5178 | } |
danielk1977 | a3ad5e7 | 2005-01-07 08:56:44 +0000 | [diff] [blame] | 5179 | rc = insertCell(pPage, pCur->idx, newCell, szNew, 0, 0); |
danielk1977 | e80463b | 2004-11-03 03:01:16 +0000 | [diff] [blame] | 5180 | if( rc!=SQLITE_OK ) goto end_insert; |
danielk1977 | ac245ec | 2005-01-14 13:50:11 +0000 | [diff] [blame] | 5181 | rc = balance(pPage, 1); |
drh | 23e11ca | 2004-05-04 17:27:28 +0000 | [diff] [blame] | 5182 | /* sqlite3BtreePageDump(pCur->pBt, pCur->pgnoRoot, 1); */ |
drh | 3fc190c | 2001-09-14 03:24:23 +0000 | [diff] [blame] | 5183 | /* fflush(stdout); */ |
danielk1977 | 299b187 | 2004-11-22 10:02:10 +0000 | [diff] [blame] | 5184 | if( rc==SQLITE_OK ){ |
| 5185 | moveToRoot(pCur); |
| 5186 | } |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 5187 | end_insert: |
| 5188 | sqliteFree(newCell); |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 5189 | return rc; |
| 5190 | } |
| 5191 | |
| 5192 | /* |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 5193 | ** Delete the entry that the cursor is pointing to. The cursor |
| 5194 | ** is left pointing at a random location. |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 5195 | */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 5196 | int sqlite3BtreeDelete(BtCursor *pCur){ |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 5197 | MemPage *pPage = pCur->pPage; |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 5198 | unsigned char *pCell; |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 5199 | int rc; |
danielk1977 | cfe9a69 | 2004-06-16 12:00:29 +0000 | [diff] [blame] | 5200 | Pgno pgnoChild = 0; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 5201 | BtShared *pBt = pCur->pBtree->pBt; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 5202 | |
drh | 7aa128d | 2002-06-21 13:09:16 +0000 | [diff] [blame] | 5203 | assert( pPage->isInit ); |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 5204 | if( pBt->inTransaction!=TRANS_WRITE ){ |
drh | f74b8d9 | 2002-09-01 23:20:45 +0000 | [diff] [blame] | 5205 | /* Must start a transaction before doing a delete */ |
| 5206 | return pBt->readOnly ? SQLITE_READONLY : SQLITE_ERROR; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 5207 | } |
drh | f74b8d9 | 2002-09-01 23:20:45 +0000 | [diff] [blame] | 5208 | assert( !pBt->readOnly ); |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 5209 | if( pCur->idx >= pPage->nCell ){ |
| 5210 | return SQLITE_ERROR; /* The cursor is not pointing to anything */ |
| 5211 | } |
drh | ecdc753 | 2001-09-23 02:35:53 +0000 | [diff] [blame] | 5212 | if( !pCur->wrFlag ){ |
| 5213 | return SQLITE_PERM; /* Did not open this cursor for writing */ |
| 5214 | } |
drh | 980b1a7 | 2006-08-16 16:42:48 +0000 | [diff] [blame] | 5215 | if( checkReadLocks(pCur->pBtree, pCur->pgnoRoot, pCur) ){ |
drh | f74b8d9 | 2002-09-01 23:20:45 +0000 | [diff] [blame] | 5216 | return SQLITE_LOCKED; /* The table pCur points to has a read lock */ |
| 5217 | } |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 5218 | |
| 5219 | /* Restore the current cursor position (a no-op if the cursor is not in |
| 5220 | ** CURSOR_REQUIRESEEK state) and save the positions of any other cursors |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 5221 | ** open on the same table. Then call sqlite3PagerWrite() on the page |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 5222 | ** that the entry will be deleted from. |
| 5223 | */ |
| 5224 | if( |
drh | bf700f3 | 2007-03-31 02:36:44 +0000 | [diff] [blame] | 5225 | (rc = restoreOrClearCursorPosition(pCur))!=0 || |
drh | d116739 | 2006-01-23 13:00:35 +0000 | [diff] [blame] | 5226 | (rc = saveAllCursors(pBt, pCur->pgnoRoot, pCur))!=0 || |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 5227 | (rc = sqlite3PagerWrite(pPage->pDbPage))!=0 |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 5228 | ){ |
| 5229 | return rc; |
| 5230 | } |
danielk1977 | e6efa74 | 2004-11-10 11:55:10 +0000 | [diff] [blame] | 5231 | |
| 5232 | /* Locate the cell within it's page and leave pCell pointing to the |
| 5233 | ** data. The clearCell() call frees any overflow pages associated with the |
| 5234 | ** cell. The cell itself is still intact. |
| 5235 | */ |
danielk1977 | 1cc5ed8 | 2007-05-16 17:28:43 +0000 | [diff] [blame] | 5236 | pCell = findCell(pPage, pCur->idx); |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 5237 | if( !pPage->leaf ){ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5238 | pgnoChild = get4byte(pCell); |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 5239 | } |
danielk1977 | 2812956 | 2005-01-11 10:25:06 +0000 | [diff] [blame] | 5240 | rc = clearCell(pPage, pCell); |
| 5241 | if( rc ) return rc; |
danielk1977 | e6efa74 | 2004-11-10 11:55:10 +0000 | [diff] [blame] | 5242 | |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 5243 | if( !pPage->leaf ){ |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 5244 | /* |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 5245 | ** 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] | 5246 | ** do something we will leave a hole on an internal page. |
| 5247 | ** We have to fill the hole by moving in a cell from a leaf. The |
| 5248 | ** next Cell after the one to be deleted is guaranteed to exist and |
danielk1977 | 299b187 | 2004-11-22 10:02:10 +0000 | [diff] [blame] | 5249 | ** to be a leaf so we can use it. |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 5250 | */ |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 5251 | BtCursor leafCur; |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 5252 | unsigned char *pNext; |
drh | 02afc86 | 2006-01-20 18:10:57 +0000 | [diff] [blame] | 5253 | int szNext; /* The compiler warning is wrong: szNext is always |
| 5254 | ** initialized before use. Adding an extra initialization |
| 5255 | ** to silence the compiler slows down the code. */ |
danielk1977 | 299b187 | 2004-11-22 10:02:10 +0000 | [diff] [blame] | 5256 | int notUsed; |
danielk1977 | 6b456a2 | 2005-03-21 04:04:02 +0000 | [diff] [blame] | 5257 | unsigned char *tempCell = 0; |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 5258 | assert( !pPage->leafData ); |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 5259 | sqlite3BtreeGetTempCursor(pCur, &leafCur); |
danielk1977 | 299b187 | 2004-11-22 10:02:10 +0000 | [diff] [blame] | 5260 | rc = sqlite3BtreeNext(&leafCur, ¬Used); |
danielk1977 | 6b456a2 | 2005-03-21 04:04:02 +0000 | [diff] [blame] | 5261 | if( rc==SQLITE_OK ){ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 5262 | rc = sqlite3PagerWrite(leafCur.pPage->pDbPage); |
danielk1977 | 6b456a2 | 2005-03-21 04:04:02 +0000 | [diff] [blame] | 5263 | } |
| 5264 | if( rc==SQLITE_OK ){ |
| 5265 | TRACE(("DELETE: table=%d delete internal from %d replace from leaf %d\n", |
| 5266 | pCur->pgnoRoot, pPage->pgno, leafCur.pPage->pgno)); |
| 5267 | dropCell(pPage, pCur->idx, cellSizePtr(pPage, pCell)); |
danielk1977 | 1cc5ed8 | 2007-05-16 17:28:43 +0000 | [diff] [blame] | 5268 | pNext = findCell(leafCur.pPage, leafCur.idx); |
danielk1977 | 6b456a2 | 2005-03-21 04:04:02 +0000 | [diff] [blame] | 5269 | szNext = cellSizePtr(leafCur.pPage, pNext); |
| 5270 | assert( MX_CELL_SIZE(pBt)>=szNext+4 ); |
| 5271 | tempCell = sqliteMallocRaw( MX_CELL_SIZE(pBt) ); |
| 5272 | if( tempCell==0 ){ |
| 5273 | rc = SQLITE_NOMEM; |
| 5274 | } |
| 5275 | } |
| 5276 | if( rc==SQLITE_OK ){ |
| 5277 | rc = insertCell(pPage, pCur->idx, pNext-4, szNext+4, tempCell, 0); |
| 5278 | } |
| 5279 | if( rc==SQLITE_OK ){ |
| 5280 | put4byte(findOverflowCell(pPage, pCur->idx), pgnoChild); |
| 5281 | rc = balance(pPage, 0); |
| 5282 | } |
| 5283 | if( rc==SQLITE_OK ){ |
| 5284 | dropCell(leafCur.pPage, leafCur.idx, szNext); |
| 5285 | rc = balance(leafCur.pPage, 0); |
| 5286 | } |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 5287 | sqliteFree(tempCell); |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 5288 | sqlite3BtreeReleaseTempCursor(&leafCur); |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 5289 | }else{ |
danielk1977 | 299b187 | 2004-11-22 10:02:10 +0000 | [diff] [blame] | 5290 | TRACE(("DELETE: table=%d delete from leaf %d\n", |
| 5291 | pCur->pgnoRoot, pPage->pgno)); |
| 5292 | dropCell(pPage, pCur->idx, cellSizePtr(pPage, pCell)); |
danielk1977 | ac245ec | 2005-01-14 13:50:11 +0000 | [diff] [blame] | 5293 | rc = balance(pPage, 0); |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 5294 | } |
danielk1977 | 6b456a2 | 2005-03-21 04:04:02 +0000 | [diff] [blame] | 5295 | if( rc==SQLITE_OK ){ |
| 5296 | moveToRoot(pCur); |
| 5297 | } |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 5298 | return rc; |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 5299 | } |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 5300 | |
| 5301 | /* |
drh | c6b52df | 2002-01-04 03:09:29 +0000 | [diff] [blame] | 5302 | ** Create a new BTree table. Write into *piTable the page |
| 5303 | ** number for the root page of the new table. |
| 5304 | ** |
drh | ab01f61 | 2004-05-22 02:55:23 +0000 | [diff] [blame] | 5305 | ** The type of type is determined by the flags parameter. Only the |
| 5306 | ** following values of flags are currently in use. Other values for |
| 5307 | ** flags might not work: |
| 5308 | ** |
| 5309 | ** BTREE_INTKEY|BTREE_LEAFDATA Used for SQL tables with rowid keys |
| 5310 | ** BTREE_ZERODATA Used for SQL indices |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 5311 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 5312 | int sqlite3BtreeCreateTable(Btree *p, int *piTable, int flags){ |
| 5313 | BtShared *pBt = p->pBt; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 5314 | MemPage *pRoot; |
| 5315 | Pgno pgnoRoot; |
| 5316 | int rc; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 5317 | if( pBt->inTransaction!=TRANS_WRITE ){ |
drh | f74b8d9 | 2002-09-01 23:20:45 +0000 | [diff] [blame] | 5318 | /* Must start a transaction first */ |
| 5319 | return pBt->readOnly ? SQLITE_READONLY : SQLITE_ERROR; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 5320 | } |
danielk1977 | 2812956 | 2005-01-11 10:25:06 +0000 | [diff] [blame] | 5321 | assert( !pBt->readOnly ); |
danielk1977 | e6efa74 | 2004-11-10 11:55:10 +0000 | [diff] [blame] | 5322 | |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 5323 | #ifdef SQLITE_OMIT_AUTOVACUUM |
drh | 4f0c587 | 2007-03-26 22:05:01 +0000 | [diff] [blame] | 5324 | rc = allocateBtreePage(pBt, &pRoot, &pgnoRoot, 1, 0); |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 5325 | if( rc ) return rc; |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 5326 | #else |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 5327 | if( pBt->autoVacuum ){ |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 5328 | Pgno pgnoMove; /* Move a page here to make room for the root-page */ |
| 5329 | MemPage *pPageMove; /* The page to move to. */ |
| 5330 | |
danielk1977 | 20713f3 | 2007-05-03 11:43:33 +0000 | [diff] [blame] | 5331 | /* Creating a new table may probably require moving an existing database |
| 5332 | ** to make room for the new tables root page. In case this page turns |
| 5333 | ** out to be an overflow page, delete all overflow page-map caches |
| 5334 | ** held by open cursors. |
| 5335 | */ |
danielk1977 | 92d4d7a | 2007-05-04 12:05:56 +0000 | [diff] [blame] | 5336 | invalidateAllOverflowCache(pBt); |
danielk1977 | 20713f3 | 2007-05-03 11:43:33 +0000 | [diff] [blame] | 5337 | |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 5338 | /* Read the value of meta[3] from the database to determine where the |
| 5339 | ** root page of the new table should go. meta[3] is the largest root-page |
| 5340 | ** created so far, so the new root-page is (meta[3]+1). |
| 5341 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 5342 | rc = sqlite3BtreeGetMeta(p, 4, &pgnoRoot); |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 5343 | if( rc!=SQLITE_OK ) return rc; |
| 5344 | pgnoRoot++; |
| 5345 | |
danielk1977 | 599fcba | 2004-11-08 07:13:13 +0000 | [diff] [blame] | 5346 | /* The new root-page may not be allocated on a pointer-map page, or the |
| 5347 | ** PENDING_BYTE page. |
| 5348 | */ |
danielk1977 | 266664d | 2006-02-10 08:24:21 +0000 | [diff] [blame] | 5349 | if( pgnoRoot==PTRMAP_PAGENO(pBt, pgnoRoot) || |
danielk1977 | 599fcba | 2004-11-08 07:13:13 +0000 | [diff] [blame] | 5350 | pgnoRoot==PENDING_BYTE_PAGE(pBt) ){ |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 5351 | pgnoRoot++; |
| 5352 | } |
| 5353 | assert( pgnoRoot>=3 ); |
| 5354 | |
| 5355 | /* Allocate a page. The page that currently resides at pgnoRoot will |
| 5356 | ** be moved to the allocated page (unless the allocated page happens |
| 5357 | ** to reside at pgnoRoot). |
| 5358 | */ |
drh | 4f0c587 | 2007-03-26 22:05:01 +0000 | [diff] [blame] | 5359 | rc = allocateBtreePage(pBt, &pPageMove, &pgnoMove, pgnoRoot, 1); |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 5360 | if( rc!=SQLITE_OK ){ |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 5361 | return rc; |
| 5362 | } |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 5363 | |
| 5364 | if( pgnoMove!=pgnoRoot ){ |
danielk1977 | f35843b | 2007-04-07 15:03:17 +0000 | [diff] [blame] | 5365 | /* pgnoRoot is the page that will be used for the root-page of |
| 5366 | ** the new table (assuming an error did not occur). But we were |
| 5367 | ** allocated pgnoMove. If required (i.e. if it was not allocated |
| 5368 | ** by extending the file), the current page at position pgnoMove |
| 5369 | ** is already journaled. |
| 5370 | */ |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 5371 | u8 eType; |
| 5372 | Pgno iPtrPage; |
| 5373 | |
| 5374 | releasePage(pPageMove); |
danielk1977 | f35843b | 2007-04-07 15:03:17 +0000 | [diff] [blame] | 5375 | |
| 5376 | /* Move the page currently at pgnoRoot to pgnoMove. */ |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 5377 | rc = sqlite3BtreeGetPage(pBt, pgnoRoot, &pRoot, 0); |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 5378 | if( rc!=SQLITE_OK ){ |
| 5379 | return rc; |
| 5380 | } |
| 5381 | rc = ptrmapGet(pBt, pgnoRoot, &eType, &iPtrPage); |
drh | ccae602 | 2005-02-26 17:31:26 +0000 | [diff] [blame] | 5382 | if( rc!=SQLITE_OK || eType==PTRMAP_ROOTPAGE || eType==PTRMAP_FREEPAGE ){ |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 5383 | releasePage(pRoot); |
| 5384 | return rc; |
| 5385 | } |
drh | ccae602 | 2005-02-26 17:31:26 +0000 | [diff] [blame] | 5386 | assert( eType!=PTRMAP_ROOTPAGE ); |
| 5387 | assert( eType!=PTRMAP_FREEPAGE ); |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 5388 | rc = sqlite3PagerWrite(pRoot->pDbPage); |
danielk1977 | 5fd057a | 2005-03-09 13:09:43 +0000 | [diff] [blame] | 5389 | if( rc!=SQLITE_OK ){ |
| 5390 | releasePage(pRoot); |
| 5391 | return rc; |
| 5392 | } |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 5393 | rc = relocatePage(pBt, pRoot, eType, iPtrPage, pgnoMove); |
| 5394 | releasePage(pRoot); |
danielk1977 | f35843b | 2007-04-07 15:03:17 +0000 | [diff] [blame] | 5395 | |
| 5396 | /* Obtain the page at pgnoRoot */ |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 5397 | if( rc!=SQLITE_OK ){ |
| 5398 | return rc; |
| 5399 | } |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 5400 | rc = sqlite3BtreeGetPage(pBt, pgnoRoot, &pRoot, 0); |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 5401 | if( rc!=SQLITE_OK ){ |
| 5402 | return rc; |
| 5403 | } |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 5404 | rc = sqlite3PagerWrite(pRoot->pDbPage); |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 5405 | if( rc!=SQLITE_OK ){ |
| 5406 | releasePage(pRoot); |
| 5407 | return rc; |
| 5408 | } |
| 5409 | }else{ |
| 5410 | pRoot = pPageMove; |
| 5411 | } |
| 5412 | |
danielk1977 | 42741be | 2005-01-08 12:42:39 +0000 | [diff] [blame] | 5413 | /* Update the pointer-map and meta-data with the new root-page number. */ |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 5414 | rc = ptrmapPut(pBt, pgnoRoot, PTRMAP_ROOTPAGE, 0); |
| 5415 | if( rc ){ |
| 5416 | releasePage(pRoot); |
| 5417 | return rc; |
| 5418 | } |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 5419 | rc = sqlite3BtreeUpdateMeta(p, 4, pgnoRoot); |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 5420 | if( rc ){ |
| 5421 | releasePage(pRoot); |
| 5422 | return rc; |
| 5423 | } |
danielk1977 | 42741be | 2005-01-08 12:42:39 +0000 | [diff] [blame] | 5424 | |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 5425 | }else{ |
drh | 4f0c587 | 2007-03-26 22:05:01 +0000 | [diff] [blame] | 5426 | rc = allocateBtreePage(pBt, &pRoot, &pgnoRoot, 1, 0); |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 5427 | if( rc ) return rc; |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 5428 | } |
| 5429 | #endif |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 5430 | assert( sqlite3PagerIswriteable(pRoot->pDbPage) ); |
drh | de64713 | 2004-05-07 17:57:49 +0000 | [diff] [blame] | 5431 | zeroPage(pRoot, flags | PTF_LEAF); |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 5432 | sqlite3PagerUnref(pRoot->pDbPage); |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 5433 | *piTable = (int)pgnoRoot; |
| 5434 | return SQLITE_OK; |
| 5435 | } |
| 5436 | |
| 5437 | /* |
| 5438 | ** Erase the given database page and all its children. Return |
| 5439 | ** the page to the freelist. |
| 5440 | */ |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 5441 | static int clearDatabasePage( |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 5442 | BtShared *pBt, /* The BTree that contains the table */ |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 5443 | Pgno pgno, /* Page number to clear */ |
| 5444 | MemPage *pParent, /* Parent page. NULL for the root */ |
| 5445 | int freePageFlag /* Deallocate page if true */ |
| 5446 | ){ |
danielk1977 | 6b456a2 | 2005-03-21 04:04:02 +0000 | [diff] [blame] | 5447 | MemPage *pPage = 0; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 5448 | int rc; |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 5449 | unsigned char *pCell; |
| 5450 | int i; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 5451 | |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 5452 | if( pgno>sqlite3PagerPagecount(pBt->pPager) ){ |
drh | 4928570 | 2005-09-17 15:20:26 +0000 | [diff] [blame] | 5453 | return SQLITE_CORRUPT_BKPT; |
danielk1977 | a1cb183 | 2005-02-12 08:59:55 +0000 | [diff] [blame] | 5454 | } |
| 5455 | |
drh | de64713 | 2004-05-07 17:57:49 +0000 | [diff] [blame] | 5456 | rc = getAndInitPage(pBt, pgno, &pPage, pParent); |
danielk1977 | 6b456a2 | 2005-03-21 04:04:02 +0000 | [diff] [blame] | 5457 | if( rc ) goto cleardatabasepage_out; |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 5458 | for(i=0; i<pPage->nCell; i++){ |
danielk1977 | 1cc5ed8 | 2007-05-16 17:28:43 +0000 | [diff] [blame] | 5459 | pCell = findCell(pPage, i); |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 5460 | if( !pPage->leaf ){ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5461 | rc = clearDatabasePage(pBt, get4byte(pCell), pPage->pParent, 1); |
danielk1977 | 6b456a2 | 2005-03-21 04:04:02 +0000 | [diff] [blame] | 5462 | if( rc ) goto cleardatabasepage_out; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 5463 | } |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 5464 | rc = clearCell(pPage, pCell); |
danielk1977 | 6b456a2 | 2005-03-21 04:04:02 +0000 | [diff] [blame] | 5465 | if( rc ) goto cleardatabasepage_out; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 5466 | } |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 5467 | if( !pPage->leaf ){ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5468 | rc = clearDatabasePage(pBt, get4byte(&pPage->aData[8]), pPage->pParent, 1); |
danielk1977 | 6b456a2 | 2005-03-21 04:04:02 +0000 | [diff] [blame] | 5469 | if( rc ) goto cleardatabasepage_out; |
drh | 2aa679f | 2001-06-25 02:11:07 +0000 | [diff] [blame] | 5470 | } |
| 5471 | if( freePageFlag ){ |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 5472 | rc = freePage(pPage); |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 5473 | }else if( (rc = sqlite3PagerWrite(pPage->pDbPage))==0 ){ |
drh | 3a4c141 | 2004-05-09 20:40:11 +0000 | [diff] [blame] | 5474 | zeroPage(pPage, pPage->aData[0] | PTF_LEAF); |
drh | 2aa679f | 2001-06-25 02:11:07 +0000 | [diff] [blame] | 5475 | } |
danielk1977 | 6b456a2 | 2005-03-21 04:04:02 +0000 | [diff] [blame] | 5476 | |
| 5477 | cleardatabasepage_out: |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 5478 | releasePage(pPage); |
drh | 2aa679f | 2001-06-25 02:11:07 +0000 | [diff] [blame] | 5479 | return rc; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 5480 | } |
| 5481 | |
| 5482 | /* |
drh | ab01f61 | 2004-05-22 02:55:23 +0000 | [diff] [blame] | 5483 | ** Delete all information from a single table in the database. iTable is |
| 5484 | ** the page number of the root of the table. After this routine returns, |
| 5485 | ** the root page is empty, but still exists. |
| 5486 | ** |
| 5487 | ** This routine will fail with SQLITE_LOCKED if there are any open |
| 5488 | ** read cursors on the table. Open write cursors are moved to the |
| 5489 | ** root of the table. |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 5490 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 5491 | int sqlite3BtreeClearTable(Btree *p, int iTable){ |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 5492 | int rc; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 5493 | BtShared *pBt = p->pBt; |
| 5494 | if( p->inTrans!=TRANS_WRITE ){ |
drh | f74b8d9 | 2002-09-01 23:20:45 +0000 | [diff] [blame] | 5495 | return pBt->readOnly ? SQLITE_READONLY : SQLITE_ERROR; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 5496 | } |
drh | 980b1a7 | 2006-08-16 16:42:48 +0000 | [diff] [blame] | 5497 | rc = checkReadLocks(p, iTable, 0); |
| 5498 | if( rc ){ |
| 5499 | return rc; |
drh | ecdc753 | 2001-09-23 02:35:53 +0000 | [diff] [blame] | 5500 | } |
danielk1977 | ed42931 | 2006-01-19 08:43:31 +0000 | [diff] [blame] | 5501 | |
| 5502 | /* Save the position of all cursors open on this table */ |
| 5503 | if( SQLITE_OK!=(rc = saveAllCursors(pBt, iTable, 0)) ){ |
| 5504 | return rc; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 5505 | } |
danielk1977 | ed42931 | 2006-01-19 08:43:31 +0000 | [diff] [blame] | 5506 | |
| 5507 | return clearDatabasePage(pBt, (Pgno)iTable, 0, 0); |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 5508 | } |
| 5509 | |
| 5510 | /* |
| 5511 | ** Erase all information in a table and add the root of the table to |
| 5512 | ** the freelist. Except, the root of the principle table (the one on |
drh | ab01f61 | 2004-05-22 02:55:23 +0000 | [diff] [blame] | 5513 | ** page 1) is never added to the freelist. |
| 5514 | ** |
| 5515 | ** This routine will fail with SQLITE_LOCKED if there are any open |
| 5516 | ** cursors on the table. |
drh | 205f48e | 2004-11-05 00:43:11 +0000 | [diff] [blame] | 5517 | ** |
| 5518 | ** If AUTOVACUUM is enabled and the page at iTable is not the last |
| 5519 | ** root page in the database file, then the last root page |
| 5520 | ** in the database file is moved into the slot formerly occupied by |
| 5521 | ** iTable and that last slot formerly occupied by the last root page |
| 5522 | ** is added to the freelist instead of iTable. In this say, all |
| 5523 | ** root pages are kept at the beginning of the database file, which |
| 5524 | ** is necessary for AUTOVACUUM to work right. *piMoved is set to the |
| 5525 | ** page number that used to be the last root page in the file before |
| 5526 | ** the move. If no page gets moved, *piMoved is set to 0. |
| 5527 | ** The last root page is recorded in meta[3] and the value of |
| 5528 | ** meta[3] is updated by this procedure. |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 5529 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 5530 | int sqlite3BtreeDropTable(Btree *p, int iTable, int *piMoved){ |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 5531 | int rc; |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 5532 | MemPage *pPage = 0; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 5533 | BtShared *pBt = p->pBt; |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 5534 | |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 5535 | if( p->inTrans!=TRANS_WRITE ){ |
drh | f74b8d9 | 2002-09-01 23:20:45 +0000 | [diff] [blame] | 5536 | return pBt->readOnly ? SQLITE_READONLY : SQLITE_ERROR; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 5537 | } |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 5538 | |
danielk1977 | e6efa74 | 2004-11-10 11:55:10 +0000 | [diff] [blame] | 5539 | /* It is illegal to drop a table if any cursors are open on the |
| 5540 | ** database. This is because in auto-vacuum mode the backend may |
| 5541 | ** need to move another root-page to fill a gap left by the deleted |
| 5542 | ** root page. If an open cursor was using this page a problem would |
| 5543 | ** occur. |
| 5544 | */ |
| 5545 | if( pBt->pCursor ){ |
| 5546 | return SQLITE_LOCKED; |
drh | 5df72a5 | 2002-06-06 23:16:05 +0000 | [diff] [blame] | 5547 | } |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 5548 | |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 5549 | rc = sqlite3BtreeGetPage(pBt, (Pgno)iTable, &pPage, 0); |
drh | 2aa679f | 2001-06-25 02:11:07 +0000 | [diff] [blame] | 5550 | if( rc ) return rc; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 5551 | rc = sqlite3BtreeClearTable(p, iTable); |
danielk1977 | 6b456a2 | 2005-03-21 04:04:02 +0000 | [diff] [blame] | 5552 | if( rc ){ |
| 5553 | releasePage(pPage); |
| 5554 | return rc; |
| 5555 | } |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 5556 | |
drh | 205f48e | 2004-11-05 00:43:11 +0000 | [diff] [blame] | 5557 | *piMoved = 0; |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 5558 | |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 5559 | if( iTable>1 ){ |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 5560 | #ifdef SQLITE_OMIT_AUTOVACUUM |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 5561 | rc = freePage(pPage); |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 5562 | releasePage(pPage); |
| 5563 | #else |
| 5564 | if( pBt->autoVacuum ){ |
| 5565 | Pgno maxRootPgno; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 5566 | rc = sqlite3BtreeGetMeta(p, 4, &maxRootPgno); |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 5567 | if( rc!=SQLITE_OK ){ |
| 5568 | releasePage(pPage); |
| 5569 | return rc; |
| 5570 | } |
| 5571 | |
| 5572 | if( iTable==maxRootPgno ){ |
| 5573 | /* If the table being dropped is the table with the largest root-page |
| 5574 | ** number in the database, put the root page on the free list. |
| 5575 | */ |
| 5576 | rc = freePage(pPage); |
| 5577 | releasePage(pPage); |
| 5578 | if( rc!=SQLITE_OK ){ |
| 5579 | return rc; |
| 5580 | } |
| 5581 | }else{ |
| 5582 | /* The table being dropped does not have the largest root-page |
| 5583 | ** number in the database. So move the page that does into the |
| 5584 | ** gap left by the deleted root-page. |
| 5585 | */ |
| 5586 | MemPage *pMove; |
| 5587 | releasePage(pPage); |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 5588 | rc = sqlite3BtreeGetPage(pBt, maxRootPgno, &pMove, 0); |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 5589 | if( rc!=SQLITE_OK ){ |
| 5590 | return rc; |
| 5591 | } |
| 5592 | rc = relocatePage(pBt, pMove, PTRMAP_ROOTPAGE, 0, iTable); |
| 5593 | releasePage(pMove); |
| 5594 | if( rc!=SQLITE_OK ){ |
| 5595 | return rc; |
| 5596 | } |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 5597 | rc = sqlite3BtreeGetPage(pBt, maxRootPgno, &pMove, 0); |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 5598 | if( rc!=SQLITE_OK ){ |
| 5599 | return rc; |
| 5600 | } |
| 5601 | rc = freePage(pMove); |
| 5602 | releasePage(pMove); |
| 5603 | if( rc!=SQLITE_OK ){ |
| 5604 | return rc; |
| 5605 | } |
| 5606 | *piMoved = maxRootPgno; |
| 5607 | } |
| 5608 | |
danielk1977 | 599fcba | 2004-11-08 07:13:13 +0000 | [diff] [blame] | 5609 | /* Set the new 'max-root-page' value in the database header. This |
| 5610 | ** is the old value less one, less one more if that happens to |
| 5611 | ** be a root-page number, less one again if that is the |
| 5612 | ** PENDING_BYTE_PAGE. |
| 5613 | */ |
danielk1977 | 87a6e73 | 2004-11-05 12:58:25 +0000 | [diff] [blame] | 5614 | maxRootPgno--; |
danielk1977 | 599fcba | 2004-11-08 07:13:13 +0000 | [diff] [blame] | 5615 | if( maxRootPgno==PENDING_BYTE_PAGE(pBt) ){ |
| 5616 | maxRootPgno--; |
| 5617 | } |
danielk1977 | 266664d | 2006-02-10 08:24:21 +0000 | [diff] [blame] | 5618 | if( maxRootPgno==PTRMAP_PAGENO(pBt, maxRootPgno) ){ |
danielk1977 | 87a6e73 | 2004-11-05 12:58:25 +0000 | [diff] [blame] | 5619 | maxRootPgno--; |
| 5620 | } |
danielk1977 | 599fcba | 2004-11-08 07:13:13 +0000 | [diff] [blame] | 5621 | assert( maxRootPgno!=PENDING_BYTE_PAGE(pBt) ); |
| 5622 | |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 5623 | rc = sqlite3BtreeUpdateMeta(p, 4, maxRootPgno); |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 5624 | }else{ |
| 5625 | rc = freePage(pPage); |
| 5626 | releasePage(pPage); |
| 5627 | } |
| 5628 | #endif |
drh | 2aa679f | 2001-06-25 02:11:07 +0000 | [diff] [blame] | 5629 | }else{ |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 5630 | /* If sqlite3BtreeDropTable was called on page 1. */ |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 5631 | zeroPage(pPage, PTF_INTKEY|PTF_LEAF ); |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 5632 | releasePage(pPage); |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 5633 | } |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 5634 | return rc; |
| 5635 | } |
| 5636 | |
drh | 001bbcb | 2003-03-19 03:14:00 +0000 | [diff] [blame] | 5637 | |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 5638 | /* |
drh | 23e11ca | 2004-05-04 17:27:28 +0000 | [diff] [blame] | 5639 | ** Read the meta-information out of a database file. Meta[0] |
| 5640 | ** is the number of free pages currently in the database. Meta[1] |
drh | a3b321d | 2004-05-11 09:31:31 +0000 | [diff] [blame] | 5641 | ** through meta[15] are available for use by higher layers. Meta[0] |
| 5642 | ** is read-only, the others are read/write. |
| 5643 | ** |
| 5644 | ** The schema layer numbers meta values differently. At the schema |
| 5645 | ** layer (and the SetCookie and ReadCookie opcodes) the number of |
| 5646 | ** 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] | 5647 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 5648 | int sqlite3BtreeGetMeta(Btree *p, int idx, u32 *pMeta){ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 5649 | DbPage *pDbPage; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 5650 | int rc; |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 5651 | unsigned char *pP1; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 5652 | BtShared *pBt = p->pBt; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 5653 | |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 5654 | /* Reading a meta-data value requires a read-lock on page 1 (and hence |
| 5655 | ** the sqlite_master table. We grab this lock regardless of whether or |
| 5656 | ** not the SQLITE_ReadUncommitted flag is set (the table rooted at page |
| 5657 | ** 1 is treated as a special case by queryTableLock() and lockTable()). |
| 5658 | */ |
| 5659 | rc = queryTableLock(p, 1, READ_LOCK); |
| 5660 | if( rc!=SQLITE_OK ){ |
| 5661 | return rc; |
| 5662 | } |
| 5663 | |
drh | 23e11ca | 2004-05-04 17:27:28 +0000 | [diff] [blame] | 5664 | assert( idx>=0 && idx<=15 ); |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 5665 | rc = sqlite3PagerGet(pBt->pPager, 1, &pDbPage); |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 5666 | if( rc ) return rc; |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 5667 | pP1 = (unsigned char *)sqlite3PagerGetData(pDbPage); |
drh | 23e11ca | 2004-05-04 17:27:28 +0000 | [diff] [blame] | 5668 | *pMeta = get4byte(&pP1[36 + idx*4]); |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 5669 | sqlite3PagerUnref(pDbPage); |
drh | ae15787 | 2004-08-14 19:20:09 +0000 | [diff] [blame] | 5670 | |
danielk1977 | 599fcba | 2004-11-08 07:13:13 +0000 | [diff] [blame] | 5671 | /* If autovacuumed is disabled in this build but we are trying to |
| 5672 | ** access an autovacuumed database, then make the database readonly. |
| 5673 | */ |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 5674 | #ifdef SQLITE_OMIT_AUTOVACUUM |
drh | ae15787 | 2004-08-14 19:20:09 +0000 | [diff] [blame] | 5675 | if( idx==4 && *pMeta>0 ) pBt->readOnly = 1; |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 5676 | #endif |
drh | ae15787 | 2004-08-14 19:20:09 +0000 | [diff] [blame] | 5677 | |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 5678 | /* Grab the read-lock on page 1. */ |
| 5679 | rc = lockTable(p, 1, READ_LOCK); |
| 5680 | return rc; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 5681 | } |
| 5682 | |
| 5683 | /* |
drh | 23e11ca | 2004-05-04 17:27:28 +0000 | [diff] [blame] | 5684 | ** Write meta-information back into the database. Meta[0] is |
| 5685 | ** read-only and may not be written. |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 5686 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 5687 | int sqlite3BtreeUpdateMeta(Btree *p, int idx, u32 iMeta){ |
| 5688 | BtShared *pBt = p->pBt; |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 5689 | unsigned char *pP1; |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 5690 | int rc; |
drh | 23e11ca | 2004-05-04 17:27:28 +0000 | [diff] [blame] | 5691 | assert( idx>=1 && idx<=15 ); |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 5692 | if( p->inTrans!=TRANS_WRITE ){ |
drh | f74b8d9 | 2002-09-01 23:20:45 +0000 | [diff] [blame] | 5693 | return pBt->readOnly ? SQLITE_READONLY : SQLITE_ERROR; |
drh | 5df72a5 | 2002-06-06 23:16:05 +0000 | [diff] [blame] | 5694 | } |
drh | de64713 | 2004-05-07 17:57:49 +0000 | [diff] [blame] | 5695 | assert( pBt->pPage1!=0 ); |
| 5696 | pP1 = pBt->pPage1->aData; |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 5697 | rc = sqlite3PagerWrite(pBt->pPage1->pDbPage); |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 5698 | if( rc ) return rc; |
drh | 23e11ca | 2004-05-04 17:27:28 +0000 | [diff] [blame] | 5699 | put4byte(&pP1[36 + idx*4], iMeta); |
danielk1977 | 27b1f95 | 2007-06-25 08:16:58 +0000 | [diff] [blame] | 5700 | if( idx==7 ){ |
| 5701 | assert( pBt->autoVacuum || iMeta==0 ); |
| 5702 | assert( iMeta==0 || iMeta==1 ); |
| 5703 | pBt->incrVacuum = iMeta; |
| 5704 | } |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 5705 | return SQLITE_OK; |
| 5706 | } |
drh | 8c42ca9 | 2001-06-22 19:15:00 +0000 | [diff] [blame] | 5707 | |
drh | f328bc8 | 2004-05-10 23:29:49 +0000 | [diff] [blame] | 5708 | /* |
| 5709 | ** Return the flag byte at the beginning of the page that the cursor |
| 5710 | ** is currently pointing to. |
| 5711 | */ |
| 5712 | int sqlite3BtreeFlags(BtCursor *pCur){ |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 5713 | /* TODO: What about CURSOR_REQUIRESEEK state? Probably need to call |
drh | 777e4c4 | 2006-01-13 04:31:58 +0000 | [diff] [blame] | 5714 | ** restoreOrClearCursorPosition() here. |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 5715 | */ |
drh | f328bc8 | 2004-05-10 23:29:49 +0000 | [diff] [blame] | 5716 | MemPage *pPage = pCur->pPage; |
| 5717 | return pPage ? pPage->aData[pPage->hdrOffset] : 0; |
| 5718 | } |
| 5719 | |
drh | dd79342 | 2001-06-28 01:54:48 +0000 | [diff] [blame] | 5720 | |
drh | dd79342 | 2001-06-28 01:54:48 +0000 | [diff] [blame] | 5721 | /* |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 5722 | ** Return the pager associated with a BTree. This routine is used for |
| 5723 | ** testing and debugging only. |
drh | dd79342 | 2001-06-28 01:54:48 +0000 | [diff] [blame] | 5724 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 5725 | Pager *sqlite3BtreePager(Btree *p){ |
| 5726 | return p->pBt->pPager; |
drh | dd79342 | 2001-06-28 01:54:48 +0000 | [diff] [blame] | 5727 | } |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 5728 | |
drh | b7f9164 | 2004-10-31 02:22:47 +0000 | [diff] [blame] | 5729 | #ifndef SQLITE_OMIT_INTEGRITY_CHECK |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 5730 | /* |
| 5731 | ** Append a message to the error message string. |
| 5732 | */ |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 5733 | static void checkAppendMsg( |
| 5734 | IntegrityCk *pCheck, |
| 5735 | char *zMsg1, |
| 5736 | const char *zFormat, |
| 5737 | ... |
| 5738 | ){ |
| 5739 | va_list ap; |
| 5740 | char *zMsg2; |
drh | 1dcdbc0 | 2007-01-27 02:24:54 +0000 | [diff] [blame] | 5741 | if( !pCheck->mxErr ) return; |
| 5742 | pCheck->mxErr--; |
| 5743 | pCheck->nErr++; |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 5744 | va_start(ap, zFormat); |
| 5745 | zMsg2 = sqlite3VMPrintf(zFormat, ap); |
| 5746 | va_end(ap); |
| 5747 | if( zMsg1==0 ) zMsg1 = ""; |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 5748 | if( pCheck->zErrMsg ){ |
| 5749 | char *zOld = pCheck->zErrMsg; |
| 5750 | pCheck->zErrMsg = 0; |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 5751 | sqlite3SetString(&pCheck->zErrMsg, zOld, "\n", zMsg1, zMsg2, (char*)0); |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 5752 | sqliteFree(zOld); |
| 5753 | }else{ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 5754 | sqlite3SetString(&pCheck->zErrMsg, zMsg1, zMsg2, (char*)0); |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 5755 | } |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 5756 | sqliteFree(zMsg2); |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 5757 | } |
drh | b7f9164 | 2004-10-31 02:22:47 +0000 | [diff] [blame] | 5758 | #endif /* SQLITE_OMIT_INTEGRITY_CHECK */ |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 5759 | |
drh | b7f9164 | 2004-10-31 02:22:47 +0000 | [diff] [blame] | 5760 | #ifndef SQLITE_OMIT_INTEGRITY_CHECK |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 5761 | /* |
| 5762 | ** Add 1 to the reference count for page iPage. If this is the second |
| 5763 | ** reference to the page, add an error message to pCheck->zErrMsg. |
| 5764 | ** Return 1 if there are 2 ore more references to the page and 0 if |
| 5765 | ** if this is the first reference to the page. |
| 5766 | ** |
| 5767 | ** Also check that the page number is in bounds. |
| 5768 | */ |
drh | aaab572 | 2002-02-19 13:39:21 +0000 | [diff] [blame] | 5769 | static int checkRef(IntegrityCk *pCheck, int iPage, char *zContext){ |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 5770 | if( iPage==0 ) return 1; |
drh | 0de8c11 | 2002-07-06 16:32:14 +0000 | [diff] [blame] | 5771 | if( iPage>pCheck->nPage || iPage<0 ){ |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 5772 | checkAppendMsg(pCheck, zContext, "invalid page number %d", iPage); |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 5773 | return 1; |
| 5774 | } |
| 5775 | if( pCheck->anRef[iPage]==1 ){ |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 5776 | checkAppendMsg(pCheck, zContext, "2nd reference to page %d", iPage); |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 5777 | return 1; |
| 5778 | } |
| 5779 | return (pCheck->anRef[iPage]++)>1; |
| 5780 | } |
| 5781 | |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 5782 | #ifndef SQLITE_OMIT_AUTOVACUUM |
| 5783 | /* |
| 5784 | ** Check that the entry in the pointer-map for page iChild maps to |
| 5785 | ** page iParent, pointer type ptrType. If not, append an error message |
| 5786 | ** to pCheck. |
| 5787 | */ |
| 5788 | static void checkPtrmap( |
| 5789 | IntegrityCk *pCheck, /* Integrity check context */ |
| 5790 | Pgno iChild, /* Child page number */ |
| 5791 | u8 eType, /* Expected pointer map type */ |
| 5792 | Pgno iParent, /* Expected pointer map parent page number */ |
| 5793 | char *zContext /* Context description (used for error msg) */ |
| 5794 | ){ |
| 5795 | int rc; |
| 5796 | u8 ePtrmapType; |
| 5797 | Pgno iPtrmapParent; |
| 5798 | |
| 5799 | rc = ptrmapGet(pCheck->pBt, iChild, &ePtrmapType, &iPtrmapParent); |
| 5800 | if( rc!=SQLITE_OK ){ |
| 5801 | checkAppendMsg(pCheck, zContext, "Failed to read ptrmap key=%d", iChild); |
| 5802 | return; |
| 5803 | } |
| 5804 | |
| 5805 | if( ePtrmapType!=eType || iPtrmapParent!=iParent ){ |
| 5806 | checkAppendMsg(pCheck, zContext, |
| 5807 | "Bad ptr map entry key=%d expected=(%d,%d) got=(%d,%d)", |
| 5808 | iChild, eType, iParent, ePtrmapType, iPtrmapParent); |
| 5809 | } |
| 5810 | } |
| 5811 | #endif |
| 5812 | |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 5813 | /* |
| 5814 | ** Check the integrity of the freelist or of an overflow page list. |
| 5815 | ** Verify that the number of pages on the list is N. |
| 5816 | */ |
drh | 30e5875 | 2002-03-02 20:41:57 +0000 | [diff] [blame] | 5817 | static void checkList( |
| 5818 | IntegrityCk *pCheck, /* Integrity checking context */ |
| 5819 | int isFreeList, /* True for a freelist. False for overflow page list */ |
| 5820 | int iPage, /* Page number for first page in the list */ |
| 5821 | int N, /* Expected number of pages in the list */ |
| 5822 | char *zContext /* Context for error messages */ |
| 5823 | ){ |
| 5824 | int i; |
drh | 3a4c141 | 2004-05-09 20:40:11 +0000 | [diff] [blame] | 5825 | int expected = N; |
| 5826 | int iFirst = iPage; |
drh | 1dcdbc0 | 2007-01-27 02:24:54 +0000 | [diff] [blame] | 5827 | while( N-- > 0 && pCheck->mxErr ){ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 5828 | DbPage *pOvflPage; |
| 5829 | unsigned char *pOvflData; |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 5830 | if( iPage<1 ){ |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 5831 | checkAppendMsg(pCheck, zContext, |
| 5832 | "%d of %d pages missing from overflow list starting at %d", |
drh | 3a4c141 | 2004-05-09 20:40:11 +0000 | [diff] [blame] | 5833 | N+1, expected, iFirst); |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 5834 | break; |
| 5835 | } |
| 5836 | if( checkRef(pCheck, iPage, zContext) ) break; |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 5837 | if( sqlite3PagerGet(pCheck->pPager, (Pgno)iPage, &pOvflPage) ){ |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 5838 | checkAppendMsg(pCheck, zContext, "failed to get page %d", iPage); |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 5839 | break; |
| 5840 | } |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 5841 | pOvflData = (unsigned char *)sqlite3PagerGetData(pOvflPage); |
drh | 30e5875 | 2002-03-02 20:41:57 +0000 | [diff] [blame] | 5842 | if( isFreeList ){ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 5843 | int n = get4byte(&pOvflData[4]); |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 5844 | #ifndef SQLITE_OMIT_AUTOVACUUM |
| 5845 | if( pCheck->pBt->autoVacuum ){ |
| 5846 | checkPtrmap(pCheck, iPage, PTRMAP_FREEPAGE, 0, zContext); |
| 5847 | } |
| 5848 | #endif |
drh | 855eb1c | 2004-08-31 13:45:11 +0000 | [diff] [blame] | 5849 | if( n>pCheck->pBt->usableSize/4-8 ){ |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 5850 | checkAppendMsg(pCheck, zContext, |
| 5851 | "freelist leaf count too big on page %d", iPage); |
drh | ee696e2 | 2004-08-30 16:52:17 +0000 | [diff] [blame] | 5852 | N--; |
| 5853 | }else{ |
| 5854 | for(i=0; i<n; i++){ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 5855 | Pgno iFreePage = get4byte(&pOvflData[8+i*4]); |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 5856 | #ifndef SQLITE_OMIT_AUTOVACUUM |
| 5857 | if( pCheck->pBt->autoVacuum ){ |
| 5858 | checkPtrmap(pCheck, iFreePage, PTRMAP_FREEPAGE, 0, zContext); |
| 5859 | } |
| 5860 | #endif |
| 5861 | checkRef(pCheck, iFreePage, zContext); |
drh | ee696e2 | 2004-08-30 16:52:17 +0000 | [diff] [blame] | 5862 | } |
| 5863 | N -= n; |
drh | 30e5875 | 2002-03-02 20:41:57 +0000 | [diff] [blame] | 5864 | } |
drh | 30e5875 | 2002-03-02 20:41:57 +0000 | [diff] [blame] | 5865 | } |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 5866 | #ifndef SQLITE_OMIT_AUTOVACUUM |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 5867 | else{ |
| 5868 | /* If this database supports auto-vacuum and iPage is not the last |
| 5869 | ** page in this overflow list, check that the pointer-map entry for |
| 5870 | ** the following page matches iPage. |
| 5871 | */ |
| 5872 | if( pCheck->pBt->autoVacuum && N>0 ){ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 5873 | i = get4byte(pOvflData); |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 5874 | checkPtrmap(pCheck, i, PTRMAP_OVERFLOW2, iPage, zContext); |
| 5875 | } |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 5876 | } |
| 5877 | #endif |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 5878 | iPage = get4byte(pOvflData); |
| 5879 | sqlite3PagerUnref(pOvflPage); |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 5880 | } |
| 5881 | } |
drh | b7f9164 | 2004-10-31 02:22:47 +0000 | [diff] [blame] | 5882 | #endif /* SQLITE_OMIT_INTEGRITY_CHECK */ |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 5883 | |
drh | b7f9164 | 2004-10-31 02:22:47 +0000 | [diff] [blame] | 5884 | #ifndef SQLITE_OMIT_INTEGRITY_CHECK |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 5885 | /* |
| 5886 | ** Do various sanity checks on a single page of a tree. Return |
| 5887 | ** the tree depth. Root pages return 0. Parents of root pages |
| 5888 | ** return 1, and so forth. |
| 5889 | ** |
| 5890 | ** These checks are done: |
| 5891 | ** |
| 5892 | ** 1. Make sure that cells and freeblocks do not overlap |
| 5893 | ** but combine to completely cover the page. |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 5894 | ** NO 2. Make sure cell keys are in order. |
| 5895 | ** NO 3. Make sure no key is less than or equal to zLowerBound. |
| 5896 | ** NO 4. Make sure no key is greater than or equal to zUpperBound. |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 5897 | ** 5. Check the integrity of overflow pages. |
| 5898 | ** 6. Recursively call checkTreePage on all children. |
| 5899 | ** 7. Verify that the depth of all children is the same. |
drh | 6019e16 | 2001-07-02 17:51:45 +0000 | [diff] [blame] | 5900 | ** 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] | 5901 | ** the root of the tree. |
| 5902 | */ |
| 5903 | static int checkTreePage( |
drh | aaab572 | 2002-02-19 13:39:21 +0000 | [diff] [blame] | 5904 | IntegrityCk *pCheck, /* Context for the sanity check */ |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 5905 | int iPage, /* Page number of the page to check */ |
| 5906 | MemPage *pParent, /* Parent page */ |
drh | 7416170 | 2006-02-24 02:53:49 +0000 | [diff] [blame] | 5907 | char *zParentContext /* Parent context */ |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 5908 | ){ |
| 5909 | MemPage *pPage; |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 5910 | int i, rc, depth, d2, pgno, cnt; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5911 | int hdr, cellStart; |
| 5912 | int nCell; |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 5913 | u8 *data; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 5914 | BtShared *pBt; |
drh | 4f26bb6 | 2005-09-08 14:17:20 +0000 | [diff] [blame] | 5915 | int usableSize; |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 5916 | char zContext[100]; |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 5917 | char *hit; |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 5918 | |
drh | 5bb3eb9 | 2007-05-04 13:15:55 +0000 | [diff] [blame] | 5919 | sqlite3_snprintf(sizeof(zContext), zContext, "Page %d: ", iPage); |
danielk1977 | ef73ee9 | 2004-11-06 12:26:07 +0000 | [diff] [blame] | 5920 | |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 5921 | /* Check that the page exists |
| 5922 | */ |
drh | d9cb6ac | 2005-10-20 07:28:17 +0000 | [diff] [blame] | 5923 | pBt = pCheck->pBt; |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 5924 | usableSize = pBt->usableSize; |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 5925 | if( iPage==0 ) return 0; |
| 5926 | if( checkRef(pCheck, iPage, zParentContext) ) return 0; |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 5927 | if( (rc = sqlite3BtreeGetPage(pBt, (Pgno)iPage, &pPage, 0))!=0 ){ |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 5928 | checkAppendMsg(pCheck, zContext, |
| 5929 | "unable to get the page. error code=%d", rc); |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 5930 | return 0; |
| 5931 | } |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 5932 | if( (rc = sqlite3BtreeInitPage(pPage, pParent))!=0 ){ |
| 5933 | checkAppendMsg(pCheck, zContext, |
| 5934 | "sqlite3BtreeInitPage() returns error code %d", rc); |
drh | 9102529 | 2004-05-03 19:49:32 +0000 | [diff] [blame] | 5935 | releasePage(pPage); |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 5936 | return 0; |
| 5937 | } |
| 5938 | |
| 5939 | /* Check out all the cells. |
| 5940 | */ |
| 5941 | depth = 0; |
drh | 1dcdbc0 | 2007-01-27 02:24:54 +0000 | [diff] [blame] | 5942 | for(i=0; i<pPage->nCell && pCheck->mxErr; i++){ |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 5943 | u8 *pCell; |
| 5944 | int sz; |
| 5945 | CellInfo info; |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 5946 | |
| 5947 | /* Check payload overflow pages |
| 5948 | */ |
drh | 5bb3eb9 | 2007-05-04 13:15:55 +0000 | [diff] [blame] | 5949 | sqlite3_snprintf(sizeof(zContext), zContext, |
| 5950 | "On tree page %d cell %d: ", iPage, i); |
danielk1977 | 1cc5ed8 | 2007-05-16 17:28:43 +0000 | [diff] [blame] | 5951 | pCell = findCell(pPage,i); |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 5952 | sqlite3BtreeParseCellPtr(pPage, pCell, &info); |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 5953 | sz = info.nData; |
| 5954 | if( !pPage->intKey ) sz += info.nKey; |
drh | 7236583 | 2007-03-06 15:53:44 +0000 | [diff] [blame] | 5955 | assert( sz==info.nPayload ); |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 5956 | if( sz>info.nLocal ){ |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 5957 | int nPage = (sz - info.nLocal + usableSize - 5)/(usableSize - 4); |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 5958 | Pgno pgnoOvfl = get4byte(&pCell[info.iOverflow]); |
| 5959 | #ifndef SQLITE_OMIT_AUTOVACUUM |
| 5960 | if( pBt->autoVacuum ){ |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 5961 | checkPtrmap(pCheck, pgnoOvfl, PTRMAP_OVERFLOW1, iPage, zContext); |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 5962 | } |
| 5963 | #endif |
| 5964 | checkList(pCheck, 0, pgnoOvfl, nPage, zContext); |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 5965 | } |
| 5966 | |
| 5967 | /* Check sanity of left child page. |
| 5968 | */ |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 5969 | if( !pPage->leaf ){ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5970 | pgno = get4byte(pCell); |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 5971 | #ifndef SQLITE_OMIT_AUTOVACUUM |
| 5972 | if( pBt->autoVacuum ){ |
| 5973 | checkPtrmap(pCheck, pgno, PTRMAP_BTREE, iPage, zContext); |
| 5974 | } |
| 5975 | #endif |
drh | 7416170 | 2006-02-24 02:53:49 +0000 | [diff] [blame] | 5976 | d2 = checkTreePage(pCheck,pgno,pPage,zContext); |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 5977 | if( i>0 && d2!=depth ){ |
| 5978 | checkAppendMsg(pCheck, zContext, "Child page depth differs"); |
| 5979 | } |
| 5980 | depth = d2; |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 5981 | } |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 5982 | } |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 5983 | if( !pPage->leaf ){ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5984 | pgno = get4byte(&pPage->aData[pPage->hdrOffset+8]); |
drh | 5bb3eb9 | 2007-05-04 13:15:55 +0000 | [diff] [blame] | 5985 | sqlite3_snprintf(sizeof(zContext), zContext, |
| 5986 | "On page %d at right child: ", iPage); |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 5987 | #ifndef SQLITE_OMIT_AUTOVACUUM |
| 5988 | if( pBt->autoVacuum ){ |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 5989 | checkPtrmap(pCheck, pgno, PTRMAP_BTREE, iPage, 0); |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 5990 | } |
| 5991 | #endif |
drh | 7416170 | 2006-02-24 02:53:49 +0000 | [diff] [blame] | 5992 | checkTreePage(pCheck, pgno, pPage, zContext); |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 5993 | } |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 5994 | |
| 5995 | /* Check for complete coverage of the page |
| 5996 | */ |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 5997 | data = pPage->aData; |
| 5998 | hdr = pPage->hdrOffset; |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 5999 | hit = sqliteMalloc( usableSize ); |
| 6000 | if( hit ){ |
| 6001 | memset(hit, 1, get2byte(&data[hdr+5])); |
| 6002 | nCell = get2byte(&data[hdr+3]); |
| 6003 | cellStart = hdr + 12 - 4*pPage->leaf; |
| 6004 | for(i=0; i<nCell; i++){ |
| 6005 | int pc = get2byte(&data[cellStart+i*2]); |
| 6006 | int size = cellSizePtr(pPage, &data[pc]); |
| 6007 | int j; |
danielk1977 | 7701e81 | 2005-01-10 12:59:51 +0000 | [diff] [blame] | 6008 | if( (pc+size-1)>=usableSize || pc<0 ){ |
| 6009 | checkAppendMsg(pCheck, 0, |
| 6010 | "Corruption detected in cell %d on page %d",i,iPage,0); |
| 6011 | }else{ |
| 6012 | for(j=pc+size-1; j>=pc; j--) hit[j]++; |
| 6013 | } |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 6014 | } |
| 6015 | for(cnt=0, i=get2byte(&data[hdr+1]); i>0 && i<usableSize && cnt<10000; |
| 6016 | cnt++){ |
| 6017 | int size = get2byte(&data[i+2]); |
| 6018 | int j; |
danielk1977 | 7701e81 | 2005-01-10 12:59:51 +0000 | [diff] [blame] | 6019 | if( (i+size-1)>=usableSize || i<0 ){ |
| 6020 | checkAppendMsg(pCheck, 0, |
| 6021 | "Corruption detected in cell %d on page %d",i,iPage,0); |
| 6022 | }else{ |
| 6023 | for(j=i+size-1; j>=i; j--) hit[j]++; |
| 6024 | } |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 6025 | i = get2byte(&data[i]); |
| 6026 | } |
| 6027 | for(i=cnt=0; i<usableSize; i++){ |
| 6028 | if( hit[i]==0 ){ |
| 6029 | cnt++; |
| 6030 | }else if( hit[i]>1 ){ |
| 6031 | checkAppendMsg(pCheck, 0, |
| 6032 | "Multiple uses for byte %d of page %d", i, iPage); |
| 6033 | break; |
| 6034 | } |
| 6035 | } |
| 6036 | if( cnt!=data[hdr+7] ){ |
| 6037 | checkAppendMsg(pCheck, 0, |
| 6038 | "Fragmented space is %d byte reported as %d on page %d", |
| 6039 | cnt, data[hdr+7], iPage); |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6040 | } |
| 6041 | } |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 6042 | sqliteFree(hit); |
drh | 6019e16 | 2001-07-02 17:51:45 +0000 | [diff] [blame] | 6043 | |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 6044 | releasePage(pPage); |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 6045 | return depth+1; |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6046 | } |
drh | b7f9164 | 2004-10-31 02:22:47 +0000 | [diff] [blame] | 6047 | #endif /* SQLITE_OMIT_INTEGRITY_CHECK */ |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6048 | |
drh | b7f9164 | 2004-10-31 02:22:47 +0000 | [diff] [blame] | 6049 | #ifndef SQLITE_OMIT_INTEGRITY_CHECK |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6050 | /* |
| 6051 | ** This routine does a complete check of the given BTree file. aRoot[] is |
| 6052 | ** an array of pages numbers were each page number is the root page of |
| 6053 | ** a table. nRoot is the number of entries in aRoot. |
| 6054 | ** |
| 6055 | ** If everything checks out, this routine returns NULL. If something is |
| 6056 | ** amiss, an error message is written into memory obtained from malloc() |
| 6057 | ** and a pointer to that error message is returned. The calling function |
| 6058 | ** is responsible for freeing the error message when it is done. |
| 6059 | */ |
drh | 1dcdbc0 | 2007-01-27 02:24:54 +0000 | [diff] [blame] | 6060 | char *sqlite3BtreeIntegrityCheck( |
| 6061 | Btree *p, /* The btree to be checked */ |
| 6062 | int *aRoot, /* An array of root pages numbers for individual trees */ |
| 6063 | int nRoot, /* Number of entries in aRoot[] */ |
| 6064 | int mxErr, /* Stop reporting errors after this many */ |
| 6065 | int *pnErr /* Write number of errors seen to this variable */ |
| 6066 | ){ |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6067 | int i; |
| 6068 | int nRef; |
drh | aaab572 | 2002-02-19 13:39:21 +0000 | [diff] [blame] | 6069 | IntegrityCk sCheck; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 6070 | BtShared *pBt = p->pBt; |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6071 | |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 6072 | nRef = sqlite3PagerRefcount(pBt->pPager); |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 6073 | if( lockBtreeWithRetry(p)!=SQLITE_OK ){ |
drh | efc251d | 2001-07-01 22:12:01 +0000 | [diff] [blame] | 6074 | return sqliteStrDup("Unable to acquire a read lock on the database"); |
| 6075 | } |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6076 | sCheck.pBt = pBt; |
| 6077 | sCheck.pPager = pBt->pPager; |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 6078 | sCheck.nPage = sqlite3PagerPagecount(sCheck.pPager); |
drh | 1dcdbc0 | 2007-01-27 02:24:54 +0000 | [diff] [blame] | 6079 | sCheck.mxErr = mxErr; |
| 6080 | sCheck.nErr = 0; |
| 6081 | *pnErr = 0; |
danielk1977 | e5321f0 | 2007-04-27 07:05:44 +0000 | [diff] [blame] | 6082 | #ifndef SQLITE_OMIT_AUTOVACUUM |
| 6083 | if( pBt->nTrunc!=0 ){ |
| 6084 | sCheck.nPage = pBt->nTrunc; |
| 6085 | } |
| 6086 | #endif |
drh | 0de8c11 | 2002-07-06 16:32:14 +0000 | [diff] [blame] | 6087 | if( sCheck.nPage==0 ){ |
| 6088 | unlockBtreeIfUnused(pBt); |
| 6089 | return 0; |
| 6090 | } |
drh | 8c1238a | 2003-01-02 14:43:55 +0000 | [diff] [blame] | 6091 | sCheck.anRef = sqliteMallocRaw( (sCheck.nPage+1)*sizeof(sCheck.anRef[0]) ); |
danielk1977 | ac245ec | 2005-01-14 13:50:11 +0000 | [diff] [blame] | 6092 | if( !sCheck.anRef ){ |
| 6093 | unlockBtreeIfUnused(pBt); |
drh | 1dcdbc0 | 2007-01-27 02:24:54 +0000 | [diff] [blame] | 6094 | *pnErr = 1; |
danielk1977 | ac245ec | 2005-01-14 13:50:11 +0000 | [diff] [blame] | 6095 | return sqlite3MPrintf("Unable to malloc %d bytes", |
| 6096 | (sCheck.nPage+1)*sizeof(sCheck.anRef[0])); |
| 6097 | } |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 6098 | for(i=0; i<=sCheck.nPage; i++){ sCheck.anRef[i] = 0; } |
drh | 42cac6d | 2004-11-20 20:31:11 +0000 | [diff] [blame] | 6099 | i = PENDING_BYTE_PAGE(pBt); |
drh | 1f59571 | 2004-06-15 01:40:29 +0000 | [diff] [blame] | 6100 | if( i<=sCheck.nPage ){ |
| 6101 | sCheck.anRef[i] = 1; |
| 6102 | } |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6103 | sCheck.zErrMsg = 0; |
| 6104 | |
| 6105 | /* Check the integrity of the freelist |
| 6106 | */ |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 6107 | checkList(&sCheck, 1, get4byte(&pBt->pPage1->aData[32]), |
| 6108 | get4byte(&pBt->pPage1->aData[36]), "Main freelist: "); |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6109 | |
| 6110 | /* Check all the tables. |
| 6111 | */ |
drh | 1dcdbc0 | 2007-01-27 02:24:54 +0000 | [diff] [blame] | 6112 | for(i=0; i<nRoot && sCheck.mxErr; i++){ |
drh | 4ff6dfa | 2002-03-03 23:06:00 +0000 | [diff] [blame] | 6113 | if( aRoot[i]==0 ) continue; |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 6114 | #ifndef SQLITE_OMIT_AUTOVACUUM |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 6115 | if( pBt->autoVacuum && aRoot[i]>1 ){ |
| 6116 | checkPtrmap(&sCheck, aRoot[i], PTRMAP_ROOTPAGE, 0, 0); |
| 6117 | } |
| 6118 | #endif |
drh | 7416170 | 2006-02-24 02:53:49 +0000 | [diff] [blame] | 6119 | checkTreePage(&sCheck, aRoot[i], 0, "List of tree roots: "); |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6120 | } |
| 6121 | |
| 6122 | /* Make sure every page in the file is referenced |
| 6123 | */ |
drh | 1dcdbc0 | 2007-01-27 02:24:54 +0000 | [diff] [blame] | 6124 | for(i=1; i<=sCheck.nPage && sCheck.mxErr; i++){ |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 6125 | #ifdef SQLITE_OMIT_AUTOVACUUM |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6126 | if( sCheck.anRef[i]==0 ){ |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 6127 | checkAppendMsg(&sCheck, 0, "Page %d is never used", i); |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6128 | } |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 6129 | #else |
| 6130 | /* If the database supports auto-vacuum, make sure no tables contain |
| 6131 | ** references to pointer-map pages. |
| 6132 | */ |
| 6133 | if( sCheck.anRef[i]==0 && |
danielk1977 | 266664d | 2006-02-10 08:24:21 +0000 | [diff] [blame] | 6134 | (PTRMAP_PAGENO(pBt, i)!=i || !pBt->autoVacuum) ){ |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 6135 | checkAppendMsg(&sCheck, 0, "Page %d is never used", i); |
| 6136 | } |
| 6137 | if( sCheck.anRef[i]!=0 && |
danielk1977 | 266664d | 2006-02-10 08:24:21 +0000 | [diff] [blame] | 6138 | (PTRMAP_PAGENO(pBt, i)==i && pBt->autoVacuum) ){ |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 6139 | checkAppendMsg(&sCheck, 0, "Pointer map page %d is referenced", i); |
| 6140 | } |
| 6141 | #endif |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6142 | } |
| 6143 | |
| 6144 | /* Make sure this analysis did not leave any unref() pages |
| 6145 | */ |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 6146 | unlockBtreeIfUnused(pBt); |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 6147 | if( nRef != sqlite3PagerRefcount(pBt->pPager) ){ |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 6148 | checkAppendMsg(&sCheck, 0, |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6149 | "Outstanding page count goes from %d to %d during this analysis", |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 6150 | nRef, sqlite3PagerRefcount(pBt->pPager) |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6151 | ); |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6152 | } |
| 6153 | |
| 6154 | /* Clean up and report errors. |
| 6155 | */ |
| 6156 | sqliteFree(sCheck.anRef); |
drh | 1dcdbc0 | 2007-01-27 02:24:54 +0000 | [diff] [blame] | 6157 | *pnErr = sCheck.nErr; |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6158 | return sCheck.zErrMsg; |
| 6159 | } |
drh | b7f9164 | 2004-10-31 02:22:47 +0000 | [diff] [blame] | 6160 | #endif /* SQLITE_OMIT_INTEGRITY_CHECK */ |
paul | b95a886 | 2003-04-01 21:16:41 +0000 | [diff] [blame] | 6161 | |
drh | 73509ee | 2003-04-06 20:44:45 +0000 | [diff] [blame] | 6162 | /* |
| 6163 | ** Return the full pathname of the underlying database file. |
| 6164 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 6165 | const char *sqlite3BtreeGetFilename(Btree *p){ |
| 6166 | assert( p->pBt->pPager!=0 ); |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 6167 | return sqlite3PagerFilename(p->pBt->pPager); |
drh | 73509ee | 2003-04-06 20:44:45 +0000 | [diff] [blame] | 6168 | } |
| 6169 | |
| 6170 | /* |
danielk1977 | 5865e3d | 2004-06-14 06:03:57 +0000 | [diff] [blame] | 6171 | ** Return the pathname of the directory that contains the database file. |
| 6172 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 6173 | const char *sqlite3BtreeGetDirname(Btree *p){ |
| 6174 | assert( p->pBt->pPager!=0 ); |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 6175 | return sqlite3PagerDirname(p->pBt->pPager); |
danielk1977 | 5865e3d | 2004-06-14 06:03:57 +0000 | [diff] [blame] | 6176 | } |
| 6177 | |
| 6178 | /* |
| 6179 | ** Return the pathname of the journal file for this database. The return |
| 6180 | ** value of this routine is the same regardless of whether the journal file |
| 6181 | ** has been created or not. |
| 6182 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 6183 | const char *sqlite3BtreeGetJournalname(Btree *p){ |
| 6184 | assert( p->pBt->pPager!=0 ); |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 6185 | return sqlite3PagerJournalname(p->pBt->pPager); |
danielk1977 | 5865e3d | 2004-06-14 06:03:57 +0000 | [diff] [blame] | 6186 | } |
| 6187 | |
drh | b7f9164 | 2004-10-31 02:22:47 +0000 | [diff] [blame] | 6188 | #ifndef SQLITE_OMIT_VACUUM |
danielk1977 | 5865e3d | 2004-06-14 06:03:57 +0000 | [diff] [blame] | 6189 | /* |
drh | f7c5753 | 2003-04-25 13:22:51 +0000 | [diff] [blame] | 6190 | ** Copy the complete content of pBtFrom into pBtTo. A transaction |
| 6191 | ** must be active for both files. |
| 6192 | ** |
| 6193 | ** The size of file pBtFrom may be reduced by this operation. |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 6194 | ** If anything goes wrong, the transaction on pBtFrom is rolled back. |
drh | 73509ee | 2003-04-06 20:44:45 +0000 | [diff] [blame] | 6195 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 6196 | int sqlite3BtreeCopyFile(Btree *pTo, Btree *pFrom){ |
drh | f7c5753 | 2003-04-25 13:22:51 +0000 | [diff] [blame] | 6197 | int rc = SQLITE_OK; |
drh | 50f2f43 | 2005-09-16 11:32:18 +0000 | [diff] [blame] | 6198 | Pgno i, nPage, nToPage, iSkip; |
drh | f7c5753 | 2003-04-25 13:22:51 +0000 | [diff] [blame] | 6199 | |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 6200 | BtShared *pBtTo = pTo->pBt; |
| 6201 | BtShared *pBtFrom = pFrom->pBt; |
| 6202 | |
| 6203 | if( pTo->inTrans!=TRANS_WRITE || pFrom->inTrans!=TRANS_WRITE ){ |
danielk1977 | ee5741e | 2004-05-31 10:01:34 +0000 | [diff] [blame] | 6204 | return SQLITE_ERROR; |
| 6205 | } |
drh | f7c5753 | 2003-04-25 13:22:51 +0000 | [diff] [blame] | 6206 | if( pBtTo->pCursor ) return SQLITE_BUSY; |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 6207 | nToPage = sqlite3PagerPagecount(pBtTo->pPager); |
| 6208 | nPage = sqlite3PagerPagecount(pBtFrom->pPager); |
drh | 50f2f43 | 2005-09-16 11:32:18 +0000 | [diff] [blame] | 6209 | iSkip = PENDING_BYTE_PAGE(pBtTo); |
danielk1977 | 369f27e | 2004-06-15 11:40:04 +0000 | [diff] [blame] | 6210 | for(i=1; rc==SQLITE_OK && i<=nPage; i++){ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 6211 | DbPage *pDbPage; |
drh | 50f2f43 | 2005-09-16 11:32:18 +0000 | [diff] [blame] | 6212 | if( i==iSkip ) continue; |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 6213 | rc = sqlite3PagerGet(pBtFrom->pPager, i, &pDbPage); |
drh | f7c5753 | 2003-04-25 13:22:51 +0000 | [diff] [blame] | 6214 | if( rc ) break; |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 6215 | rc = sqlite3PagerOverwrite(pBtTo->pPager, i, sqlite3PagerGetData(pDbPage)); |
| 6216 | sqlite3PagerUnref(pDbPage); |
drh | f7c5753 | 2003-04-25 13:22:51 +0000 | [diff] [blame] | 6217 | } |
drh | 538f570 | 2007-04-13 02:14:30 +0000 | [diff] [blame] | 6218 | |
| 6219 | /* If the file is shrinking, journal the pages that are being truncated |
| 6220 | ** so that they can be rolled back if the commit fails. |
| 6221 | */ |
drh | 2e6d11b | 2003-04-25 15:37:57 +0000 | [diff] [blame] | 6222 | for(i=nPage+1; rc==SQLITE_OK && i<=nToPage; i++){ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 6223 | DbPage *pDbPage; |
drh | 4928570 | 2005-09-17 15:20:26 +0000 | [diff] [blame] | 6224 | if( i==iSkip ) continue; |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 6225 | rc = sqlite3PagerGet(pBtTo->pPager, i, &pDbPage); |
drh | 2e6d11b | 2003-04-25 15:37:57 +0000 | [diff] [blame] | 6226 | if( rc ) break; |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 6227 | rc = sqlite3PagerWrite(pDbPage); |
drh | 538f570 | 2007-04-13 02:14:30 +0000 | [diff] [blame] | 6228 | sqlite3PagerDontWrite(pDbPage); |
| 6229 | /* Yeah. It seems wierd to call DontWrite() right after Write(). But |
| 6230 | ** that is because the names of those procedures do not exactly |
| 6231 | ** represent what they do. Write() really means "put this page in the |
| 6232 | ** rollback journal and mark it as dirty so that it will be written |
| 6233 | ** to the database file later." DontWrite() undoes the second part of |
| 6234 | ** that and prevents the page from being written to the database. The |
| 6235 | ** page is still on the rollback journal, though. And that is the whole |
| 6236 | ** point of this loop: to put pages on the rollback journal. */ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 6237 | sqlite3PagerUnref(pDbPage); |
drh | 2e6d11b | 2003-04-25 15:37:57 +0000 | [diff] [blame] | 6238 | } |
| 6239 | if( !rc && nPage<nToPage ){ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 6240 | rc = sqlite3PagerTruncate(pBtTo->pPager, nPage); |
drh | 2e6d11b | 2003-04-25 15:37:57 +0000 | [diff] [blame] | 6241 | } |
drh | 538f570 | 2007-04-13 02:14:30 +0000 | [diff] [blame] | 6242 | |
drh | f7c5753 | 2003-04-25 13:22:51 +0000 | [diff] [blame] | 6243 | if( rc ){ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 6244 | sqlite3BtreeRollback(pTo); |
drh | f7c5753 | 2003-04-25 13:22:51 +0000 | [diff] [blame] | 6245 | } |
| 6246 | return rc; |
drh | 73509ee | 2003-04-06 20:44:45 +0000 | [diff] [blame] | 6247 | } |
drh | b7f9164 | 2004-10-31 02:22:47 +0000 | [diff] [blame] | 6248 | #endif /* SQLITE_OMIT_VACUUM */ |
danielk1977 | 1d850a7 | 2004-05-31 08:26:49 +0000 | [diff] [blame] | 6249 | |
| 6250 | /* |
| 6251 | ** Return non-zero if a transaction is active. |
| 6252 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 6253 | int sqlite3BtreeIsInTrans(Btree *p){ |
| 6254 | return (p && (p->inTrans==TRANS_WRITE)); |
danielk1977 | 1d850a7 | 2004-05-31 08:26:49 +0000 | [diff] [blame] | 6255 | } |
| 6256 | |
| 6257 | /* |
| 6258 | ** Return non-zero if a statement transaction is active. |
| 6259 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 6260 | int sqlite3BtreeIsInStmt(Btree *p){ |
| 6261 | return (p->pBt && p->pBt->inStmt); |
danielk1977 | 1d850a7 | 2004-05-31 08:26:49 +0000 | [diff] [blame] | 6262 | } |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 6263 | |
| 6264 | /* |
danielk1977 | 2372c2b | 2006-06-27 16:34:56 +0000 | [diff] [blame] | 6265 | ** Return non-zero if a read (or write) transaction is active. |
| 6266 | */ |
| 6267 | int sqlite3BtreeIsInReadTrans(Btree *p){ |
| 6268 | return (p && (p->inTrans!=TRANS_NONE)); |
| 6269 | } |
| 6270 | |
| 6271 | /* |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 6272 | ** This function returns a pointer to a blob of memory associated with |
| 6273 | ** a single shared-btree. The memory is used by client code for it's own |
| 6274 | ** purposes (for example, to store a high-level schema associated with |
| 6275 | ** the shared-btree). The btree layer manages reference counting issues. |
| 6276 | ** |
| 6277 | ** The first time this is called on a shared-btree, nBytes bytes of memory |
| 6278 | ** are allocated, zeroed, and returned to the caller. For each subsequent |
| 6279 | ** call the nBytes parameter is ignored and a pointer to the same blob |
| 6280 | ** of memory returned. |
| 6281 | ** |
| 6282 | ** Just before the shared-btree is closed, the function passed as the |
| 6283 | ** xFree argument when the memory allocation was made is invoked on the |
| 6284 | ** blob of allocated memory. This function should not call sqliteFree() |
| 6285 | ** on the memory, the btree layer does that. |
| 6286 | */ |
| 6287 | void *sqlite3BtreeSchema(Btree *p, int nBytes, void(*xFree)(void *)){ |
| 6288 | BtShared *pBt = p->pBt; |
| 6289 | if( !pBt->pSchema ){ |
| 6290 | pBt->pSchema = sqliteMalloc(nBytes); |
| 6291 | pBt->xFreeSchema = xFree; |
| 6292 | } |
| 6293 | return pBt->pSchema; |
| 6294 | } |
| 6295 | |
danielk1977 | c87d34d | 2006-01-06 13:00:28 +0000 | [diff] [blame] | 6296 | /* |
| 6297 | ** Return true if another user of the same shared btree as the argument |
| 6298 | ** handle holds an exclusive lock on the sqlite_master table. |
| 6299 | */ |
| 6300 | int sqlite3BtreeSchemaLocked(Btree *p){ |
| 6301 | return (queryTableLock(p, MASTER_ROOT, READ_LOCK)!=SQLITE_OK); |
| 6302 | } |
| 6303 | |
drh | a154dcd | 2006-03-22 22:10:07 +0000 | [diff] [blame] | 6304 | |
| 6305 | #ifndef SQLITE_OMIT_SHARED_CACHE |
| 6306 | /* |
| 6307 | ** Obtain a lock on the table whose root page is iTab. The |
| 6308 | ** lock is a write lock if isWritelock is true or a read lock |
| 6309 | ** if it is false. |
| 6310 | */ |
danielk1977 | c00da10 | 2006-01-07 13:21:04 +0000 | [diff] [blame] | 6311 | int sqlite3BtreeLockTable(Btree *p, int iTab, u8 isWriteLock){ |
danielk1977 | 2e94d4d | 2006-01-09 05:36:27 +0000 | [diff] [blame] | 6312 | int rc = SQLITE_OK; |
danielk1977 | c00da10 | 2006-01-07 13:21:04 +0000 | [diff] [blame] | 6313 | u8 lockType = (isWriteLock?WRITE_LOCK:READ_LOCK); |
danielk1977 | 2e94d4d | 2006-01-09 05:36:27 +0000 | [diff] [blame] | 6314 | rc = queryTableLock(p, iTab, lockType); |
danielk1977 | c00da10 | 2006-01-07 13:21:04 +0000 | [diff] [blame] | 6315 | if( rc==SQLITE_OK ){ |
| 6316 | rc = lockTable(p, iTab, lockType); |
| 6317 | } |
| 6318 | return rc; |
| 6319 | } |
drh | a154dcd | 2006-03-22 22:10:07 +0000 | [diff] [blame] | 6320 | #endif |
danielk1977 | b82e7ed | 2006-01-11 14:09:31 +0000 | [diff] [blame] | 6321 | |
danielk1977 | b4e9af9 | 2007-05-01 17:49:49 +0000 | [diff] [blame] | 6322 | #ifndef SQLITE_OMIT_INCRBLOB |
| 6323 | /* |
| 6324 | ** Argument pCsr must be a cursor opened for writing on an |
| 6325 | ** INTKEY table currently pointing at a valid table entry. |
| 6326 | ** This function modifies the data stored as part of that entry. |
| 6327 | ** Only the data content may only be modified, it is not possible |
| 6328 | ** to change the length of the data stored. |
| 6329 | */ |
danielk1977 | dcbb5d3 | 2007-05-04 18:36:44 +0000 | [diff] [blame] | 6330 | int sqlite3BtreePutData(BtCursor *pCsr, u32 offset, u32 amt, void *z){ |
danielk1977 | d0441796 | 2007-05-02 13:16:30 +0000 | [diff] [blame] | 6331 | |
danielk1977 | dcbb5d3 | 2007-05-04 18:36:44 +0000 | [diff] [blame] | 6332 | assert(pCsr->isIncrblobHandle); |
| 6333 | if( pCsr->eState==CURSOR_REQUIRESEEK ){ |
| 6334 | return SQLITE_ABORT; |
| 6335 | } |
| 6336 | |
danielk1977 | d0441796 | 2007-05-02 13:16:30 +0000 | [diff] [blame] | 6337 | /* Check some preconditions: |
danielk1977 | dcbb5d3 | 2007-05-04 18:36:44 +0000 | [diff] [blame] | 6338 | ** (a) the cursor is open for writing, |
| 6339 | ** (b) there is no read-lock on the table being modified and |
| 6340 | ** (c) the cursor points at a valid row of an intKey table. |
danielk1977 | d0441796 | 2007-05-02 13:16:30 +0000 | [diff] [blame] | 6341 | */ |
danielk1977 | d0441796 | 2007-05-02 13:16:30 +0000 | [diff] [blame] | 6342 | if( !pCsr->wrFlag ){ |
danielk1977 | dcbb5d3 | 2007-05-04 18:36:44 +0000 | [diff] [blame] | 6343 | return SQLITE_READONLY; |
danielk1977 | d0441796 | 2007-05-02 13:16:30 +0000 | [diff] [blame] | 6344 | } |
drh | 87cc3b3 | 2007-05-08 21:45:27 +0000 | [diff] [blame] | 6345 | assert( !pCsr->pBtree->pBt->readOnly |
| 6346 | && pCsr->pBtree->pBt->inTransaction==TRANS_WRITE ); |
danielk1977 | d0441796 | 2007-05-02 13:16:30 +0000 | [diff] [blame] | 6347 | if( checkReadLocks(pCsr->pBtree, pCsr->pgnoRoot, pCsr) ){ |
| 6348 | return SQLITE_LOCKED; /* The table pCur points to has a read lock */ |
| 6349 | } |
| 6350 | if( pCsr->eState==CURSOR_INVALID || !pCsr->pPage->intKey ){ |
| 6351 | return SQLITE_ERROR; |
danielk1977 | b4e9af9 | 2007-05-01 17:49:49 +0000 | [diff] [blame] | 6352 | } |
| 6353 | |
danielk1977 | 9f8d640 | 2007-05-02 17:48:45 +0000 | [diff] [blame] | 6354 | return accessPayload(pCsr, offset, amt, (unsigned char *)z, 0, 1); |
danielk1977 | b4e9af9 | 2007-05-01 17:49:49 +0000 | [diff] [blame] | 6355 | } |
danielk1977 | 2dec970 | 2007-05-02 16:48:37 +0000 | [diff] [blame] | 6356 | |
| 6357 | /* |
| 6358 | ** 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] | 6359 | ** overflow list for the current row. This is used by cursors opened |
| 6360 | ** for incremental blob IO only. |
| 6361 | ** |
| 6362 | ** This function sets a flag only. The actual page location cache |
| 6363 | ** (stored in BtCursor.aOverflow[]) is allocated and used by function |
| 6364 | ** accessPayload() (the worker function for sqlite3BtreeData() and |
| 6365 | ** sqlite3BtreePutData()). |
danielk1977 | 2dec970 | 2007-05-02 16:48:37 +0000 | [diff] [blame] | 6366 | */ |
| 6367 | void sqlite3BtreeCacheOverflow(BtCursor *pCur){ |
danielk1977 | dcbb5d3 | 2007-05-04 18:36:44 +0000 | [diff] [blame] | 6368 | assert(!pCur->isIncrblobHandle); |
danielk1977 | 2dec970 | 2007-05-02 16:48:37 +0000 | [diff] [blame] | 6369 | assert(!pCur->aOverflow); |
danielk1977 | dcbb5d3 | 2007-05-04 18:36:44 +0000 | [diff] [blame] | 6370 | pCur->isIncrblobHandle = 1; |
danielk1977 | 2dec970 | 2007-05-02 16:48:37 +0000 | [diff] [blame] | 6371 | } |
danielk1977 | b4e9af9 | 2007-05-01 17:49:49 +0000 | [diff] [blame] | 6372 | #endif |