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 | d0679ed | 2007-08-28 22:24:34 +0000 | [diff] [blame^] | 12 | ** $Id: btree.c,v 1.414 2007/08/28 22:24:35 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 | /* |
drh | a315289 | 2007-05-05 11:48:52 +0000 | [diff] [blame] | 27 | ** Set this global variable to 1 to enable tracing using the TRACE |
| 28 | ** macro. |
drh | 615ae55 | 2005-01-16 23:21:00 +0000 | [diff] [blame] | 29 | */ |
| 30 | #if SQLITE_TEST |
drh | 0f7eb61 | 2006-08-08 13:51:43 +0000 | [diff] [blame] | 31 | int sqlite3_btree_trace=0; /* True to enable tracing */ |
drh | 615ae55 | 2005-01-16 23:21:00 +0000 | [diff] [blame] | 32 | #endif |
drh | 615ae55 | 2005-01-16 23:21:00 +0000 | [diff] [blame] | 33 | |
drh | 86f8c19 | 2007-08-22 00:39:19 +0000 | [diff] [blame] | 34 | |
| 35 | |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 36 | #ifndef SQLITE_OMIT_SHARED_CACHE |
| 37 | /* |
| 38 | ** A flag to indicate whether or not shared cache is enabled. Also, |
| 39 | ** a list of BtShared objects that are eligible for participation |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 40 | ** in shared cache. The variables have file scope during normal builds, |
drh | 86f8c19 | 2007-08-22 00:39:19 +0000 | [diff] [blame] | 41 | ** but the test harness needs to access these variables so we make them |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 42 | ** global for test builds. |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 43 | */ |
| 44 | #ifdef SQLITE_TEST |
| 45 | BtShared *sqlite3SharedCacheList = 0; |
| 46 | int sqlite3SharedCacheEnabled = 0; |
| 47 | #else |
| 48 | static BtShared *sqlite3SharedCacheList = 0; |
| 49 | static int sqlite3SharedCacheEnabled = 0; |
| 50 | #endif |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 51 | #endif /* SQLITE_OMIT_SHARED_CACHE */ |
| 52 | |
| 53 | #ifndef SQLITE_OMIT_SHARED_CACHE |
| 54 | /* |
| 55 | ** Enable or disable the shared pager and schema features. |
| 56 | ** |
| 57 | ** This routine has no effect on existing database connections. |
| 58 | ** The shared cache setting effects only future calls to |
| 59 | ** sqlite3_open(), sqlite3_open16(), or sqlite3_open_v2(). |
| 60 | */ |
| 61 | int sqlite3_enable_shared_cache(int enable){ |
| 62 | sqlite3SharedCacheEnabled = enable; |
| 63 | return SQLITE_OK; |
| 64 | } |
| 65 | #endif |
| 66 | |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 67 | |
drh | 615ae55 | 2005-01-16 23:21:00 +0000 | [diff] [blame] | 68 | /* |
drh | 66cbd15 | 2004-09-01 16:12:25 +0000 | [diff] [blame] | 69 | ** Forward declaration |
| 70 | */ |
drh | 980b1a7 | 2006-08-16 16:42:48 +0000 | [diff] [blame] | 71 | static int checkReadLocks(Btree*,Pgno,BtCursor*); |
drh | 66cbd15 | 2004-09-01 16:12:25 +0000 | [diff] [blame] | 72 | |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 73 | |
| 74 | #ifdef SQLITE_OMIT_SHARED_CACHE |
| 75 | /* |
| 76 | ** The functions queryTableLock(), lockTable() and unlockAllTables() |
| 77 | ** manipulate entries in the BtShared.pLock linked list used to store |
| 78 | ** shared-cache table level locks. If the library is compiled with the |
| 79 | ** shared-cache feature disabled, then there is only ever one user |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 80 | ** of each BtShared structure and so this locking is not necessary. |
| 81 | ** So define the lock related functions as no-ops. |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 82 | */ |
| 83 | #define queryTableLock(a,b,c) SQLITE_OK |
| 84 | #define lockTable(a,b,c) SQLITE_OK |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 85 | #define unlockAllTables(a) |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 86 | #endif |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 87 | |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 88 | #ifndef SQLITE_OMIT_SHARED_CACHE |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 89 | /* |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 90 | ** Query to see if btree handle p may obtain a lock of type eLock |
| 91 | ** (READ_LOCK or WRITE_LOCK) on the table with root-page iTab. Return |
| 92 | ** SQLITE_OK if the lock may be obtained (by calling lockTable()), or |
danielk1977 | c87d34d | 2006-01-06 13:00:28 +0000 | [diff] [blame] | 93 | ** SQLITE_LOCKED if not. |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 94 | */ |
| 95 | static int queryTableLock(Btree *p, Pgno iTab, u8 eLock){ |
| 96 | BtShared *pBt = p->pBt; |
| 97 | BtLock *pIter; |
| 98 | |
drh | d0679ed | 2007-08-28 22:24:34 +0000 | [diff] [blame^] | 99 | assert( sqlite3BtreeMutexHeld(p->pSqlite->mutex) ); |
drh | 2764170 | 2007-08-22 02:56:42 +0000 | [diff] [blame] | 100 | assert( sqlite3BtreeMutexHeld(pBt->mutex) ); |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 101 | |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 102 | /* This is a no-op if the shared-cache is not enabled */ |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 103 | if( !p->sharable ){ |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 104 | return SQLITE_OK; |
| 105 | } |
| 106 | |
| 107 | /* This (along with lockTable()) is where the ReadUncommitted flag is |
| 108 | ** dealt with. If the caller is querying for a read-lock and the flag is |
| 109 | ** set, it is unconditionally granted - even if there are write-locks |
| 110 | ** on the table. If a write-lock is requested, the ReadUncommitted flag |
| 111 | ** is not considered. |
| 112 | ** |
| 113 | ** In function lockTable(), if a read-lock is demanded and the |
| 114 | ** ReadUncommitted flag is set, no entry is added to the locks list |
| 115 | ** (BtShared.pLock). |
| 116 | ** |
| 117 | ** To summarize: If the ReadUncommitted flag is set, then read cursors do |
| 118 | ** not create or respect table locks. The locking procedure for a |
| 119 | ** write-cursor does not change. |
| 120 | */ |
| 121 | if( |
| 122 | !p->pSqlite || |
| 123 | 0==(p->pSqlite->flags&SQLITE_ReadUncommitted) || |
| 124 | eLock==WRITE_LOCK || |
drh | 47ded16 | 2006-01-06 01:42:58 +0000 | [diff] [blame] | 125 | iTab==MASTER_ROOT |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 126 | ){ |
| 127 | for(pIter=pBt->pLock; pIter; pIter=pIter->pNext){ |
| 128 | if( pIter->pBtree!=p && pIter->iTable==iTab && |
| 129 | (pIter->eLock!=eLock || eLock!=READ_LOCK) ){ |
danielk1977 | c87d34d | 2006-01-06 13:00:28 +0000 | [diff] [blame] | 130 | return SQLITE_LOCKED; |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 131 | } |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 132 | } |
| 133 | } |
| 134 | return SQLITE_OK; |
| 135 | } |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 136 | #endif /* !SQLITE_OMIT_SHARED_CACHE */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 137 | |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 138 | #ifndef SQLITE_OMIT_SHARED_CACHE |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 139 | /* |
| 140 | ** Add a lock on the table with root-page iTable to the shared-btree used |
| 141 | ** by Btree handle p. Parameter eLock must be either READ_LOCK or |
| 142 | ** WRITE_LOCK. |
| 143 | ** |
| 144 | ** SQLITE_OK is returned if the lock is added successfully. SQLITE_BUSY and |
| 145 | ** SQLITE_NOMEM may also be returned. |
| 146 | */ |
| 147 | static int lockTable(Btree *p, Pgno iTable, u8 eLock){ |
| 148 | BtShared *pBt = p->pBt; |
| 149 | BtLock *pLock = 0; |
| 150 | BtLock *pIter; |
| 151 | |
drh | d0679ed | 2007-08-28 22:24:34 +0000 | [diff] [blame^] | 152 | assert( sqlite3BtreeMutexHeld(p->pSqlite->mutex) ); |
drh | 2764170 | 2007-08-22 02:56:42 +0000 | [diff] [blame] | 153 | assert( sqlite3BtreeMutexHeld(pBt->mutex) ); |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 154 | |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 155 | /* This is a no-op if the shared-cache is not enabled */ |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 156 | if( !p->sharable ){ |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 157 | return SQLITE_OK; |
| 158 | } |
| 159 | |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 160 | assert( SQLITE_OK==queryTableLock(p, iTable, eLock) ); |
| 161 | |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 162 | /* If the read-uncommitted flag is set and a read-lock is requested, |
| 163 | ** return early without adding an entry to the BtShared.pLock list. See |
| 164 | ** comment in function queryTableLock() for more info on handling |
| 165 | ** the ReadUncommitted flag. |
| 166 | */ |
| 167 | if( |
| 168 | (p->pSqlite) && |
| 169 | (p->pSqlite->flags&SQLITE_ReadUncommitted) && |
| 170 | (eLock==READ_LOCK) && |
drh | 47ded16 | 2006-01-06 01:42:58 +0000 | [diff] [blame] | 171 | iTable!=MASTER_ROOT |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 172 | ){ |
| 173 | return SQLITE_OK; |
| 174 | } |
| 175 | |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 176 | /* First search the list for an existing lock on this table. */ |
| 177 | for(pIter=pBt->pLock; pIter; pIter=pIter->pNext){ |
| 178 | if( pIter->iTable==iTable && pIter->pBtree==p ){ |
| 179 | pLock = pIter; |
| 180 | break; |
| 181 | } |
| 182 | } |
| 183 | |
| 184 | /* If the above search did not find a BtLock struct associating Btree p |
| 185 | ** with table iTable, allocate one and link it into the list. |
| 186 | */ |
| 187 | if( !pLock ){ |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 188 | pLock = (BtLock *)sqlite3MallocZero(sizeof(BtLock)); |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 189 | if( !pLock ){ |
| 190 | return SQLITE_NOMEM; |
| 191 | } |
| 192 | pLock->iTable = iTable; |
| 193 | pLock->pBtree = p; |
| 194 | pLock->pNext = pBt->pLock; |
| 195 | pBt->pLock = pLock; |
| 196 | } |
| 197 | |
| 198 | /* Set the BtLock.eLock variable to the maximum of the current lock |
| 199 | ** and the requested lock. This means if a write-lock was already held |
| 200 | ** and a read-lock requested, we don't incorrectly downgrade the lock. |
| 201 | */ |
| 202 | assert( WRITE_LOCK>READ_LOCK ); |
danielk1977 | 5118b91 | 2005-12-30 16:31:53 +0000 | [diff] [blame] | 203 | if( eLock>pLock->eLock ){ |
| 204 | pLock->eLock = eLock; |
| 205 | } |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 206 | |
| 207 | return SQLITE_OK; |
| 208 | } |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 209 | #endif /* !SQLITE_OMIT_SHARED_CACHE */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 210 | |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 211 | #ifndef SQLITE_OMIT_SHARED_CACHE |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 212 | /* |
| 213 | ** Release all the table locks (locks obtained via calls to the lockTable() |
| 214 | ** procedure) held by Btree handle p. |
| 215 | */ |
| 216 | static void unlockAllTables(Btree *p){ |
| 217 | BtLock **ppIter = &p->pBt->pLock; |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 218 | |
drh | d0679ed | 2007-08-28 22:24:34 +0000 | [diff] [blame^] | 219 | assert( sqlite3BtreeMutexHeld(p->pSqlite->mutex) ); |
drh | 2764170 | 2007-08-22 02:56:42 +0000 | [diff] [blame] | 220 | assert( sqlite3BtreeMutexHeld(p->pBt->mutex) ); |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 221 | assert( p->sharable || 0==*ppIter ); |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 222 | |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 223 | while( *ppIter ){ |
| 224 | BtLock *pLock = *ppIter; |
| 225 | if( pLock->pBtree==p ){ |
| 226 | *ppIter = pLock->pNext; |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 227 | sqlite3_free(pLock); |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 228 | }else{ |
| 229 | ppIter = &pLock->pNext; |
| 230 | } |
| 231 | } |
| 232 | } |
| 233 | #endif /* SQLITE_OMIT_SHARED_CACHE */ |
| 234 | |
drh | 980b1a7 | 2006-08-16 16:42:48 +0000 | [diff] [blame] | 235 | static void releasePage(MemPage *pPage); /* Forward reference */ |
| 236 | |
danielk1977 | 92d4d7a | 2007-05-04 12:05:56 +0000 | [diff] [blame] | 237 | #ifndef SQLITE_OMIT_INCRBLOB |
| 238 | /* |
| 239 | ** Invalidate the overflow page-list cache for cursor pCur, if any. |
| 240 | */ |
| 241 | static void invalidateOverflowCache(BtCursor *pCur){ |
drh | d0679ed | 2007-08-28 22:24:34 +0000 | [diff] [blame^] | 242 | assert( sqlite3BtreeMutexHeld(pCur->pBt->mutex) ); |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 243 | sqlite3_free(pCur->aOverflow); |
danielk1977 | 92d4d7a | 2007-05-04 12:05:56 +0000 | [diff] [blame] | 244 | pCur->aOverflow = 0; |
| 245 | } |
| 246 | |
| 247 | /* |
| 248 | ** Invalidate the overflow page-list cache for all cursors opened |
| 249 | ** on the shared btree structure pBt. |
| 250 | */ |
| 251 | static void invalidateAllOverflowCache(BtShared *pBt){ |
| 252 | BtCursor *p; |
drh | 2764170 | 2007-08-22 02:56:42 +0000 | [diff] [blame] | 253 | assert( sqlite3BtreeMutexHeld(pBt->mutex) ); |
danielk1977 | 92d4d7a | 2007-05-04 12:05:56 +0000 | [diff] [blame] | 254 | for(p=pBt->pCursor; p; p=p->pNext){ |
| 255 | invalidateOverflowCache(p); |
| 256 | } |
| 257 | } |
| 258 | #else |
| 259 | #define invalidateOverflowCache(x) |
| 260 | #define invalidateAllOverflowCache(x) |
| 261 | #endif |
| 262 | |
drh | 980b1a7 | 2006-08-16 16:42:48 +0000 | [diff] [blame] | 263 | /* |
| 264 | ** Save the current cursor position in the variables BtCursor.nKey |
| 265 | ** and BtCursor.pKey. The cursor's state is set to CURSOR_REQUIRESEEK. |
| 266 | */ |
| 267 | static int saveCursorPosition(BtCursor *pCur){ |
| 268 | int rc; |
| 269 | |
| 270 | assert( CURSOR_VALID==pCur->eState ); |
| 271 | assert( 0==pCur->pKey ); |
drh | d0679ed | 2007-08-28 22:24:34 +0000 | [diff] [blame^] | 272 | assert( sqlite3BtreeMutexHeld(pCur->pBt->mutex) ); |
drh | 980b1a7 | 2006-08-16 16:42:48 +0000 | [diff] [blame] | 273 | |
| 274 | rc = sqlite3BtreeKeySize(pCur, &pCur->nKey); |
| 275 | |
| 276 | /* If this is an intKey table, then the above call to BtreeKeySize() |
| 277 | ** stores the integer key in pCur->nKey. In this case this value is |
| 278 | ** all that is required. Otherwise, if pCur is not open on an intKey |
| 279 | ** table, then malloc space for and store the pCur->nKey bytes of key |
| 280 | ** data. |
| 281 | */ |
| 282 | if( rc==SQLITE_OK && 0==pCur->pPage->intKey){ |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 283 | void *pKey = sqlite3_malloc(pCur->nKey); |
drh | 980b1a7 | 2006-08-16 16:42:48 +0000 | [diff] [blame] | 284 | if( pKey ){ |
| 285 | rc = sqlite3BtreeKey(pCur, 0, pCur->nKey, pKey); |
| 286 | if( rc==SQLITE_OK ){ |
| 287 | pCur->pKey = pKey; |
| 288 | }else{ |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 289 | sqlite3_free(pKey); |
drh | 980b1a7 | 2006-08-16 16:42:48 +0000 | [diff] [blame] | 290 | } |
| 291 | }else{ |
| 292 | rc = SQLITE_NOMEM; |
| 293 | } |
| 294 | } |
| 295 | assert( !pCur->pPage->intKey || !pCur->pKey ); |
| 296 | |
| 297 | if( rc==SQLITE_OK ){ |
| 298 | releasePage(pCur->pPage); |
| 299 | pCur->pPage = 0; |
| 300 | pCur->eState = CURSOR_REQUIRESEEK; |
| 301 | } |
| 302 | |
danielk1977 | 92d4d7a | 2007-05-04 12:05:56 +0000 | [diff] [blame] | 303 | invalidateOverflowCache(pCur); |
drh | 980b1a7 | 2006-08-16 16:42:48 +0000 | [diff] [blame] | 304 | return rc; |
| 305 | } |
| 306 | |
| 307 | /* |
| 308 | ** Save the positions of all cursors except pExcept open on the table |
| 309 | ** with root-page iRoot. Usually, this is called just before cursor |
| 310 | ** pExcept is used to modify the table (BtreeDelete() or BtreeInsert()). |
| 311 | */ |
| 312 | static int saveAllCursors(BtShared *pBt, Pgno iRoot, BtCursor *pExcept){ |
| 313 | BtCursor *p; |
drh | 2764170 | 2007-08-22 02:56:42 +0000 | [diff] [blame] | 314 | assert( sqlite3BtreeMutexHeld(pBt->mutex) ); |
drh | d0679ed | 2007-08-28 22:24:34 +0000 | [diff] [blame^] | 315 | assert( pExcept==0 || pExcept->pBt==pBt ); |
drh | 980b1a7 | 2006-08-16 16:42:48 +0000 | [diff] [blame] | 316 | for(p=pBt->pCursor; p; p=p->pNext){ |
| 317 | if( p!=pExcept && (0==iRoot || p->pgnoRoot==iRoot) && |
| 318 | p->eState==CURSOR_VALID ){ |
| 319 | int rc = saveCursorPosition(p); |
| 320 | if( SQLITE_OK!=rc ){ |
| 321 | return rc; |
| 322 | } |
| 323 | } |
| 324 | } |
| 325 | return SQLITE_OK; |
| 326 | } |
| 327 | |
| 328 | /* |
drh | bf700f3 | 2007-03-31 02:36:44 +0000 | [diff] [blame] | 329 | ** Clear the current cursor position. |
| 330 | */ |
| 331 | static void clearCursorPosition(BtCursor *pCur){ |
drh | d0679ed | 2007-08-28 22:24:34 +0000 | [diff] [blame^] | 332 | assert( sqlite3BtreeMutexHeld(pCur->pBt->mutex) ); |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 333 | sqlite3_free(pCur->pKey); |
drh | bf700f3 | 2007-03-31 02:36:44 +0000 | [diff] [blame] | 334 | pCur->pKey = 0; |
| 335 | pCur->eState = CURSOR_INVALID; |
| 336 | } |
| 337 | |
| 338 | /* |
drh | 980b1a7 | 2006-08-16 16:42:48 +0000 | [diff] [blame] | 339 | ** Restore the cursor to the position it was in (or as close to as possible) |
| 340 | ** when saveCursorPosition() was called. Note that this call deletes the |
| 341 | ** saved position info stored by saveCursorPosition(), so there can be |
| 342 | ** at most one effective restoreOrClearCursorPosition() call after each |
| 343 | ** saveCursorPosition(). |
| 344 | ** |
| 345 | ** If the second argument argument - doSeek - is false, then instead of |
| 346 | ** returning the cursor to it's saved position, any saved position is deleted |
| 347 | ** and the cursor state set to CURSOR_INVALID. |
| 348 | */ |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 349 | int sqlite3BtreeRestoreOrClearCursorPosition(BtCursor *pCur){ |
drh | bf700f3 | 2007-03-31 02:36:44 +0000 | [diff] [blame] | 350 | int rc; |
drh | d0679ed | 2007-08-28 22:24:34 +0000 | [diff] [blame^] | 351 | assert( sqlite3BtreeMutexHeld(pCur->pBt->mutex) ); |
drh | 980b1a7 | 2006-08-16 16:42:48 +0000 | [diff] [blame] | 352 | assert( pCur->eState==CURSOR_REQUIRESEEK ); |
danielk1977 | 32a0d8b | 2007-05-04 19:03:02 +0000 | [diff] [blame] | 353 | #ifndef SQLITE_OMIT_INCRBLOB |
danielk1977 | dcbb5d3 | 2007-05-04 18:36:44 +0000 | [diff] [blame] | 354 | if( pCur->isIncrblobHandle ){ |
| 355 | return SQLITE_ABORT; |
| 356 | } |
danielk1977 | 32a0d8b | 2007-05-04 19:03:02 +0000 | [diff] [blame] | 357 | #endif |
drh | 980b1a7 | 2006-08-16 16:42:48 +0000 | [diff] [blame] | 358 | pCur->eState = CURSOR_INVALID; |
drh | bf700f3 | 2007-03-31 02:36:44 +0000 | [diff] [blame] | 359 | rc = sqlite3BtreeMoveto(pCur, pCur->pKey, pCur->nKey, 0, &pCur->skip); |
drh | 980b1a7 | 2006-08-16 16:42:48 +0000 | [diff] [blame] | 360 | if( rc==SQLITE_OK ){ |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 361 | sqlite3_free(pCur->pKey); |
drh | 980b1a7 | 2006-08-16 16:42:48 +0000 | [diff] [blame] | 362 | pCur->pKey = 0; |
drh | bf700f3 | 2007-03-31 02:36:44 +0000 | [diff] [blame] | 363 | assert( pCur->eState==CURSOR_VALID || pCur->eState==CURSOR_INVALID ); |
drh | 980b1a7 | 2006-08-16 16:42:48 +0000 | [diff] [blame] | 364 | } |
| 365 | return rc; |
| 366 | } |
| 367 | |
drh | bf700f3 | 2007-03-31 02:36:44 +0000 | [diff] [blame] | 368 | #define restoreOrClearCursorPosition(p) \ |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 369 | (p->eState==CURSOR_REQUIRESEEK ? \ |
| 370 | sqlite3BtreeRestoreOrClearCursorPosition(p) : \ |
| 371 | SQLITE_OK) |
drh | 980b1a7 | 2006-08-16 16:42:48 +0000 | [diff] [blame] | 372 | |
danielk1977 | 599fcba | 2004-11-08 07:13:13 +0000 | [diff] [blame] | 373 | #ifndef SQLITE_OMIT_AUTOVACUUM |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 374 | /* |
drh | a315289 | 2007-05-05 11:48:52 +0000 | [diff] [blame] | 375 | ** Given a page number of a regular database page, return the page |
| 376 | ** number for the pointer-map page that contains the entry for the |
| 377 | ** input page number. |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 378 | */ |
danielk1977 | 266664d | 2006-02-10 08:24:21 +0000 | [diff] [blame] | 379 | static Pgno ptrmapPageno(BtShared *pBt, Pgno pgno){ |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 380 | int nPagesPerMapPage, iPtrMap, ret; |
drh | 2764170 | 2007-08-22 02:56:42 +0000 | [diff] [blame] | 381 | assert( sqlite3BtreeMutexHeld(pBt->mutex) ); |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 382 | nPagesPerMapPage = (pBt->usableSize/5)+1; |
| 383 | iPtrMap = (pgno-2)/nPagesPerMapPage; |
| 384 | ret = (iPtrMap*nPagesPerMapPage) + 2; |
danielk1977 | 266664d | 2006-02-10 08:24:21 +0000 | [diff] [blame] | 385 | if( ret==PENDING_BYTE_PAGE(pBt) ){ |
| 386 | ret++; |
| 387 | } |
| 388 | return ret; |
| 389 | } |
danielk1977 | a19df67 | 2004-11-03 11:37:07 +0000 | [diff] [blame] | 390 | |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 391 | /* |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 392 | ** Write an entry into the pointer map. |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 393 | ** |
| 394 | ** This routine updates the pointer map entry for page number 'key' |
| 395 | ** so that it maps to type 'eType' and parent page number 'pgno'. |
| 396 | ** An error code is returned if something goes wrong, otherwise SQLITE_OK. |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 397 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 398 | static int ptrmapPut(BtShared *pBt, Pgno key, u8 eType, Pgno parent){ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 399 | DbPage *pDbPage; /* The pointer map page */ |
| 400 | u8 *pPtrmap; /* The pointer map data */ |
| 401 | Pgno iPtrmap; /* The pointer map page number */ |
| 402 | int offset; /* Offset in pointer map page */ |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 403 | int rc; |
| 404 | |
drh | 2764170 | 2007-08-22 02:56:42 +0000 | [diff] [blame] | 405 | assert( sqlite3BtreeMutexHeld(pBt->mutex) ); |
danielk1977 | 266664d | 2006-02-10 08:24:21 +0000 | [diff] [blame] | 406 | /* The master-journal page number must never be used as a pointer map page */ |
| 407 | assert( 0==PTRMAP_ISPAGE(pBt, PENDING_BYTE_PAGE(pBt)) ); |
| 408 | |
danielk1977 | ac11ee6 | 2005-01-15 12:45:51 +0000 | [diff] [blame] | 409 | assert( pBt->autoVacuum ); |
danielk1977 | fdb7cdb | 2005-01-17 02:12:18 +0000 | [diff] [blame] | 410 | if( key==0 ){ |
drh | 4928570 | 2005-09-17 15:20:26 +0000 | [diff] [blame] | 411 | return SQLITE_CORRUPT_BKPT; |
danielk1977 | fdb7cdb | 2005-01-17 02:12:18 +0000 | [diff] [blame] | 412 | } |
danielk1977 | 266664d | 2006-02-10 08:24:21 +0000 | [diff] [blame] | 413 | iPtrmap = PTRMAP_PAGENO(pBt, key); |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 414 | rc = sqlite3PagerGet(pBt->pPager, iPtrmap, &pDbPage); |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 415 | if( rc!=SQLITE_OK ){ |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 416 | return rc; |
| 417 | } |
danielk1977 | 266664d | 2006-02-10 08:24:21 +0000 | [diff] [blame] | 418 | offset = PTRMAP_PTROFFSET(pBt, key); |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 419 | pPtrmap = (u8 *)sqlite3PagerGetData(pDbPage); |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 420 | |
drh | 615ae55 | 2005-01-16 23:21:00 +0000 | [diff] [blame] | 421 | if( eType!=pPtrmap[offset] || get4byte(&pPtrmap[offset+1])!=parent ){ |
| 422 | TRACE(("PTRMAP_UPDATE: %d->(%d,%d)\n", key, eType, parent)); |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 423 | rc = sqlite3PagerWrite(pDbPage); |
danielk1977 | 5558a8a | 2005-01-17 07:53:44 +0000 | [diff] [blame] | 424 | if( rc==SQLITE_OK ){ |
| 425 | pPtrmap[offset] = eType; |
| 426 | put4byte(&pPtrmap[offset+1], parent); |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 427 | } |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 428 | } |
| 429 | |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 430 | sqlite3PagerUnref(pDbPage); |
danielk1977 | 5558a8a | 2005-01-17 07:53:44 +0000 | [diff] [blame] | 431 | return rc; |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 432 | } |
| 433 | |
| 434 | /* |
| 435 | ** Read an entry from the pointer map. |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 436 | ** |
| 437 | ** This routine retrieves the pointer map entry for page 'key', writing |
| 438 | ** the type and parent page number to *pEType and *pPgno respectively. |
| 439 | ** An error code is returned if something goes wrong, otherwise SQLITE_OK. |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 440 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 441 | static int ptrmapGet(BtShared *pBt, Pgno key, u8 *pEType, Pgno *pPgno){ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 442 | DbPage *pDbPage; /* The pointer map page */ |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 443 | int iPtrmap; /* Pointer map page index */ |
| 444 | u8 *pPtrmap; /* Pointer map page data */ |
| 445 | int offset; /* Offset of entry in pointer map */ |
| 446 | int rc; |
| 447 | |
drh | 2764170 | 2007-08-22 02:56:42 +0000 | [diff] [blame] | 448 | assert( sqlite3BtreeMutexHeld(pBt->mutex) ); |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 449 | |
danielk1977 | 266664d | 2006-02-10 08:24:21 +0000 | [diff] [blame] | 450 | iPtrmap = PTRMAP_PAGENO(pBt, key); |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 451 | rc = sqlite3PagerGet(pBt->pPager, iPtrmap, &pDbPage); |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 452 | if( rc!=0 ){ |
| 453 | return rc; |
| 454 | } |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 455 | pPtrmap = (u8 *)sqlite3PagerGetData(pDbPage); |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 456 | |
danielk1977 | 266664d | 2006-02-10 08:24:21 +0000 | [diff] [blame] | 457 | offset = PTRMAP_PTROFFSET(pBt, key); |
drh | 43617e9 | 2006-03-06 20:55:46 +0000 | [diff] [blame] | 458 | assert( pEType!=0 ); |
| 459 | *pEType = pPtrmap[offset]; |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 460 | if( pPgno ) *pPgno = get4byte(&pPtrmap[offset+1]); |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 461 | |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 462 | sqlite3PagerUnref(pDbPage); |
drh | 4928570 | 2005-09-17 15:20:26 +0000 | [diff] [blame] | 463 | if( *pEType<1 || *pEType>5 ) return SQLITE_CORRUPT_BKPT; |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 464 | return SQLITE_OK; |
| 465 | } |
| 466 | |
| 467 | #endif /* SQLITE_OMIT_AUTOVACUUM */ |
| 468 | |
drh | 0d316a4 | 2002-08-11 20:10:47 +0000 | [diff] [blame] | 469 | /* |
drh | 271efa5 | 2004-05-30 19:19:05 +0000 | [diff] [blame] | 470 | ** Given a btree page and a cell index (0 means the first cell on |
| 471 | ** the page, 1 means the second cell, and so forth) return a pointer |
| 472 | ** to the cell content. |
| 473 | ** |
| 474 | ** This routine works only for pages that do not contain overflow cells. |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 475 | */ |
danielk1977 | 1cc5ed8 | 2007-05-16 17:28:43 +0000 | [diff] [blame] | 476 | #define findCell(pPage, iCell) \ |
| 477 | ((pPage)->aData + get2byte(&(pPage)->aData[(pPage)->cellOffset+2*(iCell)])) |
drh | e6e4d6b | 2007-08-05 23:52:05 +0000 | [diff] [blame] | 478 | #ifdef SQLITE_TEST |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 479 | u8 *sqlite3BtreeFindCell(MemPage *pPage, int iCell){ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 480 | assert( iCell>=0 ); |
drh | 029f3f8 | 2007-06-20 15:14:10 +0000 | [diff] [blame] | 481 | assert( iCell<get2byte(&pPage->aData[pPage->hdrOffset+3]) ); |
danielk1977 | 1cc5ed8 | 2007-05-16 17:28:43 +0000 | [diff] [blame] | 482 | return findCell(pPage, iCell); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 483 | } |
drh | e6e4d6b | 2007-08-05 23:52:05 +0000 | [diff] [blame] | 484 | #endif |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 485 | |
| 486 | /* |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 487 | ** This a more complex version of sqlite3BtreeFindCell() that works for |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 488 | ** pages that do contain overflow cells. See insert |
| 489 | */ |
| 490 | static u8 *findOverflowCell(MemPage *pPage, int iCell){ |
| 491 | int i; |
drh | 2764170 | 2007-08-22 02:56:42 +0000 | [diff] [blame] | 492 | assert( sqlite3BtreeMutexHeld(pPage->pBt->mutex) ); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 493 | for(i=pPage->nOverflow-1; i>=0; i--){ |
drh | 6d08b4d | 2004-07-20 12:45:22 +0000 | [diff] [blame] | 494 | int k; |
| 495 | struct _OvflCell *pOvfl; |
| 496 | pOvfl = &pPage->aOvfl[i]; |
| 497 | k = pOvfl->idx; |
| 498 | if( k<=iCell ){ |
| 499 | if( k==iCell ){ |
| 500 | return pOvfl->pCell; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 501 | } |
| 502 | iCell--; |
| 503 | } |
| 504 | } |
danielk1977 | 1cc5ed8 | 2007-05-16 17:28:43 +0000 | [diff] [blame] | 505 | return findCell(pPage, iCell); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 506 | } |
| 507 | |
| 508 | /* |
| 509 | ** Parse a cell content block and fill in the CellInfo structure. There |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 510 | ** are two versions of this function. sqlite3BtreeParseCell() takes a |
| 511 | ** cell index as the second argument and sqlite3BtreeParseCellPtr() |
| 512 | ** takes a pointer to the body of the cell as its second argument. |
danielk1977 | 1cc5ed8 | 2007-05-16 17:28:43 +0000 | [diff] [blame] | 513 | ** |
| 514 | ** Within this file, the parseCell() macro can be called instead of |
| 515 | ** sqlite3BtreeParseCellPtr(). Using some compilers, this will be faster. |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 516 | */ |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 517 | void sqlite3BtreeParseCellPtr( |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 518 | MemPage *pPage, /* Page containing the cell */ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 519 | u8 *pCell, /* Pointer to the cell text. */ |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 520 | CellInfo *pInfo /* Fill in this structure */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 521 | ){ |
drh | 271efa5 | 2004-05-30 19:19:05 +0000 | [diff] [blame] | 522 | int n; /* Number bytes in cell content header */ |
| 523 | u32 nPayload; /* Number of bytes of cell payload */ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 524 | |
drh | 2764170 | 2007-08-22 02:56:42 +0000 | [diff] [blame] | 525 | assert( sqlite3BtreeMutexHeld(pPage->pBt->mutex) ); |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 526 | |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 527 | pInfo->pCell = pCell; |
drh | ab01f61 | 2004-05-22 02:55:23 +0000 | [diff] [blame] | 528 | assert( pPage->leaf==0 || pPage->leaf==1 ); |
drh | 271efa5 | 2004-05-30 19:19:05 +0000 | [diff] [blame] | 529 | n = pPage->childPtrSize; |
| 530 | assert( n==4-4*pPage->leaf ); |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 531 | if( pPage->hasData ){ |
drh | 271efa5 | 2004-05-30 19:19:05 +0000 | [diff] [blame] | 532 | n += getVarint32(&pCell[n], &nPayload); |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 533 | }else{ |
drh | 271efa5 | 2004-05-30 19:19:05 +0000 | [diff] [blame] | 534 | nPayload = 0; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 535 | } |
drh | 271efa5 | 2004-05-30 19:19:05 +0000 | [diff] [blame] | 536 | pInfo->nData = nPayload; |
drh | 504b698 | 2006-01-22 21:52:56 +0000 | [diff] [blame] | 537 | if( pPage->intKey ){ |
| 538 | n += getVarint(&pCell[n], (u64 *)&pInfo->nKey); |
| 539 | }else{ |
| 540 | u32 x; |
| 541 | n += getVarint32(&pCell[n], &x); |
| 542 | pInfo->nKey = x; |
| 543 | nPayload += x; |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 544 | } |
drh | 7236583 | 2007-03-06 15:53:44 +0000 | [diff] [blame] | 545 | pInfo->nPayload = nPayload; |
drh | 504b698 | 2006-01-22 21:52:56 +0000 | [diff] [blame] | 546 | pInfo->nHeader = n; |
drh | 271efa5 | 2004-05-30 19:19:05 +0000 | [diff] [blame] | 547 | if( nPayload<=pPage->maxLocal ){ |
| 548 | /* This is the (easy) common case where the entire payload fits |
| 549 | ** on the local page. No overflow is required. |
| 550 | */ |
| 551 | int nSize; /* Total size of cell content in bytes */ |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 552 | pInfo->nLocal = nPayload; |
| 553 | pInfo->iOverflow = 0; |
drh | 271efa5 | 2004-05-30 19:19:05 +0000 | [diff] [blame] | 554 | nSize = nPayload + n; |
| 555 | if( nSize<4 ){ |
| 556 | nSize = 4; /* Minimum cell size is 4 */ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 557 | } |
drh | 271efa5 | 2004-05-30 19:19:05 +0000 | [diff] [blame] | 558 | pInfo->nSize = nSize; |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 559 | }else{ |
drh | 271efa5 | 2004-05-30 19:19:05 +0000 | [diff] [blame] | 560 | /* If the payload will not fit completely on the local page, we have |
| 561 | ** to decide how much to store locally and how much to spill onto |
| 562 | ** overflow pages. The strategy is to minimize the amount of unused |
| 563 | ** space on overflow pages while keeping the amount of local storage |
| 564 | ** in between minLocal and maxLocal. |
| 565 | ** |
| 566 | ** Warning: changing the way overflow payload is distributed in any |
| 567 | ** way will result in an incompatible file format. |
| 568 | */ |
| 569 | int minLocal; /* Minimum amount of payload held locally */ |
| 570 | int maxLocal; /* Maximum amount of payload held locally */ |
| 571 | int surplus; /* Overflow payload available for local storage */ |
| 572 | |
| 573 | minLocal = pPage->minLocal; |
| 574 | maxLocal = pPage->maxLocal; |
| 575 | surplus = minLocal + (nPayload - minLocal)%(pPage->pBt->usableSize - 4); |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 576 | if( surplus <= maxLocal ){ |
| 577 | pInfo->nLocal = surplus; |
| 578 | }else{ |
| 579 | pInfo->nLocal = minLocal; |
| 580 | } |
| 581 | pInfo->iOverflow = pInfo->nLocal + n; |
| 582 | pInfo->nSize = pInfo->iOverflow + 4; |
| 583 | } |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 584 | } |
danielk1977 | 1cc5ed8 | 2007-05-16 17:28:43 +0000 | [diff] [blame] | 585 | #define parseCell(pPage, iCell, pInfo) \ |
| 586 | sqlite3BtreeParseCellPtr((pPage), findCell((pPage), (iCell)), (pInfo)) |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 587 | void sqlite3BtreeParseCell( |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 588 | MemPage *pPage, /* Page containing the cell */ |
| 589 | int iCell, /* The cell index. First cell is 0 */ |
| 590 | CellInfo *pInfo /* Fill in this structure */ |
| 591 | ){ |
danielk1977 | 1cc5ed8 | 2007-05-16 17:28:43 +0000 | [diff] [blame] | 592 | parseCell(pPage, iCell, pInfo); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 593 | } |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 594 | |
| 595 | /* |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 596 | ** Compute the total number of bytes that a Cell needs in the cell |
| 597 | ** data area of the btree-page. The return number includes the cell |
| 598 | ** data header and the local payload, but not any overflow page or |
| 599 | ** the space used by the cell pointer. |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 600 | */ |
danielk1977 | bc6ada4 | 2004-06-30 08:20:16 +0000 | [diff] [blame] | 601 | #ifndef NDEBUG |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 602 | static int cellSize(MemPage *pPage, int iCell){ |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 603 | CellInfo info; |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 604 | sqlite3BtreeParseCell(pPage, iCell, &info); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 605 | return info.nSize; |
| 606 | } |
danielk1977 | bc6ada4 | 2004-06-30 08:20:16 +0000 | [diff] [blame] | 607 | #endif |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 608 | static int cellSizePtr(MemPage *pPage, u8 *pCell){ |
| 609 | CellInfo info; |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 610 | sqlite3BtreeParseCellPtr(pPage, pCell, &info); |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 611 | return info.nSize; |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 612 | } |
| 613 | |
danielk1977 | 79a40da | 2005-01-16 08:00:01 +0000 | [diff] [blame] | 614 | #ifndef SQLITE_OMIT_AUTOVACUUM |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 615 | /* |
danielk1977 | 2683665 | 2005-01-17 01:33:13 +0000 | [diff] [blame] | 616 | ** If the cell pCell, part of page pPage contains a pointer |
danielk1977 | 79a40da | 2005-01-16 08:00:01 +0000 | [diff] [blame] | 617 | ** to an overflow page, insert an entry into the pointer-map |
| 618 | ** for the overflow page. |
danielk1977 | ac11ee6 | 2005-01-15 12:45:51 +0000 | [diff] [blame] | 619 | */ |
danielk1977 | 2683665 | 2005-01-17 01:33:13 +0000 | [diff] [blame] | 620 | static int ptrmapPutOvflPtr(MemPage *pPage, u8 *pCell){ |
danielk1977 | 79a40da | 2005-01-16 08:00:01 +0000 | [diff] [blame] | 621 | if( pCell ){ |
| 622 | CellInfo info; |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 623 | sqlite3BtreeParseCellPtr(pPage, pCell, &info); |
drh | 7236583 | 2007-03-06 15:53:44 +0000 | [diff] [blame] | 624 | assert( (info.nData+(pPage->intKey?0:info.nKey))==info.nPayload ); |
danielk1977 | 79a40da | 2005-01-16 08:00:01 +0000 | [diff] [blame] | 625 | if( (info.nData+(pPage->intKey?0:info.nKey))>info.nLocal ){ |
| 626 | Pgno ovfl = get4byte(&pCell[info.iOverflow]); |
| 627 | return ptrmapPut(pPage->pBt, ovfl, PTRMAP_OVERFLOW1, pPage->pgno); |
| 628 | } |
danielk1977 | ac11ee6 | 2005-01-15 12:45:51 +0000 | [diff] [blame] | 629 | } |
danielk1977 | 79a40da | 2005-01-16 08:00:01 +0000 | [diff] [blame] | 630 | return SQLITE_OK; |
danielk1977 | ac11ee6 | 2005-01-15 12:45:51 +0000 | [diff] [blame] | 631 | } |
danielk1977 | 2683665 | 2005-01-17 01:33:13 +0000 | [diff] [blame] | 632 | /* |
| 633 | ** If the cell with index iCell on page pPage contains a pointer |
| 634 | ** to an overflow page, insert an entry into the pointer-map |
| 635 | ** for the overflow page. |
| 636 | */ |
| 637 | static int ptrmapPutOvfl(MemPage *pPage, int iCell){ |
| 638 | u8 *pCell; |
drh | 2764170 | 2007-08-22 02:56:42 +0000 | [diff] [blame] | 639 | assert( sqlite3BtreeMutexHeld(pPage->pBt->mutex) ); |
danielk1977 | 2683665 | 2005-01-17 01:33:13 +0000 | [diff] [blame] | 640 | pCell = findOverflowCell(pPage, iCell); |
| 641 | return ptrmapPutOvflPtr(pPage, pCell); |
| 642 | } |
danielk1977 | 79a40da | 2005-01-16 08:00:01 +0000 | [diff] [blame] | 643 | #endif |
| 644 | |
danielk1977 | ac11ee6 | 2005-01-15 12:45:51 +0000 | [diff] [blame] | 645 | |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 646 | /* |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 647 | ** Defragment the page given. All Cells are moved to the |
drh | 3a4a2d4 | 2005-11-24 14:24:28 +0000 | [diff] [blame] | 648 | ** end of the page and all free space is collected into one |
| 649 | ** big FreeBlk that occurs in between the header and cell |
drh | 31beae9 | 2005-11-24 14:34:36 +0000 | [diff] [blame] | 650 | ** pointer array and the cell content area. |
drh | 365d68f | 2001-05-11 11:02:46 +0000 | [diff] [blame] | 651 | */ |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 652 | static int defragmentPage(MemPage *pPage){ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 653 | int i; /* Loop counter */ |
| 654 | int pc; /* Address of a i-th cell */ |
| 655 | int addr; /* Offset of first byte after cell pointer array */ |
| 656 | int hdr; /* Offset to the page header */ |
| 657 | int size; /* Size of a cell */ |
| 658 | int usableSize; /* Number of usable bytes on a page */ |
| 659 | int cellOffset; /* Offset to the cell pointer array */ |
| 660 | int brk; /* Offset to the cell content area */ |
| 661 | int nCell; /* Number of cells on the page */ |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 662 | unsigned char *data; /* The page data */ |
| 663 | unsigned char *temp; /* Temp area for cell content */ |
drh | 2af926b | 2001-05-15 00:39:25 +0000 | [diff] [blame] | 664 | |
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!=0 ); |
drh | 90f5ecb | 2004-07-22 01:19:35 +0000 | [diff] [blame] | 667 | assert( pPage->pBt->usableSize <= SQLITE_MAX_PAGE_SIZE ); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 668 | assert( pPage->nOverflow==0 ); |
drh | 2764170 | 2007-08-22 02:56:42 +0000 | [diff] [blame] | 669 | assert( sqlite3BtreeMutexHeld(pPage->pBt->mutex) ); |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 670 | temp = sqlite3_malloc( pPage->pBt->pageSize ); |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 671 | if( temp==0 ) return SQLITE_NOMEM; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 672 | data = pPage->aData; |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 673 | hdr = pPage->hdrOffset; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 674 | cellOffset = pPage->cellOffset; |
| 675 | nCell = pPage->nCell; |
| 676 | assert( nCell==get2byte(&data[hdr+3]) ); |
| 677 | usableSize = pPage->pBt->usableSize; |
| 678 | brk = get2byte(&data[hdr+5]); |
| 679 | memcpy(&temp[brk], &data[brk], usableSize - brk); |
| 680 | brk = usableSize; |
| 681 | for(i=0; i<nCell; i++){ |
| 682 | u8 *pAddr; /* The i-th cell pointer */ |
| 683 | pAddr = &data[cellOffset + i*2]; |
| 684 | pc = get2byte(pAddr); |
| 685 | assert( pc<pPage->pBt->usableSize ); |
| 686 | size = cellSizePtr(pPage, &temp[pc]); |
| 687 | brk -= size; |
| 688 | memcpy(&data[brk], &temp[pc], size); |
| 689 | put2byte(pAddr, brk); |
drh | 2af926b | 2001-05-15 00:39:25 +0000 | [diff] [blame] | 690 | } |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 691 | assert( brk>=cellOffset+2*nCell ); |
| 692 | put2byte(&data[hdr+5], brk); |
| 693 | data[hdr+1] = 0; |
| 694 | data[hdr+2] = 0; |
| 695 | data[hdr+7] = 0; |
| 696 | addr = cellOffset+2*nCell; |
| 697 | memset(&data[addr], 0, brk-addr); |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 698 | sqlite3_free(temp); |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 699 | return SQLITE_OK; |
drh | 365d68f | 2001-05-11 11:02:46 +0000 | [diff] [blame] | 700 | } |
| 701 | |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 702 | /* |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 703 | ** Allocate nByte bytes of space on a page. |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 704 | ** |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 705 | ** Return the index into pPage->aData[] of the first byte of |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 706 | ** the new allocation. Or return 0 if there is not enough free |
| 707 | ** space on the page to satisfy the allocation request. |
drh | 2af926b | 2001-05-15 00:39:25 +0000 | [diff] [blame] | 708 | ** |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 709 | ** If the page contains nBytes of free space but does not contain |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 710 | ** nBytes of contiguous free space, then this routine automatically |
| 711 | ** calls defragementPage() to consolidate all free space before |
| 712 | ** allocating the new chunk. |
drh | 7e3b0a0 | 2001-04-28 16:52:40 +0000 | [diff] [blame] | 713 | */ |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 714 | static int allocateSpace(MemPage *pPage, int nByte){ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 715 | int addr, pc, hdr; |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 716 | int size; |
drh | 24cd67e | 2004-05-10 16:18:47 +0000 | [diff] [blame] | 717 | int nFrag; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 718 | int top; |
| 719 | int nCell; |
| 720 | int cellOffset; |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 721 | unsigned char *data; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 722 | |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 723 | data = pPage->aData; |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 724 | assert( sqlite3PagerIswriteable(pPage->pDbPage) ); |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 725 | assert( pPage->pBt ); |
drh | 2764170 | 2007-08-22 02:56:42 +0000 | [diff] [blame] | 726 | assert( sqlite3BtreeMutexHeld(pPage->pBt->mutex) ); |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 727 | if( nByte<4 ) nByte = 4; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 728 | if( pPage->nFree<nByte || pPage->nOverflow>0 ) return 0; |
| 729 | pPage->nFree -= nByte; |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 730 | hdr = pPage->hdrOffset; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 731 | |
| 732 | nFrag = data[hdr+7]; |
| 733 | if( nFrag<60 ){ |
| 734 | /* Search the freelist looking for a slot big enough to satisfy the |
| 735 | ** space request. */ |
| 736 | addr = hdr+1; |
| 737 | while( (pc = get2byte(&data[addr]))>0 ){ |
| 738 | size = get2byte(&data[pc+2]); |
| 739 | if( size>=nByte ){ |
| 740 | if( size<nByte+4 ){ |
| 741 | memcpy(&data[addr], &data[pc], 2); |
| 742 | data[hdr+7] = nFrag + size - nByte; |
| 743 | return pc; |
| 744 | }else{ |
| 745 | put2byte(&data[pc+2], size-nByte); |
| 746 | return pc + size - nByte; |
| 747 | } |
| 748 | } |
| 749 | addr = pc; |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 750 | } |
| 751 | } |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 752 | |
| 753 | /* Allocate memory from the gap in between the cell pointer array |
| 754 | ** and the cell content area. |
| 755 | */ |
| 756 | top = get2byte(&data[hdr+5]); |
| 757 | nCell = get2byte(&data[hdr+3]); |
| 758 | cellOffset = pPage->cellOffset; |
| 759 | if( nFrag>=60 || cellOffset + 2*nCell > top - nByte ){ |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 760 | if( defragmentPage(pPage) ) return 0; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 761 | top = get2byte(&data[hdr+5]); |
drh | 2af926b | 2001-05-15 00:39:25 +0000 | [diff] [blame] | 762 | } |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 763 | top -= nByte; |
| 764 | assert( cellOffset + 2*nCell <= top ); |
| 765 | put2byte(&data[hdr+5], top); |
| 766 | return top; |
drh | 7e3b0a0 | 2001-04-28 16:52:40 +0000 | [diff] [blame] | 767 | } |
| 768 | |
| 769 | /* |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 770 | ** Return a section of the pPage->aData to the freelist. |
| 771 | ** The first byte of the new free block is pPage->aDisk[start] |
| 772 | ** and the size of the block is "size" bytes. |
drh | 306dc21 | 2001-05-21 13:45:10 +0000 | [diff] [blame] | 773 | ** |
| 774 | ** Most of the effort here is involved in coalesing adjacent |
| 775 | ** free blocks into a single big free block. |
drh | 7e3b0a0 | 2001-04-28 16:52:40 +0000 | [diff] [blame] | 776 | */ |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 777 | static void freeSpace(MemPage *pPage, int start, int size){ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 778 | int addr, pbegin, hdr; |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 779 | unsigned char *data = pPage->aData; |
drh | 2af926b | 2001-05-15 00:39:25 +0000 | [diff] [blame] | 780 | |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 781 | assert( pPage->pBt!=0 ); |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 782 | assert( sqlite3PagerIswriteable(pPage->pDbPage) ); |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 783 | assert( start>=pPage->hdrOffset+6+(pPage->leaf?0:4) ); |
danielk1977 | bc6ada4 | 2004-06-30 08:20:16 +0000 | [diff] [blame] | 784 | assert( (start + size)<=pPage->pBt->usableSize ); |
drh | 2764170 | 2007-08-22 02:56:42 +0000 | [diff] [blame] | 785 | assert( sqlite3BtreeMutexHeld(pPage->pBt->mutex) ); |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 786 | if( size<4 ) size = 4; |
| 787 | |
drh | fcce93f | 2006-02-22 03:08:32 +0000 | [diff] [blame] | 788 | #ifdef SQLITE_SECURE_DELETE |
| 789 | /* Overwrite deleted information with zeros when the SECURE_DELETE |
| 790 | ** option is enabled at compile-time */ |
| 791 | memset(&data[start], 0, size); |
| 792 | #endif |
| 793 | |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 794 | /* Add the space back into the linked list of freeblocks */ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 795 | hdr = pPage->hdrOffset; |
| 796 | addr = hdr + 1; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 797 | while( (pbegin = get2byte(&data[addr]))<start && pbegin>0 ){ |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 798 | assert( pbegin<=pPage->pBt->usableSize-4 ); |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 799 | assert( pbegin>addr ); |
| 800 | addr = pbegin; |
drh | 2af926b | 2001-05-15 00:39:25 +0000 | [diff] [blame] | 801 | } |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 802 | assert( pbegin<=pPage->pBt->usableSize-4 ); |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 803 | assert( pbegin>addr || pbegin==0 ); |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 804 | put2byte(&data[addr], start); |
| 805 | put2byte(&data[start], pbegin); |
| 806 | put2byte(&data[start+2], size); |
drh | 2af926b | 2001-05-15 00:39:25 +0000 | [diff] [blame] | 807 | pPage->nFree += size; |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 808 | |
| 809 | /* Coalesce adjacent free blocks */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 810 | addr = pPage->hdrOffset + 1; |
| 811 | while( (pbegin = get2byte(&data[addr]))>0 ){ |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 812 | int pnext, psize; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 813 | assert( pbegin>addr ); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 814 | assert( pbegin<=pPage->pBt->usableSize-4 ); |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 815 | pnext = get2byte(&data[pbegin]); |
| 816 | psize = get2byte(&data[pbegin+2]); |
| 817 | if( pbegin + psize + 3 >= pnext && pnext>0 ){ |
| 818 | int frag = pnext - (pbegin+psize); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 819 | assert( frag<=data[pPage->hdrOffset+7] ); |
| 820 | data[pPage->hdrOffset+7] -= frag; |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 821 | put2byte(&data[pbegin], get2byte(&data[pnext])); |
| 822 | put2byte(&data[pbegin+2], pnext+get2byte(&data[pnext+2])-pbegin); |
| 823 | }else{ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 824 | addr = pbegin; |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 825 | } |
| 826 | } |
drh | 7e3b0a0 | 2001-04-28 16:52:40 +0000 | [diff] [blame] | 827 | |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 828 | /* If the cell content area begins with a freeblock, remove it. */ |
| 829 | if( data[hdr+1]==data[hdr+5] && data[hdr+2]==data[hdr+6] ){ |
| 830 | int top; |
| 831 | pbegin = get2byte(&data[hdr+1]); |
| 832 | memcpy(&data[hdr+1], &data[pbegin], 2); |
| 833 | top = get2byte(&data[hdr+5]); |
| 834 | put2byte(&data[hdr+5], top + get2byte(&data[pbegin+2])); |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 835 | } |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 836 | } |
| 837 | |
| 838 | /* |
drh | 271efa5 | 2004-05-30 19:19:05 +0000 | [diff] [blame] | 839 | ** Decode the flags byte (the first byte of the header) for a page |
| 840 | ** and initialize fields of the MemPage structure accordingly. |
| 841 | */ |
| 842 | static void decodeFlags(MemPage *pPage, int flagByte){ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 843 | BtShared *pBt; /* A copy of pPage->pBt */ |
drh | 271efa5 | 2004-05-30 19:19:05 +0000 | [diff] [blame] | 844 | |
| 845 | assert( pPage->hdrOffset==(pPage->pgno==1 ? 100 : 0) ); |
drh | 2764170 | 2007-08-22 02:56:42 +0000 | [diff] [blame] | 846 | assert( sqlite3BtreeMutexHeld(pPage->pBt->mutex) ); |
drh | 271efa5 | 2004-05-30 19:19:05 +0000 | [diff] [blame] | 847 | pPage->intKey = (flagByte & (PTF_INTKEY|PTF_LEAFDATA))!=0; |
| 848 | pPage->zeroData = (flagByte & PTF_ZERODATA)!=0; |
| 849 | pPage->leaf = (flagByte & PTF_LEAF)!=0; |
| 850 | pPage->childPtrSize = 4*(pPage->leaf==0); |
| 851 | pBt = pPage->pBt; |
| 852 | if( flagByte & PTF_LEAFDATA ){ |
| 853 | pPage->leafData = 1; |
| 854 | pPage->maxLocal = pBt->maxLeaf; |
| 855 | pPage->minLocal = pBt->minLeaf; |
| 856 | }else{ |
| 857 | pPage->leafData = 0; |
| 858 | pPage->maxLocal = pBt->maxLocal; |
| 859 | pPage->minLocal = pBt->minLocal; |
| 860 | } |
| 861 | pPage->hasData = !(pPage->zeroData || (!pPage->leaf && pPage->leafData)); |
| 862 | } |
| 863 | |
| 864 | /* |
drh | 7e3b0a0 | 2001-04-28 16:52:40 +0000 | [diff] [blame] | 865 | ** Initialize the auxiliary information for a disk block. |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 866 | ** |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 867 | ** The pParent parameter must be a pointer to the MemPage which |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 868 | ** is the parent of the page being initialized. The root of a |
| 869 | ** BTree has no parent and so for that page, pParent==NULL. |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 870 | ** |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 871 | ** Return SQLITE_OK on success. If we see that the page does |
drh | da47d77 | 2002-12-02 04:25:19 +0000 | [diff] [blame] | 872 | ** not contain a well-formed database page, then return |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 873 | ** SQLITE_CORRUPT. Note that a return of SQLITE_OK does not |
| 874 | ** guarantee that the page is well-formed. It only shows that |
| 875 | ** we failed to detect any corruption. |
drh | 7e3b0a0 | 2001-04-28 16:52:40 +0000 | [diff] [blame] | 876 | */ |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 877 | int sqlite3BtreeInitPage( |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 878 | MemPage *pPage, /* The page to be initialized */ |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 879 | MemPage *pParent /* The parent. Might be NULL */ |
| 880 | ){ |
drh | 271efa5 | 2004-05-30 19:19:05 +0000 | [diff] [blame] | 881 | int pc; /* Address of a freeblock within pPage->aData[] */ |
drh | 271efa5 | 2004-05-30 19:19:05 +0000 | [diff] [blame] | 882 | int hdr; /* Offset to beginning of page header */ |
| 883 | u8 *data; /* Equal to pPage->aData */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 884 | BtShared *pBt; /* The main btree structure */ |
drh | 271efa5 | 2004-05-30 19:19:05 +0000 | [diff] [blame] | 885 | int usableSize; /* Amount of usable space on each page */ |
| 886 | int cellOffset; /* Offset from start of page to first cell pointer */ |
| 887 | int nFree; /* Number of unused bytes on the page */ |
| 888 | int top; /* First byte of the cell content area */ |
drh | 2af926b | 2001-05-15 00:39:25 +0000 | [diff] [blame] | 889 | |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 890 | pBt = pPage->pBt; |
| 891 | assert( pBt!=0 ); |
| 892 | assert( pParent==0 || pParent->pBt==pBt ); |
drh | 2764170 | 2007-08-22 02:56:42 +0000 | [diff] [blame] | 893 | assert( sqlite3BtreeMutexHeld(pBt->mutex) ); |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 894 | assert( pPage->pgno==sqlite3PagerPagenumber(pPage->pDbPage) ); |
drh | 07d183d | 2005-05-01 22:52:42 +0000 | [diff] [blame] | 895 | assert( pPage->aData == &((unsigned char*)pPage)[-pBt->pageSize] ); |
drh | ee696e2 | 2004-08-30 16:52:17 +0000 | [diff] [blame] | 896 | if( pPage->pParent!=pParent && (pPage->pParent!=0 || pPage->isInit) ){ |
| 897 | /* The parent page should never change unless the file is corrupt */ |
drh | 4928570 | 2005-09-17 15:20:26 +0000 | [diff] [blame] | 898 | return SQLITE_CORRUPT_BKPT; |
drh | ee696e2 | 2004-08-30 16:52:17 +0000 | [diff] [blame] | 899 | } |
drh | 10617cd | 2004-05-14 15:27:27 +0000 | [diff] [blame] | 900 | if( pPage->isInit ) return SQLITE_OK; |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 901 | if( pPage->pParent==0 && pParent!=0 ){ |
| 902 | pPage->pParent = pParent; |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 903 | sqlite3PagerRef(pParent->pDbPage); |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 904 | } |
drh | de64713 | 2004-05-07 17:57:49 +0000 | [diff] [blame] | 905 | hdr = pPage->hdrOffset; |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 906 | data = pPage->aData; |
drh | 271efa5 | 2004-05-30 19:19:05 +0000 | [diff] [blame] | 907 | decodeFlags(pPage, data[hdr]); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 908 | pPage->nOverflow = 0; |
drh | c8629a1 | 2004-05-08 20:07:40 +0000 | [diff] [blame] | 909 | pPage->idxShift = 0; |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 910 | usableSize = pBt->usableSize; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 911 | pPage->cellOffset = cellOffset = hdr + 12 - 4*pPage->leaf; |
| 912 | top = get2byte(&data[hdr+5]); |
| 913 | pPage->nCell = get2byte(&data[hdr+3]); |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 914 | if( pPage->nCell>MX_CELL(pBt) ){ |
drh | ee696e2 | 2004-08-30 16:52:17 +0000 | [diff] [blame] | 915 | /* To many cells for a single page. The page must be corrupt */ |
drh | 4928570 | 2005-09-17 15:20:26 +0000 | [diff] [blame] | 916 | return SQLITE_CORRUPT_BKPT; |
drh | ee696e2 | 2004-08-30 16:52:17 +0000 | [diff] [blame] | 917 | } |
| 918 | if( pPage->nCell==0 && pParent!=0 && pParent->pgno!=1 ){ |
| 919 | /* All pages must have at least one cell, except for root pages */ |
drh | 4928570 | 2005-09-17 15:20:26 +0000 | [diff] [blame] | 920 | return SQLITE_CORRUPT_BKPT; |
drh | ee696e2 | 2004-08-30 16:52:17 +0000 | [diff] [blame] | 921 | } |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 922 | |
| 923 | /* Compute the total free space on the page */ |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 924 | pc = get2byte(&data[hdr+1]); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 925 | nFree = data[hdr+7] + top - (cellOffset + 2*pPage->nCell); |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 926 | while( pc>0 ){ |
| 927 | int next, size; |
drh | ee696e2 | 2004-08-30 16:52:17 +0000 | [diff] [blame] | 928 | if( pc>usableSize-4 ){ |
| 929 | /* Free block is off the page */ |
drh | 4928570 | 2005-09-17 15:20:26 +0000 | [diff] [blame] | 930 | return SQLITE_CORRUPT_BKPT; |
drh | ee696e2 | 2004-08-30 16:52:17 +0000 | [diff] [blame] | 931 | } |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 932 | next = get2byte(&data[pc]); |
| 933 | size = get2byte(&data[pc+2]); |
drh | ee696e2 | 2004-08-30 16:52:17 +0000 | [diff] [blame] | 934 | if( next>0 && next<=pc+size+3 ){ |
| 935 | /* Free blocks must be in accending order */ |
drh | 4928570 | 2005-09-17 15:20:26 +0000 | [diff] [blame] | 936 | return SQLITE_CORRUPT_BKPT; |
drh | ee696e2 | 2004-08-30 16:52:17 +0000 | [diff] [blame] | 937 | } |
drh | 3add367 | 2004-05-15 00:29:24 +0000 | [diff] [blame] | 938 | nFree += size; |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 939 | pc = next; |
| 940 | } |
drh | 3add367 | 2004-05-15 00:29:24 +0000 | [diff] [blame] | 941 | pPage->nFree = nFree; |
drh | ee696e2 | 2004-08-30 16:52:17 +0000 | [diff] [blame] | 942 | if( nFree>=usableSize ){ |
| 943 | /* Free space cannot exceed total page size */ |
drh | 4928570 | 2005-09-17 15:20:26 +0000 | [diff] [blame] | 944 | return SQLITE_CORRUPT_BKPT; |
drh | ee696e2 | 2004-08-30 16:52:17 +0000 | [diff] [blame] | 945 | } |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 946 | |
drh | de64713 | 2004-05-07 17:57:49 +0000 | [diff] [blame] | 947 | pPage->isInit = 1; |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 948 | return SQLITE_OK; |
drh | 7e3b0a0 | 2001-04-28 16:52:40 +0000 | [diff] [blame] | 949 | } |
| 950 | |
| 951 | /* |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 952 | ** Set up a raw page so that it looks like a database page holding |
| 953 | ** no entries. |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 954 | */ |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 955 | static void zeroPage(MemPage *pPage, int flags){ |
| 956 | unsigned char *data = pPage->aData; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 957 | BtShared *pBt = pPage->pBt; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 958 | int hdr = pPage->hdrOffset; |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 959 | int first; |
| 960 | |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 961 | assert( sqlite3PagerPagenumber(pPage->pDbPage)==pPage->pgno ); |
drh | 07d183d | 2005-05-01 22:52:42 +0000 | [diff] [blame] | 962 | assert( &data[pBt->pageSize] == (unsigned char*)pPage ); |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 963 | assert( sqlite3PagerIswriteable(pPage->pDbPage) ); |
drh | 2764170 | 2007-08-22 02:56:42 +0000 | [diff] [blame] | 964 | assert( sqlite3BtreeMutexHeld(pBt->mutex) ); |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 965 | memset(&data[hdr], 0, pBt->usableSize - hdr); |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 966 | data[hdr] = flags; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 967 | first = hdr + 8 + 4*((flags&PTF_LEAF)==0); |
| 968 | memset(&data[hdr+1], 0, 4); |
| 969 | data[hdr+7] = 0; |
| 970 | put2byte(&data[hdr+5], pBt->usableSize); |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 971 | pPage->nFree = pBt->usableSize - first; |
drh | 271efa5 | 2004-05-30 19:19:05 +0000 | [diff] [blame] | 972 | decodeFlags(pPage, flags); |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 973 | pPage->hdrOffset = hdr; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 974 | pPage->cellOffset = first; |
| 975 | pPage->nOverflow = 0; |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 976 | pPage->idxShift = 0; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 977 | pPage->nCell = 0; |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 978 | pPage->isInit = 1; |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 979 | } |
| 980 | |
| 981 | /* |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 982 | ** Get a page from the pager. Initialize the MemPage.pBt and |
| 983 | ** MemPage.aData elements if needed. |
drh | 538f570 | 2007-04-13 02:14:30 +0000 | [diff] [blame] | 984 | ** |
| 985 | ** If the noContent flag is set, it means that we do not care about |
| 986 | ** the content of the page at this time. So do not go to the disk |
| 987 | ** to fetch the content. Just fill in the content with zeros for now. |
| 988 | ** If in the future we call sqlite3PagerWrite() on this page, that |
| 989 | ** means we have started to be concerned about content and the disk |
| 990 | ** read should occur at that point. |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 991 | */ |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 992 | int sqlite3BtreeGetPage( |
| 993 | BtShared *pBt, /* The btree */ |
| 994 | Pgno pgno, /* Number of the page to fetch */ |
| 995 | MemPage **ppPage, /* Return the page in this parameter */ |
| 996 | int noContent /* Do not load page content if true */ |
| 997 | ){ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 998 | int rc; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 999 | MemPage *pPage; |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 1000 | DbPage *pDbPage; |
| 1001 | |
drh | 2764170 | 2007-08-22 02:56:42 +0000 | [diff] [blame] | 1002 | assert( sqlite3BtreeMutexHeld(pBt->mutex) ); |
drh | 538f570 | 2007-04-13 02:14:30 +0000 | [diff] [blame] | 1003 | rc = sqlite3PagerAcquire(pBt->pPager, pgno, (DbPage**)&pDbPage, noContent); |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1004 | if( rc ) return rc; |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 1005 | pPage = (MemPage *)sqlite3PagerGetExtra(pDbPage); |
| 1006 | pPage->aData = sqlite3PagerGetData(pDbPage); |
| 1007 | pPage->pDbPage = pDbPage; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1008 | pPage->pBt = pBt; |
| 1009 | pPage->pgno = pgno; |
drh | de64713 | 2004-05-07 17:57:49 +0000 | [diff] [blame] | 1010 | pPage->hdrOffset = pPage->pgno==1 ? 100 : 0; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1011 | *ppPage = pPage; |
| 1012 | return SQLITE_OK; |
| 1013 | } |
| 1014 | |
| 1015 | /* |
drh | de64713 | 2004-05-07 17:57:49 +0000 | [diff] [blame] | 1016 | ** Get a page from the pager and initialize it. This routine |
| 1017 | ** is just a convenience wrapper around separate calls to |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 1018 | ** sqlite3BtreeGetPage() and sqlite3BtreeInitPage(). |
drh | de64713 | 2004-05-07 17:57:49 +0000 | [diff] [blame] | 1019 | */ |
| 1020 | static int getAndInitPage( |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1021 | BtShared *pBt, /* The database file */ |
drh | de64713 | 2004-05-07 17:57:49 +0000 | [diff] [blame] | 1022 | Pgno pgno, /* Number of the page to get */ |
| 1023 | MemPage **ppPage, /* Write the page pointer here */ |
| 1024 | MemPage *pParent /* Parent of the page */ |
| 1025 | ){ |
| 1026 | int rc; |
drh | 2764170 | 2007-08-22 02:56:42 +0000 | [diff] [blame] | 1027 | assert( sqlite3BtreeMutexHeld(pBt->mutex) ); |
drh | ee696e2 | 2004-08-30 16:52:17 +0000 | [diff] [blame] | 1028 | if( pgno==0 ){ |
drh | 4928570 | 2005-09-17 15:20:26 +0000 | [diff] [blame] | 1029 | return SQLITE_CORRUPT_BKPT; |
drh | ee696e2 | 2004-08-30 16:52:17 +0000 | [diff] [blame] | 1030 | } |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 1031 | rc = sqlite3BtreeGetPage(pBt, pgno, ppPage, 0); |
drh | 10617cd | 2004-05-14 15:27:27 +0000 | [diff] [blame] | 1032 | if( rc==SQLITE_OK && (*ppPage)->isInit==0 ){ |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 1033 | rc = sqlite3BtreeInitPage(*ppPage, pParent); |
drh | de64713 | 2004-05-07 17:57:49 +0000 | [diff] [blame] | 1034 | } |
| 1035 | return rc; |
| 1036 | } |
| 1037 | |
| 1038 | /* |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1039 | ** Release a MemPage. This should be called once for each prior |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 1040 | ** call to sqlite3BtreeGetPage. |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1041 | */ |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 1042 | static void releasePage(MemPage *pPage){ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1043 | if( pPage ){ |
| 1044 | assert( pPage->aData ); |
| 1045 | assert( pPage->pBt ); |
drh | 07d183d | 2005-05-01 22:52:42 +0000 | [diff] [blame] | 1046 | assert( &pPage->aData[pPage->pBt->pageSize]==(unsigned char*)pPage ); |
drh | 2764170 | 2007-08-22 02:56:42 +0000 | [diff] [blame] | 1047 | assert( sqlite3BtreeMutexHeld(pPage->pBt->mutex) ); |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 1048 | sqlite3PagerUnref(pPage->pDbPage); |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1049 | } |
| 1050 | } |
| 1051 | |
| 1052 | /* |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 1053 | ** This routine is called when the reference count for a page |
| 1054 | ** reaches zero. We need to unref the pParent pointer when that |
| 1055 | ** happens. |
| 1056 | */ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 1057 | static void pageDestructor(DbPage *pData, int pageSize){ |
drh | 07d183d | 2005-05-01 22:52:42 +0000 | [diff] [blame] | 1058 | MemPage *pPage; |
| 1059 | assert( (pageSize & 7)==0 ); |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 1060 | pPage = (MemPage *)sqlite3PagerGetExtra(pData); |
drh | d0679ed | 2007-08-28 22:24:34 +0000 | [diff] [blame^] | 1061 | assert( pPage->isInit==0 || sqlite3BtreeMutexHeld(pPage->pBt->mutex) ); |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 1062 | if( pPage->pParent ){ |
| 1063 | MemPage *pParent = pPage->pParent; |
drh | d0679ed | 2007-08-28 22:24:34 +0000 | [diff] [blame^] | 1064 | assert( pPage->isInit==1 ); |
| 1065 | assert( pParent->pBt==pPage->pBt ); |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 1066 | pPage->pParent = 0; |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 1067 | releasePage(pParent); |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 1068 | } |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1069 | pPage->isInit = 0; |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 1070 | } |
| 1071 | |
| 1072 | /* |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 1073 | ** During a rollback, when the pager reloads information into the cache |
| 1074 | ** so that the cache is restored to its original state at the start of |
| 1075 | ** the transaction, for each page restored this routine is called. |
| 1076 | ** |
| 1077 | ** This routine needs to reset the extra data section at the end of the |
| 1078 | ** page to agree with the restored data. |
| 1079 | */ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 1080 | static void pageReinit(DbPage *pData, int pageSize){ |
drh | 07d183d | 2005-05-01 22:52:42 +0000 | [diff] [blame] | 1081 | MemPage *pPage; |
| 1082 | assert( (pageSize & 7)==0 ); |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 1083 | pPage = (MemPage *)sqlite3PagerGetExtra(pData); |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 1084 | if( pPage->isInit ){ |
drh | 2764170 | 2007-08-22 02:56:42 +0000 | [diff] [blame] | 1085 | assert( sqlite3BtreeMutexHeld(pPage->pBt->mutex) ); |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 1086 | pPage->isInit = 0; |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 1087 | sqlite3BtreeInitPage(pPage, pPage->pParent); |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 1088 | } |
| 1089 | } |
| 1090 | |
| 1091 | /* |
drh | ad3e010 | 2004-09-03 23:32:18 +0000 | [diff] [blame] | 1092 | ** Open a database file. |
| 1093 | ** |
drh | 382c024 | 2001-10-06 16:33:02 +0000 | [diff] [blame] | 1094 | ** zFilename is the name of the database file. If zFilename is NULL |
drh | 1bee3d7 | 2001-10-15 00:44:35 +0000 | [diff] [blame] | 1095 | ** a new database with a random name is created. This randomly named |
drh | 23e11ca | 2004-05-04 17:27:28 +0000 | [diff] [blame] | 1096 | ** database file will be deleted when sqlite3BtreeClose() is called. |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 1097 | ** If zFilename is ":memory:" then an in-memory database is created |
| 1098 | ** that is automatically destroyed when it is closed. |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 1099 | */ |
drh | 23e11ca | 2004-05-04 17:27:28 +0000 | [diff] [blame] | 1100 | int sqlite3BtreeOpen( |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1101 | const char *zFilename, /* Name of the file containing the BTree database */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1102 | sqlite3 *pSqlite, /* Associated database handle */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1103 | Btree **ppBtree, /* Pointer to new Btree object written here */ |
drh | 90f5ecb | 2004-07-22 01:19:35 +0000 | [diff] [blame] | 1104 | int flags /* Options */ |
drh | 6019e16 | 2001-07-02 17:51:45 +0000 | [diff] [blame] | 1105 | ){ |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 1106 | sqlite3_vfs *pVfs; /* The VFS to use for this btree */ |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 1107 | BtShared *pBt = 0; /* Shared part of btree structure */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1108 | Btree *p; /* Handle to return */ |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 1109 | int rc = SQLITE_OK; |
drh | 90f5ecb | 2004-07-22 01:19:35 +0000 | [diff] [blame] | 1110 | int nReserve; |
| 1111 | unsigned char zDbHeader[100]; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1112 | |
| 1113 | /* Set the variable isMemdb to true for an in-memory database, or |
| 1114 | ** false for a file-based database. This symbol is only required if |
| 1115 | ** either of the shared-data or autovacuum features are compiled |
| 1116 | ** into the library. |
| 1117 | */ |
| 1118 | #if !defined(SQLITE_OMIT_SHARED_CACHE) || !defined(SQLITE_OMIT_AUTOVACUUM) |
| 1119 | #ifdef SQLITE_OMIT_MEMORYDB |
drh | 980b1a7 | 2006-08-16 16:42:48 +0000 | [diff] [blame] | 1120 | const int isMemdb = 0; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1121 | #else |
drh | 980b1a7 | 2006-08-16 16:42:48 +0000 | [diff] [blame] | 1122 | const int isMemdb = zFilename && !strcmp(zFilename, ":memory:"); |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1123 | #endif |
| 1124 | #endif |
| 1125 | |
drh | d0679ed | 2007-08-28 22:24:34 +0000 | [diff] [blame^] | 1126 | assert( pSqlite!=0 ); |
| 1127 | assert( sqlite3_mutex_held(pSqlite->mutex) ); |
drh | 153c62c | 2007-08-24 03:51:33 +0000 | [diff] [blame] | 1128 | |
drh | d0679ed | 2007-08-28 22:24:34 +0000 | [diff] [blame^] | 1129 | pVfs = pSqlite->pVfs; |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 1130 | p = sqlite3MallocZero(sizeof(Btree)); |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1131 | if( !p ){ |
| 1132 | return SQLITE_NOMEM; |
| 1133 | } |
| 1134 | p->inTrans = TRANS_NONE; |
| 1135 | p->pSqlite = pSqlite; |
| 1136 | |
drh | 198bf39 | 2006-01-06 21:52:49 +0000 | [diff] [blame] | 1137 | #if !defined(SQLITE_OMIT_SHARED_CACHE) && !defined(SQLITE_OMIT_DISKIO) |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 1138 | /* |
| 1139 | ** If this Btree is a candidate for shared cache, try to find an |
| 1140 | ** existing BtShared object that we can share with |
| 1141 | */ |
| 1142 | if( (flags & BTREE_PRIVATE)==0 |
| 1143 | && isMemdb==0 |
drh | d0679ed | 2007-08-28 22:24:34 +0000 | [diff] [blame^] | 1144 | && (pSqlite->flags & SQLITE_Vtab)==0 |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 1145 | && zFilename && zFilename[0] |
| 1146 | && sqlite3SharedCacheEnabled |
| 1147 | ){ |
danielk1977 | 90949c2 | 2007-08-17 16:50:38 +0000 | [diff] [blame] | 1148 | char *zFullPathname = (char *)sqlite3_malloc(pVfs->mxPathname); |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 1149 | sqlite3_mutex *mutexShared; |
| 1150 | p->sharable = 1; |
drh | 4a50aac | 2007-08-23 02:47:53 +0000 | [diff] [blame] | 1151 | if( pSqlite ){ |
| 1152 | pSqlite->flags |= SQLITE_SharedCache; |
| 1153 | } |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1154 | if( !zFullPathname ){ |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 1155 | sqlite3_free(p); |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1156 | return SQLITE_NOMEM; |
| 1157 | } |
danielk1977 | 90949c2 | 2007-08-17 16:50:38 +0000 | [diff] [blame] | 1158 | sqlite3OsFullPathname(pVfs, zFilename, zFullPathname); |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 1159 | mutexShared = sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_MASTER); |
| 1160 | sqlite3_mutex_enter(mutexShared); |
| 1161 | for(pBt=sqlite3SharedCacheList; pBt; pBt=pBt->pNext){ |
danielk1977 | b82e7ed | 2006-01-11 14:09:31 +0000 | [diff] [blame] | 1162 | assert( pBt->nRef>0 ); |
drh | d0679ed | 2007-08-28 22:24:34 +0000 | [diff] [blame^] | 1163 | if( 0==strcmp(zFullPathname, sqlite3PagerFilename(pBt->pPager)) |
| 1164 | && sqlite3PagerVfs(pBt->pPager)==pVfs ){ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1165 | p->pBt = pBt; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1166 | pBt->nRef++; |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 1167 | break; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1168 | } |
| 1169 | } |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 1170 | sqlite3_mutex_leave(mutexShared); |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 1171 | sqlite3_free(zFullPathname); |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1172 | } |
| 1173 | #endif |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 1174 | if( pBt==0 ){ |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 1175 | /* |
| 1176 | ** The following asserts make sure that structures used by the btree are |
| 1177 | ** the right size. This is to guard against size changes that result |
| 1178 | ** when compiling on a different architecture. |
danielk1977 | 03aded4 | 2004-11-22 05:26:27 +0000 | [diff] [blame] | 1179 | */ |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 1180 | assert( sizeof(i64)==8 || sizeof(i64)==4 ); |
| 1181 | assert( sizeof(u64)==8 || sizeof(u64)==4 ); |
| 1182 | assert( sizeof(u32)==4 ); |
| 1183 | assert( sizeof(u16)==2 ); |
| 1184 | assert( sizeof(Pgno)==4 ); |
| 1185 | |
| 1186 | pBt = sqlite3MallocZero( sizeof(*pBt) ); |
| 1187 | if( pBt==0 ){ |
| 1188 | rc = SQLITE_NOMEM; |
| 1189 | goto btree_open_out; |
| 1190 | } |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 1191 | rc = sqlite3PagerOpen(pVfs, &pBt->pPager, zFilename, EXTRA_SIZE, flags); |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 1192 | if( rc==SQLITE_OK ){ |
| 1193 | rc = sqlite3PagerReadFileheader(pBt->pPager,sizeof(zDbHeader),zDbHeader); |
| 1194 | } |
| 1195 | if( rc!=SQLITE_OK ){ |
| 1196 | goto btree_open_out; |
| 1197 | } |
| 1198 | p->pBt = pBt; |
| 1199 | |
| 1200 | sqlite3PagerSetDestructor(pBt->pPager, pageDestructor); |
| 1201 | sqlite3PagerSetReiniter(pBt->pPager, pageReinit); |
| 1202 | pBt->pCursor = 0; |
| 1203 | pBt->pPage1 = 0; |
| 1204 | pBt->readOnly = sqlite3PagerIsreadonly(pBt->pPager); |
| 1205 | pBt->pageSize = get2byte(&zDbHeader[16]); |
| 1206 | if( pBt->pageSize<512 || pBt->pageSize>SQLITE_MAX_PAGE_SIZE |
| 1207 | || ((pBt->pageSize-1)&pBt->pageSize)!=0 ){ |
danielk1977 | 9663b8f | 2007-08-24 11:52:28 +0000 | [diff] [blame] | 1208 | pBt->pageSize = sqlite3PagerSetPagesize(pBt->pPager, 0); |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 1209 | pBt->maxEmbedFrac = 64; /* 25% */ |
| 1210 | pBt->minEmbedFrac = 32; /* 12.5% */ |
| 1211 | pBt->minLeafFrac = 32; /* 12.5% */ |
| 1212 | #ifndef SQLITE_OMIT_AUTOVACUUM |
| 1213 | /* If the magic name ":memory:" will create an in-memory database, then |
| 1214 | ** leave the autoVacuum mode at 0 (do not auto-vacuum), even if |
| 1215 | ** SQLITE_DEFAULT_AUTOVACUUM is true. On the other hand, if |
| 1216 | ** SQLITE_OMIT_MEMORYDB has been defined, then ":memory:" is just a |
| 1217 | ** regular file-name. In this case the auto-vacuum applies as per normal. |
| 1218 | */ |
| 1219 | if( zFilename && !isMemdb ){ |
| 1220 | pBt->autoVacuum = (SQLITE_DEFAULT_AUTOVACUUM ? 1 : 0); |
| 1221 | pBt->incrVacuum = (SQLITE_DEFAULT_AUTOVACUUM==2 ? 1 : 0); |
| 1222 | } |
| 1223 | #endif |
| 1224 | nReserve = 0; |
| 1225 | }else{ |
| 1226 | nReserve = zDbHeader[20]; |
| 1227 | pBt->maxEmbedFrac = zDbHeader[21]; |
| 1228 | pBt->minEmbedFrac = zDbHeader[22]; |
| 1229 | pBt->minLeafFrac = zDbHeader[23]; |
| 1230 | pBt->pageSizeFixed = 1; |
| 1231 | #ifndef SQLITE_OMIT_AUTOVACUUM |
| 1232 | pBt->autoVacuum = (get4byte(&zDbHeader[36 + 4*4])?1:0); |
| 1233 | pBt->incrVacuum = (get4byte(&zDbHeader[36 + 7*4])?1:0); |
| 1234 | #endif |
| 1235 | } |
| 1236 | pBt->usableSize = pBt->pageSize - nReserve; |
| 1237 | assert( (pBt->pageSize & 7)==0 ); /* 8-byte alignment of pageSize */ |
| 1238 | sqlite3PagerSetPagesize(pBt->pPager, pBt->pageSize); |
| 1239 | |
| 1240 | #if !defined(SQLITE_OMIT_SHARED_CACHE) && !defined(SQLITE_OMIT_DISKIO) |
| 1241 | /* Add the new BtShared object to the linked list sharable BtShareds. |
| 1242 | */ |
| 1243 | if( p->sharable ){ |
| 1244 | sqlite3_mutex *mutexShared; |
| 1245 | pBt->nRef = 1; |
| 1246 | mutexShared = sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_MASTER); |
| 1247 | pBt->mutex = sqlite3_mutex_alloc(SQLITE_MUTEX_FAST); |
| 1248 | sqlite3_mutex_enter(mutexShared); |
| 1249 | pBt->pNext = sqlite3SharedCacheList; |
| 1250 | sqlite3SharedCacheList = pBt; |
| 1251 | sqlite3_mutex_leave(mutexShared); |
danielk1977 | 951af80 | 2004-11-05 15:45:09 +0000 | [diff] [blame] | 1252 | } |
drh | eee46cf | 2004-11-06 00:02:48 +0000 | [diff] [blame] | 1253 | #endif |
drh | 90f5ecb | 2004-07-22 01:19:35 +0000 | [diff] [blame] | 1254 | } |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1255 | |
drh | cfed7bc | 2006-03-13 14:28:05 +0000 | [diff] [blame] | 1256 | #if !defined(SQLITE_OMIT_SHARED_CACHE) && !defined(SQLITE_OMIT_DISKIO) |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 1257 | /* If the new Btree uses a sharable pBtShared, then link the new |
| 1258 | ** Btree into the list of all sharable Btrees for the same connection. |
drh | abddb0c | 2007-08-20 13:14:28 +0000 | [diff] [blame] | 1259 | ** The list is kept in ascending order by pBt address. |
danielk1977 | 54f0198 | 2006-01-18 15:25:17 +0000 | [diff] [blame] | 1260 | */ |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 1261 | if( p->sharable ){ |
| 1262 | int i; |
| 1263 | Btree *pSib; |
| 1264 | for(i=0; i<pSqlite->nDb; i++){ |
| 1265 | if( (pSib = pSqlite->aDb[i].pBt)!=0 && pSib->sharable ){ |
| 1266 | while( pSib->pPrev ){ pSib = pSib->pPrev; } |
| 1267 | if( p->pBt<pSib->pBt ){ |
| 1268 | p->pNext = pSib; |
| 1269 | p->pPrev = 0; |
| 1270 | pSib->pPrev = p; |
| 1271 | }else{ |
drh | abddb0c | 2007-08-20 13:14:28 +0000 | [diff] [blame] | 1272 | while( pSib->pNext && pSib->pNext->pBt<p->pBt ){ |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 1273 | pSib = pSib->pNext; |
| 1274 | } |
| 1275 | p->pNext = pSib->pNext; |
| 1276 | p->pPrev = pSib; |
| 1277 | if( p->pNext ){ |
| 1278 | p->pNext->pPrev = p; |
| 1279 | } |
| 1280 | pSib->pNext = p; |
| 1281 | } |
| 1282 | break; |
| 1283 | } |
| 1284 | } |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1285 | } |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1286 | #endif |
| 1287 | *ppBtree = p; |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 1288 | |
| 1289 | btree_open_out: |
| 1290 | if( rc!=SQLITE_OK ){ |
| 1291 | if( pBt && pBt->pPager ){ |
| 1292 | sqlite3PagerClose(pBt->pPager); |
| 1293 | } |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 1294 | sqlite3_free(pBt); |
| 1295 | sqlite3_free(p); |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 1296 | *ppBtree = 0; |
| 1297 | } |
| 1298 | return rc; |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 1299 | } |
| 1300 | |
| 1301 | /* |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 1302 | ** Decrement the BtShared.nRef counter. When it reaches zero, |
| 1303 | ** remove the BtShared structure from the sharing list. Return |
| 1304 | ** true if the BtShared.nRef counter reaches zero and return |
| 1305 | ** false if it is still positive. |
| 1306 | */ |
| 1307 | static int removeFromSharingList(BtShared *pBt){ |
| 1308 | #ifndef SQLITE_OMIT_SHARED_CACHE |
| 1309 | sqlite3_mutex *pMaster; |
| 1310 | BtShared *pList; |
| 1311 | int removed = 0; |
| 1312 | |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 1313 | assert( sqlite3_mutex_notheld(pBt->mutex) ); |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 1314 | pMaster = sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_MASTER); |
| 1315 | sqlite3_mutex_enter(pMaster); |
| 1316 | pBt->nRef--; |
| 1317 | if( pBt->nRef<=0 ){ |
| 1318 | if( sqlite3SharedCacheList==pBt ){ |
| 1319 | sqlite3SharedCacheList = pBt->pNext; |
| 1320 | }else{ |
| 1321 | pList = sqlite3SharedCacheList; |
| 1322 | while( pList && pList->pNext!=pBt ){ |
| 1323 | pList=pList->pNext; |
| 1324 | } |
| 1325 | if( pList ){ |
| 1326 | pList->pNext = pBt->pNext; |
| 1327 | } |
| 1328 | } |
| 1329 | sqlite3_mutex_free(pBt->mutex); |
| 1330 | removed = 1; |
| 1331 | } |
| 1332 | sqlite3_mutex_leave(pMaster); |
| 1333 | return removed; |
| 1334 | #else |
| 1335 | return 1; |
| 1336 | #endif |
| 1337 | } |
| 1338 | |
| 1339 | /* |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 1340 | ** Close an open database and invalidate all cursors. |
| 1341 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1342 | int sqlite3BtreeClose(Btree *p){ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1343 | BtShared *pBt = p->pBt; |
| 1344 | BtCursor *pCur; |
| 1345 | |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1346 | /* Close all cursors opened via this handle. */ |
drh | d0679ed | 2007-08-28 22:24:34 +0000 | [diff] [blame^] | 1347 | assert( sqlite3_mutex_held(p->pSqlite->mutex) ); |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 1348 | sqlite3BtreeEnter(p); |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1349 | pCur = pBt->pCursor; |
| 1350 | while( pCur ){ |
| 1351 | BtCursor *pTmp = pCur; |
| 1352 | pCur = pCur->pNext; |
| 1353 | if( pTmp->pBtree==p ){ |
| 1354 | sqlite3BtreeCloseCursor(pTmp); |
| 1355 | } |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 1356 | } |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1357 | |
danielk1977 | 8d34dfd | 2006-01-24 16:37:57 +0000 | [diff] [blame] | 1358 | /* Rollback any active transaction and free the handle structure. |
| 1359 | ** The call to sqlite3BtreeRollback() drops any table-locks held by |
| 1360 | ** this handle. |
| 1361 | */ |
danielk1977 | b597f74 | 2006-01-15 11:39:18 +0000 | [diff] [blame] | 1362 | sqlite3BtreeRollback(p); |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 1363 | sqlite3BtreeLeave(p); |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1364 | |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1365 | /* If there are still other outstanding references to the shared-btree |
| 1366 | ** structure, return now. The remainder of this procedure cleans |
| 1367 | ** up the shared-btree. |
| 1368 | */ |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 1369 | assert( p->wantToLock==0 && p->locked==0 ); |
| 1370 | if( !p->sharable || removeFromSharingList(pBt) ){ |
| 1371 | /* The pBt is no longer on the sharing list, so we can access |
| 1372 | ** it without having to hold the mutex. |
| 1373 | ** |
| 1374 | ** Clean out and delete the BtShared object. |
| 1375 | */ |
| 1376 | assert( !pBt->pCursor ); |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 1377 | sqlite3PagerClose(pBt->pPager); |
| 1378 | if( pBt->xFreeSchema && pBt->pSchema ){ |
| 1379 | pBt->xFreeSchema(pBt->pSchema); |
| 1380 | } |
| 1381 | sqlite3_free(pBt->pSchema); |
| 1382 | sqlite3_free(pBt); |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1383 | } |
| 1384 | |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 1385 | #ifndef SQLITE_OMIT_SHARED_CACHE |
drh | cab5ed7 | 2007-08-22 11:41:18 +0000 | [diff] [blame] | 1386 | assert( p->wantToLock==0 ); |
| 1387 | assert( p->locked==0 ); |
| 1388 | if( p->pPrev ) p->pPrev->pNext = p->pNext; |
| 1389 | if( p->pNext ) p->pNext->pPrev = p->pPrev; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1390 | #endif |
| 1391 | |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 1392 | sqlite3_free(p); |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 1393 | return SQLITE_OK; |
| 1394 | } |
| 1395 | |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 1396 | #if SQLITE_THREADSAFE && !defined(SQLITE_OMIT_SHARED_CACHE) |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 1397 | /* |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 1398 | ** Short-cuts for entering and leaving mutexes on a cursor. |
| 1399 | */ |
drh | d0679ed | 2007-08-28 22:24:34 +0000 | [diff] [blame^] | 1400 | # define cursorEnter(X) assert( sqlite3_mutex_held(X->pBt->mutex) ) |
| 1401 | # define cursorLeave(X) |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 1402 | #else |
| 1403 | # define cursorEnter(X) |
| 1404 | # define cursorLeave(X) |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 1405 | #endif /* !SQLITE_OMIT_SHARED_CACHE */ |
| 1406 | |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 1407 | /* |
drh | 90f5ecb | 2004-07-22 01:19:35 +0000 | [diff] [blame] | 1408 | ** Change the busy handler callback function. |
| 1409 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1410 | int sqlite3BtreeSetBusyHandler(Btree *p, BusyHandler *pHandler){ |
| 1411 | BtShared *pBt = p->pBt; |
drh | d0679ed | 2007-08-28 22:24:34 +0000 | [diff] [blame^] | 1412 | assert( sqlite3_mutex_held(p->pSqlite->mutex) ); |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 1413 | sqlite3BtreeEnter(p); |
drh | b8ef32c | 2005-03-14 02:01:49 +0000 | [diff] [blame] | 1414 | pBt->pBusyHandler = pHandler; |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 1415 | sqlite3PagerSetBusyhandler(pBt->pPager, pHandler); |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 1416 | sqlite3BtreeLeave(p); |
drh | 90f5ecb | 2004-07-22 01:19:35 +0000 | [diff] [blame] | 1417 | return SQLITE_OK; |
| 1418 | } |
| 1419 | |
| 1420 | /* |
drh | da47d77 | 2002-12-02 04:25:19 +0000 | [diff] [blame] | 1421 | ** Change the limit on the number of pages allowed in the cache. |
drh | cd61c28 | 2002-03-06 22:01:34 +0000 | [diff] [blame] | 1422 | ** |
| 1423 | ** The maximum number of cache pages is set to the absolute |
| 1424 | ** value of mxPage. If mxPage is negative, the pager will |
| 1425 | ** operate asynchronously - it will not stop to do fsync()s |
| 1426 | ** to insure data is written to the disk surface before |
| 1427 | ** continuing. Transactions still work if synchronous is off, |
| 1428 | ** and the database cannot be corrupted if this program |
| 1429 | ** crashes. But if the operating system crashes or there is |
| 1430 | ** an abrupt power failure when synchronous is off, the database |
| 1431 | ** could be left in an inconsistent and unrecoverable state. |
| 1432 | ** Synchronous is on by default so database corruption is not |
| 1433 | ** normally a worry. |
drh | f57b14a | 2001-09-14 18:54:08 +0000 | [diff] [blame] | 1434 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1435 | int sqlite3BtreeSetCacheSize(Btree *p, int mxPage){ |
| 1436 | BtShared *pBt = p->pBt; |
drh | d0679ed | 2007-08-28 22:24:34 +0000 | [diff] [blame^] | 1437 | assert( sqlite3_mutex_held(p->pSqlite->mutex) ); |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 1438 | sqlite3BtreeEnter(p); |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 1439 | sqlite3PagerSetCachesize(pBt->pPager, mxPage); |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 1440 | sqlite3BtreeLeave(p); |
drh | f57b14a | 2001-09-14 18:54:08 +0000 | [diff] [blame] | 1441 | return SQLITE_OK; |
| 1442 | } |
| 1443 | |
| 1444 | /* |
drh | 973b6e3 | 2003-02-12 14:09:42 +0000 | [diff] [blame] | 1445 | ** Change the way data is synced to disk in order to increase or decrease |
| 1446 | ** how well the database resists damage due to OS crashes and power |
| 1447 | ** failures. Level 1 is the same as asynchronous (no syncs() occur and |
| 1448 | ** there is a high probability of damage) Level 2 is the default. There |
| 1449 | ** is a very low but non-zero probability of damage. Level 3 reduces the |
| 1450 | ** probability of damage to near zero but with a write performance reduction. |
| 1451 | */ |
danielk1977 | 93758c8 | 2005-01-21 08:13:14 +0000 | [diff] [blame] | 1452 | #ifndef SQLITE_OMIT_PAGER_PRAGMAS |
drh | ac530b1 | 2006-02-11 01:25:50 +0000 | [diff] [blame] | 1453 | int sqlite3BtreeSetSafetyLevel(Btree *p, int level, int fullSync){ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1454 | BtShared *pBt = p->pBt; |
drh | d0679ed | 2007-08-28 22:24:34 +0000 | [diff] [blame^] | 1455 | assert( sqlite3_mutex_held(p->pSqlite->mutex) ); |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 1456 | sqlite3BtreeEnter(p); |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 1457 | sqlite3PagerSetSafetyLevel(pBt->pPager, level, fullSync); |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 1458 | sqlite3BtreeLeave(p); |
drh | 973b6e3 | 2003-02-12 14:09:42 +0000 | [diff] [blame] | 1459 | return SQLITE_OK; |
| 1460 | } |
danielk1977 | 93758c8 | 2005-01-21 08:13:14 +0000 | [diff] [blame] | 1461 | #endif |
drh | 973b6e3 | 2003-02-12 14:09:42 +0000 | [diff] [blame] | 1462 | |
drh | 2c8997b | 2005-08-27 16:36:48 +0000 | [diff] [blame] | 1463 | /* |
| 1464 | ** Return TRUE if the given btree is set to safety level 1. In other |
| 1465 | ** words, return TRUE if no sync() occurs on the disk files. |
| 1466 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1467 | int sqlite3BtreeSyncDisabled(Btree *p){ |
| 1468 | BtShared *pBt = p->pBt; |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 1469 | int rc; |
drh | d0679ed | 2007-08-28 22:24:34 +0000 | [diff] [blame^] | 1470 | assert( sqlite3_mutex_held(p->pSqlite->mutex) ); |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 1471 | sqlite3BtreeEnter(p); |
drh | d0679ed | 2007-08-28 22:24:34 +0000 | [diff] [blame^] | 1472 | assert( pBt && pBt->pPager ); |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 1473 | rc = sqlite3PagerNosync(pBt->pPager); |
| 1474 | sqlite3BtreeLeave(p); |
| 1475 | return rc; |
drh | 2c8997b | 2005-08-27 16:36:48 +0000 | [diff] [blame] | 1476 | } |
| 1477 | |
danielk1977 | 576ec6b | 2005-01-21 11:55:25 +0000 | [diff] [blame] | 1478 | #if !defined(SQLITE_OMIT_PAGER_PRAGMAS) || !defined(SQLITE_OMIT_VACUUM) |
drh | 973b6e3 | 2003-02-12 14:09:42 +0000 | [diff] [blame] | 1479 | /* |
drh | 90f5ecb | 2004-07-22 01:19:35 +0000 | [diff] [blame] | 1480 | ** Change the default pages size and the number of reserved bytes per page. |
drh | 06f5021 | 2004-11-02 14:24:33 +0000 | [diff] [blame] | 1481 | ** |
| 1482 | ** The page size must be a power of 2 between 512 and 65536. If the page |
| 1483 | ** size supplied does not meet this constraint then the page size is not |
| 1484 | ** changed. |
| 1485 | ** |
| 1486 | ** Page sizes are constrained to be a power of two so that the region |
| 1487 | ** of the database file used for locking (beginning at PENDING_BYTE, |
| 1488 | ** the first byte past the 1GB boundary, 0x40000000) needs to occur |
| 1489 | ** at the beginning of a page. |
danielk1977 | 2812956 | 2005-01-11 10:25:06 +0000 | [diff] [blame] | 1490 | ** |
| 1491 | ** If parameter nReserve is less than zero, then the number of reserved |
| 1492 | ** bytes per page is left unchanged. |
drh | 90f5ecb | 2004-07-22 01:19:35 +0000 | [diff] [blame] | 1493 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1494 | int sqlite3BtreeSetPageSize(Btree *p, int pageSize, int nReserve){ |
| 1495 | BtShared *pBt = p->pBt; |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 1496 | sqlite3BtreeEnter(p); |
drh | 90f5ecb | 2004-07-22 01:19:35 +0000 | [diff] [blame] | 1497 | if( pBt->pageSizeFixed ){ |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 1498 | sqlite3BtreeLeave(p); |
drh | 90f5ecb | 2004-07-22 01:19:35 +0000 | [diff] [blame] | 1499 | return SQLITE_READONLY; |
| 1500 | } |
| 1501 | if( nReserve<0 ){ |
| 1502 | nReserve = pBt->pageSize - pBt->usableSize; |
| 1503 | } |
drh | 06f5021 | 2004-11-02 14:24:33 +0000 | [diff] [blame] | 1504 | if( pageSize>=512 && pageSize<=SQLITE_MAX_PAGE_SIZE && |
| 1505 | ((pageSize-1)&pageSize)==0 ){ |
drh | 07d183d | 2005-05-01 22:52:42 +0000 | [diff] [blame] | 1506 | assert( (pageSize & 7)==0 ); |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1507 | assert( !pBt->pPage1 && !pBt->pCursor ); |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 1508 | pBt->pageSize = sqlite3PagerSetPagesize(pBt->pPager, pageSize); |
drh | 90f5ecb | 2004-07-22 01:19:35 +0000 | [diff] [blame] | 1509 | } |
| 1510 | pBt->usableSize = pBt->pageSize - nReserve; |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 1511 | sqlite3BtreeLeave(p); |
drh | 90f5ecb | 2004-07-22 01:19:35 +0000 | [diff] [blame] | 1512 | return SQLITE_OK; |
| 1513 | } |
| 1514 | |
| 1515 | /* |
| 1516 | ** Return the currently defined page size |
| 1517 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1518 | int sqlite3BtreeGetPageSize(Btree *p){ |
| 1519 | return p->pBt->pageSize; |
drh | 90f5ecb | 2004-07-22 01:19:35 +0000 | [diff] [blame] | 1520 | } |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1521 | int sqlite3BtreeGetReserve(Btree *p){ |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 1522 | int n; |
| 1523 | sqlite3BtreeEnter(p); |
| 1524 | n = p->pBt->pageSize - p->pBt->usableSize; |
| 1525 | sqlite3BtreeLeave(p); |
| 1526 | return n; |
drh | 2011d5f | 2004-07-22 02:40:37 +0000 | [diff] [blame] | 1527 | } |
drh | f8e632b | 2007-05-08 14:51:36 +0000 | [diff] [blame] | 1528 | |
| 1529 | /* |
| 1530 | ** Set the maximum page count for a database if mxPage is positive. |
| 1531 | ** No changes are made if mxPage is 0 or negative. |
| 1532 | ** Regardless of the value of mxPage, return the maximum page count. |
| 1533 | */ |
| 1534 | int sqlite3BtreeMaxPageCount(Btree *p, int mxPage){ |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 1535 | int n; |
| 1536 | sqlite3BtreeEnter(p); |
| 1537 | n = sqlite3PagerMaxPageCount(p->pBt->pPager, mxPage); |
| 1538 | sqlite3BtreeLeave(p); |
| 1539 | return n; |
drh | f8e632b | 2007-05-08 14:51:36 +0000 | [diff] [blame] | 1540 | } |
danielk1977 | 576ec6b | 2005-01-21 11:55:25 +0000 | [diff] [blame] | 1541 | #endif /* !defined(SQLITE_OMIT_PAGER_PRAGMAS) || !defined(SQLITE_OMIT_VACUUM) */ |
drh | 90f5ecb | 2004-07-22 01:19:35 +0000 | [diff] [blame] | 1542 | |
| 1543 | /* |
danielk1977 | 951af80 | 2004-11-05 15:45:09 +0000 | [diff] [blame] | 1544 | ** Change the 'auto-vacuum' property of the database. If the 'autoVacuum' |
| 1545 | ** parameter is non-zero, then auto-vacuum mode is enabled. If zero, it |
| 1546 | ** is disabled. The default value for the auto-vacuum property is |
| 1547 | ** determined by the SQLITE_DEFAULT_AUTOVACUUM macro. |
| 1548 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1549 | int sqlite3BtreeSetAutoVacuum(Btree *p, int autoVacuum){ |
danielk1977 | 951af80 | 2004-11-05 15:45:09 +0000 | [diff] [blame] | 1550 | #ifdef SQLITE_OMIT_AUTOVACUUM |
drh | eee46cf | 2004-11-06 00:02:48 +0000 | [diff] [blame] | 1551 | return SQLITE_READONLY; |
danielk1977 | 951af80 | 2004-11-05 15:45:09 +0000 | [diff] [blame] | 1552 | #else |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 1553 | BtShared *pBt = p->pBt; |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 1554 | int rc = SQLITE_OK; |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 1555 | int av = (autoVacuum?1:0); |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 1556 | |
| 1557 | sqlite3BtreeEnter(p); |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 1558 | if( pBt->pageSizeFixed && av!=pBt->autoVacuum ){ |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 1559 | rc = SQLITE_READONLY; |
| 1560 | }else{ |
| 1561 | pBt->autoVacuum = av; |
danielk1977 | 951af80 | 2004-11-05 15:45:09 +0000 | [diff] [blame] | 1562 | } |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 1563 | sqlite3BtreeLeave(p); |
| 1564 | return rc; |
danielk1977 | 951af80 | 2004-11-05 15:45:09 +0000 | [diff] [blame] | 1565 | #endif |
| 1566 | } |
| 1567 | |
| 1568 | /* |
| 1569 | ** Return the value of the 'auto-vacuum' property. If auto-vacuum is |
| 1570 | ** enabled 1 is returned. Otherwise 0. |
| 1571 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1572 | int sqlite3BtreeGetAutoVacuum(Btree *p){ |
danielk1977 | 951af80 | 2004-11-05 15:45:09 +0000 | [diff] [blame] | 1573 | #ifdef SQLITE_OMIT_AUTOVACUUM |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 1574 | return BTREE_AUTOVACUUM_NONE; |
danielk1977 | 951af80 | 2004-11-05 15:45:09 +0000 | [diff] [blame] | 1575 | #else |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 1576 | int rc; |
| 1577 | sqlite3BtreeEnter(p); |
| 1578 | rc = ( |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 1579 | (!p->pBt->autoVacuum)?BTREE_AUTOVACUUM_NONE: |
| 1580 | (!p->pBt->incrVacuum)?BTREE_AUTOVACUUM_FULL: |
| 1581 | BTREE_AUTOVACUUM_INCR |
| 1582 | ); |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 1583 | sqlite3BtreeLeave(p); |
| 1584 | return rc; |
danielk1977 | 951af80 | 2004-11-05 15:45:09 +0000 | [diff] [blame] | 1585 | #endif |
| 1586 | } |
| 1587 | |
| 1588 | |
| 1589 | /* |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 1590 | ** Get a reference to pPage1 of the database file. This will |
drh | 306dc21 | 2001-05-21 13:45:10 +0000 | [diff] [blame] | 1591 | ** also acquire a readlock on that file. |
| 1592 | ** |
| 1593 | ** SQLITE_OK is returned on success. If the file is not a |
| 1594 | ** well-formed database file, then SQLITE_CORRUPT is returned. |
| 1595 | ** SQLITE_BUSY is returned if the database is locked. SQLITE_NOMEM |
drh | 4f0ee68 | 2007-03-30 20:43:40 +0000 | [diff] [blame] | 1596 | ** is returned if we run out of memory. |
drh | 306dc21 | 2001-05-21 13:45:10 +0000 | [diff] [blame] | 1597 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1598 | static int lockBtree(BtShared *pBt){ |
drh | 07d183d | 2005-05-01 22:52:42 +0000 | [diff] [blame] | 1599 | int rc, pageSize; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1600 | MemPage *pPage1; |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 1601 | |
drh | 2764170 | 2007-08-22 02:56:42 +0000 | [diff] [blame] | 1602 | assert( sqlite3BtreeMutexHeld(pBt->mutex) ); |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 1603 | if( pBt->pPage1 ) return SQLITE_OK; |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 1604 | rc = sqlite3BtreeGetPage(pBt, 1, &pPage1, 0); |
drh | 306dc21 | 2001-05-21 13:45:10 +0000 | [diff] [blame] | 1605 | if( rc!=SQLITE_OK ) return rc; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1606 | |
drh | 306dc21 | 2001-05-21 13:45:10 +0000 | [diff] [blame] | 1607 | |
| 1608 | /* Do some checking to help insure the file we opened really is |
| 1609 | ** a valid database file. |
| 1610 | */ |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 1611 | rc = SQLITE_NOTADB; |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 1612 | if( sqlite3PagerPagecount(pBt->pPager)>0 ){ |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 1613 | u8 *page1 = pPage1->aData; |
| 1614 | if( memcmp(page1, zMagicHeader, 16)!=0 ){ |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 1615 | goto page1_init_failed; |
drh | 306dc21 | 2001-05-21 13:45:10 +0000 | [diff] [blame] | 1616 | } |
drh | 309169a | 2007-04-24 17:27:51 +0000 | [diff] [blame] | 1617 | if( page1[18]>1 ){ |
| 1618 | pBt->readOnly = 1; |
| 1619 | } |
| 1620 | if( page1[19]>1 ){ |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 1621 | goto page1_init_failed; |
| 1622 | } |
drh | 07d183d | 2005-05-01 22:52:42 +0000 | [diff] [blame] | 1623 | pageSize = get2byte(&page1[16]); |
drh | 1592659 | 2007-04-06 15:02:13 +0000 | [diff] [blame] | 1624 | if( ((pageSize-1)&pageSize)!=0 || pageSize<512 ){ |
drh | 07d183d | 2005-05-01 22:52:42 +0000 | [diff] [blame] | 1625 | goto page1_init_failed; |
| 1626 | } |
| 1627 | assert( (pageSize & 7)==0 ); |
| 1628 | pBt->pageSize = pageSize; |
| 1629 | pBt->usableSize = pageSize - page1[20]; |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 1630 | if( pBt->usableSize<500 ){ |
| 1631 | goto page1_init_failed; |
| 1632 | } |
| 1633 | pBt->maxEmbedFrac = page1[21]; |
| 1634 | pBt->minEmbedFrac = page1[22]; |
| 1635 | pBt->minLeafFrac = page1[23]; |
drh | 057cd3a | 2005-02-15 16:23:02 +0000 | [diff] [blame] | 1636 | #ifndef SQLITE_OMIT_AUTOVACUUM |
| 1637 | pBt->autoVacuum = (get4byte(&page1[36 + 4*4])?1:0); |
danielk1977 | 27b1f95 | 2007-06-25 08:16:58 +0000 | [diff] [blame] | 1638 | pBt->incrVacuum = (get4byte(&page1[36 + 7*4])?1:0); |
drh | 057cd3a | 2005-02-15 16:23:02 +0000 | [diff] [blame] | 1639 | #endif |
drh | 306dc21 | 2001-05-21 13:45:10 +0000 | [diff] [blame] | 1640 | } |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 1641 | |
| 1642 | /* maxLocal is the maximum amount of payload to store locally for |
| 1643 | ** a cell. Make sure it is small enough so that at least minFanout |
| 1644 | ** cells can will fit on one page. We assume a 10-byte page header. |
| 1645 | ** Besides the payload, the cell must store: |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 1646 | ** 2-byte pointer to the cell |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 1647 | ** 4-byte child pointer |
| 1648 | ** 9-byte nKey value |
| 1649 | ** 4-byte nData value |
| 1650 | ** 4-byte overflow page pointer |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 1651 | ** So a cell consists of a 2-byte poiner, a header which is as much as |
| 1652 | ** 17 bytes long, 0 to N bytes of payload, and an optional 4 byte overflow |
| 1653 | ** page pointer. |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 1654 | */ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 1655 | pBt->maxLocal = (pBt->usableSize-12)*pBt->maxEmbedFrac/255 - 23; |
| 1656 | pBt->minLocal = (pBt->usableSize-12)*pBt->minEmbedFrac/255 - 23; |
| 1657 | pBt->maxLeaf = pBt->usableSize - 35; |
| 1658 | pBt->minLeaf = (pBt->usableSize-12)*pBt->minLeafFrac/255 - 23; |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 1659 | if( pBt->minLocal>pBt->maxLocal || pBt->maxLocal<0 ){ |
| 1660 | goto page1_init_failed; |
| 1661 | } |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 1662 | assert( pBt->maxLeaf + 23 <= MX_CELL_SIZE(pBt) ); |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1663 | pBt->pPage1 = pPage1; |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 1664 | return SQLITE_OK; |
drh | 306dc21 | 2001-05-21 13:45:10 +0000 | [diff] [blame] | 1665 | |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 1666 | page1_init_failed: |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1667 | releasePage(pPage1); |
| 1668 | pBt->pPage1 = 0; |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 1669 | return rc; |
drh | 306dc21 | 2001-05-21 13:45:10 +0000 | [diff] [blame] | 1670 | } |
| 1671 | |
| 1672 | /* |
drh | b8ef32c | 2005-03-14 02:01:49 +0000 | [diff] [blame] | 1673 | ** This routine works like lockBtree() except that it also invokes the |
| 1674 | ** busy callback if there is lock contention. |
| 1675 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1676 | static int lockBtreeWithRetry(Btree *pRef){ |
drh | b8ef32c | 2005-03-14 02:01:49 +0000 | [diff] [blame] | 1677 | int rc = SQLITE_OK; |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 1678 | |
drh | 2764170 | 2007-08-22 02:56:42 +0000 | [diff] [blame] | 1679 | assert( sqlite3BtreeMutexHeld(pRef->pSqlite->mutex) ); |
| 1680 | assert( sqlite3BtreeMutexHeld(pRef->pBt->mutex) ); |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1681 | if( pRef->inTrans==TRANS_NONE ){ |
| 1682 | u8 inTransaction = pRef->pBt->inTransaction; |
| 1683 | btreeIntegrity(pRef); |
| 1684 | rc = sqlite3BtreeBeginTrans(pRef, 0); |
| 1685 | pRef->pBt->inTransaction = inTransaction; |
| 1686 | pRef->inTrans = TRANS_NONE; |
| 1687 | if( rc==SQLITE_OK ){ |
| 1688 | pRef->pBt->nTransaction--; |
| 1689 | } |
| 1690 | btreeIntegrity(pRef); |
drh | b8ef32c | 2005-03-14 02:01:49 +0000 | [diff] [blame] | 1691 | } |
| 1692 | return rc; |
| 1693 | } |
| 1694 | |
| 1695 | |
| 1696 | /* |
drh | b8ca307 | 2001-12-05 00:21:20 +0000 | [diff] [blame] | 1697 | ** If there are no outstanding cursors and we are not in the middle |
| 1698 | ** of a transaction but there is a read lock on the database, then |
| 1699 | ** this routine unrefs the first page of the database file which |
| 1700 | ** has the effect of releasing the read lock. |
| 1701 | ** |
| 1702 | ** If there are any outstanding cursors, this routine is a no-op. |
| 1703 | ** |
| 1704 | ** If there is a transaction in progress, this routine is a no-op. |
| 1705 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1706 | static void unlockBtreeIfUnused(BtShared *pBt){ |
drh | 2764170 | 2007-08-22 02:56:42 +0000 | [diff] [blame] | 1707 | assert( sqlite3BtreeMutexHeld(pBt->mutex) ); |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1708 | if( pBt->inTransaction==TRANS_NONE && pBt->pCursor==0 && pBt->pPage1!=0 ){ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 1709 | if( sqlite3PagerRefcount(pBt->pPager)>=1 ){ |
drh | 24c9a2e | 2007-01-05 02:00:47 +0000 | [diff] [blame] | 1710 | if( pBt->pPage1->aData==0 ){ |
| 1711 | MemPage *pPage = pBt->pPage1; |
| 1712 | pPage->aData = &((u8*)pPage)[-pBt->pageSize]; |
| 1713 | pPage->pBt = pBt; |
| 1714 | pPage->pgno = 1; |
| 1715 | } |
| 1716 | releasePage(pBt->pPage1); |
drh | 51c6d96 | 2004-06-06 00:42:25 +0000 | [diff] [blame] | 1717 | } |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1718 | pBt->pPage1 = 0; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1719 | pBt->inStmt = 0; |
drh | b8ca307 | 2001-12-05 00:21:20 +0000 | [diff] [blame] | 1720 | } |
| 1721 | } |
| 1722 | |
| 1723 | /* |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 1724 | ** Create a new database by initializing the first page of the |
drh | 8c42ca9 | 2001-06-22 19:15:00 +0000 | [diff] [blame] | 1725 | ** file. |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 1726 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1727 | static int newDatabase(BtShared *pBt){ |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 1728 | MemPage *pP1; |
| 1729 | unsigned char *data; |
drh | 8c42ca9 | 2001-06-22 19:15:00 +0000 | [diff] [blame] | 1730 | int rc; |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 1731 | |
drh | 2764170 | 2007-08-22 02:56:42 +0000 | [diff] [blame] | 1732 | assert( sqlite3BtreeMutexHeld(pBt->mutex) ); |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 1733 | if( sqlite3PagerPagecount(pBt->pPager)>0 ) return SQLITE_OK; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1734 | pP1 = pBt->pPage1; |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 1735 | assert( pP1!=0 ); |
| 1736 | data = pP1->aData; |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 1737 | rc = sqlite3PagerWrite(pP1->pDbPage); |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 1738 | if( rc ) return rc; |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 1739 | memcpy(data, zMagicHeader, sizeof(zMagicHeader)); |
| 1740 | assert( sizeof(zMagicHeader)==16 ); |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 1741 | put2byte(&data[16], pBt->pageSize); |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 1742 | data[18] = 1; |
| 1743 | data[19] = 1; |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 1744 | data[20] = pBt->pageSize - pBt->usableSize; |
| 1745 | data[21] = pBt->maxEmbedFrac; |
| 1746 | data[22] = pBt->minEmbedFrac; |
| 1747 | data[23] = pBt->minLeafFrac; |
| 1748 | memset(&data[24], 0, 100-24); |
drh | e6c4381 | 2004-05-14 12:17:46 +0000 | [diff] [blame] | 1749 | zeroPage(pP1, PTF_INTKEY|PTF_LEAF|PTF_LEAFDATA ); |
drh | f2a611c | 2004-09-05 00:33:43 +0000 | [diff] [blame] | 1750 | pBt->pageSizeFixed = 1; |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 1751 | #ifndef SQLITE_OMIT_AUTOVACUUM |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 1752 | assert( pBt->autoVacuum==1 || pBt->autoVacuum==0 ); |
danielk1977 | 418899a | 2007-06-24 10:14:00 +0000 | [diff] [blame] | 1753 | assert( pBt->incrVacuum==1 || pBt->incrVacuum==0 ); |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 1754 | put4byte(&data[36 + 4*4], pBt->autoVacuum); |
danielk1977 | 418899a | 2007-06-24 10:14:00 +0000 | [diff] [blame] | 1755 | put4byte(&data[36 + 7*4], pBt->incrVacuum); |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 1756 | #endif |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 1757 | return SQLITE_OK; |
| 1758 | } |
| 1759 | |
| 1760 | /* |
danielk1977 | ee5741e | 2004-05-31 10:01:34 +0000 | [diff] [blame] | 1761 | ** Attempt to start a new transaction. A write-transaction |
drh | 684917c | 2004-10-05 02:41:42 +0000 | [diff] [blame] | 1762 | ** is started if the second argument is nonzero, otherwise a read- |
| 1763 | ** transaction. If the second argument is 2 or more and exclusive |
| 1764 | ** transaction is started, meaning that no other process is allowed |
| 1765 | ** to access the database. A preexisting transaction may not be |
drh | b8ef32c | 2005-03-14 02:01:49 +0000 | [diff] [blame] | 1766 | ** upgraded to exclusive by calling this routine a second time - the |
drh | 684917c | 2004-10-05 02:41:42 +0000 | [diff] [blame] | 1767 | ** exclusivity flag only works for a new transaction. |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 1768 | ** |
danielk1977 | ee5741e | 2004-05-31 10:01:34 +0000 | [diff] [blame] | 1769 | ** A write-transaction must be started before attempting any |
| 1770 | ** changes to the database. None of the following routines |
| 1771 | ** will work unless a transaction is started first: |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 1772 | ** |
drh | 23e11ca | 2004-05-04 17:27:28 +0000 | [diff] [blame] | 1773 | ** sqlite3BtreeCreateTable() |
| 1774 | ** sqlite3BtreeCreateIndex() |
| 1775 | ** sqlite3BtreeClearTable() |
| 1776 | ** sqlite3BtreeDropTable() |
| 1777 | ** sqlite3BtreeInsert() |
| 1778 | ** sqlite3BtreeDelete() |
| 1779 | ** sqlite3BtreeUpdateMeta() |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 1780 | ** |
drh | b8ef32c | 2005-03-14 02:01:49 +0000 | [diff] [blame] | 1781 | ** If an initial attempt to acquire the lock fails because of lock contention |
| 1782 | ** and the database was previously unlocked, then invoke the busy handler |
| 1783 | ** if there is one. But if there was previously a read-lock, do not |
| 1784 | ** invoke the busy handler - just return SQLITE_BUSY. SQLITE_BUSY is |
| 1785 | ** returned when there is already a read-lock in order to avoid a deadlock. |
| 1786 | ** |
| 1787 | ** Suppose there are two processes A and B. A has a read lock and B has |
| 1788 | ** a reserved lock. B tries to promote to exclusive but is blocked because |
| 1789 | ** of A's read lock. A tries to promote to reserved but is blocked by B. |
| 1790 | ** One or the other of the two processes must give way or there can be |
| 1791 | ** no progress. By returning SQLITE_BUSY and not invoking the busy callback |
| 1792 | ** when A already has a read lock, we encourage A to give up and let B |
| 1793 | ** proceed. |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 1794 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1795 | int sqlite3BtreeBeginTrans(Btree *p, int wrflag){ |
| 1796 | BtShared *pBt = p->pBt; |
danielk1977 | ee5741e | 2004-05-31 10:01:34 +0000 | [diff] [blame] | 1797 | int rc = SQLITE_OK; |
| 1798 | |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 1799 | sqlite3BtreeEnter(p); |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1800 | btreeIntegrity(p); |
| 1801 | |
danielk1977 | ee5741e | 2004-05-31 10:01:34 +0000 | [diff] [blame] | 1802 | /* If the btree is already in a write-transaction, or it |
| 1803 | ** is already in a read-transaction and a read-transaction |
| 1804 | ** is requested, this is a no-op. |
| 1805 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1806 | if( p->inTrans==TRANS_WRITE || (p->inTrans==TRANS_READ && !wrflag) ){ |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 1807 | goto trans_begun; |
danielk1977 | ee5741e | 2004-05-31 10:01:34 +0000 | [diff] [blame] | 1808 | } |
drh | b8ef32c | 2005-03-14 02:01:49 +0000 | [diff] [blame] | 1809 | |
| 1810 | /* Write transactions are not possible on a read-only database */ |
danielk1977 | ee5741e | 2004-05-31 10:01:34 +0000 | [diff] [blame] | 1811 | if( pBt->readOnly && wrflag ){ |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 1812 | rc = SQLITE_READONLY; |
| 1813 | goto trans_begun; |
danielk1977 | ee5741e | 2004-05-31 10:01:34 +0000 | [diff] [blame] | 1814 | } |
| 1815 | |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1816 | /* If another database handle has already opened a write transaction |
| 1817 | ** on this shared-btree structure and a second write transaction is |
| 1818 | ** requested, return SQLITE_BUSY. |
| 1819 | */ |
| 1820 | if( pBt->inTransaction==TRANS_WRITE && wrflag ){ |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 1821 | rc = SQLITE_BUSY; |
| 1822 | goto trans_begun; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1823 | } |
| 1824 | |
drh | b8ef32c | 2005-03-14 02:01:49 +0000 | [diff] [blame] | 1825 | do { |
| 1826 | if( pBt->pPage1==0 ){ |
| 1827 | rc = lockBtree(pBt); |
drh | 8c42ca9 | 2001-06-22 19:15:00 +0000 | [diff] [blame] | 1828 | } |
drh | 309169a | 2007-04-24 17:27:51 +0000 | [diff] [blame] | 1829 | |
drh | b8ef32c | 2005-03-14 02:01:49 +0000 | [diff] [blame] | 1830 | if( rc==SQLITE_OK && wrflag ){ |
drh | 309169a | 2007-04-24 17:27:51 +0000 | [diff] [blame] | 1831 | if( pBt->readOnly ){ |
| 1832 | rc = SQLITE_READONLY; |
| 1833 | }else{ |
| 1834 | rc = sqlite3PagerBegin(pBt->pPage1->pDbPage, wrflag>1); |
| 1835 | if( rc==SQLITE_OK ){ |
| 1836 | rc = newDatabase(pBt); |
| 1837 | } |
drh | b8ef32c | 2005-03-14 02:01:49 +0000 | [diff] [blame] | 1838 | } |
| 1839 | } |
| 1840 | |
| 1841 | if( rc==SQLITE_OK ){ |
drh | b8ef32c | 2005-03-14 02:01:49 +0000 | [diff] [blame] | 1842 | if( wrflag ) pBt->inStmt = 0; |
| 1843 | }else{ |
| 1844 | unlockBtreeIfUnused(pBt); |
| 1845 | } |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1846 | }while( rc==SQLITE_BUSY && pBt->inTransaction==TRANS_NONE && |
drh | a4afb65 | 2005-07-09 02:16:02 +0000 | [diff] [blame] | 1847 | sqlite3InvokeBusyHandler(pBt->pBusyHandler) ); |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1848 | |
| 1849 | if( rc==SQLITE_OK ){ |
| 1850 | if( p->inTrans==TRANS_NONE ){ |
| 1851 | pBt->nTransaction++; |
| 1852 | } |
| 1853 | p->inTrans = (wrflag?TRANS_WRITE:TRANS_READ); |
| 1854 | if( p->inTrans>pBt->inTransaction ){ |
| 1855 | pBt->inTransaction = p->inTrans; |
| 1856 | } |
| 1857 | } |
| 1858 | |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 1859 | |
| 1860 | trans_begun: |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1861 | btreeIntegrity(p); |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 1862 | sqlite3BtreeLeave(p); |
drh | b8ca307 | 2001-12-05 00:21:20 +0000 | [diff] [blame] | 1863 | return rc; |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 1864 | } |
| 1865 | |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 1866 | #ifndef SQLITE_OMIT_AUTOVACUUM |
| 1867 | |
| 1868 | /* |
| 1869 | ** Set the pointer-map entries for all children of page pPage. Also, if |
| 1870 | ** pPage contains cells that point to overflow pages, set the pointer |
| 1871 | ** map entries for the overflow pages as well. |
| 1872 | */ |
| 1873 | static int setChildPtrmaps(MemPage *pPage){ |
| 1874 | int i; /* Counter variable */ |
| 1875 | int nCell; /* Number of cells in page pPage */ |
danielk1977 | 2df71c7 | 2007-05-24 07:22:42 +0000 | [diff] [blame] | 1876 | int rc; /* Return code */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1877 | BtShared *pBt = pPage->pBt; |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 1878 | int isInitOrig = pPage->isInit; |
| 1879 | Pgno pgno = pPage->pgno; |
| 1880 | |
drh | 2764170 | 2007-08-22 02:56:42 +0000 | [diff] [blame] | 1881 | assert( sqlite3BtreeMutexHeld(pPage->pBt->mutex) ); |
danielk1977 | 2df71c7 | 2007-05-24 07:22:42 +0000 | [diff] [blame] | 1882 | rc = sqlite3BtreeInitPage(pPage, pPage->pParent); |
| 1883 | if( rc!=SQLITE_OK ){ |
| 1884 | goto set_child_ptrmaps_out; |
| 1885 | } |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 1886 | nCell = pPage->nCell; |
| 1887 | |
| 1888 | for(i=0; i<nCell; i++){ |
danielk1977 | 1cc5ed8 | 2007-05-16 17:28:43 +0000 | [diff] [blame] | 1889 | u8 *pCell = findCell(pPage, i); |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 1890 | |
danielk1977 | 2683665 | 2005-01-17 01:33:13 +0000 | [diff] [blame] | 1891 | rc = ptrmapPutOvflPtr(pPage, pCell); |
| 1892 | if( rc!=SQLITE_OK ){ |
| 1893 | goto set_child_ptrmaps_out; |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 1894 | } |
danielk1977 | 2683665 | 2005-01-17 01:33:13 +0000 | [diff] [blame] | 1895 | |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 1896 | if( !pPage->leaf ){ |
| 1897 | Pgno childPgno = get4byte(pCell); |
| 1898 | rc = ptrmapPut(pBt, childPgno, PTRMAP_BTREE, pgno); |
| 1899 | if( rc!=SQLITE_OK ) goto set_child_ptrmaps_out; |
| 1900 | } |
| 1901 | } |
| 1902 | |
| 1903 | if( !pPage->leaf ){ |
| 1904 | Pgno childPgno = get4byte(&pPage->aData[pPage->hdrOffset+8]); |
| 1905 | rc = ptrmapPut(pBt, childPgno, PTRMAP_BTREE, pgno); |
| 1906 | } |
| 1907 | |
| 1908 | set_child_ptrmaps_out: |
| 1909 | pPage->isInit = isInitOrig; |
| 1910 | return rc; |
| 1911 | } |
| 1912 | |
| 1913 | /* |
| 1914 | ** Somewhere on pPage, which is guarenteed to be a btree page, not an overflow |
| 1915 | ** page, is a pointer to page iFrom. Modify this pointer so that it points to |
| 1916 | ** iTo. Parameter eType describes the type of pointer to be modified, as |
| 1917 | ** follows: |
| 1918 | ** |
| 1919 | ** PTRMAP_BTREE: pPage is a btree-page. The pointer points at a child |
| 1920 | ** page of pPage. |
| 1921 | ** |
| 1922 | ** PTRMAP_OVERFLOW1: pPage is a btree-page. The pointer points at an overflow |
| 1923 | ** page pointed to by one of the cells on pPage. |
| 1924 | ** |
| 1925 | ** PTRMAP_OVERFLOW2: pPage is an overflow-page. The pointer points at the next |
| 1926 | ** overflow page in the list. |
| 1927 | */ |
danielk1977 | fdb7cdb | 2005-01-17 02:12:18 +0000 | [diff] [blame] | 1928 | static int modifyPagePointer(MemPage *pPage, Pgno iFrom, Pgno iTo, u8 eType){ |
drh | 2764170 | 2007-08-22 02:56:42 +0000 | [diff] [blame] | 1929 | assert( sqlite3BtreeMutexHeld(pPage->pBt->mutex) ); |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 1930 | if( eType==PTRMAP_OVERFLOW2 ){ |
danielk1977 | f78fc08 | 2004-11-02 14:40:32 +0000 | [diff] [blame] | 1931 | /* 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] | 1932 | if( get4byte(pPage->aData)!=iFrom ){ |
drh | 4928570 | 2005-09-17 15:20:26 +0000 | [diff] [blame] | 1933 | return SQLITE_CORRUPT_BKPT; |
danielk1977 | fdb7cdb | 2005-01-17 02:12:18 +0000 | [diff] [blame] | 1934 | } |
danielk1977 | f78fc08 | 2004-11-02 14:40:32 +0000 | [diff] [blame] | 1935 | put4byte(pPage->aData, iTo); |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 1936 | }else{ |
| 1937 | int isInitOrig = pPage->isInit; |
| 1938 | int i; |
| 1939 | int nCell; |
| 1940 | |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 1941 | sqlite3BtreeInitPage(pPage, 0); |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 1942 | nCell = pPage->nCell; |
| 1943 | |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 1944 | for(i=0; i<nCell; i++){ |
danielk1977 | 1cc5ed8 | 2007-05-16 17:28:43 +0000 | [diff] [blame] | 1945 | u8 *pCell = findCell(pPage, i); |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 1946 | if( eType==PTRMAP_OVERFLOW1 ){ |
| 1947 | CellInfo info; |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 1948 | sqlite3BtreeParseCellPtr(pPage, pCell, &info); |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 1949 | if( info.iOverflow ){ |
| 1950 | if( iFrom==get4byte(&pCell[info.iOverflow]) ){ |
| 1951 | put4byte(&pCell[info.iOverflow], iTo); |
| 1952 | break; |
| 1953 | } |
| 1954 | } |
| 1955 | }else{ |
| 1956 | if( get4byte(pCell)==iFrom ){ |
| 1957 | put4byte(pCell, iTo); |
| 1958 | break; |
| 1959 | } |
| 1960 | } |
| 1961 | } |
| 1962 | |
| 1963 | if( i==nCell ){ |
danielk1977 | fdb7cdb | 2005-01-17 02:12:18 +0000 | [diff] [blame] | 1964 | if( eType!=PTRMAP_BTREE || |
| 1965 | get4byte(&pPage->aData[pPage->hdrOffset+8])!=iFrom ){ |
drh | 4928570 | 2005-09-17 15:20:26 +0000 | [diff] [blame] | 1966 | return SQLITE_CORRUPT_BKPT; |
danielk1977 | fdb7cdb | 2005-01-17 02:12:18 +0000 | [diff] [blame] | 1967 | } |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 1968 | put4byte(&pPage->aData[pPage->hdrOffset+8], iTo); |
| 1969 | } |
| 1970 | |
| 1971 | pPage->isInit = isInitOrig; |
| 1972 | } |
danielk1977 | fdb7cdb | 2005-01-17 02:12:18 +0000 | [diff] [blame] | 1973 | return SQLITE_OK; |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 1974 | } |
| 1975 | |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 1976 | |
danielk1977 | 7701e81 | 2005-01-10 12:59:51 +0000 | [diff] [blame] | 1977 | /* |
| 1978 | ** Move the open database page pDbPage to location iFreePage in the |
| 1979 | ** database. The pDbPage reference remains valid. |
| 1980 | */ |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 1981 | static int relocatePage( |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1982 | BtShared *pBt, /* Btree */ |
danielk1977 | 7701e81 | 2005-01-10 12:59:51 +0000 | [diff] [blame] | 1983 | MemPage *pDbPage, /* Open page to move */ |
| 1984 | u8 eType, /* Pointer map 'type' entry for pDbPage */ |
| 1985 | Pgno iPtrPage, /* Pointer map 'page-no' entry for pDbPage */ |
| 1986 | Pgno iFreePage /* The location to move pDbPage to */ |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 1987 | ){ |
| 1988 | MemPage *pPtrPage; /* The page that contains a pointer to pDbPage */ |
| 1989 | Pgno iDbPage = pDbPage->pgno; |
| 1990 | Pager *pPager = pBt->pPager; |
| 1991 | int rc; |
| 1992 | |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 1993 | assert( eType==PTRMAP_OVERFLOW2 || eType==PTRMAP_OVERFLOW1 || |
| 1994 | eType==PTRMAP_BTREE || eType==PTRMAP_ROOTPAGE ); |
drh | 2764170 | 2007-08-22 02:56:42 +0000 | [diff] [blame] | 1995 | assert( sqlite3BtreeMutexHeld(pBt->mutex) ); |
drh | d0679ed | 2007-08-28 22:24:34 +0000 | [diff] [blame^] | 1996 | assert( pDbPage->pBt==pBt ); |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 1997 | |
| 1998 | /* Move page iDbPage from it's current location to page number iFreePage */ |
| 1999 | TRACE(("AUTOVACUUM: Moving %d to free page %d (ptr page %d type %d)\n", |
| 2000 | iDbPage, iFreePage, iPtrPage, eType)); |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 2001 | rc = sqlite3PagerMovepage(pPager, pDbPage->pDbPage, iFreePage); |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 2002 | if( rc!=SQLITE_OK ){ |
| 2003 | return rc; |
| 2004 | } |
| 2005 | pDbPage->pgno = iFreePage; |
| 2006 | |
| 2007 | /* If pDbPage was a btree-page, then it may have child pages and/or cells |
| 2008 | ** that point to overflow pages. The pointer map entries for all these |
| 2009 | ** pages need to be changed. |
| 2010 | ** |
| 2011 | ** If pDbPage is an overflow page, then the first 4 bytes may store a |
| 2012 | ** pointer to a subsequent overflow page. If this is the case, then |
| 2013 | ** the pointer map needs to be updated for the subsequent overflow page. |
| 2014 | */ |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 2015 | if( eType==PTRMAP_BTREE || eType==PTRMAP_ROOTPAGE ){ |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 2016 | rc = setChildPtrmaps(pDbPage); |
| 2017 | if( rc!=SQLITE_OK ){ |
| 2018 | return rc; |
| 2019 | } |
| 2020 | }else{ |
| 2021 | Pgno nextOvfl = get4byte(pDbPage->aData); |
| 2022 | if( nextOvfl!=0 ){ |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 2023 | rc = ptrmapPut(pBt, nextOvfl, PTRMAP_OVERFLOW2, iFreePage); |
| 2024 | if( rc!=SQLITE_OK ){ |
| 2025 | return rc; |
| 2026 | } |
| 2027 | } |
| 2028 | } |
| 2029 | |
| 2030 | /* Fix the database pointer on page iPtrPage that pointed at iDbPage so |
| 2031 | ** that it points at iFreePage. Also fix the pointer map entry for |
| 2032 | ** iPtrPage. |
| 2033 | */ |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 2034 | if( eType!=PTRMAP_ROOTPAGE ){ |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 2035 | rc = sqlite3BtreeGetPage(pBt, iPtrPage, &pPtrPage, 0); |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 2036 | if( rc!=SQLITE_OK ){ |
| 2037 | return rc; |
| 2038 | } |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 2039 | rc = sqlite3PagerWrite(pPtrPage->pDbPage); |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 2040 | if( rc!=SQLITE_OK ){ |
| 2041 | releasePage(pPtrPage); |
| 2042 | return rc; |
| 2043 | } |
danielk1977 | fdb7cdb | 2005-01-17 02:12:18 +0000 | [diff] [blame] | 2044 | rc = modifyPagePointer(pPtrPage, iDbPage, iFreePage, eType); |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 2045 | releasePage(pPtrPage); |
danielk1977 | fdb7cdb | 2005-01-17 02:12:18 +0000 | [diff] [blame] | 2046 | if( rc==SQLITE_OK ){ |
| 2047 | rc = ptrmapPut(pBt, iFreePage, eType, iPtrPage); |
| 2048 | } |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 2049 | } |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 2050 | return rc; |
| 2051 | } |
| 2052 | |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 2053 | /* Forward declaration required by incrVacuumStep(). */ |
drh | 4f0c587 | 2007-03-26 22:05:01 +0000 | [diff] [blame] | 2054 | static int allocateBtreePage(BtShared *, MemPage **, Pgno *, Pgno, u8); |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 2055 | |
| 2056 | /* |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 2057 | ** Perform a single step of an incremental-vacuum. If successful, |
| 2058 | ** return SQLITE_OK. If there is no work to do (and therefore no |
| 2059 | ** point in calling this function again), return SQLITE_DONE. |
| 2060 | ** |
| 2061 | ** More specificly, this function attempts to re-organize the |
| 2062 | ** database so that the last page of the file currently in use |
| 2063 | ** is no longer in use. |
| 2064 | ** |
| 2065 | ** If the nFin parameter is non-zero, the implementation assumes |
| 2066 | ** that the caller will keep calling incrVacuumStep() until |
| 2067 | ** it returns SQLITE_DONE or an error, and that nFin is the |
| 2068 | ** number of pages the database file will contain after this |
| 2069 | ** process is complete. |
| 2070 | */ |
| 2071 | static int incrVacuumStep(BtShared *pBt, Pgno nFin){ |
| 2072 | Pgno iLastPg; /* Last page in the database */ |
| 2073 | Pgno nFreeList; /* Number of pages still on the free-list */ |
| 2074 | |
drh | 2764170 | 2007-08-22 02:56:42 +0000 | [diff] [blame] | 2075 | assert( sqlite3BtreeMutexHeld(pBt->mutex) ); |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 2076 | iLastPg = pBt->nTrunc; |
| 2077 | if( iLastPg==0 ){ |
| 2078 | iLastPg = sqlite3PagerPagecount(pBt->pPager); |
| 2079 | } |
| 2080 | |
| 2081 | if( !PTRMAP_ISPAGE(pBt, iLastPg) && iLastPg!=PENDING_BYTE_PAGE(pBt) ){ |
| 2082 | int rc; |
| 2083 | u8 eType; |
| 2084 | Pgno iPtrPage; |
| 2085 | |
| 2086 | nFreeList = get4byte(&pBt->pPage1->aData[36]); |
| 2087 | if( nFreeList==0 || nFin==iLastPg ){ |
| 2088 | return SQLITE_DONE; |
| 2089 | } |
| 2090 | |
| 2091 | rc = ptrmapGet(pBt, iLastPg, &eType, &iPtrPage); |
| 2092 | if( rc!=SQLITE_OK ){ |
| 2093 | return rc; |
| 2094 | } |
| 2095 | if( eType==PTRMAP_ROOTPAGE ){ |
| 2096 | return SQLITE_CORRUPT_BKPT; |
| 2097 | } |
| 2098 | |
| 2099 | if( eType==PTRMAP_FREEPAGE ){ |
| 2100 | if( nFin==0 ){ |
| 2101 | /* Remove the page from the files free-list. This is not required |
danielk1977 | 4ef2449 | 2007-05-23 09:52:41 +0000 | [diff] [blame] | 2102 | ** if nFin is non-zero. In that case, the free-list will be |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 2103 | ** truncated to zero after this function returns, so it doesn't |
| 2104 | ** matter if it still contains some garbage entries. |
| 2105 | */ |
| 2106 | Pgno iFreePg; |
| 2107 | MemPage *pFreePg; |
| 2108 | rc = allocateBtreePage(pBt, &pFreePg, &iFreePg, iLastPg, 1); |
| 2109 | if( rc!=SQLITE_OK ){ |
| 2110 | return rc; |
| 2111 | } |
| 2112 | assert( iFreePg==iLastPg ); |
| 2113 | releasePage(pFreePg); |
| 2114 | } |
| 2115 | } else { |
| 2116 | Pgno iFreePg; /* Index of free page to move pLastPg to */ |
| 2117 | MemPage *pLastPg; |
| 2118 | |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 2119 | rc = sqlite3BtreeGetPage(pBt, iLastPg, &pLastPg, 0); |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 2120 | if( rc!=SQLITE_OK ){ |
| 2121 | return rc; |
| 2122 | } |
| 2123 | |
danielk1977 | b4626a3 | 2007-04-28 15:47:43 +0000 | [diff] [blame] | 2124 | /* If nFin is zero, this loop runs exactly once and page pLastPg |
| 2125 | ** is swapped with the first free page pulled off the free list. |
| 2126 | ** |
| 2127 | ** On the other hand, if nFin is greater than zero, then keep |
| 2128 | ** looping until a free-page located within the first nFin pages |
| 2129 | ** of the file is found. |
| 2130 | */ |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 2131 | do { |
| 2132 | MemPage *pFreePg; |
| 2133 | rc = allocateBtreePage(pBt, &pFreePg, &iFreePg, 0, 0); |
| 2134 | if( rc!=SQLITE_OK ){ |
| 2135 | releasePage(pLastPg); |
| 2136 | return rc; |
| 2137 | } |
| 2138 | releasePage(pFreePg); |
| 2139 | }while( nFin!=0 && iFreePg>nFin ); |
| 2140 | assert( iFreePg<iLastPg ); |
danielk1977 | b4626a3 | 2007-04-28 15:47:43 +0000 | [diff] [blame] | 2141 | |
| 2142 | rc = sqlite3PagerWrite(pLastPg->pDbPage); |
| 2143 | if( rc!=SQLITE_OK ){ |
| 2144 | return rc; |
| 2145 | } |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 2146 | rc = relocatePage(pBt, pLastPg, eType, iPtrPage, iFreePg); |
| 2147 | releasePage(pLastPg); |
| 2148 | if( rc!=SQLITE_OK ){ |
| 2149 | return rc; |
| 2150 | } |
| 2151 | } |
| 2152 | } |
| 2153 | |
| 2154 | pBt->nTrunc = iLastPg - 1; |
| 2155 | while( pBt->nTrunc==PENDING_BYTE_PAGE(pBt)||PTRMAP_ISPAGE(pBt, pBt->nTrunc) ){ |
| 2156 | pBt->nTrunc--; |
| 2157 | } |
| 2158 | return SQLITE_OK; |
| 2159 | } |
| 2160 | |
| 2161 | /* |
| 2162 | ** A write-transaction must be opened before calling this function. |
| 2163 | ** It performs a single unit of work towards an incremental vacuum. |
| 2164 | ** |
| 2165 | ** If the incremental vacuum is finished after this function has run, |
| 2166 | ** SQLITE_DONE is returned. If it is not finished, but no error occured, |
| 2167 | ** SQLITE_OK is returned. Otherwise an SQLite error code. |
| 2168 | */ |
| 2169 | int sqlite3BtreeIncrVacuum(Btree *p){ |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 2170 | int rc; |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 2171 | BtShared *pBt = p->pBt; |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 2172 | |
| 2173 | sqlite3BtreeEnter(p); |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 2174 | assert( pBt->inTransaction==TRANS_WRITE && p->inTrans==TRANS_WRITE ); |
| 2175 | if( !pBt->autoVacuum ){ |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 2176 | rc = SQLITE_DONE; |
| 2177 | }else{ |
| 2178 | invalidateAllOverflowCache(pBt); |
| 2179 | rc = incrVacuumStep(pBt, 0); |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 2180 | } |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 2181 | sqlite3BtreeLeave(p); |
| 2182 | return rc; |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 2183 | } |
| 2184 | |
| 2185 | /* |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 2186 | ** This routine is called prior to sqlite3PagerCommit when a transaction |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 2187 | ** is commited for an auto-vacuum database. |
danielk1977 | 2416872 | 2007-04-02 05:07:47 +0000 | [diff] [blame] | 2188 | ** |
| 2189 | ** If SQLITE_OK is returned, then *pnTrunc is set to the number of pages |
| 2190 | ** the database file should be truncated to during the commit process. |
| 2191 | ** i.e. the database has been reorganized so that only the first *pnTrunc |
| 2192 | ** pages are in use. |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 2193 | */ |
danielk1977 | 2416872 | 2007-04-02 05:07:47 +0000 | [diff] [blame] | 2194 | static int autoVacuumCommit(BtShared *pBt, Pgno *pnTrunc){ |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 2195 | int rc = SQLITE_OK; |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 2196 | Pager *pPager = pBt->pPager; |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 2197 | #ifndef NDEBUG |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 2198 | int nRef = sqlite3PagerRefcount(pPager); |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 2199 | #endif |
| 2200 | |
drh | 2764170 | 2007-08-22 02:56:42 +0000 | [diff] [blame] | 2201 | assert( sqlite3BtreeMutexHeld(pBt->mutex) ); |
danielk1977 | 92d4d7a | 2007-05-04 12:05:56 +0000 | [diff] [blame] | 2202 | invalidateAllOverflowCache(pBt); |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 2203 | assert(pBt->autoVacuum); |
| 2204 | if( !pBt->incrVacuum ){ |
| 2205 | Pgno nFin = 0; |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 2206 | |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 2207 | if( pBt->nTrunc==0 ){ |
| 2208 | Pgno nFree; |
| 2209 | Pgno nPtrmap; |
| 2210 | const int pgsz = pBt->pageSize; |
| 2211 | Pgno nOrig = sqlite3PagerPagecount(pBt->pPager); |
danielk1977 | e5321f0 | 2007-04-27 07:05:44 +0000 | [diff] [blame] | 2212 | |
| 2213 | if( PTRMAP_ISPAGE(pBt, nOrig) ){ |
| 2214 | return SQLITE_CORRUPT_BKPT; |
| 2215 | } |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 2216 | if( nOrig==PENDING_BYTE_PAGE(pBt) ){ |
| 2217 | nOrig--; |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 2218 | } |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 2219 | nFree = get4byte(&pBt->pPage1->aData[36]); |
| 2220 | nPtrmap = (nFree-nOrig+PTRMAP_PAGENO(pBt, nOrig)+pgsz/5)/(pgsz/5); |
| 2221 | nFin = nOrig - nFree - nPtrmap; |
| 2222 | if( nOrig>PENDING_BYTE_PAGE(pBt) && nFin<=PENDING_BYTE_PAGE(pBt) ){ |
| 2223 | nFin--; |
danielk1977 | ac11ee6 | 2005-01-15 12:45:51 +0000 | [diff] [blame] | 2224 | } |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 2225 | while( PTRMAP_ISPAGE(pBt, nFin) || nFin==PENDING_BYTE_PAGE(pBt) ){ |
| 2226 | nFin--; |
| 2227 | } |
| 2228 | } |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 2229 | |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 2230 | while( rc==SQLITE_OK ){ |
| 2231 | rc = incrVacuumStep(pBt, nFin); |
| 2232 | } |
| 2233 | if( rc==SQLITE_DONE ){ |
| 2234 | assert(nFin==0 || pBt->nTrunc==0 || nFin<=pBt->nTrunc); |
| 2235 | rc = SQLITE_OK; |
| 2236 | if( pBt->nTrunc ){ |
drh | 67f80b6 | 2007-07-23 19:26:17 +0000 | [diff] [blame] | 2237 | rc = sqlite3PagerWrite(pBt->pPage1->pDbPage); |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 2238 | put4byte(&pBt->pPage1->aData[32], 0); |
| 2239 | put4byte(&pBt->pPage1->aData[36], 0); |
| 2240 | pBt->nTrunc = nFin; |
| 2241 | } |
| 2242 | } |
| 2243 | if( rc!=SQLITE_OK ){ |
| 2244 | sqlite3PagerRollback(pPager); |
| 2245 | } |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 2246 | } |
| 2247 | |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 2248 | if( rc==SQLITE_OK ){ |
| 2249 | *pnTrunc = pBt->nTrunc; |
| 2250 | pBt->nTrunc = 0; |
| 2251 | } |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 2252 | assert( nRef==sqlite3PagerRefcount(pPager) ); |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 2253 | return rc; |
| 2254 | } |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 2255 | |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 2256 | #endif |
| 2257 | |
| 2258 | /* |
drh | 80e35f4 | 2007-03-30 14:06:34 +0000 | [diff] [blame] | 2259 | ** This routine does the first phase of a two-phase commit. This routine |
| 2260 | ** causes a rollback journal to be created (if it does not already exist) |
| 2261 | ** and populated with enough information so that if a power loss occurs |
| 2262 | ** the database can be restored to its original state by playing back |
| 2263 | ** the journal. Then the contents of the journal are flushed out to |
| 2264 | ** the disk. After the journal is safely on oxide, the changes to the |
| 2265 | ** database are written into the database file and flushed to oxide. |
| 2266 | ** At the end of this call, the rollback journal still exists on the |
| 2267 | ** disk and we are still holding all locks, so the transaction has not |
| 2268 | ** committed. See sqlite3BtreeCommit() for the second phase of the |
| 2269 | ** commit process. |
| 2270 | ** |
| 2271 | ** This call is a no-op if no write-transaction is currently active on pBt. |
| 2272 | ** |
| 2273 | ** Otherwise, sync the database file for the btree pBt. zMaster points to |
| 2274 | ** the name of a master journal file that should be written into the |
| 2275 | ** individual journal file, or is NULL, indicating no master journal file |
| 2276 | ** (single database transaction). |
| 2277 | ** |
| 2278 | ** When this is called, the master journal should already have been |
| 2279 | ** created, populated with this journal pointer and synced to disk. |
| 2280 | ** |
| 2281 | ** Once this is routine has returned, the only thing required to commit |
| 2282 | ** the write-transaction for this database file is to delete the journal. |
| 2283 | */ |
| 2284 | int sqlite3BtreeCommitPhaseOne(Btree *p, const char *zMaster){ |
| 2285 | int rc = SQLITE_OK; |
| 2286 | if( p->inTrans==TRANS_WRITE ){ |
| 2287 | BtShared *pBt = p->pBt; |
| 2288 | Pgno nTrunc = 0; |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 2289 | sqlite3BtreeEnter(p); |
drh | 80e35f4 | 2007-03-30 14:06:34 +0000 | [diff] [blame] | 2290 | #ifndef SQLITE_OMIT_AUTOVACUUM |
| 2291 | if( pBt->autoVacuum ){ |
| 2292 | rc = autoVacuumCommit(pBt, &nTrunc); |
| 2293 | if( rc!=SQLITE_OK ){ |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 2294 | sqlite3BtreeLeave(p); |
drh | 80e35f4 | 2007-03-30 14:06:34 +0000 | [diff] [blame] | 2295 | return rc; |
| 2296 | } |
| 2297 | } |
| 2298 | #endif |
| 2299 | rc = sqlite3PagerCommitPhaseOne(pBt->pPager, zMaster, nTrunc); |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 2300 | sqlite3BtreeLeave(p); |
drh | 80e35f4 | 2007-03-30 14:06:34 +0000 | [diff] [blame] | 2301 | } |
| 2302 | return rc; |
| 2303 | } |
| 2304 | |
| 2305 | /* |
drh | 2aa679f | 2001-06-25 02:11:07 +0000 | [diff] [blame] | 2306 | ** Commit the transaction currently in progress. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2307 | ** |
drh | 6e34599 | 2007-03-30 11:12:08 +0000 | [diff] [blame] | 2308 | ** This routine implements the second phase of a 2-phase commit. The |
| 2309 | ** sqlite3BtreeSync() routine does the first phase and should be invoked |
| 2310 | ** prior to calling this routine. The sqlite3BtreeSync() routine did |
| 2311 | ** all the work of writing information out to disk and flushing the |
| 2312 | ** contents so that they are written onto the disk platter. All this |
| 2313 | ** routine has to do is delete or truncate the rollback journal |
| 2314 | ** (which causes the transaction to commit) and drop locks. |
| 2315 | ** |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2316 | ** This will release the write lock on the database file. If there |
| 2317 | ** are no active cursors, it also releases the read lock. |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 2318 | */ |
drh | 80e35f4 | 2007-03-30 14:06:34 +0000 | [diff] [blame] | 2319 | int sqlite3BtreeCommitPhaseTwo(Btree *p){ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2320 | BtShared *pBt = p->pBt; |
| 2321 | |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 2322 | sqlite3BtreeEnter(p); |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2323 | btreeIntegrity(p); |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2324 | |
| 2325 | /* If the handle has a write-transaction open, commit the shared-btrees |
| 2326 | ** transaction and set the shared state to TRANS_READ. |
| 2327 | */ |
| 2328 | if( p->inTrans==TRANS_WRITE ){ |
danielk1977 | 7f7bc66 | 2006-01-23 13:47:47 +0000 | [diff] [blame] | 2329 | int rc; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2330 | assert( pBt->inTransaction==TRANS_WRITE ); |
| 2331 | assert( pBt->nTransaction>0 ); |
drh | 80e35f4 | 2007-03-30 14:06:34 +0000 | [diff] [blame] | 2332 | rc = sqlite3PagerCommitPhaseTwo(pBt->pPager); |
danielk1977 | 7f7bc66 | 2006-01-23 13:47:47 +0000 | [diff] [blame] | 2333 | if( rc!=SQLITE_OK ){ |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 2334 | sqlite3BtreeLeave(p); |
danielk1977 | 7f7bc66 | 2006-01-23 13:47:47 +0000 | [diff] [blame] | 2335 | return rc; |
| 2336 | } |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2337 | pBt->inTransaction = TRANS_READ; |
| 2338 | pBt->inStmt = 0; |
danielk1977 | ee5741e | 2004-05-31 10:01:34 +0000 | [diff] [blame] | 2339 | } |
danielk1977 | 7f7bc66 | 2006-01-23 13:47:47 +0000 | [diff] [blame] | 2340 | unlockAllTables(p); |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2341 | |
| 2342 | /* If the handle has any kind of transaction open, decrement the transaction |
| 2343 | ** count of the shared btree. If the transaction count reaches 0, set |
| 2344 | ** the shared state to TRANS_NONE. The unlockBtreeIfUnused() call below |
| 2345 | ** will unlock the pager. |
| 2346 | */ |
| 2347 | if( p->inTrans!=TRANS_NONE ){ |
| 2348 | pBt->nTransaction--; |
| 2349 | if( 0==pBt->nTransaction ){ |
| 2350 | pBt->inTransaction = TRANS_NONE; |
| 2351 | } |
| 2352 | } |
| 2353 | |
| 2354 | /* Set the handles current transaction state to TRANS_NONE and unlock |
| 2355 | ** the pager if this call closed the only read or write transaction. |
| 2356 | */ |
| 2357 | p->inTrans = TRANS_NONE; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2358 | unlockBtreeIfUnused(pBt); |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2359 | |
| 2360 | btreeIntegrity(p); |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 2361 | sqlite3BtreeLeave(p); |
danielk1977 | 7f7bc66 | 2006-01-23 13:47:47 +0000 | [diff] [blame] | 2362 | return SQLITE_OK; |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 2363 | } |
| 2364 | |
drh | 80e35f4 | 2007-03-30 14:06:34 +0000 | [diff] [blame] | 2365 | /* |
| 2366 | ** Do both phases of a commit. |
| 2367 | */ |
| 2368 | int sqlite3BtreeCommit(Btree *p){ |
| 2369 | int rc; |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 2370 | sqlite3BtreeEnter(p); |
drh | 80e35f4 | 2007-03-30 14:06:34 +0000 | [diff] [blame] | 2371 | rc = sqlite3BtreeCommitPhaseOne(p, 0); |
| 2372 | if( rc==SQLITE_OK ){ |
| 2373 | rc = sqlite3BtreeCommitPhaseTwo(p); |
| 2374 | } |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 2375 | sqlite3BtreeLeave(p); |
drh | 80e35f4 | 2007-03-30 14:06:34 +0000 | [diff] [blame] | 2376 | return rc; |
| 2377 | } |
| 2378 | |
danielk1977 | fbcd585 | 2004-06-15 02:44:18 +0000 | [diff] [blame] | 2379 | #ifndef NDEBUG |
| 2380 | /* |
| 2381 | ** Return the number of write-cursors open on this handle. This is for use |
| 2382 | ** in assert() expressions, so it is only compiled if NDEBUG is not |
| 2383 | ** defined. |
| 2384 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2385 | static int countWriteCursors(BtShared *pBt){ |
danielk1977 | fbcd585 | 2004-06-15 02:44:18 +0000 | [diff] [blame] | 2386 | BtCursor *pCur; |
| 2387 | int r = 0; |
| 2388 | for(pCur=pBt->pCursor; pCur; pCur=pCur->pNext){ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2389 | if( pCur->wrFlag ) r++; |
danielk1977 | fbcd585 | 2004-06-15 02:44:18 +0000 | [diff] [blame] | 2390 | } |
| 2391 | return r; |
| 2392 | } |
| 2393 | #endif |
| 2394 | |
drh | c39e000 | 2004-05-07 23:50:57 +0000 | [diff] [blame] | 2395 | /* |
drh | ecdc753 | 2001-09-23 02:35:53 +0000 | [diff] [blame] | 2396 | ** Rollback the transaction in progress. All cursors will be |
| 2397 | ** invalided by this operation. Any attempt to use a cursor |
| 2398 | ** that was open at the beginning of this operation will result |
| 2399 | ** in an error. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2400 | ** |
| 2401 | ** This will release the write lock on the database file. If there |
| 2402 | ** are no active cursors, it also releases the read lock. |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 2403 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2404 | int sqlite3BtreeRollback(Btree *p){ |
danielk1977 | 8d34dfd | 2006-01-24 16:37:57 +0000 | [diff] [blame] | 2405 | int rc; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2406 | BtShared *pBt = p->pBt; |
drh | 24cd67e | 2004-05-10 16:18:47 +0000 | [diff] [blame] | 2407 | MemPage *pPage1; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2408 | |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 2409 | sqlite3BtreeEnter(p); |
danielk1977 | 2b8c13e | 2006-01-24 14:21:24 +0000 | [diff] [blame] | 2410 | rc = saveAllCursors(pBt, 0, 0); |
danielk1977 | 8d34dfd | 2006-01-24 16:37:57 +0000 | [diff] [blame] | 2411 | #ifndef SQLITE_OMIT_SHARED_CACHE |
danielk1977 | 2b8c13e | 2006-01-24 14:21:24 +0000 | [diff] [blame] | 2412 | if( rc!=SQLITE_OK ){ |
danielk1977 | 8d34dfd | 2006-01-24 16:37:57 +0000 | [diff] [blame] | 2413 | /* This is a horrible situation. An IO or malloc() error occured whilst |
| 2414 | ** trying to save cursor positions. If this is an automatic rollback (as |
| 2415 | ** the result of a constraint, malloc() failure or IO error) then |
| 2416 | ** the cache may be internally inconsistent (not contain valid trees) so |
| 2417 | ** we cannot simply return the error to the caller. Instead, abort |
| 2418 | ** all queries that may be using any of the cursors that failed to save. |
| 2419 | */ |
| 2420 | while( pBt->pCursor ){ |
| 2421 | sqlite3 *db = pBt->pCursor->pBtree->pSqlite; |
| 2422 | if( db ){ |
| 2423 | sqlite3AbortOtherActiveVdbes(db, 0); |
| 2424 | } |
| 2425 | } |
danielk1977 | 2b8c13e | 2006-01-24 14:21:24 +0000 | [diff] [blame] | 2426 | } |
danielk1977 | 8d34dfd | 2006-01-24 16:37:57 +0000 | [diff] [blame] | 2427 | #endif |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2428 | btreeIntegrity(p); |
| 2429 | unlockAllTables(p); |
| 2430 | |
| 2431 | if( p->inTrans==TRANS_WRITE ){ |
danielk1977 | 8d34dfd | 2006-01-24 16:37:57 +0000 | [diff] [blame] | 2432 | int rc2; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2433 | |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 2434 | #ifndef SQLITE_OMIT_AUTOVACUUM |
| 2435 | pBt->nTrunc = 0; |
| 2436 | #endif |
| 2437 | |
danielk1977 | 8d34dfd | 2006-01-24 16:37:57 +0000 | [diff] [blame] | 2438 | assert( TRANS_WRITE==pBt->inTransaction ); |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 2439 | rc2 = sqlite3PagerRollback(pBt->pPager); |
danielk1977 | 8d34dfd | 2006-01-24 16:37:57 +0000 | [diff] [blame] | 2440 | if( rc2!=SQLITE_OK ){ |
| 2441 | rc = rc2; |
| 2442 | } |
| 2443 | |
drh | 24cd67e | 2004-05-10 16:18:47 +0000 | [diff] [blame] | 2444 | /* The rollback may have destroyed the pPage1->aData value. So |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 2445 | ** call sqlite3BtreeGetPage() on page 1 again to make |
| 2446 | ** sure pPage1->aData is set correctly. */ |
| 2447 | if( sqlite3BtreeGetPage(pBt, 1, &pPage1, 0)==SQLITE_OK ){ |
drh | 24cd67e | 2004-05-10 16:18:47 +0000 | [diff] [blame] | 2448 | releasePage(pPage1); |
| 2449 | } |
danielk1977 | fbcd585 | 2004-06-15 02:44:18 +0000 | [diff] [blame] | 2450 | assert( countWriteCursors(pBt)==0 ); |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2451 | pBt->inTransaction = TRANS_READ; |
drh | 24cd67e | 2004-05-10 16:18:47 +0000 | [diff] [blame] | 2452 | } |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2453 | |
| 2454 | if( p->inTrans!=TRANS_NONE ){ |
| 2455 | assert( pBt->nTransaction>0 ); |
| 2456 | pBt->nTransaction--; |
| 2457 | if( 0==pBt->nTransaction ){ |
| 2458 | pBt->inTransaction = TRANS_NONE; |
| 2459 | } |
| 2460 | } |
| 2461 | |
| 2462 | p->inTrans = TRANS_NONE; |
danielk1977 | ee5741e | 2004-05-31 10:01:34 +0000 | [diff] [blame] | 2463 | pBt->inStmt = 0; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2464 | unlockBtreeIfUnused(pBt); |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2465 | |
| 2466 | btreeIntegrity(p); |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 2467 | sqlite3BtreeLeave(p); |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 2468 | return rc; |
| 2469 | } |
| 2470 | |
| 2471 | /* |
drh | ab01f61 | 2004-05-22 02:55:23 +0000 | [diff] [blame] | 2472 | ** Start a statement subtransaction. The subtransaction can |
| 2473 | ** can be rolled back independently of the main transaction. |
| 2474 | ** You must start a transaction before starting a subtransaction. |
| 2475 | ** The subtransaction is ended automatically if the main transaction |
drh | 663fc63 | 2002-02-02 18:49:19 +0000 | [diff] [blame] | 2476 | ** commits or rolls back. |
| 2477 | ** |
drh | ab01f61 | 2004-05-22 02:55:23 +0000 | [diff] [blame] | 2478 | ** Only one subtransaction may be active at a time. It is an error to try |
| 2479 | ** to start a new subtransaction if another subtransaction is already active. |
| 2480 | ** |
| 2481 | ** Statement subtransactions are used around individual SQL statements |
| 2482 | ** that are contained within a BEGIN...COMMIT block. If a constraint |
| 2483 | ** error occurs within the statement, the effect of that one statement |
| 2484 | ** can be rolled back without having to rollback the entire transaction. |
drh | 663fc63 | 2002-02-02 18:49:19 +0000 | [diff] [blame] | 2485 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2486 | int sqlite3BtreeBeginStmt(Btree *p){ |
drh | 663fc63 | 2002-02-02 18:49:19 +0000 | [diff] [blame] | 2487 | int rc; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2488 | BtShared *pBt = p->pBt; |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 2489 | sqlite3BtreeEnter(p); |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2490 | if( (p->inTrans!=TRANS_WRITE) || pBt->inStmt ){ |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 2491 | rc = pBt->readOnly ? SQLITE_READONLY : SQLITE_ERROR; |
| 2492 | }else{ |
| 2493 | assert( pBt->inTransaction==TRANS_WRITE ); |
| 2494 | rc = pBt->readOnly ? SQLITE_OK : sqlite3PagerStmtBegin(pBt->pPager); |
| 2495 | pBt->inStmt = 1; |
drh | 0d65dc0 | 2002-02-03 00:56:09 +0000 | [diff] [blame] | 2496 | } |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 2497 | sqlite3BtreeLeave(p); |
drh | 663fc63 | 2002-02-02 18:49:19 +0000 | [diff] [blame] | 2498 | return rc; |
| 2499 | } |
| 2500 | |
| 2501 | |
| 2502 | /* |
drh | ab01f61 | 2004-05-22 02:55:23 +0000 | [diff] [blame] | 2503 | ** Commit the statment subtransaction currently in progress. If no |
| 2504 | ** subtransaction is active, this is a no-op. |
drh | 663fc63 | 2002-02-02 18:49:19 +0000 | [diff] [blame] | 2505 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2506 | int sqlite3BtreeCommitStmt(Btree *p){ |
drh | 663fc63 | 2002-02-02 18:49:19 +0000 | [diff] [blame] | 2507 | int rc; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2508 | BtShared *pBt = p->pBt; |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 2509 | sqlite3BtreeEnter(p); |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2510 | if( pBt->inStmt && !pBt->readOnly ){ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 2511 | rc = sqlite3PagerStmtCommit(pBt->pPager); |
drh | 663fc63 | 2002-02-02 18:49:19 +0000 | [diff] [blame] | 2512 | }else{ |
| 2513 | rc = SQLITE_OK; |
| 2514 | } |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2515 | pBt->inStmt = 0; |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 2516 | sqlite3BtreeLeave(p); |
drh | 663fc63 | 2002-02-02 18:49:19 +0000 | [diff] [blame] | 2517 | return rc; |
| 2518 | } |
| 2519 | |
| 2520 | /* |
drh | ab01f61 | 2004-05-22 02:55:23 +0000 | [diff] [blame] | 2521 | ** Rollback the active statement subtransaction. If no subtransaction |
| 2522 | ** is active this routine is a no-op. |
drh | 663fc63 | 2002-02-02 18:49:19 +0000 | [diff] [blame] | 2523 | ** |
drh | ab01f61 | 2004-05-22 02:55:23 +0000 | [diff] [blame] | 2524 | ** All cursors will be invalidated by this operation. Any attempt |
drh | 663fc63 | 2002-02-02 18:49:19 +0000 | [diff] [blame] | 2525 | ** to use a cursor that was open at the beginning of this operation |
| 2526 | ** will result in an error. |
| 2527 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2528 | int sqlite3BtreeRollbackStmt(Btree *p){ |
danielk1977 | 97a227c | 2006-01-20 16:32:04 +0000 | [diff] [blame] | 2529 | int rc = SQLITE_OK; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2530 | BtShared *pBt = p->pBt; |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 2531 | sqlite3BtreeEnter(p); |
danielk1977 | 97a227c | 2006-01-20 16:32:04 +0000 | [diff] [blame] | 2532 | sqlite3MallocDisallow(); |
| 2533 | if( pBt->inStmt && !pBt->readOnly ){ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 2534 | rc = sqlite3PagerStmtRollback(pBt->pPager); |
danielk1977 | 97a227c | 2006-01-20 16:32:04 +0000 | [diff] [blame] | 2535 | assert( countWriteCursors(pBt)==0 ); |
| 2536 | pBt->inStmt = 0; |
| 2537 | } |
| 2538 | sqlite3MallocAllow(); |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 2539 | sqlite3BtreeLeave(p); |
drh | 663fc63 | 2002-02-02 18:49:19 +0000 | [diff] [blame] | 2540 | return rc; |
| 2541 | } |
| 2542 | |
| 2543 | /* |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2544 | ** Default key comparison function to be used if no comparison function |
| 2545 | ** is specified on the sqlite3BtreeCursor() call. |
| 2546 | */ |
| 2547 | static int dfltCompare( |
| 2548 | void *NotUsed, /* User data is not used */ |
| 2549 | int n1, const void *p1, /* First key to compare */ |
| 2550 | int n2, const void *p2 /* Second key to compare */ |
| 2551 | ){ |
| 2552 | int c; |
| 2553 | c = memcmp(p1, p2, n1<n2 ? n1 : n2); |
| 2554 | if( c==0 ){ |
| 2555 | c = n1 - n2; |
| 2556 | } |
| 2557 | return c; |
| 2558 | } |
| 2559 | |
| 2560 | /* |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 2561 | ** Create a new cursor for the BTree whose root is on the page |
| 2562 | ** iTable. The act of acquiring a cursor gets a read lock on |
| 2563 | ** the database file. |
drh | 1bee3d7 | 2001-10-15 00:44:35 +0000 | [diff] [blame] | 2564 | ** |
| 2565 | ** If wrFlag==0, then the cursor can only be used for reading. |
drh | f74b8d9 | 2002-09-01 23:20:45 +0000 | [diff] [blame] | 2566 | ** If wrFlag==1, then the cursor can be used for reading or for |
| 2567 | ** writing if other conditions for writing are also met. These |
| 2568 | ** are the conditions that must be met in order for writing to |
| 2569 | ** be allowed: |
drh | 6446c4d | 2001-12-15 14:22:18 +0000 | [diff] [blame] | 2570 | ** |
drh | f74b8d9 | 2002-09-01 23:20:45 +0000 | [diff] [blame] | 2571 | ** 1: The cursor must have been opened with wrFlag==1 |
| 2572 | ** |
drh | fe5d71d | 2007-03-19 11:54:10 +0000 | [diff] [blame] | 2573 | ** 2: Other database connections that share the same pager cache |
| 2574 | ** but which are not in the READ_UNCOMMITTED state may not have |
| 2575 | ** cursors open with wrFlag==0 on the same table. Otherwise |
| 2576 | ** the changes made by this write cursor would be visible to |
| 2577 | ** the read cursors in the other database connection. |
drh | f74b8d9 | 2002-09-01 23:20:45 +0000 | [diff] [blame] | 2578 | ** |
| 2579 | ** 3: The database must be writable (not on read-only media) |
| 2580 | ** |
| 2581 | ** 4: There must be an active transaction. |
| 2582 | ** |
drh | 6446c4d | 2001-12-15 14:22:18 +0000 | [diff] [blame] | 2583 | ** No checking is done to make sure that page iTable really is the |
| 2584 | ** root page of a b-tree. If it is not, then the cursor acquired |
| 2585 | ** will not work correctly. |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2586 | ** |
| 2587 | ** The comparison function must be logically the same for every cursor |
| 2588 | ** on a particular table. Changing the comparison function will result |
| 2589 | ** in incorrect operations. If the comparison function is NULL, a |
| 2590 | ** default comparison function is used. The comparison function is |
| 2591 | ** always ignored for INTKEY tables. |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 2592 | */ |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 2593 | static int btreeCursor( |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2594 | Btree *p, /* The btree */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2595 | int iTable, /* Root page of table to open */ |
| 2596 | int wrFlag, /* 1 to write. 0 read-only */ |
| 2597 | int (*xCmp)(void*,int,const void*,int,const void*), /* Key Comparison func */ |
| 2598 | void *pArg, /* First arg to xCompare() */ |
| 2599 | BtCursor **ppCur /* Write new cursor here */ |
| 2600 | ){ |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 2601 | int rc; |
drh | 8dcd7ca | 2004-08-08 19:43:29 +0000 | [diff] [blame] | 2602 | BtCursor *pCur; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2603 | BtShared *pBt = p->pBt; |
drh | ecdc753 | 2001-09-23 02:35:53 +0000 | [diff] [blame] | 2604 | |
drh | 2764170 | 2007-08-22 02:56:42 +0000 | [diff] [blame] | 2605 | assert( sqlite3BtreeMutexHeld(pBt->mutex) ); |
drh | d0679ed | 2007-08-28 22:24:34 +0000 | [diff] [blame^] | 2606 | assert( sqlite3BtreeMutexHeld(p->pSqlite->mutex) ); |
drh | 8dcd7ca | 2004-08-08 19:43:29 +0000 | [diff] [blame] | 2607 | *ppCur = 0; |
| 2608 | if( wrFlag ){ |
drh | 8dcd7ca | 2004-08-08 19:43:29 +0000 | [diff] [blame] | 2609 | if( pBt->readOnly ){ |
| 2610 | return SQLITE_READONLY; |
| 2611 | } |
drh | 980b1a7 | 2006-08-16 16:42:48 +0000 | [diff] [blame] | 2612 | if( checkReadLocks(p, iTable, 0) ){ |
drh | 8dcd7ca | 2004-08-08 19:43:29 +0000 | [diff] [blame] | 2613 | return SQLITE_LOCKED; |
| 2614 | } |
drh | a0c9a11 | 2004-03-10 13:42:37 +0000 | [diff] [blame] | 2615 | } |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2616 | |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 2617 | if( pBt->pPage1==0 ){ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2618 | rc = lockBtreeWithRetry(p); |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 2619 | if( rc!=SQLITE_OK ){ |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 2620 | return rc; |
| 2621 | } |
drh | 1831f18 | 2007-04-24 17:35:59 +0000 | [diff] [blame] | 2622 | if( pBt->readOnly && wrFlag ){ |
| 2623 | return SQLITE_READONLY; |
| 2624 | } |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 2625 | } |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 2626 | pCur = sqlite3MallocZero( sizeof(*pCur) ); |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 2627 | if( pCur==0 ){ |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 2628 | rc = SQLITE_NOMEM; |
| 2629 | goto create_cursor_exception; |
| 2630 | } |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 2631 | pCur->pgnoRoot = (Pgno)iTable; |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 2632 | if( iTable==1 && sqlite3PagerPagecount(pBt->pPager)==0 ){ |
drh | 24cd67e | 2004-05-10 16:18:47 +0000 | [diff] [blame] | 2633 | rc = SQLITE_EMPTY; |
| 2634 | goto create_cursor_exception; |
| 2635 | } |
drh | de64713 | 2004-05-07 17:57:49 +0000 | [diff] [blame] | 2636 | rc = getAndInitPage(pBt, pCur->pgnoRoot, &pCur->pPage, 0); |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 2637 | if( rc!=SQLITE_OK ){ |
| 2638 | goto create_cursor_exception; |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 2639 | } |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2640 | |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2641 | /* Now that no other errors can occur, finish filling in the BtCursor |
| 2642 | ** variables, link the cursor into the BtShared list and set *ppCur (the |
| 2643 | ** output argument to this function). |
| 2644 | */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2645 | pCur->xCompare = xCmp ? xCmp : dfltCompare; |
| 2646 | pCur->pArg = pArg; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2647 | pCur->pBtree = p; |
drh | d0679ed | 2007-08-28 22:24:34 +0000 | [diff] [blame^] | 2648 | pCur->pBt = pBt; |
drh | ecdc753 | 2001-09-23 02:35:53 +0000 | [diff] [blame] | 2649 | pCur->wrFlag = wrFlag; |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 2650 | pCur->pNext = pBt->pCursor; |
| 2651 | if( pCur->pNext ){ |
| 2652 | pCur->pNext->pPrev = pCur; |
| 2653 | } |
| 2654 | pBt->pCursor = pCur; |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 2655 | pCur->eState = CURSOR_INVALID; |
drh | 2af926b | 2001-05-15 00:39:25 +0000 | [diff] [blame] | 2656 | *ppCur = pCur; |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 2657 | |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2658 | return SQLITE_OK; |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 2659 | |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 2660 | create_cursor_exception: |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 2661 | if( pCur ){ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2662 | releasePage(pCur->pPage); |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 2663 | sqlite3_free(pCur); |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 2664 | } |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2665 | unlockBtreeIfUnused(pBt); |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 2666 | return rc; |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 2667 | } |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 2668 | int sqlite3BtreeCursor( |
| 2669 | Btree *p, /* The btree */ |
| 2670 | int iTable, /* Root page of table to open */ |
| 2671 | int wrFlag, /* 1 to write. 0 read-only */ |
| 2672 | int (*xCmp)(void*,int,const void*,int,const void*), /* Key Comparison func */ |
| 2673 | void *pArg, /* First arg to xCompare() */ |
| 2674 | BtCursor **ppCur /* Write new cursor here */ |
| 2675 | ){ |
| 2676 | int rc; |
| 2677 | sqlite3BtreeEnter(p); |
| 2678 | rc = btreeCursor(p, iTable, wrFlag, xCmp, pArg, ppCur); |
| 2679 | sqlite3BtreeLeave(p); |
| 2680 | return rc; |
| 2681 | } |
| 2682 | |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 2683 | |
| 2684 | /* |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2685 | ** Close a cursor. The read lock on the database file is released |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 2686 | ** when the last cursor is closed. |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 2687 | */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2688 | int sqlite3BtreeCloseCursor(BtCursor *pCur){ |
drh | d0679ed | 2007-08-28 22:24:34 +0000 | [diff] [blame^] | 2689 | BtShared *pBt = pCur->pBt; |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 2690 | |
drh | d0679ed | 2007-08-28 22:24:34 +0000 | [diff] [blame^] | 2691 | assert( sqlite3_mutex_held(pCur->pBt->mutex) ); |
| 2692 | assert( sqlite3_mutex_held(pCur->pBtree->pSqlite->mutex) ); |
drh | bf700f3 | 2007-03-31 02:36:44 +0000 | [diff] [blame] | 2693 | clearCursorPosition(pCur); |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 2694 | if( pCur->pPrev ){ |
| 2695 | pCur->pPrev->pNext = pCur->pNext; |
| 2696 | }else{ |
| 2697 | pBt->pCursor = pCur->pNext; |
| 2698 | } |
| 2699 | if( pCur->pNext ){ |
| 2700 | pCur->pNext->pPrev = pCur->pPrev; |
| 2701 | } |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2702 | releasePage(pCur->pPage); |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2703 | unlockBtreeIfUnused(pBt); |
danielk1977 | 92d4d7a | 2007-05-04 12:05:56 +0000 | [diff] [blame] | 2704 | invalidateOverflowCache(pCur); |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 2705 | sqlite3_free(pCur); |
drh | 8c42ca9 | 2001-06-22 19:15:00 +0000 | [diff] [blame] | 2706 | return SQLITE_OK; |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 2707 | } |
| 2708 | |
drh | 7e3b0a0 | 2001-04-28 16:52:40 +0000 | [diff] [blame] | 2709 | /* |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 2710 | ** Make a temporary cursor by filling in the fields of pTempCur. |
| 2711 | ** The temporary cursor is not on the cursor list for the Btree. |
| 2712 | */ |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 2713 | void sqlite3BtreeGetTempCursor(BtCursor *pCur, BtCursor *pTempCur){ |
drh | d0679ed | 2007-08-28 22:24:34 +0000 | [diff] [blame^] | 2714 | cursorEnter(pCur); |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 2715 | memcpy(pTempCur, pCur, sizeof(*pCur)); |
| 2716 | pTempCur->pNext = 0; |
| 2717 | pTempCur->pPrev = 0; |
drh | ecdc753 | 2001-09-23 02:35:53 +0000 | [diff] [blame] | 2718 | if( pTempCur->pPage ){ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 2719 | sqlite3PagerRef(pTempCur->pPage->pDbPage); |
drh | ecdc753 | 2001-09-23 02:35:53 +0000 | [diff] [blame] | 2720 | } |
drh | d0679ed | 2007-08-28 22:24:34 +0000 | [diff] [blame^] | 2721 | cursorLeave(pCur); |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 2722 | } |
| 2723 | |
| 2724 | /* |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 2725 | ** Delete a temporary cursor such as was made by the CreateTemporaryCursor() |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 2726 | ** function above. |
| 2727 | */ |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 2728 | void sqlite3BtreeReleaseTempCursor(BtCursor *pCur){ |
drh | d0679ed | 2007-08-28 22:24:34 +0000 | [diff] [blame^] | 2729 | cursorEnter(pCur); |
drh | ecdc753 | 2001-09-23 02:35:53 +0000 | [diff] [blame] | 2730 | if( pCur->pPage ){ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 2731 | sqlite3PagerUnref(pCur->pPage->pDbPage); |
drh | ecdc753 | 2001-09-23 02:35:53 +0000 | [diff] [blame] | 2732 | } |
drh | d0679ed | 2007-08-28 22:24:34 +0000 | [diff] [blame^] | 2733 | cursorLeave(pCur); |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 2734 | } |
| 2735 | |
| 2736 | /* |
drh | 8605761 | 2007-06-26 01:04:48 +0000 | [diff] [blame] | 2737 | ** Make sure the BtCursor* given in the argument has a valid |
| 2738 | ** BtCursor.info structure. If it is not already valid, call |
danielk1977 | 1cc5ed8 | 2007-05-16 17:28:43 +0000 | [diff] [blame] | 2739 | ** sqlite3BtreeParseCell() to fill it in. |
drh | ab01f61 | 2004-05-22 02:55:23 +0000 | [diff] [blame] | 2740 | ** |
| 2741 | ** BtCursor.info is a cache of the information in the current cell. |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 2742 | ** Using this cache reduces the number of calls to sqlite3BtreeParseCell(). |
drh | 8605761 | 2007-06-26 01:04:48 +0000 | [diff] [blame] | 2743 | ** |
| 2744 | ** 2007-06-25: There is a bug in some versions of MSVC that cause the |
| 2745 | ** compiler to crash when getCellInfo() is implemented as a macro. |
| 2746 | ** But there is a measureable speed advantage to using the macro on gcc |
| 2747 | ** (when less compiler optimizations like -Os or -O0 are used and the |
| 2748 | ** compiler is not doing agressive inlining.) So we use a real function |
| 2749 | ** for MSVC and a macro for everything else. Ticket #2457. |
drh | 9188b38 | 2004-05-14 21:12:22 +0000 | [diff] [blame] | 2750 | */ |
drh | 9188b38 | 2004-05-14 21:12:22 +0000 | [diff] [blame] | 2751 | #ifndef NDEBUG |
danielk1977 | 1cc5ed8 | 2007-05-16 17:28:43 +0000 | [diff] [blame] | 2752 | static void assertCellInfo(BtCursor *pCur){ |
drh | 9188b38 | 2004-05-14 21:12:22 +0000 | [diff] [blame] | 2753 | CellInfo info; |
drh | 51c6d96 | 2004-06-06 00:42:25 +0000 | [diff] [blame] | 2754 | memset(&info, 0, sizeof(info)); |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 2755 | sqlite3BtreeParseCell(pCur->pPage, pCur->idx, &info); |
drh | 9188b38 | 2004-05-14 21:12:22 +0000 | [diff] [blame] | 2756 | assert( memcmp(&info, &pCur->info, sizeof(info))==0 ); |
drh | 9188b38 | 2004-05-14 21:12:22 +0000 | [diff] [blame] | 2757 | } |
danielk1977 | 1cc5ed8 | 2007-05-16 17:28:43 +0000 | [diff] [blame] | 2758 | #else |
| 2759 | #define assertCellInfo(x) |
| 2760 | #endif |
drh | 8605761 | 2007-06-26 01:04:48 +0000 | [diff] [blame] | 2761 | #ifdef _MSC_VER |
| 2762 | /* Use a real function in MSVC to work around bugs in that compiler. */ |
| 2763 | static void getCellInfo(BtCursor *pCur){ |
| 2764 | if( pCur->info.nSize==0 ){ |
| 2765 | sqlite3BtreeParseCell(pCur->pPage, pCur->idx, &pCur->info); |
| 2766 | }else{ |
| 2767 | assertCellInfo(pCur); |
| 2768 | } |
| 2769 | } |
| 2770 | #else /* if not _MSC_VER */ |
| 2771 | /* Use a macro in all other compilers so that the function is inlined */ |
| 2772 | #define getCellInfo(pCur) \ |
| 2773 | if( pCur->info.nSize==0 ){ \ |
danielk1977 | 1cc5ed8 | 2007-05-16 17:28:43 +0000 | [diff] [blame] | 2774 | sqlite3BtreeParseCell(pCur->pPage, pCur->idx, &pCur->info); \ |
drh | 8605761 | 2007-06-26 01:04:48 +0000 | [diff] [blame] | 2775 | }else{ \ |
| 2776 | assertCellInfo(pCur); \ |
| 2777 | } |
| 2778 | #endif /* _MSC_VER */ |
drh | 9188b38 | 2004-05-14 21:12:22 +0000 | [diff] [blame] | 2779 | |
| 2780 | /* |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2781 | ** Set *pSize to the size of the buffer needed to hold the value of |
| 2782 | ** the key for the current entry. If the cursor is not pointing |
| 2783 | ** to a valid entry, *pSize is set to 0. |
| 2784 | ** |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 2785 | ** For a table with the INTKEY flag set, this routine returns the key |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2786 | ** itself, not the number of bytes in the key. |
drh | 7e3b0a0 | 2001-04-28 16:52:40 +0000 | [diff] [blame] | 2787 | */ |
drh | 4a1c380 | 2004-05-12 15:15:47 +0000 | [diff] [blame] | 2788 | int sqlite3BtreeKeySize(BtCursor *pCur, i64 *pSize){ |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 2789 | int rc; |
| 2790 | |
drh | d0679ed | 2007-08-28 22:24:34 +0000 | [diff] [blame^] | 2791 | assert( sqlite3_mutex_held(pCur->pBt->mutex) ); |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 2792 | rc = restoreOrClearCursorPosition(pCur); |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 2793 | if( rc==SQLITE_OK ){ |
| 2794 | assert( pCur->eState==CURSOR_INVALID || pCur->eState==CURSOR_VALID ); |
| 2795 | if( pCur->eState==CURSOR_INVALID ){ |
| 2796 | *pSize = 0; |
| 2797 | }else{ |
drh | 8605761 | 2007-06-26 01:04:48 +0000 | [diff] [blame] | 2798 | getCellInfo(pCur); |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 2799 | *pSize = pCur->info.nKey; |
| 2800 | } |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 2801 | } |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 2802 | return rc; |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 2803 | } |
drh | 2af926b | 2001-05-15 00:39:25 +0000 | [diff] [blame] | 2804 | |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 2805 | /* |
drh | 0e1c19e | 2004-05-11 00:58:56 +0000 | [diff] [blame] | 2806 | ** Set *pSize to the number of bytes of data in the entry the |
| 2807 | ** cursor currently points to. Always return SQLITE_OK. |
| 2808 | ** Failure is not possible. If the cursor is not currently |
| 2809 | ** pointing to an entry (which can happen, for example, if |
| 2810 | ** the database is empty) then *pSize is set to 0. |
| 2811 | */ |
| 2812 | int sqlite3BtreeDataSize(BtCursor *pCur, u32 *pSize){ |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 2813 | int rc; |
| 2814 | |
drh | d0679ed | 2007-08-28 22:24:34 +0000 | [diff] [blame^] | 2815 | assert( sqlite3_mutex_held(pCur->pBt->mutex) ); |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 2816 | rc = restoreOrClearCursorPosition(pCur); |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 2817 | if( rc==SQLITE_OK ){ |
| 2818 | assert( pCur->eState==CURSOR_INVALID || pCur->eState==CURSOR_VALID ); |
| 2819 | if( pCur->eState==CURSOR_INVALID ){ |
| 2820 | /* Not pointing at a valid entry - set *pSize to 0. */ |
| 2821 | *pSize = 0; |
| 2822 | }else{ |
drh | 8605761 | 2007-06-26 01:04:48 +0000 | [diff] [blame] | 2823 | getCellInfo(pCur); |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 2824 | *pSize = pCur->info.nData; |
| 2825 | } |
drh | 0e1c19e | 2004-05-11 00:58:56 +0000 | [diff] [blame] | 2826 | } |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 2827 | return rc; |
drh | 0e1c19e | 2004-05-11 00:58:56 +0000 | [diff] [blame] | 2828 | } |
| 2829 | |
| 2830 | /* |
danielk1977 | d0441796 | 2007-05-02 13:16:30 +0000 | [diff] [blame] | 2831 | ** Given the page number of an overflow page in the database (parameter |
| 2832 | ** ovfl), this function finds the page number of the next page in the |
| 2833 | ** linked list of overflow pages. If possible, it uses the auto-vacuum |
| 2834 | ** pointer-map data instead of reading the content of page ovfl to do so. |
| 2835 | ** |
| 2836 | ** If an error occurs an SQLite error code is returned. Otherwise: |
| 2837 | ** |
| 2838 | ** Unless pPgnoNext is NULL, the page number of the next overflow |
| 2839 | ** page in the linked list is written to *pPgnoNext. If page ovfl |
| 2840 | ** is the last page in it's linked list, *pPgnoNext is set to zero. |
| 2841 | ** |
| 2842 | ** If ppPage is not NULL, *ppPage is set to the MemPage* handle |
| 2843 | ** for page ovfl. The underlying pager page may have been requested |
| 2844 | ** with the noContent flag set, so the page data accessable via |
| 2845 | ** this handle may not be trusted. |
| 2846 | */ |
| 2847 | static int getOverflowPage( |
| 2848 | BtShared *pBt, |
| 2849 | Pgno ovfl, /* Overflow page */ |
| 2850 | MemPage **ppPage, /* OUT: MemPage handle */ |
| 2851 | Pgno *pPgnoNext /* OUT: Next overflow page number */ |
| 2852 | ){ |
| 2853 | Pgno next = 0; |
| 2854 | int rc; |
| 2855 | |
drh | 2764170 | 2007-08-22 02:56:42 +0000 | [diff] [blame] | 2856 | assert( sqlite3BtreeMutexHeld(pBt->mutex) ); |
danielk1977 | d0441796 | 2007-05-02 13:16:30 +0000 | [diff] [blame] | 2857 | /* One of these must not be NULL. Otherwise, why call this function? */ |
| 2858 | assert(ppPage || pPgnoNext); |
| 2859 | |
| 2860 | /* If pPgnoNext is NULL, then this function is being called to obtain |
| 2861 | ** a MemPage* reference only. No page-data is required in this case. |
| 2862 | */ |
| 2863 | if( !pPgnoNext ){ |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 2864 | return sqlite3BtreeGetPage(pBt, ovfl, ppPage, 1); |
danielk1977 | d0441796 | 2007-05-02 13:16:30 +0000 | [diff] [blame] | 2865 | } |
| 2866 | |
| 2867 | #ifndef SQLITE_OMIT_AUTOVACUUM |
| 2868 | /* Try to find the next page in the overflow list using the |
| 2869 | ** autovacuum pointer-map pages. Guess that the next page in |
| 2870 | ** the overflow list is page number (ovfl+1). If that guess turns |
| 2871 | ** out to be wrong, fall back to loading the data of page |
| 2872 | ** number ovfl to determine the next page number. |
| 2873 | */ |
| 2874 | if( pBt->autoVacuum ){ |
| 2875 | Pgno pgno; |
| 2876 | Pgno iGuess = ovfl+1; |
| 2877 | u8 eType; |
| 2878 | |
| 2879 | while( PTRMAP_ISPAGE(pBt, iGuess) || iGuess==PENDING_BYTE_PAGE(pBt) ){ |
| 2880 | iGuess++; |
| 2881 | } |
| 2882 | |
danielk1977 | 20713f3 | 2007-05-03 11:43:33 +0000 | [diff] [blame] | 2883 | if( iGuess<=sqlite3PagerPagecount(pBt->pPager) ){ |
danielk1977 | d0441796 | 2007-05-02 13:16:30 +0000 | [diff] [blame] | 2884 | rc = ptrmapGet(pBt, iGuess, &eType, &pgno); |
| 2885 | if( rc!=SQLITE_OK ){ |
| 2886 | return rc; |
| 2887 | } |
| 2888 | if( eType==PTRMAP_OVERFLOW2 && pgno==ovfl ){ |
| 2889 | next = iGuess; |
| 2890 | } |
| 2891 | } |
| 2892 | } |
| 2893 | #endif |
| 2894 | |
| 2895 | if( next==0 || ppPage ){ |
| 2896 | MemPage *pPage = 0; |
| 2897 | |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 2898 | rc = sqlite3BtreeGetPage(pBt, ovfl, &pPage, next!=0); |
danielk1977 | d0441796 | 2007-05-02 13:16:30 +0000 | [diff] [blame] | 2899 | assert(rc==SQLITE_OK || pPage==0); |
| 2900 | if( next==0 && rc==SQLITE_OK ){ |
| 2901 | next = get4byte(pPage->aData); |
| 2902 | } |
| 2903 | |
| 2904 | if( ppPage ){ |
| 2905 | *ppPage = pPage; |
| 2906 | }else{ |
| 2907 | releasePage(pPage); |
| 2908 | } |
| 2909 | } |
| 2910 | *pPgnoNext = next; |
| 2911 | |
| 2912 | return rc; |
| 2913 | } |
| 2914 | |
danielk1977 | da10719 | 2007-05-04 08:32:13 +0000 | [diff] [blame] | 2915 | /* |
| 2916 | ** Copy data from a buffer to a page, or from a page to a buffer. |
| 2917 | ** |
| 2918 | ** pPayload is a pointer to data stored on database page pDbPage. |
| 2919 | ** If argument eOp is false, then nByte bytes of data are copied |
| 2920 | ** from pPayload to the buffer pointed at by pBuf. If eOp is true, |
| 2921 | ** then sqlite3PagerWrite() is called on pDbPage and nByte bytes |
| 2922 | ** of data are copied from the buffer pBuf to pPayload. |
| 2923 | ** |
| 2924 | ** SQLITE_OK is returned on success, otherwise an error code. |
| 2925 | */ |
| 2926 | static int copyPayload( |
| 2927 | void *pPayload, /* Pointer to page data */ |
| 2928 | void *pBuf, /* Pointer to buffer */ |
| 2929 | int nByte, /* Number of bytes to copy */ |
| 2930 | int eOp, /* 0 -> copy from page, 1 -> copy to page */ |
| 2931 | DbPage *pDbPage /* Page containing pPayload */ |
| 2932 | ){ |
| 2933 | if( eOp ){ |
| 2934 | /* Copy data from buffer to page (a write operation) */ |
| 2935 | int rc = sqlite3PagerWrite(pDbPage); |
| 2936 | if( rc!=SQLITE_OK ){ |
| 2937 | return rc; |
| 2938 | } |
| 2939 | memcpy(pPayload, pBuf, nByte); |
| 2940 | }else{ |
| 2941 | /* Copy data from page to buffer (a read operation) */ |
| 2942 | memcpy(pBuf, pPayload, nByte); |
| 2943 | } |
| 2944 | return SQLITE_OK; |
| 2945 | } |
danielk1977 | d0441796 | 2007-05-02 13:16:30 +0000 | [diff] [blame] | 2946 | |
| 2947 | /* |
danielk1977 | 9f8d640 | 2007-05-02 17:48:45 +0000 | [diff] [blame] | 2948 | ** This function is used to read or overwrite payload information |
| 2949 | ** for the entry that the pCur cursor is pointing to. If the eOp |
| 2950 | ** parameter is 0, this is a read operation (data copied into |
| 2951 | ** buffer pBuf). If it is non-zero, a write (data copied from |
| 2952 | ** buffer pBuf). |
| 2953 | ** |
| 2954 | ** A total of "amt" bytes are read or written beginning at "offset". |
| 2955 | ** Data is read to or from the buffer pBuf. |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 2956 | ** |
| 2957 | ** This routine does not make a distinction between key and data. |
danielk1977 | 9f8d640 | 2007-05-02 17:48:45 +0000 | [diff] [blame] | 2958 | ** It just reads or writes bytes from the payload area. Data might |
| 2959 | ** appear on the main page or be scattered out on multiple overflow |
| 2960 | ** pages. |
danielk1977 | da10719 | 2007-05-04 08:32:13 +0000 | [diff] [blame] | 2961 | ** |
danielk1977 | dcbb5d3 | 2007-05-04 18:36:44 +0000 | [diff] [blame] | 2962 | ** If the BtCursor.isIncrblobHandle flag is set, and the current |
danielk1977 | da10719 | 2007-05-04 08:32:13 +0000 | [diff] [blame] | 2963 | ** cursor entry uses one or more overflow pages, this function |
| 2964 | ** allocates space for and lazily popluates the overflow page-list |
| 2965 | ** cache array (BtCursor.aOverflow). Subsequent calls use this |
| 2966 | ** cache to make seeking to the supplied offset more efficient. |
| 2967 | ** |
| 2968 | ** Once an overflow page-list cache has been allocated, it may be |
| 2969 | ** invalidated if some other cursor writes to the same table, or if |
| 2970 | ** the cursor is moved to a different row. Additionally, in auto-vacuum |
| 2971 | ** mode, the following events may invalidate an overflow page-list cache. |
| 2972 | ** |
| 2973 | ** * An incremental vacuum, |
| 2974 | ** * A commit in auto_vacuum="full" mode, |
| 2975 | ** * Creating a table (may require moving an overflow page). |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 2976 | */ |
danielk1977 | 9f8d640 | 2007-05-02 17:48:45 +0000 | [diff] [blame] | 2977 | static int accessPayload( |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2978 | BtCursor *pCur, /* Cursor pointing to entry to read from */ |
| 2979 | int offset, /* Begin reading this far into payload */ |
| 2980 | int amt, /* Read this many bytes */ |
| 2981 | unsigned char *pBuf, /* Write the bytes into this buffer */ |
danielk1977 | 9f8d640 | 2007-05-02 17:48:45 +0000 | [diff] [blame] | 2982 | int skipKey, /* offset begins at data if this is true */ |
| 2983 | int eOp /* zero to read. non-zero to write. */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2984 | ){ |
| 2985 | unsigned char *aPayload; |
danielk1977 | da10719 | 2007-05-04 08:32:13 +0000 | [diff] [blame] | 2986 | int rc = SQLITE_OK; |
drh | fa1a98a | 2004-05-14 19:08:17 +0000 | [diff] [blame] | 2987 | u32 nKey; |
danielk1977 | 2dec970 | 2007-05-02 16:48:37 +0000 | [diff] [blame] | 2988 | int iIdx = 0; |
drh | d0679ed | 2007-08-28 22:24:34 +0000 | [diff] [blame^] | 2989 | MemPage *pPage = pCur->pPage; /* Btree page of current cursor entry */ |
| 2990 | BtShared *pBt = pCur->pBt; /* Btree this cursor belongs to */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2991 | |
danielk1977 | da10719 | 2007-05-04 08:32:13 +0000 | [diff] [blame] | 2992 | assert( pPage ); |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 2993 | assert( pCur->eState==CURSOR_VALID ); |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2994 | assert( pCur->idx>=0 && pCur->idx<pPage->nCell ); |
danielk1977 | da10719 | 2007-05-04 08:32:13 +0000 | [diff] [blame] | 2995 | assert( offset>=0 ); |
drh | d0679ed | 2007-08-28 22:24:34 +0000 | [diff] [blame^] | 2996 | assert( sqlite3BtreeMutexHeld(pCur->pBt->mutex) ); |
danielk1977 | da10719 | 2007-05-04 08:32:13 +0000 | [diff] [blame] | 2997 | |
drh | 8605761 | 2007-06-26 01:04:48 +0000 | [diff] [blame] | 2998 | getCellInfo(pCur); |
drh | 366fda6 | 2006-01-13 02:35:09 +0000 | [diff] [blame] | 2999 | aPayload = pCur->info.pCell + pCur->info.nHeader; |
danielk1977 | da10719 | 2007-05-04 08:32:13 +0000 | [diff] [blame] | 3000 | nKey = (pPage->intKey ? 0 : pCur->info.nKey); |
| 3001 | |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3002 | if( skipKey ){ |
drh | fa1a98a | 2004-05-14 19:08:17 +0000 | [diff] [blame] | 3003 | offset += nKey; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3004 | } |
drh | fa1a98a | 2004-05-14 19:08:17 +0000 | [diff] [blame] | 3005 | if( offset+amt > nKey+pCur->info.nData ){ |
danielk1977 | da10719 | 2007-05-04 08:32:13 +0000 | [diff] [blame] | 3006 | /* 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] | 3007 | return SQLITE_ERROR; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3008 | } |
danielk1977 | da10719 | 2007-05-04 08:32:13 +0000 | [diff] [blame] | 3009 | |
| 3010 | /* Check if data must be read/written to/from the btree page itself. */ |
drh | fa1a98a | 2004-05-14 19:08:17 +0000 | [diff] [blame] | 3011 | if( offset<pCur->info.nLocal ){ |
drh | 2af926b | 2001-05-15 00:39:25 +0000 | [diff] [blame] | 3012 | int a = amt; |
drh | fa1a98a | 2004-05-14 19:08:17 +0000 | [diff] [blame] | 3013 | if( a+offset>pCur->info.nLocal ){ |
| 3014 | a = pCur->info.nLocal - offset; |
drh | 2af926b | 2001-05-15 00:39:25 +0000 | [diff] [blame] | 3015 | } |
danielk1977 | da10719 | 2007-05-04 08:32:13 +0000 | [diff] [blame] | 3016 | rc = copyPayload(&aPayload[offset], pBuf, a, eOp, pPage->pDbPage); |
drh | 2aa679f | 2001-06-25 02:11:07 +0000 | [diff] [blame] | 3017 | offset = 0; |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 3018 | pBuf += a; |
drh | 2af926b | 2001-05-15 00:39:25 +0000 | [diff] [blame] | 3019 | amt -= a; |
drh | dd79342 | 2001-06-28 01:54:48 +0000 | [diff] [blame] | 3020 | }else{ |
drh | fa1a98a | 2004-05-14 19:08:17 +0000 | [diff] [blame] | 3021 | offset -= pCur->info.nLocal; |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 3022 | } |
danielk1977 | da10719 | 2007-05-04 08:32:13 +0000 | [diff] [blame] | 3023 | |
| 3024 | if( rc==SQLITE_OK && amt>0 ){ |
| 3025 | const int ovflSize = pBt->usableSize - 4; /* Bytes content per ovfl page */ |
| 3026 | Pgno nextPage; |
| 3027 | |
drh | fa1a98a | 2004-05-14 19:08:17 +0000 | [diff] [blame] | 3028 | nextPage = get4byte(&aPayload[pCur->info.nLocal]); |
danielk1977 | da10719 | 2007-05-04 08:32:13 +0000 | [diff] [blame] | 3029 | |
danielk1977 | 2dec970 | 2007-05-02 16:48:37 +0000 | [diff] [blame] | 3030 | #ifndef SQLITE_OMIT_INCRBLOB |
danielk1977 | dcbb5d3 | 2007-05-04 18:36:44 +0000 | [diff] [blame] | 3031 | /* If the isIncrblobHandle flag is set and the BtCursor.aOverflow[] |
danielk1977 | da10719 | 2007-05-04 08:32:13 +0000 | [diff] [blame] | 3032 | ** has not been allocated, allocate it now. The array is sized at |
| 3033 | ** one entry for each overflow page in the overflow chain. The |
| 3034 | ** page number of the first overflow page is stored in aOverflow[0], |
| 3035 | ** etc. A value of 0 in the aOverflow[] array means "not yet known" |
| 3036 | ** (the cache is lazily populated). |
| 3037 | */ |
danielk1977 | dcbb5d3 | 2007-05-04 18:36:44 +0000 | [diff] [blame] | 3038 | if( pCur->isIncrblobHandle && !pCur->aOverflow ){ |
danielk1977 | 2dec970 | 2007-05-02 16:48:37 +0000 | [diff] [blame] | 3039 | int nOvfl = (pCur->info.nPayload-pCur->info.nLocal+ovflSize-1)/ovflSize; |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 3040 | pCur->aOverflow = (Pgno *)sqlite3MallocZero(sizeof(Pgno)*nOvfl); |
danielk1977 | 2dec970 | 2007-05-02 16:48:37 +0000 | [diff] [blame] | 3041 | if( nOvfl && !pCur->aOverflow ){ |
danielk1977 | da10719 | 2007-05-04 08:32:13 +0000 | [diff] [blame] | 3042 | rc = SQLITE_NOMEM; |
danielk1977 | 2dec970 | 2007-05-02 16:48:37 +0000 | [diff] [blame] | 3043 | } |
| 3044 | } |
danielk1977 | da10719 | 2007-05-04 08:32:13 +0000 | [diff] [blame] | 3045 | |
| 3046 | /* If the overflow page-list cache has been allocated and the |
| 3047 | ** entry for the first required overflow page is valid, skip |
| 3048 | ** directly to it. |
| 3049 | */ |
danielk1977 | 2dec970 | 2007-05-02 16:48:37 +0000 | [diff] [blame] | 3050 | if( pCur->aOverflow && pCur->aOverflow[offset/ovflSize] ){ |
| 3051 | iIdx = (offset/ovflSize); |
| 3052 | nextPage = pCur->aOverflow[iIdx]; |
| 3053 | offset = (offset%ovflSize); |
| 3054 | } |
| 3055 | #endif |
danielk1977 | da10719 | 2007-05-04 08:32:13 +0000 | [diff] [blame] | 3056 | |
| 3057 | for( ; rc==SQLITE_OK && amt>0 && nextPage; iIdx++){ |
| 3058 | |
| 3059 | #ifndef SQLITE_OMIT_INCRBLOB |
| 3060 | /* If required, populate the overflow page-list cache. */ |
| 3061 | if( pCur->aOverflow ){ |
| 3062 | assert(!pCur->aOverflow[iIdx] || pCur->aOverflow[iIdx]==nextPage); |
| 3063 | pCur->aOverflow[iIdx] = nextPage; |
| 3064 | } |
| 3065 | #endif |
| 3066 | |
danielk1977 | d0441796 | 2007-05-02 13:16:30 +0000 | [diff] [blame] | 3067 | if( offset>=ovflSize ){ |
| 3068 | /* The only reason to read this page is to obtain the page |
danielk1977 | da10719 | 2007-05-04 08:32:13 +0000 | [diff] [blame] | 3069 | ** number for the next page in the overflow chain. The page |
drh | fd131da | 2007-08-07 17:13:03 +0000 | [diff] [blame] | 3070 | ** data is not required. So first try to lookup the overflow |
| 3071 | ** page-list cache, if any, then fall back to the getOverflowPage() |
danielk1977 | da10719 | 2007-05-04 08:32:13 +0000 | [diff] [blame] | 3072 | ** function. |
danielk1977 | d0441796 | 2007-05-02 13:16:30 +0000 | [diff] [blame] | 3073 | */ |
danielk1977 | 2dec970 | 2007-05-02 16:48:37 +0000 | [diff] [blame] | 3074 | #ifndef SQLITE_OMIT_INCRBLOB |
danielk1977 | da10719 | 2007-05-04 08:32:13 +0000 | [diff] [blame] | 3075 | if( pCur->aOverflow && pCur->aOverflow[iIdx+1] ){ |
| 3076 | nextPage = pCur->aOverflow[iIdx+1]; |
| 3077 | } else |
danielk1977 | 2dec970 | 2007-05-02 16:48:37 +0000 | [diff] [blame] | 3078 | #endif |
danielk1977 | da10719 | 2007-05-04 08:32:13 +0000 | [diff] [blame] | 3079 | rc = getOverflowPage(pBt, nextPage, 0, &nextPage); |
danielk1977 | da10719 | 2007-05-04 08:32:13 +0000 | [diff] [blame] | 3080 | offset -= ovflSize; |
danielk1977 | d0441796 | 2007-05-02 13:16:30 +0000 | [diff] [blame] | 3081 | }else{ |
danielk1977 | 9f8d640 | 2007-05-02 17:48:45 +0000 | [diff] [blame] | 3082 | /* Need to read this page properly. It contains some of the |
| 3083 | ** range of data that is being read (eOp==0) or written (eOp!=0). |
danielk1977 | d0441796 | 2007-05-02 13:16:30 +0000 | [diff] [blame] | 3084 | */ |
| 3085 | DbPage *pDbPage; |
danielk1977 | cfe9a69 | 2004-06-16 12:00:29 +0000 | [diff] [blame] | 3086 | int a = amt; |
danielk1977 | d0441796 | 2007-05-02 13:16:30 +0000 | [diff] [blame] | 3087 | rc = sqlite3PagerGet(pBt->pPager, nextPage, &pDbPage); |
danielk1977 | da10719 | 2007-05-04 08:32:13 +0000 | [diff] [blame] | 3088 | if( rc==SQLITE_OK ){ |
| 3089 | aPayload = sqlite3PagerGetData(pDbPage); |
| 3090 | nextPage = get4byte(aPayload); |
| 3091 | if( a + offset > ovflSize ){ |
| 3092 | a = ovflSize - offset; |
danielk1977 | 9f8d640 | 2007-05-02 17:48:45 +0000 | [diff] [blame] | 3093 | } |
danielk1977 | da10719 | 2007-05-04 08:32:13 +0000 | [diff] [blame] | 3094 | rc = copyPayload(&aPayload[offset+4], pBuf, a, eOp, pDbPage); |
| 3095 | sqlite3PagerUnref(pDbPage); |
| 3096 | offset = 0; |
| 3097 | amt -= a; |
| 3098 | pBuf += a; |
danielk1977 | 9f8d640 | 2007-05-02 17:48:45 +0000 | [diff] [blame] | 3099 | } |
danielk1977 | cfe9a69 | 2004-06-16 12:00:29 +0000 | [diff] [blame] | 3100 | } |
drh | 2af926b | 2001-05-15 00:39:25 +0000 | [diff] [blame] | 3101 | } |
drh | 2af926b | 2001-05-15 00:39:25 +0000 | [diff] [blame] | 3102 | } |
danielk1977 | cfe9a69 | 2004-06-16 12:00:29 +0000 | [diff] [blame] | 3103 | |
danielk1977 | da10719 | 2007-05-04 08:32:13 +0000 | [diff] [blame] | 3104 | if( rc==SQLITE_OK && amt>0 ){ |
drh | 4928570 | 2005-09-17 15:20:26 +0000 | [diff] [blame] | 3105 | return SQLITE_CORRUPT_BKPT; |
drh | a7fcb05 | 2001-12-14 15:09:55 +0000 | [diff] [blame] | 3106 | } |
danielk1977 | da10719 | 2007-05-04 08:32:13 +0000 | [diff] [blame] | 3107 | return rc; |
drh | 2af926b | 2001-05-15 00:39:25 +0000 | [diff] [blame] | 3108 | } |
| 3109 | |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 3110 | /* |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3111 | ** Read part of the key associated with cursor pCur. Exactly |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 3112 | ** "amt" bytes will be transfered into pBuf[]. The transfer |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3113 | ** begins at "offset". |
drh | 8c1238a | 2003-01-02 14:43:55 +0000 | [diff] [blame] | 3114 | ** |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3115 | ** Return SQLITE_OK on success or an error code if anything goes |
| 3116 | ** wrong. An error is returned if "offset+amt" is larger than |
| 3117 | ** the available payload. |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 3118 | */ |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 3119 | int sqlite3BtreeKey(BtCursor *pCur, u32 offset, u32 amt, void *pBuf){ |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 3120 | int rc; |
| 3121 | |
drh | d0679ed | 2007-08-28 22:24:34 +0000 | [diff] [blame^] | 3122 | cursorEnter(pCur); |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 3123 | rc = restoreOrClearCursorPosition(pCur); |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 3124 | if( rc==SQLITE_OK ){ |
| 3125 | assert( pCur->eState==CURSOR_VALID ); |
| 3126 | assert( pCur->pPage!=0 ); |
| 3127 | if( pCur->pPage->intKey ){ |
drh | d0679ed | 2007-08-28 22:24:34 +0000 | [diff] [blame^] | 3128 | cursorLeave(pCur); |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 3129 | return SQLITE_CORRUPT_BKPT; |
| 3130 | } |
| 3131 | assert( pCur->pPage->intKey==0 ); |
| 3132 | assert( pCur->idx>=0 && pCur->idx<pCur->pPage->nCell ); |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 3133 | rc = accessPayload(pCur, offset, amt, (unsigned char*)pBuf, 0, 0); |
drh | 6575a22 | 2005-03-10 17:06:34 +0000 | [diff] [blame] | 3134 | } |
drh | d0679ed | 2007-08-28 22:24:34 +0000 | [diff] [blame^] | 3135 | cursorLeave(pCur); |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 3136 | return rc; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3137 | } |
| 3138 | |
| 3139 | /* |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3140 | ** Read part of the data associated with cursor pCur. Exactly |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 3141 | ** "amt" bytes will be transfered into pBuf[]. The transfer |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3142 | ** begins at "offset". |
| 3143 | ** |
| 3144 | ** Return SQLITE_OK on success or an error code if anything goes |
| 3145 | ** wrong. An error is returned if "offset+amt" is larger than |
| 3146 | ** the available payload. |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 3147 | */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3148 | int sqlite3BtreeData(BtCursor *pCur, u32 offset, u32 amt, void *pBuf){ |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 3149 | int rc; |
| 3150 | |
drh | d0679ed | 2007-08-28 22:24:34 +0000 | [diff] [blame^] | 3151 | cursorEnter(pCur); |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 3152 | rc = restoreOrClearCursorPosition(pCur); |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 3153 | if( rc==SQLITE_OK ){ |
| 3154 | assert( pCur->eState==CURSOR_VALID ); |
| 3155 | assert( pCur->pPage!=0 ); |
| 3156 | assert( pCur->idx>=0 && pCur->idx<pCur->pPage->nCell ); |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 3157 | rc = accessPayload(pCur, offset, amt, pBuf, 1, 0); |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 3158 | } |
drh | d0679ed | 2007-08-28 22:24:34 +0000 | [diff] [blame^] | 3159 | cursorLeave(pCur); |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 3160 | return rc; |
drh | 2af926b | 2001-05-15 00:39:25 +0000 | [diff] [blame] | 3161 | } |
| 3162 | |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 3163 | /* |
drh | 0e1c19e | 2004-05-11 00:58:56 +0000 | [diff] [blame] | 3164 | ** Return a pointer to payload information from the entry that the |
| 3165 | ** pCur cursor is pointing to. The pointer is to the beginning of |
| 3166 | ** 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] | 3167 | ** skipKey==1. The number of bytes of available key/data is written |
| 3168 | ** into *pAmt. If *pAmt==0, then the value returned will not be |
| 3169 | ** a valid pointer. |
drh | 0e1c19e | 2004-05-11 00:58:56 +0000 | [diff] [blame] | 3170 | ** |
| 3171 | ** This routine is an optimization. It is common for the entire key |
| 3172 | ** and data to fit on the local page and for there to be no overflow |
| 3173 | ** pages. When that is so, this routine can be used to access the |
| 3174 | ** 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] | 3175 | ** onto overflow pages, then accessPayload() must be used to reassembly |
drh | 0e1c19e | 2004-05-11 00:58:56 +0000 | [diff] [blame] | 3176 | ** the key/data and copy it into a preallocated buffer. |
| 3177 | ** |
| 3178 | ** The pointer returned by this routine looks directly into the cached |
| 3179 | ** page of the database. The data might change or move the next time |
| 3180 | ** any btree routine is called. |
| 3181 | */ |
| 3182 | static const unsigned char *fetchPayload( |
| 3183 | BtCursor *pCur, /* Cursor pointing to entry to read from */ |
drh | e51c44f | 2004-05-30 20:46:09 +0000 | [diff] [blame] | 3184 | int *pAmt, /* Write the number of available bytes here */ |
drh | 0e1c19e | 2004-05-11 00:58:56 +0000 | [diff] [blame] | 3185 | int skipKey /* read beginning at data if this is true */ |
| 3186 | ){ |
| 3187 | unsigned char *aPayload; |
| 3188 | MemPage *pPage; |
drh | fa1a98a | 2004-05-14 19:08:17 +0000 | [diff] [blame] | 3189 | u32 nKey; |
| 3190 | int nLocal; |
drh | 0e1c19e | 2004-05-11 00:58:56 +0000 | [diff] [blame] | 3191 | |
| 3192 | assert( pCur!=0 && pCur->pPage!=0 ); |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 3193 | assert( pCur->eState==CURSOR_VALID ); |
drh | d0679ed | 2007-08-28 22:24:34 +0000 | [diff] [blame^] | 3194 | assert( sqlite3BtreeMutexHeld(pCur->pBt->mutex) ); |
drh | 0e1c19e | 2004-05-11 00:58:56 +0000 | [diff] [blame] | 3195 | pPage = pCur->pPage; |
drh | 0e1c19e | 2004-05-11 00:58:56 +0000 | [diff] [blame] | 3196 | assert( pCur->idx>=0 && pCur->idx<pPage->nCell ); |
drh | 8605761 | 2007-06-26 01:04:48 +0000 | [diff] [blame] | 3197 | getCellInfo(pCur); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 3198 | aPayload = pCur->info.pCell; |
drh | fa1a98a | 2004-05-14 19:08:17 +0000 | [diff] [blame] | 3199 | aPayload += pCur->info.nHeader; |
drh | 0e1c19e | 2004-05-11 00:58:56 +0000 | [diff] [blame] | 3200 | if( pPage->intKey ){ |
drh | fa1a98a | 2004-05-14 19:08:17 +0000 | [diff] [blame] | 3201 | nKey = 0; |
| 3202 | }else{ |
| 3203 | nKey = pCur->info.nKey; |
drh | 0e1c19e | 2004-05-11 00:58:56 +0000 | [diff] [blame] | 3204 | } |
drh | 0e1c19e | 2004-05-11 00:58:56 +0000 | [diff] [blame] | 3205 | if( skipKey ){ |
drh | fa1a98a | 2004-05-14 19:08:17 +0000 | [diff] [blame] | 3206 | aPayload += nKey; |
| 3207 | nLocal = pCur->info.nLocal - nKey; |
drh | 0e1c19e | 2004-05-11 00:58:56 +0000 | [diff] [blame] | 3208 | }else{ |
drh | fa1a98a | 2004-05-14 19:08:17 +0000 | [diff] [blame] | 3209 | nLocal = pCur->info.nLocal; |
drh | e51c44f | 2004-05-30 20:46:09 +0000 | [diff] [blame] | 3210 | if( nLocal>nKey ){ |
| 3211 | nLocal = nKey; |
| 3212 | } |
drh | 0e1c19e | 2004-05-11 00:58:56 +0000 | [diff] [blame] | 3213 | } |
drh | e51c44f | 2004-05-30 20:46:09 +0000 | [diff] [blame] | 3214 | *pAmt = nLocal; |
drh | 0e1c19e | 2004-05-11 00:58:56 +0000 | [diff] [blame] | 3215 | return aPayload; |
| 3216 | } |
| 3217 | |
| 3218 | |
| 3219 | /* |
drh | e51c44f | 2004-05-30 20:46:09 +0000 | [diff] [blame] | 3220 | ** For the entry that cursor pCur is point to, return as |
| 3221 | ** many bytes of the key or data as are available on the local |
| 3222 | ** b-tree page. Write the number of available bytes into *pAmt. |
drh | 0e1c19e | 2004-05-11 00:58:56 +0000 | [diff] [blame] | 3223 | ** |
| 3224 | ** The pointer returned is ephemeral. The key/data may move |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 3225 | ** or be destroyed on the next call to any Btree routine, |
| 3226 | ** including calls from other threads against the same cache. |
| 3227 | ** Hence, a mutex on the BtShared should be held prior to calling |
| 3228 | ** this routine. |
drh | 0e1c19e | 2004-05-11 00:58:56 +0000 | [diff] [blame] | 3229 | ** |
| 3230 | ** These routines is used to get quick access to key and data |
| 3231 | ** in the common case where no overflow pages are used. |
drh | 0e1c19e | 2004-05-11 00:58:56 +0000 | [diff] [blame] | 3232 | */ |
drh | e51c44f | 2004-05-30 20:46:09 +0000 | [diff] [blame] | 3233 | const void *sqlite3BtreeKeyFetch(BtCursor *pCur, int *pAmt){ |
drh | d0679ed | 2007-08-28 22:24:34 +0000 | [diff] [blame^] | 3234 | assert( sqlite3BtreeMutexHeld(pCur->pBt->mutex) ); |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 3235 | if( pCur->eState==CURSOR_VALID ){ |
| 3236 | return (const void*)fetchPayload(pCur, pAmt, 0); |
| 3237 | } |
| 3238 | return 0; |
drh | 0e1c19e | 2004-05-11 00:58:56 +0000 | [diff] [blame] | 3239 | } |
drh | e51c44f | 2004-05-30 20:46:09 +0000 | [diff] [blame] | 3240 | const void *sqlite3BtreeDataFetch(BtCursor *pCur, int *pAmt){ |
drh | d0679ed | 2007-08-28 22:24:34 +0000 | [diff] [blame^] | 3241 | assert( sqlite3BtreeMutexHeld(pCur->pBt->mutex) ); |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 3242 | if( pCur->eState==CURSOR_VALID ){ |
| 3243 | return (const void*)fetchPayload(pCur, pAmt, 1); |
| 3244 | } |
| 3245 | return 0; |
drh | 0e1c19e | 2004-05-11 00:58:56 +0000 | [diff] [blame] | 3246 | } |
| 3247 | |
| 3248 | |
| 3249 | /* |
drh | 8178a75 | 2003-01-05 21:41:40 +0000 | [diff] [blame] | 3250 | ** 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] | 3251 | ** page number of the child page to move to. |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 3252 | */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3253 | static int moveToChild(BtCursor *pCur, u32 newPgno){ |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 3254 | int rc; |
| 3255 | MemPage *pNewPage; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3256 | MemPage *pOldPage; |
drh | d0679ed | 2007-08-28 22:24:34 +0000 | [diff] [blame^] | 3257 | BtShared *pBt = pCur->pBt; |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 3258 | |
drh | 2764170 | 2007-08-22 02:56:42 +0000 | [diff] [blame] | 3259 | assert( sqlite3BtreeMutexHeld(pBt->mutex) ); |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 3260 | assert( pCur->eState==CURSOR_VALID ); |
drh | de64713 | 2004-05-07 17:57:49 +0000 | [diff] [blame] | 3261 | rc = getAndInitPage(pBt, newPgno, &pNewPage, pCur->pPage); |
drh | 6019e16 | 2001-07-02 17:51:45 +0000 | [diff] [blame] | 3262 | if( rc ) return rc; |
drh | 428ae8c | 2003-01-04 16:48:09 +0000 | [diff] [blame] | 3263 | pNewPage->idxParent = pCur->idx; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3264 | pOldPage = pCur->pPage; |
| 3265 | pOldPage->idxShift = 0; |
| 3266 | releasePage(pOldPage); |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 3267 | pCur->pPage = pNewPage; |
| 3268 | pCur->idx = 0; |
drh | 271efa5 | 2004-05-30 19:19:05 +0000 | [diff] [blame] | 3269 | pCur->info.nSize = 0; |
drh | 4be295b | 2003-12-16 03:44:47 +0000 | [diff] [blame] | 3270 | if( pNewPage->nCell<1 ){ |
drh | 4928570 | 2005-09-17 15:20:26 +0000 | [diff] [blame] | 3271 | return SQLITE_CORRUPT_BKPT; |
drh | 4be295b | 2003-12-16 03:44:47 +0000 | [diff] [blame] | 3272 | } |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 3273 | return SQLITE_OK; |
| 3274 | } |
| 3275 | |
| 3276 | /* |
drh | 8856d6a | 2004-04-29 14:42:46 +0000 | [diff] [blame] | 3277 | ** Return true if the page is the virtual root of its table. |
| 3278 | ** |
| 3279 | ** The virtual root page is the root page for most tables. But |
| 3280 | ** for the table rooted on page 1, sometime the real root page |
| 3281 | ** is empty except for the right-pointer. In such cases the |
| 3282 | ** virtual root page is the page that the right-pointer of page |
| 3283 | ** 1 is pointing to. |
| 3284 | */ |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 3285 | int sqlite3BtreeIsRootPage(MemPage *pPage){ |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 3286 | MemPage *pParent; |
| 3287 | |
drh | 2764170 | 2007-08-22 02:56:42 +0000 | [diff] [blame] | 3288 | assert( sqlite3BtreeMutexHeld(pPage->pBt->mutex) ); |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 3289 | pParent = pPage->pParent; |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 3290 | if( pParent==0 ) return 1; |
| 3291 | if( pParent->pgno>1 ) return 0; |
| 3292 | if( get2byte(&pParent->aData[pParent->hdrOffset+3])==0 ) return 1; |
drh | 8856d6a | 2004-04-29 14:42:46 +0000 | [diff] [blame] | 3293 | return 0; |
| 3294 | } |
| 3295 | |
| 3296 | /* |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 3297 | ** Move the cursor up to the parent page. |
| 3298 | ** |
| 3299 | ** pCur->idx is set to the cell index that contains the pointer |
| 3300 | ** to the page we are coming from. If we are coming from the |
| 3301 | ** right-most child page then pCur->idx is set to one more than |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 3302 | ** the largest cell index. |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 3303 | */ |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 3304 | void sqlite3BtreeMoveToParent(BtCursor *pCur){ |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 3305 | MemPage *pParent; |
drh | 8178a75 | 2003-01-05 21:41:40 +0000 | [diff] [blame] | 3306 | MemPage *pPage; |
drh | 428ae8c | 2003-01-04 16:48:09 +0000 | [diff] [blame] | 3307 | int idxParent; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3308 | |
drh | d0679ed | 2007-08-28 22:24:34 +0000 | [diff] [blame^] | 3309 | cursorEnter(pCur); |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 3310 | assert( pCur->eState==CURSOR_VALID ); |
drh | 8178a75 | 2003-01-05 21:41:40 +0000 | [diff] [blame] | 3311 | pPage = pCur->pPage; |
| 3312 | assert( pPage!=0 ); |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 3313 | assert( !sqlite3BtreeIsRootPage(pPage) ); |
drh | 8178a75 | 2003-01-05 21:41:40 +0000 | [diff] [blame] | 3314 | pParent = pPage->pParent; |
| 3315 | assert( pParent!=0 ); |
| 3316 | idxParent = pPage->idxParent; |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 3317 | sqlite3PagerRef(pParent->pDbPage); |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3318 | releasePage(pPage); |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 3319 | pCur->pPage = pParent; |
drh | 271efa5 | 2004-05-30 19:19:05 +0000 | [diff] [blame] | 3320 | pCur->info.nSize = 0; |
drh | 428ae8c | 2003-01-04 16:48:09 +0000 | [diff] [blame] | 3321 | assert( pParent->idxShift==0 ); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 3322 | pCur->idx = idxParent; |
drh | d0679ed | 2007-08-28 22:24:34 +0000 | [diff] [blame^] | 3323 | cursorLeave(pCur); |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 3324 | } |
| 3325 | |
| 3326 | /* |
| 3327 | ** Move the cursor to the root page |
| 3328 | */ |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 3329 | static int moveToRoot(BtCursor *pCur){ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3330 | MemPage *pRoot; |
drh | 777e4c4 | 2006-01-13 04:31:58 +0000 | [diff] [blame] | 3331 | int rc = SQLITE_OK; |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 3332 | Btree *p = pCur->pBtree; |
| 3333 | BtShared *pBt = p->pBt; |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 3334 | |
drh | 2764170 | 2007-08-22 02:56:42 +0000 | [diff] [blame] | 3335 | assert( sqlite3BtreeMutexHeld(pBt->mutex) ); |
drh | bf700f3 | 2007-03-31 02:36:44 +0000 | [diff] [blame] | 3336 | if( pCur->eState==CURSOR_REQUIRESEEK ){ |
| 3337 | clearCursorPosition(pCur); |
| 3338 | } |
drh | 777e4c4 | 2006-01-13 04:31:58 +0000 | [diff] [blame] | 3339 | pRoot = pCur->pPage; |
danielk1977 | 97a227c | 2006-01-20 16:32:04 +0000 | [diff] [blame] | 3340 | if( pRoot && pRoot->pgno==pCur->pgnoRoot ){ |
drh | 777e4c4 | 2006-01-13 04:31:58 +0000 | [diff] [blame] | 3341 | assert( pRoot->isInit ); |
| 3342 | }else{ |
| 3343 | if( |
| 3344 | SQLITE_OK!=(rc = getAndInitPage(pBt, pCur->pgnoRoot, &pRoot, 0)) |
| 3345 | ){ |
| 3346 | pCur->eState = CURSOR_INVALID; |
| 3347 | return rc; |
| 3348 | } |
| 3349 | releasePage(pCur->pPage); |
drh | 777e4c4 | 2006-01-13 04:31:58 +0000 | [diff] [blame] | 3350 | pCur->pPage = pRoot; |
drh | c39e000 | 2004-05-07 23:50:57 +0000 | [diff] [blame] | 3351 | } |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 3352 | pCur->idx = 0; |
drh | 271efa5 | 2004-05-30 19:19:05 +0000 | [diff] [blame] | 3353 | pCur->info.nSize = 0; |
drh | 8856d6a | 2004-04-29 14:42:46 +0000 | [diff] [blame] | 3354 | if( pRoot->nCell==0 && !pRoot->leaf ){ |
| 3355 | Pgno subpage; |
| 3356 | assert( pRoot->pgno==1 ); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 3357 | subpage = get4byte(&pRoot->aData[pRoot->hdrOffset+8]); |
drh | 8856d6a | 2004-04-29 14:42:46 +0000 | [diff] [blame] | 3358 | assert( subpage>0 ); |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 3359 | pCur->eState = CURSOR_VALID; |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 3360 | rc = moveToChild(pCur, subpage); |
drh | 8856d6a | 2004-04-29 14:42:46 +0000 | [diff] [blame] | 3361 | } |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 3362 | pCur->eState = ((pCur->pPage->nCell>0)?CURSOR_VALID:CURSOR_INVALID); |
drh | 8856d6a | 2004-04-29 14:42:46 +0000 | [diff] [blame] | 3363 | return rc; |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 3364 | } |
drh | 2af926b | 2001-05-15 00:39:25 +0000 | [diff] [blame] | 3365 | |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 3366 | /* |
| 3367 | ** Move the cursor down to the left-most leaf entry beneath the |
| 3368 | ** entry to which it is currently pointing. |
drh | 777e4c4 | 2006-01-13 04:31:58 +0000 | [diff] [blame] | 3369 | ** |
| 3370 | ** The left-most leaf is the one with the smallest key - the first |
| 3371 | ** in ascending order. |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 3372 | */ |
| 3373 | static int moveToLeftmost(BtCursor *pCur){ |
| 3374 | Pgno pgno; |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 3375 | int rc = SQLITE_OK; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3376 | MemPage *pPage; |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 3377 | |
drh | d0679ed | 2007-08-28 22:24:34 +0000 | [diff] [blame^] | 3378 | assert( sqlite3BtreeMutexHeld(pCur->pBt->mutex) ); |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 3379 | assert( pCur->eState==CURSOR_VALID ); |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 3380 | while( rc==SQLITE_OK && !(pPage = pCur->pPage)->leaf ){ |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 3381 | assert( pCur->idx>=0 && pCur->idx<pPage->nCell ); |
danielk1977 | 1cc5ed8 | 2007-05-16 17:28:43 +0000 | [diff] [blame] | 3382 | pgno = get4byte(findCell(pPage, pCur->idx)); |
drh | 8178a75 | 2003-01-05 21:41:40 +0000 | [diff] [blame] | 3383 | rc = moveToChild(pCur, pgno); |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 3384 | } |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 3385 | return rc; |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 3386 | } |
| 3387 | |
drh | 2dcc9aa | 2002-12-04 13:40:25 +0000 | [diff] [blame] | 3388 | /* |
| 3389 | ** Move the cursor down to the right-most leaf entry beneath the |
| 3390 | ** page to which it is currently pointing. Notice the difference |
| 3391 | ** between moveToLeftmost() and moveToRightmost(). moveToLeftmost() |
| 3392 | ** finds the left-most entry beneath the *entry* whereas moveToRightmost() |
| 3393 | ** finds the right-most entry beneath the *page*. |
drh | 777e4c4 | 2006-01-13 04:31:58 +0000 | [diff] [blame] | 3394 | ** |
| 3395 | ** The right-most entry is the one with the largest key - the last |
| 3396 | ** key in ascending order. |
drh | 2dcc9aa | 2002-12-04 13:40:25 +0000 | [diff] [blame] | 3397 | */ |
| 3398 | static int moveToRightmost(BtCursor *pCur){ |
| 3399 | Pgno pgno; |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 3400 | int rc = SQLITE_OK; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3401 | MemPage *pPage; |
drh | 2dcc9aa | 2002-12-04 13:40:25 +0000 | [diff] [blame] | 3402 | |
drh | d0679ed | 2007-08-28 22:24:34 +0000 | [diff] [blame^] | 3403 | assert( sqlite3BtreeMutexHeld(pCur->pBt->mutex) ); |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 3404 | assert( pCur->eState==CURSOR_VALID ); |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 3405 | while( rc==SQLITE_OK && !(pPage = pCur->pPage)->leaf ){ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 3406 | pgno = get4byte(&pPage->aData[pPage->hdrOffset+8]); |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3407 | pCur->idx = pPage->nCell; |
drh | 8178a75 | 2003-01-05 21:41:40 +0000 | [diff] [blame] | 3408 | rc = moveToChild(pCur, pgno); |
drh | 2dcc9aa | 2002-12-04 13:40:25 +0000 | [diff] [blame] | 3409 | } |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 3410 | if( rc==SQLITE_OK ){ |
| 3411 | pCur->idx = pPage->nCell - 1; |
| 3412 | pCur->info.nSize = 0; |
| 3413 | } |
drh | 2dcc9aa | 2002-12-04 13:40:25 +0000 | [diff] [blame] | 3414 | return SQLITE_OK; |
| 3415 | } |
| 3416 | |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3417 | /* Move the cursor to the first entry in the table. Return SQLITE_OK |
| 3418 | ** on success. Set *pRes to 0 if the cursor actually points to something |
drh | 77c679c | 2002-02-19 22:43:58 +0000 | [diff] [blame] | 3419 | ** or set *pRes to 1 if the table is empty. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3420 | */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3421 | int sqlite3BtreeFirst(BtCursor *pCur, int *pRes){ |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3422 | int rc; |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 3423 | |
drh | d0679ed | 2007-08-28 22:24:34 +0000 | [diff] [blame^] | 3424 | assert( sqlite3BtreeMutexHeld(pCur->pBt->mutex) ); |
drh | 2764170 | 2007-08-22 02:56:42 +0000 | [diff] [blame] | 3425 | assert( sqlite3BtreeMutexHeld(pCur->pBtree->pSqlite->mutex) ); |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3426 | rc = moveToRoot(pCur); |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 3427 | if( rc==SQLITE_OK ){ |
| 3428 | if( pCur->eState==CURSOR_INVALID ){ |
| 3429 | assert( pCur->pPage->nCell==0 ); |
| 3430 | *pRes = 1; |
| 3431 | rc = SQLITE_OK; |
| 3432 | }else{ |
| 3433 | assert( pCur->pPage->nCell>0 ); |
| 3434 | *pRes = 0; |
| 3435 | rc = moveToLeftmost(pCur); |
| 3436 | } |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3437 | } |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3438 | return rc; |
| 3439 | } |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 3440 | |
drh | 9562b55 | 2002-02-19 15:00:07 +0000 | [diff] [blame] | 3441 | /* Move the cursor to the last entry in the table. Return SQLITE_OK |
| 3442 | ** on success. Set *pRes to 0 if the cursor actually points to something |
drh | 77c679c | 2002-02-19 22:43:58 +0000 | [diff] [blame] | 3443 | ** or set *pRes to 1 if the table is empty. |
drh | 9562b55 | 2002-02-19 15:00:07 +0000 | [diff] [blame] | 3444 | */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3445 | int sqlite3BtreeLast(BtCursor *pCur, int *pRes){ |
drh | 9562b55 | 2002-02-19 15:00:07 +0000 | [diff] [blame] | 3446 | int rc; |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 3447 | |
drh | d0679ed | 2007-08-28 22:24:34 +0000 | [diff] [blame^] | 3448 | assert( sqlite3BtreeMutexHeld(pCur->pBt->mutex) ); |
drh | 2764170 | 2007-08-22 02:56:42 +0000 | [diff] [blame] | 3449 | assert( sqlite3BtreeMutexHeld(pCur->pBtree->pSqlite->mutex) ); |
drh | 9562b55 | 2002-02-19 15:00:07 +0000 | [diff] [blame] | 3450 | rc = moveToRoot(pCur); |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 3451 | if( rc==SQLITE_OK ){ |
| 3452 | if( CURSOR_INVALID==pCur->eState ){ |
| 3453 | assert( pCur->pPage->nCell==0 ); |
| 3454 | *pRes = 1; |
| 3455 | }else{ |
| 3456 | assert( pCur->eState==CURSOR_VALID ); |
| 3457 | *pRes = 0; |
| 3458 | rc = moveToRightmost(pCur); |
| 3459 | } |
drh | 9562b55 | 2002-02-19 15:00:07 +0000 | [diff] [blame] | 3460 | } |
drh | 9562b55 | 2002-02-19 15:00:07 +0000 | [diff] [blame] | 3461 | return rc; |
| 3462 | } |
| 3463 | |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3464 | /* Move the cursor so that it points to an entry near pKey/nKey. |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 3465 | ** Return a success code. |
| 3466 | ** |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3467 | ** For INTKEY tables, only the nKey parameter is used. pKey is |
| 3468 | ** ignored. For other tables, nKey is the number of bytes of data |
drh | 0b2f316 | 2005-12-21 18:36:45 +0000 | [diff] [blame] | 3469 | ** in pKey. The comparison function specified when the cursor was |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3470 | ** created is used to compare keys. |
| 3471 | ** |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 3472 | ** If an exact match is not found, then the cursor is always |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 3473 | ** left pointing at a leaf page which would hold the entry if it |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 3474 | ** were present. The cursor might point to an entry that comes |
| 3475 | ** before or after the key. |
| 3476 | ** |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 3477 | ** The result of comparing the key with the entry to which the |
drh | ab01f61 | 2004-05-22 02:55:23 +0000 | [diff] [blame] | 3478 | ** cursor is written to *pRes if pRes!=NULL. The meaning of |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 3479 | ** this value is as follows: |
| 3480 | ** |
| 3481 | ** *pRes<0 The cursor is left pointing at an entry that |
drh | 1a844c3 | 2002-12-04 22:29:28 +0000 | [diff] [blame] | 3482 | ** is smaller than pKey or if the table is empty |
| 3483 | ** and the cursor is therefore left point to nothing. |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 3484 | ** |
| 3485 | ** *pRes==0 The cursor is left pointing at an entry that |
| 3486 | ** exactly matches pKey. |
| 3487 | ** |
| 3488 | ** *pRes>0 The cursor is left pointing at an entry that |
drh | 7c717f7 | 2001-06-24 20:39:41 +0000 | [diff] [blame] | 3489 | ** is larger than pKey. |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 3490 | ** |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 3491 | */ |
drh | e4d9081 | 2007-03-29 05:51:49 +0000 | [diff] [blame] | 3492 | int sqlite3BtreeMoveto( |
| 3493 | BtCursor *pCur, /* The cursor to be moved */ |
| 3494 | const void *pKey, /* The key content for indices. Not used by tables */ |
| 3495 | i64 nKey, /* Size of pKey. Or the key for tables */ |
| 3496 | int biasRight, /* If true, bias the search to the high end */ |
| 3497 | int *pRes /* Search result flag */ |
| 3498 | ){ |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 3499 | int rc; |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 3500 | |
drh | d0679ed | 2007-08-28 22:24:34 +0000 | [diff] [blame^] | 3501 | assert( sqlite3BtreeMutexHeld(pCur->pBt->mutex) ); |
drh | 2764170 | 2007-08-22 02:56:42 +0000 | [diff] [blame] | 3502 | assert( sqlite3BtreeMutexHeld(pCur->pBtree->pSqlite->mutex) ); |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 3503 | rc = moveToRoot(pCur); |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 3504 | if( rc ){ |
| 3505 | return rc; |
| 3506 | } |
drh | c39e000 | 2004-05-07 23:50:57 +0000 | [diff] [blame] | 3507 | assert( pCur->pPage ); |
| 3508 | assert( pCur->pPage->isInit ); |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 3509 | if( pCur->eState==CURSOR_INVALID ){ |
drh | f328bc8 | 2004-05-10 23:29:49 +0000 | [diff] [blame] | 3510 | *pRes = -1; |
drh | c39e000 | 2004-05-07 23:50:57 +0000 | [diff] [blame] | 3511 | assert( pCur->pPage->nCell==0 ); |
| 3512 | return SQLITE_OK; |
| 3513 | } |
drh | 1468438 | 2006-11-30 13:05:29 +0000 | [diff] [blame] | 3514 | for(;;){ |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 3515 | int lwr, upr; |
| 3516 | Pgno chldPg; |
| 3517 | MemPage *pPage = pCur->pPage; |
drh | 1a844c3 | 2002-12-04 22:29:28 +0000 | [diff] [blame] | 3518 | int c = -1; /* pRes return if table is empty must be -1 */ |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 3519 | lwr = 0; |
| 3520 | upr = pPage->nCell-1; |
drh | 4eec4c1 | 2005-01-21 00:22:37 +0000 | [diff] [blame] | 3521 | if( !pPage->intKey && pKey==0 ){ |
drh | 4928570 | 2005-09-17 15:20:26 +0000 | [diff] [blame] | 3522 | return SQLITE_CORRUPT_BKPT; |
drh | 4eec4c1 | 2005-01-21 00:22:37 +0000 | [diff] [blame] | 3523 | } |
drh | e4d9081 | 2007-03-29 05:51:49 +0000 | [diff] [blame] | 3524 | if( biasRight ){ |
| 3525 | pCur->idx = upr; |
| 3526 | }else{ |
| 3527 | pCur->idx = (upr+lwr)/2; |
| 3528 | } |
drh | f1d68b3 | 2007-03-29 04:43:26 +0000 | [diff] [blame] | 3529 | if( lwr<=upr ) for(;;){ |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 3530 | void *pCellKey; |
drh | 4a1c380 | 2004-05-12 15:15:47 +0000 | [diff] [blame] | 3531 | i64 nCellKey; |
drh | 366fda6 | 2006-01-13 02:35:09 +0000 | [diff] [blame] | 3532 | pCur->info.nSize = 0; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3533 | if( pPage->intKey ){ |
drh | 777e4c4 | 2006-01-13 04:31:58 +0000 | [diff] [blame] | 3534 | u8 *pCell; |
danielk1977 | 1cc5ed8 | 2007-05-16 17:28:43 +0000 | [diff] [blame] | 3535 | pCell = findCell(pPage, pCur->idx) + pPage->childPtrSize; |
drh | d172f86 | 2006-01-12 15:01:15 +0000 | [diff] [blame] | 3536 | if( pPage->hasData ){ |
danielk1977 | bab45c6 | 2006-01-16 15:14:27 +0000 | [diff] [blame] | 3537 | u32 dummy; |
drh | d172f86 | 2006-01-12 15:01:15 +0000 | [diff] [blame] | 3538 | pCell += getVarint32(pCell, &dummy); |
| 3539 | } |
danielk1977 | bab45c6 | 2006-01-16 15:14:27 +0000 | [diff] [blame] | 3540 | getVarint(pCell, (u64 *)&nCellKey); |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3541 | if( nCellKey<nKey ){ |
| 3542 | c = -1; |
| 3543 | }else if( nCellKey>nKey ){ |
| 3544 | c = +1; |
| 3545 | }else{ |
| 3546 | c = 0; |
| 3547 | } |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3548 | }else{ |
drh | e51c44f | 2004-05-30 20:46:09 +0000 | [diff] [blame] | 3549 | int available; |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 3550 | pCellKey = (void *)fetchPayload(pCur, &available, 0); |
drh | 366fda6 | 2006-01-13 02:35:09 +0000 | [diff] [blame] | 3551 | nCellKey = pCur->info.nKey; |
drh | e51c44f | 2004-05-30 20:46:09 +0000 | [diff] [blame] | 3552 | if( available>=nCellKey ){ |
| 3553 | c = pCur->xCompare(pCur->pArg, nCellKey, pCellKey, nKey, pKey); |
| 3554 | }else{ |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 3555 | pCellKey = sqlite3_malloc( nCellKey ); |
drh | e51c44f | 2004-05-30 20:46:09 +0000 | [diff] [blame] | 3556 | if( pCellKey==0 ) return SQLITE_NOMEM; |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 3557 | rc = sqlite3BtreeKey(pCur, 0, nCellKey, (void *)pCellKey); |
drh | e51c44f | 2004-05-30 20:46:09 +0000 | [diff] [blame] | 3558 | c = pCur->xCompare(pCur->pArg, nCellKey, pCellKey, nKey, pKey); |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 3559 | sqlite3_free(pCellKey); |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 3560 | if( rc ){ |
| 3561 | return rc; |
| 3562 | } |
drh | e51c44f | 2004-05-30 20:46:09 +0000 | [diff] [blame] | 3563 | } |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3564 | } |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 3565 | if( c==0 ){ |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 3566 | if( pPage->leafData && !pPage->leaf ){ |
drh | fc70e6f | 2004-05-12 21:11:27 +0000 | [diff] [blame] | 3567 | lwr = pCur->idx; |
| 3568 | upr = lwr - 1; |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 3569 | break; |
| 3570 | }else{ |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 3571 | if( pRes ) *pRes = 0; |
| 3572 | return SQLITE_OK; |
| 3573 | } |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 3574 | } |
| 3575 | if( c<0 ){ |
| 3576 | lwr = pCur->idx+1; |
| 3577 | }else{ |
| 3578 | upr = pCur->idx-1; |
| 3579 | } |
drh | f1d68b3 | 2007-03-29 04:43:26 +0000 | [diff] [blame] | 3580 | if( lwr>upr ){ |
| 3581 | break; |
| 3582 | } |
| 3583 | pCur->idx = (lwr+upr)/2; |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 3584 | } |
| 3585 | assert( lwr==upr+1 ); |
drh | 7aa128d | 2002-06-21 13:09:16 +0000 | [diff] [blame] | 3586 | assert( pPage->isInit ); |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3587 | if( pPage->leaf ){ |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 3588 | chldPg = 0; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3589 | }else if( lwr>=pPage->nCell ){ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 3590 | chldPg = get4byte(&pPage->aData[pPage->hdrOffset+8]); |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 3591 | }else{ |
danielk1977 | 1cc5ed8 | 2007-05-16 17:28:43 +0000 | [diff] [blame] | 3592 | chldPg = get4byte(findCell(pPage, lwr)); |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 3593 | } |
| 3594 | if( chldPg==0 ){ |
drh | c39e000 | 2004-05-07 23:50:57 +0000 | [diff] [blame] | 3595 | assert( pCur->idx>=0 && pCur->idx<pCur->pPage->nCell ); |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 3596 | if( pRes ) *pRes = c; |
| 3597 | return SQLITE_OK; |
| 3598 | } |
drh | 428ae8c | 2003-01-04 16:48:09 +0000 | [diff] [blame] | 3599 | pCur->idx = lwr; |
drh | 271efa5 | 2004-05-30 19:19:05 +0000 | [diff] [blame] | 3600 | pCur->info.nSize = 0; |
drh | 8178a75 | 2003-01-05 21:41:40 +0000 | [diff] [blame] | 3601 | rc = moveToChild(pCur, chldPg); |
drh | c39e000 | 2004-05-07 23:50:57 +0000 | [diff] [blame] | 3602 | if( rc ){ |
| 3603 | return rc; |
| 3604 | } |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 3605 | } |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 3606 | /* NOT REACHED */ |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 3607 | } |
| 3608 | |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 3609 | |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 3610 | /* |
drh | c39e000 | 2004-05-07 23:50:57 +0000 | [diff] [blame] | 3611 | ** Return TRUE if the cursor is not pointing at an entry of the table. |
| 3612 | ** |
| 3613 | ** TRUE will be returned after a call to sqlite3BtreeNext() moves |
| 3614 | ** past the last entry in the table or sqlite3BtreePrev() moves past |
| 3615 | ** the first entry. TRUE is also returned if the table is empty. |
| 3616 | */ |
| 3617 | int sqlite3BtreeEof(BtCursor *pCur){ |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 3618 | /* TODO: What if the cursor is in CURSOR_REQUIRESEEK but all table entries |
| 3619 | ** have been deleted? This API will need to change to return an error code |
| 3620 | ** as well as the boolean result value. |
| 3621 | */ |
| 3622 | return (CURSOR_VALID!=pCur->eState); |
drh | c39e000 | 2004-05-07 23:50:57 +0000 | [diff] [blame] | 3623 | } |
| 3624 | |
| 3625 | /* |
drh | b21c8cd | 2007-08-21 19:33:56 +0000 | [diff] [blame] | 3626 | ** Return the database connection handle for a cursor. |
| 3627 | */ |
| 3628 | sqlite3 *sqlite3BtreeCursorDb(const BtCursor *pCur){ |
drh | d0679ed | 2007-08-28 22:24:34 +0000 | [diff] [blame^] | 3629 | assert( sqlite3_mutex_held(pCur->pBtree->pSqlite->mutex) ); |
drh | b21c8cd | 2007-08-21 19:33:56 +0000 | [diff] [blame] | 3630 | return pCur->pBtree->pSqlite; |
| 3631 | } |
| 3632 | |
| 3633 | /* |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 3634 | ** Advance the cursor to the next entry in the database. If |
drh | 8c1238a | 2003-01-02 14:43:55 +0000 | [diff] [blame] | 3635 | ** successful then set *pRes=0. If the cursor |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 3636 | ** was already pointing to the last entry in the database before |
drh | 8c1238a | 2003-01-02 14:43:55 +0000 | [diff] [blame] | 3637 | ** this routine was called, then set *pRes=1. |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 3638 | */ |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 3639 | static int btreeNext(BtCursor *pCur, int *pRes){ |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 3640 | int rc; |
danielk1977 | 97a227c | 2006-01-20 16:32:04 +0000 | [diff] [blame] | 3641 | MemPage *pPage; |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 3642 | |
drh | d0679ed | 2007-08-28 22:24:34 +0000 | [diff] [blame^] | 3643 | assert( sqlite3_mutex_held(pCur->pBt->mutex) ); |
drh | bf700f3 | 2007-03-31 02:36:44 +0000 | [diff] [blame] | 3644 | rc = restoreOrClearCursorPosition(pCur); |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 3645 | if( rc!=SQLITE_OK ){ |
| 3646 | return rc; |
| 3647 | } |
drh | 8c4d3a6 | 2007-04-06 01:03:32 +0000 | [diff] [blame] | 3648 | assert( pRes!=0 ); |
| 3649 | pPage = pCur->pPage; |
| 3650 | if( CURSOR_INVALID==pCur->eState ){ |
| 3651 | *pRes = 1; |
| 3652 | return SQLITE_OK; |
| 3653 | } |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 3654 | if( pCur->skip>0 ){ |
| 3655 | pCur->skip = 0; |
| 3656 | *pRes = 0; |
| 3657 | return SQLITE_OK; |
| 3658 | } |
| 3659 | pCur->skip = 0; |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 3660 | |
drh | 8178a75 | 2003-01-05 21:41:40 +0000 | [diff] [blame] | 3661 | assert( pPage->isInit ); |
drh | 8178a75 | 2003-01-05 21:41:40 +0000 | [diff] [blame] | 3662 | assert( pCur->idx<pPage->nCell ); |
danielk1977 | 6a43f9b | 2004-11-16 04:57:24 +0000 | [diff] [blame] | 3663 | |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 3664 | pCur->idx++; |
drh | 271efa5 | 2004-05-30 19:19:05 +0000 | [diff] [blame] | 3665 | pCur->info.nSize = 0; |
drh | 8178a75 | 2003-01-05 21:41:40 +0000 | [diff] [blame] | 3666 | if( pCur->idx>=pPage->nCell ){ |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 3667 | if( !pPage->leaf ){ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 3668 | rc = moveToChild(pCur, get4byte(&pPage->aData[pPage->hdrOffset+8])); |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 3669 | if( rc ) return rc; |
| 3670 | rc = moveToLeftmost(pCur); |
drh | 8c1238a | 2003-01-02 14:43:55 +0000 | [diff] [blame] | 3671 | *pRes = 0; |
| 3672 | return rc; |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 3673 | } |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 3674 | do{ |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 3675 | if( sqlite3BtreeIsRootPage(pPage) ){ |
drh | 8c1238a | 2003-01-02 14:43:55 +0000 | [diff] [blame] | 3676 | *pRes = 1; |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 3677 | pCur->eState = CURSOR_INVALID; |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 3678 | return SQLITE_OK; |
| 3679 | } |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 3680 | sqlite3BtreeMoveToParent(pCur); |
drh | 8178a75 | 2003-01-05 21:41:40 +0000 | [diff] [blame] | 3681 | pPage = pCur->pPage; |
| 3682 | }while( pCur->idx>=pPage->nCell ); |
drh | 8c1238a | 2003-01-02 14:43:55 +0000 | [diff] [blame] | 3683 | *pRes = 0; |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 3684 | if( pPage->leafData ){ |
| 3685 | rc = sqlite3BtreeNext(pCur, pRes); |
| 3686 | }else{ |
| 3687 | rc = SQLITE_OK; |
| 3688 | } |
| 3689 | return rc; |
drh | 8178a75 | 2003-01-05 21:41:40 +0000 | [diff] [blame] | 3690 | } |
| 3691 | *pRes = 0; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3692 | if( pPage->leaf ){ |
drh | 8178a75 | 2003-01-05 21:41:40 +0000 | [diff] [blame] | 3693 | return SQLITE_OK; |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 3694 | } |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 3695 | rc = moveToLeftmost(pCur); |
drh | 8c1238a | 2003-01-02 14:43:55 +0000 | [diff] [blame] | 3696 | return rc; |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 3697 | } |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 3698 | int sqlite3BtreeNext(BtCursor *pCur, int *pRes){ |
| 3699 | int rc; |
| 3700 | cursorEnter(pCur); |
| 3701 | rc = btreeNext(pCur, pRes); |
| 3702 | cursorLeave(pCur); |
| 3703 | return rc; |
| 3704 | } |
| 3705 | |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 3706 | |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 3707 | /* |
drh | 2dcc9aa | 2002-12-04 13:40:25 +0000 | [diff] [blame] | 3708 | ** 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] | 3709 | ** successful then set *pRes=0. If the cursor |
drh | 2dcc9aa | 2002-12-04 13:40:25 +0000 | [diff] [blame] | 3710 | ** was already pointing to the first entry in the database before |
drh | 8178a75 | 2003-01-05 21:41:40 +0000 | [diff] [blame] | 3711 | ** this routine was called, then set *pRes=1. |
drh | 2dcc9aa | 2002-12-04 13:40:25 +0000 | [diff] [blame] | 3712 | */ |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 3713 | static int btreePrevious(BtCursor *pCur, int *pRes){ |
drh | 2dcc9aa | 2002-12-04 13:40:25 +0000 | [diff] [blame] | 3714 | int rc; |
| 3715 | Pgno pgno; |
drh | 8178a75 | 2003-01-05 21:41:40 +0000 | [diff] [blame] | 3716 | MemPage *pPage; |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 3717 | |
drh | d0679ed | 2007-08-28 22:24:34 +0000 | [diff] [blame^] | 3718 | assert( sqlite3_mutex_held(pCur->pBt->mutex) ); |
drh | bf700f3 | 2007-03-31 02:36:44 +0000 | [diff] [blame] | 3719 | rc = restoreOrClearCursorPosition(pCur); |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 3720 | if( rc!=SQLITE_OK ){ |
| 3721 | return rc; |
| 3722 | } |
drh | 8c4d3a6 | 2007-04-06 01:03:32 +0000 | [diff] [blame] | 3723 | if( CURSOR_INVALID==pCur->eState ){ |
| 3724 | *pRes = 1; |
| 3725 | return SQLITE_OK; |
| 3726 | } |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 3727 | if( pCur->skip<0 ){ |
| 3728 | pCur->skip = 0; |
| 3729 | *pRes = 0; |
| 3730 | return SQLITE_OK; |
| 3731 | } |
| 3732 | pCur->skip = 0; |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 3733 | |
drh | 8178a75 | 2003-01-05 21:41:40 +0000 | [diff] [blame] | 3734 | pPage = pCur->pPage; |
drh | 8178a75 | 2003-01-05 21:41:40 +0000 | [diff] [blame] | 3735 | assert( pPage->isInit ); |
drh | 2dcc9aa | 2002-12-04 13:40:25 +0000 | [diff] [blame] | 3736 | assert( pCur->idx>=0 ); |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 3737 | if( !pPage->leaf ){ |
danielk1977 | 1cc5ed8 | 2007-05-16 17:28:43 +0000 | [diff] [blame] | 3738 | pgno = get4byte( findCell(pPage, pCur->idx) ); |
drh | 8178a75 | 2003-01-05 21:41:40 +0000 | [diff] [blame] | 3739 | rc = moveToChild(pCur, pgno); |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 3740 | if( rc ){ |
| 3741 | return rc; |
| 3742 | } |
drh | 2dcc9aa | 2002-12-04 13:40:25 +0000 | [diff] [blame] | 3743 | rc = moveToRightmost(pCur); |
| 3744 | }else{ |
| 3745 | while( pCur->idx==0 ){ |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 3746 | if( sqlite3BtreeIsRootPage(pPage) ){ |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 3747 | pCur->eState = CURSOR_INVALID; |
drh | c39e000 | 2004-05-07 23:50:57 +0000 | [diff] [blame] | 3748 | *pRes = 1; |
drh | 2dcc9aa | 2002-12-04 13:40:25 +0000 | [diff] [blame] | 3749 | return SQLITE_OK; |
| 3750 | } |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 3751 | sqlite3BtreeMoveToParent(pCur); |
drh | 8178a75 | 2003-01-05 21:41:40 +0000 | [diff] [blame] | 3752 | pPage = pCur->pPage; |
drh | 2dcc9aa | 2002-12-04 13:40:25 +0000 | [diff] [blame] | 3753 | } |
| 3754 | pCur->idx--; |
drh | 271efa5 | 2004-05-30 19:19:05 +0000 | [diff] [blame] | 3755 | pCur->info.nSize = 0; |
drh | 8237d45 | 2004-11-22 19:07:09 +0000 | [diff] [blame] | 3756 | if( pPage->leafData && !pPage->leaf ){ |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 3757 | rc = sqlite3BtreePrevious(pCur, pRes); |
| 3758 | }else{ |
| 3759 | rc = SQLITE_OK; |
| 3760 | } |
drh | 2dcc9aa | 2002-12-04 13:40:25 +0000 | [diff] [blame] | 3761 | } |
drh | 8178a75 | 2003-01-05 21:41:40 +0000 | [diff] [blame] | 3762 | *pRes = 0; |
drh | 2dcc9aa | 2002-12-04 13:40:25 +0000 | [diff] [blame] | 3763 | return rc; |
| 3764 | } |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 3765 | int sqlite3BtreePrevious(BtCursor *pCur, int *pRes){ |
| 3766 | int rc; |
| 3767 | cursorEnter(pCur); |
| 3768 | rc = btreePrevious(pCur, pRes); |
| 3769 | cursorLeave(pCur); |
| 3770 | return rc; |
| 3771 | } |
drh | 2dcc9aa | 2002-12-04 13:40:25 +0000 | [diff] [blame] | 3772 | |
| 3773 | /* |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 3774 | ** Allocate a new page from the database file. |
| 3775 | ** |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 3776 | ** The new page is marked as dirty. (In other words, sqlite3PagerWrite() |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 3777 | ** has already been called on the new page.) The new page has also |
| 3778 | ** been referenced and the calling routine is responsible for calling |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 3779 | ** sqlite3PagerUnref() on the new page when it is done. |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 3780 | ** |
| 3781 | ** SQLITE_OK is returned on success. Any other return value indicates |
| 3782 | ** an error. *ppPage and *pPgno are undefined in the event of an error. |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 3783 | ** Do not invoke sqlite3PagerUnref() on *ppPage if an error is returned. |
drh | bea00b9 | 2002-07-08 10:59:50 +0000 | [diff] [blame] | 3784 | ** |
drh | 199e3cf | 2002-07-18 11:01:47 +0000 | [diff] [blame] | 3785 | ** If the "nearby" parameter is not 0, then a (feeble) effort is made to |
| 3786 | ** 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] | 3787 | ** attempt to keep related pages close to each other in the database file, |
| 3788 | ** which in turn can make database access faster. |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 3789 | ** |
| 3790 | ** If the "exact" parameter is not 0, and the page-number nearby exists |
| 3791 | ** anywhere on the free-list, then it is guarenteed to be returned. This |
| 3792 | ** is only used by auto-vacuum databases when allocating a new table. |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 3793 | */ |
drh | 4f0c587 | 2007-03-26 22:05:01 +0000 | [diff] [blame] | 3794 | static int allocateBtreePage( |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 3795 | BtShared *pBt, |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 3796 | MemPage **ppPage, |
| 3797 | Pgno *pPgno, |
| 3798 | Pgno nearby, |
| 3799 | u8 exact |
| 3800 | ){ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3801 | MemPage *pPage1; |
drh | 8c42ca9 | 2001-06-22 19:15:00 +0000 | [diff] [blame] | 3802 | int rc; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3803 | int n; /* Number of pages on the freelist */ |
| 3804 | int k; /* Number of leaves on the trunk of the freelist */ |
drh | d3627af | 2006-12-18 18:34:51 +0000 | [diff] [blame] | 3805 | MemPage *pTrunk = 0; |
| 3806 | MemPage *pPrevTrunk = 0; |
drh | 30e5875 | 2002-03-02 20:41:57 +0000 | [diff] [blame] | 3807 | |
drh | 2764170 | 2007-08-22 02:56:42 +0000 | [diff] [blame] | 3808 | assert( sqlite3BtreeMutexHeld(pBt->mutex) ); |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3809 | pPage1 = pBt->pPage1; |
| 3810 | n = get4byte(&pPage1->aData[36]); |
| 3811 | if( n>0 ){ |
drh | 9102529 | 2004-05-03 19:49:32 +0000 | [diff] [blame] | 3812 | /* There are pages on the freelist. Reuse one of those pages. */ |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 3813 | Pgno iTrunk; |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 3814 | u8 searchList = 0; /* If the free-list must be searched for 'nearby' */ |
| 3815 | |
| 3816 | /* If the 'exact' parameter was true and a query of the pointer-map |
| 3817 | ** shows that the page 'nearby' is somewhere on the free-list, then |
| 3818 | ** the entire-list will be searched for that page. |
| 3819 | */ |
| 3820 | #ifndef SQLITE_OMIT_AUTOVACUUM |
danielk1977 | 4ef2449 | 2007-05-23 09:52:41 +0000 | [diff] [blame] | 3821 | if( exact && nearby<=sqlite3PagerPagecount(pBt->pPager) ){ |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 3822 | u8 eType; |
| 3823 | assert( nearby>0 ); |
| 3824 | assert( pBt->autoVacuum ); |
| 3825 | rc = ptrmapGet(pBt, nearby, &eType, 0); |
| 3826 | if( rc ) return rc; |
| 3827 | if( eType==PTRMAP_FREEPAGE ){ |
| 3828 | searchList = 1; |
| 3829 | } |
| 3830 | *pPgno = nearby; |
| 3831 | } |
| 3832 | #endif |
| 3833 | |
| 3834 | /* Decrement the free-list count by 1. Set iTrunk to the index of the |
| 3835 | ** first free-list trunk page. iPrevTrunk is initially 1. |
| 3836 | */ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 3837 | rc = sqlite3PagerWrite(pPage1->pDbPage); |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 3838 | if( rc ) return rc; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3839 | put4byte(&pPage1->aData[36], n-1); |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 3840 | |
| 3841 | /* The code within this loop is run only once if the 'searchList' variable |
| 3842 | ** is not true. Otherwise, it runs once for each trunk-page on the |
| 3843 | ** free-list until the page 'nearby' is located. |
| 3844 | */ |
| 3845 | do { |
| 3846 | pPrevTrunk = pTrunk; |
| 3847 | if( pPrevTrunk ){ |
| 3848 | iTrunk = get4byte(&pPrevTrunk->aData[0]); |
drh | bea00b9 | 2002-07-08 10:59:50 +0000 | [diff] [blame] | 3849 | }else{ |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 3850 | iTrunk = get4byte(&pPage1->aData[32]); |
drh | bea00b9 | 2002-07-08 10:59:50 +0000 | [diff] [blame] | 3851 | } |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 3852 | rc = sqlite3BtreeGetPage(pBt, iTrunk, &pTrunk, 0); |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 3853 | if( rc ){ |
drh | d3627af | 2006-12-18 18:34:51 +0000 | [diff] [blame] | 3854 | pTrunk = 0; |
| 3855 | goto end_allocate_page; |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 3856 | } |
| 3857 | |
| 3858 | k = get4byte(&pTrunk->aData[4]); |
| 3859 | if( k==0 && !searchList ){ |
| 3860 | /* The trunk has no leaves and the list is not being searched. |
| 3861 | ** So extract the trunk page itself and use it as the newly |
| 3862 | ** allocated page */ |
| 3863 | assert( pPrevTrunk==0 ); |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 3864 | rc = sqlite3PagerWrite(pTrunk->pDbPage); |
drh | d3627af | 2006-12-18 18:34:51 +0000 | [diff] [blame] | 3865 | if( rc ){ |
| 3866 | goto end_allocate_page; |
| 3867 | } |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 3868 | *pPgno = iTrunk; |
| 3869 | memcpy(&pPage1->aData[32], &pTrunk->aData[0], 4); |
| 3870 | *ppPage = pTrunk; |
| 3871 | pTrunk = 0; |
| 3872 | TRACE(("ALLOCATE: %d trunk - %d free pages left\n", *pPgno, n-1)); |
| 3873 | }else if( k>pBt->usableSize/4 - 8 ){ |
| 3874 | /* Value of k is out of range. Database corruption */ |
drh | d3627af | 2006-12-18 18:34:51 +0000 | [diff] [blame] | 3875 | rc = SQLITE_CORRUPT_BKPT; |
| 3876 | goto end_allocate_page; |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 3877 | #ifndef SQLITE_OMIT_AUTOVACUUM |
| 3878 | }else if( searchList && nearby==iTrunk ){ |
| 3879 | /* The list is being searched and this trunk page is the page |
| 3880 | ** to allocate, regardless of whether it has leaves. |
| 3881 | */ |
| 3882 | assert( *pPgno==iTrunk ); |
| 3883 | *ppPage = pTrunk; |
| 3884 | searchList = 0; |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 3885 | rc = sqlite3PagerWrite(pTrunk->pDbPage); |
drh | d3627af | 2006-12-18 18:34:51 +0000 | [diff] [blame] | 3886 | if( rc ){ |
| 3887 | goto end_allocate_page; |
| 3888 | } |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 3889 | if( k==0 ){ |
| 3890 | if( !pPrevTrunk ){ |
| 3891 | memcpy(&pPage1->aData[32], &pTrunk->aData[0], 4); |
| 3892 | }else{ |
| 3893 | memcpy(&pPrevTrunk->aData[0], &pTrunk->aData[0], 4); |
| 3894 | } |
| 3895 | }else{ |
| 3896 | /* The trunk page is required by the caller but it contains |
| 3897 | ** pointers to free-list leaves. The first leaf becomes a trunk |
| 3898 | ** page in this case. |
| 3899 | */ |
| 3900 | MemPage *pNewTrunk; |
| 3901 | Pgno iNewTrunk = get4byte(&pTrunk->aData[8]); |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 3902 | rc = sqlite3BtreeGetPage(pBt, iNewTrunk, &pNewTrunk, 0); |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 3903 | if( rc!=SQLITE_OK ){ |
drh | d3627af | 2006-12-18 18:34:51 +0000 | [diff] [blame] | 3904 | goto end_allocate_page; |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 3905 | } |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 3906 | rc = sqlite3PagerWrite(pNewTrunk->pDbPage); |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 3907 | if( rc!=SQLITE_OK ){ |
| 3908 | releasePage(pNewTrunk); |
drh | d3627af | 2006-12-18 18:34:51 +0000 | [diff] [blame] | 3909 | goto end_allocate_page; |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 3910 | } |
| 3911 | memcpy(&pNewTrunk->aData[0], &pTrunk->aData[0], 4); |
| 3912 | put4byte(&pNewTrunk->aData[4], k-1); |
| 3913 | memcpy(&pNewTrunk->aData[8], &pTrunk->aData[12], (k-1)*4); |
drh | d3627af | 2006-12-18 18:34:51 +0000 | [diff] [blame] | 3914 | releasePage(pNewTrunk); |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 3915 | if( !pPrevTrunk ){ |
| 3916 | put4byte(&pPage1->aData[32], iNewTrunk); |
| 3917 | }else{ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 3918 | rc = sqlite3PagerWrite(pPrevTrunk->pDbPage); |
drh | d3627af | 2006-12-18 18:34:51 +0000 | [diff] [blame] | 3919 | if( rc ){ |
| 3920 | goto end_allocate_page; |
| 3921 | } |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 3922 | put4byte(&pPrevTrunk->aData[0], iNewTrunk); |
| 3923 | } |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 3924 | } |
| 3925 | pTrunk = 0; |
| 3926 | TRACE(("ALLOCATE: %d trunk - %d free pages left\n", *pPgno, n-1)); |
| 3927 | #endif |
| 3928 | }else{ |
| 3929 | /* Extract a leaf from the trunk */ |
| 3930 | int closest; |
| 3931 | Pgno iPage; |
| 3932 | unsigned char *aData = pTrunk->aData; |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 3933 | rc = sqlite3PagerWrite(pTrunk->pDbPage); |
drh | d3627af | 2006-12-18 18:34:51 +0000 | [diff] [blame] | 3934 | if( rc ){ |
| 3935 | goto end_allocate_page; |
| 3936 | } |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 3937 | if( nearby>0 ){ |
| 3938 | int i, dist; |
| 3939 | closest = 0; |
| 3940 | dist = get4byte(&aData[8]) - nearby; |
| 3941 | if( dist<0 ) dist = -dist; |
| 3942 | for(i=1; i<k; i++){ |
| 3943 | int d2 = get4byte(&aData[8+i*4]) - nearby; |
| 3944 | if( d2<0 ) d2 = -d2; |
| 3945 | if( d2<dist ){ |
| 3946 | closest = i; |
| 3947 | dist = d2; |
| 3948 | } |
| 3949 | } |
| 3950 | }else{ |
| 3951 | closest = 0; |
| 3952 | } |
| 3953 | |
| 3954 | iPage = get4byte(&aData[8+closest*4]); |
| 3955 | if( !searchList || iPage==nearby ){ |
| 3956 | *pPgno = iPage; |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 3957 | if( *pPgno>sqlite3PagerPagecount(pBt->pPager) ){ |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 3958 | /* Free page off the end of the file */ |
drh | 4928570 | 2005-09-17 15:20:26 +0000 | [diff] [blame] | 3959 | return SQLITE_CORRUPT_BKPT; |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 3960 | } |
| 3961 | TRACE(("ALLOCATE: %d was leaf %d of %d on trunk %d" |
| 3962 | ": %d more free pages\n", |
| 3963 | *pPgno, closest+1, k, pTrunk->pgno, n-1)); |
| 3964 | if( closest<k-1 ){ |
| 3965 | memcpy(&aData[8+closest*4], &aData[4+k*4], 4); |
| 3966 | } |
| 3967 | put4byte(&aData[4], k-1); |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 3968 | rc = sqlite3BtreeGetPage(pBt, *pPgno, ppPage, 1); |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 3969 | if( rc==SQLITE_OK ){ |
drh | 538f570 | 2007-04-13 02:14:30 +0000 | [diff] [blame] | 3970 | sqlite3PagerDontRollback((*ppPage)->pDbPage); |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 3971 | rc = sqlite3PagerWrite((*ppPage)->pDbPage); |
danielk1977 | aac0a38 | 2005-01-16 11:07:06 +0000 | [diff] [blame] | 3972 | if( rc!=SQLITE_OK ){ |
| 3973 | releasePage(*ppPage); |
| 3974 | } |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 3975 | } |
| 3976 | searchList = 0; |
| 3977 | } |
drh | ee696e2 | 2004-08-30 16:52:17 +0000 | [diff] [blame] | 3978 | } |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 3979 | releasePage(pPrevTrunk); |
drh | d3627af | 2006-12-18 18:34:51 +0000 | [diff] [blame] | 3980 | pPrevTrunk = 0; |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 3981 | }while( searchList ); |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 3982 | }else{ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3983 | /* There are no pages on the freelist, so create a new page at the |
| 3984 | ** end of the file */ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 3985 | *pPgno = sqlite3PagerPagecount(pBt->pPager) + 1; |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 3986 | |
| 3987 | #ifndef SQLITE_OMIT_AUTOVACUUM |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 3988 | if( pBt->nTrunc ){ |
| 3989 | /* An incr-vacuum has already run within this transaction. So the |
| 3990 | ** page to allocate is not from the physical end of the file, but |
| 3991 | ** at pBt->nTrunc. |
| 3992 | */ |
| 3993 | *pPgno = pBt->nTrunc+1; |
| 3994 | if( *pPgno==PENDING_BYTE_PAGE(pBt) ){ |
| 3995 | (*pPgno)++; |
| 3996 | } |
| 3997 | } |
danielk1977 | 266664d | 2006-02-10 08:24:21 +0000 | [diff] [blame] | 3998 | if( pBt->autoVacuum && PTRMAP_ISPAGE(pBt, *pPgno) ){ |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 3999 | /* If *pPgno refers to a pointer-map page, allocate two new pages |
| 4000 | ** at the end of the file instead of one. The first allocated page |
| 4001 | ** becomes a new pointer-map page, the second is used by the caller. |
| 4002 | */ |
| 4003 | TRACE(("ALLOCATE: %d from end of file (pointer-map page)\n", *pPgno)); |
danielk1977 | 599fcba | 2004-11-08 07:13:13 +0000 | [diff] [blame] | 4004 | assert( *pPgno!=PENDING_BYTE_PAGE(pBt) ); |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 4005 | (*pPgno)++; |
| 4006 | } |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 4007 | if( pBt->nTrunc ){ |
| 4008 | pBt->nTrunc = *pPgno; |
| 4009 | } |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 4010 | #endif |
| 4011 | |
danielk1977 | 599fcba | 2004-11-08 07:13:13 +0000 | [diff] [blame] | 4012 | assert( *pPgno!=PENDING_BYTE_PAGE(pBt) ); |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 4013 | rc = sqlite3BtreeGetPage(pBt, *pPgno, ppPage, 0); |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 4014 | if( rc ) return rc; |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 4015 | rc = sqlite3PagerWrite((*ppPage)->pDbPage); |
danielk1977 | aac0a38 | 2005-01-16 11:07:06 +0000 | [diff] [blame] | 4016 | if( rc!=SQLITE_OK ){ |
| 4017 | releasePage(*ppPage); |
| 4018 | } |
drh | 3a4c141 | 2004-05-09 20:40:11 +0000 | [diff] [blame] | 4019 | TRACE(("ALLOCATE: %d from end of file\n", *pPgno)); |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 4020 | } |
danielk1977 | 599fcba | 2004-11-08 07:13:13 +0000 | [diff] [blame] | 4021 | |
| 4022 | assert( *pPgno!=PENDING_BYTE_PAGE(pBt) ); |
drh | d3627af | 2006-12-18 18:34:51 +0000 | [diff] [blame] | 4023 | |
| 4024 | end_allocate_page: |
| 4025 | releasePage(pTrunk); |
| 4026 | releasePage(pPrevTrunk); |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 4027 | return rc; |
| 4028 | } |
| 4029 | |
| 4030 | /* |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 4031 | ** Add a page of the database file to the freelist. |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 4032 | ** |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 4033 | ** sqlite3PagerUnref() is NOT called for pPage. |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 4034 | */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 4035 | static int freePage(MemPage *pPage){ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 4036 | BtShared *pBt = pPage->pBt; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 4037 | MemPage *pPage1 = pBt->pPage1; |
| 4038 | int rc, n, k; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 4039 | |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 4040 | /* Prepare the page for freeing */ |
drh | 2764170 | 2007-08-22 02:56:42 +0000 | [diff] [blame] | 4041 | assert( sqlite3BtreeMutexHeld(pPage->pBt->mutex) ); |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 4042 | assert( pPage->pgno>1 ); |
| 4043 | pPage->isInit = 0; |
| 4044 | releasePage(pPage->pParent); |
| 4045 | pPage->pParent = 0; |
| 4046 | |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 4047 | /* Increment the free page count on pPage1 */ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 4048 | rc = sqlite3PagerWrite(pPage1->pDbPage); |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 4049 | if( rc ) return rc; |
| 4050 | n = get4byte(&pPage1->aData[36]); |
| 4051 | put4byte(&pPage1->aData[36], n+1); |
| 4052 | |
drh | fcce93f | 2006-02-22 03:08:32 +0000 | [diff] [blame] | 4053 | #ifdef SQLITE_SECURE_DELETE |
| 4054 | /* If the SQLITE_SECURE_DELETE compile-time option is enabled, then |
| 4055 | ** always fully overwrite deleted information with zeros. |
| 4056 | */ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 4057 | rc = sqlite3PagerWrite(pPage->pDbPage); |
drh | fcce93f | 2006-02-22 03:08:32 +0000 | [diff] [blame] | 4058 | if( rc ) return rc; |
| 4059 | memset(pPage->aData, 0, pPage->pBt->pageSize); |
| 4060 | #endif |
| 4061 | |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 4062 | #ifndef SQLITE_OMIT_AUTOVACUUM |
| 4063 | /* If the database supports auto-vacuum, write an entry in the pointer-map |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 4064 | ** to indicate that the page is free. |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 4065 | */ |
| 4066 | if( pBt->autoVacuum ){ |
| 4067 | rc = ptrmapPut(pBt, pPage->pgno, PTRMAP_FREEPAGE, 0); |
danielk1977 | a64a035 | 2004-11-05 01:45:13 +0000 | [diff] [blame] | 4068 | if( rc ) return rc; |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 4069 | } |
| 4070 | #endif |
| 4071 | |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 4072 | if( n==0 ){ |
| 4073 | /* This is the first free page */ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 4074 | rc = sqlite3PagerWrite(pPage->pDbPage); |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 4075 | if( rc ) return rc; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 4076 | memset(pPage->aData, 0, 8); |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 4077 | put4byte(&pPage1->aData[32], pPage->pgno); |
drh | 3a4c141 | 2004-05-09 20:40:11 +0000 | [diff] [blame] | 4078 | TRACE(("FREE-PAGE: %d first\n", pPage->pgno)); |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 4079 | }else{ |
| 4080 | /* Other free pages already exist. Retrive the first trunk page |
| 4081 | ** of the freelist and find out how many leaves it has. */ |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 4082 | MemPage *pTrunk; |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 4083 | rc = sqlite3BtreeGetPage(pBt, get4byte(&pPage1->aData[32]), &pTrunk, 0); |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 4084 | if( rc ) return rc; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 4085 | k = get4byte(&pTrunk->aData[4]); |
drh | ee696e2 | 2004-08-30 16:52:17 +0000 | [diff] [blame] | 4086 | if( k>=pBt->usableSize/4 - 8 ){ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 4087 | /* The trunk is full. Turn the page being freed into a new |
| 4088 | ** trunk page with no leaves. */ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 4089 | rc = sqlite3PagerWrite(pPage->pDbPage); |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 4090 | if( rc ) return rc; |
| 4091 | put4byte(pPage->aData, pTrunk->pgno); |
| 4092 | put4byte(&pPage->aData[4], 0); |
| 4093 | put4byte(&pPage1->aData[32], pPage->pgno); |
drh | 3a4c141 | 2004-05-09 20:40:11 +0000 | [diff] [blame] | 4094 | TRACE(("FREE-PAGE: %d new trunk page replacing %d\n", |
| 4095 | pPage->pgno, pTrunk->pgno)); |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 4096 | }else{ |
| 4097 | /* Add the newly freed page as a leaf on the current trunk */ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 4098 | rc = sqlite3PagerWrite(pTrunk->pDbPage); |
drh | f534544 | 2007-04-09 12:45:02 +0000 | [diff] [blame] | 4099 | if( rc==SQLITE_OK ){ |
| 4100 | put4byte(&pTrunk->aData[4], k+1); |
| 4101 | put4byte(&pTrunk->aData[8+k*4], pPage->pgno); |
drh | fcce93f | 2006-02-22 03:08:32 +0000 | [diff] [blame] | 4102 | #ifndef SQLITE_SECURE_DELETE |
drh | 538f570 | 2007-04-13 02:14:30 +0000 | [diff] [blame] | 4103 | sqlite3PagerDontWrite(pPage->pDbPage); |
drh | fcce93f | 2006-02-22 03:08:32 +0000 | [diff] [blame] | 4104 | #endif |
drh | f534544 | 2007-04-09 12:45:02 +0000 | [diff] [blame] | 4105 | } |
drh | 3a4c141 | 2004-05-09 20:40:11 +0000 | [diff] [blame] | 4106 | 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] | 4107 | } |
| 4108 | releasePage(pTrunk); |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 4109 | } |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 4110 | return rc; |
| 4111 | } |
| 4112 | |
| 4113 | /* |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 4114 | ** Free any overflow pages associated with the given Cell. |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 4115 | */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 4116 | static int clearCell(MemPage *pPage, unsigned char *pCell){ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 4117 | BtShared *pBt = pPage->pBt; |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 4118 | CellInfo info; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 4119 | Pgno ovflPgno; |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 4120 | int rc; |
drh | 9444081 | 2007-03-06 11:42:19 +0000 | [diff] [blame] | 4121 | int nOvfl; |
| 4122 | int ovflPageSize; |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 4123 | |
drh | 2764170 | 2007-08-22 02:56:42 +0000 | [diff] [blame] | 4124 | assert( sqlite3BtreeMutexHeld(pPage->pBt->mutex) ); |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 4125 | sqlite3BtreeParseCellPtr(pPage, pCell, &info); |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 4126 | if( info.iOverflow==0 ){ |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 4127 | return SQLITE_OK; /* No overflow pages. Return without doing anything */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 4128 | } |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 4129 | ovflPgno = get4byte(&pCell[info.iOverflow]); |
drh | 9444081 | 2007-03-06 11:42:19 +0000 | [diff] [blame] | 4130 | ovflPageSize = pBt->usableSize - 4; |
drh | 7236583 | 2007-03-06 15:53:44 +0000 | [diff] [blame] | 4131 | nOvfl = (info.nPayload - info.nLocal + ovflPageSize - 1)/ovflPageSize; |
| 4132 | assert( ovflPgno==0 || nOvfl>0 ); |
| 4133 | while( nOvfl-- ){ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 4134 | MemPage *pOvfl; |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 4135 | if( ovflPgno==0 || ovflPgno>sqlite3PagerPagecount(pBt->pPager) ){ |
drh | 4928570 | 2005-09-17 15:20:26 +0000 | [diff] [blame] | 4136 | return SQLITE_CORRUPT_BKPT; |
danielk1977 | a1cb183 | 2005-02-12 08:59:55 +0000 | [diff] [blame] | 4137 | } |
danielk1977 | 8c0a959 | 2007-04-30 16:55:00 +0000 | [diff] [blame] | 4138 | |
| 4139 | rc = getOverflowPage(pBt, ovflPgno, &pOvfl, (nOvfl==0)?0:&ovflPgno); |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 4140 | if( rc ) return rc; |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 4141 | rc = freePage(pOvfl); |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 4142 | sqlite3PagerUnref(pOvfl->pDbPage); |
danielk1977 | 6b456a2 | 2005-03-21 04:04:02 +0000 | [diff] [blame] | 4143 | if( rc ) return rc; |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 4144 | } |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 4145 | return SQLITE_OK; |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 4146 | } |
| 4147 | |
| 4148 | /* |
drh | 9102529 | 2004-05-03 19:49:32 +0000 | [diff] [blame] | 4149 | ** Create the byte sequence used to represent a cell on page pPage |
| 4150 | ** and write that byte sequence into pCell[]. Overflow pages are |
| 4151 | ** allocated and filled in as necessary. The calling procedure |
| 4152 | ** is responsible for making sure sufficient space has been allocated |
| 4153 | ** for pCell[]. |
| 4154 | ** |
| 4155 | ** Note that pCell does not necessary need to point to the pPage->aData |
| 4156 | ** area. pCell might point to some temporary storage. The cell will |
| 4157 | ** be constructed in this temporary area then copied into pPage->aData |
| 4158 | ** later. |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 4159 | */ |
| 4160 | static int fillInCell( |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 4161 | MemPage *pPage, /* The page that contains the cell */ |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 4162 | unsigned char *pCell, /* Complete text of the cell */ |
drh | 4a1c380 | 2004-05-12 15:15:47 +0000 | [diff] [blame] | 4163 | const void *pKey, i64 nKey, /* The key */ |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 4164 | const void *pData,int nData, /* The data */ |
drh | b026e05 | 2007-05-02 01:34:31 +0000 | [diff] [blame] | 4165 | int nZero, /* Extra zero bytes to append to pData */ |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 4166 | int *pnSize /* Write cell size here */ |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 4167 | ){ |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 4168 | int nPayload; |
drh | 8c6fa9b | 2004-05-26 00:01:53 +0000 | [diff] [blame] | 4169 | const u8 *pSrc; |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 4170 | int nSrc, n, rc; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 4171 | int spaceLeft; |
| 4172 | MemPage *pOvfl = 0; |
drh | 9b17127 | 2004-05-08 02:03:22 +0000 | [diff] [blame] | 4173 | MemPage *pToRelease = 0; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 4174 | unsigned char *pPrior; |
| 4175 | unsigned char *pPayload; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 4176 | BtShared *pBt = pPage->pBt; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 4177 | Pgno pgnoOvfl = 0; |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 4178 | int nHeader; |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 4179 | CellInfo info; |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 4180 | |
drh | 2764170 | 2007-08-22 02:56:42 +0000 | [diff] [blame] | 4181 | assert( sqlite3BtreeMutexHeld(pPage->pBt->mutex) ); |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 4182 | |
drh | 9102529 | 2004-05-03 19:49:32 +0000 | [diff] [blame] | 4183 | /* Fill in the header. */ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4184 | nHeader = 0; |
drh | 9102529 | 2004-05-03 19:49:32 +0000 | [diff] [blame] | 4185 | if( !pPage->leaf ){ |
| 4186 | nHeader += 4; |
| 4187 | } |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 4188 | if( pPage->hasData ){ |
drh | b026e05 | 2007-05-02 01:34:31 +0000 | [diff] [blame] | 4189 | nHeader += putVarint(&pCell[nHeader], nData+nZero); |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 4190 | }else{ |
drh | b026e05 | 2007-05-02 01:34:31 +0000 | [diff] [blame] | 4191 | nData = nZero = 0; |
drh | 9102529 | 2004-05-03 19:49:32 +0000 | [diff] [blame] | 4192 | } |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 4193 | nHeader += putVarint(&pCell[nHeader], *(u64*)&nKey); |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 4194 | sqlite3BtreeParseCellPtr(pPage, pCell, &info); |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 4195 | assert( info.nHeader==nHeader ); |
| 4196 | assert( info.nKey==nKey ); |
drh | b026e05 | 2007-05-02 01:34:31 +0000 | [diff] [blame] | 4197 | assert( info.nData==nData+nZero ); |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 4198 | |
| 4199 | /* Fill in the payload */ |
drh | b026e05 | 2007-05-02 01:34:31 +0000 | [diff] [blame] | 4200 | nPayload = nData + nZero; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 4201 | if( pPage->intKey ){ |
| 4202 | pSrc = pData; |
| 4203 | nSrc = nData; |
drh | 9102529 | 2004-05-03 19:49:32 +0000 | [diff] [blame] | 4204 | nData = 0; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 4205 | }else{ |
| 4206 | nPayload += nKey; |
| 4207 | pSrc = pKey; |
| 4208 | nSrc = nKey; |
| 4209 | } |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 4210 | *pnSize = info.nSize; |
| 4211 | spaceLeft = info.nLocal; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 4212 | pPayload = &pCell[nHeader]; |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 4213 | pPrior = &pCell[info.iOverflow]; |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 4214 | |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 4215 | while( nPayload>0 ){ |
| 4216 | if( spaceLeft==0 ){ |
danielk1977 | b39f70b | 2007-05-17 18:28:11 +0000 | [diff] [blame] | 4217 | int isExact = 0; |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 4218 | #ifndef SQLITE_OMIT_AUTOVACUUM |
| 4219 | Pgno pgnoPtrmap = pgnoOvfl; /* Overflow page pointer-map entry page */ |
danielk1977 | b39f70b | 2007-05-17 18:28:11 +0000 | [diff] [blame] | 4220 | if( pBt->autoVacuum ){ |
| 4221 | do{ |
| 4222 | pgnoOvfl++; |
| 4223 | } while( |
| 4224 | PTRMAP_ISPAGE(pBt, pgnoOvfl) || pgnoOvfl==PENDING_BYTE_PAGE(pBt) |
| 4225 | ); |
danielk1977 | 89a4be8 | 2007-05-23 13:34:32 +0000 | [diff] [blame] | 4226 | if( pgnoOvfl>1 ){ |
danielk1977 | b39f70b | 2007-05-17 18:28:11 +0000 | [diff] [blame] | 4227 | /* isExact = 1; */ |
| 4228 | } |
| 4229 | } |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 4230 | #endif |
danielk1977 | b39f70b | 2007-05-17 18:28:11 +0000 | [diff] [blame] | 4231 | rc = allocateBtreePage(pBt, &pOvfl, &pgnoOvfl, pgnoOvfl, isExact); |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 4232 | #ifndef SQLITE_OMIT_AUTOVACUUM |
danielk1977 | a19df67 | 2004-11-03 11:37:07 +0000 | [diff] [blame] | 4233 | /* If the database supports auto-vacuum, and the second or subsequent |
| 4234 | ** overflow page is being allocated, add an entry to the pointer-map |
danielk1977 | 4ef2449 | 2007-05-23 09:52:41 +0000 | [diff] [blame] | 4235 | ** for that page now. |
| 4236 | ** |
| 4237 | ** If this is the first overflow page, then write a partial entry |
| 4238 | ** to the pointer-map. If we write nothing to this pointer-map slot, |
| 4239 | ** then the optimistic overflow chain processing in clearCell() |
| 4240 | ** may misinterpret the uninitialised values and delete the |
| 4241 | ** wrong pages from the database. |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 4242 | */ |
danielk1977 | 4ef2449 | 2007-05-23 09:52:41 +0000 | [diff] [blame] | 4243 | if( pBt->autoVacuum && rc==SQLITE_OK ){ |
| 4244 | u8 eType = (pgnoPtrmap?PTRMAP_OVERFLOW2:PTRMAP_OVERFLOW1); |
| 4245 | rc = ptrmapPut(pBt, pgnoOvfl, eType, pgnoPtrmap); |
danielk1977 | 89a4be8 | 2007-05-23 13:34:32 +0000 | [diff] [blame] | 4246 | if( rc ){ |
| 4247 | releasePage(pOvfl); |
| 4248 | } |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 4249 | } |
| 4250 | #endif |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 4251 | if( rc ){ |
drh | 9b17127 | 2004-05-08 02:03:22 +0000 | [diff] [blame] | 4252 | releasePage(pToRelease); |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 4253 | return rc; |
| 4254 | } |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 4255 | put4byte(pPrior, pgnoOvfl); |
drh | 9b17127 | 2004-05-08 02:03:22 +0000 | [diff] [blame] | 4256 | releasePage(pToRelease); |
| 4257 | pToRelease = pOvfl; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 4258 | pPrior = pOvfl->aData; |
| 4259 | put4byte(pPrior, 0); |
| 4260 | pPayload = &pOvfl->aData[4]; |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 4261 | spaceLeft = pBt->usableSize - 4; |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 4262 | } |
| 4263 | n = nPayload; |
| 4264 | if( n>spaceLeft ) n = spaceLeft; |
drh | b026e05 | 2007-05-02 01:34:31 +0000 | [diff] [blame] | 4265 | if( nSrc>0 ){ |
| 4266 | if( n>nSrc ) n = nSrc; |
| 4267 | assert( pSrc ); |
| 4268 | memcpy(pPayload, pSrc, n); |
| 4269 | }else{ |
| 4270 | memset(pPayload, 0, n); |
| 4271 | } |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 4272 | nPayload -= n; |
drh | de64713 | 2004-05-07 17:57:49 +0000 | [diff] [blame] | 4273 | pPayload += n; |
drh | 9b17127 | 2004-05-08 02:03:22 +0000 | [diff] [blame] | 4274 | pSrc += n; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 4275 | nSrc -= n; |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 4276 | spaceLeft -= n; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 4277 | if( nSrc==0 ){ |
| 4278 | nSrc = nData; |
| 4279 | pSrc = pData; |
| 4280 | } |
drh | dd79342 | 2001-06-28 01:54:48 +0000 | [diff] [blame] | 4281 | } |
drh | 9b17127 | 2004-05-08 02:03:22 +0000 | [diff] [blame] | 4282 | releasePage(pToRelease); |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 4283 | return SQLITE_OK; |
| 4284 | } |
| 4285 | |
| 4286 | /* |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 4287 | ** Change the MemPage.pParent pointer on the page whose number is |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 4288 | ** given in the second argument so that MemPage.pParent holds the |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 4289 | ** pointer in the third argument. |
| 4290 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 4291 | static int reparentPage(BtShared *pBt, Pgno pgno, MemPage *pNewParent, int idx){ |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 4292 | MemPage *pThis; |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 4293 | DbPage *pDbPage; |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 4294 | |
drh | 2764170 | 2007-08-22 02:56:42 +0000 | [diff] [blame] | 4295 | assert( sqlite3BtreeMutexHeld(pBt->mutex) ); |
drh | 43617e9 | 2006-03-06 20:55:46 +0000 | [diff] [blame] | 4296 | assert( pNewParent!=0 ); |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 4297 | if( pgno==0 ) return SQLITE_OK; |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 4298 | assert( pBt->pPager!=0 ); |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 4299 | pDbPage = sqlite3PagerLookup(pBt->pPager, pgno); |
| 4300 | if( pDbPage ){ |
| 4301 | pThis = (MemPage *)sqlite3PagerGetExtra(pDbPage); |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 4302 | if( pThis->isInit ){ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 4303 | assert( pThis->aData==(sqlite3PagerGetData(pDbPage)) ); |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 4304 | if( pThis->pParent!=pNewParent ){ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 4305 | if( pThis->pParent ) sqlite3PagerUnref(pThis->pParent->pDbPage); |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 4306 | pThis->pParent = pNewParent; |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 4307 | sqlite3PagerRef(pNewParent->pDbPage); |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 4308 | } |
| 4309 | pThis->idxParent = idx; |
drh | dd79342 | 2001-06-28 01:54:48 +0000 | [diff] [blame] | 4310 | } |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 4311 | sqlite3PagerUnref(pDbPage); |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 4312 | } |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 4313 | |
| 4314 | #ifndef SQLITE_OMIT_AUTOVACUUM |
| 4315 | if( pBt->autoVacuum ){ |
| 4316 | return ptrmapPut(pBt, pgno, PTRMAP_BTREE, pNewParent->pgno); |
| 4317 | } |
| 4318 | #endif |
| 4319 | return SQLITE_OK; |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 4320 | } |
| 4321 | |
danielk1977 | ac11ee6 | 2005-01-15 12:45:51 +0000 | [diff] [blame] | 4322 | |
| 4323 | |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 4324 | /* |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 4325 | ** Change the pParent pointer of all children of pPage to point back |
| 4326 | ** to pPage. |
| 4327 | ** |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 4328 | ** In other words, for every child of pPage, invoke reparentPage() |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4329 | ** to make sure that each child knows that pPage is its parent. |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 4330 | ** |
| 4331 | ** This routine gets called after you memcpy() one page into |
| 4332 | ** another. |
| 4333 | */ |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 4334 | static int reparentChildPages(MemPage *pPage){ |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 4335 | int i; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 4336 | BtShared *pBt = pPage->pBt; |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 4337 | int rc = SQLITE_OK; |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 4338 | |
drh | 2764170 | 2007-08-22 02:56:42 +0000 | [diff] [blame] | 4339 | assert( sqlite3BtreeMutexHeld(pPage->pBt->mutex) ); |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 4340 | if( pPage->leaf ) return SQLITE_OK; |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 4341 | |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 4342 | for(i=0; i<pPage->nCell; i++){ |
danielk1977 | 1cc5ed8 | 2007-05-16 17:28:43 +0000 | [diff] [blame] | 4343 | u8 *pCell = findCell(pPage, i); |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 4344 | if( !pPage->leaf ){ |
| 4345 | rc = reparentPage(pBt, get4byte(pCell), pPage, i); |
| 4346 | if( rc!=SQLITE_OK ) return rc; |
| 4347 | } |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 4348 | } |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 4349 | if( !pPage->leaf ){ |
| 4350 | rc = reparentPage(pBt, get4byte(&pPage->aData[pPage->hdrOffset+8]), |
| 4351 | pPage, i); |
| 4352 | pPage->idxShift = 0; |
| 4353 | } |
| 4354 | return rc; |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 4355 | } |
| 4356 | |
| 4357 | /* |
| 4358 | ** Remove the i-th cell from pPage. This routine effects pPage only. |
| 4359 | ** The cell content is not freed or deallocated. It is assumed that |
| 4360 | ** the cell content has been copied someplace else. This routine just |
| 4361 | ** removes the reference to the cell from pPage. |
| 4362 | ** |
| 4363 | ** "sz" must be the number of bytes in the cell. |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 4364 | */ |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 4365 | static void dropCell(MemPage *pPage, int idx, int sz){ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4366 | int i; /* Loop counter */ |
| 4367 | int pc; /* Offset to cell content of cell being deleted */ |
| 4368 | u8 *data; /* pPage->aData */ |
| 4369 | u8 *ptr; /* Used to move bytes around within data[] */ |
| 4370 | |
drh | 8c42ca9 | 2001-06-22 19:15:00 +0000 | [diff] [blame] | 4371 | assert( idx>=0 && idx<pPage->nCell ); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4372 | assert( sz==cellSize(pPage, idx) ); |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 4373 | assert( sqlite3PagerIswriteable(pPage->pDbPage) ); |
drh | 2764170 | 2007-08-22 02:56:42 +0000 | [diff] [blame] | 4374 | assert( sqlite3BtreeMutexHeld(pPage->pBt->mutex) ); |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 4375 | data = pPage->aData; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4376 | ptr = &data[pPage->cellOffset + 2*idx]; |
| 4377 | pc = get2byte(ptr); |
| 4378 | assert( pc>10 && pc+sz<=pPage->pBt->usableSize ); |
drh | de64713 | 2004-05-07 17:57:49 +0000 | [diff] [blame] | 4379 | freeSpace(pPage, pc, sz); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4380 | for(i=idx+1; i<pPage->nCell; i++, ptr+=2){ |
| 4381 | ptr[0] = ptr[2]; |
| 4382 | ptr[1] = ptr[3]; |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 4383 | } |
| 4384 | pPage->nCell--; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4385 | put2byte(&data[pPage->hdrOffset+3], pPage->nCell); |
| 4386 | pPage->nFree += 2; |
drh | 428ae8c | 2003-01-04 16:48:09 +0000 | [diff] [blame] | 4387 | pPage->idxShift = 1; |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 4388 | } |
| 4389 | |
| 4390 | /* |
| 4391 | ** Insert a new cell on pPage at cell index "i". pCell points to the |
| 4392 | ** content of the cell. |
| 4393 | ** |
| 4394 | ** 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] | 4395 | ** will not fit, then make a copy of the cell content into pTemp if |
| 4396 | ** pTemp is not null. Regardless of pTemp, allocate a new entry |
| 4397 | ** in pPage->aOvfl[] and make it point to the cell content (either |
| 4398 | ** in pTemp or the original pCell) and also record its index. |
| 4399 | ** Allocating a new entry in pPage->aCell[] implies that |
| 4400 | ** pPage->nOverflow is incremented. |
danielk1977 | a3ad5e7 | 2005-01-07 08:56:44 +0000 | [diff] [blame] | 4401 | ** |
| 4402 | ** If nSkip is non-zero, then do not copy the first nSkip bytes of the |
| 4403 | ** cell. The caller will overwrite them after this function returns. If |
drh | 4b238df | 2005-01-08 15:43:18 +0000 | [diff] [blame] | 4404 | ** 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] | 4405 | ** (but pCell+nSkip is always valid). |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 4406 | */ |
danielk1977 | e80463b | 2004-11-03 03:01:16 +0000 | [diff] [blame] | 4407 | static int insertCell( |
drh | 24cd67e | 2004-05-10 16:18:47 +0000 | [diff] [blame] | 4408 | MemPage *pPage, /* Page into which we are copying */ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4409 | int i, /* New cell becomes the i-th cell of the page */ |
| 4410 | u8 *pCell, /* Content of the new cell */ |
| 4411 | int sz, /* Bytes of content in pCell */ |
danielk1977 | a3ad5e7 | 2005-01-07 08:56:44 +0000 | [diff] [blame] | 4412 | u8 *pTemp, /* Temp storage space for pCell, if needed */ |
| 4413 | u8 nSkip /* Do not write the first nSkip bytes of the cell */ |
drh | 24cd67e | 2004-05-10 16:18:47 +0000 | [diff] [blame] | 4414 | ){ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4415 | int idx; /* Where to write new cell content in data[] */ |
| 4416 | int j; /* Loop counter */ |
| 4417 | int top; /* First byte of content for any cell in data[] */ |
| 4418 | int end; /* First byte past the last cell pointer in data[] */ |
| 4419 | int ins; /* Index in data[] where new cell pointer is inserted */ |
| 4420 | int hdr; /* Offset into data[] of the page header */ |
| 4421 | int cellOffset; /* Address of first cell pointer in data[] */ |
| 4422 | u8 *data; /* The content of the whole page */ |
| 4423 | u8 *ptr; /* Used for moving information around in data[] */ |
| 4424 | |
| 4425 | assert( i>=0 && i<=pPage->nCell+pPage->nOverflow ); |
| 4426 | assert( sz==cellSizePtr(pPage, pCell) ); |
drh | 2764170 | 2007-08-22 02:56:42 +0000 | [diff] [blame] | 4427 | assert( sqlite3BtreeMutexHeld(pPage->pBt->mutex) ); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4428 | if( pPage->nOverflow || sz+2>pPage->nFree ){ |
drh | 24cd67e | 2004-05-10 16:18:47 +0000 | [diff] [blame] | 4429 | if( pTemp ){ |
danielk1977 | a3ad5e7 | 2005-01-07 08:56:44 +0000 | [diff] [blame] | 4430 | memcpy(pTemp+nSkip, pCell+nSkip, sz-nSkip); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4431 | pCell = pTemp; |
drh | 24cd67e | 2004-05-10 16:18:47 +0000 | [diff] [blame] | 4432 | } |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4433 | j = pPage->nOverflow++; |
| 4434 | assert( j<sizeof(pPage->aOvfl)/sizeof(pPage->aOvfl[0]) ); |
| 4435 | pPage->aOvfl[j].pCell = pCell; |
| 4436 | pPage->aOvfl[j].idx = i; |
| 4437 | pPage->nFree = 0; |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 4438 | }else{ |
danielk1977 | 6e465eb | 2007-08-21 13:11:00 +0000 | [diff] [blame] | 4439 | int rc = sqlite3PagerWrite(pPage->pDbPage); |
| 4440 | if( rc!=SQLITE_OK ){ |
| 4441 | return rc; |
| 4442 | } |
| 4443 | assert( sqlite3PagerIswriteable(pPage->pDbPage) ); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4444 | data = pPage->aData; |
| 4445 | hdr = pPage->hdrOffset; |
| 4446 | top = get2byte(&data[hdr+5]); |
| 4447 | cellOffset = pPage->cellOffset; |
| 4448 | end = cellOffset + 2*pPage->nCell + 2; |
| 4449 | ins = cellOffset + 2*i; |
| 4450 | if( end > top - sz ){ |
danielk1977 | 6e465eb | 2007-08-21 13:11:00 +0000 | [diff] [blame] | 4451 | rc = defragmentPage(pPage); |
danielk1977 | 6b456a2 | 2005-03-21 04:04:02 +0000 | [diff] [blame] | 4452 | if( rc!=SQLITE_OK ) return rc; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4453 | top = get2byte(&data[hdr+5]); |
| 4454 | assert( end + sz <= top ); |
| 4455 | } |
| 4456 | idx = allocateSpace(pPage, sz); |
| 4457 | assert( idx>0 ); |
| 4458 | assert( end <= get2byte(&data[hdr+5]) ); |
| 4459 | pPage->nCell++; |
| 4460 | pPage->nFree -= 2; |
danielk1977 | a3ad5e7 | 2005-01-07 08:56:44 +0000 | [diff] [blame] | 4461 | memcpy(&data[idx+nSkip], pCell+nSkip, sz-nSkip); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4462 | for(j=end-2, ptr=&data[j]; j>ins; j-=2, ptr-=2){ |
| 4463 | ptr[0] = ptr[-2]; |
| 4464 | ptr[1] = ptr[-1]; |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 4465 | } |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4466 | put2byte(&data[ins], idx); |
| 4467 | put2byte(&data[hdr+3], pPage->nCell); |
| 4468 | pPage->idxShift = 1; |
danielk1977 | a19df67 | 2004-11-03 11:37:07 +0000 | [diff] [blame] | 4469 | #ifndef SQLITE_OMIT_AUTOVACUUM |
| 4470 | if( pPage->pBt->autoVacuum ){ |
| 4471 | /* The cell may contain a pointer to an overflow page. If so, write |
| 4472 | ** the entry for the overflow page into the pointer map. |
| 4473 | */ |
| 4474 | CellInfo info; |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 4475 | sqlite3BtreeParseCellPtr(pPage, pCell, &info); |
drh | 7236583 | 2007-03-06 15:53:44 +0000 | [diff] [blame] | 4476 | assert( (info.nData+(pPage->intKey?0:info.nKey))==info.nPayload ); |
danielk1977 | a19df67 | 2004-11-03 11:37:07 +0000 | [diff] [blame] | 4477 | if( (info.nData+(pPage->intKey?0:info.nKey))>info.nLocal ){ |
| 4478 | Pgno pgnoOvfl = get4byte(&pCell[info.iOverflow]); |
danielk1977 | 6e465eb | 2007-08-21 13:11:00 +0000 | [diff] [blame] | 4479 | rc = ptrmapPut(pPage->pBt, pgnoOvfl, PTRMAP_OVERFLOW1, pPage->pgno); |
danielk1977 | a19df67 | 2004-11-03 11:37:07 +0000 | [diff] [blame] | 4480 | if( rc!=SQLITE_OK ) return rc; |
| 4481 | } |
| 4482 | } |
| 4483 | #endif |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 4484 | } |
danielk1977 | e80463b | 2004-11-03 03:01:16 +0000 | [diff] [blame] | 4485 | |
danielk1977 | e80463b | 2004-11-03 03:01:16 +0000 | [diff] [blame] | 4486 | return SQLITE_OK; |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 4487 | } |
| 4488 | |
| 4489 | /* |
drh | fa1a98a | 2004-05-14 19:08:17 +0000 | [diff] [blame] | 4490 | ** Add a list of cells to a page. The page should be initially empty. |
| 4491 | ** The cells are guaranteed to fit on the page. |
| 4492 | */ |
| 4493 | static void assemblePage( |
| 4494 | MemPage *pPage, /* The page to be assemblied */ |
| 4495 | int nCell, /* The number of cells to add to this page */ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4496 | u8 **apCell, /* Pointers to cell bodies */ |
drh | fa1a98a | 2004-05-14 19:08:17 +0000 | [diff] [blame] | 4497 | int *aSize /* Sizes of the cells */ |
| 4498 | ){ |
| 4499 | int i; /* Loop counter */ |
| 4500 | int totalSize; /* Total size of all cells */ |
| 4501 | int hdr; /* Index of page header */ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4502 | int cellptr; /* Address of next cell pointer */ |
| 4503 | int cellbody; /* Address of next cell body */ |
drh | fa1a98a | 2004-05-14 19:08:17 +0000 | [diff] [blame] | 4504 | u8 *data; /* Data for the page */ |
| 4505 | |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4506 | assert( pPage->nOverflow==0 ); |
drh | 2764170 | 2007-08-22 02:56:42 +0000 | [diff] [blame] | 4507 | assert( sqlite3BtreeMutexHeld(pPage->pBt->mutex) ); |
drh | fa1a98a | 2004-05-14 19:08:17 +0000 | [diff] [blame] | 4508 | totalSize = 0; |
| 4509 | for(i=0; i<nCell; i++){ |
| 4510 | totalSize += aSize[i]; |
| 4511 | } |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4512 | assert( totalSize+2*nCell<=pPage->nFree ); |
drh | fa1a98a | 2004-05-14 19:08:17 +0000 | [diff] [blame] | 4513 | assert( pPage->nCell==0 ); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4514 | cellptr = pPage->cellOffset; |
drh | fa1a98a | 2004-05-14 19:08:17 +0000 | [diff] [blame] | 4515 | data = pPage->aData; |
| 4516 | hdr = pPage->hdrOffset; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4517 | put2byte(&data[hdr+3], nCell); |
drh | 09d0deb | 2005-08-02 17:13:09 +0000 | [diff] [blame] | 4518 | if( nCell ){ |
| 4519 | cellbody = allocateSpace(pPage, totalSize); |
| 4520 | assert( cellbody>0 ); |
| 4521 | assert( pPage->nFree >= 2*nCell ); |
| 4522 | pPage->nFree -= 2*nCell; |
| 4523 | for(i=0; i<nCell; i++){ |
| 4524 | put2byte(&data[cellptr], cellbody); |
| 4525 | memcpy(&data[cellbody], apCell[i], aSize[i]); |
| 4526 | cellptr += 2; |
| 4527 | cellbody += aSize[i]; |
| 4528 | } |
| 4529 | assert( cellbody==pPage->pBt->usableSize ); |
drh | fa1a98a | 2004-05-14 19:08:17 +0000 | [diff] [blame] | 4530 | } |
| 4531 | pPage->nCell = nCell; |
drh | fa1a98a | 2004-05-14 19:08:17 +0000 | [diff] [blame] | 4532 | } |
| 4533 | |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 4534 | /* |
drh | c3b7057 | 2003-01-04 19:44:07 +0000 | [diff] [blame] | 4535 | ** The following parameters determine how many adjacent pages get involved |
| 4536 | ** in a balancing operation. NN is the number of neighbors on either side |
| 4537 | ** of the page that participate in the balancing operation. NB is the |
| 4538 | ** total number of pages that participate, including the target page and |
| 4539 | ** NN neighbors on either side. |
| 4540 | ** |
| 4541 | ** The minimum value of NN is 1 (of course). Increasing NN above 1 |
| 4542 | ** (to 2 or 3) gives a modest improvement in SELECT and DELETE performance |
| 4543 | ** in exchange for a larger degradation in INSERT and UPDATE performance. |
| 4544 | ** The value of NN appears to give the best results overall. |
| 4545 | */ |
| 4546 | #define NN 1 /* Number of neighbors on either side of pPage */ |
| 4547 | #define NB (NN*2+1) /* Total pages involved in the balance */ |
| 4548 | |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4549 | /* Forward reference */ |
danielk1977 | ac245ec | 2005-01-14 13:50:11 +0000 | [diff] [blame] | 4550 | static int balance(MemPage*, int); |
| 4551 | |
drh | 615ae55 | 2005-01-16 23:21:00 +0000 | [diff] [blame] | 4552 | #ifndef SQLITE_OMIT_QUICKBALANCE |
drh | f222e71 | 2005-01-14 22:55:49 +0000 | [diff] [blame] | 4553 | /* |
| 4554 | ** This version of balance() handles the common special case where |
| 4555 | ** a new entry is being inserted on the extreme right-end of the |
| 4556 | ** tree, in other words, when the new entry will become the largest |
| 4557 | ** entry in the tree. |
| 4558 | ** |
| 4559 | ** Instead of trying balance the 3 right-most leaf pages, just add |
| 4560 | ** a new page to the right-hand side and put the one new entry in |
| 4561 | ** that page. This leaves the right side of the tree somewhat |
| 4562 | ** unbalanced. But odds are that we will be inserting new entries |
| 4563 | ** at the end soon afterwards so the nearly empty page will quickly |
| 4564 | ** fill up. On average. |
| 4565 | ** |
| 4566 | ** pPage is the leaf page which is the right-most page in the tree. |
| 4567 | ** pParent is its parent. pPage must have a single overflow entry |
| 4568 | ** which is also the right-most entry on the page. |
| 4569 | */ |
danielk1977 | ac245ec | 2005-01-14 13:50:11 +0000 | [diff] [blame] | 4570 | static int balance_quick(MemPage *pPage, MemPage *pParent){ |
| 4571 | int rc; |
| 4572 | MemPage *pNew; |
| 4573 | Pgno pgnoNew; |
| 4574 | u8 *pCell; |
| 4575 | int szCell; |
| 4576 | CellInfo info; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 4577 | BtShared *pBt = pPage->pBt; |
danielk1977 | 79a40da | 2005-01-16 08:00:01 +0000 | [diff] [blame] | 4578 | int parentIdx = pParent->nCell; /* pParent new divider cell index */ |
| 4579 | int parentSize; /* Size of new divider cell */ |
| 4580 | u8 parentCell[64]; /* Space for the new divider cell */ |
danielk1977 | ac245ec | 2005-01-14 13:50:11 +0000 | [diff] [blame] | 4581 | |
drh | 2764170 | 2007-08-22 02:56:42 +0000 | [diff] [blame] | 4582 | assert( sqlite3BtreeMutexHeld(pPage->pBt->mutex) ); |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 4583 | |
danielk1977 | ac245ec | 2005-01-14 13:50:11 +0000 | [diff] [blame] | 4584 | /* Allocate a new page. Insert the overflow cell from pPage |
| 4585 | ** into it. Then remove the overflow cell from pPage. |
| 4586 | */ |
drh | 4f0c587 | 2007-03-26 22:05:01 +0000 | [diff] [blame] | 4587 | rc = allocateBtreePage(pBt, &pNew, &pgnoNew, 0, 0); |
danielk1977 | ac245ec | 2005-01-14 13:50:11 +0000 | [diff] [blame] | 4588 | if( rc!=SQLITE_OK ){ |
| 4589 | return rc; |
| 4590 | } |
| 4591 | pCell = pPage->aOvfl[0].pCell; |
| 4592 | szCell = cellSizePtr(pPage, pCell); |
| 4593 | zeroPage(pNew, pPage->aData[0]); |
| 4594 | assemblePage(pNew, 1, &pCell, &szCell); |
| 4595 | pPage->nOverflow = 0; |
| 4596 | |
danielk1977 | 79a40da | 2005-01-16 08:00:01 +0000 | [diff] [blame] | 4597 | /* Set the parent of the newly allocated page to pParent. */ |
| 4598 | pNew->pParent = pParent; |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 4599 | sqlite3PagerRef(pParent->pDbPage); |
danielk1977 | 79a40da | 2005-01-16 08:00:01 +0000 | [diff] [blame] | 4600 | |
danielk1977 | ac245ec | 2005-01-14 13:50:11 +0000 | [diff] [blame] | 4601 | /* pPage is currently the right-child of pParent. Change this |
| 4602 | ** so that the right-child is the new page allocated above and |
danielk1977 | 79a40da | 2005-01-16 08:00:01 +0000 | [diff] [blame] | 4603 | ** pPage is the next-to-right child. |
danielk1977 | ac245ec | 2005-01-14 13:50:11 +0000 | [diff] [blame] | 4604 | */ |
danielk1977 | ac11ee6 | 2005-01-15 12:45:51 +0000 | [diff] [blame] | 4605 | assert( pPage->nCell>0 ); |
danielk1977 | 1cc5ed8 | 2007-05-16 17:28:43 +0000 | [diff] [blame] | 4606 | pCell = findCell(pPage, pPage->nCell-1); |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 4607 | sqlite3BtreeParseCellPtr(pPage, pCell, &info); |
drh | b026e05 | 2007-05-02 01:34:31 +0000 | [diff] [blame] | 4608 | rc = fillInCell(pParent, parentCell, 0, info.nKey, 0, 0, 0, &parentSize); |
danielk1977 | ac245ec | 2005-01-14 13:50:11 +0000 | [diff] [blame] | 4609 | if( rc!=SQLITE_OK ){ |
danielk1977 | 79a40da | 2005-01-16 08:00:01 +0000 | [diff] [blame] | 4610 | return rc; |
danielk1977 | ac245ec | 2005-01-14 13:50:11 +0000 | [diff] [blame] | 4611 | } |
| 4612 | assert( parentSize<64 ); |
| 4613 | rc = insertCell(pParent, parentIdx, parentCell, parentSize, 0, 4); |
| 4614 | if( rc!=SQLITE_OK ){ |
danielk1977 | 79a40da | 2005-01-16 08:00:01 +0000 | [diff] [blame] | 4615 | return rc; |
danielk1977 | ac245ec | 2005-01-14 13:50:11 +0000 | [diff] [blame] | 4616 | } |
| 4617 | put4byte(findOverflowCell(pParent,parentIdx), pPage->pgno); |
| 4618 | put4byte(&pParent->aData[pParent->hdrOffset+8], pgnoNew); |
| 4619 | |
danielk1977 | 79a40da | 2005-01-16 08:00:01 +0000 | [diff] [blame] | 4620 | #ifndef SQLITE_OMIT_AUTOVACUUM |
| 4621 | /* If this is an auto-vacuum database, update the pointer map |
| 4622 | ** with entries for the new page, and any pointer from the |
| 4623 | ** cell on the page to an overflow page. |
| 4624 | */ |
danielk1977 | ac11ee6 | 2005-01-15 12:45:51 +0000 | [diff] [blame] | 4625 | if( pBt->autoVacuum ){ |
| 4626 | rc = ptrmapPut(pBt, pgnoNew, PTRMAP_BTREE, pParent->pgno); |
danielk1977 | deb403e | 2007-05-24 09:20:16 +0000 | [diff] [blame] | 4627 | if( rc==SQLITE_OK ){ |
| 4628 | rc = ptrmapPutOvfl(pNew, 0); |
danielk1977 | ac11ee6 | 2005-01-15 12:45:51 +0000 | [diff] [blame] | 4629 | } |
danielk1977 | 79a40da | 2005-01-16 08:00:01 +0000 | [diff] [blame] | 4630 | if( rc!=SQLITE_OK ){ |
danielk1977 | deb403e | 2007-05-24 09:20:16 +0000 | [diff] [blame] | 4631 | releasePage(pNew); |
danielk1977 | 79a40da | 2005-01-16 08:00:01 +0000 | [diff] [blame] | 4632 | return rc; |
danielk1977 | ac11ee6 | 2005-01-15 12:45:51 +0000 | [diff] [blame] | 4633 | } |
| 4634 | } |
danielk1977 | 79a40da | 2005-01-16 08:00:01 +0000 | [diff] [blame] | 4635 | #endif |
danielk1977 | ac11ee6 | 2005-01-15 12:45:51 +0000 | [diff] [blame] | 4636 | |
danielk1977 | 79a40da | 2005-01-16 08:00:01 +0000 | [diff] [blame] | 4637 | /* Release the reference to the new page and balance the parent page, |
| 4638 | ** in case the divider cell inserted caused it to become overfull. |
| 4639 | */ |
danielk1977 | ac245ec | 2005-01-14 13:50:11 +0000 | [diff] [blame] | 4640 | releasePage(pNew); |
| 4641 | return balance(pParent, 0); |
| 4642 | } |
drh | 615ae55 | 2005-01-16 23:21:00 +0000 | [diff] [blame] | 4643 | #endif /* SQLITE_OMIT_QUICKBALANCE */ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4644 | |
drh | c3b7057 | 2003-01-04 19:44:07 +0000 | [diff] [blame] | 4645 | /* |
drh | ab01f61 | 2004-05-22 02:55:23 +0000 | [diff] [blame] | 4646 | ** This routine redistributes Cells on pPage and up to NN*2 siblings |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 4647 | ** 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] | 4648 | ** Usually NN siblings on either side of pPage is used in the balancing, |
| 4649 | ** though more siblings might come from one side if pPage is the first |
drh | ab01f61 | 2004-05-22 02:55:23 +0000 | [diff] [blame] | 4650 | ** 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] | 4651 | ** (something which can only happen if pPage is the root page or a |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 4652 | ** child of root) then all available siblings participate in the balancing. |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 4653 | ** |
drh | 0c6cc4e | 2004-06-15 02:13:26 +0000 | [diff] [blame] | 4654 | ** The number of siblings of pPage might be increased or decreased by one or |
| 4655 | ** 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] | 4656 | ** is special and is allowed to be nearly empty. If pPage is |
drh | 8c42ca9 | 2001-06-22 19:15:00 +0000 | [diff] [blame] | 4657 | ** the root page, then the depth of the tree might be increased |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 4658 | ** or decreased by one, as necessary, to keep the root page from being |
drh | ab01f61 | 2004-05-22 02:55:23 +0000 | [diff] [blame] | 4659 | ** overfull or completely empty. |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 4660 | ** |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 4661 | ** Note that when this routine is called, some of the Cells on pPage |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 4662 | ** might not actually be stored in pPage->aData[]. This can happen |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 4663 | ** 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] | 4664 | ** make sure all Cells for pPage once again fit in pPage->aData[]. |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 4665 | ** |
drh | 8c42ca9 | 2001-06-22 19:15:00 +0000 | [diff] [blame] | 4666 | ** In the course of balancing the siblings of pPage, the parent of pPage |
| 4667 | ** might become overfull or underfull. If that happens, then this routine |
| 4668 | ** is called recursively on the parent. |
| 4669 | ** |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4670 | ** If this routine fails for any reason, it might leave the database |
| 4671 | ** in a corrupted state. So if this routine fails, the database should |
| 4672 | ** be rolled back. |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 4673 | */ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4674 | static int balance_nonroot(MemPage *pPage){ |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 4675 | MemPage *pParent; /* The parent of pPage */ |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 4676 | BtShared *pBt; /* The whole database */ |
danielk1977 | 634f298 | 2005-03-28 08:44:07 +0000 | [diff] [blame] | 4677 | int nCell = 0; /* Number of cells in apCell[] */ |
| 4678 | int nMaxCells = 0; /* Allocated size of apCell, szCell, aFrom. */ |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 4679 | int nOld; /* Number of pages in apOld[] */ |
| 4680 | int nNew; /* Number of pages in apNew[] */ |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 4681 | int nDiv; /* Number of cells in apDiv[] */ |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 4682 | int i, j, k; /* Loop counters */ |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 4683 | int idx; /* Index of pPage in pParent->aCell[] */ |
| 4684 | int nxDiv; /* Next divider slot in pParent->aCell[] */ |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 4685 | int rc; /* The return code */ |
drh | 9102529 | 2004-05-03 19:49:32 +0000 | [diff] [blame] | 4686 | int leafCorrection; /* 4 if pPage is a leaf. 0 if not */ |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 4687 | int leafData; /* True if pPage is a leaf of a LEAFDATA tree */ |
drh | 9102529 | 2004-05-03 19:49:32 +0000 | [diff] [blame] | 4688 | int usableSpace; /* Bytes in pPage beyond the header */ |
| 4689 | int pageFlags; /* Value of pPage->aData[0] */ |
drh | 6019e16 | 2001-07-02 17:51:45 +0000 | [diff] [blame] | 4690 | int subtotal; /* Subtotal of bytes in cells on one page */ |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 4691 | int iSpace = 0; /* First unused byte of aSpace[] */ |
drh | c3b7057 | 2003-01-04 19:44:07 +0000 | [diff] [blame] | 4692 | MemPage *apOld[NB]; /* pPage and up to two siblings */ |
| 4693 | Pgno pgnoOld[NB]; /* Page numbers for each page in apOld[] */ |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 4694 | MemPage *apCopy[NB]; /* Private copies of apOld[] pages */ |
drh | a2fce64 | 2004-06-05 00:01:44 +0000 | [diff] [blame] | 4695 | MemPage *apNew[NB+2]; /* pPage and up to NB siblings after balancing */ |
| 4696 | Pgno pgnoNew[NB+2]; /* Page numbers for each page in apNew[] */ |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 4697 | u8 *apDiv[NB]; /* Divider cells in pParent */ |
drh | a2fce64 | 2004-06-05 00:01:44 +0000 | [diff] [blame] | 4698 | int cntNew[NB+2]; /* Index in aCell[] of cell after i-th page */ |
| 4699 | int szNew[NB+2]; /* Combined size of cells place on i-th page */ |
danielk1977 | 50f059b | 2005-03-29 02:54:03 +0000 | [diff] [blame] | 4700 | u8 **apCell = 0; /* All cells begin balanced */ |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 4701 | int *szCell; /* Local size of all cells in apCell[] */ |
| 4702 | u8 *aCopy[NB]; /* Space for holding data of apCopy[] */ |
| 4703 | u8 *aSpace; /* Space to hold copies of dividers cells */ |
danielk1977 | 4e17d14 | 2005-01-16 09:06:33 +0000 | [diff] [blame] | 4704 | #ifndef SQLITE_OMIT_AUTOVACUUM |
danielk1977 | ac11ee6 | 2005-01-15 12:45:51 +0000 | [diff] [blame] | 4705 | u8 *aFrom = 0; |
| 4706 | #endif |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 4707 | |
drh | 2764170 | 2007-08-22 02:56:42 +0000 | [diff] [blame] | 4708 | assert( sqlite3BtreeMutexHeld(pPage->pBt->mutex) ); |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 4709 | |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 4710 | /* |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4711 | ** Find the parent page. |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 4712 | */ |
drh | 3a4c141 | 2004-05-09 20:40:11 +0000 | [diff] [blame] | 4713 | assert( pPage->isInit ); |
danielk1977 | 6e465eb | 2007-08-21 13:11:00 +0000 | [diff] [blame] | 4714 | assert( sqlite3PagerIswriteable(pPage->pDbPage) || pPage->nOverflow==1 ); |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 4715 | pBt = pPage->pBt; |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 4716 | pParent = pPage->pParent; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4717 | assert( pParent ); |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 4718 | if( SQLITE_OK!=(rc = sqlite3PagerWrite(pParent->pDbPage)) ){ |
danielk1977 | 07cb560 | 2006-01-20 10:55:05 +0000 | [diff] [blame] | 4719 | return rc; |
| 4720 | } |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4721 | TRACE(("BALANCE: begin page %d child of %d\n", pPage->pgno, pParent->pgno)); |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 4722 | |
drh | 615ae55 | 2005-01-16 23:21:00 +0000 | [diff] [blame] | 4723 | #ifndef SQLITE_OMIT_QUICKBALANCE |
drh | f222e71 | 2005-01-14 22:55:49 +0000 | [diff] [blame] | 4724 | /* |
| 4725 | ** A special case: If a new entry has just been inserted into a |
| 4726 | ** 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] | 4727 | ** 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] | 4728 | ** largest key) then use the special balance_quick() routine for |
| 4729 | ** balancing. balance_quick() is much faster and results in a tighter |
| 4730 | ** packing of data in the common case. |
| 4731 | */ |
danielk1977 | ac245ec | 2005-01-14 13:50:11 +0000 | [diff] [blame] | 4732 | if( pPage->leaf && |
| 4733 | pPage->intKey && |
| 4734 | pPage->leafData && |
| 4735 | pPage->nOverflow==1 && |
| 4736 | pPage->aOvfl[0].idx==pPage->nCell && |
danielk1977 | ac11ee6 | 2005-01-15 12:45:51 +0000 | [diff] [blame] | 4737 | pPage->pParent->pgno!=1 && |
danielk1977 | ac245ec | 2005-01-14 13:50:11 +0000 | [diff] [blame] | 4738 | get4byte(&pParent->aData[pParent->hdrOffset+8])==pPage->pgno |
| 4739 | ){ |
danielk1977 | ac11ee6 | 2005-01-15 12:45:51 +0000 | [diff] [blame] | 4740 | /* |
| 4741 | ** TODO: Check the siblings to the left of pPage. It may be that |
| 4742 | ** they are not full and no new page is required. |
| 4743 | */ |
danielk1977 | ac245ec | 2005-01-14 13:50:11 +0000 | [diff] [blame] | 4744 | return balance_quick(pPage, pParent); |
| 4745 | } |
| 4746 | #endif |
| 4747 | |
danielk1977 | 6e465eb | 2007-08-21 13:11:00 +0000 | [diff] [blame] | 4748 | if( SQLITE_OK!=(rc = sqlite3PagerWrite(pPage->pDbPage)) ){ |
| 4749 | return rc; |
| 4750 | } |
| 4751 | |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 4752 | /* |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 4753 | ** Find the cell in the parent page whose left child points back |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 4754 | ** to pPage. The "idx" variable is the index of that cell. If pPage |
| 4755 | ** is the rightmost child of pParent then set idx to pParent->nCell |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 4756 | */ |
drh | bb49aba | 2003-01-04 18:53:27 +0000 | [diff] [blame] | 4757 | if( pParent->idxShift ){ |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 4758 | Pgno pgno; |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 4759 | pgno = pPage->pgno; |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 4760 | assert( pgno==sqlite3PagerPagenumber(pPage->pDbPage) ); |
drh | bb49aba | 2003-01-04 18:53:27 +0000 | [diff] [blame] | 4761 | for(idx=0; idx<pParent->nCell; idx++){ |
danielk1977 | 1cc5ed8 | 2007-05-16 17:28:43 +0000 | [diff] [blame] | 4762 | if( get4byte(findCell(pParent, idx))==pgno ){ |
drh | bb49aba | 2003-01-04 18:53:27 +0000 | [diff] [blame] | 4763 | break; |
| 4764 | } |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 4765 | } |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 4766 | assert( idx<pParent->nCell |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4767 | || get4byte(&pParent->aData[pParent->hdrOffset+8])==pgno ); |
drh | bb49aba | 2003-01-04 18:53:27 +0000 | [diff] [blame] | 4768 | }else{ |
| 4769 | idx = pPage->idxParent; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 4770 | } |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 4771 | |
| 4772 | /* |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 4773 | ** Initialize variables so that it will be safe to jump |
drh | 5edc312 | 2001-09-13 21:53:09 +0000 | [diff] [blame] | 4774 | ** directly to balance_cleanup at any moment. |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 4775 | */ |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 4776 | nOld = nNew = 0; |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 4777 | sqlite3PagerRef(pParent->pDbPage); |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 4778 | |
| 4779 | /* |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 4780 | ** Find sibling pages to pPage and the cells in pParent that divide |
drh | c3b7057 | 2003-01-04 19:44:07 +0000 | [diff] [blame] | 4781 | ** the siblings. An attempt is made to find NN siblings on either |
| 4782 | ** side of pPage. More siblings are taken from one side, however, if |
| 4783 | ** pPage there are fewer than NN siblings on the other side. If pParent |
| 4784 | ** has NB or fewer children then all children of pParent are taken. |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 4785 | */ |
drh | c3b7057 | 2003-01-04 19:44:07 +0000 | [diff] [blame] | 4786 | nxDiv = idx - NN; |
| 4787 | if( nxDiv + NB > pParent->nCell ){ |
| 4788 | nxDiv = pParent->nCell - NB + 1; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 4789 | } |
drh | c3b7057 | 2003-01-04 19:44:07 +0000 | [diff] [blame] | 4790 | if( nxDiv<0 ){ |
| 4791 | nxDiv = 0; |
| 4792 | } |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 4793 | nDiv = 0; |
drh | c3b7057 | 2003-01-04 19:44:07 +0000 | [diff] [blame] | 4794 | for(i=0, k=nxDiv; i<NB; i++, k++){ |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 4795 | if( k<pParent->nCell ){ |
danielk1977 | 1cc5ed8 | 2007-05-16 17:28:43 +0000 | [diff] [blame] | 4796 | apDiv[i] = findCell(pParent, k); |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 4797 | nDiv++; |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 4798 | assert( !pParent->leaf ); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4799 | pgnoOld[i] = get4byte(apDiv[i]); |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 4800 | }else if( k==pParent->nCell ){ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4801 | pgnoOld[i] = get4byte(&pParent->aData[pParent->hdrOffset+8]); |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 4802 | }else{ |
| 4803 | break; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 4804 | } |
drh | de64713 | 2004-05-07 17:57:49 +0000 | [diff] [blame] | 4805 | rc = getAndInitPage(pBt, pgnoOld[i], &apOld[i], pParent); |
drh | 6019e16 | 2001-07-02 17:51:45 +0000 | [diff] [blame] | 4806 | if( rc ) goto balance_cleanup; |
drh | 428ae8c | 2003-01-04 16:48:09 +0000 | [diff] [blame] | 4807 | apOld[i]->idxParent = k; |
drh | 9102529 | 2004-05-03 19:49:32 +0000 | [diff] [blame] | 4808 | apCopy[i] = 0; |
| 4809 | assert( i==nOld ); |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 4810 | nOld++; |
danielk1977 | 634f298 | 2005-03-28 08:44:07 +0000 | [diff] [blame] | 4811 | nMaxCells += 1+apOld[i]->nCell+apOld[i]->nOverflow; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 4812 | } |
| 4813 | |
drh | 8d97f1f | 2005-05-05 18:14:13 +0000 | [diff] [blame] | 4814 | /* Make nMaxCells a multiple of 2 in order to preserve 8-byte |
| 4815 | ** alignment */ |
| 4816 | nMaxCells = (nMaxCells + 1)&~1; |
| 4817 | |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 4818 | /* |
danielk1977 | 634f298 | 2005-03-28 08:44:07 +0000 | [diff] [blame] | 4819 | ** Allocate space for memory structures |
| 4820 | */ |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 4821 | apCell = sqlite3_malloc( |
danielk1977 | 634f298 | 2005-03-28 08:44:07 +0000 | [diff] [blame] | 4822 | nMaxCells*sizeof(u8*) /* apCell */ |
| 4823 | + nMaxCells*sizeof(int) /* szCell */ |
drh | c96d853 | 2005-05-03 12:30:33 +0000 | [diff] [blame] | 4824 | + ROUND8(sizeof(MemPage))*NB /* aCopy */ |
drh | 07d183d | 2005-05-01 22:52:42 +0000 | [diff] [blame] | 4825 | + pBt->pageSize*(5+NB) /* aSpace */ |
drh | c96d853 | 2005-05-03 12:30:33 +0000 | [diff] [blame] | 4826 | + (ISAUTOVACUUM ? nMaxCells : 0) /* aFrom */ |
danielk1977 | 634f298 | 2005-03-28 08:44:07 +0000 | [diff] [blame] | 4827 | ); |
| 4828 | if( apCell==0 ){ |
| 4829 | rc = SQLITE_NOMEM; |
| 4830 | goto balance_cleanup; |
| 4831 | } |
| 4832 | szCell = (int*)&apCell[nMaxCells]; |
| 4833 | aCopy[0] = (u8*)&szCell[nMaxCells]; |
drh | c96d853 | 2005-05-03 12:30:33 +0000 | [diff] [blame] | 4834 | assert( ((aCopy[0] - (u8*)apCell) & 7)==0 ); /* 8-byte alignment required */ |
danielk1977 | 634f298 | 2005-03-28 08:44:07 +0000 | [diff] [blame] | 4835 | for(i=1; i<NB; i++){ |
drh | c96d853 | 2005-05-03 12:30:33 +0000 | [diff] [blame] | 4836 | aCopy[i] = &aCopy[i-1][pBt->pageSize+ROUND8(sizeof(MemPage))]; |
| 4837 | assert( ((aCopy[i] - (u8*)apCell) & 7)==0 ); /* 8-byte alignment required */ |
danielk1977 | 634f298 | 2005-03-28 08:44:07 +0000 | [diff] [blame] | 4838 | } |
drh | c96d853 | 2005-05-03 12:30:33 +0000 | [diff] [blame] | 4839 | aSpace = &aCopy[NB-1][pBt->pageSize+ROUND8(sizeof(MemPage))]; |
| 4840 | assert( ((aSpace - (u8*)apCell) & 7)==0 ); /* 8-byte alignment required */ |
danielk1977 | 634f298 | 2005-03-28 08:44:07 +0000 | [diff] [blame] | 4841 | #ifndef SQLITE_OMIT_AUTOVACUUM |
| 4842 | if( pBt->autoVacuum ){ |
drh | 07d183d | 2005-05-01 22:52:42 +0000 | [diff] [blame] | 4843 | aFrom = &aSpace[5*pBt->pageSize]; |
danielk1977 | 634f298 | 2005-03-28 08:44:07 +0000 | [diff] [blame] | 4844 | } |
| 4845 | #endif |
| 4846 | |
| 4847 | /* |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 4848 | ** Make copies of the content of pPage and its siblings into aOld[]. |
| 4849 | ** The rest of this function will use data from the copies rather |
| 4850 | ** that the original pages since the original pages will be in the |
| 4851 | ** process of being overwritten. |
| 4852 | */ |
| 4853 | for(i=0; i<nOld; i++){ |
drh | 07d183d | 2005-05-01 22:52:42 +0000 | [diff] [blame] | 4854 | MemPage *p = apCopy[i] = (MemPage*)&aCopy[i][pBt->pageSize]; |
drh | 07d183d | 2005-05-01 22:52:42 +0000 | [diff] [blame] | 4855 | p->aData = &((u8*)p)[-pBt->pageSize]; |
| 4856 | memcpy(p->aData, apOld[i]->aData, pBt->pageSize + sizeof(MemPage)); |
| 4857 | /* The memcpy() above changes the value of p->aData so we have to |
| 4858 | ** set it again. */ |
drh | 07d183d | 2005-05-01 22:52:42 +0000 | [diff] [blame] | 4859 | p->aData = &((u8*)p)[-pBt->pageSize]; |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 4860 | } |
| 4861 | |
| 4862 | /* |
| 4863 | ** Load pointers to all cells on sibling pages and the divider cells |
| 4864 | ** into the local apCell[] array. Make copies of the divider cells |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 4865 | ** into space obtained form aSpace[] and remove the the divider Cells |
| 4866 | ** from pParent. |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 4867 | ** |
| 4868 | ** If the siblings are on leaf pages, then the child pointers of the |
| 4869 | ** divider cells are stripped from the cells before they are copied |
drh | 96f5b76 | 2004-05-16 16:24:36 +0000 | [diff] [blame] | 4870 | ** into aSpace[]. In this way, all cells in apCell[] are without |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 4871 | ** child pointers. If siblings are not leaves, then all cell in |
| 4872 | ** apCell[] include child pointers. Either way, all cells in apCell[] |
| 4873 | ** are alike. |
drh | 96f5b76 | 2004-05-16 16:24:36 +0000 | [diff] [blame] | 4874 | ** |
| 4875 | ** leafCorrection: 4 if pPage is a leaf. 0 if pPage is not a leaf. |
| 4876 | ** leafData: 1 if pPage holds key+data and pParent holds only keys. |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 4877 | */ |
| 4878 | nCell = 0; |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 4879 | leafCorrection = pPage->leaf*4; |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 4880 | leafData = pPage->leafData && pPage->leaf; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 4881 | for(i=0; i<nOld; i++){ |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 4882 | MemPage *pOld = apCopy[i]; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4883 | int limit = pOld->nCell+pOld->nOverflow; |
| 4884 | for(j=0; j<limit; j++){ |
danielk1977 | 634f298 | 2005-03-28 08:44:07 +0000 | [diff] [blame] | 4885 | assert( nCell<nMaxCells ); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4886 | apCell[nCell] = findOverflowCell(pOld, j); |
| 4887 | szCell[nCell] = cellSizePtr(pOld, apCell[nCell]); |
danielk1977 | ac11ee6 | 2005-01-15 12:45:51 +0000 | [diff] [blame] | 4888 | #ifndef SQLITE_OMIT_AUTOVACUUM |
| 4889 | if( pBt->autoVacuum ){ |
| 4890 | int a; |
| 4891 | aFrom[nCell] = i; |
| 4892 | for(a=0; a<pOld->nOverflow; a++){ |
| 4893 | if( pOld->aOvfl[a].pCell==apCell[nCell] ){ |
| 4894 | aFrom[nCell] = 0xFF; |
| 4895 | break; |
| 4896 | } |
| 4897 | } |
| 4898 | } |
| 4899 | #endif |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 4900 | nCell++; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 4901 | } |
| 4902 | if( i<nOld-1 ){ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4903 | int sz = cellSizePtr(pParent, apDiv[i]); |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 4904 | if( leafData ){ |
drh | 96f5b76 | 2004-05-16 16:24:36 +0000 | [diff] [blame] | 4905 | /* With the LEAFDATA flag, pParent cells hold only INTKEYs that |
| 4906 | ** are duplicates of keys on the child pages. We need to remove |
| 4907 | ** the divider cells from pParent, but the dividers cells are not |
| 4908 | ** added to apCell[] because they are duplicates of child cells. |
| 4909 | */ |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 4910 | dropCell(pParent, nxDiv, sz); |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 4911 | }else{ |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 4912 | u8 *pTemp; |
danielk1977 | 634f298 | 2005-03-28 08:44:07 +0000 | [diff] [blame] | 4913 | assert( nCell<nMaxCells ); |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 4914 | szCell[nCell] = sz; |
| 4915 | pTemp = &aSpace[iSpace]; |
| 4916 | iSpace += sz; |
drh | 07d183d | 2005-05-01 22:52:42 +0000 | [diff] [blame] | 4917 | assert( iSpace<=pBt->pageSize*5 ); |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 4918 | memcpy(pTemp, apDiv[i], sz); |
| 4919 | apCell[nCell] = pTemp+leafCorrection; |
danielk1977 | ac11ee6 | 2005-01-15 12:45:51 +0000 | [diff] [blame] | 4920 | #ifndef SQLITE_OMIT_AUTOVACUUM |
| 4921 | if( pBt->autoVacuum ){ |
| 4922 | aFrom[nCell] = 0xFF; |
| 4923 | } |
| 4924 | #endif |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 4925 | dropCell(pParent, nxDiv, sz); |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 4926 | szCell[nCell] -= leafCorrection; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4927 | assert( get4byte(pTemp)==pgnoOld[i] ); |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 4928 | if( !pOld->leaf ){ |
| 4929 | assert( leafCorrection==0 ); |
| 4930 | /* The right pointer of the child page pOld becomes the left |
| 4931 | ** pointer of the divider cell */ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4932 | memcpy(apCell[nCell], &pOld->aData[pOld->hdrOffset+8], 4); |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 4933 | }else{ |
| 4934 | assert( leafCorrection==4 ); |
danielk1977 | 39c9604 | 2007-05-12 10:41:47 +0000 | [diff] [blame] | 4935 | if( szCell[nCell]<4 ){ |
| 4936 | /* Do not allow any cells smaller than 4 bytes. */ |
| 4937 | szCell[nCell] = 4; |
| 4938 | } |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 4939 | } |
| 4940 | nCell++; |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 4941 | } |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 4942 | } |
| 4943 | } |
| 4944 | |
| 4945 | /* |
drh | 6019e16 | 2001-07-02 17:51:45 +0000 | [diff] [blame] | 4946 | ** Figure out the number of pages needed to hold all nCell cells. |
| 4947 | ** Store this number in "k". Also compute szNew[] which is the total |
| 4948 | ** 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] | 4949 | ** in apCell[] of the cell that divides page i from page i+1. |
drh | 6019e16 | 2001-07-02 17:51:45 +0000 | [diff] [blame] | 4950 | ** cntNew[k] should equal nCell. |
| 4951 | ** |
drh | 96f5b76 | 2004-05-16 16:24:36 +0000 | [diff] [blame] | 4952 | ** Values computed by this block: |
| 4953 | ** |
| 4954 | ** k: The total number of sibling pages |
| 4955 | ** szNew[i]: Spaced used on the i-th sibling page. |
| 4956 | ** cntNew[i]: Index in apCell[] and szCell[] for the first cell to |
| 4957 | ** the right of the i-th sibling page. |
| 4958 | ** usableSpace: Number of bytes of space available on each sibling. |
| 4959 | ** |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 4960 | */ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4961 | usableSpace = pBt->usableSize - 12 + leafCorrection; |
drh | 6019e16 | 2001-07-02 17:51:45 +0000 | [diff] [blame] | 4962 | for(subtotal=k=i=0; i<nCell; i++){ |
danielk1977 | 634f298 | 2005-03-28 08:44:07 +0000 | [diff] [blame] | 4963 | assert( i<nMaxCells ); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4964 | subtotal += szCell[i] + 2; |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 4965 | if( subtotal > usableSpace ){ |
drh | 6019e16 | 2001-07-02 17:51:45 +0000 | [diff] [blame] | 4966 | szNew[k] = subtotal - szCell[i]; |
| 4967 | cntNew[k] = i; |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 4968 | if( leafData ){ i--; } |
drh | 6019e16 | 2001-07-02 17:51:45 +0000 | [diff] [blame] | 4969 | subtotal = 0; |
| 4970 | k++; |
| 4971 | } |
| 4972 | } |
| 4973 | szNew[k] = subtotal; |
| 4974 | cntNew[k] = nCell; |
| 4975 | k++; |
drh | 96f5b76 | 2004-05-16 16:24:36 +0000 | [diff] [blame] | 4976 | |
| 4977 | /* |
| 4978 | ** The packing computed by the previous block is biased toward the siblings |
| 4979 | ** on the left side. The left siblings are always nearly full, while the |
| 4980 | ** right-most sibling might be nearly empty. This block of code attempts |
| 4981 | ** to adjust the packing of siblings to get a better balance. |
| 4982 | ** |
| 4983 | ** This adjustment is more than an optimization. The packing above might |
| 4984 | ** be so out of balance as to be illegal. For example, the right-most |
| 4985 | ** sibling might be completely empty. This adjustment is not optional. |
| 4986 | */ |
drh | 6019e16 | 2001-07-02 17:51:45 +0000 | [diff] [blame] | 4987 | for(i=k-1; i>0; i--){ |
drh | 96f5b76 | 2004-05-16 16:24:36 +0000 | [diff] [blame] | 4988 | int szRight = szNew[i]; /* Size of sibling on the right */ |
| 4989 | int szLeft = szNew[i-1]; /* Size of sibling on the left */ |
| 4990 | int r; /* Index of right-most cell in left sibling */ |
| 4991 | int d; /* Index of first cell to the left of right sibling */ |
| 4992 | |
| 4993 | r = cntNew[i-1] - 1; |
| 4994 | d = r + 1 - leafData; |
danielk1977 | 634f298 | 2005-03-28 08:44:07 +0000 | [diff] [blame] | 4995 | assert( d<nMaxCells ); |
| 4996 | assert( r<nMaxCells ); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4997 | while( szRight==0 || szRight+szCell[d]+2<=szLeft-(szCell[r]+2) ){ |
| 4998 | szRight += szCell[d] + 2; |
| 4999 | szLeft -= szCell[r] + 2; |
drh | 6019e16 | 2001-07-02 17:51:45 +0000 | [diff] [blame] | 5000 | cntNew[i-1]--; |
drh | 96f5b76 | 2004-05-16 16:24:36 +0000 | [diff] [blame] | 5001 | r = cntNew[i-1] - 1; |
| 5002 | d = r + 1 - leafData; |
drh | 6019e16 | 2001-07-02 17:51:45 +0000 | [diff] [blame] | 5003 | } |
drh | 96f5b76 | 2004-05-16 16:24:36 +0000 | [diff] [blame] | 5004 | szNew[i] = szRight; |
| 5005 | szNew[i-1] = szLeft; |
drh | 6019e16 | 2001-07-02 17:51:45 +0000 | [diff] [blame] | 5006 | } |
drh | 09d0deb | 2005-08-02 17:13:09 +0000 | [diff] [blame] | 5007 | |
| 5008 | /* Either we found one or more cells (cntnew[0])>0) or we are the |
| 5009 | ** a virtual root page. A virtual root page is when the real root |
| 5010 | ** page is page 1 and we are the only child of that page. |
| 5011 | */ |
| 5012 | assert( cntNew[0]>0 || (pParent->pgno==1 && pParent->nCell==0) ); |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 5013 | |
| 5014 | /* |
drh | 6b30867 | 2002-07-08 02:16:37 +0000 | [diff] [blame] | 5015 | ** Allocate k new pages. Reuse old pages where possible. |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 5016 | */ |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 5017 | assert( pPage->pgno>1 ); |
| 5018 | pageFlags = pPage->aData[0]; |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 5019 | for(i=0; i<k; i++){ |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 5020 | MemPage *pNew; |
drh | 6b30867 | 2002-07-08 02:16:37 +0000 | [diff] [blame] | 5021 | if( i<nOld ){ |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 5022 | pNew = apNew[i] = apOld[i]; |
drh | 6b30867 | 2002-07-08 02:16:37 +0000 | [diff] [blame] | 5023 | pgnoNew[i] = pgnoOld[i]; |
| 5024 | apOld[i] = 0; |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 5025 | rc = sqlite3PagerWrite(pNew->pDbPage); |
drh | f534544 | 2007-04-09 12:45:02 +0000 | [diff] [blame] | 5026 | nNew++; |
danielk1977 | 2812956 | 2005-01-11 10:25:06 +0000 | [diff] [blame] | 5027 | if( rc ) goto balance_cleanup; |
drh | 6b30867 | 2002-07-08 02:16:37 +0000 | [diff] [blame] | 5028 | }else{ |
drh | 7aa8f85 | 2006-03-28 00:24:44 +0000 | [diff] [blame] | 5029 | assert( i>0 ); |
drh | 4f0c587 | 2007-03-26 22:05:01 +0000 | [diff] [blame] | 5030 | rc = allocateBtreePage(pBt, &pNew, &pgnoNew[i], pgnoNew[i-1], 0); |
drh | 6b30867 | 2002-07-08 02:16:37 +0000 | [diff] [blame] | 5031 | if( rc ) goto balance_cleanup; |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 5032 | apNew[i] = pNew; |
drh | f534544 | 2007-04-09 12:45:02 +0000 | [diff] [blame] | 5033 | nNew++; |
drh | 6b30867 | 2002-07-08 02:16:37 +0000 | [diff] [blame] | 5034 | } |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 5035 | zeroPage(pNew, pageFlags); |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 5036 | } |
| 5037 | |
danielk1977 | 299b187 | 2004-11-22 10:02:10 +0000 | [diff] [blame] | 5038 | /* Free any old pages that were not reused as new pages. |
| 5039 | */ |
| 5040 | while( i<nOld ){ |
| 5041 | rc = freePage(apOld[i]); |
| 5042 | if( rc ) goto balance_cleanup; |
| 5043 | releasePage(apOld[i]); |
| 5044 | apOld[i] = 0; |
| 5045 | i++; |
| 5046 | } |
| 5047 | |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 5048 | /* |
drh | f9ffac9 | 2002-03-02 19:00:31 +0000 | [diff] [blame] | 5049 | ** Put the new pages in accending order. This helps to |
| 5050 | ** keep entries in the disk file in order so that a scan |
| 5051 | ** of the table is a linear scan through the file. That |
| 5052 | ** in turn helps the operating system to deliver pages |
| 5053 | ** from the disk more rapidly. |
| 5054 | ** |
| 5055 | ** An O(n^2) insertion sort algorithm is used, but since |
drh | c3b7057 | 2003-01-04 19:44:07 +0000 | [diff] [blame] | 5056 | ** n is never more than NB (a small constant), that should |
| 5057 | ** not be a problem. |
drh | f9ffac9 | 2002-03-02 19:00:31 +0000 | [diff] [blame] | 5058 | ** |
drh | c3b7057 | 2003-01-04 19:44:07 +0000 | [diff] [blame] | 5059 | ** When NB==3, this one optimization makes the database |
| 5060 | ** about 25% faster for large insertions and deletions. |
drh | f9ffac9 | 2002-03-02 19:00:31 +0000 | [diff] [blame] | 5061 | */ |
| 5062 | for(i=0; i<k-1; i++){ |
| 5063 | int minV = pgnoNew[i]; |
| 5064 | int minI = i; |
| 5065 | for(j=i+1; j<k; j++){ |
drh | 7d02cb7 | 2003-06-04 16:24:39 +0000 | [diff] [blame] | 5066 | if( pgnoNew[j]<(unsigned)minV ){ |
drh | f9ffac9 | 2002-03-02 19:00:31 +0000 | [diff] [blame] | 5067 | minI = j; |
| 5068 | minV = pgnoNew[j]; |
| 5069 | } |
| 5070 | } |
| 5071 | if( minI>i ){ |
| 5072 | int t; |
| 5073 | MemPage *pT; |
| 5074 | t = pgnoNew[i]; |
| 5075 | pT = apNew[i]; |
| 5076 | pgnoNew[i] = pgnoNew[minI]; |
| 5077 | apNew[i] = apNew[minI]; |
| 5078 | pgnoNew[minI] = t; |
| 5079 | apNew[minI] = pT; |
| 5080 | } |
| 5081 | } |
drh | a2fce64 | 2004-06-05 00:01:44 +0000 | [diff] [blame] | 5082 | 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] | 5083 | pgnoOld[0], |
| 5084 | nOld>=2 ? pgnoOld[1] : 0, |
| 5085 | nOld>=3 ? pgnoOld[2] : 0, |
drh | 10c0fa6 | 2004-05-18 12:50:17 +0000 | [diff] [blame] | 5086 | pgnoNew[0], szNew[0], |
| 5087 | nNew>=2 ? pgnoNew[1] : 0, nNew>=2 ? szNew[1] : 0, |
| 5088 | nNew>=3 ? pgnoNew[2] : 0, nNew>=3 ? szNew[2] : 0, |
drh | a2fce64 | 2004-06-05 00:01:44 +0000 | [diff] [blame] | 5089 | nNew>=4 ? pgnoNew[3] : 0, nNew>=4 ? szNew[3] : 0, |
| 5090 | nNew>=5 ? pgnoNew[4] : 0, nNew>=5 ? szNew[4] : 0)); |
drh | 24cd67e | 2004-05-10 16:18:47 +0000 | [diff] [blame] | 5091 | |
drh | f9ffac9 | 2002-03-02 19:00:31 +0000 | [diff] [blame] | 5092 | /* |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 5093 | ** Evenly distribute the data in apCell[] across the new pages. |
| 5094 | ** Insert divider cells into pParent as necessary. |
| 5095 | */ |
| 5096 | j = 0; |
| 5097 | for(i=0; i<nNew; i++){ |
danielk1977 | ac11ee6 | 2005-01-15 12:45:51 +0000 | [diff] [blame] | 5098 | /* Assemble the new sibling page. */ |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 5099 | MemPage *pNew = apNew[i]; |
drh | 19642e5 | 2005-03-29 13:17:45 +0000 | [diff] [blame] | 5100 | assert( j<nMaxCells ); |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 5101 | assert( pNew->pgno==pgnoNew[i] ); |
drh | fa1a98a | 2004-05-14 19:08:17 +0000 | [diff] [blame] | 5102 | assemblePage(pNew, cntNew[i]-j, &apCell[j], &szCell[j]); |
drh | 09d0deb | 2005-08-02 17:13:09 +0000 | [diff] [blame] | 5103 | assert( pNew->nCell>0 || (nNew==1 && cntNew[0]==0) ); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5104 | assert( pNew->nOverflow==0 ); |
danielk1977 | ac11ee6 | 2005-01-15 12:45:51 +0000 | [diff] [blame] | 5105 | |
| 5106 | #ifndef SQLITE_OMIT_AUTOVACUUM |
| 5107 | /* If this is an auto-vacuum database, update the pointer map entries |
| 5108 | ** that point to the siblings that were rearranged. These can be: left |
| 5109 | ** children of cells, the right-child of the page, or overflow pages |
| 5110 | ** pointed to by cells. |
| 5111 | */ |
| 5112 | if( pBt->autoVacuum ){ |
| 5113 | for(k=j; k<cntNew[i]; k++){ |
danielk1977 | 634f298 | 2005-03-28 08:44:07 +0000 | [diff] [blame] | 5114 | assert( k<nMaxCells ); |
danielk1977 | ac11ee6 | 2005-01-15 12:45:51 +0000 | [diff] [blame] | 5115 | if( aFrom[k]==0xFF || apCopy[aFrom[k]]->pgno!=pNew->pgno ){ |
danielk1977 | 79a40da | 2005-01-16 08:00:01 +0000 | [diff] [blame] | 5116 | rc = ptrmapPutOvfl(pNew, k-j); |
| 5117 | if( rc!=SQLITE_OK ){ |
| 5118 | goto balance_cleanup; |
danielk1977 | ac11ee6 | 2005-01-15 12:45:51 +0000 | [diff] [blame] | 5119 | } |
| 5120 | } |
| 5121 | } |
| 5122 | } |
| 5123 | #endif |
| 5124 | |
| 5125 | j = cntNew[i]; |
| 5126 | |
| 5127 | /* If the sibling page assembled above was not the right-most sibling, |
| 5128 | ** insert a divider cell into the parent page. |
| 5129 | */ |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 5130 | if( i<nNew-1 && j<nCell ){ |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 5131 | u8 *pCell; |
drh | 24cd67e | 2004-05-10 16:18:47 +0000 | [diff] [blame] | 5132 | u8 *pTemp; |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 5133 | int sz; |
danielk1977 | 634f298 | 2005-03-28 08:44:07 +0000 | [diff] [blame] | 5134 | |
| 5135 | assert( j<nMaxCells ); |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 5136 | pCell = apCell[j]; |
| 5137 | sz = szCell[j] + leafCorrection; |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 5138 | if( !pNew->leaf ){ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5139 | memcpy(&pNew->aData[8], pCell, 4); |
drh | 24cd67e | 2004-05-10 16:18:47 +0000 | [diff] [blame] | 5140 | pTemp = 0; |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 5141 | }else if( leafData ){ |
drh | fd131da | 2007-08-07 17:13:03 +0000 | [diff] [blame] | 5142 | /* If the tree is a leaf-data tree, and the siblings are leaves, |
danielk1977 | ac11ee6 | 2005-01-15 12:45:51 +0000 | [diff] [blame] | 5143 | ** then there is no divider cell in apCell[]. Instead, the divider |
| 5144 | ** cell consists of the integer key for the right-most cell of |
| 5145 | ** the sibling-page assembled above only. |
| 5146 | */ |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 5147 | CellInfo info; |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 5148 | j--; |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 5149 | sqlite3BtreeParseCellPtr(pNew, apCell[j], &info); |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 5150 | pCell = &aSpace[iSpace]; |
drh | b026e05 | 2007-05-02 01:34:31 +0000 | [diff] [blame] | 5151 | fillInCell(pParent, pCell, 0, info.nKey, 0, 0, 0, &sz); |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 5152 | iSpace += sz; |
drh | 07d183d | 2005-05-01 22:52:42 +0000 | [diff] [blame] | 5153 | assert( iSpace<=pBt->pageSize*5 ); |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 5154 | pTemp = 0; |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 5155 | }else{ |
| 5156 | pCell -= 4; |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 5157 | pTemp = &aSpace[iSpace]; |
| 5158 | iSpace += sz; |
drh | 07d183d | 2005-05-01 22:52:42 +0000 | [diff] [blame] | 5159 | assert( iSpace<=pBt->pageSize*5 ); |
danielk1977 | 4aeff62 | 2007-05-12 09:30:47 +0000 | [diff] [blame] | 5160 | /* Obscure case for non-leaf-data trees: If the cell at pCell was |
| 5161 | ** previously stored on a leaf node, and it's reported size was 4 |
| 5162 | ** bytes, then it may actually be smaller than this |
| 5163 | ** (see sqlite3BtreeParseCellPtr(), 4 bytes is the minimum size of |
| 5164 | ** any cell). But it's important to pass the correct size to |
| 5165 | ** insertCell(), so reparse the cell now. |
| 5166 | ** |
| 5167 | ** Note that this can never happen in an SQLite data file, as all |
| 5168 | ** cells are at least 4 bytes. It only happens in b-trees used |
| 5169 | ** to evaluate "IN (SELECT ...)" and similar clauses. |
| 5170 | */ |
| 5171 | if( szCell[j]==4 ){ |
| 5172 | assert(leafCorrection==4); |
| 5173 | sz = cellSizePtr(pParent, pCell); |
| 5174 | } |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 5175 | } |
danielk1977 | a3ad5e7 | 2005-01-07 08:56:44 +0000 | [diff] [blame] | 5176 | rc = insertCell(pParent, nxDiv, pCell, sz, pTemp, 4); |
danielk1977 | e80463b | 2004-11-03 03:01:16 +0000 | [diff] [blame] | 5177 | if( rc!=SQLITE_OK ) goto balance_cleanup; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5178 | put4byte(findOverflowCell(pParent,nxDiv), pNew->pgno); |
danielk1977 | ac11ee6 | 2005-01-15 12:45:51 +0000 | [diff] [blame] | 5179 | #ifndef SQLITE_OMIT_AUTOVACUUM |
| 5180 | /* If this is an auto-vacuum database, and not a leaf-data tree, |
| 5181 | ** then update the pointer map with an entry for the overflow page |
| 5182 | ** that the cell just inserted points to (if any). |
| 5183 | */ |
| 5184 | if( pBt->autoVacuum && !leafData ){ |
danielk1977 | 79a40da | 2005-01-16 08:00:01 +0000 | [diff] [blame] | 5185 | rc = ptrmapPutOvfl(pParent, nxDiv); |
| 5186 | if( rc!=SQLITE_OK ){ |
| 5187 | goto balance_cleanup; |
danielk1977 | ac11ee6 | 2005-01-15 12:45:51 +0000 | [diff] [blame] | 5188 | } |
| 5189 | } |
| 5190 | #endif |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 5191 | j++; |
| 5192 | nxDiv++; |
| 5193 | } |
| 5194 | } |
drh | 6019e16 | 2001-07-02 17:51:45 +0000 | [diff] [blame] | 5195 | assert( j==nCell ); |
drh | 7aa8f85 | 2006-03-28 00:24:44 +0000 | [diff] [blame] | 5196 | assert( nOld>0 ); |
| 5197 | assert( nNew>0 ); |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 5198 | if( (pageFlags & PTF_LEAF)==0 ){ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5199 | memcpy(&apNew[nNew-1]->aData[8], &apCopy[nOld-1]->aData[8], 4); |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 5200 | } |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5201 | if( nxDiv==pParent->nCell+pParent->nOverflow ){ |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 5202 | /* Right-most sibling is the right-most child of pParent */ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5203 | put4byte(&pParent->aData[pParent->hdrOffset+8], pgnoNew[nNew-1]); |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 5204 | }else{ |
| 5205 | /* Right-most sibling is the left child of the first entry in pParent |
| 5206 | ** past the right-most divider entry */ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5207 | put4byte(findOverflowCell(pParent, nxDiv), pgnoNew[nNew-1]); |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 5208 | } |
| 5209 | |
| 5210 | /* |
| 5211 | ** Reparent children of all cells. |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 5212 | */ |
| 5213 | for(i=0; i<nNew; i++){ |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 5214 | rc = reparentChildPages(apNew[i]); |
| 5215 | if( rc!=SQLITE_OK ) goto balance_cleanup; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 5216 | } |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 5217 | rc = reparentChildPages(pParent); |
| 5218 | if( rc!=SQLITE_OK ) goto balance_cleanup; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 5219 | |
| 5220 | /* |
drh | 3a4c141 | 2004-05-09 20:40:11 +0000 | [diff] [blame] | 5221 | ** Balance the parent page. Note that the current page (pPage) might |
danielk1977 | ac11ee6 | 2005-01-15 12:45:51 +0000 | [diff] [blame] | 5222 | ** have been added to the freelist so it might no longer be initialized. |
drh | 3a4c141 | 2004-05-09 20:40:11 +0000 | [diff] [blame] | 5223 | ** But the parent page will always be initialized. |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 5224 | */ |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 5225 | assert( pParent->isInit ); |
danielk1977 | ac245ec | 2005-01-14 13:50:11 +0000 | [diff] [blame] | 5226 | rc = balance(pParent, 0); |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 5227 | |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 5228 | /* |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 5229 | ** Cleanup before returning. |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 5230 | */ |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 5231 | balance_cleanup: |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 5232 | sqlite3_free(apCell); |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 5233 | for(i=0; i<nOld; i++){ |
drh | 9102529 | 2004-05-03 19:49:32 +0000 | [diff] [blame] | 5234 | releasePage(apOld[i]); |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 5235 | } |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 5236 | for(i=0; i<nNew; i++){ |
drh | 9102529 | 2004-05-03 19:49:32 +0000 | [diff] [blame] | 5237 | releasePage(apNew[i]); |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 5238 | } |
drh | 9102529 | 2004-05-03 19:49:32 +0000 | [diff] [blame] | 5239 | releasePage(pParent); |
drh | 3a4c141 | 2004-05-09 20:40:11 +0000 | [diff] [blame] | 5240 | TRACE(("BALANCE: finished with %d: old=%d new=%d cells=%d\n", |
| 5241 | pPage->pgno, nOld, nNew, nCell)); |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 5242 | return rc; |
| 5243 | } |
| 5244 | |
| 5245 | /* |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5246 | ** This routine is called for the root page of a btree when the root |
| 5247 | ** page contains no cells. This is an opportunity to make the tree |
| 5248 | ** shallower by one level. |
| 5249 | */ |
| 5250 | static int balance_shallower(MemPage *pPage){ |
| 5251 | MemPage *pChild; /* The only child page of pPage */ |
| 5252 | Pgno pgnoChild; /* Page number for pChild */ |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 5253 | int rc = SQLITE_OK; /* Return code from subprocedures */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 5254 | BtShared *pBt; /* The main BTree structure */ |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 5255 | int mxCellPerPage; /* Maximum number of cells per page */ |
| 5256 | u8 **apCell; /* All cells from pages being balanced */ |
| 5257 | int *szCell; /* Local size of all cells */ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5258 | |
| 5259 | assert( pPage->pParent==0 ); |
| 5260 | assert( pPage->nCell==0 ); |
drh | 2764170 | 2007-08-22 02:56:42 +0000 | [diff] [blame] | 5261 | assert( sqlite3BtreeMutexHeld(pPage->pBt->mutex) ); |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 5262 | pBt = pPage->pBt; |
| 5263 | mxCellPerPage = MX_CELL(pBt); |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 5264 | apCell = sqlite3_malloc( mxCellPerPage*(sizeof(u8*)+sizeof(int)) ); |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 5265 | if( apCell==0 ) return SQLITE_NOMEM; |
| 5266 | szCell = (int*)&apCell[mxCellPerPage]; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5267 | if( pPage->leaf ){ |
| 5268 | /* The table is completely empty */ |
| 5269 | TRACE(("BALANCE: empty table %d\n", pPage->pgno)); |
| 5270 | }else{ |
| 5271 | /* The root page is empty but has one child. Transfer the |
| 5272 | ** information from that one child into the root page if it |
| 5273 | ** will fit. This reduces the depth of the tree by one. |
| 5274 | ** |
| 5275 | ** If the root page is page 1, it has less space available than |
| 5276 | ** its child (due to the 100 byte header that occurs at the beginning |
| 5277 | ** of the database fle), so it might not be able to hold all of the |
| 5278 | ** information currently contained in the child. If this is the |
| 5279 | ** case, then do not do the transfer. Leave page 1 empty except |
| 5280 | ** for the right-pointer to the child page. The child page becomes |
| 5281 | ** the virtual root of the tree. |
| 5282 | */ |
| 5283 | pgnoChild = get4byte(&pPage->aData[pPage->hdrOffset+8]); |
| 5284 | assert( pgnoChild>0 ); |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 5285 | assert( pgnoChild<=sqlite3PagerPagecount(pPage->pBt->pPager) ); |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 5286 | rc = sqlite3BtreeGetPage(pPage->pBt, pgnoChild, &pChild, 0); |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 5287 | if( rc ) goto end_shallow_balance; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5288 | if( pPage->pgno==1 ){ |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 5289 | rc = sqlite3BtreeInitPage(pChild, pPage); |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 5290 | if( rc ) goto end_shallow_balance; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5291 | assert( pChild->nOverflow==0 ); |
| 5292 | if( pChild->nFree>=100 ){ |
| 5293 | /* The child information will fit on the root page, so do the |
| 5294 | ** copy */ |
| 5295 | int i; |
| 5296 | zeroPage(pPage, pChild->aData[0]); |
| 5297 | for(i=0; i<pChild->nCell; i++){ |
danielk1977 | 1cc5ed8 | 2007-05-16 17:28:43 +0000 | [diff] [blame] | 5298 | apCell[i] = findCell(pChild,i); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5299 | szCell[i] = cellSizePtr(pChild, apCell[i]); |
| 5300 | } |
| 5301 | assemblePage(pPage, pChild->nCell, apCell, szCell); |
danielk1977 | ae82558 | 2004-11-23 09:06:55 +0000 | [diff] [blame] | 5302 | /* Copy the right-pointer of the child to the parent. */ |
| 5303 | put4byte(&pPage->aData[pPage->hdrOffset+8], |
| 5304 | get4byte(&pChild->aData[pChild->hdrOffset+8])); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5305 | freePage(pChild); |
| 5306 | TRACE(("BALANCE: child %d transfer to page 1\n", pChild->pgno)); |
| 5307 | }else{ |
| 5308 | /* The child has more information that will fit on the root. |
| 5309 | ** The tree is already balanced. Do nothing. */ |
| 5310 | TRACE(("BALANCE: child %d will not fit on page 1\n", pChild->pgno)); |
| 5311 | } |
| 5312 | }else{ |
| 5313 | memcpy(pPage->aData, pChild->aData, pPage->pBt->usableSize); |
| 5314 | pPage->isInit = 0; |
| 5315 | pPage->pParent = 0; |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 5316 | rc = sqlite3BtreeInitPage(pPage, 0); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5317 | assert( rc==SQLITE_OK ); |
| 5318 | freePage(pChild); |
| 5319 | TRACE(("BALANCE: transfer child %d into root %d\n", |
| 5320 | pChild->pgno, pPage->pgno)); |
| 5321 | } |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 5322 | rc = reparentChildPages(pPage); |
danielk1977 | ac11ee6 | 2005-01-15 12:45:51 +0000 | [diff] [blame] | 5323 | assert( pPage->nOverflow==0 ); |
| 5324 | #ifndef SQLITE_OMIT_AUTOVACUUM |
| 5325 | if( pBt->autoVacuum ){ |
danielk1977 | aac0a38 | 2005-01-16 11:07:06 +0000 | [diff] [blame] | 5326 | int i; |
danielk1977 | ac11ee6 | 2005-01-15 12:45:51 +0000 | [diff] [blame] | 5327 | for(i=0; i<pPage->nCell; i++){ |
danielk1977 | 79a40da | 2005-01-16 08:00:01 +0000 | [diff] [blame] | 5328 | rc = ptrmapPutOvfl(pPage, i); |
| 5329 | if( rc!=SQLITE_OK ){ |
| 5330 | goto end_shallow_balance; |
danielk1977 | ac11ee6 | 2005-01-15 12:45:51 +0000 | [diff] [blame] | 5331 | } |
| 5332 | } |
| 5333 | } |
| 5334 | #endif |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5335 | releasePage(pChild); |
| 5336 | } |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 5337 | end_shallow_balance: |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 5338 | sqlite3_free(apCell); |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 5339 | return rc; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5340 | } |
| 5341 | |
| 5342 | |
| 5343 | /* |
| 5344 | ** The root page is overfull |
| 5345 | ** |
| 5346 | ** When this happens, Create a new child page and copy the |
| 5347 | ** contents of the root into the child. Then make the root |
| 5348 | ** page an empty page with rightChild pointing to the new |
| 5349 | ** child. Finally, call balance_internal() on the new child |
| 5350 | ** to cause it to split. |
| 5351 | */ |
| 5352 | static int balance_deeper(MemPage *pPage){ |
| 5353 | int rc; /* Return value from subprocedures */ |
| 5354 | MemPage *pChild; /* Pointer to a new child page */ |
| 5355 | Pgno pgnoChild; /* Page number of the new child page */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 5356 | BtShared *pBt; /* The BTree */ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5357 | int usableSize; /* Total usable size of a page */ |
| 5358 | u8 *data; /* Content of the parent page */ |
| 5359 | u8 *cdata; /* Content of the child page */ |
| 5360 | int hdr; /* Offset to page header in parent */ |
| 5361 | int brk; /* Offset to content of first cell in parent */ |
| 5362 | |
| 5363 | assert( pPage->pParent==0 ); |
| 5364 | assert( pPage->nOverflow>0 ); |
| 5365 | pBt = pPage->pBt; |
drh | 2764170 | 2007-08-22 02:56:42 +0000 | [diff] [blame] | 5366 | assert( sqlite3BtreeMutexHeld(pBt->mutex) ); |
drh | 4f0c587 | 2007-03-26 22:05:01 +0000 | [diff] [blame] | 5367 | rc = allocateBtreePage(pBt, &pChild, &pgnoChild, pPage->pgno, 0); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5368 | if( rc ) return rc; |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 5369 | assert( sqlite3PagerIswriteable(pChild->pDbPage) ); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5370 | usableSize = pBt->usableSize; |
| 5371 | data = pPage->aData; |
| 5372 | hdr = pPage->hdrOffset; |
| 5373 | brk = get2byte(&data[hdr+5]); |
| 5374 | cdata = pChild->aData; |
| 5375 | memcpy(cdata, &data[hdr], pPage->cellOffset+2*pPage->nCell-hdr); |
| 5376 | memcpy(&cdata[brk], &data[brk], usableSize-brk); |
danielk1977 | c7dc753 | 2004-11-17 10:22:03 +0000 | [diff] [blame] | 5377 | assert( pChild->isInit==0 ); |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 5378 | rc = sqlite3BtreeInitPage(pChild, pPage); |
danielk1977 | 6b456a2 | 2005-03-21 04:04:02 +0000 | [diff] [blame] | 5379 | if( rc ) goto balancedeeper_out; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5380 | memcpy(pChild->aOvfl, pPage->aOvfl, pPage->nOverflow*sizeof(pPage->aOvfl[0])); |
| 5381 | pChild->nOverflow = pPage->nOverflow; |
| 5382 | if( pChild->nOverflow ){ |
| 5383 | pChild->nFree = 0; |
| 5384 | } |
| 5385 | assert( pChild->nCell==pPage->nCell ); |
| 5386 | zeroPage(pPage, pChild->aData[0] & ~PTF_LEAF); |
| 5387 | put4byte(&pPage->aData[pPage->hdrOffset+8], pgnoChild); |
| 5388 | TRACE(("BALANCE: copy root %d into %d\n", pPage->pgno, pChild->pgno)); |
danielk1977 | 4e17d14 | 2005-01-16 09:06:33 +0000 | [diff] [blame] | 5389 | #ifndef SQLITE_OMIT_AUTOVACUUM |
danielk1977 | ac11ee6 | 2005-01-15 12:45:51 +0000 | [diff] [blame] | 5390 | if( pBt->autoVacuum ){ |
| 5391 | int i; |
| 5392 | rc = ptrmapPut(pBt, pChild->pgno, PTRMAP_BTREE, pPage->pgno); |
danielk1977 | 6b456a2 | 2005-03-21 04:04:02 +0000 | [diff] [blame] | 5393 | if( rc ) goto balancedeeper_out; |
danielk1977 | ac11ee6 | 2005-01-15 12:45:51 +0000 | [diff] [blame] | 5394 | for(i=0; i<pChild->nCell; i++){ |
danielk1977 | 79a40da | 2005-01-16 08:00:01 +0000 | [diff] [blame] | 5395 | rc = ptrmapPutOvfl(pChild, i); |
| 5396 | if( rc!=SQLITE_OK ){ |
| 5397 | return rc; |
danielk1977 | ac11ee6 | 2005-01-15 12:45:51 +0000 | [diff] [blame] | 5398 | } |
| 5399 | } |
| 5400 | } |
danielk1977 | 4e17d14 | 2005-01-16 09:06:33 +0000 | [diff] [blame] | 5401 | #endif |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5402 | rc = balance_nonroot(pChild); |
danielk1977 | 6b456a2 | 2005-03-21 04:04:02 +0000 | [diff] [blame] | 5403 | |
| 5404 | balancedeeper_out: |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5405 | releasePage(pChild); |
| 5406 | return rc; |
| 5407 | } |
| 5408 | |
| 5409 | /* |
| 5410 | ** Decide if the page pPage needs to be balanced. If balancing is |
| 5411 | ** required, call the appropriate balancing routine. |
| 5412 | */ |
danielk1977 | ac245ec | 2005-01-14 13:50:11 +0000 | [diff] [blame] | 5413 | static int balance(MemPage *pPage, int insert){ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5414 | int rc = SQLITE_OK; |
drh | 2764170 | 2007-08-22 02:56:42 +0000 | [diff] [blame] | 5415 | assert( sqlite3BtreeMutexHeld(pPage->pBt->mutex) ); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5416 | if( pPage->pParent==0 ){ |
danielk1977 | 6e465eb | 2007-08-21 13:11:00 +0000 | [diff] [blame] | 5417 | rc = sqlite3PagerWrite(pPage->pDbPage); |
| 5418 | if( rc==SQLITE_OK && pPage->nOverflow>0 ){ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5419 | rc = balance_deeper(pPage); |
| 5420 | } |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 5421 | if( rc==SQLITE_OK && pPage->nCell==0 ){ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5422 | rc = balance_shallower(pPage); |
| 5423 | } |
| 5424 | }else{ |
danielk1977 | ac245ec | 2005-01-14 13:50:11 +0000 | [diff] [blame] | 5425 | if( pPage->nOverflow>0 || |
| 5426 | (!insert && pPage->nFree>pPage->pBt->usableSize*2/3) ){ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5427 | rc = balance_nonroot(pPage); |
| 5428 | } |
| 5429 | } |
| 5430 | return rc; |
| 5431 | } |
| 5432 | |
| 5433 | /* |
drh | 8dcd7ca | 2004-08-08 19:43:29 +0000 | [diff] [blame] | 5434 | ** This routine checks all cursors that point to table pgnoRoot. |
drh | 980b1a7 | 2006-08-16 16:42:48 +0000 | [diff] [blame] | 5435 | ** If any of those cursors were opened with wrFlag==0 in a different |
| 5436 | ** database connection (a database connection that shares the pager |
| 5437 | ** cache with the current connection) and that other connection |
| 5438 | ** is not in the ReadUncommmitted state, then this routine returns |
| 5439 | ** SQLITE_LOCKED. |
danielk1977 | 299b187 | 2004-11-22 10:02:10 +0000 | [diff] [blame] | 5440 | ** |
| 5441 | ** In addition to checking for read-locks (where a read-lock |
| 5442 | ** means a cursor opened with wrFlag==0) this routine also moves |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 5443 | ** all write cursors so that they are pointing to the |
drh | 980b1a7 | 2006-08-16 16:42:48 +0000 | [diff] [blame] | 5444 | ** first Cell on the root page. This is necessary because an insert |
danielk1977 | 299b187 | 2004-11-22 10:02:10 +0000 | [diff] [blame] | 5445 | ** or delete might change the number of cells on a page or delete |
| 5446 | ** a page entirely and we do not want to leave any cursors |
| 5447 | ** pointing to non-existant pages or cells. |
drh | f74b8d9 | 2002-09-01 23:20:45 +0000 | [diff] [blame] | 5448 | */ |
drh | 980b1a7 | 2006-08-16 16:42:48 +0000 | [diff] [blame] | 5449 | static int checkReadLocks(Btree *pBtree, Pgno pgnoRoot, BtCursor *pExclude){ |
danielk1977 | 299b187 | 2004-11-22 10:02:10 +0000 | [diff] [blame] | 5450 | BtCursor *p; |
drh | 980b1a7 | 2006-08-16 16:42:48 +0000 | [diff] [blame] | 5451 | BtShared *pBt = pBtree->pBt; |
| 5452 | sqlite3 *db = pBtree->pSqlite; |
drh | 2764170 | 2007-08-22 02:56:42 +0000 | [diff] [blame] | 5453 | assert( sqlite3BtreeMutexHeld(pBt->mutex) ); |
| 5454 | assert( sqlite3BtreeMutexHeld(db->mutex) ); |
danielk1977 | 299b187 | 2004-11-22 10:02:10 +0000 | [diff] [blame] | 5455 | for(p=pBt->pCursor; p; p=p->pNext){ |
drh | 980b1a7 | 2006-08-16 16:42:48 +0000 | [diff] [blame] | 5456 | if( p==pExclude ) continue; |
| 5457 | if( p->eState!=CURSOR_VALID ) continue; |
| 5458 | if( p->pgnoRoot!=pgnoRoot ) continue; |
| 5459 | if( p->wrFlag==0 ){ |
| 5460 | sqlite3 *dbOther = p->pBtree->pSqlite; |
| 5461 | if( dbOther==0 || |
| 5462 | (dbOther!=db && (dbOther->flags & SQLITE_ReadUncommitted)==0) ){ |
| 5463 | return SQLITE_LOCKED; |
| 5464 | } |
| 5465 | }else if( p->pPage->pgno!=p->pgnoRoot ){ |
danielk1977 | 299b187 | 2004-11-22 10:02:10 +0000 | [diff] [blame] | 5466 | moveToRoot(p); |
| 5467 | } |
| 5468 | } |
drh | f74b8d9 | 2002-09-01 23:20:45 +0000 | [diff] [blame] | 5469 | return SQLITE_OK; |
| 5470 | } |
| 5471 | |
| 5472 | /* |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 5473 | ** Insert a new record into the BTree. The key is given by (pKey,nKey) |
| 5474 | ** 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] | 5475 | ** define what table the record should be inserted into. The cursor |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 5476 | ** is left pointing at a random location. |
| 5477 | ** |
| 5478 | ** For an INTKEY table, only the nKey value of the key is used. pKey is |
| 5479 | ** ignored. For a ZERODATA table, the pData and nData are both ignored. |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 5480 | */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 5481 | int sqlite3BtreeInsert( |
drh | 5c4d970 | 2001-08-20 00:33:58 +0000 | [diff] [blame] | 5482 | BtCursor *pCur, /* Insert data into the table of this cursor */ |
drh | 4a1c380 | 2004-05-12 15:15:47 +0000 | [diff] [blame] | 5483 | const void *pKey, i64 nKey, /* The key of the new record */ |
drh | e4d9081 | 2007-03-29 05:51:49 +0000 | [diff] [blame] | 5484 | const void *pData, int nData, /* The data of the new record */ |
drh | b026e05 | 2007-05-02 01:34:31 +0000 | [diff] [blame] | 5485 | int nZero, /* Number of extra 0 bytes to append to data */ |
drh | e4d9081 | 2007-03-29 05:51:49 +0000 | [diff] [blame] | 5486 | int appendBias /* True if this is likely an append */ |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 5487 | ){ |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 5488 | int rc; |
| 5489 | int loc; |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 5490 | int szNew; |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 5491 | MemPage *pPage; |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 5492 | Btree *p = pCur->pBtree; |
| 5493 | BtShared *pBt = p->pBt; |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 5494 | unsigned char *oldCell; |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 5495 | unsigned char *newCell = 0; |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 5496 | |
drh | d0679ed | 2007-08-28 22:24:34 +0000 | [diff] [blame^] | 5497 | cursorEnter(pCur); |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 5498 | if( pBt->inTransaction!=TRANS_WRITE ){ |
drh | f74b8d9 | 2002-09-01 23:20:45 +0000 | [diff] [blame] | 5499 | /* Must start a transaction before doing an insert */ |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 5500 | rc = pBt->readOnly ? SQLITE_READONLY : SQLITE_ERROR; |
drh | d0679ed | 2007-08-28 22:24:34 +0000 | [diff] [blame^] | 5501 | cursorLeave(pCur); |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 5502 | return rc; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 5503 | } |
drh | f74b8d9 | 2002-09-01 23:20:45 +0000 | [diff] [blame] | 5504 | assert( !pBt->readOnly ); |
drh | ecdc753 | 2001-09-23 02:35:53 +0000 | [diff] [blame] | 5505 | if( !pCur->wrFlag ){ |
drh | d0679ed | 2007-08-28 22:24:34 +0000 | [diff] [blame^] | 5506 | cursorLeave(pCur); |
drh | ecdc753 | 2001-09-23 02:35:53 +0000 | [diff] [blame] | 5507 | return SQLITE_PERM; /* Cursor not open for writing */ |
| 5508 | } |
drh | 980b1a7 | 2006-08-16 16:42:48 +0000 | [diff] [blame] | 5509 | if( checkReadLocks(pCur->pBtree, pCur->pgnoRoot, pCur) ){ |
drh | d0679ed | 2007-08-28 22:24:34 +0000 | [diff] [blame^] | 5510 | cursorLeave(pCur); |
drh | f74b8d9 | 2002-09-01 23:20:45 +0000 | [diff] [blame] | 5511 | return SQLITE_LOCKED; /* The table pCur points to has a read lock */ |
| 5512 | } |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 5513 | |
| 5514 | /* Save the positions of any other cursors open on this table */ |
drh | bf700f3 | 2007-03-31 02:36:44 +0000 | [diff] [blame] | 5515 | clearCursorPosition(pCur); |
danielk1977 | 2e94d4d | 2006-01-09 05:36:27 +0000 | [diff] [blame] | 5516 | if( |
danielk1977 | 2e94d4d | 2006-01-09 05:36:27 +0000 | [diff] [blame] | 5517 | SQLITE_OK!=(rc = saveAllCursors(pBt, pCur->pgnoRoot, pCur)) || |
drh | e4d9081 | 2007-03-29 05:51:49 +0000 | [diff] [blame] | 5518 | SQLITE_OK!=(rc = sqlite3BtreeMoveto(pCur, pKey, nKey, appendBias, &loc)) |
danielk1977 | 2e94d4d | 2006-01-09 05:36:27 +0000 | [diff] [blame] | 5519 | ){ |
drh | d0679ed | 2007-08-28 22:24:34 +0000 | [diff] [blame^] | 5520 | cursorLeave(pCur); |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 5521 | return rc; |
| 5522 | } |
| 5523 | |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 5524 | pPage = pCur->pPage; |
drh | 4a1c380 | 2004-05-12 15:15:47 +0000 | [diff] [blame] | 5525 | assert( pPage->intKey || nKey>=0 ); |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 5526 | assert( pPage->leaf || !pPage->leafData ); |
drh | 3a4c141 | 2004-05-09 20:40:11 +0000 | [diff] [blame] | 5527 | TRACE(("INSERT: table=%d nkey=%lld ndata=%d page=%d %s\n", |
| 5528 | pCur->pgnoRoot, nKey, nData, pPage->pgno, |
| 5529 | loc==0 ? "overwrite" : "new entry")); |
drh | 7aa128d | 2002-06-21 13:09:16 +0000 | [diff] [blame] | 5530 | assert( pPage->isInit ); |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 5531 | newCell = sqlite3_malloc( MX_CELL_SIZE(pBt) ); |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 5532 | if( newCell==0 ) return SQLITE_NOMEM; |
drh | b026e05 | 2007-05-02 01:34:31 +0000 | [diff] [blame] | 5533 | rc = fillInCell(pPage, newCell, pKey, nKey, pData, nData, nZero, &szNew); |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 5534 | if( rc ) goto end_insert; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5535 | assert( szNew==cellSizePtr(pPage, newCell) ); |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 5536 | assert( szNew<=MX_CELL_SIZE(pBt) ); |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 5537 | if( loc==0 && CURSOR_VALID==pCur->eState ){ |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 5538 | int szOld; |
| 5539 | assert( pCur->idx>=0 && pCur->idx<pPage->nCell ); |
danielk1977 | 6e465eb | 2007-08-21 13:11:00 +0000 | [diff] [blame] | 5540 | rc = sqlite3PagerWrite(pPage->pDbPage); |
| 5541 | if( rc ){ |
| 5542 | goto end_insert; |
| 5543 | } |
danielk1977 | 1cc5ed8 | 2007-05-16 17:28:43 +0000 | [diff] [blame] | 5544 | oldCell = findCell(pPage, pCur->idx); |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 5545 | if( !pPage->leaf ){ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5546 | memcpy(newCell, oldCell, 4); |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 5547 | } |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5548 | szOld = cellSizePtr(pPage, oldCell); |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 5549 | rc = clearCell(pPage, oldCell); |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 5550 | if( rc ) goto end_insert; |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 5551 | dropCell(pPage, pCur->idx, szOld); |
drh | 7c717f7 | 2001-06-24 20:39:41 +0000 | [diff] [blame] | 5552 | }else if( loc<0 && pPage->nCell>0 ){ |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 5553 | assert( pPage->leaf ); |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 5554 | pCur->idx++; |
drh | 271efa5 | 2004-05-30 19:19:05 +0000 | [diff] [blame] | 5555 | pCur->info.nSize = 0; |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 5556 | }else{ |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 5557 | assert( pPage->leaf ); |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 5558 | } |
danielk1977 | a3ad5e7 | 2005-01-07 08:56:44 +0000 | [diff] [blame] | 5559 | rc = insertCell(pPage, pCur->idx, newCell, szNew, 0, 0); |
danielk1977 | e80463b | 2004-11-03 03:01:16 +0000 | [diff] [blame] | 5560 | if( rc!=SQLITE_OK ) goto end_insert; |
danielk1977 | ac245ec | 2005-01-14 13:50:11 +0000 | [diff] [blame] | 5561 | rc = balance(pPage, 1); |
drh | 23e11ca | 2004-05-04 17:27:28 +0000 | [diff] [blame] | 5562 | /* sqlite3BtreePageDump(pCur->pBt, pCur->pgnoRoot, 1); */ |
drh | 3fc190c | 2001-09-14 03:24:23 +0000 | [diff] [blame] | 5563 | /* fflush(stdout); */ |
danielk1977 | 299b187 | 2004-11-22 10:02:10 +0000 | [diff] [blame] | 5564 | if( rc==SQLITE_OK ){ |
| 5565 | moveToRoot(pCur); |
| 5566 | } |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 5567 | end_insert: |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 5568 | sqlite3_free(newCell); |
drh | d0679ed | 2007-08-28 22:24:34 +0000 | [diff] [blame^] | 5569 | cursorLeave(pCur); |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 5570 | return rc; |
| 5571 | } |
| 5572 | |
| 5573 | /* |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 5574 | ** Delete the entry that the cursor is pointing to. The cursor |
| 5575 | ** is left pointing at a random location. |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 5576 | */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 5577 | int sqlite3BtreeDelete(BtCursor *pCur){ |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 5578 | MemPage *pPage = pCur->pPage; |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 5579 | unsigned char *pCell; |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 5580 | int rc; |
danielk1977 | cfe9a69 | 2004-06-16 12:00:29 +0000 | [diff] [blame] | 5581 | Pgno pgnoChild = 0; |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 5582 | Btree *p = pCur->pBtree; |
| 5583 | BtShared *pBt = p->pBt; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 5584 | |
drh | d0679ed | 2007-08-28 22:24:34 +0000 | [diff] [blame^] | 5585 | cursorEnter(pCur); |
drh | 7aa128d | 2002-06-21 13:09:16 +0000 | [diff] [blame] | 5586 | assert( pPage->isInit ); |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 5587 | if( pBt->inTransaction!=TRANS_WRITE ){ |
drh | f74b8d9 | 2002-09-01 23:20:45 +0000 | [diff] [blame] | 5588 | /* Must start a transaction before doing a delete */ |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 5589 | rc = pBt->readOnly ? SQLITE_READONLY : SQLITE_ERROR; |
drh | d0679ed | 2007-08-28 22:24:34 +0000 | [diff] [blame^] | 5590 | cursorLeave(pCur); |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 5591 | return rc; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 5592 | } |
drh | f74b8d9 | 2002-09-01 23:20:45 +0000 | [diff] [blame] | 5593 | assert( !pBt->readOnly ); |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 5594 | if( pCur->idx >= pPage->nCell ){ |
drh | d0679ed | 2007-08-28 22:24:34 +0000 | [diff] [blame^] | 5595 | cursorLeave(pCur); |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 5596 | return SQLITE_ERROR; /* The cursor is not pointing to anything */ |
| 5597 | } |
drh | ecdc753 | 2001-09-23 02:35:53 +0000 | [diff] [blame] | 5598 | if( !pCur->wrFlag ){ |
drh | d0679ed | 2007-08-28 22:24:34 +0000 | [diff] [blame^] | 5599 | cursorLeave(pCur); |
drh | ecdc753 | 2001-09-23 02:35:53 +0000 | [diff] [blame] | 5600 | return SQLITE_PERM; /* Did not open this cursor for writing */ |
| 5601 | } |
drh | 980b1a7 | 2006-08-16 16:42:48 +0000 | [diff] [blame] | 5602 | if( checkReadLocks(pCur->pBtree, pCur->pgnoRoot, pCur) ){ |
drh | d0679ed | 2007-08-28 22:24:34 +0000 | [diff] [blame^] | 5603 | cursorLeave(pCur); |
drh | f74b8d9 | 2002-09-01 23:20:45 +0000 | [diff] [blame] | 5604 | return SQLITE_LOCKED; /* The table pCur points to has a read lock */ |
| 5605 | } |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 5606 | |
| 5607 | /* Restore the current cursor position (a no-op if the cursor is not in |
| 5608 | ** CURSOR_REQUIRESEEK state) and save the positions of any other cursors |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 5609 | ** open on the same table. Then call sqlite3PagerWrite() on the page |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 5610 | ** that the entry will be deleted from. |
| 5611 | */ |
| 5612 | if( |
drh | bf700f3 | 2007-03-31 02:36:44 +0000 | [diff] [blame] | 5613 | (rc = restoreOrClearCursorPosition(pCur))!=0 || |
drh | d116739 | 2006-01-23 13:00:35 +0000 | [diff] [blame] | 5614 | (rc = saveAllCursors(pBt, pCur->pgnoRoot, pCur))!=0 || |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 5615 | (rc = sqlite3PagerWrite(pPage->pDbPage))!=0 |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 5616 | ){ |
drh | d0679ed | 2007-08-28 22:24:34 +0000 | [diff] [blame^] | 5617 | cursorLeave(pCur); |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 5618 | return rc; |
| 5619 | } |
danielk1977 | e6efa74 | 2004-11-10 11:55:10 +0000 | [diff] [blame] | 5620 | |
| 5621 | /* Locate the cell within it's page and leave pCell pointing to the |
| 5622 | ** data. The clearCell() call frees any overflow pages associated with the |
| 5623 | ** cell. The cell itself is still intact. |
| 5624 | */ |
danielk1977 | 1cc5ed8 | 2007-05-16 17:28:43 +0000 | [diff] [blame] | 5625 | pCell = findCell(pPage, pCur->idx); |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 5626 | if( !pPage->leaf ){ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5627 | pgnoChild = get4byte(pCell); |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 5628 | } |
danielk1977 | 2812956 | 2005-01-11 10:25:06 +0000 | [diff] [blame] | 5629 | rc = clearCell(pPage, pCell); |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 5630 | if( rc ){ |
drh | d0679ed | 2007-08-28 22:24:34 +0000 | [diff] [blame^] | 5631 | cursorLeave(pCur); |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 5632 | return rc; |
| 5633 | } |
danielk1977 | e6efa74 | 2004-11-10 11:55:10 +0000 | [diff] [blame] | 5634 | |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 5635 | if( !pPage->leaf ){ |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 5636 | /* |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 5637 | ** 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] | 5638 | ** do something we will leave a hole on an internal page. |
| 5639 | ** We have to fill the hole by moving in a cell from a leaf. The |
| 5640 | ** next Cell after the one to be deleted is guaranteed to exist and |
danielk1977 | 299b187 | 2004-11-22 10:02:10 +0000 | [diff] [blame] | 5641 | ** to be a leaf so we can use it. |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 5642 | */ |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 5643 | BtCursor leafCur; |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 5644 | unsigned char *pNext; |
drh | 02afc86 | 2006-01-20 18:10:57 +0000 | [diff] [blame] | 5645 | int szNext; /* The compiler warning is wrong: szNext is always |
| 5646 | ** initialized before use. Adding an extra initialization |
| 5647 | ** to silence the compiler slows down the code. */ |
danielk1977 | 299b187 | 2004-11-22 10:02:10 +0000 | [diff] [blame] | 5648 | int notUsed; |
danielk1977 | 6b456a2 | 2005-03-21 04:04:02 +0000 | [diff] [blame] | 5649 | unsigned char *tempCell = 0; |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 5650 | assert( !pPage->leafData ); |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 5651 | sqlite3BtreeGetTempCursor(pCur, &leafCur); |
danielk1977 | 299b187 | 2004-11-22 10:02:10 +0000 | [diff] [blame] | 5652 | rc = sqlite3BtreeNext(&leafCur, ¬Used); |
danielk1977 | 6b456a2 | 2005-03-21 04:04:02 +0000 | [diff] [blame] | 5653 | if( rc==SQLITE_OK ){ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 5654 | rc = sqlite3PagerWrite(leafCur.pPage->pDbPage); |
danielk1977 | 6b456a2 | 2005-03-21 04:04:02 +0000 | [diff] [blame] | 5655 | } |
| 5656 | if( rc==SQLITE_OK ){ |
| 5657 | TRACE(("DELETE: table=%d delete internal from %d replace from leaf %d\n", |
| 5658 | pCur->pgnoRoot, pPage->pgno, leafCur.pPage->pgno)); |
| 5659 | dropCell(pPage, pCur->idx, cellSizePtr(pPage, pCell)); |
danielk1977 | 1cc5ed8 | 2007-05-16 17:28:43 +0000 | [diff] [blame] | 5660 | pNext = findCell(leafCur.pPage, leafCur.idx); |
danielk1977 | 6b456a2 | 2005-03-21 04:04:02 +0000 | [diff] [blame] | 5661 | szNext = cellSizePtr(leafCur.pPage, pNext); |
| 5662 | assert( MX_CELL_SIZE(pBt)>=szNext+4 ); |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 5663 | tempCell = sqlite3_malloc( MX_CELL_SIZE(pBt) ); |
danielk1977 | 6b456a2 | 2005-03-21 04:04:02 +0000 | [diff] [blame] | 5664 | if( tempCell==0 ){ |
| 5665 | rc = SQLITE_NOMEM; |
| 5666 | } |
| 5667 | } |
| 5668 | if( rc==SQLITE_OK ){ |
| 5669 | rc = insertCell(pPage, pCur->idx, pNext-4, szNext+4, tempCell, 0); |
| 5670 | } |
| 5671 | if( rc==SQLITE_OK ){ |
| 5672 | put4byte(findOverflowCell(pPage, pCur->idx), pgnoChild); |
| 5673 | rc = balance(pPage, 0); |
| 5674 | } |
| 5675 | if( rc==SQLITE_OK ){ |
| 5676 | dropCell(leafCur.pPage, leafCur.idx, szNext); |
| 5677 | rc = balance(leafCur.pPage, 0); |
| 5678 | } |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 5679 | sqlite3_free(tempCell); |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 5680 | sqlite3BtreeReleaseTempCursor(&leafCur); |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 5681 | }else{ |
danielk1977 | 299b187 | 2004-11-22 10:02:10 +0000 | [diff] [blame] | 5682 | TRACE(("DELETE: table=%d delete from leaf %d\n", |
| 5683 | pCur->pgnoRoot, pPage->pgno)); |
| 5684 | dropCell(pPage, pCur->idx, cellSizePtr(pPage, pCell)); |
danielk1977 | ac245ec | 2005-01-14 13:50:11 +0000 | [diff] [blame] | 5685 | rc = balance(pPage, 0); |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 5686 | } |
danielk1977 | 6b456a2 | 2005-03-21 04:04:02 +0000 | [diff] [blame] | 5687 | if( rc==SQLITE_OK ){ |
| 5688 | moveToRoot(pCur); |
| 5689 | } |
drh | d0679ed | 2007-08-28 22:24:34 +0000 | [diff] [blame^] | 5690 | cursorLeave(pCur); |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 5691 | return rc; |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 5692 | } |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 5693 | |
| 5694 | /* |
drh | c6b52df | 2002-01-04 03:09:29 +0000 | [diff] [blame] | 5695 | ** Create a new BTree table. Write into *piTable the page |
| 5696 | ** number for the root page of the new table. |
| 5697 | ** |
drh | ab01f61 | 2004-05-22 02:55:23 +0000 | [diff] [blame] | 5698 | ** The type of type is determined by the flags parameter. Only the |
| 5699 | ** following values of flags are currently in use. Other values for |
| 5700 | ** flags might not work: |
| 5701 | ** |
| 5702 | ** BTREE_INTKEY|BTREE_LEAFDATA Used for SQL tables with rowid keys |
| 5703 | ** BTREE_ZERODATA Used for SQL indices |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 5704 | */ |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 5705 | static int btreeCreateTable(Btree *p, int *piTable, int flags){ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 5706 | BtShared *pBt = p->pBt; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 5707 | MemPage *pRoot; |
| 5708 | Pgno pgnoRoot; |
| 5709 | int rc; |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 5710 | |
drh | d0679ed | 2007-08-28 22:24:34 +0000 | [diff] [blame^] | 5711 | assert( sqlite3_mutex_held(p->pSqlite->mutex) ); |
| 5712 | assert( sqlite3_mutex_held(pBt->mutex) ); |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 5713 | if( pBt->inTransaction!=TRANS_WRITE ){ |
drh | f74b8d9 | 2002-09-01 23:20:45 +0000 | [diff] [blame] | 5714 | /* Must start a transaction first */ |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 5715 | rc = pBt->readOnly ? SQLITE_READONLY : SQLITE_ERROR; |
| 5716 | return rc; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 5717 | } |
danielk1977 | 2812956 | 2005-01-11 10:25:06 +0000 | [diff] [blame] | 5718 | assert( !pBt->readOnly ); |
danielk1977 | e6efa74 | 2004-11-10 11:55:10 +0000 | [diff] [blame] | 5719 | |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 5720 | #ifdef SQLITE_OMIT_AUTOVACUUM |
drh | 4f0c587 | 2007-03-26 22:05:01 +0000 | [diff] [blame] | 5721 | rc = allocateBtreePage(pBt, &pRoot, &pgnoRoot, 1, 0); |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 5722 | if( rc ){ |
| 5723 | return rc; |
| 5724 | } |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 5725 | #else |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 5726 | if( pBt->autoVacuum ){ |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 5727 | Pgno pgnoMove; /* Move a page here to make room for the root-page */ |
| 5728 | MemPage *pPageMove; /* The page to move to. */ |
| 5729 | |
danielk1977 | 20713f3 | 2007-05-03 11:43:33 +0000 | [diff] [blame] | 5730 | /* Creating a new table may probably require moving an existing database |
| 5731 | ** to make room for the new tables root page. In case this page turns |
| 5732 | ** out to be an overflow page, delete all overflow page-map caches |
| 5733 | ** held by open cursors. |
| 5734 | */ |
danielk1977 | 92d4d7a | 2007-05-04 12:05:56 +0000 | [diff] [blame] | 5735 | invalidateAllOverflowCache(pBt); |
danielk1977 | 20713f3 | 2007-05-03 11:43:33 +0000 | [diff] [blame] | 5736 | |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 5737 | /* Read the value of meta[3] from the database to determine where the |
| 5738 | ** root page of the new table should go. meta[3] is the largest root-page |
| 5739 | ** created so far, so the new root-page is (meta[3]+1). |
| 5740 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 5741 | rc = sqlite3BtreeGetMeta(p, 4, &pgnoRoot); |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 5742 | if( rc!=SQLITE_OK ){ |
| 5743 | return rc; |
| 5744 | } |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 5745 | pgnoRoot++; |
| 5746 | |
danielk1977 | 599fcba | 2004-11-08 07:13:13 +0000 | [diff] [blame] | 5747 | /* The new root-page may not be allocated on a pointer-map page, or the |
| 5748 | ** PENDING_BYTE page. |
| 5749 | */ |
danielk1977 | 266664d | 2006-02-10 08:24:21 +0000 | [diff] [blame] | 5750 | if( pgnoRoot==PTRMAP_PAGENO(pBt, pgnoRoot) || |
danielk1977 | 599fcba | 2004-11-08 07:13:13 +0000 | [diff] [blame] | 5751 | pgnoRoot==PENDING_BYTE_PAGE(pBt) ){ |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 5752 | pgnoRoot++; |
| 5753 | } |
| 5754 | assert( pgnoRoot>=3 ); |
| 5755 | |
| 5756 | /* Allocate a page. The page that currently resides at pgnoRoot will |
| 5757 | ** be moved to the allocated page (unless the allocated page happens |
| 5758 | ** to reside at pgnoRoot). |
| 5759 | */ |
drh | 4f0c587 | 2007-03-26 22:05:01 +0000 | [diff] [blame] | 5760 | rc = allocateBtreePage(pBt, &pPageMove, &pgnoMove, pgnoRoot, 1); |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 5761 | if( rc!=SQLITE_OK ){ |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 5762 | return rc; |
| 5763 | } |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 5764 | |
| 5765 | if( pgnoMove!=pgnoRoot ){ |
danielk1977 | f35843b | 2007-04-07 15:03:17 +0000 | [diff] [blame] | 5766 | /* pgnoRoot is the page that will be used for the root-page of |
| 5767 | ** the new table (assuming an error did not occur). But we were |
| 5768 | ** allocated pgnoMove. If required (i.e. if it was not allocated |
| 5769 | ** by extending the file), the current page at position pgnoMove |
| 5770 | ** is already journaled. |
| 5771 | */ |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 5772 | u8 eType; |
| 5773 | Pgno iPtrPage; |
| 5774 | |
| 5775 | releasePage(pPageMove); |
danielk1977 | f35843b | 2007-04-07 15:03:17 +0000 | [diff] [blame] | 5776 | |
| 5777 | /* Move the page currently at pgnoRoot to pgnoMove. */ |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 5778 | rc = sqlite3BtreeGetPage(pBt, pgnoRoot, &pRoot, 0); |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 5779 | if( rc!=SQLITE_OK ){ |
| 5780 | return rc; |
| 5781 | } |
| 5782 | rc = ptrmapGet(pBt, pgnoRoot, &eType, &iPtrPage); |
drh | ccae602 | 2005-02-26 17:31:26 +0000 | [diff] [blame] | 5783 | if( rc!=SQLITE_OK || eType==PTRMAP_ROOTPAGE || eType==PTRMAP_FREEPAGE ){ |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 5784 | releasePage(pRoot); |
| 5785 | return rc; |
| 5786 | } |
drh | ccae602 | 2005-02-26 17:31:26 +0000 | [diff] [blame] | 5787 | assert( eType!=PTRMAP_ROOTPAGE ); |
| 5788 | assert( eType!=PTRMAP_FREEPAGE ); |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 5789 | rc = sqlite3PagerWrite(pRoot->pDbPage); |
danielk1977 | 5fd057a | 2005-03-09 13:09:43 +0000 | [diff] [blame] | 5790 | if( rc!=SQLITE_OK ){ |
| 5791 | releasePage(pRoot); |
| 5792 | return rc; |
| 5793 | } |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 5794 | rc = relocatePage(pBt, pRoot, eType, iPtrPage, pgnoMove); |
| 5795 | releasePage(pRoot); |
danielk1977 | f35843b | 2007-04-07 15:03:17 +0000 | [diff] [blame] | 5796 | |
| 5797 | /* Obtain the page at pgnoRoot */ |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 5798 | if( rc!=SQLITE_OK ){ |
| 5799 | return rc; |
| 5800 | } |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 5801 | rc = sqlite3BtreeGetPage(pBt, pgnoRoot, &pRoot, 0); |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 5802 | if( rc!=SQLITE_OK ){ |
| 5803 | return rc; |
| 5804 | } |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 5805 | rc = sqlite3PagerWrite(pRoot->pDbPage); |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 5806 | if( rc!=SQLITE_OK ){ |
| 5807 | releasePage(pRoot); |
| 5808 | return rc; |
| 5809 | } |
| 5810 | }else{ |
| 5811 | pRoot = pPageMove; |
| 5812 | } |
| 5813 | |
danielk1977 | 42741be | 2005-01-08 12:42:39 +0000 | [diff] [blame] | 5814 | /* Update the pointer-map and meta-data with the new root-page number. */ |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 5815 | rc = ptrmapPut(pBt, pgnoRoot, PTRMAP_ROOTPAGE, 0); |
| 5816 | if( rc ){ |
| 5817 | releasePage(pRoot); |
| 5818 | return rc; |
| 5819 | } |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 5820 | rc = sqlite3BtreeUpdateMeta(p, 4, pgnoRoot); |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 5821 | if( rc ){ |
| 5822 | releasePage(pRoot); |
| 5823 | return rc; |
| 5824 | } |
danielk1977 | 42741be | 2005-01-08 12:42:39 +0000 | [diff] [blame] | 5825 | |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 5826 | }else{ |
drh | 4f0c587 | 2007-03-26 22:05:01 +0000 | [diff] [blame] | 5827 | rc = allocateBtreePage(pBt, &pRoot, &pgnoRoot, 1, 0); |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 5828 | if( rc ) return rc; |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 5829 | } |
| 5830 | #endif |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 5831 | assert( sqlite3PagerIswriteable(pRoot->pDbPage) ); |
drh | de64713 | 2004-05-07 17:57:49 +0000 | [diff] [blame] | 5832 | zeroPage(pRoot, flags | PTF_LEAF); |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 5833 | sqlite3PagerUnref(pRoot->pDbPage); |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 5834 | *piTable = (int)pgnoRoot; |
| 5835 | return SQLITE_OK; |
| 5836 | } |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 5837 | int sqlite3BtreeCreateTable(Btree *p, int *piTable, int flags){ |
| 5838 | int rc; |
| 5839 | sqlite3BtreeEnter(p); |
| 5840 | rc = btreeCreateTable(p, piTable, flags); |
| 5841 | sqlite3BtreeLeave(p); |
| 5842 | return rc; |
| 5843 | } |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 5844 | |
| 5845 | /* |
| 5846 | ** Erase the given database page and all its children. Return |
| 5847 | ** the page to the freelist. |
| 5848 | */ |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 5849 | static int clearDatabasePage( |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 5850 | BtShared *pBt, /* The BTree that contains the table */ |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 5851 | Pgno pgno, /* Page number to clear */ |
| 5852 | MemPage *pParent, /* Parent page. NULL for the root */ |
| 5853 | int freePageFlag /* Deallocate page if true */ |
| 5854 | ){ |
danielk1977 | 6b456a2 | 2005-03-21 04:04:02 +0000 | [diff] [blame] | 5855 | MemPage *pPage = 0; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 5856 | int rc; |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 5857 | unsigned char *pCell; |
| 5858 | int i; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 5859 | |
drh | 2764170 | 2007-08-22 02:56:42 +0000 | [diff] [blame] | 5860 | assert( sqlite3BtreeMutexHeld(pBt->mutex) ); |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 5861 | if( pgno>sqlite3PagerPagecount(pBt->pPager) ){ |
drh | 4928570 | 2005-09-17 15:20:26 +0000 | [diff] [blame] | 5862 | return SQLITE_CORRUPT_BKPT; |
danielk1977 | a1cb183 | 2005-02-12 08:59:55 +0000 | [diff] [blame] | 5863 | } |
| 5864 | |
drh | de64713 | 2004-05-07 17:57:49 +0000 | [diff] [blame] | 5865 | rc = getAndInitPage(pBt, pgno, &pPage, pParent); |
danielk1977 | 6b456a2 | 2005-03-21 04:04:02 +0000 | [diff] [blame] | 5866 | if( rc ) goto cleardatabasepage_out; |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 5867 | for(i=0; i<pPage->nCell; i++){ |
danielk1977 | 1cc5ed8 | 2007-05-16 17:28:43 +0000 | [diff] [blame] | 5868 | pCell = findCell(pPage, i); |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 5869 | if( !pPage->leaf ){ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5870 | rc = clearDatabasePage(pBt, get4byte(pCell), pPage->pParent, 1); |
danielk1977 | 6b456a2 | 2005-03-21 04:04:02 +0000 | [diff] [blame] | 5871 | if( rc ) goto cleardatabasepage_out; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 5872 | } |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 5873 | rc = clearCell(pPage, pCell); |
danielk1977 | 6b456a2 | 2005-03-21 04:04:02 +0000 | [diff] [blame] | 5874 | if( rc ) goto cleardatabasepage_out; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 5875 | } |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 5876 | if( !pPage->leaf ){ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5877 | rc = clearDatabasePage(pBt, get4byte(&pPage->aData[8]), pPage->pParent, 1); |
danielk1977 | 6b456a2 | 2005-03-21 04:04:02 +0000 | [diff] [blame] | 5878 | if( rc ) goto cleardatabasepage_out; |
drh | 2aa679f | 2001-06-25 02:11:07 +0000 | [diff] [blame] | 5879 | } |
| 5880 | if( freePageFlag ){ |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 5881 | rc = freePage(pPage); |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 5882 | }else if( (rc = sqlite3PagerWrite(pPage->pDbPage))==0 ){ |
drh | 3a4c141 | 2004-05-09 20:40:11 +0000 | [diff] [blame] | 5883 | zeroPage(pPage, pPage->aData[0] | PTF_LEAF); |
drh | 2aa679f | 2001-06-25 02:11:07 +0000 | [diff] [blame] | 5884 | } |
danielk1977 | 6b456a2 | 2005-03-21 04:04:02 +0000 | [diff] [blame] | 5885 | |
| 5886 | cleardatabasepage_out: |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 5887 | releasePage(pPage); |
drh | 2aa679f | 2001-06-25 02:11:07 +0000 | [diff] [blame] | 5888 | return rc; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 5889 | } |
| 5890 | |
| 5891 | /* |
drh | ab01f61 | 2004-05-22 02:55:23 +0000 | [diff] [blame] | 5892 | ** Delete all information from a single table in the database. iTable is |
| 5893 | ** the page number of the root of the table. After this routine returns, |
| 5894 | ** the root page is empty, but still exists. |
| 5895 | ** |
| 5896 | ** This routine will fail with SQLITE_LOCKED if there are any open |
| 5897 | ** read cursors on the table. Open write cursors are moved to the |
| 5898 | ** root of the table. |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 5899 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 5900 | int sqlite3BtreeClearTable(Btree *p, int iTable){ |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 5901 | int rc; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 5902 | BtShared *pBt = p->pBt; |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 5903 | sqlite3BtreeEnter(p); |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 5904 | if( p->inTrans!=TRANS_WRITE ){ |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 5905 | rc = pBt->readOnly ? SQLITE_READONLY : SQLITE_ERROR; |
| 5906 | }else if( (rc = checkReadLocks(p, iTable, 0))!=SQLITE_OK ){ |
| 5907 | /* nothing to do */ |
| 5908 | }else if( SQLITE_OK!=(rc = saveAllCursors(pBt, iTable, 0)) ){ |
| 5909 | /* nothing to do */ |
| 5910 | }else{ |
| 5911 | rc = clearDatabasePage(pBt, (Pgno)iTable, 0, 0); |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 5912 | } |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 5913 | sqlite3BtreeLeave(p); |
| 5914 | return rc; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 5915 | } |
| 5916 | |
| 5917 | /* |
| 5918 | ** Erase all information in a table and add the root of the table to |
| 5919 | ** the freelist. Except, the root of the principle table (the one on |
drh | ab01f61 | 2004-05-22 02:55:23 +0000 | [diff] [blame] | 5920 | ** page 1) is never added to the freelist. |
| 5921 | ** |
| 5922 | ** This routine will fail with SQLITE_LOCKED if there are any open |
| 5923 | ** cursors on the table. |
drh | 205f48e | 2004-11-05 00:43:11 +0000 | [diff] [blame] | 5924 | ** |
| 5925 | ** If AUTOVACUUM is enabled and the page at iTable is not the last |
| 5926 | ** root page in the database file, then the last root page |
| 5927 | ** in the database file is moved into the slot formerly occupied by |
| 5928 | ** iTable and that last slot formerly occupied by the last root page |
| 5929 | ** is added to the freelist instead of iTable. In this say, all |
| 5930 | ** root pages are kept at the beginning of the database file, which |
| 5931 | ** is necessary for AUTOVACUUM to work right. *piMoved is set to the |
| 5932 | ** page number that used to be the last root page in the file before |
| 5933 | ** the move. If no page gets moved, *piMoved is set to 0. |
| 5934 | ** The last root page is recorded in meta[3] and the value of |
| 5935 | ** meta[3] is updated by this procedure. |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 5936 | */ |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 5937 | static int btreeDropTable(Btree *p, int iTable, int *piMoved){ |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 5938 | int rc; |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 5939 | MemPage *pPage = 0; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 5940 | BtShared *pBt = p->pBt; |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 5941 | |
drh | 2764170 | 2007-08-22 02:56:42 +0000 | [diff] [blame] | 5942 | assert( sqlite3BtreeMutexHeld(pBt->mutex) ); |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 5943 | if( p->inTrans!=TRANS_WRITE ){ |
drh | f74b8d9 | 2002-09-01 23:20:45 +0000 | [diff] [blame] | 5944 | return pBt->readOnly ? SQLITE_READONLY : SQLITE_ERROR; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 5945 | } |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 5946 | |
danielk1977 | e6efa74 | 2004-11-10 11:55:10 +0000 | [diff] [blame] | 5947 | /* It is illegal to drop a table if any cursors are open on the |
| 5948 | ** database. This is because in auto-vacuum mode the backend may |
| 5949 | ** need to move another root-page to fill a gap left by the deleted |
| 5950 | ** root page. If an open cursor was using this page a problem would |
| 5951 | ** occur. |
| 5952 | */ |
| 5953 | if( pBt->pCursor ){ |
| 5954 | return SQLITE_LOCKED; |
drh | 5df72a5 | 2002-06-06 23:16:05 +0000 | [diff] [blame] | 5955 | } |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 5956 | |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 5957 | rc = sqlite3BtreeGetPage(pBt, (Pgno)iTable, &pPage, 0); |
drh | 2aa679f | 2001-06-25 02:11:07 +0000 | [diff] [blame] | 5958 | if( rc ) return rc; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 5959 | rc = sqlite3BtreeClearTable(p, iTable); |
danielk1977 | 6b456a2 | 2005-03-21 04:04:02 +0000 | [diff] [blame] | 5960 | if( rc ){ |
| 5961 | releasePage(pPage); |
| 5962 | return rc; |
| 5963 | } |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 5964 | |
drh | 205f48e | 2004-11-05 00:43:11 +0000 | [diff] [blame] | 5965 | *piMoved = 0; |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 5966 | |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 5967 | if( iTable>1 ){ |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 5968 | #ifdef SQLITE_OMIT_AUTOVACUUM |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 5969 | rc = freePage(pPage); |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 5970 | releasePage(pPage); |
| 5971 | #else |
| 5972 | if( pBt->autoVacuum ){ |
| 5973 | Pgno maxRootPgno; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 5974 | rc = sqlite3BtreeGetMeta(p, 4, &maxRootPgno); |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 5975 | if( rc!=SQLITE_OK ){ |
| 5976 | releasePage(pPage); |
| 5977 | return rc; |
| 5978 | } |
| 5979 | |
| 5980 | if( iTable==maxRootPgno ){ |
| 5981 | /* If the table being dropped is the table with the largest root-page |
| 5982 | ** number in the database, put the root page on the free list. |
| 5983 | */ |
| 5984 | rc = freePage(pPage); |
| 5985 | releasePage(pPage); |
| 5986 | if( rc!=SQLITE_OK ){ |
| 5987 | return rc; |
| 5988 | } |
| 5989 | }else{ |
| 5990 | /* The table being dropped does not have the largest root-page |
| 5991 | ** number in the database. So move the page that does into the |
| 5992 | ** gap left by the deleted root-page. |
| 5993 | */ |
| 5994 | MemPage *pMove; |
| 5995 | releasePage(pPage); |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 5996 | rc = sqlite3BtreeGetPage(pBt, maxRootPgno, &pMove, 0); |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 5997 | if( rc!=SQLITE_OK ){ |
| 5998 | return rc; |
| 5999 | } |
| 6000 | rc = relocatePage(pBt, pMove, PTRMAP_ROOTPAGE, 0, iTable); |
| 6001 | releasePage(pMove); |
| 6002 | if( rc!=SQLITE_OK ){ |
| 6003 | return rc; |
| 6004 | } |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 6005 | rc = sqlite3BtreeGetPage(pBt, maxRootPgno, &pMove, 0); |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 6006 | if( rc!=SQLITE_OK ){ |
| 6007 | return rc; |
| 6008 | } |
| 6009 | rc = freePage(pMove); |
| 6010 | releasePage(pMove); |
| 6011 | if( rc!=SQLITE_OK ){ |
| 6012 | return rc; |
| 6013 | } |
| 6014 | *piMoved = maxRootPgno; |
| 6015 | } |
| 6016 | |
danielk1977 | 599fcba | 2004-11-08 07:13:13 +0000 | [diff] [blame] | 6017 | /* Set the new 'max-root-page' value in the database header. This |
| 6018 | ** is the old value less one, less one more if that happens to |
| 6019 | ** be a root-page number, less one again if that is the |
| 6020 | ** PENDING_BYTE_PAGE. |
| 6021 | */ |
danielk1977 | 87a6e73 | 2004-11-05 12:58:25 +0000 | [diff] [blame] | 6022 | maxRootPgno--; |
danielk1977 | 599fcba | 2004-11-08 07:13:13 +0000 | [diff] [blame] | 6023 | if( maxRootPgno==PENDING_BYTE_PAGE(pBt) ){ |
| 6024 | maxRootPgno--; |
| 6025 | } |
danielk1977 | 266664d | 2006-02-10 08:24:21 +0000 | [diff] [blame] | 6026 | if( maxRootPgno==PTRMAP_PAGENO(pBt, maxRootPgno) ){ |
danielk1977 | 87a6e73 | 2004-11-05 12:58:25 +0000 | [diff] [blame] | 6027 | maxRootPgno--; |
| 6028 | } |
danielk1977 | 599fcba | 2004-11-08 07:13:13 +0000 | [diff] [blame] | 6029 | assert( maxRootPgno!=PENDING_BYTE_PAGE(pBt) ); |
| 6030 | |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 6031 | rc = sqlite3BtreeUpdateMeta(p, 4, maxRootPgno); |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 6032 | }else{ |
| 6033 | rc = freePage(pPage); |
| 6034 | releasePage(pPage); |
| 6035 | } |
| 6036 | #endif |
drh | 2aa679f | 2001-06-25 02:11:07 +0000 | [diff] [blame] | 6037 | }else{ |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 6038 | /* If sqlite3BtreeDropTable was called on page 1. */ |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 6039 | zeroPage(pPage, PTF_INTKEY|PTF_LEAF ); |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 6040 | releasePage(pPage); |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 6041 | } |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 6042 | return rc; |
| 6043 | } |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 6044 | int sqlite3BtreeDropTable(Btree *p, int iTable, int *piMoved){ |
| 6045 | int rc; |
| 6046 | sqlite3BtreeEnter(p); |
| 6047 | rc = btreeDropTable(p, iTable, piMoved); |
| 6048 | sqlite3BtreeLeave(p); |
| 6049 | return rc; |
| 6050 | } |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 6051 | |
drh | 001bbcb | 2003-03-19 03:14:00 +0000 | [diff] [blame] | 6052 | |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 6053 | /* |
drh | 23e11ca | 2004-05-04 17:27:28 +0000 | [diff] [blame] | 6054 | ** Read the meta-information out of a database file. Meta[0] |
| 6055 | ** is the number of free pages currently in the database. Meta[1] |
drh | a3b321d | 2004-05-11 09:31:31 +0000 | [diff] [blame] | 6056 | ** through meta[15] are available for use by higher layers. Meta[0] |
| 6057 | ** is read-only, the others are read/write. |
| 6058 | ** |
| 6059 | ** The schema layer numbers meta values differently. At the schema |
| 6060 | ** layer (and the SetCookie and ReadCookie opcodes) the number of |
| 6061 | ** 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] | 6062 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 6063 | int sqlite3BtreeGetMeta(Btree *p, int idx, u32 *pMeta){ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 6064 | DbPage *pDbPage; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 6065 | int rc; |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 6066 | unsigned char *pP1; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 6067 | BtShared *pBt = p->pBt; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 6068 | |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 6069 | sqlite3BtreeEnter(p); |
| 6070 | |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 6071 | /* Reading a meta-data value requires a read-lock on page 1 (and hence |
| 6072 | ** the sqlite_master table. We grab this lock regardless of whether or |
| 6073 | ** not the SQLITE_ReadUncommitted flag is set (the table rooted at page |
| 6074 | ** 1 is treated as a special case by queryTableLock() and lockTable()). |
| 6075 | */ |
| 6076 | rc = queryTableLock(p, 1, READ_LOCK); |
| 6077 | if( rc!=SQLITE_OK ){ |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 6078 | sqlite3BtreeLeave(p); |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 6079 | return rc; |
| 6080 | } |
| 6081 | |
drh | 23e11ca | 2004-05-04 17:27:28 +0000 | [diff] [blame] | 6082 | assert( idx>=0 && idx<=15 ); |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 6083 | rc = sqlite3PagerGet(pBt->pPager, 1, &pDbPage); |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 6084 | if( rc ){ |
| 6085 | sqlite3BtreeLeave(p); |
| 6086 | return rc; |
| 6087 | } |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 6088 | pP1 = (unsigned char *)sqlite3PagerGetData(pDbPage); |
drh | 23e11ca | 2004-05-04 17:27:28 +0000 | [diff] [blame] | 6089 | *pMeta = get4byte(&pP1[36 + idx*4]); |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 6090 | sqlite3PagerUnref(pDbPage); |
drh | ae15787 | 2004-08-14 19:20:09 +0000 | [diff] [blame] | 6091 | |
danielk1977 | 599fcba | 2004-11-08 07:13:13 +0000 | [diff] [blame] | 6092 | /* If autovacuumed is disabled in this build but we are trying to |
| 6093 | ** access an autovacuumed database, then make the database readonly. |
| 6094 | */ |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 6095 | #ifdef SQLITE_OMIT_AUTOVACUUM |
drh | ae15787 | 2004-08-14 19:20:09 +0000 | [diff] [blame] | 6096 | if( idx==4 && *pMeta>0 ) pBt->readOnly = 1; |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 6097 | #endif |
drh | ae15787 | 2004-08-14 19:20:09 +0000 | [diff] [blame] | 6098 | |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 6099 | /* Grab the read-lock on page 1. */ |
| 6100 | rc = lockTable(p, 1, READ_LOCK); |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 6101 | sqlite3BtreeLeave(p); |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 6102 | return rc; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 6103 | } |
| 6104 | |
| 6105 | /* |
drh | 23e11ca | 2004-05-04 17:27:28 +0000 | [diff] [blame] | 6106 | ** Write meta-information back into the database. Meta[0] is |
| 6107 | ** read-only and may not be written. |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 6108 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 6109 | int sqlite3BtreeUpdateMeta(Btree *p, int idx, u32 iMeta){ |
| 6110 | BtShared *pBt = p->pBt; |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 6111 | unsigned char *pP1; |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 6112 | int rc; |
drh | 23e11ca | 2004-05-04 17:27:28 +0000 | [diff] [blame] | 6113 | assert( idx>=1 && idx<=15 ); |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 6114 | sqlite3BtreeEnter(p); |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 6115 | if( p->inTrans!=TRANS_WRITE ){ |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 6116 | rc = pBt->readOnly ? SQLITE_READONLY : SQLITE_ERROR; |
| 6117 | }else{ |
| 6118 | assert( pBt->pPage1!=0 ); |
| 6119 | pP1 = pBt->pPage1->aData; |
| 6120 | rc = sqlite3PagerWrite(pBt->pPage1->pDbPage); |
| 6121 | if( rc==SQLITE_OK ){ |
| 6122 | put4byte(&pP1[36 + idx*4], iMeta); |
| 6123 | if( idx==7 ){ |
| 6124 | assert( pBt->autoVacuum || iMeta==0 ); |
| 6125 | assert( iMeta==0 || iMeta==1 ); |
| 6126 | pBt->incrVacuum = iMeta; |
| 6127 | } |
| 6128 | } |
drh | 5df72a5 | 2002-06-06 23:16:05 +0000 | [diff] [blame] | 6129 | } |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 6130 | sqlite3BtreeLeave(p); |
| 6131 | return rc; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 6132 | } |
drh | 8c42ca9 | 2001-06-22 19:15:00 +0000 | [diff] [blame] | 6133 | |
drh | f328bc8 | 2004-05-10 23:29:49 +0000 | [diff] [blame] | 6134 | /* |
| 6135 | ** Return the flag byte at the beginning of the page that the cursor |
| 6136 | ** is currently pointing to. |
| 6137 | */ |
| 6138 | int sqlite3BtreeFlags(BtCursor *pCur){ |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 6139 | /* TODO: What about CURSOR_REQUIRESEEK state? Probably need to call |
drh | 777e4c4 | 2006-01-13 04:31:58 +0000 | [diff] [blame] | 6140 | ** restoreOrClearCursorPosition() here. |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 6141 | */ |
drh | f328bc8 | 2004-05-10 23:29:49 +0000 | [diff] [blame] | 6142 | MemPage *pPage = pCur->pPage; |
drh | 2764170 | 2007-08-22 02:56:42 +0000 | [diff] [blame] | 6143 | assert( sqlite3BtreeMutexHeld(pPage->pBt->mutex) ); |
drh | d0679ed | 2007-08-28 22:24:34 +0000 | [diff] [blame^] | 6144 | assert( pPage->pBt==pCur->pBt ); |
drh | f328bc8 | 2004-05-10 23:29:49 +0000 | [diff] [blame] | 6145 | return pPage ? pPage->aData[pPage->hdrOffset] : 0; |
| 6146 | } |
| 6147 | |
drh | dd79342 | 2001-06-28 01:54:48 +0000 | [diff] [blame] | 6148 | |
drh | dd79342 | 2001-06-28 01:54:48 +0000 | [diff] [blame] | 6149 | /* |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6150 | ** Return the pager associated with a BTree. This routine is used for |
| 6151 | ** testing and debugging only. |
drh | dd79342 | 2001-06-28 01:54:48 +0000 | [diff] [blame] | 6152 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 6153 | Pager *sqlite3BtreePager(Btree *p){ |
| 6154 | return p->pBt->pPager; |
drh | dd79342 | 2001-06-28 01:54:48 +0000 | [diff] [blame] | 6155 | } |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6156 | |
drh | b7f9164 | 2004-10-31 02:22:47 +0000 | [diff] [blame] | 6157 | #ifndef SQLITE_OMIT_INTEGRITY_CHECK |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6158 | /* |
| 6159 | ** Append a message to the error message string. |
| 6160 | */ |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 6161 | static void checkAppendMsg( |
| 6162 | IntegrityCk *pCheck, |
| 6163 | char *zMsg1, |
| 6164 | const char *zFormat, |
| 6165 | ... |
| 6166 | ){ |
| 6167 | va_list ap; |
| 6168 | char *zMsg2; |
drh | 1dcdbc0 | 2007-01-27 02:24:54 +0000 | [diff] [blame] | 6169 | if( !pCheck->mxErr ) return; |
| 6170 | pCheck->mxErr--; |
| 6171 | pCheck->nErr++; |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 6172 | va_start(ap, zFormat); |
danielk1977 | 1e53695 | 2007-08-16 10:09:01 +0000 | [diff] [blame] | 6173 | zMsg2 = sqlite3VMPrintf(0, zFormat, ap); |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 6174 | va_end(ap); |
| 6175 | if( zMsg1==0 ) zMsg1 = ""; |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6176 | if( pCheck->zErrMsg ){ |
| 6177 | char *zOld = pCheck->zErrMsg; |
| 6178 | pCheck->zErrMsg = 0; |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 6179 | sqlite3SetString(&pCheck->zErrMsg, zOld, "\n", zMsg1, zMsg2, (char*)0); |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 6180 | sqlite3_free(zOld); |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6181 | }else{ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 6182 | sqlite3SetString(&pCheck->zErrMsg, zMsg1, zMsg2, (char*)0); |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6183 | } |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 6184 | sqlite3_free(zMsg2); |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6185 | } |
drh | b7f9164 | 2004-10-31 02:22:47 +0000 | [diff] [blame] | 6186 | #endif /* SQLITE_OMIT_INTEGRITY_CHECK */ |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6187 | |
drh | b7f9164 | 2004-10-31 02:22:47 +0000 | [diff] [blame] | 6188 | #ifndef SQLITE_OMIT_INTEGRITY_CHECK |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6189 | /* |
| 6190 | ** Add 1 to the reference count for page iPage. If this is the second |
| 6191 | ** reference to the page, add an error message to pCheck->zErrMsg. |
| 6192 | ** Return 1 if there are 2 ore more references to the page and 0 if |
| 6193 | ** if this is the first reference to the page. |
| 6194 | ** |
| 6195 | ** Also check that the page number is in bounds. |
| 6196 | */ |
drh | aaab572 | 2002-02-19 13:39:21 +0000 | [diff] [blame] | 6197 | static int checkRef(IntegrityCk *pCheck, int iPage, char *zContext){ |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6198 | if( iPage==0 ) return 1; |
drh | 0de8c11 | 2002-07-06 16:32:14 +0000 | [diff] [blame] | 6199 | if( iPage>pCheck->nPage || iPage<0 ){ |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 6200 | checkAppendMsg(pCheck, zContext, "invalid page number %d", iPage); |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6201 | return 1; |
| 6202 | } |
| 6203 | if( pCheck->anRef[iPage]==1 ){ |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 6204 | checkAppendMsg(pCheck, zContext, "2nd reference to page %d", iPage); |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6205 | return 1; |
| 6206 | } |
| 6207 | return (pCheck->anRef[iPage]++)>1; |
| 6208 | } |
| 6209 | |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 6210 | #ifndef SQLITE_OMIT_AUTOVACUUM |
| 6211 | /* |
| 6212 | ** Check that the entry in the pointer-map for page iChild maps to |
| 6213 | ** page iParent, pointer type ptrType. If not, append an error message |
| 6214 | ** to pCheck. |
| 6215 | */ |
| 6216 | static void checkPtrmap( |
| 6217 | IntegrityCk *pCheck, /* Integrity check context */ |
| 6218 | Pgno iChild, /* Child page number */ |
| 6219 | u8 eType, /* Expected pointer map type */ |
| 6220 | Pgno iParent, /* Expected pointer map parent page number */ |
| 6221 | char *zContext /* Context description (used for error msg) */ |
| 6222 | ){ |
| 6223 | int rc; |
| 6224 | u8 ePtrmapType; |
| 6225 | Pgno iPtrmapParent; |
| 6226 | |
| 6227 | rc = ptrmapGet(pCheck->pBt, iChild, &ePtrmapType, &iPtrmapParent); |
| 6228 | if( rc!=SQLITE_OK ){ |
| 6229 | checkAppendMsg(pCheck, zContext, "Failed to read ptrmap key=%d", iChild); |
| 6230 | return; |
| 6231 | } |
| 6232 | |
| 6233 | if( ePtrmapType!=eType || iPtrmapParent!=iParent ){ |
| 6234 | checkAppendMsg(pCheck, zContext, |
| 6235 | "Bad ptr map entry key=%d expected=(%d,%d) got=(%d,%d)", |
| 6236 | iChild, eType, iParent, ePtrmapType, iPtrmapParent); |
| 6237 | } |
| 6238 | } |
| 6239 | #endif |
| 6240 | |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6241 | /* |
| 6242 | ** Check the integrity of the freelist or of an overflow page list. |
| 6243 | ** Verify that the number of pages on the list is N. |
| 6244 | */ |
drh | 30e5875 | 2002-03-02 20:41:57 +0000 | [diff] [blame] | 6245 | static void checkList( |
| 6246 | IntegrityCk *pCheck, /* Integrity checking context */ |
| 6247 | int isFreeList, /* True for a freelist. False for overflow page list */ |
| 6248 | int iPage, /* Page number for first page in the list */ |
| 6249 | int N, /* Expected number of pages in the list */ |
| 6250 | char *zContext /* Context for error messages */ |
| 6251 | ){ |
| 6252 | int i; |
drh | 3a4c141 | 2004-05-09 20:40:11 +0000 | [diff] [blame] | 6253 | int expected = N; |
| 6254 | int iFirst = iPage; |
drh | 1dcdbc0 | 2007-01-27 02:24:54 +0000 | [diff] [blame] | 6255 | while( N-- > 0 && pCheck->mxErr ){ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 6256 | DbPage *pOvflPage; |
| 6257 | unsigned char *pOvflData; |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6258 | if( iPage<1 ){ |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 6259 | checkAppendMsg(pCheck, zContext, |
| 6260 | "%d of %d pages missing from overflow list starting at %d", |
drh | 3a4c141 | 2004-05-09 20:40:11 +0000 | [diff] [blame] | 6261 | N+1, expected, iFirst); |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6262 | break; |
| 6263 | } |
| 6264 | if( checkRef(pCheck, iPage, zContext) ) break; |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 6265 | if( sqlite3PagerGet(pCheck->pPager, (Pgno)iPage, &pOvflPage) ){ |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 6266 | checkAppendMsg(pCheck, zContext, "failed to get page %d", iPage); |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6267 | break; |
| 6268 | } |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 6269 | pOvflData = (unsigned char *)sqlite3PagerGetData(pOvflPage); |
drh | 30e5875 | 2002-03-02 20:41:57 +0000 | [diff] [blame] | 6270 | if( isFreeList ){ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 6271 | int n = get4byte(&pOvflData[4]); |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 6272 | #ifndef SQLITE_OMIT_AUTOVACUUM |
| 6273 | if( pCheck->pBt->autoVacuum ){ |
| 6274 | checkPtrmap(pCheck, iPage, PTRMAP_FREEPAGE, 0, zContext); |
| 6275 | } |
| 6276 | #endif |
drh | 855eb1c | 2004-08-31 13:45:11 +0000 | [diff] [blame] | 6277 | if( n>pCheck->pBt->usableSize/4-8 ){ |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 6278 | checkAppendMsg(pCheck, zContext, |
| 6279 | "freelist leaf count too big on page %d", iPage); |
drh | ee696e2 | 2004-08-30 16:52:17 +0000 | [diff] [blame] | 6280 | N--; |
| 6281 | }else{ |
| 6282 | for(i=0; i<n; i++){ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 6283 | Pgno iFreePage = get4byte(&pOvflData[8+i*4]); |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 6284 | #ifndef SQLITE_OMIT_AUTOVACUUM |
| 6285 | if( pCheck->pBt->autoVacuum ){ |
| 6286 | checkPtrmap(pCheck, iFreePage, PTRMAP_FREEPAGE, 0, zContext); |
| 6287 | } |
| 6288 | #endif |
| 6289 | checkRef(pCheck, iFreePage, zContext); |
drh | ee696e2 | 2004-08-30 16:52:17 +0000 | [diff] [blame] | 6290 | } |
| 6291 | N -= n; |
drh | 30e5875 | 2002-03-02 20:41:57 +0000 | [diff] [blame] | 6292 | } |
drh | 30e5875 | 2002-03-02 20:41:57 +0000 | [diff] [blame] | 6293 | } |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 6294 | #ifndef SQLITE_OMIT_AUTOVACUUM |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 6295 | else{ |
| 6296 | /* If this database supports auto-vacuum and iPage is not the last |
| 6297 | ** page in this overflow list, check that the pointer-map entry for |
| 6298 | ** the following page matches iPage. |
| 6299 | */ |
| 6300 | if( pCheck->pBt->autoVacuum && N>0 ){ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 6301 | i = get4byte(pOvflData); |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 6302 | checkPtrmap(pCheck, i, PTRMAP_OVERFLOW2, iPage, zContext); |
| 6303 | } |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 6304 | } |
| 6305 | #endif |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 6306 | iPage = get4byte(pOvflData); |
| 6307 | sqlite3PagerUnref(pOvflPage); |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6308 | } |
| 6309 | } |
drh | b7f9164 | 2004-10-31 02:22:47 +0000 | [diff] [blame] | 6310 | #endif /* SQLITE_OMIT_INTEGRITY_CHECK */ |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6311 | |
drh | b7f9164 | 2004-10-31 02:22:47 +0000 | [diff] [blame] | 6312 | #ifndef SQLITE_OMIT_INTEGRITY_CHECK |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6313 | /* |
| 6314 | ** Do various sanity checks on a single page of a tree. Return |
| 6315 | ** the tree depth. Root pages return 0. Parents of root pages |
| 6316 | ** return 1, and so forth. |
| 6317 | ** |
| 6318 | ** These checks are done: |
| 6319 | ** |
| 6320 | ** 1. Make sure that cells and freeblocks do not overlap |
| 6321 | ** but combine to completely cover the page. |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 6322 | ** NO 2. Make sure cell keys are in order. |
| 6323 | ** NO 3. Make sure no key is less than or equal to zLowerBound. |
| 6324 | ** NO 4. Make sure no key is greater than or equal to zUpperBound. |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6325 | ** 5. Check the integrity of overflow pages. |
| 6326 | ** 6. Recursively call checkTreePage on all children. |
| 6327 | ** 7. Verify that the depth of all children is the same. |
drh | 6019e16 | 2001-07-02 17:51:45 +0000 | [diff] [blame] | 6328 | ** 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] | 6329 | ** the root of the tree. |
| 6330 | */ |
| 6331 | static int checkTreePage( |
drh | aaab572 | 2002-02-19 13:39:21 +0000 | [diff] [blame] | 6332 | IntegrityCk *pCheck, /* Context for the sanity check */ |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6333 | int iPage, /* Page number of the page to check */ |
| 6334 | MemPage *pParent, /* Parent page */ |
drh | 7416170 | 2006-02-24 02:53:49 +0000 | [diff] [blame] | 6335 | char *zParentContext /* Parent context */ |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6336 | ){ |
| 6337 | MemPage *pPage; |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 6338 | int i, rc, depth, d2, pgno, cnt; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 6339 | int hdr, cellStart; |
| 6340 | int nCell; |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 6341 | u8 *data; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 6342 | BtShared *pBt; |
drh | 4f26bb6 | 2005-09-08 14:17:20 +0000 | [diff] [blame] | 6343 | int usableSize; |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6344 | char zContext[100]; |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 6345 | char *hit; |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6346 | |
drh | 5bb3eb9 | 2007-05-04 13:15:55 +0000 | [diff] [blame] | 6347 | sqlite3_snprintf(sizeof(zContext), zContext, "Page %d: ", iPage); |
danielk1977 | ef73ee9 | 2004-11-06 12:26:07 +0000 | [diff] [blame] | 6348 | |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6349 | /* Check that the page exists |
| 6350 | */ |
drh | d9cb6ac | 2005-10-20 07:28:17 +0000 | [diff] [blame] | 6351 | pBt = pCheck->pBt; |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 6352 | usableSize = pBt->usableSize; |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6353 | if( iPage==0 ) return 0; |
| 6354 | if( checkRef(pCheck, iPage, zParentContext) ) return 0; |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 6355 | if( (rc = sqlite3BtreeGetPage(pBt, (Pgno)iPage, &pPage, 0))!=0 ){ |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 6356 | checkAppendMsg(pCheck, zContext, |
| 6357 | "unable to get the page. error code=%d", rc); |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6358 | return 0; |
| 6359 | } |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 6360 | if( (rc = sqlite3BtreeInitPage(pPage, pParent))!=0 ){ |
| 6361 | checkAppendMsg(pCheck, zContext, |
| 6362 | "sqlite3BtreeInitPage() returns error code %d", rc); |
drh | 9102529 | 2004-05-03 19:49:32 +0000 | [diff] [blame] | 6363 | releasePage(pPage); |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6364 | return 0; |
| 6365 | } |
| 6366 | |
| 6367 | /* Check out all the cells. |
| 6368 | */ |
| 6369 | depth = 0; |
drh | 1dcdbc0 | 2007-01-27 02:24:54 +0000 | [diff] [blame] | 6370 | for(i=0; i<pPage->nCell && pCheck->mxErr; i++){ |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 6371 | u8 *pCell; |
| 6372 | int sz; |
| 6373 | CellInfo info; |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6374 | |
| 6375 | /* Check payload overflow pages |
| 6376 | */ |
drh | 5bb3eb9 | 2007-05-04 13:15:55 +0000 | [diff] [blame] | 6377 | sqlite3_snprintf(sizeof(zContext), zContext, |
| 6378 | "On tree page %d cell %d: ", iPage, i); |
danielk1977 | 1cc5ed8 | 2007-05-16 17:28:43 +0000 | [diff] [blame] | 6379 | pCell = findCell(pPage,i); |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 6380 | sqlite3BtreeParseCellPtr(pPage, pCell, &info); |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 6381 | sz = info.nData; |
| 6382 | if( !pPage->intKey ) sz += info.nKey; |
drh | 7236583 | 2007-03-06 15:53:44 +0000 | [diff] [blame] | 6383 | assert( sz==info.nPayload ); |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 6384 | if( sz>info.nLocal ){ |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 6385 | int nPage = (sz - info.nLocal + usableSize - 5)/(usableSize - 4); |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 6386 | Pgno pgnoOvfl = get4byte(&pCell[info.iOverflow]); |
| 6387 | #ifndef SQLITE_OMIT_AUTOVACUUM |
| 6388 | if( pBt->autoVacuum ){ |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 6389 | checkPtrmap(pCheck, pgnoOvfl, PTRMAP_OVERFLOW1, iPage, zContext); |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 6390 | } |
| 6391 | #endif |
| 6392 | checkList(pCheck, 0, pgnoOvfl, nPage, zContext); |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6393 | } |
| 6394 | |
| 6395 | /* Check sanity of left child page. |
| 6396 | */ |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 6397 | if( !pPage->leaf ){ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 6398 | pgno = get4byte(pCell); |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 6399 | #ifndef SQLITE_OMIT_AUTOVACUUM |
| 6400 | if( pBt->autoVacuum ){ |
| 6401 | checkPtrmap(pCheck, pgno, PTRMAP_BTREE, iPage, zContext); |
| 6402 | } |
| 6403 | #endif |
drh | 7416170 | 2006-02-24 02:53:49 +0000 | [diff] [blame] | 6404 | d2 = checkTreePage(pCheck,pgno,pPage,zContext); |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 6405 | if( i>0 && d2!=depth ){ |
| 6406 | checkAppendMsg(pCheck, zContext, "Child page depth differs"); |
| 6407 | } |
| 6408 | depth = d2; |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6409 | } |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6410 | } |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 6411 | if( !pPage->leaf ){ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 6412 | pgno = get4byte(&pPage->aData[pPage->hdrOffset+8]); |
drh | 5bb3eb9 | 2007-05-04 13:15:55 +0000 | [diff] [blame] | 6413 | sqlite3_snprintf(sizeof(zContext), zContext, |
| 6414 | "On page %d at right child: ", iPage); |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 6415 | #ifndef SQLITE_OMIT_AUTOVACUUM |
| 6416 | if( pBt->autoVacuum ){ |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 6417 | checkPtrmap(pCheck, pgno, PTRMAP_BTREE, iPage, 0); |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 6418 | } |
| 6419 | #endif |
drh | 7416170 | 2006-02-24 02:53:49 +0000 | [diff] [blame] | 6420 | checkTreePage(pCheck, pgno, pPage, zContext); |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 6421 | } |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6422 | |
| 6423 | /* Check for complete coverage of the page |
| 6424 | */ |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 6425 | data = pPage->aData; |
| 6426 | hdr = pPage->hdrOffset; |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 6427 | hit = sqlite3MallocZero( usableSize ); |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 6428 | if( hit ){ |
| 6429 | memset(hit, 1, get2byte(&data[hdr+5])); |
| 6430 | nCell = get2byte(&data[hdr+3]); |
| 6431 | cellStart = hdr + 12 - 4*pPage->leaf; |
| 6432 | for(i=0; i<nCell; i++){ |
| 6433 | int pc = get2byte(&data[cellStart+i*2]); |
| 6434 | int size = cellSizePtr(pPage, &data[pc]); |
| 6435 | int j; |
danielk1977 | 7701e81 | 2005-01-10 12:59:51 +0000 | [diff] [blame] | 6436 | if( (pc+size-1)>=usableSize || pc<0 ){ |
| 6437 | checkAppendMsg(pCheck, 0, |
| 6438 | "Corruption detected in cell %d on page %d",i,iPage,0); |
| 6439 | }else{ |
| 6440 | for(j=pc+size-1; j>=pc; j--) hit[j]++; |
| 6441 | } |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 6442 | } |
| 6443 | for(cnt=0, i=get2byte(&data[hdr+1]); i>0 && i<usableSize && cnt<10000; |
| 6444 | cnt++){ |
| 6445 | int size = get2byte(&data[i+2]); |
| 6446 | int j; |
danielk1977 | 7701e81 | 2005-01-10 12:59:51 +0000 | [diff] [blame] | 6447 | if( (i+size-1)>=usableSize || i<0 ){ |
| 6448 | checkAppendMsg(pCheck, 0, |
| 6449 | "Corruption detected in cell %d on page %d",i,iPage,0); |
| 6450 | }else{ |
| 6451 | for(j=i+size-1; j>=i; j--) hit[j]++; |
| 6452 | } |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 6453 | i = get2byte(&data[i]); |
| 6454 | } |
| 6455 | for(i=cnt=0; i<usableSize; i++){ |
| 6456 | if( hit[i]==0 ){ |
| 6457 | cnt++; |
| 6458 | }else if( hit[i]>1 ){ |
| 6459 | checkAppendMsg(pCheck, 0, |
| 6460 | "Multiple uses for byte %d of page %d", i, iPage); |
| 6461 | break; |
| 6462 | } |
| 6463 | } |
| 6464 | if( cnt!=data[hdr+7] ){ |
| 6465 | checkAppendMsg(pCheck, 0, |
| 6466 | "Fragmented space is %d byte reported as %d on page %d", |
| 6467 | cnt, data[hdr+7], iPage); |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6468 | } |
| 6469 | } |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 6470 | sqlite3_free(hit); |
drh | 6019e16 | 2001-07-02 17:51:45 +0000 | [diff] [blame] | 6471 | |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 6472 | releasePage(pPage); |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 6473 | return depth+1; |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6474 | } |
drh | b7f9164 | 2004-10-31 02:22:47 +0000 | [diff] [blame] | 6475 | #endif /* SQLITE_OMIT_INTEGRITY_CHECK */ |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6476 | |
drh | b7f9164 | 2004-10-31 02:22:47 +0000 | [diff] [blame] | 6477 | #ifndef SQLITE_OMIT_INTEGRITY_CHECK |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6478 | /* |
| 6479 | ** This routine does a complete check of the given BTree file. aRoot[] is |
| 6480 | ** an array of pages numbers were each page number is the root page of |
| 6481 | ** a table. nRoot is the number of entries in aRoot. |
| 6482 | ** |
| 6483 | ** If everything checks out, this routine returns NULL. If something is |
| 6484 | ** amiss, an error message is written into memory obtained from malloc() |
| 6485 | ** and a pointer to that error message is returned. The calling function |
| 6486 | ** is responsible for freeing the error message when it is done. |
| 6487 | */ |
drh | 1dcdbc0 | 2007-01-27 02:24:54 +0000 | [diff] [blame] | 6488 | char *sqlite3BtreeIntegrityCheck( |
| 6489 | Btree *p, /* The btree to be checked */ |
| 6490 | int *aRoot, /* An array of root pages numbers for individual trees */ |
| 6491 | int nRoot, /* Number of entries in aRoot[] */ |
| 6492 | int mxErr, /* Stop reporting errors after this many */ |
| 6493 | int *pnErr /* Write number of errors seen to this variable */ |
| 6494 | ){ |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6495 | int i; |
| 6496 | int nRef; |
drh | aaab572 | 2002-02-19 13:39:21 +0000 | [diff] [blame] | 6497 | IntegrityCk sCheck; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 6498 | BtShared *pBt = p->pBt; |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6499 | |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 6500 | sqlite3BtreeEnter(p); |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 6501 | nRef = sqlite3PagerRefcount(pBt->pPager); |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 6502 | if( lockBtreeWithRetry(p)!=SQLITE_OK ){ |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 6503 | sqlite3BtreeLeave(p); |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 6504 | return sqlite3StrDup("Unable to acquire a read lock on the database"); |
drh | efc251d | 2001-07-01 22:12:01 +0000 | [diff] [blame] | 6505 | } |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6506 | sCheck.pBt = pBt; |
| 6507 | sCheck.pPager = pBt->pPager; |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 6508 | sCheck.nPage = sqlite3PagerPagecount(sCheck.pPager); |
drh | 1dcdbc0 | 2007-01-27 02:24:54 +0000 | [diff] [blame] | 6509 | sCheck.mxErr = mxErr; |
| 6510 | sCheck.nErr = 0; |
| 6511 | *pnErr = 0; |
danielk1977 | e5321f0 | 2007-04-27 07:05:44 +0000 | [diff] [blame] | 6512 | #ifndef SQLITE_OMIT_AUTOVACUUM |
| 6513 | if( pBt->nTrunc!=0 ){ |
| 6514 | sCheck.nPage = pBt->nTrunc; |
| 6515 | } |
| 6516 | #endif |
drh | 0de8c11 | 2002-07-06 16:32:14 +0000 | [diff] [blame] | 6517 | if( sCheck.nPage==0 ){ |
| 6518 | unlockBtreeIfUnused(pBt); |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 6519 | sqlite3BtreeLeave(p); |
drh | 0de8c11 | 2002-07-06 16:32:14 +0000 | [diff] [blame] | 6520 | return 0; |
| 6521 | } |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 6522 | sCheck.anRef = sqlite3_malloc( (sCheck.nPage+1)*sizeof(sCheck.anRef[0]) ); |
danielk1977 | ac245ec | 2005-01-14 13:50:11 +0000 | [diff] [blame] | 6523 | if( !sCheck.anRef ){ |
| 6524 | unlockBtreeIfUnused(pBt); |
drh | 1dcdbc0 | 2007-01-27 02:24:54 +0000 | [diff] [blame] | 6525 | *pnErr = 1; |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 6526 | sqlite3BtreeLeave(p); |
danielk1977 | 1e53695 | 2007-08-16 10:09:01 +0000 | [diff] [blame] | 6527 | return sqlite3MPrintf(p->pSqlite, "Unable to malloc %d bytes", |
danielk1977 | ac245ec | 2005-01-14 13:50:11 +0000 | [diff] [blame] | 6528 | (sCheck.nPage+1)*sizeof(sCheck.anRef[0])); |
| 6529 | } |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 6530 | for(i=0; i<=sCheck.nPage; i++){ sCheck.anRef[i] = 0; } |
drh | 42cac6d | 2004-11-20 20:31:11 +0000 | [diff] [blame] | 6531 | i = PENDING_BYTE_PAGE(pBt); |
drh | 1f59571 | 2004-06-15 01:40:29 +0000 | [diff] [blame] | 6532 | if( i<=sCheck.nPage ){ |
| 6533 | sCheck.anRef[i] = 1; |
| 6534 | } |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6535 | sCheck.zErrMsg = 0; |
| 6536 | |
| 6537 | /* Check the integrity of the freelist |
| 6538 | */ |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 6539 | checkList(&sCheck, 1, get4byte(&pBt->pPage1->aData[32]), |
| 6540 | get4byte(&pBt->pPage1->aData[36]), "Main freelist: "); |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6541 | |
| 6542 | /* Check all the tables. |
| 6543 | */ |
drh | 1dcdbc0 | 2007-01-27 02:24:54 +0000 | [diff] [blame] | 6544 | for(i=0; i<nRoot && sCheck.mxErr; i++){ |
drh | 4ff6dfa | 2002-03-03 23:06:00 +0000 | [diff] [blame] | 6545 | if( aRoot[i]==0 ) continue; |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 6546 | #ifndef SQLITE_OMIT_AUTOVACUUM |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 6547 | if( pBt->autoVacuum && aRoot[i]>1 ){ |
| 6548 | checkPtrmap(&sCheck, aRoot[i], PTRMAP_ROOTPAGE, 0, 0); |
| 6549 | } |
| 6550 | #endif |
drh | 7416170 | 2006-02-24 02:53:49 +0000 | [diff] [blame] | 6551 | checkTreePage(&sCheck, aRoot[i], 0, "List of tree roots: "); |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6552 | } |
| 6553 | |
| 6554 | /* Make sure every page in the file is referenced |
| 6555 | */ |
drh | 1dcdbc0 | 2007-01-27 02:24:54 +0000 | [diff] [blame] | 6556 | for(i=1; i<=sCheck.nPage && sCheck.mxErr; i++){ |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 6557 | #ifdef SQLITE_OMIT_AUTOVACUUM |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6558 | if( sCheck.anRef[i]==0 ){ |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 6559 | checkAppendMsg(&sCheck, 0, "Page %d is never used", i); |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6560 | } |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 6561 | #else |
| 6562 | /* If the database supports auto-vacuum, make sure no tables contain |
| 6563 | ** references to pointer-map pages. |
| 6564 | */ |
| 6565 | if( sCheck.anRef[i]==0 && |
danielk1977 | 266664d | 2006-02-10 08:24:21 +0000 | [diff] [blame] | 6566 | (PTRMAP_PAGENO(pBt, i)!=i || !pBt->autoVacuum) ){ |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 6567 | checkAppendMsg(&sCheck, 0, "Page %d is never used", i); |
| 6568 | } |
| 6569 | if( sCheck.anRef[i]!=0 && |
danielk1977 | 266664d | 2006-02-10 08:24:21 +0000 | [diff] [blame] | 6570 | (PTRMAP_PAGENO(pBt, i)==i && pBt->autoVacuum) ){ |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 6571 | checkAppendMsg(&sCheck, 0, "Pointer map page %d is referenced", i); |
| 6572 | } |
| 6573 | #endif |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6574 | } |
| 6575 | |
| 6576 | /* Make sure this analysis did not leave any unref() pages |
| 6577 | */ |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 6578 | unlockBtreeIfUnused(pBt); |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 6579 | if( nRef != sqlite3PagerRefcount(pBt->pPager) ){ |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 6580 | checkAppendMsg(&sCheck, 0, |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6581 | "Outstanding page count goes from %d to %d during this analysis", |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 6582 | nRef, sqlite3PagerRefcount(pBt->pPager) |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6583 | ); |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6584 | } |
| 6585 | |
| 6586 | /* Clean up and report errors. |
| 6587 | */ |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 6588 | sqlite3BtreeLeave(p); |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 6589 | sqlite3_free(sCheck.anRef); |
drh | 1dcdbc0 | 2007-01-27 02:24:54 +0000 | [diff] [blame] | 6590 | *pnErr = sCheck.nErr; |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6591 | return sCheck.zErrMsg; |
| 6592 | } |
drh | b7f9164 | 2004-10-31 02:22:47 +0000 | [diff] [blame] | 6593 | #endif /* SQLITE_OMIT_INTEGRITY_CHECK */ |
paul | b95a886 | 2003-04-01 21:16:41 +0000 | [diff] [blame] | 6594 | |
drh | 73509ee | 2003-04-06 20:44:45 +0000 | [diff] [blame] | 6595 | /* |
| 6596 | ** Return the full pathname of the underlying database file. |
drh | d0679ed | 2007-08-28 22:24:34 +0000 | [diff] [blame^] | 6597 | ** |
| 6598 | ** The pager filename is invariant as long as the pager is |
| 6599 | ** open so it is safe to access without the BtShared mutex. |
drh | 73509ee | 2003-04-06 20:44:45 +0000 | [diff] [blame] | 6600 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 6601 | const char *sqlite3BtreeGetFilename(Btree *p){ |
| 6602 | assert( p->pBt->pPager!=0 ); |
drh | d0679ed | 2007-08-28 22:24:34 +0000 | [diff] [blame^] | 6603 | assert( sqlite3_mutex_held(p->pSqlite->mutex) ); |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 6604 | return sqlite3PagerFilename(p->pBt->pPager); |
drh | 73509ee | 2003-04-06 20:44:45 +0000 | [diff] [blame] | 6605 | } |
| 6606 | |
| 6607 | /* |
danielk1977 | 5865e3d | 2004-06-14 06:03:57 +0000 | [diff] [blame] | 6608 | ** Return the pathname of the directory that contains the database file. |
drh | d0679ed | 2007-08-28 22:24:34 +0000 | [diff] [blame^] | 6609 | ** |
| 6610 | ** The pager directory name is invariant as long as the pager is |
| 6611 | ** open so it is safe to access without the BtShared mutex. |
danielk1977 | 5865e3d | 2004-06-14 06:03:57 +0000 | [diff] [blame] | 6612 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 6613 | const char *sqlite3BtreeGetDirname(Btree *p){ |
| 6614 | assert( p->pBt->pPager!=0 ); |
drh | d0679ed | 2007-08-28 22:24:34 +0000 | [diff] [blame^] | 6615 | assert( sqlite3_mutex_held(p->pSqlite->mutex) ); |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 6616 | return sqlite3PagerDirname(p->pBt->pPager); |
danielk1977 | 5865e3d | 2004-06-14 06:03:57 +0000 | [diff] [blame] | 6617 | } |
| 6618 | |
| 6619 | /* |
| 6620 | ** Return the pathname of the journal file for this database. The return |
| 6621 | ** value of this routine is the same regardless of whether the journal file |
| 6622 | ** has been created or not. |
drh | d0679ed | 2007-08-28 22:24:34 +0000 | [diff] [blame^] | 6623 | ** |
| 6624 | ** The pager journal filename is invariant as long as the pager is |
| 6625 | ** open so it is safe to access without the BtShared mutex. |
danielk1977 | 5865e3d | 2004-06-14 06:03:57 +0000 | [diff] [blame] | 6626 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 6627 | const char *sqlite3BtreeGetJournalname(Btree *p){ |
| 6628 | assert( p->pBt->pPager!=0 ); |
drh | d0679ed | 2007-08-28 22:24:34 +0000 | [diff] [blame^] | 6629 | assert( sqlite3_mutex_held(p->pSqlite->mutex) ); |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 6630 | return sqlite3PagerJournalname(p->pBt->pPager); |
danielk1977 | 5865e3d | 2004-06-14 06:03:57 +0000 | [diff] [blame] | 6631 | } |
| 6632 | |
drh | b7f9164 | 2004-10-31 02:22:47 +0000 | [diff] [blame] | 6633 | #ifndef SQLITE_OMIT_VACUUM |
danielk1977 | 5865e3d | 2004-06-14 06:03:57 +0000 | [diff] [blame] | 6634 | /* |
drh | f7c5753 | 2003-04-25 13:22:51 +0000 | [diff] [blame] | 6635 | ** Copy the complete content of pBtFrom into pBtTo. A transaction |
| 6636 | ** must be active for both files. |
| 6637 | ** |
| 6638 | ** The size of file pBtFrom may be reduced by this operation. |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 6639 | ** If anything goes wrong, the transaction on pBtFrom is rolled back. |
drh | 73509ee | 2003-04-06 20:44:45 +0000 | [diff] [blame] | 6640 | */ |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 6641 | static int btreeCopyFile(Btree *pTo, Btree *pFrom){ |
drh | f7c5753 | 2003-04-25 13:22:51 +0000 | [diff] [blame] | 6642 | int rc = SQLITE_OK; |
drh | 50f2f43 | 2005-09-16 11:32:18 +0000 | [diff] [blame] | 6643 | Pgno i, nPage, nToPage, iSkip; |
drh | f7c5753 | 2003-04-25 13:22:51 +0000 | [diff] [blame] | 6644 | |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 6645 | BtShared *pBtTo = pTo->pBt; |
| 6646 | BtShared *pBtFrom = pFrom->pBt; |
| 6647 | |
| 6648 | if( pTo->inTrans!=TRANS_WRITE || pFrom->inTrans!=TRANS_WRITE ){ |
danielk1977 | ee5741e | 2004-05-31 10:01:34 +0000 | [diff] [blame] | 6649 | return SQLITE_ERROR; |
| 6650 | } |
drh | f7c5753 | 2003-04-25 13:22:51 +0000 | [diff] [blame] | 6651 | if( pBtTo->pCursor ) return SQLITE_BUSY; |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 6652 | nToPage = sqlite3PagerPagecount(pBtTo->pPager); |
| 6653 | nPage = sqlite3PagerPagecount(pBtFrom->pPager); |
drh | 50f2f43 | 2005-09-16 11:32:18 +0000 | [diff] [blame] | 6654 | iSkip = PENDING_BYTE_PAGE(pBtTo); |
danielk1977 | 369f27e | 2004-06-15 11:40:04 +0000 | [diff] [blame] | 6655 | for(i=1; rc==SQLITE_OK && i<=nPage; i++){ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 6656 | DbPage *pDbPage; |
drh | 50f2f43 | 2005-09-16 11:32:18 +0000 | [diff] [blame] | 6657 | if( i==iSkip ) continue; |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 6658 | rc = sqlite3PagerGet(pBtFrom->pPager, i, &pDbPage); |
drh | f7c5753 | 2003-04-25 13:22:51 +0000 | [diff] [blame] | 6659 | if( rc ) break; |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 6660 | rc = sqlite3PagerOverwrite(pBtTo->pPager, i, sqlite3PagerGetData(pDbPage)); |
| 6661 | sqlite3PagerUnref(pDbPage); |
drh | f7c5753 | 2003-04-25 13:22:51 +0000 | [diff] [blame] | 6662 | } |
drh | 538f570 | 2007-04-13 02:14:30 +0000 | [diff] [blame] | 6663 | |
| 6664 | /* If the file is shrinking, journal the pages that are being truncated |
| 6665 | ** so that they can be rolled back if the commit fails. |
| 6666 | */ |
drh | 2e6d11b | 2003-04-25 15:37:57 +0000 | [diff] [blame] | 6667 | for(i=nPage+1; rc==SQLITE_OK && i<=nToPage; i++){ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 6668 | DbPage *pDbPage; |
drh | 4928570 | 2005-09-17 15:20:26 +0000 | [diff] [blame] | 6669 | if( i==iSkip ) continue; |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 6670 | rc = sqlite3PagerGet(pBtTo->pPager, i, &pDbPage); |
drh | 2e6d11b | 2003-04-25 15:37:57 +0000 | [diff] [blame] | 6671 | if( rc ) break; |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 6672 | rc = sqlite3PagerWrite(pDbPage); |
drh | 538f570 | 2007-04-13 02:14:30 +0000 | [diff] [blame] | 6673 | sqlite3PagerDontWrite(pDbPage); |
| 6674 | /* Yeah. It seems wierd to call DontWrite() right after Write(). But |
| 6675 | ** that is because the names of those procedures do not exactly |
| 6676 | ** represent what they do. Write() really means "put this page in the |
| 6677 | ** rollback journal and mark it as dirty so that it will be written |
| 6678 | ** to the database file later." DontWrite() undoes the second part of |
| 6679 | ** that and prevents the page from being written to the database. The |
| 6680 | ** page is still on the rollback journal, though. And that is the whole |
| 6681 | ** point of this loop: to put pages on the rollback journal. */ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 6682 | sqlite3PagerUnref(pDbPage); |
drh | 2e6d11b | 2003-04-25 15:37:57 +0000 | [diff] [blame] | 6683 | } |
| 6684 | if( !rc && nPage<nToPage ){ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 6685 | rc = sqlite3PagerTruncate(pBtTo->pPager, nPage); |
drh | 2e6d11b | 2003-04-25 15:37:57 +0000 | [diff] [blame] | 6686 | } |
drh | 538f570 | 2007-04-13 02:14:30 +0000 | [diff] [blame] | 6687 | |
drh | f7c5753 | 2003-04-25 13:22:51 +0000 | [diff] [blame] | 6688 | if( rc ){ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 6689 | sqlite3BtreeRollback(pTo); |
drh | f7c5753 | 2003-04-25 13:22:51 +0000 | [diff] [blame] | 6690 | } |
| 6691 | return rc; |
drh | 73509ee | 2003-04-06 20:44:45 +0000 | [diff] [blame] | 6692 | } |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 6693 | int sqlite3BtreeCopyFile(Btree *pTo, Btree *pFrom){ |
| 6694 | int rc; |
| 6695 | sqlite3BtreeEnter(pTo); |
| 6696 | sqlite3BtreeEnter(pFrom); |
| 6697 | rc = btreeCopyFile(pTo, pFrom); |
| 6698 | sqlite3BtreeLeave(pFrom); |
| 6699 | sqlite3BtreeLeave(pTo); |
| 6700 | return rc; |
| 6701 | } |
| 6702 | |
drh | b7f9164 | 2004-10-31 02:22:47 +0000 | [diff] [blame] | 6703 | #endif /* SQLITE_OMIT_VACUUM */ |
danielk1977 | 1d850a7 | 2004-05-31 08:26:49 +0000 | [diff] [blame] | 6704 | |
| 6705 | /* |
| 6706 | ** Return non-zero if a transaction is active. |
| 6707 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 6708 | int sqlite3BtreeIsInTrans(Btree *p){ |
drh | d0679ed | 2007-08-28 22:24:34 +0000 | [diff] [blame^] | 6709 | assert( p==0 || sqlite3BtreeMutexHeld(p->pSqlite->mutex) ); |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 6710 | return (p && (p->inTrans==TRANS_WRITE)); |
danielk1977 | 1d850a7 | 2004-05-31 08:26:49 +0000 | [diff] [blame] | 6711 | } |
| 6712 | |
| 6713 | /* |
| 6714 | ** Return non-zero if a statement transaction is active. |
| 6715 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 6716 | int sqlite3BtreeIsInStmt(Btree *p){ |
drh | 2764170 | 2007-08-22 02:56:42 +0000 | [diff] [blame] | 6717 | assert( sqlite3BtreeMutexHeld(p->pBt->mutex) ); |
| 6718 | assert( sqlite3BtreeMutexHeld(p->pSqlite->mutex) ); |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 6719 | return (p->pBt && p->pBt->inStmt); |
danielk1977 | 1d850a7 | 2004-05-31 08:26:49 +0000 | [diff] [blame] | 6720 | } |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 6721 | |
| 6722 | /* |
danielk1977 | 2372c2b | 2006-06-27 16:34:56 +0000 | [diff] [blame] | 6723 | ** Return non-zero if a read (or write) transaction is active. |
| 6724 | */ |
| 6725 | int sqlite3BtreeIsInReadTrans(Btree *p){ |
drh | 2764170 | 2007-08-22 02:56:42 +0000 | [diff] [blame] | 6726 | assert( sqlite3BtreeMutexHeld(p->pSqlite->mutex) ); |
danielk1977 | 2372c2b | 2006-06-27 16:34:56 +0000 | [diff] [blame] | 6727 | return (p && (p->inTrans!=TRANS_NONE)); |
| 6728 | } |
| 6729 | |
| 6730 | /* |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 6731 | ** This function returns a pointer to a blob of memory associated with |
| 6732 | ** a single shared-btree. The memory is used by client code for it's own |
| 6733 | ** purposes (for example, to store a high-level schema associated with |
| 6734 | ** the shared-btree). The btree layer manages reference counting issues. |
| 6735 | ** |
| 6736 | ** The first time this is called on a shared-btree, nBytes bytes of memory |
| 6737 | ** are allocated, zeroed, and returned to the caller. For each subsequent |
| 6738 | ** call the nBytes parameter is ignored and a pointer to the same blob |
| 6739 | ** of memory returned. |
| 6740 | ** |
| 6741 | ** Just before the shared-btree is closed, the function passed as the |
| 6742 | ** xFree argument when the memory allocation was made is invoked on the |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 6743 | ** blob of allocated memory. This function should not call sqlite3_free() |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 6744 | ** on the memory, the btree layer does that. |
| 6745 | */ |
| 6746 | void *sqlite3BtreeSchema(Btree *p, int nBytes, void(*xFree)(void *)){ |
| 6747 | BtShared *pBt = p->pBt; |
drh | 2764170 | 2007-08-22 02:56:42 +0000 | [diff] [blame] | 6748 | sqlite3BtreeEnter(p); |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 6749 | if( !pBt->pSchema ){ |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 6750 | pBt->pSchema = sqlite3MallocZero(nBytes); |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 6751 | pBt->xFreeSchema = xFree; |
| 6752 | } |
drh | 2764170 | 2007-08-22 02:56:42 +0000 | [diff] [blame] | 6753 | sqlite3BtreeLeave(p); |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 6754 | return pBt->pSchema; |
| 6755 | } |
| 6756 | |
danielk1977 | c87d34d | 2006-01-06 13:00:28 +0000 | [diff] [blame] | 6757 | /* |
| 6758 | ** Return true if another user of the same shared btree as the argument |
| 6759 | ** handle holds an exclusive lock on the sqlite_master table. |
| 6760 | */ |
| 6761 | int sqlite3BtreeSchemaLocked(Btree *p){ |
drh | 2764170 | 2007-08-22 02:56:42 +0000 | [diff] [blame] | 6762 | int rc; |
| 6763 | assert( sqlite3BtreeMutexHeld(p->pSqlite->mutex) ); |
| 6764 | sqlite3BtreeEnter(p); |
| 6765 | rc = (queryTableLock(p, MASTER_ROOT, READ_LOCK)!=SQLITE_OK); |
| 6766 | sqlite3BtreeLeave(p); |
| 6767 | return rc; |
danielk1977 | c87d34d | 2006-01-06 13:00:28 +0000 | [diff] [blame] | 6768 | } |
| 6769 | |
drh | a154dcd | 2006-03-22 22:10:07 +0000 | [diff] [blame] | 6770 | |
| 6771 | #ifndef SQLITE_OMIT_SHARED_CACHE |
| 6772 | /* |
| 6773 | ** Obtain a lock on the table whose root page is iTab. The |
| 6774 | ** lock is a write lock if isWritelock is true or a read lock |
| 6775 | ** if it is false. |
| 6776 | */ |
danielk1977 | c00da10 | 2006-01-07 13:21:04 +0000 | [diff] [blame] | 6777 | int sqlite3BtreeLockTable(Btree *p, int iTab, u8 isWriteLock){ |
danielk1977 | 2e94d4d | 2006-01-09 05:36:27 +0000 | [diff] [blame] | 6778 | int rc = SQLITE_OK; |
danielk1977 | c00da10 | 2006-01-07 13:21:04 +0000 | [diff] [blame] | 6779 | u8 lockType = (isWriteLock?WRITE_LOCK:READ_LOCK); |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 6780 | sqlite3BtreeEnter(p); |
danielk1977 | 2e94d4d | 2006-01-09 05:36:27 +0000 | [diff] [blame] | 6781 | rc = queryTableLock(p, iTab, lockType); |
danielk1977 | c00da10 | 2006-01-07 13:21:04 +0000 | [diff] [blame] | 6782 | if( rc==SQLITE_OK ){ |
| 6783 | rc = lockTable(p, iTab, lockType); |
| 6784 | } |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 6785 | sqlite3BtreeLeave(p); |
danielk1977 | c00da10 | 2006-01-07 13:21:04 +0000 | [diff] [blame] | 6786 | return rc; |
| 6787 | } |
drh | a154dcd | 2006-03-22 22:10:07 +0000 | [diff] [blame] | 6788 | #endif |
danielk1977 | b82e7ed | 2006-01-11 14:09:31 +0000 | [diff] [blame] | 6789 | |
danielk1977 | b4e9af9 | 2007-05-01 17:49:49 +0000 | [diff] [blame] | 6790 | #ifndef SQLITE_OMIT_INCRBLOB |
| 6791 | /* |
| 6792 | ** Argument pCsr must be a cursor opened for writing on an |
| 6793 | ** INTKEY table currently pointing at a valid table entry. |
| 6794 | ** This function modifies the data stored as part of that entry. |
| 6795 | ** Only the data content may only be modified, it is not possible |
| 6796 | ** to change the length of the data stored. |
| 6797 | */ |
danielk1977 | dcbb5d3 | 2007-05-04 18:36:44 +0000 | [diff] [blame] | 6798 | int sqlite3BtreePutData(BtCursor *pCsr, u32 offset, u32 amt, void *z){ |
drh | d0679ed | 2007-08-28 22:24:34 +0000 | [diff] [blame^] | 6799 | assert( sqlite3BtreeMutexHeld(pCsr->pBt->mutex) ); |
drh | 2764170 | 2007-08-22 02:56:42 +0000 | [diff] [blame] | 6800 | assert( sqlite3BtreeMutexHeld(pCsr->pBtree->pSqlite->mutex) ); |
danielk1977 | dcbb5d3 | 2007-05-04 18:36:44 +0000 | [diff] [blame] | 6801 | assert(pCsr->isIncrblobHandle); |
| 6802 | if( pCsr->eState==CURSOR_REQUIRESEEK ){ |
| 6803 | return SQLITE_ABORT; |
| 6804 | } |
| 6805 | |
danielk1977 | d0441796 | 2007-05-02 13:16:30 +0000 | [diff] [blame] | 6806 | /* Check some preconditions: |
danielk1977 | dcbb5d3 | 2007-05-04 18:36:44 +0000 | [diff] [blame] | 6807 | ** (a) the cursor is open for writing, |
| 6808 | ** (b) there is no read-lock on the table being modified and |
| 6809 | ** (c) the cursor points at a valid row of an intKey table. |
danielk1977 | d0441796 | 2007-05-02 13:16:30 +0000 | [diff] [blame] | 6810 | */ |
danielk1977 | d0441796 | 2007-05-02 13:16:30 +0000 | [diff] [blame] | 6811 | if( !pCsr->wrFlag ){ |
danielk1977 | dcbb5d3 | 2007-05-04 18:36:44 +0000 | [diff] [blame] | 6812 | return SQLITE_READONLY; |
danielk1977 | d0441796 | 2007-05-02 13:16:30 +0000 | [diff] [blame] | 6813 | } |
drh | d0679ed | 2007-08-28 22:24:34 +0000 | [diff] [blame^] | 6814 | assert( !pCsr->pBt->readOnly |
| 6815 | && pCsr->pBt->inTransaction==TRANS_WRITE ); |
danielk1977 | d0441796 | 2007-05-02 13:16:30 +0000 | [diff] [blame] | 6816 | if( checkReadLocks(pCsr->pBtree, pCsr->pgnoRoot, pCsr) ){ |
| 6817 | return SQLITE_LOCKED; /* The table pCur points to has a read lock */ |
| 6818 | } |
| 6819 | if( pCsr->eState==CURSOR_INVALID || !pCsr->pPage->intKey ){ |
| 6820 | return SQLITE_ERROR; |
danielk1977 | b4e9af9 | 2007-05-01 17:49:49 +0000 | [diff] [blame] | 6821 | } |
| 6822 | |
danielk1977 | 9f8d640 | 2007-05-02 17:48:45 +0000 | [diff] [blame] | 6823 | return accessPayload(pCsr, offset, amt, (unsigned char *)z, 0, 1); |
danielk1977 | b4e9af9 | 2007-05-01 17:49:49 +0000 | [diff] [blame] | 6824 | } |
danielk1977 | 2dec970 | 2007-05-02 16:48:37 +0000 | [diff] [blame] | 6825 | |
| 6826 | /* |
| 6827 | ** 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] | 6828 | ** overflow list for the current row. This is used by cursors opened |
| 6829 | ** for incremental blob IO only. |
| 6830 | ** |
| 6831 | ** This function sets a flag only. The actual page location cache |
| 6832 | ** (stored in BtCursor.aOverflow[]) is allocated and used by function |
| 6833 | ** accessPayload() (the worker function for sqlite3BtreeData() and |
| 6834 | ** sqlite3BtreePutData()). |
danielk1977 | 2dec970 | 2007-05-02 16:48:37 +0000 | [diff] [blame] | 6835 | */ |
| 6836 | void sqlite3BtreeCacheOverflow(BtCursor *pCur){ |
drh | d0679ed | 2007-08-28 22:24:34 +0000 | [diff] [blame^] | 6837 | assert( sqlite3BtreeMutexHeld(pCur->pBt->mutex) ); |
drh | 2764170 | 2007-08-22 02:56:42 +0000 | [diff] [blame] | 6838 | assert( sqlite3BtreeMutexHeld(pCur->pBtree->pSqlite->mutex) ); |
danielk1977 | dcbb5d3 | 2007-05-04 18:36:44 +0000 | [diff] [blame] | 6839 | assert(!pCur->isIncrblobHandle); |
danielk1977 | 2dec970 | 2007-05-02 16:48:37 +0000 | [diff] [blame] | 6840 | assert(!pCur->aOverflow); |
danielk1977 | dcbb5d3 | 2007-05-04 18:36:44 +0000 | [diff] [blame] | 6841 | pCur->isIncrblobHandle = 1; |
danielk1977 | 2dec970 | 2007-05-02 16:48:37 +0000 | [diff] [blame] | 6842 | } |
danielk1977 | b4e9af9 | 2007-05-01 17:49:49 +0000 | [diff] [blame] | 6843 | #endif |