drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 1 | /* |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 2 | ** 2004 April 6 |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 3 | ** |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 4 | ** The author disclaims copyright to this source code. In place of |
| 5 | ** a legal notice, here is a blessing: |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 6 | ** |
drh | b19a2bc | 2001-09-16 00:13:26 +0000 | [diff] [blame] | 7 | ** May you do good and not evil. |
| 8 | ** May you find forgiveness for yourself and forgive others. |
| 9 | ** May you share freely, never taking more than you give. |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 10 | ** |
| 11 | ************************************************************************* |
danielk1977 | ad0132d | 2008-06-07 08:58:22 +0000 | [diff] [blame^] | 12 | ** $Id: btree.c,v 1.459 2008/06/07 08:58:22 danielk1977 Exp $ |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 13 | ** |
| 14 | ** This file implements a external (disk-based) database using BTrees. |
drh | a315289 | 2007-05-05 11:48:52 +0000 | [diff] [blame] | 15 | ** See the header comment on "btreeInt.h" for additional information. |
| 16 | ** Including a description of file format and an overview of operation. |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 17 | */ |
drh | a315289 | 2007-05-05 11:48:52 +0000 | [diff] [blame] | 18 | #include "btreeInt.h" |
paul | b95a886 | 2003-04-01 21:16:41 +0000 | [diff] [blame] | 19 | |
drh | 8c42ca9 | 2001-06-22 19:15:00 +0000 | [diff] [blame] | 20 | /* |
drh | a315289 | 2007-05-05 11:48:52 +0000 | [diff] [blame] | 21 | ** The header string that appears at the beginning of every |
| 22 | ** SQLite database. |
drh | 556b2a2 | 2005-06-14 16:04:05 +0000 | [diff] [blame] | 23 | */ |
drh | 556b2a2 | 2005-06-14 16:04:05 +0000 | [diff] [blame] | 24 | static const char zMagicHeader[] = SQLITE_FILE_HEADER; |
drh | 08ed44e | 2001-04-29 23:32:55 +0000 | [diff] [blame] | 25 | |
drh | 8c42ca9 | 2001-06-22 19:15:00 +0000 | [diff] [blame] | 26 | /* |
drh | a315289 | 2007-05-05 11:48:52 +0000 | [diff] [blame] | 27 | ** Set this global variable to 1 to enable tracing using the TRACE |
| 28 | ** macro. |
drh | 615ae55 | 2005-01-16 23:21:00 +0000 | [diff] [blame] | 29 | */ |
| 30 | #if SQLITE_TEST |
mlcreech | 3a00f90 | 2008-03-04 17:45:01 +0000 | [diff] [blame] | 31 | int sqlite3BtreeTrace=0; /* True to enable tracing */ |
drh | 615ae55 | 2005-01-16 23:21:00 +0000 | [diff] [blame] | 32 | #endif |
drh | 615ae55 | 2005-01-16 23:21:00 +0000 | [diff] [blame] | 33 | |
drh | 86f8c19 | 2007-08-22 00:39:19 +0000 | [diff] [blame] | 34 | |
| 35 | |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 36 | #ifndef SQLITE_OMIT_SHARED_CACHE |
| 37 | /* |
| 38 | ** A flag to indicate whether or not shared cache is enabled. Also, |
| 39 | ** a list of BtShared objects that are eligible for participation |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 40 | ** in shared cache. The variables have file scope during normal builds, |
drh | 86f8c19 | 2007-08-22 00:39:19 +0000 | [diff] [blame] | 41 | ** but the test harness needs to access these variables so we make them |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 42 | ** global for test builds. |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 43 | */ |
| 44 | #ifdef SQLITE_TEST |
| 45 | BtShared *sqlite3SharedCacheList = 0; |
| 46 | int sqlite3SharedCacheEnabled = 0; |
| 47 | #else |
| 48 | static BtShared *sqlite3SharedCacheList = 0; |
| 49 | static int sqlite3SharedCacheEnabled = 0; |
| 50 | #endif |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 51 | #endif /* SQLITE_OMIT_SHARED_CACHE */ |
| 52 | |
| 53 | #ifndef SQLITE_OMIT_SHARED_CACHE |
| 54 | /* |
| 55 | ** Enable or disable the shared pager and schema features. |
| 56 | ** |
| 57 | ** This routine has no effect on existing database connections. |
| 58 | ** The shared cache setting effects only future calls to |
| 59 | ** sqlite3_open(), sqlite3_open16(), or sqlite3_open_v2(). |
| 60 | */ |
| 61 | int sqlite3_enable_shared_cache(int enable){ |
| 62 | sqlite3SharedCacheEnabled = enable; |
| 63 | return SQLITE_OK; |
| 64 | } |
| 65 | #endif |
| 66 | |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 67 | |
drh | 615ae55 | 2005-01-16 23:21:00 +0000 | [diff] [blame] | 68 | /* |
drh | 66cbd15 | 2004-09-01 16:12:25 +0000 | [diff] [blame] | 69 | ** Forward declaration |
| 70 | */ |
drh | 980b1a7 | 2006-08-16 16:42:48 +0000 | [diff] [blame] | 71 | static int checkReadLocks(Btree*,Pgno,BtCursor*); |
drh | 66cbd15 | 2004-09-01 16:12:25 +0000 | [diff] [blame] | 72 | |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 73 | |
| 74 | #ifdef SQLITE_OMIT_SHARED_CACHE |
| 75 | /* |
| 76 | ** The functions queryTableLock(), lockTable() and unlockAllTables() |
| 77 | ** manipulate entries in the BtShared.pLock linked list used to store |
| 78 | ** shared-cache table level locks. If the library is compiled with the |
| 79 | ** shared-cache feature disabled, then there is only ever one user |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 80 | ** of each BtShared structure and so this locking is not necessary. |
| 81 | ** So define the lock related functions as no-ops. |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 82 | */ |
| 83 | #define queryTableLock(a,b,c) SQLITE_OK |
| 84 | #define lockTable(a,b,c) SQLITE_OK |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 85 | #define unlockAllTables(a) |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 86 | #endif |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 87 | |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 88 | #ifndef SQLITE_OMIT_SHARED_CACHE |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 89 | /* |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 90 | ** Query to see if btree handle p may obtain a lock of type eLock |
| 91 | ** (READ_LOCK or WRITE_LOCK) on the table with root-page iTab. Return |
| 92 | ** SQLITE_OK if the lock may be obtained (by calling lockTable()), or |
danielk1977 | c87d34d | 2006-01-06 13:00:28 +0000 | [diff] [blame] | 93 | ** SQLITE_LOCKED if not. |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 94 | */ |
| 95 | static int queryTableLock(Btree *p, Pgno iTab, u8 eLock){ |
| 96 | BtShared *pBt = p->pBt; |
| 97 | BtLock *pIter; |
| 98 | |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 99 | assert( sqlite3BtreeHoldsMutex(p) ); |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 100 | |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 101 | /* This is a no-op if the shared-cache is not enabled */ |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 102 | if( !p->sharable ){ |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 103 | return SQLITE_OK; |
| 104 | } |
| 105 | |
danielk1977 | 641b0f4 | 2007-12-21 04:47:25 +0000 | [diff] [blame] | 106 | /* If some other connection is holding an exclusive lock, the |
| 107 | ** requested lock may not be obtained. |
| 108 | */ |
| 109 | if( pBt->pExclusive && pBt->pExclusive!=p ){ |
| 110 | return SQLITE_LOCKED; |
| 111 | } |
| 112 | |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 113 | /* This (along with lockTable()) is where the ReadUncommitted flag is |
| 114 | ** dealt with. If the caller is querying for a read-lock and the flag is |
| 115 | ** set, it is unconditionally granted - even if there are write-locks |
| 116 | ** on the table. If a write-lock is requested, the ReadUncommitted flag |
| 117 | ** is not considered. |
| 118 | ** |
| 119 | ** In function lockTable(), if a read-lock is demanded and the |
| 120 | ** ReadUncommitted flag is set, no entry is added to the locks list |
| 121 | ** (BtShared.pLock). |
| 122 | ** |
| 123 | ** To summarize: If the ReadUncommitted flag is set, then read cursors do |
| 124 | ** not create or respect table locks. The locking procedure for a |
| 125 | ** write-cursor does not change. |
| 126 | */ |
| 127 | if( |
drh | e5fe690 | 2007-12-07 18:55:28 +0000 | [diff] [blame] | 128 | !p->db || |
| 129 | 0==(p->db->flags&SQLITE_ReadUncommitted) || |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 130 | eLock==WRITE_LOCK || |
drh | 47ded16 | 2006-01-06 01:42:58 +0000 | [diff] [blame] | 131 | iTab==MASTER_ROOT |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 132 | ){ |
| 133 | for(pIter=pBt->pLock; pIter; pIter=pIter->pNext){ |
| 134 | if( pIter->pBtree!=p && pIter->iTable==iTab && |
| 135 | (pIter->eLock!=eLock || eLock!=READ_LOCK) ){ |
danielk1977 | c87d34d | 2006-01-06 13:00:28 +0000 | [diff] [blame] | 136 | return SQLITE_LOCKED; |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 137 | } |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 138 | } |
| 139 | } |
| 140 | return SQLITE_OK; |
| 141 | } |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 142 | #endif /* !SQLITE_OMIT_SHARED_CACHE */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 143 | |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 144 | #ifndef SQLITE_OMIT_SHARED_CACHE |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 145 | /* |
| 146 | ** Add a lock on the table with root-page iTable to the shared-btree used |
| 147 | ** by Btree handle p. Parameter eLock must be either READ_LOCK or |
| 148 | ** WRITE_LOCK. |
| 149 | ** |
| 150 | ** SQLITE_OK is returned if the lock is added successfully. SQLITE_BUSY and |
| 151 | ** SQLITE_NOMEM may also be returned. |
| 152 | */ |
| 153 | static int lockTable(Btree *p, Pgno iTable, u8 eLock){ |
| 154 | BtShared *pBt = p->pBt; |
| 155 | BtLock *pLock = 0; |
| 156 | BtLock *pIter; |
| 157 | |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 158 | assert( sqlite3BtreeHoldsMutex(p) ); |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 159 | |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 160 | /* This is a no-op if the shared-cache is not enabled */ |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 161 | if( !p->sharable ){ |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 162 | return SQLITE_OK; |
| 163 | } |
| 164 | |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 165 | assert( SQLITE_OK==queryTableLock(p, iTable, eLock) ); |
| 166 | |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 167 | /* If the read-uncommitted flag is set and a read-lock is requested, |
| 168 | ** return early without adding an entry to the BtShared.pLock list. See |
| 169 | ** comment in function queryTableLock() for more info on handling |
| 170 | ** the ReadUncommitted flag. |
| 171 | */ |
| 172 | if( |
drh | e5fe690 | 2007-12-07 18:55:28 +0000 | [diff] [blame] | 173 | (p->db) && |
| 174 | (p->db->flags&SQLITE_ReadUncommitted) && |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 175 | (eLock==READ_LOCK) && |
drh | 47ded16 | 2006-01-06 01:42:58 +0000 | [diff] [blame] | 176 | iTable!=MASTER_ROOT |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 177 | ){ |
| 178 | return SQLITE_OK; |
| 179 | } |
| 180 | |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 181 | /* First search the list for an existing lock on this table. */ |
| 182 | for(pIter=pBt->pLock; pIter; pIter=pIter->pNext){ |
| 183 | if( pIter->iTable==iTable && pIter->pBtree==p ){ |
| 184 | pLock = pIter; |
| 185 | break; |
| 186 | } |
| 187 | } |
| 188 | |
| 189 | /* If the above search did not find a BtLock struct associating Btree p |
| 190 | ** with table iTable, allocate one and link it into the list. |
| 191 | */ |
| 192 | if( !pLock ){ |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 193 | pLock = (BtLock *)sqlite3MallocZero(sizeof(BtLock)); |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 194 | if( !pLock ){ |
| 195 | return SQLITE_NOMEM; |
| 196 | } |
| 197 | pLock->iTable = iTable; |
| 198 | pLock->pBtree = p; |
| 199 | pLock->pNext = pBt->pLock; |
| 200 | pBt->pLock = pLock; |
| 201 | } |
| 202 | |
| 203 | /* Set the BtLock.eLock variable to the maximum of the current lock |
| 204 | ** and the requested lock. This means if a write-lock was already held |
| 205 | ** and a read-lock requested, we don't incorrectly downgrade the lock. |
| 206 | */ |
| 207 | assert( WRITE_LOCK>READ_LOCK ); |
danielk1977 | 5118b91 | 2005-12-30 16:31:53 +0000 | [diff] [blame] | 208 | if( eLock>pLock->eLock ){ |
| 209 | pLock->eLock = eLock; |
| 210 | } |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 211 | |
| 212 | return SQLITE_OK; |
| 213 | } |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 214 | #endif /* !SQLITE_OMIT_SHARED_CACHE */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 215 | |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 216 | #ifndef SQLITE_OMIT_SHARED_CACHE |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 217 | /* |
| 218 | ** Release all the table locks (locks obtained via calls to the lockTable() |
| 219 | ** procedure) held by Btree handle p. |
| 220 | */ |
| 221 | static void unlockAllTables(Btree *p){ |
danielk1977 | 641b0f4 | 2007-12-21 04:47:25 +0000 | [diff] [blame] | 222 | BtShared *pBt = p->pBt; |
| 223 | BtLock **ppIter = &pBt->pLock; |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 224 | |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 225 | assert( sqlite3BtreeHoldsMutex(p) ); |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 226 | assert( p->sharable || 0==*ppIter ); |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 227 | |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 228 | while( *ppIter ){ |
| 229 | BtLock *pLock = *ppIter; |
danielk1977 | 641b0f4 | 2007-12-21 04:47:25 +0000 | [diff] [blame] | 230 | assert( pBt->pExclusive==0 || pBt->pExclusive==pLock->pBtree ); |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 231 | if( pLock->pBtree==p ){ |
| 232 | *ppIter = pLock->pNext; |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 233 | sqlite3_free(pLock); |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 234 | }else{ |
| 235 | ppIter = &pLock->pNext; |
| 236 | } |
| 237 | } |
danielk1977 | 641b0f4 | 2007-12-21 04:47:25 +0000 | [diff] [blame] | 238 | |
| 239 | if( pBt->pExclusive==p ){ |
| 240 | pBt->pExclusive = 0; |
| 241 | } |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 242 | } |
| 243 | #endif /* SQLITE_OMIT_SHARED_CACHE */ |
| 244 | |
drh | 980b1a7 | 2006-08-16 16:42:48 +0000 | [diff] [blame] | 245 | static void releasePage(MemPage *pPage); /* Forward reference */ |
| 246 | |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 247 | /* |
| 248 | ** Verify that the cursor holds a mutex on the BtShared |
| 249 | */ |
| 250 | #ifndef NDEBUG |
| 251 | static int cursorHoldsMutex(BtCursor *p){ |
drh | ff0587c | 2007-08-29 17:43:19 +0000 | [diff] [blame] | 252 | return sqlite3_mutex_held(p->pBt->mutex); |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 253 | } |
| 254 | #endif |
| 255 | |
| 256 | |
danielk1977 | 92d4d7a | 2007-05-04 12:05:56 +0000 | [diff] [blame] | 257 | #ifndef SQLITE_OMIT_INCRBLOB |
| 258 | /* |
| 259 | ** Invalidate the overflow page-list cache for cursor pCur, if any. |
| 260 | */ |
| 261 | static void invalidateOverflowCache(BtCursor *pCur){ |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 262 | assert( cursorHoldsMutex(pCur) ); |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 263 | sqlite3_free(pCur->aOverflow); |
danielk1977 | 92d4d7a | 2007-05-04 12:05:56 +0000 | [diff] [blame] | 264 | pCur->aOverflow = 0; |
| 265 | } |
| 266 | |
| 267 | /* |
| 268 | ** Invalidate the overflow page-list cache for all cursors opened |
| 269 | ** on the shared btree structure pBt. |
| 270 | */ |
| 271 | static void invalidateAllOverflowCache(BtShared *pBt){ |
| 272 | BtCursor *p; |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 273 | assert( sqlite3_mutex_held(pBt->mutex) ); |
danielk1977 | 92d4d7a | 2007-05-04 12:05:56 +0000 | [diff] [blame] | 274 | for(p=pBt->pCursor; p; p=p->pNext){ |
| 275 | invalidateOverflowCache(p); |
| 276 | } |
| 277 | } |
| 278 | #else |
| 279 | #define invalidateOverflowCache(x) |
| 280 | #define invalidateAllOverflowCache(x) |
| 281 | #endif |
| 282 | |
drh | 980b1a7 | 2006-08-16 16:42:48 +0000 | [diff] [blame] | 283 | /* |
| 284 | ** Save the current cursor position in the variables BtCursor.nKey |
| 285 | ** and BtCursor.pKey. The cursor's state is set to CURSOR_REQUIRESEEK. |
| 286 | */ |
| 287 | static int saveCursorPosition(BtCursor *pCur){ |
| 288 | int rc; |
| 289 | |
| 290 | assert( CURSOR_VALID==pCur->eState ); |
| 291 | assert( 0==pCur->pKey ); |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 292 | assert( cursorHoldsMutex(pCur) ); |
drh | 980b1a7 | 2006-08-16 16:42:48 +0000 | [diff] [blame] | 293 | |
| 294 | rc = sqlite3BtreeKeySize(pCur, &pCur->nKey); |
| 295 | |
| 296 | /* If this is an intKey table, then the above call to BtreeKeySize() |
| 297 | ** stores the integer key in pCur->nKey. In this case this value is |
| 298 | ** all that is required. Otherwise, if pCur is not open on an intKey |
| 299 | ** table, then malloc space for and store the pCur->nKey bytes of key |
| 300 | ** data. |
| 301 | */ |
| 302 | if( rc==SQLITE_OK && 0==pCur->pPage->intKey){ |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 303 | void *pKey = sqlite3_malloc(pCur->nKey); |
drh | 980b1a7 | 2006-08-16 16:42:48 +0000 | [diff] [blame] | 304 | if( pKey ){ |
| 305 | rc = sqlite3BtreeKey(pCur, 0, pCur->nKey, pKey); |
| 306 | if( rc==SQLITE_OK ){ |
| 307 | pCur->pKey = pKey; |
| 308 | }else{ |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 309 | sqlite3_free(pKey); |
drh | 980b1a7 | 2006-08-16 16:42:48 +0000 | [diff] [blame] | 310 | } |
| 311 | }else{ |
| 312 | rc = SQLITE_NOMEM; |
| 313 | } |
| 314 | } |
| 315 | assert( !pCur->pPage->intKey || !pCur->pKey ); |
| 316 | |
| 317 | if( rc==SQLITE_OK ){ |
| 318 | releasePage(pCur->pPage); |
| 319 | pCur->pPage = 0; |
| 320 | pCur->eState = CURSOR_REQUIRESEEK; |
| 321 | } |
| 322 | |
danielk1977 | 92d4d7a | 2007-05-04 12:05:56 +0000 | [diff] [blame] | 323 | invalidateOverflowCache(pCur); |
drh | 980b1a7 | 2006-08-16 16:42:48 +0000 | [diff] [blame] | 324 | return rc; |
| 325 | } |
| 326 | |
| 327 | /* |
| 328 | ** Save the positions of all cursors except pExcept open on the table |
| 329 | ** with root-page iRoot. Usually, this is called just before cursor |
| 330 | ** pExcept is used to modify the table (BtreeDelete() or BtreeInsert()). |
| 331 | */ |
| 332 | static int saveAllCursors(BtShared *pBt, Pgno iRoot, BtCursor *pExcept){ |
| 333 | BtCursor *p; |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 334 | assert( sqlite3_mutex_held(pBt->mutex) ); |
drh | d0679ed | 2007-08-28 22:24:34 +0000 | [diff] [blame] | 335 | assert( pExcept==0 || pExcept->pBt==pBt ); |
drh | 980b1a7 | 2006-08-16 16:42:48 +0000 | [diff] [blame] | 336 | for(p=pBt->pCursor; p; p=p->pNext){ |
| 337 | if( p!=pExcept && (0==iRoot || p->pgnoRoot==iRoot) && |
| 338 | p->eState==CURSOR_VALID ){ |
| 339 | int rc = saveCursorPosition(p); |
| 340 | if( SQLITE_OK!=rc ){ |
| 341 | return rc; |
| 342 | } |
| 343 | } |
| 344 | } |
| 345 | return SQLITE_OK; |
| 346 | } |
| 347 | |
| 348 | /* |
drh | bf700f3 | 2007-03-31 02:36:44 +0000 | [diff] [blame] | 349 | ** Clear the current cursor position. |
| 350 | */ |
| 351 | static void clearCursorPosition(BtCursor *pCur){ |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 352 | assert( cursorHoldsMutex(pCur) ); |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 353 | sqlite3_free(pCur->pKey); |
drh | bf700f3 | 2007-03-31 02:36:44 +0000 | [diff] [blame] | 354 | pCur->pKey = 0; |
| 355 | pCur->eState = CURSOR_INVALID; |
| 356 | } |
| 357 | |
| 358 | /* |
drh | 980b1a7 | 2006-08-16 16:42:48 +0000 | [diff] [blame] | 359 | ** Restore the cursor to the position it was in (or as close to as possible) |
| 360 | ** when saveCursorPosition() was called. Note that this call deletes the |
| 361 | ** saved position info stored by saveCursorPosition(), so there can be |
| 362 | ** at most one effective restoreOrClearCursorPosition() call after each |
| 363 | ** saveCursorPosition(). |
| 364 | ** |
| 365 | ** If the second argument argument - doSeek - is false, then instead of |
drh | 85b623f | 2007-12-13 21:54:09 +0000 | [diff] [blame] | 366 | ** returning the cursor to its saved position, any saved position is deleted |
drh | 980b1a7 | 2006-08-16 16:42:48 +0000 | [diff] [blame] | 367 | ** and the cursor state set to CURSOR_INVALID. |
| 368 | */ |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 369 | int sqlite3BtreeRestoreOrClearCursorPosition(BtCursor *pCur){ |
drh | bf700f3 | 2007-03-31 02:36:44 +0000 | [diff] [blame] | 370 | int rc; |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 371 | assert( cursorHoldsMutex(pCur) ); |
drh | fb98264 | 2007-08-30 01:19:59 +0000 | [diff] [blame] | 372 | assert( pCur->eState>=CURSOR_REQUIRESEEK ); |
| 373 | if( pCur->eState==CURSOR_FAULT ){ |
| 374 | return pCur->skip; |
| 375 | } |
danielk1977 | 32a0d8b | 2007-05-04 19:03:02 +0000 | [diff] [blame] | 376 | #ifndef SQLITE_OMIT_INCRBLOB |
danielk1977 | dcbb5d3 | 2007-05-04 18:36:44 +0000 | [diff] [blame] | 377 | if( pCur->isIncrblobHandle ){ |
| 378 | return SQLITE_ABORT; |
| 379 | } |
danielk1977 | 32a0d8b | 2007-05-04 19:03:02 +0000 | [diff] [blame] | 380 | #endif |
drh | 980b1a7 | 2006-08-16 16:42:48 +0000 | [diff] [blame] | 381 | pCur->eState = CURSOR_INVALID; |
drh | e14006d | 2008-03-25 17:23:32 +0000 | [diff] [blame] | 382 | rc = sqlite3BtreeMoveto(pCur, pCur->pKey, 0, pCur->nKey, 0, &pCur->skip); |
drh | 980b1a7 | 2006-08-16 16:42:48 +0000 | [diff] [blame] | 383 | if( rc==SQLITE_OK ){ |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 384 | sqlite3_free(pCur->pKey); |
drh | 980b1a7 | 2006-08-16 16:42:48 +0000 | [diff] [blame] | 385 | pCur->pKey = 0; |
drh | bf700f3 | 2007-03-31 02:36:44 +0000 | [diff] [blame] | 386 | assert( pCur->eState==CURSOR_VALID || pCur->eState==CURSOR_INVALID ); |
drh | 980b1a7 | 2006-08-16 16:42:48 +0000 | [diff] [blame] | 387 | } |
| 388 | return rc; |
| 389 | } |
| 390 | |
drh | bf700f3 | 2007-03-31 02:36:44 +0000 | [diff] [blame] | 391 | #define restoreOrClearCursorPosition(p) \ |
drh | fb98264 | 2007-08-30 01:19:59 +0000 | [diff] [blame] | 392 | (p->eState>=CURSOR_REQUIRESEEK ? \ |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 393 | sqlite3BtreeRestoreOrClearCursorPosition(p) : \ |
| 394 | SQLITE_OK) |
drh | 980b1a7 | 2006-08-16 16:42:48 +0000 | [diff] [blame] | 395 | |
danielk1977 | 599fcba | 2004-11-08 07:13:13 +0000 | [diff] [blame] | 396 | #ifndef SQLITE_OMIT_AUTOVACUUM |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 397 | /* |
drh | a315289 | 2007-05-05 11:48:52 +0000 | [diff] [blame] | 398 | ** Given a page number of a regular database page, return the page |
| 399 | ** number for the pointer-map page that contains the entry for the |
| 400 | ** input page number. |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 401 | */ |
danielk1977 | 266664d | 2006-02-10 08:24:21 +0000 | [diff] [blame] | 402 | static Pgno ptrmapPageno(BtShared *pBt, Pgno pgno){ |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 403 | int nPagesPerMapPage, iPtrMap, ret; |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 404 | assert( sqlite3_mutex_held(pBt->mutex) ); |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 405 | nPagesPerMapPage = (pBt->usableSize/5)+1; |
| 406 | iPtrMap = (pgno-2)/nPagesPerMapPage; |
| 407 | ret = (iPtrMap*nPagesPerMapPage) + 2; |
danielk1977 | 266664d | 2006-02-10 08:24:21 +0000 | [diff] [blame] | 408 | if( ret==PENDING_BYTE_PAGE(pBt) ){ |
| 409 | ret++; |
| 410 | } |
| 411 | return ret; |
| 412 | } |
danielk1977 | a19df67 | 2004-11-03 11:37:07 +0000 | [diff] [blame] | 413 | |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 414 | /* |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 415 | ** Write an entry into the pointer map. |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 416 | ** |
| 417 | ** This routine updates the pointer map entry for page number 'key' |
| 418 | ** so that it maps to type 'eType' and parent page number 'pgno'. |
| 419 | ** An error code is returned if something goes wrong, otherwise SQLITE_OK. |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 420 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 421 | static int ptrmapPut(BtShared *pBt, Pgno key, u8 eType, Pgno parent){ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 422 | DbPage *pDbPage; /* The pointer map page */ |
| 423 | u8 *pPtrmap; /* The pointer map data */ |
| 424 | Pgno iPtrmap; /* The pointer map page number */ |
| 425 | int offset; /* Offset in pointer map page */ |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 426 | int rc; |
| 427 | |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 428 | assert( sqlite3_mutex_held(pBt->mutex) ); |
danielk1977 | 266664d | 2006-02-10 08:24:21 +0000 | [diff] [blame] | 429 | /* The master-journal page number must never be used as a pointer map page */ |
| 430 | assert( 0==PTRMAP_ISPAGE(pBt, PENDING_BYTE_PAGE(pBt)) ); |
| 431 | |
danielk1977 | ac11ee6 | 2005-01-15 12:45:51 +0000 | [diff] [blame] | 432 | assert( pBt->autoVacuum ); |
danielk1977 | fdb7cdb | 2005-01-17 02:12:18 +0000 | [diff] [blame] | 433 | if( key==0 ){ |
drh | 4928570 | 2005-09-17 15:20:26 +0000 | [diff] [blame] | 434 | return SQLITE_CORRUPT_BKPT; |
danielk1977 | fdb7cdb | 2005-01-17 02:12:18 +0000 | [diff] [blame] | 435 | } |
danielk1977 | 266664d | 2006-02-10 08:24:21 +0000 | [diff] [blame] | 436 | iPtrmap = PTRMAP_PAGENO(pBt, key); |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 437 | rc = sqlite3PagerGet(pBt->pPager, iPtrmap, &pDbPage); |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 438 | if( rc!=SQLITE_OK ){ |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 439 | return rc; |
| 440 | } |
danielk1977 | 266664d | 2006-02-10 08:24:21 +0000 | [diff] [blame] | 441 | offset = PTRMAP_PTROFFSET(pBt, key); |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 442 | pPtrmap = (u8 *)sqlite3PagerGetData(pDbPage); |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 443 | |
drh | 615ae55 | 2005-01-16 23:21:00 +0000 | [diff] [blame] | 444 | if( eType!=pPtrmap[offset] || get4byte(&pPtrmap[offset+1])!=parent ){ |
| 445 | TRACE(("PTRMAP_UPDATE: %d->(%d,%d)\n", key, eType, parent)); |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 446 | rc = sqlite3PagerWrite(pDbPage); |
danielk1977 | 5558a8a | 2005-01-17 07:53:44 +0000 | [diff] [blame] | 447 | if( rc==SQLITE_OK ){ |
| 448 | pPtrmap[offset] = eType; |
| 449 | put4byte(&pPtrmap[offset+1], parent); |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 450 | } |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 451 | } |
| 452 | |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 453 | sqlite3PagerUnref(pDbPage); |
danielk1977 | 5558a8a | 2005-01-17 07:53:44 +0000 | [diff] [blame] | 454 | return rc; |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 455 | } |
| 456 | |
| 457 | /* |
| 458 | ** Read an entry from the pointer map. |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 459 | ** |
| 460 | ** This routine retrieves the pointer map entry for page 'key', writing |
| 461 | ** the type and parent page number to *pEType and *pPgno respectively. |
| 462 | ** An error code is returned if something goes wrong, otherwise SQLITE_OK. |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 463 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 464 | static int ptrmapGet(BtShared *pBt, Pgno key, u8 *pEType, Pgno *pPgno){ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 465 | DbPage *pDbPage; /* The pointer map page */ |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 466 | int iPtrmap; /* Pointer map page index */ |
| 467 | u8 *pPtrmap; /* Pointer map page data */ |
| 468 | int offset; /* Offset of entry in pointer map */ |
| 469 | int rc; |
| 470 | |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 471 | assert( sqlite3_mutex_held(pBt->mutex) ); |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 472 | |
danielk1977 | 266664d | 2006-02-10 08:24:21 +0000 | [diff] [blame] | 473 | iPtrmap = PTRMAP_PAGENO(pBt, key); |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 474 | rc = sqlite3PagerGet(pBt->pPager, iPtrmap, &pDbPage); |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 475 | if( rc!=0 ){ |
| 476 | return rc; |
| 477 | } |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 478 | pPtrmap = (u8 *)sqlite3PagerGetData(pDbPage); |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 479 | |
danielk1977 | 266664d | 2006-02-10 08:24:21 +0000 | [diff] [blame] | 480 | offset = PTRMAP_PTROFFSET(pBt, key); |
drh | 43617e9 | 2006-03-06 20:55:46 +0000 | [diff] [blame] | 481 | assert( pEType!=0 ); |
| 482 | *pEType = pPtrmap[offset]; |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 483 | if( pPgno ) *pPgno = get4byte(&pPtrmap[offset+1]); |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 484 | |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 485 | sqlite3PagerUnref(pDbPage); |
drh | 4928570 | 2005-09-17 15:20:26 +0000 | [diff] [blame] | 486 | if( *pEType<1 || *pEType>5 ) return SQLITE_CORRUPT_BKPT; |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 487 | return SQLITE_OK; |
| 488 | } |
| 489 | |
| 490 | #endif /* SQLITE_OMIT_AUTOVACUUM */ |
| 491 | |
drh | 0d316a4 | 2002-08-11 20:10:47 +0000 | [diff] [blame] | 492 | /* |
drh | 271efa5 | 2004-05-30 19:19:05 +0000 | [diff] [blame] | 493 | ** Given a btree page and a cell index (0 means the first cell on |
| 494 | ** the page, 1 means the second cell, and so forth) return a pointer |
| 495 | ** to the cell content. |
| 496 | ** |
| 497 | ** This routine works only for pages that do not contain overflow cells. |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 498 | */ |
danielk1977 | 1cc5ed8 | 2007-05-16 17:28:43 +0000 | [diff] [blame] | 499 | #define findCell(pPage, iCell) \ |
| 500 | ((pPage)->aData + get2byte(&(pPage)->aData[(pPage)->cellOffset+2*(iCell)])) |
drh | e6e4d6b | 2007-08-05 23:52:05 +0000 | [diff] [blame] | 501 | #ifdef SQLITE_TEST |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 502 | u8 *sqlite3BtreeFindCell(MemPage *pPage, int iCell){ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 503 | assert( iCell>=0 ); |
drh | 029f3f8 | 2007-06-20 15:14:10 +0000 | [diff] [blame] | 504 | assert( iCell<get2byte(&pPage->aData[pPage->hdrOffset+3]) ); |
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 | } |
drh | e6e4d6b | 2007-08-05 23:52:05 +0000 | [diff] [blame] | 507 | #endif |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 508 | |
| 509 | /* |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 510 | ** This a more complex version of sqlite3BtreeFindCell() that works for |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 511 | ** pages that do contain overflow cells. See insert |
| 512 | */ |
| 513 | static u8 *findOverflowCell(MemPage *pPage, int iCell){ |
| 514 | int i; |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 515 | assert( sqlite3_mutex_held(pPage->pBt->mutex) ); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 516 | for(i=pPage->nOverflow-1; i>=0; i--){ |
drh | 6d08b4d | 2004-07-20 12:45:22 +0000 | [diff] [blame] | 517 | int k; |
| 518 | struct _OvflCell *pOvfl; |
| 519 | pOvfl = &pPage->aOvfl[i]; |
| 520 | k = pOvfl->idx; |
| 521 | if( k<=iCell ){ |
| 522 | if( k==iCell ){ |
| 523 | return pOvfl->pCell; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 524 | } |
| 525 | iCell--; |
| 526 | } |
| 527 | } |
danielk1977 | 1cc5ed8 | 2007-05-16 17:28:43 +0000 | [diff] [blame] | 528 | return findCell(pPage, iCell); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 529 | } |
| 530 | |
| 531 | /* |
| 532 | ** Parse a cell content block and fill in the CellInfo structure. There |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 533 | ** are two versions of this function. sqlite3BtreeParseCell() takes a |
| 534 | ** cell index as the second argument and sqlite3BtreeParseCellPtr() |
| 535 | ** takes a pointer to the body of the cell as its second argument. |
danielk1977 | 1cc5ed8 | 2007-05-16 17:28:43 +0000 | [diff] [blame] | 536 | ** |
| 537 | ** Within this file, the parseCell() macro can be called instead of |
| 538 | ** sqlite3BtreeParseCellPtr(). Using some compilers, this will be faster. |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 539 | */ |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 540 | void sqlite3BtreeParseCellPtr( |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 541 | MemPage *pPage, /* Page containing the cell */ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 542 | u8 *pCell, /* Pointer to the cell text. */ |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 543 | CellInfo *pInfo /* Fill in this structure */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 544 | ){ |
drh | 271efa5 | 2004-05-30 19:19:05 +0000 | [diff] [blame] | 545 | int n; /* Number bytes in cell content header */ |
| 546 | u32 nPayload; /* Number of bytes of cell payload */ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 547 | |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 548 | assert( sqlite3_mutex_held(pPage->pBt->mutex) ); |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 549 | |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 550 | pInfo->pCell = pCell; |
drh | ab01f61 | 2004-05-22 02:55:23 +0000 | [diff] [blame] | 551 | assert( pPage->leaf==0 || pPage->leaf==1 ); |
drh | 271efa5 | 2004-05-30 19:19:05 +0000 | [diff] [blame] | 552 | n = pPage->childPtrSize; |
| 553 | assert( n==4-4*pPage->leaf ); |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 554 | if( pPage->hasData ){ |
shane | 3f8d5cf | 2008-04-24 19:15:09 +0000 | [diff] [blame] | 555 | n += getVarint32(&pCell[n], nPayload); |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 556 | }else{ |
drh | 271efa5 | 2004-05-30 19:19:05 +0000 | [diff] [blame] | 557 | nPayload = 0; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 558 | } |
drh | 271efa5 | 2004-05-30 19:19:05 +0000 | [diff] [blame] | 559 | pInfo->nData = nPayload; |
drh | 504b698 | 2006-01-22 21:52:56 +0000 | [diff] [blame] | 560 | if( pPage->intKey ){ |
| 561 | n += getVarint(&pCell[n], (u64 *)&pInfo->nKey); |
| 562 | }else{ |
| 563 | u32 x; |
shane | 3f8d5cf | 2008-04-24 19:15:09 +0000 | [diff] [blame] | 564 | n += getVarint32(&pCell[n], x); |
drh | 504b698 | 2006-01-22 21:52:56 +0000 | [diff] [blame] | 565 | pInfo->nKey = x; |
| 566 | nPayload += x; |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 567 | } |
drh | 7236583 | 2007-03-06 15:53:44 +0000 | [diff] [blame] | 568 | pInfo->nPayload = nPayload; |
drh | 504b698 | 2006-01-22 21:52:56 +0000 | [diff] [blame] | 569 | pInfo->nHeader = n; |
drh | 271efa5 | 2004-05-30 19:19:05 +0000 | [diff] [blame] | 570 | if( nPayload<=pPage->maxLocal ){ |
| 571 | /* This is the (easy) common case where the entire payload fits |
| 572 | ** on the local page. No overflow is required. |
| 573 | */ |
| 574 | int nSize; /* Total size of cell content in bytes */ |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 575 | pInfo->nLocal = nPayload; |
| 576 | pInfo->iOverflow = 0; |
drh | 271efa5 | 2004-05-30 19:19:05 +0000 | [diff] [blame] | 577 | nSize = nPayload + n; |
| 578 | if( nSize<4 ){ |
| 579 | nSize = 4; /* Minimum cell size is 4 */ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 580 | } |
drh | 271efa5 | 2004-05-30 19:19:05 +0000 | [diff] [blame] | 581 | pInfo->nSize = nSize; |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 582 | }else{ |
drh | 271efa5 | 2004-05-30 19:19:05 +0000 | [diff] [blame] | 583 | /* If the payload will not fit completely on the local page, we have |
| 584 | ** to decide how much to store locally and how much to spill onto |
| 585 | ** overflow pages. The strategy is to minimize the amount of unused |
| 586 | ** space on overflow pages while keeping the amount of local storage |
| 587 | ** in between minLocal and maxLocal. |
| 588 | ** |
| 589 | ** Warning: changing the way overflow payload is distributed in any |
| 590 | ** way will result in an incompatible file format. |
| 591 | */ |
| 592 | int minLocal; /* Minimum amount of payload held locally */ |
| 593 | int maxLocal; /* Maximum amount of payload held locally */ |
| 594 | int surplus; /* Overflow payload available for local storage */ |
| 595 | |
| 596 | minLocal = pPage->minLocal; |
| 597 | maxLocal = pPage->maxLocal; |
| 598 | surplus = minLocal + (nPayload - minLocal)%(pPage->pBt->usableSize - 4); |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 599 | if( surplus <= maxLocal ){ |
| 600 | pInfo->nLocal = surplus; |
| 601 | }else{ |
| 602 | pInfo->nLocal = minLocal; |
| 603 | } |
| 604 | pInfo->iOverflow = pInfo->nLocal + n; |
| 605 | pInfo->nSize = pInfo->iOverflow + 4; |
| 606 | } |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 607 | } |
danielk1977 | 1cc5ed8 | 2007-05-16 17:28:43 +0000 | [diff] [blame] | 608 | #define parseCell(pPage, iCell, pInfo) \ |
| 609 | sqlite3BtreeParseCellPtr((pPage), findCell((pPage), (iCell)), (pInfo)) |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 610 | void sqlite3BtreeParseCell( |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 611 | MemPage *pPage, /* Page containing the cell */ |
| 612 | int iCell, /* The cell index. First cell is 0 */ |
| 613 | CellInfo *pInfo /* Fill in this structure */ |
| 614 | ){ |
danielk1977 | 1cc5ed8 | 2007-05-16 17:28:43 +0000 | [diff] [blame] | 615 | parseCell(pPage, iCell, pInfo); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 616 | } |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 617 | |
| 618 | /* |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 619 | ** Compute the total number of bytes that a Cell needs in the cell |
| 620 | ** data area of the btree-page. The return number includes the cell |
| 621 | ** data header and the local payload, but not any overflow page or |
| 622 | ** the space used by the cell pointer. |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 623 | */ |
danielk1977 | bc6ada4 | 2004-06-30 08:20:16 +0000 | [diff] [blame] | 624 | #ifndef NDEBUG |
drh | a9121e4 | 2008-02-19 14:59:35 +0000 | [diff] [blame] | 625 | static u16 cellSize(MemPage *pPage, int iCell){ |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 626 | CellInfo info; |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 627 | sqlite3BtreeParseCell(pPage, iCell, &info); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 628 | return info.nSize; |
| 629 | } |
danielk1977 | bc6ada4 | 2004-06-30 08:20:16 +0000 | [diff] [blame] | 630 | #endif |
drh | a9121e4 | 2008-02-19 14:59:35 +0000 | [diff] [blame] | 631 | static u16 cellSizePtr(MemPage *pPage, u8 *pCell){ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 632 | CellInfo info; |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 633 | sqlite3BtreeParseCellPtr(pPage, pCell, &info); |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 634 | return info.nSize; |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 635 | } |
| 636 | |
danielk1977 | 79a40da | 2005-01-16 08:00:01 +0000 | [diff] [blame] | 637 | #ifndef SQLITE_OMIT_AUTOVACUUM |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 638 | /* |
danielk1977 | 2683665 | 2005-01-17 01:33:13 +0000 | [diff] [blame] | 639 | ** If the cell pCell, part of page pPage contains a pointer |
danielk1977 | 79a40da | 2005-01-16 08:00:01 +0000 | [diff] [blame] | 640 | ** to an overflow page, insert an entry into the pointer-map |
| 641 | ** for the overflow page. |
danielk1977 | ac11ee6 | 2005-01-15 12:45:51 +0000 | [diff] [blame] | 642 | */ |
danielk1977 | 2683665 | 2005-01-17 01:33:13 +0000 | [diff] [blame] | 643 | static int ptrmapPutOvflPtr(MemPage *pPage, u8 *pCell){ |
danielk1977 | 79a40da | 2005-01-16 08:00:01 +0000 | [diff] [blame] | 644 | if( pCell ){ |
| 645 | CellInfo info; |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 646 | sqlite3BtreeParseCellPtr(pPage, pCell, &info); |
drh | 7236583 | 2007-03-06 15:53:44 +0000 | [diff] [blame] | 647 | assert( (info.nData+(pPage->intKey?0:info.nKey))==info.nPayload ); |
danielk1977 | 79a40da | 2005-01-16 08:00:01 +0000 | [diff] [blame] | 648 | if( (info.nData+(pPage->intKey?0:info.nKey))>info.nLocal ){ |
| 649 | Pgno ovfl = get4byte(&pCell[info.iOverflow]); |
| 650 | return ptrmapPut(pPage->pBt, ovfl, PTRMAP_OVERFLOW1, pPage->pgno); |
| 651 | } |
danielk1977 | ac11ee6 | 2005-01-15 12:45:51 +0000 | [diff] [blame] | 652 | } |
danielk1977 | 79a40da | 2005-01-16 08:00:01 +0000 | [diff] [blame] | 653 | return SQLITE_OK; |
danielk1977 | ac11ee6 | 2005-01-15 12:45:51 +0000 | [diff] [blame] | 654 | } |
danielk1977 | 2683665 | 2005-01-17 01:33:13 +0000 | [diff] [blame] | 655 | /* |
| 656 | ** If the cell with index iCell on page pPage contains a pointer |
| 657 | ** to an overflow page, insert an entry into the pointer-map |
| 658 | ** for the overflow page. |
| 659 | */ |
| 660 | static int ptrmapPutOvfl(MemPage *pPage, int iCell){ |
| 661 | u8 *pCell; |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 662 | assert( sqlite3_mutex_held(pPage->pBt->mutex) ); |
danielk1977 | 2683665 | 2005-01-17 01:33:13 +0000 | [diff] [blame] | 663 | pCell = findOverflowCell(pPage, iCell); |
| 664 | return ptrmapPutOvflPtr(pPage, pCell); |
| 665 | } |
danielk1977 | 79a40da | 2005-01-16 08:00:01 +0000 | [diff] [blame] | 666 | #endif |
| 667 | |
danielk1977 | ac11ee6 | 2005-01-15 12:45:51 +0000 | [diff] [blame] | 668 | |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 669 | /* |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 670 | ** Defragment the page given. All Cells are moved to the |
drh | 3a4a2d4 | 2005-11-24 14:24:28 +0000 | [diff] [blame] | 671 | ** end of the page and all free space is collected into one |
| 672 | ** big FreeBlk that occurs in between the header and cell |
drh | 31beae9 | 2005-11-24 14:34:36 +0000 | [diff] [blame] | 673 | ** pointer array and the cell content area. |
drh | 365d68f | 2001-05-11 11:02:46 +0000 | [diff] [blame] | 674 | */ |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 675 | static int defragmentPage(MemPage *pPage){ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 676 | int i; /* Loop counter */ |
| 677 | int pc; /* Address of a i-th cell */ |
| 678 | int addr; /* Offset of first byte after cell pointer array */ |
| 679 | int hdr; /* Offset to the page header */ |
| 680 | int size; /* Size of a cell */ |
| 681 | int usableSize; /* Number of usable bytes on a page */ |
| 682 | int cellOffset; /* Offset to the cell pointer array */ |
| 683 | int brk; /* Offset to the cell content area */ |
| 684 | int nCell; /* Number of cells on the page */ |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 685 | unsigned char *data; /* The page data */ |
| 686 | unsigned char *temp; /* Temp area for cell content */ |
drh | 2af926b | 2001-05-15 00:39:25 +0000 | [diff] [blame] | 687 | |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 688 | assert( sqlite3PagerIswriteable(pPage->pDbPage) ); |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 689 | assert( pPage->pBt!=0 ); |
drh | 90f5ecb | 2004-07-22 01:19:35 +0000 | [diff] [blame] | 690 | assert( pPage->pBt->usableSize <= SQLITE_MAX_PAGE_SIZE ); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 691 | assert( pPage->nOverflow==0 ); |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 692 | assert( sqlite3_mutex_held(pPage->pBt->mutex) ); |
drh | 26b7994 | 2007-11-28 16:19:56 +0000 | [diff] [blame] | 693 | temp = sqlite3PagerTempSpace(pPage->pBt->pPager); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 694 | data = pPage->aData; |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 695 | hdr = pPage->hdrOffset; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 696 | cellOffset = pPage->cellOffset; |
| 697 | nCell = pPage->nCell; |
| 698 | assert( nCell==get2byte(&data[hdr+3]) ); |
| 699 | usableSize = pPage->pBt->usableSize; |
| 700 | brk = get2byte(&data[hdr+5]); |
| 701 | memcpy(&temp[brk], &data[brk], usableSize - brk); |
| 702 | brk = usableSize; |
| 703 | for(i=0; i<nCell; i++){ |
| 704 | u8 *pAddr; /* The i-th cell pointer */ |
| 705 | pAddr = &data[cellOffset + i*2]; |
| 706 | pc = get2byte(pAddr); |
| 707 | assert( pc<pPage->pBt->usableSize ); |
| 708 | size = cellSizePtr(pPage, &temp[pc]); |
| 709 | brk -= size; |
| 710 | memcpy(&data[brk], &temp[pc], size); |
| 711 | put2byte(pAddr, brk); |
drh | 2af926b | 2001-05-15 00:39:25 +0000 | [diff] [blame] | 712 | } |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 713 | assert( brk>=cellOffset+2*nCell ); |
| 714 | put2byte(&data[hdr+5], brk); |
| 715 | data[hdr+1] = 0; |
| 716 | data[hdr+2] = 0; |
| 717 | data[hdr+7] = 0; |
| 718 | addr = cellOffset+2*nCell; |
| 719 | memset(&data[addr], 0, brk-addr); |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 720 | return SQLITE_OK; |
drh | 365d68f | 2001-05-11 11:02:46 +0000 | [diff] [blame] | 721 | } |
| 722 | |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 723 | /* |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 724 | ** Allocate nByte bytes of space on a page. |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 725 | ** |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 726 | ** Return the index into pPage->aData[] of the first byte of |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 727 | ** the new allocation. Or return 0 if there is not enough free |
| 728 | ** space on the page to satisfy the allocation request. |
drh | 2af926b | 2001-05-15 00:39:25 +0000 | [diff] [blame] | 729 | ** |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 730 | ** If the page contains nBytes of free space but does not contain |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 731 | ** nBytes of contiguous free space, then this routine automatically |
| 732 | ** calls defragementPage() to consolidate all free space before |
| 733 | ** allocating the new chunk. |
drh | 7e3b0a0 | 2001-04-28 16:52:40 +0000 | [diff] [blame] | 734 | */ |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 735 | static int allocateSpace(MemPage *pPage, int nByte){ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 736 | int addr, pc, hdr; |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 737 | int size; |
drh | 24cd67e | 2004-05-10 16:18:47 +0000 | [diff] [blame] | 738 | int nFrag; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 739 | int top; |
| 740 | int nCell; |
| 741 | int cellOffset; |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 742 | unsigned char *data; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 743 | |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 744 | data = pPage->aData; |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 745 | assert( sqlite3PagerIswriteable(pPage->pDbPage) ); |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 746 | assert( pPage->pBt ); |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 747 | assert( sqlite3_mutex_held(pPage->pBt->mutex) ); |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 748 | if( nByte<4 ) nByte = 4; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 749 | if( pPage->nFree<nByte || pPage->nOverflow>0 ) return 0; |
| 750 | pPage->nFree -= nByte; |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 751 | hdr = pPage->hdrOffset; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 752 | |
| 753 | nFrag = data[hdr+7]; |
| 754 | if( nFrag<60 ){ |
| 755 | /* Search the freelist looking for a slot big enough to satisfy the |
| 756 | ** space request. */ |
| 757 | addr = hdr+1; |
| 758 | while( (pc = get2byte(&data[addr]))>0 ){ |
| 759 | size = get2byte(&data[pc+2]); |
| 760 | if( size>=nByte ){ |
| 761 | if( size<nByte+4 ){ |
| 762 | memcpy(&data[addr], &data[pc], 2); |
| 763 | data[hdr+7] = nFrag + size - nByte; |
| 764 | return pc; |
| 765 | }else{ |
| 766 | put2byte(&data[pc+2], size-nByte); |
| 767 | return pc + size - nByte; |
| 768 | } |
| 769 | } |
| 770 | addr = pc; |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 771 | } |
| 772 | } |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 773 | |
| 774 | /* Allocate memory from the gap in between the cell pointer array |
| 775 | ** and the cell content area. |
| 776 | */ |
| 777 | top = get2byte(&data[hdr+5]); |
| 778 | nCell = get2byte(&data[hdr+3]); |
| 779 | cellOffset = pPage->cellOffset; |
| 780 | if( nFrag>=60 || cellOffset + 2*nCell > top - nByte ){ |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 781 | if( defragmentPage(pPage) ) return 0; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 782 | top = get2byte(&data[hdr+5]); |
drh | 2af926b | 2001-05-15 00:39:25 +0000 | [diff] [blame] | 783 | } |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 784 | top -= nByte; |
| 785 | assert( cellOffset + 2*nCell <= top ); |
| 786 | put2byte(&data[hdr+5], top); |
| 787 | return top; |
drh | 7e3b0a0 | 2001-04-28 16:52:40 +0000 | [diff] [blame] | 788 | } |
| 789 | |
| 790 | /* |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 791 | ** Return a section of the pPage->aData to the freelist. |
| 792 | ** The first byte of the new free block is pPage->aDisk[start] |
| 793 | ** and the size of the block is "size" bytes. |
drh | 306dc21 | 2001-05-21 13:45:10 +0000 | [diff] [blame] | 794 | ** |
| 795 | ** Most of the effort here is involved in coalesing adjacent |
| 796 | ** free blocks into a single big free block. |
drh | 7e3b0a0 | 2001-04-28 16:52:40 +0000 | [diff] [blame] | 797 | */ |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 798 | static void freeSpace(MemPage *pPage, int start, int size){ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 799 | int addr, pbegin, hdr; |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 800 | unsigned char *data = pPage->aData; |
drh | 2af926b | 2001-05-15 00:39:25 +0000 | [diff] [blame] | 801 | |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 802 | assert( pPage->pBt!=0 ); |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 803 | assert( sqlite3PagerIswriteable(pPage->pDbPage) ); |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 804 | assert( start>=pPage->hdrOffset+6+(pPage->leaf?0:4) ); |
danielk1977 | bc6ada4 | 2004-06-30 08:20:16 +0000 | [diff] [blame] | 805 | assert( (start + size)<=pPage->pBt->usableSize ); |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 806 | assert( sqlite3_mutex_held(pPage->pBt->mutex) ); |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 807 | if( size<4 ) size = 4; |
| 808 | |
drh | fcce93f | 2006-02-22 03:08:32 +0000 | [diff] [blame] | 809 | #ifdef SQLITE_SECURE_DELETE |
| 810 | /* Overwrite deleted information with zeros when the SECURE_DELETE |
| 811 | ** option is enabled at compile-time */ |
| 812 | memset(&data[start], 0, size); |
| 813 | #endif |
| 814 | |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 815 | /* Add the space back into the linked list of freeblocks */ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 816 | hdr = pPage->hdrOffset; |
| 817 | addr = hdr + 1; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 818 | while( (pbegin = get2byte(&data[addr]))<start && pbegin>0 ){ |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 819 | assert( pbegin<=pPage->pBt->usableSize-4 ); |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 820 | assert( pbegin>addr ); |
| 821 | addr = pbegin; |
drh | 2af926b | 2001-05-15 00:39:25 +0000 | [diff] [blame] | 822 | } |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 823 | assert( pbegin<=pPage->pBt->usableSize-4 ); |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 824 | assert( pbegin>addr || pbegin==0 ); |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 825 | put2byte(&data[addr], start); |
| 826 | put2byte(&data[start], pbegin); |
| 827 | put2byte(&data[start+2], size); |
drh | 2af926b | 2001-05-15 00:39:25 +0000 | [diff] [blame] | 828 | pPage->nFree += size; |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 829 | |
| 830 | /* Coalesce adjacent free blocks */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 831 | addr = pPage->hdrOffset + 1; |
| 832 | while( (pbegin = get2byte(&data[addr]))>0 ){ |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 833 | int pnext, psize; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 834 | assert( pbegin>addr ); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 835 | assert( pbegin<=pPage->pBt->usableSize-4 ); |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 836 | pnext = get2byte(&data[pbegin]); |
| 837 | psize = get2byte(&data[pbegin+2]); |
| 838 | if( pbegin + psize + 3 >= pnext && pnext>0 ){ |
| 839 | int frag = pnext - (pbegin+psize); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 840 | assert( frag<=data[pPage->hdrOffset+7] ); |
| 841 | data[pPage->hdrOffset+7] -= frag; |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 842 | put2byte(&data[pbegin], get2byte(&data[pnext])); |
| 843 | put2byte(&data[pbegin+2], pnext+get2byte(&data[pnext+2])-pbegin); |
| 844 | }else{ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 845 | addr = pbegin; |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 846 | } |
| 847 | } |
drh | 7e3b0a0 | 2001-04-28 16:52:40 +0000 | [diff] [blame] | 848 | |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 849 | /* If the cell content area begins with a freeblock, remove it. */ |
| 850 | if( data[hdr+1]==data[hdr+5] && data[hdr+2]==data[hdr+6] ){ |
| 851 | int top; |
| 852 | pbegin = get2byte(&data[hdr+1]); |
| 853 | memcpy(&data[hdr+1], &data[pbegin], 2); |
| 854 | top = get2byte(&data[hdr+5]); |
| 855 | put2byte(&data[hdr+5], top + get2byte(&data[pbegin+2])); |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 856 | } |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 857 | } |
| 858 | |
| 859 | /* |
drh | 271efa5 | 2004-05-30 19:19:05 +0000 | [diff] [blame] | 860 | ** Decode the flags byte (the first byte of the header) for a page |
| 861 | ** and initialize fields of the MemPage structure accordingly. |
| 862 | */ |
| 863 | static void decodeFlags(MemPage *pPage, int flagByte){ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 864 | BtShared *pBt; /* A copy of pPage->pBt */ |
drh | 271efa5 | 2004-05-30 19:19:05 +0000 | [diff] [blame] | 865 | |
| 866 | assert( pPage->hdrOffset==(pPage->pgno==1 ? 100 : 0) ); |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 867 | assert( sqlite3_mutex_held(pPage->pBt->mutex) ); |
drh | 271efa5 | 2004-05-30 19:19:05 +0000 | [diff] [blame] | 868 | pPage->intKey = (flagByte & (PTF_INTKEY|PTF_LEAFDATA))!=0; |
| 869 | pPage->zeroData = (flagByte & PTF_ZERODATA)!=0; |
| 870 | pPage->leaf = (flagByte & PTF_LEAF)!=0; |
| 871 | pPage->childPtrSize = 4*(pPage->leaf==0); |
| 872 | pBt = pPage->pBt; |
| 873 | if( flagByte & PTF_LEAFDATA ){ |
| 874 | pPage->leafData = 1; |
| 875 | pPage->maxLocal = pBt->maxLeaf; |
| 876 | pPage->minLocal = pBt->minLeaf; |
| 877 | }else{ |
| 878 | pPage->leafData = 0; |
| 879 | pPage->maxLocal = pBt->maxLocal; |
| 880 | pPage->minLocal = pBt->minLocal; |
| 881 | } |
| 882 | pPage->hasData = !(pPage->zeroData || (!pPage->leaf && pPage->leafData)); |
| 883 | } |
| 884 | |
| 885 | /* |
drh | 7e3b0a0 | 2001-04-28 16:52:40 +0000 | [diff] [blame] | 886 | ** Initialize the auxiliary information for a disk block. |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 887 | ** |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 888 | ** The pParent parameter must be a pointer to the MemPage which |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 889 | ** is the parent of the page being initialized. The root of a |
| 890 | ** BTree has no parent and so for that page, pParent==NULL. |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 891 | ** |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 892 | ** Return SQLITE_OK on success. If we see that the page does |
drh | da47d77 | 2002-12-02 04:25:19 +0000 | [diff] [blame] | 893 | ** not contain a well-formed database page, then return |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 894 | ** SQLITE_CORRUPT. Note that a return of SQLITE_OK does not |
| 895 | ** guarantee that the page is well-formed. It only shows that |
| 896 | ** we failed to detect any corruption. |
drh | 7e3b0a0 | 2001-04-28 16:52:40 +0000 | [diff] [blame] | 897 | */ |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 898 | int sqlite3BtreeInitPage( |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 899 | MemPage *pPage, /* The page to be initialized */ |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 900 | MemPage *pParent /* The parent. Might be NULL */ |
| 901 | ){ |
drh | 271efa5 | 2004-05-30 19:19:05 +0000 | [diff] [blame] | 902 | int pc; /* Address of a freeblock within pPage->aData[] */ |
drh | 271efa5 | 2004-05-30 19:19:05 +0000 | [diff] [blame] | 903 | int hdr; /* Offset to beginning of page header */ |
| 904 | u8 *data; /* Equal to pPage->aData */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 905 | BtShared *pBt; /* The main btree structure */ |
drh | 271efa5 | 2004-05-30 19:19:05 +0000 | [diff] [blame] | 906 | int usableSize; /* Amount of usable space on each page */ |
| 907 | int cellOffset; /* Offset from start of page to first cell pointer */ |
| 908 | int nFree; /* Number of unused bytes on the page */ |
| 909 | int top; /* First byte of the cell content area */ |
drh | 2af926b | 2001-05-15 00:39:25 +0000 | [diff] [blame] | 910 | |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 911 | pBt = pPage->pBt; |
| 912 | assert( pBt!=0 ); |
| 913 | assert( pParent==0 || pParent->pBt==pBt ); |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 914 | assert( sqlite3_mutex_held(pBt->mutex) ); |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 915 | assert( pPage->pgno==sqlite3PagerPagenumber(pPage->pDbPage) ); |
drh | bf4bca5 | 2007-09-06 22:19:14 +0000 | [diff] [blame] | 916 | assert( pPage == sqlite3PagerGetExtra(pPage->pDbPage) ); |
| 917 | assert( pPage->aData == sqlite3PagerGetData(pPage->pDbPage) ); |
drh | ee696e2 | 2004-08-30 16:52:17 +0000 | [diff] [blame] | 918 | if( pPage->pParent!=pParent && (pPage->pParent!=0 || pPage->isInit) ){ |
| 919 | /* The parent page should never change unless the file is corrupt */ |
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 | 10617cd | 2004-05-14 15:27:27 +0000 | [diff] [blame] | 922 | if( pPage->isInit ) return SQLITE_OK; |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 923 | if( pPage->pParent==0 && pParent!=0 ){ |
| 924 | pPage->pParent = pParent; |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 925 | sqlite3PagerRef(pParent->pDbPage); |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 926 | } |
drh | de64713 | 2004-05-07 17:57:49 +0000 | [diff] [blame] | 927 | hdr = pPage->hdrOffset; |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 928 | data = pPage->aData; |
drh | 271efa5 | 2004-05-30 19:19:05 +0000 | [diff] [blame] | 929 | decodeFlags(pPage, data[hdr]); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 930 | pPage->nOverflow = 0; |
drh | c8629a1 | 2004-05-08 20:07:40 +0000 | [diff] [blame] | 931 | pPage->idxShift = 0; |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 932 | usableSize = pBt->usableSize; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 933 | pPage->cellOffset = cellOffset = hdr + 12 - 4*pPage->leaf; |
| 934 | top = get2byte(&data[hdr+5]); |
| 935 | pPage->nCell = get2byte(&data[hdr+3]); |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 936 | if( pPage->nCell>MX_CELL(pBt) ){ |
drh | ee696e2 | 2004-08-30 16:52:17 +0000 | [diff] [blame] | 937 | /* To many cells for a single page. The page must be corrupt */ |
drh | 4928570 | 2005-09-17 15:20:26 +0000 | [diff] [blame] | 938 | return SQLITE_CORRUPT_BKPT; |
drh | ee696e2 | 2004-08-30 16:52:17 +0000 | [diff] [blame] | 939 | } |
| 940 | if( pPage->nCell==0 && pParent!=0 && pParent->pgno!=1 ){ |
| 941 | /* All pages must have at least one cell, except for root pages */ |
drh | 4928570 | 2005-09-17 15:20:26 +0000 | [diff] [blame] | 942 | return SQLITE_CORRUPT_BKPT; |
drh | ee696e2 | 2004-08-30 16:52:17 +0000 | [diff] [blame] | 943 | } |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 944 | |
| 945 | /* Compute the total free space on the page */ |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 946 | pc = get2byte(&data[hdr+1]); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 947 | nFree = data[hdr+7] + top - (cellOffset + 2*pPage->nCell); |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 948 | while( pc>0 ){ |
| 949 | int next, size; |
drh | ee696e2 | 2004-08-30 16:52:17 +0000 | [diff] [blame] | 950 | if( pc>usableSize-4 ){ |
| 951 | /* Free block is off the page */ |
drh | 4928570 | 2005-09-17 15:20:26 +0000 | [diff] [blame] | 952 | return SQLITE_CORRUPT_BKPT; |
drh | ee696e2 | 2004-08-30 16:52:17 +0000 | [diff] [blame] | 953 | } |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 954 | next = get2byte(&data[pc]); |
| 955 | size = get2byte(&data[pc+2]); |
drh | ee696e2 | 2004-08-30 16:52:17 +0000 | [diff] [blame] | 956 | if( next>0 && next<=pc+size+3 ){ |
| 957 | /* Free blocks must be in accending order */ |
drh | 4928570 | 2005-09-17 15:20:26 +0000 | [diff] [blame] | 958 | return SQLITE_CORRUPT_BKPT; |
drh | ee696e2 | 2004-08-30 16:52:17 +0000 | [diff] [blame] | 959 | } |
drh | 3add367 | 2004-05-15 00:29:24 +0000 | [diff] [blame] | 960 | nFree += size; |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 961 | pc = next; |
| 962 | } |
drh | 3add367 | 2004-05-15 00:29:24 +0000 | [diff] [blame] | 963 | pPage->nFree = nFree; |
drh | ee696e2 | 2004-08-30 16:52:17 +0000 | [diff] [blame] | 964 | if( nFree>=usableSize ){ |
| 965 | /* Free space cannot exceed total page size */ |
drh | 4928570 | 2005-09-17 15:20:26 +0000 | [diff] [blame] | 966 | return SQLITE_CORRUPT_BKPT; |
drh | ee696e2 | 2004-08-30 16:52:17 +0000 | [diff] [blame] | 967 | } |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 968 | |
drh | de64713 | 2004-05-07 17:57:49 +0000 | [diff] [blame] | 969 | pPage->isInit = 1; |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 970 | return SQLITE_OK; |
drh | 7e3b0a0 | 2001-04-28 16:52:40 +0000 | [diff] [blame] | 971 | } |
| 972 | |
| 973 | /* |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 974 | ** Set up a raw page so that it looks like a database page holding |
| 975 | ** no entries. |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 976 | */ |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 977 | static void zeroPage(MemPage *pPage, int flags){ |
| 978 | unsigned char *data = pPage->aData; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 979 | BtShared *pBt = pPage->pBt; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 980 | int hdr = pPage->hdrOffset; |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 981 | int first; |
| 982 | |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 983 | assert( sqlite3PagerPagenumber(pPage->pDbPage)==pPage->pgno ); |
drh | bf4bca5 | 2007-09-06 22:19:14 +0000 | [diff] [blame] | 984 | assert( sqlite3PagerGetExtra(pPage->pDbPage) == (void*)pPage ); |
| 985 | assert( sqlite3PagerGetData(pPage->pDbPage) == data ); |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 986 | assert( sqlite3PagerIswriteable(pPage->pDbPage) ); |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 987 | assert( sqlite3_mutex_held(pBt->mutex) ); |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 988 | memset(&data[hdr], 0, pBt->usableSize - hdr); |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 989 | data[hdr] = flags; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 990 | first = hdr + 8 + 4*((flags&PTF_LEAF)==0); |
| 991 | memset(&data[hdr+1], 0, 4); |
| 992 | data[hdr+7] = 0; |
| 993 | put2byte(&data[hdr+5], pBt->usableSize); |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 994 | pPage->nFree = pBt->usableSize - first; |
drh | 271efa5 | 2004-05-30 19:19:05 +0000 | [diff] [blame] | 995 | decodeFlags(pPage, flags); |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 996 | pPage->hdrOffset = hdr; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 997 | pPage->cellOffset = first; |
| 998 | pPage->nOverflow = 0; |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 999 | pPage->idxShift = 0; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 1000 | pPage->nCell = 0; |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 1001 | pPage->isInit = 1; |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 1002 | } |
| 1003 | |
| 1004 | /* |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1005 | ** Get a page from the pager. Initialize the MemPage.pBt and |
| 1006 | ** MemPage.aData elements if needed. |
drh | 538f570 | 2007-04-13 02:14:30 +0000 | [diff] [blame] | 1007 | ** |
| 1008 | ** If the noContent flag is set, it means that we do not care about |
| 1009 | ** the content of the page at this time. So do not go to the disk |
| 1010 | ** to fetch the content. Just fill in the content with zeros for now. |
| 1011 | ** If in the future we call sqlite3PagerWrite() on this page, that |
| 1012 | ** means we have started to be concerned about content and the disk |
| 1013 | ** read should occur at that point. |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1014 | */ |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 1015 | int sqlite3BtreeGetPage( |
| 1016 | BtShared *pBt, /* The btree */ |
| 1017 | Pgno pgno, /* Number of the page to fetch */ |
| 1018 | MemPage **ppPage, /* Return the page in this parameter */ |
| 1019 | int noContent /* Do not load page content if true */ |
| 1020 | ){ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1021 | int rc; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1022 | MemPage *pPage; |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 1023 | DbPage *pDbPage; |
| 1024 | |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 1025 | assert( sqlite3_mutex_held(pBt->mutex) ); |
drh | 538f570 | 2007-04-13 02:14:30 +0000 | [diff] [blame] | 1026 | rc = sqlite3PagerAcquire(pBt->pPager, pgno, (DbPage**)&pDbPage, noContent); |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1027 | if( rc ) return rc; |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 1028 | pPage = (MemPage *)sqlite3PagerGetExtra(pDbPage); |
| 1029 | pPage->aData = sqlite3PagerGetData(pDbPage); |
| 1030 | pPage->pDbPage = pDbPage; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1031 | pPage->pBt = pBt; |
| 1032 | pPage->pgno = pgno; |
drh | de64713 | 2004-05-07 17:57:49 +0000 | [diff] [blame] | 1033 | pPage->hdrOffset = pPage->pgno==1 ? 100 : 0; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1034 | *ppPage = pPage; |
| 1035 | return SQLITE_OK; |
| 1036 | } |
| 1037 | |
| 1038 | /* |
drh | de64713 | 2004-05-07 17:57:49 +0000 | [diff] [blame] | 1039 | ** Get a page from the pager and initialize it. This routine |
| 1040 | ** is just a convenience wrapper around separate calls to |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 1041 | ** sqlite3BtreeGetPage() and sqlite3BtreeInitPage(). |
drh | de64713 | 2004-05-07 17:57:49 +0000 | [diff] [blame] | 1042 | */ |
| 1043 | static int getAndInitPage( |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1044 | BtShared *pBt, /* The database file */ |
drh | de64713 | 2004-05-07 17:57:49 +0000 | [diff] [blame] | 1045 | Pgno pgno, /* Number of the page to get */ |
| 1046 | MemPage **ppPage, /* Write the page pointer here */ |
| 1047 | MemPage *pParent /* Parent of the page */ |
| 1048 | ){ |
| 1049 | int rc; |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 1050 | assert( sqlite3_mutex_held(pBt->mutex) ); |
drh | ee696e2 | 2004-08-30 16:52:17 +0000 | [diff] [blame] | 1051 | if( pgno==0 ){ |
drh | 4928570 | 2005-09-17 15:20:26 +0000 | [diff] [blame] | 1052 | return SQLITE_CORRUPT_BKPT; |
drh | ee696e2 | 2004-08-30 16:52:17 +0000 | [diff] [blame] | 1053 | } |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 1054 | rc = sqlite3BtreeGetPage(pBt, pgno, ppPage, 0); |
drh | 10617cd | 2004-05-14 15:27:27 +0000 | [diff] [blame] | 1055 | if( rc==SQLITE_OK && (*ppPage)->isInit==0 ){ |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 1056 | rc = sqlite3BtreeInitPage(*ppPage, pParent); |
danielk1977 | 43e377a | 2008-05-05 12:09:32 +0000 | [diff] [blame] | 1057 | if( rc!=SQLITE_OK ){ |
| 1058 | releasePage(*ppPage); |
| 1059 | *ppPage = 0; |
| 1060 | } |
drh | de64713 | 2004-05-07 17:57:49 +0000 | [diff] [blame] | 1061 | } |
| 1062 | return rc; |
| 1063 | } |
| 1064 | |
| 1065 | /* |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1066 | ** Release a MemPage. This should be called once for each prior |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 1067 | ** call to sqlite3BtreeGetPage. |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1068 | */ |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 1069 | static void releasePage(MemPage *pPage){ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1070 | if( pPage ){ |
| 1071 | assert( pPage->aData ); |
| 1072 | assert( pPage->pBt ); |
drh | bf4bca5 | 2007-09-06 22:19:14 +0000 | [diff] [blame] | 1073 | assert( sqlite3PagerGetExtra(pPage->pDbPage) == (void*)pPage ); |
| 1074 | assert( sqlite3PagerGetData(pPage->pDbPage)==pPage->aData ); |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 1075 | assert( sqlite3_mutex_held(pPage->pBt->mutex) ); |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 1076 | sqlite3PagerUnref(pPage->pDbPage); |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1077 | } |
| 1078 | } |
| 1079 | |
| 1080 | /* |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 1081 | ** This routine is called when the reference count for a page |
| 1082 | ** reaches zero. We need to unref the pParent pointer when that |
| 1083 | ** happens. |
| 1084 | */ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 1085 | static void pageDestructor(DbPage *pData, int pageSize){ |
drh | 07d183d | 2005-05-01 22:52:42 +0000 | [diff] [blame] | 1086 | MemPage *pPage; |
| 1087 | assert( (pageSize & 7)==0 ); |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 1088 | pPage = (MemPage *)sqlite3PagerGetExtra(pData); |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 1089 | assert( pPage->isInit==0 || sqlite3_mutex_held(pPage->pBt->mutex) ); |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 1090 | if( pPage->pParent ){ |
| 1091 | MemPage *pParent = pPage->pParent; |
drh | d0679ed | 2007-08-28 22:24:34 +0000 | [diff] [blame] | 1092 | assert( pParent->pBt==pPage->pBt ); |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 1093 | pPage->pParent = 0; |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 1094 | releasePage(pParent); |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 1095 | } |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1096 | pPage->isInit = 0; |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 1097 | } |
| 1098 | |
| 1099 | /* |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 1100 | ** During a rollback, when the pager reloads information into the cache |
| 1101 | ** so that the cache is restored to its original state at the start of |
| 1102 | ** the transaction, for each page restored this routine is called. |
| 1103 | ** |
| 1104 | ** This routine needs to reset the extra data section at the end of the |
| 1105 | ** page to agree with the restored data. |
| 1106 | */ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 1107 | static void pageReinit(DbPage *pData, int pageSize){ |
drh | 07d183d | 2005-05-01 22:52:42 +0000 | [diff] [blame] | 1108 | MemPage *pPage; |
| 1109 | assert( (pageSize & 7)==0 ); |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 1110 | pPage = (MemPage *)sqlite3PagerGetExtra(pData); |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 1111 | if( pPage->isInit ){ |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 1112 | assert( sqlite3_mutex_held(pPage->pBt->mutex) ); |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 1113 | pPage->isInit = 0; |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 1114 | sqlite3BtreeInitPage(pPage, pPage->pParent); |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 1115 | } |
| 1116 | } |
| 1117 | |
| 1118 | /* |
drh | e5fe690 | 2007-12-07 18:55:28 +0000 | [diff] [blame] | 1119 | ** Invoke the busy handler for a btree. |
| 1120 | */ |
| 1121 | static int sqlite3BtreeInvokeBusyHandler(void *pArg, int n){ |
| 1122 | BtShared *pBt = (BtShared*)pArg; |
| 1123 | assert( pBt->db ); |
| 1124 | assert( sqlite3_mutex_held(pBt->db->mutex) ); |
| 1125 | return sqlite3InvokeBusyHandler(&pBt->db->busyHandler); |
| 1126 | } |
| 1127 | |
| 1128 | /* |
drh | ad3e010 | 2004-09-03 23:32:18 +0000 | [diff] [blame] | 1129 | ** Open a database file. |
| 1130 | ** |
drh | 382c024 | 2001-10-06 16:33:02 +0000 | [diff] [blame] | 1131 | ** zFilename is the name of the database file. If zFilename is NULL |
drh | 1bee3d7 | 2001-10-15 00:44:35 +0000 | [diff] [blame] | 1132 | ** a new database with a random name is created. This randomly named |
drh | 23e11ca | 2004-05-04 17:27:28 +0000 | [diff] [blame] | 1133 | ** database file will be deleted when sqlite3BtreeClose() is called. |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 1134 | ** If zFilename is ":memory:" then an in-memory database is created |
| 1135 | ** that is automatically destroyed when it is closed. |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 1136 | */ |
drh | 23e11ca | 2004-05-04 17:27:28 +0000 | [diff] [blame] | 1137 | int sqlite3BtreeOpen( |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1138 | const char *zFilename, /* Name of the file containing the BTree database */ |
drh | e5fe690 | 2007-12-07 18:55:28 +0000 | [diff] [blame] | 1139 | sqlite3 *db, /* Associated database handle */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1140 | Btree **ppBtree, /* Pointer to new Btree object written here */ |
drh | 33f4e02 | 2007-09-03 15:19:34 +0000 | [diff] [blame] | 1141 | int flags, /* Options */ |
| 1142 | int vfsFlags /* Flags passed through to sqlite3_vfs.xOpen() */ |
drh | 6019e16 | 2001-07-02 17:51:45 +0000 | [diff] [blame] | 1143 | ){ |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 1144 | sqlite3_vfs *pVfs; /* The VFS to use for this btree */ |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 1145 | BtShared *pBt = 0; /* Shared part of btree structure */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1146 | Btree *p; /* Handle to return */ |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 1147 | int rc = SQLITE_OK; |
drh | 90f5ecb | 2004-07-22 01:19:35 +0000 | [diff] [blame] | 1148 | int nReserve; |
| 1149 | unsigned char zDbHeader[100]; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1150 | |
| 1151 | /* Set the variable isMemdb to true for an in-memory database, or |
| 1152 | ** false for a file-based database. This symbol is only required if |
| 1153 | ** either of the shared-data or autovacuum features are compiled |
| 1154 | ** into the library. |
| 1155 | */ |
| 1156 | #if !defined(SQLITE_OMIT_SHARED_CACHE) || !defined(SQLITE_OMIT_AUTOVACUUM) |
| 1157 | #ifdef SQLITE_OMIT_MEMORYDB |
drh | 980b1a7 | 2006-08-16 16:42:48 +0000 | [diff] [blame] | 1158 | const int isMemdb = 0; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1159 | #else |
drh | 980b1a7 | 2006-08-16 16:42:48 +0000 | [diff] [blame] | 1160 | const int isMemdb = zFilename && !strcmp(zFilename, ":memory:"); |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1161 | #endif |
| 1162 | #endif |
| 1163 | |
drh | e5fe690 | 2007-12-07 18:55:28 +0000 | [diff] [blame] | 1164 | assert( db!=0 ); |
| 1165 | assert( sqlite3_mutex_held(db->mutex) ); |
drh | 153c62c | 2007-08-24 03:51:33 +0000 | [diff] [blame] | 1166 | |
drh | e5fe690 | 2007-12-07 18:55:28 +0000 | [diff] [blame] | 1167 | pVfs = db->pVfs; |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 1168 | p = sqlite3MallocZero(sizeof(Btree)); |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1169 | if( !p ){ |
| 1170 | return SQLITE_NOMEM; |
| 1171 | } |
| 1172 | p->inTrans = TRANS_NONE; |
drh | e5fe690 | 2007-12-07 18:55:28 +0000 | [diff] [blame] | 1173 | p->db = db; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1174 | |
drh | 198bf39 | 2006-01-06 21:52:49 +0000 | [diff] [blame] | 1175 | #if !defined(SQLITE_OMIT_SHARED_CACHE) && !defined(SQLITE_OMIT_DISKIO) |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 1176 | /* |
| 1177 | ** If this Btree is a candidate for shared cache, try to find an |
| 1178 | ** existing BtShared object that we can share with |
| 1179 | */ |
| 1180 | if( (flags & BTREE_PRIVATE)==0 |
| 1181 | && isMemdb==0 |
drh | e5fe690 | 2007-12-07 18:55:28 +0000 | [diff] [blame] | 1182 | && (db->flags & SQLITE_Vtab)==0 |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 1183 | && zFilename && zFilename[0] |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 1184 | ){ |
drh | ff0587c | 2007-08-29 17:43:19 +0000 | [diff] [blame] | 1185 | if( sqlite3SharedCacheEnabled ){ |
danielk1977 | adfb9b0 | 2007-09-17 07:02:56 +0000 | [diff] [blame] | 1186 | int nFullPathname = pVfs->mxPathname+1; |
| 1187 | char *zFullPathname = (char *)sqlite3_malloc(nFullPathname); |
drh | ff0587c | 2007-08-29 17:43:19 +0000 | [diff] [blame] | 1188 | sqlite3_mutex *mutexShared; |
| 1189 | p->sharable = 1; |
drh | e5fe690 | 2007-12-07 18:55:28 +0000 | [diff] [blame] | 1190 | if( db ){ |
| 1191 | db->flags |= SQLITE_SharedCache; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1192 | } |
drh | ff0587c | 2007-08-29 17:43:19 +0000 | [diff] [blame] | 1193 | if( !zFullPathname ){ |
| 1194 | sqlite3_free(p); |
| 1195 | return SQLITE_NOMEM; |
| 1196 | } |
danielk1977 | adfb9b0 | 2007-09-17 07:02:56 +0000 | [diff] [blame] | 1197 | sqlite3OsFullPathname(pVfs, zFilename, nFullPathname, zFullPathname); |
drh | ff0587c | 2007-08-29 17:43:19 +0000 | [diff] [blame] | 1198 | mutexShared = sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_MASTER); |
| 1199 | sqlite3_mutex_enter(mutexShared); |
| 1200 | for(pBt=sqlite3SharedCacheList; pBt; pBt=pBt->pNext){ |
| 1201 | assert( pBt->nRef>0 ); |
| 1202 | if( 0==strcmp(zFullPathname, sqlite3PagerFilename(pBt->pPager)) |
| 1203 | && sqlite3PagerVfs(pBt->pPager)==pVfs ){ |
| 1204 | p->pBt = pBt; |
| 1205 | pBt->nRef++; |
| 1206 | break; |
| 1207 | } |
| 1208 | } |
| 1209 | sqlite3_mutex_leave(mutexShared); |
| 1210 | sqlite3_free(zFullPathname); |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1211 | } |
drh | ff0587c | 2007-08-29 17:43:19 +0000 | [diff] [blame] | 1212 | #ifdef SQLITE_DEBUG |
| 1213 | else{ |
| 1214 | /* In debug mode, we mark all persistent databases as sharable |
| 1215 | ** even when they are not. This exercises the locking code and |
| 1216 | ** gives more opportunity for asserts(sqlite3_mutex_held()) |
| 1217 | ** statements to find locking problems. |
| 1218 | */ |
| 1219 | p->sharable = 1; |
| 1220 | } |
| 1221 | #endif |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1222 | } |
| 1223 | #endif |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 1224 | if( pBt==0 ){ |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 1225 | /* |
| 1226 | ** The following asserts make sure that structures used by the btree are |
| 1227 | ** the right size. This is to guard against size changes that result |
| 1228 | ** when compiling on a different architecture. |
danielk1977 | 03aded4 | 2004-11-22 05:26:27 +0000 | [diff] [blame] | 1229 | */ |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 1230 | assert( sizeof(i64)==8 || sizeof(i64)==4 ); |
| 1231 | assert( sizeof(u64)==8 || sizeof(u64)==4 ); |
| 1232 | assert( sizeof(u32)==4 ); |
| 1233 | assert( sizeof(u16)==2 ); |
| 1234 | assert( sizeof(Pgno)==4 ); |
| 1235 | |
| 1236 | pBt = sqlite3MallocZero( sizeof(*pBt) ); |
| 1237 | if( pBt==0 ){ |
| 1238 | rc = SQLITE_NOMEM; |
| 1239 | goto btree_open_out; |
| 1240 | } |
drh | e5fe690 | 2007-12-07 18:55:28 +0000 | [diff] [blame] | 1241 | pBt->busyHdr.xFunc = sqlite3BtreeInvokeBusyHandler; |
| 1242 | pBt->busyHdr.pArg = pBt; |
drh | 33f4e02 | 2007-09-03 15:19:34 +0000 | [diff] [blame] | 1243 | rc = sqlite3PagerOpen(pVfs, &pBt->pPager, zFilename, |
| 1244 | EXTRA_SIZE, flags, vfsFlags); |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 1245 | if( rc==SQLITE_OK ){ |
| 1246 | rc = sqlite3PagerReadFileheader(pBt->pPager,sizeof(zDbHeader),zDbHeader); |
| 1247 | } |
| 1248 | if( rc!=SQLITE_OK ){ |
| 1249 | goto btree_open_out; |
| 1250 | } |
drh | e5fe690 | 2007-12-07 18:55:28 +0000 | [diff] [blame] | 1251 | sqlite3PagerSetBusyhandler(pBt->pPager, &pBt->busyHdr); |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 1252 | p->pBt = pBt; |
| 1253 | |
| 1254 | sqlite3PagerSetDestructor(pBt->pPager, pageDestructor); |
| 1255 | sqlite3PagerSetReiniter(pBt->pPager, pageReinit); |
| 1256 | pBt->pCursor = 0; |
| 1257 | pBt->pPage1 = 0; |
| 1258 | pBt->readOnly = sqlite3PagerIsreadonly(pBt->pPager); |
| 1259 | pBt->pageSize = get2byte(&zDbHeader[16]); |
| 1260 | if( pBt->pageSize<512 || pBt->pageSize>SQLITE_MAX_PAGE_SIZE |
| 1261 | || ((pBt->pageSize-1)&pBt->pageSize)!=0 ){ |
danielk1977 | a1644fd | 2007-08-29 12:31:25 +0000 | [diff] [blame] | 1262 | pBt->pageSize = 0; |
| 1263 | sqlite3PagerSetPagesize(pBt->pPager, &pBt->pageSize); |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 1264 | pBt->maxEmbedFrac = 64; /* 25% */ |
| 1265 | pBt->minEmbedFrac = 32; /* 12.5% */ |
| 1266 | pBt->minLeafFrac = 32; /* 12.5% */ |
| 1267 | #ifndef SQLITE_OMIT_AUTOVACUUM |
| 1268 | /* If the magic name ":memory:" will create an in-memory database, then |
| 1269 | ** leave the autoVacuum mode at 0 (do not auto-vacuum), even if |
| 1270 | ** SQLITE_DEFAULT_AUTOVACUUM is true. On the other hand, if |
| 1271 | ** SQLITE_OMIT_MEMORYDB has been defined, then ":memory:" is just a |
| 1272 | ** regular file-name. In this case the auto-vacuum applies as per normal. |
| 1273 | */ |
| 1274 | if( zFilename && !isMemdb ){ |
| 1275 | pBt->autoVacuum = (SQLITE_DEFAULT_AUTOVACUUM ? 1 : 0); |
| 1276 | pBt->incrVacuum = (SQLITE_DEFAULT_AUTOVACUUM==2 ? 1 : 0); |
| 1277 | } |
| 1278 | #endif |
| 1279 | nReserve = 0; |
| 1280 | }else{ |
| 1281 | nReserve = zDbHeader[20]; |
| 1282 | pBt->maxEmbedFrac = zDbHeader[21]; |
| 1283 | pBt->minEmbedFrac = zDbHeader[22]; |
| 1284 | pBt->minLeafFrac = zDbHeader[23]; |
| 1285 | pBt->pageSizeFixed = 1; |
| 1286 | #ifndef SQLITE_OMIT_AUTOVACUUM |
| 1287 | pBt->autoVacuum = (get4byte(&zDbHeader[36 + 4*4])?1:0); |
| 1288 | pBt->incrVacuum = (get4byte(&zDbHeader[36 + 7*4])?1:0); |
| 1289 | #endif |
| 1290 | } |
| 1291 | pBt->usableSize = pBt->pageSize - nReserve; |
| 1292 | assert( (pBt->pageSize & 7)==0 ); /* 8-byte alignment of pageSize */ |
danielk1977 | a1644fd | 2007-08-29 12:31:25 +0000 | [diff] [blame] | 1293 | sqlite3PagerSetPagesize(pBt->pPager, &pBt->pageSize); |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 1294 | |
| 1295 | #if !defined(SQLITE_OMIT_SHARED_CACHE) && !defined(SQLITE_OMIT_DISKIO) |
| 1296 | /* Add the new BtShared object to the linked list sharable BtShareds. |
| 1297 | */ |
| 1298 | if( p->sharable ){ |
| 1299 | sqlite3_mutex *mutexShared; |
| 1300 | pBt->nRef = 1; |
| 1301 | mutexShared = sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_MASTER); |
drh | 3285db2 | 2007-09-03 22:00:39 +0000 | [diff] [blame] | 1302 | if( SQLITE_THREADSAFE ){ |
| 1303 | pBt->mutex = sqlite3_mutex_alloc(SQLITE_MUTEX_FAST); |
| 1304 | if( pBt->mutex==0 ){ |
| 1305 | rc = SQLITE_NOMEM; |
drh | e5fe690 | 2007-12-07 18:55:28 +0000 | [diff] [blame] | 1306 | db->mallocFailed = 0; |
drh | 3285db2 | 2007-09-03 22:00:39 +0000 | [diff] [blame] | 1307 | goto btree_open_out; |
| 1308 | } |
drh | ff0587c | 2007-08-29 17:43:19 +0000 | [diff] [blame] | 1309 | } |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 1310 | sqlite3_mutex_enter(mutexShared); |
| 1311 | pBt->pNext = sqlite3SharedCacheList; |
| 1312 | sqlite3SharedCacheList = pBt; |
| 1313 | sqlite3_mutex_leave(mutexShared); |
danielk1977 | 951af80 | 2004-11-05 15:45:09 +0000 | [diff] [blame] | 1314 | } |
drh | eee46cf | 2004-11-06 00:02:48 +0000 | [diff] [blame] | 1315 | #endif |
drh | 90f5ecb | 2004-07-22 01:19:35 +0000 | [diff] [blame] | 1316 | } |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1317 | |
drh | cfed7bc | 2006-03-13 14:28:05 +0000 | [diff] [blame] | 1318 | #if !defined(SQLITE_OMIT_SHARED_CACHE) && !defined(SQLITE_OMIT_DISKIO) |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 1319 | /* If the new Btree uses a sharable pBtShared, then link the new |
| 1320 | ** Btree into the list of all sharable Btrees for the same connection. |
drh | abddb0c | 2007-08-20 13:14:28 +0000 | [diff] [blame] | 1321 | ** The list is kept in ascending order by pBt address. |
danielk1977 | 54f0198 | 2006-01-18 15:25:17 +0000 | [diff] [blame] | 1322 | */ |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 1323 | if( p->sharable ){ |
| 1324 | int i; |
| 1325 | Btree *pSib; |
drh | e5fe690 | 2007-12-07 18:55:28 +0000 | [diff] [blame] | 1326 | for(i=0; i<db->nDb; i++){ |
| 1327 | if( (pSib = db->aDb[i].pBt)!=0 && pSib->sharable ){ |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 1328 | while( pSib->pPrev ){ pSib = pSib->pPrev; } |
| 1329 | if( p->pBt<pSib->pBt ){ |
| 1330 | p->pNext = pSib; |
| 1331 | p->pPrev = 0; |
| 1332 | pSib->pPrev = p; |
| 1333 | }else{ |
drh | abddb0c | 2007-08-20 13:14:28 +0000 | [diff] [blame] | 1334 | while( pSib->pNext && pSib->pNext->pBt<p->pBt ){ |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 1335 | pSib = pSib->pNext; |
| 1336 | } |
| 1337 | p->pNext = pSib->pNext; |
| 1338 | p->pPrev = pSib; |
| 1339 | if( p->pNext ){ |
| 1340 | p->pNext->pPrev = p; |
| 1341 | } |
| 1342 | pSib->pNext = p; |
| 1343 | } |
| 1344 | break; |
| 1345 | } |
| 1346 | } |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1347 | } |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1348 | #endif |
| 1349 | *ppBtree = p; |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 1350 | |
| 1351 | btree_open_out: |
| 1352 | if( rc!=SQLITE_OK ){ |
| 1353 | if( pBt && pBt->pPager ){ |
| 1354 | sqlite3PagerClose(pBt->pPager); |
| 1355 | } |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 1356 | sqlite3_free(pBt); |
| 1357 | sqlite3_free(p); |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 1358 | *ppBtree = 0; |
| 1359 | } |
| 1360 | return rc; |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 1361 | } |
| 1362 | |
| 1363 | /* |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 1364 | ** Decrement the BtShared.nRef counter. When it reaches zero, |
| 1365 | ** remove the BtShared structure from the sharing list. Return |
| 1366 | ** true if the BtShared.nRef counter reaches zero and return |
| 1367 | ** false if it is still positive. |
| 1368 | */ |
| 1369 | static int removeFromSharingList(BtShared *pBt){ |
| 1370 | #ifndef SQLITE_OMIT_SHARED_CACHE |
| 1371 | sqlite3_mutex *pMaster; |
| 1372 | BtShared *pList; |
| 1373 | int removed = 0; |
| 1374 | |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 1375 | assert( sqlite3_mutex_notheld(pBt->mutex) ); |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 1376 | pMaster = sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_MASTER); |
| 1377 | sqlite3_mutex_enter(pMaster); |
| 1378 | pBt->nRef--; |
| 1379 | if( pBt->nRef<=0 ){ |
| 1380 | if( sqlite3SharedCacheList==pBt ){ |
| 1381 | sqlite3SharedCacheList = pBt->pNext; |
| 1382 | }else{ |
| 1383 | pList = sqlite3SharedCacheList; |
| 1384 | while( pList && pList->pNext!=pBt ){ |
| 1385 | pList=pList->pNext; |
| 1386 | } |
| 1387 | if( pList ){ |
| 1388 | pList->pNext = pBt->pNext; |
| 1389 | } |
| 1390 | } |
drh | 3285db2 | 2007-09-03 22:00:39 +0000 | [diff] [blame] | 1391 | if( SQLITE_THREADSAFE ){ |
| 1392 | sqlite3_mutex_free(pBt->mutex); |
| 1393 | } |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 1394 | removed = 1; |
| 1395 | } |
| 1396 | sqlite3_mutex_leave(pMaster); |
| 1397 | return removed; |
| 1398 | #else |
| 1399 | return 1; |
| 1400 | #endif |
| 1401 | } |
| 1402 | |
| 1403 | /* |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 1404 | ** Close an open database and invalidate all cursors. |
| 1405 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1406 | int sqlite3BtreeClose(Btree *p){ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1407 | BtShared *pBt = p->pBt; |
| 1408 | BtCursor *pCur; |
| 1409 | |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1410 | /* Close all cursors opened via this handle. */ |
drh | e5fe690 | 2007-12-07 18:55:28 +0000 | [diff] [blame] | 1411 | assert( sqlite3_mutex_held(p->db->mutex) ); |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 1412 | sqlite3BtreeEnter(p); |
drh | e5fe690 | 2007-12-07 18:55:28 +0000 | [diff] [blame] | 1413 | pBt->db = p->db; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1414 | pCur = pBt->pCursor; |
| 1415 | while( pCur ){ |
| 1416 | BtCursor *pTmp = pCur; |
| 1417 | pCur = pCur->pNext; |
| 1418 | if( pTmp->pBtree==p ){ |
| 1419 | sqlite3BtreeCloseCursor(pTmp); |
| 1420 | } |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 1421 | } |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1422 | |
danielk1977 | 8d34dfd | 2006-01-24 16:37:57 +0000 | [diff] [blame] | 1423 | /* Rollback any active transaction and free the handle structure. |
| 1424 | ** The call to sqlite3BtreeRollback() drops any table-locks held by |
| 1425 | ** this handle. |
| 1426 | */ |
danielk1977 | b597f74 | 2006-01-15 11:39:18 +0000 | [diff] [blame] | 1427 | sqlite3BtreeRollback(p); |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 1428 | sqlite3BtreeLeave(p); |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1429 | |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1430 | /* If there are still other outstanding references to the shared-btree |
| 1431 | ** structure, return now. The remainder of this procedure cleans |
| 1432 | ** up the shared-btree. |
| 1433 | */ |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 1434 | assert( p->wantToLock==0 && p->locked==0 ); |
| 1435 | if( !p->sharable || removeFromSharingList(pBt) ){ |
| 1436 | /* The pBt is no longer on the sharing list, so we can access |
| 1437 | ** it without having to hold the mutex. |
| 1438 | ** |
| 1439 | ** Clean out and delete the BtShared object. |
| 1440 | */ |
| 1441 | assert( !pBt->pCursor ); |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 1442 | sqlite3PagerClose(pBt->pPager); |
| 1443 | if( pBt->xFreeSchema && pBt->pSchema ){ |
| 1444 | pBt->xFreeSchema(pBt->pSchema); |
| 1445 | } |
| 1446 | sqlite3_free(pBt->pSchema); |
danielk1977 | 52ae724 | 2008-03-25 14:24:56 +0000 | [diff] [blame] | 1447 | sqlite3_free(pBt->pTmpSpace); |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 1448 | sqlite3_free(pBt); |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1449 | } |
| 1450 | |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 1451 | #ifndef SQLITE_OMIT_SHARED_CACHE |
drh | cab5ed7 | 2007-08-22 11:41:18 +0000 | [diff] [blame] | 1452 | assert( p->wantToLock==0 ); |
| 1453 | assert( p->locked==0 ); |
| 1454 | if( p->pPrev ) p->pPrev->pNext = p->pNext; |
| 1455 | if( p->pNext ) p->pNext->pPrev = p->pPrev; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1456 | #endif |
| 1457 | |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 1458 | sqlite3_free(p); |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 1459 | return SQLITE_OK; |
| 1460 | } |
| 1461 | |
| 1462 | /* |
drh | da47d77 | 2002-12-02 04:25:19 +0000 | [diff] [blame] | 1463 | ** Change the limit on the number of pages allowed in the cache. |
drh | cd61c28 | 2002-03-06 22:01:34 +0000 | [diff] [blame] | 1464 | ** |
| 1465 | ** The maximum number of cache pages is set to the absolute |
| 1466 | ** value of mxPage. If mxPage is negative, the pager will |
| 1467 | ** operate asynchronously - it will not stop to do fsync()s |
| 1468 | ** to insure data is written to the disk surface before |
| 1469 | ** continuing. Transactions still work if synchronous is off, |
| 1470 | ** and the database cannot be corrupted if this program |
| 1471 | ** crashes. But if the operating system crashes or there is |
| 1472 | ** an abrupt power failure when synchronous is off, the database |
| 1473 | ** could be left in an inconsistent and unrecoverable state. |
| 1474 | ** Synchronous is on by default so database corruption is not |
| 1475 | ** normally a worry. |
drh | f57b14a | 2001-09-14 18:54:08 +0000 | [diff] [blame] | 1476 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1477 | int sqlite3BtreeSetCacheSize(Btree *p, int mxPage){ |
| 1478 | BtShared *pBt = p->pBt; |
drh | e5fe690 | 2007-12-07 18:55:28 +0000 | [diff] [blame] | 1479 | assert( sqlite3_mutex_held(p->db->mutex) ); |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 1480 | sqlite3BtreeEnter(p); |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 1481 | sqlite3PagerSetCachesize(pBt->pPager, mxPage); |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 1482 | sqlite3BtreeLeave(p); |
drh | f57b14a | 2001-09-14 18:54:08 +0000 | [diff] [blame] | 1483 | return SQLITE_OK; |
| 1484 | } |
| 1485 | |
| 1486 | /* |
drh | 973b6e3 | 2003-02-12 14:09:42 +0000 | [diff] [blame] | 1487 | ** Change the way data is synced to disk in order to increase or decrease |
| 1488 | ** how well the database resists damage due to OS crashes and power |
| 1489 | ** failures. Level 1 is the same as asynchronous (no syncs() occur and |
| 1490 | ** there is a high probability of damage) Level 2 is the default. There |
| 1491 | ** is a very low but non-zero probability of damage. Level 3 reduces the |
| 1492 | ** probability of damage to near zero but with a write performance reduction. |
| 1493 | */ |
danielk1977 | 93758c8 | 2005-01-21 08:13:14 +0000 | [diff] [blame] | 1494 | #ifndef SQLITE_OMIT_PAGER_PRAGMAS |
drh | ac530b1 | 2006-02-11 01:25:50 +0000 | [diff] [blame] | 1495 | int sqlite3BtreeSetSafetyLevel(Btree *p, int level, int fullSync){ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1496 | BtShared *pBt = p->pBt; |
drh | e5fe690 | 2007-12-07 18:55:28 +0000 | [diff] [blame] | 1497 | assert( sqlite3_mutex_held(p->db->mutex) ); |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 1498 | sqlite3BtreeEnter(p); |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 1499 | sqlite3PagerSetSafetyLevel(pBt->pPager, level, fullSync); |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 1500 | sqlite3BtreeLeave(p); |
drh | 973b6e3 | 2003-02-12 14:09:42 +0000 | [diff] [blame] | 1501 | return SQLITE_OK; |
| 1502 | } |
danielk1977 | 93758c8 | 2005-01-21 08:13:14 +0000 | [diff] [blame] | 1503 | #endif |
drh | 973b6e3 | 2003-02-12 14:09:42 +0000 | [diff] [blame] | 1504 | |
drh | 2c8997b | 2005-08-27 16:36:48 +0000 | [diff] [blame] | 1505 | /* |
| 1506 | ** Return TRUE if the given btree is set to safety level 1. In other |
| 1507 | ** words, return TRUE if no sync() occurs on the disk files. |
| 1508 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1509 | int sqlite3BtreeSyncDisabled(Btree *p){ |
| 1510 | BtShared *pBt = p->pBt; |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 1511 | int rc; |
drh | e5fe690 | 2007-12-07 18:55:28 +0000 | [diff] [blame] | 1512 | assert( sqlite3_mutex_held(p->db->mutex) ); |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 1513 | sqlite3BtreeEnter(p); |
drh | d0679ed | 2007-08-28 22:24:34 +0000 | [diff] [blame] | 1514 | assert( pBt && pBt->pPager ); |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 1515 | rc = sqlite3PagerNosync(pBt->pPager); |
| 1516 | sqlite3BtreeLeave(p); |
| 1517 | return rc; |
drh | 2c8997b | 2005-08-27 16:36:48 +0000 | [diff] [blame] | 1518 | } |
| 1519 | |
danielk1977 | 576ec6b | 2005-01-21 11:55:25 +0000 | [diff] [blame] | 1520 | #if !defined(SQLITE_OMIT_PAGER_PRAGMAS) || !defined(SQLITE_OMIT_VACUUM) |
drh | 973b6e3 | 2003-02-12 14:09:42 +0000 | [diff] [blame] | 1521 | /* |
drh | 90f5ecb | 2004-07-22 01:19:35 +0000 | [diff] [blame] | 1522 | ** Change the default pages size and the number of reserved bytes per page. |
drh | 06f5021 | 2004-11-02 14:24:33 +0000 | [diff] [blame] | 1523 | ** |
| 1524 | ** The page size must be a power of 2 between 512 and 65536. If the page |
| 1525 | ** size supplied does not meet this constraint then the page size is not |
| 1526 | ** changed. |
| 1527 | ** |
| 1528 | ** Page sizes are constrained to be a power of two so that the region |
| 1529 | ** of the database file used for locking (beginning at PENDING_BYTE, |
| 1530 | ** the first byte past the 1GB boundary, 0x40000000) needs to occur |
| 1531 | ** at the beginning of a page. |
danielk1977 | 2812956 | 2005-01-11 10:25:06 +0000 | [diff] [blame] | 1532 | ** |
| 1533 | ** If parameter nReserve is less than zero, then the number of reserved |
| 1534 | ** bytes per page is left unchanged. |
drh | 90f5ecb | 2004-07-22 01:19:35 +0000 | [diff] [blame] | 1535 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1536 | int sqlite3BtreeSetPageSize(Btree *p, int pageSize, int nReserve){ |
danielk1977 | a1644fd | 2007-08-29 12:31:25 +0000 | [diff] [blame] | 1537 | int rc = SQLITE_OK; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1538 | BtShared *pBt = p->pBt; |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 1539 | sqlite3BtreeEnter(p); |
drh | 90f5ecb | 2004-07-22 01:19:35 +0000 | [diff] [blame] | 1540 | if( pBt->pageSizeFixed ){ |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 1541 | sqlite3BtreeLeave(p); |
drh | 90f5ecb | 2004-07-22 01:19:35 +0000 | [diff] [blame] | 1542 | return SQLITE_READONLY; |
| 1543 | } |
| 1544 | if( nReserve<0 ){ |
| 1545 | nReserve = pBt->pageSize - pBt->usableSize; |
| 1546 | } |
drh | 06f5021 | 2004-11-02 14:24:33 +0000 | [diff] [blame] | 1547 | if( pageSize>=512 && pageSize<=SQLITE_MAX_PAGE_SIZE && |
| 1548 | ((pageSize-1)&pageSize)==0 ){ |
drh | 07d183d | 2005-05-01 22:52:42 +0000 | [diff] [blame] | 1549 | assert( (pageSize & 7)==0 ); |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1550 | assert( !pBt->pPage1 && !pBt->pCursor ); |
danielk1977 | a1644fd | 2007-08-29 12:31:25 +0000 | [diff] [blame] | 1551 | pBt->pageSize = pageSize; |
danielk1977 | 52ae724 | 2008-03-25 14:24:56 +0000 | [diff] [blame] | 1552 | sqlite3_free(pBt->pTmpSpace); |
| 1553 | pBt->pTmpSpace = 0; |
danielk1977 | a1644fd | 2007-08-29 12:31:25 +0000 | [diff] [blame] | 1554 | rc = sqlite3PagerSetPagesize(pBt->pPager, &pBt->pageSize); |
drh | 90f5ecb | 2004-07-22 01:19:35 +0000 | [diff] [blame] | 1555 | } |
| 1556 | pBt->usableSize = pBt->pageSize - nReserve; |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 1557 | sqlite3BtreeLeave(p); |
danielk1977 | a1644fd | 2007-08-29 12:31:25 +0000 | [diff] [blame] | 1558 | return rc; |
drh | 90f5ecb | 2004-07-22 01:19:35 +0000 | [diff] [blame] | 1559 | } |
| 1560 | |
| 1561 | /* |
| 1562 | ** Return the currently defined page size |
| 1563 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1564 | int sqlite3BtreeGetPageSize(Btree *p){ |
| 1565 | return p->pBt->pageSize; |
drh | 90f5ecb | 2004-07-22 01:19:35 +0000 | [diff] [blame] | 1566 | } |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1567 | int sqlite3BtreeGetReserve(Btree *p){ |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 1568 | int n; |
| 1569 | sqlite3BtreeEnter(p); |
| 1570 | n = p->pBt->pageSize - p->pBt->usableSize; |
| 1571 | sqlite3BtreeLeave(p); |
| 1572 | return n; |
drh | 2011d5f | 2004-07-22 02:40:37 +0000 | [diff] [blame] | 1573 | } |
drh | f8e632b | 2007-05-08 14:51:36 +0000 | [diff] [blame] | 1574 | |
| 1575 | /* |
| 1576 | ** Set the maximum page count for a database if mxPage is positive. |
| 1577 | ** No changes are made if mxPage is 0 or negative. |
| 1578 | ** Regardless of the value of mxPage, return the maximum page count. |
| 1579 | */ |
| 1580 | int sqlite3BtreeMaxPageCount(Btree *p, int mxPage){ |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 1581 | int n; |
| 1582 | sqlite3BtreeEnter(p); |
| 1583 | n = sqlite3PagerMaxPageCount(p->pBt->pPager, mxPage); |
| 1584 | sqlite3BtreeLeave(p); |
| 1585 | return n; |
drh | f8e632b | 2007-05-08 14:51:36 +0000 | [diff] [blame] | 1586 | } |
danielk1977 | 576ec6b | 2005-01-21 11:55:25 +0000 | [diff] [blame] | 1587 | #endif /* !defined(SQLITE_OMIT_PAGER_PRAGMAS) || !defined(SQLITE_OMIT_VACUUM) */ |
drh | 90f5ecb | 2004-07-22 01:19:35 +0000 | [diff] [blame] | 1588 | |
| 1589 | /* |
danielk1977 | 951af80 | 2004-11-05 15:45:09 +0000 | [diff] [blame] | 1590 | ** Change the 'auto-vacuum' property of the database. If the 'autoVacuum' |
| 1591 | ** parameter is non-zero, then auto-vacuum mode is enabled. If zero, it |
| 1592 | ** is disabled. The default value for the auto-vacuum property is |
| 1593 | ** determined by the SQLITE_DEFAULT_AUTOVACUUM macro. |
| 1594 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1595 | int sqlite3BtreeSetAutoVacuum(Btree *p, int autoVacuum){ |
danielk1977 | 951af80 | 2004-11-05 15:45:09 +0000 | [diff] [blame] | 1596 | #ifdef SQLITE_OMIT_AUTOVACUUM |
drh | eee46cf | 2004-11-06 00:02:48 +0000 | [diff] [blame] | 1597 | return SQLITE_READONLY; |
danielk1977 | 951af80 | 2004-11-05 15:45:09 +0000 | [diff] [blame] | 1598 | #else |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 1599 | BtShared *pBt = p->pBt; |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 1600 | int rc = SQLITE_OK; |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 1601 | int av = (autoVacuum?1:0); |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 1602 | |
| 1603 | sqlite3BtreeEnter(p); |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 1604 | if( pBt->pageSizeFixed && av!=pBt->autoVacuum ){ |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 1605 | rc = SQLITE_READONLY; |
| 1606 | }else{ |
| 1607 | pBt->autoVacuum = av; |
danielk1977 | 951af80 | 2004-11-05 15:45:09 +0000 | [diff] [blame] | 1608 | } |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 1609 | sqlite3BtreeLeave(p); |
| 1610 | return rc; |
danielk1977 | 951af80 | 2004-11-05 15:45:09 +0000 | [diff] [blame] | 1611 | #endif |
| 1612 | } |
| 1613 | |
| 1614 | /* |
| 1615 | ** Return the value of the 'auto-vacuum' property. If auto-vacuum is |
| 1616 | ** enabled 1 is returned. Otherwise 0. |
| 1617 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1618 | int sqlite3BtreeGetAutoVacuum(Btree *p){ |
danielk1977 | 951af80 | 2004-11-05 15:45:09 +0000 | [diff] [blame] | 1619 | #ifdef SQLITE_OMIT_AUTOVACUUM |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 1620 | return BTREE_AUTOVACUUM_NONE; |
danielk1977 | 951af80 | 2004-11-05 15:45:09 +0000 | [diff] [blame] | 1621 | #else |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 1622 | int rc; |
| 1623 | sqlite3BtreeEnter(p); |
| 1624 | rc = ( |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 1625 | (!p->pBt->autoVacuum)?BTREE_AUTOVACUUM_NONE: |
| 1626 | (!p->pBt->incrVacuum)?BTREE_AUTOVACUUM_FULL: |
| 1627 | BTREE_AUTOVACUUM_INCR |
| 1628 | ); |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 1629 | sqlite3BtreeLeave(p); |
| 1630 | return rc; |
danielk1977 | 951af80 | 2004-11-05 15:45:09 +0000 | [diff] [blame] | 1631 | #endif |
| 1632 | } |
| 1633 | |
| 1634 | |
| 1635 | /* |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 1636 | ** Get a reference to pPage1 of the database file. This will |
drh | 306dc21 | 2001-05-21 13:45:10 +0000 | [diff] [blame] | 1637 | ** also acquire a readlock on that file. |
| 1638 | ** |
| 1639 | ** SQLITE_OK is returned on success. If the file is not a |
| 1640 | ** well-formed database file, then SQLITE_CORRUPT is returned. |
| 1641 | ** SQLITE_BUSY is returned if the database is locked. SQLITE_NOMEM |
drh | 4f0ee68 | 2007-03-30 20:43:40 +0000 | [diff] [blame] | 1642 | ** is returned if we run out of memory. |
drh | 306dc21 | 2001-05-21 13:45:10 +0000 | [diff] [blame] | 1643 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1644 | static int lockBtree(BtShared *pBt){ |
danielk1977 | f653d78 | 2008-03-20 11:04:21 +0000 | [diff] [blame] | 1645 | int rc; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1646 | MemPage *pPage1; |
danielk1977 | 93f7af9 | 2008-05-09 16:57:50 +0000 | [diff] [blame] | 1647 | int nPage; |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 1648 | |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 1649 | assert( sqlite3_mutex_held(pBt->mutex) ); |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 1650 | if( pBt->pPage1 ) return SQLITE_OK; |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 1651 | rc = sqlite3BtreeGetPage(pBt, 1, &pPage1, 0); |
drh | 306dc21 | 2001-05-21 13:45:10 +0000 | [diff] [blame] | 1652 | if( rc!=SQLITE_OK ) return rc; |
drh | 306dc21 | 2001-05-21 13:45:10 +0000 | [diff] [blame] | 1653 | |
| 1654 | /* Do some checking to help insure the file we opened really is |
| 1655 | ** a valid database file. |
| 1656 | */ |
danielk1977 | ad0132d | 2008-06-07 08:58:22 +0000 | [diff] [blame^] | 1657 | rc = sqlite3PagerPagecount(pBt->pPager, &nPage); |
| 1658 | if( rc!=SQLITE_OK ){ |
danielk1977 | 93f7af9 | 2008-05-09 16:57:50 +0000 | [diff] [blame] | 1659 | goto page1_init_failed; |
| 1660 | }else if( nPage>0 ){ |
danielk1977 | f653d78 | 2008-03-20 11:04:21 +0000 | [diff] [blame] | 1661 | int pageSize; |
| 1662 | int usableSize; |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 1663 | u8 *page1 = pPage1->aData; |
danielk1977 | ad0132d | 2008-06-07 08:58:22 +0000 | [diff] [blame^] | 1664 | rc = SQLITE_NOTADB; |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 1665 | if( memcmp(page1, zMagicHeader, 16)!=0 ){ |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 1666 | goto page1_init_failed; |
drh | 306dc21 | 2001-05-21 13:45:10 +0000 | [diff] [blame] | 1667 | } |
drh | 309169a | 2007-04-24 17:27:51 +0000 | [diff] [blame] | 1668 | if( page1[18]>1 ){ |
| 1669 | pBt->readOnly = 1; |
| 1670 | } |
| 1671 | if( page1[19]>1 ){ |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 1672 | goto page1_init_failed; |
| 1673 | } |
drh | 07d183d | 2005-05-01 22:52:42 +0000 | [diff] [blame] | 1674 | pageSize = get2byte(&page1[16]); |
drh | 7dc385e | 2007-09-06 23:39:36 +0000 | [diff] [blame] | 1675 | if( ((pageSize-1)&pageSize)!=0 || pageSize<512 || |
| 1676 | (SQLITE_MAX_PAGE_SIZE<32768 && pageSize>SQLITE_MAX_PAGE_SIZE) |
| 1677 | ){ |
drh | 07d183d | 2005-05-01 22:52:42 +0000 | [diff] [blame] | 1678 | goto page1_init_failed; |
| 1679 | } |
| 1680 | assert( (pageSize & 7)==0 ); |
danielk1977 | f653d78 | 2008-03-20 11:04:21 +0000 | [diff] [blame] | 1681 | usableSize = pageSize - page1[20]; |
| 1682 | if( pageSize!=pBt->pageSize ){ |
| 1683 | /* After reading the first page of the database assuming a page size |
| 1684 | ** of BtShared.pageSize, we have discovered that the page-size is |
| 1685 | ** actually pageSize. Unlock the database, leave pBt->pPage1 at |
| 1686 | ** zero and return SQLITE_OK. The caller will call this function |
| 1687 | ** again with the correct page-size. |
| 1688 | */ |
| 1689 | releasePage(pPage1); |
| 1690 | pBt->usableSize = usableSize; |
| 1691 | pBt->pageSize = pageSize; |
danielk1977 | 52ae724 | 2008-03-25 14:24:56 +0000 | [diff] [blame] | 1692 | sqlite3_free(pBt->pTmpSpace); |
| 1693 | pBt->pTmpSpace = 0; |
danielk1977 | f653d78 | 2008-03-20 11:04:21 +0000 | [diff] [blame] | 1694 | sqlite3PagerSetPagesize(pBt->pPager, &pBt->pageSize); |
| 1695 | return SQLITE_OK; |
| 1696 | } |
| 1697 | if( usableSize<500 ){ |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 1698 | goto page1_init_failed; |
| 1699 | } |
danielk1977 | f653d78 | 2008-03-20 11:04:21 +0000 | [diff] [blame] | 1700 | pBt->pageSize = pageSize; |
| 1701 | pBt->usableSize = usableSize; |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 1702 | pBt->maxEmbedFrac = page1[21]; |
| 1703 | pBt->minEmbedFrac = page1[22]; |
| 1704 | pBt->minLeafFrac = page1[23]; |
drh | 057cd3a | 2005-02-15 16:23:02 +0000 | [diff] [blame] | 1705 | #ifndef SQLITE_OMIT_AUTOVACUUM |
| 1706 | pBt->autoVacuum = (get4byte(&page1[36 + 4*4])?1:0); |
danielk1977 | 27b1f95 | 2007-06-25 08:16:58 +0000 | [diff] [blame] | 1707 | pBt->incrVacuum = (get4byte(&page1[36 + 7*4])?1:0); |
drh | 057cd3a | 2005-02-15 16:23:02 +0000 | [diff] [blame] | 1708 | #endif |
drh | 306dc21 | 2001-05-21 13:45:10 +0000 | [diff] [blame] | 1709 | } |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 1710 | |
| 1711 | /* maxLocal is the maximum amount of payload to store locally for |
| 1712 | ** a cell. Make sure it is small enough so that at least minFanout |
| 1713 | ** cells can will fit on one page. We assume a 10-byte page header. |
| 1714 | ** Besides the payload, the cell must store: |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 1715 | ** 2-byte pointer to the cell |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 1716 | ** 4-byte child pointer |
| 1717 | ** 9-byte nKey value |
| 1718 | ** 4-byte nData value |
| 1719 | ** 4-byte overflow page pointer |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 1720 | ** So a cell consists of a 2-byte poiner, a header which is as much as |
| 1721 | ** 17 bytes long, 0 to N bytes of payload, and an optional 4 byte overflow |
| 1722 | ** page pointer. |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 1723 | */ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 1724 | pBt->maxLocal = (pBt->usableSize-12)*pBt->maxEmbedFrac/255 - 23; |
| 1725 | pBt->minLocal = (pBt->usableSize-12)*pBt->minEmbedFrac/255 - 23; |
| 1726 | pBt->maxLeaf = pBt->usableSize - 35; |
| 1727 | pBt->minLeaf = (pBt->usableSize-12)*pBt->minLeafFrac/255 - 23; |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 1728 | if( pBt->minLocal>pBt->maxLocal || pBt->maxLocal<0 ){ |
| 1729 | goto page1_init_failed; |
| 1730 | } |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 1731 | assert( pBt->maxLeaf + 23 <= MX_CELL_SIZE(pBt) ); |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1732 | pBt->pPage1 = pPage1; |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 1733 | return SQLITE_OK; |
drh | 306dc21 | 2001-05-21 13:45:10 +0000 | [diff] [blame] | 1734 | |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 1735 | page1_init_failed: |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1736 | releasePage(pPage1); |
| 1737 | pBt->pPage1 = 0; |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 1738 | return rc; |
drh | 306dc21 | 2001-05-21 13:45:10 +0000 | [diff] [blame] | 1739 | } |
| 1740 | |
| 1741 | /* |
drh | b8ef32c | 2005-03-14 02:01:49 +0000 | [diff] [blame] | 1742 | ** This routine works like lockBtree() except that it also invokes the |
| 1743 | ** busy callback if there is lock contention. |
| 1744 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1745 | static int lockBtreeWithRetry(Btree *pRef){ |
drh | b8ef32c | 2005-03-14 02:01:49 +0000 | [diff] [blame] | 1746 | int rc = SQLITE_OK; |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 1747 | |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 1748 | assert( sqlite3BtreeHoldsMutex(pRef) ); |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1749 | if( pRef->inTrans==TRANS_NONE ){ |
| 1750 | u8 inTransaction = pRef->pBt->inTransaction; |
| 1751 | btreeIntegrity(pRef); |
| 1752 | rc = sqlite3BtreeBeginTrans(pRef, 0); |
| 1753 | pRef->pBt->inTransaction = inTransaction; |
| 1754 | pRef->inTrans = TRANS_NONE; |
| 1755 | if( rc==SQLITE_OK ){ |
| 1756 | pRef->pBt->nTransaction--; |
| 1757 | } |
| 1758 | btreeIntegrity(pRef); |
drh | b8ef32c | 2005-03-14 02:01:49 +0000 | [diff] [blame] | 1759 | } |
| 1760 | return rc; |
| 1761 | } |
| 1762 | |
| 1763 | |
| 1764 | /* |
drh | b8ca307 | 2001-12-05 00:21:20 +0000 | [diff] [blame] | 1765 | ** If there are no outstanding cursors and we are not in the middle |
| 1766 | ** of a transaction but there is a read lock on the database, then |
| 1767 | ** this routine unrefs the first page of the database file which |
| 1768 | ** has the effect of releasing the read lock. |
| 1769 | ** |
| 1770 | ** If there are any outstanding cursors, this routine is a no-op. |
| 1771 | ** |
| 1772 | ** If there is a transaction in progress, this routine is a no-op. |
| 1773 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1774 | static void unlockBtreeIfUnused(BtShared *pBt){ |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 1775 | assert( sqlite3_mutex_held(pBt->mutex) ); |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1776 | if( pBt->inTransaction==TRANS_NONE && pBt->pCursor==0 && pBt->pPage1!=0 ){ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 1777 | if( sqlite3PagerRefcount(pBt->pPager)>=1 ){ |
drh | de4fcfd | 2008-01-19 23:50:26 +0000 | [diff] [blame] | 1778 | assert( pBt->pPage1->aData ); |
| 1779 | #if 0 |
drh | 24c9a2e | 2007-01-05 02:00:47 +0000 | [diff] [blame] | 1780 | if( pBt->pPage1->aData==0 ){ |
| 1781 | MemPage *pPage = pBt->pPage1; |
drh | bf4bca5 | 2007-09-06 22:19:14 +0000 | [diff] [blame] | 1782 | pPage->aData = sqlite3PagerGetData(pPage->pDbPage); |
drh | 24c9a2e | 2007-01-05 02:00:47 +0000 | [diff] [blame] | 1783 | pPage->pBt = pBt; |
| 1784 | pPage->pgno = 1; |
| 1785 | } |
drh | de4fcfd | 2008-01-19 23:50:26 +0000 | [diff] [blame] | 1786 | #endif |
drh | 24c9a2e | 2007-01-05 02:00:47 +0000 | [diff] [blame] | 1787 | releasePage(pBt->pPage1); |
drh | 51c6d96 | 2004-06-06 00:42:25 +0000 | [diff] [blame] | 1788 | } |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1789 | pBt->pPage1 = 0; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1790 | pBt->inStmt = 0; |
drh | b8ca307 | 2001-12-05 00:21:20 +0000 | [diff] [blame] | 1791 | } |
| 1792 | } |
| 1793 | |
| 1794 | /* |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 1795 | ** Create a new database by initializing the first page of the |
drh | 8c42ca9 | 2001-06-22 19:15:00 +0000 | [diff] [blame] | 1796 | ** file. |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 1797 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1798 | static int newDatabase(BtShared *pBt){ |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 1799 | MemPage *pP1; |
| 1800 | unsigned char *data; |
drh | 8c42ca9 | 2001-06-22 19:15:00 +0000 | [diff] [blame] | 1801 | int rc; |
danielk1977 | ad0132d | 2008-06-07 08:58:22 +0000 | [diff] [blame^] | 1802 | int nPage; |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 1803 | |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 1804 | assert( sqlite3_mutex_held(pBt->mutex) ); |
danielk1977 | ad0132d | 2008-06-07 08:58:22 +0000 | [diff] [blame^] | 1805 | rc = sqlite3PagerPagecount(pBt->pPager, &nPage); |
| 1806 | if( rc!=SQLITE_OK || nPage>0 ){ |
| 1807 | return rc; |
| 1808 | } |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1809 | pP1 = pBt->pPage1; |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 1810 | assert( pP1!=0 ); |
| 1811 | data = pP1->aData; |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 1812 | rc = sqlite3PagerWrite(pP1->pDbPage); |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 1813 | if( rc ) return rc; |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 1814 | memcpy(data, zMagicHeader, sizeof(zMagicHeader)); |
| 1815 | assert( sizeof(zMagicHeader)==16 ); |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 1816 | put2byte(&data[16], pBt->pageSize); |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 1817 | data[18] = 1; |
| 1818 | data[19] = 1; |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 1819 | data[20] = pBt->pageSize - pBt->usableSize; |
| 1820 | data[21] = pBt->maxEmbedFrac; |
| 1821 | data[22] = pBt->minEmbedFrac; |
| 1822 | data[23] = pBt->minLeafFrac; |
| 1823 | memset(&data[24], 0, 100-24); |
drh | e6c4381 | 2004-05-14 12:17:46 +0000 | [diff] [blame] | 1824 | zeroPage(pP1, PTF_INTKEY|PTF_LEAF|PTF_LEAFDATA ); |
drh | f2a611c | 2004-09-05 00:33:43 +0000 | [diff] [blame] | 1825 | pBt->pageSizeFixed = 1; |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 1826 | #ifndef SQLITE_OMIT_AUTOVACUUM |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 1827 | assert( pBt->autoVacuum==1 || pBt->autoVacuum==0 ); |
danielk1977 | 418899a | 2007-06-24 10:14:00 +0000 | [diff] [blame] | 1828 | assert( pBt->incrVacuum==1 || pBt->incrVacuum==0 ); |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 1829 | put4byte(&data[36 + 4*4], pBt->autoVacuum); |
danielk1977 | 418899a | 2007-06-24 10:14:00 +0000 | [diff] [blame] | 1830 | put4byte(&data[36 + 7*4], pBt->incrVacuum); |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 1831 | #endif |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 1832 | return SQLITE_OK; |
| 1833 | } |
| 1834 | |
| 1835 | /* |
danielk1977 | ee5741e | 2004-05-31 10:01:34 +0000 | [diff] [blame] | 1836 | ** Attempt to start a new transaction. A write-transaction |
drh | 684917c | 2004-10-05 02:41:42 +0000 | [diff] [blame] | 1837 | ** is started if the second argument is nonzero, otherwise a read- |
| 1838 | ** transaction. If the second argument is 2 or more and exclusive |
| 1839 | ** transaction is started, meaning that no other process is allowed |
| 1840 | ** to access the database. A preexisting transaction may not be |
drh | b8ef32c | 2005-03-14 02:01:49 +0000 | [diff] [blame] | 1841 | ** upgraded to exclusive by calling this routine a second time - the |
drh | 684917c | 2004-10-05 02:41:42 +0000 | [diff] [blame] | 1842 | ** exclusivity flag only works for a new transaction. |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 1843 | ** |
danielk1977 | ee5741e | 2004-05-31 10:01:34 +0000 | [diff] [blame] | 1844 | ** A write-transaction must be started before attempting any |
| 1845 | ** changes to the database. None of the following routines |
| 1846 | ** will work unless a transaction is started first: |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 1847 | ** |
drh | 23e11ca | 2004-05-04 17:27:28 +0000 | [diff] [blame] | 1848 | ** sqlite3BtreeCreateTable() |
| 1849 | ** sqlite3BtreeCreateIndex() |
| 1850 | ** sqlite3BtreeClearTable() |
| 1851 | ** sqlite3BtreeDropTable() |
| 1852 | ** sqlite3BtreeInsert() |
| 1853 | ** sqlite3BtreeDelete() |
| 1854 | ** sqlite3BtreeUpdateMeta() |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 1855 | ** |
drh | b8ef32c | 2005-03-14 02:01:49 +0000 | [diff] [blame] | 1856 | ** If an initial attempt to acquire the lock fails because of lock contention |
| 1857 | ** and the database was previously unlocked, then invoke the busy handler |
| 1858 | ** if there is one. But if there was previously a read-lock, do not |
| 1859 | ** invoke the busy handler - just return SQLITE_BUSY. SQLITE_BUSY is |
| 1860 | ** returned when there is already a read-lock in order to avoid a deadlock. |
| 1861 | ** |
| 1862 | ** Suppose there are two processes A and B. A has a read lock and B has |
| 1863 | ** a reserved lock. B tries to promote to exclusive but is blocked because |
| 1864 | ** of A's read lock. A tries to promote to reserved but is blocked by B. |
| 1865 | ** One or the other of the two processes must give way or there can be |
| 1866 | ** no progress. By returning SQLITE_BUSY and not invoking the busy callback |
| 1867 | ** when A already has a read lock, we encourage A to give up and let B |
| 1868 | ** proceed. |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 1869 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1870 | int sqlite3BtreeBeginTrans(Btree *p, int wrflag){ |
| 1871 | BtShared *pBt = p->pBt; |
danielk1977 | ee5741e | 2004-05-31 10:01:34 +0000 | [diff] [blame] | 1872 | int rc = SQLITE_OK; |
| 1873 | |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 1874 | sqlite3BtreeEnter(p); |
drh | e5fe690 | 2007-12-07 18:55:28 +0000 | [diff] [blame] | 1875 | pBt->db = p->db; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1876 | btreeIntegrity(p); |
| 1877 | |
danielk1977 | ee5741e | 2004-05-31 10:01:34 +0000 | [diff] [blame] | 1878 | /* If the btree is already in a write-transaction, or it |
| 1879 | ** is already in a read-transaction and a read-transaction |
| 1880 | ** is requested, this is a no-op. |
| 1881 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1882 | if( p->inTrans==TRANS_WRITE || (p->inTrans==TRANS_READ && !wrflag) ){ |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 1883 | goto trans_begun; |
danielk1977 | ee5741e | 2004-05-31 10:01:34 +0000 | [diff] [blame] | 1884 | } |
drh | b8ef32c | 2005-03-14 02:01:49 +0000 | [diff] [blame] | 1885 | |
| 1886 | /* Write transactions are not possible on a read-only database */ |
danielk1977 | ee5741e | 2004-05-31 10:01:34 +0000 | [diff] [blame] | 1887 | if( pBt->readOnly && wrflag ){ |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 1888 | rc = SQLITE_READONLY; |
| 1889 | goto trans_begun; |
danielk1977 | ee5741e | 2004-05-31 10:01:34 +0000 | [diff] [blame] | 1890 | } |
| 1891 | |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1892 | /* If another database handle has already opened a write transaction |
| 1893 | ** on this shared-btree structure and a second write transaction is |
| 1894 | ** requested, return SQLITE_BUSY. |
| 1895 | */ |
| 1896 | if( pBt->inTransaction==TRANS_WRITE && wrflag ){ |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 1897 | rc = SQLITE_BUSY; |
| 1898 | goto trans_begun; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1899 | } |
| 1900 | |
danielk1977 | 641b0f4 | 2007-12-21 04:47:25 +0000 | [diff] [blame] | 1901 | #ifndef SQLITE_OMIT_SHARED_CACHE |
| 1902 | if( wrflag>1 ){ |
| 1903 | BtLock *pIter; |
| 1904 | for(pIter=pBt->pLock; pIter; pIter=pIter->pNext){ |
| 1905 | if( pIter->pBtree!=p ){ |
| 1906 | rc = SQLITE_BUSY; |
| 1907 | goto trans_begun; |
| 1908 | } |
| 1909 | } |
| 1910 | } |
| 1911 | #endif |
| 1912 | |
drh | b8ef32c | 2005-03-14 02:01:49 +0000 | [diff] [blame] | 1913 | do { |
drh | 8a9c17f | 2008-05-02 14:23:54 +0000 | [diff] [blame] | 1914 | if( pBt->pPage1==0 ){ |
| 1915 | do{ |
| 1916 | rc = lockBtree(pBt); |
| 1917 | }while( pBt->pPage1==0 && rc==SQLITE_OK ); |
drh | 8c42ca9 | 2001-06-22 19:15:00 +0000 | [diff] [blame] | 1918 | } |
drh | 309169a | 2007-04-24 17:27:51 +0000 | [diff] [blame] | 1919 | |
drh | b8ef32c | 2005-03-14 02:01:49 +0000 | [diff] [blame] | 1920 | if( rc==SQLITE_OK && wrflag ){ |
drh | 309169a | 2007-04-24 17:27:51 +0000 | [diff] [blame] | 1921 | if( pBt->readOnly ){ |
| 1922 | rc = SQLITE_READONLY; |
| 1923 | }else{ |
| 1924 | rc = sqlite3PagerBegin(pBt->pPage1->pDbPage, wrflag>1); |
| 1925 | if( rc==SQLITE_OK ){ |
| 1926 | rc = newDatabase(pBt); |
| 1927 | } |
drh | b8ef32c | 2005-03-14 02:01:49 +0000 | [diff] [blame] | 1928 | } |
| 1929 | } |
| 1930 | |
| 1931 | if( rc==SQLITE_OK ){ |
drh | b8ef32c | 2005-03-14 02:01:49 +0000 | [diff] [blame] | 1932 | if( wrflag ) pBt->inStmt = 0; |
| 1933 | }else{ |
| 1934 | unlockBtreeIfUnused(pBt); |
| 1935 | } |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1936 | }while( rc==SQLITE_BUSY && pBt->inTransaction==TRANS_NONE && |
drh | e5fe690 | 2007-12-07 18:55:28 +0000 | [diff] [blame] | 1937 | sqlite3BtreeInvokeBusyHandler(pBt, 0) ); |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1938 | |
| 1939 | if( rc==SQLITE_OK ){ |
| 1940 | if( p->inTrans==TRANS_NONE ){ |
| 1941 | pBt->nTransaction++; |
| 1942 | } |
| 1943 | p->inTrans = (wrflag?TRANS_WRITE:TRANS_READ); |
| 1944 | if( p->inTrans>pBt->inTransaction ){ |
| 1945 | pBt->inTransaction = p->inTrans; |
| 1946 | } |
danielk1977 | 641b0f4 | 2007-12-21 04:47:25 +0000 | [diff] [blame] | 1947 | #ifndef SQLITE_OMIT_SHARED_CACHE |
| 1948 | if( wrflag>1 ){ |
| 1949 | assert( !pBt->pExclusive ); |
| 1950 | pBt->pExclusive = p; |
| 1951 | } |
| 1952 | #endif |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1953 | } |
| 1954 | |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 1955 | |
| 1956 | trans_begun: |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1957 | btreeIntegrity(p); |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 1958 | sqlite3BtreeLeave(p); |
drh | b8ca307 | 2001-12-05 00:21:20 +0000 | [diff] [blame] | 1959 | return rc; |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 1960 | } |
| 1961 | |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 1962 | #ifndef SQLITE_OMIT_AUTOVACUUM |
| 1963 | |
| 1964 | /* |
| 1965 | ** Set the pointer-map entries for all children of page pPage. Also, if |
| 1966 | ** pPage contains cells that point to overflow pages, set the pointer |
| 1967 | ** map entries for the overflow pages as well. |
| 1968 | */ |
| 1969 | static int setChildPtrmaps(MemPage *pPage){ |
| 1970 | int i; /* Counter variable */ |
| 1971 | int nCell; /* Number of cells in page pPage */ |
danielk1977 | 2df71c7 | 2007-05-24 07:22:42 +0000 | [diff] [blame] | 1972 | int rc; /* Return code */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1973 | BtShared *pBt = pPage->pBt; |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 1974 | int isInitOrig = pPage->isInit; |
| 1975 | Pgno pgno = pPage->pgno; |
| 1976 | |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 1977 | assert( sqlite3_mutex_held(pPage->pBt->mutex) ); |
danielk1977 | 2df71c7 | 2007-05-24 07:22:42 +0000 | [diff] [blame] | 1978 | rc = sqlite3BtreeInitPage(pPage, pPage->pParent); |
| 1979 | if( rc!=SQLITE_OK ){ |
| 1980 | goto set_child_ptrmaps_out; |
| 1981 | } |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 1982 | nCell = pPage->nCell; |
| 1983 | |
| 1984 | for(i=0; i<nCell; i++){ |
danielk1977 | 1cc5ed8 | 2007-05-16 17:28:43 +0000 | [diff] [blame] | 1985 | u8 *pCell = findCell(pPage, i); |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 1986 | |
danielk1977 | 2683665 | 2005-01-17 01:33:13 +0000 | [diff] [blame] | 1987 | rc = ptrmapPutOvflPtr(pPage, pCell); |
| 1988 | if( rc!=SQLITE_OK ){ |
| 1989 | goto set_child_ptrmaps_out; |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 1990 | } |
danielk1977 | 2683665 | 2005-01-17 01:33:13 +0000 | [diff] [blame] | 1991 | |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 1992 | if( !pPage->leaf ){ |
| 1993 | Pgno childPgno = get4byte(pCell); |
| 1994 | rc = ptrmapPut(pBt, childPgno, PTRMAP_BTREE, pgno); |
| 1995 | if( rc!=SQLITE_OK ) goto set_child_ptrmaps_out; |
| 1996 | } |
| 1997 | } |
| 1998 | |
| 1999 | if( !pPage->leaf ){ |
| 2000 | Pgno childPgno = get4byte(&pPage->aData[pPage->hdrOffset+8]); |
| 2001 | rc = ptrmapPut(pBt, childPgno, PTRMAP_BTREE, pgno); |
| 2002 | } |
| 2003 | |
| 2004 | set_child_ptrmaps_out: |
| 2005 | pPage->isInit = isInitOrig; |
| 2006 | return rc; |
| 2007 | } |
| 2008 | |
| 2009 | /* |
| 2010 | ** Somewhere on pPage, which is guarenteed to be a btree page, not an overflow |
| 2011 | ** page, is a pointer to page iFrom. Modify this pointer so that it points to |
| 2012 | ** iTo. Parameter eType describes the type of pointer to be modified, as |
| 2013 | ** follows: |
| 2014 | ** |
| 2015 | ** PTRMAP_BTREE: pPage is a btree-page. The pointer points at a child |
| 2016 | ** page of pPage. |
| 2017 | ** |
| 2018 | ** PTRMAP_OVERFLOW1: pPage is a btree-page. The pointer points at an overflow |
| 2019 | ** page pointed to by one of the cells on pPage. |
| 2020 | ** |
| 2021 | ** PTRMAP_OVERFLOW2: pPage is an overflow-page. The pointer points at the next |
| 2022 | ** overflow page in the list. |
| 2023 | */ |
danielk1977 | fdb7cdb | 2005-01-17 02:12:18 +0000 | [diff] [blame] | 2024 | static int modifyPagePointer(MemPage *pPage, Pgno iFrom, Pgno iTo, u8 eType){ |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 2025 | assert( sqlite3_mutex_held(pPage->pBt->mutex) ); |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 2026 | if( eType==PTRMAP_OVERFLOW2 ){ |
danielk1977 | f78fc08 | 2004-11-02 14:40:32 +0000 | [diff] [blame] | 2027 | /* 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] | 2028 | if( get4byte(pPage->aData)!=iFrom ){ |
drh | 4928570 | 2005-09-17 15:20:26 +0000 | [diff] [blame] | 2029 | return SQLITE_CORRUPT_BKPT; |
danielk1977 | fdb7cdb | 2005-01-17 02:12:18 +0000 | [diff] [blame] | 2030 | } |
danielk1977 | f78fc08 | 2004-11-02 14:40:32 +0000 | [diff] [blame] | 2031 | put4byte(pPage->aData, iTo); |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 2032 | }else{ |
| 2033 | int isInitOrig = pPage->isInit; |
| 2034 | int i; |
| 2035 | int nCell; |
| 2036 | |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 2037 | sqlite3BtreeInitPage(pPage, 0); |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 2038 | nCell = pPage->nCell; |
| 2039 | |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 2040 | for(i=0; i<nCell; i++){ |
danielk1977 | 1cc5ed8 | 2007-05-16 17:28:43 +0000 | [diff] [blame] | 2041 | u8 *pCell = findCell(pPage, i); |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 2042 | if( eType==PTRMAP_OVERFLOW1 ){ |
| 2043 | CellInfo info; |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 2044 | sqlite3BtreeParseCellPtr(pPage, pCell, &info); |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 2045 | if( info.iOverflow ){ |
| 2046 | if( iFrom==get4byte(&pCell[info.iOverflow]) ){ |
| 2047 | put4byte(&pCell[info.iOverflow], iTo); |
| 2048 | break; |
| 2049 | } |
| 2050 | } |
| 2051 | }else{ |
| 2052 | if( get4byte(pCell)==iFrom ){ |
| 2053 | put4byte(pCell, iTo); |
| 2054 | break; |
| 2055 | } |
| 2056 | } |
| 2057 | } |
| 2058 | |
| 2059 | if( i==nCell ){ |
danielk1977 | fdb7cdb | 2005-01-17 02:12:18 +0000 | [diff] [blame] | 2060 | if( eType!=PTRMAP_BTREE || |
| 2061 | get4byte(&pPage->aData[pPage->hdrOffset+8])!=iFrom ){ |
drh | 4928570 | 2005-09-17 15:20:26 +0000 | [diff] [blame] | 2062 | return SQLITE_CORRUPT_BKPT; |
danielk1977 | fdb7cdb | 2005-01-17 02:12:18 +0000 | [diff] [blame] | 2063 | } |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 2064 | put4byte(&pPage->aData[pPage->hdrOffset+8], iTo); |
| 2065 | } |
| 2066 | |
| 2067 | pPage->isInit = isInitOrig; |
| 2068 | } |
danielk1977 | fdb7cdb | 2005-01-17 02:12:18 +0000 | [diff] [blame] | 2069 | return SQLITE_OK; |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 2070 | } |
| 2071 | |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 2072 | |
danielk1977 | 7701e81 | 2005-01-10 12:59:51 +0000 | [diff] [blame] | 2073 | /* |
| 2074 | ** Move the open database page pDbPage to location iFreePage in the |
| 2075 | ** database. The pDbPage reference remains valid. |
| 2076 | */ |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 2077 | static int relocatePage( |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2078 | BtShared *pBt, /* Btree */ |
danielk1977 | 7701e81 | 2005-01-10 12:59:51 +0000 | [diff] [blame] | 2079 | MemPage *pDbPage, /* Open page to move */ |
| 2080 | u8 eType, /* Pointer map 'type' entry for pDbPage */ |
| 2081 | Pgno iPtrPage, /* Pointer map 'page-no' entry for pDbPage */ |
| 2082 | Pgno iFreePage /* The location to move pDbPage to */ |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 2083 | ){ |
| 2084 | MemPage *pPtrPage; /* The page that contains a pointer to pDbPage */ |
| 2085 | Pgno iDbPage = pDbPage->pgno; |
| 2086 | Pager *pPager = pBt->pPager; |
| 2087 | int rc; |
| 2088 | |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 2089 | assert( eType==PTRMAP_OVERFLOW2 || eType==PTRMAP_OVERFLOW1 || |
| 2090 | eType==PTRMAP_BTREE || eType==PTRMAP_ROOTPAGE ); |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 2091 | assert( sqlite3_mutex_held(pBt->mutex) ); |
drh | d0679ed | 2007-08-28 22:24:34 +0000 | [diff] [blame] | 2092 | assert( pDbPage->pBt==pBt ); |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 2093 | |
drh | 85b623f | 2007-12-13 21:54:09 +0000 | [diff] [blame] | 2094 | /* Move page iDbPage from its current location to page number iFreePage */ |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 2095 | TRACE(("AUTOVACUUM: Moving %d to free page %d (ptr page %d type %d)\n", |
| 2096 | iDbPage, iFreePage, iPtrPage, eType)); |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 2097 | rc = sqlite3PagerMovepage(pPager, pDbPage->pDbPage, iFreePage); |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 2098 | if( rc!=SQLITE_OK ){ |
| 2099 | return rc; |
| 2100 | } |
| 2101 | pDbPage->pgno = iFreePage; |
| 2102 | |
| 2103 | /* If pDbPage was a btree-page, then it may have child pages and/or cells |
| 2104 | ** that point to overflow pages. The pointer map entries for all these |
| 2105 | ** pages need to be changed. |
| 2106 | ** |
| 2107 | ** If pDbPage is an overflow page, then the first 4 bytes may store a |
| 2108 | ** pointer to a subsequent overflow page. If this is the case, then |
| 2109 | ** the pointer map needs to be updated for the subsequent overflow page. |
| 2110 | */ |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 2111 | if( eType==PTRMAP_BTREE || eType==PTRMAP_ROOTPAGE ){ |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 2112 | rc = setChildPtrmaps(pDbPage); |
| 2113 | if( rc!=SQLITE_OK ){ |
| 2114 | return rc; |
| 2115 | } |
| 2116 | }else{ |
| 2117 | Pgno nextOvfl = get4byte(pDbPage->aData); |
| 2118 | if( nextOvfl!=0 ){ |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 2119 | rc = ptrmapPut(pBt, nextOvfl, PTRMAP_OVERFLOW2, iFreePage); |
| 2120 | if( rc!=SQLITE_OK ){ |
| 2121 | return rc; |
| 2122 | } |
| 2123 | } |
| 2124 | } |
| 2125 | |
| 2126 | /* Fix the database pointer on page iPtrPage that pointed at iDbPage so |
| 2127 | ** that it points at iFreePage. Also fix the pointer map entry for |
| 2128 | ** iPtrPage. |
| 2129 | */ |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 2130 | if( eType!=PTRMAP_ROOTPAGE ){ |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 2131 | rc = sqlite3BtreeGetPage(pBt, iPtrPage, &pPtrPage, 0); |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 2132 | if( rc!=SQLITE_OK ){ |
| 2133 | return rc; |
| 2134 | } |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 2135 | rc = sqlite3PagerWrite(pPtrPage->pDbPage); |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 2136 | if( rc!=SQLITE_OK ){ |
| 2137 | releasePage(pPtrPage); |
| 2138 | return rc; |
| 2139 | } |
danielk1977 | fdb7cdb | 2005-01-17 02:12:18 +0000 | [diff] [blame] | 2140 | rc = modifyPagePointer(pPtrPage, iDbPage, iFreePage, eType); |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 2141 | releasePage(pPtrPage); |
danielk1977 | fdb7cdb | 2005-01-17 02:12:18 +0000 | [diff] [blame] | 2142 | if( rc==SQLITE_OK ){ |
| 2143 | rc = ptrmapPut(pBt, iFreePage, eType, iPtrPage); |
| 2144 | } |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 2145 | } |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 2146 | return rc; |
| 2147 | } |
| 2148 | |
danielk1977 | ad0132d | 2008-06-07 08:58:22 +0000 | [diff] [blame^] | 2149 | static int pagerPagecount(Pager *pPager){ |
| 2150 | int rc; |
| 2151 | int nPage; |
| 2152 | rc = sqlite3PagerPagecount(pPager, &nPage); |
| 2153 | return (rc==SQLITE_OK?nPage:-1); |
| 2154 | } |
| 2155 | |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 2156 | /* Forward declaration required by incrVacuumStep(). */ |
drh | 4f0c587 | 2007-03-26 22:05:01 +0000 | [diff] [blame] | 2157 | static int allocateBtreePage(BtShared *, MemPage **, Pgno *, Pgno, u8); |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 2158 | |
| 2159 | /* |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 2160 | ** Perform a single step of an incremental-vacuum. If successful, |
| 2161 | ** return SQLITE_OK. If there is no work to do (and therefore no |
| 2162 | ** point in calling this function again), return SQLITE_DONE. |
| 2163 | ** |
| 2164 | ** More specificly, this function attempts to re-organize the |
| 2165 | ** database so that the last page of the file currently in use |
| 2166 | ** is no longer in use. |
| 2167 | ** |
| 2168 | ** If the nFin parameter is non-zero, the implementation assumes |
| 2169 | ** that the caller will keep calling incrVacuumStep() until |
| 2170 | ** it returns SQLITE_DONE or an error, and that nFin is the |
| 2171 | ** number of pages the database file will contain after this |
| 2172 | ** process is complete. |
| 2173 | */ |
| 2174 | static int incrVacuumStep(BtShared *pBt, Pgno nFin){ |
| 2175 | Pgno iLastPg; /* Last page in the database */ |
| 2176 | Pgno nFreeList; /* Number of pages still on the free-list */ |
| 2177 | |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 2178 | assert( sqlite3_mutex_held(pBt->mutex) ); |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 2179 | iLastPg = pBt->nTrunc; |
| 2180 | if( iLastPg==0 ){ |
danielk1977 | ad0132d | 2008-06-07 08:58:22 +0000 | [diff] [blame^] | 2181 | iLastPg = pagerPagecount(pBt->pPager); |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 2182 | } |
| 2183 | |
| 2184 | if( !PTRMAP_ISPAGE(pBt, iLastPg) && iLastPg!=PENDING_BYTE_PAGE(pBt) ){ |
| 2185 | int rc; |
| 2186 | u8 eType; |
| 2187 | Pgno iPtrPage; |
| 2188 | |
| 2189 | nFreeList = get4byte(&pBt->pPage1->aData[36]); |
| 2190 | if( nFreeList==0 || nFin==iLastPg ){ |
| 2191 | return SQLITE_DONE; |
| 2192 | } |
| 2193 | |
| 2194 | rc = ptrmapGet(pBt, iLastPg, &eType, &iPtrPage); |
| 2195 | if( rc!=SQLITE_OK ){ |
| 2196 | return rc; |
| 2197 | } |
| 2198 | if( eType==PTRMAP_ROOTPAGE ){ |
| 2199 | return SQLITE_CORRUPT_BKPT; |
| 2200 | } |
| 2201 | |
| 2202 | if( eType==PTRMAP_FREEPAGE ){ |
| 2203 | if( nFin==0 ){ |
| 2204 | /* Remove the page from the files free-list. This is not required |
danielk1977 | 4ef2449 | 2007-05-23 09:52:41 +0000 | [diff] [blame] | 2205 | ** if nFin is non-zero. In that case, the free-list will be |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 2206 | ** truncated to zero after this function returns, so it doesn't |
| 2207 | ** matter if it still contains some garbage entries. |
| 2208 | */ |
| 2209 | Pgno iFreePg; |
| 2210 | MemPage *pFreePg; |
| 2211 | rc = allocateBtreePage(pBt, &pFreePg, &iFreePg, iLastPg, 1); |
| 2212 | if( rc!=SQLITE_OK ){ |
| 2213 | return rc; |
| 2214 | } |
| 2215 | assert( iFreePg==iLastPg ); |
| 2216 | releasePage(pFreePg); |
| 2217 | } |
| 2218 | } else { |
| 2219 | Pgno iFreePg; /* Index of free page to move pLastPg to */ |
| 2220 | MemPage *pLastPg; |
| 2221 | |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 2222 | rc = sqlite3BtreeGetPage(pBt, iLastPg, &pLastPg, 0); |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 2223 | if( rc!=SQLITE_OK ){ |
| 2224 | return rc; |
| 2225 | } |
| 2226 | |
danielk1977 | b4626a3 | 2007-04-28 15:47:43 +0000 | [diff] [blame] | 2227 | /* If nFin is zero, this loop runs exactly once and page pLastPg |
| 2228 | ** is swapped with the first free page pulled off the free list. |
| 2229 | ** |
| 2230 | ** On the other hand, if nFin is greater than zero, then keep |
| 2231 | ** looping until a free-page located within the first nFin pages |
| 2232 | ** of the file is found. |
| 2233 | */ |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 2234 | do { |
| 2235 | MemPage *pFreePg; |
| 2236 | rc = allocateBtreePage(pBt, &pFreePg, &iFreePg, 0, 0); |
| 2237 | if( rc!=SQLITE_OK ){ |
| 2238 | releasePage(pLastPg); |
| 2239 | return rc; |
| 2240 | } |
| 2241 | releasePage(pFreePg); |
| 2242 | }while( nFin!=0 && iFreePg>nFin ); |
| 2243 | assert( iFreePg<iLastPg ); |
danielk1977 | b4626a3 | 2007-04-28 15:47:43 +0000 | [diff] [blame] | 2244 | |
| 2245 | rc = sqlite3PagerWrite(pLastPg->pDbPage); |
danielk1977 | 662278e | 2007-11-05 15:30:12 +0000 | [diff] [blame] | 2246 | if( rc==SQLITE_OK ){ |
| 2247 | rc = relocatePage(pBt, pLastPg, eType, iPtrPage, iFreePg); |
| 2248 | } |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 2249 | releasePage(pLastPg); |
| 2250 | if( rc!=SQLITE_OK ){ |
| 2251 | return rc; |
danielk1977 | 662278e | 2007-11-05 15:30:12 +0000 | [diff] [blame] | 2252 | } |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 2253 | } |
| 2254 | } |
| 2255 | |
| 2256 | pBt->nTrunc = iLastPg - 1; |
| 2257 | while( pBt->nTrunc==PENDING_BYTE_PAGE(pBt)||PTRMAP_ISPAGE(pBt, pBt->nTrunc) ){ |
| 2258 | pBt->nTrunc--; |
| 2259 | } |
| 2260 | return SQLITE_OK; |
| 2261 | } |
| 2262 | |
| 2263 | /* |
| 2264 | ** A write-transaction must be opened before calling this function. |
| 2265 | ** It performs a single unit of work towards an incremental vacuum. |
| 2266 | ** |
| 2267 | ** If the incremental vacuum is finished after this function has run, |
| 2268 | ** SQLITE_DONE is returned. If it is not finished, but no error occured, |
| 2269 | ** SQLITE_OK is returned. Otherwise an SQLite error code. |
| 2270 | */ |
| 2271 | int sqlite3BtreeIncrVacuum(Btree *p){ |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 2272 | int rc; |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 2273 | BtShared *pBt = p->pBt; |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 2274 | |
| 2275 | sqlite3BtreeEnter(p); |
drh | e5fe690 | 2007-12-07 18:55:28 +0000 | [diff] [blame] | 2276 | pBt->db = p->db; |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 2277 | assert( pBt->inTransaction==TRANS_WRITE && p->inTrans==TRANS_WRITE ); |
| 2278 | if( !pBt->autoVacuum ){ |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 2279 | rc = SQLITE_DONE; |
| 2280 | }else{ |
| 2281 | invalidateAllOverflowCache(pBt); |
| 2282 | rc = incrVacuumStep(pBt, 0); |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 2283 | } |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 2284 | sqlite3BtreeLeave(p); |
| 2285 | return rc; |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 2286 | } |
| 2287 | |
| 2288 | /* |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 2289 | ** This routine is called prior to sqlite3PagerCommit when a transaction |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 2290 | ** is commited for an auto-vacuum database. |
danielk1977 | 2416872 | 2007-04-02 05:07:47 +0000 | [diff] [blame] | 2291 | ** |
| 2292 | ** If SQLITE_OK is returned, then *pnTrunc is set to the number of pages |
| 2293 | ** the database file should be truncated to during the commit process. |
| 2294 | ** i.e. the database has been reorganized so that only the first *pnTrunc |
| 2295 | ** pages are in use. |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 2296 | */ |
danielk1977 | 2416872 | 2007-04-02 05:07:47 +0000 | [diff] [blame] | 2297 | static int autoVacuumCommit(BtShared *pBt, Pgno *pnTrunc){ |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 2298 | int rc = SQLITE_OK; |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 2299 | Pager *pPager = pBt->pPager; |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 2300 | #ifndef NDEBUG |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 2301 | int nRef = sqlite3PagerRefcount(pPager); |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 2302 | #endif |
| 2303 | |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 2304 | assert( sqlite3_mutex_held(pBt->mutex) ); |
danielk1977 | 92d4d7a | 2007-05-04 12:05:56 +0000 | [diff] [blame] | 2305 | invalidateAllOverflowCache(pBt); |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 2306 | assert(pBt->autoVacuum); |
| 2307 | if( !pBt->incrVacuum ){ |
| 2308 | Pgno nFin = 0; |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 2309 | |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 2310 | if( pBt->nTrunc==0 ){ |
| 2311 | Pgno nFree; |
| 2312 | Pgno nPtrmap; |
| 2313 | const int pgsz = pBt->pageSize; |
danielk1977 | ad0132d | 2008-06-07 08:58:22 +0000 | [diff] [blame^] | 2314 | int nOrig = pagerPagecount(pBt->pPager); |
danielk1977 | e5321f0 | 2007-04-27 07:05:44 +0000 | [diff] [blame] | 2315 | |
| 2316 | if( PTRMAP_ISPAGE(pBt, nOrig) ){ |
| 2317 | return SQLITE_CORRUPT_BKPT; |
| 2318 | } |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 2319 | if( nOrig==PENDING_BYTE_PAGE(pBt) ){ |
| 2320 | nOrig--; |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 2321 | } |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 2322 | nFree = get4byte(&pBt->pPage1->aData[36]); |
| 2323 | nPtrmap = (nFree-nOrig+PTRMAP_PAGENO(pBt, nOrig)+pgsz/5)/(pgsz/5); |
| 2324 | nFin = nOrig - nFree - nPtrmap; |
| 2325 | if( nOrig>PENDING_BYTE_PAGE(pBt) && nFin<=PENDING_BYTE_PAGE(pBt) ){ |
| 2326 | nFin--; |
danielk1977 | ac11ee6 | 2005-01-15 12:45:51 +0000 | [diff] [blame] | 2327 | } |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 2328 | while( PTRMAP_ISPAGE(pBt, nFin) || nFin==PENDING_BYTE_PAGE(pBt) ){ |
| 2329 | nFin--; |
| 2330 | } |
| 2331 | } |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 2332 | |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 2333 | while( rc==SQLITE_OK ){ |
| 2334 | rc = incrVacuumStep(pBt, nFin); |
| 2335 | } |
| 2336 | if( rc==SQLITE_DONE ){ |
| 2337 | assert(nFin==0 || pBt->nTrunc==0 || nFin<=pBt->nTrunc); |
| 2338 | rc = SQLITE_OK; |
danielk1977 | 0ba32df | 2008-05-07 07:13:16 +0000 | [diff] [blame] | 2339 | if( pBt->nTrunc && nFin ){ |
drh | 67f80b6 | 2007-07-23 19:26:17 +0000 | [diff] [blame] | 2340 | rc = sqlite3PagerWrite(pBt->pPage1->pDbPage); |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 2341 | put4byte(&pBt->pPage1->aData[32], 0); |
| 2342 | put4byte(&pBt->pPage1->aData[36], 0); |
| 2343 | pBt->nTrunc = nFin; |
| 2344 | } |
| 2345 | } |
| 2346 | if( rc!=SQLITE_OK ){ |
| 2347 | sqlite3PagerRollback(pPager); |
| 2348 | } |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 2349 | } |
| 2350 | |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 2351 | if( rc==SQLITE_OK ){ |
| 2352 | *pnTrunc = pBt->nTrunc; |
| 2353 | pBt->nTrunc = 0; |
| 2354 | } |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 2355 | assert( nRef==sqlite3PagerRefcount(pPager) ); |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 2356 | return rc; |
| 2357 | } |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 2358 | |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 2359 | #endif |
| 2360 | |
| 2361 | /* |
drh | 80e35f4 | 2007-03-30 14:06:34 +0000 | [diff] [blame] | 2362 | ** This routine does the first phase of a two-phase commit. This routine |
| 2363 | ** causes a rollback journal to be created (if it does not already exist) |
| 2364 | ** and populated with enough information so that if a power loss occurs |
| 2365 | ** the database can be restored to its original state by playing back |
| 2366 | ** the journal. Then the contents of the journal are flushed out to |
| 2367 | ** the disk. After the journal is safely on oxide, the changes to the |
| 2368 | ** database are written into the database file and flushed to oxide. |
| 2369 | ** At the end of this call, the rollback journal still exists on the |
| 2370 | ** disk and we are still holding all locks, so the transaction has not |
| 2371 | ** committed. See sqlite3BtreeCommit() for the second phase of the |
| 2372 | ** commit process. |
| 2373 | ** |
| 2374 | ** This call is a no-op if no write-transaction is currently active on pBt. |
| 2375 | ** |
| 2376 | ** Otherwise, sync the database file for the btree pBt. zMaster points to |
| 2377 | ** the name of a master journal file that should be written into the |
| 2378 | ** individual journal file, or is NULL, indicating no master journal file |
| 2379 | ** (single database transaction). |
| 2380 | ** |
| 2381 | ** When this is called, the master journal should already have been |
| 2382 | ** created, populated with this journal pointer and synced to disk. |
| 2383 | ** |
| 2384 | ** Once this is routine has returned, the only thing required to commit |
| 2385 | ** the write-transaction for this database file is to delete the journal. |
| 2386 | */ |
| 2387 | int sqlite3BtreeCommitPhaseOne(Btree *p, const char *zMaster){ |
| 2388 | int rc = SQLITE_OK; |
| 2389 | if( p->inTrans==TRANS_WRITE ){ |
| 2390 | BtShared *pBt = p->pBt; |
| 2391 | Pgno nTrunc = 0; |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 2392 | sqlite3BtreeEnter(p); |
drh | e5fe690 | 2007-12-07 18:55:28 +0000 | [diff] [blame] | 2393 | pBt->db = p->db; |
drh | 80e35f4 | 2007-03-30 14:06:34 +0000 | [diff] [blame] | 2394 | #ifndef SQLITE_OMIT_AUTOVACUUM |
| 2395 | if( pBt->autoVacuum ){ |
| 2396 | rc = autoVacuumCommit(pBt, &nTrunc); |
| 2397 | if( rc!=SQLITE_OK ){ |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 2398 | sqlite3BtreeLeave(p); |
drh | 80e35f4 | 2007-03-30 14:06:34 +0000 | [diff] [blame] | 2399 | return rc; |
| 2400 | } |
| 2401 | } |
| 2402 | #endif |
danielk1977 | f653d78 | 2008-03-20 11:04:21 +0000 | [diff] [blame] | 2403 | rc = sqlite3PagerCommitPhaseOne(pBt->pPager, zMaster, nTrunc, 0); |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 2404 | sqlite3BtreeLeave(p); |
drh | 80e35f4 | 2007-03-30 14:06:34 +0000 | [diff] [blame] | 2405 | } |
| 2406 | return rc; |
| 2407 | } |
| 2408 | |
| 2409 | /* |
drh | 2aa679f | 2001-06-25 02:11:07 +0000 | [diff] [blame] | 2410 | ** Commit the transaction currently in progress. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2411 | ** |
drh | 6e34599 | 2007-03-30 11:12:08 +0000 | [diff] [blame] | 2412 | ** This routine implements the second phase of a 2-phase commit. The |
| 2413 | ** sqlite3BtreeSync() routine does the first phase and should be invoked |
| 2414 | ** prior to calling this routine. The sqlite3BtreeSync() routine did |
| 2415 | ** all the work of writing information out to disk and flushing the |
| 2416 | ** contents so that they are written onto the disk platter. All this |
| 2417 | ** routine has to do is delete or truncate the rollback journal |
| 2418 | ** (which causes the transaction to commit) and drop locks. |
| 2419 | ** |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2420 | ** This will release the write lock on the database file. If there |
| 2421 | ** are no active cursors, it also releases the read lock. |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 2422 | */ |
drh | 80e35f4 | 2007-03-30 14:06:34 +0000 | [diff] [blame] | 2423 | int sqlite3BtreeCommitPhaseTwo(Btree *p){ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2424 | BtShared *pBt = p->pBt; |
| 2425 | |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 2426 | sqlite3BtreeEnter(p); |
drh | e5fe690 | 2007-12-07 18:55:28 +0000 | [diff] [blame] | 2427 | pBt->db = p->db; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2428 | btreeIntegrity(p); |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2429 | |
| 2430 | /* If the handle has a write-transaction open, commit the shared-btrees |
| 2431 | ** transaction and set the shared state to TRANS_READ. |
| 2432 | */ |
| 2433 | if( p->inTrans==TRANS_WRITE ){ |
danielk1977 | 7f7bc66 | 2006-01-23 13:47:47 +0000 | [diff] [blame] | 2434 | int rc; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2435 | assert( pBt->inTransaction==TRANS_WRITE ); |
| 2436 | assert( pBt->nTransaction>0 ); |
drh | 80e35f4 | 2007-03-30 14:06:34 +0000 | [diff] [blame] | 2437 | rc = sqlite3PagerCommitPhaseTwo(pBt->pPager); |
danielk1977 | 7f7bc66 | 2006-01-23 13:47:47 +0000 | [diff] [blame] | 2438 | if( rc!=SQLITE_OK ){ |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 2439 | sqlite3BtreeLeave(p); |
danielk1977 | 7f7bc66 | 2006-01-23 13:47:47 +0000 | [diff] [blame] | 2440 | return rc; |
| 2441 | } |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2442 | pBt->inTransaction = TRANS_READ; |
| 2443 | pBt->inStmt = 0; |
danielk1977 | ee5741e | 2004-05-31 10:01:34 +0000 | [diff] [blame] | 2444 | } |
danielk1977 | 7f7bc66 | 2006-01-23 13:47:47 +0000 | [diff] [blame] | 2445 | unlockAllTables(p); |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2446 | |
| 2447 | /* If the handle has any kind of transaction open, decrement the transaction |
| 2448 | ** count of the shared btree. If the transaction count reaches 0, set |
| 2449 | ** the shared state to TRANS_NONE. The unlockBtreeIfUnused() call below |
| 2450 | ** will unlock the pager. |
| 2451 | */ |
| 2452 | if( p->inTrans!=TRANS_NONE ){ |
| 2453 | pBt->nTransaction--; |
| 2454 | if( 0==pBt->nTransaction ){ |
| 2455 | pBt->inTransaction = TRANS_NONE; |
| 2456 | } |
| 2457 | } |
| 2458 | |
| 2459 | /* Set the handles current transaction state to TRANS_NONE and unlock |
| 2460 | ** the pager if this call closed the only read or write transaction. |
| 2461 | */ |
| 2462 | p->inTrans = TRANS_NONE; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2463 | unlockBtreeIfUnused(pBt); |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2464 | |
| 2465 | btreeIntegrity(p); |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 2466 | sqlite3BtreeLeave(p); |
danielk1977 | 7f7bc66 | 2006-01-23 13:47:47 +0000 | [diff] [blame] | 2467 | return SQLITE_OK; |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 2468 | } |
| 2469 | |
drh | 80e35f4 | 2007-03-30 14:06:34 +0000 | [diff] [blame] | 2470 | /* |
| 2471 | ** Do both phases of a commit. |
| 2472 | */ |
| 2473 | int sqlite3BtreeCommit(Btree *p){ |
| 2474 | int rc; |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 2475 | sqlite3BtreeEnter(p); |
drh | 80e35f4 | 2007-03-30 14:06:34 +0000 | [diff] [blame] | 2476 | rc = sqlite3BtreeCommitPhaseOne(p, 0); |
| 2477 | if( rc==SQLITE_OK ){ |
| 2478 | rc = sqlite3BtreeCommitPhaseTwo(p); |
| 2479 | } |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 2480 | sqlite3BtreeLeave(p); |
drh | 80e35f4 | 2007-03-30 14:06:34 +0000 | [diff] [blame] | 2481 | return rc; |
| 2482 | } |
| 2483 | |
danielk1977 | fbcd585 | 2004-06-15 02:44:18 +0000 | [diff] [blame] | 2484 | #ifndef NDEBUG |
| 2485 | /* |
| 2486 | ** Return the number of write-cursors open on this handle. This is for use |
| 2487 | ** in assert() expressions, so it is only compiled if NDEBUG is not |
| 2488 | ** defined. |
drh | fb98264 | 2007-08-30 01:19:59 +0000 | [diff] [blame] | 2489 | ** |
| 2490 | ** For the purposes of this routine, a write-cursor is any cursor that |
| 2491 | ** is capable of writing to the databse. That means the cursor was |
| 2492 | ** originally opened for writing and the cursor has not be disabled |
| 2493 | ** by having its state changed to CURSOR_FAULT. |
danielk1977 | fbcd585 | 2004-06-15 02:44:18 +0000 | [diff] [blame] | 2494 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2495 | static int countWriteCursors(BtShared *pBt){ |
danielk1977 | fbcd585 | 2004-06-15 02:44:18 +0000 | [diff] [blame] | 2496 | BtCursor *pCur; |
| 2497 | int r = 0; |
| 2498 | for(pCur=pBt->pCursor; pCur; pCur=pCur->pNext){ |
drh | fb98264 | 2007-08-30 01:19:59 +0000 | [diff] [blame] | 2499 | if( pCur->wrFlag && pCur->eState!=CURSOR_FAULT ) r++; |
danielk1977 | fbcd585 | 2004-06-15 02:44:18 +0000 | [diff] [blame] | 2500 | } |
| 2501 | return r; |
| 2502 | } |
| 2503 | #endif |
| 2504 | |
drh | c39e000 | 2004-05-07 23:50:57 +0000 | [diff] [blame] | 2505 | /* |
drh | fb98264 | 2007-08-30 01:19:59 +0000 | [diff] [blame] | 2506 | ** This routine sets the state to CURSOR_FAULT and the error |
| 2507 | ** code to errCode for every cursor on BtShared that pBtree |
| 2508 | ** references. |
| 2509 | ** |
| 2510 | ** Every cursor is tripped, including cursors that belong |
| 2511 | ** to other database connections that happen to be sharing |
| 2512 | ** the cache with pBtree. |
| 2513 | ** |
| 2514 | ** This routine gets called when a rollback occurs. |
| 2515 | ** All cursors using the same cache must be tripped |
| 2516 | ** to prevent them from trying to use the btree after |
| 2517 | ** the rollback. The rollback may have deleted tables |
| 2518 | ** or moved root pages, so it is not sufficient to |
| 2519 | ** save the state of the cursor. The cursor must be |
| 2520 | ** invalidated. |
| 2521 | */ |
| 2522 | void sqlite3BtreeTripAllCursors(Btree *pBtree, int errCode){ |
| 2523 | BtCursor *p; |
| 2524 | sqlite3BtreeEnter(pBtree); |
| 2525 | for(p=pBtree->pBt->pCursor; p; p=p->pNext){ |
| 2526 | clearCursorPosition(p); |
| 2527 | p->eState = CURSOR_FAULT; |
| 2528 | p->skip = errCode; |
| 2529 | } |
| 2530 | sqlite3BtreeLeave(pBtree); |
| 2531 | } |
| 2532 | |
| 2533 | /* |
drh | ecdc753 | 2001-09-23 02:35:53 +0000 | [diff] [blame] | 2534 | ** Rollback the transaction in progress. All cursors will be |
| 2535 | ** invalided by this operation. Any attempt to use a cursor |
| 2536 | ** that was open at the beginning of this operation will result |
| 2537 | ** in an error. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2538 | ** |
| 2539 | ** This will release the write lock on the database file. If there |
| 2540 | ** are no active cursors, it also releases the read lock. |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 2541 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2542 | int sqlite3BtreeRollback(Btree *p){ |
danielk1977 | 8d34dfd | 2006-01-24 16:37:57 +0000 | [diff] [blame] | 2543 | int rc; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2544 | BtShared *pBt = p->pBt; |
drh | 24cd67e | 2004-05-10 16:18:47 +0000 | [diff] [blame] | 2545 | MemPage *pPage1; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2546 | |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 2547 | sqlite3BtreeEnter(p); |
drh | e5fe690 | 2007-12-07 18:55:28 +0000 | [diff] [blame] | 2548 | pBt->db = p->db; |
danielk1977 | 2b8c13e | 2006-01-24 14:21:24 +0000 | [diff] [blame] | 2549 | rc = saveAllCursors(pBt, 0, 0); |
danielk1977 | 8d34dfd | 2006-01-24 16:37:57 +0000 | [diff] [blame] | 2550 | #ifndef SQLITE_OMIT_SHARED_CACHE |
danielk1977 | 2b8c13e | 2006-01-24 14:21:24 +0000 | [diff] [blame] | 2551 | if( rc!=SQLITE_OK ){ |
danielk1977 | 8d34dfd | 2006-01-24 16:37:57 +0000 | [diff] [blame] | 2552 | /* This is a horrible situation. An IO or malloc() error occured whilst |
| 2553 | ** trying to save cursor positions. If this is an automatic rollback (as |
| 2554 | ** the result of a constraint, malloc() failure or IO error) then |
| 2555 | ** the cache may be internally inconsistent (not contain valid trees) so |
| 2556 | ** we cannot simply return the error to the caller. Instead, abort |
| 2557 | ** all queries that may be using any of the cursors that failed to save. |
| 2558 | */ |
drh | fb98264 | 2007-08-30 01:19:59 +0000 | [diff] [blame] | 2559 | sqlite3BtreeTripAllCursors(p, rc); |
danielk1977 | 2b8c13e | 2006-01-24 14:21:24 +0000 | [diff] [blame] | 2560 | } |
danielk1977 | 8d34dfd | 2006-01-24 16:37:57 +0000 | [diff] [blame] | 2561 | #endif |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2562 | btreeIntegrity(p); |
| 2563 | unlockAllTables(p); |
| 2564 | |
| 2565 | if( p->inTrans==TRANS_WRITE ){ |
danielk1977 | 8d34dfd | 2006-01-24 16:37:57 +0000 | [diff] [blame] | 2566 | int rc2; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2567 | |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 2568 | #ifndef SQLITE_OMIT_AUTOVACUUM |
| 2569 | pBt->nTrunc = 0; |
| 2570 | #endif |
| 2571 | |
danielk1977 | 8d34dfd | 2006-01-24 16:37:57 +0000 | [diff] [blame] | 2572 | assert( TRANS_WRITE==pBt->inTransaction ); |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 2573 | rc2 = sqlite3PagerRollback(pBt->pPager); |
danielk1977 | 8d34dfd | 2006-01-24 16:37:57 +0000 | [diff] [blame] | 2574 | if( rc2!=SQLITE_OK ){ |
| 2575 | rc = rc2; |
| 2576 | } |
| 2577 | |
drh | 24cd67e | 2004-05-10 16:18:47 +0000 | [diff] [blame] | 2578 | /* The rollback may have destroyed the pPage1->aData value. So |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 2579 | ** call sqlite3BtreeGetPage() on page 1 again to make |
| 2580 | ** sure pPage1->aData is set correctly. */ |
| 2581 | if( sqlite3BtreeGetPage(pBt, 1, &pPage1, 0)==SQLITE_OK ){ |
drh | 24cd67e | 2004-05-10 16:18:47 +0000 | [diff] [blame] | 2582 | releasePage(pPage1); |
| 2583 | } |
danielk1977 | fbcd585 | 2004-06-15 02:44:18 +0000 | [diff] [blame] | 2584 | assert( countWriteCursors(pBt)==0 ); |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2585 | pBt->inTransaction = TRANS_READ; |
drh | 24cd67e | 2004-05-10 16:18:47 +0000 | [diff] [blame] | 2586 | } |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2587 | |
| 2588 | if( p->inTrans!=TRANS_NONE ){ |
| 2589 | assert( pBt->nTransaction>0 ); |
| 2590 | pBt->nTransaction--; |
| 2591 | if( 0==pBt->nTransaction ){ |
| 2592 | pBt->inTransaction = TRANS_NONE; |
| 2593 | } |
| 2594 | } |
| 2595 | |
| 2596 | p->inTrans = TRANS_NONE; |
danielk1977 | ee5741e | 2004-05-31 10:01:34 +0000 | [diff] [blame] | 2597 | pBt->inStmt = 0; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2598 | unlockBtreeIfUnused(pBt); |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2599 | |
| 2600 | btreeIntegrity(p); |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 2601 | sqlite3BtreeLeave(p); |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 2602 | return rc; |
| 2603 | } |
| 2604 | |
| 2605 | /* |
drh | ab01f61 | 2004-05-22 02:55:23 +0000 | [diff] [blame] | 2606 | ** Start a statement subtransaction. The subtransaction can |
| 2607 | ** can be rolled back independently of the main transaction. |
| 2608 | ** You must start a transaction before starting a subtransaction. |
| 2609 | ** The subtransaction is ended automatically if the main transaction |
drh | 663fc63 | 2002-02-02 18:49:19 +0000 | [diff] [blame] | 2610 | ** commits or rolls back. |
| 2611 | ** |
drh | ab01f61 | 2004-05-22 02:55:23 +0000 | [diff] [blame] | 2612 | ** Only one subtransaction may be active at a time. It is an error to try |
| 2613 | ** to start a new subtransaction if another subtransaction is already active. |
| 2614 | ** |
| 2615 | ** Statement subtransactions are used around individual SQL statements |
| 2616 | ** that are contained within a BEGIN...COMMIT block. If a constraint |
| 2617 | ** error occurs within the statement, the effect of that one statement |
| 2618 | ** can be rolled back without having to rollback the entire transaction. |
drh | 663fc63 | 2002-02-02 18:49:19 +0000 | [diff] [blame] | 2619 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2620 | int sqlite3BtreeBeginStmt(Btree *p){ |
drh | 663fc63 | 2002-02-02 18:49:19 +0000 | [diff] [blame] | 2621 | int rc; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2622 | BtShared *pBt = p->pBt; |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 2623 | sqlite3BtreeEnter(p); |
drh | e5fe690 | 2007-12-07 18:55:28 +0000 | [diff] [blame] | 2624 | pBt->db = p->db; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2625 | if( (p->inTrans!=TRANS_WRITE) || pBt->inStmt ){ |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 2626 | rc = pBt->readOnly ? SQLITE_READONLY : SQLITE_ERROR; |
| 2627 | }else{ |
| 2628 | assert( pBt->inTransaction==TRANS_WRITE ); |
| 2629 | rc = pBt->readOnly ? SQLITE_OK : sqlite3PagerStmtBegin(pBt->pPager); |
| 2630 | pBt->inStmt = 1; |
drh | 0d65dc0 | 2002-02-03 00:56:09 +0000 | [diff] [blame] | 2631 | } |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 2632 | sqlite3BtreeLeave(p); |
drh | 663fc63 | 2002-02-02 18:49:19 +0000 | [diff] [blame] | 2633 | return rc; |
| 2634 | } |
| 2635 | |
| 2636 | |
| 2637 | /* |
drh | ab01f61 | 2004-05-22 02:55:23 +0000 | [diff] [blame] | 2638 | ** Commit the statment subtransaction currently in progress. If no |
| 2639 | ** subtransaction is active, this is a no-op. |
drh | 663fc63 | 2002-02-02 18:49:19 +0000 | [diff] [blame] | 2640 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2641 | int sqlite3BtreeCommitStmt(Btree *p){ |
drh | 663fc63 | 2002-02-02 18:49:19 +0000 | [diff] [blame] | 2642 | int rc; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2643 | BtShared *pBt = p->pBt; |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 2644 | sqlite3BtreeEnter(p); |
drh | e5fe690 | 2007-12-07 18:55:28 +0000 | [diff] [blame] | 2645 | pBt->db = p->db; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2646 | if( pBt->inStmt && !pBt->readOnly ){ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 2647 | rc = sqlite3PagerStmtCommit(pBt->pPager); |
drh | 663fc63 | 2002-02-02 18:49:19 +0000 | [diff] [blame] | 2648 | }else{ |
| 2649 | rc = SQLITE_OK; |
| 2650 | } |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2651 | pBt->inStmt = 0; |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 2652 | sqlite3BtreeLeave(p); |
drh | 663fc63 | 2002-02-02 18:49:19 +0000 | [diff] [blame] | 2653 | return rc; |
| 2654 | } |
| 2655 | |
| 2656 | /* |
drh | ab01f61 | 2004-05-22 02:55:23 +0000 | [diff] [blame] | 2657 | ** Rollback the active statement subtransaction. If no subtransaction |
| 2658 | ** is active this routine is a no-op. |
drh | 663fc63 | 2002-02-02 18:49:19 +0000 | [diff] [blame] | 2659 | ** |
drh | ab01f61 | 2004-05-22 02:55:23 +0000 | [diff] [blame] | 2660 | ** All cursors will be invalidated by this operation. Any attempt |
drh | 663fc63 | 2002-02-02 18:49:19 +0000 | [diff] [blame] | 2661 | ** to use a cursor that was open at the beginning of this operation |
| 2662 | ** will result in an error. |
| 2663 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2664 | int sqlite3BtreeRollbackStmt(Btree *p){ |
danielk1977 | 97a227c | 2006-01-20 16:32:04 +0000 | [diff] [blame] | 2665 | int rc = SQLITE_OK; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2666 | BtShared *pBt = p->pBt; |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 2667 | sqlite3BtreeEnter(p); |
drh | e5fe690 | 2007-12-07 18:55:28 +0000 | [diff] [blame] | 2668 | pBt->db = p->db; |
danielk1977 | 97a227c | 2006-01-20 16:32:04 +0000 | [diff] [blame] | 2669 | if( pBt->inStmt && !pBt->readOnly ){ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 2670 | rc = sqlite3PagerStmtRollback(pBt->pPager); |
danielk1977 | 97a227c | 2006-01-20 16:32:04 +0000 | [diff] [blame] | 2671 | assert( countWriteCursors(pBt)==0 ); |
| 2672 | pBt->inStmt = 0; |
| 2673 | } |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 2674 | sqlite3BtreeLeave(p); |
drh | 663fc63 | 2002-02-02 18:49:19 +0000 | [diff] [blame] | 2675 | return rc; |
| 2676 | } |
| 2677 | |
| 2678 | /* |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 2679 | ** Create a new cursor for the BTree whose root is on the page |
| 2680 | ** iTable. The act of acquiring a cursor gets a read lock on |
| 2681 | ** the database file. |
drh | 1bee3d7 | 2001-10-15 00:44:35 +0000 | [diff] [blame] | 2682 | ** |
| 2683 | ** If wrFlag==0, then the cursor can only be used for reading. |
drh | f74b8d9 | 2002-09-01 23:20:45 +0000 | [diff] [blame] | 2684 | ** If wrFlag==1, then the cursor can be used for reading or for |
| 2685 | ** writing if other conditions for writing are also met. These |
| 2686 | ** are the conditions that must be met in order for writing to |
| 2687 | ** be allowed: |
drh | 6446c4d | 2001-12-15 14:22:18 +0000 | [diff] [blame] | 2688 | ** |
drh | f74b8d9 | 2002-09-01 23:20:45 +0000 | [diff] [blame] | 2689 | ** 1: The cursor must have been opened with wrFlag==1 |
| 2690 | ** |
drh | fe5d71d | 2007-03-19 11:54:10 +0000 | [diff] [blame] | 2691 | ** 2: Other database connections that share the same pager cache |
| 2692 | ** but which are not in the READ_UNCOMMITTED state may not have |
| 2693 | ** cursors open with wrFlag==0 on the same table. Otherwise |
| 2694 | ** the changes made by this write cursor would be visible to |
| 2695 | ** the read cursors in the other database connection. |
drh | f74b8d9 | 2002-09-01 23:20:45 +0000 | [diff] [blame] | 2696 | ** |
| 2697 | ** 3: The database must be writable (not on read-only media) |
| 2698 | ** |
| 2699 | ** 4: There must be an active transaction. |
| 2700 | ** |
drh | 6446c4d | 2001-12-15 14:22:18 +0000 | [diff] [blame] | 2701 | ** No checking is done to make sure that page iTable really is the |
| 2702 | ** root page of a b-tree. If it is not, then the cursor acquired |
| 2703 | ** will not work correctly. |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 2704 | */ |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 2705 | static int btreeCursor( |
danielk1977 | cd3e8f7 | 2008-03-25 09:47:35 +0000 | [diff] [blame] | 2706 | Btree *p, /* The btree */ |
| 2707 | int iTable, /* Root page of table to open */ |
| 2708 | int wrFlag, /* 1 to write. 0 read-only */ |
| 2709 | struct KeyInfo *pKeyInfo, /* First arg to comparison function */ |
| 2710 | BtCursor *pCur /* Space for new cursor */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2711 | ){ |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 2712 | int rc; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2713 | BtShared *pBt = p->pBt; |
drh | ecdc753 | 2001-09-23 02:35:53 +0000 | [diff] [blame] | 2714 | |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 2715 | assert( sqlite3BtreeHoldsMutex(p) ); |
drh | 8dcd7ca | 2004-08-08 19:43:29 +0000 | [diff] [blame] | 2716 | if( wrFlag ){ |
drh | 8dcd7ca | 2004-08-08 19:43:29 +0000 | [diff] [blame] | 2717 | if( pBt->readOnly ){ |
| 2718 | return SQLITE_READONLY; |
| 2719 | } |
drh | 980b1a7 | 2006-08-16 16:42:48 +0000 | [diff] [blame] | 2720 | if( checkReadLocks(p, iTable, 0) ){ |
drh | 8dcd7ca | 2004-08-08 19:43:29 +0000 | [diff] [blame] | 2721 | return SQLITE_LOCKED; |
| 2722 | } |
drh | a0c9a11 | 2004-03-10 13:42:37 +0000 | [diff] [blame] | 2723 | } |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2724 | |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 2725 | if( pBt->pPage1==0 ){ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2726 | rc = lockBtreeWithRetry(p); |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 2727 | if( rc!=SQLITE_OK ){ |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 2728 | return rc; |
| 2729 | } |
drh | 1831f18 | 2007-04-24 17:35:59 +0000 | [diff] [blame] | 2730 | if( pBt->readOnly && wrFlag ){ |
| 2731 | return SQLITE_READONLY; |
| 2732 | } |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 2733 | } |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 2734 | pCur->pgnoRoot = (Pgno)iTable; |
danielk1977 | ad0132d | 2008-06-07 08:58:22 +0000 | [diff] [blame^] | 2735 | if( iTable==1 && pagerPagecount(pBt->pPager)==0 ){ |
drh | 24cd67e | 2004-05-10 16:18:47 +0000 | [diff] [blame] | 2736 | rc = SQLITE_EMPTY; |
| 2737 | goto create_cursor_exception; |
| 2738 | } |
drh | de64713 | 2004-05-07 17:57:49 +0000 | [diff] [blame] | 2739 | rc = getAndInitPage(pBt, pCur->pgnoRoot, &pCur->pPage, 0); |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 2740 | if( rc!=SQLITE_OK ){ |
| 2741 | goto create_cursor_exception; |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 2742 | } |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2743 | |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2744 | /* Now that no other errors can occur, finish filling in the BtCursor |
| 2745 | ** variables, link the cursor into the BtShared list and set *ppCur (the |
| 2746 | ** output argument to this function). |
| 2747 | */ |
drh | 1e968a0 | 2008-03-25 00:22:21 +0000 | [diff] [blame] | 2748 | pCur->pKeyInfo = pKeyInfo; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2749 | pCur->pBtree = p; |
drh | d0679ed | 2007-08-28 22:24:34 +0000 | [diff] [blame] | 2750 | pCur->pBt = pBt; |
drh | ecdc753 | 2001-09-23 02:35:53 +0000 | [diff] [blame] | 2751 | pCur->wrFlag = wrFlag; |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 2752 | pCur->pNext = pBt->pCursor; |
| 2753 | if( pCur->pNext ){ |
| 2754 | pCur->pNext->pPrev = pCur; |
| 2755 | } |
| 2756 | pBt->pCursor = pCur; |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 2757 | pCur->eState = CURSOR_INVALID; |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 2758 | |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2759 | return SQLITE_OK; |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 2760 | |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 2761 | create_cursor_exception: |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 2762 | if( pCur ){ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2763 | releasePage(pCur->pPage); |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 2764 | } |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2765 | unlockBtreeIfUnused(pBt); |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 2766 | return rc; |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 2767 | } |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 2768 | int sqlite3BtreeCursor( |
danielk1977 | cd3e8f7 | 2008-03-25 09:47:35 +0000 | [diff] [blame] | 2769 | Btree *p, /* The btree */ |
| 2770 | int iTable, /* Root page of table to open */ |
| 2771 | int wrFlag, /* 1 to write. 0 read-only */ |
| 2772 | struct KeyInfo *pKeyInfo, /* First arg to xCompare() */ |
| 2773 | BtCursor *pCur /* Write new cursor here */ |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 2774 | ){ |
| 2775 | int rc; |
| 2776 | sqlite3BtreeEnter(p); |
drh | e5fe690 | 2007-12-07 18:55:28 +0000 | [diff] [blame] | 2777 | p->pBt->db = p->db; |
danielk1977 | cd3e8f7 | 2008-03-25 09:47:35 +0000 | [diff] [blame] | 2778 | rc = btreeCursor(p, iTable, wrFlag, pKeyInfo, pCur); |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 2779 | sqlite3BtreeLeave(p); |
| 2780 | return rc; |
| 2781 | } |
danielk1977 | cd3e8f7 | 2008-03-25 09:47:35 +0000 | [diff] [blame] | 2782 | int sqlite3BtreeCursorSize(){ |
| 2783 | return sizeof(BtCursor); |
| 2784 | } |
| 2785 | |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 2786 | |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 2787 | |
| 2788 | /* |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2789 | ** Close a cursor. The read lock on the database file is released |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 2790 | ** when the last cursor is closed. |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 2791 | */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2792 | int sqlite3BtreeCloseCursor(BtCursor *pCur){ |
drh | ff0587c | 2007-08-29 17:43:19 +0000 | [diff] [blame] | 2793 | Btree *pBtree = pCur->pBtree; |
danielk1977 | cd3e8f7 | 2008-03-25 09:47:35 +0000 | [diff] [blame] | 2794 | if( pBtree ){ |
| 2795 | BtShared *pBt = pCur->pBt; |
| 2796 | sqlite3BtreeEnter(pBtree); |
| 2797 | pBt->db = pBtree->db; |
| 2798 | clearCursorPosition(pCur); |
| 2799 | if( pCur->pPrev ){ |
| 2800 | pCur->pPrev->pNext = pCur->pNext; |
| 2801 | }else{ |
| 2802 | pBt->pCursor = pCur->pNext; |
| 2803 | } |
| 2804 | if( pCur->pNext ){ |
| 2805 | pCur->pNext->pPrev = pCur->pPrev; |
| 2806 | } |
| 2807 | releasePage(pCur->pPage); |
| 2808 | unlockBtreeIfUnused(pBt); |
| 2809 | invalidateOverflowCache(pCur); |
| 2810 | /* sqlite3_free(pCur); */ |
| 2811 | sqlite3BtreeLeave(pBtree); |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 2812 | } |
drh | 8c42ca9 | 2001-06-22 19:15:00 +0000 | [diff] [blame] | 2813 | return SQLITE_OK; |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 2814 | } |
| 2815 | |
drh | 7e3b0a0 | 2001-04-28 16:52:40 +0000 | [diff] [blame] | 2816 | /* |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 2817 | ** Make a temporary cursor by filling in the fields of pTempCur. |
| 2818 | ** The temporary cursor is not on the cursor list for the Btree. |
| 2819 | */ |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 2820 | void sqlite3BtreeGetTempCursor(BtCursor *pCur, BtCursor *pTempCur){ |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 2821 | assert( cursorHoldsMutex(pCur) ); |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 2822 | memcpy(pTempCur, pCur, sizeof(*pCur)); |
| 2823 | pTempCur->pNext = 0; |
| 2824 | pTempCur->pPrev = 0; |
drh | ecdc753 | 2001-09-23 02:35:53 +0000 | [diff] [blame] | 2825 | if( pTempCur->pPage ){ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 2826 | sqlite3PagerRef(pTempCur->pPage->pDbPage); |
drh | ecdc753 | 2001-09-23 02:35:53 +0000 | [diff] [blame] | 2827 | } |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 2828 | } |
| 2829 | |
| 2830 | /* |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 2831 | ** Delete a temporary cursor such as was made by the CreateTemporaryCursor() |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 2832 | ** function above. |
| 2833 | */ |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 2834 | void sqlite3BtreeReleaseTempCursor(BtCursor *pCur){ |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 2835 | assert( cursorHoldsMutex(pCur) ); |
drh | ecdc753 | 2001-09-23 02:35:53 +0000 | [diff] [blame] | 2836 | if( pCur->pPage ){ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 2837 | sqlite3PagerUnref(pCur->pPage->pDbPage); |
drh | ecdc753 | 2001-09-23 02:35:53 +0000 | [diff] [blame] | 2838 | } |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 2839 | } |
| 2840 | |
| 2841 | /* |
drh | 8605761 | 2007-06-26 01:04:48 +0000 | [diff] [blame] | 2842 | ** Make sure the BtCursor* given in the argument has a valid |
| 2843 | ** BtCursor.info structure. If it is not already valid, call |
danielk1977 | 1cc5ed8 | 2007-05-16 17:28:43 +0000 | [diff] [blame] | 2844 | ** sqlite3BtreeParseCell() to fill it in. |
drh | ab01f61 | 2004-05-22 02:55:23 +0000 | [diff] [blame] | 2845 | ** |
| 2846 | ** BtCursor.info is a cache of the information in the current cell. |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 2847 | ** Using this cache reduces the number of calls to sqlite3BtreeParseCell(). |
drh | 8605761 | 2007-06-26 01:04:48 +0000 | [diff] [blame] | 2848 | ** |
| 2849 | ** 2007-06-25: There is a bug in some versions of MSVC that cause the |
| 2850 | ** compiler to crash when getCellInfo() is implemented as a macro. |
| 2851 | ** But there is a measureable speed advantage to using the macro on gcc |
| 2852 | ** (when less compiler optimizations like -Os or -O0 are used and the |
| 2853 | ** compiler is not doing agressive inlining.) So we use a real function |
| 2854 | ** for MSVC and a macro for everything else. Ticket #2457. |
drh | 9188b38 | 2004-05-14 21:12:22 +0000 | [diff] [blame] | 2855 | */ |
drh | 9188b38 | 2004-05-14 21:12:22 +0000 | [diff] [blame] | 2856 | #ifndef NDEBUG |
danielk1977 | 1cc5ed8 | 2007-05-16 17:28:43 +0000 | [diff] [blame] | 2857 | static void assertCellInfo(BtCursor *pCur){ |
drh | 9188b38 | 2004-05-14 21:12:22 +0000 | [diff] [blame] | 2858 | CellInfo info; |
drh | 51c6d96 | 2004-06-06 00:42:25 +0000 | [diff] [blame] | 2859 | memset(&info, 0, sizeof(info)); |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 2860 | sqlite3BtreeParseCell(pCur->pPage, pCur->idx, &info); |
drh | 9188b38 | 2004-05-14 21:12:22 +0000 | [diff] [blame] | 2861 | assert( memcmp(&info, &pCur->info, sizeof(info))==0 ); |
drh | 9188b38 | 2004-05-14 21:12:22 +0000 | [diff] [blame] | 2862 | } |
danielk1977 | 1cc5ed8 | 2007-05-16 17:28:43 +0000 | [diff] [blame] | 2863 | #else |
| 2864 | #define assertCellInfo(x) |
| 2865 | #endif |
drh | 8605761 | 2007-06-26 01:04:48 +0000 | [diff] [blame] | 2866 | #ifdef _MSC_VER |
| 2867 | /* Use a real function in MSVC to work around bugs in that compiler. */ |
| 2868 | static void getCellInfo(BtCursor *pCur){ |
| 2869 | if( pCur->info.nSize==0 ){ |
| 2870 | sqlite3BtreeParseCell(pCur->pPage, pCur->idx, &pCur->info); |
drh | a2c20e4 | 2008-03-29 16:01:04 +0000 | [diff] [blame] | 2871 | pCur->validNKey = 1; |
drh | 8605761 | 2007-06-26 01:04:48 +0000 | [diff] [blame] | 2872 | }else{ |
| 2873 | assertCellInfo(pCur); |
| 2874 | } |
| 2875 | } |
| 2876 | #else /* if not _MSC_VER */ |
| 2877 | /* Use a macro in all other compilers so that the function is inlined */ |
| 2878 | #define getCellInfo(pCur) \ |
| 2879 | if( pCur->info.nSize==0 ){ \ |
danielk1977 | 1cc5ed8 | 2007-05-16 17:28:43 +0000 | [diff] [blame] | 2880 | sqlite3BtreeParseCell(pCur->pPage, pCur->idx, &pCur->info); \ |
drh | a2c20e4 | 2008-03-29 16:01:04 +0000 | [diff] [blame] | 2881 | pCur->validNKey = 1; \ |
drh | 8605761 | 2007-06-26 01:04:48 +0000 | [diff] [blame] | 2882 | }else{ \ |
| 2883 | assertCellInfo(pCur); \ |
| 2884 | } |
| 2885 | #endif /* _MSC_VER */ |
drh | 9188b38 | 2004-05-14 21:12:22 +0000 | [diff] [blame] | 2886 | |
| 2887 | /* |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2888 | ** Set *pSize to the size of the buffer needed to hold the value of |
| 2889 | ** the key for the current entry. If the cursor is not pointing |
| 2890 | ** to a valid entry, *pSize is set to 0. |
| 2891 | ** |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 2892 | ** For a table with the INTKEY flag set, this routine returns the key |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2893 | ** itself, not the number of bytes in the key. |
drh | 7e3b0a0 | 2001-04-28 16:52:40 +0000 | [diff] [blame] | 2894 | */ |
drh | 4a1c380 | 2004-05-12 15:15:47 +0000 | [diff] [blame] | 2895 | int sqlite3BtreeKeySize(BtCursor *pCur, i64 *pSize){ |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 2896 | int rc; |
| 2897 | |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 2898 | assert( cursorHoldsMutex(pCur) ); |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 2899 | rc = restoreOrClearCursorPosition(pCur); |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 2900 | if( rc==SQLITE_OK ){ |
| 2901 | assert( pCur->eState==CURSOR_INVALID || pCur->eState==CURSOR_VALID ); |
| 2902 | if( pCur->eState==CURSOR_INVALID ){ |
| 2903 | *pSize = 0; |
| 2904 | }else{ |
drh | 8605761 | 2007-06-26 01:04:48 +0000 | [diff] [blame] | 2905 | getCellInfo(pCur); |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 2906 | *pSize = pCur->info.nKey; |
| 2907 | } |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 2908 | } |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 2909 | return rc; |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 2910 | } |
drh | 2af926b | 2001-05-15 00:39:25 +0000 | [diff] [blame] | 2911 | |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 2912 | /* |
drh | 0e1c19e | 2004-05-11 00:58:56 +0000 | [diff] [blame] | 2913 | ** Set *pSize to the number of bytes of data in the entry the |
| 2914 | ** cursor currently points to. Always return SQLITE_OK. |
| 2915 | ** Failure is not possible. If the cursor is not currently |
| 2916 | ** pointing to an entry (which can happen, for example, if |
| 2917 | ** the database is empty) then *pSize is set to 0. |
| 2918 | */ |
| 2919 | int sqlite3BtreeDataSize(BtCursor *pCur, u32 *pSize){ |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 2920 | int rc; |
| 2921 | |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 2922 | assert( cursorHoldsMutex(pCur) ); |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 2923 | rc = restoreOrClearCursorPosition(pCur); |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 2924 | if( rc==SQLITE_OK ){ |
| 2925 | assert( pCur->eState==CURSOR_INVALID || pCur->eState==CURSOR_VALID ); |
| 2926 | if( pCur->eState==CURSOR_INVALID ){ |
| 2927 | /* Not pointing at a valid entry - set *pSize to 0. */ |
| 2928 | *pSize = 0; |
| 2929 | }else{ |
drh | 8605761 | 2007-06-26 01:04:48 +0000 | [diff] [blame] | 2930 | getCellInfo(pCur); |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 2931 | *pSize = pCur->info.nData; |
| 2932 | } |
drh | 0e1c19e | 2004-05-11 00:58:56 +0000 | [diff] [blame] | 2933 | } |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 2934 | return rc; |
drh | 0e1c19e | 2004-05-11 00:58:56 +0000 | [diff] [blame] | 2935 | } |
| 2936 | |
| 2937 | /* |
danielk1977 | d0441796 | 2007-05-02 13:16:30 +0000 | [diff] [blame] | 2938 | ** Given the page number of an overflow page in the database (parameter |
| 2939 | ** ovfl), this function finds the page number of the next page in the |
| 2940 | ** linked list of overflow pages. If possible, it uses the auto-vacuum |
| 2941 | ** pointer-map data instead of reading the content of page ovfl to do so. |
| 2942 | ** |
| 2943 | ** If an error occurs an SQLite error code is returned. Otherwise: |
| 2944 | ** |
| 2945 | ** Unless pPgnoNext is NULL, the page number of the next overflow |
| 2946 | ** page in the linked list is written to *pPgnoNext. If page ovfl |
drh | 85b623f | 2007-12-13 21:54:09 +0000 | [diff] [blame] | 2947 | ** is the last page in its linked list, *pPgnoNext is set to zero. |
danielk1977 | d0441796 | 2007-05-02 13:16:30 +0000 | [diff] [blame] | 2948 | ** |
| 2949 | ** If ppPage is not NULL, *ppPage is set to the MemPage* handle |
| 2950 | ** for page ovfl. The underlying pager page may have been requested |
| 2951 | ** with the noContent flag set, so the page data accessable via |
| 2952 | ** this handle may not be trusted. |
| 2953 | */ |
| 2954 | static int getOverflowPage( |
| 2955 | BtShared *pBt, |
| 2956 | Pgno ovfl, /* Overflow page */ |
| 2957 | MemPage **ppPage, /* OUT: MemPage handle */ |
| 2958 | Pgno *pPgnoNext /* OUT: Next overflow page number */ |
| 2959 | ){ |
| 2960 | Pgno next = 0; |
| 2961 | int rc; |
| 2962 | |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 2963 | assert( sqlite3_mutex_held(pBt->mutex) ); |
danielk1977 | d0441796 | 2007-05-02 13:16:30 +0000 | [diff] [blame] | 2964 | /* One of these must not be NULL. Otherwise, why call this function? */ |
| 2965 | assert(ppPage || pPgnoNext); |
| 2966 | |
| 2967 | /* If pPgnoNext is NULL, then this function is being called to obtain |
| 2968 | ** a MemPage* reference only. No page-data is required in this case. |
| 2969 | */ |
| 2970 | if( !pPgnoNext ){ |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 2971 | return sqlite3BtreeGetPage(pBt, ovfl, ppPage, 1); |
danielk1977 | d0441796 | 2007-05-02 13:16:30 +0000 | [diff] [blame] | 2972 | } |
| 2973 | |
| 2974 | #ifndef SQLITE_OMIT_AUTOVACUUM |
| 2975 | /* Try to find the next page in the overflow list using the |
| 2976 | ** autovacuum pointer-map pages. Guess that the next page in |
| 2977 | ** the overflow list is page number (ovfl+1). If that guess turns |
| 2978 | ** out to be wrong, fall back to loading the data of page |
| 2979 | ** number ovfl to determine the next page number. |
| 2980 | */ |
| 2981 | if( pBt->autoVacuum ){ |
| 2982 | Pgno pgno; |
| 2983 | Pgno iGuess = ovfl+1; |
| 2984 | u8 eType; |
| 2985 | |
| 2986 | while( PTRMAP_ISPAGE(pBt, iGuess) || iGuess==PENDING_BYTE_PAGE(pBt) ){ |
| 2987 | iGuess++; |
| 2988 | } |
| 2989 | |
danielk1977 | ad0132d | 2008-06-07 08:58:22 +0000 | [diff] [blame^] | 2990 | if( iGuess<=pagerPagecount(pBt->pPager) ){ |
danielk1977 | d0441796 | 2007-05-02 13:16:30 +0000 | [diff] [blame] | 2991 | rc = ptrmapGet(pBt, iGuess, &eType, &pgno); |
| 2992 | if( rc!=SQLITE_OK ){ |
| 2993 | return rc; |
| 2994 | } |
| 2995 | if( eType==PTRMAP_OVERFLOW2 && pgno==ovfl ){ |
| 2996 | next = iGuess; |
| 2997 | } |
| 2998 | } |
| 2999 | } |
| 3000 | #endif |
| 3001 | |
| 3002 | if( next==0 || ppPage ){ |
| 3003 | MemPage *pPage = 0; |
| 3004 | |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 3005 | rc = sqlite3BtreeGetPage(pBt, ovfl, &pPage, next!=0); |
danielk1977 | d0441796 | 2007-05-02 13:16:30 +0000 | [diff] [blame] | 3006 | assert(rc==SQLITE_OK || pPage==0); |
| 3007 | if( next==0 && rc==SQLITE_OK ){ |
| 3008 | next = get4byte(pPage->aData); |
| 3009 | } |
| 3010 | |
| 3011 | if( ppPage ){ |
| 3012 | *ppPage = pPage; |
| 3013 | }else{ |
| 3014 | releasePage(pPage); |
| 3015 | } |
| 3016 | } |
| 3017 | *pPgnoNext = next; |
| 3018 | |
| 3019 | return rc; |
| 3020 | } |
| 3021 | |
danielk1977 | da10719 | 2007-05-04 08:32:13 +0000 | [diff] [blame] | 3022 | /* |
| 3023 | ** Copy data from a buffer to a page, or from a page to a buffer. |
| 3024 | ** |
| 3025 | ** pPayload is a pointer to data stored on database page pDbPage. |
| 3026 | ** If argument eOp is false, then nByte bytes of data are copied |
| 3027 | ** from pPayload to the buffer pointed at by pBuf. If eOp is true, |
| 3028 | ** then sqlite3PagerWrite() is called on pDbPage and nByte bytes |
| 3029 | ** of data are copied from the buffer pBuf to pPayload. |
| 3030 | ** |
| 3031 | ** SQLITE_OK is returned on success, otherwise an error code. |
| 3032 | */ |
| 3033 | static int copyPayload( |
| 3034 | void *pPayload, /* Pointer to page data */ |
| 3035 | void *pBuf, /* Pointer to buffer */ |
| 3036 | int nByte, /* Number of bytes to copy */ |
| 3037 | int eOp, /* 0 -> copy from page, 1 -> copy to page */ |
| 3038 | DbPage *pDbPage /* Page containing pPayload */ |
| 3039 | ){ |
| 3040 | if( eOp ){ |
| 3041 | /* Copy data from buffer to page (a write operation) */ |
| 3042 | int rc = sqlite3PagerWrite(pDbPage); |
| 3043 | if( rc!=SQLITE_OK ){ |
| 3044 | return rc; |
| 3045 | } |
| 3046 | memcpy(pPayload, pBuf, nByte); |
| 3047 | }else{ |
| 3048 | /* Copy data from page to buffer (a read operation) */ |
| 3049 | memcpy(pBuf, pPayload, nByte); |
| 3050 | } |
| 3051 | return SQLITE_OK; |
| 3052 | } |
danielk1977 | d0441796 | 2007-05-02 13:16:30 +0000 | [diff] [blame] | 3053 | |
| 3054 | /* |
danielk1977 | 9f8d640 | 2007-05-02 17:48:45 +0000 | [diff] [blame] | 3055 | ** This function is used to read or overwrite payload information |
| 3056 | ** for the entry that the pCur cursor is pointing to. If the eOp |
| 3057 | ** parameter is 0, this is a read operation (data copied into |
| 3058 | ** buffer pBuf). If it is non-zero, a write (data copied from |
| 3059 | ** buffer pBuf). |
| 3060 | ** |
| 3061 | ** A total of "amt" bytes are read or written beginning at "offset". |
| 3062 | ** Data is read to or from the buffer pBuf. |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 3063 | ** |
| 3064 | ** This routine does not make a distinction between key and data. |
danielk1977 | 9f8d640 | 2007-05-02 17:48:45 +0000 | [diff] [blame] | 3065 | ** It just reads or writes bytes from the payload area. Data might |
| 3066 | ** appear on the main page or be scattered out on multiple overflow |
| 3067 | ** pages. |
danielk1977 | da10719 | 2007-05-04 08:32:13 +0000 | [diff] [blame] | 3068 | ** |
danielk1977 | dcbb5d3 | 2007-05-04 18:36:44 +0000 | [diff] [blame] | 3069 | ** If the BtCursor.isIncrblobHandle flag is set, and the current |
danielk1977 | da10719 | 2007-05-04 08:32:13 +0000 | [diff] [blame] | 3070 | ** cursor entry uses one or more overflow pages, this function |
| 3071 | ** allocates space for and lazily popluates the overflow page-list |
| 3072 | ** cache array (BtCursor.aOverflow). Subsequent calls use this |
| 3073 | ** cache to make seeking to the supplied offset more efficient. |
| 3074 | ** |
| 3075 | ** Once an overflow page-list cache has been allocated, it may be |
| 3076 | ** invalidated if some other cursor writes to the same table, or if |
| 3077 | ** the cursor is moved to a different row. Additionally, in auto-vacuum |
| 3078 | ** mode, the following events may invalidate an overflow page-list cache. |
| 3079 | ** |
| 3080 | ** * An incremental vacuum, |
| 3081 | ** * A commit in auto_vacuum="full" mode, |
| 3082 | ** * Creating a table (may require moving an overflow page). |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 3083 | */ |
danielk1977 | 9f8d640 | 2007-05-02 17:48:45 +0000 | [diff] [blame] | 3084 | static int accessPayload( |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3085 | BtCursor *pCur, /* Cursor pointing to entry to read from */ |
| 3086 | int offset, /* Begin reading this far into payload */ |
| 3087 | int amt, /* Read this many bytes */ |
| 3088 | unsigned char *pBuf, /* Write the bytes into this buffer */ |
danielk1977 | 9f8d640 | 2007-05-02 17:48:45 +0000 | [diff] [blame] | 3089 | int skipKey, /* offset begins at data if this is true */ |
| 3090 | int eOp /* zero to read. non-zero to write. */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3091 | ){ |
| 3092 | unsigned char *aPayload; |
danielk1977 | da10719 | 2007-05-04 08:32:13 +0000 | [diff] [blame] | 3093 | int rc = SQLITE_OK; |
drh | fa1a98a | 2004-05-14 19:08:17 +0000 | [diff] [blame] | 3094 | u32 nKey; |
danielk1977 | 2dec970 | 2007-05-02 16:48:37 +0000 | [diff] [blame] | 3095 | int iIdx = 0; |
drh | d0679ed | 2007-08-28 22:24:34 +0000 | [diff] [blame] | 3096 | MemPage *pPage = pCur->pPage; /* Btree page of current cursor entry */ |
drh | 51f015e | 2007-10-16 19:45:29 +0000 | [diff] [blame] | 3097 | BtShared *pBt; /* Btree this cursor belongs to */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3098 | |
danielk1977 | da10719 | 2007-05-04 08:32:13 +0000 | [diff] [blame] | 3099 | assert( pPage ); |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 3100 | assert( pCur->eState==CURSOR_VALID ); |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3101 | assert( pCur->idx>=0 && pCur->idx<pPage->nCell ); |
danielk1977 | da10719 | 2007-05-04 08:32:13 +0000 | [diff] [blame] | 3102 | assert( offset>=0 ); |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 3103 | assert( cursorHoldsMutex(pCur) ); |
danielk1977 | da10719 | 2007-05-04 08:32:13 +0000 | [diff] [blame] | 3104 | |
drh | 8605761 | 2007-06-26 01:04:48 +0000 | [diff] [blame] | 3105 | getCellInfo(pCur); |
drh | 366fda6 | 2006-01-13 02:35:09 +0000 | [diff] [blame] | 3106 | aPayload = pCur->info.pCell + pCur->info.nHeader; |
danielk1977 | da10719 | 2007-05-04 08:32:13 +0000 | [diff] [blame] | 3107 | nKey = (pPage->intKey ? 0 : pCur->info.nKey); |
| 3108 | |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3109 | if( skipKey ){ |
drh | fa1a98a | 2004-05-14 19:08:17 +0000 | [diff] [blame] | 3110 | offset += nKey; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3111 | } |
drh | fa1a98a | 2004-05-14 19:08:17 +0000 | [diff] [blame] | 3112 | if( offset+amt > nKey+pCur->info.nData ){ |
danielk1977 | da10719 | 2007-05-04 08:32:13 +0000 | [diff] [blame] | 3113 | /* 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] | 3114 | return SQLITE_ERROR; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3115 | } |
danielk1977 | da10719 | 2007-05-04 08:32:13 +0000 | [diff] [blame] | 3116 | |
| 3117 | /* Check if data must be read/written to/from the btree page itself. */ |
drh | fa1a98a | 2004-05-14 19:08:17 +0000 | [diff] [blame] | 3118 | if( offset<pCur->info.nLocal ){ |
drh | 2af926b | 2001-05-15 00:39:25 +0000 | [diff] [blame] | 3119 | int a = amt; |
drh | fa1a98a | 2004-05-14 19:08:17 +0000 | [diff] [blame] | 3120 | if( a+offset>pCur->info.nLocal ){ |
| 3121 | a = pCur->info.nLocal - offset; |
drh | 2af926b | 2001-05-15 00:39:25 +0000 | [diff] [blame] | 3122 | } |
danielk1977 | da10719 | 2007-05-04 08:32:13 +0000 | [diff] [blame] | 3123 | rc = copyPayload(&aPayload[offset], pBuf, a, eOp, pPage->pDbPage); |
drh | 2aa679f | 2001-06-25 02:11:07 +0000 | [diff] [blame] | 3124 | offset = 0; |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 3125 | pBuf += a; |
drh | 2af926b | 2001-05-15 00:39:25 +0000 | [diff] [blame] | 3126 | amt -= a; |
drh | dd79342 | 2001-06-28 01:54:48 +0000 | [diff] [blame] | 3127 | }else{ |
drh | fa1a98a | 2004-05-14 19:08:17 +0000 | [diff] [blame] | 3128 | offset -= pCur->info.nLocal; |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 3129 | } |
danielk1977 | da10719 | 2007-05-04 08:32:13 +0000 | [diff] [blame] | 3130 | |
drh | 51f015e | 2007-10-16 19:45:29 +0000 | [diff] [blame] | 3131 | pBt = pCur->pBt; |
danielk1977 | da10719 | 2007-05-04 08:32:13 +0000 | [diff] [blame] | 3132 | if( rc==SQLITE_OK && amt>0 ){ |
| 3133 | const int ovflSize = pBt->usableSize - 4; /* Bytes content per ovfl page */ |
| 3134 | Pgno nextPage; |
| 3135 | |
drh | fa1a98a | 2004-05-14 19:08:17 +0000 | [diff] [blame] | 3136 | nextPage = get4byte(&aPayload[pCur->info.nLocal]); |
danielk1977 | da10719 | 2007-05-04 08:32:13 +0000 | [diff] [blame] | 3137 | |
danielk1977 | 2dec970 | 2007-05-02 16:48:37 +0000 | [diff] [blame] | 3138 | #ifndef SQLITE_OMIT_INCRBLOB |
danielk1977 | dcbb5d3 | 2007-05-04 18:36:44 +0000 | [diff] [blame] | 3139 | /* If the isIncrblobHandle flag is set and the BtCursor.aOverflow[] |
danielk1977 | da10719 | 2007-05-04 08:32:13 +0000 | [diff] [blame] | 3140 | ** has not been allocated, allocate it now. The array is sized at |
| 3141 | ** one entry for each overflow page in the overflow chain. The |
| 3142 | ** page number of the first overflow page is stored in aOverflow[0], |
| 3143 | ** etc. A value of 0 in the aOverflow[] array means "not yet known" |
| 3144 | ** (the cache is lazily populated). |
| 3145 | */ |
danielk1977 | dcbb5d3 | 2007-05-04 18:36:44 +0000 | [diff] [blame] | 3146 | if( pCur->isIncrblobHandle && !pCur->aOverflow ){ |
danielk1977 | 2dec970 | 2007-05-02 16:48:37 +0000 | [diff] [blame] | 3147 | int nOvfl = (pCur->info.nPayload-pCur->info.nLocal+ovflSize-1)/ovflSize; |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 3148 | pCur->aOverflow = (Pgno *)sqlite3MallocZero(sizeof(Pgno)*nOvfl); |
danielk1977 | 2dec970 | 2007-05-02 16:48:37 +0000 | [diff] [blame] | 3149 | if( nOvfl && !pCur->aOverflow ){ |
danielk1977 | da10719 | 2007-05-04 08:32:13 +0000 | [diff] [blame] | 3150 | rc = SQLITE_NOMEM; |
danielk1977 | 2dec970 | 2007-05-02 16:48:37 +0000 | [diff] [blame] | 3151 | } |
| 3152 | } |
danielk1977 | da10719 | 2007-05-04 08:32:13 +0000 | [diff] [blame] | 3153 | |
| 3154 | /* If the overflow page-list cache has been allocated and the |
| 3155 | ** entry for the first required overflow page is valid, skip |
| 3156 | ** directly to it. |
| 3157 | */ |
danielk1977 | 2dec970 | 2007-05-02 16:48:37 +0000 | [diff] [blame] | 3158 | if( pCur->aOverflow && pCur->aOverflow[offset/ovflSize] ){ |
| 3159 | iIdx = (offset/ovflSize); |
| 3160 | nextPage = pCur->aOverflow[iIdx]; |
| 3161 | offset = (offset%ovflSize); |
| 3162 | } |
| 3163 | #endif |
danielk1977 | da10719 | 2007-05-04 08:32:13 +0000 | [diff] [blame] | 3164 | |
| 3165 | for( ; rc==SQLITE_OK && amt>0 && nextPage; iIdx++){ |
| 3166 | |
| 3167 | #ifndef SQLITE_OMIT_INCRBLOB |
| 3168 | /* If required, populate the overflow page-list cache. */ |
| 3169 | if( pCur->aOverflow ){ |
| 3170 | assert(!pCur->aOverflow[iIdx] || pCur->aOverflow[iIdx]==nextPage); |
| 3171 | pCur->aOverflow[iIdx] = nextPage; |
| 3172 | } |
| 3173 | #endif |
| 3174 | |
danielk1977 | d0441796 | 2007-05-02 13:16:30 +0000 | [diff] [blame] | 3175 | if( offset>=ovflSize ){ |
| 3176 | /* The only reason to read this page is to obtain the page |
danielk1977 | da10719 | 2007-05-04 08:32:13 +0000 | [diff] [blame] | 3177 | ** number for the next page in the overflow chain. The page |
drh | fd131da | 2007-08-07 17:13:03 +0000 | [diff] [blame] | 3178 | ** data is not required. So first try to lookup the overflow |
| 3179 | ** page-list cache, if any, then fall back to the getOverflowPage() |
danielk1977 | da10719 | 2007-05-04 08:32:13 +0000 | [diff] [blame] | 3180 | ** function. |
danielk1977 | d0441796 | 2007-05-02 13:16:30 +0000 | [diff] [blame] | 3181 | */ |
danielk1977 | 2dec970 | 2007-05-02 16:48:37 +0000 | [diff] [blame] | 3182 | #ifndef SQLITE_OMIT_INCRBLOB |
danielk1977 | da10719 | 2007-05-04 08:32:13 +0000 | [diff] [blame] | 3183 | if( pCur->aOverflow && pCur->aOverflow[iIdx+1] ){ |
| 3184 | nextPage = pCur->aOverflow[iIdx+1]; |
| 3185 | } else |
danielk1977 | 2dec970 | 2007-05-02 16:48:37 +0000 | [diff] [blame] | 3186 | #endif |
danielk1977 | da10719 | 2007-05-04 08:32:13 +0000 | [diff] [blame] | 3187 | rc = getOverflowPage(pBt, nextPage, 0, &nextPage); |
danielk1977 | da10719 | 2007-05-04 08:32:13 +0000 | [diff] [blame] | 3188 | offset -= ovflSize; |
danielk1977 | d0441796 | 2007-05-02 13:16:30 +0000 | [diff] [blame] | 3189 | }else{ |
danielk1977 | 9f8d640 | 2007-05-02 17:48:45 +0000 | [diff] [blame] | 3190 | /* Need to read this page properly. It contains some of the |
| 3191 | ** range of data that is being read (eOp==0) or written (eOp!=0). |
danielk1977 | d0441796 | 2007-05-02 13:16:30 +0000 | [diff] [blame] | 3192 | */ |
| 3193 | DbPage *pDbPage; |
danielk1977 | cfe9a69 | 2004-06-16 12:00:29 +0000 | [diff] [blame] | 3194 | int a = amt; |
danielk1977 | d0441796 | 2007-05-02 13:16:30 +0000 | [diff] [blame] | 3195 | rc = sqlite3PagerGet(pBt->pPager, nextPage, &pDbPage); |
danielk1977 | da10719 | 2007-05-04 08:32:13 +0000 | [diff] [blame] | 3196 | if( rc==SQLITE_OK ){ |
| 3197 | aPayload = sqlite3PagerGetData(pDbPage); |
| 3198 | nextPage = get4byte(aPayload); |
| 3199 | if( a + offset > ovflSize ){ |
| 3200 | a = ovflSize - offset; |
danielk1977 | 9f8d640 | 2007-05-02 17:48:45 +0000 | [diff] [blame] | 3201 | } |
danielk1977 | da10719 | 2007-05-04 08:32:13 +0000 | [diff] [blame] | 3202 | rc = copyPayload(&aPayload[offset+4], pBuf, a, eOp, pDbPage); |
| 3203 | sqlite3PagerUnref(pDbPage); |
| 3204 | offset = 0; |
| 3205 | amt -= a; |
| 3206 | pBuf += a; |
danielk1977 | 9f8d640 | 2007-05-02 17:48:45 +0000 | [diff] [blame] | 3207 | } |
danielk1977 | cfe9a69 | 2004-06-16 12:00:29 +0000 | [diff] [blame] | 3208 | } |
drh | 2af926b | 2001-05-15 00:39:25 +0000 | [diff] [blame] | 3209 | } |
drh | 2af926b | 2001-05-15 00:39:25 +0000 | [diff] [blame] | 3210 | } |
danielk1977 | cfe9a69 | 2004-06-16 12:00:29 +0000 | [diff] [blame] | 3211 | |
danielk1977 | da10719 | 2007-05-04 08:32:13 +0000 | [diff] [blame] | 3212 | if( rc==SQLITE_OK && amt>0 ){ |
drh | 4928570 | 2005-09-17 15:20:26 +0000 | [diff] [blame] | 3213 | return SQLITE_CORRUPT_BKPT; |
drh | a7fcb05 | 2001-12-14 15:09:55 +0000 | [diff] [blame] | 3214 | } |
danielk1977 | da10719 | 2007-05-04 08:32:13 +0000 | [diff] [blame] | 3215 | return rc; |
drh | 2af926b | 2001-05-15 00:39:25 +0000 | [diff] [blame] | 3216 | } |
| 3217 | |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 3218 | /* |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3219 | ** Read part of the key associated with cursor pCur. Exactly |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 3220 | ** "amt" bytes will be transfered into pBuf[]. The transfer |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3221 | ** begins at "offset". |
drh | 8c1238a | 2003-01-02 14:43:55 +0000 | [diff] [blame] | 3222 | ** |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3223 | ** Return SQLITE_OK on success or an error code if anything goes |
| 3224 | ** wrong. An error is returned if "offset+amt" is larger than |
| 3225 | ** the available payload. |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 3226 | */ |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 3227 | int sqlite3BtreeKey(BtCursor *pCur, u32 offset, u32 amt, void *pBuf){ |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 3228 | int rc; |
| 3229 | |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 3230 | assert( cursorHoldsMutex(pCur) ); |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 3231 | rc = restoreOrClearCursorPosition(pCur); |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 3232 | if( rc==SQLITE_OK ){ |
| 3233 | assert( pCur->eState==CURSOR_VALID ); |
| 3234 | assert( pCur->pPage!=0 ); |
| 3235 | if( pCur->pPage->intKey ){ |
| 3236 | return SQLITE_CORRUPT_BKPT; |
| 3237 | } |
| 3238 | assert( pCur->pPage->intKey==0 ); |
| 3239 | assert( pCur->idx>=0 && pCur->idx<pCur->pPage->nCell ); |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 3240 | rc = accessPayload(pCur, offset, amt, (unsigned char*)pBuf, 0, 0); |
drh | 6575a22 | 2005-03-10 17:06:34 +0000 | [diff] [blame] | 3241 | } |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 3242 | return rc; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3243 | } |
| 3244 | |
| 3245 | /* |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3246 | ** Read part of the data associated with cursor pCur. Exactly |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 3247 | ** "amt" bytes will be transfered into pBuf[]. The transfer |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3248 | ** begins at "offset". |
| 3249 | ** |
| 3250 | ** Return SQLITE_OK on success or an error code if anything goes |
| 3251 | ** wrong. An error is returned if "offset+amt" is larger than |
| 3252 | ** the available payload. |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 3253 | */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3254 | int sqlite3BtreeData(BtCursor *pCur, u32 offset, u32 amt, void *pBuf){ |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 3255 | int rc; |
| 3256 | |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 3257 | assert( cursorHoldsMutex(pCur) ); |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 3258 | rc = restoreOrClearCursorPosition(pCur); |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 3259 | if( rc==SQLITE_OK ){ |
| 3260 | assert( pCur->eState==CURSOR_VALID ); |
| 3261 | assert( pCur->pPage!=0 ); |
| 3262 | assert( pCur->idx>=0 && pCur->idx<pCur->pPage->nCell ); |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 3263 | rc = accessPayload(pCur, offset, amt, pBuf, 1, 0); |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 3264 | } |
| 3265 | return rc; |
drh | 2af926b | 2001-05-15 00:39:25 +0000 | [diff] [blame] | 3266 | } |
| 3267 | |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 3268 | /* |
drh | 0e1c19e | 2004-05-11 00:58:56 +0000 | [diff] [blame] | 3269 | ** Return a pointer to payload information from the entry that the |
| 3270 | ** pCur cursor is pointing to. The pointer is to the beginning of |
| 3271 | ** the key if skipKey==0 and it points to the beginning of data if |
drh | e51c44f | 2004-05-30 20:46:09 +0000 | [diff] [blame] | 3272 | ** skipKey==1. The number of bytes of available key/data is written |
| 3273 | ** into *pAmt. If *pAmt==0, then the value returned will not be |
| 3274 | ** a valid pointer. |
drh | 0e1c19e | 2004-05-11 00:58:56 +0000 | [diff] [blame] | 3275 | ** |
| 3276 | ** This routine is an optimization. It is common for the entire key |
| 3277 | ** and data to fit on the local page and for there to be no overflow |
| 3278 | ** pages. When that is so, this routine can be used to access the |
| 3279 | ** key and data without making a copy. If the key and/or data spills |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 3280 | ** onto overflow pages, then accessPayload() must be used to reassembly |
drh | 0e1c19e | 2004-05-11 00:58:56 +0000 | [diff] [blame] | 3281 | ** the key/data and copy it into a preallocated buffer. |
| 3282 | ** |
| 3283 | ** The pointer returned by this routine looks directly into the cached |
| 3284 | ** page of the database. The data might change or move the next time |
| 3285 | ** any btree routine is called. |
| 3286 | */ |
| 3287 | static const unsigned char *fetchPayload( |
| 3288 | BtCursor *pCur, /* Cursor pointing to entry to read from */ |
drh | e51c44f | 2004-05-30 20:46:09 +0000 | [diff] [blame] | 3289 | int *pAmt, /* Write the number of available bytes here */ |
drh | 0e1c19e | 2004-05-11 00:58:56 +0000 | [diff] [blame] | 3290 | int skipKey /* read beginning at data if this is true */ |
| 3291 | ){ |
| 3292 | unsigned char *aPayload; |
| 3293 | MemPage *pPage; |
drh | fa1a98a | 2004-05-14 19:08:17 +0000 | [diff] [blame] | 3294 | u32 nKey; |
| 3295 | int nLocal; |
drh | 0e1c19e | 2004-05-11 00:58:56 +0000 | [diff] [blame] | 3296 | |
| 3297 | assert( pCur!=0 && pCur->pPage!=0 ); |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 3298 | assert( pCur->eState==CURSOR_VALID ); |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 3299 | assert( cursorHoldsMutex(pCur) ); |
drh | 0e1c19e | 2004-05-11 00:58:56 +0000 | [diff] [blame] | 3300 | pPage = pCur->pPage; |
drh | 0e1c19e | 2004-05-11 00:58:56 +0000 | [diff] [blame] | 3301 | assert( pCur->idx>=0 && pCur->idx<pPage->nCell ); |
drh | 8605761 | 2007-06-26 01:04:48 +0000 | [diff] [blame] | 3302 | getCellInfo(pCur); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 3303 | aPayload = pCur->info.pCell; |
drh | fa1a98a | 2004-05-14 19:08:17 +0000 | [diff] [blame] | 3304 | aPayload += pCur->info.nHeader; |
drh | 0e1c19e | 2004-05-11 00:58:56 +0000 | [diff] [blame] | 3305 | if( pPage->intKey ){ |
drh | fa1a98a | 2004-05-14 19:08:17 +0000 | [diff] [blame] | 3306 | nKey = 0; |
| 3307 | }else{ |
| 3308 | nKey = pCur->info.nKey; |
drh | 0e1c19e | 2004-05-11 00:58:56 +0000 | [diff] [blame] | 3309 | } |
drh | 0e1c19e | 2004-05-11 00:58:56 +0000 | [diff] [blame] | 3310 | if( skipKey ){ |
drh | fa1a98a | 2004-05-14 19:08:17 +0000 | [diff] [blame] | 3311 | aPayload += nKey; |
| 3312 | nLocal = pCur->info.nLocal - nKey; |
drh | 0e1c19e | 2004-05-11 00:58:56 +0000 | [diff] [blame] | 3313 | }else{ |
drh | fa1a98a | 2004-05-14 19:08:17 +0000 | [diff] [blame] | 3314 | nLocal = pCur->info.nLocal; |
drh | e51c44f | 2004-05-30 20:46:09 +0000 | [diff] [blame] | 3315 | if( nLocal>nKey ){ |
| 3316 | nLocal = nKey; |
| 3317 | } |
drh | 0e1c19e | 2004-05-11 00:58:56 +0000 | [diff] [blame] | 3318 | } |
drh | e51c44f | 2004-05-30 20:46:09 +0000 | [diff] [blame] | 3319 | *pAmt = nLocal; |
drh | 0e1c19e | 2004-05-11 00:58:56 +0000 | [diff] [blame] | 3320 | return aPayload; |
| 3321 | } |
| 3322 | |
| 3323 | |
| 3324 | /* |
drh | e51c44f | 2004-05-30 20:46:09 +0000 | [diff] [blame] | 3325 | ** For the entry that cursor pCur is point to, return as |
| 3326 | ** many bytes of the key or data as are available on the local |
| 3327 | ** b-tree page. Write the number of available bytes into *pAmt. |
drh | 0e1c19e | 2004-05-11 00:58:56 +0000 | [diff] [blame] | 3328 | ** |
| 3329 | ** The pointer returned is ephemeral. The key/data may move |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 3330 | ** or be destroyed on the next call to any Btree routine, |
| 3331 | ** including calls from other threads against the same cache. |
| 3332 | ** Hence, a mutex on the BtShared should be held prior to calling |
| 3333 | ** this routine. |
drh | 0e1c19e | 2004-05-11 00:58:56 +0000 | [diff] [blame] | 3334 | ** |
| 3335 | ** These routines is used to get quick access to key and data |
| 3336 | ** in the common case where no overflow pages are used. |
drh | 0e1c19e | 2004-05-11 00:58:56 +0000 | [diff] [blame] | 3337 | */ |
drh | e51c44f | 2004-05-30 20:46:09 +0000 | [diff] [blame] | 3338 | const void *sqlite3BtreeKeyFetch(BtCursor *pCur, int *pAmt){ |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 3339 | assert( cursorHoldsMutex(pCur) ); |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 3340 | if( pCur->eState==CURSOR_VALID ){ |
| 3341 | return (const void*)fetchPayload(pCur, pAmt, 0); |
| 3342 | } |
| 3343 | return 0; |
drh | 0e1c19e | 2004-05-11 00:58:56 +0000 | [diff] [blame] | 3344 | } |
drh | e51c44f | 2004-05-30 20:46:09 +0000 | [diff] [blame] | 3345 | const void *sqlite3BtreeDataFetch(BtCursor *pCur, int *pAmt){ |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 3346 | assert( cursorHoldsMutex(pCur) ); |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 3347 | if( pCur->eState==CURSOR_VALID ){ |
| 3348 | return (const void*)fetchPayload(pCur, pAmt, 1); |
| 3349 | } |
| 3350 | return 0; |
drh | 0e1c19e | 2004-05-11 00:58:56 +0000 | [diff] [blame] | 3351 | } |
| 3352 | |
| 3353 | |
| 3354 | /* |
drh | 8178a75 | 2003-01-05 21:41:40 +0000 | [diff] [blame] | 3355 | ** 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] | 3356 | ** page number of the child page to move to. |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 3357 | */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3358 | static int moveToChild(BtCursor *pCur, u32 newPgno){ |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 3359 | int rc; |
| 3360 | MemPage *pNewPage; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3361 | MemPage *pOldPage; |
drh | d0679ed | 2007-08-28 22:24:34 +0000 | [diff] [blame] | 3362 | BtShared *pBt = pCur->pBt; |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 3363 | |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 3364 | assert( cursorHoldsMutex(pCur) ); |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 3365 | assert( pCur->eState==CURSOR_VALID ); |
drh | de64713 | 2004-05-07 17:57:49 +0000 | [diff] [blame] | 3366 | rc = getAndInitPage(pBt, newPgno, &pNewPage, pCur->pPage); |
drh | 6019e16 | 2001-07-02 17:51:45 +0000 | [diff] [blame] | 3367 | if( rc ) return rc; |
drh | 428ae8c | 2003-01-04 16:48:09 +0000 | [diff] [blame] | 3368 | pNewPage->idxParent = pCur->idx; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3369 | pOldPage = pCur->pPage; |
| 3370 | pOldPage->idxShift = 0; |
| 3371 | releasePage(pOldPage); |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 3372 | pCur->pPage = pNewPage; |
| 3373 | pCur->idx = 0; |
drh | 271efa5 | 2004-05-30 19:19:05 +0000 | [diff] [blame] | 3374 | pCur->info.nSize = 0; |
drh | a2c20e4 | 2008-03-29 16:01:04 +0000 | [diff] [blame] | 3375 | pCur->validNKey = 0; |
drh | 4be295b | 2003-12-16 03:44:47 +0000 | [diff] [blame] | 3376 | if( pNewPage->nCell<1 ){ |
drh | 4928570 | 2005-09-17 15:20:26 +0000 | [diff] [blame] | 3377 | return SQLITE_CORRUPT_BKPT; |
drh | 4be295b | 2003-12-16 03:44:47 +0000 | [diff] [blame] | 3378 | } |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 3379 | return SQLITE_OK; |
| 3380 | } |
| 3381 | |
| 3382 | /* |
drh | 8856d6a | 2004-04-29 14:42:46 +0000 | [diff] [blame] | 3383 | ** Return true if the page is the virtual root of its table. |
| 3384 | ** |
| 3385 | ** The virtual root page is the root page for most tables. But |
| 3386 | ** for the table rooted on page 1, sometime the real root page |
| 3387 | ** is empty except for the right-pointer. In such cases the |
| 3388 | ** virtual root page is the page that the right-pointer of page |
| 3389 | ** 1 is pointing to. |
| 3390 | */ |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 3391 | int sqlite3BtreeIsRootPage(MemPage *pPage){ |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 3392 | MemPage *pParent; |
| 3393 | |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 3394 | assert( sqlite3_mutex_held(pPage->pBt->mutex) ); |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 3395 | pParent = pPage->pParent; |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 3396 | if( pParent==0 ) return 1; |
| 3397 | if( pParent->pgno>1 ) return 0; |
| 3398 | if( get2byte(&pParent->aData[pParent->hdrOffset+3])==0 ) return 1; |
drh | 8856d6a | 2004-04-29 14:42:46 +0000 | [diff] [blame] | 3399 | return 0; |
| 3400 | } |
| 3401 | |
| 3402 | /* |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 3403 | ** Move the cursor up to the parent page. |
| 3404 | ** |
| 3405 | ** pCur->idx is set to the cell index that contains the pointer |
| 3406 | ** to the page we are coming from. If we are coming from the |
| 3407 | ** right-most child page then pCur->idx is set to one more than |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 3408 | ** the largest cell index. |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 3409 | */ |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 3410 | void sqlite3BtreeMoveToParent(BtCursor *pCur){ |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 3411 | MemPage *pParent; |
drh | 8178a75 | 2003-01-05 21:41:40 +0000 | [diff] [blame] | 3412 | MemPage *pPage; |
drh | 428ae8c | 2003-01-04 16:48:09 +0000 | [diff] [blame] | 3413 | int idxParent; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3414 | |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 3415 | assert( cursorHoldsMutex(pCur) ); |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 3416 | assert( pCur->eState==CURSOR_VALID ); |
drh | 8178a75 | 2003-01-05 21:41:40 +0000 | [diff] [blame] | 3417 | pPage = pCur->pPage; |
| 3418 | assert( pPage!=0 ); |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 3419 | assert( !sqlite3BtreeIsRootPage(pPage) ); |
drh | 8178a75 | 2003-01-05 21:41:40 +0000 | [diff] [blame] | 3420 | pParent = pPage->pParent; |
| 3421 | assert( pParent!=0 ); |
| 3422 | idxParent = pPage->idxParent; |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 3423 | sqlite3PagerRef(pParent->pDbPage); |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3424 | releasePage(pPage); |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 3425 | pCur->pPage = pParent; |
drh | 271efa5 | 2004-05-30 19:19:05 +0000 | [diff] [blame] | 3426 | pCur->info.nSize = 0; |
drh | a2c20e4 | 2008-03-29 16:01:04 +0000 | [diff] [blame] | 3427 | pCur->validNKey = 0; |
drh | 428ae8c | 2003-01-04 16:48:09 +0000 | [diff] [blame] | 3428 | assert( pParent->idxShift==0 ); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 3429 | pCur->idx = idxParent; |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 3430 | } |
| 3431 | |
| 3432 | /* |
| 3433 | ** Move the cursor to the root page |
| 3434 | */ |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 3435 | static int moveToRoot(BtCursor *pCur){ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3436 | MemPage *pRoot; |
drh | 777e4c4 | 2006-01-13 04:31:58 +0000 | [diff] [blame] | 3437 | int rc = SQLITE_OK; |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 3438 | Btree *p = pCur->pBtree; |
| 3439 | BtShared *pBt = p->pBt; |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 3440 | |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 3441 | assert( cursorHoldsMutex(pCur) ); |
drh | fb98264 | 2007-08-30 01:19:59 +0000 | [diff] [blame] | 3442 | assert( CURSOR_INVALID < CURSOR_REQUIRESEEK ); |
| 3443 | assert( CURSOR_VALID < CURSOR_REQUIRESEEK ); |
| 3444 | assert( CURSOR_FAULT > CURSOR_REQUIRESEEK ); |
| 3445 | if( pCur->eState>=CURSOR_REQUIRESEEK ){ |
| 3446 | if( pCur->eState==CURSOR_FAULT ){ |
| 3447 | return pCur->skip; |
| 3448 | } |
drh | bf700f3 | 2007-03-31 02:36:44 +0000 | [diff] [blame] | 3449 | clearCursorPosition(pCur); |
| 3450 | } |
drh | 777e4c4 | 2006-01-13 04:31:58 +0000 | [diff] [blame] | 3451 | pRoot = pCur->pPage; |
danielk1977 | 97a227c | 2006-01-20 16:32:04 +0000 | [diff] [blame] | 3452 | if( pRoot && pRoot->pgno==pCur->pgnoRoot ){ |
drh | 777e4c4 | 2006-01-13 04:31:58 +0000 | [diff] [blame] | 3453 | assert( pRoot->isInit ); |
| 3454 | }else{ |
| 3455 | if( |
| 3456 | SQLITE_OK!=(rc = getAndInitPage(pBt, pCur->pgnoRoot, &pRoot, 0)) |
| 3457 | ){ |
| 3458 | pCur->eState = CURSOR_INVALID; |
| 3459 | return rc; |
| 3460 | } |
| 3461 | releasePage(pCur->pPage); |
drh | 777e4c4 | 2006-01-13 04:31:58 +0000 | [diff] [blame] | 3462 | pCur->pPage = pRoot; |
drh | c39e000 | 2004-05-07 23:50:57 +0000 | [diff] [blame] | 3463 | } |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 3464 | pCur->idx = 0; |
drh | 271efa5 | 2004-05-30 19:19:05 +0000 | [diff] [blame] | 3465 | pCur->info.nSize = 0; |
drh | a2c20e4 | 2008-03-29 16:01:04 +0000 | [diff] [blame] | 3466 | pCur->atLast = 0; |
| 3467 | pCur->validNKey = 0; |
drh | 8856d6a | 2004-04-29 14:42:46 +0000 | [diff] [blame] | 3468 | if( pRoot->nCell==0 && !pRoot->leaf ){ |
| 3469 | Pgno subpage; |
| 3470 | assert( pRoot->pgno==1 ); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 3471 | subpage = get4byte(&pRoot->aData[pRoot->hdrOffset+8]); |
drh | 8856d6a | 2004-04-29 14:42:46 +0000 | [diff] [blame] | 3472 | assert( subpage>0 ); |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 3473 | pCur->eState = CURSOR_VALID; |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 3474 | rc = moveToChild(pCur, subpage); |
drh | 8856d6a | 2004-04-29 14:42:46 +0000 | [diff] [blame] | 3475 | } |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 3476 | pCur->eState = ((pCur->pPage->nCell>0)?CURSOR_VALID:CURSOR_INVALID); |
drh | 8856d6a | 2004-04-29 14:42:46 +0000 | [diff] [blame] | 3477 | return rc; |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 3478 | } |
drh | 2af926b | 2001-05-15 00:39:25 +0000 | [diff] [blame] | 3479 | |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 3480 | /* |
| 3481 | ** Move the cursor down to the left-most leaf entry beneath the |
| 3482 | ** entry to which it is currently pointing. |
drh | 777e4c4 | 2006-01-13 04:31:58 +0000 | [diff] [blame] | 3483 | ** |
| 3484 | ** The left-most leaf is the one with the smallest key - the first |
| 3485 | ** in ascending order. |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 3486 | */ |
| 3487 | static int moveToLeftmost(BtCursor *pCur){ |
| 3488 | Pgno pgno; |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 3489 | int rc = SQLITE_OK; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3490 | MemPage *pPage; |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 3491 | |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 3492 | assert( cursorHoldsMutex(pCur) ); |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 3493 | assert( pCur->eState==CURSOR_VALID ); |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 3494 | while( rc==SQLITE_OK && !(pPage = pCur->pPage)->leaf ){ |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 3495 | assert( pCur->idx>=0 && pCur->idx<pPage->nCell ); |
danielk1977 | 1cc5ed8 | 2007-05-16 17:28:43 +0000 | [diff] [blame] | 3496 | pgno = get4byte(findCell(pPage, pCur->idx)); |
drh | 8178a75 | 2003-01-05 21:41:40 +0000 | [diff] [blame] | 3497 | rc = moveToChild(pCur, pgno); |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 3498 | } |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 3499 | return rc; |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 3500 | } |
| 3501 | |
drh | 2dcc9aa | 2002-12-04 13:40:25 +0000 | [diff] [blame] | 3502 | /* |
| 3503 | ** Move the cursor down to the right-most leaf entry beneath the |
| 3504 | ** page to which it is currently pointing. Notice the difference |
| 3505 | ** between moveToLeftmost() and moveToRightmost(). moveToLeftmost() |
| 3506 | ** finds the left-most entry beneath the *entry* whereas moveToRightmost() |
| 3507 | ** finds the right-most entry beneath the *page*. |
drh | 777e4c4 | 2006-01-13 04:31:58 +0000 | [diff] [blame] | 3508 | ** |
| 3509 | ** The right-most entry is the one with the largest key - the last |
| 3510 | ** key in ascending order. |
drh | 2dcc9aa | 2002-12-04 13:40:25 +0000 | [diff] [blame] | 3511 | */ |
| 3512 | static int moveToRightmost(BtCursor *pCur){ |
| 3513 | Pgno pgno; |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 3514 | int rc = SQLITE_OK; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3515 | MemPage *pPage; |
drh | 2dcc9aa | 2002-12-04 13:40:25 +0000 | [diff] [blame] | 3516 | |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 3517 | assert( cursorHoldsMutex(pCur) ); |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 3518 | assert( pCur->eState==CURSOR_VALID ); |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 3519 | while( rc==SQLITE_OK && !(pPage = pCur->pPage)->leaf ){ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 3520 | pgno = get4byte(&pPage->aData[pPage->hdrOffset+8]); |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3521 | pCur->idx = pPage->nCell; |
drh | 8178a75 | 2003-01-05 21:41:40 +0000 | [diff] [blame] | 3522 | rc = moveToChild(pCur, pgno); |
drh | 2dcc9aa | 2002-12-04 13:40:25 +0000 | [diff] [blame] | 3523 | } |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 3524 | if( rc==SQLITE_OK ){ |
| 3525 | pCur->idx = pPage->nCell - 1; |
| 3526 | pCur->info.nSize = 0; |
drh | a2c20e4 | 2008-03-29 16:01:04 +0000 | [diff] [blame] | 3527 | pCur->validNKey = 0; |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 3528 | } |
drh | 2dcc9aa | 2002-12-04 13:40:25 +0000 | [diff] [blame] | 3529 | return SQLITE_OK; |
| 3530 | } |
| 3531 | |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3532 | /* Move the cursor to the first entry in the table. Return SQLITE_OK |
| 3533 | ** on success. Set *pRes to 0 if the cursor actually points to something |
drh | 77c679c | 2002-02-19 22:43:58 +0000 | [diff] [blame] | 3534 | ** or set *pRes to 1 if the table is empty. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3535 | */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3536 | int sqlite3BtreeFirst(BtCursor *pCur, int *pRes){ |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3537 | int rc; |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 3538 | |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 3539 | assert( cursorHoldsMutex(pCur) ); |
drh | e5fe690 | 2007-12-07 18:55:28 +0000 | [diff] [blame] | 3540 | assert( sqlite3_mutex_held(pCur->pBtree->db->mutex) ); |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3541 | rc = moveToRoot(pCur); |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 3542 | if( rc==SQLITE_OK ){ |
| 3543 | if( pCur->eState==CURSOR_INVALID ){ |
| 3544 | assert( pCur->pPage->nCell==0 ); |
| 3545 | *pRes = 1; |
| 3546 | rc = SQLITE_OK; |
| 3547 | }else{ |
| 3548 | assert( pCur->pPage->nCell>0 ); |
| 3549 | *pRes = 0; |
| 3550 | rc = moveToLeftmost(pCur); |
| 3551 | } |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3552 | } |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3553 | return rc; |
| 3554 | } |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 3555 | |
drh | 9562b55 | 2002-02-19 15:00:07 +0000 | [diff] [blame] | 3556 | /* Move the cursor to the last entry in the table. Return SQLITE_OK |
| 3557 | ** on success. Set *pRes to 0 if the cursor actually points to something |
drh | 77c679c | 2002-02-19 22:43:58 +0000 | [diff] [blame] | 3558 | ** or set *pRes to 1 if the table is empty. |
drh | 9562b55 | 2002-02-19 15:00:07 +0000 | [diff] [blame] | 3559 | */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3560 | int sqlite3BtreeLast(BtCursor *pCur, int *pRes){ |
drh | 9562b55 | 2002-02-19 15:00:07 +0000 | [diff] [blame] | 3561 | int rc; |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 3562 | |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 3563 | assert( cursorHoldsMutex(pCur) ); |
drh | e5fe690 | 2007-12-07 18:55:28 +0000 | [diff] [blame] | 3564 | assert( sqlite3_mutex_held(pCur->pBtree->db->mutex) ); |
drh | 9562b55 | 2002-02-19 15:00:07 +0000 | [diff] [blame] | 3565 | rc = moveToRoot(pCur); |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 3566 | if( rc==SQLITE_OK ){ |
| 3567 | if( CURSOR_INVALID==pCur->eState ){ |
| 3568 | assert( pCur->pPage->nCell==0 ); |
| 3569 | *pRes = 1; |
| 3570 | }else{ |
| 3571 | assert( pCur->eState==CURSOR_VALID ); |
| 3572 | *pRes = 0; |
| 3573 | rc = moveToRightmost(pCur); |
drh | a2c20e4 | 2008-03-29 16:01:04 +0000 | [diff] [blame] | 3574 | getCellInfo(pCur); |
| 3575 | pCur->atLast = rc==SQLITE_OK; |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 3576 | } |
drh | 9562b55 | 2002-02-19 15:00:07 +0000 | [diff] [blame] | 3577 | } |
drh | 9562b55 | 2002-02-19 15:00:07 +0000 | [diff] [blame] | 3578 | return rc; |
| 3579 | } |
| 3580 | |
drh | e14006d | 2008-03-25 17:23:32 +0000 | [diff] [blame] | 3581 | /* Move the cursor so that it points to an entry near the key |
| 3582 | ** specified by pKey/nKey/pUnKey. Return a success code. |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 3583 | ** |
drh | e14006d | 2008-03-25 17:23:32 +0000 | [diff] [blame] | 3584 | ** For INTKEY tables, only the nKey parameter is used. pKey |
| 3585 | ** and pUnKey must be NULL. For index tables, either pUnKey |
| 3586 | ** must point to a key that has already been unpacked, or else |
| 3587 | ** pKey/nKey describes a blob containing the key. |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3588 | ** |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 3589 | ** If an exact match is not found, then the cursor is always |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 3590 | ** left pointing at a leaf page which would hold the entry if it |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 3591 | ** were present. The cursor might point to an entry that comes |
| 3592 | ** before or after the key. |
| 3593 | ** |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 3594 | ** The result of comparing the key with the entry to which the |
drh | ab01f61 | 2004-05-22 02:55:23 +0000 | [diff] [blame] | 3595 | ** cursor is written to *pRes if pRes!=NULL. The meaning of |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 3596 | ** this value is as follows: |
| 3597 | ** |
| 3598 | ** *pRes<0 The cursor is left pointing at an entry that |
drh | 1a844c3 | 2002-12-04 22:29:28 +0000 | [diff] [blame] | 3599 | ** is smaller than pKey or if the table is empty |
| 3600 | ** and the cursor is therefore left point to nothing. |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 3601 | ** |
| 3602 | ** *pRes==0 The cursor is left pointing at an entry that |
| 3603 | ** exactly matches pKey. |
| 3604 | ** |
| 3605 | ** *pRes>0 The cursor is left pointing at an entry that |
drh | 7c717f7 | 2001-06-24 20:39:41 +0000 | [diff] [blame] | 3606 | ** is larger than pKey. |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 3607 | ** |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 3608 | */ |
drh | e4d9081 | 2007-03-29 05:51:49 +0000 | [diff] [blame] | 3609 | int sqlite3BtreeMoveto( |
| 3610 | BtCursor *pCur, /* The cursor to be moved */ |
| 3611 | const void *pKey, /* The key content for indices. Not used by tables */ |
drh | e14006d | 2008-03-25 17:23:32 +0000 | [diff] [blame] | 3612 | UnpackedRecord *pUnKey,/* Unpacked version of pKey */ |
drh | e4d9081 | 2007-03-29 05:51:49 +0000 | [diff] [blame] | 3613 | i64 nKey, /* Size of pKey. Or the key for tables */ |
| 3614 | int biasRight, /* If true, bias the search to the high end */ |
| 3615 | int *pRes /* Search result flag */ |
| 3616 | ){ |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 3617 | int rc; |
drh | 1e968a0 | 2008-03-25 00:22:21 +0000 | [diff] [blame] | 3618 | char aSpace[200]; |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 3619 | |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 3620 | assert( cursorHoldsMutex(pCur) ); |
drh | e5fe690 | 2007-12-07 18:55:28 +0000 | [diff] [blame] | 3621 | assert( sqlite3_mutex_held(pCur->pBtree->db->mutex) ); |
drh | a2c20e4 | 2008-03-29 16:01:04 +0000 | [diff] [blame] | 3622 | |
| 3623 | /* If the cursor is already positioned at the point we are trying |
| 3624 | ** to move to, then just return without doing any work */ |
| 3625 | if( pCur->eState==CURSOR_VALID && pCur->validNKey && pCur->pPage->intKey ){ |
| 3626 | if( pCur->info.nKey==nKey ){ |
| 3627 | *pRes = 0; |
| 3628 | return SQLITE_OK; |
| 3629 | } |
| 3630 | if( pCur->atLast && pCur->info.nKey<nKey ){ |
| 3631 | *pRes = -1; |
| 3632 | return SQLITE_OK; |
| 3633 | } |
| 3634 | } |
| 3635 | |
| 3636 | |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 3637 | rc = moveToRoot(pCur); |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 3638 | if( rc ){ |
| 3639 | return rc; |
| 3640 | } |
drh | c39e000 | 2004-05-07 23:50:57 +0000 | [diff] [blame] | 3641 | assert( pCur->pPage ); |
| 3642 | assert( pCur->pPage->isInit ); |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 3643 | if( pCur->eState==CURSOR_INVALID ){ |
drh | f328bc8 | 2004-05-10 23:29:49 +0000 | [diff] [blame] | 3644 | *pRes = -1; |
drh | c39e000 | 2004-05-07 23:50:57 +0000 | [diff] [blame] | 3645 | assert( pCur->pPage->nCell==0 ); |
| 3646 | return SQLITE_OK; |
| 3647 | } |
drh | 1e968a0 | 2008-03-25 00:22:21 +0000 | [diff] [blame] | 3648 | if( pCur->pPage->intKey ){ |
drh | e14006d | 2008-03-25 17:23:32 +0000 | [diff] [blame] | 3649 | /* We are given an SQL table to search. The key is the integer |
| 3650 | ** rowid contained in nKey. pKey and pUnKey should both be NULL */ |
| 3651 | assert( pUnKey==0 ); |
| 3652 | assert( pKey==0 ); |
| 3653 | }else if( pUnKey==0 ){ |
| 3654 | /* We are to search an SQL index using a key encoded as a blob. |
| 3655 | ** The blob is found at pKey and is nKey bytes in length. Unpack |
| 3656 | ** this key so that we can use it. */ |
| 3657 | assert( pKey!=0 ); |
| 3658 | pUnKey = sqlite3VdbeRecordUnpack(pCur->pKeyInfo, nKey, pKey, |
drh | 1e968a0 | 2008-03-25 00:22:21 +0000 | [diff] [blame] | 3659 | aSpace, sizeof(aSpace)); |
drh | e14006d | 2008-03-25 17:23:32 +0000 | [diff] [blame] | 3660 | if( pUnKey==0 ) return SQLITE_NOMEM; |
| 3661 | }else{ |
| 3662 | /* We are to search an SQL index using a key that is already unpacked |
| 3663 | ** and handed to us in pUnKey. */ |
| 3664 | assert( pKey==0 ); |
drh | 1e968a0 | 2008-03-25 00:22:21 +0000 | [diff] [blame] | 3665 | } |
drh | 1468438 | 2006-11-30 13:05:29 +0000 | [diff] [blame] | 3666 | for(;;){ |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 3667 | int lwr, upr; |
| 3668 | Pgno chldPg; |
| 3669 | MemPage *pPage = pCur->pPage; |
drh | 1a844c3 | 2002-12-04 22:29:28 +0000 | [diff] [blame] | 3670 | int c = -1; /* pRes return if table is empty must be -1 */ |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 3671 | lwr = 0; |
| 3672 | upr = pPage->nCell-1; |
drh | e14006d | 2008-03-25 17:23:32 +0000 | [diff] [blame] | 3673 | if( !pPage->intKey && pUnKey==0 ){ |
drh | 1e968a0 | 2008-03-25 00:22:21 +0000 | [diff] [blame] | 3674 | rc = SQLITE_CORRUPT_BKPT; |
| 3675 | goto moveto_finish; |
drh | 4eec4c1 | 2005-01-21 00:22:37 +0000 | [diff] [blame] | 3676 | } |
drh | e4d9081 | 2007-03-29 05:51:49 +0000 | [diff] [blame] | 3677 | if( biasRight ){ |
| 3678 | pCur->idx = upr; |
| 3679 | }else{ |
| 3680 | pCur->idx = (upr+lwr)/2; |
| 3681 | } |
drh | f1d68b3 | 2007-03-29 04:43:26 +0000 | [diff] [blame] | 3682 | if( lwr<=upr ) for(;;){ |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 3683 | void *pCellKey; |
drh | 4a1c380 | 2004-05-12 15:15:47 +0000 | [diff] [blame] | 3684 | i64 nCellKey; |
drh | 366fda6 | 2006-01-13 02:35:09 +0000 | [diff] [blame] | 3685 | pCur->info.nSize = 0; |
drh | a2c20e4 | 2008-03-29 16:01:04 +0000 | [diff] [blame] | 3686 | pCur->validNKey = 1; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3687 | if( pPage->intKey ){ |
drh | 777e4c4 | 2006-01-13 04:31:58 +0000 | [diff] [blame] | 3688 | u8 *pCell; |
danielk1977 | 1cc5ed8 | 2007-05-16 17:28:43 +0000 | [diff] [blame] | 3689 | pCell = findCell(pPage, pCur->idx) + pPage->childPtrSize; |
drh | d172f86 | 2006-01-12 15:01:15 +0000 | [diff] [blame] | 3690 | if( pPage->hasData ){ |
danielk1977 | bab45c6 | 2006-01-16 15:14:27 +0000 | [diff] [blame] | 3691 | u32 dummy; |
shane | 3f8d5cf | 2008-04-24 19:15:09 +0000 | [diff] [blame] | 3692 | pCell += getVarint32(pCell, dummy); |
drh | d172f86 | 2006-01-12 15:01:15 +0000 | [diff] [blame] | 3693 | } |
drh | a2c20e4 | 2008-03-29 16:01:04 +0000 | [diff] [blame] | 3694 | getVarint(pCell, (u64*)&nCellKey); |
drh | 41eb9e9 | 2008-04-02 18:33:07 +0000 | [diff] [blame] | 3695 | if( nCellKey==nKey ){ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3696 | c = 0; |
drh | 41eb9e9 | 2008-04-02 18:33:07 +0000 | [diff] [blame] | 3697 | }else if( nCellKey<nKey ){ |
| 3698 | c = -1; |
| 3699 | }else{ |
| 3700 | assert( nCellKey>nKey ); |
| 3701 | c = +1; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3702 | } |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3703 | }else{ |
drh | e51c44f | 2004-05-30 20:46:09 +0000 | [diff] [blame] | 3704 | int available; |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 3705 | pCellKey = (void *)fetchPayload(pCur, &available, 0); |
drh | 366fda6 | 2006-01-13 02:35:09 +0000 | [diff] [blame] | 3706 | nCellKey = pCur->info.nKey; |
drh | e51c44f | 2004-05-30 20:46:09 +0000 | [diff] [blame] | 3707 | if( available>=nCellKey ){ |
drh | e14006d | 2008-03-25 17:23:32 +0000 | [diff] [blame] | 3708 | c = sqlite3VdbeRecordCompare(nCellKey, pCellKey, pUnKey); |
drh | e51c44f | 2004-05-30 20:46:09 +0000 | [diff] [blame] | 3709 | }else{ |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 3710 | pCellKey = sqlite3_malloc( nCellKey ); |
danielk1977 | 6507ecb | 2008-03-25 09:56:44 +0000 | [diff] [blame] | 3711 | if( pCellKey==0 ){ |
| 3712 | rc = SQLITE_NOMEM; |
| 3713 | goto moveto_finish; |
| 3714 | } |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 3715 | rc = sqlite3BtreeKey(pCur, 0, nCellKey, (void *)pCellKey); |
drh | e14006d | 2008-03-25 17:23:32 +0000 | [diff] [blame] | 3716 | c = sqlite3VdbeRecordCompare(nCellKey, pCellKey, pUnKey); |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 3717 | sqlite3_free(pCellKey); |
drh | 1e968a0 | 2008-03-25 00:22:21 +0000 | [diff] [blame] | 3718 | if( rc ) goto moveto_finish; |
drh | e51c44f | 2004-05-30 20:46:09 +0000 | [diff] [blame] | 3719 | } |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3720 | } |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 3721 | if( c==0 ){ |
drh | a2c20e4 | 2008-03-29 16:01:04 +0000 | [diff] [blame] | 3722 | pCur->info.nKey = nCellKey; |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 3723 | if( pPage->leafData && !pPage->leaf ){ |
drh | fc70e6f | 2004-05-12 21:11:27 +0000 | [diff] [blame] | 3724 | lwr = pCur->idx; |
| 3725 | upr = lwr - 1; |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 3726 | break; |
| 3727 | }else{ |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 3728 | if( pRes ) *pRes = 0; |
drh | 1e968a0 | 2008-03-25 00:22:21 +0000 | [diff] [blame] | 3729 | rc = SQLITE_OK; |
| 3730 | goto moveto_finish; |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 3731 | } |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 3732 | } |
| 3733 | if( c<0 ){ |
| 3734 | lwr = pCur->idx+1; |
| 3735 | }else{ |
| 3736 | upr = pCur->idx-1; |
| 3737 | } |
drh | f1d68b3 | 2007-03-29 04:43:26 +0000 | [diff] [blame] | 3738 | if( lwr>upr ){ |
drh | a2c20e4 | 2008-03-29 16:01:04 +0000 | [diff] [blame] | 3739 | pCur->info.nKey = nCellKey; |
drh | f1d68b3 | 2007-03-29 04:43:26 +0000 | [diff] [blame] | 3740 | break; |
| 3741 | } |
| 3742 | pCur->idx = (lwr+upr)/2; |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 3743 | } |
| 3744 | assert( lwr==upr+1 ); |
drh | 7aa128d | 2002-06-21 13:09:16 +0000 | [diff] [blame] | 3745 | assert( pPage->isInit ); |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3746 | if( pPage->leaf ){ |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 3747 | chldPg = 0; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3748 | }else if( lwr>=pPage->nCell ){ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 3749 | chldPg = get4byte(&pPage->aData[pPage->hdrOffset+8]); |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 3750 | }else{ |
danielk1977 | 1cc5ed8 | 2007-05-16 17:28:43 +0000 | [diff] [blame] | 3751 | chldPg = get4byte(findCell(pPage, lwr)); |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 3752 | } |
| 3753 | if( chldPg==0 ){ |
drh | c39e000 | 2004-05-07 23:50:57 +0000 | [diff] [blame] | 3754 | assert( pCur->idx>=0 && pCur->idx<pCur->pPage->nCell ); |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 3755 | if( pRes ) *pRes = c; |
drh | 1e968a0 | 2008-03-25 00:22:21 +0000 | [diff] [blame] | 3756 | rc = SQLITE_OK; |
| 3757 | goto moveto_finish; |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 3758 | } |
drh | 428ae8c | 2003-01-04 16:48:09 +0000 | [diff] [blame] | 3759 | pCur->idx = lwr; |
drh | 271efa5 | 2004-05-30 19:19:05 +0000 | [diff] [blame] | 3760 | pCur->info.nSize = 0; |
drh | a2c20e4 | 2008-03-29 16:01:04 +0000 | [diff] [blame] | 3761 | pCur->validNKey = 0; |
drh | 8178a75 | 2003-01-05 21:41:40 +0000 | [diff] [blame] | 3762 | rc = moveToChild(pCur, chldPg); |
drh | 1e968a0 | 2008-03-25 00:22:21 +0000 | [diff] [blame] | 3763 | if( rc ) goto moveto_finish; |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 3764 | } |
drh | 1e968a0 | 2008-03-25 00:22:21 +0000 | [diff] [blame] | 3765 | moveto_finish: |
drh | e14006d | 2008-03-25 17:23:32 +0000 | [diff] [blame] | 3766 | if( pKey ){ |
| 3767 | /* If we created our own unpacked key at the top of this |
| 3768 | ** procedure, then destroy that key before returning. */ |
| 3769 | sqlite3VdbeDeleteUnpackedRecord(pUnKey); |
| 3770 | } |
drh | 1e968a0 | 2008-03-25 00:22:21 +0000 | [diff] [blame] | 3771 | return rc; |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 3772 | } |
| 3773 | |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 3774 | |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 3775 | /* |
drh | c39e000 | 2004-05-07 23:50:57 +0000 | [diff] [blame] | 3776 | ** Return TRUE if the cursor is not pointing at an entry of the table. |
| 3777 | ** |
| 3778 | ** TRUE will be returned after a call to sqlite3BtreeNext() moves |
| 3779 | ** past the last entry in the table or sqlite3BtreePrev() moves past |
| 3780 | ** the first entry. TRUE is also returned if the table is empty. |
| 3781 | */ |
| 3782 | int sqlite3BtreeEof(BtCursor *pCur){ |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 3783 | /* TODO: What if the cursor is in CURSOR_REQUIRESEEK but all table entries |
| 3784 | ** have been deleted? This API will need to change to return an error code |
| 3785 | ** as well as the boolean result value. |
| 3786 | */ |
| 3787 | return (CURSOR_VALID!=pCur->eState); |
drh | c39e000 | 2004-05-07 23:50:57 +0000 | [diff] [blame] | 3788 | } |
| 3789 | |
| 3790 | /* |
drh | b21c8cd | 2007-08-21 19:33:56 +0000 | [diff] [blame] | 3791 | ** Return the database connection handle for a cursor. |
| 3792 | */ |
| 3793 | sqlite3 *sqlite3BtreeCursorDb(const BtCursor *pCur){ |
drh | e5fe690 | 2007-12-07 18:55:28 +0000 | [diff] [blame] | 3794 | assert( sqlite3_mutex_held(pCur->pBtree->db->mutex) ); |
| 3795 | return pCur->pBtree->db; |
drh | b21c8cd | 2007-08-21 19:33:56 +0000 | [diff] [blame] | 3796 | } |
| 3797 | |
| 3798 | /* |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 3799 | ** Advance the cursor to the next entry in the database. If |
drh | 8c1238a | 2003-01-02 14:43:55 +0000 | [diff] [blame] | 3800 | ** successful then set *pRes=0. If the cursor |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 3801 | ** was already pointing to the last entry in the database before |
drh | 8c1238a | 2003-01-02 14:43:55 +0000 | [diff] [blame] | 3802 | ** this routine was called, then set *pRes=1. |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 3803 | */ |
drh | d094db1 | 2008-04-03 21:46:57 +0000 | [diff] [blame] | 3804 | int sqlite3BtreeNext(BtCursor *pCur, int *pRes){ |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 3805 | int rc; |
danielk1977 | 97a227c | 2006-01-20 16:32:04 +0000 | [diff] [blame] | 3806 | MemPage *pPage; |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 3807 | |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 3808 | assert( cursorHoldsMutex(pCur) ); |
drh | bf700f3 | 2007-03-31 02:36:44 +0000 | [diff] [blame] | 3809 | rc = restoreOrClearCursorPosition(pCur); |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 3810 | if( rc!=SQLITE_OK ){ |
| 3811 | return rc; |
| 3812 | } |
drh | 8c4d3a6 | 2007-04-06 01:03:32 +0000 | [diff] [blame] | 3813 | assert( pRes!=0 ); |
| 3814 | pPage = pCur->pPage; |
| 3815 | if( CURSOR_INVALID==pCur->eState ){ |
| 3816 | *pRes = 1; |
| 3817 | return SQLITE_OK; |
| 3818 | } |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 3819 | if( pCur->skip>0 ){ |
| 3820 | pCur->skip = 0; |
| 3821 | *pRes = 0; |
| 3822 | return SQLITE_OK; |
| 3823 | } |
| 3824 | pCur->skip = 0; |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 3825 | |
drh | 8178a75 | 2003-01-05 21:41:40 +0000 | [diff] [blame] | 3826 | assert( pPage->isInit ); |
drh | 8178a75 | 2003-01-05 21:41:40 +0000 | [diff] [blame] | 3827 | assert( pCur->idx<pPage->nCell ); |
danielk1977 | 6a43f9b | 2004-11-16 04:57:24 +0000 | [diff] [blame] | 3828 | |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 3829 | pCur->idx++; |
drh | 271efa5 | 2004-05-30 19:19:05 +0000 | [diff] [blame] | 3830 | pCur->info.nSize = 0; |
drh | a2c20e4 | 2008-03-29 16:01:04 +0000 | [diff] [blame] | 3831 | pCur->validNKey = 0; |
drh | 8178a75 | 2003-01-05 21:41:40 +0000 | [diff] [blame] | 3832 | if( pCur->idx>=pPage->nCell ){ |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 3833 | if( !pPage->leaf ){ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 3834 | rc = moveToChild(pCur, get4byte(&pPage->aData[pPage->hdrOffset+8])); |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 3835 | if( rc ) return rc; |
| 3836 | rc = moveToLeftmost(pCur); |
drh | 8c1238a | 2003-01-02 14:43:55 +0000 | [diff] [blame] | 3837 | *pRes = 0; |
| 3838 | return rc; |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 3839 | } |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 3840 | do{ |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 3841 | if( sqlite3BtreeIsRootPage(pPage) ){ |
drh | 8c1238a | 2003-01-02 14:43:55 +0000 | [diff] [blame] | 3842 | *pRes = 1; |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 3843 | pCur->eState = CURSOR_INVALID; |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 3844 | return SQLITE_OK; |
| 3845 | } |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 3846 | sqlite3BtreeMoveToParent(pCur); |
drh | 8178a75 | 2003-01-05 21:41:40 +0000 | [diff] [blame] | 3847 | pPage = pCur->pPage; |
| 3848 | }while( pCur->idx>=pPage->nCell ); |
drh | 8c1238a | 2003-01-02 14:43:55 +0000 | [diff] [blame] | 3849 | *pRes = 0; |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 3850 | if( pPage->leafData ){ |
| 3851 | rc = sqlite3BtreeNext(pCur, pRes); |
| 3852 | }else{ |
| 3853 | rc = SQLITE_OK; |
| 3854 | } |
| 3855 | return rc; |
drh | 8178a75 | 2003-01-05 21:41:40 +0000 | [diff] [blame] | 3856 | } |
| 3857 | *pRes = 0; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3858 | if( pPage->leaf ){ |
drh | 8178a75 | 2003-01-05 21:41:40 +0000 | [diff] [blame] | 3859 | return SQLITE_OK; |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 3860 | } |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 3861 | rc = moveToLeftmost(pCur); |
drh | 8c1238a | 2003-01-02 14:43:55 +0000 | [diff] [blame] | 3862 | return rc; |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 3863 | } |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 3864 | |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 3865 | |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 3866 | /* |
drh | 2dcc9aa | 2002-12-04 13:40:25 +0000 | [diff] [blame] | 3867 | ** 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] | 3868 | ** successful then set *pRes=0. If the cursor |
drh | 2dcc9aa | 2002-12-04 13:40:25 +0000 | [diff] [blame] | 3869 | ** was already pointing to the first entry in the database before |
drh | 8178a75 | 2003-01-05 21:41:40 +0000 | [diff] [blame] | 3870 | ** this routine was called, then set *pRes=1. |
drh | 2dcc9aa | 2002-12-04 13:40:25 +0000 | [diff] [blame] | 3871 | */ |
drh | d094db1 | 2008-04-03 21:46:57 +0000 | [diff] [blame] | 3872 | int sqlite3BtreePrevious(BtCursor *pCur, int *pRes){ |
drh | 2dcc9aa | 2002-12-04 13:40:25 +0000 | [diff] [blame] | 3873 | int rc; |
| 3874 | Pgno pgno; |
drh | 8178a75 | 2003-01-05 21:41:40 +0000 | [diff] [blame] | 3875 | MemPage *pPage; |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 3876 | |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 3877 | assert( cursorHoldsMutex(pCur) ); |
drh | bf700f3 | 2007-03-31 02:36:44 +0000 | [diff] [blame] | 3878 | rc = restoreOrClearCursorPosition(pCur); |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 3879 | if( rc!=SQLITE_OK ){ |
| 3880 | return rc; |
| 3881 | } |
drh | a2c20e4 | 2008-03-29 16:01:04 +0000 | [diff] [blame] | 3882 | pCur->atLast = 0; |
drh | 8c4d3a6 | 2007-04-06 01:03:32 +0000 | [diff] [blame] | 3883 | if( CURSOR_INVALID==pCur->eState ){ |
| 3884 | *pRes = 1; |
| 3885 | return SQLITE_OK; |
| 3886 | } |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 3887 | if( pCur->skip<0 ){ |
| 3888 | pCur->skip = 0; |
| 3889 | *pRes = 0; |
| 3890 | return SQLITE_OK; |
| 3891 | } |
| 3892 | pCur->skip = 0; |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 3893 | |
drh | 8178a75 | 2003-01-05 21:41:40 +0000 | [diff] [blame] | 3894 | pPage = pCur->pPage; |
drh | 8178a75 | 2003-01-05 21:41:40 +0000 | [diff] [blame] | 3895 | assert( pPage->isInit ); |
drh | 2dcc9aa | 2002-12-04 13:40:25 +0000 | [diff] [blame] | 3896 | assert( pCur->idx>=0 ); |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 3897 | if( !pPage->leaf ){ |
danielk1977 | 1cc5ed8 | 2007-05-16 17:28:43 +0000 | [diff] [blame] | 3898 | pgno = get4byte( findCell(pPage, pCur->idx) ); |
drh | 8178a75 | 2003-01-05 21:41:40 +0000 | [diff] [blame] | 3899 | rc = moveToChild(pCur, pgno); |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 3900 | if( rc ){ |
| 3901 | return rc; |
| 3902 | } |
drh | 2dcc9aa | 2002-12-04 13:40:25 +0000 | [diff] [blame] | 3903 | rc = moveToRightmost(pCur); |
| 3904 | }else{ |
| 3905 | while( pCur->idx==0 ){ |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 3906 | if( sqlite3BtreeIsRootPage(pPage) ){ |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 3907 | pCur->eState = CURSOR_INVALID; |
drh | c39e000 | 2004-05-07 23:50:57 +0000 | [diff] [blame] | 3908 | *pRes = 1; |
drh | 2dcc9aa | 2002-12-04 13:40:25 +0000 | [diff] [blame] | 3909 | return SQLITE_OK; |
| 3910 | } |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 3911 | sqlite3BtreeMoveToParent(pCur); |
drh | 8178a75 | 2003-01-05 21:41:40 +0000 | [diff] [blame] | 3912 | pPage = pCur->pPage; |
drh | 2dcc9aa | 2002-12-04 13:40:25 +0000 | [diff] [blame] | 3913 | } |
| 3914 | pCur->idx--; |
drh | 271efa5 | 2004-05-30 19:19:05 +0000 | [diff] [blame] | 3915 | pCur->info.nSize = 0; |
drh | a2c20e4 | 2008-03-29 16:01:04 +0000 | [diff] [blame] | 3916 | pCur->validNKey = 0; |
drh | 8237d45 | 2004-11-22 19:07:09 +0000 | [diff] [blame] | 3917 | if( pPage->leafData && !pPage->leaf ){ |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 3918 | rc = sqlite3BtreePrevious(pCur, pRes); |
| 3919 | }else{ |
| 3920 | rc = SQLITE_OK; |
| 3921 | } |
drh | 2dcc9aa | 2002-12-04 13:40:25 +0000 | [diff] [blame] | 3922 | } |
drh | 8178a75 | 2003-01-05 21:41:40 +0000 | [diff] [blame] | 3923 | *pRes = 0; |
drh | 2dcc9aa | 2002-12-04 13:40:25 +0000 | [diff] [blame] | 3924 | return rc; |
| 3925 | } |
| 3926 | |
| 3927 | /* |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 3928 | ** Allocate a new page from the database file. |
| 3929 | ** |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 3930 | ** The new page is marked as dirty. (In other words, sqlite3PagerWrite() |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 3931 | ** has already been called on the new page.) The new page has also |
| 3932 | ** been referenced and the calling routine is responsible for calling |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 3933 | ** sqlite3PagerUnref() on the new page when it is done. |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 3934 | ** |
| 3935 | ** SQLITE_OK is returned on success. Any other return value indicates |
| 3936 | ** an error. *ppPage and *pPgno are undefined in the event of an error. |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 3937 | ** Do not invoke sqlite3PagerUnref() on *ppPage if an error is returned. |
drh | bea00b9 | 2002-07-08 10:59:50 +0000 | [diff] [blame] | 3938 | ** |
drh | 199e3cf | 2002-07-18 11:01:47 +0000 | [diff] [blame] | 3939 | ** If the "nearby" parameter is not 0, then a (feeble) effort is made to |
| 3940 | ** locate a page close to the page number "nearby". This can be used in an |
drh | bea00b9 | 2002-07-08 10:59:50 +0000 | [diff] [blame] | 3941 | ** attempt to keep related pages close to each other in the database file, |
| 3942 | ** which in turn can make database access faster. |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 3943 | ** |
| 3944 | ** If the "exact" parameter is not 0, and the page-number nearby exists |
| 3945 | ** anywhere on the free-list, then it is guarenteed to be returned. This |
| 3946 | ** is only used by auto-vacuum databases when allocating a new table. |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 3947 | */ |
drh | 4f0c587 | 2007-03-26 22:05:01 +0000 | [diff] [blame] | 3948 | static int allocateBtreePage( |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 3949 | BtShared *pBt, |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 3950 | MemPage **ppPage, |
| 3951 | Pgno *pPgno, |
| 3952 | Pgno nearby, |
| 3953 | u8 exact |
| 3954 | ){ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3955 | MemPage *pPage1; |
drh | 8c42ca9 | 2001-06-22 19:15:00 +0000 | [diff] [blame] | 3956 | int rc; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3957 | int n; /* Number of pages on the freelist */ |
| 3958 | int k; /* Number of leaves on the trunk of the freelist */ |
drh | d3627af | 2006-12-18 18:34:51 +0000 | [diff] [blame] | 3959 | MemPage *pTrunk = 0; |
| 3960 | MemPage *pPrevTrunk = 0; |
drh | 30e5875 | 2002-03-02 20:41:57 +0000 | [diff] [blame] | 3961 | |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 3962 | assert( sqlite3_mutex_held(pBt->mutex) ); |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3963 | pPage1 = pBt->pPage1; |
| 3964 | n = get4byte(&pPage1->aData[36]); |
| 3965 | if( n>0 ){ |
drh | 9102529 | 2004-05-03 19:49:32 +0000 | [diff] [blame] | 3966 | /* There are pages on the freelist. Reuse one of those pages. */ |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 3967 | Pgno iTrunk; |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 3968 | u8 searchList = 0; /* If the free-list must be searched for 'nearby' */ |
| 3969 | |
| 3970 | /* If the 'exact' parameter was true and a query of the pointer-map |
| 3971 | ** shows that the page 'nearby' is somewhere on the free-list, then |
| 3972 | ** the entire-list will be searched for that page. |
| 3973 | */ |
| 3974 | #ifndef SQLITE_OMIT_AUTOVACUUM |
danielk1977 | ad0132d | 2008-06-07 08:58:22 +0000 | [diff] [blame^] | 3975 | if( exact && nearby<=pagerPagecount(pBt->pPager) ){ |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 3976 | u8 eType; |
| 3977 | assert( nearby>0 ); |
| 3978 | assert( pBt->autoVacuum ); |
| 3979 | rc = ptrmapGet(pBt, nearby, &eType, 0); |
| 3980 | if( rc ) return rc; |
| 3981 | if( eType==PTRMAP_FREEPAGE ){ |
| 3982 | searchList = 1; |
| 3983 | } |
| 3984 | *pPgno = nearby; |
| 3985 | } |
| 3986 | #endif |
| 3987 | |
| 3988 | /* Decrement the free-list count by 1. Set iTrunk to the index of the |
| 3989 | ** first free-list trunk page. iPrevTrunk is initially 1. |
| 3990 | */ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 3991 | rc = sqlite3PagerWrite(pPage1->pDbPage); |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 3992 | if( rc ) return rc; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3993 | put4byte(&pPage1->aData[36], n-1); |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 3994 | |
| 3995 | /* The code within this loop is run only once if the 'searchList' variable |
| 3996 | ** is not true. Otherwise, it runs once for each trunk-page on the |
| 3997 | ** free-list until the page 'nearby' is located. |
| 3998 | */ |
| 3999 | do { |
| 4000 | pPrevTrunk = pTrunk; |
| 4001 | if( pPrevTrunk ){ |
| 4002 | iTrunk = get4byte(&pPrevTrunk->aData[0]); |
drh | bea00b9 | 2002-07-08 10:59:50 +0000 | [diff] [blame] | 4003 | }else{ |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 4004 | iTrunk = get4byte(&pPage1->aData[32]); |
drh | bea00b9 | 2002-07-08 10:59:50 +0000 | [diff] [blame] | 4005 | } |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 4006 | rc = sqlite3BtreeGetPage(pBt, iTrunk, &pTrunk, 0); |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 4007 | if( rc ){ |
drh | d3627af | 2006-12-18 18:34:51 +0000 | [diff] [blame] | 4008 | pTrunk = 0; |
| 4009 | goto end_allocate_page; |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 4010 | } |
| 4011 | |
| 4012 | k = get4byte(&pTrunk->aData[4]); |
| 4013 | if( k==0 && !searchList ){ |
| 4014 | /* The trunk has no leaves and the list is not being searched. |
| 4015 | ** So extract the trunk page itself and use it as the newly |
| 4016 | ** allocated page */ |
| 4017 | assert( pPrevTrunk==0 ); |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 4018 | rc = sqlite3PagerWrite(pTrunk->pDbPage); |
drh | d3627af | 2006-12-18 18:34:51 +0000 | [diff] [blame] | 4019 | if( rc ){ |
| 4020 | goto end_allocate_page; |
| 4021 | } |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 4022 | *pPgno = iTrunk; |
| 4023 | memcpy(&pPage1->aData[32], &pTrunk->aData[0], 4); |
| 4024 | *ppPage = pTrunk; |
| 4025 | pTrunk = 0; |
| 4026 | TRACE(("ALLOCATE: %d trunk - %d free pages left\n", *pPgno, n-1)); |
| 4027 | }else if( k>pBt->usableSize/4 - 8 ){ |
| 4028 | /* Value of k is out of range. Database corruption */ |
drh | d3627af | 2006-12-18 18:34:51 +0000 | [diff] [blame] | 4029 | rc = SQLITE_CORRUPT_BKPT; |
| 4030 | goto end_allocate_page; |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 4031 | #ifndef SQLITE_OMIT_AUTOVACUUM |
| 4032 | }else if( searchList && nearby==iTrunk ){ |
| 4033 | /* The list is being searched and this trunk page is the page |
| 4034 | ** to allocate, regardless of whether it has leaves. |
| 4035 | */ |
| 4036 | assert( *pPgno==iTrunk ); |
| 4037 | *ppPage = pTrunk; |
| 4038 | searchList = 0; |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 4039 | rc = sqlite3PagerWrite(pTrunk->pDbPage); |
drh | d3627af | 2006-12-18 18:34:51 +0000 | [diff] [blame] | 4040 | if( rc ){ |
| 4041 | goto end_allocate_page; |
| 4042 | } |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 4043 | if( k==0 ){ |
| 4044 | if( !pPrevTrunk ){ |
| 4045 | memcpy(&pPage1->aData[32], &pTrunk->aData[0], 4); |
| 4046 | }else{ |
| 4047 | memcpy(&pPrevTrunk->aData[0], &pTrunk->aData[0], 4); |
| 4048 | } |
| 4049 | }else{ |
| 4050 | /* The trunk page is required by the caller but it contains |
| 4051 | ** pointers to free-list leaves. The first leaf becomes a trunk |
| 4052 | ** page in this case. |
| 4053 | */ |
| 4054 | MemPage *pNewTrunk; |
| 4055 | Pgno iNewTrunk = get4byte(&pTrunk->aData[8]); |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 4056 | rc = sqlite3BtreeGetPage(pBt, iNewTrunk, &pNewTrunk, 0); |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 4057 | if( rc!=SQLITE_OK ){ |
drh | d3627af | 2006-12-18 18:34:51 +0000 | [diff] [blame] | 4058 | goto end_allocate_page; |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 4059 | } |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 4060 | rc = sqlite3PagerWrite(pNewTrunk->pDbPage); |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 4061 | if( rc!=SQLITE_OK ){ |
| 4062 | releasePage(pNewTrunk); |
drh | d3627af | 2006-12-18 18:34:51 +0000 | [diff] [blame] | 4063 | goto end_allocate_page; |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 4064 | } |
| 4065 | memcpy(&pNewTrunk->aData[0], &pTrunk->aData[0], 4); |
| 4066 | put4byte(&pNewTrunk->aData[4], k-1); |
| 4067 | memcpy(&pNewTrunk->aData[8], &pTrunk->aData[12], (k-1)*4); |
drh | d3627af | 2006-12-18 18:34:51 +0000 | [diff] [blame] | 4068 | releasePage(pNewTrunk); |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 4069 | if( !pPrevTrunk ){ |
| 4070 | put4byte(&pPage1->aData[32], iNewTrunk); |
| 4071 | }else{ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 4072 | rc = sqlite3PagerWrite(pPrevTrunk->pDbPage); |
drh | d3627af | 2006-12-18 18:34:51 +0000 | [diff] [blame] | 4073 | if( rc ){ |
| 4074 | goto end_allocate_page; |
| 4075 | } |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 4076 | put4byte(&pPrevTrunk->aData[0], iNewTrunk); |
| 4077 | } |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 4078 | } |
| 4079 | pTrunk = 0; |
| 4080 | TRACE(("ALLOCATE: %d trunk - %d free pages left\n", *pPgno, n-1)); |
| 4081 | #endif |
| 4082 | }else{ |
| 4083 | /* Extract a leaf from the trunk */ |
| 4084 | int closest; |
| 4085 | Pgno iPage; |
| 4086 | unsigned char *aData = pTrunk->aData; |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 4087 | rc = sqlite3PagerWrite(pTrunk->pDbPage); |
drh | d3627af | 2006-12-18 18:34:51 +0000 | [diff] [blame] | 4088 | if( rc ){ |
| 4089 | goto end_allocate_page; |
| 4090 | } |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 4091 | if( nearby>0 ){ |
| 4092 | int i, dist; |
| 4093 | closest = 0; |
| 4094 | dist = get4byte(&aData[8]) - nearby; |
| 4095 | if( dist<0 ) dist = -dist; |
| 4096 | for(i=1; i<k; i++){ |
| 4097 | int d2 = get4byte(&aData[8+i*4]) - nearby; |
| 4098 | if( d2<0 ) d2 = -d2; |
| 4099 | if( d2<dist ){ |
| 4100 | closest = i; |
| 4101 | dist = d2; |
| 4102 | } |
| 4103 | } |
| 4104 | }else{ |
| 4105 | closest = 0; |
| 4106 | } |
| 4107 | |
| 4108 | iPage = get4byte(&aData[8+closest*4]); |
| 4109 | if( !searchList || iPage==nearby ){ |
| 4110 | *pPgno = iPage; |
danielk1977 | ad0132d | 2008-06-07 08:58:22 +0000 | [diff] [blame^] | 4111 | int nPage; |
| 4112 | nPage = pagerPagecount(pBt->pPager); |
| 4113 | if( *pPgno>nPage ){ |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 4114 | /* Free page off the end of the file */ |
danielk1977 | 43e377a | 2008-05-05 12:09:32 +0000 | [diff] [blame] | 4115 | rc = SQLITE_CORRUPT_BKPT; |
| 4116 | goto end_allocate_page; |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 4117 | } |
| 4118 | TRACE(("ALLOCATE: %d was leaf %d of %d on trunk %d" |
| 4119 | ": %d more free pages\n", |
| 4120 | *pPgno, closest+1, k, pTrunk->pgno, n-1)); |
| 4121 | if( closest<k-1 ){ |
| 4122 | memcpy(&aData[8+closest*4], &aData[4+k*4], 4); |
| 4123 | } |
| 4124 | put4byte(&aData[4], k-1); |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 4125 | rc = sqlite3BtreeGetPage(pBt, *pPgno, ppPage, 1); |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 4126 | if( rc==SQLITE_OK ){ |
drh | 538f570 | 2007-04-13 02:14:30 +0000 | [diff] [blame] | 4127 | sqlite3PagerDontRollback((*ppPage)->pDbPage); |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 4128 | rc = sqlite3PagerWrite((*ppPage)->pDbPage); |
danielk1977 | aac0a38 | 2005-01-16 11:07:06 +0000 | [diff] [blame] | 4129 | if( rc!=SQLITE_OK ){ |
| 4130 | releasePage(*ppPage); |
| 4131 | } |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 4132 | } |
| 4133 | searchList = 0; |
| 4134 | } |
drh | ee696e2 | 2004-08-30 16:52:17 +0000 | [diff] [blame] | 4135 | } |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 4136 | releasePage(pPrevTrunk); |
drh | d3627af | 2006-12-18 18:34:51 +0000 | [diff] [blame] | 4137 | pPrevTrunk = 0; |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 4138 | }while( searchList ); |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 4139 | }else{ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 4140 | /* There are no pages on the freelist, so create a new page at the |
| 4141 | ** end of the file */ |
danielk1977 | ad0132d | 2008-06-07 08:58:22 +0000 | [diff] [blame^] | 4142 | int nPage = pagerPagecount(pBt->pPager); |
| 4143 | *pPgno = nPage + 1; |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 4144 | |
| 4145 | #ifndef SQLITE_OMIT_AUTOVACUUM |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 4146 | if( pBt->nTrunc ){ |
| 4147 | /* An incr-vacuum has already run within this transaction. So the |
| 4148 | ** page to allocate is not from the physical end of the file, but |
| 4149 | ** at pBt->nTrunc. |
| 4150 | */ |
| 4151 | *pPgno = pBt->nTrunc+1; |
| 4152 | if( *pPgno==PENDING_BYTE_PAGE(pBt) ){ |
| 4153 | (*pPgno)++; |
| 4154 | } |
| 4155 | } |
danielk1977 | 266664d | 2006-02-10 08:24:21 +0000 | [diff] [blame] | 4156 | if( pBt->autoVacuum && PTRMAP_ISPAGE(pBt, *pPgno) ){ |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 4157 | /* If *pPgno refers to a pointer-map page, allocate two new pages |
| 4158 | ** at the end of the file instead of one. The first allocated page |
| 4159 | ** becomes a new pointer-map page, the second is used by the caller. |
| 4160 | */ |
| 4161 | TRACE(("ALLOCATE: %d from end of file (pointer-map page)\n", *pPgno)); |
danielk1977 | 599fcba | 2004-11-08 07:13:13 +0000 | [diff] [blame] | 4162 | assert( *pPgno!=PENDING_BYTE_PAGE(pBt) ); |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 4163 | (*pPgno)++; |
drh | 7219043 | 2008-01-31 14:54:43 +0000 | [diff] [blame] | 4164 | if( *pPgno==PENDING_BYTE_PAGE(pBt) ){ (*pPgno)++; } |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 4165 | } |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 4166 | if( pBt->nTrunc ){ |
| 4167 | pBt->nTrunc = *pPgno; |
| 4168 | } |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 4169 | #endif |
| 4170 | |
danielk1977 | 599fcba | 2004-11-08 07:13:13 +0000 | [diff] [blame] | 4171 | assert( *pPgno!=PENDING_BYTE_PAGE(pBt) ); |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 4172 | rc = sqlite3BtreeGetPage(pBt, *pPgno, ppPage, 0); |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 4173 | if( rc ) return rc; |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 4174 | rc = sqlite3PagerWrite((*ppPage)->pDbPage); |
danielk1977 | aac0a38 | 2005-01-16 11:07:06 +0000 | [diff] [blame] | 4175 | if( rc!=SQLITE_OK ){ |
| 4176 | releasePage(*ppPage); |
| 4177 | } |
drh | 3a4c141 | 2004-05-09 20:40:11 +0000 | [diff] [blame] | 4178 | TRACE(("ALLOCATE: %d from end of file\n", *pPgno)); |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 4179 | } |
danielk1977 | 599fcba | 2004-11-08 07:13:13 +0000 | [diff] [blame] | 4180 | |
| 4181 | assert( *pPgno!=PENDING_BYTE_PAGE(pBt) ); |
drh | d3627af | 2006-12-18 18:34:51 +0000 | [diff] [blame] | 4182 | |
| 4183 | end_allocate_page: |
| 4184 | releasePage(pTrunk); |
| 4185 | releasePage(pPrevTrunk); |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 4186 | return rc; |
| 4187 | } |
| 4188 | |
| 4189 | /* |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 4190 | ** Add a page of the database file to the freelist. |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 4191 | ** |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 4192 | ** sqlite3PagerUnref() is NOT called for pPage. |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 4193 | */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 4194 | static int freePage(MemPage *pPage){ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 4195 | BtShared *pBt = pPage->pBt; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 4196 | MemPage *pPage1 = pBt->pPage1; |
| 4197 | int rc, n, k; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 4198 | |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 4199 | /* Prepare the page for freeing */ |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 4200 | assert( sqlite3_mutex_held(pPage->pBt->mutex) ); |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 4201 | assert( pPage->pgno>1 ); |
| 4202 | pPage->isInit = 0; |
| 4203 | releasePage(pPage->pParent); |
| 4204 | pPage->pParent = 0; |
| 4205 | |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 4206 | /* Increment the free page count on pPage1 */ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 4207 | rc = sqlite3PagerWrite(pPage1->pDbPage); |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 4208 | if( rc ) return rc; |
| 4209 | n = get4byte(&pPage1->aData[36]); |
| 4210 | put4byte(&pPage1->aData[36], n+1); |
| 4211 | |
drh | fcce93f | 2006-02-22 03:08:32 +0000 | [diff] [blame] | 4212 | #ifdef SQLITE_SECURE_DELETE |
| 4213 | /* If the SQLITE_SECURE_DELETE compile-time option is enabled, then |
| 4214 | ** always fully overwrite deleted information with zeros. |
| 4215 | */ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 4216 | rc = sqlite3PagerWrite(pPage->pDbPage); |
drh | fcce93f | 2006-02-22 03:08:32 +0000 | [diff] [blame] | 4217 | if( rc ) return rc; |
| 4218 | memset(pPage->aData, 0, pPage->pBt->pageSize); |
| 4219 | #endif |
| 4220 | |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 4221 | #ifndef SQLITE_OMIT_AUTOVACUUM |
| 4222 | /* If the database supports auto-vacuum, write an entry in the pointer-map |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 4223 | ** to indicate that the page is free. |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 4224 | */ |
| 4225 | if( pBt->autoVacuum ){ |
| 4226 | rc = ptrmapPut(pBt, pPage->pgno, PTRMAP_FREEPAGE, 0); |
danielk1977 | a64a035 | 2004-11-05 01:45:13 +0000 | [diff] [blame] | 4227 | if( rc ) return rc; |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 4228 | } |
| 4229 | #endif |
| 4230 | |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 4231 | if( n==0 ){ |
| 4232 | /* This is the first free page */ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 4233 | rc = sqlite3PagerWrite(pPage->pDbPage); |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 4234 | if( rc ) return rc; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 4235 | memset(pPage->aData, 0, 8); |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 4236 | put4byte(&pPage1->aData[32], pPage->pgno); |
drh | 3a4c141 | 2004-05-09 20:40:11 +0000 | [diff] [blame] | 4237 | TRACE(("FREE-PAGE: %d first\n", pPage->pgno)); |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 4238 | }else{ |
| 4239 | /* Other free pages already exist. Retrive the first trunk page |
| 4240 | ** of the freelist and find out how many leaves it has. */ |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 4241 | MemPage *pTrunk; |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 4242 | rc = sqlite3BtreeGetPage(pBt, get4byte(&pPage1->aData[32]), &pTrunk, 0); |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 4243 | if( rc ) return rc; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 4244 | k = get4byte(&pTrunk->aData[4]); |
drh | ee696e2 | 2004-08-30 16:52:17 +0000 | [diff] [blame] | 4245 | if( k>=pBt->usableSize/4 - 8 ){ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 4246 | /* The trunk is full. Turn the page being freed into a new |
| 4247 | ** trunk page with no leaves. */ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 4248 | rc = sqlite3PagerWrite(pPage->pDbPage); |
drh | b9ee493 | 2007-09-07 14:32:06 +0000 | [diff] [blame] | 4249 | if( rc==SQLITE_OK ){ |
| 4250 | put4byte(pPage->aData, pTrunk->pgno); |
| 4251 | put4byte(&pPage->aData[4], 0); |
| 4252 | put4byte(&pPage1->aData[32], pPage->pgno); |
| 4253 | TRACE(("FREE-PAGE: %d new trunk page replacing %d\n", |
| 4254 | pPage->pgno, pTrunk->pgno)); |
| 4255 | } |
| 4256 | }else if( k<0 ){ |
| 4257 | rc = SQLITE_CORRUPT; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 4258 | }else{ |
| 4259 | /* Add the newly freed page as a leaf on the current trunk */ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 4260 | rc = sqlite3PagerWrite(pTrunk->pDbPage); |
drh | f534544 | 2007-04-09 12:45:02 +0000 | [diff] [blame] | 4261 | if( rc==SQLITE_OK ){ |
| 4262 | put4byte(&pTrunk->aData[4], k+1); |
| 4263 | put4byte(&pTrunk->aData[8+k*4], pPage->pgno); |
drh | fcce93f | 2006-02-22 03:08:32 +0000 | [diff] [blame] | 4264 | #ifndef SQLITE_SECURE_DELETE |
drh | 538f570 | 2007-04-13 02:14:30 +0000 | [diff] [blame] | 4265 | sqlite3PagerDontWrite(pPage->pDbPage); |
drh | fcce93f | 2006-02-22 03:08:32 +0000 | [diff] [blame] | 4266 | #endif |
drh | f534544 | 2007-04-09 12:45:02 +0000 | [diff] [blame] | 4267 | } |
drh | 3a4c141 | 2004-05-09 20:40:11 +0000 | [diff] [blame] | 4268 | 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] | 4269 | } |
| 4270 | releasePage(pTrunk); |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 4271 | } |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 4272 | return rc; |
| 4273 | } |
| 4274 | |
| 4275 | /* |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 4276 | ** Free any overflow pages associated with the given Cell. |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 4277 | */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 4278 | static int clearCell(MemPage *pPage, unsigned char *pCell){ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 4279 | BtShared *pBt = pPage->pBt; |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 4280 | CellInfo info; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 4281 | Pgno ovflPgno; |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 4282 | int rc; |
drh | 9444081 | 2007-03-06 11:42:19 +0000 | [diff] [blame] | 4283 | int nOvfl; |
| 4284 | int ovflPageSize; |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 4285 | |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 4286 | assert( sqlite3_mutex_held(pPage->pBt->mutex) ); |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 4287 | sqlite3BtreeParseCellPtr(pPage, pCell, &info); |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 4288 | if( info.iOverflow==0 ){ |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 4289 | return SQLITE_OK; /* No overflow pages. Return without doing anything */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 4290 | } |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 4291 | ovflPgno = get4byte(&pCell[info.iOverflow]); |
drh | 9444081 | 2007-03-06 11:42:19 +0000 | [diff] [blame] | 4292 | ovflPageSize = pBt->usableSize - 4; |
drh | 7236583 | 2007-03-06 15:53:44 +0000 | [diff] [blame] | 4293 | nOvfl = (info.nPayload - info.nLocal + ovflPageSize - 1)/ovflPageSize; |
| 4294 | assert( ovflPgno==0 || nOvfl>0 ); |
| 4295 | while( nOvfl-- ){ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 4296 | MemPage *pOvfl; |
danielk1977 | ad0132d | 2008-06-07 08:58:22 +0000 | [diff] [blame^] | 4297 | if( ovflPgno==0 || ovflPgno>pagerPagecount(pBt->pPager) ){ |
drh | 4928570 | 2005-09-17 15:20:26 +0000 | [diff] [blame] | 4298 | return SQLITE_CORRUPT_BKPT; |
danielk1977 | a1cb183 | 2005-02-12 08:59:55 +0000 | [diff] [blame] | 4299 | } |
danielk1977 | 8c0a959 | 2007-04-30 16:55:00 +0000 | [diff] [blame] | 4300 | |
| 4301 | rc = getOverflowPage(pBt, ovflPgno, &pOvfl, (nOvfl==0)?0:&ovflPgno); |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 4302 | if( rc ) return rc; |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 4303 | rc = freePage(pOvfl); |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 4304 | sqlite3PagerUnref(pOvfl->pDbPage); |
danielk1977 | 6b456a2 | 2005-03-21 04:04:02 +0000 | [diff] [blame] | 4305 | if( rc ) return rc; |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 4306 | } |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 4307 | return SQLITE_OK; |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 4308 | } |
| 4309 | |
| 4310 | /* |
drh | 9102529 | 2004-05-03 19:49:32 +0000 | [diff] [blame] | 4311 | ** Create the byte sequence used to represent a cell on page pPage |
| 4312 | ** and write that byte sequence into pCell[]. Overflow pages are |
| 4313 | ** allocated and filled in as necessary. The calling procedure |
| 4314 | ** is responsible for making sure sufficient space has been allocated |
| 4315 | ** for pCell[]. |
| 4316 | ** |
| 4317 | ** Note that pCell does not necessary need to point to the pPage->aData |
| 4318 | ** area. pCell might point to some temporary storage. The cell will |
| 4319 | ** be constructed in this temporary area then copied into pPage->aData |
| 4320 | ** later. |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 4321 | */ |
| 4322 | static int fillInCell( |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 4323 | MemPage *pPage, /* The page that contains the cell */ |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 4324 | unsigned char *pCell, /* Complete text of the cell */ |
drh | 4a1c380 | 2004-05-12 15:15:47 +0000 | [diff] [blame] | 4325 | const void *pKey, i64 nKey, /* The key */ |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 4326 | const void *pData,int nData, /* The data */ |
drh | b026e05 | 2007-05-02 01:34:31 +0000 | [diff] [blame] | 4327 | int nZero, /* Extra zero bytes to append to pData */ |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 4328 | int *pnSize /* Write cell size here */ |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 4329 | ){ |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 4330 | int nPayload; |
drh | 8c6fa9b | 2004-05-26 00:01:53 +0000 | [diff] [blame] | 4331 | const u8 *pSrc; |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 4332 | int nSrc, n, rc; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 4333 | int spaceLeft; |
| 4334 | MemPage *pOvfl = 0; |
drh | 9b17127 | 2004-05-08 02:03:22 +0000 | [diff] [blame] | 4335 | MemPage *pToRelease = 0; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 4336 | unsigned char *pPrior; |
| 4337 | unsigned char *pPayload; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 4338 | BtShared *pBt = pPage->pBt; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 4339 | Pgno pgnoOvfl = 0; |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 4340 | int nHeader; |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 4341 | CellInfo info; |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 4342 | |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 4343 | assert( sqlite3_mutex_held(pPage->pBt->mutex) ); |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 4344 | |
drh | 9102529 | 2004-05-03 19:49:32 +0000 | [diff] [blame] | 4345 | /* Fill in the header. */ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4346 | nHeader = 0; |
drh | 9102529 | 2004-05-03 19:49:32 +0000 | [diff] [blame] | 4347 | if( !pPage->leaf ){ |
| 4348 | nHeader += 4; |
| 4349 | } |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 4350 | if( pPage->hasData ){ |
drh | b026e05 | 2007-05-02 01:34:31 +0000 | [diff] [blame] | 4351 | nHeader += putVarint(&pCell[nHeader], nData+nZero); |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 4352 | }else{ |
drh | b026e05 | 2007-05-02 01:34:31 +0000 | [diff] [blame] | 4353 | nData = nZero = 0; |
drh | 9102529 | 2004-05-03 19:49:32 +0000 | [diff] [blame] | 4354 | } |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 4355 | nHeader += putVarint(&pCell[nHeader], *(u64*)&nKey); |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 4356 | sqlite3BtreeParseCellPtr(pPage, pCell, &info); |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 4357 | assert( info.nHeader==nHeader ); |
| 4358 | assert( info.nKey==nKey ); |
drh | b026e05 | 2007-05-02 01:34:31 +0000 | [diff] [blame] | 4359 | assert( info.nData==nData+nZero ); |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 4360 | |
| 4361 | /* Fill in the payload */ |
drh | b026e05 | 2007-05-02 01:34:31 +0000 | [diff] [blame] | 4362 | nPayload = nData + nZero; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 4363 | if( pPage->intKey ){ |
| 4364 | pSrc = pData; |
| 4365 | nSrc = nData; |
drh | 9102529 | 2004-05-03 19:49:32 +0000 | [diff] [blame] | 4366 | nData = 0; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 4367 | }else{ |
| 4368 | nPayload += nKey; |
| 4369 | pSrc = pKey; |
| 4370 | nSrc = nKey; |
| 4371 | } |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 4372 | *pnSize = info.nSize; |
| 4373 | spaceLeft = info.nLocal; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 4374 | pPayload = &pCell[nHeader]; |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 4375 | pPrior = &pCell[info.iOverflow]; |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 4376 | |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 4377 | while( nPayload>0 ){ |
| 4378 | if( spaceLeft==0 ){ |
danielk1977 | b39f70b | 2007-05-17 18:28:11 +0000 | [diff] [blame] | 4379 | int isExact = 0; |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 4380 | #ifndef SQLITE_OMIT_AUTOVACUUM |
| 4381 | Pgno pgnoPtrmap = pgnoOvfl; /* Overflow page pointer-map entry page */ |
danielk1977 | b39f70b | 2007-05-17 18:28:11 +0000 | [diff] [blame] | 4382 | if( pBt->autoVacuum ){ |
| 4383 | do{ |
| 4384 | pgnoOvfl++; |
| 4385 | } while( |
| 4386 | PTRMAP_ISPAGE(pBt, pgnoOvfl) || pgnoOvfl==PENDING_BYTE_PAGE(pBt) |
| 4387 | ); |
danielk1977 | 89a4be8 | 2007-05-23 13:34:32 +0000 | [diff] [blame] | 4388 | if( pgnoOvfl>1 ){ |
danielk1977 | b39f70b | 2007-05-17 18:28:11 +0000 | [diff] [blame] | 4389 | /* isExact = 1; */ |
| 4390 | } |
| 4391 | } |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 4392 | #endif |
danielk1977 | b39f70b | 2007-05-17 18:28:11 +0000 | [diff] [blame] | 4393 | rc = allocateBtreePage(pBt, &pOvfl, &pgnoOvfl, pgnoOvfl, isExact); |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 4394 | #ifndef SQLITE_OMIT_AUTOVACUUM |
danielk1977 | a19df67 | 2004-11-03 11:37:07 +0000 | [diff] [blame] | 4395 | /* If the database supports auto-vacuum, and the second or subsequent |
| 4396 | ** overflow page is being allocated, add an entry to the pointer-map |
danielk1977 | 4ef2449 | 2007-05-23 09:52:41 +0000 | [diff] [blame] | 4397 | ** for that page now. |
| 4398 | ** |
| 4399 | ** If this is the first overflow page, then write a partial entry |
| 4400 | ** to the pointer-map. If we write nothing to this pointer-map slot, |
| 4401 | ** then the optimistic overflow chain processing in clearCell() |
| 4402 | ** may misinterpret the uninitialised values and delete the |
| 4403 | ** wrong pages from the database. |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 4404 | */ |
danielk1977 | 4ef2449 | 2007-05-23 09:52:41 +0000 | [diff] [blame] | 4405 | if( pBt->autoVacuum && rc==SQLITE_OK ){ |
| 4406 | u8 eType = (pgnoPtrmap?PTRMAP_OVERFLOW2:PTRMAP_OVERFLOW1); |
| 4407 | rc = ptrmapPut(pBt, pgnoOvfl, eType, pgnoPtrmap); |
danielk1977 | 89a4be8 | 2007-05-23 13:34:32 +0000 | [diff] [blame] | 4408 | if( rc ){ |
| 4409 | releasePage(pOvfl); |
| 4410 | } |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 4411 | } |
| 4412 | #endif |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 4413 | if( rc ){ |
drh | 9b17127 | 2004-05-08 02:03:22 +0000 | [diff] [blame] | 4414 | releasePage(pToRelease); |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 4415 | return rc; |
| 4416 | } |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 4417 | put4byte(pPrior, pgnoOvfl); |
drh | 9b17127 | 2004-05-08 02:03:22 +0000 | [diff] [blame] | 4418 | releasePage(pToRelease); |
| 4419 | pToRelease = pOvfl; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 4420 | pPrior = pOvfl->aData; |
| 4421 | put4byte(pPrior, 0); |
| 4422 | pPayload = &pOvfl->aData[4]; |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 4423 | spaceLeft = pBt->usableSize - 4; |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 4424 | } |
| 4425 | n = nPayload; |
| 4426 | if( n>spaceLeft ) n = spaceLeft; |
drh | b026e05 | 2007-05-02 01:34:31 +0000 | [diff] [blame] | 4427 | if( nSrc>0 ){ |
| 4428 | if( n>nSrc ) n = nSrc; |
| 4429 | assert( pSrc ); |
| 4430 | memcpy(pPayload, pSrc, n); |
| 4431 | }else{ |
| 4432 | memset(pPayload, 0, n); |
| 4433 | } |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 4434 | nPayload -= n; |
drh | de64713 | 2004-05-07 17:57:49 +0000 | [diff] [blame] | 4435 | pPayload += n; |
drh | 9b17127 | 2004-05-08 02:03:22 +0000 | [diff] [blame] | 4436 | pSrc += n; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 4437 | nSrc -= n; |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 4438 | spaceLeft -= n; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 4439 | if( nSrc==0 ){ |
| 4440 | nSrc = nData; |
| 4441 | pSrc = pData; |
| 4442 | } |
drh | dd79342 | 2001-06-28 01:54:48 +0000 | [diff] [blame] | 4443 | } |
drh | 9b17127 | 2004-05-08 02:03:22 +0000 | [diff] [blame] | 4444 | releasePage(pToRelease); |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 4445 | return SQLITE_OK; |
| 4446 | } |
| 4447 | |
| 4448 | /* |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 4449 | ** Change the MemPage.pParent pointer on the page whose number is |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 4450 | ** given in the second argument so that MemPage.pParent holds the |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 4451 | ** pointer in the third argument. |
| 4452 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 4453 | static int reparentPage(BtShared *pBt, Pgno pgno, MemPage *pNewParent, int idx){ |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 4454 | MemPage *pThis; |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 4455 | DbPage *pDbPage; |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 4456 | |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 4457 | assert( sqlite3_mutex_held(pBt->mutex) ); |
drh | 43617e9 | 2006-03-06 20:55:46 +0000 | [diff] [blame] | 4458 | assert( pNewParent!=0 ); |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 4459 | if( pgno==0 ) return SQLITE_OK; |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 4460 | assert( pBt->pPager!=0 ); |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 4461 | pDbPage = sqlite3PagerLookup(pBt->pPager, pgno); |
| 4462 | if( pDbPage ){ |
| 4463 | pThis = (MemPage *)sqlite3PagerGetExtra(pDbPage); |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 4464 | if( pThis->isInit ){ |
drh | bf4bca5 | 2007-09-06 22:19:14 +0000 | [diff] [blame] | 4465 | assert( pThis->aData==sqlite3PagerGetData(pDbPage) ); |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 4466 | if( pThis->pParent!=pNewParent ){ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 4467 | if( pThis->pParent ) sqlite3PagerUnref(pThis->pParent->pDbPage); |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 4468 | pThis->pParent = pNewParent; |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 4469 | sqlite3PagerRef(pNewParent->pDbPage); |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 4470 | } |
| 4471 | pThis->idxParent = idx; |
drh | dd79342 | 2001-06-28 01:54:48 +0000 | [diff] [blame] | 4472 | } |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 4473 | sqlite3PagerUnref(pDbPage); |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 4474 | } |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 4475 | |
| 4476 | #ifndef SQLITE_OMIT_AUTOVACUUM |
| 4477 | if( pBt->autoVacuum ){ |
| 4478 | return ptrmapPut(pBt, pgno, PTRMAP_BTREE, pNewParent->pgno); |
| 4479 | } |
| 4480 | #endif |
| 4481 | return SQLITE_OK; |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 4482 | } |
| 4483 | |
danielk1977 | ac11ee6 | 2005-01-15 12:45:51 +0000 | [diff] [blame] | 4484 | |
| 4485 | |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 4486 | /* |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 4487 | ** Change the pParent pointer of all children of pPage to point back |
| 4488 | ** to pPage. |
| 4489 | ** |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 4490 | ** In other words, for every child of pPage, invoke reparentPage() |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4491 | ** to make sure that each child knows that pPage is its parent. |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 4492 | ** |
| 4493 | ** This routine gets called after you memcpy() one page into |
| 4494 | ** another. |
| 4495 | */ |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 4496 | static int reparentChildPages(MemPage *pPage){ |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 4497 | int i; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 4498 | BtShared *pBt = pPage->pBt; |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 4499 | int rc = SQLITE_OK; |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 4500 | |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 4501 | assert( sqlite3_mutex_held(pPage->pBt->mutex) ); |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 4502 | if( pPage->leaf ) return SQLITE_OK; |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 4503 | |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 4504 | for(i=0; i<pPage->nCell; i++){ |
danielk1977 | 1cc5ed8 | 2007-05-16 17:28:43 +0000 | [diff] [blame] | 4505 | u8 *pCell = findCell(pPage, i); |
drh | cfc2e7f | 2008-03-23 00:20:36 +0000 | [diff] [blame] | 4506 | rc = reparentPage(pBt, get4byte(pCell), pPage, i); |
| 4507 | if( rc!=SQLITE_OK ) return rc; |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 4508 | } |
drh | cfc2e7f | 2008-03-23 00:20:36 +0000 | [diff] [blame] | 4509 | rc = reparentPage(pBt, get4byte(&pPage->aData[pPage->hdrOffset+8]), |
| 4510 | pPage, i); |
| 4511 | pPage->idxShift = 0; |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 4512 | return rc; |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 4513 | } |
| 4514 | |
| 4515 | /* |
| 4516 | ** Remove the i-th cell from pPage. This routine effects pPage only. |
| 4517 | ** The cell content is not freed or deallocated. It is assumed that |
| 4518 | ** the cell content has been copied someplace else. This routine just |
| 4519 | ** removes the reference to the cell from pPage. |
| 4520 | ** |
| 4521 | ** "sz" must be the number of bytes in the cell. |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 4522 | */ |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 4523 | static void dropCell(MemPage *pPage, int idx, int sz){ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4524 | int i; /* Loop counter */ |
| 4525 | int pc; /* Offset to cell content of cell being deleted */ |
| 4526 | u8 *data; /* pPage->aData */ |
| 4527 | u8 *ptr; /* Used to move bytes around within data[] */ |
| 4528 | |
drh | 8c42ca9 | 2001-06-22 19:15:00 +0000 | [diff] [blame] | 4529 | assert( idx>=0 && idx<pPage->nCell ); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4530 | assert( sz==cellSize(pPage, idx) ); |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 4531 | assert( sqlite3PagerIswriteable(pPage->pDbPage) ); |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 4532 | assert( sqlite3_mutex_held(pPage->pBt->mutex) ); |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 4533 | data = pPage->aData; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4534 | ptr = &data[pPage->cellOffset + 2*idx]; |
| 4535 | pc = get2byte(ptr); |
| 4536 | assert( pc>10 && pc+sz<=pPage->pBt->usableSize ); |
drh | de64713 | 2004-05-07 17:57:49 +0000 | [diff] [blame] | 4537 | freeSpace(pPage, pc, sz); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4538 | for(i=idx+1; i<pPage->nCell; i++, ptr+=2){ |
| 4539 | ptr[0] = ptr[2]; |
| 4540 | ptr[1] = ptr[3]; |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 4541 | } |
| 4542 | pPage->nCell--; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4543 | put2byte(&data[pPage->hdrOffset+3], pPage->nCell); |
| 4544 | pPage->nFree += 2; |
drh | 428ae8c | 2003-01-04 16:48:09 +0000 | [diff] [blame] | 4545 | pPage->idxShift = 1; |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 4546 | } |
| 4547 | |
| 4548 | /* |
| 4549 | ** Insert a new cell on pPage at cell index "i". pCell points to the |
| 4550 | ** content of the cell. |
| 4551 | ** |
| 4552 | ** If the cell content will fit on the page, then put it there. If it |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4553 | ** will not fit, then make a copy of the cell content into pTemp if |
| 4554 | ** pTemp is not null. Regardless of pTemp, allocate a new entry |
| 4555 | ** in pPage->aOvfl[] and make it point to the cell content (either |
| 4556 | ** in pTemp or the original pCell) and also record its index. |
| 4557 | ** Allocating a new entry in pPage->aCell[] implies that |
| 4558 | ** pPage->nOverflow is incremented. |
danielk1977 | a3ad5e7 | 2005-01-07 08:56:44 +0000 | [diff] [blame] | 4559 | ** |
| 4560 | ** If nSkip is non-zero, then do not copy the first nSkip bytes of the |
| 4561 | ** cell. The caller will overwrite them after this function returns. If |
drh | 4b238df | 2005-01-08 15:43:18 +0000 | [diff] [blame] | 4562 | ** 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] | 4563 | ** (but pCell+nSkip is always valid). |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 4564 | */ |
danielk1977 | e80463b | 2004-11-03 03:01:16 +0000 | [diff] [blame] | 4565 | static int insertCell( |
drh | 24cd67e | 2004-05-10 16:18:47 +0000 | [diff] [blame] | 4566 | MemPage *pPage, /* Page into which we are copying */ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4567 | int i, /* New cell becomes the i-th cell of the page */ |
| 4568 | u8 *pCell, /* Content of the new cell */ |
| 4569 | int sz, /* Bytes of content in pCell */ |
danielk1977 | a3ad5e7 | 2005-01-07 08:56:44 +0000 | [diff] [blame] | 4570 | u8 *pTemp, /* Temp storage space for pCell, if needed */ |
| 4571 | u8 nSkip /* Do not write the first nSkip bytes of the cell */ |
drh | 24cd67e | 2004-05-10 16:18:47 +0000 | [diff] [blame] | 4572 | ){ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4573 | int idx; /* Where to write new cell content in data[] */ |
| 4574 | int j; /* Loop counter */ |
| 4575 | int top; /* First byte of content for any cell in data[] */ |
| 4576 | int end; /* First byte past the last cell pointer in data[] */ |
| 4577 | int ins; /* Index in data[] where new cell pointer is inserted */ |
| 4578 | int hdr; /* Offset into data[] of the page header */ |
| 4579 | int cellOffset; /* Address of first cell pointer in data[] */ |
| 4580 | u8 *data; /* The content of the whole page */ |
| 4581 | u8 *ptr; /* Used for moving information around in data[] */ |
| 4582 | |
| 4583 | assert( i>=0 && i<=pPage->nCell+pPage->nOverflow ); |
| 4584 | assert( sz==cellSizePtr(pPage, pCell) ); |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 4585 | assert( sqlite3_mutex_held(pPage->pBt->mutex) ); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4586 | if( pPage->nOverflow || sz+2>pPage->nFree ){ |
drh | 24cd67e | 2004-05-10 16:18:47 +0000 | [diff] [blame] | 4587 | if( pTemp ){ |
danielk1977 | a3ad5e7 | 2005-01-07 08:56:44 +0000 | [diff] [blame] | 4588 | memcpy(pTemp+nSkip, pCell+nSkip, sz-nSkip); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4589 | pCell = pTemp; |
drh | 24cd67e | 2004-05-10 16:18:47 +0000 | [diff] [blame] | 4590 | } |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4591 | j = pPage->nOverflow++; |
| 4592 | assert( j<sizeof(pPage->aOvfl)/sizeof(pPage->aOvfl[0]) ); |
| 4593 | pPage->aOvfl[j].pCell = pCell; |
| 4594 | pPage->aOvfl[j].idx = i; |
| 4595 | pPage->nFree = 0; |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 4596 | }else{ |
danielk1977 | 6e465eb | 2007-08-21 13:11:00 +0000 | [diff] [blame] | 4597 | int rc = sqlite3PagerWrite(pPage->pDbPage); |
| 4598 | if( rc!=SQLITE_OK ){ |
| 4599 | return rc; |
| 4600 | } |
| 4601 | assert( sqlite3PagerIswriteable(pPage->pDbPage) ); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4602 | data = pPage->aData; |
| 4603 | hdr = pPage->hdrOffset; |
| 4604 | top = get2byte(&data[hdr+5]); |
| 4605 | cellOffset = pPage->cellOffset; |
| 4606 | end = cellOffset + 2*pPage->nCell + 2; |
| 4607 | ins = cellOffset + 2*i; |
| 4608 | if( end > top - sz ){ |
danielk1977 | 6e465eb | 2007-08-21 13:11:00 +0000 | [diff] [blame] | 4609 | rc = defragmentPage(pPage); |
danielk1977 | 6b456a2 | 2005-03-21 04:04:02 +0000 | [diff] [blame] | 4610 | if( rc!=SQLITE_OK ) return rc; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4611 | top = get2byte(&data[hdr+5]); |
| 4612 | assert( end + sz <= top ); |
| 4613 | } |
| 4614 | idx = allocateSpace(pPage, sz); |
| 4615 | assert( idx>0 ); |
| 4616 | assert( end <= get2byte(&data[hdr+5]) ); |
| 4617 | pPage->nCell++; |
| 4618 | pPage->nFree -= 2; |
danielk1977 | a3ad5e7 | 2005-01-07 08:56:44 +0000 | [diff] [blame] | 4619 | memcpy(&data[idx+nSkip], pCell+nSkip, sz-nSkip); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4620 | for(j=end-2, ptr=&data[j]; j>ins; j-=2, ptr-=2){ |
| 4621 | ptr[0] = ptr[-2]; |
| 4622 | ptr[1] = ptr[-1]; |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 4623 | } |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4624 | put2byte(&data[ins], idx); |
| 4625 | put2byte(&data[hdr+3], pPage->nCell); |
| 4626 | pPage->idxShift = 1; |
danielk1977 | a19df67 | 2004-11-03 11:37:07 +0000 | [diff] [blame] | 4627 | #ifndef SQLITE_OMIT_AUTOVACUUM |
| 4628 | if( pPage->pBt->autoVacuum ){ |
| 4629 | /* The cell may contain a pointer to an overflow page. If so, write |
| 4630 | ** the entry for the overflow page into the pointer map. |
| 4631 | */ |
| 4632 | CellInfo info; |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 4633 | sqlite3BtreeParseCellPtr(pPage, pCell, &info); |
drh | 7236583 | 2007-03-06 15:53:44 +0000 | [diff] [blame] | 4634 | assert( (info.nData+(pPage->intKey?0:info.nKey))==info.nPayload ); |
danielk1977 | a19df67 | 2004-11-03 11:37:07 +0000 | [diff] [blame] | 4635 | if( (info.nData+(pPage->intKey?0:info.nKey))>info.nLocal ){ |
| 4636 | Pgno pgnoOvfl = get4byte(&pCell[info.iOverflow]); |
danielk1977 | 6e465eb | 2007-08-21 13:11:00 +0000 | [diff] [blame] | 4637 | rc = ptrmapPut(pPage->pBt, pgnoOvfl, PTRMAP_OVERFLOW1, pPage->pgno); |
danielk1977 | a19df67 | 2004-11-03 11:37:07 +0000 | [diff] [blame] | 4638 | if( rc!=SQLITE_OK ) return rc; |
| 4639 | } |
| 4640 | } |
| 4641 | #endif |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 4642 | } |
danielk1977 | e80463b | 2004-11-03 03:01:16 +0000 | [diff] [blame] | 4643 | |
danielk1977 | e80463b | 2004-11-03 03:01:16 +0000 | [diff] [blame] | 4644 | return SQLITE_OK; |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 4645 | } |
| 4646 | |
| 4647 | /* |
drh | fa1a98a | 2004-05-14 19:08:17 +0000 | [diff] [blame] | 4648 | ** Add a list of cells to a page. The page should be initially empty. |
| 4649 | ** The cells are guaranteed to fit on the page. |
| 4650 | */ |
| 4651 | static void assemblePage( |
| 4652 | MemPage *pPage, /* The page to be assemblied */ |
| 4653 | int nCell, /* The number of cells to add to this page */ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4654 | u8 **apCell, /* Pointers to cell bodies */ |
drh | a9121e4 | 2008-02-19 14:59:35 +0000 | [diff] [blame] | 4655 | u16 *aSize /* Sizes of the cells */ |
drh | fa1a98a | 2004-05-14 19:08:17 +0000 | [diff] [blame] | 4656 | ){ |
| 4657 | int i; /* Loop counter */ |
| 4658 | int totalSize; /* Total size of all cells */ |
| 4659 | int hdr; /* Index of page header */ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4660 | int cellptr; /* Address of next cell pointer */ |
| 4661 | int cellbody; /* Address of next cell body */ |
drh | fa1a98a | 2004-05-14 19:08:17 +0000 | [diff] [blame] | 4662 | u8 *data; /* Data for the page */ |
| 4663 | |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4664 | assert( pPage->nOverflow==0 ); |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 4665 | assert( sqlite3_mutex_held(pPage->pBt->mutex) ); |
drh | fa1a98a | 2004-05-14 19:08:17 +0000 | [diff] [blame] | 4666 | totalSize = 0; |
| 4667 | for(i=0; i<nCell; i++){ |
| 4668 | totalSize += aSize[i]; |
| 4669 | } |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4670 | assert( totalSize+2*nCell<=pPage->nFree ); |
drh | fa1a98a | 2004-05-14 19:08:17 +0000 | [diff] [blame] | 4671 | assert( pPage->nCell==0 ); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4672 | cellptr = pPage->cellOffset; |
drh | fa1a98a | 2004-05-14 19:08:17 +0000 | [diff] [blame] | 4673 | data = pPage->aData; |
| 4674 | hdr = pPage->hdrOffset; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4675 | put2byte(&data[hdr+3], nCell); |
drh | 09d0deb | 2005-08-02 17:13:09 +0000 | [diff] [blame] | 4676 | if( nCell ){ |
| 4677 | cellbody = allocateSpace(pPage, totalSize); |
| 4678 | assert( cellbody>0 ); |
| 4679 | assert( pPage->nFree >= 2*nCell ); |
| 4680 | pPage->nFree -= 2*nCell; |
| 4681 | for(i=0; i<nCell; i++){ |
| 4682 | put2byte(&data[cellptr], cellbody); |
| 4683 | memcpy(&data[cellbody], apCell[i], aSize[i]); |
| 4684 | cellptr += 2; |
| 4685 | cellbody += aSize[i]; |
| 4686 | } |
| 4687 | assert( cellbody==pPage->pBt->usableSize ); |
drh | fa1a98a | 2004-05-14 19:08:17 +0000 | [diff] [blame] | 4688 | } |
| 4689 | pPage->nCell = nCell; |
drh | fa1a98a | 2004-05-14 19:08:17 +0000 | [diff] [blame] | 4690 | } |
| 4691 | |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 4692 | /* |
drh | c3b7057 | 2003-01-04 19:44:07 +0000 | [diff] [blame] | 4693 | ** The following parameters determine how many adjacent pages get involved |
| 4694 | ** in a balancing operation. NN is the number of neighbors on either side |
| 4695 | ** of the page that participate in the balancing operation. NB is the |
| 4696 | ** total number of pages that participate, including the target page and |
| 4697 | ** NN neighbors on either side. |
| 4698 | ** |
| 4699 | ** The minimum value of NN is 1 (of course). Increasing NN above 1 |
| 4700 | ** (to 2 or 3) gives a modest improvement in SELECT and DELETE performance |
| 4701 | ** in exchange for a larger degradation in INSERT and UPDATE performance. |
| 4702 | ** The value of NN appears to give the best results overall. |
| 4703 | */ |
| 4704 | #define NN 1 /* Number of neighbors on either side of pPage */ |
| 4705 | #define NB (NN*2+1) /* Total pages involved in the balance */ |
| 4706 | |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4707 | /* Forward reference */ |
danielk1977 | ac245ec | 2005-01-14 13:50:11 +0000 | [diff] [blame] | 4708 | static int balance(MemPage*, int); |
| 4709 | |
drh | 615ae55 | 2005-01-16 23:21:00 +0000 | [diff] [blame] | 4710 | #ifndef SQLITE_OMIT_QUICKBALANCE |
drh | f222e71 | 2005-01-14 22:55:49 +0000 | [diff] [blame] | 4711 | /* |
| 4712 | ** This version of balance() handles the common special case where |
| 4713 | ** a new entry is being inserted on the extreme right-end of the |
| 4714 | ** tree, in other words, when the new entry will become the largest |
| 4715 | ** entry in the tree. |
| 4716 | ** |
| 4717 | ** Instead of trying balance the 3 right-most leaf pages, just add |
| 4718 | ** a new page to the right-hand side and put the one new entry in |
| 4719 | ** that page. This leaves the right side of the tree somewhat |
| 4720 | ** unbalanced. But odds are that we will be inserting new entries |
| 4721 | ** at the end soon afterwards so the nearly empty page will quickly |
| 4722 | ** fill up. On average. |
| 4723 | ** |
| 4724 | ** pPage is the leaf page which is the right-most page in the tree. |
| 4725 | ** pParent is its parent. pPage must have a single overflow entry |
| 4726 | ** which is also the right-most entry on the page. |
| 4727 | */ |
danielk1977 | ac245ec | 2005-01-14 13:50:11 +0000 | [diff] [blame] | 4728 | static int balance_quick(MemPage *pPage, MemPage *pParent){ |
| 4729 | int rc; |
| 4730 | MemPage *pNew; |
| 4731 | Pgno pgnoNew; |
| 4732 | u8 *pCell; |
drh | a9121e4 | 2008-02-19 14:59:35 +0000 | [diff] [blame] | 4733 | u16 szCell; |
danielk1977 | ac245ec | 2005-01-14 13:50:11 +0000 | [diff] [blame] | 4734 | CellInfo info; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 4735 | BtShared *pBt = pPage->pBt; |
danielk1977 | 79a40da | 2005-01-16 08:00:01 +0000 | [diff] [blame] | 4736 | int parentIdx = pParent->nCell; /* pParent new divider cell index */ |
| 4737 | int parentSize; /* Size of new divider cell */ |
| 4738 | u8 parentCell[64]; /* Space for the new divider cell */ |
danielk1977 | ac245ec | 2005-01-14 13:50:11 +0000 | [diff] [blame] | 4739 | |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 4740 | assert( sqlite3_mutex_held(pPage->pBt->mutex) ); |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 4741 | |
danielk1977 | ac245ec | 2005-01-14 13:50:11 +0000 | [diff] [blame] | 4742 | /* Allocate a new page. Insert the overflow cell from pPage |
| 4743 | ** into it. Then remove the overflow cell from pPage. |
| 4744 | */ |
drh | 4f0c587 | 2007-03-26 22:05:01 +0000 | [diff] [blame] | 4745 | rc = allocateBtreePage(pBt, &pNew, &pgnoNew, 0, 0); |
danielk1977 | ac245ec | 2005-01-14 13:50:11 +0000 | [diff] [blame] | 4746 | if( rc!=SQLITE_OK ){ |
| 4747 | return rc; |
| 4748 | } |
| 4749 | pCell = pPage->aOvfl[0].pCell; |
| 4750 | szCell = cellSizePtr(pPage, pCell); |
| 4751 | zeroPage(pNew, pPage->aData[0]); |
| 4752 | assemblePage(pNew, 1, &pCell, &szCell); |
| 4753 | pPage->nOverflow = 0; |
| 4754 | |
danielk1977 | 79a40da | 2005-01-16 08:00:01 +0000 | [diff] [blame] | 4755 | /* Set the parent of the newly allocated page to pParent. */ |
| 4756 | pNew->pParent = pParent; |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 4757 | sqlite3PagerRef(pParent->pDbPage); |
danielk1977 | 79a40da | 2005-01-16 08:00:01 +0000 | [diff] [blame] | 4758 | |
danielk1977 | ac245ec | 2005-01-14 13:50:11 +0000 | [diff] [blame] | 4759 | /* pPage is currently the right-child of pParent. Change this |
| 4760 | ** so that the right-child is the new page allocated above and |
danielk1977 | 79a40da | 2005-01-16 08:00:01 +0000 | [diff] [blame] | 4761 | ** pPage is the next-to-right child. |
danielk1977 | ac245ec | 2005-01-14 13:50:11 +0000 | [diff] [blame] | 4762 | */ |
danielk1977 | ac11ee6 | 2005-01-15 12:45:51 +0000 | [diff] [blame] | 4763 | assert( pPage->nCell>0 ); |
danielk1977 | 1cc5ed8 | 2007-05-16 17:28:43 +0000 | [diff] [blame] | 4764 | pCell = findCell(pPage, pPage->nCell-1); |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 4765 | sqlite3BtreeParseCellPtr(pPage, pCell, &info); |
drh | b026e05 | 2007-05-02 01:34:31 +0000 | [diff] [blame] | 4766 | rc = fillInCell(pParent, parentCell, 0, info.nKey, 0, 0, 0, &parentSize); |
danielk1977 | ac245ec | 2005-01-14 13:50:11 +0000 | [diff] [blame] | 4767 | if( rc!=SQLITE_OK ){ |
danielk1977 | 79a40da | 2005-01-16 08:00:01 +0000 | [diff] [blame] | 4768 | return rc; |
danielk1977 | ac245ec | 2005-01-14 13:50:11 +0000 | [diff] [blame] | 4769 | } |
| 4770 | assert( parentSize<64 ); |
| 4771 | rc = insertCell(pParent, parentIdx, parentCell, parentSize, 0, 4); |
| 4772 | if( rc!=SQLITE_OK ){ |
danielk1977 | 79a40da | 2005-01-16 08:00:01 +0000 | [diff] [blame] | 4773 | return rc; |
danielk1977 | ac245ec | 2005-01-14 13:50:11 +0000 | [diff] [blame] | 4774 | } |
| 4775 | put4byte(findOverflowCell(pParent,parentIdx), pPage->pgno); |
| 4776 | put4byte(&pParent->aData[pParent->hdrOffset+8], pgnoNew); |
| 4777 | |
danielk1977 | 79a40da | 2005-01-16 08:00:01 +0000 | [diff] [blame] | 4778 | #ifndef SQLITE_OMIT_AUTOVACUUM |
| 4779 | /* If this is an auto-vacuum database, update the pointer map |
| 4780 | ** with entries for the new page, and any pointer from the |
| 4781 | ** cell on the page to an overflow page. |
| 4782 | */ |
danielk1977 | ac11ee6 | 2005-01-15 12:45:51 +0000 | [diff] [blame] | 4783 | if( pBt->autoVacuum ){ |
| 4784 | rc = ptrmapPut(pBt, pgnoNew, PTRMAP_BTREE, pParent->pgno); |
danielk1977 | deb403e | 2007-05-24 09:20:16 +0000 | [diff] [blame] | 4785 | if( rc==SQLITE_OK ){ |
| 4786 | rc = ptrmapPutOvfl(pNew, 0); |
danielk1977 | ac11ee6 | 2005-01-15 12:45:51 +0000 | [diff] [blame] | 4787 | } |
danielk1977 | 79a40da | 2005-01-16 08:00:01 +0000 | [diff] [blame] | 4788 | if( rc!=SQLITE_OK ){ |
danielk1977 | deb403e | 2007-05-24 09:20:16 +0000 | [diff] [blame] | 4789 | releasePage(pNew); |
danielk1977 | 79a40da | 2005-01-16 08:00:01 +0000 | [diff] [blame] | 4790 | return rc; |
danielk1977 | ac11ee6 | 2005-01-15 12:45:51 +0000 | [diff] [blame] | 4791 | } |
| 4792 | } |
danielk1977 | 79a40da | 2005-01-16 08:00:01 +0000 | [diff] [blame] | 4793 | #endif |
danielk1977 | ac11ee6 | 2005-01-15 12:45:51 +0000 | [diff] [blame] | 4794 | |
danielk1977 | 79a40da | 2005-01-16 08:00:01 +0000 | [diff] [blame] | 4795 | /* Release the reference to the new page and balance the parent page, |
| 4796 | ** in case the divider cell inserted caused it to become overfull. |
| 4797 | */ |
danielk1977 | ac245ec | 2005-01-14 13:50:11 +0000 | [diff] [blame] | 4798 | releasePage(pNew); |
| 4799 | return balance(pParent, 0); |
| 4800 | } |
drh | 615ae55 | 2005-01-16 23:21:00 +0000 | [diff] [blame] | 4801 | #endif /* SQLITE_OMIT_QUICKBALANCE */ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4802 | |
drh | c3b7057 | 2003-01-04 19:44:07 +0000 | [diff] [blame] | 4803 | /* |
drh | ab01f61 | 2004-05-22 02:55:23 +0000 | [diff] [blame] | 4804 | ** This routine redistributes Cells on pPage and up to NN*2 siblings |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 4805 | ** 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] | 4806 | ** Usually NN siblings on either side of pPage is used in the balancing, |
| 4807 | ** though more siblings might come from one side if pPage is the first |
drh | ab01f61 | 2004-05-22 02:55:23 +0000 | [diff] [blame] | 4808 | ** 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] | 4809 | ** (something which can only happen if pPage is the root page or a |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 4810 | ** child of root) then all available siblings participate in the balancing. |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 4811 | ** |
drh | 0c6cc4e | 2004-06-15 02:13:26 +0000 | [diff] [blame] | 4812 | ** The number of siblings of pPage might be increased or decreased by one or |
| 4813 | ** two in an effort to keep pages nearly full but not over full. The root page |
drh | ab01f61 | 2004-05-22 02:55:23 +0000 | [diff] [blame] | 4814 | ** is special and is allowed to be nearly empty. If pPage is |
drh | 8c42ca9 | 2001-06-22 19:15:00 +0000 | [diff] [blame] | 4815 | ** the root page, then the depth of the tree might be increased |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 4816 | ** or decreased by one, as necessary, to keep the root page from being |
drh | ab01f61 | 2004-05-22 02:55:23 +0000 | [diff] [blame] | 4817 | ** overfull or completely empty. |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 4818 | ** |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 4819 | ** Note that when this routine is called, some of the Cells on pPage |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 4820 | ** might not actually be stored in pPage->aData[]. This can happen |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 4821 | ** 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] | 4822 | ** make sure all Cells for pPage once again fit in pPage->aData[]. |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 4823 | ** |
drh | 8c42ca9 | 2001-06-22 19:15:00 +0000 | [diff] [blame] | 4824 | ** In the course of balancing the siblings of pPage, the parent of pPage |
| 4825 | ** might become overfull or underfull. If that happens, then this routine |
| 4826 | ** is called recursively on the parent. |
| 4827 | ** |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4828 | ** If this routine fails for any reason, it might leave the database |
| 4829 | ** in a corrupted state. So if this routine fails, the database should |
| 4830 | ** be rolled back. |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 4831 | */ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4832 | static int balance_nonroot(MemPage *pPage){ |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 4833 | MemPage *pParent; /* The parent of pPage */ |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 4834 | BtShared *pBt; /* The whole database */ |
danielk1977 | 634f298 | 2005-03-28 08:44:07 +0000 | [diff] [blame] | 4835 | int nCell = 0; /* Number of cells in apCell[] */ |
| 4836 | int nMaxCells = 0; /* Allocated size of apCell, szCell, aFrom. */ |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 4837 | int nOld; /* Number of pages in apOld[] */ |
| 4838 | int nNew; /* Number of pages in apNew[] */ |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 4839 | int nDiv; /* Number of cells in apDiv[] */ |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 4840 | int i, j, k; /* Loop counters */ |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 4841 | int idx; /* Index of pPage in pParent->aCell[] */ |
| 4842 | int nxDiv; /* Next divider slot in pParent->aCell[] */ |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 4843 | int rc; /* The return code */ |
drh | 9102529 | 2004-05-03 19:49:32 +0000 | [diff] [blame] | 4844 | int leafCorrection; /* 4 if pPage is a leaf. 0 if not */ |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 4845 | int leafData; /* True if pPage is a leaf of a LEAFDATA tree */ |
drh | 9102529 | 2004-05-03 19:49:32 +0000 | [diff] [blame] | 4846 | int usableSpace; /* Bytes in pPage beyond the header */ |
| 4847 | int pageFlags; /* Value of pPage->aData[0] */ |
drh | 6019e16 | 2001-07-02 17:51:45 +0000 | [diff] [blame] | 4848 | int subtotal; /* Subtotal of bytes in cells on one page */ |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 4849 | int iSpace = 0; /* First unused byte of aSpace[] */ |
drh | c3b7057 | 2003-01-04 19:44:07 +0000 | [diff] [blame] | 4850 | MemPage *apOld[NB]; /* pPage and up to two siblings */ |
| 4851 | Pgno pgnoOld[NB]; /* Page numbers for each page in apOld[] */ |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 4852 | MemPage *apCopy[NB]; /* Private copies of apOld[] pages */ |
drh | a2fce64 | 2004-06-05 00:01:44 +0000 | [diff] [blame] | 4853 | MemPage *apNew[NB+2]; /* pPage and up to NB siblings after balancing */ |
| 4854 | Pgno pgnoNew[NB+2]; /* Page numbers for each page in apNew[] */ |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 4855 | u8 *apDiv[NB]; /* Divider cells in pParent */ |
drh | a2fce64 | 2004-06-05 00:01:44 +0000 | [diff] [blame] | 4856 | int cntNew[NB+2]; /* Index in aCell[] of cell after i-th page */ |
| 4857 | int szNew[NB+2]; /* Combined size of cells place on i-th page */ |
danielk1977 | 50f059b | 2005-03-29 02:54:03 +0000 | [diff] [blame] | 4858 | u8 **apCell = 0; /* All cells begin balanced */ |
drh | a9121e4 | 2008-02-19 14:59:35 +0000 | [diff] [blame] | 4859 | u16 *szCell; /* Local size of all cells in apCell[] */ |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 4860 | u8 *aCopy[NB]; /* Space for holding data of apCopy[] */ |
| 4861 | u8 *aSpace; /* Space to hold copies of dividers cells */ |
danielk1977 | 4e17d14 | 2005-01-16 09:06:33 +0000 | [diff] [blame] | 4862 | #ifndef SQLITE_OMIT_AUTOVACUUM |
danielk1977 | ac11ee6 | 2005-01-15 12:45:51 +0000 | [diff] [blame] | 4863 | u8 *aFrom = 0; |
| 4864 | #endif |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 4865 | |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 4866 | assert( sqlite3_mutex_held(pPage->pBt->mutex) ); |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 4867 | |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 4868 | /* |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4869 | ** Find the parent page. |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 4870 | */ |
drh | 3a4c141 | 2004-05-09 20:40:11 +0000 | [diff] [blame] | 4871 | assert( pPage->isInit ); |
danielk1977 | 6e465eb | 2007-08-21 13:11:00 +0000 | [diff] [blame] | 4872 | assert( sqlite3PagerIswriteable(pPage->pDbPage) || pPage->nOverflow==1 ); |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 4873 | pBt = pPage->pBt; |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 4874 | pParent = pPage->pParent; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4875 | assert( pParent ); |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 4876 | if( SQLITE_OK!=(rc = sqlite3PagerWrite(pParent->pDbPage)) ){ |
danielk1977 | 07cb560 | 2006-01-20 10:55:05 +0000 | [diff] [blame] | 4877 | return rc; |
| 4878 | } |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4879 | TRACE(("BALANCE: begin page %d child of %d\n", pPage->pgno, pParent->pgno)); |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 4880 | |
drh | 615ae55 | 2005-01-16 23:21:00 +0000 | [diff] [blame] | 4881 | #ifndef SQLITE_OMIT_QUICKBALANCE |
drh | f222e71 | 2005-01-14 22:55:49 +0000 | [diff] [blame] | 4882 | /* |
| 4883 | ** A special case: If a new entry has just been inserted into a |
| 4884 | ** table (that is, a btree with integer keys and all data at the leaves) |
drh | 09d0deb | 2005-08-02 17:13:09 +0000 | [diff] [blame] | 4885 | ** 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] | 4886 | ** largest key) then use the special balance_quick() routine for |
| 4887 | ** balancing. balance_quick() is much faster and results in a tighter |
| 4888 | ** packing of data in the common case. |
| 4889 | */ |
danielk1977 | ac245ec | 2005-01-14 13:50:11 +0000 | [diff] [blame] | 4890 | if( pPage->leaf && |
| 4891 | pPage->intKey && |
| 4892 | pPage->leafData && |
| 4893 | pPage->nOverflow==1 && |
| 4894 | pPage->aOvfl[0].idx==pPage->nCell && |
danielk1977 | ac11ee6 | 2005-01-15 12:45:51 +0000 | [diff] [blame] | 4895 | pPage->pParent->pgno!=1 && |
danielk1977 | ac245ec | 2005-01-14 13:50:11 +0000 | [diff] [blame] | 4896 | get4byte(&pParent->aData[pParent->hdrOffset+8])==pPage->pgno |
| 4897 | ){ |
danielk1977 | ac11ee6 | 2005-01-15 12:45:51 +0000 | [diff] [blame] | 4898 | /* |
| 4899 | ** TODO: Check the siblings to the left of pPage. It may be that |
| 4900 | ** they are not full and no new page is required. |
| 4901 | */ |
danielk1977 | ac245ec | 2005-01-14 13:50:11 +0000 | [diff] [blame] | 4902 | return balance_quick(pPage, pParent); |
| 4903 | } |
| 4904 | #endif |
| 4905 | |
danielk1977 | 6e465eb | 2007-08-21 13:11:00 +0000 | [diff] [blame] | 4906 | if( SQLITE_OK!=(rc = sqlite3PagerWrite(pPage->pDbPage)) ){ |
| 4907 | return rc; |
| 4908 | } |
| 4909 | |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 4910 | /* |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 4911 | ** Find the cell in the parent page whose left child points back |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 4912 | ** to pPage. The "idx" variable is the index of that cell. If pPage |
| 4913 | ** is the rightmost child of pParent then set idx to pParent->nCell |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 4914 | */ |
drh | bb49aba | 2003-01-04 18:53:27 +0000 | [diff] [blame] | 4915 | if( pParent->idxShift ){ |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 4916 | Pgno pgno; |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 4917 | pgno = pPage->pgno; |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 4918 | assert( pgno==sqlite3PagerPagenumber(pPage->pDbPage) ); |
drh | bb49aba | 2003-01-04 18:53:27 +0000 | [diff] [blame] | 4919 | for(idx=0; idx<pParent->nCell; idx++){ |
danielk1977 | 1cc5ed8 | 2007-05-16 17:28:43 +0000 | [diff] [blame] | 4920 | if( get4byte(findCell(pParent, idx))==pgno ){ |
drh | bb49aba | 2003-01-04 18:53:27 +0000 | [diff] [blame] | 4921 | break; |
| 4922 | } |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 4923 | } |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 4924 | assert( idx<pParent->nCell |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4925 | || get4byte(&pParent->aData[pParent->hdrOffset+8])==pgno ); |
drh | bb49aba | 2003-01-04 18:53:27 +0000 | [diff] [blame] | 4926 | }else{ |
| 4927 | idx = pPage->idxParent; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 4928 | } |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 4929 | |
| 4930 | /* |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 4931 | ** Initialize variables so that it will be safe to jump |
drh | 5edc312 | 2001-09-13 21:53:09 +0000 | [diff] [blame] | 4932 | ** directly to balance_cleanup at any moment. |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 4933 | */ |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 4934 | nOld = nNew = 0; |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 4935 | sqlite3PagerRef(pParent->pDbPage); |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 4936 | |
| 4937 | /* |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 4938 | ** Find sibling pages to pPage and the cells in pParent that divide |
drh | c3b7057 | 2003-01-04 19:44:07 +0000 | [diff] [blame] | 4939 | ** the siblings. An attempt is made to find NN siblings on either |
| 4940 | ** side of pPage. More siblings are taken from one side, however, if |
| 4941 | ** pPage there are fewer than NN siblings on the other side. If pParent |
| 4942 | ** has NB or fewer children then all children of pParent are taken. |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 4943 | */ |
drh | c3b7057 | 2003-01-04 19:44:07 +0000 | [diff] [blame] | 4944 | nxDiv = idx - NN; |
| 4945 | if( nxDiv + NB > pParent->nCell ){ |
| 4946 | nxDiv = pParent->nCell - NB + 1; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 4947 | } |
drh | c3b7057 | 2003-01-04 19:44:07 +0000 | [diff] [blame] | 4948 | if( nxDiv<0 ){ |
| 4949 | nxDiv = 0; |
| 4950 | } |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 4951 | nDiv = 0; |
drh | c3b7057 | 2003-01-04 19:44:07 +0000 | [diff] [blame] | 4952 | for(i=0, k=nxDiv; i<NB; i++, k++){ |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 4953 | if( k<pParent->nCell ){ |
danielk1977 | 1cc5ed8 | 2007-05-16 17:28:43 +0000 | [diff] [blame] | 4954 | apDiv[i] = findCell(pParent, k); |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 4955 | nDiv++; |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 4956 | assert( !pParent->leaf ); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4957 | pgnoOld[i] = get4byte(apDiv[i]); |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 4958 | }else if( k==pParent->nCell ){ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4959 | pgnoOld[i] = get4byte(&pParent->aData[pParent->hdrOffset+8]); |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 4960 | }else{ |
| 4961 | break; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 4962 | } |
drh | de64713 | 2004-05-07 17:57:49 +0000 | [diff] [blame] | 4963 | rc = getAndInitPage(pBt, pgnoOld[i], &apOld[i], pParent); |
drh | 6019e16 | 2001-07-02 17:51:45 +0000 | [diff] [blame] | 4964 | if( rc ) goto balance_cleanup; |
drh | 428ae8c | 2003-01-04 16:48:09 +0000 | [diff] [blame] | 4965 | apOld[i]->idxParent = k; |
drh | 9102529 | 2004-05-03 19:49:32 +0000 | [diff] [blame] | 4966 | apCopy[i] = 0; |
| 4967 | assert( i==nOld ); |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 4968 | nOld++; |
danielk1977 | 634f298 | 2005-03-28 08:44:07 +0000 | [diff] [blame] | 4969 | nMaxCells += 1+apOld[i]->nCell+apOld[i]->nOverflow; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 4970 | } |
| 4971 | |
drh | a9121e4 | 2008-02-19 14:59:35 +0000 | [diff] [blame] | 4972 | /* Make nMaxCells a multiple of 4 in order to preserve 8-byte |
drh | 8d97f1f | 2005-05-05 18:14:13 +0000 | [diff] [blame] | 4973 | ** alignment */ |
drh | a9121e4 | 2008-02-19 14:59:35 +0000 | [diff] [blame] | 4974 | nMaxCells = (nMaxCells + 3)&~3; |
drh | 8d97f1f | 2005-05-05 18:14:13 +0000 | [diff] [blame] | 4975 | |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 4976 | /* |
danielk1977 | 634f298 | 2005-03-28 08:44:07 +0000 | [diff] [blame] | 4977 | ** Allocate space for memory structures |
| 4978 | */ |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 4979 | apCell = sqlite3_malloc( |
drh | a9121e4 | 2008-02-19 14:59:35 +0000 | [diff] [blame] | 4980 | nMaxCells*sizeof(u8*) /* apCell */ |
| 4981 | + nMaxCells*sizeof(u16) /* szCell */ |
| 4982 | + (ROUND8(sizeof(MemPage))+pBt->pageSize)*NB /* aCopy */ |
| 4983 | + pBt->pageSize*5 /* aSpace */ |
| 4984 | + (ISAUTOVACUUM ? nMaxCells : 0) /* aFrom */ |
danielk1977 | 634f298 | 2005-03-28 08:44:07 +0000 | [diff] [blame] | 4985 | ); |
| 4986 | if( apCell==0 ){ |
| 4987 | rc = SQLITE_NOMEM; |
| 4988 | goto balance_cleanup; |
| 4989 | } |
drh | a9121e4 | 2008-02-19 14:59:35 +0000 | [diff] [blame] | 4990 | szCell = (u16*)&apCell[nMaxCells]; |
danielk1977 | 634f298 | 2005-03-28 08:44:07 +0000 | [diff] [blame] | 4991 | aCopy[0] = (u8*)&szCell[nMaxCells]; |
drh | c96d853 | 2005-05-03 12:30:33 +0000 | [diff] [blame] | 4992 | assert( ((aCopy[0] - (u8*)apCell) & 7)==0 ); /* 8-byte alignment required */ |
danielk1977 | 634f298 | 2005-03-28 08:44:07 +0000 | [diff] [blame] | 4993 | for(i=1; i<NB; i++){ |
drh | c96d853 | 2005-05-03 12:30:33 +0000 | [diff] [blame] | 4994 | aCopy[i] = &aCopy[i-1][pBt->pageSize+ROUND8(sizeof(MemPage))]; |
| 4995 | assert( ((aCopy[i] - (u8*)apCell) & 7)==0 ); /* 8-byte alignment required */ |
danielk1977 | 634f298 | 2005-03-28 08:44:07 +0000 | [diff] [blame] | 4996 | } |
drh | c96d853 | 2005-05-03 12:30:33 +0000 | [diff] [blame] | 4997 | aSpace = &aCopy[NB-1][pBt->pageSize+ROUND8(sizeof(MemPage))]; |
| 4998 | assert( ((aSpace - (u8*)apCell) & 7)==0 ); /* 8-byte alignment required */ |
danielk1977 | 634f298 | 2005-03-28 08:44:07 +0000 | [diff] [blame] | 4999 | #ifndef SQLITE_OMIT_AUTOVACUUM |
| 5000 | if( pBt->autoVacuum ){ |
drh | 07d183d | 2005-05-01 22:52:42 +0000 | [diff] [blame] | 5001 | aFrom = &aSpace[5*pBt->pageSize]; |
danielk1977 | 634f298 | 2005-03-28 08:44:07 +0000 | [diff] [blame] | 5002 | } |
| 5003 | #endif |
| 5004 | |
| 5005 | /* |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 5006 | ** Make copies of the content of pPage and its siblings into aOld[]. |
| 5007 | ** The rest of this function will use data from the copies rather |
| 5008 | ** that the original pages since the original pages will be in the |
| 5009 | ** process of being overwritten. |
| 5010 | */ |
| 5011 | for(i=0; i<nOld; i++){ |
drh | bf4bca5 | 2007-09-06 22:19:14 +0000 | [diff] [blame] | 5012 | MemPage *p = apCopy[i] = (MemPage*)aCopy[i]; |
| 5013 | memcpy(p, apOld[i], sizeof(MemPage)); |
| 5014 | p->aData = (void*)&p[1]; |
| 5015 | memcpy(p->aData, apOld[i]->aData, pBt->pageSize); |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 5016 | } |
| 5017 | |
| 5018 | /* |
| 5019 | ** Load pointers to all cells on sibling pages and the divider cells |
| 5020 | ** into the local apCell[] array. Make copies of the divider cells |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 5021 | ** into space obtained form aSpace[] and remove the the divider Cells |
| 5022 | ** from pParent. |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 5023 | ** |
| 5024 | ** If the siblings are on leaf pages, then the child pointers of the |
| 5025 | ** divider cells are stripped from the cells before they are copied |
drh | 96f5b76 | 2004-05-16 16:24:36 +0000 | [diff] [blame] | 5026 | ** into aSpace[]. In this way, all cells in apCell[] are without |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 5027 | ** child pointers. If siblings are not leaves, then all cell in |
| 5028 | ** apCell[] include child pointers. Either way, all cells in apCell[] |
| 5029 | ** are alike. |
drh | 96f5b76 | 2004-05-16 16:24:36 +0000 | [diff] [blame] | 5030 | ** |
| 5031 | ** leafCorrection: 4 if pPage is a leaf. 0 if pPage is not a leaf. |
| 5032 | ** leafData: 1 if pPage holds key+data and pParent holds only keys. |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 5033 | */ |
| 5034 | nCell = 0; |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 5035 | leafCorrection = pPage->leaf*4; |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 5036 | leafData = pPage->leafData && pPage->leaf; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 5037 | for(i=0; i<nOld; i++){ |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 5038 | MemPage *pOld = apCopy[i]; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5039 | int limit = pOld->nCell+pOld->nOverflow; |
| 5040 | for(j=0; j<limit; j++){ |
danielk1977 | 634f298 | 2005-03-28 08:44:07 +0000 | [diff] [blame] | 5041 | assert( nCell<nMaxCells ); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5042 | apCell[nCell] = findOverflowCell(pOld, j); |
| 5043 | szCell[nCell] = cellSizePtr(pOld, apCell[nCell]); |
danielk1977 | ac11ee6 | 2005-01-15 12:45:51 +0000 | [diff] [blame] | 5044 | #ifndef SQLITE_OMIT_AUTOVACUUM |
| 5045 | if( pBt->autoVacuum ){ |
| 5046 | int a; |
| 5047 | aFrom[nCell] = i; |
| 5048 | for(a=0; a<pOld->nOverflow; a++){ |
| 5049 | if( pOld->aOvfl[a].pCell==apCell[nCell] ){ |
| 5050 | aFrom[nCell] = 0xFF; |
| 5051 | break; |
| 5052 | } |
| 5053 | } |
| 5054 | } |
| 5055 | #endif |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 5056 | nCell++; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 5057 | } |
| 5058 | if( i<nOld-1 ){ |
drh | a9121e4 | 2008-02-19 14:59:35 +0000 | [diff] [blame] | 5059 | u16 sz = cellSizePtr(pParent, apDiv[i]); |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 5060 | if( leafData ){ |
drh | 96f5b76 | 2004-05-16 16:24:36 +0000 | [diff] [blame] | 5061 | /* With the LEAFDATA flag, pParent cells hold only INTKEYs that |
| 5062 | ** are duplicates of keys on the child pages. We need to remove |
| 5063 | ** the divider cells from pParent, but the dividers cells are not |
| 5064 | ** added to apCell[] because they are duplicates of child cells. |
| 5065 | */ |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 5066 | dropCell(pParent, nxDiv, sz); |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 5067 | }else{ |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 5068 | u8 *pTemp; |
danielk1977 | 634f298 | 2005-03-28 08:44:07 +0000 | [diff] [blame] | 5069 | assert( nCell<nMaxCells ); |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 5070 | szCell[nCell] = sz; |
| 5071 | pTemp = &aSpace[iSpace]; |
| 5072 | iSpace += sz; |
drh | 07d183d | 2005-05-01 22:52:42 +0000 | [diff] [blame] | 5073 | assert( iSpace<=pBt->pageSize*5 ); |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 5074 | memcpy(pTemp, apDiv[i], sz); |
| 5075 | apCell[nCell] = pTemp+leafCorrection; |
danielk1977 | ac11ee6 | 2005-01-15 12:45:51 +0000 | [diff] [blame] | 5076 | #ifndef SQLITE_OMIT_AUTOVACUUM |
| 5077 | if( pBt->autoVacuum ){ |
| 5078 | aFrom[nCell] = 0xFF; |
| 5079 | } |
| 5080 | #endif |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 5081 | dropCell(pParent, nxDiv, sz); |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 5082 | szCell[nCell] -= leafCorrection; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5083 | assert( get4byte(pTemp)==pgnoOld[i] ); |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 5084 | if( !pOld->leaf ){ |
| 5085 | assert( leafCorrection==0 ); |
| 5086 | /* The right pointer of the child page pOld becomes the left |
| 5087 | ** pointer of the divider cell */ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5088 | memcpy(apCell[nCell], &pOld->aData[pOld->hdrOffset+8], 4); |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 5089 | }else{ |
| 5090 | assert( leafCorrection==4 ); |
danielk1977 | 39c9604 | 2007-05-12 10:41:47 +0000 | [diff] [blame] | 5091 | if( szCell[nCell]<4 ){ |
| 5092 | /* Do not allow any cells smaller than 4 bytes. */ |
| 5093 | szCell[nCell] = 4; |
| 5094 | } |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 5095 | } |
| 5096 | nCell++; |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 5097 | } |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 5098 | } |
| 5099 | } |
| 5100 | |
| 5101 | /* |
drh | 6019e16 | 2001-07-02 17:51:45 +0000 | [diff] [blame] | 5102 | ** Figure out the number of pages needed to hold all nCell cells. |
| 5103 | ** Store this number in "k". Also compute szNew[] which is the total |
| 5104 | ** size of all cells on the i-th page and cntNew[] which is the index |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 5105 | ** in apCell[] of the cell that divides page i from page i+1. |
drh | 6019e16 | 2001-07-02 17:51:45 +0000 | [diff] [blame] | 5106 | ** cntNew[k] should equal nCell. |
| 5107 | ** |
drh | 96f5b76 | 2004-05-16 16:24:36 +0000 | [diff] [blame] | 5108 | ** Values computed by this block: |
| 5109 | ** |
| 5110 | ** k: The total number of sibling pages |
| 5111 | ** szNew[i]: Spaced used on the i-th sibling page. |
| 5112 | ** cntNew[i]: Index in apCell[] and szCell[] for the first cell to |
| 5113 | ** the right of the i-th sibling page. |
| 5114 | ** usableSpace: Number of bytes of space available on each sibling. |
| 5115 | ** |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 5116 | */ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5117 | usableSpace = pBt->usableSize - 12 + leafCorrection; |
drh | 6019e16 | 2001-07-02 17:51:45 +0000 | [diff] [blame] | 5118 | for(subtotal=k=i=0; i<nCell; i++){ |
danielk1977 | 634f298 | 2005-03-28 08:44:07 +0000 | [diff] [blame] | 5119 | assert( i<nMaxCells ); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5120 | subtotal += szCell[i] + 2; |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 5121 | if( subtotal > usableSpace ){ |
drh | 6019e16 | 2001-07-02 17:51:45 +0000 | [diff] [blame] | 5122 | szNew[k] = subtotal - szCell[i]; |
| 5123 | cntNew[k] = i; |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 5124 | if( leafData ){ i--; } |
drh | 6019e16 | 2001-07-02 17:51:45 +0000 | [diff] [blame] | 5125 | subtotal = 0; |
| 5126 | k++; |
| 5127 | } |
| 5128 | } |
| 5129 | szNew[k] = subtotal; |
| 5130 | cntNew[k] = nCell; |
| 5131 | k++; |
drh | 96f5b76 | 2004-05-16 16:24:36 +0000 | [diff] [blame] | 5132 | |
| 5133 | /* |
| 5134 | ** The packing computed by the previous block is biased toward the siblings |
| 5135 | ** on the left side. The left siblings are always nearly full, while the |
| 5136 | ** right-most sibling might be nearly empty. This block of code attempts |
| 5137 | ** to adjust the packing of siblings to get a better balance. |
| 5138 | ** |
| 5139 | ** This adjustment is more than an optimization. The packing above might |
| 5140 | ** be so out of balance as to be illegal. For example, the right-most |
| 5141 | ** sibling might be completely empty. This adjustment is not optional. |
| 5142 | */ |
drh | 6019e16 | 2001-07-02 17:51:45 +0000 | [diff] [blame] | 5143 | for(i=k-1; i>0; i--){ |
drh | 96f5b76 | 2004-05-16 16:24:36 +0000 | [diff] [blame] | 5144 | int szRight = szNew[i]; /* Size of sibling on the right */ |
| 5145 | int szLeft = szNew[i-1]; /* Size of sibling on the left */ |
| 5146 | int r; /* Index of right-most cell in left sibling */ |
| 5147 | int d; /* Index of first cell to the left of right sibling */ |
| 5148 | |
| 5149 | r = cntNew[i-1] - 1; |
| 5150 | d = r + 1 - leafData; |
danielk1977 | 634f298 | 2005-03-28 08:44:07 +0000 | [diff] [blame] | 5151 | assert( d<nMaxCells ); |
| 5152 | assert( r<nMaxCells ); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5153 | while( szRight==0 || szRight+szCell[d]+2<=szLeft-(szCell[r]+2) ){ |
| 5154 | szRight += szCell[d] + 2; |
| 5155 | szLeft -= szCell[r] + 2; |
drh | 6019e16 | 2001-07-02 17:51:45 +0000 | [diff] [blame] | 5156 | cntNew[i-1]--; |
drh | 96f5b76 | 2004-05-16 16:24:36 +0000 | [diff] [blame] | 5157 | r = cntNew[i-1] - 1; |
| 5158 | d = r + 1 - leafData; |
drh | 6019e16 | 2001-07-02 17:51:45 +0000 | [diff] [blame] | 5159 | } |
drh | 96f5b76 | 2004-05-16 16:24:36 +0000 | [diff] [blame] | 5160 | szNew[i] = szRight; |
| 5161 | szNew[i-1] = szLeft; |
drh | 6019e16 | 2001-07-02 17:51:45 +0000 | [diff] [blame] | 5162 | } |
drh | 09d0deb | 2005-08-02 17:13:09 +0000 | [diff] [blame] | 5163 | |
| 5164 | /* Either we found one or more cells (cntnew[0])>0) or we are the |
| 5165 | ** a virtual root page. A virtual root page is when the real root |
| 5166 | ** page is page 1 and we are the only child of that page. |
| 5167 | */ |
| 5168 | assert( cntNew[0]>0 || (pParent->pgno==1 && pParent->nCell==0) ); |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 5169 | |
| 5170 | /* |
drh | 6b30867 | 2002-07-08 02:16:37 +0000 | [diff] [blame] | 5171 | ** Allocate k new pages. Reuse old pages where possible. |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 5172 | */ |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 5173 | assert( pPage->pgno>1 ); |
| 5174 | pageFlags = pPage->aData[0]; |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 5175 | for(i=0; i<k; i++){ |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 5176 | MemPage *pNew; |
drh | 6b30867 | 2002-07-08 02:16:37 +0000 | [diff] [blame] | 5177 | if( i<nOld ){ |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 5178 | pNew = apNew[i] = apOld[i]; |
drh | 6b30867 | 2002-07-08 02:16:37 +0000 | [diff] [blame] | 5179 | pgnoNew[i] = pgnoOld[i]; |
| 5180 | apOld[i] = 0; |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 5181 | rc = sqlite3PagerWrite(pNew->pDbPage); |
drh | f534544 | 2007-04-09 12:45:02 +0000 | [diff] [blame] | 5182 | nNew++; |
danielk1977 | 2812956 | 2005-01-11 10:25:06 +0000 | [diff] [blame] | 5183 | if( rc ) goto balance_cleanup; |
drh | 6b30867 | 2002-07-08 02:16:37 +0000 | [diff] [blame] | 5184 | }else{ |
drh | 7aa8f85 | 2006-03-28 00:24:44 +0000 | [diff] [blame] | 5185 | assert( i>0 ); |
drh | 4f0c587 | 2007-03-26 22:05:01 +0000 | [diff] [blame] | 5186 | rc = allocateBtreePage(pBt, &pNew, &pgnoNew[i], pgnoNew[i-1], 0); |
drh | 6b30867 | 2002-07-08 02:16:37 +0000 | [diff] [blame] | 5187 | if( rc ) goto balance_cleanup; |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 5188 | apNew[i] = pNew; |
drh | f534544 | 2007-04-09 12:45:02 +0000 | [diff] [blame] | 5189 | nNew++; |
drh | 6b30867 | 2002-07-08 02:16:37 +0000 | [diff] [blame] | 5190 | } |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 5191 | zeroPage(pNew, pageFlags); |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 5192 | } |
| 5193 | |
danielk1977 | 299b187 | 2004-11-22 10:02:10 +0000 | [diff] [blame] | 5194 | /* Free any old pages that were not reused as new pages. |
| 5195 | */ |
| 5196 | while( i<nOld ){ |
| 5197 | rc = freePage(apOld[i]); |
| 5198 | if( rc ) goto balance_cleanup; |
| 5199 | releasePage(apOld[i]); |
| 5200 | apOld[i] = 0; |
| 5201 | i++; |
| 5202 | } |
| 5203 | |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 5204 | /* |
drh | f9ffac9 | 2002-03-02 19:00:31 +0000 | [diff] [blame] | 5205 | ** Put the new pages in accending order. This helps to |
| 5206 | ** keep entries in the disk file in order so that a scan |
| 5207 | ** of the table is a linear scan through the file. That |
| 5208 | ** in turn helps the operating system to deliver pages |
| 5209 | ** from the disk more rapidly. |
| 5210 | ** |
| 5211 | ** An O(n^2) insertion sort algorithm is used, but since |
drh | c3b7057 | 2003-01-04 19:44:07 +0000 | [diff] [blame] | 5212 | ** n is never more than NB (a small constant), that should |
| 5213 | ** not be a problem. |
drh | f9ffac9 | 2002-03-02 19:00:31 +0000 | [diff] [blame] | 5214 | ** |
drh | c3b7057 | 2003-01-04 19:44:07 +0000 | [diff] [blame] | 5215 | ** When NB==3, this one optimization makes the database |
| 5216 | ** about 25% faster for large insertions and deletions. |
drh | f9ffac9 | 2002-03-02 19:00:31 +0000 | [diff] [blame] | 5217 | */ |
| 5218 | for(i=0; i<k-1; i++){ |
| 5219 | int minV = pgnoNew[i]; |
| 5220 | int minI = i; |
| 5221 | for(j=i+1; j<k; j++){ |
drh | 7d02cb7 | 2003-06-04 16:24:39 +0000 | [diff] [blame] | 5222 | if( pgnoNew[j]<(unsigned)minV ){ |
drh | f9ffac9 | 2002-03-02 19:00:31 +0000 | [diff] [blame] | 5223 | minI = j; |
| 5224 | minV = pgnoNew[j]; |
| 5225 | } |
| 5226 | } |
| 5227 | if( minI>i ){ |
| 5228 | int t; |
| 5229 | MemPage *pT; |
| 5230 | t = pgnoNew[i]; |
| 5231 | pT = apNew[i]; |
| 5232 | pgnoNew[i] = pgnoNew[minI]; |
| 5233 | apNew[i] = apNew[minI]; |
| 5234 | pgnoNew[minI] = t; |
| 5235 | apNew[minI] = pT; |
| 5236 | } |
| 5237 | } |
drh | a2fce64 | 2004-06-05 00:01:44 +0000 | [diff] [blame] | 5238 | 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] | 5239 | pgnoOld[0], |
| 5240 | nOld>=2 ? pgnoOld[1] : 0, |
| 5241 | nOld>=3 ? pgnoOld[2] : 0, |
drh | 10c0fa6 | 2004-05-18 12:50:17 +0000 | [diff] [blame] | 5242 | pgnoNew[0], szNew[0], |
| 5243 | nNew>=2 ? pgnoNew[1] : 0, nNew>=2 ? szNew[1] : 0, |
| 5244 | nNew>=3 ? pgnoNew[2] : 0, nNew>=3 ? szNew[2] : 0, |
drh | a2fce64 | 2004-06-05 00:01:44 +0000 | [diff] [blame] | 5245 | nNew>=4 ? pgnoNew[3] : 0, nNew>=4 ? szNew[3] : 0, |
| 5246 | nNew>=5 ? pgnoNew[4] : 0, nNew>=5 ? szNew[4] : 0)); |
drh | 24cd67e | 2004-05-10 16:18:47 +0000 | [diff] [blame] | 5247 | |
drh | f9ffac9 | 2002-03-02 19:00:31 +0000 | [diff] [blame] | 5248 | /* |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 5249 | ** Evenly distribute the data in apCell[] across the new pages. |
| 5250 | ** Insert divider cells into pParent as necessary. |
| 5251 | */ |
| 5252 | j = 0; |
| 5253 | for(i=0; i<nNew; i++){ |
danielk1977 | ac11ee6 | 2005-01-15 12:45:51 +0000 | [diff] [blame] | 5254 | /* Assemble the new sibling page. */ |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 5255 | MemPage *pNew = apNew[i]; |
drh | 19642e5 | 2005-03-29 13:17:45 +0000 | [diff] [blame] | 5256 | assert( j<nMaxCells ); |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 5257 | assert( pNew->pgno==pgnoNew[i] ); |
drh | fa1a98a | 2004-05-14 19:08:17 +0000 | [diff] [blame] | 5258 | assemblePage(pNew, cntNew[i]-j, &apCell[j], &szCell[j]); |
drh | 09d0deb | 2005-08-02 17:13:09 +0000 | [diff] [blame] | 5259 | assert( pNew->nCell>0 || (nNew==1 && cntNew[0]==0) ); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5260 | assert( pNew->nOverflow==0 ); |
danielk1977 | ac11ee6 | 2005-01-15 12:45:51 +0000 | [diff] [blame] | 5261 | |
| 5262 | #ifndef SQLITE_OMIT_AUTOVACUUM |
| 5263 | /* If this is an auto-vacuum database, update the pointer map entries |
| 5264 | ** that point to the siblings that were rearranged. These can be: left |
| 5265 | ** children of cells, the right-child of the page, or overflow pages |
| 5266 | ** pointed to by cells. |
| 5267 | */ |
| 5268 | if( pBt->autoVacuum ){ |
| 5269 | for(k=j; k<cntNew[i]; k++){ |
danielk1977 | 634f298 | 2005-03-28 08:44:07 +0000 | [diff] [blame] | 5270 | assert( k<nMaxCells ); |
danielk1977 | ac11ee6 | 2005-01-15 12:45:51 +0000 | [diff] [blame] | 5271 | if( aFrom[k]==0xFF || apCopy[aFrom[k]]->pgno!=pNew->pgno ){ |
danielk1977 | 79a40da | 2005-01-16 08:00:01 +0000 | [diff] [blame] | 5272 | rc = ptrmapPutOvfl(pNew, k-j); |
| 5273 | if( rc!=SQLITE_OK ){ |
| 5274 | goto balance_cleanup; |
danielk1977 | ac11ee6 | 2005-01-15 12:45:51 +0000 | [diff] [blame] | 5275 | } |
| 5276 | } |
| 5277 | } |
| 5278 | } |
| 5279 | #endif |
| 5280 | |
| 5281 | j = cntNew[i]; |
| 5282 | |
| 5283 | /* If the sibling page assembled above was not the right-most sibling, |
| 5284 | ** insert a divider cell into the parent page. |
| 5285 | */ |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 5286 | if( i<nNew-1 && j<nCell ){ |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 5287 | u8 *pCell; |
drh | 24cd67e | 2004-05-10 16:18:47 +0000 | [diff] [blame] | 5288 | u8 *pTemp; |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 5289 | int sz; |
danielk1977 | 634f298 | 2005-03-28 08:44:07 +0000 | [diff] [blame] | 5290 | |
| 5291 | assert( j<nMaxCells ); |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 5292 | pCell = apCell[j]; |
| 5293 | sz = szCell[j] + leafCorrection; |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 5294 | if( !pNew->leaf ){ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5295 | memcpy(&pNew->aData[8], pCell, 4); |
drh | 24cd67e | 2004-05-10 16:18:47 +0000 | [diff] [blame] | 5296 | pTemp = 0; |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 5297 | }else if( leafData ){ |
drh | fd131da | 2007-08-07 17:13:03 +0000 | [diff] [blame] | 5298 | /* If the tree is a leaf-data tree, and the siblings are leaves, |
danielk1977 | ac11ee6 | 2005-01-15 12:45:51 +0000 | [diff] [blame] | 5299 | ** then there is no divider cell in apCell[]. Instead, the divider |
| 5300 | ** cell consists of the integer key for the right-most cell of |
| 5301 | ** the sibling-page assembled above only. |
| 5302 | */ |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 5303 | CellInfo info; |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 5304 | j--; |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 5305 | sqlite3BtreeParseCellPtr(pNew, apCell[j], &info); |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 5306 | pCell = &aSpace[iSpace]; |
drh | b026e05 | 2007-05-02 01:34:31 +0000 | [diff] [blame] | 5307 | fillInCell(pParent, pCell, 0, info.nKey, 0, 0, 0, &sz); |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 5308 | iSpace += sz; |
drh | 07d183d | 2005-05-01 22:52:42 +0000 | [diff] [blame] | 5309 | assert( iSpace<=pBt->pageSize*5 ); |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 5310 | pTemp = 0; |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 5311 | }else{ |
| 5312 | pCell -= 4; |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 5313 | pTemp = &aSpace[iSpace]; |
| 5314 | iSpace += sz; |
drh | 07d183d | 2005-05-01 22:52:42 +0000 | [diff] [blame] | 5315 | assert( iSpace<=pBt->pageSize*5 ); |
danielk1977 | 4aeff62 | 2007-05-12 09:30:47 +0000 | [diff] [blame] | 5316 | /* Obscure case for non-leaf-data trees: If the cell at pCell was |
drh | 85b623f | 2007-12-13 21:54:09 +0000 | [diff] [blame] | 5317 | ** previously stored on a leaf node, and its reported size was 4 |
danielk1977 | 4aeff62 | 2007-05-12 09:30:47 +0000 | [diff] [blame] | 5318 | ** bytes, then it may actually be smaller than this |
| 5319 | ** (see sqlite3BtreeParseCellPtr(), 4 bytes is the minimum size of |
drh | 85b623f | 2007-12-13 21:54:09 +0000 | [diff] [blame] | 5320 | ** any cell). But it is important to pass the correct size to |
danielk1977 | 4aeff62 | 2007-05-12 09:30:47 +0000 | [diff] [blame] | 5321 | ** insertCell(), so reparse the cell now. |
| 5322 | ** |
| 5323 | ** Note that this can never happen in an SQLite data file, as all |
| 5324 | ** cells are at least 4 bytes. It only happens in b-trees used |
| 5325 | ** to evaluate "IN (SELECT ...)" and similar clauses. |
| 5326 | */ |
| 5327 | if( szCell[j]==4 ){ |
| 5328 | assert(leafCorrection==4); |
| 5329 | sz = cellSizePtr(pParent, pCell); |
| 5330 | } |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 5331 | } |
danielk1977 | a3ad5e7 | 2005-01-07 08:56:44 +0000 | [diff] [blame] | 5332 | rc = insertCell(pParent, nxDiv, pCell, sz, pTemp, 4); |
danielk1977 | e80463b | 2004-11-03 03:01:16 +0000 | [diff] [blame] | 5333 | if( rc!=SQLITE_OK ) goto balance_cleanup; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5334 | put4byte(findOverflowCell(pParent,nxDiv), pNew->pgno); |
danielk1977 | ac11ee6 | 2005-01-15 12:45:51 +0000 | [diff] [blame] | 5335 | #ifndef SQLITE_OMIT_AUTOVACUUM |
| 5336 | /* If this is an auto-vacuum database, and not a leaf-data tree, |
| 5337 | ** then update the pointer map with an entry for the overflow page |
| 5338 | ** that the cell just inserted points to (if any). |
| 5339 | */ |
| 5340 | if( pBt->autoVacuum && !leafData ){ |
danielk1977 | 79a40da | 2005-01-16 08:00:01 +0000 | [diff] [blame] | 5341 | rc = ptrmapPutOvfl(pParent, nxDiv); |
| 5342 | if( rc!=SQLITE_OK ){ |
| 5343 | goto balance_cleanup; |
danielk1977 | ac11ee6 | 2005-01-15 12:45:51 +0000 | [diff] [blame] | 5344 | } |
| 5345 | } |
| 5346 | #endif |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 5347 | j++; |
| 5348 | nxDiv++; |
| 5349 | } |
| 5350 | } |
drh | 6019e16 | 2001-07-02 17:51:45 +0000 | [diff] [blame] | 5351 | assert( j==nCell ); |
drh | 7aa8f85 | 2006-03-28 00:24:44 +0000 | [diff] [blame] | 5352 | assert( nOld>0 ); |
| 5353 | assert( nNew>0 ); |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 5354 | if( (pageFlags & PTF_LEAF)==0 ){ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5355 | memcpy(&apNew[nNew-1]->aData[8], &apCopy[nOld-1]->aData[8], 4); |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 5356 | } |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5357 | if( nxDiv==pParent->nCell+pParent->nOverflow ){ |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 5358 | /* Right-most sibling is the right-most child of pParent */ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5359 | put4byte(&pParent->aData[pParent->hdrOffset+8], pgnoNew[nNew-1]); |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 5360 | }else{ |
| 5361 | /* Right-most sibling is the left child of the first entry in pParent |
| 5362 | ** past the right-most divider entry */ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5363 | put4byte(findOverflowCell(pParent, nxDiv), pgnoNew[nNew-1]); |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 5364 | } |
| 5365 | |
| 5366 | /* |
| 5367 | ** Reparent children of all cells. |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 5368 | */ |
| 5369 | for(i=0; i<nNew; i++){ |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 5370 | rc = reparentChildPages(apNew[i]); |
| 5371 | if( rc!=SQLITE_OK ) goto balance_cleanup; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 5372 | } |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 5373 | rc = reparentChildPages(pParent); |
| 5374 | if( rc!=SQLITE_OK ) goto balance_cleanup; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 5375 | |
| 5376 | /* |
drh | 3a4c141 | 2004-05-09 20:40:11 +0000 | [diff] [blame] | 5377 | ** Balance the parent page. Note that the current page (pPage) might |
danielk1977 | ac11ee6 | 2005-01-15 12:45:51 +0000 | [diff] [blame] | 5378 | ** have been added to the freelist so it might no longer be initialized. |
drh | 3a4c141 | 2004-05-09 20:40:11 +0000 | [diff] [blame] | 5379 | ** But the parent page will always be initialized. |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 5380 | */ |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 5381 | assert( pParent->isInit ); |
danielk1977 | ac245ec | 2005-01-14 13:50:11 +0000 | [diff] [blame] | 5382 | rc = balance(pParent, 0); |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 5383 | |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 5384 | /* |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 5385 | ** Cleanup before returning. |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 5386 | */ |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 5387 | balance_cleanup: |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 5388 | sqlite3_free(apCell); |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 5389 | for(i=0; i<nOld; i++){ |
drh | 9102529 | 2004-05-03 19:49:32 +0000 | [diff] [blame] | 5390 | releasePage(apOld[i]); |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 5391 | } |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 5392 | for(i=0; i<nNew; i++){ |
drh | 9102529 | 2004-05-03 19:49:32 +0000 | [diff] [blame] | 5393 | releasePage(apNew[i]); |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 5394 | } |
drh | 9102529 | 2004-05-03 19:49:32 +0000 | [diff] [blame] | 5395 | releasePage(pParent); |
drh | 3a4c141 | 2004-05-09 20:40:11 +0000 | [diff] [blame] | 5396 | TRACE(("BALANCE: finished with %d: old=%d new=%d cells=%d\n", |
| 5397 | pPage->pgno, nOld, nNew, nCell)); |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 5398 | return rc; |
| 5399 | } |
| 5400 | |
| 5401 | /* |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5402 | ** This routine is called for the root page of a btree when the root |
| 5403 | ** page contains no cells. This is an opportunity to make the tree |
| 5404 | ** shallower by one level. |
| 5405 | */ |
| 5406 | static int balance_shallower(MemPage *pPage){ |
| 5407 | MemPage *pChild; /* The only child page of pPage */ |
| 5408 | Pgno pgnoChild; /* Page number for pChild */ |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 5409 | int rc = SQLITE_OK; /* Return code from subprocedures */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 5410 | BtShared *pBt; /* The main BTree structure */ |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 5411 | int mxCellPerPage; /* Maximum number of cells per page */ |
| 5412 | u8 **apCell; /* All cells from pages being balanced */ |
drh | a9121e4 | 2008-02-19 14:59:35 +0000 | [diff] [blame] | 5413 | u16 *szCell; /* Local size of all cells */ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5414 | |
| 5415 | assert( pPage->pParent==0 ); |
| 5416 | assert( pPage->nCell==0 ); |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 5417 | assert( sqlite3_mutex_held(pPage->pBt->mutex) ); |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 5418 | pBt = pPage->pBt; |
| 5419 | mxCellPerPage = MX_CELL(pBt); |
drh | a9121e4 | 2008-02-19 14:59:35 +0000 | [diff] [blame] | 5420 | apCell = sqlite3_malloc( mxCellPerPage*(sizeof(u8*)+sizeof(u16)) ); |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 5421 | if( apCell==0 ) return SQLITE_NOMEM; |
drh | a9121e4 | 2008-02-19 14:59:35 +0000 | [diff] [blame] | 5422 | szCell = (u16*)&apCell[mxCellPerPage]; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5423 | if( pPage->leaf ){ |
| 5424 | /* The table is completely empty */ |
| 5425 | TRACE(("BALANCE: empty table %d\n", pPage->pgno)); |
| 5426 | }else{ |
| 5427 | /* The root page is empty but has one child. Transfer the |
| 5428 | ** information from that one child into the root page if it |
| 5429 | ** will fit. This reduces the depth of the tree by one. |
| 5430 | ** |
| 5431 | ** If the root page is page 1, it has less space available than |
| 5432 | ** its child (due to the 100 byte header that occurs at the beginning |
| 5433 | ** of the database fle), so it might not be able to hold all of the |
| 5434 | ** information currently contained in the child. If this is the |
| 5435 | ** case, then do not do the transfer. Leave page 1 empty except |
| 5436 | ** for the right-pointer to the child page. The child page becomes |
| 5437 | ** the virtual root of the tree. |
| 5438 | */ |
| 5439 | pgnoChild = get4byte(&pPage->aData[pPage->hdrOffset+8]); |
| 5440 | assert( pgnoChild>0 ); |
danielk1977 | ad0132d | 2008-06-07 08:58:22 +0000 | [diff] [blame^] | 5441 | assert( pgnoChild<=pagerPagecount(pPage->pBt->pPager) ); |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 5442 | rc = sqlite3BtreeGetPage(pPage->pBt, pgnoChild, &pChild, 0); |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 5443 | if( rc ) goto end_shallow_balance; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5444 | if( pPage->pgno==1 ){ |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 5445 | rc = sqlite3BtreeInitPage(pChild, pPage); |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 5446 | if( rc ) goto end_shallow_balance; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5447 | assert( pChild->nOverflow==0 ); |
| 5448 | if( pChild->nFree>=100 ){ |
| 5449 | /* The child information will fit on the root page, so do the |
| 5450 | ** copy */ |
| 5451 | int i; |
| 5452 | zeroPage(pPage, pChild->aData[0]); |
| 5453 | for(i=0; i<pChild->nCell; i++){ |
danielk1977 | 1cc5ed8 | 2007-05-16 17:28:43 +0000 | [diff] [blame] | 5454 | apCell[i] = findCell(pChild,i); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5455 | szCell[i] = cellSizePtr(pChild, apCell[i]); |
| 5456 | } |
| 5457 | assemblePage(pPage, pChild->nCell, apCell, szCell); |
danielk1977 | ae82558 | 2004-11-23 09:06:55 +0000 | [diff] [blame] | 5458 | /* Copy the right-pointer of the child to the parent. */ |
| 5459 | put4byte(&pPage->aData[pPage->hdrOffset+8], |
| 5460 | get4byte(&pChild->aData[pChild->hdrOffset+8])); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5461 | freePage(pChild); |
| 5462 | TRACE(("BALANCE: child %d transfer to page 1\n", pChild->pgno)); |
| 5463 | }else{ |
| 5464 | /* The child has more information that will fit on the root. |
| 5465 | ** The tree is already balanced. Do nothing. */ |
| 5466 | TRACE(("BALANCE: child %d will not fit on page 1\n", pChild->pgno)); |
| 5467 | } |
| 5468 | }else{ |
| 5469 | memcpy(pPage->aData, pChild->aData, pPage->pBt->usableSize); |
| 5470 | pPage->isInit = 0; |
| 5471 | pPage->pParent = 0; |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 5472 | rc = sqlite3BtreeInitPage(pPage, 0); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5473 | assert( rc==SQLITE_OK ); |
| 5474 | freePage(pChild); |
| 5475 | TRACE(("BALANCE: transfer child %d into root %d\n", |
| 5476 | pChild->pgno, pPage->pgno)); |
| 5477 | } |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 5478 | rc = reparentChildPages(pPage); |
danielk1977 | ac11ee6 | 2005-01-15 12:45:51 +0000 | [diff] [blame] | 5479 | assert( pPage->nOverflow==0 ); |
| 5480 | #ifndef SQLITE_OMIT_AUTOVACUUM |
| 5481 | if( pBt->autoVacuum ){ |
danielk1977 | aac0a38 | 2005-01-16 11:07:06 +0000 | [diff] [blame] | 5482 | int i; |
danielk1977 | ac11ee6 | 2005-01-15 12:45:51 +0000 | [diff] [blame] | 5483 | for(i=0; i<pPage->nCell; i++){ |
danielk1977 | 79a40da | 2005-01-16 08:00:01 +0000 | [diff] [blame] | 5484 | rc = ptrmapPutOvfl(pPage, i); |
| 5485 | if( rc!=SQLITE_OK ){ |
| 5486 | goto end_shallow_balance; |
danielk1977 | ac11ee6 | 2005-01-15 12:45:51 +0000 | [diff] [blame] | 5487 | } |
| 5488 | } |
| 5489 | } |
| 5490 | #endif |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5491 | releasePage(pChild); |
| 5492 | } |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 5493 | end_shallow_balance: |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 5494 | sqlite3_free(apCell); |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 5495 | return rc; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5496 | } |
| 5497 | |
| 5498 | |
| 5499 | /* |
| 5500 | ** The root page is overfull |
| 5501 | ** |
| 5502 | ** When this happens, Create a new child page and copy the |
| 5503 | ** contents of the root into the child. Then make the root |
| 5504 | ** page an empty page with rightChild pointing to the new |
| 5505 | ** child. Finally, call balance_internal() on the new child |
| 5506 | ** to cause it to split. |
| 5507 | */ |
| 5508 | static int balance_deeper(MemPage *pPage){ |
| 5509 | int rc; /* Return value from subprocedures */ |
| 5510 | MemPage *pChild; /* Pointer to a new child page */ |
| 5511 | Pgno pgnoChild; /* Page number of the new child page */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 5512 | BtShared *pBt; /* The BTree */ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5513 | int usableSize; /* Total usable size of a page */ |
| 5514 | u8 *data; /* Content of the parent page */ |
| 5515 | u8 *cdata; /* Content of the child page */ |
| 5516 | int hdr; /* Offset to page header in parent */ |
| 5517 | int brk; /* Offset to content of first cell in parent */ |
| 5518 | |
| 5519 | assert( pPage->pParent==0 ); |
| 5520 | assert( pPage->nOverflow>0 ); |
| 5521 | pBt = pPage->pBt; |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 5522 | assert( sqlite3_mutex_held(pBt->mutex) ); |
drh | 4f0c587 | 2007-03-26 22:05:01 +0000 | [diff] [blame] | 5523 | rc = allocateBtreePage(pBt, &pChild, &pgnoChild, pPage->pgno, 0); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5524 | if( rc ) return rc; |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 5525 | assert( sqlite3PagerIswriteable(pChild->pDbPage) ); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5526 | usableSize = pBt->usableSize; |
| 5527 | data = pPage->aData; |
| 5528 | hdr = pPage->hdrOffset; |
| 5529 | brk = get2byte(&data[hdr+5]); |
| 5530 | cdata = pChild->aData; |
| 5531 | memcpy(cdata, &data[hdr], pPage->cellOffset+2*pPage->nCell-hdr); |
| 5532 | memcpy(&cdata[brk], &data[brk], usableSize-brk); |
danielk1977 | c7dc753 | 2004-11-17 10:22:03 +0000 | [diff] [blame] | 5533 | assert( pChild->isInit==0 ); |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 5534 | rc = sqlite3BtreeInitPage(pChild, pPage); |
danielk1977 | 6b456a2 | 2005-03-21 04:04:02 +0000 | [diff] [blame] | 5535 | if( rc ) goto balancedeeper_out; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5536 | memcpy(pChild->aOvfl, pPage->aOvfl, pPage->nOverflow*sizeof(pPage->aOvfl[0])); |
| 5537 | pChild->nOverflow = pPage->nOverflow; |
| 5538 | if( pChild->nOverflow ){ |
| 5539 | pChild->nFree = 0; |
| 5540 | } |
| 5541 | assert( pChild->nCell==pPage->nCell ); |
| 5542 | zeroPage(pPage, pChild->aData[0] & ~PTF_LEAF); |
| 5543 | put4byte(&pPage->aData[pPage->hdrOffset+8], pgnoChild); |
| 5544 | TRACE(("BALANCE: copy root %d into %d\n", pPage->pgno, pChild->pgno)); |
danielk1977 | 4e17d14 | 2005-01-16 09:06:33 +0000 | [diff] [blame] | 5545 | #ifndef SQLITE_OMIT_AUTOVACUUM |
danielk1977 | ac11ee6 | 2005-01-15 12:45:51 +0000 | [diff] [blame] | 5546 | if( pBt->autoVacuum ){ |
| 5547 | int i; |
| 5548 | rc = ptrmapPut(pBt, pChild->pgno, PTRMAP_BTREE, pPage->pgno); |
danielk1977 | 6b456a2 | 2005-03-21 04:04:02 +0000 | [diff] [blame] | 5549 | if( rc ) goto balancedeeper_out; |
danielk1977 | ac11ee6 | 2005-01-15 12:45:51 +0000 | [diff] [blame] | 5550 | for(i=0; i<pChild->nCell; i++){ |
danielk1977 | 79a40da | 2005-01-16 08:00:01 +0000 | [diff] [blame] | 5551 | rc = ptrmapPutOvfl(pChild, i); |
| 5552 | if( rc!=SQLITE_OK ){ |
| 5553 | return rc; |
danielk1977 | ac11ee6 | 2005-01-15 12:45:51 +0000 | [diff] [blame] | 5554 | } |
| 5555 | } |
| 5556 | } |
danielk1977 | 4e17d14 | 2005-01-16 09:06:33 +0000 | [diff] [blame] | 5557 | #endif |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5558 | rc = balance_nonroot(pChild); |
danielk1977 | 6b456a2 | 2005-03-21 04:04:02 +0000 | [diff] [blame] | 5559 | |
| 5560 | balancedeeper_out: |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5561 | releasePage(pChild); |
| 5562 | return rc; |
| 5563 | } |
| 5564 | |
| 5565 | /* |
| 5566 | ** Decide if the page pPage needs to be balanced. If balancing is |
| 5567 | ** required, call the appropriate balancing routine. |
| 5568 | */ |
danielk1977 | ac245ec | 2005-01-14 13:50:11 +0000 | [diff] [blame] | 5569 | static int balance(MemPage *pPage, int insert){ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5570 | int rc = SQLITE_OK; |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 5571 | assert( sqlite3_mutex_held(pPage->pBt->mutex) ); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5572 | if( pPage->pParent==0 ){ |
danielk1977 | 6e465eb | 2007-08-21 13:11:00 +0000 | [diff] [blame] | 5573 | rc = sqlite3PagerWrite(pPage->pDbPage); |
| 5574 | if( rc==SQLITE_OK && pPage->nOverflow>0 ){ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5575 | rc = balance_deeper(pPage); |
| 5576 | } |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 5577 | if( rc==SQLITE_OK && pPage->nCell==0 ){ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5578 | rc = balance_shallower(pPage); |
| 5579 | } |
| 5580 | }else{ |
danielk1977 | ac245ec | 2005-01-14 13:50:11 +0000 | [diff] [blame] | 5581 | if( pPage->nOverflow>0 || |
| 5582 | (!insert && pPage->nFree>pPage->pBt->usableSize*2/3) ){ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5583 | rc = balance_nonroot(pPage); |
| 5584 | } |
| 5585 | } |
| 5586 | return rc; |
| 5587 | } |
| 5588 | |
| 5589 | /* |
drh | 8dcd7ca | 2004-08-08 19:43:29 +0000 | [diff] [blame] | 5590 | ** This routine checks all cursors that point to table pgnoRoot. |
drh | 980b1a7 | 2006-08-16 16:42:48 +0000 | [diff] [blame] | 5591 | ** If any of those cursors were opened with wrFlag==0 in a different |
| 5592 | ** database connection (a database connection that shares the pager |
| 5593 | ** cache with the current connection) and that other connection |
| 5594 | ** is not in the ReadUncommmitted state, then this routine returns |
| 5595 | ** SQLITE_LOCKED. |
danielk1977 | 299b187 | 2004-11-22 10:02:10 +0000 | [diff] [blame] | 5596 | ** |
| 5597 | ** In addition to checking for read-locks (where a read-lock |
| 5598 | ** means a cursor opened with wrFlag==0) this routine also moves |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 5599 | ** all write cursors so that they are pointing to the |
drh | 980b1a7 | 2006-08-16 16:42:48 +0000 | [diff] [blame] | 5600 | ** first Cell on the root page. This is necessary because an insert |
danielk1977 | 299b187 | 2004-11-22 10:02:10 +0000 | [diff] [blame] | 5601 | ** or delete might change the number of cells on a page or delete |
| 5602 | ** a page entirely and we do not want to leave any cursors |
| 5603 | ** pointing to non-existant pages or cells. |
drh | f74b8d9 | 2002-09-01 23:20:45 +0000 | [diff] [blame] | 5604 | */ |
drh | 980b1a7 | 2006-08-16 16:42:48 +0000 | [diff] [blame] | 5605 | static int checkReadLocks(Btree *pBtree, Pgno pgnoRoot, BtCursor *pExclude){ |
danielk1977 | 299b187 | 2004-11-22 10:02:10 +0000 | [diff] [blame] | 5606 | BtCursor *p; |
drh | 980b1a7 | 2006-08-16 16:42:48 +0000 | [diff] [blame] | 5607 | BtShared *pBt = pBtree->pBt; |
drh | e5fe690 | 2007-12-07 18:55:28 +0000 | [diff] [blame] | 5608 | sqlite3 *db = pBtree->db; |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 5609 | assert( sqlite3BtreeHoldsMutex(pBtree) ); |
danielk1977 | 299b187 | 2004-11-22 10:02:10 +0000 | [diff] [blame] | 5610 | for(p=pBt->pCursor; p; p=p->pNext){ |
drh | 980b1a7 | 2006-08-16 16:42:48 +0000 | [diff] [blame] | 5611 | if( p==pExclude ) continue; |
| 5612 | if( p->eState!=CURSOR_VALID ) continue; |
| 5613 | if( p->pgnoRoot!=pgnoRoot ) continue; |
| 5614 | if( p->wrFlag==0 ){ |
drh | e5fe690 | 2007-12-07 18:55:28 +0000 | [diff] [blame] | 5615 | sqlite3 *dbOther = p->pBtree->db; |
drh | 980b1a7 | 2006-08-16 16:42:48 +0000 | [diff] [blame] | 5616 | if( dbOther==0 || |
| 5617 | (dbOther!=db && (dbOther->flags & SQLITE_ReadUncommitted)==0) ){ |
| 5618 | return SQLITE_LOCKED; |
| 5619 | } |
| 5620 | }else if( p->pPage->pgno!=p->pgnoRoot ){ |
danielk1977 | 299b187 | 2004-11-22 10:02:10 +0000 | [diff] [blame] | 5621 | moveToRoot(p); |
| 5622 | } |
| 5623 | } |
drh | f74b8d9 | 2002-09-01 23:20:45 +0000 | [diff] [blame] | 5624 | return SQLITE_OK; |
| 5625 | } |
| 5626 | |
| 5627 | /* |
danielk1977 | 52ae724 | 2008-03-25 14:24:56 +0000 | [diff] [blame] | 5628 | ** Make sure pBt->pTmpSpace points to an allocation of |
| 5629 | ** MX_CELL_SIZE(pBt) bytes. |
| 5630 | */ |
| 5631 | static void allocateTempSpace(BtShared *pBt){ |
| 5632 | if( !pBt->pTmpSpace ){ |
| 5633 | pBt->pTmpSpace = sqlite3_malloc(MX_CELL_SIZE(pBt)); |
| 5634 | } |
| 5635 | } |
| 5636 | |
| 5637 | /* |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 5638 | ** Insert a new record into the BTree. The key is given by (pKey,nKey) |
| 5639 | ** and the data is given by (pData,nData). The cursor is used only to |
drh | 9102529 | 2004-05-03 19:49:32 +0000 | [diff] [blame] | 5640 | ** define what table the record should be inserted into. The cursor |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 5641 | ** is left pointing at a random location. |
| 5642 | ** |
| 5643 | ** For an INTKEY table, only the nKey value of the key is used. pKey is |
| 5644 | ** ignored. For a ZERODATA table, the pData and nData are both ignored. |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 5645 | */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 5646 | int sqlite3BtreeInsert( |
drh | 5c4d970 | 2001-08-20 00:33:58 +0000 | [diff] [blame] | 5647 | BtCursor *pCur, /* Insert data into the table of this cursor */ |
drh | 4a1c380 | 2004-05-12 15:15:47 +0000 | [diff] [blame] | 5648 | const void *pKey, i64 nKey, /* The key of the new record */ |
drh | e4d9081 | 2007-03-29 05:51:49 +0000 | [diff] [blame] | 5649 | const void *pData, int nData, /* The data of the new record */ |
drh | b026e05 | 2007-05-02 01:34:31 +0000 | [diff] [blame] | 5650 | int nZero, /* Number of extra 0 bytes to append to data */ |
drh | e4d9081 | 2007-03-29 05:51:49 +0000 | [diff] [blame] | 5651 | int appendBias /* True if this is likely an append */ |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 5652 | ){ |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 5653 | int rc; |
| 5654 | int loc; |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 5655 | int szNew; |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 5656 | MemPage *pPage; |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 5657 | Btree *p = pCur->pBtree; |
| 5658 | BtShared *pBt = p->pBt; |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 5659 | unsigned char *oldCell; |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 5660 | unsigned char *newCell = 0; |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 5661 | |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 5662 | assert( cursorHoldsMutex(pCur) ); |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 5663 | if( pBt->inTransaction!=TRANS_WRITE ){ |
drh | f74b8d9 | 2002-09-01 23:20:45 +0000 | [diff] [blame] | 5664 | /* Must start a transaction before doing an insert */ |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 5665 | rc = pBt->readOnly ? SQLITE_READONLY : SQLITE_ERROR; |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 5666 | return rc; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 5667 | } |
drh | f74b8d9 | 2002-09-01 23:20:45 +0000 | [diff] [blame] | 5668 | assert( !pBt->readOnly ); |
drh | ecdc753 | 2001-09-23 02:35:53 +0000 | [diff] [blame] | 5669 | if( !pCur->wrFlag ){ |
| 5670 | return SQLITE_PERM; /* Cursor not open for writing */ |
| 5671 | } |
drh | 980b1a7 | 2006-08-16 16:42:48 +0000 | [diff] [blame] | 5672 | if( checkReadLocks(pCur->pBtree, pCur->pgnoRoot, pCur) ){ |
drh | f74b8d9 | 2002-09-01 23:20:45 +0000 | [diff] [blame] | 5673 | return SQLITE_LOCKED; /* The table pCur points to has a read lock */ |
| 5674 | } |
drh | fb98264 | 2007-08-30 01:19:59 +0000 | [diff] [blame] | 5675 | if( pCur->eState==CURSOR_FAULT ){ |
| 5676 | return pCur->skip; |
| 5677 | } |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 5678 | |
| 5679 | /* Save the positions of any other cursors open on this table */ |
drh | bf700f3 | 2007-03-31 02:36:44 +0000 | [diff] [blame] | 5680 | clearCursorPosition(pCur); |
danielk1977 | 2e94d4d | 2006-01-09 05:36:27 +0000 | [diff] [blame] | 5681 | if( |
danielk1977 | 2e94d4d | 2006-01-09 05:36:27 +0000 | [diff] [blame] | 5682 | SQLITE_OK!=(rc = saveAllCursors(pBt, pCur->pgnoRoot, pCur)) || |
drh | e14006d | 2008-03-25 17:23:32 +0000 | [diff] [blame] | 5683 | SQLITE_OK!=(rc = sqlite3BtreeMoveto(pCur, pKey, 0, nKey, appendBias, &loc)) |
danielk1977 | 2e94d4d | 2006-01-09 05:36:27 +0000 | [diff] [blame] | 5684 | ){ |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 5685 | return rc; |
| 5686 | } |
| 5687 | |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 5688 | pPage = pCur->pPage; |
drh | 4a1c380 | 2004-05-12 15:15:47 +0000 | [diff] [blame] | 5689 | assert( pPage->intKey || nKey>=0 ); |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 5690 | assert( pPage->leaf || !pPage->leafData ); |
drh | 3a4c141 | 2004-05-09 20:40:11 +0000 | [diff] [blame] | 5691 | TRACE(("INSERT: table=%d nkey=%lld ndata=%d page=%d %s\n", |
| 5692 | pCur->pgnoRoot, nKey, nData, pPage->pgno, |
| 5693 | loc==0 ? "overwrite" : "new entry")); |
drh | 7aa128d | 2002-06-21 13:09:16 +0000 | [diff] [blame] | 5694 | assert( pPage->isInit ); |
danielk1977 | 52ae724 | 2008-03-25 14:24:56 +0000 | [diff] [blame] | 5695 | allocateTempSpace(pBt); |
| 5696 | newCell = pBt->pTmpSpace; |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 5697 | if( newCell==0 ) return SQLITE_NOMEM; |
drh | b026e05 | 2007-05-02 01:34:31 +0000 | [diff] [blame] | 5698 | rc = fillInCell(pPage, newCell, pKey, nKey, pData, nData, nZero, &szNew); |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 5699 | if( rc ) goto end_insert; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5700 | assert( szNew==cellSizePtr(pPage, newCell) ); |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 5701 | assert( szNew<=MX_CELL_SIZE(pBt) ); |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 5702 | if( loc==0 && CURSOR_VALID==pCur->eState ){ |
drh | a9121e4 | 2008-02-19 14:59:35 +0000 | [diff] [blame] | 5703 | u16 szOld; |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 5704 | assert( pCur->idx>=0 && pCur->idx<pPage->nCell ); |
danielk1977 | 6e465eb | 2007-08-21 13:11:00 +0000 | [diff] [blame] | 5705 | rc = sqlite3PagerWrite(pPage->pDbPage); |
| 5706 | if( rc ){ |
| 5707 | goto end_insert; |
| 5708 | } |
danielk1977 | 1cc5ed8 | 2007-05-16 17:28:43 +0000 | [diff] [blame] | 5709 | oldCell = findCell(pPage, pCur->idx); |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 5710 | if( !pPage->leaf ){ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5711 | memcpy(newCell, oldCell, 4); |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 5712 | } |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5713 | szOld = cellSizePtr(pPage, oldCell); |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 5714 | rc = clearCell(pPage, oldCell); |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 5715 | if( rc ) goto end_insert; |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 5716 | dropCell(pPage, pCur->idx, szOld); |
drh | 7c717f7 | 2001-06-24 20:39:41 +0000 | [diff] [blame] | 5717 | }else if( loc<0 && pPage->nCell>0 ){ |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 5718 | assert( pPage->leaf ); |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 5719 | pCur->idx++; |
drh | 271efa5 | 2004-05-30 19:19:05 +0000 | [diff] [blame] | 5720 | pCur->info.nSize = 0; |
drh | a2c20e4 | 2008-03-29 16:01:04 +0000 | [diff] [blame] | 5721 | pCur->validNKey = 0; |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 5722 | }else{ |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 5723 | assert( pPage->leaf ); |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 5724 | } |
danielk1977 | a3ad5e7 | 2005-01-07 08:56:44 +0000 | [diff] [blame] | 5725 | rc = insertCell(pPage, pCur->idx, newCell, szNew, 0, 0); |
danielk1977 | e80463b | 2004-11-03 03:01:16 +0000 | [diff] [blame] | 5726 | if( rc!=SQLITE_OK ) goto end_insert; |
danielk1977 | ac245ec | 2005-01-14 13:50:11 +0000 | [diff] [blame] | 5727 | rc = balance(pPage, 1); |
drh | 23e11ca | 2004-05-04 17:27:28 +0000 | [diff] [blame] | 5728 | /* sqlite3BtreePageDump(pCur->pBt, pCur->pgnoRoot, 1); */ |
drh | 3fc190c | 2001-09-14 03:24:23 +0000 | [diff] [blame] | 5729 | /* fflush(stdout); */ |
danielk1977 | 299b187 | 2004-11-22 10:02:10 +0000 | [diff] [blame] | 5730 | if( rc==SQLITE_OK ){ |
| 5731 | moveToRoot(pCur); |
| 5732 | } |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 5733 | end_insert: |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 5734 | return rc; |
| 5735 | } |
| 5736 | |
| 5737 | /* |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 5738 | ** Delete the entry that the cursor is pointing to. The cursor |
| 5739 | ** is left pointing at a random location. |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 5740 | */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 5741 | int sqlite3BtreeDelete(BtCursor *pCur){ |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 5742 | MemPage *pPage = pCur->pPage; |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 5743 | unsigned char *pCell; |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 5744 | int rc; |
danielk1977 | cfe9a69 | 2004-06-16 12:00:29 +0000 | [diff] [blame] | 5745 | Pgno pgnoChild = 0; |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 5746 | Btree *p = pCur->pBtree; |
| 5747 | BtShared *pBt = p->pBt; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 5748 | |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 5749 | assert( cursorHoldsMutex(pCur) ); |
drh | 7aa128d | 2002-06-21 13:09:16 +0000 | [diff] [blame] | 5750 | assert( pPage->isInit ); |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 5751 | if( pBt->inTransaction!=TRANS_WRITE ){ |
drh | f74b8d9 | 2002-09-01 23:20:45 +0000 | [diff] [blame] | 5752 | /* Must start a transaction before doing a delete */ |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 5753 | rc = pBt->readOnly ? SQLITE_READONLY : SQLITE_ERROR; |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 5754 | return rc; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 5755 | } |
drh | f74b8d9 | 2002-09-01 23:20:45 +0000 | [diff] [blame] | 5756 | assert( !pBt->readOnly ); |
drh | fb98264 | 2007-08-30 01:19:59 +0000 | [diff] [blame] | 5757 | if( pCur->eState==CURSOR_FAULT ){ |
| 5758 | return pCur->skip; |
| 5759 | } |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 5760 | if( pCur->idx >= pPage->nCell ){ |
| 5761 | return SQLITE_ERROR; /* The cursor is not pointing to anything */ |
| 5762 | } |
drh | ecdc753 | 2001-09-23 02:35:53 +0000 | [diff] [blame] | 5763 | if( !pCur->wrFlag ){ |
| 5764 | return SQLITE_PERM; /* Did not open this cursor for writing */ |
| 5765 | } |
drh | 980b1a7 | 2006-08-16 16:42:48 +0000 | [diff] [blame] | 5766 | if( checkReadLocks(pCur->pBtree, pCur->pgnoRoot, pCur) ){ |
drh | f74b8d9 | 2002-09-01 23:20:45 +0000 | [diff] [blame] | 5767 | return SQLITE_LOCKED; /* The table pCur points to has a read lock */ |
| 5768 | } |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 5769 | |
| 5770 | /* Restore the current cursor position (a no-op if the cursor is not in |
| 5771 | ** CURSOR_REQUIRESEEK state) and save the positions of any other cursors |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 5772 | ** open on the same table. Then call sqlite3PagerWrite() on the page |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 5773 | ** that the entry will be deleted from. |
| 5774 | */ |
| 5775 | if( |
drh | bf700f3 | 2007-03-31 02:36:44 +0000 | [diff] [blame] | 5776 | (rc = restoreOrClearCursorPosition(pCur))!=0 || |
drh | d116739 | 2006-01-23 13:00:35 +0000 | [diff] [blame] | 5777 | (rc = saveAllCursors(pBt, pCur->pgnoRoot, pCur))!=0 || |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 5778 | (rc = sqlite3PagerWrite(pPage->pDbPage))!=0 |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 5779 | ){ |
| 5780 | return rc; |
| 5781 | } |
danielk1977 | e6efa74 | 2004-11-10 11:55:10 +0000 | [diff] [blame] | 5782 | |
drh | 85b623f | 2007-12-13 21:54:09 +0000 | [diff] [blame] | 5783 | /* Locate the cell within its page and leave pCell pointing to the |
danielk1977 | e6efa74 | 2004-11-10 11:55:10 +0000 | [diff] [blame] | 5784 | ** data. The clearCell() call frees any overflow pages associated with the |
| 5785 | ** cell. The cell itself is still intact. |
| 5786 | */ |
danielk1977 | 1cc5ed8 | 2007-05-16 17:28:43 +0000 | [diff] [blame] | 5787 | pCell = findCell(pPage, pCur->idx); |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 5788 | if( !pPage->leaf ){ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5789 | pgnoChild = get4byte(pCell); |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 5790 | } |
danielk1977 | 2812956 | 2005-01-11 10:25:06 +0000 | [diff] [blame] | 5791 | rc = clearCell(pPage, pCell); |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 5792 | if( rc ){ |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 5793 | return rc; |
| 5794 | } |
danielk1977 | e6efa74 | 2004-11-10 11:55:10 +0000 | [diff] [blame] | 5795 | |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 5796 | if( !pPage->leaf ){ |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 5797 | /* |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 5798 | ** 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] | 5799 | ** do something we will leave a hole on an internal page. |
| 5800 | ** We have to fill the hole by moving in a cell from a leaf. The |
| 5801 | ** next Cell after the one to be deleted is guaranteed to exist and |
danielk1977 | 299b187 | 2004-11-22 10:02:10 +0000 | [diff] [blame] | 5802 | ** to be a leaf so we can use it. |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 5803 | */ |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 5804 | BtCursor leafCur; |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 5805 | unsigned char *pNext; |
danielk1977 | 299b187 | 2004-11-22 10:02:10 +0000 | [diff] [blame] | 5806 | int notUsed; |
danielk1977 | 6b456a2 | 2005-03-21 04:04:02 +0000 | [diff] [blame] | 5807 | unsigned char *tempCell = 0; |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 5808 | assert( !pPage->leafData ); |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 5809 | sqlite3BtreeGetTempCursor(pCur, &leafCur); |
danielk1977 | 299b187 | 2004-11-22 10:02:10 +0000 | [diff] [blame] | 5810 | rc = sqlite3BtreeNext(&leafCur, ¬Used); |
danielk1977 | 6b456a2 | 2005-03-21 04:04:02 +0000 | [diff] [blame] | 5811 | if( rc==SQLITE_OK ){ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 5812 | rc = sqlite3PagerWrite(leafCur.pPage->pDbPage); |
danielk1977 | 6b456a2 | 2005-03-21 04:04:02 +0000 | [diff] [blame] | 5813 | } |
| 5814 | if( rc==SQLITE_OK ){ |
drh | a9121e4 | 2008-02-19 14:59:35 +0000 | [diff] [blame] | 5815 | u16 szNext; |
danielk1977 | 6b456a2 | 2005-03-21 04:04:02 +0000 | [diff] [blame] | 5816 | TRACE(("DELETE: table=%d delete internal from %d replace from leaf %d\n", |
| 5817 | pCur->pgnoRoot, pPage->pgno, leafCur.pPage->pgno)); |
| 5818 | dropCell(pPage, pCur->idx, cellSizePtr(pPage, pCell)); |
danielk1977 | 1cc5ed8 | 2007-05-16 17:28:43 +0000 | [diff] [blame] | 5819 | pNext = findCell(leafCur.pPage, leafCur.idx); |
danielk1977 | 6b456a2 | 2005-03-21 04:04:02 +0000 | [diff] [blame] | 5820 | szNext = cellSizePtr(leafCur.pPage, pNext); |
| 5821 | assert( MX_CELL_SIZE(pBt)>=szNext+4 ); |
danielk1977 | 52ae724 | 2008-03-25 14:24:56 +0000 | [diff] [blame] | 5822 | allocateTempSpace(pBt); |
| 5823 | tempCell = pBt->pTmpSpace; |
danielk1977 | 6b456a2 | 2005-03-21 04:04:02 +0000 | [diff] [blame] | 5824 | if( tempCell==0 ){ |
| 5825 | rc = SQLITE_NOMEM; |
| 5826 | } |
danielk1977 | 8ea1cfa | 2008-01-01 06:19:02 +0000 | [diff] [blame] | 5827 | if( rc==SQLITE_OK ){ |
| 5828 | rc = insertCell(pPage, pCur->idx, pNext-4, szNext+4, tempCell, 0); |
| 5829 | } |
| 5830 | if( rc==SQLITE_OK ){ |
| 5831 | put4byte(findOverflowCell(pPage, pCur->idx), pgnoChild); |
| 5832 | rc = balance(pPage, 0); |
| 5833 | } |
| 5834 | if( rc==SQLITE_OK ){ |
| 5835 | dropCell(leafCur.pPage, leafCur.idx, szNext); |
| 5836 | rc = balance(leafCur.pPage, 0); |
| 5837 | } |
danielk1977 | 6b456a2 | 2005-03-21 04:04:02 +0000 | [diff] [blame] | 5838 | } |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 5839 | sqlite3BtreeReleaseTempCursor(&leafCur); |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 5840 | }else{ |
danielk1977 | 299b187 | 2004-11-22 10:02:10 +0000 | [diff] [blame] | 5841 | TRACE(("DELETE: table=%d delete from leaf %d\n", |
| 5842 | pCur->pgnoRoot, pPage->pgno)); |
| 5843 | dropCell(pPage, pCur->idx, cellSizePtr(pPage, pCell)); |
danielk1977 | ac245ec | 2005-01-14 13:50:11 +0000 | [diff] [blame] | 5844 | rc = balance(pPage, 0); |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 5845 | } |
danielk1977 | 6b456a2 | 2005-03-21 04:04:02 +0000 | [diff] [blame] | 5846 | if( rc==SQLITE_OK ){ |
| 5847 | moveToRoot(pCur); |
| 5848 | } |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 5849 | return rc; |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 5850 | } |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 5851 | |
| 5852 | /* |
drh | c6b52df | 2002-01-04 03:09:29 +0000 | [diff] [blame] | 5853 | ** Create a new BTree table. Write into *piTable the page |
| 5854 | ** number for the root page of the new table. |
| 5855 | ** |
drh | ab01f61 | 2004-05-22 02:55:23 +0000 | [diff] [blame] | 5856 | ** The type of type is determined by the flags parameter. Only the |
| 5857 | ** following values of flags are currently in use. Other values for |
| 5858 | ** flags might not work: |
| 5859 | ** |
| 5860 | ** BTREE_INTKEY|BTREE_LEAFDATA Used for SQL tables with rowid keys |
| 5861 | ** BTREE_ZERODATA Used for SQL indices |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 5862 | */ |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 5863 | static int btreeCreateTable(Btree *p, int *piTable, int flags){ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 5864 | BtShared *pBt = p->pBt; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 5865 | MemPage *pRoot; |
| 5866 | Pgno pgnoRoot; |
| 5867 | int rc; |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 5868 | |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 5869 | assert( sqlite3BtreeHoldsMutex(p) ); |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 5870 | if( pBt->inTransaction!=TRANS_WRITE ){ |
drh | f74b8d9 | 2002-09-01 23:20:45 +0000 | [diff] [blame] | 5871 | /* Must start a transaction first */ |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 5872 | rc = pBt->readOnly ? SQLITE_READONLY : SQLITE_ERROR; |
| 5873 | return rc; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 5874 | } |
danielk1977 | 2812956 | 2005-01-11 10:25:06 +0000 | [diff] [blame] | 5875 | assert( !pBt->readOnly ); |
danielk1977 | e6efa74 | 2004-11-10 11:55:10 +0000 | [diff] [blame] | 5876 | |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 5877 | #ifdef SQLITE_OMIT_AUTOVACUUM |
drh | 4f0c587 | 2007-03-26 22:05:01 +0000 | [diff] [blame] | 5878 | rc = allocateBtreePage(pBt, &pRoot, &pgnoRoot, 1, 0); |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 5879 | if( rc ){ |
| 5880 | return rc; |
| 5881 | } |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 5882 | #else |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 5883 | if( pBt->autoVacuum ){ |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 5884 | Pgno pgnoMove; /* Move a page here to make room for the root-page */ |
| 5885 | MemPage *pPageMove; /* The page to move to. */ |
| 5886 | |
danielk1977 | 20713f3 | 2007-05-03 11:43:33 +0000 | [diff] [blame] | 5887 | /* Creating a new table may probably require moving an existing database |
| 5888 | ** to make room for the new tables root page. In case this page turns |
| 5889 | ** out to be an overflow page, delete all overflow page-map caches |
| 5890 | ** held by open cursors. |
| 5891 | */ |
danielk1977 | 92d4d7a | 2007-05-04 12:05:56 +0000 | [diff] [blame] | 5892 | invalidateAllOverflowCache(pBt); |
danielk1977 | 20713f3 | 2007-05-03 11:43:33 +0000 | [diff] [blame] | 5893 | |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 5894 | /* Read the value of meta[3] from the database to determine where the |
| 5895 | ** root page of the new table should go. meta[3] is the largest root-page |
| 5896 | ** created so far, so the new root-page is (meta[3]+1). |
| 5897 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 5898 | rc = sqlite3BtreeGetMeta(p, 4, &pgnoRoot); |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 5899 | if( rc!=SQLITE_OK ){ |
| 5900 | return rc; |
| 5901 | } |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 5902 | pgnoRoot++; |
| 5903 | |
danielk1977 | 599fcba | 2004-11-08 07:13:13 +0000 | [diff] [blame] | 5904 | /* The new root-page may not be allocated on a pointer-map page, or the |
| 5905 | ** PENDING_BYTE page. |
| 5906 | */ |
drh | 7219043 | 2008-01-31 14:54:43 +0000 | [diff] [blame] | 5907 | while( pgnoRoot==PTRMAP_PAGENO(pBt, pgnoRoot) || |
danielk1977 | 599fcba | 2004-11-08 07:13:13 +0000 | [diff] [blame] | 5908 | pgnoRoot==PENDING_BYTE_PAGE(pBt) ){ |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 5909 | pgnoRoot++; |
| 5910 | } |
| 5911 | assert( pgnoRoot>=3 ); |
| 5912 | |
| 5913 | /* Allocate a page. The page that currently resides at pgnoRoot will |
| 5914 | ** be moved to the allocated page (unless the allocated page happens |
| 5915 | ** to reside at pgnoRoot). |
| 5916 | */ |
drh | 4f0c587 | 2007-03-26 22:05:01 +0000 | [diff] [blame] | 5917 | rc = allocateBtreePage(pBt, &pPageMove, &pgnoMove, pgnoRoot, 1); |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 5918 | if( rc!=SQLITE_OK ){ |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 5919 | return rc; |
| 5920 | } |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 5921 | |
| 5922 | if( pgnoMove!=pgnoRoot ){ |
danielk1977 | f35843b | 2007-04-07 15:03:17 +0000 | [diff] [blame] | 5923 | /* pgnoRoot is the page that will be used for the root-page of |
| 5924 | ** the new table (assuming an error did not occur). But we were |
| 5925 | ** allocated pgnoMove. If required (i.e. if it was not allocated |
| 5926 | ** by extending the file), the current page at position pgnoMove |
| 5927 | ** is already journaled. |
| 5928 | */ |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 5929 | u8 eType; |
| 5930 | Pgno iPtrPage; |
| 5931 | |
| 5932 | releasePage(pPageMove); |
danielk1977 | f35843b | 2007-04-07 15:03:17 +0000 | [diff] [blame] | 5933 | |
| 5934 | /* Move the page currently at pgnoRoot to pgnoMove. */ |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 5935 | rc = sqlite3BtreeGetPage(pBt, pgnoRoot, &pRoot, 0); |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 5936 | if( rc!=SQLITE_OK ){ |
| 5937 | return rc; |
| 5938 | } |
| 5939 | rc = ptrmapGet(pBt, pgnoRoot, &eType, &iPtrPage); |
drh | ccae602 | 2005-02-26 17:31:26 +0000 | [diff] [blame] | 5940 | if( rc!=SQLITE_OK || eType==PTRMAP_ROOTPAGE || eType==PTRMAP_FREEPAGE ){ |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 5941 | releasePage(pRoot); |
| 5942 | return rc; |
| 5943 | } |
drh | ccae602 | 2005-02-26 17:31:26 +0000 | [diff] [blame] | 5944 | assert( eType!=PTRMAP_ROOTPAGE ); |
| 5945 | assert( eType!=PTRMAP_FREEPAGE ); |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 5946 | rc = sqlite3PagerWrite(pRoot->pDbPage); |
danielk1977 | 5fd057a | 2005-03-09 13:09:43 +0000 | [diff] [blame] | 5947 | if( rc!=SQLITE_OK ){ |
| 5948 | releasePage(pRoot); |
| 5949 | return rc; |
| 5950 | } |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 5951 | rc = relocatePage(pBt, pRoot, eType, iPtrPage, pgnoMove); |
| 5952 | releasePage(pRoot); |
danielk1977 | f35843b | 2007-04-07 15:03:17 +0000 | [diff] [blame] | 5953 | |
| 5954 | /* Obtain the page at pgnoRoot */ |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 5955 | if( rc!=SQLITE_OK ){ |
| 5956 | return rc; |
| 5957 | } |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 5958 | rc = sqlite3BtreeGetPage(pBt, pgnoRoot, &pRoot, 0); |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 5959 | if( rc!=SQLITE_OK ){ |
| 5960 | return rc; |
| 5961 | } |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 5962 | rc = sqlite3PagerWrite(pRoot->pDbPage); |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 5963 | if( rc!=SQLITE_OK ){ |
| 5964 | releasePage(pRoot); |
| 5965 | return rc; |
| 5966 | } |
| 5967 | }else{ |
| 5968 | pRoot = pPageMove; |
| 5969 | } |
| 5970 | |
danielk1977 | 42741be | 2005-01-08 12:42:39 +0000 | [diff] [blame] | 5971 | /* Update the pointer-map and meta-data with the new root-page number. */ |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 5972 | rc = ptrmapPut(pBt, pgnoRoot, PTRMAP_ROOTPAGE, 0); |
| 5973 | if( rc ){ |
| 5974 | releasePage(pRoot); |
| 5975 | return rc; |
| 5976 | } |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 5977 | rc = sqlite3BtreeUpdateMeta(p, 4, pgnoRoot); |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 5978 | if( rc ){ |
| 5979 | releasePage(pRoot); |
| 5980 | return rc; |
| 5981 | } |
danielk1977 | 42741be | 2005-01-08 12:42:39 +0000 | [diff] [blame] | 5982 | |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 5983 | }else{ |
drh | 4f0c587 | 2007-03-26 22:05:01 +0000 | [diff] [blame] | 5984 | rc = allocateBtreePage(pBt, &pRoot, &pgnoRoot, 1, 0); |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 5985 | if( rc ) return rc; |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 5986 | } |
| 5987 | #endif |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 5988 | assert( sqlite3PagerIswriteable(pRoot->pDbPage) ); |
drh | de64713 | 2004-05-07 17:57:49 +0000 | [diff] [blame] | 5989 | zeroPage(pRoot, flags | PTF_LEAF); |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 5990 | sqlite3PagerUnref(pRoot->pDbPage); |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 5991 | *piTable = (int)pgnoRoot; |
| 5992 | return SQLITE_OK; |
| 5993 | } |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 5994 | int sqlite3BtreeCreateTable(Btree *p, int *piTable, int flags){ |
| 5995 | int rc; |
| 5996 | sqlite3BtreeEnter(p); |
drh | e5fe690 | 2007-12-07 18:55:28 +0000 | [diff] [blame] | 5997 | p->pBt->db = p->db; |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 5998 | rc = btreeCreateTable(p, piTable, flags); |
| 5999 | sqlite3BtreeLeave(p); |
| 6000 | return rc; |
| 6001 | } |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 6002 | |
| 6003 | /* |
| 6004 | ** Erase the given database page and all its children. Return |
| 6005 | ** the page to the freelist. |
| 6006 | */ |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 6007 | static int clearDatabasePage( |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 6008 | BtShared *pBt, /* The BTree that contains the table */ |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 6009 | Pgno pgno, /* Page number to clear */ |
| 6010 | MemPage *pParent, /* Parent page. NULL for the root */ |
| 6011 | int freePageFlag /* Deallocate page if true */ |
| 6012 | ){ |
danielk1977 | 6b456a2 | 2005-03-21 04:04:02 +0000 | [diff] [blame] | 6013 | MemPage *pPage = 0; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 6014 | int rc; |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 6015 | unsigned char *pCell; |
| 6016 | int i; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 6017 | |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 6018 | assert( sqlite3_mutex_held(pBt->mutex) ); |
danielk1977 | ad0132d | 2008-06-07 08:58:22 +0000 | [diff] [blame^] | 6019 | if( pgno>pagerPagecount(pBt->pPager) ){ |
drh | 4928570 | 2005-09-17 15:20:26 +0000 | [diff] [blame] | 6020 | return SQLITE_CORRUPT_BKPT; |
danielk1977 | a1cb183 | 2005-02-12 08:59:55 +0000 | [diff] [blame] | 6021 | } |
| 6022 | |
drh | de64713 | 2004-05-07 17:57:49 +0000 | [diff] [blame] | 6023 | rc = getAndInitPage(pBt, pgno, &pPage, pParent); |
danielk1977 | 6b456a2 | 2005-03-21 04:04:02 +0000 | [diff] [blame] | 6024 | if( rc ) goto cleardatabasepage_out; |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 6025 | for(i=0; i<pPage->nCell; i++){ |
danielk1977 | 1cc5ed8 | 2007-05-16 17:28:43 +0000 | [diff] [blame] | 6026 | pCell = findCell(pPage, i); |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 6027 | if( !pPage->leaf ){ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 6028 | rc = clearDatabasePage(pBt, get4byte(pCell), pPage->pParent, 1); |
danielk1977 | 6b456a2 | 2005-03-21 04:04:02 +0000 | [diff] [blame] | 6029 | if( rc ) goto cleardatabasepage_out; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 6030 | } |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 6031 | rc = clearCell(pPage, pCell); |
danielk1977 | 6b456a2 | 2005-03-21 04:04:02 +0000 | [diff] [blame] | 6032 | if( rc ) goto cleardatabasepage_out; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 6033 | } |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 6034 | if( !pPage->leaf ){ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 6035 | rc = clearDatabasePage(pBt, get4byte(&pPage->aData[8]), pPage->pParent, 1); |
danielk1977 | 6b456a2 | 2005-03-21 04:04:02 +0000 | [diff] [blame] | 6036 | if( rc ) goto cleardatabasepage_out; |
drh | 2aa679f | 2001-06-25 02:11:07 +0000 | [diff] [blame] | 6037 | } |
| 6038 | if( freePageFlag ){ |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 6039 | rc = freePage(pPage); |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 6040 | }else if( (rc = sqlite3PagerWrite(pPage->pDbPage))==0 ){ |
drh | 3a4c141 | 2004-05-09 20:40:11 +0000 | [diff] [blame] | 6041 | zeroPage(pPage, pPage->aData[0] | PTF_LEAF); |
drh | 2aa679f | 2001-06-25 02:11:07 +0000 | [diff] [blame] | 6042 | } |
danielk1977 | 6b456a2 | 2005-03-21 04:04:02 +0000 | [diff] [blame] | 6043 | |
| 6044 | cleardatabasepage_out: |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 6045 | releasePage(pPage); |
drh | 2aa679f | 2001-06-25 02:11:07 +0000 | [diff] [blame] | 6046 | return rc; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 6047 | } |
| 6048 | |
| 6049 | /* |
drh | ab01f61 | 2004-05-22 02:55:23 +0000 | [diff] [blame] | 6050 | ** Delete all information from a single table in the database. iTable is |
| 6051 | ** the page number of the root of the table. After this routine returns, |
| 6052 | ** the root page is empty, but still exists. |
| 6053 | ** |
| 6054 | ** This routine will fail with SQLITE_LOCKED if there are any open |
| 6055 | ** read cursors on the table. Open write cursors are moved to the |
| 6056 | ** root of the table. |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 6057 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 6058 | int sqlite3BtreeClearTable(Btree *p, int iTable){ |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 6059 | int rc; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 6060 | BtShared *pBt = p->pBt; |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 6061 | sqlite3BtreeEnter(p); |
drh | e5fe690 | 2007-12-07 18:55:28 +0000 | [diff] [blame] | 6062 | pBt->db = p->db; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 6063 | if( p->inTrans!=TRANS_WRITE ){ |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 6064 | rc = pBt->readOnly ? SQLITE_READONLY : SQLITE_ERROR; |
| 6065 | }else if( (rc = checkReadLocks(p, iTable, 0))!=SQLITE_OK ){ |
| 6066 | /* nothing to do */ |
| 6067 | }else if( SQLITE_OK!=(rc = saveAllCursors(pBt, iTable, 0)) ){ |
| 6068 | /* nothing to do */ |
| 6069 | }else{ |
| 6070 | rc = clearDatabasePage(pBt, (Pgno)iTable, 0, 0); |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 6071 | } |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 6072 | sqlite3BtreeLeave(p); |
| 6073 | return rc; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 6074 | } |
| 6075 | |
| 6076 | /* |
| 6077 | ** Erase all information in a table and add the root of the table to |
| 6078 | ** the freelist. Except, the root of the principle table (the one on |
drh | ab01f61 | 2004-05-22 02:55:23 +0000 | [diff] [blame] | 6079 | ** page 1) is never added to the freelist. |
| 6080 | ** |
| 6081 | ** This routine will fail with SQLITE_LOCKED if there are any open |
| 6082 | ** cursors on the table. |
drh | 205f48e | 2004-11-05 00:43:11 +0000 | [diff] [blame] | 6083 | ** |
| 6084 | ** If AUTOVACUUM is enabled and the page at iTable is not the last |
| 6085 | ** root page in the database file, then the last root page |
| 6086 | ** in the database file is moved into the slot formerly occupied by |
| 6087 | ** iTable and that last slot formerly occupied by the last root page |
| 6088 | ** is added to the freelist instead of iTable. In this say, all |
| 6089 | ** root pages are kept at the beginning of the database file, which |
| 6090 | ** is necessary for AUTOVACUUM to work right. *piMoved is set to the |
| 6091 | ** page number that used to be the last root page in the file before |
| 6092 | ** the move. If no page gets moved, *piMoved is set to 0. |
| 6093 | ** The last root page is recorded in meta[3] and the value of |
| 6094 | ** meta[3] is updated by this procedure. |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 6095 | */ |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 6096 | static int btreeDropTable(Btree *p, int iTable, int *piMoved){ |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 6097 | int rc; |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 6098 | MemPage *pPage = 0; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 6099 | BtShared *pBt = p->pBt; |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 6100 | |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 6101 | assert( sqlite3BtreeHoldsMutex(p) ); |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 6102 | if( p->inTrans!=TRANS_WRITE ){ |
drh | f74b8d9 | 2002-09-01 23:20:45 +0000 | [diff] [blame] | 6103 | return pBt->readOnly ? SQLITE_READONLY : SQLITE_ERROR; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 6104 | } |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 6105 | |
danielk1977 | e6efa74 | 2004-11-10 11:55:10 +0000 | [diff] [blame] | 6106 | /* It is illegal to drop a table if any cursors are open on the |
| 6107 | ** database. This is because in auto-vacuum mode the backend may |
| 6108 | ** need to move another root-page to fill a gap left by the deleted |
| 6109 | ** root page. If an open cursor was using this page a problem would |
| 6110 | ** occur. |
| 6111 | */ |
| 6112 | if( pBt->pCursor ){ |
| 6113 | return SQLITE_LOCKED; |
drh | 5df72a5 | 2002-06-06 23:16:05 +0000 | [diff] [blame] | 6114 | } |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 6115 | |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 6116 | rc = sqlite3BtreeGetPage(pBt, (Pgno)iTable, &pPage, 0); |
drh | 2aa679f | 2001-06-25 02:11:07 +0000 | [diff] [blame] | 6117 | if( rc ) return rc; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 6118 | rc = sqlite3BtreeClearTable(p, iTable); |
danielk1977 | 6b456a2 | 2005-03-21 04:04:02 +0000 | [diff] [blame] | 6119 | if( rc ){ |
| 6120 | releasePage(pPage); |
| 6121 | return rc; |
| 6122 | } |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 6123 | |
drh | 205f48e | 2004-11-05 00:43:11 +0000 | [diff] [blame] | 6124 | *piMoved = 0; |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 6125 | |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 6126 | if( iTable>1 ){ |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 6127 | #ifdef SQLITE_OMIT_AUTOVACUUM |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 6128 | rc = freePage(pPage); |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 6129 | releasePage(pPage); |
| 6130 | #else |
| 6131 | if( pBt->autoVacuum ){ |
| 6132 | Pgno maxRootPgno; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 6133 | rc = sqlite3BtreeGetMeta(p, 4, &maxRootPgno); |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 6134 | if( rc!=SQLITE_OK ){ |
| 6135 | releasePage(pPage); |
| 6136 | return rc; |
| 6137 | } |
| 6138 | |
| 6139 | if( iTable==maxRootPgno ){ |
| 6140 | /* If the table being dropped is the table with the largest root-page |
| 6141 | ** number in the database, put the root page on the free list. |
| 6142 | */ |
| 6143 | rc = freePage(pPage); |
| 6144 | releasePage(pPage); |
| 6145 | if( rc!=SQLITE_OK ){ |
| 6146 | return rc; |
| 6147 | } |
| 6148 | }else{ |
| 6149 | /* The table being dropped does not have the largest root-page |
| 6150 | ** number in the database. So move the page that does into the |
| 6151 | ** gap left by the deleted root-page. |
| 6152 | */ |
| 6153 | MemPage *pMove; |
| 6154 | releasePage(pPage); |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 6155 | rc = sqlite3BtreeGetPage(pBt, maxRootPgno, &pMove, 0); |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 6156 | if( rc!=SQLITE_OK ){ |
| 6157 | return rc; |
| 6158 | } |
| 6159 | rc = relocatePage(pBt, pMove, PTRMAP_ROOTPAGE, 0, iTable); |
| 6160 | releasePage(pMove); |
| 6161 | if( rc!=SQLITE_OK ){ |
| 6162 | return rc; |
| 6163 | } |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 6164 | rc = sqlite3BtreeGetPage(pBt, maxRootPgno, &pMove, 0); |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 6165 | if( rc!=SQLITE_OK ){ |
| 6166 | return rc; |
| 6167 | } |
| 6168 | rc = freePage(pMove); |
| 6169 | releasePage(pMove); |
| 6170 | if( rc!=SQLITE_OK ){ |
| 6171 | return rc; |
| 6172 | } |
| 6173 | *piMoved = maxRootPgno; |
| 6174 | } |
| 6175 | |
danielk1977 | 599fcba | 2004-11-08 07:13:13 +0000 | [diff] [blame] | 6176 | /* Set the new 'max-root-page' value in the database header. This |
| 6177 | ** is the old value less one, less one more if that happens to |
| 6178 | ** be a root-page number, less one again if that is the |
| 6179 | ** PENDING_BYTE_PAGE. |
| 6180 | */ |
danielk1977 | 87a6e73 | 2004-11-05 12:58:25 +0000 | [diff] [blame] | 6181 | maxRootPgno--; |
danielk1977 | 599fcba | 2004-11-08 07:13:13 +0000 | [diff] [blame] | 6182 | if( maxRootPgno==PENDING_BYTE_PAGE(pBt) ){ |
| 6183 | maxRootPgno--; |
| 6184 | } |
danielk1977 | 266664d | 2006-02-10 08:24:21 +0000 | [diff] [blame] | 6185 | if( maxRootPgno==PTRMAP_PAGENO(pBt, maxRootPgno) ){ |
danielk1977 | 87a6e73 | 2004-11-05 12:58:25 +0000 | [diff] [blame] | 6186 | maxRootPgno--; |
| 6187 | } |
danielk1977 | 599fcba | 2004-11-08 07:13:13 +0000 | [diff] [blame] | 6188 | assert( maxRootPgno!=PENDING_BYTE_PAGE(pBt) ); |
| 6189 | |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 6190 | rc = sqlite3BtreeUpdateMeta(p, 4, maxRootPgno); |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 6191 | }else{ |
| 6192 | rc = freePage(pPage); |
| 6193 | releasePage(pPage); |
| 6194 | } |
| 6195 | #endif |
drh | 2aa679f | 2001-06-25 02:11:07 +0000 | [diff] [blame] | 6196 | }else{ |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 6197 | /* If sqlite3BtreeDropTable was called on page 1. */ |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 6198 | zeroPage(pPage, PTF_INTKEY|PTF_LEAF ); |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 6199 | releasePage(pPage); |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 6200 | } |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 6201 | return rc; |
| 6202 | } |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 6203 | int sqlite3BtreeDropTable(Btree *p, int iTable, int *piMoved){ |
| 6204 | int rc; |
| 6205 | sqlite3BtreeEnter(p); |
drh | e5fe690 | 2007-12-07 18:55:28 +0000 | [diff] [blame] | 6206 | p->pBt->db = p->db; |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 6207 | rc = btreeDropTable(p, iTable, piMoved); |
| 6208 | sqlite3BtreeLeave(p); |
| 6209 | return rc; |
| 6210 | } |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 6211 | |
drh | 001bbcb | 2003-03-19 03:14:00 +0000 | [diff] [blame] | 6212 | |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 6213 | /* |
drh | 23e11ca | 2004-05-04 17:27:28 +0000 | [diff] [blame] | 6214 | ** Read the meta-information out of a database file. Meta[0] |
| 6215 | ** is the number of free pages currently in the database. Meta[1] |
drh | a3b321d | 2004-05-11 09:31:31 +0000 | [diff] [blame] | 6216 | ** through meta[15] are available for use by higher layers. Meta[0] |
| 6217 | ** is read-only, the others are read/write. |
| 6218 | ** |
| 6219 | ** The schema layer numbers meta values differently. At the schema |
| 6220 | ** layer (and the SetCookie and ReadCookie opcodes) the number of |
| 6221 | ** free pages is not visible. So Cookie[0] is the same as Meta[1]. |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 6222 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 6223 | int sqlite3BtreeGetMeta(Btree *p, int idx, u32 *pMeta){ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 6224 | DbPage *pDbPage; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 6225 | int rc; |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 6226 | unsigned char *pP1; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 6227 | BtShared *pBt = p->pBt; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 6228 | |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 6229 | sqlite3BtreeEnter(p); |
drh | e5fe690 | 2007-12-07 18:55:28 +0000 | [diff] [blame] | 6230 | pBt->db = p->db; |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 6231 | |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 6232 | /* Reading a meta-data value requires a read-lock on page 1 (and hence |
| 6233 | ** the sqlite_master table. We grab this lock regardless of whether or |
| 6234 | ** not the SQLITE_ReadUncommitted flag is set (the table rooted at page |
| 6235 | ** 1 is treated as a special case by queryTableLock() and lockTable()). |
| 6236 | */ |
| 6237 | rc = queryTableLock(p, 1, READ_LOCK); |
| 6238 | if( rc!=SQLITE_OK ){ |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 6239 | sqlite3BtreeLeave(p); |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 6240 | return rc; |
| 6241 | } |
| 6242 | |
drh | 23e11ca | 2004-05-04 17:27:28 +0000 | [diff] [blame] | 6243 | assert( idx>=0 && idx<=15 ); |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 6244 | rc = sqlite3PagerGet(pBt->pPager, 1, &pDbPage); |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 6245 | if( rc ){ |
| 6246 | sqlite3BtreeLeave(p); |
| 6247 | return rc; |
| 6248 | } |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 6249 | pP1 = (unsigned char *)sqlite3PagerGetData(pDbPage); |
drh | 23e11ca | 2004-05-04 17:27:28 +0000 | [diff] [blame] | 6250 | *pMeta = get4byte(&pP1[36 + idx*4]); |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 6251 | sqlite3PagerUnref(pDbPage); |
drh | ae15787 | 2004-08-14 19:20:09 +0000 | [diff] [blame] | 6252 | |
danielk1977 | 599fcba | 2004-11-08 07:13:13 +0000 | [diff] [blame] | 6253 | /* If autovacuumed is disabled in this build but we are trying to |
| 6254 | ** access an autovacuumed database, then make the database readonly. |
| 6255 | */ |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 6256 | #ifdef SQLITE_OMIT_AUTOVACUUM |
drh | ae15787 | 2004-08-14 19:20:09 +0000 | [diff] [blame] | 6257 | if( idx==4 && *pMeta>0 ) pBt->readOnly = 1; |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 6258 | #endif |
drh | ae15787 | 2004-08-14 19:20:09 +0000 | [diff] [blame] | 6259 | |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 6260 | /* Grab the read-lock on page 1. */ |
| 6261 | rc = lockTable(p, 1, READ_LOCK); |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 6262 | sqlite3BtreeLeave(p); |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 6263 | return rc; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 6264 | } |
| 6265 | |
| 6266 | /* |
drh | 23e11ca | 2004-05-04 17:27:28 +0000 | [diff] [blame] | 6267 | ** Write meta-information back into the database. Meta[0] is |
| 6268 | ** read-only and may not be written. |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 6269 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 6270 | int sqlite3BtreeUpdateMeta(Btree *p, int idx, u32 iMeta){ |
| 6271 | BtShared *pBt = p->pBt; |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 6272 | unsigned char *pP1; |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 6273 | int rc; |
drh | 23e11ca | 2004-05-04 17:27:28 +0000 | [diff] [blame] | 6274 | assert( idx>=1 && idx<=15 ); |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 6275 | sqlite3BtreeEnter(p); |
drh | e5fe690 | 2007-12-07 18:55:28 +0000 | [diff] [blame] | 6276 | pBt->db = p->db; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 6277 | if( p->inTrans!=TRANS_WRITE ){ |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 6278 | rc = pBt->readOnly ? SQLITE_READONLY : SQLITE_ERROR; |
| 6279 | }else{ |
| 6280 | assert( pBt->pPage1!=0 ); |
| 6281 | pP1 = pBt->pPage1->aData; |
| 6282 | rc = sqlite3PagerWrite(pBt->pPage1->pDbPage); |
| 6283 | if( rc==SQLITE_OK ){ |
| 6284 | put4byte(&pP1[36 + idx*4], iMeta); |
danielk1977 | 4152e67 | 2007-09-12 17:01:45 +0000 | [diff] [blame] | 6285 | #ifndef SQLITE_OMIT_AUTOVACUUM |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 6286 | if( idx==7 ){ |
| 6287 | assert( pBt->autoVacuum || iMeta==0 ); |
| 6288 | assert( iMeta==0 || iMeta==1 ); |
| 6289 | pBt->incrVacuum = iMeta; |
| 6290 | } |
danielk1977 | 4152e67 | 2007-09-12 17:01:45 +0000 | [diff] [blame] | 6291 | #endif |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 6292 | } |
drh | 5df72a5 | 2002-06-06 23:16:05 +0000 | [diff] [blame] | 6293 | } |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 6294 | sqlite3BtreeLeave(p); |
| 6295 | return rc; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 6296 | } |
drh | 8c42ca9 | 2001-06-22 19:15:00 +0000 | [diff] [blame] | 6297 | |
drh | f328bc8 | 2004-05-10 23:29:49 +0000 | [diff] [blame] | 6298 | /* |
| 6299 | ** Return the flag byte at the beginning of the page that the cursor |
| 6300 | ** is currently pointing to. |
| 6301 | */ |
| 6302 | int sqlite3BtreeFlags(BtCursor *pCur){ |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 6303 | /* TODO: What about CURSOR_REQUIRESEEK state? Probably need to call |
drh | 777e4c4 | 2006-01-13 04:31:58 +0000 | [diff] [blame] | 6304 | ** restoreOrClearCursorPosition() here. |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 6305 | */ |
danielk1977 | e448dc4 | 2008-01-02 11:50:51 +0000 | [diff] [blame] | 6306 | MemPage *pPage; |
| 6307 | restoreOrClearCursorPosition(pCur); |
| 6308 | pPage = pCur->pPage; |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 6309 | assert( cursorHoldsMutex(pCur) ); |
drh | d0679ed | 2007-08-28 22:24:34 +0000 | [diff] [blame] | 6310 | assert( pPage->pBt==pCur->pBt ); |
drh | f328bc8 | 2004-05-10 23:29:49 +0000 | [diff] [blame] | 6311 | return pPage ? pPage->aData[pPage->hdrOffset] : 0; |
| 6312 | } |
| 6313 | |
drh | dd79342 | 2001-06-28 01:54:48 +0000 | [diff] [blame] | 6314 | |
drh | dd79342 | 2001-06-28 01:54:48 +0000 | [diff] [blame] | 6315 | /* |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6316 | ** Return the pager associated with a BTree. This routine is used for |
| 6317 | ** testing and debugging only. |
drh | dd79342 | 2001-06-28 01:54:48 +0000 | [diff] [blame] | 6318 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 6319 | Pager *sqlite3BtreePager(Btree *p){ |
| 6320 | return p->pBt->pPager; |
drh | dd79342 | 2001-06-28 01:54:48 +0000 | [diff] [blame] | 6321 | } |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6322 | |
drh | b7f9164 | 2004-10-31 02:22:47 +0000 | [diff] [blame] | 6323 | #ifndef SQLITE_OMIT_INTEGRITY_CHECK |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6324 | /* |
| 6325 | ** Append a message to the error message string. |
| 6326 | */ |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 6327 | static void checkAppendMsg( |
| 6328 | IntegrityCk *pCheck, |
| 6329 | char *zMsg1, |
| 6330 | const char *zFormat, |
| 6331 | ... |
| 6332 | ){ |
| 6333 | va_list ap; |
| 6334 | char *zMsg2; |
drh | 1dcdbc0 | 2007-01-27 02:24:54 +0000 | [diff] [blame] | 6335 | if( !pCheck->mxErr ) return; |
| 6336 | pCheck->mxErr--; |
| 6337 | pCheck->nErr++; |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 6338 | va_start(ap, zFormat); |
danielk1977 | 1e53695 | 2007-08-16 10:09:01 +0000 | [diff] [blame] | 6339 | zMsg2 = sqlite3VMPrintf(0, zFormat, ap); |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 6340 | va_end(ap); |
| 6341 | if( zMsg1==0 ) zMsg1 = ""; |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6342 | if( pCheck->zErrMsg ){ |
| 6343 | char *zOld = pCheck->zErrMsg; |
| 6344 | pCheck->zErrMsg = 0; |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 6345 | sqlite3SetString(&pCheck->zErrMsg, zOld, "\n", zMsg1, zMsg2, (char*)0); |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 6346 | sqlite3_free(zOld); |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6347 | }else{ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 6348 | sqlite3SetString(&pCheck->zErrMsg, zMsg1, zMsg2, (char*)0); |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6349 | } |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 6350 | sqlite3_free(zMsg2); |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6351 | } |
drh | b7f9164 | 2004-10-31 02:22:47 +0000 | [diff] [blame] | 6352 | #endif /* SQLITE_OMIT_INTEGRITY_CHECK */ |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6353 | |
drh | b7f9164 | 2004-10-31 02:22:47 +0000 | [diff] [blame] | 6354 | #ifndef SQLITE_OMIT_INTEGRITY_CHECK |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6355 | /* |
| 6356 | ** Add 1 to the reference count for page iPage. If this is the second |
| 6357 | ** reference to the page, add an error message to pCheck->zErrMsg. |
| 6358 | ** Return 1 if there are 2 ore more references to the page and 0 if |
| 6359 | ** if this is the first reference to the page. |
| 6360 | ** |
| 6361 | ** Also check that the page number is in bounds. |
| 6362 | */ |
drh | aaab572 | 2002-02-19 13:39:21 +0000 | [diff] [blame] | 6363 | static int checkRef(IntegrityCk *pCheck, int iPage, char *zContext){ |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6364 | if( iPage==0 ) return 1; |
drh | 0de8c11 | 2002-07-06 16:32:14 +0000 | [diff] [blame] | 6365 | if( iPage>pCheck->nPage || iPage<0 ){ |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 6366 | checkAppendMsg(pCheck, zContext, "invalid page number %d", iPage); |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6367 | return 1; |
| 6368 | } |
| 6369 | if( pCheck->anRef[iPage]==1 ){ |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 6370 | checkAppendMsg(pCheck, zContext, "2nd reference to page %d", iPage); |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6371 | return 1; |
| 6372 | } |
| 6373 | return (pCheck->anRef[iPage]++)>1; |
| 6374 | } |
| 6375 | |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 6376 | #ifndef SQLITE_OMIT_AUTOVACUUM |
| 6377 | /* |
| 6378 | ** Check that the entry in the pointer-map for page iChild maps to |
| 6379 | ** page iParent, pointer type ptrType. If not, append an error message |
| 6380 | ** to pCheck. |
| 6381 | */ |
| 6382 | static void checkPtrmap( |
| 6383 | IntegrityCk *pCheck, /* Integrity check context */ |
| 6384 | Pgno iChild, /* Child page number */ |
| 6385 | u8 eType, /* Expected pointer map type */ |
| 6386 | Pgno iParent, /* Expected pointer map parent page number */ |
| 6387 | char *zContext /* Context description (used for error msg) */ |
| 6388 | ){ |
| 6389 | int rc; |
| 6390 | u8 ePtrmapType; |
| 6391 | Pgno iPtrmapParent; |
| 6392 | |
| 6393 | rc = ptrmapGet(pCheck->pBt, iChild, &ePtrmapType, &iPtrmapParent); |
| 6394 | if( rc!=SQLITE_OK ){ |
| 6395 | checkAppendMsg(pCheck, zContext, "Failed to read ptrmap key=%d", iChild); |
| 6396 | return; |
| 6397 | } |
| 6398 | |
| 6399 | if( ePtrmapType!=eType || iPtrmapParent!=iParent ){ |
| 6400 | checkAppendMsg(pCheck, zContext, |
| 6401 | "Bad ptr map entry key=%d expected=(%d,%d) got=(%d,%d)", |
| 6402 | iChild, eType, iParent, ePtrmapType, iPtrmapParent); |
| 6403 | } |
| 6404 | } |
| 6405 | #endif |
| 6406 | |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6407 | /* |
| 6408 | ** Check the integrity of the freelist or of an overflow page list. |
| 6409 | ** Verify that the number of pages on the list is N. |
| 6410 | */ |
drh | 30e5875 | 2002-03-02 20:41:57 +0000 | [diff] [blame] | 6411 | static void checkList( |
| 6412 | IntegrityCk *pCheck, /* Integrity checking context */ |
| 6413 | int isFreeList, /* True for a freelist. False for overflow page list */ |
| 6414 | int iPage, /* Page number for first page in the list */ |
| 6415 | int N, /* Expected number of pages in the list */ |
| 6416 | char *zContext /* Context for error messages */ |
| 6417 | ){ |
| 6418 | int i; |
drh | 3a4c141 | 2004-05-09 20:40:11 +0000 | [diff] [blame] | 6419 | int expected = N; |
| 6420 | int iFirst = iPage; |
drh | 1dcdbc0 | 2007-01-27 02:24:54 +0000 | [diff] [blame] | 6421 | while( N-- > 0 && pCheck->mxErr ){ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 6422 | DbPage *pOvflPage; |
| 6423 | unsigned char *pOvflData; |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6424 | if( iPage<1 ){ |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 6425 | checkAppendMsg(pCheck, zContext, |
| 6426 | "%d of %d pages missing from overflow list starting at %d", |
drh | 3a4c141 | 2004-05-09 20:40:11 +0000 | [diff] [blame] | 6427 | N+1, expected, iFirst); |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6428 | break; |
| 6429 | } |
| 6430 | if( checkRef(pCheck, iPage, zContext) ) break; |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 6431 | if( sqlite3PagerGet(pCheck->pPager, (Pgno)iPage, &pOvflPage) ){ |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 6432 | checkAppendMsg(pCheck, zContext, "failed to get page %d", iPage); |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6433 | break; |
| 6434 | } |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 6435 | pOvflData = (unsigned char *)sqlite3PagerGetData(pOvflPage); |
drh | 30e5875 | 2002-03-02 20:41:57 +0000 | [diff] [blame] | 6436 | if( isFreeList ){ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 6437 | int n = get4byte(&pOvflData[4]); |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 6438 | #ifndef SQLITE_OMIT_AUTOVACUUM |
| 6439 | if( pCheck->pBt->autoVacuum ){ |
| 6440 | checkPtrmap(pCheck, iPage, PTRMAP_FREEPAGE, 0, zContext); |
| 6441 | } |
| 6442 | #endif |
drh | 855eb1c | 2004-08-31 13:45:11 +0000 | [diff] [blame] | 6443 | if( n>pCheck->pBt->usableSize/4-8 ){ |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 6444 | checkAppendMsg(pCheck, zContext, |
| 6445 | "freelist leaf count too big on page %d", iPage); |
drh | ee696e2 | 2004-08-30 16:52:17 +0000 | [diff] [blame] | 6446 | N--; |
| 6447 | }else{ |
| 6448 | for(i=0; i<n; i++){ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 6449 | Pgno iFreePage = get4byte(&pOvflData[8+i*4]); |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 6450 | #ifndef SQLITE_OMIT_AUTOVACUUM |
| 6451 | if( pCheck->pBt->autoVacuum ){ |
| 6452 | checkPtrmap(pCheck, iFreePage, PTRMAP_FREEPAGE, 0, zContext); |
| 6453 | } |
| 6454 | #endif |
| 6455 | checkRef(pCheck, iFreePage, zContext); |
drh | ee696e2 | 2004-08-30 16:52:17 +0000 | [diff] [blame] | 6456 | } |
| 6457 | N -= n; |
drh | 30e5875 | 2002-03-02 20:41:57 +0000 | [diff] [blame] | 6458 | } |
drh | 30e5875 | 2002-03-02 20:41:57 +0000 | [diff] [blame] | 6459 | } |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 6460 | #ifndef SQLITE_OMIT_AUTOVACUUM |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 6461 | else{ |
| 6462 | /* If this database supports auto-vacuum and iPage is not the last |
| 6463 | ** page in this overflow list, check that the pointer-map entry for |
| 6464 | ** the following page matches iPage. |
| 6465 | */ |
| 6466 | if( pCheck->pBt->autoVacuum && N>0 ){ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 6467 | i = get4byte(pOvflData); |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 6468 | checkPtrmap(pCheck, i, PTRMAP_OVERFLOW2, iPage, zContext); |
| 6469 | } |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 6470 | } |
| 6471 | #endif |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 6472 | iPage = get4byte(pOvflData); |
| 6473 | sqlite3PagerUnref(pOvflPage); |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6474 | } |
| 6475 | } |
drh | b7f9164 | 2004-10-31 02:22:47 +0000 | [diff] [blame] | 6476 | #endif /* SQLITE_OMIT_INTEGRITY_CHECK */ |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6477 | |
drh | b7f9164 | 2004-10-31 02:22:47 +0000 | [diff] [blame] | 6478 | #ifndef SQLITE_OMIT_INTEGRITY_CHECK |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6479 | /* |
| 6480 | ** Do various sanity checks on a single page of a tree. Return |
| 6481 | ** the tree depth. Root pages return 0. Parents of root pages |
| 6482 | ** return 1, and so forth. |
| 6483 | ** |
| 6484 | ** These checks are done: |
| 6485 | ** |
| 6486 | ** 1. Make sure that cells and freeblocks do not overlap |
| 6487 | ** but combine to completely cover the page. |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 6488 | ** NO 2. Make sure cell keys are in order. |
| 6489 | ** NO 3. Make sure no key is less than or equal to zLowerBound. |
| 6490 | ** NO 4. Make sure no key is greater than or equal to zUpperBound. |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6491 | ** 5. Check the integrity of overflow pages. |
| 6492 | ** 6. Recursively call checkTreePage on all children. |
| 6493 | ** 7. Verify that the depth of all children is the same. |
drh | 6019e16 | 2001-07-02 17:51:45 +0000 | [diff] [blame] | 6494 | ** 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] | 6495 | ** the root of the tree. |
| 6496 | */ |
| 6497 | static int checkTreePage( |
drh | aaab572 | 2002-02-19 13:39:21 +0000 | [diff] [blame] | 6498 | IntegrityCk *pCheck, /* Context for the sanity check */ |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6499 | int iPage, /* Page number of the page to check */ |
| 6500 | MemPage *pParent, /* Parent page */ |
drh | 7416170 | 2006-02-24 02:53:49 +0000 | [diff] [blame] | 6501 | char *zParentContext /* Parent context */ |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6502 | ){ |
| 6503 | MemPage *pPage; |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 6504 | int i, rc, depth, d2, pgno, cnt; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 6505 | int hdr, cellStart; |
| 6506 | int nCell; |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 6507 | u8 *data; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 6508 | BtShared *pBt; |
drh | 4f26bb6 | 2005-09-08 14:17:20 +0000 | [diff] [blame] | 6509 | int usableSize; |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6510 | char zContext[100]; |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 6511 | char *hit; |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6512 | |
drh | 5bb3eb9 | 2007-05-04 13:15:55 +0000 | [diff] [blame] | 6513 | sqlite3_snprintf(sizeof(zContext), zContext, "Page %d: ", iPage); |
danielk1977 | ef73ee9 | 2004-11-06 12:26:07 +0000 | [diff] [blame] | 6514 | |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6515 | /* Check that the page exists |
| 6516 | */ |
drh | d9cb6ac | 2005-10-20 07:28:17 +0000 | [diff] [blame] | 6517 | pBt = pCheck->pBt; |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 6518 | usableSize = pBt->usableSize; |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6519 | if( iPage==0 ) return 0; |
| 6520 | if( checkRef(pCheck, iPage, zParentContext) ) return 0; |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 6521 | if( (rc = sqlite3BtreeGetPage(pBt, (Pgno)iPage, &pPage, 0))!=0 ){ |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 6522 | checkAppendMsg(pCheck, zContext, |
| 6523 | "unable to get the page. error code=%d", rc); |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6524 | return 0; |
| 6525 | } |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 6526 | if( (rc = sqlite3BtreeInitPage(pPage, pParent))!=0 ){ |
| 6527 | checkAppendMsg(pCheck, zContext, |
| 6528 | "sqlite3BtreeInitPage() returns error code %d", rc); |
drh | 9102529 | 2004-05-03 19:49:32 +0000 | [diff] [blame] | 6529 | releasePage(pPage); |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6530 | return 0; |
| 6531 | } |
| 6532 | |
| 6533 | /* Check out all the cells. |
| 6534 | */ |
| 6535 | depth = 0; |
drh | 1dcdbc0 | 2007-01-27 02:24:54 +0000 | [diff] [blame] | 6536 | for(i=0; i<pPage->nCell && pCheck->mxErr; i++){ |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 6537 | u8 *pCell; |
| 6538 | int sz; |
| 6539 | CellInfo info; |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6540 | |
| 6541 | /* Check payload overflow pages |
| 6542 | */ |
drh | 5bb3eb9 | 2007-05-04 13:15:55 +0000 | [diff] [blame] | 6543 | sqlite3_snprintf(sizeof(zContext), zContext, |
| 6544 | "On tree page %d cell %d: ", iPage, i); |
danielk1977 | 1cc5ed8 | 2007-05-16 17:28:43 +0000 | [diff] [blame] | 6545 | pCell = findCell(pPage,i); |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 6546 | sqlite3BtreeParseCellPtr(pPage, pCell, &info); |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 6547 | sz = info.nData; |
| 6548 | if( !pPage->intKey ) sz += info.nKey; |
drh | 7236583 | 2007-03-06 15:53:44 +0000 | [diff] [blame] | 6549 | assert( sz==info.nPayload ); |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 6550 | if( sz>info.nLocal ){ |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 6551 | int nPage = (sz - info.nLocal + usableSize - 5)/(usableSize - 4); |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 6552 | Pgno pgnoOvfl = get4byte(&pCell[info.iOverflow]); |
| 6553 | #ifndef SQLITE_OMIT_AUTOVACUUM |
| 6554 | if( pBt->autoVacuum ){ |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 6555 | checkPtrmap(pCheck, pgnoOvfl, PTRMAP_OVERFLOW1, iPage, zContext); |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 6556 | } |
| 6557 | #endif |
| 6558 | checkList(pCheck, 0, pgnoOvfl, nPage, zContext); |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6559 | } |
| 6560 | |
| 6561 | /* Check sanity of left child page. |
| 6562 | */ |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 6563 | if( !pPage->leaf ){ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 6564 | pgno = get4byte(pCell); |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 6565 | #ifndef SQLITE_OMIT_AUTOVACUUM |
| 6566 | if( pBt->autoVacuum ){ |
| 6567 | checkPtrmap(pCheck, pgno, PTRMAP_BTREE, iPage, zContext); |
| 6568 | } |
| 6569 | #endif |
drh | 7416170 | 2006-02-24 02:53:49 +0000 | [diff] [blame] | 6570 | d2 = checkTreePage(pCheck,pgno,pPage,zContext); |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 6571 | if( i>0 && d2!=depth ){ |
| 6572 | checkAppendMsg(pCheck, zContext, "Child page depth differs"); |
| 6573 | } |
| 6574 | depth = d2; |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6575 | } |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6576 | } |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 6577 | if( !pPage->leaf ){ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 6578 | pgno = get4byte(&pPage->aData[pPage->hdrOffset+8]); |
drh | 5bb3eb9 | 2007-05-04 13:15:55 +0000 | [diff] [blame] | 6579 | sqlite3_snprintf(sizeof(zContext), zContext, |
| 6580 | "On page %d at right child: ", iPage); |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 6581 | #ifndef SQLITE_OMIT_AUTOVACUUM |
| 6582 | if( pBt->autoVacuum ){ |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 6583 | checkPtrmap(pCheck, pgno, PTRMAP_BTREE, iPage, 0); |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 6584 | } |
| 6585 | #endif |
drh | 7416170 | 2006-02-24 02:53:49 +0000 | [diff] [blame] | 6586 | checkTreePage(pCheck, pgno, pPage, zContext); |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 6587 | } |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6588 | |
| 6589 | /* Check for complete coverage of the page |
| 6590 | */ |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 6591 | data = pPage->aData; |
| 6592 | hdr = pPage->hdrOffset; |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 6593 | hit = sqlite3MallocZero( usableSize ); |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 6594 | if( hit ){ |
| 6595 | memset(hit, 1, get2byte(&data[hdr+5])); |
| 6596 | nCell = get2byte(&data[hdr+3]); |
| 6597 | cellStart = hdr + 12 - 4*pPage->leaf; |
| 6598 | for(i=0; i<nCell; i++){ |
| 6599 | int pc = get2byte(&data[cellStart+i*2]); |
drh | a9121e4 | 2008-02-19 14:59:35 +0000 | [diff] [blame] | 6600 | u16 size = cellSizePtr(pPage, &data[pc]); |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 6601 | int j; |
danielk1977 | 7701e81 | 2005-01-10 12:59:51 +0000 | [diff] [blame] | 6602 | if( (pc+size-1)>=usableSize || pc<0 ){ |
| 6603 | checkAppendMsg(pCheck, 0, |
| 6604 | "Corruption detected in cell %d on page %d",i,iPage,0); |
| 6605 | }else{ |
| 6606 | for(j=pc+size-1; j>=pc; j--) hit[j]++; |
| 6607 | } |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 6608 | } |
| 6609 | for(cnt=0, i=get2byte(&data[hdr+1]); i>0 && i<usableSize && cnt<10000; |
| 6610 | cnt++){ |
| 6611 | int size = get2byte(&data[i+2]); |
| 6612 | int j; |
danielk1977 | 7701e81 | 2005-01-10 12:59:51 +0000 | [diff] [blame] | 6613 | if( (i+size-1)>=usableSize || i<0 ){ |
| 6614 | checkAppendMsg(pCheck, 0, |
| 6615 | "Corruption detected in cell %d on page %d",i,iPage,0); |
| 6616 | }else{ |
| 6617 | for(j=i+size-1; j>=i; j--) hit[j]++; |
| 6618 | } |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 6619 | i = get2byte(&data[i]); |
| 6620 | } |
| 6621 | for(i=cnt=0; i<usableSize; i++){ |
| 6622 | if( hit[i]==0 ){ |
| 6623 | cnt++; |
| 6624 | }else if( hit[i]>1 ){ |
| 6625 | checkAppendMsg(pCheck, 0, |
| 6626 | "Multiple uses for byte %d of page %d", i, iPage); |
| 6627 | break; |
| 6628 | } |
| 6629 | } |
| 6630 | if( cnt!=data[hdr+7] ){ |
| 6631 | checkAppendMsg(pCheck, 0, |
| 6632 | "Fragmented space is %d byte reported as %d on page %d", |
| 6633 | cnt, data[hdr+7], iPage); |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6634 | } |
| 6635 | } |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 6636 | sqlite3_free(hit); |
drh | 6019e16 | 2001-07-02 17:51:45 +0000 | [diff] [blame] | 6637 | |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 6638 | releasePage(pPage); |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 6639 | return depth+1; |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6640 | } |
drh | b7f9164 | 2004-10-31 02:22:47 +0000 | [diff] [blame] | 6641 | #endif /* SQLITE_OMIT_INTEGRITY_CHECK */ |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6642 | |
drh | b7f9164 | 2004-10-31 02:22:47 +0000 | [diff] [blame] | 6643 | #ifndef SQLITE_OMIT_INTEGRITY_CHECK |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6644 | /* |
| 6645 | ** This routine does a complete check of the given BTree file. aRoot[] is |
| 6646 | ** an array of pages numbers were each page number is the root page of |
| 6647 | ** a table. nRoot is the number of entries in aRoot. |
| 6648 | ** |
| 6649 | ** If everything checks out, this routine returns NULL. If something is |
| 6650 | ** amiss, an error message is written into memory obtained from malloc() |
| 6651 | ** and a pointer to that error message is returned. The calling function |
| 6652 | ** is responsible for freeing the error message when it is done. |
| 6653 | */ |
drh | 1dcdbc0 | 2007-01-27 02:24:54 +0000 | [diff] [blame] | 6654 | char *sqlite3BtreeIntegrityCheck( |
| 6655 | Btree *p, /* The btree to be checked */ |
| 6656 | int *aRoot, /* An array of root pages numbers for individual trees */ |
| 6657 | int nRoot, /* Number of entries in aRoot[] */ |
| 6658 | int mxErr, /* Stop reporting errors after this many */ |
| 6659 | int *pnErr /* Write number of errors seen to this variable */ |
| 6660 | ){ |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6661 | int i; |
| 6662 | int nRef; |
drh | aaab572 | 2002-02-19 13:39:21 +0000 | [diff] [blame] | 6663 | IntegrityCk sCheck; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 6664 | BtShared *pBt = p->pBt; |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6665 | |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 6666 | sqlite3BtreeEnter(p); |
drh | e5fe690 | 2007-12-07 18:55:28 +0000 | [diff] [blame] | 6667 | pBt->db = p->db; |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 6668 | nRef = sqlite3PagerRefcount(pBt->pPager); |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 6669 | if( lockBtreeWithRetry(p)!=SQLITE_OK ){ |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 6670 | sqlite3BtreeLeave(p); |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 6671 | return sqlite3StrDup("Unable to acquire a read lock on the database"); |
drh | efc251d | 2001-07-01 22:12:01 +0000 | [diff] [blame] | 6672 | } |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6673 | sCheck.pBt = pBt; |
| 6674 | sCheck.pPager = pBt->pPager; |
danielk1977 | ad0132d | 2008-06-07 08:58:22 +0000 | [diff] [blame^] | 6675 | sCheck.nPage = pagerPagecount(sCheck.pPager); |
drh | 1dcdbc0 | 2007-01-27 02:24:54 +0000 | [diff] [blame] | 6676 | sCheck.mxErr = mxErr; |
| 6677 | sCheck.nErr = 0; |
| 6678 | *pnErr = 0; |
danielk1977 | e5321f0 | 2007-04-27 07:05:44 +0000 | [diff] [blame] | 6679 | #ifndef SQLITE_OMIT_AUTOVACUUM |
| 6680 | if( pBt->nTrunc!=0 ){ |
| 6681 | sCheck.nPage = pBt->nTrunc; |
| 6682 | } |
| 6683 | #endif |
drh | 0de8c11 | 2002-07-06 16:32:14 +0000 | [diff] [blame] | 6684 | if( sCheck.nPage==0 ){ |
| 6685 | unlockBtreeIfUnused(pBt); |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 6686 | sqlite3BtreeLeave(p); |
drh | 0de8c11 | 2002-07-06 16:32:14 +0000 | [diff] [blame] | 6687 | return 0; |
| 6688 | } |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 6689 | sCheck.anRef = sqlite3_malloc( (sCheck.nPage+1)*sizeof(sCheck.anRef[0]) ); |
danielk1977 | ac245ec | 2005-01-14 13:50:11 +0000 | [diff] [blame] | 6690 | if( !sCheck.anRef ){ |
| 6691 | unlockBtreeIfUnused(pBt); |
drh | 1dcdbc0 | 2007-01-27 02:24:54 +0000 | [diff] [blame] | 6692 | *pnErr = 1; |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 6693 | sqlite3BtreeLeave(p); |
drh | e5fe690 | 2007-12-07 18:55:28 +0000 | [diff] [blame] | 6694 | return sqlite3MPrintf(p->db, "Unable to malloc %d bytes", |
danielk1977 | ac245ec | 2005-01-14 13:50:11 +0000 | [diff] [blame] | 6695 | (sCheck.nPage+1)*sizeof(sCheck.anRef[0])); |
| 6696 | } |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 6697 | for(i=0; i<=sCheck.nPage; i++){ sCheck.anRef[i] = 0; } |
drh | 42cac6d | 2004-11-20 20:31:11 +0000 | [diff] [blame] | 6698 | i = PENDING_BYTE_PAGE(pBt); |
drh | 1f59571 | 2004-06-15 01:40:29 +0000 | [diff] [blame] | 6699 | if( i<=sCheck.nPage ){ |
| 6700 | sCheck.anRef[i] = 1; |
| 6701 | } |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6702 | sCheck.zErrMsg = 0; |
| 6703 | |
| 6704 | /* Check the integrity of the freelist |
| 6705 | */ |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 6706 | checkList(&sCheck, 1, get4byte(&pBt->pPage1->aData[32]), |
| 6707 | get4byte(&pBt->pPage1->aData[36]), "Main freelist: "); |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6708 | |
| 6709 | /* Check all the tables. |
| 6710 | */ |
drh | 1dcdbc0 | 2007-01-27 02:24:54 +0000 | [diff] [blame] | 6711 | for(i=0; i<nRoot && sCheck.mxErr; i++){ |
drh | 4ff6dfa | 2002-03-03 23:06:00 +0000 | [diff] [blame] | 6712 | if( aRoot[i]==0 ) continue; |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 6713 | #ifndef SQLITE_OMIT_AUTOVACUUM |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 6714 | if( pBt->autoVacuum && aRoot[i]>1 ){ |
| 6715 | checkPtrmap(&sCheck, aRoot[i], PTRMAP_ROOTPAGE, 0, 0); |
| 6716 | } |
| 6717 | #endif |
drh | 7416170 | 2006-02-24 02:53:49 +0000 | [diff] [blame] | 6718 | checkTreePage(&sCheck, aRoot[i], 0, "List of tree roots: "); |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6719 | } |
| 6720 | |
| 6721 | /* Make sure every page in the file is referenced |
| 6722 | */ |
drh | 1dcdbc0 | 2007-01-27 02:24:54 +0000 | [diff] [blame] | 6723 | for(i=1; i<=sCheck.nPage && sCheck.mxErr; i++){ |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 6724 | #ifdef SQLITE_OMIT_AUTOVACUUM |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6725 | if( sCheck.anRef[i]==0 ){ |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 6726 | checkAppendMsg(&sCheck, 0, "Page %d is never used", i); |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6727 | } |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 6728 | #else |
| 6729 | /* If the database supports auto-vacuum, make sure no tables contain |
| 6730 | ** references to pointer-map pages. |
| 6731 | */ |
| 6732 | if( sCheck.anRef[i]==0 && |
danielk1977 | 266664d | 2006-02-10 08:24:21 +0000 | [diff] [blame] | 6733 | (PTRMAP_PAGENO(pBt, i)!=i || !pBt->autoVacuum) ){ |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 6734 | checkAppendMsg(&sCheck, 0, "Page %d is never used", i); |
| 6735 | } |
| 6736 | if( sCheck.anRef[i]!=0 && |
danielk1977 | 266664d | 2006-02-10 08:24:21 +0000 | [diff] [blame] | 6737 | (PTRMAP_PAGENO(pBt, i)==i && pBt->autoVacuum) ){ |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 6738 | checkAppendMsg(&sCheck, 0, "Pointer map page %d is referenced", i); |
| 6739 | } |
| 6740 | #endif |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6741 | } |
| 6742 | |
| 6743 | /* Make sure this analysis did not leave any unref() pages |
| 6744 | */ |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 6745 | unlockBtreeIfUnused(pBt); |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 6746 | if( nRef != sqlite3PagerRefcount(pBt->pPager) ){ |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 6747 | checkAppendMsg(&sCheck, 0, |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6748 | "Outstanding page count goes from %d to %d during this analysis", |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 6749 | nRef, sqlite3PagerRefcount(pBt->pPager) |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6750 | ); |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6751 | } |
| 6752 | |
| 6753 | /* Clean up and report errors. |
| 6754 | */ |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 6755 | sqlite3BtreeLeave(p); |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 6756 | sqlite3_free(sCheck.anRef); |
drh | 1dcdbc0 | 2007-01-27 02:24:54 +0000 | [diff] [blame] | 6757 | *pnErr = sCheck.nErr; |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6758 | return sCheck.zErrMsg; |
| 6759 | } |
drh | b7f9164 | 2004-10-31 02:22:47 +0000 | [diff] [blame] | 6760 | #endif /* SQLITE_OMIT_INTEGRITY_CHECK */ |
paul | b95a886 | 2003-04-01 21:16:41 +0000 | [diff] [blame] | 6761 | |
drh | 73509ee | 2003-04-06 20:44:45 +0000 | [diff] [blame] | 6762 | /* |
| 6763 | ** Return the full pathname of the underlying database file. |
drh | d0679ed | 2007-08-28 22:24:34 +0000 | [diff] [blame] | 6764 | ** |
| 6765 | ** The pager filename is invariant as long as the pager is |
| 6766 | ** open so it is safe to access without the BtShared mutex. |
drh | 73509ee | 2003-04-06 20:44:45 +0000 | [diff] [blame] | 6767 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 6768 | const char *sqlite3BtreeGetFilename(Btree *p){ |
| 6769 | assert( p->pBt->pPager!=0 ); |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 6770 | return sqlite3PagerFilename(p->pBt->pPager); |
drh | 73509ee | 2003-04-06 20:44:45 +0000 | [diff] [blame] | 6771 | } |
| 6772 | |
| 6773 | /* |
danielk1977 | 5865e3d | 2004-06-14 06:03:57 +0000 | [diff] [blame] | 6774 | ** Return the pathname of the directory that contains the database file. |
drh | d0679ed | 2007-08-28 22:24:34 +0000 | [diff] [blame] | 6775 | ** |
| 6776 | ** The pager directory name is invariant as long as the pager is |
| 6777 | ** open so it is safe to access without the BtShared mutex. |
danielk1977 | 5865e3d | 2004-06-14 06:03:57 +0000 | [diff] [blame] | 6778 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 6779 | const char *sqlite3BtreeGetDirname(Btree *p){ |
| 6780 | assert( p->pBt->pPager!=0 ); |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 6781 | return sqlite3PagerDirname(p->pBt->pPager); |
danielk1977 | 5865e3d | 2004-06-14 06:03:57 +0000 | [diff] [blame] | 6782 | } |
| 6783 | |
| 6784 | /* |
| 6785 | ** Return the pathname of the journal file for this database. The return |
| 6786 | ** value of this routine is the same regardless of whether the journal file |
| 6787 | ** has been created or not. |
drh | d0679ed | 2007-08-28 22:24:34 +0000 | [diff] [blame] | 6788 | ** |
| 6789 | ** The pager journal filename is invariant as long as the pager is |
| 6790 | ** open so it is safe to access without the BtShared mutex. |
danielk1977 | 5865e3d | 2004-06-14 06:03:57 +0000 | [diff] [blame] | 6791 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 6792 | const char *sqlite3BtreeGetJournalname(Btree *p){ |
| 6793 | assert( p->pBt->pPager!=0 ); |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 6794 | return sqlite3PagerJournalname(p->pBt->pPager); |
danielk1977 | 5865e3d | 2004-06-14 06:03:57 +0000 | [diff] [blame] | 6795 | } |
| 6796 | |
drh | b7f9164 | 2004-10-31 02:22:47 +0000 | [diff] [blame] | 6797 | #ifndef SQLITE_OMIT_VACUUM |
danielk1977 | 5865e3d | 2004-06-14 06:03:57 +0000 | [diff] [blame] | 6798 | /* |
drh | f7c5753 | 2003-04-25 13:22:51 +0000 | [diff] [blame] | 6799 | ** Copy the complete content of pBtFrom into pBtTo. A transaction |
| 6800 | ** must be active for both files. |
| 6801 | ** |
danielk1977 | f653d78 | 2008-03-20 11:04:21 +0000 | [diff] [blame] | 6802 | ** The size of file pTo may be reduced by this operation. |
| 6803 | ** If anything goes wrong, the transaction on pTo is rolled back. |
| 6804 | ** |
| 6805 | ** If successful, CommitPhaseOne() may be called on pTo before returning. |
| 6806 | ** The caller should finish committing the transaction on pTo by calling |
| 6807 | ** sqlite3BtreeCommit(). |
drh | 73509ee | 2003-04-06 20:44:45 +0000 | [diff] [blame] | 6808 | */ |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 6809 | static int btreeCopyFile(Btree *pTo, Btree *pFrom){ |
drh | f7c5753 | 2003-04-25 13:22:51 +0000 | [diff] [blame] | 6810 | int rc = SQLITE_OK; |
danielk1977 | f653d78 | 2008-03-20 11:04:21 +0000 | [diff] [blame] | 6811 | Pgno i; |
| 6812 | |
| 6813 | Pgno nFromPage; /* Number of pages in pFrom */ |
| 6814 | Pgno nToPage; /* Number of pages in pTo */ |
| 6815 | Pgno nNewPage; /* Number of pages in pTo after the copy */ |
| 6816 | |
| 6817 | Pgno iSkip; /* Pending byte page in pTo */ |
| 6818 | int nToPageSize; /* Page size of pTo in bytes */ |
| 6819 | int nFromPageSize; /* Page size of pFrom in bytes */ |
drh | f7c5753 | 2003-04-25 13:22:51 +0000 | [diff] [blame] | 6820 | |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 6821 | BtShared *pBtTo = pTo->pBt; |
| 6822 | BtShared *pBtFrom = pFrom->pBt; |
drh | e5fe690 | 2007-12-07 18:55:28 +0000 | [diff] [blame] | 6823 | pBtTo->db = pTo->db; |
| 6824 | pBtFrom->db = pFrom->db; |
danielk1977 | f653d78 | 2008-03-20 11:04:21 +0000 | [diff] [blame] | 6825 | |
| 6826 | nToPageSize = pBtTo->pageSize; |
| 6827 | nFromPageSize = pBtFrom->pageSize; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 6828 | |
| 6829 | if( pTo->inTrans!=TRANS_WRITE || pFrom->inTrans!=TRANS_WRITE ){ |
danielk1977 | ee5741e | 2004-05-31 10:01:34 +0000 | [diff] [blame] | 6830 | return SQLITE_ERROR; |
| 6831 | } |
danielk1977 | f653d78 | 2008-03-20 11:04:21 +0000 | [diff] [blame] | 6832 | if( pBtTo->pCursor ){ |
| 6833 | return SQLITE_BUSY; |
drh | f7c5753 | 2003-04-25 13:22:51 +0000 | [diff] [blame] | 6834 | } |
drh | 538f570 | 2007-04-13 02:14:30 +0000 | [diff] [blame] | 6835 | |
danielk1977 | ad0132d | 2008-06-07 08:58:22 +0000 | [diff] [blame^] | 6836 | nToPage = pagerPagecount(pBtTo->pPager); |
| 6837 | nFromPage = pagerPagecount(pBtFrom->pPager); |
danielk1977 | f653d78 | 2008-03-20 11:04:21 +0000 | [diff] [blame] | 6838 | iSkip = PENDING_BYTE_PAGE(pBtTo); |
| 6839 | |
| 6840 | /* Variable nNewPage is the number of pages required to store the |
| 6841 | ** contents of pFrom using the current page-size of pTo. |
drh | 538f570 | 2007-04-13 02:14:30 +0000 | [diff] [blame] | 6842 | */ |
danielk1977 | f653d78 | 2008-03-20 11:04:21 +0000 | [diff] [blame] | 6843 | nNewPage = ((i64)nFromPage * (i64)nFromPageSize + (i64)nToPageSize - 1) / |
| 6844 | (i64)nToPageSize; |
| 6845 | |
| 6846 | for(i=1; rc==SQLITE_OK && (i<=nToPage || i<=nNewPage); i++){ |
| 6847 | |
| 6848 | /* Journal the original page. |
| 6849 | ** |
| 6850 | ** iSkip is the page number of the locking page (PENDING_BYTE_PAGE) |
| 6851 | ** in database *pTo (before the copy). This page is never written |
| 6852 | ** into the journal file. Unless i==iSkip or the page was not |
| 6853 | ** present in pTo before the copy operation, journal page i from pTo. |
| 6854 | */ |
| 6855 | if( i!=iSkip && i<=nToPage ){ |
danielk1977 | 4abd544 | 2008-05-05 15:26:50 +0000 | [diff] [blame] | 6856 | DbPage *pDbPage = 0; |
danielk1977 | f653d78 | 2008-03-20 11:04:21 +0000 | [diff] [blame] | 6857 | rc = sqlite3PagerGet(pBtTo->pPager, i, &pDbPage); |
danielk1977 | 4abd544 | 2008-05-05 15:26:50 +0000 | [diff] [blame] | 6858 | if( rc==SQLITE_OK ){ |
| 6859 | rc = sqlite3PagerWrite(pDbPage); |
danielk1977 | df2566a | 2008-05-07 19:11:03 +0000 | [diff] [blame] | 6860 | if( rc==SQLITE_OK && i>nFromPage ){ |
| 6861 | /* Yeah. It seems wierd to call DontWrite() right after Write(). But |
| 6862 | ** that is because the names of those procedures do not exactly |
| 6863 | ** represent what they do. Write() really means "put this page in the |
| 6864 | ** rollback journal and mark it as dirty so that it will be written |
| 6865 | ** to the database file later." DontWrite() undoes the second part of |
| 6866 | ** that and prevents the page from being written to the database. The |
| 6867 | ** page is still on the rollback journal, though. And that is the |
| 6868 | ** whole point of this block: to put pages on the rollback journal. |
| 6869 | */ |
| 6870 | sqlite3PagerDontWrite(pDbPage); |
| 6871 | } |
| 6872 | sqlite3PagerUnref(pDbPage); |
danielk1977 | f653d78 | 2008-03-20 11:04:21 +0000 | [diff] [blame] | 6873 | } |
danielk1977 | f653d78 | 2008-03-20 11:04:21 +0000 | [diff] [blame] | 6874 | } |
| 6875 | |
| 6876 | /* Overwrite the data in page i of the target database */ |
| 6877 | if( rc==SQLITE_OK && i!=iSkip && i<=nNewPage ){ |
| 6878 | |
| 6879 | DbPage *pToPage = 0; |
| 6880 | sqlite3_int64 iOff; |
| 6881 | |
| 6882 | rc = sqlite3PagerGet(pBtTo->pPager, i, &pToPage); |
| 6883 | if( rc==SQLITE_OK ){ |
| 6884 | rc = sqlite3PagerWrite(pToPage); |
| 6885 | } |
| 6886 | |
| 6887 | for( |
| 6888 | iOff=(i-1)*nToPageSize; |
| 6889 | rc==SQLITE_OK && iOff<i*nToPageSize; |
| 6890 | iOff += nFromPageSize |
| 6891 | ){ |
| 6892 | DbPage *pFromPage = 0; |
| 6893 | Pgno iFrom = (iOff/nFromPageSize)+1; |
| 6894 | |
| 6895 | if( iFrom==PENDING_BYTE_PAGE(pBtFrom) ){ |
| 6896 | continue; |
| 6897 | } |
| 6898 | |
| 6899 | rc = sqlite3PagerGet(pBtFrom->pPager, iFrom, &pFromPage); |
| 6900 | if( rc==SQLITE_OK ){ |
| 6901 | char *zTo = sqlite3PagerGetData(pToPage); |
| 6902 | char *zFrom = sqlite3PagerGetData(pFromPage); |
| 6903 | int nCopy; |
| 6904 | |
| 6905 | if( nFromPageSize>=nToPageSize ){ |
| 6906 | zFrom += ((i-1)*nToPageSize - ((iFrom-1)*nFromPageSize)); |
| 6907 | nCopy = nToPageSize; |
| 6908 | }else{ |
| 6909 | zTo += (((iFrom-1)*nFromPageSize) - (i-1)*nToPageSize); |
| 6910 | nCopy = nFromPageSize; |
| 6911 | } |
| 6912 | |
| 6913 | memcpy(zTo, zFrom, nCopy); |
| 6914 | sqlite3PagerUnref(pFromPage); |
| 6915 | } |
| 6916 | } |
| 6917 | |
| 6918 | if( pToPage ) sqlite3PagerUnref(pToPage); |
| 6919 | } |
drh | 2e6d11b | 2003-04-25 15:37:57 +0000 | [diff] [blame] | 6920 | } |
danielk1977 | f653d78 | 2008-03-20 11:04:21 +0000 | [diff] [blame] | 6921 | |
| 6922 | /* If things have worked so far, the database file may need to be |
| 6923 | ** truncated. The complex part is that it may need to be truncated to |
| 6924 | ** a size that is not an integer multiple of nToPageSize - the current |
| 6925 | ** page size used by the pager associated with B-Tree pTo. |
| 6926 | ** |
| 6927 | ** For example, say the page-size of pTo is 2048 bytes and the original |
| 6928 | ** number of pages is 5 (10 KB file). If pFrom has a page size of 1024 |
| 6929 | ** bytes and 9 pages, then the file needs to be truncated to 9KB. |
| 6930 | */ |
| 6931 | if( rc==SQLITE_OK ){ |
| 6932 | if( nFromPageSize!=nToPageSize ){ |
| 6933 | sqlite3_file *pFile = sqlite3PagerFile(pBtTo->pPager); |
| 6934 | i64 iSize = (i64)nFromPageSize * (i64)nFromPage; |
| 6935 | i64 iNow = (i64)((nToPage>nNewPage)?nToPage:nNewPage) * (i64)nToPageSize; |
| 6936 | i64 iPending = ((i64)PENDING_BYTE_PAGE(pBtTo)-1) *(i64)nToPageSize; |
| 6937 | |
| 6938 | assert( iSize<=iNow ); |
| 6939 | |
| 6940 | /* Commit phase one syncs the journal file associated with pTo |
| 6941 | ** containing the original data. It does not sync the database file |
| 6942 | ** itself. After doing this it is safe to use OsTruncate() and other |
| 6943 | ** file APIs on the database file directly. |
| 6944 | */ |
| 6945 | pBtTo->db = pTo->db; |
| 6946 | rc = sqlite3PagerCommitPhaseOne(pBtTo->pPager, 0, 0, 1); |
| 6947 | if( iSize<iNow && rc==SQLITE_OK ){ |
| 6948 | rc = sqlite3OsTruncate(pFile, iSize); |
| 6949 | } |
| 6950 | |
| 6951 | /* The loop that copied data from database pFrom to pTo did not |
| 6952 | ** populate the locking page of database pTo. If the page-size of |
| 6953 | ** pFrom is smaller than that of pTo, this means some data will |
| 6954 | ** not have been copied. |
| 6955 | ** |
| 6956 | ** This block copies the missing data from database pFrom to pTo |
| 6957 | ** using file APIs. This is safe because at this point we know that |
| 6958 | ** all of the original data from pTo has been synced into the |
| 6959 | ** journal file. At this point it would be safe to do anything at |
| 6960 | ** all to the database file except truncate it to zero bytes. |
| 6961 | */ |
| 6962 | if( rc==SQLITE_OK && nFromPageSize<nToPageSize && iSize>iPending){ |
| 6963 | i64 iOff; |
| 6964 | for( |
| 6965 | iOff=iPending; |
| 6966 | rc==SQLITE_OK && iOff<(iPending+nToPageSize); |
| 6967 | iOff += nFromPageSize |
| 6968 | ){ |
| 6969 | DbPage *pFromPage = 0; |
| 6970 | Pgno iFrom = (iOff/nFromPageSize)+1; |
| 6971 | |
| 6972 | if( iFrom==PENDING_BYTE_PAGE(pBtFrom) || iFrom>nFromPage ){ |
| 6973 | continue; |
| 6974 | } |
| 6975 | |
| 6976 | rc = sqlite3PagerGet(pBtFrom->pPager, iFrom, &pFromPage); |
| 6977 | if( rc==SQLITE_OK ){ |
| 6978 | char *zFrom = sqlite3PagerGetData(pFromPage); |
| 6979 | rc = sqlite3OsWrite(pFile, zFrom, nFromPageSize, iOff); |
| 6980 | sqlite3PagerUnref(pFromPage); |
| 6981 | } |
| 6982 | } |
| 6983 | } |
| 6984 | |
| 6985 | /* Sync the database file */ |
| 6986 | if( rc==SQLITE_OK ){ |
| 6987 | rc = sqlite3PagerSync(pBtTo->pPager); |
| 6988 | } |
| 6989 | }else{ |
| 6990 | rc = sqlite3PagerTruncate(pBtTo->pPager, nNewPage); |
| 6991 | } |
| 6992 | if( rc==SQLITE_OK ){ |
| 6993 | pBtTo->pageSizeFixed = 0; |
| 6994 | } |
drh | 2e6d11b | 2003-04-25 15:37:57 +0000 | [diff] [blame] | 6995 | } |
drh | 538f570 | 2007-04-13 02:14:30 +0000 | [diff] [blame] | 6996 | |
drh | f7c5753 | 2003-04-25 13:22:51 +0000 | [diff] [blame] | 6997 | if( rc ){ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 6998 | sqlite3BtreeRollback(pTo); |
drh | f7c5753 | 2003-04-25 13:22:51 +0000 | [diff] [blame] | 6999 | } |
danielk1977 | f653d78 | 2008-03-20 11:04:21 +0000 | [diff] [blame] | 7000 | |
drh | f7c5753 | 2003-04-25 13:22:51 +0000 | [diff] [blame] | 7001 | return rc; |
drh | 73509ee | 2003-04-06 20:44:45 +0000 | [diff] [blame] | 7002 | } |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 7003 | int sqlite3BtreeCopyFile(Btree *pTo, Btree *pFrom){ |
| 7004 | int rc; |
| 7005 | sqlite3BtreeEnter(pTo); |
| 7006 | sqlite3BtreeEnter(pFrom); |
| 7007 | rc = btreeCopyFile(pTo, pFrom); |
| 7008 | sqlite3BtreeLeave(pFrom); |
| 7009 | sqlite3BtreeLeave(pTo); |
| 7010 | return rc; |
| 7011 | } |
| 7012 | |
drh | b7f9164 | 2004-10-31 02:22:47 +0000 | [diff] [blame] | 7013 | #endif /* SQLITE_OMIT_VACUUM */ |
danielk1977 | 1d850a7 | 2004-05-31 08:26:49 +0000 | [diff] [blame] | 7014 | |
| 7015 | /* |
| 7016 | ** Return non-zero if a transaction is active. |
| 7017 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 7018 | int sqlite3BtreeIsInTrans(Btree *p){ |
drh | e5fe690 | 2007-12-07 18:55:28 +0000 | [diff] [blame] | 7019 | assert( p==0 || sqlite3_mutex_held(p->db->mutex) ); |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 7020 | return (p && (p->inTrans==TRANS_WRITE)); |
danielk1977 | 1d850a7 | 2004-05-31 08:26:49 +0000 | [diff] [blame] | 7021 | } |
| 7022 | |
| 7023 | /* |
| 7024 | ** Return non-zero if a statement transaction is active. |
| 7025 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 7026 | int sqlite3BtreeIsInStmt(Btree *p){ |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 7027 | assert( sqlite3BtreeHoldsMutex(p) ); |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 7028 | return (p->pBt && p->pBt->inStmt); |
danielk1977 | 1d850a7 | 2004-05-31 08:26:49 +0000 | [diff] [blame] | 7029 | } |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 7030 | |
| 7031 | /* |
danielk1977 | 2372c2b | 2006-06-27 16:34:56 +0000 | [diff] [blame] | 7032 | ** Return non-zero if a read (or write) transaction is active. |
| 7033 | */ |
| 7034 | int sqlite3BtreeIsInReadTrans(Btree *p){ |
drh | e5fe690 | 2007-12-07 18:55:28 +0000 | [diff] [blame] | 7035 | assert( sqlite3_mutex_held(p->db->mutex) ); |
danielk1977 | 2372c2b | 2006-06-27 16:34:56 +0000 | [diff] [blame] | 7036 | return (p && (p->inTrans!=TRANS_NONE)); |
| 7037 | } |
| 7038 | |
| 7039 | /* |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 7040 | ** This function returns a pointer to a blob of memory associated with |
drh | 85b623f | 2007-12-13 21:54:09 +0000 | [diff] [blame] | 7041 | ** a single shared-btree. The memory is used by client code for its own |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 7042 | ** purposes (for example, to store a high-level schema associated with |
| 7043 | ** the shared-btree). The btree layer manages reference counting issues. |
| 7044 | ** |
| 7045 | ** The first time this is called on a shared-btree, nBytes bytes of memory |
| 7046 | ** are allocated, zeroed, and returned to the caller. For each subsequent |
| 7047 | ** call the nBytes parameter is ignored and a pointer to the same blob |
| 7048 | ** of memory returned. |
| 7049 | ** |
| 7050 | ** Just before the shared-btree is closed, the function passed as the |
| 7051 | ** xFree argument when the memory allocation was made is invoked on the |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 7052 | ** blob of allocated memory. This function should not call sqlite3_free() |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 7053 | ** on the memory, the btree layer does that. |
| 7054 | */ |
| 7055 | void *sqlite3BtreeSchema(Btree *p, int nBytes, void(*xFree)(void *)){ |
| 7056 | BtShared *pBt = p->pBt; |
drh | 2764170 | 2007-08-22 02:56:42 +0000 | [diff] [blame] | 7057 | sqlite3BtreeEnter(p); |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 7058 | if( !pBt->pSchema ){ |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 7059 | pBt->pSchema = sqlite3MallocZero(nBytes); |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 7060 | pBt->xFreeSchema = xFree; |
| 7061 | } |
drh | 2764170 | 2007-08-22 02:56:42 +0000 | [diff] [blame] | 7062 | sqlite3BtreeLeave(p); |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 7063 | return pBt->pSchema; |
| 7064 | } |
| 7065 | |
danielk1977 | c87d34d | 2006-01-06 13:00:28 +0000 | [diff] [blame] | 7066 | /* |
| 7067 | ** Return true if another user of the same shared btree as the argument |
| 7068 | ** handle holds an exclusive lock on the sqlite_master table. |
| 7069 | */ |
| 7070 | int sqlite3BtreeSchemaLocked(Btree *p){ |
drh | 2764170 | 2007-08-22 02:56:42 +0000 | [diff] [blame] | 7071 | int rc; |
drh | e5fe690 | 2007-12-07 18:55:28 +0000 | [diff] [blame] | 7072 | assert( sqlite3_mutex_held(p->db->mutex) ); |
drh | 2764170 | 2007-08-22 02:56:42 +0000 | [diff] [blame] | 7073 | sqlite3BtreeEnter(p); |
| 7074 | rc = (queryTableLock(p, MASTER_ROOT, READ_LOCK)!=SQLITE_OK); |
| 7075 | sqlite3BtreeLeave(p); |
| 7076 | return rc; |
danielk1977 | c87d34d | 2006-01-06 13:00:28 +0000 | [diff] [blame] | 7077 | } |
| 7078 | |
drh | a154dcd | 2006-03-22 22:10:07 +0000 | [diff] [blame] | 7079 | |
| 7080 | #ifndef SQLITE_OMIT_SHARED_CACHE |
| 7081 | /* |
| 7082 | ** Obtain a lock on the table whose root page is iTab. The |
| 7083 | ** lock is a write lock if isWritelock is true or a read lock |
| 7084 | ** if it is false. |
| 7085 | */ |
danielk1977 | c00da10 | 2006-01-07 13:21:04 +0000 | [diff] [blame] | 7086 | int sqlite3BtreeLockTable(Btree *p, int iTab, u8 isWriteLock){ |
danielk1977 | 2e94d4d | 2006-01-09 05:36:27 +0000 | [diff] [blame] | 7087 | int rc = SQLITE_OK; |
drh | 6a9ad3d | 2008-04-02 16:29:30 +0000 | [diff] [blame] | 7088 | if( p->sharable ){ |
| 7089 | u8 lockType = READ_LOCK + isWriteLock; |
| 7090 | assert( READ_LOCK+1==WRITE_LOCK ); |
| 7091 | assert( isWriteLock==0 || isWriteLock==1 ); |
| 7092 | sqlite3BtreeEnter(p); |
| 7093 | rc = queryTableLock(p, iTab, lockType); |
| 7094 | if( rc==SQLITE_OK ){ |
| 7095 | rc = lockTable(p, iTab, lockType); |
| 7096 | } |
| 7097 | sqlite3BtreeLeave(p); |
danielk1977 | c00da10 | 2006-01-07 13:21:04 +0000 | [diff] [blame] | 7098 | } |
| 7099 | return rc; |
| 7100 | } |
drh | a154dcd | 2006-03-22 22:10:07 +0000 | [diff] [blame] | 7101 | #endif |
danielk1977 | b82e7ed | 2006-01-11 14:09:31 +0000 | [diff] [blame] | 7102 | |
danielk1977 | b4e9af9 | 2007-05-01 17:49:49 +0000 | [diff] [blame] | 7103 | #ifndef SQLITE_OMIT_INCRBLOB |
| 7104 | /* |
| 7105 | ** Argument pCsr must be a cursor opened for writing on an |
| 7106 | ** INTKEY table currently pointing at a valid table entry. |
| 7107 | ** This function modifies the data stored as part of that entry. |
| 7108 | ** Only the data content may only be modified, it is not possible |
| 7109 | ** to change the length of the data stored. |
| 7110 | */ |
danielk1977 | dcbb5d3 | 2007-05-04 18:36:44 +0000 | [diff] [blame] | 7111 | int sqlite3BtreePutData(BtCursor *pCsr, u32 offset, u32 amt, void *z){ |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 7112 | assert( cursorHoldsMutex(pCsr) ); |
drh | e5fe690 | 2007-12-07 18:55:28 +0000 | [diff] [blame] | 7113 | assert( sqlite3_mutex_held(pCsr->pBtree->db->mutex) ); |
danielk1977 | dcbb5d3 | 2007-05-04 18:36:44 +0000 | [diff] [blame] | 7114 | assert(pCsr->isIncrblobHandle); |
drh | fb98264 | 2007-08-30 01:19:59 +0000 | [diff] [blame] | 7115 | if( pCsr->eState>=CURSOR_REQUIRESEEK ){ |
| 7116 | if( pCsr->eState==CURSOR_FAULT ){ |
| 7117 | return pCsr->skip; |
| 7118 | }else{ |
| 7119 | return SQLITE_ABORT; |
| 7120 | } |
danielk1977 | dcbb5d3 | 2007-05-04 18:36:44 +0000 | [diff] [blame] | 7121 | } |
| 7122 | |
danielk1977 | d0441796 | 2007-05-02 13:16:30 +0000 | [diff] [blame] | 7123 | /* Check some preconditions: |
danielk1977 | dcbb5d3 | 2007-05-04 18:36:44 +0000 | [diff] [blame] | 7124 | ** (a) the cursor is open for writing, |
| 7125 | ** (b) there is no read-lock on the table being modified and |
| 7126 | ** (c) the cursor points at a valid row of an intKey table. |
danielk1977 | d0441796 | 2007-05-02 13:16:30 +0000 | [diff] [blame] | 7127 | */ |
danielk1977 | d0441796 | 2007-05-02 13:16:30 +0000 | [diff] [blame] | 7128 | if( !pCsr->wrFlag ){ |
danielk1977 | dcbb5d3 | 2007-05-04 18:36:44 +0000 | [diff] [blame] | 7129 | return SQLITE_READONLY; |
danielk1977 | d0441796 | 2007-05-02 13:16:30 +0000 | [diff] [blame] | 7130 | } |
drh | d0679ed | 2007-08-28 22:24:34 +0000 | [diff] [blame] | 7131 | assert( !pCsr->pBt->readOnly |
| 7132 | && pCsr->pBt->inTransaction==TRANS_WRITE ); |
danielk1977 | d0441796 | 2007-05-02 13:16:30 +0000 | [diff] [blame] | 7133 | if( checkReadLocks(pCsr->pBtree, pCsr->pgnoRoot, pCsr) ){ |
| 7134 | return SQLITE_LOCKED; /* The table pCur points to has a read lock */ |
| 7135 | } |
| 7136 | if( pCsr->eState==CURSOR_INVALID || !pCsr->pPage->intKey ){ |
| 7137 | return SQLITE_ERROR; |
danielk1977 | b4e9af9 | 2007-05-01 17:49:49 +0000 | [diff] [blame] | 7138 | } |
| 7139 | |
danielk1977 | 9f8d640 | 2007-05-02 17:48:45 +0000 | [diff] [blame] | 7140 | return accessPayload(pCsr, offset, amt, (unsigned char *)z, 0, 1); |
danielk1977 | b4e9af9 | 2007-05-01 17:49:49 +0000 | [diff] [blame] | 7141 | } |
danielk1977 | 2dec970 | 2007-05-02 16:48:37 +0000 | [diff] [blame] | 7142 | |
| 7143 | /* |
| 7144 | ** 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] | 7145 | ** overflow list for the current row. This is used by cursors opened |
| 7146 | ** for incremental blob IO only. |
| 7147 | ** |
| 7148 | ** This function sets a flag only. The actual page location cache |
| 7149 | ** (stored in BtCursor.aOverflow[]) is allocated and used by function |
| 7150 | ** accessPayload() (the worker function for sqlite3BtreeData() and |
| 7151 | ** sqlite3BtreePutData()). |
danielk1977 | 2dec970 | 2007-05-02 16:48:37 +0000 | [diff] [blame] | 7152 | */ |
| 7153 | void sqlite3BtreeCacheOverflow(BtCursor *pCur){ |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 7154 | assert( cursorHoldsMutex(pCur) ); |
drh | e5fe690 | 2007-12-07 18:55:28 +0000 | [diff] [blame] | 7155 | assert( sqlite3_mutex_held(pCur->pBtree->db->mutex) ); |
danielk1977 | dcbb5d3 | 2007-05-04 18:36:44 +0000 | [diff] [blame] | 7156 | assert(!pCur->isIncrblobHandle); |
danielk1977 | 2dec970 | 2007-05-02 16:48:37 +0000 | [diff] [blame] | 7157 | assert(!pCur->aOverflow); |
danielk1977 | dcbb5d3 | 2007-05-04 18:36:44 +0000 | [diff] [blame] | 7158 | pCur->isIncrblobHandle = 1; |
danielk1977 | 2dec970 | 2007-05-02 16:48:37 +0000 | [diff] [blame] | 7159 | } |
danielk1977 | b4e9af9 | 2007-05-01 17:49:49 +0000 | [diff] [blame] | 7160 | #endif |