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 | f653d78 | 2008-03-20 11:04:21 +0000 | [diff] [blame^] | 12 | ** $Id: btree.c,v 1.441 2008/03/20 11:04:21 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 | bf700f3 | 2007-03-31 02:36:44 +0000 | [diff] [blame] | 382 | rc = sqlite3BtreeMoveto(pCur, pCur->pKey, 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 ){ |
drh | 271efa5 | 2004-05-30 19:19:05 +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; |
| 564 | n += getVarint32(&pCell[n], &x); |
| 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); |
drh | de64713 | 2004-05-07 17:57:49 +0000 | [diff] [blame] | 1057 | } |
| 1058 | return rc; |
| 1059 | } |
| 1060 | |
| 1061 | /* |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1062 | ** Release a MemPage. This should be called once for each prior |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 1063 | ** call to sqlite3BtreeGetPage. |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1064 | */ |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 1065 | static void releasePage(MemPage *pPage){ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1066 | if( pPage ){ |
| 1067 | assert( pPage->aData ); |
| 1068 | assert( pPage->pBt ); |
drh | bf4bca5 | 2007-09-06 22:19:14 +0000 | [diff] [blame] | 1069 | assert( sqlite3PagerGetExtra(pPage->pDbPage) == (void*)pPage ); |
| 1070 | assert( sqlite3PagerGetData(pPage->pDbPage)==pPage->aData ); |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 1071 | assert( sqlite3_mutex_held(pPage->pBt->mutex) ); |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 1072 | sqlite3PagerUnref(pPage->pDbPage); |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1073 | } |
| 1074 | } |
| 1075 | |
| 1076 | /* |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 1077 | ** This routine is called when the reference count for a page |
| 1078 | ** reaches zero. We need to unref the pParent pointer when that |
| 1079 | ** happens. |
| 1080 | */ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 1081 | static void pageDestructor(DbPage *pData, int pageSize){ |
drh | 07d183d | 2005-05-01 22:52:42 +0000 | [diff] [blame] | 1082 | MemPage *pPage; |
| 1083 | assert( (pageSize & 7)==0 ); |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 1084 | pPage = (MemPage *)sqlite3PagerGetExtra(pData); |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 1085 | assert( pPage->isInit==0 || sqlite3_mutex_held(pPage->pBt->mutex) ); |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 1086 | if( pPage->pParent ){ |
| 1087 | MemPage *pParent = pPage->pParent; |
drh | d0679ed | 2007-08-28 22:24:34 +0000 | [diff] [blame] | 1088 | assert( pParent->pBt==pPage->pBt ); |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 1089 | pPage->pParent = 0; |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 1090 | releasePage(pParent); |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 1091 | } |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1092 | pPage->isInit = 0; |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 1093 | } |
| 1094 | |
| 1095 | /* |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 1096 | ** During a rollback, when the pager reloads information into the cache |
| 1097 | ** so that the cache is restored to its original state at the start of |
| 1098 | ** the transaction, for each page restored this routine is called. |
| 1099 | ** |
| 1100 | ** This routine needs to reset the extra data section at the end of the |
| 1101 | ** page to agree with the restored data. |
| 1102 | */ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 1103 | static void pageReinit(DbPage *pData, int pageSize){ |
drh | 07d183d | 2005-05-01 22:52:42 +0000 | [diff] [blame] | 1104 | MemPage *pPage; |
| 1105 | assert( (pageSize & 7)==0 ); |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 1106 | pPage = (MemPage *)sqlite3PagerGetExtra(pData); |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 1107 | if( pPage->isInit ){ |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 1108 | assert( sqlite3_mutex_held(pPage->pBt->mutex) ); |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 1109 | pPage->isInit = 0; |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 1110 | sqlite3BtreeInitPage(pPage, pPage->pParent); |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 1111 | } |
| 1112 | } |
| 1113 | |
| 1114 | /* |
drh | e5fe690 | 2007-12-07 18:55:28 +0000 | [diff] [blame] | 1115 | ** Invoke the busy handler for a btree. |
| 1116 | */ |
| 1117 | static int sqlite3BtreeInvokeBusyHandler(void *pArg, int n){ |
| 1118 | BtShared *pBt = (BtShared*)pArg; |
| 1119 | assert( pBt->db ); |
| 1120 | assert( sqlite3_mutex_held(pBt->db->mutex) ); |
| 1121 | return sqlite3InvokeBusyHandler(&pBt->db->busyHandler); |
| 1122 | } |
| 1123 | |
| 1124 | /* |
drh | ad3e010 | 2004-09-03 23:32:18 +0000 | [diff] [blame] | 1125 | ** Open a database file. |
| 1126 | ** |
drh | 382c024 | 2001-10-06 16:33:02 +0000 | [diff] [blame] | 1127 | ** zFilename is the name of the database file. If zFilename is NULL |
drh | 1bee3d7 | 2001-10-15 00:44:35 +0000 | [diff] [blame] | 1128 | ** a new database with a random name is created. This randomly named |
drh | 23e11ca | 2004-05-04 17:27:28 +0000 | [diff] [blame] | 1129 | ** database file will be deleted when sqlite3BtreeClose() is called. |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 1130 | ** If zFilename is ":memory:" then an in-memory database is created |
| 1131 | ** that is automatically destroyed when it is closed. |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 1132 | */ |
drh | 23e11ca | 2004-05-04 17:27:28 +0000 | [diff] [blame] | 1133 | int sqlite3BtreeOpen( |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1134 | const char *zFilename, /* Name of the file containing the BTree database */ |
drh | e5fe690 | 2007-12-07 18:55:28 +0000 | [diff] [blame] | 1135 | sqlite3 *db, /* Associated database handle */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1136 | Btree **ppBtree, /* Pointer to new Btree object written here */ |
drh | 33f4e02 | 2007-09-03 15:19:34 +0000 | [diff] [blame] | 1137 | int flags, /* Options */ |
| 1138 | int vfsFlags /* Flags passed through to sqlite3_vfs.xOpen() */ |
drh | 6019e16 | 2001-07-02 17:51:45 +0000 | [diff] [blame] | 1139 | ){ |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 1140 | sqlite3_vfs *pVfs; /* The VFS to use for this btree */ |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 1141 | BtShared *pBt = 0; /* Shared part of btree structure */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1142 | Btree *p; /* Handle to return */ |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 1143 | int rc = SQLITE_OK; |
drh | 90f5ecb | 2004-07-22 01:19:35 +0000 | [diff] [blame] | 1144 | int nReserve; |
| 1145 | unsigned char zDbHeader[100]; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1146 | |
| 1147 | /* Set the variable isMemdb to true for an in-memory database, or |
| 1148 | ** false for a file-based database. This symbol is only required if |
| 1149 | ** either of the shared-data or autovacuum features are compiled |
| 1150 | ** into the library. |
| 1151 | */ |
| 1152 | #if !defined(SQLITE_OMIT_SHARED_CACHE) || !defined(SQLITE_OMIT_AUTOVACUUM) |
| 1153 | #ifdef SQLITE_OMIT_MEMORYDB |
drh | 980b1a7 | 2006-08-16 16:42:48 +0000 | [diff] [blame] | 1154 | const int isMemdb = 0; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1155 | #else |
drh | 980b1a7 | 2006-08-16 16:42:48 +0000 | [diff] [blame] | 1156 | const int isMemdb = zFilename && !strcmp(zFilename, ":memory:"); |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1157 | #endif |
| 1158 | #endif |
| 1159 | |
drh | e5fe690 | 2007-12-07 18:55:28 +0000 | [diff] [blame] | 1160 | assert( db!=0 ); |
| 1161 | assert( sqlite3_mutex_held(db->mutex) ); |
drh | 153c62c | 2007-08-24 03:51:33 +0000 | [diff] [blame] | 1162 | |
drh | e5fe690 | 2007-12-07 18:55:28 +0000 | [diff] [blame] | 1163 | pVfs = db->pVfs; |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 1164 | p = sqlite3MallocZero(sizeof(Btree)); |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1165 | if( !p ){ |
| 1166 | return SQLITE_NOMEM; |
| 1167 | } |
| 1168 | p->inTrans = TRANS_NONE; |
drh | e5fe690 | 2007-12-07 18:55:28 +0000 | [diff] [blame] | 1169 | p->db = db; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1170 | |
drh | 198bf39 | 2006-01-06 21:52:49 +0000 | [diff] [blame] | 1171 | #if !defined(SQLITE_OMIT_SHARED_CACHE) && !defined(SQLITE_OMIT_DISKIO) |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 1172 | /* |
| 1173 | ** If this Btree is a candidate for shared cache, try to find an |
| 1174 | ** existing BtShared object that we can share with |
| 1175 | */ |
| 1176 | if( (flags & BTREE_PRIVATE)==0 |
| 1177 | && isMemdb==0 |
drh | e5fe690 | 2007-12-07 18:55:28 +0000 | [diff] [blame] | 1178 | && (db->flags & SQLITE_Vtab)==0 |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 1179 | && zFilename && zFilename[0] |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 1180 | ){ |
drh | ff0587c | 2007-08-29 17:43:19 +0000 | [diff] [blame] | 1181 | if( sqlite3SharedCacheEnabled ){ |
danielk1977 | adfb9b0 | 2007-09-17 07:02:56 +0000 | [diff] [blame] | 1182 | int nFullPathname = pVfs->mxPathname+1; |
| 1183 | char *zFullPathname = (char *)sqlite3_malloc(nFullPathname); |
drh | ff0587c | 2007-08-29 17:43:19 +0000 | [diff] [blame] | 1184 | sqlite3_mutex *mutexShared; |
| 1185 | p->sharable = 1; |
drh | e5fe690 | 2007-12-07 18:55:28 +0000 | [diff] [blame] | 1186 | if( db ){ |
| 1187 | db->flags |= SQLITE_SharedCache; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1188 | } |
drh | ff0587c | 2007-08-29 17:43:19 +0000 | [diff] [blame] | 1189 | if( !zFullPathname ){ |
| 1190 | sqlite3_free(p); |
| 1191 | return SQLITE_NOMEM; |
| 1192 | } |
danielk1977 | adfb9b0 | 2007-09-17 07:02:56 +0000 | [diff] [blame] | 1193 | sqlite3OsFullPathname(pVfs, zFilename, nFullPathname, zFullPathname); |
drh | ff0587c | 2007-08-29 17:43:19 +0000 | [diff] [blame] | 1194 | mutexShared = sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_MASTER); |
| 1195 | sqlite3_mutex_enter(mutexShared); |
| 1196 | for(pBt=sqlite3SharedCacheList; pBt; pBt=pBt->pNext){ |
| 1197 | assert( pBt->nRef>0 ); |
| 1198 | if( 0==strcmp(zFullPathname, sqlite3PagerFilename(pBt->pPager)) |
| 1199 | && sqlite3PagerVfs(pBt->pPager)==pVfs ){ |
| 1200 | p->pBt = pBt; |
| 1201 | pBt->nRef++; |
| 1202 | break; |
| 1203 | } |
| 1204 | } |
| 1205 | sqlite3_mutex_leave(mutexShared); |
| 1206 | sqlite3_free(zFullPathname); |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1207 | } |
drh | ff0587c | 2007-08-29 17:43:19 +0000 | [diff] [blame] | 1208 | #ifdef SQLITE_DEBUG |
| 1209 | else{ |
| 1210 | /* In debug mode, we mark all persistent databases as sharable |
| 1211 | ** even when they are not. This exercises the locking code and |
| 1212 | ** gives more opportunity for asserts(sqlite3_mutex_held()) |
| 1213 | ** statements to find locking problems. |
| 1214 | */ |
| 1215 | p->sharable = 1; |
| 1216 | } |
| 1217 | #endif |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1218 | } |
| 1219 | #endif |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 1220 | if( pBt==0 ){ |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 1221 | /* |
| 1222 | ** The following asserts make sure that structures used by the btree are |
| 1223 | ** the right size. This is to guard against size changes that result |
| 1224 | ** when compiling on a different architecture. |
danielk1977 | 03aded4 | 2004-11-22 05:26:27 +0000 | [diff] [blame] | 1225 | */ |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 1226 | assert( sizeof(i64)==8 || sizeof(i64)==4 ); |
| 1227 | assert( sizeof(u64)==8 || sizeof(u64)==4 ); |
| 1228 | assert( sizeof(u32)==4 ); |
| 1229 | assert( sizeof(u16)==2 ); |
| 1230 | assert( sizeof(Pgno)==4 ); |
| 1231 | |
| 1232 | pBt = sqlite3MallocZero( sizeof(*pBt) ); |
| 1233 | if( pBt==0 ){ |
| 1234 | rc = SQLITE_NOMEM; |
| 1235 | goto btree_open_out; |
| 1236 | } |
drh | e5fe690 | 2007-12-07 18:55:28 +0000 | [diff] [blame] | 1237 | pBt->busyHdr.xFunc = sqlite3BtreeInvokeBusyHandler; |
| 1238 | pBt->busyHdr.pArg = pBt; |
drh | 33f4e02 | 2007-09-03 15:19:34 +0000 | [diff] [blame] | 1239 | rc = sqlite3PagerOpen(pVfs, &pBt->pPager, zFilename, |
| 1240 | EXTRA_SIZE, flags, vfsFlags); |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 1241 | if( rc==SQLITE_OK ){ |
| 1242 | rc = sqlite3PagerReadFileheader(pBt->pPager,sizeof(zDbHeader),zDbHeader); |
| 1243 | } |
| 1244 | if( rc!=SQLITE_OK ){ |
| 1245 | goto btree_open_out; |
| 1246 | } |
drh | e5fe690 | 2007-12-07 18:55:28 +0000 | [diff] [blame] | 1247 | sqlite3PagerSetBusyhandler(pBt->pPager, &pBt->busyHdr); |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 1248 | p->pBt = pBt; |
| 1249 | |
| 1250 | sqlite3PagerSetDestructor(pBt->pPager, pageDestructor); |
| 1251 | sqlite3PagerSetReiniter(pBt->pPager, pageReinit); |
| 1252 | pBt->pCursor = 0; |
| 1253 | pBt->pPage1 = 0; |
| 1254 | pBt->readOnly = sqlite3PagerIsreadonly(pBt->pPager); |
| 1255 | pBt->pageSize = get2byte(&zDbHeader[16]); |
| 1256 | if( pBt->pageSize<512 || pBt->pageSize>SQLITE_MAX_PAGE_SIZE |
| 1257 | || ((pBt->pageSize-1)&pBt->pageSize)!=0 ){ |
danielk1977 | a1644fd | 2007-08-29 12:31:25 +0000 | [diff] [blame] | 1258 | pBt->pageSize = 0; |
| 1259 | sqlite3PagerSetPagesize(pBt->pPager, &pBt->pageSize); |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 1260 | pBt->maxEmbedFrac = 64; /* 25% */ |
| 1261 | pBt->minEmbedFrac = 32; /* 12.5% */ |
| 1262 | pBt->minLeafFrac = 32; /* 12.5% */ |
| 1263 | #ifndef SQLITE_OMIT_AUTOVACUUM |
| 1264 | /* If the magic name ":memory:" will create an in-memory database, then |
| 1265 | ** leave the autoVacuum mode at 0 (do not auto-vacuum), even if |
| 1266 | ** SQLITE_DEFAULT_AUTOVACUUM is true. On the other hand, if |
| 1267 | ** SQLITE_OMIT_MEMORYDB has been defined, then ":memory:" is just a |
| 1268 | ** regular file-name. In this case the auto-vacuum applies as per normal. |
| 1269 | */ |
| 1270 | if( zFilename && !isMemdb ){ |
| 1271 | pBt->autoVacuum = (SQLITE_DEFAULT_AUTOVACUUM ? 1 : 0); |
| 1272 | pBt->incrVacuum = (SQLITE_DEFAULT_AUTOVACUUM==2 ? 1 : 0); |
| 1273 | } |
| 1274 | #endif |
| 1275 | nReserve = 0; |
| 1276 | }else{ |
| 1277 | nReserve = zDbHeader[20]; |
| 1278 | pBt->maxEmbedFrac = zDbHeader[21]; |
| 1279 | pBt->minEmbedFrac = zDbHeader[22]; |
| 1280 | pBt->minLeafFrac = zDbHeader[23]; |
| 1281 | pBt->pageSizeFixed = 1; |
| 1282 | #ifndef SQLITE_OMIT_AUTOVACUUM |
| 1283 | pBt->autoVacuum = (get4byte(&zDbHeader[36 + 4*4])?1:0); |
| 1284 | pBt->incrVacuum = (get4byte(&zDbHeader[36 + 7*4])?1:0); |
| 1285 | #endif |
| 1286 | } |
| 1287 | pBt->usableSize = pBt->pageSize - nReserve; |
| 1288 | assert( (pBt->pageSize & 7)==0 ); /* 8-byte alignment of pageSize */ |
danielk1977 | a1644fd | 2007-08-29 12:31:25 +0000 | [diff] [blame] | 1289 | sqlite3PagerSetPagesize(pBt->pPager, &pBt->pageSize); |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 1290 | |
| 1291 | #if !defined(SQLITE_OMIT_SHARED_CACHE) && !defined(SQLITE_OMIT_DISKIO) |
| 1292 | /* Add the new BtShared object to the linked list sharable BtShareds. |
| 1293 | */ |
| 1294 | if( p->sharable ){ |
| 1295 | sqlite3_mutex *mutexShared; |
| 1296 | pBt->nRef = 1; |
| 1297 | mutexShared = sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_MASTER); |
drh | 3285db2 | 2007-09-03 22:00:39 +0000 | [diff] [blame] | 1298 | if( SQLITE_THREADSAFE ){ |
| 1299 | pBt->mutex = sqlite3_mutex_alloc(SQLITE_MUTEX_FAST); |
| 1300 | if( pBt->mutex==0 ){ |
| 1301 | rc = SQLITE_NOMEM; |
drh | e5fe690 | 2007-12-07 18:55:28 +0000 | [diff] [blame] | 1302 | db->mallocFailed = 0; |
drh | 3285db2 | 2007-09-03 22:00:39 +0000 | [diff] [blame] | 1303 | goto btree_open_out; |
| 1304 | } |
drh | ff0587c | 2007-08-29 17:43:19 +0000 | [diff] [blame] | 1305 | } |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 1306 | sqlite3_mutex_enter(mutexShared); |
| 1307 | pBt->pNext = sqlite3SharedCacheList; |
| 1308 | sqlite3SharedCacheList = pBt; |
| 1309 | sqlite3_mutex_leave(mutexShared); |
danielk1977 | 951af80 | 2004-11-05 15:45:09 +0000 | [diff] [blame] | 1310 | } |
drh | eee46cf | 2004-11-06 00:02:48 +0000 | [diff] [blame] | 1311 | #endif |
drh | 90f5ecb | 2004-07-22 01:19:35 +0000 | [diff] [blame] | 1312 | } |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1313 | |
drh | cfed7bc | 2006-03-13 14:28:05 +0000 | [diff] [blame] | 1314 | #if !defined(SQLITE_OMIT_SHARED_CACHE) && !defined(SQLITE_OMIT_DISKIO) |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 1315 | /* If the new Btree uses a sharable pBtShared, then link the new |
| 1316 | ** Btree into the list of all sharable Btrees for the same connection. |
drh | abddb0c | 2007-08-20 13:14:28 +0000 | [diff] [blame] | 1317 | ** The list is kept in ascending order by pBt address. |
danielk1977 | 54f0198 | 2006-01-18 15:25:17 +0000 | [diff] [blame] | 1318 | */ |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 1319 | if( p->sharable ){ |
| 1320 | int i; |
| 1321 | Btree *pSib; |
drh | e5fe690 | 2007-12-07 18:55:28 +0000 | [diff] [blame] | 1322 | for(i=0; i<db->nDb; i++){ |
| 1323 | if( (pSib = db->aDb[i].pBt)!=0 && pSib->sharable ){ |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 1324 | while( pSib->pPrev ){ pSib = pSib->pPrev; } |
| 1325 | if( p->pBt<pSib->pBt ){ |
| 1326 | p->pNext = pSib; |
| 1327 | p->pPrev = 0; |
| 1328 | pSib->pPrev = p; |
| 1329 | }else{ |
drh | abddb0c | 2007-08-20 13:14:28 +0000 | [diff] [blame] | 1330 | while( pSib->pNext && pSib->pNext->pBt<p->pBt ){ |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 1331 | pSib = pSib->pNext; |
| 1332 | } |
| 1333 | p->pNext = pSib->pNext; |
| 1334 | p->pPrev = pSib; |
| 1335 | if( p->pNext ){ |
| 1336 | p->pNext->pPrev = p; |
| 1337 | } |
| 1338 | pSib->pNext = p; |
| 1339 | } |
| 1340 | break; |
| 1341 | } |
| 1342 | } |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1343 | } |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1344 | #endif |
| 1345 | *ppBtree = p; |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 1346 | |
| 1347 | btree_open_out: |
| 1348 | if( rc!=SQLITE_OK ){ |
| 1349 | if( pBt && pBt->pPager ){ |
| 1350 | sqlite3PagerClose(pBt->pPager); |
| 1351 | } |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 1352 | sqlite3_free(pBt); |
| 1353 | sqlite3_free(p); |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 1354 | *ppBtree = 0; |
| 1355 | } |
| 1356 | return rc; |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 1357 | } |
| 1358 | |
| 1359 | /* |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 1360 | ** Decrement the BtShared.nRef counter. When it reaches zero, |
| 1361 | ** remove the BtShared structure from the sharing list. Return |
| 1362 | ** true if the BtShared.nRef counter reaches zero and return |
| 1363 | ** false if it is still positive. |
| 1364 | */ |
| 1365 | static int removeFromSharingList(BtShared *pBt){ |
| 1366 | #ifndef SQLITE_OMIT_SHARED_CACHE |
| 1367 | sqlite3_mutex *pMaster; |
| 1368 | BtShared *pList; |
| 1369 | int removed = 0; |
| 1370 | |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 1371 | assert( sqlite3_mutex_notheld(pBt->mutex) ); |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 1372 | pMaster = sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_MASTER); |
| 1373 | sqlite3_mutex_enter(pMaster); |
| 1374 | pBt->nRef--; |
| 1375 | if( pBt->nRef<=0 ){ |
| 1376 | if( sqlite3SharedCacheList==pBt ){ |
| 1377 | sqlite3SharedCacheList = pBt->pNext; |
| 1378 | }else{ |
| 1379 | pList = sqlite3SharedCacheList; |
| 1380 | while( pList && pList->pNext!=pBt ){ |
| 1381 | pList=pList->pNext; |
| 1382 | } |
| 1383 | if( pList ){ |
| 1384 | pList->pNext = pBt->pNext; |
| 1385 | } |
| 1386 | } |
drh | 3285db2 | 2007-09-03 22:00:39 +0000 | [diff] [blame] | 1387 | if( SQLITE_THREADSAFE ){ |
| 1388 | sqlite3_mutex_free(pBt->mutex); |
| 1389 | } |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 1390 | removed = 1; |
| 1391 | } |
| 1392 | sqlite3_mutex_leave(pMaster); |
| 1393 | return removed; |
| 1394 | #else |
| 1395 | return 1; |
| 1396 | #endif |
| 1397 | } |
| 1398 | |
| 1399 | /* |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 1400 | ** Close an open database and invalidate all cursors. |
| 1401 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1402 | int sqlite3BtreeClose(Btree *p){ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1403 | BtShared *pBt = p->pBt; |
| 1404 | BtCursor *pCur; |
| 1405 | |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1406 | /* Close all cursors opened via this handle. */ |
drh | e5fe690 | 2007-12-07 18:55:28 +0000 | [diff] [blame] | 1407 | assert( sqlite3_mutex_held(p->db->mutex) ); |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 1408 | sqlite3BtreeEnter(p); |
drh | e5fe690 | 2007-12-07 18:55:28 +0000 | [diff] [blame] | 1409 | pBt->db = p->db; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1410 | pCur = pBt->pCursor; |
| 1411 | while( pCur ){ |
| 1412 | BtCursor *pTmp = pCur; |
| 1413 | pCur = pCur->pNext; |
| 1414 | if( pTmp->pBtree==p ){ |
| 1415 | sqlite3BtreeCloseCursor(pTmp); |
| 1416 | } |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 1417 | } |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1418 | |
danielk1977 | 8d34dfd | 2006-01-24 16:37:57 +0000 | [diff] [blame] | 1419 | /* Rollback any active transaction and free the handle structure. |
| 1420 | ** The call to sqlite3BtreeRollback() drops any table-locks held by |
| 1421 | ** this handle. |
| 1422 | */ |
danielk1977 | b597f74 | 2006-01-15 11:39:18 +0000 | [diff] [blame] | 1423 | sqlite3BtreeRollback(p); |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 1424 | sqlite3BtreeLeave(p); |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1425 | |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1426 | /* If there are still other outstanding references to the shared-btree |
| 1427 | ** structure, return now. The remainder of this procedure cleans |
| 1428 | ** up the shared-btree. |
| 1429 | */ |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 1430 | assert( p->wantToLock==0 && p->locked==0 ); |
| 1431 | if( !p->sharable || removeFromSharingList(pBt) ){ |
| 1432 | /* The pBt is no longer on the sharing list, so we can access |
| 1433 | ** it without having to hold the mutex. |
| 1434 | ** |
| 1435 | ** Clean out and delete the BtShared object. |
| 1436 | */ |
| 1437 | assert( !pBt->pCursor ); |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 1438 | sqlite3PagerClose(pBt->pPager); |
| 1439 | if( pBt->xFreeSchema && pBt->pSchema ){ |
| 1440 | pBt->xFreeSchema(pBt->pSchema); |
| 1441 | } |
| 1442 | sqlite3_free(pBt->pSchema); |
| 1443 | sqlite3_free(pBt); |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1444 | } |
| 1445 | |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 1446 | #ifndef SQLITE_OMIT_SHARED_CACHE |
drh | cab5ed7 | 2007-08-22 11:41:18 +0000 | [diff] [blame] | 1447 | assert( p->wantToLock==0 ); |
| 1448 | assert( p->locked==0 ); |
| 1449 | if( p->pPrev ) p->pPrev->pNext = p->pNext; |
| 1450 | if( p->pNext ) p->pNext->pPrev = p->pPrev; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1451 | #endif |
| 1452 | |
drh | e53831d | 2007-08-17 01:14:38 +0000 | [diff] [blame] | 1453 | sqlite3_free(p); |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 1454 | return SQLITE_OK; |
| 1455 | } |
| 1456 | |
| 1457 | /* |
drh | da47d77 | 2002-12-02 04:25:19 +0000 | [diff] [blame] | 1458 | ** Change the limit on the number of pages allowed in the cache. |
drh | cd61c28 | 2002-03-06 22:01:34 +0000 | [diff] [blame] | 1459 | ** |
| 1460 | ** The maximum number of cache pages is set to the absolute |
| 1461 | ** value of mxPage. If mxPage is negative, the pager will |
| 1462 | ** operate asynchronously - it will not stop to do fsync()s |
| 1463 | ** to insure data is written to the disk surface before |
| 1464 | ** continuing. Transactions still work if synchronous is off, |
| 1465 | ** and the database cannot be corrupted if this program |
| 1466 | ** crashes. But if the operating system crashes or there is |
| 1467 | ** an abrupt power failure when synchronous is off, the database |
| 1468 | ** could be left in an inconsistent and unrecoverable state. |
| 1469 | ** Synchronous is on by default so database corruption is not |
| 1470 | ** normally a worry. |
drh | f57b14a | 2001-09-14 18:54:08 +0000 | [diff] [blame] | 1471 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1472 | int sqlite3BtreeSetCacheSize(Btree *p, int mxPage){ |
| 1473 | BtShared *pBt = p->pBt; |
drh | e5fe690 | 2007-12-07 18:55:28 +0000 | [diff] [blame] | 1474 | assert( sqlite3_mutex_held(p->db->mutex) ); |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 1475 | sqlite3BtreeEnter(p); |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 1476 | sqlite3PagerSetCachesize(pBt->pPager, mxPage); |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 1477 | sqlite3BtreeLeave(p); |
drh | f57b14a | 2001-09-14 18:54:08 +0000 | [diff] [blame] | 1478 | return SQLITE_OK; |
| 1479 | } |
| 1480 | |
| 1481 | /* |
drh | 973b6e3 | 2003-02-12 14:09:42 +0000 | [diff] [blame] | 1482 | ** Change the way data is synced to disk in order to increase or decrease |
| 1483 | ** how well the database resists damage due to OS crashes and power |
| 1484 | ** failures. Level 1 is the same as asynchronous (no syncs() occur and |
| 1485 | ** there is a high probability of damage) Level 2 is the default. There |
| 1486 | ** is a very low but non-zero probability of damage. Level 3 reduces the |
| 1487 | ** probability of damage to near zero but with a write performance reduction. |
| 1488 | */ |
danielk1977 | 93758c8 | 2005-01-21 08:13:14 +0000 | [diff] [blame] | 1489 | #ifndef SQLITE_OMIT_PAGER_PRAGMAS |
drh | ac530b1 | 2006-02-11 01:25:50 +0000 | [diff] [blame] | 1490 | int sqlite3BtreeSetSafetyLevel(Btree *p, int level, int fullSync){ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1491 | BtShared *pBt = p->pBt; |
drh | e5fe690 | 2007-12-07 18:55:28 +0000 | [diff] [blame] | 1492 | assert( sqlite3_mutex_held(p->db->mutex) ); |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 1493 | sqlite3BtreeEnter(p); |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 1494 | sqlite3PagerSetSafetyLevel(pBt->pPager, level, fullSync); |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 1495 | sqlite3BtreeLeave(p); |
drh | 973b6e3 | 2003-02-12 14:09:42 +0000 | [diff] [blame] | 1496 | return SQLITE_OK; |
| 1497 | } |
danielk1977 | 93758c8 | 2005-01-21 08:13:14 +0000 | [diff] [blame] | 1498 | #endif |
drh | 973b6e3 | 2003-02-12 14:09:42 +0000 | [diff] [blame] | 1499 | |
drh | 2c8997b | 2005-08-27 16:36:48 +0000 | [diff] [blame] | 1500 | /* |
| 1501 | ** Return TRUE if the given btree is set to safety level 1. In other |
| 1502 | ** words, return TRUE if no sync() occurs on the disk files. |
| 1503 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1504 | int sqlite3BtreeSyncDisabled(Btree *p){ |
| 1505 | BtShared *pBt = p->pBt; |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 1506 | int rc; |
drh | e5fe690 | 2007-12-07 18:55:28 +0000 | [diff] [blame] | 1507 | assert( sqlite3_mutex_held(p->db->mutex) ); |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 1508 | sqlite3BtreeEnter(p); |
drh | d0679ed | 2007-08-28 22:24:34 +0000 | [diff] [blame] | 1509 | assert( pBt && pBt->pPager ); |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 1510 | rc = sqlite3PagerNosync(pBt->pPager); |
| 1511 | sqlite3BtreeLeave(p); |
| 1512 | return rc; |
drh | 2c8997b | 2005-08-27 16:36:48 +0000 | [diff] [blame] | 1513 | } |
| 1514 | |
danielk1977 | 576ec6b | 2005-01-21 11:55:25 +0000 | [diff] [blame] | 1515 | #if !defined(SQLITE_OMIT_PAGER_PRAGMAS) || !defined(SQLITE_OMIT_VACUUM) |
drh | 973b6e3 | 2003-02-12 14:09:42 +0000 | [diff] [blame] | 1516 | /* |
drh | 90f5ecb | 2004-07-22 01:19:35 +0000 | [diff] [blame] | 1517 | ** Change the default pages size and the number of reserved bytes per page. |
drh | 06f5021 | 2004-11-02 14:24:33 +0000 | [diff] [blame] | 1518 | ** |
| 1519 | ** The page size must be a power of 2 between 512 and 65536. If the page |
| 1520 | ** size supplied does not meet this constraint then the page size is not |
| 1521 | ** changed. |
| 1522 | ** |
| 1523 | ** Page sizes are constrained to be a power of two so that the region |
| 1524 | ** of the database file used for locking (beginning at PENDING_BYTE, |
| 1525 | ** the first byte past the 1GB boundary, 0x40000000) needs to occur |
| 1526 | ** at the beginning of a page. |
danielk1977 | 2812956 | 2005-01-11 10:25:06 +0000 | [diff] [blame] | 1527 | ** |
| 1528 | ** If parameter nReserve is less than zero, then the number of reserved |
| 1529 | ** bytes per page is left unchanged. |
drh | 90f5ecb | 2004-07-22 01:19:35 +0000 | [diff] [blame] | 1530 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1531 | int sqlite3BtreeSetPageSize(Btree *p, int pageSize, int nReserve){ |
danielk1977 | a1644fd | 2007-08-29 12:31:25 +0000 | [diff] [blame] | 1532 | int rc = SQLITE_OK; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1533 | BtShared *pBt = p->pBt; |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 1534 | sqlite3BtreeEnter(p); |
drh | 90f5ecb | 2004-07-22 01:19:35 +0000 | [diff] [blame] | 1535 | if( pBt->pageSizeFixed ){ |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 1536 | sqlite3BtreeLeave(p); |
drh | 90f5ecb | 2004-07-22 01:19:35 +0000 | [diff] [blame] | 1537 | return SQLITE_READONLY; |
| 1538 | } |
| 1539 | if( nReserve<0 ){ |
| 1540 | nReserve = pBt->pageSize - pBt->usableSize; |
| 1541 | } |
drh | 06f5021 | 2004-11-02 14:24:33 +0000 | [diff] [blame] | 1542 | if( pageSize>=512 && pageSize<=SQLITE_MAX_PAGE_SIZE && |
| 1543 | ((pageSize-1)&pageSize)==0 ){ |
drh | 07d183d | 2005-05-01 22:52:42 +0000 | [diff] [blame] | 1544 | assert( (pageSize & 7)==0 ); |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1545 | assert( !pBt->pPage1 && !pBt->pCursor ); |
danielk1977 | a1644fd | 2007-08-29 12:31:25 +0000 | [diff] [blame] | 1546 | pBt->pageSize = pageSize; |
| 1547 | rc = sqlite3PagerSetPagesize(pBt->pPager, &pBt->pageSize); |
drh | 90f5ecb | 2004-07-22 01:19:35 +0000 | [diff] [blame] | 1548 | } |
| 1549 | pBt->usableSize = pBt->pageSize - nReserve; |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 1550 | sqlite3BtreeLeave(p); |
danielk1977 | a1644fd | 2007-08-29 12:31:25 +0000 | [diff] [blame] | 1551 | return rc; |
drh | 90f5ecb | 2004-07-22 01:19:35 +0000 | [diff] [blame] | 1552 | } |
| 1553 | |
| 1554 | /* |
| 1555 | ** Return the currently defined page size |
| 1556 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1557 | int sqlite3BtreeGetPageSize(Btree *p){ |
| 1558 | return p->pBt->pageSize; |
drh | 90f5ecb | 2004-07-22 01:19:35 +0000 | [diff] [blame] | 1559 | } |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1560 | int sqlite3BtreeGetReserve(Btree *p){ |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 1561 | int n; |
| 1562 | sqlite3BtreeEnter(p); |
| 1563 | n = p->pBt->pageSize - p->pBt->usableSize; |
| 1564 | sqlite3BtreeLeave(p); |
| 1565 | return n; |
drh | 2011d5f | 2004-07-22 02:40:37 +0000 | [diff] [blame] | 1566 | } |
drh | f8e632b | 2007-05-08 14:51:36 +0000 | [diff] [blame] | 1567 | |
| 1568 | /* |
| 1569 | ** Set the maximum page count for a database if mxPage is positive. |
| 1570 | ** No changes are made if mxPage is 0 or negative. |
| 1571 | ** Regardless of the value of mxPage, return the maximum page count. |
| 1572 | */ |
| 1573 | int sqlite3BtreeMaxPageCount(Btree *p, int mxPage){ |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 1574 | int n; |
| 1575 | sqlite3BtreeEnter(p); |
| 1576 | n = sqlite3PagerMaxPageCount(p->pBt->pPager, mxPage); |
| 1577 | sqlite3BtreeLeave(p); |
| 1578 | return n; |
drh | f8e632b | 2007-05-08 14:51:36 +0000 | [diff] [blame] | 1579 | } |
danielk1977 | 576ec6b | 2005-01-21 11:55:25 +0000 | [diff] [blame] | 1580 | #endif /* !defined(SQLITE_OMIT_PAGER_PRAGMAS) || !defined(SQLITE_OMIT_VACUUM) */ |
drh | 90f5ecb | 2004-07-22 01:19:35 +0000 | [diff] [blame] | 1581 | |
| 1582 | /* |
danielk1977 | 951af80 | 2004-11-05 15:45:09 +0000 | [diff] [blame] | 1583 | ** Change the 'auto-vacuum' property of the database. If the 'autoVacuum' |
| 1584 | ** parameter is non-zero, then auto-vacuum mode is enabled. If zero, it |
| 1585 | ** is disabled. The default value for the auto-vacuum property is |
| 1586 | ** determined by the SQLITE_DEFAULT_AUTOVACUUM macro. |
| 1587 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1588 | int sqlite3BtreeSetAutoVacuum(Btree *p, int autoVacuum){ |
danielk1977 | 951af80 | 2004-11-05 15:45:09 +0000 | [diff] [blame] | 1589 | #ifdef SQLITE_OMIT_AUTOVACUUM |
drh | eee46cf | 2004-11-06 00:02:48 +0000 | [diff] [blame] | 1590 | return SQLITE_READONLY; |
danielk1977 | 951af80 | 2004-11-05 15:45:09 +0000 | [diff] [blame] | 1591 | #else |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 1592 | BtShared *pBt = p->pBt; |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 1593 | int rc = SQLITE_OK; |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 1594 | int av = (autoVacuum?1:0); |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 1595 | |
| 1596 | sqlite3BtreeEnter(p); |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 1597 | if( pBt->pageSizeFixed && av!=pBt->autoVacuum ){ |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 1598 | rc = SQLITE_READONLY; |
| 1599 | }else{ |
| 1600 | pBt->autoVacuum = av; |
danielk1977 | 951af80 | 2004-11-05 15:45:09 +0000 | [diff] [blame] | 1601 | } |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 1602 | sqlite3BtreeLeave(p); |
| 1603 | return rc; |
danielk1977 | 951af80 | 2004-11-05 15:45:09 +0000 | [diff] [blame] | 1604 | #endif |
| 1605 | } |
| 1606 | |
| 1607 | /* |
| 1608 | ** Return the value of the 'auto-vacuum' property. If auto-vacuum is |
| 1609 | ** enabled 1 is returned. Otherwise 0. |
| 1610 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1611 | int sqlite3BtreeGetAutoVacuum(Btree *p){ |
danielk1977 | 951af80 | 2004-11-05 15:45:09 +0000 | [diff] [blame] | 1612 | #ifdef SQLITE_OMIT_AUTOVACUUM |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 1613 | return BTREE_AUTOVACUUM_NONE; |
danielk1977 | 951af80 | 2004-11-05 15:45:09 +0000 | [diff] [blame] | 1614 | #else |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 1615 | int rc; |
| 1616 | sqlite3BtreeEnter(p); |
| 1617 | rc = ( |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 1618 | (!p->pBt->autoVacuum)?BTREE_AUTOVACUUM_NONE: |
| 1619 | (!p->pBt->incrVacuum)?BTREE_AUTOVACUUM_FULL: |
| 1620 | BTREE_AUTOVACUUM_INCR |
| 1621 | ); |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 1622 | sqlite3BtreeLeave(p); |
| 1623 | return rc; |
danielk1977 | 951af80 | 2004-11-05 15:45:09 +0000 | [diff] [blame] | 1624 | #endif |
| 1625 | } |
| 1626 | |
| 1627 | |
| 1628 | /* |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 1629 | ** Get a reference to pPage1 of the database file. This will |
drh | 306dc21 | 2001-05-21 13:45:10 +0000 | [diff] [blame] | 1630 | ** also acquire a readlock on that file. |
| 1631 | ** |
| 1632 | ** SQLITE_OK is returned on success. If the file is not a |
| 1633 | ** well-formed database file, then SQLITE_CORRUPT is returned. |
| 1634 | ** SQLITE_BUSY is returned if the database is locked. SQLITE_NOMEM |
drh | 4f0ee68 | 2007-03-30 20:43:40 +0000 | [diff] [blame] | 1635 | ** is returned if we run out of memory. |
drh | 306dc21 | 2001-05-21 13:45:10 +0000 | [diff] [blame] | 1636 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1637 | static int lockBtree(BtShared *pBt){ |
danielk1977 | f653d78 | 2008-03-20 11:04:21 +0000 | [diff] [blame^] | 1638 | int rc; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1639 | MemPage *pPage1; |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 1640 | |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 1641 | assert( sqlite3_mutex_held(pBt->mutex) ); |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 1642 | if( pBt->pPage1 ) return SQLITE_OK; |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 1643 | rc = sqlite3BtreeGetPage(pBt, 1, &pPage1, 0); |
drh | 306dc21 | 2001-05-21 13:45:10 +0000 | [diff] [blame] | 1644 | if( rc!=SQLITE_OK ) return rc; |
drh | 306dc21 | 2001-05-21 13:45:10 +0000 | [diff] [blame] | 1645 | |
| 1646 | /* Do some checking to help insure the file we opened really is |
| 1647 | ** a valid database file. |
| 1648 | */ |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 1649 | rc = SQLITE_NOTADB; |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 1650 | if( sqlite3PagerPagecount(pBt->pPager)>0 ){ |
danielk1977 | f653d78 | 2008-03-20 11:04:21 +0000 | [diff] [blame^] | 1651 | int pageSize; |
| 1652 | int usableSize; |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 1653 | u8 *page1 = pPage1->aData; |
| 1654 | if( memcmp(page1, zMagicHeader, 16)!=0 ){ |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 1655 | goto page1_init_failed; |
drh | 306dc21 | 2001-05-21 13:45:10 +0000 | [diff] [blame] | 1656 | } |
drh | 309169a | 2007-04-24 17:27:51 +0000 | [diff] [blame] | 1657 | if( page1[18]>1 ){ |
| 1658 | pBt->readOnly = 1; |
| 1659 | } |
| 1660 | if( page1[19]>1 ){ |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 1661 | goto page1_init_failed; |
| 1662 | } |
drh | 07d183d | 2005-05-01 22:52:42 +0000 | [diff] [blame] | 1663 | pageSize = get2byte(&page1[16]); |
drh | 7dc385e | 2007-09-06 23:39:36 +0000 | [diff] [blame] | 1664 | if( ((pageSize-1)&pageSize)!=0 || pageSize<512 || |
| 1665 | (SQLITE_MAX_PAGE_SIZE<32768 && pageSize>SQLITE_MAX_PAGE_SIZE) |
| 1666 | ){ |
drh | 07d183d | 2005-05-01 22:52:42 +0000 | [diff] [blame] | 1667 | goto page1_init_failed; |
| 1668 | } |
| 1669 | assert( (pageSize & 7)==0 ); |
danielk1977 | f653d78 | 2008-03-20 11:04:21 +0000 | [diff] [blame^] | 1670 | usableSize = pageSize - page1[20]; |
| 1671 | if( pageSize!=pBt->pageSize ){ |
| 1672 | /* After reading the first page of the database assuming a page size |
| 1673 | ** of BtShared.pageSize, we have discovered that the page-size is |
| 1674 | ** actually pageSize. Unlock the database, leave pBt->pPage1 at |
| 1675 | ** zero and return SQLITE_OK. The caller will call this function |
| 1676 | ** again with the correct page-size. |
| 1677 | */ |
| 1678 | releasePage(pPage1); |
| 1679 | pBt->usableSize = usableSize; |
| 1680 | pBt->pageSize = pageSize; |
| 1681 | sqlite3PagerSetPagesize(pBt->pPager, &pBt->pageSize); |
| 1682 | return SQLITE_OK; |
| 1683 | } |
| 1684 | if( usableSize<500 ){ |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 1685 | goto page1_init_failed; |
| 1686 | } |
danielk1977 | f653d78 | 2008-03-20 11:04:21 +0000 | [diff] [blame^] | 1687 | pBt->pageSize = pageSize; |
| 1688 | pBt->usableSize = usableSize; |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 1689 | pBt->maxEmbedFrac = page1[21]; |
| 1690 | pBt->minEmbedFrac = page1[22]; |
| 1691 | pBt->minLeafFrac = page1[23]; |
drh | 057cd3a | 2005-02-15 16:23:02 +0000 | [diff] [blame] | 1692 | #ifndef SQLITE_OMIT_AUTOVACUUM |
| 1693 | pBt->autoVacuum = (get4byte(&page1[36 + 4*4])?1:0); |
danielk1977 | 27b1f95 | 2007-06-25 08:16:58 +0000 | [diff] [blame] | 1694 | pBt->incrVacuum = (get4byte(&page1[36 + 7*4])?1:0); |
drh | 057cd3a | 2005-02-15 16:23:02 +0000 | [diff] [blame] | 1695 | #endif |
drh | 306dc21 | 2001-05-21 13:45:10 +0000 | [diff] [blame] | 1696 | } |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 1697 | |
| 1698 | /* maxLocal is the maximum amount of payload to store locally for |
| 1699 | ** a cell. Make sure it is small enough so that at least minFanout |
| 1700 | ** cells can will fit on one page. We assume a 10-byte page header. |
| 1701 | ** Besides the payload, the cell must store: |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 1702 | ** 2-byte pointer to the cell |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 1703 | ** 4-byte child pointer |
| 1704 | ** 9-byte nKey value |
| 1705 | ** 4-byte nData value |
| 1706 | ** 4-byte overflow page pointer |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 1707 | ** So a cell consists of a 2-byte poiner, a header which is as much as |
| 1708 | ** 17 bytes long, 0 to N bytes of payload, and an optional 4 byte overflow |
| 1709 | ** page pointer. |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 1710 | */ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 1711 | pBt->maxLocal = (pBt->usableSize-12)*pBt->maxEmbedFrac/255 - 23; |
| 1712 | pBt->minLocal = (pBt->usableSize-12)*pBt->minEmbedFrac/255 - 23; |
| 1713 | pBt->maxLeaf = pBt->usableSize - 35; |
| 1714 | pBt->minLeaf = (pBt->usableSize-12)*pBt->minLeafFrac/255 - 23; |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 1715 | if( pBt->minLocal>pBt->maxLocal || pBt->maxLocal<0 ){ |
| 1716 | goto page1_init_failed; |
| 1717 | } |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 1718 | assert( pBt->maxLeaf + 23 <= MX_CELL_SIZE(pBt) ); |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1719 | pBt->pPage1 = pPage1; |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 1720 | return SQLITE_OK; |
drh | 306dc21 | 2001-05-21 13:45:10 +0000 | [diff] [blame] | 1721 | |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 1722 | page1_init_failed: |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1723 | releasePage(pPage1); |
| 1724 | pBt->pPage1 = 0; |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 1725 | return rc; |
drh | 306dc21 | 2001-05-21 13:45:10 +0000 | [diff] [blame] | 1726 | } |
| 1727 | |
| 1728 | /* |
drh | b8ef32c | 2005-03-14 02:01:49 +0000 | [diff] [blame] | 1729 | ** This routine works like lockBtree() except that it also invokes the |
| 1730 | ** busy callback if there is lock contention. |
| 1731 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1732 | static int lockBtreeWithRetry(Btree *pRef){ |
drh | b8ef32c | 2005-03-14 02:01:49 +0000 | [diff] [blame] | 1733 | int rc = SQLITE_OK; |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 1734 | |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 1735 | assert( sqlite3BtreeHoldsMutex(pRef) ); |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1736 | if( pRef->inTrans==TRANS_NONE ){ |
| 1737 | u8 inTransaction = pRef->pBt->inTransaction; |
| 1738 | btreeIntegrity(pRef); |
| 1739 | rc = sqlite3BtreeBeginTrans(pRef, 0); |
| 1740 | pRef->pBt->inTransaction = inTransaction; |
| 1741 | pRef->inTrans = TRANS_NONE; |
| 1742 | if( rc==SQLITE_OK ){ |
| 1743 | pRef->pBt->nTransaction--; |
| 1744 | } |
| 1745 | btreeIntegrity(pRef); |
drh | b8ef32c | 2005-03-14 02:01:49 +0000 | [diff] [blame] | 1746 | } |
| 1747 | return rc; |
| 1748 | } |
| 1749 | |
| 1750 | |
| 1751 | /* |
drh | b8ca307 | 2001-12-05 00:21:20 +0000 | [diff] [blame] | 1752 | ** If there are no outstanding cursors and we are not in the middle |
| 1753 | ** of a transaction but there is a read lock on the database, then |
| 1754 | ** this routine unrefs the first page of the database file which |
| 1755 | ** has the effect of releasing the read lock. |
| 1756 | ** |
| 1757 | ** If there are any outstanding cursors, this routine is a no-op. |
| 1758 | ** |
| 1759 | ** If there is a transaction in progress, this routine is a no-op. |
| 1760 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1761 | static void unlockBtreeIfUnused(BtShared *pBt){ |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 1762 | assert( sqlite3_mutex_held(pBt->mutex) ); |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1763 | if( pBt->inTransaction==TRANS_NONE && pBt->pCursor==0 && pBt->pPage1!=0 ){ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 1764 | if( sqlite3PagerRefcount(pBt->pPager)>=1 ){ |
drh | de4fcfd | 2008-01-19 23:50:26 +0000 | [diff] [blame] | 1765 | assert( pBt->pPage1->aData ); |
| 1766 | #if 0 |
drh | 24c9a2e | 2007-01-05 02:00:47 +0000 | [diff] [blame] | 1767 | if( pBt->pPage1->aData==0 ){ |
| 1768 | MemPage *pPage = pBt->pPage1; |
drh | bf4bca5 | 2007-09-06 22:19:14 +0000 | [diff] [blame] | 1769 | pPage->aData = sqlite3PagerGetData(pPage->pDbPage); |
drh | 24c9a2e | 2007-01-05 02:00:47 +0000 | [diff] [blame] | 1770 | pPage->pBt = pBt; |
| 1771 | pPage->pgno = 1; |
| 1772 | } |
drh | de4fcfd | 2008-01-19 23:50:26 +0000 | [diff] [blame] | 1773 | #endif |
drh | 24c9a2e | 2007-01-05 02:00:47 +0000 | [diff] [blame] | 1774 | releasePage(pBt->pPage1); |
drh | 51c6d96 | 2004-06-06 00:42:25 +0000 | [diff] [blame] | 1775 | } |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1776 | pBt->pPage1 = 0; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1777 | pBt->inStmt = 0; |
drh | b8ca307 | 2001-12-05 00:21:20 +0000 | [diff] [blame] | 1778 | } |
| 1779 | } |
| 1780 | |
| 1781 | /* |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 1782 | ** Create a new database by initializing the first page of the |
drh | 8c42ca9 | 2001-06-22 19:15:00 +0000 | [diff] [blame] | 1783 | ** file. |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 1784 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1785 | static int newDatabase(BtShared *pBt){ |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 1786 | MemPage *pP1; |
| 1787 | unsigned char *data; |
drh | 8c42ca9 | 2001-06-22 19:15:00 +0000 | [diff] [blame] | 1788 | int rc; |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 1789 | |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 1790 | assert( sqlite3_mutex_held(pBt->mutex) ); |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 1791 | if( sqlite3PagerPagecount(pBt->pPager)>0 ) return SQLITE_OK; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 1792 | pP1 = pBt->pPage1; |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 1793 | assert( pP1!=0 ); |
| 1794 | data = pP1->aData; |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 1795 | rc = sqlite3PagerWrite(pP1->pDbPage); |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 1796 | if( rc ) return rc; |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 1797 | memcpy(data, zMagicHeader, sizeof(zMagicHeader)); |
| 1798 | assert( sizeof(zMagicHeader)==16 ); |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 1799 | put2byte(&data[16], pBt->pageSize); |
drh | 9e572e6 | 2004-04-23 23:43:10 +0000 | [diff] [blame] | 1800 | data[18] = 1; |
| 1801 | data[19] = 1; |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 1802 | data[20] = pBt->pageSize - pBt->usableSize; |
| 1803 | data[21] = pBt->maxEmbedFrac; |
| 1804 | data[22] = pBt->minEmbedFrac; |
| 1805 | data[23] = pBt->minLeafFrac; |
| 1806 | memset(&data[24], 0, 100-24); |
drh | e6c4381 | 2004-05-14 12:17:46 +0000 | [diff] [blame] | 1807 | zeroPage(pP1, PTF_INTKEY|PTF_LEAF|PTF_LEAFDATA ); |
drh | f2a611c | 2004-09-05 00:33:43 +0000 | [diff] [blame] | 1808 | pBt->pageSizeFixed = 1; |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 1809 | #ifndef SQLITE_OMIT_AUTOVACUUM |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 1810 | assert( pBt->autoVacuum==1 || pBt->autoVacuum==0 ); |
danielk1977 | 418899a | 2007-06-24 10:14:00 +0000 | [diff] [blame] | 1811 | assert( pBt->incrVacuum==1 || pBt->incrVacuum==0 ); |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 1812 | put4byte(&data[36 + 4*4], pBt->autoVacuum); |
danielk1977 | 418899a | 2007-06-24 10:14:00 +0000 | [diff] [blame] | 1813 | put4byte(&data[36 + 7*4], pBt->incrVacuum); |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 1814 | #endif |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 1815 | return SQLITE_OK; |
| 1816 | } |
| 1817 | |
| 1818 | /* |
danielk1977 | ee5741e | 2004-05-31 10:01:34 +0000 | [diff] [blame] | 1819 | ** Attempt to start a new transaction. A write-transaction |
drh | 684917c | 2004-10-05 02:41:42 +0000 | [diff] [blame] | 1820 | ** is started if the second argument is nonzero, otherwise a read- |
| 1821 | ** transaction. If the second argument is 2 or more and exclusive |
| 1822 | ** transaction is started, meaning that no other process is allowed |
| 1823 | ** to access the database. A preexisting transaction may not be |
drh | b8ef32c | 2005-03-14 02:01:49 +0000 | [diff] [blame] | 1824 | ** upgraded to exclusive by calling this routine a second time - the |
drh | 684917c | 2004-10-05 02:41:42 +0000 | [diff] [blame] | 1825 | ** exclusivity flag only works for a new transaction. |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 1826 | ** |
danielk1977 | ee5741e | 2004-05-31 10:01:34 +0000 | [diff] [blame] | 1827 | ** A write-transaction must be started before attempting any |
| 1828 | ** changes to the database. None of the following routines |
| 1829 | ** will work unless a transaction is started first: |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 1830 | ** |
drh | 23e11ca | 2004-05-04 17:27:28 +0000 | [diff] [blame] | 1831 | ** sqlite3BtreeCreateTable() |
| 1832 | ** sqlite3BtreeCreateIndex() |
| 1833 | ** sqlite3BtreeClearTable() |
| 1834 | ** sqlite3BtreeDropTable() |
| 1835 | ** sqlite3BtreeInsert() |
| 1836 | ** sqlite3BtreeDelete() |
| 1837 | ** sqlite3BtreeUpdateMeta() |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 1838 | ** |
drh | b8ef32c | 2005-03-14 02:01:49 +0000 | [diff] [blame] | 1839 | ** If an initial attempt to acquire the lock fails because of lock contention |
| 1840 | ** and the database was previously unlocked, then invoke the busy handler |
| 1841 | ** if there is one. But if there was previously a read-lock, do not |
| 1842 | ** invoke the busy handler - just return SQLITE_BUSY. SQLITE_BUSY is |
| 1843 | ** returned when there is already a read-lock in order to avoid a deadlock. |
| 1844 | ** |
| 1845 | ** Suppose there are two processes A and B. A has a read lock and B has |
| 1846 | ** a reserved lock. B tries to promote to exclusive but is blocked because |
| 1847 | ** of A's read lock. A tries to promote to reserved but is blocked by B. |
| 1848 | ** One or the other of the two processes must give way or there can be |
| 1849 | ** no progress. By returning SQLITE_BUSY and not invoking the busy callback |
| 1850 | ** when A already has a read lock, we encourage A to give up and let B |
| 1851 | ** proceed. |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 1852 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1853 | int sqlite3BtreeBeginTrans(Btree *p, int wrflag){ |
| 1854 | BtShared *pBt = p->pBt; |
danielk1977 | ee5741e | 2004-05-31 10:01:34 +0000 | [diff] [blame] | 1855 | int rc = SQLITE_OK; |
| 1856 | |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 1857 | sqlite3BtreeEnter(p); |
drh | e5fe690 | 2007-12-07 18:55:28 +0000 | [diff] [blame] | 1858 | pBt->db = p->db; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1859 | btreeIntegrity(p); |
| 1860 | |
danielk1977 | ee5741e | 2004-05-31 10:01:34 +0000 | [diff] [blame] | 1861 | /* If the btree is already in a write-transaction, or it |
| 1862 | ** is already in a read-transaction and a read-transaction |
| 1863 | ** is requested, this is a no-op. |
| 1864 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1865 | if( p->inTrans==TRANS_WRITE || (p->inTrans==TRANS_READ && !wrflag) ){ |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 1866 | goto trans_begun; |
danielk1977 | ee5741e | 2004-05-31 10:01:34 +0000 | [diff] [blame] | 1867 | } |
drh | b8ef32c | 2005-03-14 02:01:49 +0000 | [diff] [blame] | 1868 | |
| 1869 | /* Write transactions are not possible on a read-only database */ |
danielk1977 | ee5741e | 2004-05-31 10:01:34 +0000 | [diff] [blame] | 1870 | if( pBt->readOnly && wrflag ){ |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 1871 | rc = SQLITE_READONLY; |
| 1872 | goto trans_begun; |
danielk1977 | ee5741e | 2004-05-31 10:01:34 +0000 | [diff] [blame] | 1873 | } |
| 1874 | |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1875 | /* If another database handle has already opened a write transaction |
| 1876 | ** on this shared-btree structure and a second write transaction is |
| 1877 | ** requested, return SQLITE_BUSY. |
| 1878 | */ |
| 1879 | if( pBt->inTransaction==TRANS_WRITE && wrflag ){ |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 1880 | rc = SQLITE_BUSY; |
| 1881 | goto trans_begun; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1882 | } |
| 1883 | |
danielk1977 | 641b0f4 | 2007-12-21 04:47:25 +0000 | [diff] [blame] | 1884 | #ifndef SQLITE_OMIT_SHARED_CACHE |
| 1885 | if( wrflag>1 ){ |
| 1886 | BtLock *pIter; |
| 1887 | for(pIter=pBt->pLock; pIter; pIter=pIter->pNext){ |
| 1888 | if( pIter->pBtree!=p ){ |
| 1889 | rc = SQLITE_BUSY; |
| 1890 | goto trans_begun; |
| 1891 | } |
| 1892 | } |
| 1893 | } |
| 1894 | #endif |
| 1895 | |
drh | b8ef32c | 2005-03-14 02:01:49 +0000 | [diff] [blame] | 1896 | do { |
danielk1977 | f653d78 | 2008-03-20 11:04:21 +0000 | [diff] [blame^] | 1897 | while( rc==SQLITE_OK && pBt->pPage1==0 ){ |
drh | b8ef32c | 2005-03-14 02:01:49 +0000 | [diff] [blame] | 1898 | rc = lockBtree(pBt); |
drh | 8c42ca9 | 2001-06-22 19:15:00 +0000 | [diff] [blame] | 1899 | } |
drh | 309169a | 2007-04-24 17:27:51 +0000 | [diff] [blame] | 1900 | |
drh | b8ef32c | 2005-03-14 02:01:49 +0000 | [diff] [blame] | 1901 | if( rc==SQLITE_OK && wrflag ){ |
drh | 309169a | 2007-04-24 17:27:51 +0000 | [diff] [blame] | 1902 | if( pBt->readOnly ){ |
| 1903 | rc = SQLITE_READONLY; |
| 1904 | }else{ |
| 1905 | rc = sqlite3PagerBegin(pBt->pPage1->pDbPage, wrflag>1); |
| 1906 | if( rc==SQLITE_OK ){ |
| 1907 | rc = newDatabase(pBt); |
| 1908 | } |
drh | b8ef32c | 2005-03-14 02:01:49 +0000 | [diff] [blame] | 1909 | } |
| 1910 | } |
| 1911 | |
| 1912 | if( rc==SQLITE_OK ){ |
drh | b8ef32c | 2005-03-14 02:01:49 +0000 | [diff] [blame] | 1913 | if( wrflag ) pBt->inStmt = 0; |
| 1914 | }else{ |
| 1915 | unlockBtreeIfUnused(pBt); |
| 1916 | } |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1917 | }while( rc==SQLITE_BUSY && pBt->inTransaction==TRANS_NONE && |
drh | e5fe690 | 2007-12-07 18:55:28 +0000 | [diff] [blame] | 1918 | sqlite3BtreeInvokeBusyHandler(pBt, 0) ); |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1919 | |
| 1920 | if( rc==SQLITE_OK ){ |
| 1921 | if( p->inTrans==TRANS_NONE ){ |
| 1922 | pBt->nTransaction++; |
| 1923 | } |
| 1924 | p->inTrans = (wrflag?TRANS_WRITE:TRANS_READ); |
| 1925 | if( p->inTrans>pBt->inTransaction ){ |
| 1926 | pBt->inTransaction = p->inTrans; |
| 1927 | } |
danielk1977 | 641b0f4 | 2007-12-21 04:47:25 +0000 | [diff] [blame] | 1928 | #ifndef SQLITE_OMIT_SHARED_CACHE |
| 1929 | if( wrflag>1 ){ |
| 1930 | assert( !pBt->pExclusive ); |
| 1931 | pBt->pExclusive = p; |
| 1932 | } |
| 1933 | #endif |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1934 | } |
| 1935 | |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 1936 | |
| 1937 | trans_begun: |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1938 | btreeIntegrity(p); |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 1939 | sqlite3BtreeLeave(p); |
drh | b8ca307 | 2001-12-05 00:21:20 +0000 | [diff] [blame] | 1940 | return rc; |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 1941 | } |
| 1942 | |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 1943 | #ifndef SQLITE_OMIT_AUTOVACUUM |
| 1944 | |
| 1945 | /* |
| 1946 | ** Set the pointer-map entries for all children of page pPage. Also, if |
| 1947 | ** pPage contains cells that point to overflow pages, set the pointer |
| 1948 | ** map entries for the overflow pages as well. |
| 1949 | */ |
| 1950 | static int setChildPtrmaps(MemPage *pPage){ |
| 1951 | int i; /* Counter variable */ |
| 1952 | int nCell; /* Number of cells in page pPage */ |
danielk1977 | 2df71c7 | 2007-05-24 07:22:42 +0000 | [diff] [blame] | 1953 | int rc; /* Return code */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 1954 | BtShared *pBt = pPage->pBt; |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 1955 | int isInitOrig = pPage->isInit; |
| 1956 | Pgno pgno = pPage->pgno; |
| 1957 | |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 1958 | assert( sqlite3_mutex_held(pPage->pBt->mutex) ); |
danielk1977 | 2df71c7 | 2007-05-24 07:22:42 +0000 | [diff] [blame] | 1959 | rc = sqlite3BtreeInitPage(pPage, pPage->pParent); |
| 1960 | if( rc!=SQLITE_OK ){ |
| 1961 | goto set_child_ptrmaps_out; |
| 1962 | } |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 1963 | nCell = pPage->nCell; |
| 1964 | |
| 1965 | for(i=0; i<nCell; i++){ |
danielk1977 | 1cc5ed8 | 2007-05-16 17:28:43 +0000 | [diff] [blame] | 1966 | u8 *pCell = findCell(pPage, i); |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 1967 | |
danielk1977 | 2683665 | 2005-01-17 01:33:13 +0000 | [diff] [blame] | 1968 | rc = ptrmapPutOvflPtr(pPage, pCell); |
| 1969 | if( rc!=SQLITE_OK ){ |
| 1970 | goto set_child_ptrmaps_out; |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 1971 | } |
danielk1977 | 2683665 | 2005-01-17 01:33:13 +0000 | [diff] [blame] | 1972 | |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 1973 | if( !pPage->leaf ){ |
| 1974 | Pgno childPgno = get4byte(pCell); |
| 1975 | rc = ptrmapPut(pBt, childPgno, PTRMAP_BTREE, pgno); |
| 1976 | if( rc!=SQLITE_OK ) goto set_child_ptrmaps_out; |
| 1977 | } |
| 1978 | } |
| 1979 | |
| 1980 | if( !pPage->leaf ){ |
| 1981 | Pgno childPgno = get4byte(&pPage->aData[pPage->hdrOffset+8]); |
| 1982 | rc = ptrmapPut(pBt, childPgno, PTRMAP_BTREE, pgno); |
| 1983 | } |
| 1984 | |
| 1985 | set_child_ptrmaps_out: |
| 1986 | pPage->isInit = isInitOrig; |
| 1987 | return rc; |
| 1988 | } |
| 1989 | |
| 1990 | /* |
| 1991 | ** Somewhere on pPage, which is guarenteed to be a btree page, not an overflow |
| 1992 | ** page, is a pointer to page iFrom. Modify this pointer so that it points to |
| 1993 | ** iTo. Parameter eType describes the type of pointer to be modified, as |
| 1994 | ** follows: |
| 1995 | ** |
| 1996 | ** PTRMAP_BTREE: pPage is a btree-page. The pointer points at a child |
| 1997 | ** page of pPage. |
| 1998 | ** |
| 1999 | ** PTRMAP_OVERFLOW1: pPage is a btree-page. The pointer points at an overflow |
| 2000 | ** page pointed to by one of the cells on pPage. |
| 2001 | ** |
| 2002 | ** PTRMAP_OVERFLOW2: pPage is an overflow-page. The pointer points at the next |
| 2003 | ** overflow page in the list. |
| 2004 | */ |
danielk1977 | fdb7cdb | 2005-01-17 02:12:18 +0000 | [diff] [blame] | 2005 | static int modifyPagePointer(MemPage *pPage, Pgno iFrom, Pgno iTo, u8 eType){ |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 2006 | assert( sqlite3_mutex_held(pPage->pBt->mutex) ); |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 2007 | if( eType==PTRMAP_OVERFLOW2 ){ |
danielk1977 | f78fc08 | 2004-11-02 14:40:32 +0000 | [diff] [blame] | 2008 | /* 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] | 2009 | if( get4byte(pPage->aData)!=iFrom ){ |
drh | 4928570 | 2005-09-17 15:20:26 +0000 | [diff] [blame] | 2010 | return SQLITE_CORRUPT_BKPT; |
danielk1977 | fdb7cdb | 2005-01-17 02:12:18 +0000 | [diff] [blame] | 2011 | } |
danielk1977 | f78fc08 | 2004-11-02 14:40:32 +0000 | [diff] [blame] | 2012 | put4byte(pPage->aData, iTo); |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 2013 | }else{ |
| 2014 | int isInitOrig = pPage->isInit; |
| 2015 | int i; |
| 2016 | int nCell; |
| 2017 | |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 2018 | sqlite3BtreeInitPage(pPage, 0); |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 2019 | nCell = pPage->nCell; |
| 2020 | |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 2021 | for(i=0; i<nCell; i++){ |
danielk1977 | 1cc5ed8 | 2007-05-16 17:28:43 +0000 | [diff] [blame] | 2022 | u8 *pCell = findCell(pPage, i); |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 2023 | if( eType==PTRMAP_OVERFLOW1 ){ |
| 2024 | CellInfo info; |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 2025 | sqlite3BtreeParseCellPtr(pPage, pCell, &info); |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 2026 | if( info.iOverflow ){ |
| 2027 | if( iFrom==get4byte(&pCell[info.iOverflow]) ){ |
| 2028 | put4byte(&pCell[info.iOverflow], iTo); |
| 2029 | break; |
| 2030 | } |
| 2031 | } |
| 2032 | }else{ |
| 2033 | if( get4byte(pCell)==iFrom ){ |
| 2034 | put4byte(pCell, iTo); |
| 2035 | break; |
| 2036 | } |
| 2037 | } |
| 2038 | } |
| 2039 | |
| 2040 | if( i==nCell ){ |
danielk1977 | fdb7cdb | 2005-01-17 02:12:18 +0000 | [diff] [blame] | 2041 | if( eType!=PTRMAP_BTREE || |
| 2042 | get4byte(&pPage->aData[pPage->hdrOffset+8])!=iFrom ){ |
drh | 4928570 | 2005-09-17 15:20:26 +0000 | [diff] [blame] | 2043 | return SQLITE_CORRUPT_BKPT; |
danielk1977 | fdb7cdb | 2005-01-17 02:12:18 +0000 | [diff] [blame] | 2044 | } |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 2045 | put4byte(&pPage->aData[pPage->hdrOffset+8], iTo); |
| 2046 | } |
| 2047 | |
| 2048 | pPage->isInit = isInitOrig; |
| 2049 | } |
danielk1977 | fdb7cdb | 2005-01-17 02:12:18 +0000 | [diff] [blame] | 2050 | return SQLITE_OK; |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 2051 | } |
| 2052 | |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 2053 | |
danielk1977 | 7701e81 | 2005-01-10 12:59:51 +0000 | [diff] [blame] | 2054 | /* |
| 2055 | ** Move the open database page pDbPage to location iFreePage in the |
| 2056 | ** database. The pDbPage reference remains valid. |
| 2057 | */ |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 2058 | static int relocatePage( |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2059 | BtShared *pBt, /* Btree */ |
danielk1977 | 7701e81 | 2005-01-10 12:59:51 +0000 | [diff] [blame] | 2060 | MemPage *pDbPage, /* Open page to move */ |
| 2061 | u8 eType, /* Pointer map 'type' entry for pDbPage */ |
| 2062 | Pgno iPtrPage, /* Pointer map 'page-no' entry for pDbPage */ |
| 2063 | Pgno iFreePage /* The location to move pDbPage to */ |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 2064 | ){ |
| 2065 | MemPage *pPtrPage; /* The page that contains a pointer to pDbPage */ |
| 2066 | Pgno iDbPage = pDbPage->pgno; |
| 2067 | Pager *pPager = pBt->pPager; |
| 2068 | int rc; |
| 2069 | |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 2070 | assert( eType==PTRMAP_OVERFLOW2 || eType==PTRMAP_OVERFLOW1 || |
| 2071 | eType==PTRMAP_BTREE || eType==PTRMAP_ROOTPAGE ); |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 2072 | assert( sqlite3_mutex_held(pBt->mutex) ); |
drh | d0679ed | 2007-08-28 22:24:34 +0000 | [diff] [blame] | 2073 | assert( pDbPage->pBt==pBt ); |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 2074 | |
drh | 85b623f | 2007-12-13 21:54:09 +0000 | [diff] [blame] | 2075 | /* Move page iDbPage from its current location to page number iFreePage */ |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 2076 | TRACE(("AUTOVACUUM: Moving %d to free page %d (ptr page %d type %d)\n", |
| 2077 | iDbPage, iFreePage, iPtrPage, eType)); |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 2078 | rc = sqlite3PagerMovepage(pPager, pDbPage->pDbPage, iFreePage); |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 2079 | if( rc!=SQLITE_OK ){ |
| 2080 | return rc; |
| 2081 | } |
| 2082 | pDbPage->pgno = iFreePage; |
| 2083 | |
| 2084 | /* If pDbPage was a btree-page, then it may have child pages and/or cells |
| 2085 | ** that point to overflow pages. The pointer map entries for all these |
| 2086 | ** pages need to be changed. |
| 2087 | ** |
| 2088 | ** If pDbPage is an overflow page, then the first 4 bytes may store a |
| 2089 | ** pointer to a subsequent overflow page. If this is the case, then |
| 2090 | ** the pointer map needs to be updated for the subsequent overflow page. |
| 2091 | */ |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 2092 | if( eType==PTRMAP_BTREE || eType==PTRMAP_ROOTPAGE ){ |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 2093 | rc = setChildPtrmaps(pDbPage); |
| 2094 | if( rc!=SQLITE_OK ){ |
| 2095 | return rc; |
| 2096 | } |
| 2097 | }else{ |
| 2098 | Pgno nextOvfl = get4byte(pDbPage->aData); |
| 2099 | if( nextOvfl!=0 ){ |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 2100 | rc = ptrmapPut(pBt, nextOvfl, PTRMAP_OVERFLOW2, iFreePage); |
| 2101 | if( rc!=SQLITE_OK ){ |
| 2102 | return rc; |
| 2103 | } |
| 2104 | } |
| 2105 | } |
| 2106 | |
| 2107 | /* Fix the database pointer on page iPtrPage that pointed at iDbPage so |
| 2108 | ** that it points at iFreePage. Also fix the pointer map entry for |
| 2109 | ** iPtrPage. |
| 2110 | */ |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 2111 | if( eType!=PTRMAP_ROOTPAGE ){ |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 2112 | rc = sqlite3BtreeGetPage(pBt, iPtrPage, &pPtrPage, 0); |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 2113 | if( rc!=SQLITE_OK ){ |
| 2114 | return rc; |
| 2115 | } |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 2116 | rc = sqlite3PagerWrite(pPtrPage->pDbPage); |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 2117 | if( rc!=SQLITE_OK ){ |
| 2118 | releasePage(pPtrPage); |
| 2119 | return rc; |
| 2120 | } |
danielk1977 | fdb7cdb | 2005-01-17 02:12:18 +0000 | [diff] [blame] | 2121 | rc = modifyPagePointer(pPtrPage, iDbPage, iFreePage, eType); |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 2122 | releasePage(pPtrPage); |
danielk1977 | fdb7cdb | 2005-01-17 02:12:18 +0000 | [diff] [blame] | 2123 | if( rc==SQLITE_OK ){ |
| 2124 | rc = ptrmapPut(pBt, iFreePage, eType, iPtrPage); |
| 2125 | } |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 2126 | } |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 2127 | return rc; |
| 2128 | } |
| 2129 | |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 2130 | /* Forward declaration required by incrVacuumStep(). */ |
drh | 4f0c587 | 2007-03-26 22:05:01 +0000 | [diff] [blame] | 2131 | static int allocateBtreePage(BtShared *, MemPage **, Pgno *, Pgno, u8); |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 2132 | |
| 2133 | /* |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 2134 | ** Perform a single step of an incremental-vacuum. If successful, |
| 2135 | ** return SQLITE_OK. If there is no work to do (and therefore no |
| 2136 | ** point in calling this function again), return SQLITE_DONE. |
| 2137 | ** |
| 2138 | ** More specificly, this function attempts to re-organize the |
| 2139 | ** database so that the last page of the file currently in use |
| 2140 | ** is no longer in use. |
| 2141 | ** |
| 2142 | ** If the nFin parameter is non-zero, the implementation assumes |
| 2143 | ** that the caller will keep calling incrVacuumStep() until |
| 2144 | ** it returns SQLITE_DONE or an error, and that nFin is the |
| 2145 | ** number of pages the database file will contain after this |
| 2146 | ** process is complete. |
| 2147 | */ |
| 2148 | static int incrVacuumStep(BtShared *pBt, Pgno nFin){ |
| 2149 | Pgno iLastPg; /* Last page in the database */ |
| 2150 | Pgno nFreeList; /* Number of pages still on the free-list */ |
| 2151 | |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 2152 | assert( sqlite3_mutex_held(pBt->mutex) ); |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 2153 | iLastPg = pBt->nTrunc; |
| 2154 | if( iLastPg==0 ){ |
| 2155 | iLastPg = sqlite3PagerPagecount(pBt->pPager); |
| 2156 | } |
| 2157 | |
| 2158 | if( !PTRMAP_ISPAGE(pBt, iLastPg) && iLastPg!=PENDING_BYTE_PAGE(pBt) ){ |
| 2159 | int rc; |
| 2160 | u8 eType; |
| 2161 | Pgno iPtrPage; |
| 2162 | |
| 2163 | nFreeList = get4byte(&pBt->pPage1->aData[36]); |
| 2164 | if( nFreeList==0 || nFin==iLastPg ){ |
| 2165 | return SQLITE_DONE; |
| 2166 | } |
| 2167 | |
| 2168 | rc = ptrmapGet(pBt, iLastPg, &eType, &iPtrPage); |
| 2169 | if( rc!=SQLITE_OK ){ |
| 2170 | return rc; |
| 2171 | } |
| 2172 | if( eType==PTRMAP_ROOTPAGE ){ |
| 2173 | return SQLITE_CORRUPT_BKPT; |
| 2174 | } |
| 2175 | |
| 2176 | if( eType==PTRMAP_FREEPAGE ){ |
| 2177 | if( nFin==0 ){ |
| 2178 | /* Remove the page from the files free-list. This is not required |
danielk1977 | 4ef2449 | 2007-05-23 09:52:41 +0000 | [diff] [blame] | 2179 | ** if nFin is non-zero. In that case, the free-list will be |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 2180 | ** truncated to zero after this function returns, so it doesn't |
| 2181 | ** matter if it still contains some garbage entries. |
| 2182 | */ |
| 2183 | Pgno iFreePg; |
| 2184 | MemPage *pFreePg; |
| 2185 | rc = allocateBtreePage(pBt, &pFreePg, &iFreePg, iLastPg, 1); |
| 2186 | if( rc!=SQLITE_OK ){ |
| 2187 | return rc; |
| 2188 | } |
| 2189 | assert( iFreePg==iLastPg ); |
| 2190 | releasePage(pFreePg); |
| 2191 | } |
| 2192 | } else { |
| 2193 | Pgno iFreePg; /* Index of free page to move pLastPg to */ |
| 2194 | MemPage *pLastPg; |
| 2195 | |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 2196 | rc = sqlite3BtreeGetPage(pBt, iLastPg, &pLastPg, 0); |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 2197 | if( rc!=SQLITE_OK ){ |
| 2198 | return rc; |
| 2199 | } |
| 2200 | |
danielk1977 | b4626a3 | 2007-04-28 15:47:43 +0000 | [diff] [blame] | 2201 | /* If nFin is zero, this loop runs exactly once and page pLastPg |
| 2202 | ** is swapped with the first free page pulled off the free list. |
| 2203 | ** |
| 2204 | ** On the other hand, if nFin is greater than zero, then keep |
| 2205 | ** looping until a free-page located within the first nFin pages |
| 2206 | ** of the file is found. |
| 2207 | */ |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 2208 | do { |
| 2209 | MemPage *pFreePg; |
| 2210 | rc = allocateBtreePage(pBt, &pFreePg, &iFreePg, 0, 0); |
| 2211 | if( rc!=SQLITE_OK ){ |
| 2212 | releasePage(pLastPg); |
| 2213 | return rc; |
| 2214 | } |
| 2215 | releasePage(pFreePg); |
| 2216 | }while( nFin!=0 && iFreePg>nFin ); |
| 2217 | assert( iFreePg<iLastPg ); |
danielk1977 | b4626a3 | 2007-04-28 15:47:43 +0000 | [diff] [blame] | 2218 | |
| 2219 | rc = sqlite3PagerWrite(pLastPg->pDbPage); |
danielk1977 | 662278e | 2007-11-05 15:30:12 +0000 | [diff] [blame] | 2220 | if( rc==SQLITE_OK ){ |
| 2221 | rc = relocatePage(pBt, pLastPg, eType, iPtrPage, iFreePg); |
| 2222 | } |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 2223 | releasePage(pLastPg); |
| 2224 | if( rc!=SQLITE_OK ){ |
| 2225 | return rc; |
danielk1977 | 662278e | 2007-11-05 15:30:12 +0000 | [diff] [blame] | 2226 | } |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 2227 | } |
| 2228 | } |
| 2229 | |
| 2230 | pBt->nTrunc = iLastPg - 1; |
| 2231 | while( pBt->nTrunc==PENDING_BYTE_PAGE(pBt)||PTRMAP_ISPAGE(pBt, pBt->nTrunc) ){ |
| 2232 | pBt->nTrunc--; |
| 2233 | } |
| 2234 | return SQLITE_OK; |
| 2235 | } |
| 2236 | |
| 2237 | /* |
| 2238 | ** A write-transaction must be opened before calling this function. |
| 2239 | ** It performs a single unit of work towards an incremental vacuum. |
| 2240 | ** |
| 2241 | ** If the incremental vacuum is finished after this function has run, |
| 2242 | ** SQLITE_DONE is returned. If it is not finished, but no error occured, |
| 2243 | ** SQLITE_OK is returned. Otherwise an SQLite error code. |
| 2244 | */ |
| 2245 | int sqlite3BtreeIncrVacuum(Btree *p){ |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 2246 | int rc; |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 2247 | BtShared *pBt = p->pBt; |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 2248 | |
| 2249 | sqlite3BtreeEnter(p); |
drh | e5fe690 | 2007-12-07 18:55:28 +0000 | [diff] [blame] | 2250 | pBt->db = p->db; |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 2251 | assert( pBt->inTransaction==TRANS_WRITE && p->inTrans==TRANS_WRITE ); |
| 2252 | if( !pBt->autoVacuum ){ |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 2253 | rc = SQLITE_DONE; |
| 2254 | }else{ |
| 2255 | invalidateAllOverflowCache(pBt); |
| 2256 | rc = incrVacuumStep(pBt, 0); |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 2257 | } |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 2258 | sqlite3BtreeLeave(p); |
| 2259 | return rc; |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 2260 | } |
| 2261 | |
| 2262 | /* |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 2263 | ** This routine is called prior to sqlite3PagerCommit when a transaction |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 2264 | ** is commited for an auto-vacuum database. |
danielk1977 | 2416872 | 2007-04-02 05:07:47 +0000 | [diff] [blame] | 2265 | ** |
| 2266 | ** If SQLITE_OK is returned, then *pnTrunc is set to the number of pages |
| 2267 | ** the database file should be truncated to during the commit process. |
| 2268 | ** i.e. the database has been reorganized so that only the first *pnTrunc |
| 2269 | ** pages are in use. |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 2270 | */ |
danielk1977 | 2416872 | 2007-04-02 05:07:47 +0000 | [diff] [blame] | 2271 | static int autoVacuumCommit(BtShared *pBt, Pgno *pnTrunc){ |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 2272 | int rc = SQLITE_OK; |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 2273 | Pager *pPager = pBt->pPager; |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 2274 | #ifndef NDEBUG |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 2275 | int nRef = sqlite3PagerRefcount(pPager); |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 2276 | #endif |
| 2277 | |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 2278 | assert( sqlite3_mutex_held(pBt->mutex) ); |
danielk1977 | 92d4d7a | 2007-05-04 12:05:56 +0000 | [diff] [blame] | 2279 | invalidateAllOverflowCache(pBt); |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 2280 | assert(pBt->autoVacuum); |
| 2281 | if( !pBt->incrVacuum ){ |
| 2282 | Pgno nFin = 0; |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 2283 | |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 2284 | if( pBt->nTrunc==0 ){ |
| 2285 | Pgno nFree; |
| 2286 | Pgno nPtrmap; |
| 2287 | const int pgsz = pBt->pageSize; |
| 2288 | Pgno nOrig = sqlite3PagerPagecount(pBt->pPager); |
danielk1977 | e5321f0 | 2007-04-27 07:05:44 +0000 | [diff] [blame] | 2289 | |
| 2290 | if( PTRMAP_ISPAGE(pBt, nOrig) ){ |
| 2291 | return SQLITE_CORRUPT_BKPT; |
| 2292 | } |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 2293 | if( nOrig==PENDING_BYTE_PAGE(pBt) ){ |
| 2294 | nOrig--; |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 2295 | } |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 2296 | nFree = get4byte(&pBt->pPage1->aData[36]); |
| 2297 | nPtrmap = (nFree-nOrig+PTRMAP_PAGENO(pBt, nOrig)+pgsz/5)/(pgsz/5); |
| 2298 | nFin = nOrig - nFree - nPtrmap; |
| 2299 | if( nOrig>PENDING_BYTE_PAGE(pBt) && nFin<=PENDING_BYTE_PAGE(pBt) ){ |
| 2300 | nFin--; |
danielk1977 | ac11ee6 | 2005-01-15 12:45:51 +0000 | [diff] [blame] | 2301 | } |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 2302 | while( PTRMAP_ISPAGE(pBt, nFin) || nFin==PENDING_BYTE_PAGE(pBt) ){ |
| 2303 | nFin--; |
| 2304 | } |
| 2305 | } |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 2306 | |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 2307 | while( rc==SQLITE_OK ){ |
| 2308 | rc = incrVacuumStep(pBt, nFin); |
| 2309 | } |
| 2310 | if( rc==SQLITE_DONE ){ |
| 2311 | assert(nFin==0 || pBt->nTrunc==0 || nFin<=pBt->nTrunc); |
| 2312 | rc = SQLITE_OK; |
| 2313 | if( pBt->nTrunc ){ |
drh | 67f80b6 | 2007-07-23 19:26:17 +0000 | [diff] [blame] | 2314 | rc = sqlite3PagerWrite(pBt->pPage1->pDbPage); |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 2315 | put4byte(&pBt->pPage1->aData[32], 0); |
| 2316 | put4byte(&pBt->pPage1->aData[36], 0); |
| 2317 | pBt->nTrunc = nFin; |
| 2318 | } |
| 2319 | } |
| 2320 | if( rc!=SQLITE_OK ){ |
| 2321 | sqlite3PagerRollback(pPager); |
| 2322 | } |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 2323 | } |
| 2324 | |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 2325 | if( rc==SQLITE_OK ){ |
| 2326 | *pnTrunc = pBt->nTrunc; |
| 2327 | pBt->nTrunc = 0; |
| 2328 | } |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 2329 | assert( nRef==sqlite3PagerRefcount(pPager) ); |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 2330 | return rc; |
| 2331 | } |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 2332 | |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 2333 | #endif |
| 2334 | |
| 2335 | /* |
drh | 80e35f4 | 2007-03-30 14:06:34 +0000 | [diff] [blame] | 2336 | ** This routine does the first phase of a two-phase commit. This routine |
| 2337 | ** causes a rollback journal to be created (if it does not already exist) |
| 2338 | ** and populated with enough information so that if a power loss occurs |
| 2339 | ** the database can be restored to its original state by playing back |
| 2340 | ** the journal. Then the contents of the journal are flushed out to |
| 2341 | ** the disk. After the journal is safely on oxide, the changes to the |
| 2342 | ** database are written into the database file and flushed to oxide. |
| 2343 | ** At the end of this call, the rollback journal still exists on the |
| 2344 | ** disk and we are still holding all locks, so the transaction has not |
| 2345 | ** committed. See sqlite3BtreeCommit() for the second phase of the |
| 2346 | ** commit process. |
| 2347 | ** |
| 2348 | ** This call is a no-op if no write-transaction is currently active on pBt. |
| 2349 | ** |
| 2350 | ** Otherwise, sync the database file for the btree pBt. zMaster points to |
| 2351 | ** the name of a master journal file that should be written into the |
| 2352 | ** individual journal file, or is NULL, indicating no master journal file |
| 2353 | ** (single database transaction). |
| 2354 | ** |
| 2355 | ** When this is called, the master journal should already have been |
| 2356 | ** created, populated with this journal pointer and synced to disk. |
| 2357 | ** |
| 2358 | ** Once this is routine has returned, the only thing required to commit |
| 2359 | ** the write-transaction for this database file is to delete the journal. |
| 2360 | */ |
| 2361 | int sqlite3BtreeCommitPhaseOne(Btree *p, const char *zMaster){ |
| 2362 | int rc = SQLITE_OK; |
| 2363 | if( p->inTrans==TRANS_WRITE ){ |
| 2364 | BtShared *pBt = p->pBt; |
| 2365 | Pgno nTrunc = 0; |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 2366 | sqlite3BtreeEnter(p); |
drh | e5fe690 | 2007-12-07 18:55:28 +0000 | [diff] [blame] | 2367 | pBt->db = p->db; |
drh | 80e35f4 | 2007-03-30 14:06:34 +0000 | [diff] [blame] | 2368 | #ifndef SQLITE_OMIT_AUTOVACUUM |
| 2369 | if( pBt->autoVacuum ){ |
| 2370 | rc = autoVacuumCommit(pBt, &nTrunc); |
| 2371 | if( rc!=SQLITE_OK ){ |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 2372 | sqlite3BtreeLeave(p); |
drh | 80e35f4 | 2007-03-30 14:06:34 +0000 | [diff] [blame] | 2373 | return rc; |
| 2374 | } |
| 2375 | } |
| 2376 | #endif |
danielk1977 | f653d78 | 2008-03-20 11:04:21 +0000 | [diff] [blame^] | 2377 | rc = sqlite3PagerCommitPhaseOne(pBt->pPager, zMaster, nTrunc, 0); |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 2378 | sqlite3BtreeLeave(p); |
drh | 80e35f4 | 2007-03-30 14:06:34 +0000 | [diff] [blame] | 2379 | } |
| 2380 | return rc; |
| 2381 | } |
| 2382 | |
| 2383 | /* |
drh | 2aa679f | 2001-06-25 02:11:07 +0000 | [diff] [blame] | 2384 | ** Commit the transaction currently in progress. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2385 | ** |
drh | 6e34599 | 2007-03-30 11:12:08 +0000 | [diff] [blame] | 2386 | ** This routine implements the second phase of a 2-phase commit. The |
| 2387 | ** sqlite3BtreeSync() routine does the first phase and should be invoked |
| 2388 | ** prior to calling this routine. The sqlite3BtreeSync() routine did |
| 2389 | ** all the work of writing information out to disk and flushing the |
| 2390 | ** contents so that they are written onto the disk platter. All this |
| 2391 | ** routine has to do is delete or truncate the rollback journal |
| 2392 | ** (which causes the transaction to commit) and drop locks. |
| 2393 | ** |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2394 | ** This will release the write lock on the database file. If there |
| 2395 | ** are no active cursors, it also releases the read lock. |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 2396 | */ |
drh | 80e35f4 | 2007-03-30 14:06:34 +0000 | [diff] [blame] | 2397 | int sqlite3BtreeCommitPhaseTwo(Btree *p){ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2398 | BtShared *pBt = p->pBt; |
| 2399 | |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 2400 | sqlite3BtreeEnter(p); |
drh | e5fe690 | 2007-12-07 18:55:28 +0000 | [diff] [blame] | 2401 | pBt->db = p->db; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2402 | btreeIntegrity(p); |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2403 | |
| 2404 | /* If the handle has a write-transaction open, commit the shared-btrees |
| 2405 | ** transaction and set the shared state to TRANS_READ. |
| 2406 | */ |
| 2407 | if( p->inTrans==TRANS_WRITE ){ |
danielk1977 | 7f7bc66 | 2006-01-23 13:47:47 +0000 | [diff] [blame] | 2408 | int rc; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2409 | assert( pBt->inTransaction==TRANS_WRITE ); |
| 2410 | assert( pBt->nTransaction>0 ); |
drh | 80e35f4 | 2007-03-30 14:06:34 +0000 | [diff] [blame] | 2411 | rc = sqlite3PagerCommitPhaseTwo(pBt->pPager); |
danielk1977 | 7f7bc66 | 2006-01-23 13:47:47 +0000 | [diff] [blame] | 2412 | if( rc!=SQLITE_OK ){ |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 2413 | sqlite3BtreeLeave(p); |
danielk1977 | 7f7bc66 | 2006-01-23 13:47:47 +0000 | [diff] [blame] | 2414 | return rc; |
| 2415 | } |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2416 | pBt->inTransaction = TRANS_READ; |
| 2417 | pBt->inStmt = 0; |
danielk1977 | ee5741e | 2004-05-31 10:01:34 +0000 | [diff] [blame] | 2418 | } |
danielk1977 | 7f7bc66 | 2006-01-23 13:47:47 +0000 | [diff] [blame] | 2419 | unlockAllTables(p); |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2420 | |
| 2421 | /* If the handle has any kind of transaction open, decrement the transaction |
| 2422 | ** count of the shared btree. If the transaction count reaches 0, set |
| 2423 | ** the shared state to TRANS_NONE. The unlockBtreeIfUnused() call below |
| 2424 | ** will unlock the pager. |
| 2425 | */ |
| 2426 | if( p->inTrans!=TRANS_NONE ){ |
| 2427 | pBt->nTransaction--; |
| 2428 | if( 0==pBt->nTransaction ){ |
| 2429 | pBt->inTransaction = TRANS_NONE; |
| 2430 | } |
| 2431 | } |
| 2432 | |
| 2433 | /* Set the handles current transaction state to TRANS_NONE and unlock |
| 2434 | ** the pager if this call closed the only read or write transaction. |
| 2435 | */ |
| 2436 | p->inTrans = TRANS_NONE; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2437 | unlockBtreeIfUnused(pBt); |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2438 | |
| 2439 | btreeIntegrity(p); |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 2440 | sqlite3BtreeLeave(p); |
danielk1977 | 7f7bc66 | 2006-01-23 13:47:47 +0000 | [diff] [blame] | 2441 | return SQLITE_OK; |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 2442 | } |
| 2443 | |
drh | 80e35f4 | 2007-03-30 14:06:34 +0000 | [diff] [blame] | 2444 | /* |
| 2445 | ** Do both phases of a commit. |
| 2446 | */ |
| 2447 | int sqlite3BtreeCommit(Btree *p){ |
| 2448 | int rc; |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 2449 | sqlite3BtreeEnter(p); |
drh | 80e35f4 | 2007-03-30 14:06:34 +0000 | [diff] [blame] | 2450 | rc = sqlite3BtreeCommitPhaseOne(p, 0); |
| 2451 | if( rc==SQLITE_OK ){ |
| 2452 | rc = sqlite3BtreeCommitPhaseTwo(p); |
| 2453 | } |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 2454 | sqlite3BtreeLeave(p); |
drh | 80e35f4 | 2007-03-30 14:06:34 +0000 | [diff] [blame] | 2455 | return rc; |
| 2456 | } |
| 2457 | |
danielk1977 | fbcd585 | 2004-06-15 02:44:18 +0000 | [diff] [blame] | 2458 | #ifndef NDEBUG |
| 2459 | /* |
| 2460 | ** Return the number of write-cursors open on this handle. This is for use |
| 2461 | ** in assert() expressions, so it is only compiled if NDEBUG is not |
| 2462 | ** defined. |
drh | fb98264 | 2007-08-30 01:19:59 +0000 | [diff] [blame] | 2463 | ** |
| 2464 | ** For the purposes of this routine, a write-cursor is any cursor that |
| 2465 | ** is capable of writing to the databse. That means the cursor was |
| 2466 | ** originally opened for writing and the cursor has not be disabled |
| 2467 | ** by having its state changed to CURSOR_FAULT. |
danielk1977 | fbcd585 | 2004-06-15 02:44:18 +0000 | [diff] [blame] | 2468 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2469 | static int countWriteCursors(BtShared *pBt){ |
danielk1977 | fbcd585 | 2004-06-15 02:44:18 +0000 | [diff] [blame] | 2470 | BtCursor *pCur; |
| 2471 | int r = 0; |
| 2472 | for(pCur=pBt->pCursor; pCur; pCur=pCur->pNext){ |
drh | fb98264 | 2007-08-30 01:19:59 +0000 | [diff] [blame] | 2473 | if( pCur->wrFlag && pCur->eState!=CURSOR_FAULT ) r++; |
danielk1977 | fbcd585 | 2004-06-15 02:44:18 +0000 | [diff] [blame] | 2474 | } |
| 2475 | return r; |
| 2476 | } |
| 2477 | #endif |
| 2478 | |
drh | c39e000 | 2004-05-07 23:50:57 +0000 | [diff] [blame] | 2479 | /* |
drh | fb98264 | 2007-08-30 01:19:59 +0000 | [diff] [blame] | 2480 | ** This routine sets the state to CURSOR_FAULT and the error |
| 2481 | ** code to errCode for every cursor on BtShared that pBtree |
| 2482 | ** references. |
| 2483 | ** |
| 2484 | ** Every cursor is tripped, including cursors that belong |
| 2485 | ** to other database connections that happen to be sharing |
| 2486 | ** the cache with pBtree. |
| 2487 | ** |
| 2488 | ** This routine gets called when a rollback occurs. |
| 2489 | ** All cursors using the same cache must be tripped |
| 2490 | ** to prevent them from trying to use the btree after |
| 2491 | ** the rollback. The rollback may have deleted tables |
| 2492 | ** or moved root pages, so it is not sufficient to |
| 2493 | ** save the state of the cursor. The cursor must be |
| 2494 | ** invalidated. |
| 2495 | */ |
| 2496 | void sqlite3BtreeTripAllCursors(Btree *pBtree, int errCode){ |
| 2497 | BtCursor *p; |
| 2498 | sqlite3BtreeEnter(pBtree); |
| 2499 | for(p=pBtree->pBt->pCursor; p; p=p->pNext){ |
| 2500 | clearCursorPosition(p); |
| 2501 | p->eState = CURSOR_FAULT; |
| 2502 | p->skip = errCode; |
| 2503 | } |
| 2504 | sqlite3BtreeLeave(pBtree); |
| 2505 | } |
| 2506 | |
| 2507 | /* |
drh | ecdc753 | 2001-09-23 02:35:53 +0000 | [diff] [blame] | 2508 | ** Rollback the transaction in progress. All cursors will be |
| 2509 | ** invalided by this operation. Any attempt to use a cursor |
| 2510 | ** that was open at the beginning of this operation will result |
| 2511 | ** in an error. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2512 | ** |
| 2513 | ** This will release the write lock on the database file. If there |
| 2514 | ** are no active cursors, it also releases the read lock. |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 2515 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2516 | int sqlite3BtreeRollback(Btree *p){ |
danielk1977 | 8d34dfd | 2006-01-24 16:37:57 +0000 | [diff] [blame] | 2517 | int rc; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2518 | BtShared *pBt = p->pBt; |
drh | 24cd67e | 2004-05-10 16:18:47 +0000 | [diff] [blame] | 2519 | MemPage *pPage1; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2520 | |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 2521 | sqlite3BtreeEnter(p); |
drh | e5fe690 | 2007-12-07 18:55:28 +0000 | [diff] [blame] | 2522 | pBt->db = p->db; |
danielk1977 | 2b8c13e | 2006-01-24 14:21:24 +0000 | [diff] [blame] | 2523 | rc = saveAllCursors(pBt, 0, 0); |
danielk1977 | 8d34dfd | 2006-01-24 16:37:57 +0000 | [diff] [blame] | 2524 | #ifndef SQLITE_OMIT_SHARED_CACHE |
danielk1977 | 2b8c13e | 2006-01-24 14:21:24 +0000 | [diff] [blame] | 2525 | if( rc!=SQLITE_OK ){ |
danielk1977 | 8d34dfd | 2006-01-24 16:37:57 +0000 | [diff] [blame] | 2526 | /* This is a horrible situation. An IO or malloc() error occured whilst |
| 2527 | ** trying to save cursor positions. If this is an automatic rollback (as |
| 2528 | ** the result of a constraint, malloc() failure or IO error) then |
| 2529 | ** the cache may be internally inconsistent (not contain valid trees) so |
| 2530 | ** we cannot simply return the error to the caller. Instead, abort |
| 2531 | ** all queries that may be using any of the cursors that failed to save. |
| 2532 | */ |
drh | fb98264 | 2007-08-30 01:19:59 +0000 | [diff] [blame] | 2533 | sqlite3BtreeTripAllCursors(p, rc); |
danielk1977 | 2b8c13e | 2006-01-24 14:21:24 +0000 | [diff] [blame] | 2534 | } |
danielk1977 | 8d34dfd | 2006-01-24 16:37:57 +0000 | [diff] [blame] | 2535 | #endif |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2536 | btreeIntegrity(p); |
| 2537 | unlockAllTables(p); |
| 2538 | |
| 2539 | if( p->inTrans==TRANS_WRITE ){ |
danielk1977 | 8d34dfd | 2006-01-24 16:37:57 +0000 | [diff] [blame] | 2540 | int rc2; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2541 | |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 2542 | #ifndef SQLITE_OMIT_AUTOVACUUM |
| 2543 | pBt->nTrunc = 0; |
| 2544 | #endif |
| 2545 | |
danielk1977 | 8d34dfd | 2006-01-24 16:37:57 +0000 | [diff] [blame] | 2546 | assert( TRANS_WRITE==pBt->inTransaction ); |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 2547 | rc2 = sqlite3PagerRollback(pBt->pPager); |
danielk1977 | 8d34dfd | 2006-01-24 16:37:57 +0000 | [diff] [blame] | 2548 | if( rc2!=SQLITE_OK ){ |
| 2549 | rc = rc2; |
| 2550 | } |
| 2551 | |
drh | 24cd67e | 2004-05-10 16:18:47 +0000 | [diff] [blame] | 2552 | /* The rollback may have destroyed the pPage1->aData value. So |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 2553 | ** call sqlite3BtreeGetPage() on page 1 again to make |
| 2554 | ** sure pPage1->aData is set correctly. */ |
| 2555 | if( sqlite3BtreeGetPage(pBt, 1, &pPage1, 0)==SQLITE_OK ){ |
drh | 24cd67e | 2004-05-10 16:18:47 +0000 | [diff] [blame] | 2556 | releasePage(pPage1); |
| 2557 | } |
danielk1977 | fbcd585 | 2004-06-15 02:44:18 +0000 | [diff] [blame] | 2558 | assert( countWriteCursors(pBt)==0 ); |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2559 | pBt->inTransaction = TRANS_READ; |
drh | 24cd67e | 2004-05-10 16:18:47 +0000 | [diff] [blame] | 2560 | } |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2561 | |
| 2562 | if( p->inTrans!=TRANS_NONE ){ |
| 2563 | assert( pBt->nTransaction>0 ); |
| 2564 | pBt->nTransaction--; |
| 2565 | if( 0==pBt->nTransaction ){ |
| 2566 | pBt->inTransaction = TRANS_NONE; |
| 2567 | } |
| 2568 | } |
| 2569 | |
| 2570 | p->inTrans = TRANS_NONE; |
danielk1977 | ee5741e | 2004-05-31 10:01:34 +0000 | [diff] [blame] | 2571 | pBt->inStmt = 0; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2572 | unlockBtreeIfUnused(pBt); |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2573 | |
| 2574 | btreeIntegrity(p); |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 2575 | sqlite3BtreeLeave(p); |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 2576 | return rc; |
| 2577 | } |
| 2578 | |
| 2579 | /* |
drh | ab01f61 | 2004-05-22 02:55:23 +0000 | [diff] [blame] | 2580 | ** Start a statement subtransaction. The subtransaction can |
| 2581 | ** can be rolled back independently of the main transaction. |
| 2582 | ** You must start a transaction before starting a subtransaction. |
| 2583 | ** The subtransaction is ended automatically if the main transaction |
drh | 663fc63 | 2002-02-02 18:49:19 +0000 | [diff] [blame] | 2584 | ** commits or rolls back. |
| 2585 | ** |
drh | ab01f61 | 2004-05-22 02:55:23 +0000 | [diff] [blame] | 2586 | ** Only one subtransaction may be active at a time. It is an error to try |
| 2587 | ** to start a new subtransaction if another subtransaction is already active. |
| 2588 | ** |
| 2589 | ** Statement subtransactions are used around individual SQL statements |
| 2590 | ** that are contained within a BEGIN...COMMIT block. If a constraint |
| 2591 | ** error occurs within the statement, the effect of that one statement |
| 2592 | ** can be rolled back without having to rollback the entire transaction. |
drh | 663fc63 | 2002-02-02 18:49:19 +0000 | [diff] [blame] | 2593 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2594 | int sqlite3BtreeBeginStmt(Btree *p){ |
drh | 663fc63 | 2002-02-02 18:49:19 +0000 | [diff] [blame] | 2595 | int rc; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2596 | BtShared *pBt = p->pBt; |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 2597 | sqlite3BtreeEnter(p); |
drh | e5fe690 | 2007-12-07 18:55:28 +0000 | [diff] [blame] | 2598 | pBt->db = p->db; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2599 | if( (p->inTrans!=TRANS_WRITE) || pBt->inStmt ){ |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 2600 | rc = pBt->readOnly ? SQLITE_READONLY : SQLITE_ERROR; |
| 2601 | }else{ |
| 2602 | assert( pBt->inTransaction==TRANS_WRITE ); |
| 2603 | rc = pBt->readOnly ? SQLITE_OK : sqlite3PagerStmtBegin(pBt->pPager); |
| 2604 | pBt->inStmt = 1; |
drh | 0d65dc0 | 2002-02-03 00:56:09 +0000 | [diff] [blame] | 2605 | } |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 2606 | sqlite3BtreeLeave(p); |
drh | 663fc63 | 2002-02-02 18:49:19 +0000 | [diff] [blame] | 2607 | return rc; |
| 2608 | } |
| 2609 | |
| 2610 | |
| 2611 | /* |
drh | ab01f61 | 2004-05-22 02:55:23 +0000 | [diff] [blame] | 2612 | ** Commit the statment subtransaction currently in progress. If no |
| 2613 | ** subtransaction is active, this is a no-op. |
drh | 663fc63 | 2002-02-02 18:49:19 +0000 | [diff] [blame] | 2614 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2615 | int sqlite3BtreeCommitStmt(Btree *p){ |
drh | 663fc63 | 2002-02-02 18:49:19 +0000 | [diff] [blame] | 2616 | int rc; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2617 | BtShared *pBt = p->pBt; |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 2618 | sqlite3BtreeEnter(p); |
drh | e5fe690 | 2007-12-07 18:55:28 +0000 | [diff] [blame] | 2619 | pBt->db = p->db; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2620 | if( pBt->inStmt && !pBt->readOnly ){ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 2621 | rc = sqlite3PagerStmtCommit(pBt->pPager); |
drh | 663fc63 | 2002-02-02 18:49:19 +0000 | [diff] [blame] | 2622 | }else{ |
| 2623 | rc = SQLITE_OK; |
| 2624 | } |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2625 | pBt->inStmt = 0; |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 2626 | sqlite3BtreeLeave(p); |
drh | 663fc63 | 2002-02-02 18:49:19 +0000 | [diff] [blame] | 2627 | return rc; |
| 2628 | } |
| 2629 | |
| 2630 | /* |
drh | ab01f61 | 2004-05-22 02:55:23 +0000 | [diff] [blame] | 2631 | ** Rollback the active statement subtransaction. If no subtransaction |
| 2632 | ** is active this routine is a no-op. |
drh | 663fc63 | 2002-02-02 18:49:19 +0000 | [diff] [blame] | 2633 | ** |
drh | ab01f61 | 2004-05-22 02:55:23 +0000 | [diff] [blame] | 2634 | ** All cursors will be invalidated by this operation. Any attempt |
drh | 663fc63 | 2002-02-02 18:49:19 +0000 | [diff] [blame] | 2635 | ** to use a cursor that was open at the beginning of this operation |
| 2636 | ** will result in an error. |
| 2637 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2638 | int sqlite3BtreeRollbackStmt(Btree *p){ |
danielk1977 | 97a227c | 2006-01-20 16:32:04 +0000 | [diff] [blame] | 2639 | int rc = SQLITE_OK; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2640 | BtShared *pBt = p->pBt; |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 2641 | sqlite3BtreeEnter(p); |
drh | e5fe690 | 2007-12-07 18:55:28 +0000 | [diff] [blame] | 2642 | pBt->db = p->db; |
danielk1977 | 97a227c | 2006-01-20 16:32:04 +0000 | [diff] [blame] | 2643 | if( pBt->inStmt && !pBt->readOnly ){ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 2644 | rc = sqlite3PagerStmtRollback(pBt->pPager); |
danielk1977 | 97a227c | 2006-01-20 16:32:04 +0000 | [diff] [blame] | 2645 | assert( countWriteCursors(pBt)==0 ); |
| 2646 | pBt->inStmt = 0; |
| 2647 | } |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 2648 | sqlite3BtreeLeave(p); |
drh | 663fc63 | 2002-02-02 18:49:19 +0000 | [diff] [blame] | 2649 | return rc; |
| 2650 | } |
| 2651 | |
| 2652 | /* |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2653 | ** Default key comparison function to be used if no comparison function |
| 2654 | ** is specified on the sqlite3BtreeCursor() call. |
| 2655 | */ |
| 2656 | static int dfltCompare( |
| 2657 | void *NotUsed, /* User data is not used */ |
| 2658 | int n1, const void *p1, /* First key to compare */ |
| 2659 | int n2, const void *p2 /* Second key to compare */ |
| 2660 | ){ |
| 2661 | int c; |
| 2662 | c = memcmp(p1, p2, n1<n2 ? n1 : n2); |
| 2663 | if( c==0 ){ |
| 2664 | c = n1 - n2; |
| 2665 | } |
| 2666 | return c; |
| 2667 | } |
| 2668 | |
| 2669 | /* |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 2670 | ** Create a new cursor for the BTree whose root is on the page |
| 2671 | ** iTable. The act of acquiring a cursor gets a read lock on |
| 2672 | ** the database file. |
drh | 1bee3d7 | 2001-10-15 00:44:35 +0000 | [diff] [blame] | 2673 | ** |
| 2674 | ** If wrFlag==0, then the cursor can only be used for reading. |
drh | f74b8d9 | 2002-09-01 23:20:45 +0000 | [diff] [blame] | 2675 | ** If wrFlag==1, then the cursor can be used for reading or for |
| 2676 | ** writing if other conditions for writing are also met. These |
| 2677 | ** are the conditions that must be met in order for writing to |
| 2678 | ** be allowed: |
drh | 6446c4d | 2001-12-15 14:22:18 +0000 | [diff] [blame] | 2679 | ** |
drh | f74b8d9 | 2002-09-01 23:20:45 +0000 | [diff] [blame] | 2680 | ** 1: The cursor must have been opened with wrFlag==1 |
| 2681 | ** |
drh | fe5d71d | 2007-03-19 11:54:10 +0000 | [diff] [blame] | 2682 | ** 2: Other database connections that share the same pager cache |
| 2683 | ** but which are not in the READ_UNCOMMITTED state may not have |
| 2684 | ** cursors open with wrFlag==0 on the same table. Otherwise |
| 2685 | ** the changes made by this write cursor would be visible to |
| 2686 | ** the read cursors in the other database connection. |
drh | f74b8d9 | 2002-09-01 23:20:45 +0000 | [diff] [blame] | 2687 | ** |
| 2688 | ** 3: The database must be writable (not on read-only media) |
| 2689 | ** |
| 2690 | ** 4: There must be an active transaction. |
| 2691 | ** |
drh | 6446c4d | 2001-12-15 14:22:18 +0000 | [diff] [blame] | 2692 | ** No checking is done to make sure that page iTable really is the |
| 2693 | ** root page of a b-tree. If it is not, then the cursor acquired |
| 2694 | ** will not work correctly. |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2695 | ** |
| 2696 | ** The comparison function must be logically the same for every cursor |
| 2697 | ** on a particular table. Changing the comparison function will result |
| 2698 | ** in incorrect operations. If the comparison function is NULL, a |
| 2699 | ** default comparison function is used. The comparison function is |
| 2700 | ** always ignored for INTKEY tables. |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 2701 | */ |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 2702 | static int btreeCursor( |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2703 | Btree *p, /* The btree */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2704 | int iTable, /* Root page of table to open */ |
| 2705 | int wrFlag, /* 1 to write. 0 read-only */ |
| 2706 | int (*xCmp)(void*,int,const void*,int,const void*), /* Key Comparison func */ |
| 2707 | void *pArg, /* First arg to xCompare() */ |
| 2708 | BtCursor **ppCur /* Write new cursor here */ |
| 2709 | ){ |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 2710 | int rc; |
drh | 8dcd7ca | 2004-08-08 19:43:29 +0000 | [diff] [blame] | 2711 | BtCursor *pCur; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2712 | BtShared *pBt = p->pBt; |
drh | ecdc753 | 2001-09-23 02:35:53 +0000 | [diff] [blame] | 2713 | |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 2714 | assert( sqlite3BtreeHoldsMutex(p) ); |
drh | 8dcd7ca | 2004-08-08 19:43:29 +0000 | [diff] [blame] | 2715 | *ppCur = 0; |
| 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 | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 2734 | pCur = sqlite3MallocZero( sizeof(*pCur) ); |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 2735 | if( pCur==0 ){ |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 2736 | rc = SQLITE_NOMEM; |
| 2737 | goto create_cursor_exception; |
| 2738 | } |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 2739 | pCur->pgnoRoot = (Pgno)iTable; |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 2740 | if( iTable==1 && sqlite3PagerPagecount(pBt->pPager)==0 ){ |
drh | 24cd67e | 2004-05-10 16:18:47 +0000 | [diff] [blame] | 2741 | rc = SQLITE_EMPTY; |
| 2742 | goto create_cursor_exception; |
| 2743 | } |
drh | de64713 | 2004-05-07 17:57:49 +0000 | [diff] [blame] | 2744 | rc = getAndInitPage(pBt, pCur->pgnoRoot, &pCur->pPage, 0); |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 2745 | if( rc!=SQLITE_OK ){ |
| 2746 | goto create_cursor_exception; |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 2747 | } |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2748 | |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2749 | /* Now that no other errors can occur, finish filling in the BtCursor |
| 2750 | ** variables, link the cursor into the BtShared list and set *ppCur (the |
| 2751 | ** output argument to this function). |
| 2752 | */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2753 | pCur->xCompare = xCmp ? xCmp : dfltCompare; |
| 2754 | pCur->pArg = pArg; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2755 | pCur->pBtree = p; |
drh | d0679ed | 2007-08-28 22:24:34 +0000 | [diff] [blame] | 2756 | pCur->pBt = pBt; |
drh | ecdc753 | 2001-09-23 02:35:53 +0000 | [diff] [blame] | 2757 | pCur->wrFlag = wrFlag; |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 2758 | pCur->pNext = pBt->pCursor; |
| 2759 | if( pCur->pNext ){ |
| 2760 | pCur->pNext->pPrev = pCur; |
| 2761 | } |
| 2762 | pBt->pCursor = pCur; |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 2763 | pCur->eState = CURSOR_INVALID; |
drh | 2af926b | 2001-05-15 00:39:25 +0000 | [diff] [blame] | 2764 | *ppCur = pCur; |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 2765 | |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 2766 | return SQLITE_OK; |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 2767 | |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 2768 | create_cursor_exception: |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 2769 | if( pCur ){ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2770 | releasePage(pCur->pPage); |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 2771 | sqlite3_free(pCur); |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 2772 | } |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2773 | unlockBtreeIfUnused(pBt); |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 2774 | return rc; |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 2775 | } |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 2776 | int sqlite3BtreeCursor( |
| 2777 | Btree *p, /* The btree */ |
| 2778 | int iTable, /* Root page of table to open */ |
| 2779 | int wrFlag, /* 1 to write. 0 read-only */ |
| 2780 | int (*xCmp)(void*,int,const void*,int,const void*), /* Key Comparison func */ |
| 2781 | void *pArg, /* First arg to xCompare() */ |
| 2782 | BtCursor **ppCur /* Write new cursor here */ |
| 2783 | ){ |
| 2784 | int rc; |
| 2785 | sqlite3BtreeEnter(p); |
drh | e5fe690 | 2007-12-07 18:55:28 +0000 | [diff] [blame] | 2786 | p->pBt->db = p->db; |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 2787 | rc = btreeCursor(p, iTable, wrFlag, xCmp, pArg, ppCur); |
| 2788 | sqlite3BtreeLeave(p); |
| 2789 | return rc; |
| 2790 | } |
| 2791 | |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 2792 | |
| 2793 | /* |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2794 | ** Close a cursor. The read lock on the database file is released |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 2795 | ** when the last cursor is closed. |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 2796 | */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2797 | int sqlite3BtreeCloseCursor(BtCursor *pCur){ |
drh | d0679ed | 2007-08-28 22:24:34 +0000 | [diff] [blame] | 2798 | BtShared *pBt = pCur->pBt; |
drh | ff0587c | 2007-08-29 17:43:19 +0000 | [diff] [blame] | 2799 | Btree *pBtree = pCur->pBtree; |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 2800 | |
drh | ff0587c | 2007-08-29 17:43:19 +0000 | [diff] [blame] | 2801 | sqlite3BtreeEnter(pBtree); |
drh | e5fe690 | 2007-12-07 18:55:28 +0000 | [diff] [blame] | 2802 | pBt->db = pBtree->db; |
drh | bf700f3 | 2007-03-31 02:36:44 +0000 | [diff] [blame] | 2803 | clearCursorPosition(pCur); |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 2804 | if( pCur->pPrev ){ |
| 2805 | pCur->pPrev->pNext = pCur->pNext; |
| 2806 | }else{ |
| 2807 | pBt->pCursor = pCur->pNext; |
| 2808 | } |
| 2809 | if( pCur->pNext ){ |
| 2810 | pCur->pNext->pPrev = pCur->pPrev; |
| 2811 | } |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2812 | releasePage(pCur->pPage); |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 2813 | unlockBtreeIfUnused(pBt); |
danielk1977 | 92d4d7a | 2007-05-04 12:05:56 +0000 | [diff] [blame] | 2814 | invalidateOverflowCache(pCur); |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 2815 | sqlite3_free(pCur); |
drh | ff0587c | 2007-08-29 17:43:19 +0000 | [diff] [blame] | 2816 | sqlite3BtreeLeave(pBtree); |
drh | 8c42ca9 | 2001-06-22 19:15:00 +0000 | [diff] [blame] | 2817 | return SQLITE_OK; |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 2818 | } |
| 2819 | |
drh | 7e3b0a0 | 2001-04-28 16:52:40 +0000 | [diff] [blame] | 2820 | /* |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 2821 | ** Make a temporary cursor by filling in the fields of pTempCur. |
| 2822 | ** The temporary cursor is not on the cursor list for the Btree. |
| 2823 | */ |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 2824 | void sqlite3BtreeGetTempCursor(BtCursor *pCur, BtCursor *pTempCur){ |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 2825 | assert( cursorHoldsMutex(pCur) ); |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 2826 | memcpy(pTempCur, pCur, sizeof(*pCur)); |
| 2827 | pTempCur->pNext = 0; |
| 2828 | pTempCur->pPrev = 0; |
drh | ecdc753 | 2001-09-23 02:35:53 +0000 | [diff] [blame] | 2829 | if( pTempCur->pPage ){ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 2830 | sqlite3PagerRef(pTempCur->pPage->pDbPage); |
drh | ecdc753 | 2001-09-23 02:35:53 +0000 | [diff] [blame] | 2831 | } |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 2832 | } |
| 2833 | |
| 2834 | /* |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 2835 | ** Delete a temporary cursor such as was made by the CreateTemporaryCursor() |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 2836 | ** function above. |
| 2837 | */ |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 2838 | void sqlite3BtreeReleaseTempCursor(BtCursor *pCur){ |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 2839 | assert( cursorHoldsMutex(pCur) ); |
drh | ecdc753 | 2001-09-23 02:35:53 +0000 | [diff] [blame] | 2840 | if( pCur->pPage ){ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 2841 | sqlite3PagerUnref(pCur->pPage->pDbPage); |
drh | ecdc753 | 2001-09-23 02:35:53 +0000 | [diff] [blame] | 2842 | } |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 2843 | } |
| 2844 | |
| 2845 | /* |
drh | 8605761 | 2007-06-26 01:04:48 +0000 | [diff] [blame] | 2846 | ** Make sure the BtCursor* given in the argument has a valid |
| 2847 | ** BtCursor.info structure. If it is not already valid, call |
danielk1977 | 1cc5ed8 | 2007-05-16 17:28:43 +0000 | [diff] [blame] | 2848 | ** sqlite3BtreeParseCell() to fill it in. |
drh | ab01f61 | 2004-05-22 02:55:23 +0000 | [diff] [blame] | 2849 | ** |
| 2850 | ** BtCursor.info is a cache of the information in the current cell. |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 2851 | ** Using this cache reduces the number of calls to sqlite3BtreeParseCell(). |
drh | 8605761 | 2007-06-26 01:04:48 +0000 | [diff] [blame] | 2852 | ** |
| 2853 | ** 2007-06-25: There is a bug in some versions of MSVC that cause the |
| 2854 | ** compiler to crash when getCellInfo() is implemented as a macro. |
| 2855 | ** But there is a measureable speed advantage to using the macro on gcc |
| 2856 | ** (when less compiler optimizations like -Os or -O0 are used and the |
| 2857 | ** compiler is not doing agressive inlining.) So we use a real function |
| 2858 | ** for MSVC and a macro for everything else. Ticket #2457. |
drh | 9188b38 | 2004-05-14 21:12:22 +0000 | [diff] [blame] | 2859 | */ |
drh | 9188b38 | 2004-05-14 21:12:22 +0000 | [diff] [blame] | 2860 | #ifndef NDEBUG |
danielk1977 | 1cc5ed8 | 2007-05-16 17:28:43 +0000 | [diff] [blame] | 2861 | static void assertCellInfo(BtCursor *pCur){ |
drh | 9188b38 | 2004-05-14 21:12:22 +0000 | [diff] [blame] | 2862 | CellInfo info; |
drh | 51c6d96 | 2004-06-06 00:42:25 +0000 | [diff] [blame] | 2863 | memset(&info, 0, sizeof(info)); |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 2864 | sqlite3BtreeParseCell(pCur->pPage, pCur->idx, &info); |
drh | 9188b38 | 2004-05-14 21:12:22 +0000 | [diff] [blame] | 2865 | assert( memcmp(&info, &pCur->info, sizeof(info))==0 ); |
drh | 9188b38 | 2004-05-14 21:12:22 +0000 | [diff] [blame] | 2866 | } |
danielk1977 | 1cc5ed8 | 2007-05-16 17:28:43 +0000 | [diff] [blame] | 2867 | #else |
| 2868 | #define assertCellInfo(x) |
| 2869 | #endif |
drh | 8605761 | 2007-06-26 01:04:48 +0000 | [diff] [blame] | 2870 | #ifdef _MSC_VER |
| 2871 | /* Use a real function in MSVC to work around bugs in that compiler. */ |
| 2872 | static void getCellInfo(BtCursor *pCur){ |
| 2873 | if( pCur->info.nSize==0 ){ |
| 2874 | sqlite3BtreeParseCell(pCur->pPage, pCur->idx, &pCur->info); |
| 2875 | }else{ |
| 2876 | assertCellInfo(pCur); |
| 2877 | } |
| 2878 | } |
| 2879 | #else /* if not _MSC_VER */ |
| 2880 | /* Use a macro in all other compilers so that the function is inlined */ |
| 2881 | #define getCellInfo(pCur) \ |
| 2882 | if( pCur->info.nSize==0 ){ \ |
danielk1977 | 1cc5ed8 | 2007-05-16 17:28:43 +0000 | [diff] [blame] | 2883 | sqlite3BtreeParseCell(pCur->pPage, pCur->idx, &pCur->info); \ |
drh | 8605761 | 2007-06-26 01:04:48 +0000 | [diff] [blame] | 2884 | }else{ \ |
| 2885 | assertCellInfo(pCur); \ |
| 2886 | } |
| 2887 | #endif /* _MSC_VER */ |
drh | 9188b38 | 2004-05-14 21:12:22 +0000 | [diff] [blame] | 2888 | |
| 2889 | /* |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2890 | ** Set *pSize to the size of the buffer needed to hold the value of |
| 2891 | ** the key for the current entry. If the cursor is not pointing |
| 2892 | ** to a valid entry, *pSize is set to 0. |
| 2893 | ** |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 2894 | ** For a table with the INTKEY flag set, this routine returns the key |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 2895 | ** itself, not the number of bytes in the key. |
drh | 7e3b0a0 | 2001-04-28 16:52:40 +0000 | [diff] [blame] | 2896 | */ |
drh | 4a1c380 | 2004-05-12 15:15:47 +0000 | [diff] [blame] | 2897 | int sqlite3BtreeKeySize(BtCursor *pCur, i64 *pSize){ |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 2898 | int rc; |
| 2899 | |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 2900 | assert( cursorHoldsMutex(pCur) ); |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 2901 | rc = restoreOrClearCursorPosition(pCur); |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 2902 | if( rc==SQLITE_OK ){ |
| 2903 | assert( pCur->eState==CURSOR_INVALID || pCur->eState==CURSOR_VALID ); |
| 2904 | if( pCur->eState==CURSOR_INVALID ){ |
| 2905 | *pSize = 0; |
| 2906 | }else{ |
drh | 8605761 | 2007-06-26 01:04:48 +0000 | [diff] [blame] | 2907 | getCellInfo(pCur); |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 2908 | *pSize = pCur->info.nKey; |
| 2909 | } |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 2910 | } |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 2911 | return rc; |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 2912 | } |
drh | 2af926b | 2001-05-15 00:39:25 +0000 | [diff] [blame] | 2913 | |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 2914 | /* |
drh | 0e1c19e | 2004-05-11 00:58:56 +0000 | [diff] [blame] | 2915 | ** Set *pSize to the number of bytes of data in the entry the |
| 2916 | ** cursor currently points to. Always return SQLITE_OK. |
| 2917 | ** Failure is not possible. If the cursor is not currently |
| 2918 | ** pointing to an entry (which can happen, for example, if |
| 2919 | ** the database is empty) then *pSize is set to 0. |
| 2920 | */ |
| 2921 | int sqlite3BtreeDataSize(BtCursor *pCur, u32 *pSize){ |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 2922 | int rc; |
| 2923 | |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 2924 | assert( cursorHoldsMutex(pCur) ); |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 2925 | rc = restoreOrClearCursorPosition(pCur); |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 2926 | if( rc==SQLITE_OK ){ |
| 2927 | assert( pCur->eState==CURSOR_INVALID || pCur->eState==CURSOR_VALID ); |
| 2928 | if( pCur->eState==CURSOR_INVALID ){ |
| 2929 | /* Not pointing at a valid entry - set *pSize to 0. */ |
| 2930 | *pSize = 0; |
| 2931 | }else{ |
drh | 8605761 | 2007-06-26 01:04:48 +0000 | [diff] [blame] | 2932 | getCellInfo(pCur); |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 2933 | *pSize = pCur->info.nData; |
| 2934 | } |
drh | 0e1c19e | 2004-05-11 00:58:56 +0000 | [diff] [blame] | 2935 | } |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 2936 | return rc; |
drh | 0e1c19e | 2004-05-11 00:58:56 +0000 | [diff] [blame] | 2937 | } |
| 2938 | |
| 2939 | /* |
danielk1977 | d0441796 | 2007-05-02 13:16:30 +0000 | [diff] [blame] | 2940 | ** Given the page number of an overflow page in the database (parameter |
| 2941 | ** ovfl), this function finds the page number of the next page in the |
| 2942 | ** linked list of overflow pages. If possible, it uses the auto-vacuum |
| 2943 | ** pointer-map data instead of reading the content of page ovfl to do so. |
| 2944 | ** |
| 2945 | ** If an error occurs an SQLite error code is returned. Otherwise: |
| 2946 | ** |
| 2947 | ** Unless pPgnoNext is NULL, the page number of the next overflow |
| 2948 | ** page in the linked list is written to *pPgnoNext. If page ovfl |
drh | 85b623f | 2007-12-13 21:54:09 +0000 | [diff] [blame] | 2949 | ** is the last page in its linked list, *pPgnoNext is set to zero. |
danielk1977 | d0441796 | 2007-05-02 13:16:30 +0000 | [diff] [blame] | 2950 | ** |
| 2951 | ** If ppPage is not NULL, *ppPage is set to the MemPage* handle |
| 2952 | ** for page ovfl. The underlying pager page may have been requested |
| 2953 | ** with the noContent flag set, so the page data accessable via |
| 2954 | ** this handle may not be trusted. |
| 2955 | */ |
| 2956 | static int getOverflowPage( |
| 2957 | BtShared *pBt, |
| 2958 | Pgno ovfl, /* Overflow page */ |
| 2959 | MemPage **ppPage, /* OUT: MemPage handle */ |
| 2960 | Pgno *pPgnoNext /* OUT: Next overflow page number */ |
| 2961 | ){ |
| 2962 | Pgno next = 0; |
| 2963 | int rc; |
| 2964 | |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 2965 | assert( sqlite3_mutex_held(pBt->mutex) ); |
danielk1977 | d0441796 | 2007-05-02 13:16:30 +0000 | [diff] [blame] | 2966 | /* One of these must not be NULL. Otherwise, why call this function? */ |
| 2967 | assert(ppPage || pPgnoNext); |
| 2968 | |
| 2969 | /* If pPgnoNext is NULL, then this function is being called to obtain |
| 2970 | ** a MemPage* reference only. No page-data is required in this case. |
| 2971 | */ |
| 2972 | if( !pPgnoNext ){ |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 2973 | return sqlite3BtreeGetPage(pBt, ovfl, ppPage, 1); |
danielk1977 | d0441796 | 2007-05-02 13:16:30 +0000 | [diff] [blame] | 2974 | } |
| 2975 | |
| 2976 | #ifndef SQLITE_OMIT_AUTOVACUUM |
| 2977 | /* Try to find the next page in the overflow list using the |
| 2978 | ** autovacuum pointer-map pages. Guess that the next page in |
| 2979 | ** the overflow list is page number (ovfl+1). If that guess turns |
| 2980 | ** out to be wrong, fall back to loading the data of page |
| 2981 | ** number ovfl to determine the next page number. |
| 2982 | */ |
| 2983 | if( pBt->autoVacuum ){ |
| 2984 | Pgno pgno; |
| 2985 | Pgno iGuess = ovfl+1; |
| 2986 | u8 eType; |
| 2987 | |
| 2988 | while( PTRMAP_ISPAGE(pBt, iGuess) || iGuess==PENDING_BYTE_PAGE(pBt) ){ |
| 2989 | iGuess++; |
| 2990 | } |
| 2991 | |
danielk1977 | 20713f3 | 2007-05-03 11:43:33 +0000 | [diff] [blame] | 2992 | if( iGuess<=sqlite3PagerPagecount(pBt->pPager) ){ |
danielk1977 | d0441796 | 2007-05-02 13:16:30 +0000 | [diff] [blame] | 2993 | rc = ptrmapGet(pBt, iGuess, &eType, &pgno); |
| 2994 | if( rc!=SQLITE_OK ){ |
| 2995 | return rc; |
| 2996 | } |
| 2997 | if( eType==PTRMAP_OVERFLOW2 && pgno==ovfl ){ |
| 2998 | next = iGuess; |
| 2999 | } |
| 3000 | } |
| 3001 | } |
| 3002 | #endif |
| 3003 | |
| 3004 | if( next==0 || ppPage ){ |
| 3005 | MemPage *pPage = 0; |
| 3006 | |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 3007 | rc = sqlite3BtreeGetPage(pBt, ovfl, &pPage, next!=0); |
danielk1977 | d0441796 | 2007-05-02 13:16:30 +0000 | [diff] [blame] | 3008 | assert(rc==SQLITE_OK || pPage==0); |
| 3009 | if( next==0 && rc==SQLITE_OK ){ |
| 3010 | next = get4byte(pPage->aData); |
| 3011 | } |
| 3012 | |
| 3013 | if( ppPage ){ |
| 3014 | *ppPage = pPage; |
| 3015 | }else{ |
| 3016 | releasePage(pPage); |
| 3017 | } |
| 3018 | } |
| 3019 | *pPgnoNext = next; |
| 3020 | |
| 3021 | return rc; |
| 3022 | } |
| 3023 | |
danielk1977 | da10719 | 2007-05-04 08:32:13 +0000 | [diff] [blame] | 3024 | /* |
| 3025 | ** Copy data from a buffer to a page, or from a page to a buffer. |
| 3026 | ** |
| 3027 | ** pPayload is a pointer to data stored on database page pDbPage. |
| 3028 | ** If argument eOp is false, then nByte bytes of data are copied |
| 3029 | ** from pPayload to the buffer pointed at by pBuf. If eOp is true, |
| 3030 | ** then sqlite3PagerWrite() is called on pDbPage and nByte bytes |
| 3031 | ** of data are copied from the buffer pBuf to pPayload. |
| 3032 | ** |
| 3033 | ** SQLITE_OK is returned on success, otherwise an error code. |
| 3034 | */ |
| 3035 | static int copyPayload( |
| 3036 | void *pPayload, /* Pointer to page data */ |
| 3037 | void *pBuf, /* Pointer to buffer */ |
| 3038 | int nByte, /* Number of bytes to copy */ |
| 3039 | int eOp, /* 0 -> copy from page, 1 -> copy to page */ |
| 3040 | DbPage *pDbPage /* Page containing pPayload */ |
| 3041 | ){ |
| 3042 | if( eOp ){ |
| 3043 | /* Copy data from buffer to page (a write operation) */ |
| 3044 | int rc = sqlite3PagerWrite(pDbPage); |
| 3045 | if( rc!=SQLITE_OK ){ |
| 3046 | return rc; |
| 3047 | } |
| 3048 | memcpy(pPayload, pBuf, nByte); |
| 3049 | }else{ |
| 3050 | /* Copy data from page to buffer (a read operation) */ |
| 3051 | memcpy(pBuf, pPayload, nByte); |
| 3052 | } |
| 3053 | return SQLITE_OK; |
| 3054 | } |
danielk1977 | d0441796 | 2007-05-02 13:16:30 +0000 | [diff] [blame] | 3055 | |
| 3056 | /* |
danielk1977 | 9f8d640 | 2007-05-02 17:48:45 +0000 | [diff] [blame] | 3057 | ** This function is used to read or overwrite payload information |
| 3058 | ** for the entry that the pCur cursor is pointing to. If the eOp |
| 3059 | ** parameter is 0, this is a read operation (data copied into |
| 3060 | ** buffer pBuf). If it is non-zero, a write (data copied from |
| 3061 | ** buffer pBuf). |
| 3062 | ** |
| 3063 | ** A total of "amt" bytes are read or written beginning at "offset". |
| 3064 | ** Data is read to or from the buffer pBuf. |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 3065 | ** |
| 3066 | ** This routine does not make a distinction between key and data. |
danielk1977 | 9f8d640 | 2007-05-02 17:48:45 +0000 | [diff] [blame] | 3067 | ** It just reads or writes bytes from the payload area. Data might |
| 3068 | ** appear on the main page or be scattered out on multiple overflow |
| 3069 | ** pages. |
danielk1977 | da10719 | 2007-05-04 08:32:13 +0000 | [diff] [blame] | 3070 | ** |
danielk1977 | dcbb5d3 | 2007-05-04 18:36:44 +0000 | [diff] [blame] | 3071 | ** If the BtCursor.isIncrblobHandle flag is set, and the current |
danielk1977 | da10719 | 2007-05-04 08:32:13 +0000 | [diff] [blame] | 3072 | ** cursor entry uses one or more overflow pages, this function |
| 3073 | ** allocates space for and lazily popluates the overflow page-list |
| 3074 | ** cache array (BtCursor.aOverflow). Subsequent calls use this |
| 3075 | ** cache to make seeking to the supplied offset more efficient. |
| 3076 | ** |
| 3077 | ** Once an overflow page-list cache has been allocated, it may be |
| 3078 | ** invalidated if some other cursor writes to the same table, or if |
| 3079 | ** the cursor is moved to a different row. Additionally, in auto-vacuum |
| 3080 | ** mode, the following events may invalidate an overflow page-list cache. |
| 3081 | ** |
| 3082 | ** * An incremental vacuum, |
| 3083 | ** * A commit in auto_vacuum="full" mode, |
| 3084 | ** * Creating a table (may require moving an overflow page). |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 3085 | */ |
danielk1977 | 9f8d640 | 2007-05-02 17:48:45 +0000 | [diff] [blame] | 3086 | static int accessPayload( |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3087 | BtCursor *pCur, /* Cursor pointing to entry to read from */ |
| 3088 | int offset, /* Begin reading this far into payload */ |
| 3089 | int amt, /* Read this many bytes */ |
| 3090 | unsigned char *pBuf, /* Write the bytes into this buffer */ |
danielk1977 | 9f8d640 | 2007-05-02 17:48:45 +0000 | [diff] [blame] | 3091 | int skipKey, /* offset begins at data if this is true */ |
| 3092 | int eOp /* zero to read. non-zero to write. */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3093 | ){ |
| 3094 | unsigned char *aPayload; |
danielk1977 | da10719 | 2007-05-04 08:32:13 +0000 | [diff] [blame] | 3095 | int rc = SQLITE_OK; |
drh | fa1a98a | 2004-05-14 19:08:17 +0000 | [diff] [blame] | 3096 | u32 nKey; |
danielk1977 | 2dec970 | 2007-05-02 16:48:37 +0000 | [diff] [blame] | 3097 | int iIdx = 0; |
drh | d0679ed | 2007-08-28 22:24:34 +0000 | [diff] [blame] | 3098 | MemPage *pPage = pCur->pPage; /* Btree page of current cursor entry */ |
drh | 51f015e | 2007-10-16 19:45:29 +0000 | [diff] [blame] | 3099 | BtShared *pBt; /* Btree this cursor belongs to */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3100 | |
danielk1977 | da10719 | 2007-05-04 08:32:13 +0000 | [diff] [blame] | 3101 | assert( pPage ); |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 3102 | assert( pCur->eState==CURSOR_VALID ); |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3103 | assert( pCur->idx>=0 && pCur->idx<pPage->nCell ); |
danielk1977 | da10719 | 2007-05-04 08:32:13 +0000 | [diff] [blame] | 3104 | assert( offset>=0 ); |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 3105 | assert( cursorHoldsMutex(pCur) ); |
danielk1977 | da10719 | 2007-05-04 08:32:13 +0000 | [diff] [blame] | 3106 | |
drh | 8605761 | 2007-06-26 01:04:48 +0000 | [diff] [blame] | 3107 | getCellInfo(pCur); |
drh | 366fda6 | 2006-01-13 02:35:09 +0000 | [diff] [blame] | 3108 | aPayload = pCur->info.pCell + pCur->info.nHeader; |
danielk1977 | da10719 | 2007-05-04 08:32:13 +0000 | [diff] [blame] | 3109 | nKey = (pPage->intKey ? 0 : pCur->info.nKey); |
| 3110 | |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3111 | if( skipKey ){ |
drh | fa1a98a | 2004-05-14 19:08:17 +0000 | [diff] [blame] | 3112 | offset += nKey; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3113 | } |
drh | fa1a98a | 2004-05-14 19:08:17 +0000 | [diff] [blame] | 3114 | if( offset+amt > nKey+pCur->info.nData ){ |
danielk1977 | da10719 | 2007-05-04 08:32:13 +0000 | [diff] [blame] | 3115 | /* 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] | 3116 | return SQLITE_ERROR; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3117 | } |
danielk1977 | da10719 | 2007-05-04 08:32:13 +0000 | [diff] [blame] | 3118 | |
| 3119 | /* Check if data must be read/written to/from the btree page itself. */ |
drh | fa1a98a | 2004-05-14 19:08:17 +0000 | [diff] [blame] | 3120 | if( offset<pCur->info.nLocal ){ |
drh | 2af926b | 2001-05-15 00:39:25 +0000 | [diff] [blame] | 3121 | int a = amt; |
drh | fa1a98a | 2004-05-14 19:08:17 +0000 | [diff] [blame] | 3122 | if( a+offset>pCur->info.nLocal ){ |
| 3123 | a = pCur->info.nLocal - offset; |
drh | 2af926b | 2001-05-15 00:39:25 +0000 | [diff] [blame] | 3124 | } |
danielk1977 | da10719 | 2007-05-04 08:32:13 +0000 | [diff] [blame] | 3125 | rc = copyPayload(&aPayload[offset], pBuf, a, eOp, pPage->pDbPage); |
drh | 2aa679f | 2001-06-25 02:11:07 +0000 | [diff] [blame] | 3126 | offset = 0; |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 3127 | pBuf += a; |
drh | 2af926b | 2001-05-15 00:39:25 +0000 | [diff] [blame] | 3128 | amt -= a; |
drh | dd79342 | 2001-06-28 01:54:48 +0000 | [diff] [blame] | 3129 | }else{ |
drh | fa1a98a | 2004-05-14 19:08:17 +0000 | [diff] [blame] | 3130 | offset -= pCur->info.nLocal; |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 3131 | } |
danielk1977 | da10719 | 2007-05-04 08:32:13 +0000 | [diff] [blame] | 3132 | |
drh | 51f015e | 2007-10-16 19:45:29 +0000 | [diff] [blame] | 3133 | pBt = pCur->pBt; |
danielk1977 | da10719 | 2007-05-04 08:32:13 +0000 | [diff] [blame] | 3134 | if( rc==SQLITE_OK && amt>0 ){ |
| 3135 | const int ovflSize = pBt->usableSize - 4; /* Bytes content per ovfl page */ |
| 3136 | Pgno nextPage; |
| 3137 | |
drh | fa1a98a | 2004-05-14 19:08:17 +0000 | [diff] [blame] | 3138 | nextPage = get4byte(&aPayload[pCur->info.nLocal]); |
danielk1977 | da10719 | 2007-05-04 08:32:13 +0000 | [diff] [blame] | 3139 | |
danielk1977 | 2dec970 | 2007-05-02 16:48:37 +0000 | [diff] [blame] | 3140 | #ifndef SQLITE_OMIT_INCRBLOB |
danielk1977 | dcbb5d3 | 2007-05-04 18:36:44 +0000 | [diff] [blame] | 3141 | /* If the isIncrblobHandle flag is set and the BtCursor.aOverflow[] |
danielk1977 | da10719 | 2007-05-04 08:32:13 +0000 | [diff] [blame] | 3142 | ** has not been allocated, allocate it now. The array is sized at |
| 3143 | ** one entry for each overflow page in the overflow chain. The |
| 3144 | ** page number of the first overflow page is stored in aOverflow[0], |
| 3145 | ** etc. A value of 0 in the aOverflow[] array means "not yet known" |
| 3146 | ** (the cache is lazily populated). |
| 3147 | */ |
danielk1977 | dcbb5d3 | 2007-05-04 18:36:44 +0000 | [diff] [blame] | 3148 | if( pCur->isIncrblobHandle && !pCur->aOverflow ){ |
danielk1977 | 2dec970 | 2007-05-02 16:48:37 +0000 | [diff] [blame] | 3149 | int nOvfl = (pCur->info.nPayload-pCur->info.nLocal+ovflSize-1)/ovflSize; |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 3150 | pCur->aOverflow = (Pgno *)sqlite3MallocZero(sizeof(Pgno)*nOvfl); |
danielk1977 | 2dec970 | 2007-05-02 16:48:37 +0000 | [diff] [blame] | 3151 | if( nOvfl && !pCur->aOverflow ){ |
danielk1977 | da10719 | 2007-05-04 08:32:13 +0000 | [diff] [blame] | 3152 | rc = SQLITE_NOMEM; |
danielk1977 | 2dec970 | 2007-05-02 16:48:37 +0000 | [diff] [blame] | 3153 | } |
| 3154 | } |
danielk1977 | da10719 | 2007-05-04 08:32:13 +0000 | [diff] [blame] | 3155 | |
| 3156 | /* If the overflow page-list cache has been allocated and the |
| 3157 | ** entry for the first required overflow page is valid, skip |
| 3158 | ** directly to it. |
| 3159 | */ |
danielk1977 | 2dec970 | 2007-05-02 16:48:37 +0000 | [diff] [blame] | 3160 | if( pCur->aOverflow && pCur->aOverflow[offset/ovflSize] ){ |
| 3161 | iIdx = (offset/ovflSize); |
| 3162 | nextPage = pCur->aOverflow[iIdx]; |
| 3163 | offset = (offset%ovflSize); |
| 3164 | } |
| 3165 | #endif |
danielk1977 | da10719 | 2007-05-04 08:32:13 +0000 | [diff] [blame] | 3166 | |
| 3167 | for( ; rc==SQLITE_OK && amt>0 && nextPage; iIdx++){ |
| 3168 | |
| 3169 | #ifndef SQLITE_OMIT_INCRBLOB |
| 3170 | /* If required, populate the overflow page-list cache. */ |
| 3171 | if( pCur->aOverflow ){ |
| 3172 | assert(!pCur->aOverflow[iIdx] || pCur->aOverflow[iIdx]==nextPage); |
| 3173 | pCur->aOverflow[iIdx] = nextPage; |
| 3174 | } |
| 3175 | #endif |
| 3176 | |
danielk1977 | d0441796 | 2007-05-02 13:16:30 +0000 | [diff] [blame] | 3177 | if( offset>=ovflSize ){ |
| 3178 | /* The only reason to read this page is to obtain the page |
danielk1977 | da10719 | 2007-05-04 08:32:13 +0000 | [diff] [blame] | 3179 | ** number for the next page in the overflow chain. The page |
drh | fd131da | 2007-08-07 17:13:03 +0000 | [diff] [blame] | 3180 | ** data is not required. So first try to lookup the overflow |
| 3181 | ** page-list cache, if any, then fall back to the getOverflowPage() |
danielk1977 | da10719 | 2007-05-04 08:32:13 +0000 | [diff] [blame] | 3182 | ** function. |
danielk1977 | d0441796 | 2007-05-02 13:16:30 +0000 | [diff] [blame] | 3183 | */ |
danielk1977 | 2dec970 | 2007-05-02 16:48:37 +0000 | [diff] [blame] | 3184 | #ifndef SQLITE_OMIT_INCRBLOB |
danielk1977 | da10719 | 2007-05-04 08:32:13 +0000 | [diff] [blame] | 3185 | if( pCur->aOverflow && pCur->aOverflow[iIdx+1] ){ |
| 3186 | nextPage = pCur->aOverflow[iIdx+1]; |
| 3187 | } else |
danielk1977 | 2dec970 | 2007-05-02 16:48:37 +0000 | [diff] [blame] | 3188 | #endif |
danielk1977 | da10719 | 2007-05-04 08:32:13 +0000 | [diff] [blame] | 3189 | rc = getOverflowPage(pBt, nextPage, 0, &nextPage); |
danielk1977 | da10719 | 2007-05-04 08:32:13 +0000 | [diff] [blame] | 3190 | offset -= ovflSize; |
danielk1977 | d0441796 | 2007-05-02 13:16:30 +0000 | [diff] [blame] | 3191 | }else{ |
danielk1977 | 9f8d640 | 2007-05-02 17:48:45 +0000 | [diff] [blame] | 3192 | /* Need to read this page properly. It contains some of the |
| 3193 | ** range of data that is being read (eOp==0) or written (eOp!=0). |
danielk1977 | d0441796 | 2007-05-02 13:16:30 +0000 | [diff] [blame] | 3194 | */ |
| 3195 | DbPage *pDbPage; |
danielk1977 | cfe9a69 | 2004-06-16 12:00:29 +0000 | [diff] [blame] | 3196 | int a = amt; |
danielk1977 | d0441796 | 2007-05-02 13:16:30 +0000 | [diff] [blame] | 3197 | rc = sqlite3PagerGet(pBt->pPager, nextPage, &pDbPage); |
danielk1977 | da10719 | 2007-05-04 08:32:13 +0000 | [diff] [blame] | 3198 | if( rc==SQLITE_OK ){ |
| 3199 | aPayload = sqlite3PagerGetData(pDbPage); |
| 3200 | nextPage = get4byte(aPayload); |
| 3201 | if( a + offset > ovflSize ){ |
| 3202 | a = ovflSize - offset; |
danielk1977 | 9f8d640 | 2007-05-02 17:48:45 +0000 | [diff] [blame] | 3203 | } |
danielk1977 | da10719 | 2007-05-04 08:32:13 +0000 | [diff] [blame] | 3204 | rc = copyPayload(&aPayload[offset+4], pBuf, a, eOp, pDbPage); |
| 3205 | sqlite3PagerUnref(pDbPage); |
| 3206 | offset = 0; |
| 3207 | amt -= a; |
| 3208 | pBuf += a; |
danielk1977 | 9f8d640 | 2007-05-02 17:48:45 +0000 | [diff] [blame] | 3209 | } |
danielk1977 | cfe9a69 | 2004-06-16 12:00:29 +0000 | [diff] [blame] | 3210 | } |
drh | 2af926b | 2001-05-15 00:39:25 +0000 | [diff] [blame] | 3211 | } |
drh | 2af926b | 2001-05-15 00:39:25 +0000 | [diff] [blame] | 3212 | } |
danielk1977 | cfe9a69 | 2004-06-16 12:00:29 +0000 | [diff] [blame] | 3213 | |
danielk1977 | da10719 | 2007-05-04 08:32:13 +0000 | [diff] [blame] | 3214 | if( rc==SQLITE_OK && amt>0 ){ |
drh | 4928570 | 2005-09-17 15:20:26 +0000 | [diff] [blame] | 3215 | return SQLITE_CORRUPT_BKPT; |
drh | a7fcb05 | 2001-12-14 15:09:55 +0000 | [diff] [blame] | 3216 | } |
danielk1977 | da10719 | 2007-05-04 08:32:13 +0000 | [diff] [blame] | 3217 | return rc; |
drh | 2af926b | 2001-05-15 00:39:25 +0000 | [diff] [blame] | 3218 | } |
| 3219 | |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 3220 | /* |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3221 | ** Read part of the key associated with cursor pCur. Exactly |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 3222 | ** "amt" bytes will be transfered into pBuf[]. The transfer |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3223 | ** begins at "offset". |
drh | 8c1238a | 2003-01-02 14:43:55 +0000 | [diff] [blame] | 3224 | ** |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3225 | ** Return SQLITE_OK on success or an error code if anything goes |
| 3226 | ** wrong. An error is returned if "offset+amt" is larger than |
| 3227 | ** the available payload. |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 3228 | */ |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 3229 | int sqlite3BtreeKey(BtCursor *pCur, u32 offset, u32 amt, void *pBuf){ |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 3230 | int rc; |
| 3231 | |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 3232 | assert( cursorHoldsMutex(pCur) ); |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 3233 | rc = restoreOrClearCursorPosition(pCur); |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 3234 | if( rc==SQLITE_OK ){ |
| 3235 | assert( pCur->eState==CURSOR_VALID ); |
| 3236 | assert( pCur->pPage!=0 ); |
| 3237 | if( pCur->pPage->intKey ){ |
| 3238 | return SQLITE_CORRUPT_BKPT; |
| 3239 | } |
| 3240 | assert( pCur->pPage->intKey==0 ); |
| 3241 | assert( pCur->idx>=0 && pCur->idx<pCur->pPage->nCell ); |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 3242 | rc = accessPayload(pCur, offset, amt, (unsigned char*)pBuf, 0, 0); |
drh | 6575a22 | 2005-03-10 17:06:34 +0000 | [diff] [blame] | 3243 | } |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 3244 | return rc; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3245 | } |
| 3246 | |
| 3247 | /* |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3248 | ** Read part of the data associated with cursor pCur. Exactly |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 3249 | ** "amt" bytes will be transfered into pBuf[]. The transfer |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3250 | ** begins at "offset". |
| 3251 | ** |
| 3252 | ** Return SQLITE_OK on success or an error code if anything goes |
| 3253 | ** wrong. An error is returned if "offset+amt" is larger than |
| 3254 | ** the available payload. |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 3255 | */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3256 | int sqlite3BtreeData(BtCursor *pCur, u32 offset, u32 amt, void *pBuf){ |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 3257 | int rc; |
| 3258 | |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 3259 | assert( cursorHoldsMutex(pCur) ); |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 3260 | rc = restoreOrClearCursorPosition(pCur); |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 3261 | if( rc==SQLITE_OK ){ |
| 3262 | assert( pCur->eState==CURSOR_VALID ); |
| 3263 | assert( pCur->pPage!=0 ); |
| 3264 | assert( pCur->idx>=0 && pCur->idx<pCur->pPage->nCell ); |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 3265 | rc = accessPayload(pCur, offset, amt, pBuf, 1, 0); |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 3266 | } |
| 3267 | return rc; |
drh | 2af926b | 2001-05-15 00:39:25 +0000 | [diff] [blame] | 3268 | } |
| 3269 | |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 3270 | /* |
drh | 0e1c19e | 2004-05-11 00:58:56 +0000 | [diff] [blame] | 3271 | ** Return a pointer to payload information from the entry that the |
| 3272 | ** pCur cursor is pointing to. The pointer is to the beginning of |
| 3273 | ** 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] | 3274 | ** skipKey==1. The number of bytes of available key/data is written |
| 3275 | ** into *pAmt. If *pAmt==0, then the value returned will not be |
| 3276 | ** a valid pointer. |
drh | 0e1c19e | 2004-05-11 00:58:56 +0000 | [diff] [blame] | 3277 | ** |
| 3278 | ** This routine is an optimization. It is common for the entire key |
| 3279 | ** and data to fit on the local page and for there to be no overflow |
| 3280 | ** pages. When that is so, this routine can be used to access the |
| 3281 | ** 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] | 3282 | ** onto overflow pages, then accessPayload() must be used to reassembly |
drh | 0e1c19e | 2004-05-11 00:58:56 +0000 | [diff] [blame] | 3283 | ** the key/data and copy it into a preallocated buffer. |
| 3284 | ** |
| 3285 | ** The pointer returned by this routine looks directly into the cached |
| 3286 | ** page of the database. The data might change or move the next time |
| 3287 | ** any btree routine is called. |
| 3288 | */ |
| 3289 | static const unsigned char *fetchPayload( |
| 3290 | BtCursor *pCur, /* Cursor pointing to entry to read from */ |
drh | e51c44f | 2004-05-30 20:46:09 +0000 | [diff] [blame] | 3291 | int *pAmt, /* Write the number of available bytes here */ |
drh | 0e1c19e | 2004-05-11 00:58:56 +0000 | [diff] [blame] | 3292 | int skipKey /* read beginning at data if this is true */ |
| 3293 | ){ |
| 3294 | unsigned char *aPayload; |
| 3295 | MemPage *pPage; |
drh | fa1a98a | 2004-05-14 19:08:17 +0000 | [diff] [blame] | 3296 | u32 nKey; |
| 3297 | int nLocal; |
drh | 0e1c19e | 2004-05-11 00:58:56 +0000 | [diff] [blame] | 3298 | |
| 3299 | assert( pCur!=0 && pCur->pPage!=0 ); |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 3300 | assert( pCur->eState==CURSOR_VALID ); |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 3301 | assert( cursorHoldsMutex(pCur) ); |
drh | 0e1c19e | 2004-05-11 00:58:56 +0000 | [diff] [blame] | 3302 | pPage = pCur->pPage; |
drh | 0e1c19e | 2004-05-11 00:58:56 +0000 | [diff] [blame] | 3303 | assert( pCur->idx>=0 && pCur->idx<pPage->nCell ); |
drh | 8605761 | 2007-06-26 01:04:48 +0000 | [diff] [blame] | 3304 | getCellInfo(pCur); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 3305 | aPayload = pCur->info.pCell; |
drh | fa1a98a | 2004-05-14 19:08:17 +0000 | [diff] [blame] | 3306 | aPayload += pCur->info.nHeader; |
drh | 0e1c19e | 2004-05-11 00:58:56 +0000 | [diff] [blame] | 3307 | if( pPage->intKey ){ |
drh | fa1a98a | 2004-05-14 19:08:17 +0000 | [diff] [blame] | 3308 | nKey = 0; |
| 3309 | }else{ |
| 3310 | nKey = pCur->info.nKey; |
drh | 0e1c19e | 2004-05-11 00:58:56 +0000 | [diff] [blame] | 3311 | } |
drh | 0e1c19e | 2004-05-11 00:58:56 +0000 | [diff] [blame] | 3312 | if( skipKey ){ |
drh | fa1a98a | 2004-05-14 19:08:17 +0000 | [diff] [blame] | 3313 | aPayload += nKey; |
| 3314 | nLocal = pCur->info.nLocal - nKey; |
drh | 0e1c19e | 2004-05-11 00:58:56 +0000 | [diff] [blame] | 3315 | }else{ |
drh | fa1a98a | 2004-05-14 19:08:17 +0000 | [diff] [blame] | 3316 | nLocal = pCur->info.nLocal; |
drh | e51c44f | 2004-05-30 20:46:09 +0000 | [diff] [blame] | 3317 | if( nLocal>nKey ){ |
| 3318 | nLocal = nKey; |
| 3319 | } |
drh | 0e1c19e | 2004-05-11 00:58:56 +0000 | [diff] [blame] | 3320 | } |
drh | e51c44f | 2004-05-30 20:46:09 +0000 | [diff] [blame] | 3321 | *pAmt = nLocal; |
drh | 0e1c19e | 2004-05-11 00:58:56 +0000 | [diff] [blame] | 3322 | return aPayload; |
| 3323 | } |
| 3324 | |
| 3325 | |
| 3326 | /* |
drh | e51c44f | 2004-05-30 20:46:09 +0000 | [diff] [blame] | 3327 | ** For the entry that cursor pCur is point to, return as |
| 3328 | ** many bytes of the key or data as are available on the local |
| 3329 | ** b-tree page. Write the number of available bytes into *pAmt. |
drh | 0e1c19e | 2004-05-11 00:58:56 +0000 | [diff] [blame] | 3330 | ** |
| 3331 | ** The pointer returned is ephemeral. The key/data may move |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 3332 | ** or be destroyed on the next call to any Btree routine, |
| 3333 | ** including calls from other threads against the same cache. |
| 3334 | ** Hence, a mutex on the BtShared should be held prior to calling |
| 3335 | ** this routine. |
drh | 0e1c19e | 2004-05-11 00:58:56 +0000 | [diff] [blame] | 3336 | ** |
| 3337 | ** These routines is used to get quick access to key and data |
| 3338 | ** in the common case where no overflow pages are used. |
drh | 0e1c19e | 2004-05-11 00:58:56 +0000 | [diff] [blame] | 3339 | */ |
drh | e51c44f | 2004-05-30 20:46:09 +0000 | [diff] [blame] | 3340 | const void *sqlite3BtreeKeyFetch(BtCursor *pCur, int *pAmt){ |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 3341 | assert( cursorHoldsMutex(pCur) ); |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 3342 | if( pCur->eState==CURSOR_VALID ){ |
| 3343 | return (const void*)fetchPayload(pCur, pAmt, 0); |
| 3344 | } |
| 3345 | return 0; |
drh | 0e1c19e | 2004-05-11 00:58:56 +0000 | [diff] [blame] | 3346 | } |
drh | e51c44f | 2004-05-30 20:46:09 +0000 | [diff] [blame] | 3347 | const void *sqlite3BtreeDataFetch(BtCursor *pCur, int *pAmt){ |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 3348 | assert( cursorHoldsMutex(pCur) ); |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 3349 | if( pCur->eState==CURSOR_VALID ){ |
| 3350 | return (const void*)fetchPayload(pCur, pAmt, 1); |
| 3351 | } |
| 3352 | return 0; |
drh | 0e1c19e | 2004-05-11 00:58:56 +0000 | [diff] [blame] | 3353 | } |
| 3354 | |
| 3355 | |
| 3356 | /* |
drh | 8178a75 | 2003-01-05 21:41:40 +0000 | [diff] [blame] | 3357 | ** 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] | 3358 | ** page number of the child page to move to. |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 3359 | */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3360 | static int moveToChild(BtCursor *pCur, u32 newPgno){ |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 3361 | int rc; |
| 3362 | MemPage *pNewPage; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3363 | MemPage *pOldPage; |
drh | d0679ed | 2007-08-28 22:24:34 +0000 | [diff] [blame] | 3364 | BtShared *pBt = pCur->pBt; |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 3365 | |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 3366 | assert( cursorHoldsMutex(pCur) ); |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 3367 | assert( pCur->eState==CURSOR_VALID ); |
drh | de64713 | 2004-05-07 17:57:49 +0000 | [diff] [blame] | 3368 | rc = getAndInitPage(pBt, newPgno, &pNewPage, pCur->pPage); |
drh | 6019e16 | 2001-07-02 17:51:45 +0000 | [diff] [blame] | 3369 | if( rc ) return rc; |
drh | 428ae8c | 2003-01-04 16:48:09 +0000 | [diff] [blame] | 3370 | pNewPage->idxParent = pCur->idx; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3371 | pOldPage = pCur->pPage; |
| 3372 | pOldPage->idxShift = 0; |
| 3373 | releasePage(pOldPage); |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 3374 | pCur->pPage = pNewPage; |
| 3375 | pCur->idx = 0; |
drh | 271efa5 | 2004-05-30 19:19:05 +0000 | [diff] [blame] | 3376 | pCur->info.nSize = 0; |
drh | 4be295b | 2003-12-16 03:44:47 +0000 | [diff] [blame] | 3377 | if( pNewPage->nCell<1 ){ |
drh | 4928570 | 2005-09-17 15:20:26 +0000 | [diff] [blame] | 3378 | return SQLITE_CORRUPT_BKPT; |
drh | 4be295b | 2003-12-16 03:44:47 +0000 | [diff] [blame] | 3379 | } |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 3380 | return SQLITE_OK; |
| 3381 | } |
| 3382 | |
| 3383 | /* |
drh | 8856d6a | 2004-04-29 14:42:46 +0000 | [diff] [blame] | 3384 | ** Return true if the page is the virtual root of its table. |
| 3385 | ** |
| 3386 | ** The virtual root page is the root page for most tables. But |
| 3387 | ** for the table rooted on page 1, sometime the real root page |
| 3388 | ** is empty except for the right-pointer. In such cases the |
| 3389 | ** virtual root page is the page that the right-pointer of page |
| 3390 | ** 1 is pointing to. |
| 3391 | */ |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 3392 | int sqlite3BtreeIsRootPage(MemPage *pPage){ |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 3393 | MemPage *pParent; |
| 3394 | |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 3395 | assert( sqlite3_mutex_held(pPage->pBt->mutex) ); |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 3396 | pParent = pPage->pParent; |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 3397 | if( pParent==0 ) return 1; |
| 3398 | if( pParent->pgno>1 ) return 0; |
| 3399 | if( get2byte(&pParent->aData[pParent->hdrOffset+3])==0 ) return 1; |
drh | 8856d6a | 2004-04-29 14:42:46 +0000 | [diff] [blame] | 3400 | return 0; |
| 3401 | } |
| 3402 | |
| 3403 | /* |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 3404 | ** Move the cursor up to the parent page. |
| 3405 | ** |
| 3406 | ** pCur->idx is set to the cell index that contains the pointer |
| 3407 | ** to the page we are coming from. If we are coming from the |
| 3408 | ** right-most child page then pCur->idx is set to one more than |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 3409 | ** the largest cell index. |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 3410 | */ |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 3411 | void sqlite3BtreeMoveToParent(BtCursor *pCur){ |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 3412 | MemPage *pParent; |
drh | 8178a75 | 2003-01-05 21:41:40 +0000 | [diff] [blame] | 3413 | MemPage *pPage; |
drh | 428ae8c | 2003-01-04 16:48:09 +0000 | [diff] [blame] | 3414 | int idxParent; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3415 | |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 3416 | assert( cursorHoldsMutex(pCur) ); |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 3417 | assert( pCur->eState==CURSOR_VALID ); |
drh | 8178a75 | 2003-01-05 21:41:40 +0000 | [diff] [blame] | 3418 | pPage = pCur->pPage; |
| 3419 | assert( pPage!=0 ); |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 3420 | assert( !sqlite3BtreeIsRootPage(pPage) ); |
drh | 8178a75 | 2003-01-05 21:41:40 +0000 | [diff] [blame] | 3421 | pParent = pPage->pParent; |
| 3422 | assert( pParent!=0 ); |
| 3423 | idxParent = pPage->idxParent; |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 3424 | sqlite3PagerRef(pParent->pDbPage); |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3425 | releasePage(pPage); |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 3426 | pCur->pPage = pParent; |
drh | 271efa5 | 2004-05-30 19:19:05 +0000 | [diff] [blame] | 3427 | pCur->info.nSize = 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 | 8856d6a | 2004-04-29 14:42:46 +0000 | [diff] [blame] | 3466 | if( pRoot->nCell==0 && !pRoot->leaf ){ |
| 3467 | Pgno subpage; |
| 3468 | assert( pRoot->pgno==1 ); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 3469 | subpage = get4byte(&pRoot->aData[pRoot->hdrOffset+8]); |
drh | 8856d6a | 2004-04-29 14:42:46 +0000 | [diff] [blame] | 3470 | assert( subpage>0 ); |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 3471 | pCur->eState = CURSOR_VALID; |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 3472 | rc = moveToChild(pCur, subpage); |
drh | 8856d6a | 2004-04-29 14:42:46 +0000 | [diff] [blame] | 3473 | } |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 3474 | pCur->eState = ((pCur->pPage->nCell>0)?CURSOR_VALID:CURSOR_INVALID); |
drh | 8856d6a | 2004-04-29 14:42:46 +0000 | [diff] [blame] | 3475 | return rc; |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 3476 | } |
drh | 2af926b | 2001-05-15 00:39:25 +0000 | [diff] [blame] | 3477 | |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 3478 | /* |
| 3479 | ** Move the cursor down to the left-most leaf entry beneath the |
| 3480 | ** entry to which it is currently pointing. |
drh | 777e4c4 | 2006-01-13 04:31:58 +0000 | [diff] [blame] | 3481 | ** |
| 3482 | ** The left-most leaf is the one with the smallest key - the first |
| 3483 | ** in ascending order. |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 3484 | */ |
| 3485 | static int moveToLeftmost(BtCursor *pCur){ |
| 3486 | Pgno pgno; |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 3487 | int rc = SQLITE_OK; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3488 | MemPage *pPage; |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 3489 | |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 3490 | assert( cursorHoldsMutex(pCur) ); |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 3491 | assert( pCur->eState==CURSOR_VALID ); |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 3492 | while( rc==SQLITE_OK && !(pPage = pCur->pPage)->leaf ){ |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 3493 | assert( pCur->idx>=0 && pCur->idx<pPage->nCell ); |
danielk1977 | 1cc5ed8 | 2007-05-16 17:28:43 +0000 | [diff] [blame] | 3494 | pgno = get4byte(findCell(pPage, pCur->idx)); |
drh | 8178a75 | 2003-01-05 21:41:40 +0000 | [diff] [blame] | 3495 | rc = moveToChild(pCur, pgno); |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 3496 | } |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 3497 | return rc; |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 3498 | } |
| 3499 | |
drh | 2dcc9aa | 2002-12-04 13:40:25 +0000 | [diff] [blame] | 3500 | /* |
| 3501 | ** Move the cursor down to the right-most leaf entry beneath the |
| 3502 | ** page to which it is currently pointing. Notice the difference |
| 3503 | ** between moveToLeftmost() and moveToRightmost(). moveToLeftmost() |
| 3504 | ** finds the left-most entry beneath the *entry* whereas moveToRightmost() |
| 3505 | ** finds the right-most entry beneath the *page*. |
drh | 777e4c4 | 2006-01-13 04:31:58 +0000 | [diff] [blame] | 3506 | ** |
| 3507 | ** The right-most entry is the one with the largest key - the last |
| 3508 | ** key in ascending order. |
drh | 2dcc9aa | 2002-12-04 13:40:25 +0000 | [diff] [blame] | 3509 | */ |
| 3510 | static int moveToRightmost(BtCursor *pCur){ |
| 3511 | Pgno pgno; |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 3512 | int rc = SQLITE_OK; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3513 | MemPage *pPage; |
drh | 2dcc9aa | 2002-12-04 13:40:25 +0000 | [diff] [blame] | 3514 | |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 3515 | assert( cursorHoldsMutex(pCur) ); |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 3516 | assert( pCur->eState==CURSOR_VALID ); |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 3517 | while( rc==SQLITE_OK && !(pPage = pCur->pPage)->leaf ){ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 3518 | pgno = get4byte(&pPage->aData[pPage->hdrOffset+8]); |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3519 | pCur->idx = pPage->nCell; |
drh | 8178a75 | 2003-01-05 21:41:40 +0000 | [diff] [blame] | 3520 | rc = moveToChild(pCur, pgno); |
drh | 2dcc9aa | 2002-12-04 13:40:25 +0000 | [diff] [blame] | 3521 | } |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 3522 | if( rc==SQLITE_OK ){ |
| 3523 | pCur->idx = pPage->nCell - 1; |
| 3524 | pCur->info.nSize = 0; |
| 3525 | } |
drh | 2dcc9aa | 2002-12-04 13:40:25 +0000 | [diff] [blame] | 3526 | return SQLITE_OK; |
| 3527 | } |
| 3528 | |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3529 | /* Move the cursor to the first entry in the table. Return SQLITE_OK |
| 3530 | ** on success. Set *pRes to 0 if the cursor actually points to something |
drh | 77c679c | 2002-02-19 22:43:58 +0000 | [diff] [blame] | 3531 | ** or set *pRes to 1 if the table is empty. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3532 | */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3533 | int sqlite3BtreeFirst(BtCursor *pCur, int *pRes){ |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3534 | int rc; |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 3535 | |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 3536 | assert( cursorHoldsMutex(pCur) ); |
drh | e5fe690 | 2007-12-07 18:55:28 +0000 | [diff] [blame] | 3537 | assert( sqlite3_mutex_held(pCur->pBtree->db->mutex) ); |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3538 | rc = moveToRoot(pCur); |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 3539 | if( rc==SQLITE_OK ){ |
| 3540 | if( pCur->eState==CURSOR_INVALID ){ |
| 3541 | assert( pCur->pPage->nCell==0 ); |
| 3542 | *pRes = 1; |
| 3543 | rc = SQLITE_OK; |
| 3544 | }else{ |
| 3545 | assert( pCur->pPage->nCell>0 ); |
| 3546 | *pRes = 0; |
| 3547 | rc = moveToLeftmost(pCur); |
| 3548 | } |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3549 | } |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 3550 | return rc; |
| 3551 | } |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 3552 | |
drh | 9562b55 | 2002-02-19 15:00:07 +0000 | [diff] [blame] | 3553 | /* Move the cursor to the last entry in the table. Return SQLITE_OK |
| 3554 | ** on success. Set *pRes to 0 if the cursor actually points to something |
drh | 77c679c | 2002-02-19 22:43:58 +0000 | [diff] [blame] | 3555 | ** or set *pRes to 1 if the table is empty. |
drh | 9562b55 | 2002-02-19 15:00:07 +0000 | [diff] [blame] | 3556 | */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3557 | int sqlite3BtreeLast(BtCursor *pCur, int *pRes){ |
drh | 9562b55 | 2002-02-19 15:00:07 +0000 | [diff] [blame] | 3558 | int rc; |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 3559 | |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 3560 | assert( cursorHoldsMutex(pCur) ); |
drh | e5fe690 | 2007-12-07 18:55:28 +0000 | [diff] [blame] | 3561 | assert( sqlite3_mutex_held(pCur->pBtree->db->mutex) ); |
drh | 9562b55 | 2002-02-19 15:00:07 +0000 | [diff] [blame] | 3562 | rc = moveToRoot(pCur); |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 3563 | if( rc==SQLITE_OK ){ |
| 3564 | if( CURSOR_INVALID==pCur->eState ){ |
| 3565 | assert( pCur->pPage->nCell==0 ); |
| 3566 | *pRes = 1; |
| 3567 | }else{ |
| 3568 | assert( pCur->eState==CURSOR_VALID ); |
| 3569 | *pRes = 0; |
| 3570 | rc = moveToRightmost(pCur); |
| 3571 | } |
drh | 9562b55 | 2002-02-19 15:00:07 +0000 | [diff] [blame] | 3572 | } |
drh | 9562b55 | 2002-02-19 15:00:07 +0000 | [diff] [blame] | 3573 | return rc; |
| 3574 | } |
| 3575 | |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3576 | /* Move the cursor so that it points to an entry near pKey/nKey. |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 3577 | ** Return a success code. |
| 3578 | ** |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3579 | ** For INTKEY tables, only the nKey parameter is used. pKey is |
| 3580 | ** ignored. For other tables, nKey is the number of bytes of data |
drh | 0b2f316 | 2005-12-21 18:36:45 +0000 | [diff] [blame] | 3581 | ** in pKey. The comparison function specified when the cursor was |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3582 | ** created is used to compare keys. |
| 3583 | ** |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 3584 | ** If an exact match is not found, then the cursor is always |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 3585 | ** left pointing at a leaf page which would hold the entry if it |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 3586 | ** were present. The cursor might point to an entry that comes |
| 3587 | ** before or after the key. |
| 3588 | ** |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 3589 | ** The result of comparing the key with the entry to which the |
drh | ab01f61 | 2004-05-22 02:55:23 +0000 | [diff] [blame] | 3590 | ** cursor is written to *pRes if pRes!=NULL. The meaning of |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 3591 | ** this value is as follows: |
| 3592 | ** |
| 3593 | ** *pRes<0 The cursor is left pointing at an entry that |
drh | 1a844c3 | 2002-12-04 22:29:28 +0000 | [diff] [blame] | 3594 | ** is smaller than pKey or if the table is empty |
| 3595 | ** and the cursor is therefore left point to nothing. |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 3596 | ** |
| 3597 | ** *pRes==0 The cursor is left pointing at an entry that |
| 3598 | ** exactly matches pKey. |
| 3599 | ** |
| 3600 | ** *pRes>0 The cursor is left pointing at an entry that |
drh | 7c717f7 | 2001-06-24 20:39:41 +0000 | [diff] [blame] | 3601 | ** is larger than pKey. |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 3602 | ** |
drh | a059ad0 | 2001-04-17 20:09:11 +0000 | [diff] [blame] | 3603 | */ |
drh | e4d9081 | 2007-03-29 05:51:49 +0000 | [diff] [blame] | 3604 | int sqlite3BtreeMoveto( |
| 3605 | BtCursor *pCur, /* The cursor to be moved */ |
| 3606 | const void *pKey, /* The key content for indices. Not used by tables */ |
| 3607 | i64 nKey, /* Size of pKey. Or the key for tables */ |
| 3608 | int biasRight, /* If true, bias the search to the high end */ |
| 3609 | int *pRes /* Search result flag */ |
| 3610 | ){ |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 3611 | int rc; |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 3612 | |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 3613 | assert( cursorHoldsMutex(pCur) ); |
drh | e5fe690 | 2007-12-07 18:55:28 +0000 | [diff] [blame] | 3614 | assert( sqlite3_mutex_held(pCur->pBtree->db->mutex) ); |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 3615 | rc = moveToRoot(pCur); |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 3616 | if( rc ){ |
| 3617 | return rc; |
| 3618 | } |
drh | c39e000 | 2004-05-07 23:50:57 +0000 | [diff] [blame] | 3619 | assert( pCur->pPage ); |
| 3620 | assert( pCur->pPage->isInit ); |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 3621 | if( pCur->eState==CURSOR_INVALID ){ |
drh | f328bc8 | 2004-05-10 23:29:49 +0000 | [diff] [blame] | 3622 | *pRes = -1; |
drh | c39e000 | 2004-05-07 23:50:57 +0000 | [diff] [blame] | 3623 | assert( pCur->pPage->nCell==0 ); |
| 3624 | return SQLITE_OK; |
| 3625 | } |
drh | 1468438 | 2006-11-30 13:05:29 +0000 | [diff] [blame] | 3626 | for(;;){ |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 3627 | int lwr, upr; |
| 3628 | Pgno chldPg; |
| 3629 | MemPage *pPage = pCur->pPage; |
drh | 1a844c3 | 2002-12-04 22:29:28 +0000 | [diff] [blame] | 3630 | int c = -1; /* pRes return if table is empty must be -1 */ |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 3631 | lwr = 0; |
| 3632 | upr = pPage->nCell-1; |
drh | 4eec4c1 | 2005-01-21 00:22:37 +0000 | [diff] [blame] | 3633 | if( !pPage->intKey && pKey==0 ){ |
drh | 4928570 | 2005-09-17 15:20:26 +0000 | [diff] [blame] | 3634 | return SQLITE_CORRUPT_BKPT; |
drh | 4eec4c1 | 2005-01-21 00:22:37 +0000 | [diff] [blame] | 3635 | } |
drh | e4d9081 | 2007-03-29 05:51:49 +0000 | [diff] [blame] | 3636 | if( biasRight ){ |
| 3637 | pCur->idx = upr; |
| 3638 | }else{ |
| 3639 | pCur->idx = (upr+lwr)/2; |
| 3640 | } |
drh | f1d68b3 | 2007-03-29 04:43:26 +0000 | [diff] [blame] | 3641 | if( lwr<=upr ) for(;;){ |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 3642 | void *pCellKey; |
drh | 4a1c380 | 2004-05-12 15:15:47 +0000 | [diff] [blame] | 3643 | i64 nCellKey; |
drh | 366fda6 | 2006-01-13 02:35:09 +0000 | [diff] [blame] | 3644 | pCur->info.nSize = 0; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3645 | if( pPage->intKey ){ |
drh | 777e4c4 | 2006-01-13 04:31:58 +0000 | [diff] [blame] | 3646 | u8 *pCell; |
danielk1977 | 1cc5ed8 | 2007-05-16 17:28:43 +0000 | [diff] [blame] | 3647 | pCell = findCell(pPage, pCur->idx) + pPage->childPtrSize; |
drh | d172f86 | 2006-01-12 15:01:15 +0000 | [diff] [blame] | 3648 | if( pPage->hasData ){ |
danielk1977 | bab45c6 | 2006-01-16 15:14:27 +0000 | [diff] [blame] | 3649 | u32 dummy; |
drh | d172f86 | 2006-01-12 15:01:15 +0000 | [diff] [blame] | 3650 | pCell += getVarint32(pCell, &dummy); |
| 3651 | } |
danielk1977 | bab45c6 | 2006-01-16 15:14:27 +0000 | [diff] [blame] | 3652 | getVarint(pCell, (u64 *)&nCellKey); |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3653 | if( nCellKey<nKey ){ |
| 3654 | c = -1; |
| 3655 | }else if( nCellKey>nKey ){ |
| 3656 | c = +1; |
| 3657 | }else{ |
| 3658 | c = 0; |
| 3659 | } |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3660 | }else{ |
drh | e51c44f | 2004-05-30 20:46:09 +0000 | [diff] [blame] | 3661 | int available; |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 3662 | pCellKey = (void *)fetchPayload(pCur, &available, 0); |
drh | 366fda6 | 2006-01-13 02:35:09 +0000 | [diff] [blame] | 3663 | nCellKey = pCur->info.nKey; |
drh | e51c44f | 2004-05-30 20:46:09 +0000 | [diff] [blame] | 3664 | if( available>=nCellKey ){ |
| 3665 | c = pCur->xCompare(pCur->pArg, nCellKey, pCellKey, nKey, pKey); |
| 3666 | }else{ |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 3667 | pCellKey = sqlite3_malloc( nCellKey ); |
drh | e51c44f | 2004-05-30 20:46:09 +0000 | [diff] [blame] | 3668 | if( pCellKey==0 ) return SQLITE_NOMEM; |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 3669 | rc = sqlite3BtreeKey(pCur, 0, nCellKey, (void *)pCellKey); |
drh | e51c44f | 2004-05-30 20:46:09 +0000 | [diff] [blame] | 3670 | c = pCur->xCompare(pCur->pArg, nCellKey, pCellKey, nKey, pKey); |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 3671 | sqlite3_free(pCellKey); |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 3672 | if( rc ){ |
| 3673 | return rc; |
| 3674 | } |
drh | e51c44f | 2004-05-30 20:46:09 +0000 | [diff] [blame] | 3675 | } |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3676 | } |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 3677 | if( c==0 ){ |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 3678 | if( pPage->leafData && !pPage->leaf ){ |
drh | fc70e6f | 2004-05-12 21:11:27 +0000 | [diff] [blame] | 3679 | lwr = pCur->idx; |
| 3680 | upr = lwr - 1; |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 3681 | break; |
| 3682 | }else{ |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 3683 | if( pRes ) *pRes = 0; |
| 3684 | return SQLITE_OK; |
| 3685 | } |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 3686 | } |
| 3687 | if( c<0 ){ |
| 3688 | lwr = pCur->idx+1; |
| 3689 | }else{ |
| 3690 | upr = pCur->idx-1; |
| 3691 | } |
drh | f1d68b3 | 2007-03-29 04:43:26 +0000 | [diff] [blame] | 3692 | if( lwr>upr ){ |
| 3693 | break; |
| 3694 | } |
| 3695 | pCur->idx = (lwr+upr)/2; |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 3696 | } |
| 3697 | assert( lwr==upr+1 ); |
drh | 7aa128d | 2002-06-21 13:09:16 +0000 | [diff] [blame] | 3698 | assert( pPage->isInit ); |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3699 | if( pPage->leaf ){ |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 3700 | chldPg = 0; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3701 | }else if( lwr>=pPage->nCell ){ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 3702 | chldPg = get4byte(&pPage->aData[pPage->hdrOffset+8]); |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 3703 | }else{ |
danielk1977 | 1cc5ed8 | 2007-05-16 17:28:43 +0000 | [diff] [blame] | 3704 | chldPg = get4byte(findCell(pPage, lwr)); |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 3705 | } |
| 3706 | if( chldPg==0 ){ |
drh | c39e000 | 2004-05-07 23:50:57 +0000 | [diff] [blame] | 3707 | assert( pCur->idx>=0 && pCur->idx<pCur->pPage->nCell ); |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 3708 | if( pRes ) *pRes = c; |
| 3709 | return SQLITE_OK; |
| 3710 | } |
drh | 428ae8c | 2003-01-04 16:48:09 +0000 | [diff] [blame] | 3711 | pCur->idx = lwr; |
drh | 271efa5 | 2004-05-30 19:19:05 +0000 | [diff] [blame] | 3712 | pCur->info.nSize = 0; |
drh | 8178a75 | 2003-01-05 21:41:40 +0000 | [diff] [blame] | 3713 | rc = moveToChild(pCur, chldPg); |
drh | c39e000 | 2004-05-07 23:50:57 +0000 | [diff] [blame] | 3714 | if( rc ){ |
| 3715 | return rc; |
| 3716 | } |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 3717 | } |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 3718 | /* NOT REACHED */ |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 3719 | } |
| 3720 | |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 3721 | |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 3722 | /* |
drh | c39e000 | 2004-05-07 23:50:57 +0000 | [diff] [blame] | 3723 | ** Return TRUE if the cursor is not pointing at an entry of the table. |
| 3724 | ** |
| 3725 | ** TRUE will be returned after a call to sqlite3BtreeNext() moves |
| 3726 | ** past the last entry in the table or sqlite3BtreePrev() moves past |
| 3727 | ** the first entry. TRUE is also returned if the table is empty. |
| 3728 | */ |
| 3729 | int sqlite3BtreeEof(BtCursor *pCur){ |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 3730 | /* TODO: What if the cursor is in CURSOR_REQUIRESEEK but all table entries |
| 3731 | ** have been deleted? This API will need to change to return an error code |
| 3732 | ** as well as the boolean result value. |
| 3733 | */ |
| 3734 | return (CURSOR_VALID!=pCur->eState); |
drh | c39e000 | 2004-05-07 23:50:57 +0000 | [diff] [blame] | 3735 | } |
| 3736 | |
| 3737 | /* |
drh | b21c8cd | 2007-08-21 19:33:56 +0000 | [diff] [blame] | 3738 | ** Return the database connection handle for a cursor. |
| 3739 | */ |
| 3740 | sqlite3 *sqlite3BtreeCursorDb(const BtCursor *pCur){ |
drh | e5fe690 | 2007-12-07 18:55:28 +0000 | [diff] [blame] | 3741 | assert( sqlite3_mutex_held(pCur->pBtree->db->mutex) ); |
| 3742 | return pCur->pBtree->db; |
drh | b21c8cd | 2007-08-21 19:33:56 +0000 | [diff] [blame] | 3743 | } |
| 3744 | |
| 3745 | /* |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 3746 | ** Advance the cursor to the next entry in the database. If |
drh | 8c1238a | 2003-01-02 14:43:55 +0000 | [diff] [blame] | 3747 | ** successful then set *pRes=0. If the cursor |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 3748 | ** was already pointing to the last entry in the database before |
drh | 8c1238a | 2003-01-02 14:43:55 +0000 | [diff] [blame] | 3749 | ** this routine was called, then set *pRes=1. |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 3750 | */ |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 3751 | static int btreeNext(BtCursor *pCur, int *pRes){ |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 3752 | int rc; |
danielk1977 | 97a227c | 2006-01-20 16:32:04 +0000 | [diff] [blame] | 3753 | MemPage *pPage; |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 3754 | |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 3755 | assert( cursorHoldsMutex(pCur) ); |
drh | bf700f3 | 2007-03-31 02:36:44 +0000 | [diff] [blame] | 3756 | rc = restoreOrClearCursorPosition(pCur); |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 3757 | if( rc!=SQLITE_OK ){ |
| 3758 | return rc; |
| 3759 | } |
drh | 8c4d3a6 | 2007-04-06 01:03:32 +0000 | [diff] [blame] | 3760 | assert( pRes!=0 ); |
| 3761 | pPage = pCur->pPage; |
| 3762 | if( CURSOR_INVALID==pCur->eState ){ |
| 3763 | *pRes = 1; |
| 3764 | return SQLITE_OK; |
| 3765 | } |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 3766 | if( pCur->skip>0 ){ |
| 3767 | pCur->skip = 0; |
| 3768 | *pRes = 0; |
| 3769 | return SQLITE_OK; |
| 3770 | } |
| 3771 | pCur->skip = 0; |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 3772 | |
drh | 8178a75 | 2003-01-05 21:41:40 +0000 | [diff] [blame] | 3773 | assert( pPage->isInit ); |
drh | 8178a75 | 2003-01-05 21:41:40 +0000 | [diff] [blame] | 3774 | assert( pCur->idx<pPage->nCell ); |
danielk1977 | 6a43f9b | 2004-11-16 04:57:24 +0000 | [diff] [blame] | 3775 | |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 3776 | pCur->idx++; |
drh | 271efa5 | 2004-05-30 19:19:05 +0000 | [diff] [blame] | 3777 | pCur->info.nSize = 0; |
drh | 8178a75 | 2003-01-05 21:41:40 +0000 | [diff] [blame] | 3778 | if( pCur->idx>=pPage->nCell ){ |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 3779 | if( !pPage->leaf ){ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 3780 | rc = moveToChild(pCur, get4byte(&pPage->aData[pPage->hdrOffset+8])); |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 3781 | if( rc ) return rc; |
| 3782 | rc = moveToLeftmost(pCur); |
drh | 8c1238a | 2003-01-02 14:43:55 +0000 | [diff] [blame] | 3783 | *pRes = 0; |
| 3784 | return rc; |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 3785 | } |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 3786 | do{ |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 3787 | if( sqlite3BtreeIsRootPage(pPage) ){ |
drh | 8c1238a | 2003-01-02 14:43:55 +0000 | [diff] [blame] | 3788 | *pRes = 1; |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 3789 | pCur->eState = CURSOR_INVALID; |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 3790 | return SQLITE_OK; |
| 3791 | } |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 3792 | sqlite3BtreeMoveToParent(pCur); |
drh | 8178a75 | 2003-01-05 21:41:40 +0000 | [diff] [blame] | 3793 | pPage = pCur->pPage; |
| 3794 | }while( pCur->idx>=pPage->nCell ); |
drh | 8c1238a | 2003-01-02 14:43:55 +0000 | [diff] [blame] | 3795 | *pRes = 0; |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 3796 | if( pPage->leafData ){ |
| 3797 | rc = sqlite3BtreeNext(pCur, pRes); |
| 3798 | }else{ |
| 3799 | rc = SQLITE_OK; |
| 3800 | } |
| 3801 | return rc; |
drh | 8178a75 | 2003-01-05 21:41:40 +0000 | [diff] [blame] | 3802 | } |
| 3803 | *pRes = 0; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3804 | if( pPage->leaf ){ |
drh | 8178a75 | 2003-01-05 21:41:40 +0000 | [diff] [blame] | 3805 | return SQLITE_OK; |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 3806 | } |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 3807 | rc = moveToLeftmost(pCur); |
drh | 8c1238a | 2003-01-02 14:43:55 +0000 | [diff] [blame] | 3808 | return rc; |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 3809 | } |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 3810 | int sqlite3BtreeNext(BtCursor *pCur, int *pRes){ |
| 3811 | int rc; |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 3812 | assert( cursorHoldsMutex(pCur) ); |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 3813 | rc = btreeNext(pCur, pRes); |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 3814 | return rc; |
| 3815 | } |
| 3816 | |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 3817 | |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 3818 | /* |
drh | 2dcc9aa | 2002-12-04 13:40:25 +0000 | [diff] [blame] | 3819 | ** 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] | 3820 | ** successful then set *pRes=0. If the cursor |
drh | 2dcc9aa | 2002-12-04 13:40:25 +0000 | [diff] [blame] | 3821 | ** was already pointing to the first entry in the database before |
drh | 8178a75 | 2003-01-05 21:41:40 +0000 | [diff] [blame] | 3822 | ** this routine was called, then set *pRes=1. |
drh | 2dcc9aa | 2002-12-04 13:40:25 +0000 | [diff] [blame] | 3823 | */ |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 3824 | static int btreePrevious(BtCursor *pCur, int *pRes){ |
drh | 2dcc9aa | 2002-12-04 13:40:25 +0000 | [diff] [blame] | 3825 | int rc; |
| 3826 | Pgno pgno; |
drh | 8178a75 | 2003-01-05 21:41:40 +0000 | [diff] [blame] | 3827 | MemPage *pPage; |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 3828 | |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 3829 | assert( cursorHoldsMutex(pCur) ); |
drh | bf700f3 | 2007-03-31 02:36:44 +0000 | [diff] [blame] | 3830 | rc = restoreOrClearCursorPosition(pCur); |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 3831 | if( rc!=SQLITE_OK ){ |
| 3832 | return rc; |
| 3833 | } |
drh | 8c4d3a6 | 2007-04-06 01:03:32 +0000 | [diff] [blame] | 3834 | if( CURSOR_INVALID==pCur->eState ){ |
| 3835 | *pRes = 1; |
| 3836 | return SQLITE_OK; |
| 3837 | } |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 3838 | if( pCur->skip<0 ){ |
| 3839 | pCur->skip = 0; |
| 3840 | *pRes = 0; |
| 3841 | return SQLITE_OK; |
| 3842 | } |
| 3843 | pCur->skip = 0; |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 3844 | |
drh | 8178a75 | 2003-01-05 21:41:40 +0000 | [diff] [blame] | 3845 | pPage = pCur->pPage; |
drh | 8178a75 | 2003-01-05 21:41:40 +0000 | [diff] [blame] | 3846 | assert( pPage->isInit ); |
drh | 2dcc9aa | 2002-12-04 13:40:25 +0000 | [diff] [blame] | 3847 | assert( pCur->idx>=0 ); |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 3848 | if( !pPage->leaf ){ |
danielk1977 | 1cc5ed8 | 2007-05-16 17:28:43 +0000 | [diff] [blame] | 3849 | pgno = get4byte( findCell(pPage, pCur->idx) ); |
drh | 8178a75 | 2003-01-05 21:41:40 +0000 | [diff] [blame] | 3850 | rc = moveToChild(pCur, pgno); |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 3851 | if( rc ){ |
| 3852 | return rc; |
| 3853 | } |
drh | 2dcc9aa | 2002-12-04 13:40:25 +0000 | [diff] [blame] | 3854 | rc = moveToRightmost(pCur); |
| 3855 | }else{ |
| 3856 | while( pCur->idx==0 ){ |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 3857 | if( sqlite3BtreeIsRootPage(pPage) ){ |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 3858 | pCur->eState = CURSOR_INVALID; |
drh | c39e000 | 2004-05-07 23:50:57 +0000 | [diff] [blame] | 3859 | *pRes = 1; |
drh | 2dcc9aa | 2002-12-04 13:40:25 +0000 | [diff] [blame] | 3860 | return SQLITE_OK; |
| 3861 | } |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 3862 | sqlite3BtreeMoveToParent(pCur); |
drh | 8178a75 | 2003-01-05 21:41:40 +0000 | [diff] [blame] | 3863 | pPage = pCur->pPage; |
drh | 2dcc9aa | 2002-12-04 13:40:25 +0000 | [diff] [blame] | 3864 | } |
| 3865 | pCur->idx--; |
drh | 271efa5 | 2004-05-30 19:19:05 +0000 | [diff] [blame] | 3866 | pCur->info.nSize = 0; |
drh | 8237d45 | 2004-11-22 19:07:09 +0000 | [diff] [blame] | 3867 | if( pPage->leafData && !pPage->leaf ){ |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 3868 | rc = sqlite3BtreePrevious(pCur, pRes); |
| 3869 | }else{ |
| 3870 | rc = SQLITE_OK; |
| 3871 | } |
drh | 2dcc9aa | 2002-12-04 13:40:25 +0000 | [diff] [blame] | 3872 | } |
drh | 8178a75 | 2003-01-05 21:41:40 +0000 | [diff] [blame] | 3873 | *pRes = 0; |
drh | 2dcc9aa | 2002-12-04 13:40:25 +0000 | [diff] [blame] | 3874 | return rc; |
| 3875 | } |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 3876 | int sqlite3BtreePrevious(BtCursor *pCur, int *pRes){ |
| 3877 | int rc; |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 3878 | assert( cursorHoldsMutex(pCur) ); |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 3879 | rc = btreePrevious(pCur, pRes); |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 3880 | return rc; |
| 3881 | } |
drh | 2dcc9aa | 2002-12-04 13:40:25 +0000 | [diff] [blame] | 3882 | |
| 3883 | /* |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 3884 | ** Allocate a new page from the database file. |
| 3885 | ** |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 3886 | ** The new page is marked as dirty. (In other words, sqlite3PagerWrite() |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 3887 | ** has already been called on the new page.) The new page has also |
| 3888 | ** been referenced and the calling routine is responsible for calling |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 3889 | ** sqlite3PagerUnref() on the new page when it is done. |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 3890 | ** |
| 3891 | ** SQLITE_OK is returned on success. Any other return value indicates |
| 3892 | ** an error. *ppPage and *pPgno are undefined in the event of an error. |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 3893 | ** Do not invoke sqlite3PagerUnref() on *ppPage if an error is returned. |
drh | bea00b9 | 2002-07-08 10:59:50 +0000 | [diff] [blame] | 3894 | ** |
drh | 199e3cf | 2002-07-18 11:01:47 +0000 | [diff] [blame] | 3895 | ** If the "nearby" parameter is not 0, then a (feeble) effort is made to |
| 3896 | ** 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] | 3897 | ** attempt to keep related pages close to each other in the database file, |
| 3898 | ** which in turn can make database access faster. |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 3899 | ** |
| 3900 | ** If the "exact" parameter is not 0, and the page-number nearby exists |
| 3901 | ** anywhere on the free-list, then it is guarenteed to be returned. This |
| 3902 | ** is only used by auto-vacuum databases when allocating a new table. |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 3903 | */ |
drh | 4f0c587 | 2007-03-26 22:05:01 +0000 | [diff] [blame] | 3904 | static int allocateBtreePage( |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 3905 | BtShared *pBt, |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 3906 | MemPage **ppPage, |
| 3907 | Pgno *pPgno, |
| 3908 | Pgno nearby, |
| 3909 | u8 exact |
| 3910 | ){ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3911 | MemPage *pPage1; |
drh | 8c42ca9 | 2001-06-22 19:15:00 +0000 | [diff] [blame] | 3912 | int rc; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3913 | int n; /* Number of pages on the freelist */ |
| 3914 | int k; /* Number of leaves on the trunk of the freelist */ |
drh | d3627af | 2006-12-18 18:34:51 +0000 | [diff] [blame] | 3915 | MemPage *pTrunk = 0; |
| 3916 | MemPage *pPrevTrunk = 0; |
drh | 30e5875 | 2002-03-02 20:41:57 +0000 | [diff] [blame] | 3917 | |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 3918 | assert( sqlite3_mutex_held(pBt->mutex) ); |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3919 | pPage1 = pBt->pPage1; |
| 3920 | n = get4byte(&pPage1->aData[36]); |
| 3921 | if( n>0 ){ |
drh | 9102529 | 2004-05-03 19:49:32 +0000 | [diff] [blame] | 3922 | /* There are pages on the freelist. Reuse one of those pages. */ |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 3923 | Pgno iTrunk; |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 3924 | u8 searchList = 0; /* If the free-list must be searched for 'nearby' */ |
| 3925 | |
| 3926 | /* If the 'exact' parameter was true and a query of the pointer-map |
| 3927 | ** shows that the page 'nearby' is somewhere on the free-list, then |
| 3928 | ** the entire-list will be searched for that page. |
| 3929 | */ |
| 3930 | #ifndef SQLITE_OMIT_AUTOVACUUM |
danielk1977 | 4ef2449 | 2007-05-23 09:52:41 +0000 | [diff] [blame] | 3931 | if( exact && nearby<=sqlite3PagerPagecount(pBt->pPager) ){ |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 3932 | u8 eType; |
| 3933 | assert( nearby>0 ); |
| 3934 | assert( pBt->autoVacuum ); |
| 3935 | rc = ptrmapGet(pBt, nearby, &eType, 0); |
| 3936 | if( rc ) return rc; |
| 3937 | if( eType==PTRMAP_FREEPAGE ){ |
| 3938 | searchList = 1; |
| 3939 | } |
| 3940 | *pPgno = nearby; |
| 3941 | } |
| 3942 | #endif |
| 3943 | |
| 3944 | /* Decrement the free-list count by 1. Set iTrunk to the index of the |
| 3945 | ** first free-list trunk page. iPrevTrunk is initially 1. |
| 3946 | */ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 3947 | rc = sqlite3PagerWrite(pPage1->pDbPage); |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 3948 | if( rc ) return rc; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 3949 | put4byte(&pPage1->aData[36], n-1); |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 3950 | |
| 3951 | /* The code within this loop is run only once if the 'searchList' variable |
| 3952 | ** is not true. Otherwise, it runs once for each trunk-page on the |
| 3953 | ** free-list until the page 'nearby' is located. |
| 3954 | */ |
| 3955 | do { |
| 3956 | pPrevTrunk = pTrunk; |
| 3957 | if( pPrevTrunk ){ |
| 3958 | iTrunk = get4byte(&pPrevTrunk->aData[0]); |
drh | bea00b9 | 2002-07-08 10:59:50 +0000 | [diff] [blame] | 3959 | }else{ |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 3960 | iTrunk = get4byte(&pPage1->aData[32]); |
drh | bea00b9 | 2002-07-08 10:59:50 +0000 | [diff] [blame] | 3961 | } |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 3962 | rc = sqlite3BtreeGetPage(pBt, iTrunk, &pTrunk, 0); |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 3963 | if( rc ){ |
drh | d3627af | 2006-12-18 18:34:51 +0000 | [diff] [blame] | 3964 | pTrunk = 0; |
| 3965 | goto end_allocate_page; |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 3966 | } |
| 3967 | |
| 3968 | k = get4byte(&pTrunk->aData[4]); |
| 3969 | if( k==0 && !searchList ){ |
| 3970 | /* The trunk has no leaves and the list is not being searched. |
| 3971 | ** So extract the trunk page itself and use it as the newly |
| 3972 | ** allocated page */ |
| 3973 | assert( pPrevTrunk==0 ); |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 3974 | rc = sqlite3PagerWrite(pTrunk->pDbPage); |
drh | d3627af | 2006-12-18 18:34:51 +0000 | [diff] [blame] | 3975 | if( rc ){ |
| 3976 | goto end_allocate_page; |
| 3977 | } |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 3978 | *pPgno = iTrunk; |
| 3979 | memcpy(&pPage1->aData[32], &pTrunk->aData[0], 4); |
| 3980 | *ppPage = pTrunk; |
| 3981 | pTrunk = 0; |
| 3982 | TRACE(("ALLOCATE: %d trunk - %d free pages left\n", *pPgno, n-1)); |
| 3983 | }else if( k>pBt->usableSize/4 - 8 ){ |
| 3984 | /* Value of k is out of range. Database corruption */ |
drh | d3627af | 2006-12-18 18:34:51 +0000 | [diff] [blame] | 3985 | rc = SQLITE_CORRUPT_BKPT; |
| 3986 | goto end_allocate_page; |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 3987 | #ifndef SQLITE_OMIT_AUTOVACUUM |
| 3988 | }else if( searchList && nearby==iTrunk ){ |
| 3989 | /* The list is being searched and this trunk page is the page |
| 3990 | ** to allocate, regardless of whether it has leaves. |
| 3991 | */ |
| 3992 | assert( *pPgno==iTrunk ); |
| 3993 | *ppPage = pTrunk; |
| 3994 | searchList = 0; |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 3995 | rc = sqlite3PagerWrite(pTrunk->pDbPage); |
drh | d3627af | 2006-12-18 18:34:51 +0000 | [diff] [blame] | 3996 | if( rc ){ |
| 3997 | goto end_allocate_page; |
| 3998 | } |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 3999 | if( k==0 ){ |
| 4000 | if( !pPrevTrunk ){ |
| 4001 | memcpy(&pPage1->aData[32], &pTrunk->aData[0], 4); |
| 4002 | }else{ |
| 4003 | memcpy(&pPrevTrunk->aData[0], &pTrunk->aData[0], 4); |
| 4004 | } |
| 4005 | }else{ |
| 4006 | /* The trunk page is required by the caller but it contains |
| 4007 | ** pointers to free-list leaves. The first leaf becomes a trunk |
| 4008 | ** page in this case. |
| 4009 | */ |
| 4010 | MemPage *pNewTrunk; |
| 4011 | Pgno iNewTrunk = get4byte(&pTrunk->aData[8]); |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 4012 | rc = sqlite3BtreeGetPage(pBt, iNewTrunk, &pNewTrunk, 0); |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 4013 | if( rc!=SQLITE_OK ){ |
drh | d3627af | 2006-12-18 18:34:51 +0000 | [diff] [blame] | 4014 | goto end_allocate_page; |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 4015 | } |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 4016 | rc = sqlite3PagerWrite(pNewTrunk->pDbPage); |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 4017 | if( rc!=SQLITE_OK ){ |
| 4018 | releasePage(pNewTrunk); |
drh | d3627af | 2006-12-18 18:34:51 +0000 | [diff] [blame] | 4019 | goto end_allocate_page; |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 4020 | } |
| 4021 | memcpy(&pNewTrunk->aData[0], &pTrunk->aData[0], 4); |
| 4022 | put4byte(&pNewTrunk->aData[4], k-1); |
| 4023 | memcpy(&pNewTrunk->aData[8], &pTrunk->aData[12], (k-1)*4); |
drh | d3627af | 2006-12-18 18:34:51 +0000 | [diff] [blame] | 4024 | releasePage(pNewTrunk); |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 4025 | if( !pPrevTrunk ){ |
| 4026 | put4byte(&pPage1->aData[32], iNewTrunk); |
| 4027 | }else{ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 4028 | rc = sqlite3PagerWrite(pPrevTrunk->pDbPage); |
drh | d3627af | 2006-12-18 18:34:51 +0000 | [diff] [blame] | 4029 | if( rc ){ |
| 4030 | goto end_allocate_page; |
| 4031 | } |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 4032 | put4byte(&pPrevTrunk->aData[0], iNewTrunk); |
| 4033 | } |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 4034 | } |
| 4035 | pTrunk = 0; |
| 4036 | TRACE(("ALLOCATE: %d trunk - %d free pages left\n", *pPgno, n-1)); |
| 4037 | #endif |
| 4038 | }else{ |
| 4039 | /* Extract a leaf from the trunk */ |
| 4040 | int closest; |
| 4041 | Pgno iPage; |
| 4042 | unsigned char *aData = pTrunk->aData; |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 4043 | rc = sqlite3PagerWrite(pTrunk->pDbPage); |
drh | d3627af | 2006-12-18 18:34:51 +0000 | [diff] [blame] | 4044 | if( rc ){ |
| 4045 | goto end_allocate_page; |
| 4046 | } |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 4047 | if( nearby>0 ){ |
| 4048 | int i, dist; |
| 4049 | closest = 0; |
| 4050 | dist = get4byte(&aData[8]) - nearby; |
| 4051 | if( dist<0 ) dist = -dist; |
| 4052 | for(i=1; i<k; i++){ |
| 4053 | int d2 = get4byte(&aData[8+i*4]) - nearby; |
| 4054 | if( d2<0 ) d2 = -d2; |
| 4055 | if( d2<dist ){ |
| 4056 | closest = i; |
| 4057 | dist = d2; |
| 4058 | } |
| 4059 | } |
| 4060 | }else{ |
| 4061 | closest = 0; |
| 4062 | } |
| 4063 | |
| 4064 | iPage = get4byte(&aData[8+closest*4]); |
| 4065 | if( !searchList || iPage==nearby ){ |
| 4066 | *pPgno = iPage; |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 4067 | if( *pPgno>sqlite3PagerPagecount(pBt->pPager) ){ |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 4068 | /* Free page off the end of the file */ |
drh | 4928570 | 2005-09-17 15:20:26 +0000 | [diff] [blame] | 4069 | return SQLITE_CORRUPT_BKPT; |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 4070 | } |
| 4071 | TRACE(("ALLOCATE: %d was leaf %d of %d on trunk %d" |
| 4072 | ": %d more free pages\n", |
| 4073 | *pPgno, closest+1, k, pTrunk->pgno, n-1)); |
| 4074 | if( closest<k-1 ){ |
| 4075 | memcpy(&aData[8+closest*4], &aData[4+k*4], 4); |
| 4076 | } |
| 4077 | put4byte(&aData[4], k-1); |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 4078 | rc = sqlite3BtreeGetPage(pBt, *pPgno, ppPage, 1); |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 4079 | if( rc==SQLITE_OK ){ |
drh | 538f570 | 2007-04-13 02:14:30 +0000 | [diff] [blame] | 4080 | sqlite3PagerDontRollback((*ppPage)->pDbPage); |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 4081 | rc = sqlite3PagerWrite((*ppPage)->pDbPage); |
danielk1977 | aac0a38 | 2005-01-16 11:07:06 +0000 | [diff] [blame] | 4082 | if( rc!=SQLITE_OK ){ |
| 4083 | releasePage(*ppPage); |
| 4084 | } |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 4085 | } |
| 4086 | searchList = 0; |
| 4087 | } |
drh | ee696e2 | 2004-08-30 16:52:17 +0000 | [diff] [blame] | 4088 | } |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 4089 | releasePage(pPrevTrunk); |
drh | d3627af | 2006-12-18 18:34:51 +0000 | [diff] [blame] | 4090 | pPrevTrunk = 0; |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 4091 | }while( searchList ); |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 4092 | }else{ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 4093 | /* There are no pages on the freelist, so create a new page at the |
| 4094 | ** end of the file */ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 4095 | *pPgno = sqlite3PagerPagecount(pBt->pPager) + 1; |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 4096 | |
| 4097 | #ifndef SQLITE_OMIT_AUTOVACUUM |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 4098 | if( pBt->nTrunc ){ |
| 4099 | /* An incr-vacuum has already run within this transaction. So the |
| 4100 | ** page to allocate is not from the physical end of the file, but |
| 4101 | ** at pBt->nTrunc. |
| 4102 | */ |
| 4103 | *pPgno = pBt->nTrunc+1; |
| 4104 | if( *pPgno==PENDING_BYTE_PAGE(pBt) ){ |
| 4105 | (*pPgno)++; |
| 4106 | } |
| 4107 | } |
danielk1977 | 266664d | 2006-02-10 08:24:21 +0000 | [diff] [blame] | 4108 | if( pBt->autoVacuum && PTRMAP_ISPAGE(pBt, *pPgno) ){ |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 4109 | /* If *pPgno refers to a pointer-map page, allocate two new pages |
| 4110 | ** at the end of the file instead of one. The first allocated page |
| 4111 | ** becomes a new pointer-map page, the second is used by the caller. |
| 4112 | */ |
| 4113 | TRACE(("ALLOCATE: %d from end of file (pointer-map page)\n", *pPgno)); |
danielk1977 | 599fcba | 2004-11-08 07:13:13 +0000 | [diff] [blame] | 4114 | assert( *pPgno!=PENDING_BYTE_PAGE(pBt) ); |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 4115 | (*pPgno)++; |
drh | 7219043 | 2008-01-31 14:54:43 +0000 | [diff] [blame] | 4116 | if( *pPgno==PENDING_BYTE_PAGE(pBt) ){ (*pPgno)++; } |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 4117 | } |
danielk1977 | dddbcdc | 2007-04-26 14:42:34 +0000 | [diff] [blame] | 4118 | if( pBt->nTrunc ){ |
| 4119 | pBt->nTrunc = *pPgno; |
| 4120 | } |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 4121 | #endif |
| 4122 | |
danielk1977 | 599fcba | 2004-11-08 07:13:13 +0000 | [diff] [blame] | 4123 | assert( *pPgno!=PENDING_BYTE_PAGE(pBt) ); |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 4124 | rc = sqlite3BtreeGetPage(pBt, *pPgno, ppPage, 0); |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 4125 | if( rc ) return rc; |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 4126 | rc = sqlite3PagerWrite((*ppPage)->pDbPage); |
danielk1977 | aac0a38 | 2005-01-16 11:07:06 +0000 | [diff] [blame] | 4127 | if( rc!=SQLITE_OK ){ |
| 4128 | releasePage(*ppPage); |
| 4129 | } |
drh | 3a4c141 | 2004-05-09 20:40:11 +0000 | [diff] [blame] | 4130 | TRACE(("ALLOCATE: %d from end of file\n", *pPgno)); |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 4131 | } |
danielk1977 | 599fcba | 2004-11-08 07:13:13 +0000 | [diff] [blame] | 4132 | |
| 4133 | assert( *pPgno!=PENDING_BYTE_PAGE(pBt) ); |
drh | d3627af | 2006-12-18 18:34:51 +0000 | [diff] [blame] | 4134 | |
| 4135 | end_allocate_page: |
| 4136 | releasePage(pTrunk); |
| 4137 | releasePage(pPrevTrunk); |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 4138 | return rc; |
| 4139 | } |
| 4140 | |
| 4141 | /* |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 4142 | ** Add a page of the database file to the freelist. |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 4143 | ** |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 4144 | ** sqlite3PagerUnref() is NOT called for pPage. |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 4145 | */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 4146 | static int freePage(MemPage *pPage){ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 4147 | BtShared *pBt = pPage->pBt; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 4148 | MemPage *pPage1 = pBt->pPage1; |
| 4149 | int rc, n, k; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 4150 | |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 4151 | /* Prepare the page for freeing */ |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 4152 | assert( sqlite3_mutex_held(pPage->pBt->mutex) ); |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 4153 | assert( pPage->pgno>1 ); |
| 4154 | pPage->isInit = 0; |
| 4155 | releasePage(pPage->pParent); |
| 4156 | pPage->pParent = 0; |
| 4157 | |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 4158 | /* Increment the free page count on pPage1 */ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 4159 | rc = sqlite3PagerWrite(pPage1->pDbPage); |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 4160 | if( rc ) return rc; |
| 4161 | n = get4byte(&pPage1->aData[36]); |
| 4162 | put4byte(&pPage1->aData[36], n+1); |
| 4163 | |
drh | fcce93f | 2006-02-22 03:08:32 +0000 | [diff] [blame] | 4164 | #ifdef SQLITE_SECURE_DELETE |
| 4165 | /* If the SQLITE_SECURE_DELETE compile-time option is enabled, then |
| 4166 | ** always fully overwrite deleted information with zeros. |
| 4167 | */ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 4168 | rc = sqlite3PagerWrite(pPage->pDbPage); |
drh | fcce93f | 2006-02-22 03:08:32 +0000 | [diff] [blame] | 4169 | if( rc ) return rc; |
| 4170 | memset(pPage->aData, 0, pPage->pBt->pageSize); |
| 4171 | #endif |
| 4172 | |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 4173 | #ifndef SQLITE_OMIT_AUTOVACUUM |
| 4174 | /* If the database supports auto-vacuum, write an entry in the pointer-map |
danielk1977 | cb1a7eb | 2004-11-05 12:27:02 +0000 | [diff] [blame] | 4175 | ** to indicate that the page is free. |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 4176 | */ |
| 4177 | if( pBt->autoVacuum ){ |
| 4178 | rc = ptrmapPut(pBt, pPage->pgno, PTRMAP_FREEPAGE, 0); |
danielk1977 | a64a035 | 2004-11-05 01:45:13 +0000 | [diff] [blame] | 4179 | if( rc ) return rc; |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 4180 | } |
| 4181 | #endif |
| 4182 | |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 4183 | if( n==0 ){ |
| 4184 | /* This is the first free page */ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 4185 | rc = sqlite3PagerWrite(pPage->pDbPage); |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 4186 | if( rc ) return rc; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 4187 | memset(pPage->aData, 0, 8); |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 4188 | put4byte(&pPage1->aData[32], pPage->pgno); |
drh | 3a4c141 | 2004-05-09 20:40:11 +0000 | [diff] [blame] | 4189 | TRACE(("FREE-PAGE: %d first\n", pPage->pgno)); |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 4190 | }else{ |
| 4191 | /* Other free pages already exist. Retrive the first trunk page |
| 4192 | ** of the freelist and find out how many leaves it has. */ |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 4193 | MemPage *pTrunk; |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 4194 | rc = sqlite3BtreeGetPage(pBt, get4byte(&pPage1->aData[32]), &pTrunk, 0); |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 4195 | if( rc ) return rc; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 4196 | k = get4byte(&pTrunk->aData[4]); |
drh | ee696e2 | 2004-08-30 16:52:17 +0000 | [diff] [blame] | 4197 | if( k>=pBt->usableSize/4 - 8 ){ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 4198 | /* The trunk is full. Turn the page being freed into a new |
| 4199 | ** trunk page with no leaves. */ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 4200 | rc = sqlite3PagerWrite(pPage->pDbPage); |
drh | b9ee493 | 2007-09-07 14:32:06 +0000 | [diff] [blame] | 4201 | if( rc==SQLITE_OK ){ |
| 4202 | put4byte(pPage->aData, pTrunk->pgno); |
| 4203 | put4byte(&pPage->aData[4], 0); |
| 4204 | put4byte(&pPage1->aData[32], pPage->pgno); |
| 4205 | TRACE(("FREE-PAGE: %d new trunk page replacing %d\n", |
| 4206 | pPage->pgno, pTrunk->pgno)); |
| 4207 | } |
| 4208 | }else if( k<0 ){ |
| 4209 | rc = SQLITE_CORRUPT; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 4210 | }else{ |
| 4211 | /* Add the newly freed page as a leaf on the current trunk */ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 4212 | rc = sqlite3PagerWrite(pTrunk->pDbPage); |
drh | f534544 | 2007-04-09 12:45:02 +0000 | [diff] [blame] | 4213 | if( rc==SQLITE_OK ){ |
| 4214 | put4byte(&pTrunk->aData[4], k+1); |
| 4215 | put4byte(&pTrunk->aData[8+k*4], pPage->pgno); |
drh | fcce93f | 2006-02-22 03:08:32 +0000 | [diff] [blame] | 4216 | #ifndef SQLITE_SECURE_DELETE |
drh | 538f570 | 2007-04-13 02:14:30 +0000 | [diff] [blame] | 4217 | sqlite3PagerDontWrite(pPage->pDbPage); |
drh | fcce93f | 2006-02-22 03:08:32 +0000 | [diff] [blame] | 4218 | #endif |
drh | f534544 | 2007-04-09 12:45:02 +0000 | [diff] [blame] | 4219 | } |
drh | 3a4c141 | 2004-05-09 20:40:11 +0000 | [diff] [blame] | 4220 | 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] | 4221 | } |
| 4222 | releasePage(pTrunk); |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 4223 | } |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 4224 | return rc; |
| 4225 | } |
| 4226 | |
| 4227 | /* |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 4228 | ** Free any overflow pages associated with the given Cell. |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 4229 | */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 4230 | static int clearCell(MemPage *pPage, unsigned char *pCell){ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 4231 | BtShared *pBt = pPage->pBt; |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 4232 | CellInfo info; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 4233 | Pgno ovflPgno; |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 4234 | int rc; |
drh | 9444081 | 2007-03-06 11:42:19 +0000 | [diff] [blame] | 4235 | int nOvfl; |
| 4236 | int ovflPageSize; |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 4237 | |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 4238 | assert( sqlite3_mutex_held(pPage->pBt->mutex) ); |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 4239 | sqlite3BtreeParseCellPtr(pPage, pCell, &info); |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 4240 | if( info.iOverflow==0 ){ |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 4241 | return SQLITE_OK; /* No overflow pages. Return without doing anything */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 4242 | } |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 4243 | ovflPgno = get4byte(&pCell[info.iOverflow]); |
drh | 9444081 | 2007-03-06 11:42:19 +0000 | [diff] [blame] | 4244 | ovflPageSize = pBt->usableSize - 4; |
drh | 7236583 | 2007-03-06 15:53:44 +0000 | [diff] [blame] | 4245 | nOvfl = (info.nPayload - info.nLocal + ovflPageSize - 1)/ovflPageSize; |
| 4246 | assert( ovflPgno==0 || nOvfl>0 ); |
| 4247 | while( nOvfl-- ){ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 4248 | MemPage *pOvfl; |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 4249 | if( ovflPgno==0 || ovflPgno>sqlite3PagerPagecount(pBt->pPager) ){ |
drh | 4928570 | 2005-09-17 15:20:26 +0000 | [diff] [blame] | 4250 | return SQLITE_CORRUPT_BKPT; |
danielk1977 | a1cb183 | 2005-02-12 08:59:55 +0000 | [diff] [blame] | 4251 | } |
danielk1977 | 8c0a959 | 2007-04-30 16:55:00 +0000 | [diff] [blame] | 4252 | |
| 4253 | rc = getOverflowPage(pBt, ovflPgno, &pOvfl, (nOvfl==0)?0:&ovflPgno); |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 4254 | if( rc ) return rc; |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 4255 | rc = freePage(pOvfl); |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 4256 | sqlite3PagerUnref(pOvfl->pDbPage); |
danielk1977 | 6b456a2 | 2005-03-21 04:04:02 +0000 | [diff] [blame] | 4257 | if( rc ) return rc; |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 4258 | } |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 4259 | return SQLITE_OK; |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 4260 | } |
| 4261 | |
| 4262 | /* |
drh | 9102529 | 2004-05-03 19:49:32 +0000 | [diff] [blame] | 4263 | ** Create the byte sequence used to represent a cell on page pPage |
| 4264 | ** and write that byte sequence into pCell[]. Overflow pages are |
| 4265 | ** allocated and filled in as necessary. The calling procedure |
| 4266 | ** is responsible for making sure sufficient space has been allocated |
| 4267 | ** for pCell[]. |
| 4268 | ** |
| 4269 | ** Note that pCell does not necessary need to point to the pPage->aData |
| 4270 | ** area. pCell might point to some temporary storage. The cell will |
| 4271 | ** be constructed in this temporary area then copied into pPage->aData |
| 4272 | ** later. |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 4273 | */ |
| 4274 | static int fillInCell( |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 4275 | MemPage *pPage, /* The page that contains the cell */ |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 4276 | unsigned char *pCell, /* Complete text of the cell */ |
drh | 4a1c380 | 2004-05-12 15:15:47 +0000 | [diff] [blame] | 4277 | const void *pKey, i64 nKey, /* The key */ |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 4278 | const void *pData,int nData, /* The data */ |
drh | b026e05 | 2007-05-02 01:34:31 +0000 | [diff] [blame] | 4279 | int nZero, /* Extra zero bytes to append to pData */ |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 4280 | int *pnSize /* Write cell size here */ |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 4281 | ){ |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 4282 | int nPayload; |
drh | 8c6fa9b | 2004-05-26 00:01:53 +0000 | [diff] [blame] | 4283 | const u8 *pSrc; |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 4284 | int nSrc, n, rc; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 4285 | int spaceLeft; |
| 4286 | MemPage *pOvfl = 0; |
drh | 9b17127 | 2004-05-08 02:03:22 +0000 | [diff] [blame] | 4287 | MemPage *pToRelease = 0; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 4288 | unsigned char *pPrior; |
| 4289 | unsigned char *pPayload; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 4290 | BtShared *pBt = pPage->pBt; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 4291 | Pgno pgnoOvfl = 0; |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 4292 | int nHeader; |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 4293 | CellInfo info; |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 4294 | |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 4295 | assert( sqlite3_mutex_held(pPage->pBt->mutex) ); |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 4296 | |
drh | 9102529 | 2004-05-03 19:49:32 +0000 | [diff] [blame] | 4297 | /* Fill in the header. */ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4298 | nHeader = 0; |
drh | 9102529 | 2004-05-03 19:49:32 +0000 | [diff] [blame] | 4299 | if( !pPage->leaf ){ |
| 4300 | nHeader += 4; |
| 4301 | } |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 4302 | if( pPage->hasData ){ |
drh | b026e05 | 2007-05-02 01:34:31 +0000 | [diff] [blame] | 4303 | nHeader += putVarint(&pCell[nHeader], nData+nZero); |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 4304 | }else{ |
drh | b026e05 | 2007-05-02 01:34:31 +0000 | [diff] [blame] | 4305 | nData = nZero = 0; |
drh | 9102529 | 2004-05-03 19:49:32 +0000 | [diff] [blame] | 4306 | } |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 4307 | nHeader += putVarint(&pCell[nHeader], *(u64*)&nKey); |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 4308 | sqlite3BtreeParseCellPtr(pPage, pCell, &info); |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 4309 | assert( info.nHeader==nHeader ); |
| 4310 | assert( info.nKey==nKey ); |
drh | b026e05 | 2007-05-02 01:34:31 +0000 | [diff] [blame] | 4311 | assert( info.nData==nData+nZero ); |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 4312 | |
| 4313 | /* Fill in the payload */ |
drh | b026e05 | 2007-05-02 01:34:31 +0000 | [diff] [blame] | 4314 | nPayload = nData + nZero; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 4315 | if( pPage->intKey ){ |
| 4316 | pSrc = pData; |
| 4317 | nSrc = nData; |
drh | 9102529 | 2004-05-03 19:49:32 +0000 | [diff] [blame] | 4318 | nData = 0; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 4319 | }else{ |
| 4320 | nPayload += nKey; |
| 4321 | pSrc = pKey; |
| 4322 | nSrc = nKey; |
| 4323 | } |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 4324 | *pnSize = info.nSize; |
| 4325 | spaceLeft = info.nLocal; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 4326 | pPayload = &pCell[nHeader]; |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 4327 | pPrior = &pCell[info.iOverflow]; |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 4328 | |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 4329 | while( nPayload>0 ){ |
| 4330 | if( spaceLeft==0 ){ |
danielk1977 | b39f70b | 2007-05-17 18:28:11 +0000 | [diff] [blame] | 4331 | int isExact = 0; |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 4332 | #ifndef SQLITE_OMIT_AUTOVACUUM |
| 4333 | Pgno pgnoPtrmap = pgnoOvfl; /* Overflow page pointer-map entry page */ |
danielk1977 | b39f70b | 2007-05-17 18:28:11 +0000 | [diff] [blame] | 4334 | if( pBt->autoVacuum ){ |
| 4335 | do{ |
| 4336 | pgnoOvfl++; |
| 4337 | } while( |
| 4338 | PTRMAP_ISPAGE(pBt, pgnoOvfl) || pgnoOvfl==PENDING_BYTE_PAGE(pBt) |
| 4339 | ); |
danielk1977 | 89a4be8 | 2007-05-23 13:34:32 +0000 | [diff] [blame] | 4340 | if( pgnoOvfl>1 ){ |
danielk1977 | b39f70b | 2007-05-17 18:28:11 +0000 | [diff] [blame] | 4341 | /* isExact = 1; */ |
| 4342 | } |
| 4343 | } |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 4344 | #endif |
danielk1977 | b39f70b | 2007-05-17 18:28:11 +0000 | [diff] [blame] | 4345 | rc = allocateBtreePage(pBt, &pOvfl, &pgnoOvfl, pgnoOvfl, isExact); |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 4346 | #ifndef SQLITE_OMIT_AUTOVACUUM |
danielk1977 | a19df67 | 2004-11-03 11:37:07 +0000 | [diff] [blame] | 4347 | /* If the database supports auto-vacuum, and the second or subsequent |
| 4348 | ** overflow page is being allocated, add an entry to the pointer-map |
danielk1977 | 4ef2449 | 2007-05-23 09:52:41 +0000 | [diff] [blame] | 4349 | ** for that page now. |
| 4350 | ** |
| 4351 | ** If this is the first overflow page, then write a partial entry |
| 4352 | ** to the pointer-map. If we write nothing to this pointer-map slot, |
| 4353 | ** then the optimistic overflow chain processing in clearCell() |
| 4354 | ** may misinterpret the uninitialised values and delete the |
| 4355 | ** wrong pages from the database. |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 4356 | */ |
danielk1977 | 4ef2449 | 2007-05-23 09:52:41 +0000 | [diff] [blame] | 4357 | if( pBt->autoVacuum && rc==SQLITE_OK ){ |
| 4358 | u8 eType = (pgnoPtrmap?PTRMAP_OVERFLOW2:PTRMAP_OVERFLOW1); |
| 4359 | rc = ptrmapPut(pBt, pgnoOvfl, eType, pgnoPtrmap); |
danielk1977 | 89a4be8 | 2007-05-23 13:34:32 +0000 | [diff] [blame] | 4360 | if( rc ){ |
| 4361 | releasePage(pOvfl); |
| 4362 | } |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 4363 | } |
| 4364 | #endif |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 4365 | if( rc ){ |
drh | 9b17127 | 2004-05-08 02:03:22 +0000 | [diff] [blame] | 4366 | releasePage(pToRelease); |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 4367 | return rc; |
| 4368 | } |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 4369 | put4byte(pPrior, pgnoOvfl); |
drh | 9b17127 | 2004-05-08 02:03:22 +0000 | [diff] [blame] | 4370 | releasePage(pToRelease); |
| 4371 | pToRelease = pOvfl; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 4372 | pPrior = pOvfl->aData; |
| 4373 | put4byte(pPrior, 0); |
| 4374 | pPayload = &pOvfl->aData[4]; |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 4375 | spaceLeft = pBt->usableSize - 4; |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 4376 | } |
| 4377 | n = nPayload; |
| 4378 | if( n>spaceLeft ) n = spaceLeft; |
drh | b026e05 | 2007-05-02 01:34:31 +0000 | [diff] [blame] | 4379 | if( nSrc>0 ){ |
| 4380 | if( n>nSrc ) n = nSrc; |
| 4381 | assert( pSrc ); |
| 4382 | memcpy(pPayload, pSrc, n); |
| 4383 | }else{ |
| 4384 | memset(pPayload, 0, n); |
| 4385 | } |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 4386 | nPayload -= n; |
drh | de64713 | 2004-05-07 17:57:49 +0000 | [diff] [blame] | 4387 | pPayload += n; |
drh | 9b17127 | 2004-05-08 02:03:22 +0000 | [diff] [blame] | 4388 | pSrc += n; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 4389 | nSrc -= n; |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 4390 | spaceLeft -= n; |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 4391 | if( nSrc==0 ){ |
| 4392 | nSrc = nData; |
| 4393 | pSrc = pData; |
| 4394 | } |
drh | dd79342 | 2001-06-28 01:54:48 +0000 | [diff] [blame] | 4395 | } |
drh | 9b17127 | 2004-05-08 02:03:22 +0000 | [diff] [blame] | 4396 | releasePage(pToRelease); |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 4397 | return SQLITE_OK; |
| 4398 | } |
| 4399 | |
| 4400 | /* |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 4401 | ** Change the MemPage.pParent pointer on the page whose number is |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 4402 | ** given in the second argument so that MemPage.pParent holds the |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 4403 | ** pointer in the third argument. |
| 4404 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 4405 | static int reparentPage(BtShared *pBt, Pgno pgno, MemPage *pNewParent, int idx){ |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 4406 | MemPage *pThis; |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 4407 | DbPage *pDbPage; |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 4408 | |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 4409 | assert( sqlite3_mutex_held(pBt->mutex) ); |
drh | 43617e9 | 2006-03-06 20:55:46 +0000 | [diff] [blame] | 4410 | assert( pNewParent!=0 ); |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 4411 | if( pgno==0 ) return SQLITE_OK; |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 4412 | assert( pBt->pPager!=0 ); |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 4413 | pDbPage = sqlite3PagerLookup(pBt->pPager, pgno); |
| 4414 | if( pDbPage ){ |
| 4415 | pThis = (MemPage *)sqlite3PagerGetExtra(pDbPage); |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 4416 | if( pThis->isInit ){ |
drh | bf4bca5 | 2007-09-06 22:19:14 +0000 | [diff] [blame] | 4417 | assert( pThis->aData==sqlite3PagerGetData(pDbPage) ); |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 4418 | if( pThis->pParent!=pNewParent ){ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 4419 | if( pThis->pParent ) sqlite3PagerUnref(pThis->pParent->pDbPage); |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 4420 | pThis->pParent = pNewParent; |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 4421 | sqlite3PagerRef(pNewParent->pDbPage); |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 4422 | } |
| 4423 | pThis->idxParent = idx; |
drh | dd79342 | 2001-06-28 01:54:48 +0000 | [diff] [blame] | 4424 | } |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 4425 | sqlite3PagerUnref(pDbPage); |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 4426 | } |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 4427 | |
| 4428 | #ifndef SQLITE_OMIT_AUTOVACUUM |
| 4429 | if( pBt->autoVacuum ){ |
| 4430 | return ptrmapPut(pBt, pgno, PTRMAP_BTREE, pNewParent->pgno); |
| 4431 | } |
| 4432 | #endif |
| 4433 | return SQLITE_OK; |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 4434 | } |
| 4435 | |
danielk1977 | ac11ee6 | 2005-01-15 12:45:51 +0000 | [diff] [blame] | 4436 | |
| 4437 | |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 4438 | /* |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 4439 | ** Change the pParent pointer of all children of pPage to point back |
| 4440 | ** to pPage. |
| 4441 | ** |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 4442 | ** In other words, for every child of pPage, invoke reparentPage() |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4443 | ** to make sure that each child knows that pPage is its parent. |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 4444 | ** |
| 4445 | ** This routine gets called after you memcpy() one page into |
| 4446 | ** another. |
| 4447 | */ |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 4448 | static int reparentChildPages(MemPage *pPage){ |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 4449 | int i; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 4450 | BtShared *pBt = pPage->pBt; |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 4451 | int rc = SQLITE_OK; |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 4452 | |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 4453 | assert( sqlite3_mutex_held(pPage->pBt->mutex) ); |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 4454 | if( pPage->leaf ) return SQLITE_OK; |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 4455 | |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 4456 | for(i=0; i<pPage->nCell; i++){ |
danielk1977 | 1cc5ed8 | 2007-05-16 17:28:43 +0000 | [diff] [blame] | 4457 | u8 *pCell = findCell(pPage, i); |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 4458 | if( !pPage->leaf ){ |
| 4459 | rc = reparentPage(pBt, get4byte(pCell), pPage, i); |
| 4460 | if( rc!=SQLITE_OK ) return rc; |
| 4461 | } |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 4462 | } |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 4463 | if( !pPage->leaf ){ |
| 4464 | rc = reparentPage(pBt, get4byte(&pPage->aData[pPage->hdrOffset+8]), |
| 4465 | pPage, i); |
| 4466 | pPage->idxShift = 0; |
| 4467 | } |
| 4468 | return rc; |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 4469 | } |
| 4470 | |
| 4471 | /* |
| 4472 | ** Remove the i-th cell from pPage. This routine effects pPage only. |
| 4473 | ** The cell content is not freed or deallocated. It is assumed that |
| 4474 | ** the cell content has been copied someplace else. This routine just |
| 4475 | ** removes the reference to the cell from pPage. |
| 4476 | ** |
| 4477 | ** "sz" must be the number of bytes in the cell. |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 4478 | */ |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 4479 | static void dropCell(MemPage *pPage, int idx, int sz){ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4480 | int i; /* Loop counter */ |
| 4481 | int pc; /* Offset to cell content of cell being deleted */ |
| 4482 | u8 *data; /* pPage->aData */ |
| 4483 | u8 *ptr; /* Used to move bytes around within data[] */ |
| 4484 | |
drh | 8c42ca9 | 2001-06-22 19:15:00 +0000 | [diff] [blame] | 4485 | assert( idx>=0 && idx<pPage->nCell ); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4486 | assert( sz==cellSize(pPage, idx) ); |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 4487 | assert( sqlite3PagerIswriteable(pPage->pDbPage) ); |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 4488 | assert( sqlite3_mutex_held(pPage->pBt->mutex) ); |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 4489 | data = pPage->aData; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4490 | ptr = &data[pPage->cellOffset + 2*idx]; |
| 4491 | pc = get2byte(ptr); |
| 4492 | assert( pc>10 && pc+sz<=pPage->pBt->usableSize ); |
drh | de64713 | 2004-05-07 17:57:49 +0000 | [diff] [blame] | 4493 | freeSpace(pPage, pc, sz); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4494 | for(i=idx+1; i<pPage->nCell; i++, ptr+=2){ |
| 4495 | ptr[0] = ptr[2]; |
| 4496 | ptr[1] = ptr[3]; |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 4497 | } |
| 4498 | pPage->nCell--; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4499 | put2byte(&data[pPage->hdrOffset+3], pPage->nCell); |
| 4500 | pPage->nFree += 2; |
drh | 428ae8c | 2003-01-04 16:48:09 +0000 | [diff] [blame] | 4501 | pPage->idxShift = 1; |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 4502 | } |
| 4503 | |
| 4504 | /* |
| 4505 | ** Insert a new cell on pPage at cell index "i". pCell points to the |
| 4506 | ** content of the cell. |
| 4507 | ** |
| 4508 | ** 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] | 4509 | ** will not fit, then make a copy of the cell content into pTemp if |
| 4510 | ** pTemp is not null. Regardless of pTemp, allocate a new entry |
| 4511 | ** in pPage->aOvfl[] and make it point to the cell content (either |
| 4512 | ** in pTemp or the original pCell) and also record its index. |
| 4513 | ** Allocating a new entry in pPage->aCell[] implies that |
| 4514 | ** pPage->nOverflow is incremented. |
danielk1977 | a3ad5e7 | 2005-01-07 08:56:44 +0000 | [diff] [blame] | 4515 | ** |
| 4516 | ** If nSkip is non-zero, then do not copy the first nSkip bytes of the |
| 4517 | ** cell. The caller will overwrite them after this function returns. If |
drh | 4b238df | 2005-01-08 15:43:18 +0000 | [diff] [blame] | 4518 | ** 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] | 4519 | ** (but pCell+nSkip is always valid). |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 4520 | */ |
danielk1977 | e80463b | 2004-11-03 03:01:16 +0000 | [diff] [blame] | 4521 | static int insertCell( |
drh | 24cd67e | 2004-05-10 16:18:47 +0000 | [diff] [blame] | 4522 | MemPage *pPage, /* Page into which we are copying */ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4523 | int i, /* New cell becomes the i-th cell of the page */ |
| 4524 | u8 *pCell, /* Content of the new cell */ |
| 4525 | int sz, /* Bytes of content in pCell */ |
danielk1977 | a3ad5e7 | 2005-01-07 08:56:44 +0000 | [diff] [blame] | 4526 | u8 *pTemp, /* Temp storage space for pCell, if needed */ |
| 4527 | u8 nSkip /* Do not write the first nSkip bytes of the cell */ |
drh | 24cd67e | 2004-05-10 16:18:47 +0000 | [diff] [blame] | 4528 | ){ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4529 | int idx; /* Where to write new cell content in data[] */ |
| 4530 | int j; /* Loop counter */ |
| 4531 | int top; /* First byte of content for any cell in data[] */ |
| 4532 | int end; /* First byte past the last cell pointer in data[] */ |
| 4533 | int ins; /* Index in data[] where new cell pointer is inserted */ |
| 4534 | int hdr; /* Offset into data[] of the page header */ |
| 4535 | int cellOffset; /* Address of first cell pointer in data[] */ |
| 4536 | u8 *data; /* The content of the whole page */ |
| 4537 | u8 *ptr; /* Used for moving information around in data[] */ |
| 4538 | |
| 4539 | assert( i>=0 && i<=pPage->nCell+pPage->nOverflow ); |
| 4540 | assert( sz==cellSizePtr(pPage, pCell) ); |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 4541 | assert( sqlite3_mutex_held(pPage->pBt->mutex) ); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4542 | if( pPage->nOverflow || sz+2>pPage->nFree ){ |
drh | 24cd67e | 2004-05-10 16:18:47 +0000 | [diff] [blame] | 4543 | if( pTemp ){ |
danielk1977 | a3ad5e7 | 2005-01-07 08:56:44 +0000 | [diff] [blame] | 4544 | memcpy(pTemp+nSkip, pCell+nSkip, sz-nSkip); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4545 | pCell = pTemp; |
drh | 24cd67e | 2004-05-10 16:18:47 +0000 | [diff] [blame] | 4546 | } |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4547 | j = pPage->nOverflow++; |
| 4548 | assert( j<sizeof(pPage->aOvfl)/sizeof(pPage->aOvfl[0]) ); |
| 4549 | pPage->aOvfl[j].pCell = pCell; |
| 4550 | pPage->aOvfl[j].idx = i; |
| 4551 | pPage->nFree = 0; |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 4552 | }else{ |
danielk1977 | 6e465eb | 2007-08-21 13:11:00 +0000 | [diff] [blame] | 4553 | int rc = sqlite3PagerWrite(pPage->pDbPage); |
| 4554 | if( rc!=SQLITE_OK ){ |
| 4555 | return rc; |
| 4556 | } |
| 4557 | assert( sqlite3PagerIswriteable(pPage->pDbPage) ); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4558 | data = pPage->aData; |
| 4559 | hdr = pPage->hdrOffset; |
| 4560 | top = get2byte(&data[hdr+5]); |
| 4561 | cellOffset = pPage->cellOffset; |
| 4562 | end = cellOffset + 2*pPage->nCell + 2; |
| 4563 | ins = cellOffset + 2*i; |
| 4564 | if( end > top - sz ){ |
danielk1977 | 6e465eb | 2007-08-21 13:11:00 +0000 | [diff] [blame] | 4565 | rc = defragmentPage(pPage); |
danielk1977 | 6b456a2 | 2005-03-21 04:04:02 +0000 | [diff] [blame] | 4566 | if( rc!=SQLITE_OK ) return rc; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4567 | top = get2byte(&data[hdr+5]); |
| 4568 | assert( end + sz <= top ); |
| 4569 | } |
| 4570 | idx = allocateSpace(pPage, sz); |
| 4571 | assert( idx>0 ); |
| 4572 | assert( end <= get2byte(&data[hdr+5]) ); |
| 4573 | pPage->nCell++; |
| 4574 | pPage->nFree -= 2; |
danielk1977 | a3ad5e7 | 2005-01-07 08:56:44 +0000 | [diff] [blame] | 4575 | memcpy(&data[idx+nSkip], pCell+nSkip, sz-nSkip); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4576 | for(j=end-2, ptr=&data[j]; j>ins; j-=2, ptr-=2){ |
| 4577 | ptr[0] = ptr[-2]; |
| 4578 | ptr[1] = ptr[-1]; |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 4579 | } |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4580 | put2byte(&data[ins], idx); |
| 4581 | put2byte(&data[hdr+3], pPage->nCell); |
| 4582 | pPage->idxShift = 1; |
danielk1977 | a19df67 | 2004-11-03 11:37:07 +0000 | [diff] [blame] | 4583 | #ifndef SQLITE_OMIT_AUTOVACUUM |
| 4584 | if( pPage->pBt->autoVacuum ){ |
| 4585 | /* The cell may contain a pointer to an overflow page. If so, write |
| 4586 | ** the entry for the overflow page into the pointer map. |
| 4587 | */ |
| 4588 | CellInfo info; |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 4589 | sqlite3BtreeParseCellPtr(pPage, pCell, &info); |
drh | 7236583 | 2007-03-06 15:53:44 +0000 | [diff] [blame] | 4590 | assert( (info.nData+(pPage->intKey?0:info.nKey))==info.nPayload ); |
danielk1977 | a19df67 | 2004-11-03 11:37:07 +0000 | [diff] [blame] | 4591 | if( (info.nData+(pPage->intKey?0:info.nKey))>info.nLocal ){ |
| 4592 | Pgno pgnoOvfl = get4byte(&pCell[info.iOverflow]); |
danielk1977 | 6e465eb | 2007-08-21 13:11:00 +0000 | [diff] [blame] | 4593 | rc = ptrmapPut(pPage->pBt, pgnoOvfl, PTRMAP_OVERFLOW1, pPage->pgno); |
danielk1977 | a19df67 | 2004-11-03 11:37:07 +0000 | [diff] [blame] | 4594 | if( rc!=SQLITE_OK ) return rc; |
| 4595 | } |
| 4596 | } |
| 4597 | #endif |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 4598 | } |
danielk1977 | e80463b | 2004-11-03 03:01:16 +0000 | [diff] [blame] | 4599 | |
danielk1977 | e80463b | 2004-11-03 03:01:16 +0000 | [diff] [blame] | 4600 | return SQLITE_OK; |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 4601 | } |
| 4602 | |
| 4603 | /* |
drh | fa1a98a | 2004-05-14 19:08:17 +0000 | [diff] [blame] | 4604 | ** Add a list of cells to a page. The page should be initially empty. |
| 4605 | ** The cells are guaranteed to fit on the page. |
| 4606 | */ |
| 4607 | static void assemblePage( |
| 4608 | MemPage *pPage, /* The page to be assemblied */ |
| 4609 | int nCell, /* The number of cells to add to this page */ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4610 | u8 **apCell, /* Pointers to cell bodies */ |
drh | a9121e4 | 2008-02-19 14:59:35 +0000 | [diff] [blame] | 4611 | u16 *aSize /* Sizes of the cells */ |
drh | fa1a98a | 2004-05-14 19:08:17 +0000 | [diff] [blame] | 4612 | ){ |
| 4613 | int i; /* Loop counter */ |
| 4614 | int totalSize; /* Total size of all cells */ |
| 4615 | int hdr; /* Index of page header */ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4616 | int cellptr; /* Address of next cell pointer */ |
| 4617 | int cellbody; /* Address of next cell body */ |
drh | fa1a98a | 2004-05-14 19:08:17 +0000 | [diff] [blame] | 4618 | u8 *data; /* Data for the page */ |
| 4619 | |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4620 | assert( pPage->nOverflow==0 ); |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 4621 | assert( sqlite3_mutex_held(pPage->pBt->mutex) ); |
drh | fa1a98a | 2004-05-14 19:08:17 +0000 | [diff] [blame] | 4622 | totalSize = 0; |
| 4623 | for(i=0; i<nCell; i++){ |
| 4624 | totalSize += aSize[i]; |
| 4625 | } |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4626 | assert( totalSize+2*nCell<=pPage->nFree ); |
drh | fa1a98a | 2004-05-14 19:08:17 +0000 | [diff] [blame] | 4627 | assert( pPage->nCell==0 ); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4628 | cellptr = pPage->cellOffset; |
drh | fa1a98a | 2004-05-14 19:08:17 +0000 | [diff] [blame] | 4629 | data = pPage->aData; |
| 4630 | hdr = pPage->hdrOffset; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4631 | put2byte(&data[hdr+3], nCell); |
drh | 09d0deb | 2005-08-02 17:13:09 +0000 | [diff] [blame] | 4632 | if( nCell ){ |
| 4633 | cellbody = allocateSpace(pPage, totalSize); |
| 4634 | assert( cellbody>0 ); |
| 4635 | assert( pPage->nFree >= 2*nCell ); |
| 4636 | pPage->nFree -= 2*nCell; |
| 4637 | for(i=0; i<nCell; i++){ |
| 4638 | put2byte(&data[cellptr], cellbody); |
| 4639 | memcpy(&data[cellbody], apCell[i], aSize[i]); |
| 4640 | cellptr += 2; |
| 4641 | cellbody += aSize[i]; |
| 4642 | } |
| 4643 | assert( cellbody==pPage->pBt->usableSize ); |
drh | fa1a98a | 2004-05-14 19:08:17 +0000 | [diff] [blame] | 4644 | } |
| 4645 | pPage->nCell = nCell; |
drh | fa1a98a | 2004-05-14 19:08:17 +0000 | [diff] [blame] | 4646 | } |
| 4647 | |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 4648 | /* |
drh | c3b7057 | 2003-01-04 19:44:07 +0000 | [diff] [blame] | 4649 | ** The following parameters determine how many adjacent pages get involved |
| 4650 | ** in a balancing operation. NN is the number of neighbors on either side |
| 4651 | ** of the page that participate in the balancing operation. NB is the |
| 4652 | ** total number of pages that participate, including the target page and |
| 4653 | ** NN neighbors on either side. |
| 4654 | ** |
| 4655 | ** The minimum value of NN is 1 (of course). Increasing NN above 1 |
| 4656 | ** (to 2 or 3) gives a modest improvement in SELECT and DELETE performance |
| 4657 | ** in exchange for a larger degradation in INSERT and UPDATE performance. |
| 4658 | ** The value of NN appears to give the best results overall. |
| 4659 | */ |
| 4660 | #define NN 1 /* Number of neighbors on either side of pPage */ |
| 4661 | #define NB (NN*2+1) /* Total pages involved in the balance */ |
| 4662 | |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4663 | /* Forward reference */ |
danielk1977 | ac245ec | 2005-01-14 13:50:11 +0000 | [diff] [blame] | 4664 | static int balance(MemPage*, int); |
| 4665 | |
drh | 615ae55 | 2005-01-16 23:21:00 +0000 | [diff] [blame] | 4666 | #ifndef SQLITE_OMIT_QUICKBALANCE |
drh | f222e71 | 2005-01-14 22:55:49 +0000 | [diff] [blame] | 4667 | /* |
| 4668 | ** This version of balance() handles the common special case where |
| 4669 | ** a new entry is being inserted on the extreme right-end of the |
| 4670 | ** tree, in other words, when the new entry will become the largest |
| 4671 | ** entry in the tree. |
| 4672 | ** |
| 4673 | ** Instead of trying balance the 3 right-most leaf pages, just add |
| 4674 | ** a new page to the right-hand side and put the one new entry in |
| 4675 | ** that page. This leaves the right side of the tree somewhat |
| 4676 | ** unbalanced. But odds are that we will be inserting new entries |
| 4677 | ** at the end soon afterwards so the nearly empty page will quickly |
| 4678 | ** fill up. On average. |
| 4679 | ** |
| 4680 | ** pPage is the leaf page which is the right-most page in the tree. |
| 4681 | ** pParent is its parent. pPage must have a single overflow entry |
| 4682 | ** which is also the right-most entry on the page. |
| 4683 | */ |
danielk1977 | ac245ec | 2005-01-14 13:50:11 +0000 | [diff] [blame] | 4684 | static int balance_quick(MemPage *pPage, MemPage *pParent){ |
| 4685 | int rc; |
| 4686 | MemPage *pNew; |
| 4687 | Pgno pgnoNew; |
| 4688 | u8 *pCell; |
drh | a9121e4 | 2008-02-19 14:59:35 +0000 | [diff] [blame] | 4689 | u16 szCell; |
danielk1977 | ac245ec | 2005-01-14 13:50:11 +0000 | [diff] [blame] | 4690 | CellInfo info; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 4691 | BtShared *pBt = pPage->pBt; |
danielk1977 | 79a40da | 2005-01-16 08:00:01 +0000 | [diff] [blame] | 4692 | int parentIdx = pParent->nCell; /* pParent new divider cell index */ |
| 4693 | int parentSize; /* Size of new divider cell */ |
| 4694 | u8 parentCell[64]; /* Space for the new divider cell */ |
danielk1977 | ac245ec | 2005-01-14 13:50:11 +0000 | [diff] [blame] | 4695 | |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 4696 | assert( sqlite3_mutex_held(pPage->pBt->mutex) ); |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 4697 | |
danielk1977 | ac245ec | 2005-01-14 13:50:11 +0000 | [diff] [blame] | 4698 | /* Allocate a new page. Insert the overflow cell from pPage |
| 4699 | ** into it. Then remove the overflow cell from pPage. |
| 4700 | */ |
drh | 4f0c587 | 2007-03-26 22:05:01 +0000 | [diff] [blame] | 4701 | rc = allocateBtreePage(pBt, &pNew, &pgnoNew, 0, 0); |
danielk1977 | ac245ec | 2005-01-14 13:50:11 +0000 | [diff] [blame] | 4702 | if( rc!=SQLITE_OK ){ |
| 4703 | return rc; |
| 4704 | } |
| 4705 | pCell = pPage->aOvfl[0].pCell; |
| 4706 | szCell = cellSizePtr(pPage, pCell); |
| 4707 | zeroPage(pNew, pPage->aData[0]); |
| 4708 | assemblePage(pNew, 1, &pCell, &szCell); |
| 4709 | pPage->nOverflow = 0; |
| 4710 | |
danielk1977 | 79a40da | 2005-01-16 08:00:01 +0000 | [diff] [blame] | 4711 | /* Set the parent of the newly allocated page to pParent. */ |
| 4712 | pNew->pParent = pParent; |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 4713 | sqlite3PagerRef(pParent->pDbPage); |
danielk1977 | 79a40da | 2005-01-16 08:00:01 +0000 | [diff] [blame] | 4714 | |
danielk1977 | ac245ec | 2005-01-14 13:50:11 +0000 | [diff] [blame] | 4715 | /* pPage is currently the right-child of pParent. Change this |
| 4716 | ** so that the right-child is the new page allocated above and |
danielk1977 | 79a40da | 2005-01-16 08:00:01 +0000 | [diff] [blame] | 4717 | ** pPage is the next-to-right child. |
danielk1977 | ac245ec | 2005-01-14 13:50:11 +0000 | [diff] [blame] | 4718 | */ |
danielk1977 | ac11ee6 | 2005-01-15 12:45:51 +0000 | [diff] [blame] | 4719 | assert( pPage->nCell>0 ); |
danielk1977 | 1cc5ed8 | 2007-05-16 17:28:43 +0000 | [diff] [blame] | 4720 | pCell = findCell(pPage, pPage->nCell-1); |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 4721 | sqlite3BtreeParseCellPtr(pPage, pCell, &info); |
drh | b026e05 | 2007-05-02 01:34:31 +0000 | [diff] [blame] | 4722 | rc = fillInCell(pParent, parentCell, 0, info.nKey, 0, 0, 0, &parentSize); |
danielk1977 | ac245ec | 2005-01-14 13:50:11 +0000 | [diff] [blame] | 4723 | if( rc!=SQLITE_OK ){ |
danielk1977 | 79a40da | 2005-01-16 08:00:01 +0000 | [diff] [blame] | 4724 | return rc; |
danielk1977 | ac245ec | 2005-01-14 13:50:11 +0000 | [diff] [blame] | 4725 | } |
| 4726 | assert( parentSize<64 ); |
| 4727 | rc = insertCell(pParent, parentIdx, parentCell, parentSize, 0, 4); |
| 4728 | if( rc!=SQLITE_OK ){ |
danielk1977 | 79a40da | 2005-01-16 08:00:01 +0000 | [diff] [blame] | 4729 | return rc; |
danielk1977 | ac245ec | 2005-01-14 13:50:11 +0000 | [diff] [blame] | 4730 | } |
| 4731 | put4byte(findOverflowCell(pParent,parentIdx), pPage->pgno); |
| 4732 | put4byte(&pParent->aData[pParent->hdrOffset+8], pgnoNew); |
| 4733 | |
danielk1977 | 79a40da | 2005-01-16 08:00:01 +0000 | [diff] [blame] | 4734 | #ifndef SQLITE_OMIT_AUTOVACUUM |
| 4735 | /* If this is an auto-vacuum database, update the pointer map |
| 4736 | ** with entries for the new page, and any pointer from the |
| 4737 | ** cell on the page to an overflow page. |
| 4738 | */ |
danielk1977 | ac11ee6 | 2005-01-15 12:45:51 +0000 | [diff] [blame] | 4739 | if( pBt->autoVacuum ){ |
| 4740 | rc = ptrmapPut(pBt, pgnoNew, PTRMAP_BTREE, pParent->pgno); |
danielk1977 | deb403e | 2007-05-24 09:20:16 +0000 | [diff] [blame] | 4741 | if( rc==SQLITE_OK ){ |
| 4742 | rc = ptrmapPutOvfl(pNew, 0); |
danielk1977 | ac11ee6 | 2005-01-15 12:45:51 +0000 | [diff] [blame] | 4743 | } |
danielk1977 | 79a40da | 2005-01-16 08:00:01 +0000 | [diff] [blame] | 4744 | if( rc!=SQLITE_OK ){ |
danielk1977 | deb403e | 2007-05-24 09:20:16 +0000 | [diff] [blame] | 4745 | releasePage(pNew); |
danielk1977 | 79a40da | 2005-01-16 08:00:01 +0000 | [diff] [blame] | 4746 | return rc; |
danielk1977 | ac11ee6 | 2005-01-15 12:45:51 +0000 | [diff] [blame] | 4747 | } |
| 4748 | } |
danielk1977 | 79a40da | 2005-01-16 08:00:01 +0000 | [diff] [blame] | 4749 | #endif |
danielk1977 | ac11ee6 | 2005-01-15 12:45:51 +0000 | [diff] [blame] | 4750 | |
danielk1977 | 79a40da | 2005-01-16 08:00:01 +0000 | [diff] [blame] | 4751 | /* Release the reference to the new page and balance the parent page, |
| 4752 | ** in case the divider cell inserted caused it to become overfull. |
| 4753 | */ |
danielk1977 | ac245ec | 2005-01-14 13:50:11 +0000 | [diff] [blame] | 4754 | releasePage(pNew); |
| 4755 | return balance(pParent, 0); |
| 4756 | } |
drh | 615ae55 | 2005-01-16 23:21:00 +0000 | [diff] [blame] | 4757 | #endif /* SQLITE_OMIT_QUICKBALANCE */ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4758 | |
drh | c3b7057 | 2003-01-04 19:44:07 +0000 | [diff] [blame] | 4759 | /* |
drh | ab01f61 | 2004-05-22 02:55:23 +0000 | [diff] [blame] | 4760 | ** This routine redistributes Cells on pPage and up to NN*2 siblings |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 4761 | ** 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] | 4762 | ** Usually NN siblings on either side of pPage is used in the balancing, |
| 4763 | ** though more siblings might come from one side if pPage is the first |
drh | ab01f61 | 2004-05-22 02:55:23 +0000 | [diff] [blame] | 4764 | ** 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] | 4765 | ** (something which can only happen if pPage is the root page or a |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 4766 | ** child of root) then all available siblings participate in the balancing. |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 4767 | ** |
drh | 0c6cc4e | 2004-06-15 02:13:26 +0000 | [diff] [blame] | 4768 | ** The number of siblings of pPage might be increased or decreased by one or |
| 4769 | ** 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] | 4770 | ** is special and is allowed to be nearly empty. If pPage is |
drh | 8c42ca9 | 2001-06-22 19:15:00 +0000 | [diff] [blame] | 4771 | ** the root page, then the depth of the tree might be increased |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 4772 | ** or decreased by one, as necessary, to keep the root page from being |
drh | ab01f61 | 2004-05-22 02:55:23 +0000 | [diff] [blame] | 4773 | ** overfull or completely empty. |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 4774 | ** |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 4775 | ** Note that when this routine is called, some of the Cells on pPage |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 4776 | ** might not actually be stored in pPage->aData[]. This can happen |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 4777 | ** 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] | 4778 | ** make sure all Cells for pPage once again fit in pPage->aData[]. |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 4779 | ** |
drh | 8c42ca9 | 2001-06-22 19:15:00 +0000 | [diff] [blame] | 4780 | ** In the course of balancing the siblings of pPage, the parent of pPage |
| 4781 | ** might become overfull or underfull. If that happens, then this routine |
| 4782 | ** is called recursively on the parent. |
| 4783 | ** |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 4784 | ** If this routine fails for any reason, it might leave the database |
| 4785 | ** in a corrupted state. So if this routine fails, the database should |
| 4786 | ** be rolled back. |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 4787 | */ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4788 | static int balance_nonroot(MemPage *pPage){ |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 4789 | MemPage *pParent; /* The parent of pPage */ |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 4790 | BtShared *pBt; /* The whole database */ |
danielk1977 | 634f298 | 2005-03-28 08:44:07 +0000 | [diff] [blame] | 4791 | int nCell = 0; /* Number of cells in apCell[] */ |
| 4792 | int nMaxCells = 0; /* Allocated size of apCell, szCell, aFrom. */ |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 4793 | int nOld; /* Number of pages in apOld[] */ |
| 4794 | int nNew; /* Number of pages in apNew[] */ |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 4795 | int nDiv; /* Number of cells in apDiv[] */ |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 4796 | int i, j, k; /* Loop counters */ |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 4797 | int idx; /* Index of pPage in pParent->aCell[] */ |
| 4798 | int nxDiv; /* Next divider slot in pParent->aCell[] */ |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 4799 | int rc; /* The return code */ |
drh | 9102529 | 2004-05-03 19:49:32 +0000 | [diff] [blame] | 4800 | int leafCorrection; /* 4 if pPage is a leaf. 0 if not */ |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 4801 | int leafData; /* True if pPage is a leaf of a LEAFDATA tree */ |
drh | 9102529 | 2004-05-03 19:49:32 +0000 | [diff] [blame] | 4802 | int usableSpace; /* Bytes in pPage beyond the header */ |
| 4803 | int pageFlags; /* Value of pPage->aData[0] */ |
drh | 6019e16 | 2001-07-02 17:51:45 +0000 | [diff] [blame] | 4804 | int subtotal; /* Subtotal of bytes in cells on one page */ |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 4805 | int iSpace = 0; /* First unused byte of aSpace[] */ |
drh | c3b7057 | 2003-01-04 19:44:07 +0000 | [diff] [blame] | 4806 | MemPage *apOld[NB]; /* pPage and up to two siblings */ |
| 4807 | Pgno pgnoOld[NB]; /* Page numbers for each page in apOld[] */ |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 4808 | MemPage *apCopy[NB]; /* Private copies of apOld[] pages */ |
drh | a2fce64 | 2004-06-05 00:01:44 +0000 | [diff] [blame] | 4809 | MemPage *apNew[NB+2]; /* pPage and up to NB siblings after balancing */ |
| 4810 | Pgno pgnoNew[NB+2]; /* Page numbers for each page in apNew[] */ |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 4811 | u8 *apDiv[NB]; /* Divider cells in pParent */ |
drh | a2fce64 | 2004-06-05 00:01:44 +0000 | [diff] [blame] | 4812 | int cntNew[NB+2]; /* Index in aCell[] of cell after i-th page */ |
| 4813 | int szNew[NB+2]; /* Combined size of cells place on i-th page */ |
danielk1977 | 50f059b | 2005-03-29 02:54:03 +0000 | [diff] [blame] | 4814 | u8 **apCell = 0; /* All cells begin balanced */ |
drh | a9121e4 | 2008-02-19 14:59:35 +0000 | [diff] [blame] | 4815 | u16 *szCell; /* Local size of all cells in apCell[] */ |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 4816 | u8 *aCopy[NB]; /* Space for holding data of apCopy[] */ |
| 4817 | u8 *aSpace; /* Space to hold copies of dividers cells */ |
danielk1977 | 4e17d14 | 2005-01-16 09:06:33 +0000 | [diff] [blame] | 4818 | #ifndef SQLITE_OMIT_AUTOVACUUM |
danielk1977 | ac11ee6 | 2005-01-15 12:45:51 +0000 | [diff] [blame] | 4819 | u8 *aFrom = 0; |
| 4820 | #endif |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 4821 | |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 4822 | assert( sqlite3_mutex_held(pPage->pBt->mutex) ); |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 4823 | |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 4824 | /* |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4825 | ** Find the parent page. |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 4826 | */ |
drh | 3a4c141 | 2004-05-09 20:40:11 +0000 | [diff] [blame] | 4827 | assert( pPage->isInit ); |
danielk1977 | 6e465eb | 2007-08-21 13:11:00 +0000 | [diff] [blame] | 4828 | assert( sqlite3PagerIswriteable(pPage->pDbPage) || pPage->nOverflow==1 ); |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 4829 | pBt = pPage->pBt; |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 4830 | pParent = pPage->pParent; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4831 | assert( pParent ); |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 4832 | if( SQLITE_OK!=(rc = sqlite3PagerWrite(pParent->pDbPage)) ){ |
danielk1977 | 07cb560 | 2006-01-20 10:55:05 +0000 | [diff] [blame] | 4833 | return rc; |
| 4834 | } |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4835 | TRACE(("BALANCE: begin page %d child of %d\n", pPage->pgno, pParent->pgno)); |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 4836 | |
drh | 615ae55 | 2005-01-16 23:21:00 +0000 | [diff] [blame] | 4837 | #ifndef SQLITE_OMIT_QUICKBALANCE |
drh | f222e71 | 2005-01-14 22:55:49 +0000 | [diff] [blame] | 4838 | /* |
| 4839 | ** A special case: If a new entry has just been inserted into a |
| 4840 | ** 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] | 4841 | ** 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] | 4842 | ** largest key) then use the special balance_quick() routine for |
| 4843 | ** balancing. balance_quick() is much faster and results in a tighter |
| 4844 | ** packing of data in the common case. |
| 4845 | */ |
danielk1977 | ac245ec | 2005-01-14 13:50:11 +0000 | [diff] [blame] | 4846 | if( pPage->leaf && |
| 4847 | pPage->intKey && |
| 4848 | pPage->leafData && |
| 4849 | pPage->nOverflow==1 && |
| 4850 | pPage->aOvfl[0].idx==pPage->nCell && |
danielk1977 | ac11ee6 | 2005-01-15 12:45:51 +0000 | [diff] [blame] | 4851 | pPage->pParent->pgno!=1 && |
danielk1977 | ac245ec | 2005-01-14 13:50:11 +0000 | [diff] [blame] | 4852 | get4byte(&pParent->aData[pParent->hdrOffset+8])==pPage->pgno |
| 4853 | ){ |
danielk1977 | ac11ee6 | 2005-01-15 12:45:51 +0000 | [diff] [blame] | 4854 | /* |
| 4855 | ** TODO: Check the siblings to the left of pPage. It may be that |
| 4856 | ** they are not full and no new page is required. |
| 4857 | */ |
danielk1977 | ac245ec | 2005-01-14 13:50:11 +0000 | [diff] [blame] | 4858 | return balance_quick(pPage, pParent); |
| 4859 | } |
| 4860 | #endif |
| 4861 | |
danielk1977 | 6e465eb | 2007-08-21 13:11:00 +0000 | [diff] [blame] | 4862 | if( SQLITE_OK!=(rc = sqlite3PagerWrite(pPage->pDbPage)) ){ |
| 4863 | return rc; |
| 4864 | } |
| 4865 | |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 4866 | /* |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 4867 | ** Find the cell in the parent page whose left child points back |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 4868 | ** to pPage. The "idx" variable is the index of that cell. If pPage |
| 4869 | ** is the rightmost child of pParent then set idx to pParent->nCell |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 4870 | */ |
drh | bb49aba | 2003-01-04 18:53:27 +0000 | [diff] [blame] | 4871 | if( pParent->idxShift ){ |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 4872 | Pgno pgno; |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 4873 | pgno = pPage->pgno; |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 4874 | assert( pgno==sqlite3PagerPagenumber(pPage->pDbPage) ); |
drh | bb49aba | 2003-01-04 18:53:27 +0000 | [diff] [blame] | 4875 | for(idx=0; idx<pParent->nCell; idx++){ |
danielk1977 | 1cc5ed8 | 2007-05-16 17:28:43 +0000 | [diff] [blame] | 4876 | if( get4byte(findCell(pParent, idx))==pgno ){ |
drh | bb49aba | 2003-01-04 18:53:27 +0000 | [diff] [blame] | 4877 | break; |
| 4878 | } |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 4879 | } |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 4880 | assert( idx<pParent->nCell |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4881 | || get4byte(&pParent->aData[pParent->hdrOffset+8])==pgno ); |
drh | bb49aba | 2003-01-04 18:53:27 +0000 | [diff] [blame] | 4882 | }else{ |
| 4883 | idx = pPage->idxParent; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 4884 | } |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 4885 | |
| 4886 | /* |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 4887 | ** Initialize variables so that it will be safe to jump |
drh | 5edc312 | 2001-09-13 21:53:09 +0000 | [diff] [blame] | 4888 | ** directly to balance_cleanup at any moment. |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 4889 | */ |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 4890 | nOld = nNew = 0; |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 4891 | sqlite3PagerRef(pParent->pDbPage); |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 4892 | |
| 4893 | /* |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 4894 | ** Find sibling pages to pPage and the cells in pParent that divide |
drh | c3b7057 | 2003-01-04 19:44:07 +0000 | [diff] [blame] | 4895 | ** the siblings. An attempt is made to find NN siblings on either |
| 4896 | ** side of pPage. More siblings are taken from one side, however, if |
| 4897 | ** pPage there are fewer than NN siblings on the other side. If pParent |
| 4898 | ** has NB or fewer children then all children of pParent are taken. |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 4899 | */ |
drh | c3b7057 | 2003-01-04 19:44:07 +0000 | [diff] [blame] | 4900 | nxDiv = idx - NN; |
| 4901 | if( nxDiv + NB > pParent->nCell ){ |
| 4902 | nxDiv = pParent->nCell - NB + 1; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 4903 | } |
drh | c3b7057 | 2003-01-04 19:44:07 +0000 | [diff] [blame] | 4904 | if( nxDiv<0 ){ |
| 4905 | nxDiv = 0; |
| 4906 | } |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 4907 | nDiv = 0; |
drh | c3b7057 | 2003-01-04 19:44:07 +0000 | [diff] [blame] | 4908 | for(i=0, k=nxDiv; i<NB; i++, k++){ |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 4909 | if( k<pParent->nCell ){ |
danielk1977 | 1cc5ed8 | 2007-05-16 17:28:43 +0000 | [diff] [blame] | 4910 | apDiv[i] = findCell(pParent, k); |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 4911 | nDiv++; |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 4912 | assert( !pParent->leaf ); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4913 | pgnoOld[i] = get4byte(apDiv[i]); |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 4914 | }else if( k==pParent->nCell ){ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4915 | pgnoOld[i] = get4byte(&pParent->aData[pParent->hdrOffset+8]); |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 4916 | }else{ |
| 4917 | break; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 4918 | } |
drh | de64713 | 2004-05-07 17:57:49 +0000 | [diff] [blame] | 4919 | rc = getAndInitPage(pBt, pgnoOld[i], &apOld[i], pParent); |
drh | 6019e16 | 2001-07-02 17:51:45 +0000 | [diff] [blame] | 4920 | if( rc ) goto balance_cleanup; |
drh | 428ae8c | 2003-01-04 16:48:09 +0000 | [diff] [blame] | 4921 | apOld[i]->idxParent = k; |
drh | 9102529 | 2004-05-03 19:49:32 +0000 | [diff] [blame] | 4922 | apCopy[i] = 0; |
| 4923 | assert( i==nOld ); |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 4924 | nOld++; |
danielk1977 | 634f298 | 2005-03-28 08:44:07 +0000 | [diff] [blame] | 4925 | nMaxCells += 1+apOld[i]->nCell+apOld[i]->nOverflow; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 4926 | } |
| 4927 | |
drh | a9121e4 | 2008-02-19 14:59:35 +0000 | [diff] [blame] | 4928 | /* Make nMaxCells a multiple of 4 in order to preserve 8-byte |
drh | 8d97f1f | 2005-05-05 18:14:13 +0000 | [diff] [blame] | 4929 | ** alignment */ |
drh | a9121e4 | 2008-02-19 14:59:35 +0000 | [diff] [blame] | 4930 | nMaxCells = (nMaxCells + 3)&~3; |
drh | 8d97f1f | 2005-05-05 18:14:13 +0000 | [diff] [blame] | 4931 | |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 4932 | /* |
danielk1977 | 634f298 | 2005-03-28 08:44:07 +0000 | [diff] [blame] | 4933 | ** Allocate space for memory structures |
| 4934 | */ |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 4935 | apCell = sqlite3_malloc( |
drh | a9121e4 | 2008-02-19 14:59:35 +0000 | [diff] [blame] | 4936 | nMaxCells*sizeof(u8*) /* apCell */ |
| 4937 | + nMaxCells*sizeof(u16) /* szCell */ |
| 4938 | + (ROUND8(sizeof(MemPage))+pBt->pageSize)*NB /* aCopy */ |
| 4939 | + pBt->pageSize*5 /* aSpace */ |
| 4940 | + (ISAUTOVACUUM ? nMaxCells : 0) /* aFrom */ |
danielk1977 | 634f298 | 2005-03-28 08:44:07 +0000 | [diff] [blame] | 4941 | ); |
| 4942 | if( apCell==0 ){ |
| 4943 | rc = SQLITE_NOMEM; |
| 4944 | goto balance_cleanup; |
| 4945 | } |
drh | a9121e4 | 2008-02-19 14:59:35 +0000 | [diff] [blame] | 4946 | szCell = (u16*)&apCell[nMaxCells]; |
danielk1977 | 634f298 | 2005-03-28 08:44:07 +0000 | [diff] [blame] | 4947 | aCopy[0] = (u8*)&szCell[nMaxCells]; |
drh | c96d853 | 2005-05-03 12:30:33 +0000 | [diff] [blame] | 4948 | assert( ((aCopy[0] - (u8*)apCell) & 7)==0 ); /* 8-byte alignment required */ |
danielk1977 | 634f298 | 2005-03-28 08:44:07 +0000 | [diff] [blame] | 4949 | for(i=1; i<NB; i++){ |
drh | c96d853 | 2005-05-03 12:30:33 +0000 | [diff] [blame] | 4950 | aCopy[i] = &aCopy[i-1][pBt->pageSize+ROUND8(sizeof(MemPage))]; |
| 4951 | assert( ((aCopy[i] - (u8*)apCell) & 7)==0 ); /* 8-byte alignment required */ |
danielk1977 | 634f298 | 2005-03-28 08:44:07 +0000 | [diff] [blame] | 4952 | } |
drh | c96d853 | 2005-05-03 12:30:33 +0000 | [diff] [blame] | 4953 | aSpace = &aCopy[NB-1][pBt->pageSize+ROUND8(sizeof(MemPage))]; |
| 4954 | assert( ((aSpace - (u8*)apCell) & 7)==0 ); /* 8-byte alignment required */ |
danielk1977 | 634f298 | 2005-03-28 08:44:07 +0000 | [diff] [blame] | 4955 | #ifndef SQLITE_OMIT_AUTOVACUUM |
| 4956 | if( pBt->autoVacuum ){ |
drh | 07d183d | 2005-05-01 22:52:42 +0000 | [diff] [blame] | 4957 | aFrom = &aSpace[5*pBt->pageSize]; |
danielk1977 | 634f298 | 2005-03-28 08:44:07 +0000 | [diff] [blame] | 4958 | } |
| 4959 | #endif |
| 4960 | |
| 4961 | /* |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 4962 | ** Make copies of the content of pPage and its siblings into aOld[]. |
| 4963 | ** The rest of this function will use data from the copies rather |
| 4964 | ** that the original pages since the original pages will be in the |
| 4965 | ** process of being overwritten. |
| 4966 | */ |
| 4967 | for(i=0; i<nOld; i++){ |
drh | bf4bca5 | 2007-09-06 22:19:14 +0000 | [diff] [blame] | 4968 | MemPage *p = apCopy[i] = (MemPage*)aCopy[i]; |
| 4969 | memcpy(p, apOld[i], sizeof(MemPage)); |
| 4970 | p->aData = (void*)&p[1]; |
| 4971 | memcpy(p->aData, apOld[i]->aData, pBt->pageSize); |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 4972 | } |
| 4973 | |
| 4974 | /* |
| 4975 | ** Load pointers to all cells on sibling pages and the divider cells |
| 4976 | ** into the local apCell[] array. Make copies of the divider cells |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 4977 | ** into space obtained form aSpace[] and remove the the divider Cells |
| 4978 | ** from pParent. |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 4979 | ** |
| 4980 | ** If the siblings are on leaf pages, then the child pointers of the |
| 4981 | ** divider cells are stripped from the cells before they are copied |
drh | 96f5b76 | 2004-05-16 16:24:36 +0000 | [diff] [blame] | 4982 | ** into aSpace[]. In this way, all cells in apCell[] are without |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 4983 | ** child pointers. If siblings are not leaves, then all cell in |
| 4984 | ** apCell[] include child pointers. Either way, all cells in apCell[] |
| 4985 | ** are alike. |
drh | 96f5b76 | 2004-05-16 16:24:36 +0000 | [diff] [blame] | 4986 | ** |
| 4987 | ** leafCorrection: 4 if pPage is a leaf. 0 if pPage is not a leaf. |
| 4988 | ** leafData: 1 if pPage holds key+data and pParent holds only keys. |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 4989 | */ |
| 4990 | nCell = 0; |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 4991 | leafCorrection = pPage->leaf*4; |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 4992 | leafData = pPage->leafData && pPage->leaf; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 4993 | for(i=0; i<nOld; i++){ |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 4994 | MemPage *pOld = apCopy[i]; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4995 | int limit = pOld->nCell+pOld->nOverflow; |
| 4996 | for(j=0; j<limit; j++){ |
danielk1977 | 634f298 | 2005-03-28 08:44:07 +0000 | [diff] [blame] | 4997 | assert( nCell<nMaxCells ); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 4998 | apCell[nCell] = findOverflowCell(pOld, j); |
| 4999 | szCell[nCell] = cellSizePtr(pOld, apCell[nCell]); |
danielk1977 | ac11ee6 | 2005-01-15 12:45:51 +0000 | [diff] [blame] | 5000 | #ifndef SQLITE_OMIT_AUTOVACUUM |
| 5001 | if( pBt->autoVacuum ){ |
| 5002 | int a; |
| 5003 | aFrom[nCell] = i; |
| 5004 | for(a=0; a<pOld->nOverflow; a++){ |
| 5005 | if( pOld->aOvfl[a].pCell==apCell[nCell] ){ |
| 5006 | aFrom[nCell] = 0xFF; |
| 5007 | break; |
| 5008 | } |
| 5009 | } |
| 5010 | } |
| 5011 | #endif |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 5012 | nCell++; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 5013 | } |
| 5014 | if( i<nOld-1 ){ |
drh | a9121e4 | 2008-02-19 14:59:35 +0000 | [diff] [blame] | 5015 | u16 sz = cellSizePtr(pParent, apDiv[i]); |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 5016 | if( leafData ){ |
drh | 96f5b76 | 2004-05-16 16:24:36 +0000 | [diff] [blame] | 5017 | /* With the LEAFDATA flag, pParent cells hold only INTKEYs that |
| 5018 | ** are duplicates of keys on the child pages. We need to remove |
| 5019 | ** the divider cells from pParent, but the dividers cells are not |
| 5020 | ** added to apCell[] because they are duplicates of child cells. |
| 5021 | */ |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 5022 | dropCell(pParent, nxDiv, sz); |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 5023 | }else{ |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 5024 | u8 *pTemp; |
danielk1977 | 634f298 | 2005-03-28 08:44:07 +0000 | [diff] [blame] | 5025 | assert( nCell<nMaxCells ); |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 5026 | szCell[nCell] = sz; |
| 5027 | pTemp = &aSpace[iSpace]; |
| 5028 | iSpace += sz; |
drh | 07d183d | 2005-05-01 22:52:42 +0000 | [diff] [blame] | 5029 | assert( iSpace<=pBt->pageSize*5 ); |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 5030 | memcpy(pTemp, apDiv[i], sz); |
| 5031 | apCell[nCell] = pTemp+leafCorrection; |
danielk1977 | ac11ee6 | 2005-01-15 12:45:51 +0000 | [diff] [blame] | 5032 | #ifndef SQLITE_OMIT_AUTOVACUUM |
| 5033 | if( pBt->autoVacuum ){ |
| 5034 | aFrom[nCell] = 0xFF; |
| 5035 | } |
| 5036 | #endif |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 5037 | dropCell(pParent, nxDiv, sz); |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 5038 | szCell[nCell] -= leafCorrection; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5039 | assert( get4byte(pTemp)==pgnoOld[i] ); |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 5040 | if( !pOld->leaf ){ |
| 5041 | assert( leafCorrection==0 ); |
| 5042 | /* The right pointer of the child page pOld becomes the left |
| 5043 | ** pointer of the divider cell */ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5044 | memcpy(apCell[nCell], &pOld->aData[pOld->hdrOffset+8], 4); |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 5045 | }else{ |
| 5046 | assert( leafCorrection==4 ); |
danielk1977 | 39c9604 | 2007-05-12 10:41:47 +0000 | [diff] [blame] | 5047 | if( szCell[nCell]<4 ){ |
| 5048 | /* Do not allow any cells smaller than 4 bytes. */ |
| 5049 | szCell[nCell] = 4; |
| 5050 | } |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 5051 | } |
| 5052 | nCell++; |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 5053 | } |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 5054 | } |
| 5055 | } |
| 5056 | |
| 5057 | /* |
drh | 6019e16 | 2001-07-02 17:51:45 +0000 | [diff] [blame] | 5058 | ** Figure out the number of pages needed to hold all nCell cells. |
| 5059 | ** Store this number in "k". Also compute szNew[] which is the total |
| 5060 | ** 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] | 5061 | ** in apCell[] of the cell that divides page i from page i+1. |
drh | 6019e16 | 2001-07-02 17:51:45 +0000 | [diff] [blame] | 5062 | ** cntNew[k] should equal nCell. |
| 5063 | ** |
drh | 96f5b76 | 2004-05-16 16:24:36 +0000 | [diff] [blame] | 5064 | ** Values computed by this block: |
| 5065 | ** |
| 5066 | ** k: The total number of sibling pages |
| 5067 | ** szNew[i]: Spaced used on the i-th sibling page. |
| 5068 | ** cntNew[i]: Index in apCell[] and szCell[] for the first cell to |
| 5069 | ** the right of the i-th sibling page. |
| 5070 | ** usableSpace: Number of bytes of space available on each sibling. |
| 5071 | ** |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 5072 | */ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5073 | usableSpace = pBt->usableSize - 12 + leafCorrection; |
drh | 6019e16 | 2001-07-02 17:51:45 +0000 | [diff] [blame] | 5074 | for(subtotal=k=i=0; i<nCell; i++){ |
danielk1977 | 634f298 | 2005-03-28 08:44:07 +0000 | [diff] [blame] | 5075 | assert( i<nMaxCells ); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5076 | subtotal += szCell[i] + 2; |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 5077 | if( subtotal > usableSpace ){ |
drh | 6019e16 | 2001-07-02 17:51:45 +0000 | [diff] [blame] | 5078 | szNew[k] = subtotal - szCell[i]; |
| 5079 | cntNew[k] = i; |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 5080 | if( leafData ){ i--; } |
drh | 6019e16 | 2001-07-02 17:51:45 +0000 | [diff] [blame] | 5081 | subtotal = 0; |
| 5082 | k++; |
| 5083 | } |
| 5084 | } |
| 5085 | szNew[k] = subtotal; |
| 5086 | cntNew[k] = nCell; |
| 5087 | k++; |
drh | 96f5b76 | 2004-05-16 16:24:36 +0000 | [diff] [blame] | 5088 | |
| 5089 | /* |
| 5090 | ** The packing computed by the previous block is biased toward the siblings |
| 5091 | ** on the left side. The left siblings are always nearly full, while the |
| 5092 | ** right-most sibling might be nearly empty. This block of code attempts |
| 5093 | ** to adjust the packing of siblings to get a better balance. |
| 5094 | ** |
| 5095 | ** This adjustment is more than an optimization. The packing above might |
| 5096 | ** be so out of balance as to be illegal. For example, the right-most |
| 5097 | ** sibling might be completely empty. This adjustment is not optional. |
| 5098 | */ |
drh | 6019e16 | 2001-07-02 17:51:45 +0000 | [diff] [blame] | 5099 | for(i=k-1; i>0; i--){ |
drh | 96f5b76 | 2004-05-16 16:24:36 +0000 | [diff] [blame] | 5100 | int szRight = szNew[i]; /* Size of sibling on the right */ |
| 5101 | int szLeft = szNew[i-1]; /* Size of sibling on the left */ |
| 5102 | int r; /* Index of right-most cell in left sibling */ |
| 5103 | int d; /* Index of first cell to the left of right sibling */ |
| 5104 | |
| 5105 | r = cntNew[i-1] - 1; |
| 5106 | d = r + 1 - leafData; |
danielk1977 | 634f298 | 2005-03-28 08:44:07 +0000 | [diff] [blame] | 5107 | assert( d<nMaxCells ); |
| 5108 | assert( r<nMaxCells ); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5109 | while( szRight==0 || szRight+szCell[d]+2<=szLeft-(szCell[r]+2) ){ |
| 5110 | szRight += szCell[d] + 2; |
| 5111 | szLeft -= szCell[r] + 2; |
drh | 6019e16 | 2001-07-02 17:51:45 +0000 | [diff] [blame] | 5112 | cntNew[i-1]--; |
drh | 96f5b76 | 2004-05-16 16:24:36 +0000 | [diff] [blame] | 5113 | r = cntNew[i-1] - 1; |
| 5114 | d = r + 1 - leafData; |
drh | 6019e16 | 2001-07-02 17:51:45 +0000 | [diff] [blame] | 5115 | } |
drh | 96f5b76 | 2004-05-16 16:24:36 +0000 | [diff] [blame] | 5116 | szNew[i] = szRight; |
| 5117 | szNew[i-1] = szLeft; |
drh | 6019e16 | 2001-07-02 17:51:45 +0000 | [diff] [blame] | 5118 | } |
drh | 09d0deb | 2005-08-02 17:13:09 +0000 | [diff] [blame] | 5119 | |
| 5120 | /* Either we found one or more cells (cntnew[0])>0) or we are the |
| 5121 | ** a virtual root page. A virtual root page is when the real root |
| 5122 | ** page is page 1 and we are the only child of that page. |
| 5123 | */ |
| 5124 | assert( cntNew[0]>0 || (pParent->pgno==1 && pParent->nCell==0) ); |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 5125 | |
| 5126 | /* |
drh | 6b30867 | 2002-07-08 02:16:37 +0000 | [diff] [blame] | 5127 | ** Allocate k new pages. Reuse old pages where possible. |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 5128 | */ |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 5129 | assert( pPage->pgno>1 ); |
| 5130 | pageFlags = pPage->aData[0]; |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 5131 | for(i=0; i<k; i++){ |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 5132 | MemPage *pNew; |
drh | 6b30867 | 2002-07-08 02:16:37 +0000 | [diff] [blame] | 5133 | if( i<nOld ){ |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 5134 | pNew = apNew[i] = apOld[i]; |
drh | 6b30867 | 2002-07-08 02:16:37 +0000 | [diff] [blame] | 5135 | pgnoNew[i] = pgnoOld[i]; |
| 5136 | apOld[i] = 0; |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 5137 | rc = sqlite3PagerWrite(pNew->pDbPage); |
drh | f534544 | 2007-04-09 12:45:02 +0000 | [diff] [blame] | 5138 | nNew++; |
danielk1977 | 2812956 | 2005-01-11 10:25:06 +0000 | [diff] [blame] | 5139 | if( rc ) goto balance_cleanup; |
drh | 6b30867 | 2002-07-08 02:16:37 +0000 | [diff] [blame] | 5140 | }else{ |
drh | 7aa8f85 | 2006-03-28 00:24:44 +0000 | [diff] [blame] | 5141 | assert( i>0 ); |
drh | 4f0c587 | 2007-03-26 22:05:01 +0000 | [diff] [blame] | 5142 | rc = allocateBtreePage(pBt, &pNew, &pgnoNew[i], pgnoNew[i-1], 0); |
drh | 6b30867 | 2002-07-08 02:16:37 +0000 | [diff] [blame] | 5143 | if( rc ) goto balance_cleanup; |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 5144 | apNew[i] = pNew; |
drh | f534544 | 2007-04-09 12:45:02 +0000 | [diff] [blame] | 5145 | nNew++; |
drh | 6b30867 | 2002-07-08 02:16:37 +0000 | [diff] [blame] | 5146 | } |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 5147 | zeroPage(pNew, pageFlags); |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 5148 | } |
| 5149 | |
danielk1977 | 299b187 | 2004-11-22 10:02:10 +0000 | [diff] [blame] | 5150 | /* Free any old pages that were not reused as new pages. |
| 5151 | */ |
| 5152 | while( i<nOld ){ |
| 5153 | rc = freePage(apOld[i]); |
| 5154 | if( rc ) goto balance_cleanup; |
| 5155 | releasePage(apOld[i]); |
| 5156 | apOld[i] = 0; |
| 5157 | i++; |
| 5158 | } |
| 5159 | |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 5160 | /* |
drh | f9ffac9 | 2002-03-02 19:00:31 +0000 | [diff] [blame] | 5161 | ** Put the new pages in accending order. This helps to |
| 5162 | ** keep entries in the disk file in order so that a scan |
| 5163 | ** of the table is a linear scan through the file. That |
| 5164 | ** in turn helps the operating system to deliver pages |
| 5165 | ** from the disk more rapidly. |
| 5166 | ** |
| 5167 | ** An O(n^2) insertion sort algorithm is used, but since |
drh | c3b7057 | 2003-01-04 19:44:07 +0000 | [diff] [blame] | 5168 | ** n is never more than NB (a small constant), that should |
| 5169 | ** not be a problem. |
drh | f9ffac9 | 2002-03-02 19:00:31 +0000 | [diff] [blame] | 5170 | ** |
drh | c3b7057 | 2003-01-04 19:44:07 +0000 | [diff] [blame] | 5171 | ** When NB==3, this one optimization makes the database |
| 5172 | ** about 25% faster for large insertions and deletions. |
drh | f9ffac9 | 2002-03-02 19:00:31 +0000 | [diff] [blame] | 5173 | */ |
| 5174 | for(i=0; i<k-1; i++){ |
| 5175 | int minV = pgnoNew[i]; |
| 5176 | int minI = i; |
| 5177 | for(j=i+1; j<k; j++){ |
drh | 7d02cb7 | 2003-06-04 16:24:39 +0000 | [diff] [blame] | 5178 | if( pgnoNew[j]<(unsigned)minV ){ |
drh | f9ffac9 | 2002-03-02 19:00:31 +0000 | [diff] [blame] | 5179 | minI = j; |
| 5180 | minV = pgnoNew[j]; |
| 5181 | } |
| 5182 | } |
| 5183 | if( minI>i ){ |
| 5184 | int t; |
| 5185 | MemPage *pT; |
| 5186 | t = pgnoNew[i]; |
| 5187 | pT = apNew[i]; |
| 5188 | pgnoNew[i] = pgnoNew[minI]; |
| 5189 | apNew[i] = apNew[minI]; |
| 5190 | pgnoNew[minI] = t; |
| 5191 | apNew[minI] = pT; |
| 5192 | } |
| 5193 | } |
drh | a2fce64 | 2004-06-05 00:01:44 +0000 | [diff] [blame] | 5194 | 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] | 5195 | pgnoOld[0], |
| 5196 | nOld>=2 ? pgnoOld[1] : 0, |
| 5197 | nOld>=3 ? pgnoOld[2] : 0, |
drh | 10c0fa6 | 2004-05-18 12:50:17 +0000 | [diff] [blame] | 5198 | pgnoNew[0], szNew[0], |
| 5199 | nNew>=2 ? pgnoNew[1] : 0, nNew>=2 ? szNew[1] : 0, |
| 5200 | nNew>=3 ? pgnoNew[2] : 0, nNew>=3 ? szNew[2] : 0, |
drh | a2fce64 | 2004-06-05 00:01:44 +0000 | [diff] [blame] | 5201 | nNew>=4 ? pgnoNew[3] : 0, nNew>=4 ? szNew[3] : 0, |
| 5202 | nNew>=5 ? pgnoNew[4] : 0, nNew>=5 ? szNew[4] : 0)); |
drh | 24cd67e | 2004-05-10 16:18:47 +0000 | [diff] [blame] | 5203 | |
drh | f9ffac9 | 2002-03-02 19:00:31 +0000 | [diff] [blame] | 5204 | /* |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 5205 | ** Evenly distribute the data in apCell[] across the new pages. |
| 5206 | ** Insert divider cells into pParent as necessary. |
| 5207 | */ |
| 5208 | j = 0; |
| 5209 | for(i=0; i<nNew; i++){ |
danielk1977 | ac11ee6 | 2005-01-15 12:45:51 +0000 | [diff] [blame] | 5210 | /* Assemble the new sibling page. */ |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 5211 | MemPage *pNew = apNew[i]; |
drh | 19642e5 | 2005-03-29 13:17:45 +0000 | [diff] [blame] | 5212 | assert( j<nMaxCells ); |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 5213 | assert( pNew->pgno==pgnoNew[i] ); |
drh | fa1a98a | 2004-05-14 19:08:17 +0000 | [diff] [blame] | 5214 | assemblePage(pNew, cntNew[i]-j, &apCell[j], &szCell[j]); |
drh | 09d0deb | 2005-08-02 17:13:09 +0000 | [diff] [blame] | 5215 | assert( pNew->nCell>0 || (nNew==1 && cntNew[0]==0) ); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5216 | assert( pNew->nOverflow==0 ); |
danielk1977 | ac11ee6 | 2005-01-15 12:45:51 +0000 | [diff] [blame] | 5217 | |
| 5218 | #ifndef SQLITE_OMIT_AUTOVACUUM |
| 5219 | /* If this is an auto-vacuum database, update the pointer map entries |
| 5220 | ** that point to the siblings that were rearranged. These can be: left |
| 5221 | ** children of cells, the right-child of the page, or overflow pages |
| 5222 | ** pointed to by cells. |
| 5223 | */ |
| 5224 | if( pBt->autoVacuum ){ |
| 5225 | for(k=j; k<cntNew[i]; k++){ |
danielk1977 | 634f298 | 2005-03-28 08:44:07 +0000 | [diff] [blame] | 5226 | assert( k<nMaxCells ); |
danielk1977 | ac11ee6 | 2005-01-15 12:45:51 +0000 | [diff] [blame] | 5227 | if( aFrom[k]==0xFF || apCopy[aFrom[k]]->pgno!=pNew->pgno ){ |
danielk1977 | 79a40da | 2005-01-16 08:00:01 +0000 | [diff] [blame] | 5228 | rc = ptrmapPutOvfl(pNew, k-j); |
| 5229 | if( rc!=SQLITE_OK ){ |
| 5230 | goto balance_cleanup; |
danielk1977 | ac11ee6 | 2005-01-15 12:45:51 +0000 | [diff] [blame] | 5231 | } |
| 5232 | } |
| 5233 | } |
| 5234 | } |
| 5235 | #endif |
| 5236 | |
| 5237 | j = cntNew[i]; |
| 5238 | |
| 5239 | /* If the sibling page assembled above was not the right-most sibling, |
| 5240 | ** insert a divider cell into the parent page. |
| 5241 | */ |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 5242 | if( i<nNew-1 && j<nCell ){ |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 5243 | u8 *pCell; |
drh | 24cd67e | 2004-05-10 16:18:47 +0000 | [diff] [blame] | 5244 | u8 *pTemp; |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 5245 | int sz; |
danielk1977 | 634f298 | 2005-03-28 08:44:07 +0000 | [diff] [blame] | 5246 | |
| 5247 | assert( j<nMaxCells ); |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 5248 | pCell = apCell[j]; |
| 5249 | sz = szCell[j] + leafCorrection; |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 5250 | if( !pNew->leaf ){ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5251 | memcpy(&pNew->aData[8], pCell, 4); |
drh | 24cd67e | 2004-05-10 16:18:47 +0000 | [diff] [blame] | 5252 | pTemp = 0; |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 5253 | }else if( leafData ){ |
drh | fd131da | 2007-08-07 17:13:03 +0000 | [diff] [blame] | 5254 | /* If the tree is a leaf-data tree, and the siblings are leaves, |
danielk1977 | ac11ee6 | 2005-01-15 12:45:51 +0000 | [diff] [blame] | 5255 | ** then there is no divider cell in apCell[]. Instead, the divider |
| 5256 | ** cell consists of the integer key for the right-most cell of |
| 5257 | ** the sibling-page assembled above only. |
| 5258 | */ |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 5259 | CellInfo info; |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 5260 | j--; |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 5261 | sqlite3BtreeParseCellPtr(pNew, apCell[j], &info); |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 5262 | pCell = &aSpace[iSpace]; |
drh | b026e05 | 2007-05-02 01:34:31 +0000 | [diff] [blame] | 5263 | fillInCell(pParent, pCell, 0, info.nKey, 0, 0, 0, &sz); |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 5264 | iSpace += sz; |
drh | 07d183d | 2005-05-01 22:52:42 +0000 | [diff] [blame] | 5265 | assert( iSpace<=pBt->pageSize*5 ); |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 5266 | pTemp = 0; |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 5267 | }else{ |
| 5268 | pCell -= 4; |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 5269 | pTemp = &aSpace[iSpace]; |
| 5270 | iSpace += sz; |
drh | 07d183d | 2005-05-01 22:52:42 +0000 | [diff] [blame] | 5271 | assert( iSpace<=pBt->pageSize*5 ); |
danielk1977 | 4aeff62 | 2007-05-12 09:30:47 +0000 | [diff] [blame] | 5272 | /* Obscure case for non-leaf-data trees: If the cell at pCell was |
drh | 85b623f | 2007-12-13 21:54:09 +0000 | [diff] [blame] | 5273 | ** previously stored on a leaf node, and its reported size was 4 |
danielk1977 | 4aeff62 | 2007-05-12 09:30:47 +0000 | [diff] [blame] | 5274 | ** bytes, then it may actually be smaller than this |
| 5275 | ** (see sqlite3BtreeParseCellPtr(), 4 bytes is the minimum size of |
drh | 85b623f | 2007-12-13 21:54:09 +0000 | [diff] [blame] | 5276 | ** any cell). But it is important to pass the correct size to |
danielk1977 | 4aeff62 | 2007-05-12 09:30:47 +0000 | [diff] [blame] | 5277 | ** insertCell(), so reparse the cell now. |
| 5278 | ** |
| 5279 | ** Note that this can never happen in an SQLite data file, as all |
| 5280 | ** cells are at least 4 bytes. It only happens in b-trees used |
| 5281 | ** to evaluate "IN (SELECT ...)" and similar clauses. |
| 5282 | */ |
| 5283 | if( szCell[j]==4 ){ |
| 5284 | assert(leafCorrection==4); |
| 5285 | sz = cellSizePtr(pParent, pCell); |
| 5286 | } |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 5287 | } |
danielk1977 | a3ad5e7 | 2005-01-07 08:56:44 +0000 | [diff] [blame] | 5288 | rc = insertCell(pParent, nxDiv, pCell, sz, pTemp, 4); |
danielk1977 | e80463b | 2004-11-03 03:01:16 +0000 | [diff] [blame] | 5289 | if( rc!=SQLITE_OK ) goto balance_cleanup; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5290 | put4byte(findOverflowCell(pParent,nxDiv), pNew->pgno); |
danielk1977 | ac11ee6 | 2005-01-15 12:45:51 +0000 | [diff] [blame] | 5291 | #ifndef SQLITE_OMIT_AUTOVACUUM |
| 5292 | /* If this is an auto-vacuum database, and not a leaf-data tree, |
| 5293 | ** then update the pointer map with an entry for the overflow page |
| 5294 | ** that the cell just inserted points to (if any). |
| 5295 | */ |
| 5296 | if( pBt->autoVacuum && !leafData ){ |
danielk1977 | 79a40da | 2005-01-16 08:00:01 +0000 | [diff] [blame] | 5297 | rc = ptrmapPutOvfl(pParent, nxDiv); |
| 5298 | if( rc!=SQLITE_OK ){ |
| 5299 | goto balance_cleanup; |
danielk1977 | ac11ee6 | 2005-01-15 12:45:51 +0000 | [diff] [blame] | 5300 | } |
| 5301 | } |
| 5302 | #endif |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 5303 | j++; |
| 5304 | nxDiv++; |
| 5305 | } |
| 5306 | } |
drh | 6019e16 | 2001-07-02 17:51:45 +0000 | [diff] [blame] | 5307 | assert( j==nCell ); |
drh | 7aa8f85 | 2006-03-28 00:24:44 +0000 | [diff] [blame] | 5308 | assert( nOld>0 ); |
| 5309 | assert( nNew>0 ); |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 5310 | if( (pageFlags & PTF_LEAF)==0 ){ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5311 | memcpy(&apNew[nNew-1]->aData[8], &apCopy[nOld-1]->aData[8], 4); |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 5312 | } |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5313 | if( nxDiv==pParent->nCell+pParent->nOverflow ){ |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 5314 | /* Right-most sibling is the right-most child of pParent */ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5315 | put4byte(&pParent->aData[pParent->hdrOffset+8], pgnoNew[nNew-1]); |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 5316 | }else{ |
| 5317 | /* Right-most sibling is the left child of the first entry in pParent |
| 5318 | ** past the right-most divider entry */ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5319 | put4byte(findOverflowCell(pParent, nxDiv), pgnoNew[nNew-1]); |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 5320 | } |
| 5321 | |
| 5322 | /* |
| 5323 | ** Reparent children of all cells. |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 5324 | */ |
| 5325 | for(i=0; i<nNew; i++){ |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 5326 | rc = reparentChildPages(apNew[i]); |
| 5327 | if( rc!=SQLITE_OK ) goto balance_cleanup; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 5328 | } |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 5329 | rc = reparentChildPages(pParent); |
| 5330 | if( rc!=SQLITE_OK ) goto balance_cleanup; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 5331 | |
| 5332 | /* |
drh | 3a4c141 | 2004-05-09 20:40:11 +0000 | [diff] [blame] | 5333 | ** Balance the parent page. Note that the current page (pPage) might |
danielk1977 | ac11ee6 | 2005-01-15 12:45:51 +0000 | [diff] [blame] | 5334 | ** have been added to the freelist so it might no longer be initialized. |
drh | 3a4c141 | 2004-05-09 20:40:11 +0000 | [diff] [blame] | 5335 | ** But the parent page will always be initialized. |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 5336 | */ |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 5337 | assert( pParent->isInit ); |
danielk1977 | ac245ec | 2005-01-14 13:50:11 +0000 | [diff] [blame] | 5338 | rc = balance(pParent, 0); |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 5339 | |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 5340 | /* |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 5341 | ** Cleanup before returning. |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 5342 | */ |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 5343 | balance_cleanup: |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 5344 | sqlite3_free(apCell); |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 5345 | for(i=0; i<nOld; i++){ |
drh | 9102529 | 2004-05-03 19:49:32 +0000 | [diff] [blame] | 5346 | releasePage(apOld[i]); |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 5347 | } |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 5348 | for(i=0; i<nNew; i++){ |
drh | 9102529 | 2004-05-03 19:49:32 +0000 | [diff] [blame] | 5349 | releasePage(apNew[i]); |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 5350 | } |
drh | 9102529 | 2004-05-03 19:49:32 +0000 | [diff] [blame] | 5351 | releasePage(pParent); |
drh | 3a4c141 | 2004-05-09 20:40:11 +0000 | [diff] [blame] | 5352 | TRACE(("BALANCE: finished with %d: old=%d new=%d cells=%d\n", |
| 5353 | pPage->pgno, nOld, nNew, nCell)); |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 5354 | return rc; |
| 5355 | } |
| 5356 | |
| 5357 | /* |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5358 | ** This routine is called for the root page of a btree when the root |
| 5359 | ** page contains no cells. This is an opportunity to make the tree |
| 5360 | ** shallower by one level. |
| 5361 | */ |
| 5362 | static int balance_shallower(MemPage *pPage){ |
| 5363 | MemPage *pChild; /* The only child page of pPage */ |
| 5364 | Pgno pgnoChild; /* Page number for pChild */ |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 5365 | int rc = SQLITE_OK; /* Return code from subprocedures */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 5366 | BtShared *pBt; /* The main BTree structure */ |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 5367 | int mxCellPerPage; /* Maximum number of cells per page */ |
| 5368 | u8 **apCell; /* All cells from pages being balanced */ |
drh | a9121e4 | 2008-02-19 14:59:35 +0000 | [diff] [blame] | 5369 | u16 *szCell; /* Local size of all cells */ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5370 | |
| 5371 | assert( pPage->pParent==0 ); |
| 5372 | assert( pPage->nCell==0 ); |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 5373 | assert( sqlite3_mutex_held(pPage->pBt->mutex) ); |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 5374 | pBt = pPage->pBt; |
| 5375 | mxCellPerPage = MX_CELL(pBt); |
drh | a9121e4 | 2008-02-19 14:59:35 +0000 | [diff] [blame] | 5376 | apCell = sqlite3_malloc( mxCellPerPage*(sizeof(u8*)+sizeof(u16)) ); |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 5377 | if( apCell==0 ) return SQLITE_NOMEM; |
drh | a9121e4 | 2008-02-19 14:59:35 +0000 | [diff] [blame] | 5378 | szCell = (u16*)&apCell[mxCellPerPage]; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5379 | if( pPage->leaf ){ |
| 5380 | /* The table is completely empty */ |
| 5381 | TRACE(("BALANCE: empty table %d\n", pPage->pgno)); |
| 5382 | }else{ |
| 5383 | /* The root page is empty but has one child. Transfer the |
| 5384 | ** information from that one child into the root page if it |
| 5385 | ** will fit. This reduces the depth of the tree by one. |
| 5386 | ** |
| 5387 | ** If the root page is page 1, it has less space available than |
| 5388 | ** its child (due to the 100 byte header that occurs at the beginning |
| 5389 | ** of the database fle), so it might not be able to hold all of the |
| 5390 | ** information currently contained in the child. If this is the |
| 5391 | ** case, then do not do the transfer. Leave page 1 empty except |
| 5392 | ** for the right-pointer to the child page. The child page becomes |
| 5393 | ** the virtual root of the tree. |
| 5394 | */ |
| 5395 | pgnoChild = get4byte(&pPage->aData[pPage->hdrOffset+8]); |
| 5396 | assert( pgnoChild>0 ); |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 5397 | assert( pgnoChild<=sqlite3PagerPagecount(pPage->pBt->pPager) ); |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 5398 | rc = sqlite3BtreeGetPage(pPage->pBt, pgnoChild, &pChild, 0); |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 5399 | if( rc ) goto end_shallow_balance; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5400 | if( pPage->pgno==1 ){ |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 5401 | rc = sqlite3BtreeInitPage(pChild, pPage); |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 5402 | if( rc ) goto end_shallow_balance; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5403 | assert( pChild->nOverflow==0 ); |
| 5404 | if( pChild->nFree>=100 ){ |
| 5405 | /* The child information will fit on the root page, so do the |
| 5406 | ** copy */ |
| 5407 | int i; |
| 5408 | zeroPage(pPage, pChild->aData[0]); |
| 5409 | for(i=0; i<pChild->nCell; i++){ |
danielk1977 | 1cc5ed8 | 2007-05-16 17:28:43 +0000 | [diff] [blame] | 5410 | apCell[i] = findCell(pChild,i); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5411 | szCell[i] = cellSizePtr(pChild, apCell[i]); |
| 5412 | } |
| 5413 | assemblePage(pPage, pChild->nCell, apCell, szCell); |
danielk1977 | ae82558 | 2004-11-23 09:06:55 +0000 | [diff] [blame] | 5414 | /* Copy the right-pointer of the child to the parent. */ |
| 5415 | put4byte(&pPage->aData[pPage->hdrOffset+8], |
| 5416 | get4byte(&pChild->aData[pChild->hdrOffset+8])); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5417 | freePage(pChild); |
| 5418 | TRACE(("BALANCE: child %d transfer to page 1\n", pChild->pgno)); |
| 5419 | }else{ |
| 5420 | /* The child has more information that will fit on the root. |
| 5421 | ** The tree is already balanced. Do nothing. */ |
| 5422 | TRACE(("BALANCE: child %d will not fit on page 1\n", pChild->pgno)); |
| 5423 | } |
| 5424 | }else{ |
| 5425 | memcpy(pPage->aData, pChild->aData, pPage->pBt->usableSize); |
| 5426 | pPage->isInit = 0; |
| 5427 | pPage->pParent = 0; |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 5428 | rc = sqlite3BtreeInitPage(pPage, 0); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5429 | assert( rc==SQLITE_OK ); |
| 5430 | freePage(pChild); |
| 5431 | TRACE(("BALANCE: transfer child %d into root %d\n", |
| 5432 | pChild->pgno, pPage->pgno)); |
| 5433 | } |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 5434 | rc = reparentChildPages(pPage); |
danielk1977 | ac11ee6 | 2005-01-15 12:45:51 +0000 | [diff] [blame] | 5435 | assert( pPage->nOverflow==0 ); |
| 5436 | #ifndef SQLITE_OMIT_AUTOVACUUM |
| 5437 | if( pBt->autoVacuum ){ |
danielk1977 | aac0a38 | 2005-01-16 11:07:06 +0000 | [diff] [blame] | 5438 | int i; |
danielk1977 | ac11ee6 | 2005-01-15 12:45:51 +0000 | [diff] [blame] | 5439 | for(i=0; i<pPage->nCell; i++){ |
danielk1977 | 79a40da | 2005-01-16 08:00:01 +0000 | [diff] [blame] | 5440 | rc = ptrmapPutOvfl(pPage, i); |
| 5441 | if( rc!=SQLITE_OK ){ |
| 5442 | goto end_shallow_balance; |
danielk1977 | ac11ee6 | 2005-01-15 12:45:51 +0000 | [diff] [blame] | 5443 | } |
| 5444 | } |
| 5445 | } |
| 5446 | #endif |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5447 | releasePage(pChild); |
| 5448 | } |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 5449 | end_shallow_balance: |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 5450 | sqlite3_free(apCell); |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 5451 | return rc; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5452 | } |
| 5453 | |
| 5454 | |
| 5455 | /* |
| 5456 | ** The root page is overfull |
| 5457 | ** |
| 5458 | ** When this happens, Create a new child page and copy the |
| 5459 | ** contents of the root into the child. Then make the root |
| 5460 | ** page an empty page with rightChild pointing to the new |
| 5461 | ** child. Finally, call balance_internal() on the new child |
| 5462 | ** to cause it to split. |
| 5463 | */ |
| 5464 | static int balance_deeper(MemPage *pPage){ |
| 5465 | int rc; /* Return value from subprocedures */ |
| 5466 | MemPage *pChild; /* Pointer to a new child page */ |
| 5467 | Pgno pgnoChild; /* Page number of the new child page */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 5468 | BtShared *pBt; /* The BTree */ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5469 | int usableSize; /* Total usable size of a page */ |
| 5470 | u8 *data; /* Content of the parent page */ |
| 5471 | u8 *cdata; /* Content of the child page */ |
| 5472 | int hdr; /* Offset to page header in parent */ |
| 5473 | int brk; /* Offset to content of first cell in parent */ |
| 5474 | |
| 5475 | assert( pPage->pParent==0 ); |
| 5476 | assert( pPage->nOverflow>0 ); |
| 5477 | pBt = pPage->pBt; |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 5478 | assert( sqlite3_mutex_held(pBt->mutex) ); |
drh | 4f0c587 | 2007-03-26 22:05:01 +0000 | [diff] [blame] | 5479 | rc = allocateBtreePage(pBt, &pChild, &pgnoChild, pPage->pgno, 0); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5480 | if( rc ) return rc; |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 5481 | assert( sqlite3PagerIswriteable(pChild->pDbPage) ); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5482 | usableSize = pBt->usableSize; |
| 5483 | data = pPage->aData; |
| 5484 | hdr = pPage->hdrOffset; |
| 5485 | brk = get2byte(&data[hdr+5]); |
| 5486 | cdata = pChild->aData; |
| 5487 | memcpy(cdata, &data[hdr], pPage->cellOffset+2*pPage->nCell-hdr); |
| 5488 | memcpy(&cdata[brk], &data[brk], usableSize-brk); |
danielk1977 | c7dc753 | 2004-11-17 10:22:03 +0000 | [diff] [blame] | 5489 | assert( pChild->isInit==0 ); |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 5490 | rc = sqlite3BtreeInitPage(pChild, pPage); |
danielk1977 | 6b456a2 | 2005-03-21 04:04:02 +0000 | [diff] [blame] | 5491 | if( rc ) goto balancedeeper_out; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5492 | memcpy(pChild->aOvfl, pPage->aOvfl, pPage->nOverflow*sizeof(pPage->aOvfl[0])); |
| 5493 | pChild->nOverflow = pPage->nOverflow; |
| 5494 | if( pChild->nOverflow ){ |
| 5495 | pChild->nFree = 0; |
| 5496 | } |
| 5497 | assert( pChild->nCell==pPage->nCell ); |
| 5498 | zeroPage(pPage, pChild->aData[0] & ~PTF_LEAF); |
| 5499 | put4byte(&pPage->aData[pPage->hdrOffset+8], pgnoChild); |
| 5500 | TRACE(("BALANCE: copy root %d into %d\n", pPage->pgno, pChild->pgno)); |
danielk1977 | 4e17d14 | 2005-01-16 09:06:33 +0000 | [diff] [blame] | 5501 | #ifndef SQLITE_OMIT_AUTOVACUUM |
danielk1977 | ac11ee6 | 2005-01-15 12:45:51 +0000 | [diff] [blame] | 5502 | if( pBt->autoVacuum ){ |
| 5503 | int i; |
| 5504 | rc = ptrmapPut(pBt, pChild->pgno, PTRMAP_BTREE, pPage->pgno); |
danielk1977 | 6b456a2 | 2005-03-21 04:04:02 +0000 | [diff] [blame] | 5505 | if( rc ) goto balancedeeper_out; |
danielk1977 | ac11ee6 | 2005-01-15 12:45:51 +0000 | [diff] [blame] | 5506 | for(i=0; i<pChild->nCell; i++){ |
danielk1977 | 79a40da | 2005-01-16 08:00:01 +0000 | [diff] [blame] | 5507 | rc = ptrmapPutOvfl(pChild, i); |
| 5508 | if( rc!=SQLITE_OK ){ |
| 5509 | return rc; |
danielk1977 | ac11ee6 | 2005-01-15 12:45:51 +0000 | [diff] [blame] | 5510 | } |
| 5511 | } |
| 5512 | } |
danielk1977 | 4e17d14 | 2005-01-16 09:06:33 +0000 | [diff] [blame] | 5513 | #endif |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5514 | rc = balance_nonroot(pChild); |
danielk1977 | 6b456a2 | 2005-03-21 04:04:02 +0000 | [diff] [blame] | 5515 | |
| 5516 | balancedeeper_out: |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5517 | releasePage(pChild); |
| 5518 | return rc; |
| 5519 | } |
| 5520 | |
| 5521 | /* |
| 5522 | ** Decide if the page pPage needs to be balanced. If balancing is |
| 5523 | ** required, call the appropriate balancing routine. |
| 5524 | */ |
danielk1977 | ac245ec | 2005-01-14 13:50:11 +0000 | [diff] [blame] | 5525 | static int balance(MemPage *pPage, int insert){ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5526 | int rc = SQLITE_OK; |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 5527 | assert( sqlite3_mutex_held(pPage->pBt->mutex) ); |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5528 | if( pPage->pParent==0 ){ |
danielk1977 | 6e465eb | 2007-08-21 13:11:00 +0000 | [diff] [blame] | 5529 | rc = sqlite3PagerWrite(pPage->pDbPage); |
| 5530 | if( rc==SQLITE_OK && pPage->nOverflow>0 ){ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5531 | rc = balance_deeper(pPage); |
| 5532 | } |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 5533 | if( rc==SQLITE_OK && pPage->nCell==0 ){ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5534 | rc = balance_shallower(pPage); |
| 5535 | } |
| 5536 | }else{ |
danielk1977 | ac245ec | 2005-01-14 13:50:11 +0000 | [diff] [blame] | 5537 | if( pPage->nOverflow>0 || |
| 5538 | (!insert && pPage->nFree>pPage->pBt->usableSize*2/3) ){ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5539 | rc = balance_nonroot(pPage); |
| 5540 | } |
| 5541 | } |
| 5542 | return rc; |
| 5543 | } |
| 5544 | |
| 5545 | /* |
drh | 8dcd7ca | 2004-08-08 19:43:29 +0000 | [diff] [blame] | 5546 | ** This routine checks all cursors that point to table pgnoRoot. |
drh | 980b1a7 | 2006-08-16 16:42:48 +0000 | [diff] [blame] | 5547 | ** If any of those cursors were opened with wrFlag==0 in a different |
| 5548 | ** database connection (a database connection that shares the pager |
| 5549 | ** cache with the current connection) and that other connection |
| 5550 | ** is not in the ReadUncommmitted state, then this routine returns |
| 5551 | ** SQLITE_LOCKED. |
danielk1977 | 299b187 | 2004-11-22 10:02:10 +0000 | [diff] [blame] | 5552 | ** |
| 5553 | ** In addition to checking for read-locks (where a read-lock |
| 5554 | ** means a cursor opened with wrFlag==0) this routine also moves |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 5555 | ** all write cursors so that they are pointing to the |
drh | 980b1a7 | 2006-08-16 16:42:48 +0000 | [diff] [blame] | 5556 | ** first Cell on the root page. This is necessary because an insert |
danielk1977 | 299b187 | 2004-11-22 10:02:10 +0000 | [diff] [blame] | 5557 | ** or delete might change the number of cells on a page or delete |
| 5558 | ** a page entirely and we do not want to leave any cursors |
| 5559 | ** pointing to non-existant pages or cells. |
drh | f74b8d9 | 2002-09-01 23:20:45 +0000 | [diff] [blame] | 5560 | */ |
drh | 980b1a7 | 2006-08-16 16:42:48 +0000 | [diff] [blame] | 5561 | static int checkReadLocks(Btree *pBtree, Pgno pgnoRoot, BtCursor *pExclude){ |
danielk1977 | 299b187 | 2004-11-22 10:02:10 +0000 | [diff] [blame] | 5562 | BtCursor *p; |
drh | 980b1a7 | 2006-08-16 16:42:48 +0000 | [diff] [blame] | 5563 | BtShared *pBt = pBtree->pBt; |
drh | e5fe690 | 2007-12-07 18:55:28 +0000 | [diff] [blame] | 5564 | sqlite3 *db = pBtree->db; |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 5565 | assert( sqlite3BtreeHoldsMutex(pBtree) ); |
danielk1977 | 299b187 | 2004-11-22 10:02:10 +0000 | [diff] [blame] | 5566 | for(p=pBt->pCursor; p; p=p->pNext){ |
drh | 980b1a7 | 2006-08-16 16:42:48 +0000 | [diff] [blame] | 5567 | if( p==pExclude ) continue; |
| 5568 | if( p->eState!=CURSOR_VALID ) continue; |
| 5569 | if( p->pgnoRoot!=pgnoRoot ) continue; |
| 5570 | if( p->wrFlag==0 ){ |
drh | e5fe690 | 2007-12-07 18:55:28 +0000 | [diff] [blame] | 5571 | sqlite3 *dbOther = p->pBtree->db; |
drh | 980b1a7 | 2006-08-16 16:42:48 +0000 | [diff] [blame] | 5572 | if( dbOther==0 || |
| 5573 | (dbOther!=db && (dbOther->flags & SQLITE_ReadUncommitted)==0) ){ |
| 5574 | return SQLITE_LOCKED; |
| 5575 | } |
| 5576 | }else if( p->pPage->pgno!=p->pgnoRoot ){ |
danielk1977 | 299b187 | 2004-11-22 10:02:10 +0000 | [diff] [blame] | 5577 | moveToRoot(p); |
| 5578 | } |
| 5579 | } |
drh | f74b8d9 | 2002-09-01 23:20:45 +0000 | [diff] [blame] | 5580 | return SQLITE_OK; |
| 5581 | } |
| 5582 | |
| 5583 | /* |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 5584 | ** Insert a new record into the BTree. The key is given by (pKey,nKey) |
| 5585 | ** 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] | 5586 | ** define what table the record should be inserted into. The cursor |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 5587 | ** is left pointing at a random location. |
| 5588 | ** |
| 5589 | ** For an INTKEY table, only the nKey value of the key is used. pKey is |
| 5590 | ** ignored. For a ZERODATA table, the pData and nData are both ignored. |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 5591 | */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 5592 | int sqlite3BtreeInsert( |
drh | 5c4d970 | 2001-08-20 00:33:58 +0000 | [diff] [blame] | 5593 | BtCursor *pCur, /* Insert data into the table of this cursor */ |
drh | 4a1c380 | 2004-05-12 15:15:47 +0000 | [diff] [blame] | 5594 | const void *pKey, i64 nKey, /* The key of the new record */ |
drh | e4d9081 | 2007-03-29 05:51:49 +0000 | [diff] [blame] | 5595 | const void *pData, int nData, /* The data of the new record */ |
drh | b026e05 | 2007-05-02 01:34:31 +0000 | [diff] [blame] | 5596 | int nZero, /* Number of extra 0 bytes to append to data */ |
drh | e4d9081 | 2007-03-29 05:51:49 +0000 | [diff] [blame] | 5597 | int appendBias /* True if this is likely an append */ |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 5598 | ){ |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 5599 | int rc; |
| 5600 | int loc; |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 5601 | int szNew; |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 5602 | MemPage *pPage; |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 5603 | Btree *p = pCur->pBtree; |
| 5604 | BtShared *pBt = p->pBt; |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 5605 | unsigned char *oldCell; |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 5606 | unsigned char *newCell = 0; |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 5607 | |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 5608 | assert( cursorHoldsMutex(pCur) ); |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 5609 | if( pBt->inTransaction!=TRANS_WRITE ){ |
drh | f74b8d9 | 2002-09-01 23:20:45 +0000 | [diff] [blame] | 5610 | /* Must start a transaction before doing an insert */ |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 5611 | rc = pBt->readOnly ? SQLITE_READONLY : SQLITE_ERROR; |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 5612 | return rc; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 5613 | } |
drh | f74b8d9 | 2002-09-01 23:20:45 +0000 | [diff] [blame] | 5614 | assert( !pBt->readOnly ); |
drh | ecdc753 | 2001-09-23 02:35:53 +0000 | [diff] [blame] | 5615 | if( !pCur->wrFlag ){ |
| 5616 | return SQLITE_PERM; /* Cursor not open for writing */ |
| 5617 | } |
drh | 980b1a7 | 2006-08-16 16:42:48 +0000 | [diff] [blame] | 5618 | if( checkReadLocks(pCur->pBtree, pCur->pgnoRoot, pCur) ){ |
drh | f74b8d9 | 2002-09-01 23:20:45 +0000 | [diff] [blame] | 5619 | return SQLITE_LOCKED; /* The table pCur points to has a read lock */ |
| 5620 | } |
drh | fb98264 | 2007-08-30 01:19:59 +0000 | [diff] [blame] | 5621 | if( pCur->eState==CURSOR_FAULT ){ |
| 5622 | return pCur->skip; |
| 5623 | } |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 5624 | |
| 5625 | /* Save the positions of any other cursors open on this table */ |
drh | bf700f3 | 2007-03-31 02:36:44 +0000 | [diff] [blame] | 5626 | clearCursorPosition(pCur); |
danielk1977 | 2e94d4d | 2006-01-09 05:36:27 +0000 | [diff] [blame] | 5627 | if( |
danielk1977 | 2e94d4d | 2006-01-09 05:36:27 +0000 | [diff] [blame] | 5628 | SQLITE_OK!=(rc = saveAllCursors(pBt, pCur->pgnoRoot, pCur)) || |
drh | e4d9081 | 2007-03-29 05:51:49 +0000 | [diff] [blame] | 5629 | SQLITE_OK!=(rc = sqlite3BtreeMoveto(pCur, pKey, nKey, appendBias, &loc)) |
danielk1977 | 2e94d4d | 2006-01-09 05:36:27 +0000 | [diff] [blame] | 5630 | ){ |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 5631 | return rc; |
| 5632 | } |
| 5633 | |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 5634 | pPage = pCur->pPage; |
drh | 4a1c380 | 2004-05-12 15:15:47 +0000 | [diff] [blame] | 5635 | assert( pPage->intKey || nKey>=0 ); |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 5636 | assert( pPage->leaf || !pPage->leafData ); |
drh | 3a4c141 | 2004-05-09 20:40:11 +0000 | [diff] [blame] | 5637 | TRACE(("INSERT: table=%d nkey=%lld ndata=%d page=%d %s\n", |
| 5638 | pCur->pgnoRoot, nKey, nData, pPage->pgno, |
| 5639 | loc==0 ? "overwrite" : "new entry")); |
drh | 7aa128d | 2002-06-21 13:09:16 +0000 | [diff] [blame] | 5640 | assert( pPage->isInit ); |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 5641 | newCell = sqlite3_malloc( MX_CELL_SIZE(pBt) ); |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 5642 | if( newCell==0 ) return SQLITE_NOMEM; |
drh | b026e05 | 2007-05-02 01:34:31 +0000 | [diff] [blame] | 5643 | rc = fillInCell(pPage, newCell, pKey, nKey, pData, nData, nZero, &szNew); |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 5644 | if( rc ) goto end_insert; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5645 | assert( szNew==cellSizePtr(pPage, newCell) ); |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 5646 | assert( szNew<=MX_CELL_SIZE(pBt) ); |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 5647 | if( loc==0 && CURSOR_VALID==pCur->eState ){ |
drh | a9121e4 | 2008-02-19 14:59:35 +0000 | [diff] [blame] | 5648 | u16 szOld; |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 5649 | assert( pCur->idx>=0 && pCur->idx<pPage->nCell ); |
danielk1977 | 6e465eb | 2007-08-21 13:11:00 +0000 | [diff] [blame] | 5650 | rc = sqlite3PagerWrite(pPage->pDbPage); |
| 5651 | if( rc ){ |
| 5652 | goto end_insert; |
| 5653 | } |
danielk1977 | 1cc5ed8 | 2007-05-16 17:28:43 +0000 | [diff] [blame] | 5654 | oldCell = findCell(pPage, pCur->idx); |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 5655 | if( !pPage->leaf ){ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5656 | memcpy(newCell, oldCell, 4); |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 5657 | } |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5658 | szOld = cellSizePtr(pPage, oldCell); |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 5659 | rc = clearCell(pPage, oldCell); |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 5660 | if( rc ) goto end_insert; |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 5661 | dropCell(pPage, pCur->idx, szOld); |
drh | 7c717f7 | 2001-06-24 20:39:41 +0000 | [diff] [blame] | 5662 | }else if( loc<0 && pPage->nCell>0 ){ |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 5663 | assert( pPage->leaf ); |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 5664 | pCur->idx++; |
drh | 271efa5 | 2004-05-30 19:19:05 +0000 | [diff] [blame] | 5665 | pCur->info.nSize = 0; |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 5666 | }else{ |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 5667 | assert( pPage->leaf ); |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 5668 | } |
danielk1977 | a3ad5e7 | 2005-01-07 08:56:44 +0000 | [diff] [blame] | 5669 | rc = insertCell(pPage, pCur->idx, newCell, szNew, 0, 0); |
danielk1977 | e80463b | 2004-11-03 03:01:16 +0000 | [diff] [blame] | 5670 | if( rc!=SQLITE_OK ) goto end_insert; |
danielk1977 | ac245ec | 2005-01-14 13:50:11 +0000 | [diff] [blame] | 5671 | rc = balance(pPage, 1); |
drh | 23e11ca | 2004-05-04 17:27:28 +0000 | [diff] [blame] | 5672 | /* sqlite3BtreePageDump(pCur->pBt, pCur->pgnoRoot, 1); */ |
drh | 3fc190c | 2001-09-14 03:24:23 +0000 | [diff] [blame] | 5673 | /* fflush(stdout); */ |
danielk1977 | 299b187 | 2004-11-22 10:02:10 +0000 | [diff] [blame] | 5674 | if( rc==SQLITE_OK ){ |
| 5675 | moveToRoot(pCur); |
| 5676 | } |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 5677 | end_insert: |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 5678 | sqlite3_free(newCell); |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 5679 | return rc; |
| 5680 | } |
| 5681 | |
| 5682 | /* |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 5683 | ** Delete the entry that the cursor is pointing to. The cursor |
| 5684 | ** is left pointing at a random location. |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 5685 | */ |
drh | 3aac2dd | 2004-04-26 14:10:20 +0000 | [diff] [blame] | 5686 | int sqlite3BtreeDelete(BtCursor *pCur){ |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 5687 | MemPage *pPage = pCur->pPage; |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 5688 | unsigned char *pCell; |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 5689 | int rc; |
danielk1977 | cfe9a69 | 2004-06-16 12:00:29 +0000 | [diff] [blame] | 5690 | Pgno pgnoChild = 0; |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 5691 | Btree *p = pCur->pBtree; |
| 5692 | BtShared *pBt = p->pBt; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 5693 | |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 5694 | assert( cursorHoldsMutex(pCur) ); |
drh | 7aa128d | 2002-06-21 13:09:16 +0000 | [diff] [blame] | 5695 | assert( pPage->isInit ); |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 5696 | if( pBt->inTransaction!=TRANS_WRITE ){ |
drh | f74b8d9 | 2002-09-01 23:20:45 +0000 | [diff] [blame] | 5697 | /* Must start a transaction before doing a delete */ |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 5698 | rc = pBt->readOnly ? SQLITE_READONLY : SQLITE_ERROR; |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 5699 | return rc; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 5700 | } |
drh | f74b8d9 | 2002-09-01 23:20:45 +0000 | [diff] [blame] | 5701 | assert( !pBt->readOnly ); |
drh | fb98264 | 2007-08-30 01:19:59 +0000 | [diff] [blame] | 5702 | if( pCur->eState==CURSOR_FAULT ){ |
| 5703 | return pCur->skip; |
| 5704 | } |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 5705 | if( pCur->idx >= pPage->nCell ){ |
| 5706 | return SQLITE_ERROR; /* The cursor is not pointing to anything */ |
| 5707 | } |
drh | ecdc753 | 2001-09-23 02:35:53 +0000 | [diff] [blame] | 5708 | if( !pCur->wrFlag ){ |
| 5709 | return SQLITE_PERM; /* Did not open this cursor for writing */ |
| 5710 | } |
drh | 980b1a7 | 2006-08-16 16:42:48 +0000 | [diff] [blame] | 5711 | if( checkReadLocks(pCur->pBtree, pCur->pgnoRoot, pCur) ){ |
drh | f74b8d9 | 2002-09-01 23:20:45 +0000 | [diff] [blame] | 5712 | return SQLITE_LOCKED; /* The table pCur points to has a read lock */ |
| 5713 | } |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 5714 | |
| 5715 | /* Restore the current cursor position (a no-op if the cursor is not in |
| 5716 | ** CURSOR_REQUIRESEEK state) and save the positions of any other cursors |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 5717 | ** open on the same table. Then call sqlite3PagerWrite() on the page |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 5718 | ** that the entry will be deleted from. |
| 5719 | */ |
| 5720 | if( |
drh | bf700f3 | 2007-03-31 02:36:44 +0000 | [diff] [blame] | 5721 | (rc = restoreOrClearCursorPosition(pCur))!=0 || |
drh | d116739 | 2006-01-23 13:00:35 +0000 | [diff] [blame] | 5722 | (rc = saveAllCursors(pBt, pCur->pgnoRoot, pCur))!=0 || |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 5723 | (rc = sqlite3PagerWrite(pPage->pDbPage))!=0 |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 5724 | ){ |
| 5725 | return rc; |
| 5726 | } |
danielk1977 | e6efa74 | 2004-11-10 11:55:10 +0000 | [diff] [blame] | 5727 | |
drh | 85b623f | 2007-12-13 21:54:09 +0000 | [diff] [blame] | 5728 | /* Locate the cell within its page and leave pCell pointing to the |
danielk1977 | e6efa74 | 2004-11-10 11:55:10 +0000 | [diff] [blame] | 5729 | ** data. The clearCell() call frees any overflow pages associated with the |
| 5730 | ** cell. The cell itself is still intact. |
| 5731 | */ |
danielk1977 | 1cc5ed8 | 2007-05-16 17:28:43 +0000 | [diff] [blame] | 5732 | pCell = findCell(pPage, pCur->idx); |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 5733 | if( !pPage->leaf ){ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5734 | pgnoChild = get4byte(pCell); |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 5735 | } |
danielk1977 | 2812956 | 2005-01-11 10:25:06 +0000 | [diff] [blame] | 5736 | rc = clearCell(pPage, pCell); |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 5737 | if( rc ){ |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 5738 | return rc; |
| 5739 | } |
danielk1977 | e6efa74 | 2004-11-10 11:55:10 +0000 | [diff] [blame] | 5740 | |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 5741 | if( !pPage->leaf ){ |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 5742 | /* |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 5743 | ** 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] | 5744 | ** do something we will leave a hole on an internal page. |
| 5745 | ** We have to fill the hole by moving in a cell from a leaf. The |
| 5746 | ** next Cell after the one to be deleted is guaranteed to exist and |
danielk1977 | 299b187 | 2004-11-22 10:02:10 +0000 | [diff] [blame] | 5747 | ** to be a leaf so we can use it. |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 5748 | */ |
drh | 14acc04 | 2001-06-10 19:56:58 +0000 | [diff] [blame] | 5749 | BtCursor leafCur; |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 5750 | unsigned char *pNext; |
danielk1977 | 299b187 | 2004-11-22 10:02:10 +0000 | [diff] [blame] | 5751 | int notUsed; |
danielk1977 | 6b456a2 | 2005-03-21 04:04:02 +0000 | [diff] [blame] | 5752 | unsigned char *tempCell = 0; |
drh | 8b18dd4 | 2004-05-12 19:18:15 +0000 | [diff] [blame] | 5753 | assert( !pPage->leafData ); |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 5754 | sqlite3BtreeGetTempCursor(pCur, &leafCur); |
danielk1977 | 299b187 | 2004-11-22 10:02:10 +0000 | [diff] [blame] | 5755 | rc = sqlite3BtreeNext(&leafCur, ¬Used); |
danielk1977 | 6b456a2 | 2005-03-21 04:04:02 +0000 | [diff] [blame] | 5756 | if( rc==SQLITE_OK ){ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 5757 | rc = sqlite3PagerWrite(leafCur.pPage->pDbPage); |
danielk1977 | 6b456a2 | 2005-03-21 04:04:02 +0000 | [diff] [blame] | 5758 | } |
| 5759 | if( rc==SQLITE_OK ){ |
drh | a9121e4 | 2008-02-19 14:59:35 +0000 | [diff] [blame] | 5760 | u16 szNext; |
danielk1977 | 6b456a2 | 2005-03-21 04:04:02 +0000 | [diff] [blame] | 5761 | TRACE(("DELETE: table=%d delete internal from %d replace from leaf %d\n", |
| 5762 | pCur->pgnoRoot, pPage->pgno, leafCur.pPage->pgno)); |
| 5763 | dropCell(pPage, pCur->idx, cellSizePtr(pPage, pCell)); |
danielk1977 | 1cc5ed8 | 2007-05-16 17:28:43 +0000 | [diff] [blame] | 5764 | pNext = findCell(leafCur.pPage, leafCur.idx); |
danielk1977 | 6b456a2 | 2005-03-21 04:04:02 +0000 | [diff] [blame] | 5765 | szNext = cellSizePtr(leafCur.pPage, pNext); |
| 5766 | assert( MX_CELL_SIZE(pBt)>=szNext+4 ); |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 5767 | tempCell = sqlite3_malloc( MX_CELL_SIZE(pBt) ); |
danielk1977 | 6b456a2 | 2005-03-21 04:04:02 +0000 | [diff] [blame] | 5768 | if( tempCell==0 ){ |
| 5769 | rc = SQLITE_NOMEM; |
| 5770 | } |
danielk1977 | 8ea1cfa | 2008-01-01 06:19:02 +0000 | [diff] [blame] | 5771 | if( rc==SQLITE_OK ){ |
| 5772 | rc = insertCell(pPage, pCur->idx, pNext-4, szNext+4, tempCell, 0); |
| 5773 | } |
| 5774 | if( rc==SQLITE_OK ){ |
| 5775 | put4byte(findOverflowCell(pPage, pCur->idx), pgnoChild); |
| 5776 | rc = balance(pPage, 0); |
| 5777 | } |
| 5778 | if( rc==SQLITE_OK ){ |
| 5779 | dropCell(leafCur.pPage, leafCur.idx, szNext); |
| 5780 | rc = balance(leafCur.pPage, 0); |
| 5781 | } |
danielk1977 | 6b456a2 | 2005-03-21 04:04:02 +0000 | [diff] [blame] | 5782 | } |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 5783 | sqlite3_free(tempCell); |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 5784 | sqlite3BtreeReleaseTempCursor(&leafCur); |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 5785 | }else{ |
danielk1977 | 299b187 | 2004-11-22 10:02:10 +0000 | [diff] [blame] | 5786 | TRACE(("DELETE: table=%d delete from leaf %d\n", |
| 5787 | pCur->pgnoRoot, pPage->pgno)); |
| 5788 | dropCell(pPage, pCur->idx, cellSizePtr(pPage, pCell)); |
danielk1977 | ac245ec | 2005-01-14 13:50:11 +0000 | [diff] [blame] | 5789 | rc = balance(pPage, 0); |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 5790 | } |
danielk1977 | 6b456a2 | 2005-03-21 04:04:02 +0000 | [diff] [blame] | 5791 | if( rc==SQLITE_OK ){ |
| 5792 | moveToRoot(pCur); |
| 5793 | } |
drh | 5e2f8b9 | 2001-05-28 00:41:15 +0000 | [diff] [blame] | 5794 | return rc; |
drh | 3b7511c | 2001-05-26 13:15:44 +0000 | [diff] [blame] | 5795 | } |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 5796 | |
| 5797 | /* |
drh | c6b52df | 2002-01-04 03:09:29 +0000 | [diff] [blame] | 5798 | ** Create a new BTree table. Write into *piTable the page |
| 5799 | ** number for the root page of the new table. |
| 5800 | ** |
drh | ab01f61 | 2004-05-22 02:55:23 +0000 | [diff] [blame] | 5801 | ** The type of type is determined by the flags parameter. Only the |
| 5802 | ** following values of flags are currently in use. Other values for |
| 5803 | ** flags might not work: |
| 5804 | ** |
| 5805 | ** BTREE_INTKEY|BTREE_LEAFDATA Used for SQL tables with rowid keys |
| 5806 | ** BTREE_ZERODATA Used for SQL indices |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 5807 | */ |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 5808 | static int btreeCreateTable(Btree *p, int *piTable, int flags){ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 5809 | BtShared *pBt = p->pBt; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 5810 | MemPage *pRoot; |
| 5811 | Pgno pgnoRoot; |
| 5812 | int rc; |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 5813 | |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 5814 | assert( sqlite3BtreeHoldsMutex(p) ); |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 5815 | if( pBt->inTransaction!=TRANS_WRITE ){ |
drh | f74b8d9 | 2002-09-01 23:20:45 +0000 | [diff] [blame] | 5816 | /* Must start a transaction first */ |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 5817 | rc = pBt->readOnly ? SQLITE_READONLY : SQLITE_ERROR; |
| 5818 | return rc; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 5819 | } |
danielk1977 | 2812956 | 2005-01-11 10:25:06 +0000 | [diff] [blame] | 5820 | assert( !pBt->readOnly ); |
danielk1977 | e6efa74 | 2004-11-10 11:55:10 +0000 | [diff] [blame] | 5821 | |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 5822 | #ifdef SQLITE_OMIT_AUTOVACUUM |
drh | 4f0c587 | 2007-03-26 22:05:01 +0000 | [diff] [blame] | 5823 | rc = allocateBtreePage(pBt, &pRoot, &pgnoRoot, 1, 0); |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 5824 | if( rc ){ |
| 5825 | return rc; |
| 5826 | } |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 5827 | #else |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 5828 | if( pBt->autoVacuum ){ |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 5829 | Pgno pgnoMove; /* Move a page here to make room for the root-page */ |
| 5830 | MemPage *pPageMove; /* The page to move to. */ |
| 5831 | |
danielk1977 | 20713f3 | 2007-05-03 11:43:33 +0000 | [diff] [blame] | 5832 | /* Creating a new table may probably require moving an existing database |
| 5833 | ** to make room for the new tables root page. In case this page turns |
| 5834 | ** out to be an overflow page, delete all overflow page-map caches |
| 5835 | ** held by open cursors. |
| 5836 | */ |
danielk1977 | 92d4d7a | 2007-05-04 12:05:56 +0000 | [diff] [blame] | 5837 | invalidateAllOverflowCache(pBt); |
danielk1977 | 20713f3 | 2007-05-03 11:43:33 +0000 | [diff] [blame] | 5838 | |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 5839 | /* Read the value of meta[3] from the database to determine where the |
| 5840 | ** root page of the new table should go. meta[3] is the largest root-page |
| 5841 | ** created so far, so the new root-page is (meta[3]+1). |
| 5842 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 5843 | rc = sqlite3BtreeGetMeta(p, 4, &pgnoRoot); |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 5844 | if( rc!=SQLITE_OK ){ |
| 5845 | return rc; |
| 5846 | } |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 5847 | pgnoRoot++; |
| 5848 | |
danielk1977 | 599fcba | 2004-11-08 07:13:13 +0000 | [diff] [blame] | 5849 | /* The new root-page may not be allocated on a pointer-map page, or the |
| 5850 | ** PENDING_BYTE page. |
| 5851 | */ |
drh | 7219043 | 2008-01-31 14:54:43 +0000 | [diff] [blame] | 5852 | while( pgnoRoot==PTRMAP_PAGENO(pBt, pgnoRoot) || |
danielk1977 | 599fcba | 2004-11-08 07:13:13 +0000 | [diff] [blame] | 5853 | pgnoRoot==PENDING_BYTE_PAGE(pBt) ){ |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 5854 | pgnoRoot++; |
| 5855 | } |
| 5856 | assert( pgnoRoot>=3 ); |
| 5857 | |
| 5858 | /* Allocate a page. The page that currently resides at pgnoRoot will |
| 5859 | ** be moved to the allocated page (unless the allocated page happens |
| 5860 | ** to reside at pgnoRoot). |
| 5861 | */ |
drh | 4f0c587 | 2007-03-26 22:05:01 +0000 | [diff] [blame] | 5862 | rc = allocateBtreePage(pBt, &pPageMove, &pgnoMove, pgnoRoot, 1); |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 5863 | if( rc!=SQLITE_OK ){ |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 5864 | return rc; |
| 5865 | } |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 5866 | |
| 5867 | if( pgnoMove!=pgnoRoot ){ |
danielk1977 | f35843b | 2007-04-07 15:03:17 +0000 | [diff] [blame] | 5868 | /* pgnoRoot is the page that will be used for the root-page of |
| 5869 | ** the new table (assuming an error did not occur). But we were |
| 5870 | ** allocated pgnoMove. If required (i.e. if it was not allocated |
| 5871 | ** by extending the file), the current page at position pgnoMove |
| 5872 | ** is already journaled. |
| 5873 | */ |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 5874 | u8 eType; |
| 5875 | Pgno iPtrPage; |
| 5876 | |
| 5877 | releasePage(pPageMove); |
danielk1977 | f35843b | 2007-04-07 15:03:17 +0000 | [diff] [blame] | 5878 | |
| 5879 | /* Move the page currently at pgnoRoot to pgnoMove. */ |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 5880 | rc = sqlite3BtreeGetPage(pBt, pgnoRoot, &pRoot, 0); |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 5881 | if( rc!=SQLITE_OK ){ |
| 5882 | return rc; |
| 5883 | } |
| 5884 | rc = ptrmapGet(pBt, pgnoRoot, &eType, &iPtrPage); |
drh | ccae602 | 2005-02-26 17:31:26 +0000 | [diff] [blame] | 5885 | if( rc!=SQLITE_OK || eType==PTRMAP_ROOTPAGE || eType==PTRMAP_FREEPAGE ){ |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 5886 | releasePage(pRoot); |
| 5887 | return rc; |
| 5888 | } |
drh | ccae602 | 2005-02-26 17:31:26 +0000 | [diff] [blame] | 5889 | assert( eType!=PTRMAP_ROOTPAGE ); |
| 5890 | assert( eType!=PTRMAP_FREEPAGE ); |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 5891 | rc = sqlite3PagerWrite(pRoot->pDbPage); |
danielk1977 | 5fd057a | 2005-03-09 13:09:43 +0000 | [diff] [blame] | 5892 | if( rc!=SQLITE_OK ){ |
| 5893 | releasePage(pRoot); |
| 5894 | return rc; |
| 5895 | } |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 5896 | rc = relocatePage(pBt, pRoot, eType, iPtrPage, pgnoMove); |
| 5897 | releasePage(pRoot); |
danielk1977 | f35843b | 2007-04-07 15:03:17 +0000 | [diff] [blame] | 5898 | |
| 5899 | /* Obtain the page at pgnoRoot */ |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 5900 | if( rc!=SQLITE_OK ){ |
| 5901 | return rc; |
| 5902 | } |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 5903 | rc = sqlite3BtreeGetPage(pBt, pgnoRoot, &pRoot, 0); |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 5904 | if( rc!=SQLITE_OK ){ |
| 5905 | return rc; |
| 5906 | } |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 5907 | rc = sqlite3PagerWrite(pRoot->pDbPage); |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 5908 | if( rc!=SQLITE_OK ){ |
| 5909 | releasePage(pRoot); |
| 5910 | return rc; |
| 5911 | } |
| 5912 | }else{ |
| 5913 | pRoot = pPageMove; |
| 5914 | } |
| 5915 | |
danielk1977 | 42741be | 2005-01-08 12:42:39 +0000 | [diff] [blame] | 5916 | /* Update the pointer-map and meta-data with the new root-page number. */ |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 5917 | rc = ptrmapPut(pBt, pgnoRoot, PTRMAP_ROOTPAGE, 0); |
| 5918 | if( rc ){ |
| 5919 | releasePage(pRoot); |
| 5920 | return rc; |
| 5921 | } |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 5922 | rc = sqlite3BtreeUpdateMeta(p, 4, pgnoRoot); |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 5923 | if( rc ){ |
| 5924 | releasePage(pRoot); |
| 5925 | return rc; |
| 5926 | } |
danielk1977 | 42741be | 2005-01-08 12:42:39 +0000 | [diff] [blame] | 5927 | |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 5928 | }else{ |
drh | 4f0c587 | 2007-03-26 22:05:01 +0000 | [diff] [blame] | 5929 | rc = allocateBtreePage(pBt, &pRoot, &pgnoRoot, 1, 0); |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 5930 | if( rc ) return rc; |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 5931 | } |
| 5932 | #endif |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 5933 | assert( sqlite3PagerIswriteable(pRoot->pDbPage) ); |
drh | de64713 | 2004-05-07 17:57:49 +0000 | [diff] [blame] | 5934 | zeroPage(pRoot, flags | PTF_LEAF); |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 5935 | sqlite3PagerUnref(pRoot->pDbPage); |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 5936 | *piTable = (int)pgnoRoot; |
| 5937 | return SQLITE_OK; |
| 5938 | } |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 5939 | int sqlite3BtreeCreateTable(Btree *p, int *piTable, int flags){ |
| 5940 | int rc; |
| 5941 | sqlite3BtreeEnter(p); |
drh | e5fe690 | 2007-12-07 18:55:28 +0000 | [diff] [blame] | 5942 | p->pBt->db = p->db; |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 5943 | rc = btreeCreateTable(p, piTable, flags); |
| 5944 | sqlite3BtreeLeave(p); |
| 5945 | return rc; |
| 5946 | } |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 5947 | |
| 5948 | /* |
| 5949 | ** Erase the given database page and all its children. Return |
| 5950 | ** the page to the freelist. |
| 5951 | */ |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 5952 | static int clearDatabasePage( |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 5953 | BtShared *pBt, /* The BTree that contains the table */ |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 5954 | Pgno pgno, /* Page number to clear */ |
| 5955 | MemPage *pParent, /* Parent page. NULL for the root */ |
| 5956 | int freePageFlag /* Deallocate page if true */ |
| 5957 | ){ |
danielk1977 | 6b456a2 | 2005-03-21 04:04:02 +0000 | [diff] [blame] | 5958 | MemPage *pPage = 0; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 5959 | int rc; |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 5960 | unsigned char *pCell; |
| 5961 | int i; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 5962 | |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 5963 | assert( sqlite3_mutex_held(pBt->mutex) ); |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 5964 | if( pgno>sqlite3PagerPagecount(pBt->pPager) ){ |
drh | 4928570 | 2005-09-17 15:20:26 +0000 | [diff] [blame] | 5965 | return SQLITE_CORRUPT_BKPT; |
danielk1977 | a1cb183 | 2005-02-12 08:59:55 +0000 | [diff] [blame] | 5966 | } |
| 5967 | |
drh | de64713 | 2004-05-07 17:57:49 +0000 | [diff] [blame] | 5968 | rc = getAndInitPage(pBt, pgno, &pPage, pParent); |
danielk1977 | 6b456a2 | 2005-03-21 04:04:02 +0000 | [diff] [blame] | 5969 | if( rc ) goto cleardatabasepage_out; |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 5970 | for(i=0; i<pPage->nCell; i++){ |
danielk1977 | 1cc5ed8 | 2007-05-16 17:28:43 +0000 | [diff] [blame] | 5971 | pCell = findCell(pPage, i); |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 5972 | if( !pPage->leaf ){ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5973 | rc = clearDatabasePage(pBt, get4byte(pCell), pPage->pParent, 1); |
danielk1977 | 6b456a2 | 2005-03-21 04:04:02 +0000 | [diff] [blame] | 5974 | if( rc ) goto cleardatabasepage_out; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 5975 | } |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 5976 | rc = clearCell(pPage, pCell); |
danielk1977 | 6b456a2 | 2005-03-21 04:04:02 +0000 | [diff] [blame] | 5977 | if( rc ) goto cleardatabasepage_out; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 5978 | } |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 5979 | if( !pPage->leaf ){ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 5980 | rc = clearDatabasePage(pBt, get4byte(&pPage->aData[8]), pPage->pParent, 1); |
danielk1977 | 6b456a2 | 2005-03-21 04:04:02 +0000 | [diff] [blame] | 5981 | if( rc ) goto cleardatabasepage_out; |
drh | 2aa679f | 2001-06-25 02:11:07 +0000 | [diff] [blame] | 5982 | } |
| 5983 | if( freePageFlag ){ |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 5984 | rc = freePage(pPage); |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 5985 | }else if( (rc = sqlite3PagerWrite(pPage->pDbPage))==0 ){ |
drh | 3a4c141 | 2004-05-09 20:40:11 +0000 | [diff] [blame] | 5986 | zeroPage(pPage, pPage->aData[0] | PTF_LEAF); |
drh | 2aa679f | 2001-06-25 02:11:07 +0000 | [diff] [blame] | 5987 | } |
danielk1977 | 6b456a2 | 2005-03-21 04:04:02 +0000 | [diff] [blame] | 5988 | |
| 5989 | cleardatabasepage_out: |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 5990 | releasePage(pPage); |
drh | 2aa679f | 2001-06-25 02:11:07 +0000 | [diff] [blame] | 5991 | return rc; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 5992 | } |
| 5993 | |
| 5994 | /* |
drh | ab01f61 | 2004-05-22 02:55:23 +0000 | [diff] [blame] | 5995 | ** Delete all information from a single table in the database. iTable is |
| 5996 | ** the page number of the root of the table. After this routine returns, |
| 5997 | ** the root page is empty, but still exists. |
| 5998 | ** |
| 5999 | ** This routine will fail with SQLITE_LOCKED if there are any open |
| 6000 | ** read cursors on the table. Open write cursors are moved to the |
| 6001 | ** root of the table. |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 6002 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 6003 | int sqlite3BtreeClearTable(Btree *p, int iTable){ |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 6004 | int rc; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 6005 | BtShared *pBt = p->pBt; |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 6006 | sqlite3BtreeEnter(p); |
drh | e5fe690 | 2007-12-07 18:55:28 +0000 | [diff] [blame] | 6007 | pBt->db = p->db; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 6008 | if( p->inTrans!=TRANS_WRITE ){ |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 6009 | rc = pBt->readOnly ? SQLITE_READONLY : SQLITE_ERROR; |
| 6010 | }else if( (rc = checkReadLocks(p, iTable, 0))!=SQLITE_OK ){ |
| 6011 | /* nothing to do */ |
| 6012 | }else if( SQLITE_OK!=(rc = saveAllCursors(pBt, iTable, 0)) ){ |
| 6013 | /* nothing to do */ |
| 6014 | }else{ |
| 6015 | rc = clearDatabasePage(pBt, (Pgno)iTable, 0, 0); |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 6016 | } |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 6017 | sqlite3BtreeLeave(p); |
| 6018 | return rc; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 6019 | } |
| 6020 | |
| 6021 | /* |
| 6022 | ** Erase all information in a table and add the root of the table to |
| 6023 | ** the freelist. Except, the root of the principle table (the one on |
drh | ab01f61 | 2004-05-22 02:55:23 +0000 | [diff] [blame] | 6024 | ** page 1) is never added to the freelist. |
| 6025 | ** |
| 6026 | ** This routine will fail with SQLITE_LOCKED if there are any open |
| 6027 | ** cursors on the table. |
drh | 205f48e | 2004-11-05 00:43:11 +0000 | [diff] [blame] | 6028 | ** |
| 6029 | ** If AUTOVACUUM is enabled and the page at iTable is not the last |
| 6030 | ** root page in the database file, then the last root page |
| 6031 | ** in the database file is moved into the slot formerly occupied by |
| 6032 | ** iTable and that last slot formerly occupied by the last root page |
| 6033 | ** is added to the freelist instead of iTable. In this say, all |
| 6034 | ** root pages are kept at the beginning of the database file, which |
| 6035 | ** is necessary for AUTOVACUUM to work right. *piMoved is set to the |
| 6036 | ** page number that used to be the last root page in the file before |
| 6037 | ** the move. If no page gets moved, *piMoved is set to 0. |
| 6038 | ** The last root page is recorded in meta[3] and the value of |
| 6039 | ** meta[3] is updated by this procedure. |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 6040 | */ |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 6041 | static int btreeDropTable(Btree *p, int iTable, int *piMoved){ |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 6042 | int rc; |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 6043 | MemPage *pPage = 0; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 6044 | BtShared *pBt = p->pBt; |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 6045 | |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 6046 | assert( sqlite3BtreeHoldsMutex(p) ); |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 6047 | if( p->inTrans!=TRANS_WRITE ){ |
drh | f74b8d9 | 2002-09-01 23:20:45 +0000 | [diff] [blame] | 6048 | return pBt->readOnly ? SQLITE_READONLY : SQLITE_ERROR; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 6049 | } |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 6050 | |
danielk1977 | e6efa74 | 2004-11-10 11:55:10 +0000 | [diff] [blame] | 6051 | /* It is illegal to drop a table if any cursors are open on the |
| 6052 | ** database. This is because in auto-vacuum mode the backend may |
| 6053 | ** need to move another root-page to fill a gap left by the deleted |
| 6054 | ** root page. If an open cursor was using this page a problem would |
| 6055 | ** occur. |
| 6056 | */ |
| 6057 | if( pBt->pCursor ){ |
| 6058 | return SQLITE_LOCKED; |
drh | 5df72a5 | 2002-06-06 23:16:05 +0000 | [diff] [blame] | 6059 | } |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 6060 | |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 6061 | rc = sqlite3BtreeGetPage(pBt, (Pgno)iTable, &pPage, 0); |
drh | 2aa679f | 2001-06-25 02:11:07 +0000 | [diff] [blame] | 6062 | if( rc ) return rc; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 6063 | rc = sqlite3BtreeClearTable(p, iTable); |
danielk1977 | 6b456a2 | 2005-03-21 04:04:02 +0000 | [diff] [blame] | 6064 | if( rc ){ |
| 6065 | releasePage(pPage); |
| 6066 | return rc; |
| 6067 | } |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 6068 | |
drh | 205f48e | 2004-11-05 00:43:11 +0000 | [diff] [blame] | 6069 | *piMoved = 0; |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 6070 | |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 6071 | if( iTable>1 ){ |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 6072 | #ifdef SQLITE_OMIT_AUTOVACUUM |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 6073 | rc = freePage(pPage); |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 6074 | releasePage(pPage); |
| 6075 | #else |
| 6076 | if( pBt->autoVacuum ){ |
| 6077 | Pgno maxRootPgno; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 6078 | rc = sqlite3BtreeGetMeta(p, 4, &maxRootPgno); |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 6079 | if( rc!=SQLITE_OK ){ |
| 6080 | releasePage(pPage); |
| 6081 | return rc; |
| 6082 | } |
| 6083 | |
| 6084 | if( iTable==maxRootPgno ){ |
| 6085 | /* If the table being dropped is the table with the largest root-page |
| 6086 | ** number in the database, put the root page on the free list. |
| 6087 | */ |
| 6088 | rc = freePage(pPage); |
| 6089 | releasePage(pPage); |
| 6090 | if( rc!=SQLITE_OK ){ |
| 6091 | return rc; |
| 6092 | } |
| 6093 | }else{ |
| 6094 | /* The table being dropped does not have the largest root-page |
| 6095 | ** number in the database. So move the page that does into the |
| 6096 | ** gap left by the deleted root-page. |
| 6097 | */ |
| 6098 | MemPage *pMove; |
| 6099 | releasePage(pPage); |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 6100 | rc = sqlite3BtreeGetPage(pBt, maxRootPgno, &pMove, 0); |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 6101 | if( rc!=SQLITE_OK ){ |
| 6102 | return rc; |
| 6103 | } |
| 6104 | rc = relocatePage(pBt, pMove, PTRMAP_ROOTPAGE, 0, iTable); |
| 6105 | releasePage(pMove); |
| 6106 | if( rc!=SQLITE_OK ){ |
| 6107 | return rc; |
| 6108 | } |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 6109 | rc = sqlite3BtreeGetPage(pBt, maxRootPgno, &pMove, 0); |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 6110 | if( rc!=SQLITE_OK ){ |
| 6111 | return rc; |
| 6112 | } |
| 6113 | rc = freePage(pMove); |
| 6114 | releasePage(pMove); |
| 6115 | if( rc!=SQLITE_OK ){ |
| 6116 | return rc; |
| 6117 | } |
| 6118 | *piMoved = maxRootPgno; |
| 6119 | } |
| 6120 | |
danielk1977 | 599fcba | 2004-11-08 07:13:13 +0000 | [diff] [blame] | 6121 | /* Set the new 'max-root-page' value in the database header. This |
| 6122 | ** is the old value less one, less one more if that happens to |
| 6123 | ** be a root-page number, less one again if that is the |
| 6124 | ** PENDING_BYTE_PAGE. |
| 6125 | */ |
danielk1977 | 87a6e73 | 2004-11-05 12:58:25 +0000 | [diff] [blame] | 6126 | maxRootPgno--; |
danielk1977 | 599fcba | 2004-11-08 07:13:13 +0000 | [diff] [blame] | 6127 | if( maxRootPgno==PENDING_BYTE_PAGE(pBt) ){ |
| 6128 | maxRootPgno--; |
| 6129 | } |
danielk1977 | 266664d | 2006-02-10 08:24:21 +0000 | [diff] [blame] | 6130 | if( maxRootPgno==PTRMAP_PAGENO(pBt, maxRootPgno) ){ |
danielk1977 | 87a6e73 | 2004-11-05 12:58:25 +0000 | [diff] [blame] | 6131 | maxRootPgno--; |
| 6132 | } |
danielk1977 | 599fcba | 2004-11-08 07:13:13 +0000 | [diff] [blame] | 6133 | assert( maxRootPgno!=PENDING_BYTE_PAGE(pBt) ); |
| 6134 | |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 6135 | rc = sqlite3BtreeUpdateMeta(p, 4, maxRootPgno); |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 6136 | }else{ |
| 6137 | rc = freePage(pPage); |
| 6138 | releasePage(pPage); |
| 6139 | } |
| 6140 | #endif |
drh | 2aa679f | 2001-06-25 02:11:07 +0000 | [diff] [blame] | 6141 | }else{ |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 6142 | /* If sqlite3BtreeDropTable was called on page 1. */ |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 6143 | zeroPage(pPage, PTF_INTKEY|PTF_LEAF ); |
danielk1977 | a0bf265 | 2004-11-04 14:30:04 +0000 | [diff] [blame] | 6144 | releasePage(pPage); |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 6145 | } |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 6146 | return rc; |
| 6147 | } |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 6148 | int sqlite3BtreeDropTable(Btree *p, int iTable, int *piMoved){ |
| 6149 | int rc; |
| 6150 | sqlite3BtreeEnter(p); |
drh | e5fe690 | 2007-12-07 18:55:28 +0000 | [diff] [blame] | 6151 | p->pBt->db = p->db; |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 6152 | rc = btreeDropTable(p, iTable, piMoved); |
| 6153 | sqlite3BtreeLeave(p); |
| 6154 | return rc; |
| 6155 | } |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 6156 | |
drh | 001bbcb | 2003-03-19 03:14:00 +0000 | [diff] [blame] | 6157 | |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 6158 | /* |
drh | 23e11ca | 2004-05-04 17:27:28 +0000 | [diff] [blame] | 6159 | ** Read the meta-information out of a database file. Meta[0] |
| 6160 | ** is the number of free pages currently in the database. Meta[1] |
drh | a3b321d | 2004-05-11 09:31:31 +0000 | [diff] [blame] | 6161 | ** through meta[15] are available for use by higher layers. Meta[0] |
| 6162 | ** is read-only, the others are read/write. |
| 6163 | ** |
| 6164 | ** The schema layer numbers meta values differently. At the schema |
| 6165 | ** layer (and the SetCookie and ReadCookie opcodes) the number of |
| 6166 | ** 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] | 6167 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 6168 | int sqlite3BtreeGetMeta(Btree *p, int idx, u32 *pMeta){ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 6169 | DbPage *pDbPage; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 6170 | int rc; |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 6171 | unsigned char *pP1; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 6172 | BtShared *pBt = p->pBt; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 6173 | |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 6174 | sqlite3BtreeEnter(p); |
drh | e5fe690 | 2007-12-07 18:55:28 +0000 | [diff] [blame] | 6175 | pBt->db = p->db; |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 6176 | |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 6177 | /* Reading a meta-data value requires a read-lock on page 1 (and hence |
| 6178 | ** the sqlite_master table. We grab this lock regardless of whether or |
| 6179 | ** not the SQLITE_ReadUncommitted flag is set (the table rooted at page |
| 6180 | ** 1 is treated as a special case by queryTableLock() and lockTable()). |
| 6181 | */ |
| 6182 | rc = queryTableLock(p, 1, READ_LOCK); |
| 6183 | if( rc!=SQLITE_OK ){ |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 6184 | sqlite3BtreeLeave(p); |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 6185 | return rc; |
| 6186 | } |
| 6187 | |
drh | 23e11ca | 2004-05-04 17:27:28 +0000 | [diff] [blame] | 6188 | assert( idx>=0 && idx<=15 ); |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 6189 | rc = sqlite3PagerGet(pBt->pPager, 1, &pDbPage); |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 6190 | if( rc ){ |
| 6191 | sqlite3BtreeLeave(p); |
| 6192 | return rc; |
| 6193 | } |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 6194 | pP1 = (unsigned char *)sqlite3PagerGetData(pDbPage); |
drh | 23e11ca | 2004-05-04 17:27:28 +0000 | [diff] [blame] | 6195 | *pMeta = get4byte(&pP1[36 + idx*4]); |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 6196 | sqlite3PagerUnref(pDbPage); |
drh | ae15787 | 2004-08-14 19:20:09 +0000 | [diff] [blame] | 6197 | |
danielk1977 | 599fcba | 2004-11-08 07:13:13 +0000 | [diff] [blame] | 6198 | /* If autovacuumed is disabled in this build but we are trying to |
| 6199 | ** access an autovacuumed database, then make the database readonly. |
| 6200 | */ |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 6201 | #ifdef SQLITE_OMIT_AUTOVACUUM |
drh | ae15787 | 2004-08-14 19:20:09 +0000 | [diff] [blame] | 6202 | if( idx==4 && *pMeta>0 ) pBt->readOnly = 1; |
danielk1977 | 003ba06 | 2004-11-04 02:57:33 +0000 | [diff] [blame] | 6203 | #endif |
drh | ae15787 | 2004-08-14 19:20:09 +0000 | [diff] [blame] | 6204 | |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 6205 | /* Grab the read-lock on page 1. */ |
| 6206 | rc = lockTable(p, 1, READ_LOCK); |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 6207 | sqlite3BtreeLeave(p); |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 6208 | return rc; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 6209 | } |
| 6210 | |
| 6211 | /* |
drh | 23e11ca | 2004-05-04 17:27:28 +0000 | [diff] [blame] | 6212 | ** Write meta-information back into the database. Meta[0] is |
| 6213 | ** read-only and may not be written. |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 6214 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 6215 | int sqlite3BtreeUpdateMeta(Btree *p, int idx, u32 iMeta){ |
| 6216 | BtShared *pBt = p->pBt; |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 6217 | unsigned char *pP1; |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 6218 | int rc; |
drh | 23e11ca | 2004-05-04 17:27:28 +0000 | [diff] [blame] | 6219 | assert( idx>=1 && idx<=15 ); |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 6220 | sqlite3BtreeEnter(p); |
drh | e5fe690 | 2007-12-07 18:55:28 +0000 | [diff] [blame] | 6221 | pBt->db = p->db; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 6222 | if( p->inTrans!=TRANS_WRITE ){ |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 6223 | rc = pBt->readOnly ? SQLITE_READONLY : SQLITE_ERROR; |
| 6224 | }else{ |
| 6225 | assert( pBt->pPage1!=0 ); |
| 6226 | pP1 = pBt->pPage1->aData; |
| 6227 | rc = sqlite3PagerWrite(pBt->pPage1->pDbPage); |
| 6228 | if( rc==SQLITE_OK ){ |
| 6229 | put4byte(&pP1[36 + idx*4], iMeta); |
danielk1977 | 4152e67 | 2007-09-12 17:01:45 +0000 | [diff] [blame] | 6230 | #ifndef SQLITE_OMIT_AUTOVACUUM |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 6231 | if( idx==7 ){ |
| 6232 | assert( pBt->autoVacuum || iMeta==0 ); |
| 6233 | assert( iMeta==0 || iMeta==1 ); |
| 6234 | pBt->incrVacuum = iMeta; |
| 6235 | } |
danielk1977 | 4152e67 | 2007-09-12 17:01:45 +0000 | [diff] [blame] | 6236 | #endif |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 6237 | } |
drh | 5df72a5 | 2002-06-06 23:16:05 +0000 | [diff] [blame] | 6238 | } |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 6239 | sqlite3BtreeLeave(p); |
| 6240 | return rc; |
drh | 8b2f49b | 2001-06-08 00:21:52 +0000 | [diff] [blame] | 6241 | } |
drh | 8c42ca9 | 2001-06-22 19:15:00 +0000 | [diff] [blame] | 6242 | |
drh | f328bc8 | 2004-05-10 23:29:49 +0000 | [diff] [blame] | 6243 | /* |
| 6244 | ** Return the flag byte at the beginning of the page that the cursor |
| 6245 | ** is currently pointing to. |
| 6246 | */ |
| 6247 | int sqlite3BtreeFlags(BtCursor *pCur){ |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 6248 | /* TODO: What about CURSOR_REQUIRESEEK state? Probably need to call |
drh | 777e4c4 | 2006-01-13 04:31:58 +0000 | [diff] [blame] | 6249 | ** restoreOrClearCursorPosition() here. |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 6250 | */ |
danielk1977 | e448dc4 | 2008-01-02 11:50:51 +0000 | [diff] [blame] | 6251 | MemPage *pPage; |
| 6252 | restoreOrClearCursorPosition(pCur); |
| 6253 | pPage = pCur->pPage; |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 6254 | assert( cursorHoldsMutex(pCur) ); |
drh | d0679ed | 2007-08-28 22:24:34 +0000 | [diff] [blame] | 6255 | assert( pPage->pBt==pCur->pBt ); |
drh | f328bc8 | 2004-05-10 23:29:49 +0000 | [diff] [blame] | 6256 | return pPage ? pPage->aData[pPage->hdrOffset] : 0; |
| 6257 | } |
| 6258 | |
drh | dd79342 | 2001-06-28 01:54:48 +0000 | [diff] [blame] | 6259 | |
drh | dd79342 | 2001-06-28 01:54:48 +0000 | [diff] [blame] | 6260 | /* |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6261 | ** Return the pager associated with a BTree. This routine is used for |
| 6262 | ** testing and debugging only. |
drh | dd79342 | 2001-06-28 01:54:48 +0000 | [diff] [blame] | 6263 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 6264 | Pager *sqlite3BtreePager(Btree *p){ |
| 6265 | return p->pBt->pPager; |
drh | dd79342 | 2001-06-28 01:54:48 +0000 | [diff] [blame] | 6266 | } |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6267 | |
drh | b7f9164 | 2004-10-31 02:22:47 +0000 | [diff] [blame] | 6268 | #ifndef SQLITE_OMIT_INTEGRITY_CHECK |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6269 | /* |
| 6270 | ** Append a message to the error message string. |
| 6271 | */ |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 6272 | static void checkAppendMsg( |
| 6273 | IntegrityCk *pCheck, |
| 6274 | char *zMsg1, |
| 6275 | const char *zFormat, |
| 6276 | ... |
| 6277 | ){ |
| 6278 | va_list ap; |
| 6279 | char *zMsg2; |
drh | 1dcdbc0 | 2007-01-27 02:24:54 +0000 | [diff] [blame] | 6280 | if( !pCheck->mxErr ) return; |
| 6281 | pCheck->mxErr--; |
| 6282 | pCheck->nErr++; |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 6283 | va_start(ap, zFormat); |
danielk1977 | 1e53695 | 2007-08-16 10:09:01 +0000 | [diff] [blame] | 6284 | zMsg2 = sqlite3VMPrintf(0, zFormat, ap); |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 6285 | va_end(ap); |
| 6286 | if( zMsg1==0 ) zMsg1 = ""; |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6287 | if( pCheck->zErrMsg ){ |
| 6288 | char *zOld = pCheck->zErrMsg; |
| 6289 | pCheck->zErrMsg = 0; |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 6290 | sqlite3SetString(&pCheck->zErrMsg, zOld, "\n", zMsg1, zMsg2, (char*)0); |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 6291 | sqlite3_free(zOld); |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6292 | }else{ |
danielk1977 | 4adee20 | 2004-05-08 08:23:19 +0000 | [diff] [blame] | 6293 | sqlite3SetString(&pCheck->zErrMsg, zMsg1, zMsg2, (char*)0); |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6294 | } |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 6295 | sqlite3_free(zMsg2); |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6296 | } |
drh | b7f9164 | 2004-10-31 02:22:47 +0000 | [diff] [blame] | 6297 | #endif /* SQLITE_OMIT_INTEGRITY_CHECK */ |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6298 | |
drh | b7f9164 | 2004-10-31 02:22:47 +0000 | [diff] [blame] | 6299 | #ifndef SQLITE_OMIT_INTEGRITY_CHECK |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6300 | /* |
| 6301 | ** Add 1 to the reference count for page iPage. If this is the second |
| 6302 | ** reference to the page, add an error message to pCheck->zErrMsg. |
| 6303 | ** Return 1 if there are 2 ore more references to the page and 0 if |
| 6304 | ** if this is the first reference to the page. |
| 6305 | ** |
| 6306 | ** Also check that the page number is in bounds. |
| 6307 | */ |
drh | aaab572 | 2002-02-19 13:39:21 +0000 | [diff] [blame] | 6308 | static int checkRef(IntegrityCk *pCheck, int iPage, char *zContext){ |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6309 | if( iPage==0 ) return 1; |
drh | 0de8c11 | 2002-07-06 16:32:14 +0000 | [diff] [blame] | 6310 | if( iPage>pCheck->nPage || iPage<0 ){ |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 6311 | checkAppendMsg(pCheck, zContext, "invalid page number %d", iPage); |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6312 | return 1; |
| 6313 | } |
| 6314 | if( pCheck->anRef[iPage]==1 ){ |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 6315 | checkAppendMsg(pCheck, zContext, "2nd reference to page %d", iPage); |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6316 | return 1; |
| 6317 | } |
| 6318 | return (pCheck->anRef[iPage]++)>1; |
| 6319 | } |
| 6320 | |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 6321 | #ifndef SQLITE_OMIT_AUTOVACUUM |
| 6322 | /* |
| 6323 | ** Check that the entry in the pointer-map for page iChild maps to |
| 6324 | ** page iParent, pointer type ptrType. If not, append an error message |
| 6325 | ** to pCheck. |
| 6326 | */ |
| 6327 | static void checkPtrmap( |
| 6328 | IntegrityCk *pCheck, /* Integrity check context */ |
| 6329 | Pgno iChild, /* Child page number */ |
| 6330 | u8 eType, /* Expected pointer map type */ |
| 6331 | Pgno iParent, /* Expected pointer map parent page number */ |
| 6332 | char *zContext /* Context description (used for error msg) */ |
| 6333 | ){ |
| 6334 | int rc; |
| 6335 | u8 ePtrmapType; |
| 6336 | Pgno iPtrmapParent; |
| 6337 | |
| 6338 | rc = ptrmapGet(pCheck->pBt, iChild, &ePtrmapType, &iPtrmapParent); |
| 6339 | if( rc!=SQLITE_OK ){ |
| 6340 | checkAppendMsg(pCheck, zContext, "Failed to read ptrmap key=%d", iChild); |
| 6341 | return; |
| 6342 | } |
| 6343 | |
| 6344 | if( ePtrmapType!=eType || iPtrmapParent!=iParent ){ |
| 6345 | checkAppendMsg(pCheck, zContext, |
| 6346 | "Bad ptr map entry key=%d expected=(%d,%d) got=(%d,%d)", |
| 6347 | iChild, eType, iParent, ePtrmapType, iPtrmapParent); |
| 6348 | } |
| 6349 | } |
| 6350 | #endif |
| 6351 | |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6352 | /* |
| 6353 | ** Check the integrity of the freelist or of an overflow page list. |
| 6354 | ** Verify that the number of pages on the list is N. |
| 6355 | */ |
drh | 30e5875 | 2002-03-02 20:41:57 +0000 | [diff] [blame] | 6356 | static void checkList( |
| 6357 | IntegrityCk *pCheck, /* Integrity checking context */ |
| 6358 | int isFreeList, /* True for a freelist. False for overflow page list */ |
| 6359 | int iPage, /* Page number for first page in the list */ |
| 6360 | int N, /* Expected number of pages in the list */ |
| 6361 | char *zContext /* Context for error messages */ |
| 6362 | ){ |
| 6363 | int i; |
drh | 3a4c141 | 2004-05-09 20:40:11 +0000 | [diff] [blame] | 6364 | int expected = N; |
| 6365 | int iFirst = iPage; |
drh | 1dcdbc0 | 2007-01-27 02:24:54 +0000 | [diff] [blame] | 6366 | while( N-- > 0 && pCheck->mxErr ){ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 6367 | DbPage *pOvflPage; |
| 6368 | unsigned char *pOvflData; |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6369 | if( iPage<1 ){ |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 6370 | checkAppendMsg(pCheck, zContext, |
| 6371 | "%d of %d pages missing from overflow list starting at %d", |
drh | 3a4c141 | 2004-05-09 20:40:11 +0000 | [diff] [blame] | 6372 | N+1, expected, iFirst); |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6373 | break; |
| 6374 | } |
| 6375 | if( checkRef(pCheck, iPage, zContext) ) break; |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 6376 | if( sqlite3PagerGet(pCheck->pPager, (Pgno)iPage, &pOvflPage) ){ |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 6377 | checkAppendMsg(pCheck, zContext, "failed to get page %d", iPage); |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6378 | break; |
| 6379 | } |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 6380 | pOvflData = (unsigned char *)sqlite3PagerGetData(pOvflPage); |
drh | 30e5875 | 2002-03-02 20:41:57 +0000 | [diff] [blame] | 6381 | if( isFreeList ){ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 6382 | int n = get4byte(&pOvflData[4]); |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 6383 | #ifndef SQLITE_OMIT_AUTOVACUUM |
| 6384 | if( pCheck->pBt->autoVacuum ){ |
| 6385 | checkPtrmap(pCheck, iPage, PTRMAP_FREEPAGE, 0, zContext); |
| 6386 | } |
| 6387 | #endif |
drh | 855eb1c | 2004-08-31 13:45:11 +0000 | [diff] [blame] | 6388 | if( n>pCheck->pBt->usableSize/4-8 ){ |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 6389 | checkAppendMsg(pCheck, zContext, |
| 6390 | "freelist leaf count too big on page %d", iPage); |
drh | ee696e2 | 2004-08-30 16:52:17 +0000 | [diff] [blame] | 6391 | N--; |
| 6392 | }else{ |
| 6393 | for(i=0; i<n; i++){ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 6394 | Pgno iFreePage = get4byte(&pOvflData[8+i*4]); |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 6395 | #ifndef SQLITE_OMIT_AUTOVACUUM |
| 6396 | if( pCheck->pBt->autoVacuum ){ |
| 6397 | checkPtrmap(pCheck, iFreePage, PTRMAP_FREEPAGE, 0, zContext); |
| 6398 | } |
| 6399 | #endif |
| 6400 | checkRef(pCheck, iFreePage, zContext); |
drh | ee696e2 | 2004-08-30 16:52:17 +0000 | [diff] [blame] | 6401 | } |
| 6402 | N -= n; |
drh | 30e5875 | 2002-03-02 20:41:57 +0000 | [diff] [blame] | 6403 | } |
drh | 30e5875 | 2002-03-02 20:41:57 +0000 | [diff] [blame] | 6404 | } |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 6405 | #ifndef SQLITE_OMIT_AUTOVACUUM |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 6406 | else{ |
| 6407 | /* If this database supports auto-vacuum and iPage is not the last |
| 6408 | ** page in this overflow list, check that the pointer-map entry for |
| 6409 | ** the following page matches iPage. |
| 6410 | */ |
| 6411 | if( pCheck->pBt->autoVacuum && N>0 ){ |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 6412 | i = get4byte(pOvflData); |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 6413 | checkPtrmap(pCheck, i, PTRMAP_OVERFLOW2, iPage, zContext); |
| 6414 | } |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 6415 | } |
| 6416 | #endif |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 6417 | iPage = get4byte(pOvflData); |
| 6418 | sqlite3PagerUnref(pOvflPage); |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6419 | } |
| 6420 | } |
drh | b7f9164 | 2004-10-31 02:22:47 +0000 | [diff] [blame] | 6421 | #endif /* SQLITE_OMIT_INTEGRITY_CHECK */ |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6422 | |
drh | b7f9164 | 2004-10-31 02:22:47 +0000 | [diff] [blame] | 6423 | #ifndef SQLITE_OMIT_INTEGRITY_CHECK |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6424 | /* |
| 6425 | ** Do various sanity checks on a single page of a tree. Return |
| 6426 | ** the tree depth. Root pages return 0. Parents of root pages |
| 6427 | ** return 1, and so forth. |
| 6428 | ** |
| 6429 | ** These checks are done: |
| 6430 | ** |
| 6431 | ** 1. Make sure that cells and freeblocks do not overlap |
| 6432 | ** but combine to completely cover the page. |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 6433 | ** NO 2. Make sure cell keys are in order. |
| 6434 | ** NO 3. Make sure no key is less than or equal to zLowerBound. |
| 6435 | ** NO 4. Make sure no key is greater than or equal to zUpperBound. |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6436 | ** 5. Check the integrity of overflow pages. |
| 6437 | ** 6. Recursively call checkTreePage on all children. |
| 6438 | ** 7. Verify that the depth of all children is the same. |
drh | 6019e16 | 2001-07-02 17:51:45 +0000 | [diff] [blame] | 6439 | ** 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] | 6440 | ** the root of the tree. |
| 6441 | */ |
| 6442 | static int checkTreePage( |
drh | aaab572 | 2002-02-19 13:39:21 +0000 | [diff] [blame] | 6443 | IntegrityCk *pCheck, /* Context for the sanity check */ |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6444 | int iPage, /* Page number of the page to check */ |
| 6445 | MemPage *pParent, /* Parent page */ |
drh | 7416170 | 2006-02-24 02:53:49 +0000 | [diff] [blame] | 6446 | char *zParentContext /* Parent context */ |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6447 | ){ |
| 6448 | MemPage *pPage; |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 6449 | int i, rc, depth, d2, pgno, cnt; |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 6450 | int hdr, cellStart; |
| 6451 | int nCell; |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 6452 | u8 *data; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 6453 | BtShared *pBt; |
drh | 4f26bb6 | 2005-09-08 14:17:20 +0000 | [diff] [blame] | 6454 | int usableSize; |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6455 | char zContext[100]; |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 6456 | char *hit; |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6457 | |
drh | 5bb3eb9 | 2007-05-04 13:15:55 +0000 | [diff] [blame] | 6458 | sqlite3_snprintf(sizeof(zContext), zContext, "Page %d: ", iPage); |
danielk1977 | ef73ee9 | 2004-11-06 12:26:07 +0000 | [diff] [blame] | 6459 | |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6460 | /* Check that the page exists |
| 6461 | */ |
drh | d9cb6ac | 2005-10-20 07:28:17 +0000 | [diff] [blame] | 6462 | pBt = pCheck->pBt; |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 6463 | usableSize = pBt->usableSize; |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6464 | if( iPage==0 ) return 0; |
| 6465 | if( checkRef(pCheck, iPage, zParentContext) ) return 0; |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 6466 | if( (rc = sqlite3BtreeGetPage(pBt, (Pgno)iPage, &pPage, 0))!=0 ){ |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 6467 | checkAppendMsg(pCheck, zContext, |
| 6468 | "unable to get the page. error code=%d", rc); |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6469 | return 0; |
| 6470 | } |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 6471 | if( (rc = sqlite3BtreeInitPage(pPage, pParent))!=0 ){ |
| 6472 | checkAppendMsg(pCheck, zContext, |
| 6473 | "sqlite3BtreeInitPage() returns error code %d", rc); |
drh | 9102529 | 2004-05-03 19:49:32 +0000 | [diff] [blame] | 6474 | releasePage(pPage); |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6475 | return 0; |
| 6476 | } |
| 6477 | |
| 6478 | /* Check out all the cells. |
| 6479 | */ |
| 6480 | depth = 0; |
drh | 1dcdbc0 | 2007-01-27 02:24:54 +0000 | [diff] [blame] | 6481 | for(i=0; i<pPage->nCell && pCheck->mxErr; i++){ |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 6482 | u8 *pCell; |
| 6483 | int sz; |
| 6484 | CellInfo info; |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6485 | |
| 6486 | /* Check payload overflow pages |
| 6487 | */ |
drh | 5bb3eb9 | 2007-05-04 13:15:55 +0000 | [diff] [blame] | 6488 | sqlite3_snprintf(sizeof(zContext), zContext, |
| 6489 | "On tree page %d cell %d: ", iPage, i); |
danielk1977 | 1cc5ed8 | 2007-05-16 17:28:43 +0000 | [diff] [blame] | 6490 | pCell = findCell(pPage,i); |
drh | 16a9b83 | 2007-05-05 18:39:25 +0000 | [diff] [blame] | 6491 | sqlite3BtreeParseCellPtr(pPage, pCell, &info); |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 6492 | sz = info.nData; |
| 6493 | if( !pPage->intKey ) sz += info.nKey; |
drh | 7236583 | 2007-03-06 15:53:44 +0000 | [diff] [blame] | 6494 | assert( sz==info.nPayload ); |
drh | 6f11bef | 2004-05-13 01:12:56 +0000 | [diff] [blame] | 6495 | if( sz>info.nLocal ){ |
drh | b6f4148 | 2004-05-14 01:58:11 +0000 | [diff] [blame] | 6496 | int nPage = (sz - info.nLocal + usableSize - 5)/(usableSize - 4); |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 6497 | Pgno pgnoOvfl = get4byte(&pCell[info.iOverflow]); |
| 6498 | #ifndef SQLITE_OMIT_AUTOVACUUM |
| 6499 | if( pBt->autoVacuum ){ |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 6500 | checkPtrmap(pCheck, pgnoOvfl, PTRMAP_OVERFLOW1, iPage, zContext); |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 6501 | } |
| 6502 | #endif |
| 6503 | checkList(pCheck, 0, pgnoOvfl, nPage, zContext); |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6504 | } |
| 6505 | |
| 6506 | /* Check sanity of left child page. |
| 6507 | */ |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 6508 | if( !pPage->leaf ){ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 6509 | pgno = get4byte(pCell); |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 6510 | #ifndef SQLITE_OMIT_AUTOVACUUM |
| 6511 | if( pBt->autoVacuum ){ |
| 6512 | checkPtrmap(pCheck, pgno, PTRMAP_BTREE, iPage, zContext); |
| 6513 | } |
| 6514 | #endif |
drh | 7416170 | 2006-02-24 02:53:49 +0000 | [diff] [blame] | 6515 | d2 = checkTreePage(pCheck,pgno,pPage,zContext); |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 6516 | if( i>0 && d2!=depth ){ |
| 6517 | checkAppendMsg(pCheck, zContext, "Child page depth differs"); |
| 6518 | } |
| 6519 | depth = d2; |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6520 | } |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6521 | } |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 6522 | if( !pPage->leaf ){ |
drh | 4360515 | 2004-05-29 21:46:49 +0000 | [diff] [blame] | 6523 | pgno = get4byte(&pPage->aData[pPage->hdrOffset+8]); |
drh | 5bb3eb9 | 2007-05-04 13:15:55 +0000 | [diff] [blame] | 6524 | sqlite3_snprintf(sizeof(zContext), zContext, |
| 6525 | "On page %d at right child: ", iPage); |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 6526 | #ifndef SQLITE_OMIT_AUTOVACUUM |
| 6527 | if( pBt->autoVacuum ){ |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 6528 | checkPtrmap(pCheck, pgno, PTRMAP_BTREE, iPage, 0); |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 6529 | } |
| 6530 | #endif |
drh | 7416170 | 2006-02-24 02:53:49 +0000 | [diff] [blame] | 6531 | checkTreePage(pCheck, pgno, pPage, zContext); |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 6532 | } |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6533 | |
| 6534 | /* Check for complete coverage of the page |
| 6535 | */ |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 6536 | data = pPage->aData; |
| 6537 | hdr = pPage->hdrOffset; |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 6538 | hit = sqlite3MallocZero( usableSize ); |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 6539 | if( hit ){ |
| 6540 | memset(hit, 1, get2byte(&data[hdr+5])); |
| 6541 | nCell = get2byte(&data[hdr+3]); |
| 6542 | cellStart = hdr + 12 - 4*pPage->leaf; |
| 6543 | for(i=0; i<nCell; i++){ |
| 6544 | int pc = get2byte(&data[cellStart+i*2]); |
drh | a9121e4 | 2008-02-19 14:59:35 +0000 | [diff] [blame] | 6545 | u16 size = cellSizePtr(pPage, &data[pc]); |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 6546 | int j; |
danielk1977 | 7701e81 | 2005-01-10 12:59:51 +0000 | [diff] [blame] | 6547 | if( (pc+size-1)>=usableSize || pc<0 ){ |
| 6548 | checkAppendMsg(pCheck, 0, |
| 6549 | "Corruption detected in cell %d on page %d",i,iPage,0); |
| 6550 | }else{ |
| 6551 | for(j=pc+size-1; j>=pc; j--) hit[j]++; |
| 6552 | } |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 6553 | } |
| 6554 | for(cnt=0, i=get2byte(&data[hdr+1]); i>0 && i<usableSize && cnt<10000; |
| 6555 | cnt++){ |
| 6556 | int size = get2byte(&data[i+2]); |
| 6557 | int j; |
danielk1977 | 7701e81 | 2005-01-10 12:59:51 +0000 | [diff] [blame] | 6558 | if( (i+size-1)>=usableSize || i<0 ){ |
| 6559 | checkAppendMsg(pCheck, 0, |
| 6560 | "Corruption detected in cell %d on page %d",i,iPage,0); |
| 6561 | }else{ |
| 6562 | for(j=i+size-1; j>=i; j--) hit[j]++; |
| 6563 | } |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 6564 | i = get2byte(&data[i]); |
| 6565 | } |
| 6566 | for(i=cnt=0; i<usableSize; i++){ |
| 6567 | if( hit[i]==0 ){ |
| 6568 | cnt++; |
| 6569 | }else if( hit[i]>1 ){ |
| 6570 | checkAppendMsg(pCheck, 0, |
| 6571 | "Multiple uses for byte %d of page %d", i, iPage); |
| 6572 | break; |
| 6573 | } |
| 6574 | } |
| 6575 | if( cnt!=data[hdr+7] ){ |
| 6576 | checkAppendMsg(pCheck, 0, |
| 6577 | "Fragmented space is %d byte reported as %d on page %d", |
| 6578 | cnt, data[hdr+7], iPage); |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6579 | } |
| 6580 | } |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 6581 | sqlite3_free(hit); |
drh | 6019e16 | 2001-07-02 17:51:45 +0000 | [diff] [blame] | 6582 | |
drh | 4b70f11 | 2004-05-02 21:12:19 +0000 | [diff] [blame] | 6583 | releasePage(pPage); |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 6584 | return depth+1; |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6585 | } |
drh | b7f9164 | 2004-10-31 02:22:47 +0000 | [diff] [blame] | 6586 | #endif /* SQLITE_OMIT_INTEGRITY_CHECK */ |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6587 | |
drh | b7f9164 | 2004-10-31 02:22:47 +0000 | [diff] [blame] | 6588 | #ifndef SQLITE_OMIT_INTEGRITY_CHECK |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6589 | /* |
| 6590 | ** This routine does a complete check of the given BTree file. aRoot[] is |
| 6591 | ** an array of pages numbers were each page number is the root page of |
| 6592 | ** a table. nRoot is the number of entries in aRoot. |
| 6593 | ** |
| 6594 | ** If everything checks out, this routine returns NULL. If something is |
| 6595 | ** amiss, an error message is written into memory obtained from malloc() |
| 6596 | ** and a pointer to that error message is returned. The calling function |
| 6597 | ** is responsible for freeing the error message when it is done. |
| 6598 | */ |
drh | 1dcdbc0 | 2007-01-27 02:24:54 +0000 | [diff] [blame] | 6599 | char *sqlite3BtreeIntegrityCheck( |
| 6600 | Btree *p, /* The btree to be checked */ |
| 6601 | int *aRoot, /* An array of root pages numbers for individual trees */ |
| 6602 | int nRoot, /* Number of entries in aRoot[] */ |
| 6603 | int mxErr, /* Stop reporting errors after this many */ |
| 6604 | int *pnErr /* Write number of errors seen to this variable */ |
| 6605 | ){ |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6606 | int i; |
| 6607 | int nRef; |
drh | aaab572 | 2002-02-19 13:39:21 +0000 | [diff] [blame] | 6608 | IntegrityCk sCheck; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 6609 | BtShared *pBt = p->pBt; |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6610 | |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 6611 | sqlite3BtreeEnter(p); |
drh | e5fe690 | 2007-12-07 18:55:28 +0000 | [diff] [blame] | 6612 | pBt->db = p->db; |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 6613 | nRef = sqlite3PagerRefcount(pBt->pPager); |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 6614 | if( lockBtreeWithRetry(p)!=SQLITE_OK ){ |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 6615 | sqlite3BtreeLeave(p); |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 6616 | return sqlite3StrDup("Unable to acquire a read lock on the database"); |
drh | efc251d | 2001-07-01 22:12:01 +0000 | [diff] [blame] | 6617 | } |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6618 | sCheck.pBt = pBt; |
| 6619 | sCheck.pPager = pBt->pPager; |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 6620 | sCheck.nPage = sqlite3PagerPagecount(sCheck.pPager); |
drh | 1dcdbc0 | 2007-01-27 02:24:54 +0000 | [diff] [blame] | 6621 | sCheck.mxErr = mxErr; |
| 6622 | sCheck.nErr = 0; |
| 6623 | *pnErr = 0; |
danielk1977 | e5321f0 | 2007-04-27 07:05:44 +0000 | [diff] [blame] | 6624 | #ifndef SQLITE_OMIT_AUTOVACUUM |
| 6625 | if( pBt->nTrunc!=0 ){ |
| 6626 | sCheck.nPage = pBt->nTrunc; |
| 6627 | } |
| 6628 | #endif |
drh | 0de8c11 | 2002-07-06 16:32:14 +0000 | [diff] [blame] | 6629 | if( sCheck.nPage==0 ){ |
| 6630 | unlockBtreeIfUnused(pBt); |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 6631 | sqlite3BtreeLeave(p); |
drh | 0de8c11 | 2002-07-06 16:32:14 +0000 | [diff] [blame] | 6632 | return 0; |
| 6633 | } |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 6634 | sCheck.anRef = sqlite3_malloc( (sCheck.nPage+1)*sizeof(sCheck.anRef[0]) ); |
danielk1977 | ac245ec | 2005-01-14 13:50:11 +0000 | [diff] [blame] | 6635 | if( !sCheck.anRef ){ |
| 6636 | unlockBtreeIfUnused(pBt); |
drh | 1dcdbc0 | 2007-01-27 02:24:54 +0000 | [diff] [blame] | 6637 | *pnErr = 1; |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 6638 | sqlite3BtreeLeave(p); |
drh | e5fe690 | 2007-12-07 18:55:28 +0000 | [diff] [blame] | 6639 | return sqlite3MPrintf(p->db, "Unable to malloc %d bytes", |
danielk1977 | ac245ec | 2005-01-14 13:50:11 +0000 | [diff] [blame] | 6640 | (sCheck.nPage+1)*sizeof(sCheck.anRef[0])); |
| 6641 | } |
drh | da200cc | 2004-05-09 11:51:38 +0000 | [diff] [blame] | 6642 | for(i=0; i<=sCheck.nPage; i++){ sCheck.anRef[i] = 0; } |
drh | 42cac6d | 2004-11-20 20:31:11 +0000 | [diff] [blame] | 6643 | i = PENDING_BYTE_PAGE(pBt); |
drh | 1f59571 | 2004-06-15 01:40:29 +0000 | [diff] [blame] | 6644 | if( i<=sCheck.nPage ){ |
| 6645 | sCheck.anRef[i] = 1; |
| 6646 | } |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6647 | sCheck.zErrMsg = 0; |
| 6648 | |
| 6649 | /* Check the integrity of the freelist |
| 6650 | */ |
drh | a34b676 | 2004-05-07 13:30:42 +0000 | [diff] [blame] | 6651 | checkList(&sCheck, 1, get4byte(&pBt->pPage1->aData[32]), |
| 6652 | get4byte(&pBt->pPage1->aData[36]), "Main freelist: "); |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6653 | |
| 6654 | /* Check all the tables. |
| 6655 | */ |
drh | 1dcdbc0 | 2007-01-27 02:24:54 +0000 | [diff] [blame] | 6656 | for(i=0; i<nRoot && sCheck.mxErr; i++){ |
drh | 4ff6dfa | 2002-03-03 23:06:00 +0000 | [diff] [blame] | 6657 | if( aRoot[i]==0 ) continue; |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 6658 | #ifndef SQLITE_OMIT_AUTOVACUUM |
danielk1977 | 687566d | 2004-11-02 12:56:41 +0000 | [diff] [blame] | 6659 | if( pBt->autoVacuum && aRoot[i]>1 ){ |
| 6660 | checkPtrmap(&sCheck, aRoot[i], PTRMAP_ROOTPAGE, 0, 0); |
| 6661 | } |
| 6662 | #endif |
drh | 7416170 | 2006-02-24 02:53:49 +0000 | [diff] [blame] | 6663 | checkTreePage(&sCheck, aRoot[i], 0, "List of tree roots: "); |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6664 | } |
| 6665 | |
| 6666 | /* Make sure every page in the file is referenced |
| 6667 | */ |
drh | 1dcdbc0 | 2007-01-27 02:24:54 +0000 | [diff] [blame] | 6668 | for(i=1; i<=sCheck.nPage && sCheck.mxErr; i++){ |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 6669 | #ifdef SQLITE_OMIT_AUTOVACUUM |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6670 | if( sCheck.anRef[i]==0 ){ |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 6671 | checkAppendMsg(&sCheck, 0, "Page %d is never used", i); |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6672 | } |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 6673 | #else |
| 6674 | /* If the database supports auto-vacuum, make sure no tables contain |
| 6675 | ** references to pointer-map pages. |
| 6676 | */ |
| 6677 | if( sCheck.anRef[i]==0 && |
danielk1977 | 266664d | 2006-02-10 08:24:21 +0000 | [diff] [blame] | 6678 | (PTRMAP_PAGENO(pBt, i)!=i || !pBt->autoVacuum) ){ |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 6679 | checkAppendMsg(&sCheck, 0, "Page %d is never used", i); |
| 6680 | } |
| 6681 | if( sCheck.anRef[i]!=0 && |
danielk1977 | 266664d | 2006-02-10 08:24:21 +0000 | [diff] [blame] | 6682 | (PTRMAP_PAGENO(pBt, i)==i && pBt->autoVacuum) ){ |
danielk1977 | afcdd02 | 2004-10-31 16:25:42 +0000 | [diff] [blame] | 6683 | checkAppendMsg(&sCheck, 0, "Pointer map page %d is referenced", i); |
| 6684 | } |
| 6685 | #endif |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6686 | } |
| 6687 | |
| 6688 | /* Make sure this analysis did not leave any unref() pages |
| 6689 | */ |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 6690 | unlockBtreeIfUnused(pBt); |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 6691 | if( nRef != sqlite3PagerRefcount(pBt->pPager) ){ |
drh | 2e38c32 | 2004-09-03 18:38:44 +0000 | [diff] [blame] | 6692 | checkAppendMsg(&sCheck, 0, |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6693 | "Outstanding page count goes from %d to %d during this analysis", |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 6694 | nRef, sqlite3PagerRefcount(pBt->pPager) |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6695 | ); |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6696 | } |
| 6697 | |
| 6698 | /* Clean up and report errors. |
| 6699 | */ |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 6700 | sqlite3BtreeLeave(p); |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 6701 | sqlite3_free(sCheck.anRef); |
drh | 1dcdbc0 | 2007-01-27 02:24:54 +0000 | [diff] [blame] | 6702 | *pnErr = sCheck.nErr; |
drh | 5eddca6 | 2001-06-30 21:53:53 +0000 | [diff] [blame] | 6703 | return sCheck.zErrMsg; |
| 6704 | } |
drh | b7f9164 | 2004-10-31 02:22:47 +0000 | [diff] [blame] | 6705 | #endif /* SQLITE_OMIT_INTEGRITY_CHECK */ |
paul | b95a886 | 2003-04-01 21:16:41 +0000 | [diff] [blame] | 6706 | |
drh | 73509ee | 2003-04-06 20:44:45 +0000 | [diff] [blame] | 6707 | /* |
| 6708 | ** Return the full pathname of the underlying database file. |
drh | d0679ed | 2007-08-28 22:24:34 +0000 | [diff] [blame] | 6709 | ** |
| 6710 | ** The pager filename is invariant as long as the pager is |
| 6711 | ** open so it is safe to access without the BtShared mutex. |
drh | 73509ee | 2003-04-06 20:44:45 +0000 | [diff] [blame] | 6712 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 6713 | const char *sqlite3BtreeGetFilename(Btree *p){ |
| 6714 | assert( p->pBt->pPager!=0 ); |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 6715 | return sqlite3PagerFilename(p->pBt->pPager); |
drh | 73509ee | 2003-04-06 20:44:45 +0000 | [diff] [blame] | 6716 | } |
| 6717 | |
| 6718 | /* |
danielk1977 | 5865e3d | 2004-06-14 06:03:57 +0000 | [diff] [blame] | 6719 | ** Return the pathname of the directory that contains the database file. |
drh | d0679ed | 2007-08-28 22:24:34 +0000 | [diff] [blame] | 6720 | ** |
| 6721 | ** The pager directory name is invariant as long as the pager is |
| 6722 | ** open so it is safe to access without the BtShared mutex. |
danielk1977 | 5865e3d | 2004-06-14 06:03:57 +0000 | [diff] [blame] | 6723 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 6724 | const char *sqlite3BtreeGetDirname(Btree *p){ |
| 6725 | assert( p->pBt->pPager!=0 ); |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 6726 | return sqlite3PagerDirname(p->pBt->pPager); |
danielk1977 | 5865e3d | 2004-06-14 06:03:57 +0000 | [diff] [blame] | 6727 | } |
| 6728 | |
| 6729 | /* |
| 6730 | ** Return the pathname of the journal file for this database. The return |
| 6731 | ** value of this routine is the same regardless of whether the journal file |
| 6732 | ** has been created or not. |
drh | d0679ed | 2007-08-28 22:24:34 +0000 | [diff] [blame] | 6733 | ** |
| 6734 | ** The pager journal filename is invariant as long as the pager is |
| 6735 | ** open so it is safe to access without the BtShared mutex. |
danielk1977 | 5865e3d | 2004-06-14 06:03:57 +0000 | [diff] [blame] | 6736 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 6737 | const char *sqlite3BtreeGetJournalname(Btree *p){ |
| 6738 | assert( p->pBt->pPager!=0 ); |
danielk1977 | 3b8a05f | 2007-03-19 17:44:26 +0000 | [diff] [blame] | 6739 | return sqlite3PagerJournalname(p->pBt->pPager); |
danielk1977 | 5865e3d | 2004-06-14 06:03:57 +0000 | [diff] [blame] | 6740 | } |
| 6741 | |
drh | b7f9164 | 2004-10-31 02:22:47 +0000 | [diff] [blame] | 6742 | #ifndef SQLITE_OMIT_VACUUM |
danielk1977 | 5865e3d | 2004-06-14 06:03:57 +0000 | [diff] [blame] | 6743 | /* |
drh | f7c5753 | 2003-04-25 13:22:51 +0000 | [diff] [blame] | 6744 | ** Copy the complete content of pBtFrom into pBtTo. A transaction |
| 6745 | ** must be active for both files. |
| 6746 | ** |
danielk1977 | f653d78 | 2008-03-20 11:04:21 +0000 | [diff] [blame^] | 6747 | ** The size of file pTo may be reduced by this operation. |
| 6748 | ** If anything goes wrong, the transaction on pTo is rolled back. |
| 6749 | ** |
| 6750 | ** If successful, CommitPhaseOne() may be called on pTo before returning. |
| 6751 | ** The caller should finish committing the transaction on pTo by calling |
| 6752 | ** sqlite3BtreeCommit(). |
drh | 73509ee | 2003-04-06 20:44:45 +0000 | [diff] [blame] | 6753 | */ |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 6754 | static int btreeCopyFile(Btree *pTo, Btree *pFrom){ |
drh | f7c5753 | 2003-04-25 13:22:51 +0000 | [diff] [blame] | 6755 | int rc = SQLITE_OK; |
danielk1977 | f653d78 | 2008-03-20 11:04:21 +0000 | [diff] [blame^] | 6756 | Pgno i; |
| 6757 | |
| 6758 | Pgno nFromPage; /* Number of pages in pFrom */ |
| 6759 | Pgno nToPage; /* Number of pages in pTo */ |
| 6760 | Pgno nNewPage; /* Number of pages in pTo after the copy */ |
| 6761 | |
| 6762 | Pgno iSkip; /* Pending byte page in pTo */ |
| 6763 | int nToPageSize; /* Page size of pTo in bytes */ |
| 6764 | int nFromPageSize; /* Page size of pFrom in bytes */ |
drh | f7c5753 | 2003-04-25 13:22:51 +0000 | [diff] [blame] | 6765 | |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 6766 | BtShared *pBtTo = pTo->pBt; |
| 6767 | BtShared *pBtFrom = pFrom->pBt; |
drh | e5fe690 | 2007-12-07 18:55:28 +0000 | [diff] [blame] | 6768 | pBtTo->db = pTo->db; |
| 6769 | pBtFrom->db = pFrom->db; |
danielk1977 | f653d78 | 2008-03-20 11:04:21 +0000 | [diff] [blame^] | 6770 | |
| 6771 | nToPageSize = pBtTo->pageSize; |
| 6772 | nFromPageSize = pBtFrom->pageSize; |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 6773 | |
| 6774 | if( pTo->inTrans!=TRANS_WRITE || pFrom->inTrans!=TRANS_WRITE ){ |
danielk1977 | ee5741e | 2004-05-31 10:01:34 +0000 | [diff] [blame] | 6775 | return SQLITE_ERROR; |
| 6776 | } |
danielk1977 | f653d78 | 2008-03-20 11:04:21 +0000 | [diff] [blame^] | 6777 | if( pBtTo->pCursor ){ |
| 6778 | return SQLITE_BUSY; |
drh | f7c5753 | 2003-04-25 13:22:51 +0000 | [diff] [blame] | 6779 | } |
drh | 538f570 | 2007-04-13 02:14:30 +0000 | [diff] [blame] | 6780 | |
danielk1977 | f653d78 | 2008-03-20 11:04:21 +0000 | [diff] [blame^] | 6781 | nToPage = sqlite3PagerPagecount(pBtTo->pPager); |
| 6782 | nFromPage = sqlite3PagerPagecount(pBtFrom->pPager); |
| 6783 | iSkip = PENDING_BYTE_PAGE(pBtTo); |
| 6784 | |
| 6785 | /* Variable nNewPage is the number of pages required to store the |
| 6786 | ** contents of pFrom using the current page-size of pTo. |
drh | 538f570 | 2007-04-13 02:14:30 +0000 | [diff] [blame] | 6787 | */ |
danielk1977 | f653d78 | 2008-03-20 11:04:21 +0000 | [diff] [blame^] | 6788 | nNewPage = ((i64)nFromPage * (i64)nFromPageSize + (i64)nToPageSize - 1) / |
| 6789 | (i64)nToPageSize; |
| 6790 | |
| 6791 | for(i=1; rc==SQLITE_OK && (i<=nToPage || i<=nNewPage); i++){ |
| 6792 | |
| 6793 | /* Journal the original page. |
| 6794 | ** |
| 6795 | ** iSkip is the page number of the locking page (PENDING_BYTE_PAGE) |
| 6796 | ** in database *pTo (before the copy). This page is never written |
| 6797 | ** into the journal file. Unless i==iSkip or the page was not |
| 6798 | ** present in pTo before the copy operation, journal page i from pTo. |
| 6799 | */ |
| 6800 | if( i!=iSkip && i<=nToPage ){ |
| 6801 | DbPage *pDbPage; |
| 6802 | rc = sqlite3PagerGet(pBtTo->pPager, i, &pDbPage); |
| 6803 | if( rc ){ |
| 6804 | break; |
| 6805 | } |
| 6806 | rc = sqlite3PagerWrite(pDbPage); |
| 6807 | if( rc ){ |
| 6808 | break; |
| 6809 | } |
| 6810 | if( i>nFromPage ){ |
| 6811 | /* Yeah. It seems wierd to call DontWrite() right after Write(). But |
| 6812 | ** that is because the names of those procedures do not exactly |
| 6813 | ** represent what they do. Write() really means "put this page in the |
| 6814 | ** rollback journal and mark it as dirty so that it will be written |
| 6815 | ** to the database file later." DontWrite() undoes the second part of |
| 6816 | ** that and prevents the page from being written to the database. The |
| 6817 | ** page is still on the rollback journal, though. And that is the |
| 6818 | ** whole point of this block: to put pages on the rollback journal. |
| 6819 | */ |
| 6820 | sqlite3PagerDontWrite(pDbPage); |
| 6821 | } |
| 6822 | sqlite3PagerUnref(pDbPage); |
| 6823 | } |
| 6824 | |
| 6825 | /* Overwrite the data in page i of the target database */ |
| 6826 | if( rc==SQLITE_OK && i!=iSkip && i<=nNewPage ){ |
| 6827 | |
| 6828 | DbPage *pToPage = 0; |
| 6829 | sqlite3_int64 iOff; |
| 6830 | |
| 6831 | rc = sqlite3PagerGet(pBtTo->pPager, i, &pToPage); |
| 6832 | if( rc==SQLITE_OK ){ |
| 6833 | rc = sqlite3PagerWrite(pToPage); |
| 6834 | } |
| 6835 | |
| 6836 | for( |
| 6837 | iOff=(i-1)*nToPageSize; |
| 6838 | rc==SQLITE_OK && iOff<i*nToPageSize; |
| 6839 | iOff += nFromPageSize |
| 6840 | ){ |
| 6841 | DbPage *pFromPage = 0; |
| 6842 | Pgno iFrom = (iOff/nFromPageSize)+1; |
| 6843 | |
| 6844 | if( iFrom==PENDING_BYTE_PAGE(pBtFrom) ){ |
| 6845 | continue; |
| 6846 | } |
| 6847 | |
| 6848 | rc = sqlite3PagerGet(pBtFrom->pPager, iFrom, &pFromPage); |
| 6849 | if( rc==SQLITE_OK ){ |
| 6850 | char *zTo = sqlite3PagerGetData(pToPage); |
| 6851 | char *zFrom = sqlite3PagerGetData(pFromPage); |
| 6852 | int nCopy; |
| 6853 | |
| 6854 | if( nFromPageSize>=nToPageSize ){ |
| 6855 | zFrom += ((i-1)*nToPageSize - ((iFrom-1)*nFromPageSize)); |
| 6856 | nCopy = nToPageSize; |
| 6857 | }else{ |
| 6858 | zTo += (((iFrom-1)*nFromPageSize) - (i-1)*nToPageSize); |
| 6859 | nCopy = nFromPageSize; |
| 6860 | } |
| 6861 | |
| 6862 | memcpy(zTo, zFrom, nCopy); |
| 6863 | sqlite3PagerUnref(pFromPage); |
| 6864 | } |
| 6865 | } |
| 6866 | |
| 6867 | if( pToPage ) sqlite3PagerUnref(pToPage); |
| 6868 | } |
drh | 2e6d11b | 2003-04-25 15:37:57 +0000 | [diff] [blame] | 6869 | } |
danielk1977 | f653d78 | 2008-03-20 11:04:21 +0000 | [diff] [blame^] | 6870 | |
| 6871 | /* If things have worked so far, the database file may need to be |
| 6872 | ** truncated. The complex part is that it may need to be truncated to |
| 6873 | ** a size that is not an integer multiple of nToPageSize - the current |
| 6874 | ** page size used by the pager associated with B-Tree pTo. |
| 6875 | ** |
| 6876 | ** For example, say the page-size of pTo is 2048 bytes and the original |
| 6877 | ** number of pages is 5 (10 KB file). If pFrom has a page size of 1024 |
| 6878 | ** bytes and 9 pages, then the file needs to be truncated to 9KB. |
| 6879 | */ |
| 6880 | if( rc==SQLITE_OK ){ |
| 6881 | if( nFromPageSize!=nToPageSize ){ |
| 6882 | sqlite3_file *pFile = sqlite3PagerFile(pBtTo->pPager); |
| 6883 | i64 iSize = (i64)nFromPageSize * (i64)nFromPage; |
| 6884 | i64 iNow = (i64)((nToPage>nNewPage)?nToPage:nNewPage) * (i64)nToPageSize; |
| 6885 | i64 iPending = ((i64)PENDING_BYTE_PAGE(pBtTo)-1) *(i64)nToPageSize; |
| 6886 | |
| 6887 | assert( iSize<=iNow ); |
| 6888 | |
| 6889 | /* Commit phase one syncs the journal file associated with pTo |
| 6890 | ** containing the original data. It does not sync the database file |
| 6891 | ** itself. After doing this it is safe to use OsTruncate() and other |
| 6892 | ** file APIs on the database file directly. |
| 6893 | */ |
| 6894 | pBtTo->db = pTo->db; |
| 6895 | rc = sqlite3PagerCommitPhaseOne(pBtTo->pPager, 0, 0, 1); |
| 6896 | if( iSize<iNow && rc==SQLITE_OK ){ |
| 6897 | rc = sqlite3OsTruncate(pFile, iSize); |
| 6898 | } |
| 6899 | |
| 6900 | /* The loop that copied data from database pFrom to pTo did not |
| 6901 | ** populate the locking page of database pTo. If the page-size of |
| 6902 | ** pFrom is smaller than that of pTo, this means some data will |
| 6903 | ** not have been copied. |
| 6904 | ** |
| 6905 | ** This block copies the missing data from database pFrom to pTo |
| 6906 | ** using file APIs. This is safe because at this point we know that |
| 6907 | ** all of the original data from pTo has been synced into the |
| 6908 | ** journal file. At this point it would be safe to do anything at |
| 6909 | ** all to the database file except truncate it to zero bytes. |
| 6910 | */ |
| 6911 | if( rc==SQLITE_OK && nFromPageSize<nToPageSize && iSize>iPending){ |
| 6912 | i64 iOff; |
| 6913 | for( |
| 6914 | iOff=iPending; |
| 6915 | rc==SQLITE_OK && iOff<(iPending+nToPageSize); |
| 6916 | iOff += nFromPageSize |
| 6917 | ){ |
| 6918 | DbPage *pFromPage = 0; |
| 6919 | Pgno iFrom = (iOff/nFromPageSize)+1; |
| 6920 | |
| 6921 | if( iFrom==PENDING_BYTE_PAGE(pBtFrom) || iFrom>nFromPage ){ |
| 6922 | continue; |
| 6923 | } |
| 6924 | |
| 6925 | rc = sqlite3PagerGet(pBtFrom->pPager, iFrom, &pFromPage); |
| 6926 | if( rc==SQLITE_OK ){ |
| 6927 | char *zFrom = sqlite3PagerGetData(pFromPage); |
| 6928 | rc = sqlite3OsWrite(pFile, zFrom, nFromPageSize, iOff); |
| 6929 | sqlite3PagerUnref(pFromPage); |
| 6930 | } |
| 6931 | } |
| 6932 | } |
| 6933 | |
| 6934 | /* Sync the database file */ |
| 6935 | if( rc==SQLITE_OK ){ |
| 6936 | rc = sqlite3PagerSync(pBtTo->pPager); |
| 6937 | } |
| 6938 | }else{ |
| 6939 | rc = sqlite3PagerTruncate(pBtTo->pPager, nNewPage); |
| 6940 | } |
| 6941 | if( rc==SQLITE_OK ){ |
| 6942 | pBtTo->pageSizeFixed = 0; |
| 6943 | } |
drh | 2e6d11b | 2003-04-25 15:37:57 +0000 | [diff] [blame] | 6944 | } |
drh | 538f570 | 2007-04-13 02:14:30 +0000 | [diff] [blame] | 6945 | |
drh | f7c5753 | 2003-04-25 13:22:51 +0000 | [diff] [blame] | 6946 | if( rc ){ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 6947 | sqlite3BtreeRollback(pTo); |
drh | f7c5753 | 2003-04-25 13:22:51 +0000 | [diff] [blame] | 6948 | } |
danielk1977 | f653d78 | 2008-03-20 11:04:21 +0000 | [diff] [blame^] | 6949 | |
drh | f7c5753 | 2003-04-25 13:22:51 +0000 | [diff] [blame] | 6950 | return rc; |
drh | 73509ee | 2003-04-06 20:44:45 +0000 | [diff] [blame] | 6951 | } |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 6952 | int sqlite3BtreeCopyFile(Btree *pTo, Btree *pFrom){ |
| 6953 | int rc; |
| 6954 | sqlite3BtreeEnter(pTo); |
| 6955 | sqlite3BtreeEnter(pFrom); |
| 6956 | rc = btreeCopyFile(pTo, pFrom); |
| 6957 | sqlite3BtreeLeave(pFrom); |
| 6958 | sqlite3BtreeLeave(pTo); |
| 6959 | return rc; |
| 6960 | } |
| 6961 | |
drh | b7f9164 | 2004-10-31 02:22:47 +0000 | [diff] [blame] | 6962 | #endif /* SQLITE_OMIT_VACUUM */ |
danielk1977 | 1d850a7 | 2004-05-31 08:26:49 +0000 | [diff] [blame] | 6963 | |
| 6964 | /* |
| 6965 | ** Return non-zero if a transaction is active. |
| 6966 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 6967 | int sqlite3BtreeIsInTrans(Btree *p){ |
drh | e5fe690 | 2007-12-07 18:55:28 +0000 | [diff] [blame] | 6968 | assert( p==0 || sqlite3_mutex_held(p->db->mutex) ); |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 6969 | return (p && (p->inTrans==TRANS_WRITE)); |
danielk1977 | 1d850a7 | 2004-05-31 08:26:49 +0000 | [diff] [blame] | 6970 | } |
| 6971 | |
| 6972 | /* |
| 6973 | ** Return non-zero if a statement transaction is active. |
| 6974 | */ |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 6975 | int sqlite3BtreeIsInStmt(Btree *p){ |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 6976 | assert( sqlite3BtreeHoldsMutex(p) ); |
danielk1977 | aef0bf6 | 2005-12-30 16:28:01 +0000 | [diff] [blame] | 6977 | return (p->pBt && p->pBt->inStmt); |
danielk1977 | 1d850a7 | 2004-05-31 08:26:49 +0000 | [diff] [blame] | 6978 | } |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 6979 | |
| 6980 | /* |
danielk1977 | 2372c2b | 2006-06-27 16:34:56 +0000 | [diff] [blame] | 6981 | ** Return non-zero if a read (or write) transaction is active. |
| 6982 | */ |
| 6983 | int sqlite3BtreeIsInReadTrans(Btree *p){ |
drh | e5fe690 | 2007-12-07 18:55:28 +0000 | [diff] [blame] | 6984 | assert( sqlite3_mutex_held(p->db->mutex) ); |
danielk1977 | 2372c2b | 2006-06-27 16:34:56 +0000 | [diff] [blame] | 6985 | return (p && (p->inTrans!=TRANS_NONE)); |
| 6986 | } |
| 6987 | |
| 6988 | /* |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 6989 | ** This function returns a pointer to a blob of memory associated with |
drh | 85b623f | 2007-12-13 21:54:09 +0000 | [diff] [blame] | 6990 | ** 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] | 6991 | ** purposes (for example, to store a high-level schema associated with |
| 6992 | ** the shared-btree). The btree layer manages reference counting issues. |
| 6993 | ** |
| 6994 | ** The first time this is called on a shared-btree, nBytes bytes of memory |
| 6995 | ** are allocated, zeroed, and returned to the caller. For each subsequent |
| 6996 | ** call the nBytes parameter is ignored and a pointer to the same blob |
| 6997 | ** of memory returned. |
| 6998 | ** |
| 6999 | ** Just before the shared-btree is closed, the function passed as the |
| 7000 | ** xFree argument when the memory allocation was made is invoked on the |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 7001 | ** blob of allocated memory. This function should not call sqlite3_free() |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 7002 | ** on the memory, the btree layer does that. |
| 7003 | */ |
| 7004 | void *sqlite3BtreeSchema(Btree *p, int nBytes, void(*xFree)(void *)){ |
| 7005 | BtShared *pBt = p->pBt; |
drh | 2764170 | 2007-08-22 02:56:42 +0000 | [diff] [blame] | 7006 | sqlite3BtreeEnter(p); |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 7007 | if( !pBt->pSchema ){ |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 7008 | pBt->pSchema = sqlite3MallocZero(nBytes); |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 7009 | pBt->xFreeSchema = xFree; |
| 7010 | } |
drh | 2764170 | 2007-08-22 02:56:42 +0000 | [diff] [blame] | 7011 | sqlite3BtreeLeave(p); |
danielk1977 | da18423 | 2006-01-05 11:34:32 +0000 | [diff] [blame] | 7012 | return pBt->pSchema; |
| 7013 | } |
| 7014 | |
danielk1977 | c87d34d | 2006-01-06 13:00:28 +0000 | [diff] [blame] | 7015 | /* |
| 7016 | ** Return true if another user of the same shared btree as the argument |
| 7017 | ** handle holds an exclusive lock on the sqlite_master table. |
| 7018 | */ |
| 7019 | int sqlite3BtreeSchemaLocked(Btree *p){ |
drh | 2764170 | 2007-08-22 02:56:42 +0000 | [diff] [blame] | 7020 | int rc; |
drh | e5fe690 | 2007-12-07 18:55:28 +0000 | [diff] [blame] | 7021 | assert( sqlite3_mutex_held(p->db->mutex) ); |
drh | 2764170 | 2007-08-22 02:56:42 +0000 | [diff] [blame] | 7022 | sqlite3BtreeEnter(p); |
| 7023 | rc = (queryTableLock(p, MASTER_ROOT, READ_LOCK)!=SQLITE_OK); |
| 7024 | sqlite3BtreeLeave(p); |
| 7025 | return rc; |
danielk1977 | c87d34d | 2006-01-06 13:00:28 +0000 | [diff] [blame] | 7026 | } |
| 7027 | |
drh | a154dcd | 2006-03-22 22:10:07 +0000 | [diff] [blame] | 7028 | |
| 7029 | #ifndef SQLITE_OMIT_SHARED_CACHE |
| 7030 | /* |
| 7031 | ** Obtain a lock on the table whose root page is iTab. The |
| 7032 | ** lock is a write lock if isWritelock is true or a read lock |
| 7033 | ** if it is false. |
| 7034 | */ |
danielk1977 | c00da10 | 2006-01-07 13:21:04 +0000 | [diff] [blame] | 7035 | int sqlite3BtreeLockTable(Btree *p, int iTab, u8 isWriteLock){ |
danielk1977 | 2e94d4d | 2006-01-09 05:36:27 +0000 | [diff] [blame] | 7036 | int rc = SQLITE_OK; |
danielk1977 | c00da10 | 2006-01-07 13:21:04 +0000 | [diff] [blame] | 7037 | u8 lockType = (isWriteLock?WRITE_LOCK:READ_LOCK); |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 7038 | sqlite3BtreeEnter(p); |
danielk1977 | 2e94d4d | 2006-01-09 05:36:27 +0000 | [diff] [blame] | 7039 | rc = queryTableLock(p, iTab, lockType); |
danielk1977 | c00da10 | 2006-01-07 13:21:04 +0000 | [diff] [blame] | 7040 | if( rc==SQLITE_OK ){ |
| 7041 | rc = lockTable(p, iTab, lockType); |
| 7042 | } |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 7043 | sqlite3BtreeLeave(p); |
danielk1977 | c00da10 | 2006-01-07 13:21:04 +0000 | [diff] [blame] | 7044 | return rc; |
| 7045 | } |
drh | a154dcd | 2006-03-22 22:10:07 +0000 | [diff] [blame] | 7046 | #endif |
danielk1977 | b82e7ed | 2006-01-11 14:09:31 +0000 | [diff] [blame] | 7047 | |
danielk1977 | b4e9af9 | 2007-05-01 17:49:49 +0000 | [diff] [blame] | 7048 | #ifndef SQLITE_OMIT_INCRBLOB |
| 7049 | /* |
| 7050 | ** Argument pCsr must be a cursor opened for writing on an |
| 7051 | ** INTKEY table currently pointing at a valid table entry. |
| 7052 | ** This function modifies the data stored as part of that entry. |
| 7053 | ** Only the data content may only be modified, it is not possible |
| 7054 | ** to change the length of the data stored. |
| 7055 | */ |
danielk1977 | dcbb5d3 | 2007-05-04 18:36:44 +0000 | [diff] [blame] | 7056 | int sqlite3BtreePutData(BtCursor *pCsr, u32 offset, u32 amt, void *z){ |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 7057 | assert( cursorHoldsMutex(pCsr) ); |
drh | e5fe690 | 2007-12-07 18:55:28 +0000 | [diff] [blame] | 7058 | assert( sqlite3_mutex_held(pCsr->pBtree->db->mutex) ); |
danielk1977 | dcbb5d3 | 2007-05-04 18:36:44 +0000 | [diff] [blame] | 7059 | assert(pCsr->isIncrblobHandle); |
drh | fb98264 | 2007-08-30 01:19:59 +0000 | [diff] [blame] | 7060 | if( pCsr->eState>=CURSOR_REQUIRESEEK ){ |
| 7061 | if( pCsr->eState==CURSOR_FAULT ){ |
| 7062 | return pCsr->skip; |
| 7063 | }else{ |
| 7064 | return SQLITE_ABORT; |
| 7065 | } |
danielk1977 | dcbb5d3 | 2007-05-04 18:36:44 +0000 | [diff] [blame] | 7066 | } |
| 7067 | |
danielk1977 | d0441796 | 2007-05-02 13:16:30 +0000 | [diff] [blame] | 7068 | /* Check some preconditions: |
danielk1977 | dcbb5d3 | 2007-05-04 18:36:44 +0000 | [diff] [blame] | 7069 | ** (a) the cursor is open for writing, |
| 7070 | ** (b) there is no read-lock on the table being modified and |
| 7071 | ** (c) the cursor points at a valid row of an intKey table. |
danielk1977 | d0441796 | 2007-05-02 13:16:30 +0000 | [diff] [blame] | 7072 | */ |
danielk1977 | d0441796 | 2007-05-02 13:16:30 +0000 | [diff] [blame] | 7073 | if( !pCsr->wrFlag ){ |
danielk1977 | dcbb5d3 | 2007-05-04 18:36:44 +0000 | [diff] [blame] | 7074 | return SQLITE_READONLY; |
danielk1977 | d0441796 | 2007-05-02 13:16:30 +0000 | [diff] [blame] | 7075 | } |
drh | d0679ed | 2007-08-28 22:24:34 +0000 | [diff] [blame] | 7076 | assert( !pCsr->pBt->readOnly |
| 7077 | && pCsr->pBt->inTransaction==TRANS_WRITE ); |
danielk1977 | d0441796 | 2007-05-02 13:16:30 +0000 | [diff] [blame] | 7078 | if( checkReadLocks(pCsr->pBtree, pCsr->pgnoRoot, pCsr) ){ |
| 7079 | return SQLITE_LOCKED; /* The table pCur points to has a read lock */ |
| 7080 | } |
| 7081 | if( pCsr->eState==CURSOR_INVALID || !pCsr->pPage->intKey ){ |
| 7082 | return SQLITE_ERROR; |
danielk1977 | b4e9af9 | 2007-05-01 17:49:49 +0000 | [diff] [blame] | 7083 | } |
| 7084 | |
danielk1977 | 9f8d640 | 2007-05-02 17:48:45 +0000 | [diff] [blame] | 7085 | return accessPayload(pCsr, offset, amt, (unsigned char *)z, 0, 1); |
danielk1977 | b4e9af9 | 2007-05-01 17:49:49 +0000 | [diff] [blame] | 7086 | } |
danielk1977 | 2dec970 | 2007-05-02 16:48:37 +0000 | [diff] [blame] | 7087 | |
| 7088 | /* |
| 7089 | ** 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] | 7090 | ** overflow list for the current row. This is used by cursors opened |
| 7091 | ** for incremental blob IO only. |
| 7092 | ** |
| 7093 | ** This function sets a flag only. The actual page location cache |
| 7094 | ** (stored in BtCursor.aOverflow[]) is allocated and used by function |
| 7095 | ** accessPayload() (the worker function for sqlite3BtreeData() and |
| 7096 | ** sqlite3BtreePutData()). |
danielk1977 | 2dec970 | 2007-05-02 16:48:37 +0000 | [diff] [blame] | 7097 | */ |
| 7098 | void sqlite3BtreeCacheOverflow(BtCursor *pCur){ |
drh | 1fee73e | 2007-08-29 04:00:57 +0000 | [diff] [blame] | 7099 | assert( cursorHoldsMutex(pCur) ); |
drh | e5fe690 | 2007-12-07 18:55:28 +0000 | [diff] [blame] | 7100 | assert( sqlite3_mutex_held(pCur->pBtree->db->mutex) ); |
danielk1977 | dcbb5d3 | 2007-05-04 18:36:44 +0000 | [diff] [blame] | 7101 | assert(!pCur->isIncrblobHandle); |
danielk1977 | 2dec970 | 2007-05-02 16:48:37 +0000 | [diff] [blame] | 7102 | assert(!pCur->aOverflow); |
danielk1977 | dcbb5d3 | 2007-05-04 18:36:44 +0000 | [diff] [blame] | 7103 | pCur->isIncrblobHandle = 1; |
danielk1977 | 2dec970 | 2007-05-02 16:48:37 +0000 | [diff] [blame] | 7104 | } |
danielk1977 | b4e9af9 | 2007-05-01 17:49:49 +0000 | [diff] [blame] | 7105 | #endif |